fsl_pci.c 21 KB

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