fsl_pci_init.c 23 KB

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