pcie_starfive_jh7110.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * StarFive PLDA PCIe host controller driver
  4. *
  5. * Copyright (C) 2023 StarFive Technology Co., Ltd.
  6. * Author: Mason Huo <mason.huo@starfivetech.com>
  7. *
  8. */
  9. #include <common.h>
  10. #include <clk.h>
  11. #include <dm.h>
  12. #include <pci.h>
  13. #include <pci_ids.h>
  14. #include <power-domain.h>
  15. #include <regmap.h>
  16. #include <reset.h>
  17. #include <syscon.h>
  18. #include <asm/global_data.h>
  19. #include <asm/io.h>
  20. #include <asm-generic/gpio.h>
  21. #include <dm/device_compat.h>
  22. #include <dm/pinctrl.h>
  23. #include <linux/delay.h>
  24. #include <linux/iopoll.h>
  25. #include "pcie_plda_common.h"
  26. /* system control */
  27. #define STG_SYSCON_K_RP_NEP_MASK BIT(8)
  28. #define STG_SYSCON_AXI4_SLVL_ARFUNC_MASK GENMASK(22, 8)
  29. #define STG_SYSCON_AXI4_SLVL_ARFUNC_SHIFT 8
  30. #define STG_SYSCON_AXI4_SLVL_AWFUNC_MASK GENMASK(14, 0)
  31. #define STG_SYSCON_CLKREQ_MASK BIT(22)
  32. #define STG_SYSCON_CKREF_SRC_SHIFT 18
  33. #define STG_SYSCON_CKREF_SRC_MASK GENMASK(19, 18)
  34. DECLARE_GLOBAL_DATA_PTR;
  35. struct starfive_pcie {
  36. struct pcie_plda plda;
  37. struct clk_bulk clks;
  38. struct reset_ctl_bulk rsts;
  39. struct gpio_desc reset_gpio;
  40. struct regmap *regmap;
  41. u32 stg_arfun;
  42. u32 stg_awfun;
  43. u32 stg_rp_nep;
  44. };
  45. static int starfive_pcie_atr_init(struct starfive_pcie *priv)
  46. {
  47. struct udevice *ctlr = pci_get_controller(priv->plda.dev);
  48. struct pci_controller *hose = dev_get_uclass_priv(ctlr);
  49. int i, ret;
  50. /*
  51. * As the two host bridges in JH7110 soc have the same default
  52. * address translation table, this cause the second root port can't
  53. * access it's host bridge config space correctly.
  54. * To workaround, config the ATR of host bridge config space by SW.
  55. */
  56. ret = plda_pcie_set_atr_entry(&priv->plda,
  57. (phys_addr_t)priv->plda.cfg_base, 0,
  58. priv->plda.cfg_size,
  59. XR3PCI_ATR_TRSLID_PCIE_CONFIG);
  60. if (ret)
  61. return ret;
  62. for (i = 0; i < hose->region_count; i++) {
  63. if (hose->regions[i].flags == PCI_REGION_SYS_MEMORY)
  64. continue;
  65. /* Only support identity mappings. */
  66. if (hose->regions[i].bus_start !=
  67. hose->regions[i].phys_start)
  68. return -EINVAL;
  69. ret = plda_pcie_set_atr_entry(&priv->plda,
  70. hose->regions[i].phys_start,
  71. hose->regions[i].bus_start,
  72. hose->regions[i].size,
  73. XR3PCI_ATR_TRSLID_PCIE_MEMORY);
  74. if (ret)
  75. return ret;
  76. }
  77. return 0;
  78. }
  79. static int starfive_pcie_get_syscon(struct udevice *dev)
  80. {
  81. struct starfive_pcie *priv = dev_get_priv(dev);
  82. struct udevice *syscon;
  83. struct ofnode_phandle_args syscfg_phandle;
  84. u32 cells[4];
  85. int ret;
  86. /* get corresponding syscon phandle */
  87. ret = dev_read_phandle_with_args(dev, "starfive,stg-syscon", NULL, 0, 0,
  88. &syscfg_phandle);
  89. if (ret < 0) {
  90. dev_err(dev, "Can't get syscfg phandle: %d\n", ret);
  91. return ret;
  92. }
  93. ret = uclass_get_device_by_ofnode(UCLASS_SYSCON, syscfg_phandle.node,
  94. &syscon);
  95. if (ret) {
  96. dev_err(dev, "Unable to find syscon device (%d)\n", ret);
  97. return ret;
  98. }
  99. priv->regmap = syscon_get_regmap(syscon);
  100. if (!priv->regmap) {
  101. dev_err(dev, "Unable to find regmap\n");
  102. return -ENODEV;
  103. }
  104. /* get syscon register offset */
  105. ret = dev_read_u32_array(dev, "starfive,stg-syscon",
  106. cells, ARRAY_SIZE(cells));
  107. if (ret) {
  108. dev_err(dev, "Get syscon register err %d\n", ret);
  109. return -EINVAL;
  110. }
  111. dev_dbg(dev, "Get syscon values: %x, %x, %x\n",
  112. cells[1], cells[2], cells[3]);
  113. priv->stg_arfun = cells[1];
  114. priv->stg_awfun = cells[2];
  115. priv->stg_rp_nep = cells[3];
  116. return 0;
  117. }
  118. static int starfive_pcie_parse_dt(struct udevice *dev)
  119. {
  120. struct starfive_pcie *priv = dev_get_priv(dev);
  121. int ret;
  122. priv->plda.reg_base = (void *)dev_read_addr_name(dev, "reg");
  123. if (priv->plda.reg_base == (void __iomem *)FDT_ADDR_T_NONE) {
  124. dev_err(dev, "Missing required reg address range\n");
  125. return -EINVAL;
  126. }
  127. priv->plda.cfg_base =
  128. (void *)dev_read_addr_size_name(dev,
  129. "config",
  130. &priv->plda.cfg_size);
  131. if (priv->plda.cfg_base == (void __iomem *)FDT_ADDR_T_NONE) {
  132. dev_err(dev, "Missing required config address range");
  133. return -EINVAL;
  134. }
  135. ret = starfive_pcie_get_syscon(dev);
  136. if (ret) {
  137. dev_err(dev, "Can't get syscon: %d\n", ret);
  138. return ret;
  139. }
  140. ret = reset_get_bulk(dev, &priv->rsts);
  141. if (ret) {
  142. dev_err(dev, "Can't get reset: %d\n", ret);
  143. return ret;
  144. }
  145. ret = clk_get_bulk(dev, &priv->clks);
  146. if (ret) {
  147. dev_err(dev, "Can't get clock: %d\n", ret);
  148. return ret;
  149. }
  150. ret = gpio_request_by_name(dev, "reset-gpios", 0, &priv->reset_gpio,
  151. GPIOD_IS_OUT);
  152. if (ret) {
  153. dev_err(dev, "Can't get reset-gpio: %d\n", ret);
  154. return ret;
  155. }
  156. if (!dm_gpio_is_valid(&priv->reset_gpio)) {
  157. dev_err(dev, "reset-gpio is not valid\n");
  158. return -EINVAL;
  159. }
  160. return 0;
  161. }
  162. static int starfive_pcie_init_port(struct udevice *dev)
  163. {
  164. int ret, i;
  165. struct starfive_pcie *priv = dev_get_priv(dev);
  166. struct pcie_plda *plda = &priv->plda;
  167. ret = clk_enable_bulk(&priv->clks);
  168. if (ret) {
  169. dev_err(dev, "Failed to enable clks (ret=%d)\n", ret);
  170. return ret;
  171. }
  172. ret = reset_deassert_bulk(&priv->rsts);
  173. if (ret) {
  174. dev_err(dev, "Failed to deassert resets (ret=%d)\n", ret);
  175. goto err_deassert_clk;
  176. }
  177. dm_gpio_set_value(&priv->reset_gpio, 1);
  178. /* Disable physical functions except #0 */
  179. for (i = 1; i < PLDA_FUNC_NUM; i++) {
  180. regmap_update_bits(priv->regmap,
  181. priv->stg_arfun,
  182. STG_SYSCON_AXI4_SLVL_ARFUNC_MASK,
  183. (i << PLDA_PHY_FUNC_SHIFT) <<
  184. STG_SYSCON_AXI4_SLVL_ARFUNC_SHIFT);
  185. regmap_update_bits(priv->regmap,
  186. priv->stg_awfun,
  187. STG_SYSCON_AXI4_SLVL_AWFUNC_MASK,
  188. i << PLDA_PHY_FUNC_SHIFT);
  189. plda_pcie_disable_func(plda);
  190. }
  191. /* Disable physical functions */
  192. regmap_update_bits(priv->regmap,
  193. priv->stg_arfun,
  194. STG_SYSCON_AXI4_SLVL_ARFUNC_MASK,
  195. 0);
  196. regmap_update_bits(priv->regmap,
  197. priv->stg_awfun,
  198. STG_SYSCON_AXI4_SLVL_AWFUNC_MASK,
  199. 0);
  200. plda_pcie_enable_root_port(plda);
  201. /* PCIe PCI Standard Configuration Identification Settings. */
  202. plda_pcie_set_standard_class(plda);
  203. /*
  204. * The LTR message forwarding of PCIe Message Reception was set by core
  205. * as default, but the forward id & addr are also need to be reset.
  206. * If we do not disable LTR message forwarding here, or set a legal
  207. * forwarding address, the kernel will get stuck after this driver probe.
  208. * To workaround, disable the LTR message forwarding support on
  209. * PCIe Message Reception.
  210. */
  211. plda_pcie_disable_ltr(plda);
  212. /* Prefetchable memory window 64-bit addressing support */
  213. plda_pcie_set_pref_win_64bit(plda);
  214. starfive_pcie_atr_init(priv);
  215. dm_gpio_set_value(&priv->reset_gpio, 0);
  216. /* Ensure that PERST in default at least 300 ms */
  217. mdelay(300);
  218. return 0;
  219. err_deassert_clk:
  220. clk_disable_bulk(&priv->clks);
  221. return ret;
  222. }
  223. static int starfive_pcie_probe(struct udevice *dev)
  224. {
  225. struct starfive_pcie *priv = dev_get_priv(dev);
  226. int ret;
  227. priv->plda.atr_table_num = 0;
  228. priv->plda.dev = dev;
  229. ret = starfive_pcie_parse_dt(dev);
  230. if (ret)
  231. return ret;
  232. regmap_update_bits(priv->regmap,
  233. priv->stg_rp_nep,
  234. STG_SYSCON_K_RP_NEP_MASK,
  235. STG_SYSCON_K_RP_NEP_MASK);
  236. regmap_update_bits(priv->regmap,
  237. priv->stg_awfun,
  238. STG_SYSCON_CKREF_SRC_MASK,
  239. 2 << STG_SYSCON_CKREF_SRC_SHIFT);
  240. regmap_update_bits(priv->regmap,
  241. priv->stg_awfun,
  242. STG_SYSCON_CLKREQ_MASK,
  243. STG_SYSCON_CLKREQ_MASK);
  244. ret = starfive_pcie_init_port(dev);
  245. if (ret)
  246. return ret;
  247. dev_err(dev, "Starfive PCIe bus probed.\n");
  248. return 0;
  249. }
  250. static const struct dm_pci_ops starfive_pcie_ops = {
  251. .read_config = plda_pcie_config_read,
  252. .write_config = plda_pcie_config_write,
  253. };
  254. static const struct udevice_id starfive_pcie_ids[] = {
  255. { .compatible = "starfive,jh7110-pcie" },
  256. { }
  257. };
  258. U_BOOT_DRIVER(starfive_pcie_drv) = {
  259. .name = "starfive_7110_pcie",
  260. .id = UCLASS_PCI,
  261. .of_match = starfive_pcie_ids,
  262. .ops = &starfive_pcie_ops,
  263. .probe = starfive_pcie_probe,
  264. .priv_auto = sizeof(struct starfive_pcie),
  265. };