fsl_pci.c 34 KB

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