fsl_pci.c 25 KB

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