fsl_pci_init.c 15 KB

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