pcie_layerscape_fixup.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2017 NXP
  4. * Copyright 2014-2015 Freescale Semiconductor, Inc.
  5. * Layerscape PCIe driver
  6. */
  7. #include <common.h>
  8. #include <pci.h>
  9. #include <asm/arch/fsl_serdes.h>
  10. #include <asm/io.h>
  11. #include <errno.h>
  12. #ifdef CONFIG_OF_BOARD_SETUP
  13. #include <linux/libfdt.h>
  14. #include <fdt_support.h>
  15. #ifdef CONFIG_ARM
  16. #include <asm/arch/clock.h>
  17. #endif
  18. #include "pcie_layerscape.h"
  19. #if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
  20. /*
  21. * Return next available LUT index.
  22. */
  23. static int ls_pcie_next_lut_index(struct ls_pcie *pcie)
  24. {
  25. if (pcie->next_lut_index < PCIE_LUT_ENTRY_COUNT)
  26. return pcie->next_lut_index++;
  27. else
  28. return -ENOSPC; /* LUT is full */
  29. }
  30. /* returns the next available streamid for pcie, -errno if failed */
  31. static int ls_pcie_next_streamid(void)
  32. {
  33. static int next_stream_id = FSL_PEX_STREAM_ID_START;
  34. if (next_stream_id > FSL_PEX_STREAM_ID_END)
  35. return -EINVAL;
  36. return next_stream_id++;
  37. }
  38. static void lut_writel(struct ls_pcie *pcie, unsigned int value,
  39. unsigned int offset)
  40. {
  41. if (pcie->big_endian)
  42. out_be32(pcie->lut + offset, value);
  43. else
  44. out_le32(pcie->lut + offset, value);
  45. }
  46. /*
  47. * Program a single LUT entry
  48. */
  49. static void ls_pcie_lut_set_mapping(struct ls_pcie *pcie, int index, u32 devid,
  50. u32 streamid)
  51. {
  52. /* leave mask as all zeroes, want to match all bits */
  53. lut_writel(pcie, devid << 16, PCIE_LUT_UDR(index));
  54. lut_writel(pcie, streamid | PCIE_LUT_ENABLE, PCIE_LUT_LDR(index));
  55. }
  56. /*
  57. * An msi-map is a property to be added to the pci controller
  58. * node. It is a table, where each entry consists of 4 fields
  59. * e.g.:
  60. *
  61. * msi-map = <[devid] [phandle-to-msi-ctrl] [stream-id] [count]
  62. * [devid] [phandle-to-msi-ctrl] [stream-id] [count]>;
  63. */
  64. static void fdt_pcie_set_msi_map_entry(void *blob, struct ls_pcie *pcie,
  65. u32 devid, u32 streamid)
  66. {
  67. u32 *prop;
  68. u32 phandle;
  69. int nodeoffset;
  70. uint svr;
  71. char *compat = NULL;
  72. /* find pci controller node */
  73. nodeoffset = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie",
  74. pcie->dbi_res.start);
  75. if (nodeoffset < 0) {
  76. #ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
  77. svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
  78. if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
  79. svr == SVR_LS2048A || svr == SVR_LS2044A ||
  80. svr == SVR_LS2081A || svr == SVR_LS2041A)
  81. compat = "fsl,ls2088a-pcie";
  82. else
  83. compat = CONFIG_FSL_PCIE_COMPAT;
  84. if (compat)
  85. nodeoffset = fdt_node_offset_by_compat_reg(blob,
  86. compat, pcie->dbi_res.start);
  87. #endif
  88. if (nodeoffset < 0)
  89. return;
  90. }
  91. /* get phandle to MSI controller */
  92. prop = (u32 *)fdt_getprop(blob, nodeoffset, "msi-parent", 0);
  93. if (prop == NULL) {
  94. debug("\n%s: ERROR: missing msi-parent: PCIe%d\n",
  95. __func__, pcie->idx);
  96. return;
  97. }
  98. phandle = fdt32_to_cpu(*prop);
  99. /* set one msi-map row */
  100. fdt_appendprop_u32(blob, nodeoffset, "msi-map", devid);
  101. fdt_appendprop_u32(blob, nodeoffset, "msi-map", phandle);
  102. fdt_appendprop_u32(blob, nodeoffset, "msi-map", streamid);
  103. fdt_appendprop_u32(blob, nodeoffset, "msi-map", 1);
  104. }
  105. /*
  106. * An iommu-map is a property to be added to the pci controller
  107. * node. It is a table, where each entry consists of 4 fields
  108. * e.g.:
  109. *
  110. * iommu-map = <[devid] [phandle-to-iommu-ctrl] [stream-id] [count]
  111. * [devid] [phandle-to-iommu-ctrl] [stream-id] [count]>;
  112. */
  113. static void fdt_pcie_set_iommu_map_entry(void *blob, struct ls_pcie *pcie,
  114. u32 devid, u32 streamid)
  115. {
  116. u32 *prop;
  117. u32 iommu_map[4];
  118. int nodeoffset;
  119. int lenp;
  120. uint svr;
  121. char *compat = NULL;
  122. /* find pci controller node */
  123. nodeoffset = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie",
  124. pcie->dbi_res.start);
  125. if (nodeoffset < 0) {
  126. #ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
  127. svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
  128. if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
  129. svr == SVR_LS2048A || svr == SVR_LS2044A ||
  130. svr == SVR_LS2081A || svr == SVR_LS2041A)
  131. compat = "fsl,ls2088a-pcie";
  132. else
  133. compat = CONFIG_FSL_PCIE_COMPAT;
  134. if (compat)
  135. nodeoffset = fdt_node_offset_by_compat_reg(blob,
  136. compat, pcie->dbi_res.start);
  137. #endif
  138. if (nodeoffset < 0)
  139. return;
  140. }
  141. /* get phandle to iommu controller */
  142. prop = fdt_getprop_w(blob, nodeoffset, "iommu-map", &lenp);
  143. if (prop == NULL) {
  144. debug("\n%s: ERROR: missing iommu-map: PCIe%d\n",
  145. __func__, pcie->idx);
  146. return;
  147. }
  148. /* set iommu-map row */
  149. iommu_map[0] = cpu_to_fdt32(devid);
  150. iommu_map[1] = *++prop;
  151. iommu_map[2] = cpu_to_fdt32(streamid);
  152. iommu_map[3] = cpu_to_fdt32(1);
  153. if (devid == 0) {
  154. fdt_setprop_inplace(blob, nodeoffset, "iommu-map",
  155. iommu_map, 16);
  156. } else {
  157. fdt_appendprop(blob, nodeoffset, "iommu-map", iommu_map, 16);
  158. }
  159. }
  160. static void fdt_fixup_pcie(void *blob)
  161. {
  162. struct udevice *dev, *bus;
  163. struct ls_pcie *pcie;
  164. int streamid;
  165. int index;
  166. pci_dev_t bdf;
  167. /* Scan all known buses */
  168. for (pci_find_first_device(&dev);
  169. dev;
  170. pci_find_next_device(&dev)) {
  171. for (bus = dev; device_is_on_pci_bus(bus);)
  172. bus = bus->parent;
  173. pcie = dev_get_priv(bus);
  174. streamid = ls_pcie_next_streamid();
  175. if (streamid < 0) {
  176. debug("ERROR: no stream ids free\n");
  177. continue;
  178. }
  179. index = ls_pcie_next_lut_index(pcie);
  180. if (index < 0) {
  181. debug("ERROR: no LUT indexes free\n");
  182. continue;
  183. }
  184. /* the DT fixup must be relative to the hose first_busno */
  185. bdf = dm_pci_get_bdf(dev) - PCI_BDF(bus->seq, 0, 0);
  186. /* map PCI b.d.f to streamID in LUT */
  187. ls_pcie_lut_set_mapping(pcie, index, bdf >> 8,
  188. streamid);
  189. /* update msi-map in device tree */
  190. fdt_pcie_set_msi_map_entry(blob, pcie, bdf >> 8,
  191. streamid);
  192. /* update iommu-map in device tree */
  193. fdt_pcie_set_iommu_map_entry(blob, pcie, bdf >> 8,
  194. streamid);
  195. }
  196. }
  197. #endif
  198. static void ft_pcie_ls_setup(void *blob, struct ls_pcie *pcie)
  199. {
  200. int off;
  201. uint svr;
  202. char *compat = NULL;
  203. off = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie",
  204. pcie->dbi_res.start);
  205. if (off < 0) {
  206. #ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
  207. svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
  208. if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
  209. svr == SVR_LS2048A || svr == SVR_LS2044A ||
  210. svr == SVR_LS2081A || svr == SVR_LS2041A)
  211. compat = "fsl,ls2088a-pcie";
  212. else
  213. compat = CONFIG_FSL_PCIE_COMPAT;
  214. if (compat)
  215. off = fdt_node_offset_by_compat_reg(blob,
  216. compat, pcie->dbi_res.start);
  217. #endif
  218. if (off < 0)
  219. return;
  220. }
  221. if (pcie->enabled)
  222. fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
  223. else
  224. fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
  225. }
  226. /* Fixup Kernel DT for PCIe */
  227. void ft_pci_setup(void *blob, bd_t *bd)
  228. {
  229. struct ls_pcie *pcie;
  230. list_for_each_entry(pcie, &ls_pcie_list, list)
  231. ft_pcie_ls_setup(blob, pcie);
  232. #if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
  233. fdt_fixup_pcie(blob);
  234. #endif
  235. }
  236. #else /* !CONFIG_OF_BOARD_SETUP */
  237. void ft_pci_setup(void *blob, bd_t *bd)
  238. {
  239. }
  240. #endif