pcie_layerscape.c 13 KB

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