fsl_pci.c 33 KB

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