pcie-artpec6.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for Axis ARTPEC-6 SoC
  4. *
  5. * Author: Niklas Cassel <niklas.cassel@axis.com>
  6. *
  7. * Based on work done by Phil Edworthy <phil@edworthys.org>
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/of.h>
  13. #include <linux/pci.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/resource.h>
  16. #include <linux/signal.h>
  17. #include <linux/types.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/regmap.h>
  21. #include "pcie-designware.h"
  22. #define to_artpec6_pcie(x) dev_get_drvdata((x)->dev)
  23. enum artpec_pcie_variants {
  24. ARTPEC6,
  25. ARTPEC7,
  26. };
  27. struct artpec6_pcie {
  28. struct dw_pcie *pci;
  29. struct regmap *regmap; /* DT axis,syscon-pcie */
  30. void __iomem *phy_base; /* DT phy */
  31. enum artpec_pcie_variants variant;
  32. enum dw_pcie_device_mode mode;
  33. };
  34. struct artpec_pcie_of_data {
  35. enum artpec_pcie_variants variant;
  36. enum dw_pcie_device_mode mode;
  37. };
  38. static const struct of_device_id artpec6_pcie_of_match[];
  39. /* ARTPEC-6 specific registers */
  40. #define PCIECFG 0x18
  41. #define PCIECFG_DBG_OEN BIT(24)
  42. #define PCIECFG_CORE_RESET_REQ BIT(21)
  43. #define PCIECFG_LTSSM_ENABLE BIT(20)
  44. #define PCIECFG_DEVICE_TYPE_MASK GENMASK(19, 16)
  45. #define PCIECFG_CLKREQ_B BIT(11)
  46. #define PCIECFG_REFCLK_ENABLE BIT(10)
  47. #define PCIECFG_PLL_ENABLE BIT(9)
  48. #define PCIECFG_PCLK_ENABLE BIT(8)
  49. #define PCIECFG_RISRCREN BIT(4)
  50. #define PCIECFG_MODE_TX_DRV_EN BIT(3)
  51. #define PCIECFG_CISRREN BIT(2)
  52. #define PCIECFG_MACRO_ENABLE BIT(0)
  53. /* ARTPEC-7 specific fields */
  54. #define PCIECFG_REFCLKSEL BIT(23)
  55. #define PCIECFG_NOC_RESET BIT(3)
  56. #define PCIESTAT 0x1c
  57. /* ARTPEC-7 specific fields */
  58. #define PCIESTAT_EXTREFCLK BIT(3)
  59. #define NOCCFG 0x40
  60. #define NOCCFG_ENABLE_CLK_PCIE BIT(4)
  61. #define NOCCFG_POWER_PCIE_IDLEACK BIT(3)
  62. #define NOCCFG_POWER_PCIE_IDLE BIT(2)
  63. #define NOCCFG_POWER_PCIE_IDLEREQ BIT(1)
  64. #define PHY_STATUS 0x118
  65. #define PHY_COSPLLLOCK BIT(0)
  66. #define PHY_TX_ASIC_OUT 0x4040
  67. #define PHY_TX_ASIC_OUT_TX_ACK BIT(0)
  68. #define PHY_RX_ASIC_OUT 0x405c
  69. #define PHY_RX_ASIC_OUT_ACK BIT(0)
  70. static u32 artpec6_pcie_readl(struct artpec6_pcie *artpec6_pcie, u32 offset)
  71. {
  72. u32 val;
  73. regmap_read(artpec6_pcie->regmap, offset, &val);
  74. return val;
  75. }
  76. static void artpec6_pcie_writel(struct artpec6_pcie *artpec6_pcie, u32 offset, u32 val)
  77. {
  78. regmap_write(artpec6_pcie->regmap, offset, val);
  79. }
  80. static u64 artpec6_pcie_cpu_addr_fixup(struct dw_pcie *pci, u64 cpu_addr)
  81. {
  82. struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
  83. struct dw_pcie_rp *pp = &pci->pp;
  84. struct dw_pcie_ep *ep = &pci->ep;
  85. switch (artpec6_pcie->mode) {
  86. case DW_PCIE_RC_TYPE:
  87. return cpu_addr - pp->cfg0_base;
  88. case DW_PCIE_EP_TYPE:
  89. return cpu_addr - ep->phys_base;
  90. default:
  91. dev_err(pci->dev, "UNKNOWN device type\n");
  92. }
  93. return cpu_addr;
  94. }
  95. static int artpec6_pcie_establish_link(struct dw_pcie *pci)
  96. {
  97. struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
  98. u32 val;
  99. val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
  100. val |= PCIECFG_LTSSM_ENABLE;
  101. artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
  102. return 0;
  103. }
  104. static void artpec6_pcie_stop_link(struct dw_pcie *pci)
  105. {
  106. struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
  107. u32 val;
  108. val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
  109. val &= ~PCIECFG_LTSSM_ENABLE;
  110. artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
  111. }
  112. static const struct dw_pcie_ops dw_pcie_ops = {
  113. .cpu_addr_fixup = artpec6_pcie_cpu_addr_fixup,
  114. .start_link = artpec6_pcie_establish_link,
  115. .stop_link = artpec6_pcie_stop_link,
  116. };
  117. static void artpec6_pcie_wait_for_phy_a6(struct artpec6_pcie *artpec6_pcie)
  118. {
  119. struct dw_pcie *pci = artpec6_pcie->pci;
  120. struct device *dev = pci->dev;
  121. u32 val;
  122. unsigned int retries;
  123. retries = 50;
  124. do {
  125. usleep_range(1000, 2000);
  126. val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
  127. retries--;
  128. } while (retries &&
  129. (val & (NOCCFG_POWER_PCIE_IDLEACK | NOCCFG_POWER_PCIE_IDLE)));
  130. if (!retries)
  131. dev_err(dev, "PCIe clock manager did not leave idle state\n");
  132. retries = 50;
  133. do {
  134. usleep_range(1000, 2000);
  135. val = readl(artpec6_pcie->phy_base + PHY_STATUS);
  136. retries--;
  137. } while (retries && !(val & PHY_COSPLLLOCK));
  138. if (!retries)
  139. dev_err(dev, "PHY PLL did not lock\n");
  140. }
  141. static void artpec6_pcie_wait_for_phy_a7(struct artpec6_pcie *artpec6_pcie)
  142. {
  143. struct dw_pcie *pci = artpec6_pcie->pci;
  144. struct device *dev = pci->dev;
  145. u32 val;
  146. u16 phy_status_tx, phy_status_rx;
  147. unsigned int retries;
  148. retries = 50;
  149. do {
  150. usleep_range(1000, 2000);
  151. val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
  152. retries--;
  153. } while (retries &&
  154. (val & (NOCCFG_POWER_PCIE_IDLEACK | NOCCFG_POWER_PCIE_IDLE)));
  155. if (!retries)
  156. dev_err(dev, "PCIe clock manager did not leave idle state\n");
  157. retries = 50;
  158. do {
  159. usleep_range(1000, 2000);
  160. phy_status_tx = readw(artpec6_pcie->phy_base + PHY_TX_ASIC_OUT);
  161. phy_status_rx = readw(artpec6_pcie->phy_base + PHY_RX_ASIC_OUT);
  162. retries--;
  163. } while (retries && ((phy_status_tx & PHY_TX_ASIC_OUT_TX_ACK) ||
  164. (phy_status_rx & PHY_RX_ASIC_OUT_ACK)));
  165. if (!retries)
  166. dev_err(dev, "PHY did not enter Pn state\n");
  167. }
  168. static void artpec6_pcie_wait_for_phy(struct artpec6_pcie *artpec6_pcie)
  169. {
  170. switch (artpec6_pcie->variant) {
  171. case ARTPEC6:
  172. artpec6_pcie_wait_for_phy_a6(artpec6_pcie);
  173. break;
  174. case ARTPEC7:
  175. artpec6_pcie_wait_for_phy_a7(artpec6_pcie);
  176. break;
  177. }
  178. }
  179. static void artpec6_pcie_init_phy_a6(struct artpec6_pcie *artpec6_pcie)
  180. {
  181. u32 val;
  182. val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
  183. val |= PCIECFG_RISRCREN | /* Receiver term. 50 Ohm */
  184. PCIECFG_MODE_TX_DRV_EN |
  185. PCIECFG_CISRREN | /* Reference clock term. 100 Ohm */
  186. PCIECFG_MACRO_ENABLE;
  187. val |= PCIECFG_REFCLK_ENABLE;
  188. val &= ~PCIECFG_DBG_OEN;
  189. val &= ~PCIECFG_CLKREQ_B;
  190. artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
  191. usleep_range(5000, 6000);
  192. val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
  193. val |= NOCCFG_ENABLE_CLK_PCIE;
  194. artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
  195. usleep_range(20, 30);
  196. val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
  197. val |= PCIECFG_PCLK_ENABLE | PCIECFG_PLL_ENABLE;
  198. artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
  199. usleep_range(6000, 7000);
  200. val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
  201. val &= ~NOCCFG_POWER_PCIE_IDLEREQ;
  202. artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
  203. }
  204. static void artpec6_pcie_init_phy_a7(struct artpec6_pcie *artpec6_pcie)
  205. {
  206. struct dw_pcie *pci = artpec6_pcie->pci;
  207. u32 val;
  208. bool extrefclk;
  209. /* Check if external reference clock is connected */
  210. val = artpec6_pcie_readl(artpec6_pcie, PCIESTAT);
  211. extrefclk = !!(val & PCIESTAT_EXTREFCLK);
  212. dev_dbg(pci->dev, "Using reference clock: %s\n",
  213. extrefclk ? "external" : "internal");
  214. val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
  215. val |= PCIECFG_RISRCREN | /* Receiver term. 50 Ohm */
  216. PCIECFG_PCLK_ENABLE;
  217. if (extrefclk)
  218. val |= PCIECFG_REFCLKSEL;
  219. else
  220. val &= ~PCIECFG_REFCLKSEL;
  221. artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
  222. usleep_range(10, 20);
  223. val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
  224. val |= NOCCFG_ENABLE_CLK_PCIE;
  225. artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
  226. usleep_range(20, 30);
  227. val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
  228. val &= ~NOCCFG_POWER_PCIE_IDLEREQ;
  229. artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
  230. }
  231. static void artpec6_pcie_init_phy(struct artpec6_pcie *artpec6_pcie)
  232. {
  233. switch (artpec6_pcie->variant) {
  234. case ARTPEC6:
  235. artpec6_pcie_init_phy_a6(artpec6_pcie);
  236. break;
  237. case ARTPEC7:
  238. artpec6_pcie_init_phy_a7(artpec6_pcie);
  239. break;
  240. }
  241. }
  242. static void artpec6_pcie_assert_core_reset(struct artpec6_pcie *artpec6_pcie)
  243. {
  244. u32 val;
  245. val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
  246. switch (artpec6_pcie->variant) {
  247. case ARTPEC6:
  248. val |= PCIECFG_CORE_RESET_REQ;
  249. break;
  250. case ARTPEC7:
  251. val &= ~PCIECFG_NOC_RESET;
  252. break;
  253. }
  254. artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
  255. }
  256. static void artpec6_pcie_deassert_core_reset(struct artpec6_pcie *artpec6_pcie)
  257. {
  258. u32 val;
  259. val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
  260. switch (artpec6_pcie->variant) {
  261. case ARTPEC6:
  262. val &= ~PCIECFG_CORE_RESET_REQ;
  263. break;
  264. case ARTPEC7:
  265. val |= PCIECFG_NOC_RESET;
  266. break;
  267. }
  268. artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
  269. usleep_range(100, 200);
  270. }
  271. static int artpec6_pcie_host_init(struct dw_pcie_rp *pp)
  272. {
  273. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  274. struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
  275. if (artpec6_pcie->variant == ARTPEC7) {
  276. pci->n_fts[0] = 180;
  277. pci->n_fts[1] = 180;
  278. }
  279. artpec6_pcie_assert_core_reset(artpec6_pcie);
  280. artpec6_pcie_init_phy(artpec6_pcie);
  281. artpec6_pcie_deassert_core_reset(artpec6_pcie);
  282. artpec6_pcie_wait_for_phy(artpec6_pcie);
  283. return 0;
  284. }
  285. static const struct dw_pcie_host_ops artpec6_pcie_host_ops = {
  286. .init = artpec6_pcie_host_init,
  287. };
  288. static void artpec6_pcie_ep_init(struct dw_pcie_ep *ep)
  289. {
  290. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  291. struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
  292. enum pci_barno bar;
  293. artpec6_pcie_assert_core_reset(artpec6_pcie);
  294. artpec6_pcie_init_phy(artpec6_pcie);
  295. artpec6_pcie_deassert_core_reset(artpec6_pcie);
  296. artpec6_pcie_wait_for_phy(artpec6_pcie);
  297. for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
  298. dw_pcie_ep_reset_bar(pci, bar);
  299. }
  300. static int artpec6_pcie_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
  301. unsigned int type, u16 interrupt_num)
  302. {
  303. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  304. switch (type) {
  305. case PCI_IRQ_INTX:
  306. dev_err(pci->dev, "EP cannot trigger INTx IRQs\n");
  307. return -EINVAL;
  308. case PCI_IRQ_MSI:
  309. return dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num);
  310. default:
  311. dev_err(pci->dev, "UNKNOWN IRQ type\n");
  312. }
  313. return 0;
  314. }
  315. static const struct pci_epc_features artpec6_pcie_epc_features = {
  316. DWC_EPC_COMMON_FEATURES,
  317. .msi_capable = true,
  318. };
  319. static const struct pci_epc_features *
  320. artpec6_pcie_get_features(struct dw_pcie_ep *ep)
  321. {
  322. return &artpec6_pcie_epc_features;
  323. }
  324. static const struct dw_pcie_ep_ops pcie_ep_ops = {
  325. .init = artpec6_pcie_ep_init,
  326. .raise_irq = artpec6_pcie_raise_irq,
  327. .get_features = artpec6_pcie_get_features,
  328. };
  329. static int artpec6_pcie_probe(struct platform_device *pdev)
  330. {
  331. struct device *dev = &pdev->dev;
  332. struct dw_pcie *pci;
  333. struct artpec6_pcie *artpec6_pcie;
  334. int ret;
  335. const struct artpec_pcie_of_data *data;
  336. enum artpec_pcie_variants variant;
  337. enum dw_pcie_device_mode mode;
  338. u32 val;
  339. data = of_device_get_match_data(dev);
  340. if (!data)
  341. return -EINVAL;
  342. variant = (enum artpec_pcie_variants)data->variant;
  343. mode = (enum dw_pcie_device_mode)data->mode;
  344. artpec6_pcie = devm_kzalloc(dev, sizeof(*artpec6_pcie), GFP_KERNEL);
  345. if (!artpec6_pcie)
  346. return -ENOMEM;
  347. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  348. if (!pci)
  349. return -ENOMEM;
  350. pci->dev = dev;
  351. pci->ops = &dw_pcie_ops;
  352. artpec6_pcie->pci = pci;
  353. artpec6_pcie->variant = variant;
  354. artpec6_pcie->mode = mode;
  355. artpec6_pcie->phy_base =
  356. devm_platform_ioremap_resource_byname(pdev, "phy");
  357. if (IS_ERR(artpec6_pcie->phy_base))
  358. return PTR_ERR(artpec6_pcie->phy_base);
  359. artpec6_pcie->regmap =
  360. syscon_regmap_lookup_by_phandle(dev->of_node,
  361. "axis,syscon-pcie");
  362. if (IS_ERR(artpec6_pcie->regmap))
  363. return PTR_ERR(artpec6_pcie->regmap);
  364. platform_set_drvdata(pdev, artpec6_pcie);
  365. switch (artpec6_pcie->mode) {
  366. case DW_PCIE_RC_TYPE:
  367. if (!IS_ENABLED(CONFIG_PCIE_ARTPEC6_HOST))
  368. return -ENODEV;
  369. pci->pp.ops = &artpec6_pcie_host_ops;
  370. ret = dw_pcie_host_init(&pci->pp);
  371. if (ret < 0)
  372. return ret;
  373. break;
  374. case DW_PCIE_EP_TYPE:
  375. if (!IS_ENABLED(CONFIG_PCIE_ARTPEC6_EP))
  376. return -ENODEV;
  377. val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
  378. val &= ~PCIECFG_DEVICE_TYPE_MASK;
  379. artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
  380. pci->ep.ops = &pcie_ep_ops;
  381. ret = dw_pcie_ep_init(&pci->ep);
  382. if (ret)
  383. return ret;
  384. ret = dw_pcie_ep_init_registers(&pci->ep);
  385. if (ret) {
  386. dev_err(dev, "Failed to initialize DWC endpoint registers\n");
  387. dw_pcie_ep_deinit(&pci->ep);
  388. return ret;
  389. }
  390. pci_epc_init_notify(pci->ep.epc);
  391. break;
  392. default:
  393. dev_err(dev, "INVALID device type %d\n", artpec6_pcie->mode);
  394. }
  395. return 0;
  396. }
  397. static const struct artpec_pcie_of_data artpec6_pcie_rc_of_data = {
  398. .variant = ARTPEC6,
  399. .mode = DW_PCIE_RC_TYPE,
  400. };
  401. static const struct artpec_pcie_of_data artpec6_pcie_ep_of_data = {
  402. .variant = ARTPEC6,
  403. .mode = DW_PCIE_EP_TYPE,
  404. };
  405. static const struct artpec_pcie_of_data artpec7_pcie_rc_of_data = {
  406. .variant = ARTPEC7,
  407. .mode = DW_PCIE_RC_TYPE,
  408. };
  409. static const struct artpec_pcie_of_data artpec7_pcie_ep_of_data = {
  410. .variant = ARTPEC7,
  411. .mode = DW_PCIE_EP_TYPE,
  412. };
  413. static const struct of_device_id artpec6_pcie_of_match[] = {
  414. {
  415. .compatible = "axis,artpec6-pcie",
  416. .data = &artpec6_pcie_rc_of_data,
  417. },
  418. {
  419. .compatible = "axis,artpec6-pcie-ep",
  420. .data = &artpec6_pcie_ep_of_data,
  421. },
  422. {
  423. .compatible = "axis,artpec7-pcie",
  424. .data = &artpec7_pcie_rc_of_data,
  425. },
  426. {
  427. .compatible = "axis,artpec7-pcie-ep",
  428. .data = &artpec7_pcie_ep_of_data,
  429. },
  430. {},
  431. };
  432. static struct platform_driver artpec6_pcie_driver = {
  433. .probe = artpec6_pcie_probe,
  434. .driver = {
  435. .name = "artpec6-pcie",
  436. .of_match_table = artpec6_pcie_of_match,
  437. .suppress_bind_attrs = true,
  438. },
  439. };
  440. builtin_platform_driver(artpec6_pcie_driver);