fsl_pci.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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 <asm/io.h>
  30. #include <asm/prom.h>
  31. #include <asm/pci-bridge.h>
  32. #include <asm/machdep.h>
  33. #include <sysdev/fsl_soc.h>
  34. #include <sysdev/fsl_pci.h>
  35. static int fsl_pcie_bus_fixup, is_mpc83xx_pci;
  36. static void quirk_fsl_pcie_header(struct pci_dev *dev)
  37. {
  38. u8 hdr_type;
  39. /* if we aren't a PCIe don't bother */
  40. if (!pci_find_capability(dev, PCI_CAP_ID_EXP))
  41. return;
  42. /* if we aren't in host mode don't bother */
  43. pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type);
  44. if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE)
  45. return;
  46. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  47. fsl_pcie_bus_fixup = 1;
  48. return;
  49. }
  50. static int fsl_indirect_read_config(struct pci_bus *, unsigned int,
  51. int, int, u32 *);
  52. static int fsl_pcie_check_link(struct pci_controller *hose)
  53. {
  54. u32 val = 0;
  55. if (hose->indirect_type & PPC_INDIRECT_TYPE_FSL_CFG_REG_LINK) {
  56. if (hose->ops->read == fsl_indirect_read_config) {
  57. struct pci_bus bus;
  58. bus.number = 0;
  59. bus.sysdata = hose;
  60. bus.ops = hose->ops;
  61. indirect_read_config(&bus, 0, PCIE_LTSSM, 4, &val);
  62. } else
  63. early_read_config_dword(hose, 0, 0, PCIE_LTSSM, &val);
  64. if (val < PCIE_LTSSM_L0)
  65. return 1;
  66. } else {
  67. struct ccsr_pci __iomem *pci = hose->private_data;
  68. /* for PCIe IP rev 3.0 or greater use CSR0 for link state */
  69. val = (in_be32(&pci->pex_csr0) & PEX_CSR0_LTSSM_MASK)
  70. >> PEX_CSR0_LTSSM_SHIFT;
  71. if (val != PEX_CSR0_LTSSM_L0)
  72. return 1;
  73. }
  74. return 0;
  75. }
  76. static int fsl_indirect_read_config(struct pci_bus *bus, unsigned int devfn,
  77. int offset, int len, u32 *val)
  78. {
  79. struct pci_controller *hose = pci_bus_to_host(bus);
  80. if (fsl_pcie_check_link(hose))
  81. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  82. else
  83. hose->indirect_type &= ~PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  84. return indirect_read_config(bus, devfn, offset, len, val);
  85. }
  86. static struct pci_ops fsl_indirect_pci_ops =
  87. {
  88. .read = fsl_indirect_read_config,
  89. .write = indirect_write_config,
  90. };
  91. static void __init fsl_setup_indirect_pci(struct pci_controller* hose,
  92. resource_size_t cfg_addr,
  93. resource_size_t cfg_data, u32 flags)
  94. {
  95. setup_indirect_pci(hose, cfg_addr, cfg_data, flags);
  96. hose->ops = &fsl_indirect_pci_ops;
  97. }
  98. #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
  99. #define MAX_PHYS_ADDR_BITS 40
  100. static u64 pci64_dma_offset = 1ull << MAX_PHYS_ADDR_BITS;
  101. static int fsl_pci_dma_set_mask(struct device *dev, u64 dma_mask)
  102. {
  103. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  104. return -EIO;
  105. /*
  106. * Fixup PCI devices that are able to DMA to above the physical
  107. * address width of the SoC such that we can address any internal
  108. * SoC address from across PCI if needed
  109. */
  110. if ((dev->bus == &pci_bus_type) &&
  111. dma_mask >= DMA_BIT_MASK(MAX_PHYS_ADDR_BITS)) {
  112. set_dma_ops(dev, &dma_direct_ops);
  113. set_dma_offset(dev, pci64_dma_offset);
  114. }
  115. *dev->dma_mask = dma_mask;
  116. return 0;
  117. }
  118. static int setup_one_atmu(struct ccsr_pci __iomem *pci,
  119. unsigned int index, const struct resource *res,
  120. resource_size_t offset)
  121. {
  122. resource_size_t pci_addr = res->start - offset;
  123. resource_size_t phys_addr = res->start;
  124. resource_size_t size = resource_size(res);
  125. u32 flags = 0x80044000; /* enable & mem R/W */
  126. unsigned int i;
  127. pr_debug("PCI MEM resource start 0x%016llx, size 0x%016llx.\n",
  128. (u64)res->start, (u64)size);
  129. if (res->flags & IORESOURCE_PREFETCH)
  130. flags |= 0x10000000; /* enable relaxed ordering */
  131. for (i = 0; size > 0; i++) {
  132. unsigned int bits = min(ilog2(size),
  133. __ffs(pci_addr | phys_addr));
  134. if (index + i >= 5)
  135. return -1;
  136. out_be32(&pci->pow[index + i].potar, pci_addr >> 12);
  137. out_be32(&pci->pow[index + i].potear, (u64)pci_addr >> 44);
  138. out_be32(&pci->pow[index + i].powbar, phys_addr >> 12);
  139. out_be32(&pci->pow[index + i].powar, flags | (bits - 1));
  140. pci_addr += (resource_size_t)1U << bits;
  141. phys_addr += (resource_size_t)1U << bits;
  142. size -= (resource_size_t)1U << bits;
  143. }
  144. return i;
  145. }
  146. /* atmu setup for fsl pci/pcie controller */
  147. static void setup_pci_atmu(struct pci_controller *hose)
  148. {
  149. struct ccsr_pci __iomem *pci = hose->private_data;
  150. int i, j, n, mem_log, win_idx = 3, start_idx = 1, end_idx = 4;
  151. u64 mem, sz, paddr_hi = 0;
  152. u64 paddr_lo = ULLONG_MAX;
  153. u32 pcicsrbar = 0, pcicsrbar_sz;
  154. u32 piwar = PIWAR_EN | PIWAR_PF | PIWAR_TGI_LOCAL |
  155. PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
  156. const char *name = hose->dn->full_name;
  157. const u64 *reg;
  158. int len;
  159. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  160. if (in_be32(&pci->block_rev1) >= PCIE_IP_REV_2_2) {
  161. win_idx = 2;
  162. start_idx = 0;
  163. end_idx = 3;
  164. }
  165. }
  166. /* Disable all windows (except powar0 since it's ignored) */
  167. for(i = 1; i < 5; i++)
  168. out_be32(&pci->pow[i].powar, 0);
  169. for (i = start_idx; i < end_idx; i++)
  170. out_be32(&pci->piw[i].piwar, 0);
  171. /* Setup outbound MEM window */
  172. for(i = 0, j = 1; i < 3; i++) {
  173. if (!(hose->mem_resources[i].flags & IORESOURCE_MEM))
  174. continue;
  175. paddr_lo = min(paddr_lo, (u64)hose->mem_resources[i].start);
  176. paddr_hi = max(paddr_hi, (u64)hose->mem_resources[i].end);
  177. n = setup_one_atmu(pci, j, &hose->mem_resources[i],
  178. hose->pci_mem_offset);
  179. if (n < 0 || j >= 5) {
  180. pr_err("Ran out of outbound PCI ATMUs for resource %d!\n", i);
  181. hose->mem_resources[i].flags |= IORESOURCE_DISABLED;
  182. } else
  183. j += n;
  184. }
  185. /* Setup outbound IO window */
  186. if (hose->io_resource.flags & IORESOURCE_IO) {
  187. if (j >= 5) {
  188. pr_err("Ran out of outbound PCI ATMUs for IO resource\n");
  189. } else {
  190. pr_debug("PCI IO resource start 0x%016llx, size 0x%016llx, "
  191. "phy base 0x%016llx.\n",
  192. (u64)hose->io_resource.start,
  193. (u64)resource_size(&hose->io_resource),
  194. (u64)hose->io_base_phys);
  195. out_be32(&pci->pow[j].potar, (hose->io_resource.start >> 12));
  196. out_be32(&pci->pow[j].potear, 0);
  197. out_be32(&pci->pow[j].powbar, (hose->io_base_phys >> 12));
  198. /* Enable, IO R/W */
  199. out_be32(&pci->pow[j].powar, 0x80088000
  200. | (ilog2(hose->io_resource.end
  201. - hose->io_resource.start + 1) - 1));
  202. }
  203. }
  204. /* convert to pci address space */
  205. paddr_hi -= hose->pci_mem_offset;
  206. paddr_lo -= hose->pci_mem_offset;
  207. if (paddr_hi == paddr_lo) {
  208. pr_err("%s: No outbound window space\n", name);
  209. return;
  210. }
  211. if (paddr_lo == 0) {
  212. pr_err("%s: No space for inbound window\n", name);
  213. return;
  214. }
  215. /* setup PCSRBAR/PEXCSRBAR */
  216. early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, 0xffffffff);
  217. early_read_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, &pcicsrbar_sz);
  218. pcicsrbar_sz = ~pcicsrbar_sz + 1;
  219. if (paddr_hi < (0x100000000ull - pcicsrbar_sz) ||
  220. (paddr_lo > 0x100000000ull))
  221. pcicsrbar = 0x100000000ull - pcicsrbar_sz;
  222. else
  223. pcicsrbar = (paddr_lo - pcicsrbar_sz) & -pcicsrbar_sz;
  224. early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, pcicsrbar);
  225. paddr_lo = min(paddr_lo, (u64)pcicsrbar);
  226. pr_info("%s: PCICSRBAR @ 0x%x\n", name, pcicsrbar);
  227. /* Setup inbound mem window */
  228. mem = memblock_end_of_DRAM();
  229. /*
  230. * The msi-address-64 property, if it exists, indicates the physical
  231. * address of the MSIIR register. Normally, this register is located
  232. * inside CCSR, so the ATMU that covers all of CCSR is used. But if
  233. * this property exists, then we normally need to create a new ATMU
  234. * for it. For now, however, we cheat. The only entity that creates
  235. * this property is the Freescale hypervisor, and the address is
  236. * specified in the partition configuration. Typically, the address
  237. * is located in the page immediately after the end of DDR. If so, we
  238. * can avoid allocating a new ATMU by extending the DDR ATMU by one
  239. * page.
  240. */
  241. reg = of_get_property(hose->dn, "msi-address-64", &len);
  242. if (reg && (len == sizeof(u64))) {
  243. u64 address = be64_to_cpup(reg);
  244. if ((address >= mem) && (address < (mem + PAGE_SIZE))) {
  245. pr_info("%s: extending DDR ATMU to cover MSIIR", name);
  246. mem += PAGE_SIZE;
  247. } else {
  248. /* TODO: Create a new ATMU for MSIIR */
  249. pr_warn("%s: msi-address-64 address of %llx is "
  250. "unsupported\n", name, address);
  251. }
  252. }
  253. sz = min(mem, paddr_lo);
  254. mem_log = ilog2(sz);
  255. /* PCIe can overmap inbound & outbound since RX & TX are separated */
  256. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  257. /* Size window to exact size if power-of-two or one size up */
  258. if ((1ull << mem_log) != mem) {
  259. if ((1ull << mem_log) > mem)
  260. pr_info("%s: Setting PCI inbound window "
  261. "greater than memory size\n", name);
  262. mem_log++;
  263. }
  264. piwar |= ((mem_log - 1) & PIWAR_SZ_MASK);
  265. /* Setup inbound memory window */
  266. out_be32(&pci->piw[win_idx].pitar, 0x00000000);
  267. out_be32(&pci->piw[win_idx].piwbar, 0x00000000);
  268. out_be32(&pci->piw[win_idx].piwar, piwar);
  269. win_idx--;
  270. hose->dma_window_base_cur = 0x00000000;
  271. hose->dma_window_size = (resource_size_t)sz;
  272. /*
  273. * if we have >4G of memory setup second PCI inbound window to
  274. * let devices that are 64-bit address capable to work w/o
  275. * SWIOTLB and access the full range of memory
  276. */
  277. if (sz != mem) {
  278. mem_log = ilog2(mem);
  279. /* Size window up if we dont fit in exact power-of-2 */
  280. if ((1ull << mem_log) != mem)
  281. mem_log++;
  282. piwar = (piwar & ~PIWAR_SZ_MASK) | (mem_log - 1);
  283. /* Setup inbound memory window */
  284. out_be32(&pci->piw[win_idx].pitar, 0x00000000);
  285. out_be32(&pci->piw[win_idx].piwbear,
  286. pci64_dma_offset >> 44);
  287. out_be32(&pci->piw[win_idx].piwbar,
  288. pci64_dma_offset >> 12);
  289. out_be32(&pci->piw[win_idx].piwar, piwar);
  290. /*
  291. * install our own dma_set_mask handler to fixup dma_ops
  292. * and dma_offset
  293. */
  294. ppc_md.dma_set_mask = fsl_pci_dma_set_mask;
  295. pr_info("%s: Setup 64-bit PCI DMA window\n", name);
  296. }
  297. } else {
  298. u64 paddr = 0;
  299. /* Setup inbound memory window */
  300. out_be32(&pci->piw[win_idx].pitar, paddr >> 12);
  301. out_be32(&pci->piw[win_idx].piwbar, paddr >> 12);
  302. out_be32(&pci->piw[win_idx].piwar, (piwar | (mem_log - 1)));
  303. win_idx--;
  304. paddr += 1ull << mem_log;
  305. sz -= 1ull << mem_log;
  306. if (sz) {
  307. mem_log = ilog2(sz);
  308. piwar |= (mem_log - 1);
  309. out_be32(&pci->piw[win_idx].pitar, paddr >> 12);
  310. out_be32(&pci->piw[win_idx].piwbar, paddr >> 12);
  311. out_be32(&pci->piw[win_idx].piwar, piwar);
  312. win_idx--;
  313. paddr += 1ull << mem_log;
  314. }
  315. hose->dma_window_base_cur = 0x00000000;
  316. hose->dma_window_size = (resource_size_t)paddr;
  317. }
  318. if (hose->dma_window_size < mem) {
  319. #ifndef CONFIG_SWIOTLB
  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. fsl_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. /* 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. fsl_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. #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
  724. static const struct of_device_id pci_ids[] = {
  725. { .compatible = "fsl,mpc8540-pci", },
  726. { .compatible = "fsl,mpc8548-pcie", },
  727. { .compatible = "fsl,mpc8610-pci", },
  728. { .compatible = "fsl,mpc8641-pcie", },
  729. { .compatible = "fsl,qoriq-pcie-v2.1", },
  730. { .compatible = "fsl,qoriq-pcie-v2.2", },
  731. { .compatible = "fsl,qoriq-pcie-v2.3", },
  732. { .compatible = "fsl,qoriq-pcie-v2.4", },
  733. { .compatible = "fsl,qoriq-pcie-v3.0", },
  734. /*
  735. * The following entries are for compatibility with older device
  736. * trees.
  737. */
  738. { .compatible = "fsl,p1022-pcie", },
  739. { .compatible = "fsl,p4080-pcie", },
  740. {},
  741. };
  742. struct device_node *fsl_pci_primary;
  743. void fsl_pci_assign_primary(void)
  744. {
  745. struct device_node *np;
  746. /* Callers can specify the primary bus using other means. */
  747. if (fsl_pci_primary)
  748. return;
  749. /* If a PCI host bridge contains an ISA node, it's primary. */
  750. np = of_find_node_by_type(NULL, "isa");
  751. while ((fsl_pci_primary = of_get_parent(np))) {
  752. of_node_put(np);
  753. np = fsl_pci_primary;
  754. if (of_match_node(pci_ids, np) && of_device_is_available(np))
  755. return;
  756. }
  757. /*
  758. * If there's no PCI host bridge with ISA, arbitrarily
  759. * designate one as primary. This can go away once
  760. * various bugs with primary-less systems are fixed.
  761. */
  762. for_each_matching_node(np, pci_ids) {
  763. if (of_device_is_available(np)) {
  764. fsl_pci_primary = np;
  765. of_node_put(np);
  766. return;
  767. }
  768. }
  769. }
  770. static int fsl_pci_probe(struct platform_device *pdev)
  771. {
  772. int ret;
  773. struct device_node *node;
  774. #ifdef CONFIG_SWIOTLB
  775. struct pci_controller *hose;
  776. #endif
  777. node = pdev->dev.of_node;
  778. ret = fsl_add_bridge(pdev, fsl_pci_primary == node);
  779. #ifdef CONFIG_SWIOTLB
  780. if (ret == 0) {
  781. hose = pci_find_hose_for_OF_device(pdev->dev.of_node);
  782. /*
  783. * if we couldn't map all of DRAM via the dma windows
  784. * we need SWIOTLB to handle buffers located outside of
  785. * dma capable memory region
  786. */
  787. if (memblock_end_of_DRAM() - 1 > hose->dma_window_base_cur +
  788. hose->dma_window_size)
  789. ppc_swiotlb_enable = 1;
  790. }
  791. #endif
  792. mpc85xx_pci_err_probe(pdev);
  793. return 0;
  794. }
  795. #ifdef CONFIG_PM
  796. static int fsl_pci_resume(struct device *dev)
  797. {
  798. struct pci_controller *hose;
  799. struct resource pci_rsrc;
  800. hose = pci_find_hose_for_OF_device(dev->of_node);
  801. if (!hose)
  802. return -ENODEV;
  803. if (of_address_to_resource(dev->of_node, 0, &pci_rsrc)) {
  804. dev_err(dev, "Get pci register base failed.");
  805. return -ENODEV;
  806. }
  807. setup_pci_atmu(hose, &pci_rsrc);
  808. return 0;
  809. }
  810. static const struct dev_pm_ops pci_pm_ops = {
  811. .resume = fsl_pci_resume,
  812. };
  813. #define PCI_PM_OPS (&pci_pm_ops)
  814. #else
  815. #define PCI_PM_OPS NULL
  816. #endif
  817. static struct platform_driver fsl_pci_driver = {
  818. .driver = {
  819. .name = "fsl-pci",
  820. .pm = PCI_PM_OPS,
  821. .of_match_table = pci_ids,
  822. },
  823. .probe = fsl_pci_probe,
  824. };
  825. static int __init fsl_pci_init(void)
  826. {
  827. return platform_driver_register(&fsl_pci_driver);
  828. }
  829. arch_initcall(fsl_pci_init);
  830. #endif