fsl_pci_init.c 24 KB

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