fsl_pci.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. /*
  2. * MPC83xx/85xx/86xx PCI/PCIE support routing.
  3. *
  4. * Copyright 2007-2012 Freescale Semiconductor, Inc.
  5. * Copyright 2008-2009 MontaVista Software, Inc.
  6. *
  7. * Initial author: Xianghua Xiao <x.xiao@freescale.com>
  8. * Recode: ZHANG WEI <wei.zhang@freescale.com>
  9. * Rewrite the routing for Frescale PCI and PCI Express
  10. * Roy Zang <tie-fei.zang@freescale.com>
  11. * MPC83xx PCI-Express support:
  12. * Tony Li <tony.li@freescale.com>
  13. * Anton Vorontsov <avorontsov@ru.mvista.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the
  17. * Free Software Foundation; either version 2 of the License, or (at your
  18. * option) any later version.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/pci.h>
  22. #include <linux/delay.h>
  23. #include <linux/string.h>
  24. #include <linux/fsl/edac.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/memblock.h>
  28. #include <linux/log2.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/slab.h>
  31. #include <linux/suspend.h>
  32. #include <linux/syscore_ops.h>
  33. #include <linux/uaccess.h>
  34. #include <asm/io.h>
  35. #include <asm/prom.h>
  36. #include <asm/pci-bridge.h>
  37. #include <asm/ppc-pci.h>
  38. #include <asm/machdep.h>
  39. #include <asm/mpc85xx.h>
  40. #include <asm/disassemble.h>
  41. #include <asm/ppc-opcode.h>
  42. #include <sysdev/fsl_soc.h>
  43. #include <sysdev/fsl_pci.h>
  44. static int fsl_pcie_bus_fixup, is_mpc83xx_pci;
  45. static void quirk_fsl_pcie_early(struct pci_dev *dev)
  46. {
  47. u8 hdr_type;
  48. /* if we aren't a PCIe don't bother */
  49. if (!pci_is_pcie(dev))
  50. return;
  51. /* if we aren't in host mode don't bother */
  52. pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type);
  53. if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE)
  54. return;
  55. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  56. fsl_pcie_bus_fixup = 1;
  57. return;
  58. }
  59. static int fsl_indirect_read_config(struct pci_bus *, unsigned int,
  60. int, int, u32 *);
  61. static int fsl_pcie_check_link(struct pci_controller *hose)
  62. {
  63. u32 val = 0;
  64. if (hose->indirect_type & PPC_INDIRECT_TYPE_FSL_CFG_REG_LINK) {
  65. if (hose->ops->read == fsl_indirect_read_config)
  66. __indirect_read_config(hose, hose->first_busno, 0,
  67. PCIE_LTSSM, 4, &val);
  68. else
  69. early_read_config_dword(hose, 0, 0, PCIE_LTSSM, &val);
  70. if (val < PCIE_LTSSM_L0)
  71. return 1;
  72. } else {
  73. struct ccsr_pci __iomem *pci = hose->private_data;
  74. /* for PCIe IP rev 3.0 or greater use CSR0 for link state */
  75. val = (in_be32(&pci->pex_csr0) & PEX_CSR0_LTSSM_MASK)
  76. >> PEX_CSR0_LTSSM_SHIFT;
  77. if (val != PEX_CSR0_LTSSM_L0)
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. static int fsl_indirect_read_config(struct pci_bus *bus, unsigned int devfn,
  83. int offset, int len, u32 *val)
  84. {
  85. struct pci_controller *hose = pci_bus_to_host(bus);
  86. if (fsl_pcie_check_link(hose))
  87. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  88. else
  89. hose->indirect_type &= ~PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  90. return indirect_read_config(bus, devfn, offset, len, val);
  91. }
  92. #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
  93. static struct pci_ops fsl_indirect_pcie_ops =
  94. {
  95. .read = fsl_indirect_read_config,
  96. .write = indirect_write_config,
  97. };
  98. static u64 pci64_dma_offset;
  99. #ifdef CONFIG_SWIOTLB
  100. static void setup_swiotlb_ops(struct pci_controller *hose)
  101. {
  102. if (ppc_swiotlb_enable) {
  103. hose->controller_ops.dma_dev_setup = pci_dma_dev_setup_swiotlb;
  104. set_pci_dma_ops(&powerpc_swiotlb_dma_ops);
  105. }
  106. }
  107. #else
  108. static inline void setup_swiotlb_ops(struct pci_controller *hose) {}
  109. #endif
  110. static int fsl_pci_dma_set_mask(struct device *dev, u64 dma_mask)
  111. {
  112. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  113. return -EIO;
  114. /*
  115. * Fix up PCI devices that are able to DMA to the large inbound
  116. * mapping that allows addressing any RAM address from across PCI.
  117. */
  118. if (dev_is_pci(dev) && dma_mask >= pci64_dma_offset * 2 - 1) {
  119. set_dma_ops(dev, &dma_nommu_ops);
  120. set_dma_offset(dev, pci64_dma_offset);
  121. }
  122. *dev->dma_mask = dma_mask;
  123. return 0;
  124. }
  125. static int setup_one_atmu(struct ccsr_pci __iomem *pci,
  126. unsigned int index, const struct resource *res,
  127. resource_size_t offset)
  128. {
  129. resource_size_t pci_addr = res->start - offset;
  130. resource_size_t phys_addr = res->start;
  131. resource_size_t size = resource_size(res);
  132. u32 flags = 0x80044000; /* enable & mem R/W */
  133. unsigned int i;
  134. pr_debug("PCI MEM resource start 0x%016llx, size 0x%016llx.\n",
  135. (u64)res->start, (u64)size);
  136. if (res->flags & IORESOURCE_PREFETCH)
  137. flags |= 0x10000000; /* enable relaxed ordering */
  138. for (i = 0; size > 0; i++) {
  139. unsigned int bits = min_t(u32, ilog2(size),
  140. __ffs(pci_addr | phys_addr));
  141. if (index + i >= 5)
  142. return -1;
  143. out_be32(&pci->pow[index + i].potar, pci_addr >> 12);
  144. out_be32(&pci->pow[index + i].potear, (u64)pci_addr >> 44);
  145. out_be32(&pci->pow[index + i].powbar, phys_addr >> 12);
  146. out_be32(&pci->pow[index + i].powar, flags | (bits - 1));
  147. pci_addr += (resource_size_t)1U << bits;
  148. phys_addr += (resource_size_t)1U << bits;
  149. size -= (resource_size_t)1U << bits;
  150. }
  151. return i;
  152. }
  153. static bool is_kdump(void)
  154. {
  155. struct device_node *node;
  156. node = of_find_node_by_type(NULL, "memory");
  157. if (!node) {
  158. WARN_ON_ONCE(1);
  159. return false;
  160. }
  161. return of_property_read_bool(node, "linux,usable-memory");
  162. }
  163. /* atmu setup for fsl pci/pcie controller */
  164. static void setup_pci_atmu(struct pci_controller *hose)
  165. {
  166. struct ccsr_pci __iomem *pci = hose->private_data;
  167. int i, j, n, mem_log, win_idx = 3, start_idx = 1, end_idx = 4;
  168. u64 mem, sz, paddr_hi = 0;
  169. u64 offset = 0, paddr_lo = ULLONG_MAX;
  170. u32 pcicsrbar = 0, pcicsrbar_sz;
  171. u32 piwar = PIWAR_EN | PIWAR_PF | PIWAR_TGI_LOCAL |
  172. PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
  173. const u64 *reg;
  174. int len;
  175. bool setup_inbound;
  176. /*
  177. * If this is kdump, we don't want to trigger a bunch of PCI
  178. * errors by closing the window on in-flight DMA.
  179. *
  180. * We still run most of the function's logic so that things like
  181. * hose->dma_window_size still get set.
  182. */
  183. setup_inbound = !is_kdump();
  184. if (of_device_is_compatible(hose->dn, "fsl,bsc9132-pcie")) {
  185. /*
  186. * BSC9132 Rev1.0 has an issue where all the PEX inbound
  187. * windows have implemented the default target value as 0xf
  188. * for CCSR space.In all Freescale legacy devices the target
  189. * of 0xf is reserved for local memory space. 9132 Rev1.0
  190. * now has local mempry space mapped to target 0x0 instead of
  191. * 0xf. Hence adding a workaround to remove the target 0xf
  192. * defined for memory space from Inbound window attributes.
  193. */
  194. piwar &= ~PIWAR_TGI_LOCAL;
  195. }
  196. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  197. if (in_be32(&pci->block_rev1) >= PCIE_IP_REV_2_2) {
  198. win_idx = 2;
  199. start_idx = 0;
  200. end_idx = 3;
  201. }
  202. }
  203. /* Disable all windows (except powar0 since it's ignored) */
  204. for(i = 1; i < 5; i++)
  205. out_be32(&pci->pow[i].powar, 0);
  206. if (setup_inbound) {
  207. for (i = start_idx; i < end_idx; i++)
  208. out_be32(&pci->piw[i].piwar, 0);
  209. }
  210. /* Setup outbound MEM window */
  211. for(i = 0, j = 1; i < 3; i++) {
  212. if (!(hose->mem_resources[i].flags & IORESOURCE_MEM))
  213. continue;
  214. paddr_lo = min(paddr_lo, (u64)hose->mem_resources[i].start);
  215. paddr_hi = max(paddr_hi, (u64)hose->mem_resources[i].end);
  216. /* We assume all memory resources have the same offset */
  217. offset = hose->mem_offset[i];
  218. n = setup_one_atmu(pci, j, &hose->mem_resources[i], offset);
  219. if (n < 0 || j >= 5) {
  220. pr_err("Ran out of outbound PCI ATMUs for resource %d!\n", i);
  221. hose->mem_resources[i].flags |= IORESOURCE_DISABLED;
  222. } else
  223. j += n;
  224. }
  225. /* Setup outbound IO window */
  226. if (hose->io_resource.flags & IORESOURCE_IO) {
  227. if (j >= 5) {
  228. pr_err("Ran out of outbound PCI ATMUs for IO resource\n");
  229. } else {
  230. pr_debug("PCI IO resource start 0x%016llx, size 0x%016llx, "
  231. "phy base 0x%016llx.\n",
  232. (u64)hose->io_resource.start,
  233. (u64)resource_size(&hose->io_resource),
  234. (u64)hose->io_base_phys);
  235. out_be32(&pci->pow[j].potar, (hose->io_resource.start >> 12));
  236. out_be32(&pci->pow[j].potear, 0);
  237. out_be32(&pci->pow[j].powbar, (hose->io_base_phys >> 12));
  238. /* Enable, IO R/W */
  239. out_be32(&pci->pow[j].powar, 0x80088000
  240. | (ilog2(hose->io_resource.end
  241. - hose->io_resource.start + 1) - 1));
  242. }
  243. }
  244. /* convert to pci address space */
  245. paddr_hi -= offset;
  246. paddr_lo -= offset;
  247. if (paddr_hi == paddr_lo) {
  248. pr_err("%pOF: No outbound window space\n", hose->dn);
  249. return;
  250. }
  251. if (paddr_lo == 0) {
  252. pr_err("%pOF: No space for inbound window\n", hose->dn);
  253. return;
  254. }
  255. /* setup PCSRBAR/PEXCSRBAR */
  256. early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, 0xffffffff);
  257. early_read_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, &pcicsrbar_sz);
  258. pcicsrbar_sz = ~pcicsrbar_sz + 1;
  259. if (paddr_hi < (0x100000000ull - pcicsrbar_sz) ||
  260. (paddr_lo > 0x100000000ull))
  261. pcicsrbar = 0x100000000ull - pcicsrbar_sz;
  262. else
  263. pcicsrbar = (paddr_lo - pcicsrbar_sz) & -pcicsrbar_sz;
  264. early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, pcicsrbar);
  265. paddr_lo = min(paddr_lo, (u64)pcicsrbar);
  266. pr_info("%pOF: PCICSRBAR @ 0x%x\n", hose->dn, pcicsrbar);
  267. /* Setup inbound mem window */
  268. mem = memblock_end_of_DRAM();
  269. pr_info("%s: end of DRAM %llx\n", __func__, mem);
  270. /*
  271. * The msi-address-64 property, if it exists, indicates the physical
  272. * address of the MSIIR register. Normally, this register is located
  273. * inside CCSR, so the ATMU that covers all of CCSR is used. But if
  274. * this property exists, then we normally need to create a new ATMU
  275. * for it. For now, however, we cheat. The only entity that creates
  276. * this property is the Freescale hypervisor, and the address is
  277. * specified in the partition configuration. Typically, the address
  278. * is located in the page immediately after the end of DDR. If so, we
  279. * can avoid allocating a new ATMU by extending the DDR ATMU by one
  280. * page.
  281. */
  282. reg = of_get_property(hose->dn, "msi-address-64", &len);
  283. if (reg && (len == sizeof(u64))) {
  284. u64 address = be64_to_cpup(reg);
  285. if ((address >= mem) && (address < (mem + PAGE_SIZE))) {
  286. pr_info("%pOF: extending DDR ATMU to cover MSIIR", hose->dn);
  287. mem += PAGE_SIZE;
  288. } else {
  289. /* TODO: Create a new ATMU for MSIIR */
  290. pr_warn("%pOF: msi-address-64 address of %llx is "
  291. "unsupported\n", hose->dn, address);
  292. }
  293. }
  294. sz = min(mem, paddr_lo);
  295. mem_log = ilog2(sz);
  296. /* PCIe can overmap inbound & outbound since RX & TX are separated */
  297. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  298. /* Size window to exact size if power-of-two or one size up */
  299. if ((1ull << mem_log) != mem) {
  300. mem_log++;
  301. if ((1ull << mem_log) > mem)
  302. pr_info("%pOF: Setting PCI inbound window "
  303. "greater than memory size\n", hose->dn);
  304. }
  305. piwar |= ((mem_log - 1) & PIWAR_SZ_MASK);
  306. if (setup_inbound) {
  307. /* Setup inbound memory window */
  308. out_be32(&pci->piw[win_idx].pitar, 0x00000000);
  309. out_be32(&pci->piw[win_idx].piwbar, 0x00000000);
  310. out_be32(&pci->piw[win_idx].piwar, piwar);
  311. }
  312. win_idx--;
  313. hose->dma_window_base_cur = 0x00000000;
  314. hose->dma_window_size = (resource_size_t)sz;
  315. /*
  316. * if we have >4G of memory setup second PCI inbound window to
  317. * let devices that are 64-bit address capable to work w/o
  318. * SWIOTLB and access the full range of memory
  319. */
  320. if (sz != mem) {
  321. mem_log = ilog2(mem);
  322. /* Size window up if we dont fit in exact power-of-2 */
  323. if ((1ull << mem_log) != mem)
  324. mem_log++;
  325. piwar = (piwar & ~PIWAR_SZ_MASK) | (mem_log - 1);
  326. pci64_dma_offset = 1ULL << mem_log;
  327. if (setup_inbound) {
  328. /* Setup inbound memory window */
  329. out_be32(&pci->piw[win_idx].pitar, 0x00000000);
  330. out_be32(&pci->piw[win_idx].piwbear,
  331. pci64_dma_offset >> 44);
  332. out_be32(&pci->piw[win_idx].piwbar,
  333. pci64_dma_offset >> 12);
  334. out_be32(&pci->piw[win_idx].piwar, piwar);
  335. }
  336. /*
  337. * install our own dma_set_mask handler to fixup dma_ops
  338. * and dma_offset
  339. */
  340. ppc_md.dma_set_mask = fsl_pci_dma_set_mask;
  341. pr_info("%pOF: Setup 64-bit PCI DMA window\n", hose->dn);
  342. }
  343. } else {
  344. u64 paddr = 0;
  345. if (setup_inbound) {
  346. /* Setup inbound memory window */
  347. out_be32(&pci->piw[win_idx].pitar, paddr >> 12);
  348. out_be32(&pci->piw[win_idx].piwbar, paddr >> 12);
  349. out_be32(&pci->piw[win_idx].piwar,
  350. (piwar | (mem_log - 1)));
  351. }
  352. win_idx--;
  353. paddr += 1ull << mem_log;
  354. sz -= 1ull << mem_log;
  355. if (sz) {
  356. mem_log = ilog2(sz);
  357. piwar |= (mem_log - 1);
  358. if (setup_inbound) {
  359. out_be32(&pci->piw[win_idx].pitar,
  360. paddr >> 12);
  361. out_be32(&pci->piw[win_idx].piwbar,
  362. paddr >> 12);
  363. out_be32(&pci->piw[win_idx].piwar, piwar);
  364. }
  365. win_idx--;
  366. paddr += 1ull << mem_log;
  367. }
  368. hose->dma_window_base_cur = 0x00000000;
  369. hose->dma_window_size = (resource_size_t)paddr;
  370. }
  371. if (hose->dma_window_size < mem) {
  372. #ifdef CONFIG_SWIOTLB
  373. ppc_swiotlb_enable = 1;
  374. #else
  375. pr_err("%pOF: ERROR: Memory size exceeds PCI ATMU ability to "
  376. "map - enable CONFIG_SWIOTLB to avoid dma errors.\n",
  377. hose->dn);
  378. #endif
  379. /* adjusting outbound windows could reclaim space in mem map */
  380. if (paddr_hi < 0xffffffffull)
  381. pr_warn("%pOF: WARNING: Outbound window cfg leaves "
  382. "gaps in memory map. Adjusting the memory map "
  383. "could reduce unnecessary bounce buffering.\n",
  384. hose->dn);
  385. pr_info("%pOF: DMA window size is 0x%llx\n", hose->dn,
  386. (u64)hose->dma_window_size);
  387. }
  388. }
  389. static void __init setup_pci_cmd(struct pci_controller *hose)
  390. {
  391. u16 cmd;
  392. int cap_x;
  393. early_read_config_word(hose, 0, 0, PCI_COMMAND, &cmd);
  394. cmd |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY
  395. | PCI_COMMAND_IO;
  396. early_write_config_word(hose, 0, 0, PCI_COMMAND, cmd);
  397. cap_x = early_find_capability(hose, 0, 0, PCI_CAP_ID_PCIX);
  398. if (cap_x) {
  399. int pci_x_cmd = cap_x + PCI_X_CMD;
  400. cmd = PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ
  401. | PCI_X_CMD_ERO | PCI_X_CMD_DPERR_E;
  402. early_write_config_word(hose, 0, 0, pci_x_cmd, cmd);
  403. } else {
  404. early_write_config_byte(hose, 0, 0, PCI_LATENCY_TIMER, 0x80);
  405. }
  406. }
  407. void fsl_pcibios_fixup_bus(struct pci_bus *bus)
  408. {
  409. struct pci_controller *hose = pci_bus_to_host(bus);
  410. int i, is_pcie = 0, no_link;
  411. /* The root complex bridge comes up with bogus resources,
  412. * we copy the PHB ones in.
  413. *
  414. * With the current generic PCI code, the PHB bus no longer
  415. * has bus->resource[0..4] set, so things are a bit more
  416. * tricky.
  417. */
  418. if (fsl_pcie_bus_fixup)
  419. is_pcie = early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP);
  420. no_link = !!(hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK);
  421. if (bus->parent == hose->bus && (is_pcie || no_link)) {
  422. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; ++i) {
  423. struct resource *res = bus->resource[i];
  424. struct resource *par;
  425. if (!res)
  426. continue;
  427. if (i == 0)
  428. par = &hose->io_resource;
  429. else if (i < 4)
  430. par = &hose->mem_resources[i-1];
  431. else par = NULL;
  432. res->start = par ? par->start : 0;
  433. res->end = par ? par->end : 0;
  434. res->flags = par ? par->flags : 0;
  435. }
  436. }
  437. }
  438. int fsl_add_bridge(struct platform_device *pdev, int is_primary)
  439. {
  440. int len;
  441. struct pci_controller *hose;
  442. struct resource rsrc;
  443. const int *bus_range;
  444. u8 hdr_type, progif;
  445. struct device_node *dev;
  446. struct ccsr_pci __iomem *pci;
  447. u16 temp;
  448. u32 svr = mfspr(SPRN_SVR);
  449. dev = pdev->dev.of_node;
  450. if (!of_device_is_available(dev)) {
  451. pr_warn("%pOF: disabled\n", dev);
  452. return -ENODEV;
  453. }
  454. pr_debug("Adding PCI host bridge %pOF\n", dev);
  455. /* Fetch host bridge registers address */
  456. if (of_address_to_resource(dev, 0, &rsrc)) {
  457. printk(KERN_WARNING "Can't get pci register base!");
  458. return -ENOMEM;
  459. }
  460. /* Get bus range if any */
  461. bus_range = of_get_property(dev, "bus-range", &len);
  462. if (bus_range == NULL || len < 2 * sizeof(int))
  463. printk(KERN_WARNING "Can't get bus-range for %pOF, assume"
  464. " bus 0\n", dev);
  465. pci_add_flags(PCI_REASSIGN_ALL_BUS);
  466. hose = pcibios_alloc_controller(dev);
  467. if (!hose)
  468. return -ENOMEM;
  469. /* set platform device as the parent */
  470. hose->parent = &pdev->dev;
  471. hose->first_busno = bus_range ? bus_range[0] : 0x0;
  472. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  473. pr_debug("PCI memory map start 0x%016llx, size 0x%016llx\n",
  474. (u64)rsrc.start, (u64)resource_size(&rsrc));
  475. pci = hose->private_data = ioremap(rsrc.start, resource_size(&rsrc));
  476. if (!hose->private_data)
  477. goto no_bridge;
  478. setup_indirect_pci(hose, rsrc.start, rsrc.start + 0x4,
  479. PPC_INDIRECT_TYPE_BIG_ENDIAN);
  480. if (in_be32(&pci->block_rev1) < PCIE_IP_REV_3_0)
  481. hose->indirect_type |= PPC_INDIRECT_TYPE_FSL_CFG_REG_LINK;
  482. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  483. /* use fsl_indirect_read_config for PCIe */
  484. hose->ops = &fsl_indirect_pcie_ops;
  485. /* For PCIE read HEADER_TYPE to identify controller mode */
  486. early_read_config_byte(hose, 0, 0, PCI_HEADER_TYPE, &hdr_type);
  487. if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE)
  488. goto no_bridge;
  489. } else {
  490. /* For PCI read PROG to identify controller mode */
  491. early_read_config_byte(hose, 0, 0, PCI_CLASS_PROG, &progif);
  492. if ((progif & 1) &&
  493. !of_property_read_bool(dev, "fsl,pci-agent-force-enum"))
  494. goto no_bridge;
  495. }
  496. setup_pci_cmd(hose);
  497. /* check PCI express link status */
  498. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  499. hose->indirect_type |= PPC_INDIRECT_TYPE_EXT_REG |
  500. PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS;
  501. if (fsl_pcie_check_link(hose))
  502. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  503. } else {
  504. /*
  505. * Set PBFR(PCI Bus Function Register)[10] = 1 to
  506. * disable the combining of crossing cacheline
  507. * boundary requests into one burst transaction.
  508. * PCI-X operation is not affected.
  509. * Fix erratum PCI 5 on MPC8548
  510. */
  511. #define PCI_BUS_FUNCTION 0x44
  512. #define PCI_BUS_FUNCTION_MDS 0x400 /* Master disable streaming */
  513. if (((SVR_SOC_VER(svr) == SVR_8543) ||
  514. (SVR_SOC_VER(svr) == SVR_8545) ||
  515. (SVR_SOC_VER(svr) == SVR_8547) ||
  516. (SVR_SOC_VER(svr) == SVR_8548)) &&
  517. !early_find_capability(hose, 0, 0, PCI_CAP_ID_PCIX)) {
  518. early_read_config_word(hose, 0, 0,
  519. PCI_BUS_FUNCTION, &temp);
  520. temp |= PCI_BUS_FUNCTION_MDS;
  521. early_write_config_word(hose, 0, 0,
  522. PCI_BUS_FUNCTION, temp);
  523. }
  524. }
  525. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  526. "Firmware bus number: %d->%d\n",
  527. (unsigned long long)rsrc.start, hose->first_busno,
  528. hose->last_busno);
  529. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  530. hose, hose->cfg_addr, hose->cfg_data);
  531. /* Interpret the "ranges" property */
  532. /* This also maps the I/O region and sets isa_io/mem_base */
  533. pci_process_bridge_OF_ranges(hose, dev, is_primary);
  534. /* Setup PEX window registers */
  535. setup_pci_atmu(hose);
  536. /* Set up controller operations */
  537. setup_swiotlb_ops(hose);
  538. return 0;
  539. no_bridge:
  540. iounmap(hose->private_data);
  541. /* unmap cfg_data & cfg_addr separately if not on same page */
  542. if (((unsigned long)hose->cfg_data & PAGE_MASK) !=
  543. ((unsigned long)hose->cfg_addr & PAGE_MASK))
  544. iounmap(hose->cfg_data);
  545. iounmap(hose->cfg_addr);
  546. pcibios_free_controller(hose);
  547. return -ENODEV;
  548. }
  549. #endif /* CONFIG_FSL_SOC_BOOKE || CONFIG_PPC_86xx */
  550. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID,
  551. quirk_fsl_pcie_early);
  552. #if defined(CONFIG_PPC_83xx) || defined(CONFIG_PPC_MPC512x)
  553. struct mpc83xx_pcie_priv {
  554. void __iomem *cfg_type0;
  555. void __iomem *cfg_type1;
  556. u32 dev_base;
  557. };
  558. struct pex_inbound_window {
  559. u32 ar;
  560. u32 tar;
  561. u32 barl;
  562. u32 barh;
  563. };
  564. /*
  565. * With the convention of u-boot, the PCIE outbound window 0 serves
  566. * as configuration transactions outbound.
  567. */
  568. #define PEX_OUTWIN0_BAR 0xCA4
  569. #define PEX_OUTWIN0_TAL 0xCA8
  570. #define PEX_OUTWIN0_TAH 0xCAC
  571. #define PEX_RC_INWIN_BASE 0xE60
  572. #define PEX_RCIWARn_EN 0x1
  573. static int mpc83xx_pcie_exclude_device(struct pci_bus *bus, unsigned int devfn)
  574. {
  575. struct pci_controller *hose = pci_bus_to_host(bus);
  576. if (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)
  577. return PCIBIOS_DEVICE_NOT_FOUND;
  578. /*
  579. * Workaround for the HW bug: for Type 0 configure transactions the
  580. * PCI-E controller does not check the device number bits and just
  581. * assumes that the device number bits are 0.
  582. */
  583. if (bus->number == hose->first_busno ||
  584. bus->primary == hose->first_busno) {
  585. if (devfn & 0xf8)
  586. return PCIBIOS_DEVICE_NOT_FOUND;
  587. }
  588. if (ppc_md.pci_exclude_device) {
  589. if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
  590. return PCIBIOS_DEVICE_NOT_FOUND;
  591. }
  592. return PCIBIOS_SUCCESSFUL;
  593. }
  594. static void __iomem *mpc83xx_pcie_remap_cfg(struct pci_bus *bus,
  595. unsigned int devfn, int offset)
  596. {
  597. struct pci_controller *hose = pci_bus_to_host(bus);
  598. struct mpc83xx_pcie_priv *pcie = hose->dn->data;
  599. u32 dev_base = bus->number << 24 | devfn << 16;
  600. int ret;
  601. ret = mpc83xx_pcie_exclude_device(bus, devfn);
  602. if (ret)
  603. return NULL;
  604. offset &= 0xfff;
  605. /* Type 0 */
  606. if (bus->number == hose->first_busno)
  607. return pcie->cfg_type0 + offset;
  608. if (pcie->dev_base == dev_base)
  609. goto mapped;
  610. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, dev_base);
  611. pcie->dev_base = dev_base;
  612. mapped:
  613. return pcie->cfg_type1 + offset;
  614. }
  615. static int mpc83xx_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
  616. int offset, int len, u32 val)
  617. {
  618. struct pci_controller *hose = pci_bus_to_host(bus);
  619. /* PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS */
  620. if (offset == PCI_PRIMARY_BUS && bus->number == hose->first_busno)
  621. val &= 0xffffff00;
  622. return pci_generic_config_write(bus, devfn, offset, len, val);
  623. }
  624. static struct pci_ops mpc83xx_pcie_ops = {
  625. .map_bus = mpc83xx_pcie_remap_cfg,
  626. .read = pci_generic_config_read,
  627. .write = mpc83xx_pcie_write_config,
  628. };
  629. static int __init mpc83xx_pcie_setup(struct pci_controller *hose,
  630. struct resource *reg)
  631. {
  632. struct mpc83xx_pcie_priv *pcie;
  633. u32 cfg_bar;
  634. int ret = -ENOMEM;
  635. pcie = zalloc_maybe_bootmem(sizeof(*pcie), GFP_KERNEL);
  636. if (!pcie)
  637. return ret;
  638. pcie->cfg_type0 = ioremap(reg->start, resource_size(reg));
  639. if (!pcie->cfg_type0)
  640. goto err0;
  641. cfg_bar = in_le32(pcie->cfg_type0 + PEX_OUTWIN0_BAR);
  642. if (!cfg_bar) {
  643. /* PCI-E isn't configured. */
  644. ret = -ENODEV;
  645. goto err1;
  646. }
  647. pcie->cfg_type1 = ioremap(cfg_bar, 0x1000);
  648. if (!pcie->cfg_type1)
  649. goto err1;
  650. WARN_ON(hose->dn->data);
  651. hose->dn->data = pcie;
  652. hose->ops = &mpc83xx_pcie_ops;
  653. hose->indirect_type |= PPC_INDIRECT_TYPE_FSL_CFG_REG_LINK;
  654. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAH, 0);
  655. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, 0);
  656. if (fsl_pcie_check_link(hose))
  657. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  658. return 0;
  659. err1:
  660. iounmap(pcie->cfg_type0);
  661. err0:
  662. kfree(pcie);
  663. return ret;
  664. }
  665. int __init mpc83xx_add_bridge(struct device_node *dev)
  666. {
  667. int ret;
  668. int len;
  669. struct pci_controller *hose;
  670. struct resource rsrc_reg;
  671. struct resource rsrc_cfg;
  672. const int *bus_range;
  673. int primary;
  674. is_mpc83xx_pci = 1;
  675. if (!of_device_is_available(dev)) {
  676. pr_warn("%pOF: disabled by the firmware.\n",
  677. dev);
  678. return -ENODEV;
  679. }
  680. pr_debug("Adding PCI host bridge %pOF\n", dev);
  681. /* Fetch host bridge registers address */
  682. if (of_address_to_resource(dev, 0, &rsrc_reg)) {
  683. printk(KERN_WARNING "Can't get pci register base!\n");
  684. return -ENOMEM;
  685. }
  686. memset(&rsrc_cfg, 0, sizeof(rsrc_cfg));
  687. if (of_address_to_resource(dev, 1, &rsrc_cfg)) {
  688. printk(KERN_WARNING
  689. "No pci config register base in dev tree, "
  690. "using default\n");
  691. /*
  692. * MPC83xx supports up to two host controllers
  693. * one at 0x8500 has config space registers at 0x8300
  694. * one at 0x8600 has config space registers at 0x8380
  695. */
  696. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  697. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8300;
  698. else if ((rsrc_reg.start & 0xfffff) == 0x8600)
  699. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8380;
  700. }
  701. /*
  702. * Controller at offset 0x8500 is primary
  703. */
  704. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  705. primary = 1;
  706. else
  707. primary = 0;
  708. /* Get bus range if any */
  709. bus_range = of_get_property(dev, "bus-range", &len);
  710. if (bus_range == NULL || len < 2 * sizeof(int)) {
  711. printk(KERN_WARNING "Can't get bus-range for %pOF, assume"
  712. " bus 0\n", dev);
  713. }
  714. pci_add_flags(PCI_REASSIGN_ALL_BUS);
  715. hose = pcibios_alloc_controller(dev);
  716. if (!hose)
  717. return -ENOMEM;
  718. hose->first_busno = bus_range ? bus_range[0] : 0;
  719. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  720. if (of_device_is_compatible(dev, "fsl,mpc8314-pcie")) {
  721. ret = mpc83xx_pcie_setup(hose, &rsrc_reg);
  722. if (ret)
  723. goto err0;
  724. } else {
  725. setup_indirect_pci(hose, rsrc_cfg.start,
  726. rsrc_cfg.start + 4, 0);
  727. }
  728. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  729. "Firmware bus number: %d->%d\n",
  730. (unsigned long long)rsrc_reg.start, hose->first_busno,
  731. hose->last_busno);
  732. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  733. hose, hose->cfg_addr, hose->cfg_data);
  734. /* Interpret the "ranges" property */
  735. /* This also maps the I/O region and sets isa_io/mem_base */
  736. pci_process_bridge_OF_ranges(hose, dev, primary);
  737. return 0;
  738. err0:
  739. pcibios_free_controller(hose);
  740. return ret;
  741. }
  742. #endif /* CONFIG_PPC_83xx */
  743. u64 fsl_pci_immrbar_base(struct pci_controller *hose)
  744. {
  745. #ifdef CONFIG_PPC_83xx
  746. if (is_mpc83xx_pci) {
  747. struct mpc83xx_pcie_priv *pcie = hose->dn->data;
  748. struct pex_inbound_window *in;
  749. int i;
  750. /* Walk the Root Complex Inbound windows to match IMMR base */
  751. in = pcie->cfg_type0 + PEX_RC_INWIN_BASE;
  752. for (i = 0; i < 4; i++) {
  753. /* not enabled, skip */
  754. if (!(in_le32(&in[i].ar) & PEX_RCIWARn_EN))
  755. continue;
  756. if (get_immrbase() == in_le32(&in[i].tar))
  757. return (u64)in_le32(&in[i].barh) << 32 |
  758. in_le32(&in[i].barl);
  759. }
  760. printk(KERN_WARNING "could not find PCI BAR matching IMMR\n");
  761. }
  762. #endif
  763. #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
  764. if (!is_mpc83xx_pci) {
  765. u32 base;
  766. pci_bus_read_config_dword(hose->bus,
  767. PCI_DEVFN(0, 0), PCI_BASE_ADDRESS_0, &base);
  768. /*
  769. * For PEXCSRBAR, bit 3-0 indicate prefetchable and
  770. * address type. So when getting base address, these
  771. * bits should be masked
  772. */
  773. base &= PCI_BASE_ADDRESS_MEM_MASK;
  774. return base;
  775. }
  776. #endif
  777. return 0;
  778. }
  779. #ifdef CONFIG_E500
  780. static int mcheck_handle_load(struct pt_regs *regs, u32 inst)
  781. {
  782. unsigned int rd, ra, rb, d;
  783. rd = get_rt(inst);
  784. ra = get_ra(inst);
  785. rb = get_rb(inst);
  786. d = get_d(inst);
  787. switch (get_op(inst)) {
  788. case 31:
  789. switch (get_xop(inst)) {
  790. case OP_31_XOP_LWZX:
  791. case OP_31_XOP_LWBRX:
  792. regs->gpr[rd] = 0xffffffff;
  793. break;
  794. case OP_31_XOP_LWZUX:
  795. regs->gpr[rd] = 0xffffffff;
  796. regs->gpr[ra] += regs->gpr[rb];
  797. break;
  798. case OP_31_XOP_LBZX:
  799. regs->gpr[rd] = 0xff;
  800. break;
  801. case OP_31_XOP_LBZUX:
  802. regs->gpr[rd] = 0xff;
  803. regs->gpr[ra] += regs->gpr[rb];
  804. break;
  805. case OP_31_XOP_LHZX:
  806. case OP_31_XOP_LHBRX:
  807. regs->gpr[rd] = 0xffff;
  808. break;
  809. case OP_31_XOP_LHZUX:
  810. regs->gpr[rd] = 0xffff;
  811. regs->gpr[ra] += regs->gpr[rb];
  812. break;
  813. case OP_31_XOP_LHAX:
  814. regs->gpr[rd] = ~0UL;
  815. break;
  816. case OP_31_XOP_LHAUX:
  817. regs->gpr[rd] = ~0UL;
  818. regs->gpr[ra] += regs->gpr[rb];
  819. break;
  820. default:
  821. return 0;
  822. }
  823. break;
  824. case OP_LWZ:
  825. regs->gpr[rd] = 0xffffffff;
  826. break;
  827. case OP_LWZU:
  828. regs->gpr[rd] = 0xffffffff;
  829. regs->gpr[ra] += (s16)d;
  830. break;
  831. case OP_LBZ:
  832. regs->gpr[rd] = 0xff;
  833. break;
  834. case OP_LBZU:
  835. regs->gpr[rd] = 0xff;
  836. regs->gpr[ra] += (s16)d;
  837. break;
  838. case OP_LHZ:
  839. regs->gpr[rd] = 0xffff;
  840. break;
  841. case OP_LHZU:
  842. regs->gpr[rd] = 0xffff;
  843. regs->gpr[ra] += (s16)d;
  844. break;
  845. case OP_LHA:
  846. regs->gpr[rd] = ~0UL;
  847. break;
  848. case OP_LHAU:
  849. regs->gpr[rd] = ~0UL;
  850. regs->gpr[ra] += (s16)d;
  851. break;
  852. default:
  853. return 0;
  854. }
  855. return 1;
  856. }
  857. static int is_in_pci_mem_space(phys_addr_t addr)
  858. {
  859. struct pci_controller *hose;
  860. struct resource *res;
  861. int i;
  862. list_for_each_entry(hose, &hose_list, list_node) {
  863. if (!(hose->indirect_type & PPC_INDIRECT_TYPE_EXT_REG))
  864. continue;
  865. for (i = 0; i < 3; i++) {
  866. res = &hose->mem_resources[i];
  867. if ((res->flags & IORESOURCE_MEM) &&
  868. addr >= res->start && addr <= res->end)
  869. return 1;
  870. }
  871. }
  872. return 0;
  873. }
  874. int fsl_pci_mcheck_exception(struct pt_regs *regs)
  875. {
  876. u32 inst;
  877. int ret;
  878. phys_addr_t addr = 0;
  879. /* Let KVM/QEMU deal with the exception */
  880. if (regs->msr & MSR_GS)
  881. return 0;
  882. #ifdef CONFIG_PHYS_64BIT
  883. addr = mfspr(SPRN_MCARU);
  884. addr <<= 32;
  885. #endif
  886. addr += mfspr(SPRN_MCAR);
  887. if (is_in_pci_mem_space(addr)) {
  888. if (user_mode(regs)) {
  889. pagefault_disable();
  890. ret = get_user(inst, (__u32 __user *)regs->nip);
  891. pagefault_enable();
  892. } else {
  893. ret = probe_kernel_address((void *)regs->nip, inst);
  894. }
  895. if (!ret && mcheck_handle_load(regs, inst)) {
  896. regs->nip += 4;
  897. return 1;
  898. }
  899. }
  900. return 0;
  901. }
  902. #endif
  903. #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
  904. static const struct of_device_id pci_ids[] = {
  905. { .compatible = "fsl,mpc8540-pci", },
  906. { .compatible = "fsl,mpc8548-pcie", },
  907. { .compatible = "fsl,mpc8610-pci", },
  908. { .compatible = "fsl,mpc8641-pcie", },
  909. { .compatible = "fsl,qoriq-pcie", },
  910. { .compatible = "fsl,qoriq-pcie-v2.1", },
  911. { .compatible = "fsl,qoriq-pcie-v2.2", },
  912. { .compatible = "fsl,qoriq-pcie-v2.3", },
  913. { .compatible = "fsl,qoriq-pcie-v2.4", },
  914. { .compatible = "fsl,qoriq-pcie-v3.0", },
  915. /*
  916. * The following entries are for compatibility with older device
  917. * trees.
  918. */
  919. { .compatible = "fsl,p1022-pcie", },
  920. { .compatible = "fsl,p4080-pcie", },
  921. {},
  922. };
  923. struct device_node *fsl_pci_primary;
  924. void fsl_pci_assign_primary(void)
  925. {
  926. struct device_node *np;
  927. /* Callers can specify the primary bus using other means. */
  928. if (fsl_pci_primary)
  929. return;
  930. /* If a PCI host bridge contains an ISA node, it's primary. */
  931. np = of_find_node_by_type(NULL, "isa");
  932. while ((fsl_pci_primary = of_get_parent(np))) {
  933. of_node_put(np);
  934. np = fsl_pci_primary;
  935. if (of_match_node(pci_ids, np) && of_device_is_available(np))
  936. return;
  937. }
  938. /*
  939. * If there's no PCI host bridge with ISA, arbitrarily
  940. * designate one as primary. This can go away once
  941. * various bugs with primary-less systems are fixed.
  942. */
  943. for_each_matching_node(np, pci_ids) {
  944. if (of_device_is_available(np)) {
  945. fsl_pci_primary = np;
  946. of_node_put(np);
  947. return;
  948. }
  949. }
  950. }
  951. #ifdef CONFIG_PM_SLEEP
  952. static irqreturn_t fsl_pci_pme_handle(int irq, void *dev_id)
  953. {
  954. struct pci_controller *hose = dev_id;
  955. struct ccsr_pci __iomem *pci = hose->private_data;
  956. u32 dr;
  957. dr = in_be32(&pci->pex_pme_mes_dr);
  958. if (!dr)
  959. return IRQ_NONE;
  960. out_be32(&pci->pex_pme_mes_dr, dr);
  961. return IRQ_HANDLED;
  962. }
  963. static int fsl_pci_pme_probe(struct pci_controller *hose)
  964. {
  965. struct ccsr_pci __iomem *pci;
  966. struct pci_dev *dev;
  967. int pme_irq;
  968. int res;
  969. u16 pms;
  970. /* Get hose's pci_dev */
  971. dev = list_first_entry(&hose->bus->devices, typeof(*dev), bus_list);
  972. /* PME Disable */
  973. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pms);
  974. pms &= ~PCI_PM_CTRL_PME_ENABLE;
  975. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pms);
  976. pme_irq = irq_of_parse_and_map(hose->dn, 0);
  977. if (!pme_irq) {
  978. dev_err(&dev->dev, "Failed to map PME interrupt.\n");
  979. return -ENXIO;
  980. }
  981. res = devm_request_irq(hose->parent, pme_irq,
  982. fsl_pci_pme_handle,
  983. IRQF_SHARED,
  984. "[PCI] PME", hose);
  985. if (res < 0) {
  986. dev_err(&dev->dev, "Unable to request irq %d for PME\n", pme_irq);
  987. irq_dispose_mapping(pme_irq);
  988. return -ENODEV;
  989. }
  990. pci = hose->private_data;
  991. /* Enable PTOD, ENL23D & EXL23D */
  992. clrbits32(&pci->pex_pme_mes_disr,
  993. PME_DISR_EN_PTOD | PME_DISR_EN_ENL23D | PME_DISR_EN_EXL23D);
  994. out_be32(&pci->pex_pme_mes_ier, 0);
  995. setbits32(&pci->pex_pme_mes_ier,
  996. PME_DISR_EN_PTOD | PME_DISR_EN_ENL23D | PME_DISR_EN_EXL23D);
  997. /* PME Enable */
  998. pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pms);
  999. pms |= PCI_PM_CTRL_PME_ENABLE;
  1000. pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pms);
  1001. return 0;
  1002. }
  1003. static void send_pme_turnoff_message(struct pci_controller *hose)
  1004. {
  1005. struct ccsr_pci __iomem *pci = hose->private_data;
  1006. u32 dr;
  1007. int i;
  1008. /* Send PME_Turn_Off Message Request */
  1009. setbits32(&pci->pex_pmcr, PEX_PMCR_PTOMR);
  1010. /* Wait trun off done */
  1011. for (i = 0; i < 150; i++) {
  1012. dr = in_be32(&pci->pex_pme_mes_dr);
  1013. if (dr) {
  1014. out_be32(&pci->pex_pme_mes_dr, dr);
  1015. break;
  1016. }
  1017. udelay(1000);
  1018. }
  1019. }
  1020. static void fsl_pci_syscore_do_suspend(struct pci_controller *hose)
  1021. {
  1022. send_pme_turnoff_message(hose);
  1023. }
  1024. static int fsl_pci_syscore_suspend(void)
  1025. {
  1026. struct pci_controller *hose, *tmp;
  1027. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  1028. fsl_pci_syscore_do_suspend(hose);
  1029. return 0;
  1030. }
  1031. static void fsl_pci_syscore_do_resume(struct pci_controller *hose)
  1032. {
  1033. struct ccsr_pci __iomem *pci = hose->private_data;
  1034. u32 dr;
  1035. int i;
  1036. /* Send Exit L2 State Message */
  1037. setbits32(&pci->pex_pmcr, PEX_PMCR_EXL2S);
  1038. /* Wait exit done */
  1039. for (i = 0; i < 150; i++) {
  1040. dr = in_be32(&pci->pex_pme_mes_dr);
  1041. if (dr) {
  1042. out_be32(&pci->pex_pme_mes_dr, dr);
  1043. break;
  1044. }
  1045. udelay(1000);
  1046. }
  1047. setup_pci_atmu(hose);
  1048. }
  1049. static void fsl_pci_syscore_resume(void)
  1050. {
  1051. struct pci_controller *hose, *tmp;
  1052. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  1053. fsl_pci_syscore_do_resume(hose);
  1054. }
  1055. static struct syscore_ops pci_syscore_pm_ops = {
  1056. .suspend = fsl_pci_syscore_suspend,
  1057. .resume = fsl_pci_syscore_resume,
  1058. };
  1059. #endif
  1060. void fsl_pcibios_fixup_phb(struct pci_controller *phb)
  1061. {
  1062. #ifdef CONFIG_PM_SLEEP
  1063. fsl_pci_pme_probe(phb);
  1064. #endif
  1065. }
  1066. static int add_err_dev(struct platform_device *pdev)
  1067. {
  1068. struct platform_device *errdev;
  1069. struct mpc85xx_edac_pci_plat_data pd = {
  1070. .of_node = pdev->dev.of_node
  1071. };
  1072. errdev = platform_device_register_resndata(&pdev->dev,
  1073. "mpc85xx-pci-edac",
  1074. PLATFORM_DEVID_AUTO,
  1075. pdev->resource,
  1076. pdev->num_resources,
  1077. &pd, sizeof(pd));
  1078. return PTR_ERR_OR_ZERO(errdev);
  1079. }
  1080. static int fsl_pci_probe(struct platform_device *pdev)
  1081. {
  1082. struct device_node *node;
  1083. int ret;
  1084. node = pdev->dev.of_node;
  1085. ret = fsl_add_bridge(pdev, fsl_pci_primary == node);
  1086. if (ret)
  1087. return ret;
  1088. ret = add_err_dev(pdev);
  1089. if (ret)
  1090. dev_err(&pdev->dev, "couldn't register error device: %d\n",
  1091. ret);
  1092. return 0;
  1093. }
  1094. static struct platform_driver fsl_pci_driver = {
  1095. .driver = {
  1096. .name = "fsl-pci",
  1097. .of_match_table = pci_ids,
  1098. },
  1099. .probe = fsl_pci_probe,
  1100. };
  1101. static int __init fsl_pci_init(void)
  1102. {
  1103. #ifdef CONFIG_PM_SLEEP
  1104. register_syscore_ops(&pci_syscore_pm_ops);
  1105. #endif
  1106. return platform_driver_register(&fsl_pci_driver);
  1107. }
  1108. arch_initcall(fsl_pci_init);
  1109. #endif