fsl_pci.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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/init.h>
  25. #include <linux/bootmem.h>
  26. #include <linux/memblock.h>
  27. #include <linux/log2.h>
  28. #include <linux/slab.h>
  29. #include <linux/uaccess.h>
  30. #include <asm/io.h>
  31. #include <asm/prom.h>
  32. #include <asm/pci-bridge.h>
  33. #include <asm/ppc-pci.h>
  34. #include <asm/machdep.h>
  35. #include <asm/disassemble.h>
  36. #include <asm/ppc-opcode.h>
  37. #include <sysdev/fsl_soc.h>
  38. #include <sysdev/fsl_pci.h>
  39. static int fsl_pcie_bus_fixup, is_mpc83xx_pci;
  40. static void quirk_fsl_pcie_early(struct pci_dev *dev)
  41. {
  42. u8 hdr_type;
  43. /* if we aren't a PCIe don't bother */
  44. if (!pci_is_pcie(dev))
  45. return;
  46. /* if we aren't in host mode don't bother */
  47. pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type);
  48. if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE)
  49. return;
  50. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  51. fsl_pcie_bus_fixup = 1;
  52. return;
  53. }
  54. static int fsl_indirect_read_config(struct pci_bus *, unsigned int,
  55. int, int, u32 *);
  56. static int fsl_pcie_check_link(struct pci_controller *hose)
  57. {
  58. u32 val = 0;
  59. if (hose->indirect_type & PPC_INDIRECT_TYPE_FSL_CFG_REG_LINK) {
  60. if (hose->ops->read == fsl_indirect_read_config) {
  61. struct pci_bus bus;
  62. bus.number = hose->first_busno;
  63. bus.sysdata = hose;
  64. bus.ops = hose->ops;
  65. indirect_read_config(&bus, 0, PCIE_LTSSM, 4, &val);
  66. } else
  67. early_read_config_dword(hose, 0, 0, PCIE_LTSSM, &val);
  68. if (val < PCIE_LTSSM_L0)
  69. return 1;
  70. } else {
  71. struct ccsr_pci __iomem *pci = hose->private_data;
  72. /* for PCIe IP rev 3.0 or greater use CSR0 for link state */
  73. val = (in_be32(&pci->pex_csr0) & PEX_CSR0_LTSSM_MASK)
  74. >> PEX_CSR0_LTSSM_SHIFT;
  75. if (val != PEX_CSR0_LTSSM_L0)
  76. return 1;
  77. }
  78. return 0;
  79. }
  80. static int fsl_indirect_read_config(struct pci_bus *bus, unsigned int devfn,
  81. int offset, int len, u32 *val)
  82. {
  83. struct pci_controller *hose = pci_bus_to_host(bus);
  84. if (fsl_pcie_check_link(hose))
  85. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  86. else
  87. hose->indirect_type &= ~PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  88. return indirect_read_config(bus, devfn, offset, len, val);
  89. }
  90. #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
  91. static struct pci_ops fsl_indirect_pcie_ops =
  92. {
  93. .read = fsl_indirect_read_config,
  94. .write = indirect_write_config,
  95. };
  96. #define MAX_PHYS_ADDR_BITS 40
  97. static u64 pci64_dma_offset = 1ull << MAX_PHYS_ADDR_BITS;
  98. static int fsl_pci_dma_set_mask(struct device *dev, u64 dma_mask)
  99. {
  100. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  101. return -EIO;
  102. /*
  103. * Fixup PCI devices that are able to DMA to above the physical
  104. * address width of the SoC such that we can address any internal
  105. * SoC address from across PCI if needed
  106. */
  107. if ((dev->bus == &pci_bus_type) &&
  108. dma_mask >= DMA_BIT_MASK(MAX_PHYS_ADDR_BITS)) {
  109. set_dma_ops(dev, &dma_direct_ops);
  110. set_dma_offset(dev, pci64_dma_offset);
  111. }
  112. *dev->dma_mask = dma_mask;
  113. return 0;
  114. }
  115. static int setup_one_atmu(struct ccsr_pci __iomem *pci,
  116. unsigned int index, const struct resource *res,
  117. resource_size_t offset)
  118. {
  119. resource_size_t pci_addr = res->start - offset;
  120. resource_size_t phys_addr = res->start;
  121. resource_size_t size = resource_size(res);
  122. u32 flags = 0x80044000; /* enable & mem R/W */
  123. unsigned int i;
  124. pr_debug("PCI MEM resource start 0x%016llx, size 0x%016llx.\n",
  125. (u64)res->start, (u64)size);
  126. if (res->flags & IORESOURCE_PREFETCH)
  127. flags |= 0x10000000; /* enable relaxed ordering */
  128. for (i = 0; size > 0; i++) {
  129. unsigned int bits = min(ilog2(size),
  130. __ffs(pci_addr | phys_addr));
  131. if (index + i >= 5)
  132. return -1;
  133. out_be32(&pci->pow[index + i].potar, pci_addr >> 12);
  134. out_be32(&pci->pow[index + i].potear, (u64)pci_addr >> 44);
  135. out_be32(&pci->pow[index + i].powbar, phys_addr >> 12);
  136. out_be32(&pci->pow[index + i].powar, flags | (bits - 1));
  137. pci_addr += (resource_size_t)1U << bits;
  138. phys_addr += (resource_size_t)1U << bits;
  139. size -= (resource_size_t)1U << bits;
  140. }
  141. return i;
  142. }
  143. /* atmu setup for fsl pci/pcie controller */
  144. static void setup_pci_atmu(struct pci_controller *hose)
  145. {
  146. struct ccsr_pci __iomem *pci = hose->private_data;
  147. int i, j, n, mem_log, win_idx = 3, start_idx = 1, end_idx = 4;
  148. u64 mem, sz, paddr_hi = 0;
  149. u64 offset = 0, paddr_lo = ULLONG_MAX;
  150. u32 pcicsrbar = 0, pcicsrbar_sz;
  151. u32 piwar = PIWAR_EN | PIWAR_PF | PIWAR_TGI_LOCAL |
  152. PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
  153. const char *name = hose->dn->full_name;
  154. const u64 *reg;
  155. int len;
  156. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  157. if (in_be32(&pci->block_rev1) >= PCIE_IP_REV_2_2) {
  158. win_idx = 2;
  159. start_idx = 0;
  160. end_idx = 3;
  161. }
  162. }
  163. /* Disable all windows (except powar0 since it's ignored) */
  164. for(i = 1; i < 5; i++)
  165. out_be32(&pci->pow[i].powar, 0);
  166. for (i = start_idx; i < end_idx; i++)
  167. out_be32(&pci->piw[i].piwar, 0);
  168. /* Setup outbound MEM window */
  169. for(i = 0, j = 1; i < 3; i++) {
  170. if (!(hose->mem_resources[i].flags & IORESOURCE_MEM))
  171. continue;
  172. paddr_lo = min(paddr_lo, (u64)hose->mem_resources[i].start);
  173. paddr_hi = max(paddr_hi, (u64)hose->mem_resources[i].end);
  174. /* We assume all memory resources have the same offset */
  175. offset = hose->mem_offset[i];
  176. n = setup_one_atmu(pci, j, &hose->mem_resources[i], offset);
  177. if (n < 0 || j >= 5) {
  178. pr_err("Ran out of outbound PCI ATMUs for resource %d!\n", i);
  179. hose->mem_resources[i].flags |= IORESOURCE_DISABLED;
  180. } else
  181. j += n;
  182. }
  183. /* Setup outbound IO window */
  184. if (hose->io_resource.flags & IORESOURCE_IO) {
  185. if (j >= 5) {
  186. pr_err("Ran out of outbound PCI ATMUs for IO resource\n");
  187. } else {
  188. pr_debug("PCI IO resource start 0x%016llx, size 0x%016llx, "
  189. "phy base 0x%016llx.\n",
  190. (u64)hose->io_resource.start,
  191. (u64)resource_size(&hose->io_resource),
  192. (u64)hose->io_base_phys);
  193. out_be32(&pci->pow[j].potar, (hose->io_resource.start >> 12));
  194. out_be32(&pci->pow[j].potear, 0);
  195. out_be32(&pci->pow[j].powbar, (hose->io_base_phys >> 12));
  196. /* Enable, IO R/W */
  197. out_be32(&pci->pow[j].powar, 0x80088000
  198. | (ilog2(hose->io_resource.end
  199. - hose->io_resource.start + 1) - 1));
  200. }
  201. }
  202. /* convert to pci address space */
  203. paddr_hi -= offset;
  204. paddr_lo -= offset;
  205. if (paddr_hi == paddr_lo) {
  206. pr_err("%s: No outbound window space\n", name);
  207. return;
  208. }
  209. if (paddr_lo == 0) {
  210. pr_err("%s: No space for inbound window\n", name);
  211. return;
  212. }
  213. /* setup PCSRBAR/PEXCSRBAR */
  214. early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, 0xffffffff);
  215. early_read_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, &pcicsrbar_sz);
  216. pcicsrbar_sz = ~pcicsrbar_sz + 1;
  217. if (paddr_hi < (0x100000000ull - pcicsrbar_sz) ||
  218. (paddr_lo > 0x100000000ull))
  219. pcicsrbar = 0x100000000ull - pcicsrbar_sz;
  220. else
  221. pcicsrbar = (paddr_lo - pcicsrbar_sz) & -pcicsrbar_sz;
  222. early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, pcicsrbar);
  223. paddr_lo = min(paddr_lo, (u64)pcicsrbar);
  224. pr_info("%s: PCICSRBAR @ 0x%x\n", name, pcicsrbar);
  225. /* Setup inbound mem window */
  226. mem = memblock_end_of_DRAM();
  227. /*
  228. * The msi-address-64 property, if it exists, indicates the physical
  229. * address of the MSIIR register. Normally, this register is located
  230. * inside CCSR, so the ATMU that covers all of CCSR is used. But if
  231. * this property exists, then we normally need to create a new ATMU
  232. * for it. For now, however, we cheat. The only entity that creates
  233. * this property is the Freescale hypervisor, and the address is
  234. * specified in the partition configuration. Typically, the address
  235. * is located in the page immediately after the end of DDR. If so, we
  236. * can avoid allocating a new ATMU by extending the DDR ATMU by one
  237. * page.
  238. */
  239. reg = of_get_property(hose->dn, "msi-address-64", &len);
  240. if (reg && (len == sizeof(u64))) {
  241. u64 address = be64_to_cpup(reg);
  242. if ((address >= mem) && (address < (mem + PAGE_SIZE))) {
  243. pr_info("%s: extending DDR ATMU to cover MSIIR", name);
  244. mem += PAGE_SIZE;
  245. } else {
  246. /* TODO: Create a new ATMU for MSIIR */
  247. pr_warn("%s: msi-address-64 address of %llx is "
  248. "unsupported\n", name, address);
  249. }
  250. }
  251. sz = min(mem, paddr_lo);
  252. mem_log = ilog2(sz);
  253. /* PCIe can overmap inbound & outbound since RX & TX are separated */
  254. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  255. /* Size window to exact size if power-of-two or one size up */
  256. if ((1ull << mem_log) != mem) {
  257. mem_log++;
  258. if ((1ull << mem_log) > mem)
  259. pr_info("%s: Setting PCI inbound window "
  260. "greater than memory size\n", name);
  261. }
  262. piwar |= ((mem_log - 1) & PIWAR_SZ_MASK);
  263. /* Setup inbound memory window */
  264. out_be32(&pci->piw[win_idx].pitar, 0x00000000);
  265. out_be32(&pci->piw[win_idx].piwbar, 0x00000000);
  266. out_be32(&pci->piw[win_idx].piwar, piwar);
  267. win_idx--;
  268. hose->dma_window_base_cur = 0x00000000;
  269. hose->dma_window_size = (resource_size_t)sz;
  270. /*
  271. * if we have >4G of memory setup second PCI inbound window to
  272. * let devices that are 64-bit address capable to work w/o
  273. * SWIOTLB and access the full range of memory
  274. */
  275. if (sz != mem) {
  276. mem_log = ilog2(mem);
  277. /* Size window up if we dont fit in exact power-of-2 */
  278. if ((1ull << mem_log) != mem)
  279. mem_log++;
  280. piwar = (piwar & ~PIWAR_SZ_MASK) | (mem_log - 1);
  281. /* Setup inbound memory window */
  282. out_be32(&pci->piw[win_idx].pitar, 0x00000000);
  283. out_be32(&pci->piw[win_idx].piwbear,
  284. pci64_dma_offset >> 44);
  285. out_be32(&pci->piw[win_idx].piwbar,
  286. pci64_dma_offset >> 12);
  287. out_be32(&pci->piw[win_idx].piwar, piwar);
  288. /*
  289. * install our own dma_set_mask handler to fixup dma_ops
  290. * and dma_offset
  291. */
  292. ppc_md.dma_set_mask = fsl_pci_dma_set_mask;
  293. pr_info("%s: Setup 64-bit PCI DMA window\n", name);
  294. }
  295. } else {
  296. u64 paddr = 0;
  297. /* Setup inbound memory window */
  298. out_be32(&pci->piw[win_idx].pitar, paddr >> 12);
  299. out_be32(&pci->piw[win_idx].piwbar, paddr >> 12);
  300. out_be32(&pci->piw[win_idx].piwar, (piwar | (mem_log - 1)));
  301. win_idx--;
  302. paddr += 1ull << mem_log;
  303. sz -= 1ull << mem_log;
  304. if (sz) {
  305. mem_log = ilog2(sz);
  306. piwar |= (mem_log - 1);
  307. out_be32(&pci->piw[win_idx].pitar, paddr >> 12);
  308. out_be32(&pci->piw[win_idx].piwbar, paddr >> 12);
  309. out_be32(&pci->piw[win_idx].piwar, piwar);
  310. win_idx--;
  311. paddr += 1ull << mem_log;
  312. }
  313. hose->dma_window_base_cur = 0x00000000;
  314. hose->dma_window_size = (resource_size_t)paddr;
  315. }
  316. if (hose->dma_window_size < mem) {
  317. #ifdef CONFIG_SWIOTLB
  318. ppc_swiotlb_enable = 1;
  319. #else
  320. pr_err("%s: ERROR: Memory size exceeds PCI ATMU ability to "
  321. "map - enable CONFIG_SWIOTLB to avoid dma errors.\n",
  322. name);
  323. #endif
  324. /* adjusting outbound windows could reclaim space in mem map */
  325. if (paddr_hi < 0xffffffffull)
  326. pr_warning("%s: WARNING: Outbound window cfg leaves "
  327. "gaps in memory map. Adjusting the memory map "
  328. "could reduce unnecessary bounce buffering.\n",
  329. name);
  330. pr_info("%s: DMA window size is 0x%llx\n", name,
  331. (u64)hose->dma_window_size);
  332. }
  333. }
  334. static void __init setup_pci_cmd(struct pci_controller *hose)
  335. {
  336. u16 cmd;
  337. int cap_x;
  338. early_read_config_word(hose, 0, 0, PCI_COMMAND, &cmd);
  339. cmd |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY
  340. | PCI_COMMAND_IO;
  341. early_write_config_word(hose, 0, 0, PCI_COMMAND, cmd);
  342. cap_x = early_find_capability(hose, 0, 0, PCI_CAP_ID_PCIX);
  343. if (cap_x) {
  344. int pci_x_cmd = cap_x + PCI_X_CMD;
  345. cmd = PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ
  346. | PCI_X_CMD_ERO | PCI_X_CMD_DPERR_E;
  347. early_write_config_word(hose, 0, 0, pci_x_cmd, cmd);
  348. } else {
  349. early_write_config_byte(hose, 0, 0, PCI_LATENCY_TIMER, 0x80);
  350. }
  351. }
  352. void fsl_pcibios_fixup_bus(struct pci_bus *bus)
  353. {
  354. struct pci_controller *hose = pci_bus_to_host(bus);
  355. int i, is_pcie = 0, no_link;
  356. /* The root complex bridge comes up with bogus resources,
  357. * we copy the PHB ones in.
  358. *
  359. * With the current generic PCI code, the PHB bus no longer
  360. * has bus->resource[0..4] set, so things are a bit more
  361. * tricky.
  362. */
  363. if (fsl_pcie_bus_fixup)
  364. is_pcie = early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP);
  365. no_link = !!(hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK);
  366. if (bus->parent == hose->bus && (is_pcie || no_link)) {
  367. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; ++i) {
  368. struct resource *res = bus->resource[i];
  369. struct resource *par;
  370. if (!res)
  371. continue;
  372. if (i == 0)
  373. par = &hose->io_resource;
  374. else if (i < 4)
  375. par = &hose->mem_resources[i-1];
  376. else par = NULL;
  377. res->start = par ? par->start : 0;
  378. res->end = par ? par->end : 0;
  379. res->flags = par ? par->flags : 0;
  380. }
  381. }
  382. }
  383. int __init fsl_add_bridge(struct platform_device *pdev, int is_primary)
  384. {
  385. int len;
  386. struct pci_controller *hose;
  387. struct resource rsrc;
  388. const int *bus_range;
  389. u8 hdr_type, progif;
  390. struct device_node *dev;
  391. struct ccsr_pci __iomem *pci;
  392. dev = pdev->dev.of_node;
  393. if (!of_device_is_available(dev)) {
  394. pr_warning("%s: disabled\n", dev->full_name);
  395. return -ENODEV;
  396. }
  397. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  398. /* Fetch host bridge registers address */
  399. if (of_address_to_resource(dev, 0, &rsrc)) {
  400. printk(KERN_WARNING "Can't get pci register base!");
  401. return -ENOMEM;
  402. }
  403. /* Get bus range if any */
  404. bus_range = of_get_property(dev, "bus-range", &len);
  405. if (bus_range == NULL || len < 2 * sizeof(int))
  406. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  407. " bus 0\n", dev->full_name);
  408. pci_add_flags(PCI_REASSIGN_ALL_BUS);
  409. hose = pcibios_alloc_controller(dev);
  410. if (!hose)
  411. return -ENOMEM;
  412. /* set platform device as the parent */
  413. hose->parent = &pdev->dev;
  414. hose->first_busno = bus_range ? bus_range[0] : 0x0;
  415. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  416. pr_debug("PCI memory map start 0x%016llx, size 0x%016llx\n",
  417. (u64)rsrc.start, (u64)resource_size(&rsrc));
  418. pci = hose->private_data = ioremap(rsrc.start, resource_size(&rsrc));
  419. if (!hose->private_data)
  420. goto no_bridge;
  421. setup_indirect_pci(hose, rsrc.start, rsrc.start + 0x4,
  422. PPC_INDIRECT_TYPE_BIG_ENDIAN);
  423. if (in_be32(&pci->block_rev1) < PCIE_IP_REV_3_0)
  424. hose->indirect_type |= PPC_INDIRECT_TYPE_FSL_CFG_REG_LINK;
  425. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  426. /* use fsl_indirect_read_config for PCIe */
  427. hose->ops = &fsl_indirect_pcie_ops;
  428. /* For PCIE read HEADER_TYPE to identify controler mode */
  429. early_read_config_byte(hose, 0, 0, PCI_HEADER_TYPE, &hdr_type);
  430. if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE)
  431. goto no_bridge;
  432. } else {
  433. /* For PCI read PROG to identify controller mode */
  434. early_read_config_byte(hose, 0, 0, PCI_CLASS_PROG, &progif);
  435. if ((progif & 1) == 1)
  436. goto no_bridge;
  437. }
  438. setup_pci_cmd(hose);
  439. /* check PCI express link status */
  440. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  441. hose->indirect_type |= PPC_INDIRECT_TYPE_EXT_REG |
  442. PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS;
  443. if (fsl_pcie_check_link(hose))
  444. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  445. }
  446. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  447. "Firmware bus number: %d->%d\n",
  448. (unsigned long long)rsrc.start, hose->first_busno,
  449. hose->last_busno);
  450. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  451. hose, hose->cfg_addr, hose->cfg_data);
  452. /* Interpret the "ranges" property */
  453. /* This also maps the I/O region and sets isa_io/mem_base */
  454. pci_process_bridge_OF_ranges(hose, dev, is_primary);
  455. /* Setup PEX window registers */
  456. setup_pci_atmu(hose);
  457. return 0;
  458. no_bridge:
  459. iounmap(hose->private_data);
  460. /* unmap cfg_data & cfg_addr separately if not on same page */
  461. if (((unsigned long)hose->cfg_data & PAGE_MASK) !=
  462. ((unsigned long)hose->cfg_addr & PAGE_MASK))
  463. iounmap(hose->cfg_data);
  464. iounmap(hose->cfg_addr);
  465. pcibios_free_controller(hose);
  466. return -ENODEV;
  467. }
  468. #endif /* CONFIG_FSL_SOC_BOOKE || CONFIG_PPC_86xx */
  469. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID,
  470. quirk_fsl_pcie_early);
  471. #if defined(CONFIG_PPC_83xx) || defined(CONFIG_PPC_MPC512x)
  472. struct mpc83xx_pcie_priv {
  473. void __iomem *cfg_type0;
  474. void __iomem *cfg_type1;
  475. u32 dev_base;
  476. };
  477. struct pex_inbound_window {
  478. u32 ar;
  479. u32 tar;
  480. u32 barl;
  481. u32 barh;
  482. };
  483. /*
  484. * With the convention of u-boot, the PCIE outbound window 0 serves
  485. * as configuration transactions outbound.
  486. */
  487. #define PEX_OUTWIN0_BAR 0xCA4
  488. #define PEX_OUTWIN0_TAL 0xCA8
  489. #define PEX_OUTWIN0_TAH 0xCAC
  490. #define PEX_RC_INWIN_BASE 0xE60
  491. #define PEX_RCIWARn_EN 0x1
  492. static int mpc83xx_pcie_exclude_device(struct pci_bus *bus, unsigned int devfn)
  493. {
  494. struct pci_controller *hose = pci_bus_to_host(bus);
  495. if (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)
  496. return PCIBIOS_DEVICE_NOT_FOUND;
  497. /*
  498. * Workaround for the HW bug: for Type 0 configure transactions the
  499. * PCI-E controller does not check the device number bits and just
  500. * assumes that the device number bits are 0.
  501. */
  502. if (bus->number == hose->first_busno ||
  503. bus->primary == hose->first_busno) {
  504. if (devfn & 0xf8)
  505. return PCIBIOS_DEVICE_NOT_FOUND;
  506. }
  507. if (ppc_md.pci_exclude_device) {
  508. if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
  509. return PCIBIOS_DEVICE_NOT_FOUND;
  510. }
  511. return PCIBIOS_SUCCESSFUL;
  512. }
  513. static void __iomem *mpc83xx_pcie_remap_cfg(struct pci_bus *bus,
  514. unsigned int devfn, int offset)
  515. {
  516. struct pci_controller *hose = pci_bus_to_host(bus);
  517. struct mpc83xx_pcie_priv *pcie = hose->dn->data;
  518. u32 dev_base = bus->number << 24 | devfn << 16;
  519. int ret;
  520. ret = mpc83xx_pcie_exclude_device(bus, devfn);
  521. if (ret)
  522. return NULL;
  523. offset &= 0xfff;
  524. /* Type 0 */
  525. if (bus->number == hose->first_busno)
  526. return pcie->cfg_type0 + offset;
  527. if (pcie->dev_base == dev_base)
  528. goto mapped;
  529. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, dev_base);
  530. pcie->dev_base = dev_base;
  531. mapped:
  532. return pcie->cfg_type1 + offset;
  533. }
  534. static int mpc83xx_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
  535. int offset, int len, u32 *val)
  536. {
  537. void __iomem *cfg_addr;
  538. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  539. if (!cfg_addr)
  540. return PCIBIOS_DEVICE_NOT_FOUND;
  541. switch (len) {
  542. case 1:
  543. *val = in_8(cfg_addr);
  544. break;
  545. case 2:
  546. *val = in_le16(cfg_addr);
  547. break;
  548. default:
  549. *val = in_le32(cfg_addr);
  550. break;
  551. }
  552. return PCIBIOS_SUCCESSFUL;
  553. }
  554. static int mpc83xx_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
  555. int offset, int len, u32 val)
  556. {
  557. struct pci_controller *hose = pci_bus_to_host(bus);
  558. void __iomem *cfg_addr;
  559. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  560. if (!cfg_addr)
  561. return PCIBIOS_DEVICE_NOT_FOUND;
  562. /* PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS */
  563. if (offset == PCI_PRIMARY_BUS && bus->number == hose->first_busno)
  564. val &= 0xffffff00;
  565. switch (len) {
  566. case 1:
  567. out_8(cfg_addr, val);
  568. break;
  569. case 2:
  570. out_le16(cfg_addr, val);
  571. break;
  572. default:
  573. out_le32(cfg_addr, val);
  574. break;
  575. }
  576. return PCIBIOS_SUCCESSFUL;
  577. }
  578. static struct pci_ops mpc83xx_pcie_ops = {
  579. .read = mpc83xx_pcie_read_config,
  580. .write = mpc83xx_pcie_write_config,
  581. };
  582. static int __init mpc83xx_pcie_setup(struct pci_controller *hose,
  583. struct resource *reg)
  584. {
  585. struct mpc83xx_pcie_priv *pcie;
  586. u32 cfg_bar;
  587. int ret = -ENOMEM;
  588. pcie = zalloc_maybe_bootmem(sizeof(*pcie), GFP_KERNEL);
  589. if (!pcie)
  590. return ret;
  591. pcie->cfg_type0 = ioremap(reg->start, resource_size(reg));
  592. if (!pcie->cfg_type0)
  593. goto err0;
  594. cfg_bar = in_le32(pcie->cfg_type0 + PEX_OUTWIN0_BAR);
  595. if (!cfg_bar) {
  596. /* PCI-E isn't configured. */
  597. ret = -ENODEV;
  598. goto err1;
  599. }
  600. pcie->cfg_type1 = ioremap(cfg_bar, 0x1000);
  601. if (!pcie->cfg_type1)
  602. goto err1;
  603. WARN_ON(hose->dn->data);
  604. hose->dn->data = pcie;
  605. hose->ops = &mpc83xx_pcie_ops;
  606. hose->indirect_type |= PPC_INDIRECT_TYPE_FSL_CFG_REG_LINK;
  607. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAH, 0);
  608. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, 0);
  609. if (fsl_pcie_check_link(hose))
  610. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  611. return 0;
  612. err1:
  613. iounmap(pcie->cfg_type0);
  614. err0:
  615. kfree(pcie);
  616. return ret;
  617. }
  618. int __init mpc83xx_add_bridge(struct device_node *dev)
  619. {
  620. int ret;
  621. int len;
  622. struct pci_controller *hose;
  623. struct resource rsrc_reg;
  624. struct resource rsrc_cfg;
  625. const int *bus_range;
  626. int primary;
  627. is_mpc83xx_pci = 1;
  628. if (!of_device_is_available(dev)) {
  629. pr_warning("%s: disabled by the firmware.\n",
  630. dev->full_name);
  631. return -ENODEV;
  632. }
  633. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  634. /* Fetch host bridge registers address */
  635. if (of_address_to_resource(dev, 0, &rsrc_reg)) {
  636. printk(KERN_WARNING "Can't get pci register base!\n");
  637. return -ENOMEM;
  638. }
  639. memset(&rsrc_cfg, 0, sizeof(rsrc_cfg));
  640. if (of_address_to_resource(dev, 1, &rsrc_cfg)) {
  641. printk(KERN_WARNING
  642. "No pci config register base in dev tree, "
  643. "using default\n");
  644. /*
  645. * MPC83xx supports up to two host controllers
  646. * one at 0x8500 has config space registers at 0x8300
  647. * one at 0x8600 has config space registers at 0x8380
  648. */
  649. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  650. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8300;
  651. else if ((rsrc_reg.start & 0xfffff) == 0x8600)
  652. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8380;
  653. }
  654. /*
  655. * Controller at offset 0x8500 is primary
  656. */
  657. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  658. primary = 1;
  659. else
  660. primary = 0;
  661. /* Get bus range if any */
  662. bus_range = of_get_property(dev, "bus-range", &len);
  663. if (bus_range == NULL || len < 2 * sizeof(int)) {
  664. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  665. " bus 0\n", dev->full_name);
  666. }
  667. pci_add_flags(PCI_REASSIGN_ALL_BUS);
  668. hose = pcibios_alloc_controller(dev);
  669. if (!hose)
  670. return -ENOMEM;
  671. hose->first_busno = bus_range ? bus_range[0] : 0;
  672. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  673. if (of_device_is_compatible(dev, "fsl,mpc8314-pcie")) {
  674. ret = mpc83xx_pcie_setup(hose, &rsrc_reg);
  675. if (ret)
  676. goto err0;
  677. } else {
  678. setup_indirect_pci(hose, rsrc_cfg.start,
  679. rsrc_cfg.start + 4, 0);
  680. }
  681. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  682. "Firmware bus number: %d->%d\n",
  683. (unsigned long long)rsrc_reg.start, hose->first_busno,
  684. hose->last_busno);
  685. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  686. hose, hose->cfg_addr, hose->cfg_data);
  687. /* Interpret the "ranges" property */
  688. /* This also maps the I/O region and sets isa_io/mem_base */
  689. pci_process_bridge_OF_ranges(hose, dev, primary);
  690. return 0;
  691. err0:
  692. pcibios_free_controller(hose);
  693. return ret;
  694. }
  695. #endif /* CONFIG_PPC_83xx */
  696. u64 fsl_pci_immrbar_base(struct pci_controller *hose)
  697. {
  698. #ifdef CONFIG_PPC_83xx
  699. if (is_mpc83xx_pci) {
  700. struct mpc83xx_pcie_priv *pcie = hose->dn->data;
  701. struct pex_inbound_window *in;
  702. int i;
  703. /* Walk the Root Complex Inbound windows to match IMMR base */
  704. in = pcie->cfg_type0 + PEX_RC_INWIN_BASE;
  705. for (i = 0; i < 4; i++) {
  706. /* not enabled, skip */
  707. if (!in_le32(&in[i].ar) & PEX_RCIWARn_EN)
  708. continue;
  709. if (get_immrbase() == in_le32(&in[i].tar))
  710. return (u64)in_le32(&in[i].barh) << 32 |
  711. in_le32(&in[i].barl);
  712. }
  713. printk(KERN_WARNING "could not find PCI BAR matching IMMR\n");
  714. }
  715. #endif
  716. #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
  717. if (!is_mpc83xx_pci) {
  718. u32 base;
  719. pci_bus_read_config_dword(hose->bus,
  720. PCI_DEVFN(0, 0), PCI_BASE_ADDRESS_0, &base);
  721. return base;
  722. }
  723. #endif
  724. return 0;
  725. }
  726. #ifdef CONFIG_E500
  727. static int mcheck_handle_load(struct pt_regs *regs, u32 inst)
  728. {
  729. unsigned int rd, ra, rb, d;
  730. rd = get_rt(inst);
  731. ra = get_ra(inst);
  732. rb = get_rb(inst);
  733. d = get_d(inst);
  734. switch (get_op(inst)) {
  735. case 31:
  736. switch (get_xop(inst)) {
  737. case OP_31_XOP_LWZX:
  738. case OP_31_XOP_LWBRX:
  739. regs->gpr[rd] = 0xffffffff;
  740. break;
  741. case OP_31_XOP_LWZUX:
  742. regs->gpr[rd] = 0xffffffff;
  743. regs->gpr[ra] += regs->gpr[rb];
  744. break;
  745. case OP_31_XOP_LBZX:
  746. regs->gpr[rd] = 0xff;
  747. break;
  748. case OP_31_XOP_LBZUX:
  749. regs->gpr[rd] = 0xff;
  750. regs->gpr[ra] += regs->gpr[rb];
  751. break;
  752. case OP_31_XOP_LHZX:
  753. case OP_31_XOP_LHBRX:
  754. regs->gpr[rd] = 0xffff;
  755. break;
  756. case OP_31_XOP_LHZUX:
  757. regs->gpr[rd] = 0xffff;
  758. regs->gpr[ra] += regs->gpr[rb];
  759. break;
  760. case OP_31_XOP_LHAX:
  761. regs->gpr[rd] = ~0UL;
  762. break;
  763. case OP_31_XOP_LHAUX:
  764. regs->gpr[rd] = ~0UL;
  765. regs->gpr[ra] += regs->gpr[rb];
  766. break;
  767. default:
  768. return 0;
  769. }
  770. break;
  771. case OP_LWZ:
  772. regs->gpr[rd] = 0xffffffff;
  773. break;
  774. case OP_LWZU:
  775. regs->gpr[rd] = 0xffffffff;
  776. regs->gpr[ra] += (s16)d;
  777. break;
  778. case OP_LBZ:
  779. regs->gpr[rd] = 0xff;
  780. break;
  781. case OP_LBZU:
  782. regs->gpr[rd] = 0xff;
  783. regs->gpr[ra] += (s16)d;
  784. break;
  785. case OP_LHZ:
  786. regs->gpr[rd] = 0xffff;
  787. break;
  788. case OP_LHZU:
  789. regs->gpr[rd] = 0xffff;
  790. regs->gpr[ra] += (s16)d;
  791. break;
  792. case OP_LHA:
  793. regs->gpr[rd] = ~0UL;
  794. break;
  795. case OP_LHAU:
  796. regs->gpr[rd] = ~0UL;
  797. regs->gpr[ra] += (s16)d;
  798. break;
  799. default:
  800. return 0;
  801. }
  802. return 1;
  803. }
  804. static int is_in_pci_mem_space(phys_addr_t addr)
  805. {
  806. struct pci_controller *hose;
  807. struct resource *res;
  808. int i;
  809. list_for_each_entry(hose, &hose_list, list_node) {
  810. if (!(hose->indirect_type & PPC_INDIRECT_TYPE_EXT_REG))
  811. continue;
  812. for (i = 0; i < 3; i++) {
  813. res = &hose->mem_resources[i];
  814. if ((res->flags & IORESOURCE_MEM) &&
  815. addr >= res->start && addr <= res->end)
  816. return 1;
  817. }
  818. }
  819. return 0;
  820. }
  821. int fsl_pci_mcheck_exception(struct pt_regs *regs)
  822. {
  823. u32 inst;
  824. int ret;
  825. phys_addr_t addr = 0;
  826. /* Let KVM/QEMU deal with the exception */
  827. if (regs->msr & MSR_GS)
  828. return 0;
  829. #ifdef CONFIG_PHYS_64BIT
  830. addr = mfspr(SPRN_MCARU);
  831. addr <<= 32;
  832. #endif
  833. addr += mfspr(SPRN_MCAR);
  834. if (is_in_pci_mem_space(addr)) {
  835. if (user_mode(regs)) {
  836. pagefault_disable();
  837. ret = get_user(regs->nip, &inst);
  838. pagefault_enable();
  839. } else {
  840. ret = probe_kernel_address(regs->nip, inst);
  841. }
  842. if (mcheck_handle_load(regs, inst)) {
  843. regs->nip += 4;
  844. return 1;
  845. }
  846. }
  847. return 0;
  848. }
  849. #endif
  850. #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
  851. static const struct of_device_id pci_ids[] = {
  852. { .compatible = "fsl,mpc8540-pci", },
  853. { .compatible = "fsl,mpc8548-pcie", },
  854. { .compatible = "fsl,mpc8610-pci", },
  855. { .compatible = "fsl,mpc8641-pcie", },
  856. { .compatible = "fsl,qoriq-pcie-v2.1", },
  857. { .compatible = "fsl,qoriq-pcie-v2.2", },
  858. { .compatible = "fsl,qoriq-pcie-v2.3", },
  859. { .compatible = "fsl,qoriq-pcie-v2.4", },
  860. { .compatible = "fsl,qoriq-pcie-v3.0", },
  861. /*
  862. * The following entries are for compatibility with older device
  863. * trees.
  864. */
  865. { .compatible = "fsl,p1022-pcie", },
  866. { .compatible = "fsl,p4080-pcie", },
  867. {},
  868. };
  869. struct device_node *fsl_pci_primary;
  870. void fsl_pci_assign_primary(void)
  871. {
  872. struct device_node *np;
  873. /* Callers can specify the primary bus using other means. */
  874. if (fsl_pci_primary)
  875. return;
  876. /* If a PCI host bridge contains an ISA node, it's primary. */
  877. np = of_find_node_by_type(NULL, "isa");
  878. while ((fsl_pci_primary = of_get_parent(np))) {
  879. of_node_put(np);
  880. np = fsl_pci_primary;
  881. if (of_match_node(pci_ids, np) && of_device_is_available(np))
  882. return;
  883. }
  884. /*
  885. * If there's no PCI host bridge with ISA, arbitrarily
  886. * designate one as primary. This can go away once
  887. * various bugs with primary-less systems are fixed.
  888. */
  889. for_each_matching_node(np, pci_ids) {
  890. if (of_device_is_available(np)) {
  891. fsl_pci_primary = np;
  892. of_node_put(np);
  893. return;
  894. }
  895. }
  896. }
  897. static int fsl_pci_probe(struct platform_device *pdev)
  898. {
  899. int ret;
  900. struct device_node *node;
  901. node = pdev->dev.of_node;
  902. ret = fsl_add_bridge(pdev, fsl_pci_primary == node);
  903. mpc85xx_pci_err_probe(pdev);
  904. return 0;
  905. }
  906. #ifdef CONFIG_PM
  907. static int fsl_pci_resume(struct device *dev)
  908. {
  909. struct pci_controller *hose;
  910. struct resource pci_rsrc;
  911. hose = pci_find_hose_for_OF_device(dev->of_node);
  912. if (!hose)
  913. return -ENODEV;
  914. if (of_address_to_resource(dev->of_node, 0, &pci_rsrc)) {
  915. dev_err(dev, "Get pci register base failed.");
  916. return -ENODEV;
  917. }
  918. setup_pci_atmu(hose);
  919. return 0;
  920. }
  921. static const struct dev_pm_ops pci_pm_ops = {
  922. .resume = fsl_pci_resume,
  923. };
  924. #define PCI_PM_OPS (&pci_pm_ops)
  925. #else
  926. #define PCI_PM_OPS NULL
  927. #endif
  928. static struct platform_driver fsl_pci_driver = {
  929. .driver = {
  930. .name = "fsl-pci",
  931. .pm = PCI_PM_OPS,
  932. .of_match_table = pci_ids,
  933. },
  934. .probe = fsl_pci_probe,
  935. };
  936. static int __init fsl_pci_init(void)
  937. {
  938. return platform_driver_register(&fsl_pci_driver);
  939. }
  940. arch_initcall(fsl_pci_init);
  941. #endif