fsl_pci.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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_header(struct pci_dev *dev)
  41. {
  42. u8 hdr_type;
  43. /* if we aren't a PCIe don't bother */
  44. if (!pci_find_capability(dev, PCI_CAP_ID_EXP))
  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 = 0;
  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. #ifndef CONFIG_SWIOTLB
  318. pr_err("%s: ERROR: Memory size exceeds PCI ATMU ability to "
  319. "map - enable CONFIG_SWIOTLB to avoid dma errors.\n",
  320. name);
  321. #endif
  322. /* adjusting outbound windows could reclaim space in mem map */
  323. if (paddr_hi < 0xffffffffull)
  324. pr_warning("%s: WARNING: Outbound window cfg leaves "
  325. "gaps in memory map. Adjusting the memory map "
  326. "could reduce unnecessary bounce buffering.\n",
  327. name);
  328. pr_info("%s: DMA window size is 0x%llx\n", name,
  329. (u64)hose->dma_window_size);
  330. }
  331. }
  332. static void __init setup_pci_cmd(struct pci_controller *hose)
  333. {
  334. u16 cmd;
  335. int cap_x;
  336. early_read_config_word(hose, 0, 0, PCI_COMMAND, &cmd);
  337. cmd |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY
  338. | PCI_COMMAND_IO;
  339. early_write_config_word(hose, 0, 0, PCI_COMMAND, cmd);
  340. cap_x = early_find_capability(hose, 0, 0, PCI_CAP_ID_PCIX);
  341. if (cap_x) {
  342. int pci_x_cmd = cap_x + PCI_X_CMD;
  343. cmd = PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ
  344. | PCI_X_CMD_ERO | PCI_X_CMD_DPERR_E;
  345. early_write_config_word(hose, 0, 0, pci_x_cmd, cmd);
  346. } else {
  347. early_write_config_byte(hose, 0, 0, PCI_LATENCY_TIMER, 0x80);
  348. }
  349. }
  350. void fsl_pcibios_fixup_bus(struct pci_bus *bus)
  351. {
  352. struct pci_controller *hose = pci_bus_to_host(bus);
  353. int i, is_pcie = 0, no_link;
  354. /* The root complex bridge comes up with bogus resources,
  355. * we copy the PHB ones in.
  356. *
  357. * With the current generic PCI code, the PHB bus no longer
  358. * has bus->resource[0..4] set, so things are a bit more
  359. * tricky.
  360. */
  361. if (fsl_pcie_bus_fixup)
  362. is_pcie = early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP);
  363. no_link = !!(hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK);
  364. if (bus->parent == hose->bus && (is_pcie || no_link)) {
  365. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; ++i) {
  366. struct resource *res = bus->resource[i];
  367. struct resource *par;
  368. if (!res)
  369. continue;
  370. if (i == 0)
  371. par = &hose->io_resource;
  372. else if (i < 4)
  373. par = &hose->mem_resources[i-1];
  374. else par = NULL;
  375. res->start = par ? par->start : 0;
  376. res->end = par ? par->end : 0;
  377. res->flags = par ? par->flags : 0;
  378. }
  379. }
  380. }
  381. int __init fsl_add_bridge(struct platform_device *pdev, int is_primary)
  382. {
  383. int len;
  384. struct pci_controller *hose;
  385. struct resource rsrc;
  386. const int *bus_range;
  387. u8 hdr_type, progif;
  388. struct device_node *dev;
  389. struct ccsr_pci __iomem *pci;
  390. dev = pdev->dev.of_node;
  391. if (!of_device_is_available(dev)) {
  392. pr_warning("%s: disabled\n", dev->full_name);
  393. return -ENODEV;
  394. }
  395. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  396. /* Fetch host bridge registers address */
  397. if (of_address_to_resource(dev, 0, &rsrc)) {
  398. printk(KERN_WARNING "Can't get pci register base!");
  399. return -ENOMEM;
  400. }
  401. /* Get bus range if any */
  402. bus_range = of_get_property(dev, "bus-range", &len);
  403. if (bus_range == NULL || len < 2 * sizeof(int))
  404. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  405. " bus 0\n", dev->full_name);
  406. pci_add_flags(PCI_REASSIGN_ALL_BUS);
  407. hose = pcibios_alloc_controller(dev);
  408. if (!hose)
  409. return -ENOMEM;
  410. /* set platform device as the parent */
  411. hose->parent = &pdev->dev;
  412. hose->first_busno = bus_range ? bus_range[0] : 0x0;
  413. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  414. pr_debug("PCI memory map start 0x%016llx, size 0x%016llx\n",
  415. (u64)rsrc.start, (u64)resource_size(&rsrc));
  416. pci = hose->private_data = ioremap(rsrc.start, resource_size(&rsrc));
  417. if (!hose->private_data)
  418. goto no_bridge;
  419. setup_indirect_pci(hose, rsrc.start, rsrc.start + 0x4,
  420. PPC_INDIRECT_TYPE_BIG_ENDIAN);
  421. if (in_be32(&pci->block_rev1) < PCIE_IP_REV_3_0)
  422. hose->indirect_type |= PPC_INDIRECT_TYPE_FSL_CFG_REG_LINK;
  423. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  424. /* use fsl_indirect_read_config for PCIe */
  425. hose->ops = &fsl_indirect_pcie_ops;
  426. /* For PCIE read HEADER_TYPE to identify controler mode */
  427. early_read_config_byte(hose, 0, 0, PCI_HEADER_TYPE, &hdr_type);
  428. if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE)
  429. goto no_bridge;
  430. } else {
  431. /* For PCI read PROG to identify controller mode */
  432. early_read_config_byte(hose, 0, 0, PCI_CLASS_PROG, &progif);
  433. if ((progif & 1) == 1)
  434. goto no_bridge;
  435. }
  436. setup_pci_cmd(hose);
  437. /* check PCI express link status */
  438. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  439. hose->indirect_type |= PPC_INDIRECT_TYPE_EXT_REG |
  440. PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS;
  441. if (fsl_pcie_check_link(hose))
  442. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  443. }
  444. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  445. "Firmware bus number: %d->%d\n",
  446. (unsigned long long)rsrc.start, hose->first_busno,
  447. hose->last_busno);
  448. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  449. hose, hose->cfg_addr, hose->cfg_data);
  450. /* Interpret the "ranges" property */
  451. /* This also maps the I/O region and sets isa_io/mem_base */
  452. pci_process_bridge_OF_ranges(hose, dev, is_primary);
  453. /* Setup PEX window registers */
  454. setup_pci_atmu(hose);
  455. return 0;
  456. no_bridge:
  457. iounmap(hose->private_data);
  458. /* unmap cfg_data & cfg_addr separately if not on same page */
  459. if (((unsigned long)hose->cfg_data & PAGE_MASK) !=
  460. ((unsigned long)hose->cfg_addr & PAGE_MASK))
  461. iounmap(hose->cfg_data);
  462. iounmap(hose->cfg_addr);
  463. pcibios_free_controller(hose);
  464. return -ENODEV;
  465. }
  466. #endif /* CONFIG_FSL_SOC_BOOKE || CONFIG_PPC_86xx */
  467. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, quirk_fsl_pcie_header);
  468. #if defined(CONFIG_PPC_83xx) || defined(CONFIG_PPC_MPC512x)
  469. struct mpc83xx_pcie_priv {
  470. void __iomem *cfg_type0;
  471. void __iomem *cfg_type1;
  472. u32 dev_base;
  473. };
  474. struct pex_inbound_window {
  475. u32 ar;
  476. u32 tar;
  477. u32 barl;
  478. u32 barh;
  479. };
  480. /*
  481. * With the convention of u-boot, the PCIE outbound window 0 serves
  482. * as configuration transactions outbound.
  483. */
  484. #define PEX_OUTWIN0_BAR 0xCA4
  485. #define PEX_OUTWIN0_TAL 0xCA8
  486. #define PEX_OUTWIN0_TAH 0xCAC
  487. #define PEX_RC_INWIN_BASE 0xE60
  488. #define PEX_RCIWARn_EN 0x1
  489. static int mpc83xx_pcie_exclude_device(struct pci_bus *bus, unsigned int devfn)
  490. {
  491. struct pci_controller *hose = pci_bus_to_host(bus);
  492. if (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)
  493. return PCIBIOS_DEVICE_NOT_FOUND;
  494. /*
  495. * Workaround for the HW bug: for Type 0 configure transactions the
  496. * PCI-E controller does not check the device number bits and just
  497. * assumes that the device number bits are 0.
  498. */
  499. if (bus->number == hose->first_busno ||
  500. bus->primary == hose->first_busno) {
  501. if (devfn & 0xf8)
  502. return PCIBIOS_DEVICE_NOT_FOUND;
  503. }
  504. if (ppc_md.pci_exclude_device) {
  505. if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
  506. return PCIBIOS_DEVICE_NOT_FOUND;
  507. }
  508. return PCIBIOS_SUCCESSFUL;
  509. }
  510. static void __iomem *mpc83xx_pcie_remap_cfg(struct pci_bus *bus,
  511. unsigned int devfn, int offset)
  512. {
  513. struct pci_controller *hose = pci_bus_to_host(bus);
  514. struct mpc83xx_pcie_priv *pcie = hose->dn->data;
  515. u32 dev_base = bus->number << 24 | devfn << 16;
  516. int ret;
  517. ret = mpc83xx_pcie_exclude_device(bus, devfn);
  518. if (ret)
  519. return NULL;
  520. offset &= 0xfff;
  521. /* Type 0 */
  522. if (bus->number == hose->first_busno)
  523. return pcie->cfg_type0 + offset;
  524. if (pcie->dev_base == dev_base)
  525. goto mapped;
  526. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, dev_base);
  527. pcie->dev_base = dev_base;
  528. mapped:
  529. return pcie->cfg_type1 + offset;
  530. }
  531. static int mpc83xx_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
  532. int offset, int len, u32 *val)
  533. {
  534. void __iomem *cfg_addr;
  535. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  536. if (!cfg_addr)
  537. return PCIBIOS_DEVICE_NOT_FOUND;
  538. switch (len) {
  539. case 1:
  540. *val = in_8(cfg_addr);
  541. break;
  542. case 2:
  543. *val = in_le16(cfg_addr);
  544. break;
  545. default:
  546. *val = in_le32(cfg_addr);
  547. break;
  548. }
  549. return PCIBIOS_SUCCESSFUL;
  550. }
  551. static int mpc83xx_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
  552. int offset, int len, u32 val)
  553. {
  554. struct pci_controller *hose = pci_bus_to_host(bus);
  555. void __iomem *cfg_addr;
  556. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  557. if (!cfg_addr)
  558. return PCIBIOS_DEVICE_NOT_FOUND;
  559. /* PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS */
  560. if (offset == PCI_PRIMARY_BUS && bus->number == hose->first_busno)
  561. val &= 0xffffff00;
  562. switch (len) {
  563. case 1:
  564. out_8(cfg_addr, val);
  565. break;
  566. case 2:
  567. out_le16(cfg_addr, val);
  568. break;
  569. default:
  570. out_le32(cfg_addr, val);
  571. break;
  572. }
  573. return PCIBIOS_SUCCESSFUL;
  574. }
  575. static struct pci_ops mpc83xx_pcie_ops = {
  576. .read = mpc83xx_pcie_read_config,
  577. .write = mpc83xx_pcie_write_config,
  578. };
  579. static int __init mpc83xx_pcie_setup(struct pci_controller *hose,
  580. struct resource *reg)
  581. {
  582. struct mpc83xx_pcie_priv *pcie;
  583. u32 cfg_bar;
  584. int ret = -ENOMEM;
  585. pcie = zalloc_maybe_bootmem(sizeof(*pcie), GFP_KERNEL);
  586. if (!pcie)
  587. return ret;
  588. pcie->cfg_type0 = ioremap(reg->start, resource_size(reg));
  589. if (!pcie->cfg_type0)
  590. goto err0;
  591. cfg_bar = in_le32(pcie->cfg_type0 + PEX_OUTWIN0_BAR);
  592. if (!cfg_bar) {
  593. /* PCI-E isn't configured. */
  594. ret = -ENODEV;
  595. goto err1;
  596. }
  597. pcie->cfg_type1 = ioremap(cfg_bar, 0x1000);
  598. if (!pcie->cfg_type1)
  599. goto err1;
  600. WARN_ON(hose->dn->data);
  601. hose->dn->data = pcie;
  602. hose->ops = &mpc83xx_pcie_ops;
  603. hose->indirect_type |= PPC_INDIRECT_TYPE_FSL_CFG_REG_LINK;
  604. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAH, 0);
  605. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, 0);
  606. if (fsl_pcie_check_link(hose))
  607. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  608. return 0;
  609. err1:
  610. iounmap(pcie->cfg_type0);
  611. err0:
  612. kfree(pcie);
  613. return ret;
  614. }
  615. int __init mpc83xx_add_bridge(struct device_node *dev)
  616. {
  617. int ret;
  618. int len;
  619. struct pci_controller *hose;
  620. struct resource rsrc_reg;
  621. struct resource rsrc_cfg;
  622. const int *bus_range;
  623. int primary;
  624. is_mpc83xx_pci = 1;
  625. if (!of_device_is_available(dev)) {
  626. pr_warning("%s: disabled by the firmware.\n",
  627. dev->full_name);
  628. return -ENODEV;
  629. }
  630. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  631. /* Fetch host bridge registers address */
  632. if (of_address_to_resource(dev, 0, &rsrc_reg)) {
  633. printk(KERN_WARNING "Can't get pci register base!\n");
  634. return -ENOMEM;
  635. }
  636. memset(&rsrc_cfg, 0, sizeof(rsrc_cfg));
  637. if (of_address_to_resource(dev, 1, &rsrc_cfg)) {
  638. printk(KERN_WARNING
  639. "No pci config register base in dev tree, "
  640. "using default\n");
  641. /*
  642. * MPC83xx supports up to two host controllers
  643. * one at 0x8500 has config space registers at 0x8300
  644. * one at 0x8600 has config space registers at 0x8380
  645. */
  646. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  647. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8300;
  648. else if ((rsrc_reg.start & 0xfffff) == 0x8600)
  649. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8380;
  650. }
  651. /*
  652. * Controller at offset 0x8500 is primary
  653. */
  654. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  655. primary = 1;
  656. else
  657. primary = 0;
  658. /* Get bus range if any */
  659. bus_range = of_get_property(dev, "bus-range", &len);
  660. if (bus_range == NULL || len < 2 * sizeof(int)) {
  661. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  662. " bus 0\n", dev->full_name);
  663. }
  664. pci_add_flags(PCI_REASSIGN_ALL_BUS);
  665. hose = pcibios_alloc_controller(dev);
  666. if (!hose)
  667. return -ENOMEM;
  668. hose->first_busno = bus_range ? bus_range[0] : 0;
  669. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  670. if (of_device_is_compatible(dev, "fsl,mpc8314-pcie")) {
  671. ret = mpc83xx_pcie_setup(hose, &rsrc_reg);
  672. if (ret)
  673. goto err0;
  674. } else {
  675. setup_indirect_pci(hose, rsrc_cfg.start,
  676. rsrc_cfg.start + 4, 0);
  677. }
  678. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  679. "Firmware bus number: %d->%d\n",
  680. (unsigned long long)rsrc_reg.start, hose->first_busno,
  681. hose->last_busno);
  682. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  683. hose, hose->cfg_addr, hose->cfg_data);
  684. /* Interpret the "ranges" property */
  685. /* This also maps the I/O region and sets isa_io/mem_base */
  686. pci_process_bridge_OF_ranges(hose, dev, primary);
  687. return 0;
  688. err0:
  689. pcibios_free_controller(hose);
  690. return ret;
  691. }
  692. #endif /* CONFIG_PPC_83xx */
  693. u64 fsl_pci_immrbar_base(struct pci_controller *hose)
  694. {
  695. #ifdef CONFIG_PPC_83xx
  696. if (is_mpc83xx_pci) {
  697. struct mpc83xx_pcie_priv *pcie = hose->dn->data;
  698. struct pex_inbound_window *in;
  699. int i;
  700. /* Walk the Root Complex Inbound windows to match IMMR base */
  701. in = pcie->cfg_type0 + PEX_RC_INWIN_BASE;
  702. for (i = 0; i < 4; i++) {
  703. /* not enabled, skip */
  704. if (!in_le32(&in[i].ar) & PEX_RCIWARn_EN)
  705. continue;
  706. if (get_immrbase() == in_le32(&in[i].tar))
  707. return (u64)in_le32(&in[i].barh) << 32 |
  708. in_le32(&in[i].barl);
  709. }
  710. printk(KERN_WARNING "could not find PCI BAR matching IMMR\n");
  711. }
  712. #endif
  713. #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
  714. if (!is_mpc83xx_pci) {
  715. u32 base;
  716. pci_bus_read_config_dword(hose->bus,
  717. PCI_DEVFN(0, 0), PCI_BASE_ADDRESS_0, &base);
  718. return base;
  719. }
  720. #endif
  721. return 0;
  722. }
  723. #ifdef CONFIG_E500
  724. static int mcheck_handle_load(struct pt_regs *regs, u32 inst)
  725. {
  726. unsigned int rd, ra, rb, d;
  727. rd = get_rt(inst);
  728. ra = get_ra(inst);
  729. rb = get_rb(inst);
  730. d = get_d(inst);
  731. switch (get_op(inst)) {
  732. case 31:
  733. switch (get_xop(inst)) {
  734. case OP_31_XOP_LWZX:
  735. case OP_31_XOP_LWBRX:
  736. regs->gpr[rd] = 0xffffffff;
  737. break;
  738. case OP_31_XOP_LWZUX:
  739. regs->gpr[rd] = 0xffffffff;
  740. regs->gpr[ra] += regs->gpr[rb];
  741. break;
  742. case OP_31_XOP_LBZX:
  743. regs->gpr[rd] = 0xff;
  744. break;
  745. case OP_31_XOP_LBZUX:
  746. regs->gpr[rd] = 0xff;
  747. regs->gpr[ra] += regs->gpr[rb];
  748. break;
  749. case OP_31_XOP_LHZX:
  750. case OP_31_XOP_LHBRX:
  751. regs->gpr[rd] = 0xffff;
  752. break;
  753. case OP_31_XOP_LHZUX:
  754. regs->gpr[rd] = 0xffff;
  755. regs->gpr[ra] += regs->gpr[rb];
  756. break;
  757. case OP_31_XOP_LHAX:
  758. regs->gpr[rd] = ~0UL;
  759. break;
  760. case OP_31_XOP_LHAUX:
  761. regs->gpr[rd] = ~0UL;
  762. regs->gpr[ra] += regs->gpr[rb];
  763. break;
  764. default:
  765. return 0;
  766. }
  767. break;
  768. case OP_LWZ:
  769. regs->gpr[rd] = 0xffffffff;
  770. break;
  771. case OP_LWZU:
  772. regs->gpr[rd] = 0xffffffff;
  773. regs->gpr[ra] += (s16)d;
  774. break;
  775. case OP_LBZ:
  776. regs->gpr[rd] = 0xff;
  777. break;
  778. case OP_LBZU:
  779. regs->gpr[rd] = 0xff;
  780. regs->gpr[ra] += (s16)d;
  781. break;
  782. case OP_LHZ:
  783. regs->gpr[rd] = 0xffff;
  784. break;
  785. case OP_LHZU:
  786. regs->gpr[rd] = 0xffff;
  787. regs->gpr[ra] += (s16)d;
  788. break;
  789. case OP_LHA:
  790. regs->gpr[rd] = ~0UL;
  791. break;
  792. case OP_LHAU:
  793. regs->gpr[rd] = ~0UL;
  794. regs->gpr[ra] += (s16)d;
  795. break;
  796. default:
  797. return 0;
  798. }
  799. return 1;
  800. }
  801. static int is_in_pci_mem_space(phys_addr_t addr)
  802. {
  803. struct pci_controller *hose;
  804. struct resource *res;
  805. int i;
  806. list_for_each_entry(hose, &hose_list, list_node) {
  807. if (!(hose->indirect_type & PPC_INDIRECT_TYPE_EXT_REG))
  808. continue;
  809. for (i = 0; i < 3; i++) {
  810. res = &hose->mem_resources[i];
  811. if ((res->flags & IORESOURCE_MEM) &&
  812. addr >= res->start && addr <= res->end)
  813. return 1;
  814. }
  815. }
  816. return 0;
  817. }
  818. int fsl_pci_mcheck_exception(struct pt_regs *regs)
  819. {
  820. u32 inst;
  821. int ret;
  822. phys_addr_t addr = 0;
  823. /* Let KVM/QEMU deal with the exception */
  824. if (regs->msr & MSR_GS)
  825. return 0;
  826. #ifdef CONFIG_PHYS_64BIT
  827. addr = mfspr(SPRN_MCARU);
  828. addr <<= 32;
  829. #endif
  830. addr += mfspr(SPRN_MCAR);
  831. if (is_in_pci_mem_space(addr)) {
  832. if (user_mode(regs)) {
  833. pagefault_disable();
  834. ret = get_user(regs->nip, &inst);
  835. pagefault_enable();
  836. } else {
  837. ret = probe_kernel_address(regs->nip, inst);
  838. }
  839. if (mcheck_handle_load(regs, inst)) {
  840. regs->nip += 4;
  841. return 1;
  842. }
  843. }
  844. return 0;
  845. }
  846. #endif
  847. #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
  848. static const struct of_device_id pci_ids[] = {
  849. { .compatible = "fsl,mpc8540-pci", },
  850. { .compatible = "fsl,mpc8548-pcie", },
  851. { .compatible = "fsl,mpc8610-pci", },
  852. { .compatible = "fsl,mpc8641-pcie", },
  853. { .compatible = "fsl,qoriq-pcie-v2.1", },
  854. { .compatible = "fsl,qoriq-pcie-v2.2", },
  855. { .compatible = "fsl,qoriq-pcie-v2.3", },
  856. { .compatible = "fsl,qoriq-pcie-v2.4", },
  857. { .compatible = "fsl,qoriq-pcie-v3.0", },
  858. /*
  859. * The following entries are for compatibility with older device
  860. * trees.
  861. */
  862. { .compatible = "fsl,p1022-pcie", },
  863. { .compatible = "fsl,p4080-pcie", },
  864. {},
  865. };
  866. struct device_node *fsl_pci_primary;
  867. void fsl_pci_assign_primary(void)
  868. {
  869. struct device_node *np;
  870. /* Callers can specify the primary bus using other means. */
  871. if (fsl_pci_primary)
  872. return;
  873. /* If a PCI host bridge contains an ISA node, it's primary. */
  874. np = of_find_node_by_type(NULL, "isa");
  875. while ((fsl_pci_primary = of_get_parent(np))) {
  876. of_node_put(np);
  877. np = fsl_pci_primary;
  878. if (of_match_node(pci_ids, np) && of_device_is_available(np))
  879. return;
  880. }
  881. /*
  882. * If there's no PCI host bridge with ISA, arbitrarily
  883. * designate one as primary. This can go away once
  884. * various bugs with primary-less systems are fixed.
  885. */
  886. for_each_matching_node(np, pci_ids) {
  887. if (of_device_is_available(np)) {
  888. fsl_pci_primary = np;
  889. of_node_put(np);
  890. return;
  891. }
  892. }
  893. }
  894. static int fsl_pci_probe(struct platform_device *pdev)
  895. {
  896. int ret;
  897. struct device_node *node;
  898. #ifdef CONFIG_SWIOTLB
  899. struct pci_controller *hose;
  900. #endif
  901. node = pdev->dev.of_node;
  902. ret = fsl_add_bridge(pdev, fsl_pci_primary == node);
  903. #ifdef CONFIG_SWIOTLB
  904. if (ret == 0) {
  905. hose = pci_find_hose_for_OF_device(pdev->dev.of_node);
  906. /*
  907. * if we couldn't map all of DRAM via the dma windows
  908. * we need SWIOTLB to handle buffers located outside of
  909. * dma capable memory region
  910. */
  911. if (memblock_end_of_DRAM() - 1 > hose->dma_window_base_cur +
  912. hose->dma_window_size)
  913. ppc_swiotlb_enable = 1;
  914. }
  915. #endif
  916. mpc85xx_pci_err_probe(pdev);
  917. return 0;
  918. }
  919. #ifdef CONFIG_PM
  920. static int fsl_pci_resume(struct device *dev)
  921. {
  922. struct pci_controller *hose;
  923. struct resource pci_rsrc;
  924. hose = pci_find_hose_for_OF_device(dev->of_node);
  925. if (!hose)
  926. return -ENODEV;
  927. if (of_address_to_resource(dev->of_node, 0, &pci_rsrc)) {
  928. dev_err(dev, "Get pci register base failed.");
  929. return -ENODEV;
  930. }
  931. setup_pci_atmu(hose);
  932. return 0;
  933. }
  934. static const struct dev_pm_ops pci_pm_ops = {
  935. .resume = fsl_pci_resume,
  936. };
  937. #define PCI_PM_OPS (&pci_pm_ops)
  938. #else
  939. #define PCI_PM_OPS NULL
  940. #endif
  941. static struct platform_driver fsl_pci_driver = {
  942. .driver = {
  943. .name = "fsl-pci",
  944. .pm = PCI_PM_OPS,
  945. .of_match_table = pci_ids,
  946. },
  947. .probe = fsl_pci_probe,
  948. };
  949. static int __init fsl_pci_init(void)
  950. {
  951. return platform_driver_register(&fsl_pci_driver);
  952. }
  953. arch_initcall(fsl_pci_init);
  954. #endif