pcie_layerscape.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. * Copyright 2014-2015 Freescale Semiconductor, Inc.
  3. * Layerscape PCIe driver
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <asm/arch/fsl_serdes.h>
  9. #include <pci.h>
  10. #include <asm/io.h>
  11. #include <errno.h>
  12. #include <malloc.h>
  13. #include <asm/arch-fsl-lsch3/fdt.h>
  14. #ifndef CONFIG_SYS_PCI_MEMORY_BUS
  15. #define CONFIG_SYS_PCI_MEMORY_BUS CONFIG_SYS_SDRAM_BASE
  16. #endif
  17. #ifndef CONFIG_SYS_PCI_MEMORY_PHYS
  18. #define CONFIG_SYS_PCI_MEMORY_PHYS CONFIG_SYS_SDRAM_BASE
  19. #endif
  20. #ifndef CONFIG_SYS_PCI_MEMORY_SIZE
  21. #define CONFIG_SYS_PCI_MEMORY_SIZE (2 * 1024 * 1024 * 1024UL) /* 2G */
  22. #endif
  23. #ifndef CONFIG_SYS_PCI_EP_MEMORY_BASE
  24. #define CONFIG_SYS_PCI_EP_MEMORY_BASE CONFIG_SYS_LOAD_ADDR
  25. #endif
  26. /* iATU registers */
  27. #define PCIE_ATU_VIEWPORT 0x900
  28. #define PCIE_ATU_REGION_INBOUND (0x1 << 31)
  29. #define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
  30. #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
  31. #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
  32. #define PCIE_ATU_REGION_INDEX2 (0x2 << 0)
  33. #define PCIE_ATU_REGION_INDEX3 (0x3 << 0)
  34. #define PCIE_ATU_CR1 0x904
  35. #define PCIE_ATU_TYPE_MEM (0x0 << 0)
  36. #define PCIE_ATU_TYPE_IO (0x2 << 0)
  37. #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
  38. #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
  39. #define PCIE_ATU_CR2 0x908
  40. #define PCIE_ATU_ENABLE (0x1 << 31)
  41. #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
  42. #define PCIE_ATU_BAR_NUM(bar) ((bar) << 8)
  43. #define PCIE_ATU_LOWER_BASE 0x90C
  44. #define PCIE_ATU_UPPER_BASE 0x910
  45. #define PCIE_ATU_LIMIT 0x914
  46. #define PCIE_ATU_LOWER_TARGET 0x918
  47. #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
  48. #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
  49. #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
  50. #define PCIE_ATU_UPPER_TARGET 0x91C
  51. /* LUT registers */
  52. #define PCIE_LUT_BASE 0x80000
  53. #define PCIE_LUT_LCTRL0 0x7F8
  54. #define PCIE_LUT_DBG 0x7FC
  55. #define PCIE_DBI_RO_WR_EN 0x8bc
  56. #define PCIE_LINK_CAP 0x7c
  57. #define PCIE_LINK_SPEED_MASK 0xf
  58. #define PCIE_LINK_STA 0x82
  59. #define LTSSM_STATE_MASK 0x3f
  60. #define LTSSM_PCIE_L0 0x11 /* L0 state */
  61. #define PCIE_DBI_SIZE 0x100000 /* 1M */
  62. #define PCIE_LCTRL0_CFG2_ENABLE (1 << 31)
  63. #define PCIE_LCTRL0_VF(vf) ((vf) << 22)
  64. #define PCIE_LCTRL0_PF(pf) ((pf) << 16)
  65. #define PCIE_LCTRL0_VF_ACTIVE (1 << 21)
  66. #define PCIE_LCTRL0_VAL(pf, vf) (PCIE_LCTRL0_PF(pf) | \
  67. PCIE_LCTRL0_VF(vf) | \
  68. ((vf) == 0 ? 0 : PCIE_LCTRL0_VF_ACTIVE) | \
  69. PCIE_LCTRL0_CFG2_ENABLE)
  70. #define PCIE_NO_SRIOV_BAR_BASE 0x1000
  71. #define PCIE_PF_NUM 2
  72. #define PCIE_VF_NUM 64
  73. #define PCIE_BAR0_SIZE (4 * 1024) /* 4K */
  74. #define PCIE_BAR1_SIZE (8 * 1024) /* 8K for MSIX */
  75. #define PCIE_BAR2_SIZE (4 * 1024) /* 4K */
  76. #define PCIE_BAR4_SIZE (1 * 1024 * 1024) /* 1M */
  77. struct ls_pcie {
  78. int idx;
  79. void __iomem *dbi;
  80. void __iomem *va_cfg0;
  81. void __iomem *va_cfg1;
  82. struct pci_controller hose;
  83. };
  84. struct ls_pcie_info {
  85. unsigned long regs;
  86. int pci_num;
  87. u64 phys_base;
  88. u64 cfg0_phys;
  89. u64 cfg0_size;
  90. u64 cfg1_phys;
  91. u64 cfg1_size;
  92. u64 mem_bus;
  93. u64 mem_phys;
  94. u64 mem_size;
  95. u64 io_bus;
  96. u64 io_phys;
  97. u64 io_size;
  98. };
  99. #define SET_LS_PCIE_INFO(x, num) \
  100. { \
  101. x.regs = CONFIG_SYS_PCIE##num##_ADDR; \
  102. x.phys_base = CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
  103. x.cfg0_phys = CONFIG_SYS_PCIE_CFG0_PHYS_OFF + \
  104. CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
  105. x.cfg0_size = CONFIG_SYS_PCIE_CFG0_SIZE; \
  106. x.cfg1_phys = CONFIG_SYS_PCIE_CFG1_PHYS_OFF + \
  107. CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
  108. x.cfg1_size = CONFIG_SYS_PCIE_CFG1_SIZE; \
  109. x.mem_bus = CONFIG_SYS_PCIE_MEM_BUS; \
  110. x.mem_phys = CONFIG_SYS_PCIE_MEM_PHYS_OFF + \
  111. CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
  112. x.mem_size = CONFIG_SYS_PCIE_MEM_SIZE; \
  113. x.io_bus = CONFIG_SYS_PCIE_IO_BUS; \
  114. x.io_phys = CONFIG_SYS_PCIE_IO_PHYS_OFF + \
  115. CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
  116. x.io_size = CONFIG_SYS_PCIE_IO_SIZE; \
  117. x.pci_num = num; \
  118. }
  119. #ifdef CONFIG_LS102XA
  120. #include <asm/arch/immap_ls102xa.h>
  121. /* PEX1/2 Misc Ports Status Register */
  122. #define LTSSM_STATE_SHIFT 20
  123. static int ls_pcie_link_state(struct ls_pcie *pcie)
  124. {
  125. u32 state;
  126. struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_FSL_SCFG_ADDR;
  127. state = in_be32(&scfg->pexmscportsr[pcie->idx]);
  128. state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
  129. if (state < LTSSM_PCIE_L0) {
  130. debug("....PCIe link error. LTSSM=0x%02x.\n", state);
  131. return 0;
  132. }
  133. return 1;
  134. }
  135. #else
  136. static int ls_pcie_link_state(struct ls_pcie *pcie)
  137. {
  138. u32 state;
  139. state = readl(pcie->dbi + PCIE_LUT_BASE + PCIE_LUT_DBG) &
  140. LTSSM_STATE_MASK;
  141. if (state < LTSSM_PCIE_L0) {
  142. debug("....PCIe link error. LTSSM=0x%02x.\n", state);
  143. return 0;
  144. }
  145. return 1;
  146. }
  147. #endif
  148. static int ls_pcie_link_up(struct ls_pcie *pcie)
  149. {
  150. int state;
  151. u32 cap;
  152. state = ls_pcie_link_state(pcie);
  153. if (state)
  154. return state;
  155. /* Try to download speed to gen1 */
  156. cap = readl(pcie->dbi + PCIE_LINK_CAP);
  157. writel((cap & (~PCIE_LINK_SPEED_MASK)) | 1, pcie->dbi + PCIE_LINK_CAP);
  158. /*
  159. * Notice: the following delay has critical impact on link training
  160. * if too short (<30ms) the link doesn't get up.
  161. */
  162. mdelay(100);
  163. state = ls_pcie_link_state(pcie);
  164. if (state)
  165. return state;
  166. writel(cap, pcie->dbi + PCIE_LINK_CAP);
  167. return 0;
  168. }
  169. static void ls_pcie_cfg0_set_busdev(struct ls_pcie *pcie, u32 busdev)
  170. {
  171. writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
  172. pcie->dbi + PCIE_ATU_VIEWPORT);
  173. writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
  174. }
  175. static void ls_pcie_cfg1_set_busdev(struct ls_pcie *pcie, u32 busdev)
  176. {
  177. writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
  178. pcie->dbi + PCIE_ATU_VIEWPORT);
  179. writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
  180. }
  181. static void ls_pcie_iatu_outbound_set(struct ls_pcie *pcie, int idx, int type,
  182. u64 phys, u64 bus_addr, pci_size_t size)
  183. {
  184. writel(PCIE_ATU_REGION_OUTBOUND | idx, pcie->dbi + PCIE_ATU_VIEWPORT);
  185. writel((u32)phys, pcie->dbi + PCIE_ATU_LOWER_BASE);
  186. writel(phys >> 32, pcie->dbi + PCIE_ATU_UPPER_BASE);
  187. writel(phys + size - 1, pcie->dbi + PCIE_ATU_LIMIT);
  188. writel((u32)bus_addr, pcie->dbi + PCIE_ATU_LOWER_TARGET);
  189. writel(bus_addr >> 32, pcie->dbi + PCIE_ATU_UPPER_TARGET);
  190. writel(type, pcie->dbi + PCIE_ATU_CR1);
  191. writel(PCIE_ATU_ENABLE, pcie->dbi + PCIE_ATU_CR2);
  192. }
  193. /* Use bar match mode and MEM type as default */
  194. static void ls_pcie_iatu_inbound_set(struct ls_pcie *pcie, int idx,
  195. int bar, u64 phys)
  196. {
  197. writel(PCIE_ATU_REGION_INBOUND | idx, pcie->dbi + PCIE_ATU_VIEWPORT);
  198. writel((u32)phys, pcie->dbi + PCIE_ATU_LOWER_TARGET);
  199. writel(phys >> 32, pcie->dbi + PCIE_ATU_UPPER_TARGET);
  200. writel(PCIE_ATU_TYPE_MEM, pcie->dbi + PCIE_ATU_CR1);
  201. writel(PCIE_ATU_ENABLE | PCIE_ATU_BAR_MODE_ENABLE |
  202. PCIE_ATU_BAR_NUM(bar), pcie->dbi + PCIE_ATU_CR2);
  203. }
  204. static void ls_pcie_setup_atu(struct ls_pcie *pcie, struct ls_pcie_info *info)
  205. {
  206. #ifdef DEBUG
  207. int i;
  208. #endif
  209. /* ATU 0 : OUTBOUND : CFG0 */
  210. ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
  211. PCIE_ATU_TYPE_CFG0,
  212. info->cfg0_phys,
  213. 0,
  214. info->cfg0_size);
  215. /* ATU 1 : OUTBOUND : CFG1 */
  216. ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1,
  217. PCIE_ATU_TYPE_CFG1,
  218. info->cfg1_phys,
  219. 0,
  220. info->cfg1_size);
  221. /* ATU 2 : OUTBOUND : MEM */
  222. ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX2,
  223. PCIE_ATU_TYPE_MEM,
  224. info->mem_phys,
  225. info->mem_bus,
  226. info->mem_size);
  227. /* ATU 3 : OUTBOUND : IO */
  228. ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX3,
  229. PCIE_ATU_TYPE_IO,
  230. info->io_phys,
  231. info->io_bus,
  232. info->io_size);
  233. #ifdef DEBUG
  234. for (i = 0; i <= PCIE_ATU_REGION_INDEX3; i++) {
  235. writel(PCIE_ATU_REGION_OUTBOUND | i,
  236. pcie->dbi + PCIE_ATU_VIEWPORT);
  237. debug("iATU%d:\n", i);
  238. debug("\tLOWER PHYS 0x%08x\n",
  239. readl(pcie->dbi + PCIE_ATU_LOWER_BASE));
  240. debug("\tUPPER PHYS 0x%08x\n",
  241. readl(pcie->dbi + PCIE_ATU_UPPER_BASE));
  242. debug("\tLOWER BUS 0x%08x\n",
  243. readl(pcie->dbi + PCIE_ATU_LOWER_TARGET));
  244. debug("\tUPPER BUS 0x%08x\n",
  245. readl(pcie->dbi + PCIE_ATU_UPPER_TARGET));
  246. debug("\tLIMIT 0x%08x\n",
  247. readl(pcie->dbi + PCIE_ATU_LIMIT));
  248. debug("\tCR1 0x%08x\n",
  249. readl(pcie->dbi + PCIE_ATU_CR1));
  250. debug("\tCR2 0x%08x\n",
  251. readl(pcie->dbi + PCIE_ATU_CR2));
  252. }
  253. #endif
  254. }
  255. int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
  256. {
  257. /* Do not skip controller */
  258. return 0;
  259. }
  260. static int ls_pcie_addr_valid(struct pci_controller *hose, pci_dev_t d)
  261. {
  262. if (PCI_DEV(d) > 0)
  263. return -EINVAL;
  264. /* Controller does not support multi-function in RC mode */
  265. if ((PCI_BUS(d) == hose->first_busno) && (PCI_FUNC(d) > 0))
  266. return -EINVAL;
  267. return 0;
  268. }
  269. static int ls_pcie_read_config(struct pci_controller *hose, pci_dev_t d,
  270. int where, u32 *val)
  271. {
  272. struct ls_pcie *pcie = hose->priv_data;
  273. u32 busdev, *addr;
  274. if (ls_pcie_addr_valid(hose, d)) {
  275. *val = 0xffffffff;
  276. return -EINVAL;
  277. }
  278. if (PCI_BUS(d) == hose->first_busno) {
  279. addr = pcie->dbi + (where & ~0x3);
  280. } else {
  281. busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
  282. PCIE_ATU_DEV(PCI_DEV(d)) |
  283. PCIE_ATU_FUNC(PCI_FUNC(d));
  284. if (PCI_BUS(d) == hose->first_busno + 1) {
  285. ls_pcie_cfg0_set_busdev(pcie, busdev);
  286. addr = pcie->va_cfg0 + (where & ~0x3);
  287. } else {
  288. ls_pcie_cfg1_set_busdev(pcie, busdev);
  289. addr = pcie->va_cfg1 + (where & ~0x3);
  290. }
  291. }
  292. *val = readl(addr);
  293. return 0;
  294. }
  295. static int ls_pcie_write_config(struct pci_controller *hose, pci_dev_t d,
  296. int where, u32 val)
  297. {
  298. struct ls_pcie *pcie = hose->priv_data;
  299. u32 busdev, *addr;
  300. if (ls_pcie_addr_valid(hose, d))
  301. return -EINVAL;
  302. if (PCI_BUS(d) == hose->first_busno) {
  303. addr = pcie->dbi + (where & ~0x3);
  304. } else {
  305. busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
  306. PCIE_ATU_DEV(PCI_DEV(d)) |
  307. PCIE_ATU_FUNC(PCI_FUNC(d));
  308. if (PCI_BUS(d) == hose->first_busno + 1) {
  309. ls_pcie_cfg0_set_busdev(pcie, busdev);
  310. addr = pcie->va_cfg0 + (where & ~0x3);
  311. } else {
  312. ls_pcie_cfg1_set_busdev(pcie, busdev);
  313. addr = pcie->va_cfg1 + (where & ~0x3);
  314. }
  315. }
  316. writel(val, addr);
  317. return 0;
  318. }
  319. static void ls_pcie_setup_ctrl(struct ls_pcie *pcie,
  320. struct ls_pcie_info *info)
  321. {
  322. struct pci_controller *hose = &pcie->hose;
  323. pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
  324. ls_pcie_setup_atu(pcie, info);
  325. pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, 0);
  326. /* program correct class for RC */
  327. writel(1, pcie->dbi + PCIE_DBI_RO_WR_EN);
  328. pci_hose_write_config_word(hose, dev, PCI_CLASS_DEVICE,
  329. PCI_CLASS_BRIDGE_PCI);
  330. #ifndef CONFIG_LS102XA
  331. writel(0, pcie->dbi + PCIE_DBI_RO_WR_EN);
  332. #endif
  333. }
  334. static void ls_pcie_ep_setup_atu(struct ls_pcie *pcie,
  335. struct ls_pcie_info *info)
  336. {
  337. u64 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE;
  338. /* ATU 0 : INBOUND : map BAR0 */
  339. ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX0, 0, phys);
  340. /* ATU 1 : INBOUND : map BAR1 */
  341. phys += PCIE_BAR1_SIZE;
  342. ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX1, 1, phys);
  343. /* ATU 2 : INBOUND : map BAR2 */
  344. phys += PCIE_BAR2_SIZE;
  345. ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX2, 2, phys);
  346. /* ATU 3 : INBOUND : map BAR4 */
  347. phys = CONFIG_SYS_PCI_EP_MEMORY_BASE + PCIE_BAR4_SIZE;
  348. ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX3, 4, phys);
  349. /* ATU 0 : OUTBOUND : map 4G MEM */
  350. ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
  351. PCIE_ATU_TYPE_MEM,
  352. info->phys_base,
  353. 0,
  354. 4 * 1024 * 1024 * 1024ULL);
  355. }
  356. /* BAR0 and BAR1 are 32bit BAR2 and BAR4 are 64bit */
  357. static void ls_pcie_ep_setup_bar(void *bar_base, int bar, u32 size)
  358. {
  359. if (size < 4 * 1024)
  360. return;
  361. switch (bar) {
  362. case 0:
  363. writel(size - 1, bar_base + PCI_BASE_ADDRESS_0);
  364. break;
  365. case 1:
  366. writel(size - 1, bar_base + PCI_BASE_ADDRESS_1);
  367. break;
  368. case 2:
  369. writel(size - 1, bar_base + PCI_BASE_ADDRESS_2);
  370. writel(0, bar_base + PCI_BASE_ADDRESS_3);
  371. break;
  372. case 4:
  373. writel(size - 1, bar_base + PCI_BASE_ADDRESS_4);
  374. writel(0, bar_base + PCI_BASE_ADDRESS_5);
  375. break;
  376. default:
  377. break;
  378. }
  379. }
  380. static void ls_pcie_ep_setup_bars(void *bar_base)
  381. {
  382. /* BAR0 - 32bit - 4K configuration */
  383. ls_pcie_ep_setup_bar(bar_base, 0, PCIE_BAR0_SIZE);
  384. /* BAR1 - 32bit - 8K MSIX*/
  385. ls_pcie_ep_setup_bar(bar_base, 1, PCIE_BAR1_SIZE);
  386. /* BAR2 - 64bit - 4K MEM desciptor */
  387. ls_pcie_ep_setup_bar(bar_base, 2, PCIE_BAR2_SIZE);
  388. /* BAR4 - 64bit - 1M MEM*/
  389. ls_pcie_ep_setup_bar(bar_base, 4, PCIE_BAR4_SIZE);
  390. }
  391. static void ls_pcie_setup_ep(struct ls_pcie *pcie, struct ls_pcie_info *info)
  392. {
  393. struct pci_controller *hose = &pcie->hose;
  394. pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
  395. int sriov;
  396. sriov = pci_hose_find_ext_capability(hose, dev, PCI_EXT_CAP_ID_SRIOV);
  397. if (sriov) {
  398. int pf, vf;
  399. for (pf = 0; pf < PCIE_PF_NUM; pf++) {
  400. for (vf = 0; vf <= PCIE_VF_NUM; vf++) {
  401. writel(PCIE_LCTRL0_VAL(pf, vf),
  402. pcie->dbi + PCIE_LUT_BASE +
  403. PCIE_LUT_LCTRL0);
  404. ls_pcie_ep_setup_bars(pcie->dbi);
  405. ls_pcie_ep_setup_atu(pcie, info);
  406. }
  407. }
  408. /* Disable CFG2 */
  409. writel(0, pcie->dbi + PCIE_LUT_BASE + PCIE_LUT_LCTRL0);
  410. } else {
  411. ls_pcie_ep_setup_bars(pcie->dbi + PCIE_NO_SRIOV_BAR_BASE);
  412. ls_pcie_ep_setup_atu(pcie, info);
  413. }
  414. }
  415. int ls_pcie_init_ctrl(int busno, enum srds_prtcl dev, struct ls_pcie_info *info)
  416. {
  417. struct ls_pcie *pcie;
  418. struct pci_controller *hose;
  419. int num = dev - PCIE1;
  420. pci_dev_t pdev = PCI_BDF(busno, 0, 0);
  421. int i, linkup, ep_mode;
  422. u8 header_type;
  423. u16 temp16;
  424. if (!is_serdes_configured(dev)) {
  425. printf("PCIe%d: disabled\n", num + 1);
  426. return busno;
  427. }
  428. pcie = malloc(sizeof(*pcie));
  429. if (!pcie)
  430. return busno;
  431. memset(pcie, 0, sizeof(*pcie));
  432. hose = &pcie->hose;
  433. hose->priv_data = pcie;
  434. hose->first_busno = busno;
  435. pcie->idx = num;
  436. pcie->dbi = map_physmem(info->regs, PCIE_DBI_SIZE, MAP_NOCACHE);
  437. pcie->va_cfg0 = map_physmem(info->cfg0_phys,
  438. info->cfg0_size,
  439. MAP_NOCACHE);
  440. pcie->va_cfg1 = map_physmem(info->cfg1_phys,
  441. info->cfg1_size,
  442. MAP_NOCACHE);
  443. /* outbound memory */
  444. pci_set_region(&hose->regions[0],
  445. (pci_size_t)info->mem_bus,
  446. (phys_size_t)info->mem_phys,
  447. (pci_size_t)info->mem_size,
  448. PCI_REGION_MEM);
  449. /* outbound io */
  450. pci_set_region(&hose->regions[1],
  451. (pci_size_t)info->io_bus,
  452. (phys_size_t)info->io_phys,
  453. (pci_size_t)info->io_size,
  454. PCI_REGION_IO);
  455. /* System memory space */
  456. pci_set_region(&hose->regions[2],
  457. CONFIG_SYS_PCI_MEMORY_BUS,
  458. CONFIG_SYS_PCI_MEMORY_PHYS,
  459. CONFIG_SYS_PCI_MEMORY_SIZE,
  460. PCI_REGION_SYS_MEMORY);
  461. hose->region_count = 3;
  462. for (i = 0; i < hose->region_count; i++)
  463. debug("PCI reg:%d %016llx:%016llx %016llx %08lx\n",
  464. i,
  465. (u64)hose->regions[i].phys_start,
  466. (u64)hose->regions[i].bus_start,
  467. (u64)hose->regions[i].size,
  468. hose->regions[i].flags);
  469. pci_set_ops(hose,
  470. pci_hose_read_config_byte_via_dword,
  471. pci_hose_read_config_word_via_dword,
  472. ls_pcie_read_config,
  473. pci_hose_write_config_byte_via_dword,
  474. pci_hose_write_config_word_via_dword,
  475. ls_pcie_write_config);
  476. pci_hose_read_config_byte(hose, pdev, PCI_HEADER_TYPE, &header_type);
  477. ep_mode = (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
  478. printf("PCIe%u: %s ", info->pci_num,
  479. ep_mode ? "Endpoint" : "Root Complex");
  480. if (ep_mode)
  481. ls_pcie_setup_ep(pcie, info);
  482. else
  483. ls_pcie_setup_ctrl(pcie, info);
  484. linkup = ls_pcie_link_up(pcie);
  485. if (!linkup) {
  486. /* Let the user know there's no PCIe link */
  487. printf("no link, regs @ 0x%lx\n", info->regs);
  488. hose->last_busno = hose->first_busno;
  489. return busno;
  490. }
  491. /* Print the negotiated PCIe link width */
  492. pci_hose_read_config_word(hose, pdev, PCIE_LINK_STA, &temp16);
  493. printf("x%d gen%d, regs @ 0x%lx\n", (temp16 & 0x3f0) >> 4,
  494. (temp16 & 0xf), info->regs);
  495. if (ep_mode)
  496. return busno;
  497. pci_register_hose(hose);
  498. hose->last_busno = pci_hose_scan(hose);
  499. printf("PCIe%x: Bus %02x - %02x\n",
  500. info->pci_num, hose->first_busno, hose->last_busno);
  501. return hose->last_busno + 1;
  502. }
  503. int ls_pcie_init_board(int busno)
  504. {
  505. struct ls_pcie_info info;
  506. #ifdef CONFIG_PCIE1
  507. SET_LS_PCIE_INFO(info, 1);
  508. busno = ls_pcie_init_ctrl(busno, PCIE1, &info);
  509. #endif
  510. #ifdef CONFIG_PCIE2
  511. SET_LS_PCIE_INFO(info, 2);
  512. busno = ls_pcie_init_ctrl(busno, PCIE2, &info);
  513. #endif
  514. #ifdef CONFIG_PCIE3
  515. SET_LS_PCIE_INFO(info, 3);
  516. busno = ls_pcie_init_ctrl(busno, PCIE3, &info);
  517. #endif
  518. #ifdef CONFIG_PCIE4
  519. SET_LS_PCIE_INFO(info, 4);
  520. busno = ls_pcie_init_ctrl(busno, PCIE4, &info);
  521. #endif
  522. return busno;
  523. }
  524. void pci_init_board(void)
  525. {
  526. ls_pcie_init_board(0);
  527. }
  528. #ifdef CONFIG_OF_BOARD_SETUP
  529. #include <libfdt.h>
  530. #include <fdt_support.h>
  531. static void ft_pcie_ls_setup(void *blob, const char *pci_compat,
  532. unsigned long ctrl_addr, enum srds_prtcl dev)
  533. {
  534. int off;
  535. off = fdt_node_offset_by_compat_reg(blob, pci_compat,
  536. (phys_addr_t)ctrl_addr);
  537. if (off < 0)
  538. return;
  539. if (!is_serdes_configured(dev))
  540. fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
  541. }
  542. void ft_pci_setup(void *blob, bd_t *bd)
  543. {
  544. #ifdef CONFIG_PCIE1
  545. ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE1_ADDR, PCIE1);
  546. #endif
  547. #ifdef CONFIG_PCIE2
  548. ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE2_ADDR, PCIE2);
  549. #endif
  550. #ifdef CONFIG_PCIE3
  551. ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE3_ADDR, PCIE3);
  552. #endif
  553. #ifdef CONFIG_PCIE4
  554. ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE4_ADDR, PCIE4);
  555. #endif
  556. }
  557. #else
  558. void ft_pci_setup(void *blob, bd_t *bd)
  559. {
  560. }
  561. #endif
  562. #ifdef CONFIG_LS2085A
  563. void pcie_set_available_streamids(void *blob, const char *pcie_path,
  564. u32 *stream_ids, int count)
  565. {
  566. int nodeoffset;
  567. int i;
  568. nodeoffset = fdt_path_offset(blob, pcie_path);
  569. if (nodeoffset < 0) {
  570. printf("\n%s: ERROR: unable to update PCIe node\n", __func__);
  571. return;
  572. }
  573. /* for each stream ID, append to mmu-masters */
  574. for (i = 0; i < count; i++) {
  575. fdt_appendprop_u32(blob, nodeoffset, "available-stream-ids",
  576. stream_ids[i]);
  577. }
  578. }
  579. #define MAX_STREAM_IDS 4
  580. void fdt_fixup_smmu_pcie(void *blob)
  581. {
  582. int count;
  583. u32 stream_ids[MAX_STREAM_IDS];
  584. u32 ctlr_streamid = 0x300;
  585. #ifdef CONFIG_PCIE1
  586. /* PEX1 stream ID fixup */
  587. count = FSL_PEX1_STREAM_ID_END - FSL_PEX1_STREAM_ID_START + 1;
  588. alloc_stream_ids(FSL_PEX1_STREAM_ID_START, count, stream_ids,
  589. MAX_STREAM_IDS);
  590. pcie_set_available_streamids(blob, "/pcie@3400000", stream_ids, count);
  591. append_mmu_masters(blob, "/iommu@5000000", "/pcie@3400000",
  592. &ctlr_streamid, 1);
  593. #endif
  594. #ifdef CONFIG_PCIE2
  595. /* PEX2 stream ID fixup */
  596. count = FSL_PEX2_STREAM_ID_END - FSL_PEX2_STREAM_ID_START + 1;
  597. alloc_stream_ids(FSL_PEX2_STREAM_ID_START, count, stream_ids,
  598. MAX_STREAM_IDS);
  599. pcie_set_available_streamids(blob, "/pcie@3500000", stream_ids, count);
  600. append_mmu_masters(blob, "/iommu@5000000", "/pcie@3500000",
  601. &ctlr_streamid, 1);
  602. #endif
  603. #ifdef CONFIG_PCIE3
  604. /* PEX3 stream ID fixup */
  605. count = FSL_PEX3_STREAM_ID_END - FSL_PEX3_STREAM_ID_START + 1;
  606. alloc_stream_ids(FSL_PEX3_STREAM_ID_START, count, stream_ids,
  607. MAX_STREAM_IDS);
  608. pcie_set_available_streamids(blob, "/pcie@3600000", stream_ids, count);
  609. append_mmu_masters(blob, "/iommu@5000000", "/pcie@3600000",
  610. &ctlr_streamid, 1);
  611. #endif
  612. #ifdef CONFIG_PCIE4
  613. /* PEX4 stream ID fixup */
  614. count = FSL_PEX4_STREAM_ID_END - FSL_PEX4_STREAM_ID_START + 1;
  615. alloc_stream_ids(FSL_PEX4_STREAM_ID_START, count, stream_ids,
  616. MAX_STREAM_IDS);
  617. pcie_set_available_streamids(blob, "/pcie@3700000", stream_ids, count);
  618. append_mmu_masters(blob, "/iommu@5000000", "/pcie@3700000",
  619. &ctlr_streamid, 1);
  620. #endif
  621. }
  622. #endif