fsl_pci.c 25 KB

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