pcie-uniphier.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for UniPhier SoCs
  4. * Copyright 2018 Socionext Inc.
  5. * Author: Kunihiko Hayashi <[email protected]>
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/irqchip/chained_irq.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/pci.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/reset.h>
  21. #include "pcie-designware.h"
  22. #define PCL_PINCTRL0 0x002c
  23. #define PCL_PERST_PLDN_REGEN BIT(12)
  24. #define PCL_PERST_NOE_REGEN BIT(11)
  25. #define PCL_PERST_OUT_REGEN BIT(8)
  26. #define PCL_PERST_PLDN_REGVAL BIT(4)
  27. #define PCL_PERST_NOE_REGVAL BIT(3)
  28. #define PCL_PERST_OUT_REGVAL BIT(0)
  29. #define PCL_PIPEMON 0x0044
  30. #define PCL_PCLK_ALIVE BIT(15)
  31. #define PCL_MODE 0x8000
  32. #define PCL_MODE_REGEN BIT(8)
  33. #define PCL_MODE_REGVAL BIT(0)
  34. #define PCL_APP_READY_CTRL 0x8008
  35. #define PCL_APP_LTSSM_ENABLE BIT(0)
  36. #define PCL_APP_PM0 0x8078
  37. #define PCL_SYS_AUX_PWR_DET BIT(8)
  38. #define PCL_RCV_INT 0x8108
  39. #define PCL_RCV_INT_ALL_ENABLE GENMASK(20, 17)
  40. #define PCL_CFG_BW_MGT_STATUS BIT(4)
  41. #define PCL_CFG_LINK_AUTO_BW_STATUS BIT(3)
  42. #define PCL_CFG_AER_RC_ERR_MSI_STATUS BIT(2)
  43. #define PCL_CFG_PME_MSI_STATUS BIT(1)
  44. #define PCL_RCV_INTX 0x810c
  45. #define PCL_RCV_INTX_ALL_ENABLE GENMASK(19, 16)
  46. #define PCL_RCV_INTX_ALL_MASK GENMASK(11, 8)
  47. #define PCL_RCV_INTX_MASK_SHIFT 8
  48. #define PCL_RCV_INTX_ALL_STATUS GENMASK(3, 0)
  49. #define PCL_RCV_INTX_STATUS_SHIFT 0
  50. #define PCL_STATUS_LINK 0x8140
  51. #define PCL_RDLH_LINK_UP BIT(1)
  52. #define PCL_XMLH_LINK_UP BIT(0)
  53. struct uniphier_pcie {
  54. struct dw_pcie pci;
  55. void __iomem *base;
  56. struct clk *clk;
  57. struct reset_control *rst;
  58. struct phy *phy;
  59. struct irq_domain *legacy_irq_domain;
  60. };
  61. #define to_uniphier_pcie(x) dev_get_drvdata((x)->dev)
  62. static void uniphier_pcie_ltssm_enable(struct uniphier_pcie *pcie,
  63. bool enable)
  64. {
  65. u32 val;
  66. val = readl(pcie->base + PCL_APP_READY_CTRL);
  67. if (enable)
  68. val |= PCL_APP_LTSSM_ENABLE;
  69. else
  70. val &= ~PCL_APP_LTSSM_ENABLE;
  71. writel(val, pcie->base + PCL_APP_READY_CTRL);
  72. }
  73. static void uniphier_pcie_init_rc(struct uniphier_pcie *pcie)
  74. {
  75. u32 val;
  76. /* set RC MODE */
  77. val = readl(pcie->base + PCL_MODE);
  78. val |= PCL_MODE_REGEN;
  79. val &= ~PCL_MODE_REGVAL;
  80. writel(val, pcie->base + PCL_MODE);
  81. /* use auxiliary power detection */
  82. val = readl(pcie->base + PCL_APP_PM0);
  83. val |= PCL_SYS_AUX_PWR_DET;
  84. writel(val, pcie->base + PCL_APP_PM0);
  85. /* assert PERST# */
  86. val = readl(pcie->base + PCL_PINCTRL0);
  87. val &= ~(PCL_PERST_NOE_REGVAL | PCL_PERST_OUT_REGVAL
  88. | PCL_PERST_PLDN_REGVAL);
  89. val |= PCL_PERST_NOE_REGEN | PCL_PERST_OUT_REGEN
  90. | PCL_PERST_PLDN_REGEN;
  91. writel(val, pcie->base + PCL_PINCTRL0);
  92. uniphier_pcie_ltssm_enable(pcie, false);
  93. usleep_range(100000, 200000);
  94. /* deassert PERST# */
  95. val = readl(pcie->base + PCL_PINCTRL0);
  96. val |= PCL_PERST_OUT_REGVAL | PCL_PERST_OUT_REGEN;
  97. writel(val, pcie->base + PCL_PINCTRL0);
  98. }
  99. static int uniphier_pcie_wait_rc(struct uniphier_pcie *pcie)
  100. {
  101. u32 status;
  102. int ret;
  103. /* wait PIPE clock */
  104. ret = readl_poll_timeout(pcie->base + PCL_PIPEMON, status,
  105. status & PCL_PCLK_ALIVE, 100000, 1000000);
  106. if (ret) {
  107. dev_err(pcie->pci.dev,
  108. "Failed to initialize controller in RC mode\n");
  109. return ret;
  110. }
  111. return 0;
  112. }
  113. static int uniphier_pcie_link_up(struct dw_pcie *pci)
  114. {
  115. struct uniphier_pcie *pcie = to_uniphier_pcie(pci);
  116. u32 val, mask;
  117. val = readl(pcie->base + PCL_STATUS_LINK);
  118. mask = PCL_RDLH_LINK_UP | PCL_XMLH_LINK_UP;
  119. return (val & mask) == mask;
  120. }
  121. static int uniphier_pcie_start_link(struct dw_pcie *pci)
  122. {
  123. struct uniphier_pcie *pcie = to_uniphier_pcie(pci);
  124. uniphier_pcie_ltssm_enable(pcie, true);
  125. return 0;
  126. }
  127. static void uniphier_pcie_stop_link(struct dw_pcie *pci)
  128. {
  129. struct uniphier_pcie *pcie = to_uniphier_pcie(pci);
  130. uniphier_pcie_ltssm_enable(pcie, false);
  131. }
  132. static void uniphier_pcie_irq_enable(struct uniphier_pcie *pcie)
  133. {
  134. writel(PCL_RCV_INT_ALL_ENABLE, pcie->base + PCL_RCV_INT);
  135. writel(PCL_RCV_INTX_ALL_ENABLE, pcie->base + PCL_RCV_INTX);
  136. }
  137. static void uniphier_pcie_irq_mask(struct irq_data *d)
  138. {
  139. struct dw_pcie_rp *pp = irq_data_get_irq_chip_data(d);
  140. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  141. struct uniphier_pcie *pcie = to_uniphier_pcie(pci);
  142. unsigned long flags;
  143. u32 val;
  144. raw_spin_lock_irqsave(&pp->lock, flags);
  145. val = readl(pcie->base + PCL_RCV_INTX);
  146. val |= BIT(irqd_to_hwirq(d) + PCL_RCV_INTX_MASK_SHIFT);
  147. writel(val, pcie->base + PCL_RCV_INTX);
  148. raw_spin_unlock_irqrestore(&pp->lock, flags);
  149. }
  150. static void uniphier_pcie_irq_unmask(struct irq_data *d)
  151. {
  152. struct dw_pcie_rp *pp = irq_data_get_irq_chip_data(d);
  153. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  154. struct uniphier_pcie *pcie = to_uniphier_pcie(pci);
  155. unsigned long flags;
  156. u32 val;
  157. raw_spin_lock_irqsave(&pp->lock, flags);
  158. val = readl(pcie->base + PCL_RCV_INTX);
  159. val &= ~BIT(irqd_to_hwirq(d) + PCL_RCV_INTX_MASK_SHIFT);
  160. writel(val, pcie->base + PCL_RCV_INTX);
  161. raw_spin_unlock_irqrestore(&pp->lock, flags);
  162. }
  163. static struct irq_chip uniphier_pcie_irq_chip = {
  164. .name = "PCI",
  165. .irq_mask = uniphier_pcie_irq_mask,
  166. .irq_unmask = uniphier_pcie_irq_unmask,
  167. };
  168. static int uniphier_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  169. irq_hw_number_t hwirq)
  170. {
  171. irq_set_chip_and_handler(irq, &uniphier_pcie_irq_chip,
  172. handle_level_irq);
  173. irq_set_chip_data(irq, domain->host_data);
  174. return 0;
  175. }
  176. static const struct irq_domain_ops uniphier_intx_domain_ops = {
  177. .map = uniphier_pcie_intx_map,
  178. };
  179. static void uniphier_pcie_irq_handler(struct irq_desc *desc)
  180. {
  181. struct dw_pcie_rp *pp = irq_desc_get_handler_data(desc);
  182. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  183. struct uniphier_pcie *pcie = to_uniphier_pcie(pci);
  184. struct irq_chip *chip = irq_desc_get_chip(desc);
  185. unsigned long reg;
  186. u32 val, bit;
  187. /* INT for debug */
  188. val = readl(pcie->base + PCL_RCV_INT);
  189. if (val & PCL_CFG_BW_MGT_STATUS)
  190. dev_dbg(pci->dev, "Link Bandwidth Management Event\n");
  191. if (val & PCL_CFG_LINK_AUTO_BW_STATUS)
  192. dev_dbg(pci->dev, "Link Autonomous Bandwidth Event\n");
  193. if (val & PCL_CFG_AER_RC_ERR_MSI_STATUS)
  194. dev_dbg(pci->dev, "Root Error\n");
  195. if (val & PCL_CFG_PME_MSI_STATUS)
  196. dev_dbg(pci->dev, "PME Interrupt\n");
  197. writel(val, pcie->base + PCL_RCV_INT);
  198. /* INTx */
  199. chained_irq_enter(chip, desc);
  200. val = readl(pcie->base + PCL_RCV_INTX);
  201. reg = FIELD_GET(PCL_RCV_INTX_ALL_STATUS, val);
  202. for_each_set_bit(bit, &reg, PCI_NUM_INTX)
  203. generic_handle_domain_irq(pcie->legacy_irq_domain, bit);
  204. chained_irq_exit(chip, desc);
  205. }
  206. static int uniphier_pcie_config_legacy_irq(struct dw_pcie_rp *pp)
  207. {
  208. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  209. struct uniphier_pcie *pcie = to_uniphier_pcie(pci);
  210. struct device_node *np = pci->dev->of_node;
  211. struct device_node *np_intc;
  212. int ret = 0;
  213. np_intc = of_get_child_by_name(np, "legacy-interrupt-controller");
  214. if (!np_intc) {
  215. dev_err(pci->dev, "Failed to get legacy-interrupt-controller node\n");
  216. return -EINVAL;
  217. }
  218. pp->irq = irq_of_parse_and_map(np_intc, 0);
  219. if (!pp->irq) {
  220. dev_err(pci->dev, "Failed to get an IRQ entry in legacy-interrupt-controller\n");
  221. ret = -EINVAL;
  222. goto out_put_node;
  223. }
  224. pcie->legacy_irq_domain = irq_domain_add_linear(np_intc, PCI_NUM_INTX,
  225. &uniphier_intx_domain_ops, pp);
  226. if (!pcie->legacy_irq_domain) {
  227. dev_err(pci->dev, "Failed to get INTx domain\n");
  228. ret = -ENODEV;
  229. goto out_put_node;
  230. }
  231. irq_set_chained_handler_and_data(pp->irq, uniphier_pcie_irq_handler,
  232. pp);
  233. out_put_node:
  234. of_node_put(np_intc);
  235. return ret;
  236. }
  237. static int uniphier_pcie_host_init(struct dw_pcie_rp *pp)
  238. {
  239. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  240. struct uniphier_pcie *pcie = to_uniphier_pcie(pci);
  241. int ret;
  242. ret = uniphier_pcie_config_legacy_irq(pp);
  243. if (ret)
  244. return ret;
  245. uniphier_pcie_irq_enable(pcie);
  246. return 0;
  247. }
  248. static const struct dw_pcie_host_ops uniphier_pcie_host_ops = {
  249. .host_init = uniphier_pcie_host_init,
  250. };
  251. static int uniphier_pcie_host_enable(struct uniphier_pcie *pcie)
  252. {
  253. int ret;
  254. ret = clk_prepare_enable(pcie->clk);
  255. if (ret)
  256. return ret;
  257. ret = reset_control_deassert(pcie->rst);
  258. if (ret)
  259. goto out_clk_disable;
  260. uniphier_pcie_init_rc(pcie);
  261. ret = phy_init(pcie->phy);
  262. if (ret)
  263. goto out_rst_assert;
  264. ret = uniphier_pcie_wait_rc(pcie);
  265. if (ret)
  266. goto out_phy_exit;
  267. return 0;
  268. out_phy_exit:
  269. phy_exit(pcie->phy);
  270. out_rst_assert:
  271. reset_control_assert(pcie->rst);
  272. out_clk_disable:
  273. clk_disable_unprepare(pcie->clk);
  274. return ret;
  275. }
  276. static const struct dw_pcie_ops dw_pcie_ops = {
  277. .start_link = uniphier_pcie_start_link,
  278. .stop_link = uniphier_pcie_stop_link,
  279. .link_up = uniphier_pcie_link_up,
  280. };
  281. static int uniphier_pcie_probe(struct platform_device *pdev)
  282. {
  283. struct device *dev = &pdev->dev;
  284. struct uniphier_pcie *pcie;
  285. int ret;
  286. pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
  287. if (!pcie)
  288. return -ENOMEM;
  289. pcie->pci.dev = dev;
  290. pcie->pci.ops = &dw_pcie_ops;
  291. pcie->base = devm_platform_ioremap_resource_byname(pdev, "link");
  292. if (IS_ERR(pcie->base))
  293. return PTR_ERR(pcie->base);
  294. pcie->clk = devm_clk_get(dev, NULL);
  295. if (IS_ERR(pcie->clk))
  296. return PTR_ERR(pcie->clk);
  297. pcie->rst = devm_reset_control_get_shared(dev, NULL);
  298. if (IS_ERR(pcie->rst))
  299. return PTR_ERR(pcie->rst);
  300. pcie->phy = devm_phy_optional_get(dev, "pcie-phy");
  301. if (IS_ERR(pcie->phy))
  302. return PTR_ERR(pcie->phy);
  303. platform_set_drvdata(pdev, pcie);
  304. ret = uniphier_pcie_host_enable(pcie);
  305. if (ret)
  306. return ret;
  307. pcie->pci.pp.ops = &uniphier_pcie_host_ops;
  308. return dw_pcie_host_init(&pcie->pci.pp);
  309. }
  310. static const struct of_device_id uniphier_pcie_match[] = {
  311. { .compatible = "socionext,uniphier-pcie", },
  312. { /* sentinel */ },
  313. };
  314. static struct platform_driver uniphier_pcie_driver = {
  315. .probe = uniphier_pcie_probe,
  316. .driver = {
  317. .name = "uniphier-pcie",
  318. .of_match_table = uniphier_pcie_match,
  319. },
  320. };
  321. builtin_platform_driver(uniphier_pcie_driver);