pcie_layerscape.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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_SYS_PCI_MEMORY_BUS
  14. #define CONFIG_SYS_PCI_MEMORY_BUS CONFIG_SYS_SDRAM_BASE
  15. #endif
  16. #ifndef CONFIG_SYS_PCI_MEMORY_PHYS
  17. #define CONFIG_SYS_PCI_MEMORY_PHYS CONFIG_SYS_SDRAM_BASE
  18. #endif
  19. #ifndef CONFIG_SYS_PCI_MEMORY_SIZE
  20. #define CONFIG_SYS_PCI_MEMORY_SIZE (2 * 1024 * 1024 * 1024UL) /* 2G */
  21. #endif
  22. /* iATU registers */
  23. #define PCIE_ATU_VIEWPORT 0x900
  24. #define PCIE_ATU_REGION_INBOUND (0x1 << 31)
  25. #define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
  26. #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
  27. #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
  28. #define PCIE_ATU_REGION_INDEX2 (0x2 << 0)
  29. #define PCIE_ATU_REGION_INDEX3 (0x3 << 0)
  30. #define PCIE_ATU_CR1 0x904
  31. #define PCIE_ATU_TYPE_MEM (0x0 << 0)
  32. #define PCIE_ATU_TYPE_IO (0x2 << 0)
  33. #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
  34. #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
  35. #define PCIE_ATU_CR2 0x908
  36. #define PCIE_ATU_ENABLE (0x1 << 31)
  37. #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
  38. #define PCIE_ATU_LOWER_BASE 0x90C
  39. #define PCIE_ATU_UPPER_BASE 0x910
  40. #define PCIE_ATU_LIMIT 0x914
  41. #define PCIE_ATU_LOWER_TARGET 0x918
  42. #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
  43. #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
  44. #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
  45. #define PCIE_ATU_UPPER_TARGET 0x91C
  46. /* LUT registers */
  47. #define PCIE_LUT_BASE 0x80000
  48. #define PCIE_LUT_DBG 0x7FC
  49. #define PCIE_DBI_RO_WR_EN 0x8bc
  50. #define PCIE_LINK_CAP 0x7c
  51. #define PCIE_LINK_SPEED_MASK 0xf
  52. #define PCIE_LINK_STA 0x82
  53. #define LTSSM_STATE_MASK 0x3f
  54. #define LTSSM_PCIE_L0 0x11 /* L0 state */
  55. #define PCIE_DBI_SIZE 0x100000 /* 1M */
  56. struct ls_pcie {
  57. int idx;
  58. void __iomem *dbi;
  59. void __iomem *va_cfg0;
  60. void __iomem *va_cfg1;
  61. struct pci_controller hose;
  62. };
  63. struct ls_pcie_info {
  64. unsigned long regs;
  65. int pci_num;
  66. u64 cfg0_phys;
  67. u64 cfg0_size;
  68. u64 cfg1_phys;
  69. u64 cfg1_size;
  70. u64 mem_bus;
  71. u64 mem_phys;
  72. u64 mem_size;
  73. u64 io_bus;
  74. u64 io_phys;
  75. u64 io_size;
  76. };
  77. #define SET_LS_PCIE_INFO(x, num) \
  78. { \
  79. x.regs = CONFIG_SYS_PCIE##num##_ADDR; \
  80. x.cfg0_phys = CONFIG_SYS_PCIE_CFG0_PHYS_OFF + \
  81. CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
  82. x.cfg0_size = CONFIG_SYS_PCIE_CFG0_SIZE; \
  83. x.cfg1_phys = CONFIG_SYS_PCIE_CFG1_PHYS_OFF + \
  84. CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
  85. x.cfg1_size = CONFIG_SYS_PCIE_CFG1_SIZE; \
  86. x.mem_bus = CONFIG_SYS_PCIE_MEM_BUS; \
  87. x.mem_phys = CONFIG_SYS_PCIE_MEM_PHYS_OFF + \
  88. CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
  89. x.mem_size = CONFIG_SYS_PCIE_MEM_SIZE; \
  90. x.io_bus = CONFIG_SYS_PCIE_IO_BUS; \
  91. x.io_phys = CONFIG_SYS_PCIE_IO_PHYS_OFF + \
  92. CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
  93. x.io_size = CONFIG_SYS_PCIE_IO_SIZE; \
  94. x.pci_num = num; \
  95. }
  96. #ifdef CONFIG_LS102XA
  97. #include <asm/arch/immap_ls102xa.h>
  98. /* PEX1/2 Misc Ports Status Register */
  99. #define LTSSM_STATE_SHIFT 20
  100. static int ls_pcie_link_state(struct ls_pcie *pcie)
  101. {
  102. u32 state;
  103. struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_FSL_SCFG_ADDR;
  104. state = in_be32(&scfg->pexmscportsr[pcie->idx]);
  105. state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
  106. if (state < LTSSM_PCIE_L0) {
  107. debug("....PCIe link error. LTSSM=0x%02x.\n", state);
  108. return 0;
  109. }
  110. return 1;
  111. }
  112. #else
  113. static int ls_pcie_link_state(struct ls_pcie *pcie)
  114. {
  115. u32 state;
  116. state = readl(pcie->dbi + PCIE_LUT_BASE + PCIE_LUT_DBG) &
  117. LTSSM_STATE_MASK;
  118. if (state < LTSSM_PCIE_L0) {
  119. debug("....PCIe link error. LTSSM=0x%02x.\n", state);
  120. return 0;
  121. }
  122. return 1;
  123. }
  124. #endif
  125. static int ls_pcie_link_up(struct ls_pcie *pcie)
  126. {
  127. int state;
  128. u32 cap;
  129. state = ls_pcie_link_state(pcie);
  130. if (state)
  131. return state;
  132. /* Try to download speed to gen1 */
  133. cap = readl(pcie->dbi + PCIE_LINK_CAP);
  134. writel((cap & (~PCIE_LINK_SPEED_MASK)) | 1, pcie->dbi + PCIE_LINK_CAP);
  135. /*
  136. * Notice: the following delay has critical impact on link training
  137. * if too short (<30ms) the link doesn't get up.
  138. */
  139. mdelay(100);
  140. state = ls_pcie_link_state(pcie);
  141. if (state)
  142. return state;
  143. writel(cap, pcie->dbi + PCIE_LINK_CAP);
  144. return 0;
  145. }
  146. static void ls_pcie_cfg0_set_busdev(struct ls_pcie *pcie, u32 busdev)
  147. {
  148. writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
  149. pcie->dbi + PCIE_ATU_VIEWPORT);
  150. writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
  151. }
  152. static void ls_pcie_cfg1_set_busdev(struct ls_pcie *pcie, u32 busdev)
  153. {
  154. writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
  155. pcie->dbi + PCIE_ATU_VIEWPORT);
  156. writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
  157. }
  158. static void ls_pcie_iatu_outbound_set(struct ls_pcie *pcie, int idx, int type,
  159. u64 phys, u64 bus_addr, pci_size_t size)
  160. {
  161. writel(PCIE_ATU_REGION_OUTBOUND | idx, pcie->dbi + PCIE_ATU_VIEWPORT);
  162. writel((u32)phys, pcie->dbi + PCIE_ATU_LOWER_BASE);
  163. writel(phys >> 32, pcie->dbi + PCIE_ATU_UPPER_BASE);
  164. writel(phys + size - 1, pcie->dbi + PCIE_ATU_LIMIT);
  165. writel((u32)bus_addr, pcie->dbi + PCIE_ATU_LOWER_TARGET);
  166. writel(bus_addr >> 32, pcie->dbi + PCIE_ATU_UPPER_TARGET);
  167. writel(type, pcie->dbi + PCIE_ATU_CR1);
  168. writel(PCIE_ATU_ENABLE, pcie->dbi + PCIE_ATU_CR2);
  169. }
  170. static void ls_pcie_setup_atu(struct ls_pcie *pcie, struct ls_pcie_info *info)
  171. {
  172. #ifdef DEBUG
  173. int i;
  174. #endif
  175. /* ATU 0 : OUTBOUND : CFG0 */
  176. ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
  177. PCIE_ATU_TYPE_CFG0,
  178. info->cfg0_phys,
  179. 0,
  180. info->cfg0_size);
  181. /* ATU 1 : OUTBOUND : CFG1 */
  182. ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1,
  183. PCIE_ATU_TYPE_CFG1,
  184. info->cfg1_phys,
  185. 0,
  186. info->cfg1_size);
  187. /* ATU 2 : OUTBOUND : MEM */
  188. ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX2,
  189. PCIE_ATU_TYPE_MEM,
  190. info->mem_phys,
  191. info->mem_bus,
  192. info->mem_size);
  193. /* ATU 3 : OUTBOUND : IO */
  194. ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX3,
  195. PCIE_ATU_TYPE_IO,
  196. info->io_phys,
  197. info->io_bus,
  198. info->io_size);
  199. #ifdef DEBUG
  200. for (i = 0; i <= PCIE_ATU_REGION_INDEX3; i++) {
  201. writel(PCIE_ATU_REGION_OUTBOUND | i,
  202. pcie->dbi + PCIE_ATU_VIEWPORT);
  203. debug("iATU%d:\n", i);
  204. debug("\tLOWER PHYS 0x%08x\n",
  205. readl(pcie->dbi + PCIE_ATU_LOWER_BASE));
  206. debug("\tUPPER PHYS 0x%08x\n",
  207. readl(pcie->dbi + PCIE_ATU_UPPER_BASE));
  208. debug("\tLOWER BUS 0x%08x\n",
  209. readl(pcie->dbi + PCIE_ATU_LOWER_TARGET));
  210. debug("\tUPPER BUS 0x%08x\n",
  211. readl(pcie->dbi + PCIE_ATU_UPPER_TARGET));
  212. debug("\tLIMIT 0x%08x\n",
  213. readl(pcie->dbi + PCIE_ATU_LIMIT));
  214. debug("\tCR1 0x%08x\n",
  215. readl(pcie->dbi + PCIE_ATU_CR1));
  216. debug("\tCR2 0x%08x\n",
  217. readl(pcie->dbi + PCIE_ATU_CR2));
  218. }
  219. #endif
  220. }
  221. int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
  222. {
  223. /* Do not skip controller */
  224. return 0;
  225. }
  226. static int ls_pcie_addr_valid(struct pci_controller *hose, pci_dev_t d)
  227. {
  228. if (PCI_DEV(d) > 0)
  229. return -EINVAL;
  230. /* Controller does not support multi-function in RC mode */
  231. if ((PCI_BUS(d) == hose->first_busno) && (PCI_FUNC(d) > 0))
  232. return -EINVAL;
  233. return 0;
  234. }
  235. static int ls_pcie_read_config(struct pci_controller *hose, pci_dev_t d,
  236. int where, u32 *val)
  237. {
  238. struct ls_pcie *pcie = hose->priv_data;
  239. u32 busdev, *addr;
  240. if (ls_pcie_addr_valid(hose, d)) {
  241. *val = 0xffffffff;
  242. return -EINVAL;
  243. }
  244. if (PCI_BUS(d) == hose->first_busno) {
  245. addr = pcie->dbi + (where & ~0x3);
  246. } else {
  247. busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
  248. PCIE_ATU_DEV(PCI_DEV(d)) |
  249. PCIE_ATU_FUNC(PCI_FUNC(d));
  250. if (PCI_BUS(d) == hose->first_busno + 1) {
  251. ls_pcie_cfg0_set_busdev(pcie, busdev);
  252. addr = pcie->va_cfg0 + (where & ~0x3);
  253. } else {
  254. ls_pcie_cfg1_set_busdev(pcie, busdev);
  255. addr = pcie->va_cfg1 + (where & ~0x3);
  256. }
  257. }
  258. *val = readl(addr);
  259. return 0;
  260. }
  261. static int ls_pcie_write_config(struct pci_controller *hose, pci_dev_t d,
  262. int where, u32 val)
  263. {
  264. struct ls_pcie *pcie = hose->priv_data;
  265. u32 busdev, *addr;
  266. if (ls_pcie_addr_valid(hose, d))
  267. return -EINVAL;
  268. if (PCI_BUS(d) == hose->first_busno) {
  269. addr = pcie->dbi + (where & ~0x3);
  270. } else {
  271. busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
  272. PCIE_ATU_DEV(PCI_DEV(d)) |
  273. PCIE_ATU_FUNC(PCI_FUNC(d));
  274. if (PCI_BUS(d) == hose->first_busno + 1) {
  275. ls_pcie_cfg0_set_busdev(pcie, busdev);
  276. addr = pcie->va_cfg0 + (where & ~0x3);
  277. } else {
  278. ls_pcie_cfg1_set_busdev(pcie, busdev);
  279. addr = pcie->va_cfg1 + (where & ~0x3);
  280. }
  281. }
  282. writel(val, addr);
  283. return 0;
  284. }
  285. static void ls_pcie_setup_ctrl(struct ls_pcie *pcie,
  286. struct ls_pcie_info *info)
  287. {
  288. struct pci_controller *hose = &pcie->hose;
  289. pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
  290. ls_pcie_setup_atu(pcie, info);
  291. pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, 0);
  292. /* program correct class for RC */
  293. writel(1, pcie->dbi + PCIE_DBI_RO_WR_EN);
  294. pci_hose_write_config_word(hose, dev, PCI_CLASS_DEVICE,
  295. PCI_CLASS_BRIDGE_PCI);
  296. #ifndef CONFIG_LS102XA
  297. writel(0, pcie->dbi + PCIE_DBI_RO_WR_EN);
  298. #endif
  299. }
  300. int ls_pcie_init_ctrl(int busno, enum srds_prtcl dev, struct ls_pcie_info *info)
  301. {
  302. struct ls_pcie *pcie;
  303. struct pci_controller *hose;
  304. int num = dev - PCIE1;
  305. pci_dev_t pdev = PCI_BDF(busno, 0, 0);
  306. int i, linkup, ep_mode;
  307. u8 header_type;
  308. u16 temp16;
  309. if (!is_serdes_configured(dev)) {
  310. printf("PCIe%d: disabled\n", num + 1);
  311. return busno;
  312. }
  313. pcie = malloc(sizeof(*pcie));
  314. if (!pcie)
  315. return busno;
  316. memset(pcie, 0, sizeof(*pcie));
  317. hose = &pcie->hose;
  318. hose->priv_data = pcie;
  319. hose->first_busno = busno;
  320. pcie->idx = num;
  321. pcie->dbi = map_physmem(info->regs, PCIE_DBI_SIZE, MAP_NOCACHE);
  322. pcie->va_cfg0 = map_physmem(info->cfg0_phys,
  323. info->cfg0_size,
  324. MAP_NOCACHE);
  325. pcie->va_cfg1 = map_physmem(info->cfg1_phys,
  326. info->cfg1_size,
  327. MAP_NOCACHE);
  328. /* outbound memory */
  329. pci_set_region(&hose->regions[0],
  330. (pci_size_t)info->mem_bus,
  331. (phys_size_t)info->mem_phys,
  332. (pci_size_t)info->mem_size,
  333. PCI_REGION_MEM);
  334. /* outbound io */
  335. pci_set_region(&hose->regions[1],
  336. (pci_size_t)info->io_bus,
  337. (phys_size_t)info->io_phys,
  338. (pci_size_t)info->io_size,
  339. PCI_REGION_IO);
  340. /* System memory space */
  341. pci_set_region(&hose->regions[2],
  342. CONFIG_SYS_PCI_MEMORY_BUS,
  343. CONFIG_SYS_PCI_MEMORY_PHYS,
  344. CONFIG_SYS_PCI_MEMORY_SIZE,
  345. PCI_REGION_SYS_MEMORY);
  346. hose->region_count = 3;
  347. for (i = 0; i < hose->region_count; i++)
  348. debug("PCI reg:%d %016llx:%016llx %016llx %08lx\n",
  349. i,
  350. (u64)hose->regions[i].phys_start,
  351. (u64)hose->regions[i].bus_start,
  352. (u64)hose->regions[i].size,
  353. hose->regions[i].flags);
  354. pci_set_ops(hose,
  355. pci_hose_read_config_byte_via_dword,
  356. pci_hose_read_config_word_via_dword,
  357. ls_pcie_read_config,
  358. pci_hose_write_config_byte_via_dword,
  359. pci_hose_write_config_word_via_dword,
  360. ls_pcie_write_config);
  361. pci_hose_read_config_byte(hose, pdev, PCI_HEADER_TYPE, &header_type);
  362. ep_mode = (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
  363. printf("PCIe%u: %s ", info->pci_num,
  364. ep_mode ? "Endpoint" : "Root Complex");
  365. linkup = ls_pcie_link_up(pcie);
  366. if (!linkup) {
  367. /* Let the user know there's no PCIe link */
  368. printf("no link, regs @ 0x%lx\n", info->regs);
  369. hose->last_busno = hose->first_busno;
  370. return busno;
  371. }
  372. /* Print the negotiated PCIe link width */
  373. pci_hose_read_config_word(hose, pdev, PCIE_LINK_STA, &temp16);
  374. printf("x%d gen%d, regs @ 0x%lx\n", (temp16 & 0x3f0) >> 4,
  375. (temp16 & 0xf), info->regs);
  376. if (ep_mode)
  377. return busno;
  378. ls_pcie_setup_ctrl(pcie, info);
  379. pci_register_hose(hose);
  380. hose->last_busno = pci_hose_scan(hose);
  381. printf("PCIe%x: Bus %02x - %02x\n",
  382. info->pci_num, hose->first_busno, hose->last_busno);
  383. return hose->last_busno + 1;
  384. }
  385. int ls_pcie_init_board(int busno)
  386. {
  387. struct ls_pcie_info info;
  388. #ifdef CONFIG_PCIE1
  389. SET_LS_PCIE_INFO(info, 1);
  390. busno = ls_pcie_init_ctrl(busno, PCIE1, &info);
  391. #endif
  392. #ifdef CONFIG_PCIE2
  393. SET_LS_PCIE_INFO(info, 2);
  394. busno = ls_pcie_init_ctrl(busno, PCIE2, &info);
  395. #endif
  396. #ifdef CONFIG_PCIE3
  397. SET_LS_PCIE_INFO(info, 3);
  398. busno = ls_pcie_init_ctrl(busno, PCIE3, &info);
  399. #endif
  400. #ifdef CONFIG_PCIE4
  401. SET_LS_PCIE_INFO(info, 4);
  402. busno = ls_pcie_init_ctrl(busno, PCIE4, &info);
  403. #endif
  404. return busno;
  405. }
  406. void pci_init_board(void)
  407. {
  408. ls_pcie_init_board(0);
  409. }
  410. #ifdef CONFIG_OF_BOARD_SETUP
  411. #include <libfdt.h>
  412. #include <fdt_support.h>
  413. static void ft_pcie_ls_setup(void *blob, const char *pci_compat,
  414. unsigned long ctrl_addr, enum srds_prtcl dev)
  415. {
  416. int off;
  417. off = fdt_node_offset_by_compat_reg(blob, pci_compat,
  418. (phys_addr_t)ctrl_addr);
  419. if (off < 0)
  420. return;
  421. if (!is_serdes_configured(dev))
  422. fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
  423. }
  424. void ft_pci_setup(void *blob, bd_t *bd)
  425. {
  426. #ifdef CONFIG_PCIE1
  427. ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE1_ADDR, PCIE1);
  428. #endif
  429. #ifdef CONFIG_PCIE2
  430. ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE2_ADDR, PCIE2);
  431. #endif
  432. #ifdef CONFIG_PCIE3
  433. ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE3_ADDR, PCIE3);
  434. #endif
  435. #ifdef CONFIG_PCIE4
  436. ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE4_ADDR, PCIE4);
  437. #endif
  438. }
  439. #else
  440. void ft_pci_setup(void *blob, bd_t *bd)
  441. {
  442. }
  443. #endif