pcie_layerscape.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. #ifndef CONFIG_LS102XA
  14. #include <asm/arch/fdt.h>
  15. #include <asm/arch/soc.h>
  16. #endif
  17. #ifndef CONFIG_SYS_PCI_MEMORY_BUS
  18. #define CONFIG_SYS_PCI_MEMORY_BUS CONFIG_SYS_SDRAM_BASE
  19. #endif
  20. #ifndef CONFIG_SYS_PCI_MEMORY_PHYS
  21. #define CONFIG_SYS_PCI_MEMORY_PHYS CONFIG_SYS_SDRAM_BASE
  22. #endif
  23. #ifndef CONFIG_SYS_PCI_MEMORY_SIZE
  24. #define CONFIG_SYS_PCI_MEMORY_SIZE (2 * 1024 * 1024 * 1024UL) /* 2G */
  25. #endif
  26. #ifndef CONFIG_SYS_PCI_EP_MEMORY_BASE
  27. #define CONFIG_SYS_PCI_EP_MEMORY_BASE CONFIG_SYS_LOAD_ADDR
  28. #endif
  29. /* iATU registers */
  30. #define PCIE_ATU_VIEWPORT 0x900
  31. #define PCIE_ATU_REGION_INBOUND (0x1 << 31)
  32. #define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
  33. #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
  34. #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
  35. #define PCIE_ATU_REGION_INDEX2 (0x2 << 0)
  36. #define PCIE_ATU_REGION_INDEX3 (0x3 << 0)
  37. #define PCIE_ATU_CR1 0x904
  38. #define PCIE_ATU_TYPE_MEM (0x0 << 0)
  39. #define PCIE_ATU_TYPE_IO (0x2 << 0)
  40. #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
  41. #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
  42. #define PCIE_ATU_CR2 0x908
  43. #define PCIE_ATU_ENABLE (0x1 << 31)
  44. #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
  45. #define PCIE_ATU_BAR_NUM(bar) ((bar) << 8)
  46. #define PCIE_ATU_LOWER_BASE 0x90C
  47. #define PCIE_ATU_UPPER_BASE 0x910
  48. #define PCIE_ATU_LIMIT 0x914
  49. #define PCIE_ATU_LOWER_TARGET 0x918
  50. #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
  51. #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
  52. #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
  53. #define PCIE_ATU_UPPER_TARGET 0x91C
  54. #define PCIE_DBI_RO_WR_EN 0x8bc
  55. #define PCIE_LINK_CAP 0x7c
  56. #define PCIE_LINK_SPEED_MASK 0xf
  57. #define PCIE_LINK_STA 0x82
  58. #define LTSSM_STATE_MASK 0x3f
  59. #define LTSSM_PCIE_L0 0x11 /* L0 state */
  60. #define PCIE_DBI_SIZE 0x100000 /* 1M */
  61. #define PCIE_LCTRL0_CFG2_ENABLE (1 << 31)
  62. #define PCIE_LCTRL0_VF(vf) ((vf) << 22)
  63. #define PCIE_LCTRL0_PF(pf) ((pf) << 16)
  64. #define PCIE_LCTRL0_VF_ACTIVE (1 << 21)
  65. #define PCIE_LCTRL0_VAL(pf, vf) (PCIE_LCTRL0_PF(pf) | \
  66. PCIE_LCTRL0_VF(vf) | \
  67. ((vf) == 0 ? 0 : PCIE_LCTRL0_VF_ACTIVE) | \
  68. PCIE_LCTRL0_CFG2_ENABLE)
  69. #define PCIE_NO_SRIOV_BAR_BASE 0x1000
  70. #define PCIE_PF_NUM 2
  71. #define PCIE_VF_NUM 64
  72. #define PCIE_BAR0_SIZE (4 * 1024) /* 4K */
  73. #define PCIE_BAR1_SIZE (8 * 1024) /* 8K for MSIX */
  74. #define PCIE_BAR2_SIZE (4 * 1024) /* 4K */
  75. #define PCIE_BAR4_SIZE (1 * 1024 * 1024) /* 1M */
  76. struct ls_pcie {
  77. int idx;
  78. void __iomem *dbi;
  79. void __iomem *va_cfg0;
  80. void __iomem *va_cfg1;
  81. int next_lut_index;
  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 = pex_lut_in32(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 0;
  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. #ifndef CONFIG_LS102XA
  402. writel(PCIE_LCTRL0_VAL(pf, vf),
  403. pcie->dbi + PCIE_LUT_BASE +
  404. PCIE_LUT_LCTRL0);
  405. #endif
  406. ls_pcie_ep_setup_bars(pcie->dbi);
  407. ls_pcie_ep_setup_atu(pcie, info);
  408. }
  409. }
  410. /* Disable CFG2 */
  411. #ifndef CONFIG_LS102XA
  412. writel(0, pcie->dbi + PCIE_LUT_BASE + PCIE_LUT_LCTRL0);
  413. #endif
  414. } else {
  415. ls_pcie_ep_setup_bars(pcie->dbi + PCIE_NO_SRIOV_BAR_BASE);
  416. ls_pcie_ep_setup_atu(pcie, info);
  417. }
  418. }
  419. #ifdef CONFIG_FSL_LSCH3
  420. /*
  421. * Return next available LUT index.
  422. */
  423. static int ls_pcie_next_lut_index(struct ls_pcie *pcie)
  424. {
  425. if (pcie->next_lut_index < PCIE_LUT_ENTRY_COUNT)
  426. return pcie->next_lut_index++;
  427. else
  428. return -1; /* LUT is full */
  429. }
  430. /*
  431. * Program a single LUT entry
  432. */
  433. static void ls_pcie_lut_set_mapping(struct ls_pcie *pcie, int index, u32 devid,
  434. u32 streamid)
  435. {
  436. void __iomem *lut;
  437. lut = pcie->dbi + PCIE_LUT_BASE;
  438. /* leave mask as all zeroes, want to match all bits */
  439. writel((devid << 16), lut + PCIE_LUT_UDR(index));
  440. writel(streamid | PCIE_LUT_ENABLE, lut + PCIE_LUT_LDR(index));
  441. }
  442. /* returns the next available streamid */
  443. static u32 ls_pcie_next_streamid(void)
  444. {
  445. static int next_stream_id = FSL_PEX_STREAM_ID_START;
  446. if (next_stream_id > FSL_PEX_STREAM_ID_END)
  447. return 0xffffffff;
  448. return next_stream_id++;
  449. }
  450. /*
  451. * An msi-map is a property to be added to the pci controller
  452. * node. It is a table, where each entry consists of 4 fields
  453. * e.g.:
  454. *
  455. * msi-map = <[devid] [phandle-to-msi-ctrl] [stream-id] [count]
  456. * [devid] [phandle-to-msi-ctrl] [stream-id] [count]>;
  457. */
  458. static void fdt_pcie_set_msi_map_entry(void *blob, struct ls_pcie *pcie,
  459. u32 devid, u32 streamid)
  460. {
  461. char pcie_path[19];
  462. u32 *prop;
  463. u32 phandle;
  464. int nodeoffset;
  465. /* find pci controller node */
  466. snprintf(pcie_path, sizeof(pcie_path), "/soc/pcie@%llx",
  467. (u64)pcie->dbi);
  468. nodeoffset = fdt_path_offset(blob, pcie_path);
  469. if (nodeoffset < 0) {
  470. printf("\n%s: ERROR: unable to update PCIe node: %s\n",
  471. __func__, pcie_path);
  472. return;
  473. }
  474. /* get phandle to MSI controller */
  475. prop = (u32 *)fdt_getprop(blob, nodeoffset, "msi-parent", 0);
  476. if (prop == NULL) {
  477. printf("\n%s: ERROR: missing msi-parent: %s\n", __func__,
  478. pcie_path);
  479. return;
  480. }
  481. phandle = be32_to_cpu(*prop);
  482. /* set one msi-map row */
  483. fdt_appendprop_u32(blob, nodeoffset, "msi-map", devid);
  484. fdt_appendprop_u32(blob, nodeoffset, "msi-map", phandle);
  485. fdt_appendprop_u32(blob, nodeoffset, "msi-map", streamid);
  486. fdt_appendprop_u32(blob, nodeoffset, "msi-map", 1);
  487. }
  488. static void fdt_fixup_pcie(void *blob)
  489. {
  490. unsigned int found_multi = 0;
  491. unsigned char header_type;
  492. int index;
  493. u32 streamid;
  494. pci_dev_t dev, bdf;
  495. int bus;
  496. unsigned short id;
  497. struct pci_controller *hose;
  498. struct ls_pcie *pcie;
  499. int i;
  500. for (i = 0, hose = pci_get_hose_head(); hose; hose = hose->next, i++) {
  501. pcie = hose->priv_data;
  502. for (bus = hose->first_busno; bus <= hose->last_busno; bus++) {
  503. for (dev = PCI_BDF(bus, 0, 0);
  504. dev < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1,
  505. PCI_MAX_PCI_FUNCTIONS - 1);
  506. dev += PCI_BDF(0, 0, 1)) {
  507. if (PCI_FUNC(dev) && !found_multi)
  508. continue;
  509. pci_read_config_word(dev, PCI_VENDOR_ID, &id);
  510. pci_read_config_byte(dev, PCI_HEADER_TYPE,
  511. &header_type);
  512. if ((id == 0xFFFF) || (id == 0x0000))
  513. continue;
  514. if (!PCI_FUNC(dev))
  515. found_multi = header_type & 0x80;
  516. streamid = ls_pcie_next_streamid();
  517. if (streamid == 0xffffffff) {
  518. printf("ERROR: no stream ids free\n");
  519. continue;
  520. }
  521. index = ls_pcie_next_lut_index(pcie);
  522. if (index < 0) {
  523. printf("ERROR: no LUT indexes free\n");
  524. continue;
  525. }
  526. /* the DT fixup must be relative to the hose first_busno */
  527. bdf = dev - PCI_BDF(hose->first_busno, 0, 0);
  528. /* map PCI b.d.f to streamID in LUT */
  529. ls_pcie_lut_set_mapping(pcie, index, bdf >> 8,
  530. streamid);
  531. /* update msi-map in device tree */
  532. fdt_pcie_set_msi_map_entry(blob, pcie, bdf >> 8,
  533. streamid);
  534. }
  535. }
  536. }
  537. }
  538. #endif
  539. int ls_pcie_init_ctrl(int busno, enum srds_prtcl dev, struct ls_pcie_info *info)
  540. {
  541. struct ls_pcie *pcie;
  542. struct pci_controller *hose;
  543. int num = dev - PCIE1;
  544. pci_dev_t pdev = PCI_BDF(busno, 0, 0);
  545. int i, linkup, ep_mode;
  546. u8 header_type;
  547. u16 temp16;
  548. if (!is_serdes_configured(dev)) {
  549. printf("PCIe%d: disabled\n", num + 1);
  550. return busno;
  551. }
  552. pcie = malloc(sizeof(*pcie));
  553. if (!pcie)
  554. return busno;
  555. memset(pcie, 0, sizeof(*pcie));
  556. hose = &pcie->hose;
  557. hose->priv_data = pcie;
  558. hose->first_busno = busno;
  559. pcie->idx = num;
  560. pcie->dbi = map_physmem(info->regs, PCIE_DBI_SIZE, MAP_NOCACHE);
  561. pcie->va_cfg0 = map_physmem(info->cfg0_phys,
  562. info->cfg0_size,
  563. MAP_NOCACHE);
  564. pcie->va_cfg1 = map_physmem(info->cfg1_phys,
  565. info->cfg1_size,
  566. MAP_NOCACHE);
  567. pcie->next_lut_index = 0;
  568. /* outbound memory */
  569. pci_set_region(&hose->regions[0],
  570. (pci_size_t)info->mem_bus,
  571. (phys_size_t)info->mem_phys,
  572. (pci_size_t)info->mem_size,
  573. PCI_REGION_MEM);
  574. /* outbound io */
  575. pci_set_region(&hose->regions[1],
  576. (pci_size_t)info->io_bus,
  577. (phys_size_t)info->io_phys,
  578. (pci_size_t)info->io_size,
  579. PCI_REGION_IO);
  580. /* System memory space */
  581. pci_set_region(&hose->regions[2],
  582. CONFIG_SYS_PCI_MEMORY_BUS,
  583. CONFIG_SYS_PCI_MEMORY_PHYS,
  584. CONFIG_SYS_PCI_MEMORY_SIZE,
  585. PCI_REGION_SYS_MEMORY);
  586. hose->region_count = 3;
  587. for (i = 0; i < hose->region_count; i++)
  588. debug("PCI reg:%d %016llx:%016llx %016llx %08lx\n",
  589. i,
  590. (u64)hose->regions[i].phys_start,
  591. (u64)hose->regions[i].bus_start,
  592. (u64)hose->regions[i].size,
  593. hose->regions[i].flags);
  594. pci_set_ops(hose,
  595. pci_hose_read_config_byte_via_dword,
  596. pci_hose_read_config_word_via_dword,
  597. ls_pcie_read_config,
  598. pci_hose_write_config_byte_via_dword,
  599. pci_hose_write_config_word_via_dword,
  600. ls_pcie_write_config);
  601. pci_hose_read_config_byte(hose, pdev, PCI_HEADER_TYPE, &header_type);
  602. ep_mode = (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
  603. printf("PCIe%u: %s ", info->pci_num,
  604. ep_mode ? "Endpoint" : "Root Complex");
  605. if (ep_mode)
  606. ls_pcie_setup_ep(pcie, info);
  607. else
  608. ls_pcie_setup_ctrl(pcie, info);
  609. linkup = ls_pcie_link_up(pcie);
  610. if (!linkup) {
  611. /* Let the user know there's no PCIe link */
  612. printf("no link, regs @ 0x%lx\n", info->regs);
  613. hose->last_busno = hose->first_busno;
  614. return busno;
  615. }
  616. /* Print the negotiated PCIe link width */
  617. pci_hose_read_config_word(hose, pdev, PCIE_LINK_STA, &temp16);
  618. printf("x%d gen%d, regs @ 0x%lx\n", (temp16 & 0x3f0) >> 4,
  619. (temp16 & 0xf), info->regs);
  620. if (ep_mode)
  621. return busno;
  622. pci_register_hose(hose);
  623. hose->last_busno = pci_hose_scan(hose);
  624. printf("PCIe%x: Bus %02x - %02x\n",
  625. info->pci_num, hose->first_busno, hose->last_busno);
  626. return hose->last_busno + 1;
  627. }
  628. int ls_pcie_init_board(int busno)
  629. {
  630. struct ls_pcie_info info;
  631. #ifdef CONFIG_PCIE1
  632. SET_LS_PCIE_INFO(info, 1);
  633. busno = ls_pcie_init_ctrl(busno, PCIE1, &info);
  634. #endif
  635. #ifdef CONFIG_PCIE2
  636. SET_LS_PCIE_INFO(info, 2);
  637. busno = ls_pcie_init_ctrl(busno, PCIE2, &info);
  638. #endif
  639. #ifdef CONFIG_PCIE3
  640. SET_LS_PCIE_INFO(info, 3);
  641. busno = ls_pcie_init_ctrl(busno, PCIE3, &info);
  642. #endif
  643. #ifdef CONFIG_PCIE4
  644. SET_LS_PCIE_INFO(info, 4);
  645. busno = ls_pcie_init_ctrl(busno, PCIE4, &info);
  646. #endif
  647. return busno;
  648. }
  649. void pci_init_board(void)
  650. {
  651. ls_pcie_init_board(0);
  652. }
  653. #ifdef CONFIG_OF_BOARD_SETUP
  654. #include <libfdt.h>
  655. #include <fdt_support.h>
  656. static void ft_pcie_ls_setup(void *blob, const char *pci_compat,
  657. unsigned long ctrl_addr, enum srds_prtcl dev)
  658. {
  659. int off;
  660. off = fdt_node_offset_by_compat_reg(blob, pci_compat,
  661. (phys_addr_t)ctrl_addr);
  662. if (off < 0)
  663. return;
  664. if (!is_serdes_configured(dev))
  665. fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
  666. }
  667. void ft_pci_setup(void *blob, bd_t *bd)
  668. {
  669. #ifdef CONFIG_PCIE1
  670. ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE1_ADDR, PCIE1);
  671. #endif
  672. #ifdef CONFIG_PCIE2
  673. ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE2_ADDR, PCIE2);
  674. #endif
  675. #ifdef CONFIG_PCIE3
  676. ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE3_ADDR, PCIE3);
  677. #endif
  678. #ifdef CONFIG_PCIE4
  679. ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE4_ADDR, PCIE4);
  680. #endif
  681. #ifdef CONFIG_FSL_LSCH3
  682. fdt_fixup_pcie(blob);
  683. #endif
  684. }
  685. #else
  686. void ft_pci_setup(void *blob, bd_t *bd)
  687. {
  688. }
  689. #endif