fsl_pci_init.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2007-2012 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <malloc.h>
  7. #include <asm/fsl_serdes.h>
  8. DECLARE_GLOBAL_DATA_PTR;
  9. /*
  10. * PCI/PCIE Controller initialization for mpc85xx/mpc86xx soc's
  11. *
  12. * Initialize controller and call the common driver/pci pci_hose_scan to
  13. * scan for bridges and devices.
  14. *
  15. * Hose fields which need to be pre-initialized by board specific code:
  16. * regions[]
  17. * first_busno
  18. *
  19. * Fields updated:
  20. * last_busno
  21. */
  22. #include <pci.h>
  23. #include <asm/io.h>
  24. #include <asm/fsl_pci.h>
  25. #ifndef CONFIG_SYS_PCI_MEMORY_BUS
  26. #define CONFIG_SYS_PCI_MEMORY_BUS 0
  27. #endif
  28. #ifndef CONFIG_SYS_PCI_MEMORY_PHYS
  29. #define CONFIG_SYS_PCI_MEMORY_PHYS 0
  30. #endif
  31. #if defined(CONFIG_SYS_PCI_64BIT) && !defined(CONFIG_SYS_PCI64_MEMORY_BUS)
  32. #define CONFIG_SYS_PCI64_MEMORY_BUS (64ull*1024*1024*1024)
  33. #endif
  34. /* Setup one inbound ATMU window.
  35. *
  36. * We let the caller decide what the window size should be
  37. */
  38. static void set_inbound_window(volatile pit_t *pi,
  39. struct pci_region *r,
  40. u64 size)
  41. {
  42. u32 sz = (__ilog2_u64(size) - 1);
  43. #ifdef CONFIG_SYS_FSL_ERRATUM_A005434
  44. u32 flag = 0;
  45. #else
  46. u32 flag = PIWAR_LOCAL;
  47. #endif
  48. flag |= PIWAR_EN | PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
  49. out_be32(&pi->pitar, r->phys_start >> 12);
  50. out_be32(&pi->piwbar, r->bus_start >> 12);
  51. #ifdef CONFIG_SYS_PCI_64BIT
  52. out_be32(&pi->piwbear, r->bus_start >> 44);
  53. #else
  54. out_be32(&pi->piwbear, 0);
  55. #endif
  56. if (r->flags & PCI_REGION_PREFETCH)
  57. flag |= PIWAR_PF;
  58. out_be32(&pi->piwar, flag | sz);
  59. }
  60. int fsl_setup_hose(struct pci_controller *hose, unsigned long addr)
  61. {
  62. volatile ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *) addr;
  63. /* Reset hose to make sure its in a clean state */
  64. memset(hose, 0, sizeof(struct pci_controller));
  65. pci_setup_indirect(hose, (u32)&pci->cfg_addr, (u32)&pci->cfg_data);
  66. return fsl_is_pci_agent(hose);
  67. }
  68. static int fsl_pci_setup_inbound_windows(struct pci_controller *hose,
  69. u64 out_lo, u8 pcie_cap,
  70. volatile pit_t *pi)
  71. {
  72. struct pci_region *r = hose->regions + hose->region_count;
  73. u64 sz = min((u64)gd->ram_size, (1ull << 32));
  74. phys_addr_t phys_start = CONFIG_SYS_PCI_MEMORY_PHYS;
  75. pci_addr_t bus_start = CONFIG_SYS_PCI_MEMORY_BUS;
  76. pci_size_t pci_sz;
  77. /* we have no space available for inbound memory mapping */
  78. if (bus_start > out_lo) {
  79. printf ("no space for inbound mapping of memory\n");
  80. return 0;
  81. }
  82. /* limit size */
  83. if ((bus_start + sz) > out_lo) {
  84. sz = out_lo - bus_start;
  85. debug ("limiting size to %llx\n", sz);
  86. }
  87. pci_sz = 1ull << __ilog2_u64(sz);
  88. /*
  89. * we can overlap inbound/outbound windows on PCI-E since RX & TX
  90. * links a separate
  91. */
  92. if ((pcie_cap == PCI_CAP_ID_EXP) && (pci_sz < sz)) {
  93. debug ("R0 bus_start: %llx phys_start: %llx size: %llx\n",
  94. (u64)bus_start, (u64)phys_start, (u64)sz);
  95. pci_set_region(r, bus_start, phys_start, sz,
  96. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
  97. PCI_REGION_PREFETCH);
  98. /* if we aren't an exact power of two match, pci_sz is smaller
  99. * round it up to the next power of two. We report the actual
  100. * size to pci region tracking.
  101. */
  102. if (pci_sz != sz)
  103. sz = 2ull << __ilog2_u64(sz);
  104. set_inbound_window(pi--, r++, sz);
  105. sz = 0; /* make sure we dont set the R2 window */
  106. } else {
  107. debug ("R0 bus_start: %llx phys_start: %llx size: %llx\n",
  108. (u64)bus_start, (u64)phys_start, (u64)pci_sz);
  109. pci_set_region(r, bus_start, phys_start, pci_sz,
  110. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
  111. PCI_REGION_PREFETCH);
  112. set_inbound_window(pi--, r++, pci_sz);
  113. sz -= pci_sz;
  114. bus_start += pci_sz;
  115. phys_start += pci_sz;
  116. pci_sz = 1ull << __ilog2_u64(sz);
  117. if (sz) {
  118. debug ("R1 bus_start: %llx phys_start: %llx size: %llx\n",
  119. (u64)bus_start, (u64)phys_start, (u64)pci_sz);
  120. pci_set_region(r, bus_start, phys_start, pci_sz,
  121. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
  122. PCI_REGION_PREFETCH);
  123. set_inbound_window(pi--, r++, pci_sz);
  124. sz -= pci_sz;
  125. bus_start += pci_sz;
  126. phys_start += pci_sz;
  127. }
  128. }
  129. #if defined(CONFIG_PHYS_64BIT) && defined(CONFIG_SYS_PCI_64BIT)
  130. /*
  131. * On 64-bit capable systems, set up a mapping for all of DRAM
  132. * in high pci address space.
  133. */
  134. pci_sz = 1ull << __ilog2_u64(gd->ram_size);
  135. /* round up to the next largest power of two */
  136. if (gd->ram_size > pci_sz)
  137. pci_sz = 1ull << (__ilog2_u64(gd->ram_size) + 1);
  138. debug ("R64 bus_start: %llx phys_start: %llx size: %llx\n",
  139. (u64)CONFIG_SYS_PCI64_MEMORY_BUS,
  140. (u64)CONFIG_SYS_PCI_MEMORY_PHYS,
  141. (u64)pci_sz);
  142. pci_set_region(r,
  143. CONFIG_SYS_PCI64_MEMORY_BUS,
  144. CONFIG_SYS_PCI_MEMORY_PHYS,
  145. pci_sz,
  146. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
  147. PCI_REGION_PREFETCH);
  148. set_inbound_window(pi--, r++, pci_sz);
  149. #else
  150. pci_sz = 1ull << __ilog2_u64(sz);
  151. if (sz) {
  152. debug ("R2 bus_start: %llx phys_start: %llx size: %llx\n",
  153. (u64)bus_start, (u64)phys_start, (u64)pci_sz);
  154. pci_set_region(r, bus_start, phys_start, pci_sz,
  155. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
  156. PCI_REGION_PREFETCH);
  157. sz -= pci_sz;
  158. bus_start += pci_sz;
  159. phys_start += pci_sz;
  160. set_inbound_window(pi--, r++, pci_sz);
  161. }
  162. #endif
  163. #ifdef CONFIG_PHYS_64BIT
  164. if (sz && (((u64)gd->ram_size) < (1ull << 32)))
  165. printf("Was not able to map all of memory via "
  166. "inbound windows -- %lld remaining\n", sz);
  167. #endif
  168. hose->region_count = r - hose->regions;
  169. return 1;
  170. }
  171. #ifdef CONFIG_SRIO_PCIE_BOOT_MASTER
  172. static void fsl_pcie_boot_master(pit_t *pi)
  173. {
  174. /* configure inbound window for slave's u-boot image */
  175. debug("PCIEBOOT - MASTER: Inbound window for slave's image; "
  176. "Local = 0x%llx, Bus = 0x%llx, Size = 0x%x\n",
  177. (u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
  178. (u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS1,
  179. CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
  180. struct pci_region r_inbound;
  181. u32 sz_inbound = __ilog2_u64(CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE)
  182. - 1;
  183. pci_set_region(&r_inbound,
  184. CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS1,
  185. CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
  186. sz_inbound,
  187. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  188. set_inbound_window(pi--, &r_inbound,
  189. CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
  190. /* configure inbound window for slave's u-boot image */
  191. debug("PCIEBOOT - MASTER: Inbound window for slave's image; "
  192. "Local = 0x%llx, Bus = 0x%llx, Size = 0x%x\n",
  193. (u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
  194. (u64)CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS2,
  195. CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
  196. pci_set_region(&r_inbound,
  197. CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_BUS2,
  198. CONFIG_SRIO_PCIE_BOOT_IMAGE_MEM_PHYS,
  199. sz_inbound,
  200. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  201. set_inbound_window(pi--, &r_inbound,
  202. CONFIG_SRIO_PCIE_BOOT_IMAGE_SIZE);
  203. /* configure inbound window for slave's ucode and ENV */
  204. debug("PCIEBOOT - MASTER: Inbound window for slave's "
  205. "ucode and ENV; "
  206. "Local = 0x%llx, Bus = 0x%llx, Size = 0x%x\n",
  207. (u64)CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_PHYS,
  208. (u64)CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_BUS,
  209. CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_SIZE);
  210. sz_inbound = __ilog2_u64(CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_SIZE)
  211. - 1;
  212. pci_set_region(&r_inbound,
  213. CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_BUS,
  214. CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_MEM_PHYS,
  215. sz_inbound,
  216. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  217. set_inbound_window(pi--, &r_inbound,
  218. CONFIG_SRIO_PCIE_BOOT_UCODE_ENV_SIZE);
  219. }
  220. static void fsl_pcie_boot_master_release_slave(int port)
  221. {
  222. unsigned long release_addr;
  223. /* now release slave's core 0 */
  224. switch (port) {
  225. case 1:
  226. release_addr = CONFIG_SYS_PCIE1_MEM_VIRT
  227. + CONFIG_SRIO_PCIE_BOOT_BRR_OFFSET;
  228. break;
  229. #ifdef CONFIG_SYS_PCIE2_MEM_VIRT
  230. case 2:
  231. release_addr = CONFIG_SYS_PCIE2_MEM_VIRT
  232. + CONFIG_SRIO_PCIE_BOOT_BRR_OFFSET;
  233. break;
  234. #endif
  235. #ifdef CONFIG_SYS_PCIE3_MEM_VIRT
  236. case 3:
  237. release_addr = CONFIG_SYS_PCIE3_MEM_VIRT
  238. + CONFIG_SRIO_PCIE_BOOT_BRR_OFFSET;
  239. break;
  240. #endif
  241. default:
  242. release_addr = 0;
  243. break;
  244. }
  245. if (release_addr != 0) {
  246. out_be32((void *)release_addr,
  247. CONFIG_SRIO_PCIE_BOOT_RELEASE_MASK);
  248. debug("PCIEBOOT - MASTER: "
  249. "Release slave successfully! Now the slave should start up!\n");
  250. } else {
  251. debug("PCIEBOOT - MASTER: "
  252. "Release slave failed!\n");
  253. }
  254. }
  255. #endif
  256. void fsl_pci_init(struct pci_controller *hose, struct fsl_pci_info *pci_info)
  257. {
  258. u32 cfg_addr = (u32)&((ccsr_fsl_pci_t *)pci_info->regs)->cfg_addr;
  259. u32 cfg_data = (u32)&((ccsr_fsl_pci_t *)pci_info->regs)->cfg_data;
  260. u16 temp16;
  261. u32 temp32;
  262. u32 block_rev;
  263. int enabled, r, inbound = 0;
  264. u16 ltssm;
  265. u8 temp8, pcie_cap;
  266. int pcie_cap_pos;
  267. int pci_dcr;
  268. int pci_dsr;
  269. int pci_lsr;
  270. #if defined(CONFIG_FSL_PCIE_DISABLE_ASPM)
  271. int pci_lcr;
  272. #endif
  273. volatile ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *)cfg_addr;
  274. struct pci_region *reg = hose->regions + hose->region_count;
  275. pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
  276. /* Initialize ATMU registers based on hose regions and flags */
  277. volatile pot_t *po = &pci->pot[1]; /* skip 0 */
  278. volatile pit_t *pi;
  279. u64 out_hi = 0, out_lo = -1ULL;
  280. u32 pcicsrbar, pcicsrbar_sz;
  281. pci_setup_indirect(hose, cfg_addr, cfg_data);
  282. #ifdef PEX_CCB_DIV
  283. /* Configure the PCIE controller core clock ratio */
  284. pci_hose_write_config_dword(hose, dev, 0x440,
  285. ((gd->bus_clk / 1000000) *
  286. (16 / PEX_CCB_DIV)) / 333);
  287. #endif
  288. block_rev = in_be32(&pci->block_rev1);
  289. if (PEX_IP_BLK_REV_2_2 <= block_rev) {
  290. pi = &pci->pit[2]; /* 0xDC0 */
  291. } else {
  292. pi = &pci->pit[3]; /* 0xDE0 */
  293. }
  294. /* Handle setup of outbound windows first */
  295. for (r = 0; r < hose->region_count; r++) {
  296. unsigned long flags = hose->regions[r].flags;
  297. u32 sz = (__ilog2_u64((u64)hose->regions[r].size) - 1);
  298. flags &= PCI_REGION_SYS_MEMORY|PCI_REGION_TYPE;
  299. if (flags != PCI_REGION_SYS_MEMORY) {
  300. u64 start = hose->regions[r].bus_start;
  301. u64 end = start + hose->regions[r].size;
  302. out_be32(&po->powbar, hose->regions[r].phys_start >> 12);
  303. out_be32(&po->potar, start >> 12);
  304. #ifdef CONFIG_SYS_PCI_64BIT
  305. out_be32(&po->potear, start >> 44);
  306. #else
  307. out_be32(&po->potear, 0);
  308. #endif
  309. if (hose->regions[r].flags & PCI_REGION_IO) {
  310. out_be32(&po->powar, POWAR_EN | sz |
  311. POWAR_IO_READ | POWAR_IO_WRITE);
  312. } else {
  313. out_be32(&po->powar, POWAR_EN | sz |
  314. POWAR_MEM_READ | POWAR_MEM_WRITE);
  315. out_lo = min(start, out_lo);
  316. out_hi = max(end, out_hi);
  317. }
  318. po++;
  319. }
  320. }
  321. debug("Outbound memory range: %llx:%llx\n", out_lo, out_hi);
  322. /* setup PCSRBAR/PEXCSRBAR */
  323. pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, 0xffffffff);
  324. pci_hose_read_config_dword (hose, dev, PCI_BASE_ADDRESS_0, &pcicsrbar_sz);
  325. pcicsrbar_sz = ~pcicsrbar_sz + 1;
  326. if (out_hi < (0x100000000ull - pcicsrbar_sz) ||
  327. (out_lo > 0x100000000ull))
  328. pcicsrbar = 0x100000000ull - pcicsrbar_sz;
  329. else
  330. pcicsrbar = (out_lo - pcicsrbar_sz) & -pcicsrbar_sz;
  331. pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, pcicsrbar);
  332. out_lo = min(out_lo, (u64)pcicsrbar);
  333. debug("PCICSRBAR @ 0x%x\n", pcicsrbar);
  334. pci_set_region(reg++, pcicsrbar, CONFIG_SYS_CCSRBAR_PHYS,
  335. pcicsrbar_sz, PCI_REGION_SYS_MEMORY);
  336. hose->region_count++;
  337. /* see if we are a PCIe or PCI controller */
  338. pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
  339. pci_dcr = pcie_cap_pos + 0x08;
  340. pci_dsr = pcie_cap_pos + 0x0a;
  341. pci_lsr = pcie_cap_pos + 0x12;
  342. pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
  343. #ifdef CONFIG_SRIO_PCIE_BOOT_MASTER
  344. /* boot from PCIE --master */
  345. char *s = env_get("bootmaster");
  346. char pcie[6];
  347. sprintf(pcie, "PCIE%d", pci_info->pci_num);
  348. if (s && (strcmp(s, pcie) == 0)) {
  349. debug("PCIEBOOT - MASTER: Master port [ %d ] for pcie boot.\n",
  350. pci_info->pci_num);
  351. fsl_pcie_boot_master((pit_t *)pi);
  352. } else {
  353. /* inbound */
  354. inbound = fsl_pci_setup_inbound_windows(hose,
  355. out_lo, pcie_cap, pi);
  356. }
  357. #else
  358. /* inbound */
  359. inbound = fsl_pci_setup_inbound_windows(hose, out_lo, pcie_cap, pi);
  360. #endif
  361. for (r = 0; r < hose->region_count; r++)
  362. debug("PCI reg:%d %016llx:%016llx %016llx %08lx\n", r,
  363. (u64)hose->regions[r].phys_start,
  364. (u64)hose->regions[r].bus_start,
  365. (u64)hose->regions[r].size,
  366. hose->regions[r].flags);
  367. pci_register_hose(hose);
  368. pciauto_config_init(hose); /* grab pci_{mem,prefetch,io} */
  369. hose->current_busno = hose->first_busno;
  370. out_be32(&pci->pedr, 0xffffffff); /* Clear any errors */
  371. out_be32(&pci->peer, ~0x20140); /* Enable All Error Interrupts except
  372. * - Master abort (pci)
  373. * - Master PERR (pci)
  374. * - ICCA (PCIe)
  375. */
  376. pci_hose_read_config_dword(hose, dev, pci_dcr, &temp32);
  377. temp32 |= 0xf000e; /* set URR, FER, NFER (but not CER) */
  378. pci_hose_write_config_dword(hose, dev, pci_dcr, temp32);
  379. #if defined(CONFIG_FSL_PCIE_DISABLE_ASPM)
  380. pci_lcr = pcie_cap_pos + 0x10;
  381. temp32 = 0;
  382. pci_hose_read_config_dword(hose, dev, pci_lcr, &temp32);
  383. temp32 &= ~0x03; /* Disable ASPM */
  384. pci_hose_write_config_dword(hose, dev, pci_lcr, temp32);
  385. udelay(1);
  386. #endif
  387. if (pcie_cap == PCI_CAP_ID_EXP) {
  388. if (block_rev >= PEX_IP_BLK_REV_3_0) {
  389. #define PEX_CSR0_LTSSM_MASK 0xFC
  390. #define PEX_CSR0_LTSSM_SHIFT 2
  391. ltssm = (in_be32(&pci->pex_csr0)
  392. & PEX_CSR0_LTSSM_MASK) >> PEX_CSR0_LTSSM_SHIFT;
  393. enabled = (ltssm == 0x11) ? 1 : 0;
  394. #ifdef CONFIG_FSL_PCIE_RESET
  395. int i;
  396. /* assert PCIe reset */
  397. setbits_be32(&pci->pdb_stat, 0x08000000);
  398. (void) in_be32(&pci->pdb_stat);
  399. udelay(1000);
  400. /* clear PCIe reset */
  401. clrbits_be32(&pci->pdb_stat, 0x08000000);
  402. asm("sync;isync");
  403. for (i = 0; i < 100 && ltssm < PCI_LTSSM_L0; i++) {
  404. pci_hose_read_config_word(hose, dev, PCI_LTSSM,
  405. &ltssm);
  406. udelay(1000);
  407. }
  408. #endif
  409. } else {
  410. /* pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm); */
  411. /* enabled = ltssm >= PCI_LTSSM_L0; */
  412. pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm);
  413. enabled = ltssm >= PCI_LTSSM_L0;
  414. #ifdef CONFIG_FSL_PCIE_RESET
  415. if (ltssm == 1) {
  416. int i;
  417. debug("....PCIe link error. " "LTSSM=0x%02x.", ltssm);
  418. /* assert PCIe reset */
  419. setbits_be32(&pci->pdb_stat, 0x08000000);
  420. (void) in_be32(&pci->pdb_stat);
  421. udelay(100);
  422. debug(" Asserting PCIe reset @%p = %x\n",
  423. &pci->pdb_stat, in_be32(&pci->pdb_stat));
  424. /* clear PCIe reset */
  425. clrbits_be32(&pci->pdb_stat, 0x08000000);
  426. asm("sync;isync");
  427. for (i=0; i<100 && ltssm < PCI_LTSSM_L0; i++) {
  428. pci_hose_read_config_word(hose, dev, PCI_LTSSM,
  429. &ltssm);
  430. udelay(1000);
  431. debug("....PCIe link error. "
  432. "LTSSM=0x%02x.\n", ltssm);
  433. }
  434. enabled = ltssm >= PCI_LTSSM_L0;
  435. /* we need to re-write the bar0 since a reset will
  436. * clear it
  437. */
  438. pci_hose_write_config_dword(hose, dev,
  439. PCI_BASE_ADDRESS_0, pcicsrbar);
  440. }
  441. #endif
  442. }
  443. #ifdef CONFIG_SYS_P4080_ERRATUM_PCIE_A003
  444. if (enabled == 0) {
  445. serdes_corenet_t *srds_regs = (void *)CONFIG_SYS_FSL_CORENET_SERDES_ADDR;
  446. temp32 = in_be32(&srds_regs->srdspccr0);
  447. if ((temp32 >> 28) == 3) {
  448. int i;
  449. out_be32(&srds_regs->srdspccr0, 2 << 28);
  450. setbits_be32(&pci->pdb_stat, 0x08000000);
  451. in_be32(&pci->pdb_stat);
  452. udelay(100);
  453. clrbits_be32(&pci->pdb_stat, 0x08000000);
  454. asm("sync;isync");
  455. for (i=0; i < 100 && ltssm < PCI_LTSSM_L0; i++) {
  456. pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm);
  457. udelay(1000);
  458. }
  459. enabled = ltssm >= PCI_LTSSM_L0;
  460. }
  461. }
  462. #endif
  463. if (!enabled) {
  464. /* Let the user know there's no PCIe link for root
  465. * complex. for endpoint, the link may not setup, so
  466. * print undetermined.
  467. */
  468. if (fsl_is_pci_agent(hose))
  469. printf("undetermined, regs @ 0x%lx\n", pci_info->regs);
  470. else
  471. printf("no link, regs @ 0x%lx\n", pci_info->regs);
  472. hose->last_busno = hose->first_busno;
  473. return;
  474. }
  475. out_be32(&pci->pme_msg_det, 0xffffffff);
  476. out_be32(&pci->pme_msg_int_en, 0xffffffff);
  477. /* Print the negotiated PCIe link width */
  478. pci_hose_read_config_word(hose, dev, pci_lsr, &temp16);
  479. printf("x%d gen%d, regs @ 0x%lx\n", (temp16 & 0x3f0) >> 4,
  480. (temp16 & 0xf), pci_info->regs);
  481. hose->current_busno++; /* Start scan with secondary */
  482. pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
  483. }
  484. #ifdef CONFIG_SYS_FSL_ERRATUM_A007815
  485. /* The Read-Only Write Enable bit defaults to 1 instead of 0.
  486. * Set to 0 to protect the read-only registers.
  487. */
  488. clrbits_be32(&pci->dbi_ro_wr_en, 0x01);
  489. #endif
  490. /* Use generic setup_device to initialize standard pci regs,
  491. * but do not allocate any windows since any BAR found (such
  492. * as PCSRBAR) is not in this cpu's memory space.
  493. */
  494. pciauto_setup_device(hose, dev, 0, hose->pci_mem,
  495. hose->pci_prefetch, hose->pci_io);
  496. if (inbound) {
  497. pci_hose_read_config_word(hose, dev, PCI_COMMAND, &temp16);
  498. pci_hose_write_config_word(hose, dev, PCI_COMMAND,
  499. temp16 | PCI_COMMAND_MEMORY);
  500. }
  501. #ifndef CONFIG_PCI_NOSCAN
  502. if (!fsl_is_pci_agent(hose)) {
  503. debug(" Scanning PCI bus %02x\n",
  504. hose->current_busno);
  505. hose->last_busno = pci_hose_scan_bus(hose, hose->current_busno);
  506. } else {
  507. debug(" Not scanning PCI bus %02x. PI=%x\n",
  508. hose->current_busno, temp8);
  509. hose->last_busno = hose->current_busno;
  510. }
  511. /* if we are PCIe - update limit regs and subordinate busno
  512. * for the virtual P2P bridge
  513. */
  514. if (pcie_cap == PCI_CAP_ID_EXP) {
  515. pciauto_postscan_setup_bridge(hose, dev, hose->last_busno);
  516. }
  517. #else
  518. hose->last_busno = hose->current_busno;
  519. #endif
  520. /* Clear all error indications */
  521. if (pcie_cap == PCI_CAP_ID_EXP)
  522. out_be32(&pci->pme_msg_det, 0xffffffff);
  523. out_be32(&pci->pedr, 0xffffffff);
  524. pci_hose_read_config_word(hose, dev, pci_dsr, &temp16);
  525. if (temp16) {
  526. pci_hose_write_config_word(hose, dev, pci_dsr, 0xffff);
  527. }
  528. pci_hose_read_config_word (hose, dev, PCI_SEC_STATUS, &temp16);
  529. if (temp16) {
  530. pci_hose_write_config_word(hose, dev, PCI_SEC_STATUS, 0xffff);
  531. }
  532. }
  533. int fsl_is_pci_agent(struct pci_controller *hose)
  534. {
  535. int pcie_cap_pos;
  536. u8 pcie_cap;
  537. pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
  538. pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
  539. pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
  540. if (pcie_cap == PCI_CAP_ID_EXP) {
  541. u8 header_type;
  542. pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE,
  543. &header_type);
  544. return (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
  545. } else {
  546. u8 prog_if;
  547. pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &prog_if);
  548. /* Programming Interface (PCI_CLASS_PROG)
  549. * 0 == pci host or pcie root-complex,
  550. * 1 == pci agent or pcie end-point
  551. */
  552. return (prog_if == FSL_PROG_IF_AGENT);
  553. }
  554. }
  555. int fsl_pci_init_port(struct fsl_pci_info *pci_info,
  556. struct pci_controller *hose, int busno)
  557. {
  558. volatile ccsr_fsl_pci_t *pci;
  559. struct pci_region *r;
  560. pci_dev_t dev = PCI_BDF(busno,0,0);
  561. int pcie_cap_pos;
  562. u8 pcie_cap;
  563. pci = (ccsr_fsl_pci_t *) pci_info->regs;
  564. /* on non-PCIe controllers we don't have pme_msg_det so this code
  565. * should do nothing since the read will return 0
  566. */
  567. if (in_be32(&pci->pme_msg_det)) {
  568. out_be32(&pci->pme_msg_det, 0xffffffff);
  569. debug (" with errors. Clearing. Now 0x%08x",
  570. pci->pme_msg_det);
  571. }
  572. r = hose->regions + hose->region_count;
  573. /* outbound memory */
  574. pci_set_region(r++,
  575. pci_info->mem_bus,
  576. pci_info->mem_phys,
  577. pci_info->mem_size,
  578. PCI_REGION_MEM);
  579. /* outbound io */
  580. pci_set_region(r++,
  581. pci_info->io_bus,
  582. pci_info->io_phys,
  583. pci_info->io_size,
  584. PCI_REGION_IO);
  585. hose->region_count = r - hose->regions;
  586. hose->first_busno = busno;
  587. fsl_pci_init(hose, pci_info);
  588. if (fsl_is_pci_agent(hose)) {
  589. fsl_pci_config_unlock(hose);
  590. hose->last_busno = hose->first_busno;
  591. #ifdef CONFIG_SRIO_PCIE_BOOT_MASTER
  592. } else {
  593. /* boot from PCIE --master releases slave's core 0 */
  594. char *s = env_get("bootmaster");
  595. char pcie[6];
  596. sprintf(pcie, "PCIE%d", pci_info->pci_num);
  597. if (s && (strcmp(s, pcie) == 0))
  598. fsl_pcie_boot_master_release_slave(pci_info->pci_num);
  599. #endif
  600. }
  601. pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
  602. pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
  603. printf("PCI%s%x: Bus %02x - %02x\n", pcie_cap == PCI_CAP_ID_EXP ?
  604. "e" : "", pci_info->pci_num,
  605. hose->first_busno, hose->last_busno);
  606. return(hose->last_busno + 1);
  607. }
  608. /* Enable inbound PCI config cycles for agent/endpoint interface */
  609. void fsl_pci_config_unlock(struct pci_controller *hose)
  610. {
  611. pci_dev_t dev = PCI_BDF(hose->first_busno,0,0);
  612. int pcie_cap_pos;
  613. u8 pcie_cap;
  614. u16 pbfr;
  615. if (!fsl_is_pci_agent(hose))
  616. return;
  617. pcie_cap_pos = pci_hose_find_capability(hose, dev, PCI_CAP_ID_EXP);
  618. pci_hose_read_config_byte(hose, dev, pcie_cap_pos, &pcie_cap);
  619. if (pcie_cap != 0x0) {
  620. ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *)hose->cfg_addr;
  621. u32 block_rev = in_be32(&pci->block_rev1);
  622. /* PCIe - set CFG_READY bit of Configuration Ready Register */
  623. if (block_rev >= PEX_IP_BLK_REV_3_0)
  624. setbits_be32(&pci->config, FSL_PCIE_V3_CFG_RDY);
  625. else
  626. pci_hose_write_config_byte(hose, dev,
  627. FSL_PCIE_CFG_RDY, 0x1);
  628. } else {
  629. /* PCI - clear ACL bit of PBFR */
  630. pci_hose_read_config_word(hose, dev, FSL_PCI_PBFR, &pbfr);
  631. pbfr &= ~0x20;
  632. pci_hose_write_config_word(hose, dev, FSL_PCI_PBFR, pbfr);
  633. }
  634. }
  635. #if defined(CONFIG_PCIE1) || defined(CONFIG_PCIE2) || \
  636. defined(CONFIG_PCIE3) || defined(CONFIG_PCIE4)
  637. int fsl_configure_pcie(struct fsl_pci_info *info,
  638. struct pci_controller *hose,
  639. const char *connected, int busno)
  640. {
  641. int is_endpoint;
  642. set_next_law(info->mem_phys, law_size_bits(info->mem_size), info->law);
  643. set_next_law(info->io_phys, law_size_bits(info->io_size), info->law);
  644. is_endpoint = fsl_setup_hose(hose, info->regs);
  645. printf("PCIe%u: %s", info->pci_num,
  646. is_endpoint ? "Endpoint" : "Root Complex");
  647. if (connected)
  648. printf(" of %s", connected);
  649. puts(", ");
  650. return fsl_pci_init_port(info, hose, busno);
  651. }
  652. #if defined(CONFIG_FSL_CORENET)
  653. #ifdef CONFIG_SYS_FSL_QORIQ_CHASSIS2
  654. #define _DEVDISR_PCIE1 FSL_CORENET_DEVDISR3_PCIE1
  655. #define _DEVDISR_PCIE2 FSL_CORENET_DEVDISR3_PCIE2
  656. #define _DEVDISR_PCIE3 FSL_CORENET_DEVDISR3_PCIE3
  657. #define _DEVDISR_PCIE4 FSL_CORENET_DEVDISR3_PCIE4
  658. #else
  659. #define _DEVDISR_PCIE1 FSL_CORENET_DEVDISR_PCIE1
  660. #define _DEVDISR_PCIE2 FSL_CORENET_DEVDISR_PCIE2
  661. #define _DEVDISR_PCIE3 FSL_CORENET_DEVDISR_PCIE3
  662. #define _DEVDISR_PCIE4 FSL_CORENET_DEVDISR_PCIE4
  663. #endif
  664. #define CONFIG_SYS_MPC8xxx_GUTS_ADDR CONFIG_SYS_MPC85xx_GUTS_ADDR
  665. #elif defined(CONFIG_MPC85xx)
  666. #define _DEVDISR_PCIE1 MPC85xx_DEVDISR_PCIE
  667. #define _DEVDISR_PCIE2 MPC85xx_DEVDISR_PCIE2
  668. #define _DEVDISR_PCIE3 MPC85xx_DEVDISR_PCIE3
  669. #define _DEVDISR_PCIE4 0
  670. #define CONFIG_SYS_MPC8xxx_GUTS_ADDR CONFIG_SYS_MPC85xx_GUTS_ADDR
  671. #elif defined(CONFIG_MPC86xx)
  672. #define _DEVDISR_PCIE1 MPC86xx_DEVDISR_PCIE1
  673. #define _DEVDISR_PCIE2 MPC86xx_DEVDISR_PCIE2
  674. #define _DEVDISR_PCIE3 0
  675. #define _DEVDISR_PCIE4 0
  676. #define CONFIG_SYS_MPC8xxx_GUTS_ADDR \
  677. (&((immap_t *)CONFIG_SYS_IMMR)->im_gur)
  678. #else
  679. #error "No defines for DEVDISR_PCIE"
  680. #endif
  681. /* Implement a dummy function for those platforms w/o SERDES */
  682. static const char *__board_serdes_name(enum srds_prtcl device)
  683. {
  684. switch (device) {
  685. #ifdef CONFIG_SYS_PCIE1_NAME
  686. case PCIE1:
  687. return CONFIG_SYS_PCIE1_NAME;
  688. #endif
  689. #ifdef CONFIG_SYS_PCIE2_NAME
  690. case PCIE2:
  691. return CONFIG_SYS_PCIE2_NAME;
  692. #endif
  693. #ifdef CONFIG_SYS_PCIE3_NAME
  694. case PCIE3:
  695. return CONFIG_SYS_PCIE3_NAME;
  696. #endif
  697. #ifdef CONFIG_SYS_PCIE4_NAME
  698. case PCIE4:
  699. return CONFIG_SYS_PCIE4_NAME;
  700. #endif
  701. default:
  702. return NULL;
  703. }
  704. return NULL;
  705. }
  706. __attribute__((weak, alias("__board_serdes_name"))) const char *
  707. board_serdes_name(enum srds_prtcl device);
  708. static u32 devdisr_mask[] = {
  709. _DEVDISR_PCIE1,
  710. _DEVDISR_PCIE2,
  711. _DEVDISR_PCIE3,
  712. _DEVDISR_PCIE4,
  713. };
  714. int fsl_pcie_init_ctrl(int busno, u32 devdisr, enum srds_prtcl dev,
  715. struct fsl_pci_info *pci_info)
  716. {
  717. struct pci_controller *hose;
  718. int num = dev - PCIE1;
  719. hose = calloc(1, sizeof(struct pci_controller));
  720. if (!hose)
  721. return busno;
  722. if (is_serdes_configured(dev) && !(devdisr & devdisr_mask[num])) {
  723. busno = fsl_configure_pcie(pci_info, hose,
  724. board_serdes_name(dev), busno);
  725. } else {
  726. printf("PCIe%d: disabled\n", num + 1);
  727. }
  728. return busno;
  729. }
  730. int fsl_pcie_init_board(int busno)
  731. {
  732. struct fsl_pci_info pci_info;
  733. ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC8xxx_GUTS_ADDR;
  734. u32 devdisr;
  735. u32 *addr;
  736. #ifdef CONFIG_SYS_FSL_QORIQ_CHASSIS2
  737. addr = &gur->devdisr3;
  738. #else
  739. addr = &gur->devdisr;
  740. #endif
  741. devdisr = in_be32(addr);
  742. #ifdef CONFIG_PCIE1
  743. SET_STD_PCIE_INFO(pci_info, 1);
  744. busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE1, &pci_info);
  745. #else
  746. setbits_be32(addr, _DEVDISR_PCIE1); /* disable */
  747. #endif
  748. #ifdef CONFIG_PCIE2
  749. SET_STD_PCIE_INFO(pci_info, 2);
  750. busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE2, &pci_info);
  751. #else
  752. setbits_be32(addr, _DEVDISR_PCIE2); /* disable */
  753. #endif
  754. #ifdef CONFIG_PCIE3
  755. SET_STD_PCIE_INFO(pci_info, 3);
  756. busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE3, &pci_info);
  757. #else
  758. setbits_be32(addr, _DEVDISR_PCIE3); /* disable */
  759. #endif
  760. #ifdef CONFIG_PCIE4
  761. SET_STD_PCIE_INFO(pci_info, 4);
  762. busno = fsl_pcie_init_ctrl(busno, devdisr, PCIE4, &pci_info);
  763. #else
  764. setbits_be32(addr, _DEVDISR_PCIE4); /* disable */
  765. #endif
  766. return busno;
  767. }
  768. #else
  769. int fsl_pcie_init_ctrl(int busno, u32 devdisr, enum srds_prtcl dev,
  770. struct fsl_pci_info *pci_info)
  771. {
  772. return busno;
  773. }
  774. int fsl_pcie_init_board(int busno)
  775. {
  776. return busno;
  777. }
  778. #endif
  779. #ifdef CONFIG_OF_BOARD_SETUP
  780. #include <linux/libfdt.h>
  781. #include <fdt_support.h>
  782. void ft_fsl_pci_setup(void *blob, const char *pci_compat,
  783. unsigned long ctrl_addr)
  784. {
  785. int off;
  786. u32 bus_range[2];
  787. phys_addr_t p_ctrl_addr = (phys_addr_t)ctrl_addr;
  788. struct pci_controller *hose;
  789. hose = find_hose_by_cfg_addr((void *)(ctrl_addr));
  790. /* convert ctrl_addr to true physical address */
  791. p_ctrl_addr = (phys_addr_t)ctrl_addr - CONFIG_SYS_CCSRBAR;
  792. p_ctrl_addr += CONFIG_SYS_CCSRBAR_PHYS;
  793. off = fdt_node_offset_by_compat_reg(blob, pci_compat, p_ctrl_addr);
  794. if (off < 0)
  795. return;
  796. /* We assume a cfg_addr not being set means we didn't setup the controller */
  797. if ((hose == NULL) || (hose->cfg_addr == NULL)) {
  798. fdt_del_node(blob, off);
  799. } else {
  800. bus_range[0] = 0;
  801. bus_range[1] = hose->last_busno - hose->first_busno;
  802. fdt_setprop(blob, off, "bus-range", &bus_range[0], 2*4);
  803. fdt_pci_dma_ranges(blob, off, hose);
  804. }
  805. }
  806. #endif