fsl_pci.c 26 KB

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