fsl_pci.c 26 KB

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