pcie-starfive.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PCIe host controller driver for StarFive JH7110 Soc.
  4. *
  5. * Copyright (C) 2023 StarFive Technology Co., Ltd.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/module.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/of_pci.h>
  18. #include <linux/pci.h>
  19. #include <linux/phy/phy.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/regmap.h>
  23. #include <linux/reset.h>
  24. #include "../../pci.h"
  25. #include "pcie-plda.h"
  26. #define PCIE_FUNC_NUM 4
  27. /* system control */
  28. #define STG_SYSCON_PCIE0_BASE 0x48
  29. #define STG_SYSCON_PCIE1_BASE 0x1f8
  30. #define STG_SYSCON_AR_OFFSET 0x78
  31. #define STG_SYSCON_AXI4_SLVL_AR_MASK GENMASK(22, 8)
  32. #define STG_SYSCON_AXI4_SLVL_PHY_AR(x) FIELD_PREP(GENMASK(20, 17), x)
  33. #define STG_SYSCON_AW_OFFSET 0x7c
  34. #define STG_SYSCON_AXI4_SLVL_AW_MASK GENMASK(14, 0)
  35. #define STG_SYSCON_AXI4_SLVL_PHY_AW(x) FIELD_PREP(GENMASK(12, 9), x)
  36. #define STG_SYSCON_CLKREQ BIT(22)
  37. #define STG_SYSCON_CKREF_SRC_MASK GENMASK(19, 18)
  38. #define STG_SYSCON_RP_NEP_OFFSET 0xe8
  39. #define STG_SYSCON_K_RP_NEP BIT(8)
  40. #define STG_SYSCON_LNKSTA_OFFSET 0x170
  41. #define DATA_LINK_ACTIVE BIT(5)
  42. /* Parameters for the waiting for link up routine */
  43. #define LINK_WAIT_MAX_RETRIES 10
  44. #define LINK_WAIT_USLEEP_MIN 90000
  45. #define LINK_WAIT_USLEEP_MAX 100000
  46. struct starfive_jh7110_pcie {
  47. struct plda_pcie_rp plda;
  48. struct reset_control *resets;
  49. struct clk_bulk_data *clks;
  50. struct regmap *reg_syscon;
  51. struct regulator *vpcie3v3;
  52. struct gpio_desc *reset_gpio;
  53. struct phy *phy;
  54. unsigned int stg_pcie_base;
  55. int num_clks;
  56. };
  57. /*
  58. * JH7110 PCIe port BAR0/1 can be configured as 64-bit prefetchable memory
  59. * space. PCIe read and write requests targeting BAR0/1 are routed to so called
  60. * 'Bridge Configuration space' in PLDA IP datasheet, which contains the bridge
  61. * internal registers, such as interrupt, DMA and ATU registers...
  62. * JH7110 can access the Bridge Configuration space by local bus, and don`t
  63. * want the bridge internal registers accessed by the DMA from EP devices.
  64. * Thus, they are unimplemented and should be hidden here.
  65. */
  66. static bool starfive_pcie_hide_rc_bar(struct pci_bus *bus, unsigned int devfn,
  67. int offset)
  68. {
  69. if (pci_is_root_bus(bus) && !devfn &&
  70. (offset == PCI_BASE_ADDRESS_0 || offset == PCI_BASE_ADDRESS_1))
  71. return true;
  72. return false;
  73. }
  74. static int starfive_pcie_config_write(struct pci_bus *bus, unsigned int devfn,
  75. int where, int size, u32 value)
  76. {
  77. if (starfive_pcie_hide_rc_bar(bus, devfn, where))
  78. return PCIBIOS_SUCCESSFUL;
  79. return pci_generic_config_write(bus, devfn, where, size, value);
  80. }
  81. static int starfive_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
  82. int where, int size, u32 *value)
  83. {
  84. if (starfive_pcie_hide_rc_bar(bus, devfn, where)) {
  85. *value = 0;
  86. return PCIBIOS_SUCCESSFUL;
  87. }
  88. return pci_generic_config_read(bus, devfn, where, size, value);
  89. }
  90. static int starfive_pcie_parse_dt(struct starfive_jh7110_pcie *pcie,
  91. struct device *dev)
  92. {
  93. int domain_nr;
  94. pcie->num_clks = devm_clk_bulk_get_all(dev, &pcie->clks);
  95. if (pcie->num_clks < 0)
  96. return dev_err_probe(dev, pcie->num_clks,
  97. "failed to get pcie clocks\n");
  98. pcie->resets = devm_reset_control_array_get_exclusive(dev);
  99. if (IS_ERR(pcie->resets))
  100. return dev_err_probe(dev, PTR_ERR(pcie->resets),
  101. "failed to get pcie resets");
  102. pcie->reg_syscon =
  103. syscon_regmap_lookup_by_phandle(dev->of_node,
  104. "starfive,stg-syscon");
  105. if (IS_ERR(pcie->reg_syscon))
  106. return dev_err_probe(dev, PTR_ERR(pcie->reg_syscon),
  107. "failed to parse starfive,stg-syscon\n");
  108. pcie->phy = devm_phy_optional_get(dev, NULL);
  109. if (IS_ERR(pcie->phy))
  110. return dev_err_probe(dev, PTR_ERR(pcie->phy),
  111. "failed to get pcie phy\n");
  112. /*
  113. * The PCIe domain numbers are set to be static in JH7110 DTS.
  114. * As the STG system controller defines different bases in PCIe RP0 &
  115. * RP1, we use them to identify which controller is doing the hardware
  116. * initialization.
  117. */
  118. domain_nr = of_get_pci_domain_nr(dev->of_node);
  119. if (domain_nr < 0 || domain_nr > 1)
  120. return dev_err_probe(dev, -ENODEV,
  121. "failed to get valid pcie domain\n");
  122. if (domain_nr == 0)
  123. pcie->stg_pcie_base = STG_SYSCON_PCIE0_BASE;
  124. else
  125. pcie->stg_pcie_base = STG_SYSCON_PCIE1_BASE;
  126. pcie->reset_gpio = devm_gpiod_get_optional(dev, "perst",
  127. GPIOD_OUT_HIGH);
  128. if (IS_ERR(pcie->reset_gpio))
  129. return dev_err_probe(dev, PTR_ERR(pcie->reset_gpio),
  130. "failed to get perst-gpio\n");
  131. pcie->vpcie3v3 = devm_regulator_get_optional(dev, "vpcie3v3");
  132. if (IS_ERR(pcie->vpcie3v3)) {
  133. if (PTR_ERR(pcie->vpcie3v3) != -ENODEV)
  134. return dev_err_probe(dev, PTR_ERR(pcie->vpcie3v3),
  135. "failed to get vpcie3v3 regulator\n");
  136. pcie->vpcie3v3 = NULL;
  137. }
  138. return 0;
  139. }
  140. static struct pci_ops starfive_pcie_ops = {
  141. .map_bus = plda_pcie_map_bus,
  142. .read = starfive_pcie_config_read,
  143. .write = starfive_pcie_config_write,
  144. };
  145. static int starfive_pcie_clk_rst_init(struct starfive_jh7110_pcie *pcie)
  146. {
  147. struct device *dev = pcie->plda.dev;
  148. int ret;
  149. ret = clk_bulk_prepare_enable(pcie->num_clks, pcie->clks);
  150. if (ret)
  151. return dev_err_probe(dev, ret, "failed to enable clocks\n");
  152. ret = reset_control_deassert(pcie->resets);
  153. if (ret) {
  154. clk_bulk_disable_unprepare(pcie->num_clks, pcie->clks);
  155. dev_err_probe(dev, ret, "failed to deassert resets\n");
  156. }
  157. return ret;
  158. }
  159. static void starfive_pcie_clk_rst_deinit(struct starfive_jh7110_pcie *pcie)
  160. {
  161. reset_control_assert(pcie->resets);
  162. clk_bulk_disable_unprepare(pcie->num_clks, pcie->clks);
  163. }
  164. static bool starfive_pcie_link_up(struct plda_pcie_rp *plda)
  165. {
  166. struct starfive_jh7110_pcie *pcie =
  167. container_of(plda, struct starfive_jh7110_pcie, plda);
  168. int ret;
  169. u32 stg_reg_val;
  170. ret = regmap_read(pcie->reg_syscon,
  171. pcie->stg_pcie_base + STG_SYSCON_LNKSTA_OFFSET,
  172. &stg_reg_val);
  173. if (ret) {
  174. dev_err(pcie->plda.dev, "failed to read link status\n");
  175. return false;
  176. }
  177. return !!(stg_reg_val & DATA_LINK_ACTIVE);
  178. }
  179. static int starfive_pcie_host_wait_for_link(struct starfive_jh7110_pcie *pcie)
  180. {
  181. int retries;
  182. /* Check if the link is up or not */
  183. for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
  184. if (starfive_pcie_link_up(&pcie->plda)) {
  185. dev_info(pcie->plda.dev, "port link up\n");
  186. return 0;
  187. }
  188. usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
  189. }
  190. return -ETIMEDOUT;
  191. }
  192. static int starfive_pcie_enable_phy(struct device *dev,
  193. struct starfive_jh7110_pcie *pcie)
  194. {
  195. int ret;
  196. if (!pcie->phy)
  197. return 0;
  198. ret = phy_init(pcie->phy);
  199. if (ret)
  200. return dev_err_probe(dev, ret,
  201. "failed to initialize pcie phy\n");
  202. ret = phy_set_mode(pcie->phy, PHY_MODE_PCIE);
  203. if (ret) {
  204. dev_err_probe(dev, ret, "failed to set pcie mode\n");
  205. goto err_phy_on;
  206. }
  207. ret = phy_power_on(pcie->phy);
  208. if (ret) {
  209. dev_err_probe(dev, ret, "failed to power on pcie phy\n");
  210. goto err_phy_on;
  211. }
  212. return 0;
  213. err_phy_on:
  214. phy_exit(pcie->phy);
  215. return ret;
  216. }
  217. static void starfive_pcie_disable_phy(struct starfive_jh7110_pcie *pcie)
  218. {
  219. phy_power_off(pcie->phy);
  220. phy_exit(pcie->phy);
  221. }
  222. static void starfive_pcie_host_deinit(struct plda_pcie_rp *plda)
  223. {
  224. struct starfive_jh7110_pcie *pcie =
  225. container_of(plda, struct starfive_jh7110_pcie, plda);
  226. starfive_pcie_clk_rst_deinit(pcie);
  227. if (pcie->vpcie3v3)
  228. regulator_disable(pcie->vpcie3v3);
  229. starfive_pcie_disable_phy(pcie);
  230. }
  231. static int starfive_pcie_host_init(struct plda_pcie_rp *plda)
  232. {
  233. struct starfive_jh7110_pcie *pcie =
  234. container_of(plda, struct starfive_jh7110_pcie, plda);
  235. struct device *dev = plda->dev;
  236. int ret;
  237. int i;
  238. ret = starfive_pcie_enable_phy(dev, pcie);
  239. if (ret)
  240. return ret;
  241. regmap_update_bits(pcie->reg_syscon,
  242. pcie->stg_pcie_base + STG_SYSCON_RP_NEP_OFFSET,
  243. STG_SYSCON_K_RP_NEP, STG_SYSCON_K_RP_NEP);
  244. regmap_update_bits(pcie->reg_syscon,
  245. pcie->stg_pcie_base + STG_SYSCON_AW_OFFSET,
  246. STG_SYSCON_CKREF_SRC_MASK,
  247. FIELD_PREP(STG_SYSCON_CKREF_SRC_MASK, 2));
  248. regmap_update_bits(pcie->reg_syscon,
  249. pcie->stg_pcie_base + STG_SYSCON_AW_OFFSET,
  250. STG_SYSCON_CLKREQ, STG_SYSCON_CLKREQ);
  251. ret = starfive_pcie_clk_rst_init(pcie);
  252. if (ret)
  253. return ret;
  254. if (pcie->vpcie3v3) {
  255. ret = regulator_enable(pcie->vpcie3v3);
  256. if (ret)
  257. dev_err_probe(dev, ret, "failed to enable vpcie3v3 regulator\n");
  258. }
  259. if (pcie->reset_gpio)
  260. gpiod_set_value_cansleep(pcie->reset_gpio, 1);
  261. /* Disable physical functions except #0 */
  262. for (i = 1; i < PCIE_FUNC_NUM; i++) {
  263. regmap_update_bits(pcie->reg_syscon,
  264. pcie->stg_pcie_base + STG_SYSCON_AR_OFFSET,
  265. STG_SYSCON_AXI4_SLVL_AR_MASK,
  266. STG_SYSCON_AXI4_SLVL_PHY_AR(i));
  267. regmap_update_bits(pcie->reg_syscon,
  268. pcie->stg_pcie_base + STG_SYSCON_AW_OFFSET,
  269. STG_SYSCON_AXI4_SLVL_AW_MASK,
  270. STG_SYSCON_AXI4_SLVL_PHY_AW(i));
  271. plda_pcie_disable_func(plda);
  272. }
  273. regmap_update_bits(pcie->reg_syscon,
  274. pcie->stg_pcie_base + STG_SYSCON_AR_OFFSET,
  275. STG_SYSCON_AXI4_SLVL_AR_MASK, 0);
  276. regmap_update_bits(pcie->reg_syscon,
  277. pcie->stg_pcie_base + STG_SYSCON_AW_OFFSET,
  278. STG_SYSCON_AXI4_SLVL_AW_MASK, 0);
  279. plda_pcie_enable_root_port(plda);
  280. plda_pcie_write_rc_bar(plda, 0);
  281. /* PCIe PCI Standard Configuration Identification Settings. */
  282. plda_pcie_set_standard_class(plda);
  283. /*
  284. * The LTR message receiving is enabled by the register "PCIe Message
  285. * Reception" as default, but the forward id & addr are uninitialized.
  286. * If we do not disable LTR message forwarding here, or set a legal
  287. * forwarding address, the kernel will get stuck.
  288. * To workaround, disable the LTR message forwarding here before using
  289. * this feature.
  290. */
  291. plda_pcie_disable_ltr(plda);
  292. /*
  293. * Enable the prefetchable memory window 64-bit addressing in JH7110.
  294. * The 64-bits prefetchable address translation configurations in ATU
  295. * can be work after enable the register setting below.
  296. */
  297. plda_pcie_set_pref_win_64bit(plda);
  298. /*
  299. * Ensure that PERST has been asserted for at least 100 ms,
  300. * the sleep value is T_PVPERL from PCIe CEM spec r2.0 (Table 2-4)
  301. */
  302. msleep(100);
  303. if (pcie->reset_gpio)
  304. gpiod_set_value_cansleep(pcie->reset_gpio, 0);
  305. /*
  306. * With a Downstream Port (<=5GT/s), software must wait a minimum
  307. * of 100ms following exit from a conventional reset before
  308. * sending a configuration request to the device.
  309. */
  310. msleep(PCIE_RESET_CONFIG_WAIT_MS);
  311. if (starfive_pcie_host_wait_for_link(pcie))
  312. dev_info(dev, "port link down\n");
  313. return 0;
  314. }
  315. static const struct plda_pcie_host_ops sf_host_ops = {
  316. .host_init = starfive_pcie_host_init,
  317. .host_deinit = starfive_pcie_host_deinit,
  318. };
  319. static const struct plda_event stf_pcie_event = {
  320. .intx_event = EVENT_PM_MSI_INT_INTX,
  321. .msi_event = EVENT_PM_MSI_INT_MSI
  322. };
  323. static int starfive_pcie_probe(struct platform_device *pdev)
  324. {
  325. struct starfive_jh7110_pcie *pcie;
  326. struct device *dev = &pdev->dev;
  327. struct plda_pcie_rp *plda;
  328. int ret;
  329. pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
  330. if (!pcie)
  331. return -ENOMEM;
  332. plda = &pcie->plda;
  333. plda->dev = dev;
  334. ret = starfive_pcie_parse_dt(pcie, dev);
  335. if (ret)
  336. return ret;
  337. pm_runtime_enable(&pdev->dev);
  338. pm_runtime_get_sync(&pdev->dev);
  339. plda->host_ops = &sf_host_ops;
  340. plda->num_events = PLDA_MAX_EVENT_NUM;
  341. /* mask doorbell event */
  342. plda->events_bitmap = GENMASK(PLDA_INT_EVENT_NUM - 1, 0)
  343. & ~BIT(PLDA_AXI_DOORBELL)
  344. & ~BIT(PLDA_PCIE_DOORBELL);
  345. plda->events_bitmap <<= PLDA_NUM_DMA_EVENTS;
  346. ret = plda_pcie_host_init(&pcie->plda, &starfive_pcie_ops,
  347. &stf_pcie_event);
  348. if (ret) {
  349. pm_runtime_put_sync(&pdev->dev);
  350. pm_runtime_disable(&pdev->dev);
  351. return ret;
  352. }
  353. platform_set_drvdata(pdev, pcie);
  354. return 0;
  355. }
  356. static void starfive_pcie_remove(struct platform_device *pdev)
  357. {
  358. struct starfive_jh7110_pcie *pcie = platform_get_drvdata(pdev);
  359. pm_runtime_put(&pdev->dev);
  360. pm_runtime_disable(&pdev->dev);
  361. plda_pcie_host_deinit(&pcie->plda);
  362. platform_set_drvdata(pdev, NULL);
  363. }
  364. static int starfive_pcie_suspend_noirq(struct device *dev)
  365. {
  366. struct starfive_jh7110_pcie *pcie = dev_get_drvdata(dev);
  367. clk_bulk_disable_unprepare(pcie->num_clks, pcie->clks);
  368. starfive_pcie_disable_phy(pcie);
  369. return 0;
  370. }
  371. static int starfive_pcie_resume_noirq(struct device *dev)
  372. {
  373. struct starfive_jh7110_pcie *pcie = dev_get_drvdata(dev);
  374. int ret;
  375. ret = starfive_pcie_enable_phy(dev, pcie);
  376. if (ret)
  377. return ret;
  378. ret = clk_bulk_prepare_enable(pcie->num_clks, pcie->clks);
  379. if (ret) {
  380. dev_err(dev, "failed to enable clocks\n");
  381. starfive_pcie_disable_phy(pcie);
  382. return ret;
  383. }
  384. return 0;
  385. }
  386. static const struct dev_pm_ops starfive_pcie_pm_ops = {
  387. NOIRQ_SYSTEM_SLEEP_PM_OPS(starfive_pcie_suspend_noirq,
  388. starfive_pcie_resume_noirq)
  389. };
  390. static const struct of_device_id starfive_pcie_of_match[] = {
  391. { .compatible = "starfive,jh7110-pcie", },
  392. { /* sentinel */ }
  393. };
  394. MODULE_DEVICE_TABLE(of, starfive_pcie_of_match);
  395. static struct platform_driver starfive_pcie_driver = {
  396. .driver = {
  397. .name = "pcie-starfive",
  398. .of_match_table = of_match_ptr(starfive_pcie_of_match),
  399. .pm = pm_sleep_ptr(&starfive_pcie_pm_ops),
  400. },
  401. .probe = starfive_pcie_probe,
  402. .remove = starfive_pcie_remove,
  403. };
  404. module_platform_driver(starfive_pcie_driver);
  405. MODULE_DESCRIPTION("StarFive JH7110 PCIe host driver");
  406. MODULE_LICENSE("GPL v2");