fsl_pci_init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Copyright 2007-2010 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. DECLARE_GLOBAL_DATA_PTR;
  21. /*
  22. * PCI/PCIE Controller initialization for mpc85xx/mpc86xx soc's
  23. *
  24. * Initialize controller and call the common driver/pci pci_hose_scan to
  25. * scan for bridges and devices.
  26. *
  27. * Hose fields which need to be pre-initialized by board specific code:
  28. * regions[]
  29. * first_busno
  30. *
  31. * Fields updated:
  32. * last_busno
  33. */
  34. #include <pci.h>
  35. #include <asm/io.h>
  36. #include <asm/fsl_pci.h>
  37. /* Freescale-specific PCI config registers */
  38. #define FSL_PCI_PBFR 0x44
  39. #define FSL_PCIE_CAP_ID 0x4c
  40. #define FSL_PCIE_CFG_RDY 0x4b0
  41. #define FSL_PROG_IF_AGENT 0x1
  42. void pciauto_prescan_setup_bridge(struct pci_controller *hose,
  43. pci_dev_t dev, int sub_bus);
  44. void pciauto_postscan_setup_bridge(struct pci_controller *hose,
  45. pci_dev_t dev, int sub_bus);
  46. void pciauto_config_init(struct pci_controller *hose);
  47. #ifndef CONFIG_SYS_PCI_MEMORY_BUS
  48. #define CONFIG_SYS_PCI_MEMORY_BUS 0
  49. #endif
  50. #ifndef CONFIG_SYS_PCI_MEMORY_PHYS
  51. #define CONFIG_SYS_PCI_MEMORY_PHYS 0
  52. #endif
  53. #if defined(CONFIG_SYS_PCI_64BIT) && !defined(CONFIG_SYS_PCI64_MEMORY_BUS)
  54. #define CONFIG_SYS_PCI64_MEMORY_BUS (64ull*1024*1024*1024)
  55. #endif
  56. /* Setup one inbound ATMU window.
  57. *
  58. * We let the caller decide what the window size should be
  59. */
  60. static void set_inbound_window(volatile pit_t *pi,
  61. struct pci_region *r,
  62. u64 size)
  63. {
  64. u32 sz = (__ilog2_u64(size) - 1);
  65. u32 flag = PIWAR_EN | PIWAR_LOCAL |
  66. PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
  67. out_be32(&pi->pitar, r->phys_start >> 12);
  68. out_be32(&pi->piwbar, r->bus_start >> 12);
  69. #ifdef CONFIG_SYS_PCI_64BIT
  70. out_be32(&pi->piwbear, r->bus_start >> 44);
  71. #else
  72. out_be32(&pi->piwbear, 0);
  73. #endif
  74. if (r->flags & PCI_REGION_PREFETCH)
  75. flag |= PIWAR_PF;
  76. out_be32(&pi->piwar, flag | sz);
  77. }
  78. int fsl_setup_hose(struct pci_controller *hose, unsigned long addr)
  79. {
  80. volatile ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *) addr;
  81. pci_setup_indirect(hose, (u32)&pci->cfg_addr, (u32)&pci->cfg_data);
  82. return fsl_is_pci_agent(hose);
  83. }
  84. static int fsl_pci_setup_inbound_windows(struct pci_controller *hose,
  85. u64 out_lo, u8 pcie_cap,
  86. volatile pit_t *pi)
  87. {
  88. struct pci_region *r = hose->regions + hose->region_count;
  89. u64 sz = min((u64)gd->ram_size, (1ull << 32));
  90. phys_addr_t phys_start = CONFIG_SYS_PCI_MEMORY_PHYS;
  91. pci_addr_t bus_start = CONFIG_SYS_PCI_MEMORY_BUS;
  92. pci_size_t pci_sz;
  93. /* we have no space available for inbound memory mapping */
  94. if (bus_start > out_lo) {
  95. printf ("no space for inbound mapping of memory\n");
  96. return 0;
  97. }
  98. /* limit size */
  99. if ((bus_start + sz) > out_lo) {
  100. sz = out_lo - bus_start;
  101. debug ("limiting size to %llx\n", sz);
  102. }
  103. pci_sz = 1ull << __ilog2_u64(sz);
  104. /*
  105. * we can overlap inbound/outbound windows on PCI-E since RX & TX
  106. * links a separate
  107. */
  108. if ((pcie_cap == PCI_CAP_ID_EXP) && (pci_sz < sz)) {
  109. debug ("R0 bus_start: %llx phys_start: %llx size: %llx\n",
  110. (u64)bus_start, (u64)phys_start, (u64)sz);
  111. pci_set_region(r, bus_start, phys_start, sz,
  112. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
  113. PCI_REGION_PREFETCH);
  114. /* if we aren't an exact power of two match, pci_sz is smaller
  115. * round it up to the next power of two. We report the actual
  116. * size to pci region tracking.
  117. */
  118. if (pci_sz != sz)
  119. sz = 2ull << __ilog2_u64(sz);
  120. set_inbound_window(pi--, r++, sz);
  121. sz = 0; /* make sure we dont set the R2 window */
  122. } else {
  123. debug ("R0 bus_start: %llx phys_start: %llx size: %llx\n",
  124. (u64)bus_start, (u64)phys_start, (u64)pci_sz);
  125. pci_set_region(r, bus_start, phys_start, pci_sz,
  126. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
  127. PCI_REGION_PREFETCH);
  128. set_inbound_window(pi--, r++, pci_sz);
  129. sz -= pci_sz;
  130. bus_start += pci_sz;
  131. phys_start += pci_sz;
  132. pci_sz = 1ull << __ilog2_u64(sz);
  133. if (sz) {
  134. debug ("R1 bus_start: %llx phys_start: %llx size: %llx\n",
  135. (u64)bus_start, (u64)phys_start, (u64)pci_sz);
  136. pci_set_region(r, bus_start, phys_start, pci_sz,
  137. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
  138. PCI_REGION_PREFETCH);
  139. set_inbound_window(pi--, r++, pci_sz);
  140. sz -= pci_sz;
  141. bus_start += pci_sz;
  142. phys_start += pci_sz;
  143. }
  144. }
  145. #if defined(CONFIG_PHYS_64BIT) && defined(CONFIG_SYS_PCI_64BIT)
  146. /*
  147. * On 64-bit capable systems, set up a mapping for all of DRAM
  148. * in high pci address space.
  149. */
  150. pci_sz = 1ull << __ilog2_u64(gd->ram_size);
  151. /* round up to the next largest power of two */
  152. if (gd->ram_size > pci_sz)
  153. pci_sz = 1ull << (__ilog2_u64(gd->ram_size) + 1);
  154. debug ("R64 bus_start: %llx phys_start: %llx size: %llx\n",
  155. (u64)CONFIG_SYS_PCI64_MEMORY_BUS,
  156. (u64)CONFIG_SYS_PCI_MEMORY_PHYS,
  157. (u64)pci_sz);
  158. pci_set_region(r,
  159. CONFIG_SYS_PCI64_MEMORY_BUS,
  160. CONFIG_SYS_PCI_MEMORY_PHYS,
  161. pci_sz,
  162. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
  163. PCI_REGION_PREFETCH);
  164. set_inbound_window(pi--, r++, pci_sz);
  165. #else
  166. pci_sz = 1ull << __ilog2_u64(sz);
  167. if (sz) {
  168. debug ("R2 bus_start: %llx phys_start: %llx size: %llx\n",
  169. (u64)bus_start, (u64)phys_start, (u64)pci_sz);
  170. pci_set_region(r, bus_start, phys_start, pci_sz,
  171. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
  172. PCI_REGION_PREFETCH);
  173. sz -= pci_sz;
  174. bus_start += pci_sz;
  175. phys_start += pci_sz;
  176. set_inbound_window(pi--, r++, pci_sz);
  177. }
  178. #endif
  179. #ifdef CONFIG_PHYS_64BIT
  180. if (sz && (((u64)gd->ram_size) < (1ull << 32)))
  181. printf("Was not able to map all of memory via "
  182. "inbound windows -- %lld remaining\n", sz);
  183. #endif
  184. hose->region_count = r - hose->regions;
  185. return 1;
  186. }
  187. void fsl_pci_init(struct pci_controller *hose, u32 cfg_addr, u32 cfg_data)
  188. {
  189. u16 temp16;
  190. u32 temp32;
  191. int enabled, r, inbound = 0;
  192. u16 ltssm;
  193. u8 temp8, pcie_cap;
  194. volatile ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *)cfg_addr;
  195. struct pci_region *reg = hose->regions + hose->region_count;
  196. pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
  197. /* Initialize ATMU registers based on hose regions and flags */
  198. volatile pot_t *po = &pci->pot[1]; /* skip 0 */
  199. volatile pit_t *pi = &pci->pit[2]; /* ranges from: 3 to 1 */
  200. u64 out_hi = 0, out_lo = -1ULL;
  201. u32 pcicsrbar, pcicsrbar_sz;
  202. #ifdef DEBUG
  203. int neg_link_w;
  204. #endif
  205. pci_setup_indirect(hose, cfg_addr, cfg_data);
  206. /* Handle setup of outbound windows first */
  207. for (r = 0; r < hose->region_count; r++) {
  208. unsigned long flags = hose->regions[r].flags;
  209. u32 sz = (__ilog2_u64((u64)hose->regions[r].size) - 1);
  210. flags &= PCI_REGION_SYS_MEMORY|PCI_REGION_TYPE;
  211. if (flags != PCI_REGION_SYS_MEMORY) {
  212. u64 start = hose->regions[r].bus_start;
  213. u64 end = start + hose->regions[r].size;
  214. out_be32(&po->powbar, hose->regions[r].phys_start >> 12);
  215. out_be32(&po->potar, start >> 12);
  216. #ifdef CONFIG_SYS_PCI_64BIT
  217. out_be32(&po->potear, start >> 44);
  218. #else
  219. out_be32(&po->potear, 0);
  220. #endif
  221. if (hose->regions[r].flags & PCI_REGION_IO) {
  222. out_be32(&po->powar, POWAR_EN | sz |
  223. POWAR_IO_READ | POWAR_IO_WRITE);
  224. } else {
  225. out_be32(&po->powar, POWAR_EN | sz |
  226. POWAR_MEM_READ | POWAR_MEM_WRITE);
  227. out_lo = min(start, out_lo);
  228. out_hi = max(end, out_hi);
  229. }
  230. po++;
  231. }
  232. }
  233. debug("Outbound memory range: %llx:%llx\n", out_lo, out_hi);
  234. /* setup PCSRBAR/PEXCSRBAR */
  235. pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, 0xffffffff);
  236. pci_hose_read_config_dword (hose, dev, PCI_BASE_ADDRESS_0, &pcicsrbar_sz);
  237. pcicsrbar_sz = ~pcicsrbar_sz + 1;
  238. if (out_hi < (0x100000000ull - pcicsrbar_sz) ||
  239. (out_lo > 0x100000000ull))
  240. pcicsrbar = 0x100000000ull - pcicsrbar_sz;
  241. else
  242. pcicsrbar = (out_lo - pcicsrbar_sz) & -pcicsrbar_sz;
  243. pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, pcicsrbar);
  244. out_lo = min(out_lo, (u64)pcicsrbar);
  245. debug("PCICSRBAR @ 0x%x\n", pcicsrbar);
  246. pci_set_region(reg++, pcicsrbar, CONFIG_SYS_CCSRBAR_PHYS,
  247. pcicsrbar_sz, PCI_REGION_SYS_MEMORY);
  248. hose->region_count++;
  249. /* see if we are a PCIe or PCI controller */
  250. pci_hose_read_config_byte(hose, dev, FSL_PCIE_CAP_ID, &pcie_cap);
  251. /* inbound */
  252. inbound = fsl_pci_setup_inbound_windows(hose, out_lo, pcie_cap, pi);
  253. for (r = 0; r < hose->region_count; r++)
  254. debug("PCI reg:%d %016llx:%016llx %016llx %08x\n", r,
  255. (u64)hose->regions[r].phys_start,
  256. hose->regions[r].bus_start,
  257. hose->regions[r].size,
  258. hose->regions[r].flags);
  259. pci_register_hose(hose);
  260. pciauto_config_init(hose); /* grab pci_{mem,prefetch,io} */
  261. hose->current_busno = hose->first_busno;
  262. out_be32(&pci->pedr, 0xffffffff); /* Clear any errors */
  263. out_be32(&pci->peer, ~0x20140); /* Enable All Error Interupts except
  264. * - Master abort (pci)
  265. * - Master PERR (pci)
  266. * - ICCA (PCIe)
  267. */
  268. pci_hose_read_config_dword(hose, dev, PCI_DCR, &temp32);
  269. temp32 |= 0xf000e; /* set URR, FER, NFER (but not CER) */
  270. pci_hose_write_config_dword(hose, dev, PCI_DCR, temp32);
  271. if (pcie_cap == PCI_CAP_ID_EXP) {
  272. pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm);
  273. enabled = ltssm >= PCI_LTSSM_L0;
  274. #ifdef CONFIG_FSL_PCIE_RESET
  275. if (ltssm == 1) {
  276. int i;
  277. debug("....PCIe link error. " "LTSSM=0x%02x.", ltssm);
  278. /* assert PCIe reset */
  279. setbits_be32(&pci->pdb_stat, 0x08000000);
  280. (void) in_be32(&pci->pdb_stat);
  281. udelay(100);
  282. debug(" Asserting PCIe reset @%x = %x\n",
  283. &pci->pdb_stat, in_be32(&pci->pdb_stat));
  284. /* clear PCIe reset */
  285. clrbits_be32(&pci->pdb_stat, 0x08000000);
  286. asm("sync;isync");
  287. for (i=0; i<100 && ltssm < PCI_LTSSM_L0; i++) {
  288. pci_hose_read_config_word(hose, dev, PCI_LTSSM,
  289. &ltssm);
  290. udelay(1000);
  291. debug("....PCIe link error. "
  292. "LTSSM=0x%02x.\n", ltssm);
  293. }
  294. enabled = ltssm >= PCI_LTSSM_L0;
  295. /* we need to re-write the bar0 since a reset will
  296. * clear it
  297. */
  298. pci_hose_write_config_dword(hose, dev,
  299. PCI_BASE_ADDRESS_0, pcicsrbar);
  300. }
  301. #endif
  302. if (!enabled) {
  303. debug("....PCIE link error. Skipping scan."
  304. "LTSSM=0x%02x\n", ltssm);
  305. hose->last_busno = hose->first_busno;
  306. return;
  307. }
  308. out_be32(&pci->pme_msg_det, 0xffffffff);
  309. out_be32(&pci->pme_msg_int_en, 0xffffffff);
  310. #ifdef DEBUG
  311. pci_hose_read_config_word(hose, dev, PCI_LSR, &temp16);
  312. neg_link_w = (temp16 & 0x3f0 ) >> 4;
  313. printf("...PCIE LTSSM=0x%x, Negotiated link width=%d\n",
  314. ltssm, neg_link_w);
  315. #endif
  316. hose->current_busno++; /* Start scan with secondary */
  317. pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
  318. }
  319. /* Use generic setup_device to initialize standard pci regs,
  320. * but do not allocate any windows since any BAR found (such
  321. * as PCSRBAR) is not in this cpu's memory space.
  322. */
  323. pciauto_setup_device(hose, dev, 0, hose->pci_mem,
  324. hose->pci_prefetch, hose->pci_io);
  325. if (inbound) {
  326. pci_hose_read_config_word(hose, dev, PCI_COMMAND, &temp16);
  327. pci_hose_write_config_word(hose, dev, PCI_COMMAND,
  328. temp16 | PCI_COMMAND_MEMORY);
  329. }
  330. #ifndef CONFIG_PCI_NOSCAN
  331. pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &temp8);
  332. /* Programming Interface (PCI_CLASS_PROG)
  333. * 0 == pci host or pcie root-complex,
  334. * 1 == pci agent or pcie end-point
  335. */
  336. if (!temp8) {
  337. printf(" Scanning PCI bus %02x\n",
  338. hose->current_busno);
  339. hose->last_busno = pci_hose_scan_bus(hose, hose->current_busno);
  340. } else {
  341. debug(" Not scanning PCI bus %02x. PI=%x\n",
  342. hose->current_busno, temp8);
  343. hose->last_busno = hose->current_busno;
  344. }
  345. /* if we are PCIe - update limit regs and subordinate busno
  346. * for the virtual P2P bridge
  347. */
  348. if (pcie_cap == PCI_CAP_ID_EXP) {
  349. pciauto_postscan_setup_bridge(hose, dev, hose->last_busno);
  350. }
  351. #else
  352. hose->last_busno = hose->current_busno;
  353. #endif
  354. /* Clear all error indications */
  355. if (pcie_cap == PCI_CAP_ID_EXP)
  356. out_be32(&pci->pme_msg_det, 0xffffffff);
  357. out_be32(&pci->pedr, 0xffffffff);
  358. pci_hose_read_config_word (hose, dev, PCI_DSR, &temp16);
  359. if (temp16) {
  360. pci_hose_write_config_word(hose, dev, PCI_DSR, 0xffff);
  361. }
  362. pci_hose_read_config_word (hose, dev, PCI_SEC_STATUS, &temp16);
  363. if (temp16) {
  364. pci_hose_write_config_word(hose, dev, PCI_SEC_STATUS, 0xffff);
  365. }
  366. }
  367. int fsl_is_pci_agent(struct pci_controller *hose)
  368. {
  369. u8 prog_if;
  370. pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
  371. pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &prog_if);
  372. return (prog_if == FSL_PROG_IF_AGENT);
  373. }
  374. int fsl_pci_init_port(struct fsl_pci_info *pci_info,
  375. struct pci_controller *hose, int busno)
  376. {
  377. volatile ccsr_fsl_pci_t *pci;
  378. struct pci_region *r;
  379. pci = (ccsr_fsl_pci_t *) pci_info->regs;
  380. /* on non-PCIe controllers we don't have pme_msg_det so this code
  381. * should do nothing since the read will return 0
  382. */
  383. if (in_be32(&pci->pme_msg_det)) {
  384. out_be32(&pci->pme_msg_det, 0xffffffff);
  385. debug (" with errors. Clearing. Now 0x%08x",
  386. pci->pme_msg_det);
  387. }
  388. r = hose->regions + hose->region_count;
  389. /* outbound memory */
  390. pci_set_region(r++,
  391. pci_info->mem_bus,
  392. pci_info->mem_phys,
  393. pci_info->mem_size,
  394. PCI_REGION_MEM);
  395. /* outbound io */
  396. pci_set_region(r++,
  397. pci_info->io_bus,
  398. pci_info->io_phys,
  399. pci_info->io_size,
  400. PCI_REGION_IO);
  401. hose->region_count = r - hose->regions;
  402. hose->first_busno = busno;
  403. fsl_pci_init(hose, (u32)&pci->cfg_addr, (u32)&pci->cfg_data);
  404. if (fsl_is_pci_agent(hose)) {
  405. fsl_pci_config_unlock(hose);
  406. hose->last_busno = hose->first_busno;
  407. }
  408. printf(" PCIE%x on bus %02x - %02x\n", pci_info->pci_num,
  409. hose->first_busno, hose->last_busno);
  410. return(hose->last_busno + 1);
  411. }
  412. /* Enable inbound PCI config cycles for agent/endpoint interface */
  413. void fsl_pci_config_unlock(struct pci_controller *hose)
  414. {
  415. pci_dev_t dev = PCI_BDF(hose->first_busno,0,0);
  416. u8 agent;
  417. u8 pcie_cap;
  418. u16 pbfr;
  419. pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &agent);
  420. if (!agent)
  421. return;
  422. pci_hose_read_config_byte(hose, dev, FSL_PCIE_CAP_ID, &pcie_cap);
  423. if (pcie_cap != 0x0) {
  424. /* PCIe - set CFG_READY bit of Configuration Ready Register */
  425. pci_hose_write_config_byte(hose, dev, FSL_PCIE_CFG_RDY, 0x1);
  426. } else {
  427. /* PCI - clear ACL bit of PBFR */
  428. pci_hose_read_config_word(hose, dev, FSL_PCI_PBFR, &pbfr);
  429. pbfr &= ~0x20;
  430. pci_hose_write_config_word(hose, dev, FSL_PCI_PBFR, pbfr);
  431. }
  432. }
  433. #ifdef CONFIG_OF_BOARD_SETUP
  434. #include <libfdt.h>
  435. #include <fdt_support.h>
  436. void ft_fsl_pci_setup(void *blob, const char *pci_alias,
  437. struct pci_controller *hose)
  438. {
  439. int off = fdt_path_offset(blob, pci_alias);
  440. u32 bus_range[2];
  441. if (off < 0)
  442. return;
  443. /* We assume a cfg_addr not being set means we didn't setup the controller */
  444. if ((hose == NULL) || (hose->cfg_addr == NULL)) {
  445. fdt_del_node_and_alias(blob, pci_alias);
  446. } else {
  447. bus_range[0] = 0;
  448. bus_range[1] = hose->last_busno - hose->first_busno;
  449. fdt_setprop(blob, off, "bus-range", &bus_range[0], 2*4);
  450. fdt_pci_dma_ranges(blob, off, hose);
  451. }
  452. }
  453. #endif