fsl_pci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * MPC83xx/85xx/86xx PCI/PCIE support routing.
  3. *
  4. * Copyright 2007-2011 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 __init quirk_fsl_pcie_header(struct pci_dev *dev)
  37. {
  38. u8 progif;
  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_CLASS_PROG, &progif);
  44. if (progif & 0x1)
  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 __init 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 __init 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. char *name = hose->dn->full_name;
  118. pr_debug("PCI memory map start 0x%016llx, size 0x%016llx\n",
  119. (u64)rsrc->start, (u64)resource_size(rsrc));
  120. if (of_device_is_compatible(hose->dn, "fsl,qoriq-pcie-v2.2")) {
  121. win_idx = 2;
  122. start_idx = 0;
  123. end_idx = 3;
  124. }
  125. pci = ioremap(rsrc->start, resource_size(rsrc));
  126. if (!pci) {
  127. dev_err(hose->parent, "Unable to map ATMU registers\n");
  128. return;
  129. }
  130. /* Disable all windows (except powar0 since it's ignored) */
  131. for(i = 1; i < 5; i++)
  132. out_be32(&pci->pow[i].powar, 0);
  133. for (i = start_idx; i < end_idx; i++)
  134. out_be32(&pci->piw[i].piwar, 0);
  135. /* Setup outbound MEM window */
  136. for(i = 0, j = 1; i < 3; i++) {
  137. if (!(hose->mem_resources[i].flags & IORESOURCE_MEM))
  138. continue;
  139. paddr_lo = min(paddr_lo, (u64)hose->mem_resources[i].start);
  140. paddr_hi = max(paddr_hi, (u64)hose->mem_resources[i].end);
  141. n = setup_one_atmu(pci, j, &hose->mem_resources[i],
  142. hose->pci_mem_offset);
  143. if (n < 0 || j >= 5) {
  144. pr_err("Ran out of outbound PCI ATMUs for resource %d!\n", i);
  145. hose->mem_resources[i].flags |= IORESOURCE_DISABLED;
  146. } else
  147. j += n;
  148. }
  149. /* Setup outbound IO window */
  150. if (hose->io_resource.flags & IORESOURCE_IO) {
  151. if (j >= 5) {
  152. pr_err("Ran out of outbound PCI ATMUs for IO resource\n");
  153. } else {
  154. pr_debug("PCI IO resource start 0x%016llx, size 0x%016llx, "
  155. "phy base 0x%016llx.\n",
  156. (u64)hose->io_resource.start,
  157. (u64)resource_size(&hose->io_resource),
  158. (u64)hose->io_base_phys);
  159. out_be32(&pci->pow[j].potar, (hose->io_resource.start >> 12));
  160. out_be32(&pci->pow[j].potear, 0);
  161. out_be32(&pci->pow[j].powbar, (hose->io_base_phys >> 12));
  162. /* Enable, IO R/W */
  163. out_be32(&pci->pow[j].powar, 0x80088000
  164. | (__ilog2(hose->io_resource.end
  165. - hose->io_resource.start + 1) - 1));
  166. }
  167. }
  168. /* convert to pci address space */
  169. paddr_hi -= hose->pci_mem_offset;
  170. paddr_lo -= hose->pci_mem_offset;
  171. if (paddr_hi == paddr_lo) {
  172. pr_err("%s: No outbound window space\n", name);
  173. return ;
  174. }
  175. if (paddr_lo == 0) {
  176. pr_err("%s: No space for inbound window\n", name);
  177. return ;
  178. }
  179. /* setup PCSRBAR/PEXCSRBAR */
  180. early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, 0xffffffff);
  181. early_read_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, &pcicsrbar_sz);
  182. pcicsrbar_sz = ~pcicsrbar_sz + 1;
  183. if (paddr_hi < (0x100000000ull - pcicsrbar_sz) ||
  184. (paddr_lo > 0x100000000ull))
  185. pcicsrbar = 0x100000000ull - pcicsrbar_sz;
  186. else
  187. pcicsrbar = (paddr_lo - pcicsrbar_sz) & -pcicsrbar_sz;
  188. early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, pcicsrbar);
  189. paddr_lo = min(paddr_lo, (u64)pcicsrbar);
  190. pr_info("%s: PCICSRBAR @ 0x%x\n", name, pcicsrbar);
  191. /* Setup inbound mem window */
  192. mem = memblock_end_of_DRAM();
  193. sz = min(mem, paddr_lo);
  194. mem_log = __ilog2_u64(sz);
  195. /* PCIe can overmap inbound & outbound since RX & TX are separated */
  196. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  197. /* Size window to exact size if power-of-two or one size up */
  198. if ((1ull << mem_log) != mem) {
  199. if ((1ull << mem_log) > mem)
  200. pr_info("%s: Setting PCI inbound window "
  201. "greater than memory size\n", name);
  202. mem_log++;
  203. }
  204. piwar |= ((mem_log - 1) & PIWAR_SZ_MASK);
  205. /* Setup inbound memory window */
  206. out_be32(&pci->piw[win_idx].pitar, 0x00000000);
  207. out_be32(&pci->piw[win_idx].piwbar, 0x00000000);
  208. out_be32(&pci->piw[win_idx].piwar, piwar);
  209. win_idx--;
  210. hose->dma_window_base_cur = 0x00000000;
  211. hose->dma_window_size = (resource_size_t)sz;
  212. /*
  213. * if we have >4G of memory setup second PCI inbound window to
  214. * let devices that are 64-bit address capable to work w/o
  215. * SWIOTLB and access the full range of memory
  216. */
  217. if (sz != mem) {
  218. mem_log = __ilog2_u64(mem);
  219. /* Size window up if we dont fit in exact power-of-2 */
  220. if ((1ull << mem_log) != mem)
  221. mem_log++;
  222. piwar = (piwar & ~PIWAR_SZ_MASK) | (mem_log - 1);
  223. /* Setup inbound memory window */
  224. out_be32(&pci->piw[win_idx].pitar, 0x00000000);
  225. out_be32(&pci->piw[win_idx].piwbear,
  226. pci64_dma_offset >> 44);
  227. out_be32(&pci->piw[win_idx].piwbar,
  228. pci64_dma_offset >> 12);
  229. out_be32(&pci->piw[win_idx].piwar, piwar);
  230. /*
  231. * install our own dma_set_mask handler to fixup dma_ops
  232. * and dma_offset
  233. */
  234. ppc_md.dma_set_mask = fsl_pci_dma_set_mask;
  235. pr_info("%s: Setup 64-bit PCI DMA window\n", name);
  236. }
  237. } else {
  238. u64 paddr = 0;
  239. /* Setup inbound memory window */
  240. out_be32(&pci->piw[win_idx].pitar, paddr >> 12);
  241. out_be32(&pci->piw[win_idx].piwbar, paddr >> 12);
  242. out_be32(&pci->piw[win_idx].piwar, (piwar | (mem_log - 1)));
  243. win_idx--;
  244. paddr += 1ull << mem_log;
  245. sz -= 1ull << mem_log;
  246. if (sz) {
  247. mem_log = __ilog2_u64(sz);
  248. piwar |= (mem_log - 1);
  249. out_be32(&pci->piw[win_idx].pitar, paddr >> 12);
  250. out_be32(&pci->piw[win_idx].piwbar, paddr >> 12);
  251. out_be32(&pci->piw[win_idx].piwar, piwar);
  252. win_idx--;
  253. paddr += 1ull << mem_log;
  254. }
  255. hose->dma_window_base_cur = 0x00000000;
  256. hose->dma_window_size = (resource_size_t)paddr;
  257. }
  258. if (hose->dma_window_size < mem) {
  259. #ifndef CONFIG_SWIOTLB
  260. pr_err("%s: ERROR: Memory size exceeds PCI ATMU ability to "
  261. "map - enable CONFIG_SWIOTLB to avoid dma errors.\n",
  262. name);
  263. #endif
  264. /* adjusting outbound windows could reclaim space in mem map */
  265. if (paddr_hi < 0xffffffffull)
  266. pr_warning("%s: WARNING: Outbound window cfg leaves "
  267. "gaps in memory map. Adjusting the memory map "
  268. "could reduce unnecessary bounce buffering.\n",
  269. name);
  270. pr_info("%s: DMA window size is 0x%llx\n", name,
  271. (u64)hose->dma_window_size);
  272. }
  273. iounmap(pci);
  274. }
  275. static void __init setup_pci_cmd(struct pci_controller *hose)
  276. {
  277. u16 cmd;
  278. int cap_x;
  279. early_read_config_word(hose, 0, 0, PCI_COMMAND, &cmd);
  280. cmd |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY
  281. | PCI_COMMAND_IO;
  282. early_write_config_word(hose, 0, 0, PCI_COMMAND, cmd);
  283. cap_x = early_find_capability(hose, 0, 0, PCI_CAP_ID_PCIX);
  284. if (cap_x) {
  285. int pci_x_cmd = cap_x + PCI_X_CMD;
  286. cmd = PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ
  287. | PCI_X_CMD_ERO | PCI_X_CMD_DPERR_E;
  288. early_write_config_word(hose, 0, 0, pci_x_cmd, cmd);
  289. } else {
  290. early_write_config_byte(hose, 0, 0, PCI_LATENCY_TIMER, 0x80);
  291. }
  292. }
  293. void fsl_pcibios_fixup_bus(struct pci_bus *bus)
  294. {
  295. struct pci_controller *hose = pci_bus_to_host(bus);
  296. int i;
  297. if ((bus->parent == hose->bus) &&
  298. ((fsl_pcie_bus_fixup &&
  299. early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) ||
  300. (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)))
  301. {
  302. for (i = 0; i < 4; ++i) {
  303. struct resource *res = bus->resource[i];
  304. struct resource *par = bus->parent->resource[i];
  305. if (res) {
  306. res->start = 0;
  307. res->end = 0;
  308. res->flags = 0;
  309. }
  310. if (res && par) {
  311. res->start = par->start;
  312. res->end = par->end;
  313. res->flags = par->flags;
  314. }
  315. }
  316. }
  317. }
  318. int __init fsl_add_bridge(struct device_node *dev, int is_primary)
  319. {
  320. int len;
  321. struct pci_controller *hose;
  322. struct resource rsrc;
  323. const int *bus_range;
  324. u8 progif;
  325. if (!of_device_is_available(dev)) {
  326. pr_warning("%s: disabled\n", dev->full_name);
  327. return -ENODEV;
  328. }
  329. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  330. /* Fetch host bridge registers address */
  331. if (of_address_to_resource(dev, 0, &rsrc)) {
  332. printk(KERN_WARNING "Can't get pci register base!");
  333. return -ENOMEM;
  334. }
  335. /* Get bus range if any */
  336. bus_range = of_get_property(dev, "bus-range", &len);
  337. if (bus_range == NULL || len < 2 * sizeof(int))
  338. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  339. " bus 0\n", dev->full_name);
  340. pci_add_flags(PCI_REASSIGN_ALL_BUS);
  341. hose = pcibios_alloc_controller(dev);
  342. if (!hose)
  343. return -ENOMEM;
  344. hose->first_busno = bus_range ? bus_range[0] : 0x0;
  345. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  346. setup_indirect_pci(hose, rsrc.start, rsrc.start + 0x4,
  347. PPC_INDIRECT_TYPE_BIG_ENDIAN);
  348. early_read_config_byte(hose, 0, 0, PCI_CLASS_PROG, &progif);
  349. if ((progif & 1) == 1) {
  350. /* unmap cfg_data & cfg_addr separately if not on same page */
  351. if (((unsigned long)hose->cfg_data & PAGE_MASK) !=
  352. ((unsigned long)hose->cfg_addr & PAGE_MASK))
  353. iounmap(hose->cfg_data);
  354. iounmap(hose->cfg_addr);
  355. pcibios_free_controller(hose);
  356. return 0;
  357. }
  358. setup_pci_cmd(hose);
  359. /* check PCI express link status */
  360. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  361. hose->indirect_type |= PPC_INDIRECT_TYPE_EXT_REG |
  362. PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS;
  363. if (fsl_pcie_check_link(hose))
  364. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  365. }
  366. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  367. "Firmware bus number: %d->%d\n",
  368. (unsigned long long)rsrc.start, hose->first_busno,
  369. hose->last_busno);
  370. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  371. hose, hose->cfg_addr, hose->cfg_data);
  372. /* Interpret the "ranges" property */
  373. /* This also maps the I/O region and sets isa_io/mem_base */
  374. pci_process_bridge_OF_ranges(hose, dev, is_primary);
  375. /* Setup PEX window registers */
  376. setup_pci_atmu(hose, &rsrc);
  377. return 0;
  378. }
  379. #endif /* CONFIG_FSL_SOC_BOOKE || CONFIG_PPC_86xx */
  380. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, quirk_fsl_pcie_header);
  381. #if defined(CONFIG_PPC_83xx) || defined(CONFIG_PPC_MPC512x)
  382. struct mpc83xx_pcie_priv {
  383. void __iomem *cfg_type0;
  384. void __iomem *cfg_type1;
  385. u32 dev_base;
  386. };
  387. struct pex_inbound_window {
  388. u32 ar;
  389. u32 tar;
  390. u32 barl;
  391. u32 barh;
  392. };
  393. /*
  394. * With the convention of u-boot, the PCIE outbound window 0 serves
  395. * as configuration transactions outbound.
  396. */
  397. #define PEX_OUTWIN0_BAR 0xCA4
  398. #define PEX_OUTWIN0_TAL 0xCA8
  399. #define PEX_OUTWIN0_TAH 0xCAC
  400. #define PEX_RC_INWIN_BASE 0xE60
  401. #define PEX_RCIWARn_EN 0x1
  402. static int mpc83xx_pcie_exclude_device(struct pci_bus *bus, unsigned int devfn)
  403. {
  404. struct pci_controller *hose = pci_bus_to_host(bus);
  405. if (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)
  406. return PCIBIOS_DEVICE_NOT_FOUND;
  407. /*
  408. * Workaround for the HW bug: for Type 0 configure transactions the
  409. * PCI-E controller does not check the device number bits and just
  410. * assumes that the device number bits are 0.
  411. */
  412. if (bus->number == hose->first_busno ||
  413. bus->primary == hose->first_busno) {
  414. if (devfn & 0xf8)
  415. return PCIBIOS_DEVICE_NOT_FOUND;
  416. }
  417. if (ppc_md.pci_exclude_device) {
  418. if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
  419. return PCIBIOS_DEVICE_NOT_FOUND;
  420. }
  421. return PCIBIOS_SUCCESSFUL;
  422. }
  423. static void __iomem *mpc83xx_pcie_remap_cfg(struct pci_bus *bus,
  424. unsigned int devfn, int offset)
  425. {
  426. struct pci_controller *hose = pci_bus_to_host(bus);
  427. struct mpc83xx_pcie_priv *pcie = hose->dn->data;
  428. u32 dev_base = bus->number << 24 | devfn << 16;
  429. int ret;
  430. ret = mpc83xx_pcie_exclude_device(bus, devfn);
  431. if (ret)
  432. return NULL;
  433. offset &= 0xfff;
  434. /* Type 0 */
  435. if (bus->number == hose->first_busno)
  436. return pcie->cfg_type0 + offset;
  437. if (pcie->dev_base == dev_base)
  438. goto mapped;
  439. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, dev_base);
  440. pcie->dev_base = dev_base;
  441. mapped:
  442. return pcie->cfg_type1 + offset;
  443. }
  444. static int mpc83xx_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
  445. int offset, int len, u32 *val)
  446. {
  447. void __iomem *cfg_addr;
  448. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  449. if (!cfg_addr)
  450. return PCIBIOS_DEVICE_NOT_FOUND;
  451. switch (len) {
  452. case 1:
  453. *val = in_8(cfg_addr);
  454. break;
  455. case 2:
  456. *val = in_le16(cfg_addr);
  457. break;
  458. default:
  459. *val = in_le32(cfg_addr);
  460. break;
  461. }
  462. return PCIBIOS_SUCCESSFUL;
  463. }
  464. static int mpc83xx_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
  465. int offset, int len, u32 val)
  466. {
  467. struct pci_controller *hose = pci_bus_to_host(bus);
  468. void __iomem *cfg_addr;
  469. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  470. if (!cfg_addr)
  471. return PCIBIOS_DEVICE_NOT_FOUND;
  472. /* PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS */
  473. if (offset == PCI_PRIMARY_BUS && bus->number == hose->first_busno)
  474. val &= 0xffffff00;
  475. switch (len) {
  476. case 1:
  477. out_8(cfg_addr, val);
  478. break;
  479. case 2:
  480. out_le16(cfg_addr, val);
  481. break;
  482. default:
  483. out_le32(cfg_addr, val);
  484. break;
  485. }
  486. return PCIBIOS_SUCCESSFUL;
  487. }
  488. static struct pci_ops mpc83xx_pcie_ops = {
  489. .read = mpc83xx_pcie_read_config,
  490. .write = mpc83xx_pcie_write_config,
  491. };
  492. static int __init mpc83xx_pcie_setup(struct pci_controller *hose,
  493. struct resource *reg)
  494. {
  495. struct mpc83xx_pcie_priv *pcie;
  496. u32 cfg_bar;
  497. int ret = -ENOMEM;
  498. pcie = zalloc_maybe_bootmem(sizeof(*pcie), GFP_KERNEL);
  499. if (!pcie)
  500. return ret;
  501. pcie->cfg_type0 = ioremap(reg->start, resource_size(reg));
  502. if (!pcie->cfg_type0)
  503. goto err0;
  504. cfg_bar = in_le32(pcie->cfg_type0 + PEX_OUTWIN0_BAR);
  505. if (!cfg_bar) {
  506. /* PCI-E isn't configured. */
  507. ret = -ENODEV;
  508. goto err1;
  509. }
  510. pcie->cfg_type1 = ioremap(cfg_bar, 0x1000);
  511. if (!pcie->cfg_type1)
  512. goto err1;
  513. WARN_ON(hose->dn->data);
  514. hose->dn->data = pcie;
  515. hose->ops = &mpc83xx_pcie_ops;
  516. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAH, 0);
  517. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, 0);
  518. if (fsl_pcie_check_link(hose))
  519. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  520. return 0;
  521. err1:
  522. iounmap(pcie->cfg_type0);
  523. err0:
  524. kfree(pcie);
  525. return ret;
  526. }
  527. int __init mpc83xx_add_bridge(struct device_node *dev)
  528. {
  529. int ret;
  530. int len;
  531. struct pci_controller *hose;
  532. struct resource rsrc_reg;
  533. struct resource rsrc_cfg;
  534. const int *bus_range;
  535. int primary;
  536. is_mpc83xx_pci = 1;
  537. if (!of_device_is_available(dev)) {
  538. pr_warning("%s: disabled by the firmware.\n",
  539. dev->full_name);
  540. return -ENODEV;
  541. }
  542. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  543. /* Fetch host bridge registers address */
  544. if (of_address_to_resource(dev, 0, &rsrc_reg)) {
  545. printk(KERN_WARNING "Can't get pci register base!\n");
  546. return -ENOMEM;
  547. }
  548. memset(&rsrc_cfg, 0, sizeof(rsrc_cfg));
  549. if (of_address_to_resource(dev, 1, &rsrc_cfg)) {
  550. printk(KERN_WARNING
  551. "No pci config register base in dev tree, "
  552. "using default\n");
  553. /*
  554. * MPC83xx supports up to two host controllers
  555. * one at 0x8500 has config space registers at 0x8300
  556. * one at 0x8600 has config space registers at 0x8380
  557. */
  558. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  559. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8300;
  560. else if ((rsrc_reg.start & 0xfffff) == 0x8600)
  561. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8380;
  562. }
  563. /*
  564. * Controller at offset 0x8500 is primary
  565. */
  566. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  567. primary = 1;
  568. else
  569. primary = 0;
  570. /* Get bus range if any */
  571. bus_range = of_get_property(dev, "bus-range", &len);
  572. if (bus_range == NULL || len < 2 * sizeof(int)) {
  573. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  574. " bus 0\n", dev->full_name);
  575. }
  576. pci_add_flags(PCI_REASSIGN_ALL_BUS);
  577. hose = pcibios_alloc_controller(dev);
  578. if (!hose)
  579. return -ENOMEM;
  580. hose->first_busno = bus_range ? bus_range[0] : 0;
  581. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  582. if (of_device_is_compatible(dev, "fsl,mpc8314-pcie")) {
  583. ret = mpc83xx_pcie_setup(hose, &rsrc_reg);
  584. if (ret)
  585. goto err0;
  586. } else {
  587. setup_indirect_pci(hose, rsrc_cfg.start,
  588. rsrc_cfg.start + 4, 0);
  589. }
  590. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  591. "Firmware bus number: %d->%d\n",
  592. (unsigned long long)rsrc_reg.start, hose->first_busno,
  593. hose->last_busno);
  594. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  595. hose, hose->cfg_addr, hose->cfg_data);
  596. /* Interpret the "ranges" property */
  597. /* This also maps the I/O region and sets isa_io/mem_base */
  598. pci_process_bridge_OF_ranges(hose, dev, primary);
  599. return 0;
  600. err0:
  601. pcibios_free_controller(hose);
  602. return ret;
  603. }
  604. #endif /* CONFIG_PPC_83xx */
  605. u64 fsl_pci_immrbar_base(struct pci_controller *hose)
  606. {
  607. #ifdef CONFIG_PPC_83xx
  608. if (is_mpc83xx_pci) {
  609. struct mpc83xx_pcie_priv *pcie = hose->dn->data;
  610. struct pex_inbound_window *in;
  611. int i;
  612. /* Walk the Root Complex Inbound windows to match IMMR base */
  613. in = pcie->cfg_type0 + PEX_RC_INWIN_BASE;
  614. for (i = 0; i < 4; i++) {
  615. /* not enabled, skip */
  616. if (!in_le32(&in[i].ar) & PEX_RCIWARn_EN)
  617. continue;
  618. if (get_immrbase() == in_le32(&in[i].tar))
  619. return (u64)in_le32(&in[i].barh) << 32 |
  620. in_le32(&in[i].barl);
  621. }
  622. printk(KERN_WARNING "could not find PCI BAR matching IMMR\n");
  623. }
  624. #endif
  625. #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
  626. if (!is_mpc83xx_pci) {
  627. u32 base;
  628. pci_bus_read_config_dword(hose->bus,
  629. PCI_DEVFN(0, 0), PCI_BASE_ADDRESS_0, &base);
  630. return base;
  631. }
  632. #endif
  633. return 0;
  634. }