pcie_layerscape_rc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2020 NXP
  4. * Layerscape PCIe driver
  5. */
  6. #include <common.h>
  7. #include <asm/arch/fsl_serdes.h>
  8. #include <pci.h>
  9. #include <asm/global_data.h>
  10. #include <asm/io.h>
  11. #include <errno.h>
  12. #include <malloc.h>
  13. #include <dm.h>
  14. #include <dm/devres.h>
  15. #if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3) || \
  16. defined(CONFIG_ARM)
  17. #include <asm/arch/clock.h>
  18. #endif
  19. #include "pcie_layerscape.h"
  20. DECLARE_GLOBAL_DATA_PTR;
  21. static void ls_pcie_cfg0_set_busdev(struct ls_pcie_rc *pcie_rc, u32 busdev)
  22. {
  23. struct ls_pcie *pcie = pcie_rc->pcie;
  24. dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
  25. PCIE_ATU_VIEWPORT);
  26. dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET);
  27. }
  28. static void ls_pcie_cfg1_set_busdev(struct ls_pcie_rc *pcie_rc, u32 busdev)
  29. {
  30. struct ls_pcie *pcie = pcie_rc->pcie;
  31. dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
  32. PCIE_ATU_VIEWPORT);
  33. dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET);
  34. }
  35. static void ls_pcie_setup_atu(struct ls_pcie_rc *pcie_rc)
  36. {
  37. struct pci_region *io, *mem, *pref;
  38. unsigned long long offset = 0;
  39. struct ls_pcie *pcie = pcie_rc->pcie;
  40. int idx = 0;
  41. uint svr;
  42. svr = get_svr();
  43. if (((svr >> SVR_VAR_PER_SHIFT) & SVR_LS102XA_MASK) == SVR_LS102XA) {
  44. offset = LS1021_PCIE_SPACE_OFFSET +
  45. LS1021_PCIE_SPACE_SIZE * pcie->idx;
  46. }
  47. /* ATU 0 : OUTBOUND : CFG0 */
  48. ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
  49. PCIE_ATU_TYPE_CFG0,
  50. pcie_rc->cfg_res.start + offset,
  51. 0,
  52. fdt_resource_size(&pcie_rc->cfg_res) / 2);
  53. /* ATU 1 : OUTBOUND : CFG1 */
  54. ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1,
  55. PCIE_ATU_TYPE_CFG1,
  56. pcie_rc->cfg_res.start + offset +
  57. fdt_resource_size(&pcie_rc->cfg_res) / 2,
  58. 0,
  59. fdt_resource_size(&pcie_rc->cfg_res) / 2);
  60. pci_get_regions(pcie_rc->bus, &io, &mem, &pref);
  61. idx = PCIE_ATU_REGION_INDEX1 + 1;
  62. /* Fix the pcie memory map for LS2088A series SoCs */
  63. svr = (svr >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
  64. if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
  65. svr == SVR_LS2048A || svr == SVR_LS2044A ||
  66. svr == SVR_LS2081A || svr == SVR_LS2041A) {
  67. if (io)
  68. io->phys_start = (io->phys_start &
  69. (PCIE_PHYS_SIZE - 1)) +
  70. LS2088A_PCIE1_PHYS_ADDR +
  71. LS2088A_PCIE_PHYS_SIZE * pcie->idx;
  72. if (mem)
  73. mem->phys_start = (mem->phys_start &
  74. (PCIE_PHYS_SIZE - 1)) +
  75. LS2088A_PCIE1_PHYS_ADDR +
  76. LS2088A_PCIE_PHYS_SIZE * pcie->idx;
  77. if (pref)
  78. pref->phys_start = (pref->phys_start &
  79. (PCIE_PHYS_SIZE - 1)) +
  80. LS2088A_PCIE1_PHYS_ADDR +
  81. LS2088A_PCIE_PHYS_SIZE * pcie->idx;
  82. }
  83. if (io)
  84. /* ATU : OUTBOUND : IO */
  85. ls_pcie_atu_outbound_set(pcie, idx++,
  86. PCIE_ATU_TYPE_IO,
  87. io->phys_start + offset,
  88. io->bus_start,
  89. io->size);
  90. if (mem)
  91. /* ATU : OUTBOUND : MEM */
  92. ls_pcie_atu_outbound_set(pcie, idx++,
  93. PCIE_ATU_TYPE_MEM,
  94. mem->phys_start + offset,
  95. mem->bus_start,
  96. mem->size);
  97. if (pref)
  98. /* ATU : OUTBOUND : pref */
  99. ls_pcie_atu_outbound_set(pcie, idx++,
  100. PCIE_ATU_TYPE_MEM,
  101. pref->phys_start + offset,
  102. pref->bus_start,
  103. pref->size);
  104. ls_pcie_dump_atu(pcie, PCIE_ATU_REGION_NUM, PCIE_ATU_REGION_OUTBOUND);
  105. }
  106. /* Return 0 if the address is valid, -errno if not valid */
  107. static int ls_pcie_addr_valid(struct ls_pcie_rc *pcie_rc, pci_dev_t bdf)
  108. {
  109. struct udevice *bus = pcie_rc->bus;
  110. struct ls_pcie *pcie = pcie_rc->pcie;
  111. if (pcie->mode == PCI_HEADER_TYPE_NORMAL)
  112. return -ENODEV;
  113. if (!pcie_rc->enabled)
  114. return -ENXIO;
  115. if (PCI_BUS(bdf) < dev_seq(bus))
  116. return -EINVAL;
  117. if ((PCI_BUS(bdf) > dev_seq(bus)) && (!ls_pcie_link_up(pcie)))
  118. return -EINVAL;
  119. if (PCI_BUS(bdf) <= (dev_seq(bus) + 1) && (PCI_DEV(bdf) > 0))
  120. return -EINVAL;
  121. return 0;
  122. }
  123. int ls_pcie_conf_address(const struct udevice *bus, pci_dev_t bdf,
  124. uint offset, void **paddress)
  125. {
  126. struct ls_pcie_rc *pcie_rc = dev_get_priv(bus);
  127. struct ls_pcie *pcie = pcie_rc->pcie;
  128. u32 busdev;
  129. if (ls_pcie_addr_valid(pcie_rc, bdf))
  130. return -EINVAL;
  131. if (PCI_BUS(bdf) == dev_seq(bus)) {
  132. *paddress = pcie->dbi + offset;
  133. return 0;
  134. }
  135. busdev = PCIE_ATU_BUS(PCI_BUS(bdf) - dev_seq(bus)) |
  136. PCIE_ATU_DEV(PCI_DEV(bdf)) |
  137. PCIE_ATU_FUNC(PCI_FUNC(bdf));
  138. if (PCI_BUS(bdf) == dev_seq(bus) + 1) {
  139. ls_pcie_cfg0_set_busdev(pcie_rc, busdev);
  140. *paddress = pcie_rc->cfg0 + offset;
  141. } else {
  142. ls_pcie_cfg1_set_busdev(pcie_rc, busdev);
  143. *paddress = pcie_rc->cfg1 + offset;
  144. }
  145. return 0;
  146. }
  147. static int ls_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
  148. uint offset, ulong *valuep,
  149. enum pci_size_t size)
  150. {
  151. return pci_generic_mmap_read_config(bus, ls_pcie_conf_address,
  152. bdf, offset, valuep, size);
  153. }
  154. static int ls_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
  155. uint offset, ulong value,
  156. enum pci_size_t size)
  157. {
  158. return pci_generic_mmap_write_config(bus, ls_pcie_conf_address,
  159. bdf, offset, value, size);
  160. }
  161. /* Clear multi-function bit */
  162. static void ls_pcie_clear_multifunction(struct ls_pcie_rc *pcie_rc)
  163. {
  164. struct ls_pcie *pcie = pcie_rc->pcie;
  165. writeb(PCI_HEADER_TYPE_BRIDGE, pcie->dbi + PCI_HEADER_TYPE);
  166. }
  167. /* Fix class value */
  168. static void ls_pcie_fix_class(struct ls_pcie_rc *pcie_rc)
  169. {
  170. struct ls_pcie *pcie = pcie_rc->pcie;
  171. writew(PCI_CLASS_BRIDGE_PCI, pcie->dbi + PCI_CLASS_DEVICE);
  172. }
  173. /* Drop MSG TLP except for Vendor MSG */
  174. static void ls_pcie_drop_msg_tlp(struct ls_pcie_rc *pcie_rc)
  175. {
  176. struct ls_pcie *pcie = pcie_rc->pcie;
  177. u32 val;
  178. val = dbi_readl(pcie, PCIE_STRFMR1);
  179. val &= 0xDFFFFFFF;
  180. dbi_writel(pcie, val, PCIE_STRFMR1);
  181. }
  182. /* Disable all bars in RC mode */
  183. static void ls_pcie_disable_bars(struct ls_pcie_rc *pcie_rc)
  184. {
  185. struct ls_pcie *pcie = pcie_rc->pcie;
  186. dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_0);
  187. dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_1);
  188. dbi_writel(pcie, 0xfffffffe, PCIE_CS2_OFFSET + PCI_ROM_ADDRESS1);
  189. }
  190. static void ls_pcie_setup_ctrl(struct ls_pcie_rc *pcie_rc)
  191. {
  192. struct ls_pcie *pcie = pcie_rc->pcie;
  193. ls_pcie_setup_atu(pcie_rc);
  194. ls_pcie_dbi_ro_wr_en(pcie);
  195. ls_pcie_fix_class(pcie_rc);
  196. ls_pcie_clear_multifunction(pcie_rc);
  197. ls_pcie_drop_msg_tlp(pcie_rc);
  198. ls_pcie_dbi_ro_wr_dis(pcie);
  199. ls_pcie_disable_bars(pcie_rc);
  200. pcie_rc->stream_id_cur = 0;
  201. }
  202. static int ls_pcie_probe(struct udevice *dev)
  203. {
  204. struct ls_pcie_rc *pcie_rc = dev_get_priv(dev);
  205. const void *fdt = gd->fdt_blob;
  206. int node = dev_of_offset(dev);
  207. struct ls_pcie *pcie;
  208. u16 link_sta;
  209. uint svr;
  210. int ret;
  211. fdt_size_t cfg_size;
  212. pcie_rc->bus = dev;
  213. pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
  214. if (!pcie)
  215. return -ENOMEM;
  216. pcie_rc->pcie = pcie;
  217. ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
  218. "dbi", &pcie_rc->dbi_res);
  219. if (ret) {
  220. printf("ls-pcie: resource \"dbi\" not found\n");
  221. return ret;
  222. }
  223. pcie->idx = (pcie_rc->dbi_res.start - PCIE_SYS_BASE_ADDR) /
  224. PCIE_CCSR_SIZE;
  225. list_add(&pcie_rc->list, &ls_pcie_list);
  226. pcie_rc->enabled = is_serdes_configured(PCIE_SRDS_PRTCL(pcie->idx));
  227. if (!pcie_rc->enabled) {
  228. printf("PCIe%d: %s disabled\n", PCIE_SRDS_PRTCL(pcie->idx),
  229. dev->name);
  230. return 0;
  231. }
  232. pcie->dbi = map_physmem(pcie_rc->dbi_res.start,
  233. fdt_resource_size(&pcie_rc->dbi_res),
  234. MAP_NOCACHE);
  235. pcie->mode = readb(pcie->dbi + PCI_HEADER_TYPE) & 0x7f;
  236. if (pcie->mode == PCI_HEADER_TYPE_NORMAL)
  237. return 0;
  238. ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
  239. "lut", &pcie_rc->lut_res);
  240. if (!ret)
  241. pcie->lut = map_physmem(pcie_rc->lut_res.start,
  242. fdt_resource_size(&pcie_rc->lut_res),
  243. MAP_NOCACHE);
  244. ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
  245. "ctrl", &pcie_rc->ctrl_res);
  246. if (!ret)
  247. pcie->ctrl = map_physmem(pcie_rc->ctrl_res.start,
  248. fdt_resource_size(&pcie_rc->ctrl_res),
  249. MAP_NOCACHE);
  250. if (!pcie->ctrl)
  251. pcie->ctrl = pcie->lut;
  252. if (!pcie->ctrl) {
  253. printf("%s: NOT find CTRL\n", dev->name);
  254. return -1;
  255. }
  256. ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
  257. "config", &pcie_rc->cfg_res);
  258. if (ret) {
  259. printf("%s: resource \"config\" not found\n", dev->name);
  260. return ret;
  261. }
  262. cfg_size = fdt_resource_size(&pcie_rc->cfg_res);
  263. if (cfg_size < SZ_8K) {
  264. printf("PCIe%d: %s Invalid size(0x%llx) for resource \"config\",expected minimum 0x%x\n",
  265. PCIE_SRDS_PRTCL(pcie->idx), dev->name, (u64)cfg_size, SZ_8K);
  266. return 0;
  267. }
  268. /*
  269. * Fix the pcie memory map address and PF control registers address
  270. * for LS2088A series SoCs
  271. */
  272. svr = get_svr();
  273. svr = (svr >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
  274. if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
  275. svr == SVR_LS2048A || svr == SVR_LS2044A ||
  276. svr == SVR_LS2081A || svr == SVR_LS2041A) {
  277. pcie_rc->cfg_res.start = LS2088A_PCIE1_PHYS_ADDR +
  278. LS2088A_PCIE_PHYS_SIZE * pcie->idx;
  279. pcie_rc->cfg_res.end = pcie_rc->cfg_res.start + cfg_size;
  280. pcie->ctrl = pcie->lut + 0x40000;
  281. }
  282. pcie_rc->cfg0 = map_physmem(pcie_rc->cfg_res.start,
  283. fdt_resource_size(&pcie_rc->cfg_res),
  284. MAP_NOCACHE);
  285. pcie_rc->cfg1 = pcie_rc->cfg0 +
  286. fdt_resource_size(&pcie_rc->cfg_res) / 2;
  287. pcie->big_endian = fdtdec_get_bool(fdt, node, "big-endian");
  288. debug("%s dbi:%lx lut:%lx ctrl:0x%lx cfg0:0x%lx, big-endian:%d\n",
  289. dev->name, (unsigned long)pcie->dbi, (unsigned long)pcie->lut,
  290. (unsigned long)pcie->ctrl, (unsigned long)pcie_rc->cfg0,
  291. pcie->big_endian);
  292. printf("PCIe%u: %s %s", PCIE_SRDS_PRTCL(pcie->idx), dev->name,
  293. "Root Complex");
  294. ls_pcie_setup_ctrl(pcie_rc);
  295. if (!ls_pcie_link_up(pcie)) {
  296. /* Let the user know there's no PCIe link */
  297. printf(": no link\n");
  298. return 0;
  299. }
  300. /* Print the negotiated PCIe link width */
  301. link_sta = readw(pcie->dbi + PCIE_LINK_STA);
  302. printf(": x%d gen%d\n", (link_sta & PCIE_LINK_WIDTH_MASK) >> 4,
  303. link_sta & PCIE_LINK_SPEED_MASK);
  304. return 0;
  305. }
  306. static const struct dm_pci_ops ls_pcie_ops = {
  307. .read_config = ls_pcie_read_config,
  308. .write_config = ls_pcie_write_config,
  309. };
  310. static const struct udevice_id ls_pcie_ids[] = {
  311. { .compatible = "fsl,ls-pcie" },
  312. { }
  313. };
  314. U_BOOT_DRIVER(pci_layerscape) = {
  315. .name = "pci_layerscape",
  316. .id = UCLASS_PCI,
  317. .of_match = ls_pcie_ids,
  318. .ops = &ls_pcie_ops,
  319. .probe = ls_pcie_probe,
  320. .priv_auto = sizeof(struct ls_pcie_rc),
  321. };