pcie-artpec6.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for Axis ARTPEC-6 SoC
  4. *
  5. * Author: Niklas Cassel <[email protected]>
  6. *
  7. * Based on work done by Phil Edworthy <[email protected]>
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/of_device.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 pci_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 pci_addr - pp->cfg0_base;
  88. case DW_PCIE_EP_TYPE:
  89. return pci_addr - ep->phys_base;
  90. default:
  91. dev_err(pci->dev, "UNKNOWN device type\n");
  92. }
  93. return pci_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. .host_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. enum pci_epc_irq_type type, u16 interrupt_num)
  302. {
  303. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  304. switch (type) {
  305. case PCI_EPC_IRQ_LEGACY:
  306. dev_err(pci->dev, "EP cannot trigger legacy IRQs\n");
  307. return -EINVAL;
  308. case PCI_EPC_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 dw_pcie_ep_ops pcie_ep_ops = {
  316. .ep_init = artpec6_pcie_ep_init,
  317. .raise_irq = artpec6_pcie_raise_irq,
  318. };
  319. static int artpec6_pcie_probe(struct platform_device *pdev)
  320. {
  321. struct device *dev = &pdev->dev;
  322. struct dw_pcie *pci;
  323. struct artpec6_pcie *artpec6_pcie;
  324. int ret;
  325. const struct artpec_pcie_of_data *data;
  326. enum artpec_pcie_variants variant;
  327. enum dw_pcie_device_mode mode;
  328. u32 val;
  329. data = of_device_get_match_data(dev);
  330. if (!data)
  331. return -EINVAL;
  332. variant = (enum artpec_pcie_variants)data->variant;
  333. mode = (enum dw_pcie_device_mode)data->mode;
  334. artpec6_pcie = devm_kzalloc(dev, sizeof(*artpec6_pcie), GFP_KERNEL);
  335. if (!artpec6_pcie)
  336. return -ENOMEM;
  337. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  338. if (!pci)
  339. return -ENOMEM;
  340. pci->dev = dev;
  341. pci->ops = &dw_pcie_ops;
  342. artpec6_pcie->pci = pci;
  343. artpec6_pcie->variant = variant;
  344. artpec6_pcie->mode = mode;
  345. artpec6_pcie->phy_base =
  346. devm_platform_ioremap_resource_byname(pdev, "phy");
  347. if (IS_ERR(artpec6_pcie->phy_base))
  348. return PTR_ERR(artpec6_pcie->phy_base);
  349. artpec6_pcie->regmap =
  350. syscon_regmap_lookup_by_phandle(dev->of_node,
  351. "axis,syscon-pcie");
  352. if (IS_ERR(artpec6_pcie->regmap))
  353. return PTR_ERR(artpec6_pcie->regmap);
  354. platform_set_drvdata(pdev, artpec6_pcie);
  355. switch (artpec6_pcie->mode) {
  356. case DW_PCIE_RC_TYPE:
  357. if (!IS_ENABLED(CONFIG_PCIE_ARTPEC6_HOST))
  358. return -ENODEV;
  359. pci->pp.ops = &artpec6_pcie_host_ops;
  360. ret = dw_pcie_host_init(&pci->pp);
  361. if (ret < 0)
  362. return ret;
  363. break;
  364. case DW_PCIE_EP_TYPE:
  365. if (!IS_ENABLED(CONFIG_PCIE_ARTPEC6_EP))
  366. return -ENODEV;
  367. val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
  368. val &= ~PCIECFG_DEVICE_TYPE_MASK;
  369. artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
  370. pci->ep.ops = &pcie_ep_ops;
  371. return dw_pcie_ep_init(&pci->ep);
  372. default:
  373. dev_err(dev, "INVALID device type %d\n", artpec6_pcie->mode);
  374. }
  375. return 0;
  376. }
  377. static const struct artpec_pcie_of_data artpec6_pcie_rc_of_data = {
  378. .variant = ARTPEC6,
  379. .mode = DW_PCIE_RC_TYPE,
  380. };
  381. static const struct artpec_pcie_of_data artpec6_pcie_ep_of_data = {
  382. .variant = ARTPEC6,
  383. .mode = DW_PCIE_EP_TYPE,
  384. };
  385. static const struct artpec_pcie_of_data artpec7_pcie_rc_of_data = {
  386. .variant = ARTPEC7,
  387. .mode = DW_PCIE_RC_TYPE,
  388. };
  389. static const struct artpec_pcie_of_data artpec7_pcie_ep_of_data = {
  390. .variant = ARTPEC7,
  391. .mode = DW_PCIE_EP_TYPE,
  392. };
  393. static const struct of_device_id artpec6_pcie_of_match[] = {
  394. {
  395. .compatible = "axis,artpec6-pcie",
  396. .data = &artpec6_pcie_rc_of_data,
  397. },
  398. {
  399. .compatible = "axis,artpec6-pcie-ep",
  400. .data = &artpec6_pcie_ep_of_data,
  401. },
  402. {
  403. .compatible = "axis,artpec7-pcie",
  404. .data = &artpec7_pcie_rc_of_data,
  405. },
  406. {
  407. .compatible = "axis,artpec7-pcie-ep",
  408. .data = &artpec7_pcie_ep_of_data,
  409. },
  410. {},
  411. };
  412. static struct platform_driver artpec6_pcie_driver = {
  413. .probe = artpec6_pcie_probe,
  414. .driver = {
  415. .name = "artpec6-pcie",
  416. .of_match_table = artpec6_pcie_of_match,
  417. .suppress_bind_attrs = true,
  418. },
  419. };
  420. builtin_platform_driver(artpec6_pcie_driver);