fsl_pci.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. * MPC83xx/85xx/86xx PCI/PCIE support routing.
  3. *
  4. * Copyright 2007-2009 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/lmb.h>
  27. #include <linux/log2.h>
  28. #include <asm/io.h>
  29. #include <asm/prom.h>
  30. #include <asm/pci-bridge.h>
  31. #include <asm/machdep.h>
  32. #include <sysdev/fsl_soc.h>
  33. #include <sysdev/fsl_pci.h>
  34. static int fsl_pcie_bus_fixup;
  35. static void __init quirk_fsl_pcie_header(struct pci_dev *dev)
  36. {
  37. /* if we aren't a PCIe don't bother */
  38. if (!pci_find_capability(dev, PCI_CAP_ID_EXP))
  39. return;
  40. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  41. fsl_pcie_bus_fixup = 1;
  42. return;
  43. }
  44. static int __init fsl_pcie_check_link(struct pci_controller *hose)
  45. {
  46. u32 val;
  47. early_read_config_dword(hose, 0, 0, PCIE_LTSSM, &val);
  48. if (val < PCIE_LTSSM_L0)
  49. return 1;
  50. return 0;
  51. }
  52. #if defined(CONFIG_PPC_85xx) || defined(CONFIG_PPC_86xx)
  53. static int __init setup_one_atmu(struct ccsr_pci __iomem *pci,
  54. unsigned int index, const struct resource *res,
  55. resource_size_t offset)
  56. {
  57. resource_size_t pci_addr = res->start - offset;
  58. resource_size_t phys_addr = res->start;
  59. resource_size_t size = res->end - res->start + 1;
  60. u32 flags = 0x80044000; /* enable & mem R/W */
  61. unsigned int i;
  62. pr_debug("PCI MEM resource start 0x%016llx, size 0x%016llx.\n",
  63. (u64)res->start, (u64)size);
  64. if (res->flags & IORESOURCE_PREFETCH)
  65. flags |= 0x10000000; /* enable relaxed ordering */
  66. for (i = 0; size > 0; i++) {
  67. unsigned int bits = min(__ilog2(size),
  68. __ffs(pci_addr | phys_addr));
  69. if (index + i >= 5)
  70. return -1;
  71. out_be32(&pci->pow[index + i].potar, pci_addr >> 12);
  72. out_be32(&pci->pow[index + i].potear, (u64)pci_addr >> 44);
  73. out_be32(&pci->pow[index + i].powbar, phys_addr >> 12);
  74. out_be32(&pci->pow[index + i].powar, flags | (bits - 1));
  75. pci_addr += (resource_size_t)1U << bits;
  76. phys_addr += (resource_size_t)1U << bits;
  77. size -= (resource_size_t)1U << bits;
  78. }
  79. return i;
  80. }
  81. /* atmu setup for fsl pci/pcie controller */
  82. static void __init setup_pci_atmu(struct pci_controller *hose,
  83. struct resource *rsrc)
  84. {
  85. struct ccsr_pci __iomem *pci;
  86. int i, j, n, mem_log, win_idx = 2;
  87. u64 mem, sz, paddr_hi = 0;
  88. u64 paddr_lo = ULLONG_MAX;
  89. u32 pcicsrbar = 0, pcicsrbar_sz;
  90. u32 piwar = PIWAR_EN | PIWAR_PF | PIWAR_TGI_LOCAL |
  91. PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
  92. char *name = hose->dn->full_name;
  93. pr_debug("PCI memory map start 0x%016llx, size 0x%016llx\n",
  94. (u64)rsrc->start, (u64)rsrc->end - (u64)rsrc->start + 1);
  95. pci = ioremap(rsrc->start, rsrc->end - rsrc->start + 1);
  96. if (!pci) {
  97. dev_err(hose->parent, "Unable to map ATMU registers\n");
  98. return;
  99. }
  100. /* Disable all windows (except powar0 since it's ignored) */
  101. for(i = 1; i < 5; i++)
  102. out_be32(&pci->pow[i].powar, 0);
  103. for(i = 0; i < 3; i++)
  104. out_be32(&pci->piw[i].piwar, 0);
  105. /* Setup outbound MEM window */
  106. for(i = 0, j = 1; i < 3; i++) {
  107. if (!(hose->mem_resources[i].flags & IORESOURCE_MEM))
  108. continue;
  109. paddr_lo = min(paddr_lo, (u64)hose->mem_resources[i].start);
  110. paddr_hi = max(paddr_hi, (u64)hose->mem_resources[i].end);
  111. n = setup_one_atmu(pci, j, &hose->mem_resources[i],
  112. hose->pci_mem_offset);
  113. if (n < 0 || j >= 5) {
  114. pr_err("Ran out of outbound PCI ATMUs for resource %d!\n", i);
  115. hose->mem_resources[i].flags |= IORESOURCE_DISABLED;
  116. } else
  117. j += n;
  118. }
  119. /* Setup outbound IO window */
  120. if (hose->io_resource.flags & IORESOURCE_IO) {
  121. if (j >= 5) {
  122. pr_err("Ran out of outbound PCI ATMUs for IO resource\n");
  123. } else {
  124. pr_debug("PCI IO resource start 0x%016llx, size 0x%016llx, "
  125. "phy base 0x%016llx.\n",
  126. (u64)hose->io_resource.start,
  127. (u64)hose->io_resource.end - (u64)hose->io_resource.start + 1,
  128. (u64)hose->io_base_phys);
  129. out_be32(&pci->pow[j].potar, (hose->io_resource.start >> 12));
  130. out_be32(&pci->pow[j].potear, 0);
  131. out_be32(&pci->pow[j].powbar, (hose->io_base_phys >> 12));
  132. /* Enable, IO R/W */
  133. out_be32(&pci->pow[j].powar, 0x80088000
  134. | (__ilog2(hose->io_resource.end
  135. - hose->io_resource.start + 1) - 1));
  136. }
  137. }
  138. /* convert to pci address space */
  139. paddr_hi -= hose->pci_mem_offset;
  140. paddr_lo -= hose->pci_mem_offset;
  141. if (paddr_hi == paddr_lo) {
  142. pr_err("%s: No outbound window space\n", name);
  143. return ;
  144. }
  145. if (paddr_lo == 0) {
  146. pr_err("%s: No space for inbound window\n", name);
  147. return ;
  148. }
  149. /* setup PCSRBAR/PEXCSRBAR */
  150. early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, 0xffffffff);
  151. early_read_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, &pcicsrbar_sz);
  152. pcicsrbar_sz = ~pcicsrbar_sz + 1;
  153. if (paddr_hi < (0x100000000ull - pcicsrbar_sz) ||
  154. (paddr_lo > 0x100000000ull))
  155. pcicsrbar = 0x100000000ull - pcicsrbar_sz;
  156. else
  157. pcicsrbar = (paddr_lo - pcicsrbar_sz) & -pcicsrbar_sz;
  158. early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, pcicsrbar);
  159. paddr_lo = min(paddr_lo, (u64)pcicsrbar);
  160. pr_info("%s: PCICSRBAR @ 0x%x\n", name, pcicsrbar);
  161. /* Setup inbound mem window */
  162. mem = lmb_end_of_DRAM();
  163. sz = min(mem, paddr_lo);
  164. mem_log = __ilog2_u64(sz);
  165. /* PCIe can overmap inbound & outbound since RX & TX are separated */
  166. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  167. /* Size window to exact size if power-of-two or one size up */
  168. if ((1ull << mem_log) != mem) {
  169. if ((1ull << mem_log) > mem)
  170. pr_info("%s: Setting PCI inbound window "
  171. "greater than memory size\n", name);
  172. mem_log++;
  173. }
  174. piwar |= (mem_log - 1);
  175. /* Setup inbound memory window */
  176. out_be32(&pci->piw[win_idx].pitar, 0x00000000);
  177. out_be32(&pci->piw[win_idx].piwbar, 0x00000000);
  178. out_be32(&pci->piw[win_idx].piwar, piwar);
  179. win_idx--;
  180. hose->dma_window_base_cur = 0x00000000;
  181. hose->dma_window_size = (resource_size_t)sz;
  182. } else {
  183. u64 paddr = 0;
  184. /* Setup inbound memory window */
  185. out_be32(&pci->piw[win_idx].pitar, paddr >> 12);
  186. out_be32(&pci->piw[win_idx].piwbar, paddr >> 12);
  187. out_be32(&pci->piw[win_idx].piwar, (piwar | (mem_log - 1)));
  188. win_idx--;
  189. paddr += 1ull << mem_log;
  190. sz -= 1ull << mem_log;
  191. if (sz) {
  192. mem_log = __ilog2_u64(sz);
  193. piwar |= (mem_log - 1);
  194. out_be32(&pci->piw[win_idx].pitar, paddr >> 12);
  195. out_be32(&pci->piw[win_idx].piwbar, paddr >> 12);
  196. out_be32(&pci->piw[win_idx].piwar, piwar);
  197. win_idx--;
  198. paddr += 1ull << mem_log;
  199. }
  200. hose->dma_window_base_cur = 0x00000000;
  201. hose->dma_window_size = (resource_size_t)paddr;
  202. }
  203. if (hose->dma_window_size < mem) {
  204. #ifndef CONFIG_SWIOTLB
  205. pr_err("%s: ERROR: Memory size exceeds PCI ATMU ability to "
  206. "map - enable CONFIG_SWIOTLB to avoid dma errors.\n",
  207. name);
  208. #endif
  209. /* adjusting outbound windows could reclaim space in mem map */
  210. if (paddr_hi < 0xffffffffull)
  211. pr_warning("%s: WARNING: Outbound window cfg leaves "
  212. "gaps in memory map. Adjusting the memory map "
  213. "could reduce unnecessary bounce buffering.\n",
  214. name);
  215. pr_info("%s: DMA window size is 0x%llx\n", name,
  216. (u64)hose->dma_window_size);
  217. }
  218. iounmap(pci);
  219. }
  220. static void __init setup_pci_cmd(struct pci_controller *hose)
  221. {
  222. u16 cmd;
  223. int cap_x;
  224. early_read_config_word(hose, 0, 0, PCI_COMMAND, &cmd);
  225. cmd |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY
  226. | PCI_COMMAND_IO;
  227. early_write_config_word(hose, 0, 0, PCI_COMMAND, cmd);
  228. cap_x = early_find_capability(hose, 0, 0, PCI_CAP_ID_PCIX);
  229. if (cap_x) {
  230. int pci_x_cmd = cap_x + PCI_X_CMD;
  231. cmd = PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ
  232. | PCI_X_CMD_ERO | PCI_X_CMD_DPERR_E;
  233. early_write_config_word(hose, 0, 0, pci_x_cmd, cmd);
  234. } else {
  235. early_write_config_byte(hose, 0, 0, PCI_LATENCY_TIMER, 0x80);
  236. }
  237. }
  238. void fsl_pcibios_fixup_bus(struct pci_bus *bus)
  239. {
  240. struct pci_controller *hose = pci_bus_to_host(bus);
  241. int i;
  242. if ((bus->parent == hose->bus) &&
  243. ((fsl_pcie_bus_fixup &&
  244. early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) ||
  245. (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)))
  246. {
  247. for (i = 0; i < 4; ++i) {
  248. struct resource *res = bus->resource[i];
  249. struct resource *par = bus->parent->resource[i];
  250. if (res) {
  251. res->start = 0;
  252. res->end = 0;
  253. res->flags = 0;
  254. }
  255. if (res && par) {
  256. res->start = par->start;
  257. res->end = par->end;
  258. res->flags = par->flags;
  259. }
  260. }
  261. }
  262. }
  263. int __init fsl_add_bridge(struct device_node *dev, int is_primary)
  264. {
  265. int len;
  266. struct pci_controller *hose;
  267. struct resource rsrc;
  268. const int *bus_range;
  269. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  270. /* Fetch host bridge registers address */
  271. if (of_address_to_resource(dev, 0, &rsrc)) {
  272. printk(KERN_WARNING "Can't get pci register base!");
  273. return -ENOMEM;
  274. }
  275. /* Get bus range if any */
  276. bus_range = of_get_property(dev, "bus-range", &len);
  277. if (bus_range == NULL || len < 2 * sizeof(int))
  278. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  279. " bus 0\n", dev->full_name);
  280. ppc_pci_add_flags(PPC_PCI_REASSIGN_ALL_BUS);
  281. hose = pcibios_alloc_controller(dev);
  282. if (!hose)
  283. return -ENOMEM;
  284. hose->first_busno = bus_range ? bus_range[0] : 0x0;
  285. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  286. setup_indirect_pci(hose, rsrc.start, rsrc.start + 0x4,
  287. PPC_INDIRECT_TYPE_BIG_ENDIAN);
  288. setup_pci_cmd(hose);
  289. /* check PCI express link status */
  290. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  291. hose->indirect_type |= PPC_INDIRECT_TYPE_EXT_REG |
  292. PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS;
  293. if (fsl_pcie_check_link(hose))
  294. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  295. }
  296. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  297. "Firmware bus number: %d->%d\n",
  298. (unsigned long long)rsrc.start, hose->first_busno,
  299. hose->last_busno);
  300. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  301. hose, hose->cfg_addr, hose->cfg_data);
  302. /* Interpret the "ranges" property */
  303. /* This also maps the I/O region and sets isa_io/mem_base */
  304. pci_process_bridge_OF_ranges(hose, dev, is_primary);
  305. /* Setup PEX window registers */
  306. setup_pci_atmu(hose, &rsrc);
  307. return 0;
  308. }
  309. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8548E, quirk_fsl_pcie_header);
  310. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8548, quirk_fsl_pcie_header);
  311. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8543E, quirk_fsl_pcie_header);
  312. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8543, quirk_fsl_pcie_header);
  313. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8547E, quirk_fsl_pcie_header);
  314. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8545E, quirk_fsl_pcie_header);
  315. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8545, quirk_fsl_pcie_header);
  316. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8569E, quirk_fsl_pcie_header);
  317. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8569, quirk_fsl_pcie_header);
  318. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8568E, quirk_fsl_pcie_header);
  319. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8568, quirk_fsl_pcie_header);
  320. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8567E, quirk_fsl_pcie_header);
  321. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8567, quirk_fsl_pcie_header);
  322. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8533E, quirk_fsl_pcie_header);
  323. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8533, quirk_fsl_pcie_header);
  324. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8544E, quirk_fsl_pcie_header);
  325. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8544, quirk_fsl_pcie_header);
  326. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8572E, quirk_fsl_pcie_header);
  327. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8572, quirk_fsl_pcie_header);
  328. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8536E, quirk_fsl_pcie_header);
  329. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8536, quirk_fsl_pcie_header);
  330. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8641, quirk_fsl_pcie_header);
  331. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8641D, quirk_fsl_pcie_header);
  332. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8610, quirk_fsl_pcie_header);
  333. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2020E, quirk_fsl_pcie_header);
  334. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2020, quirk_fsl_pcie_header);
  335. #endif /* CONFIG_PPC_85xx || CONFIG_PPC_86xx */
  336. #if defined(CONFIG_PPC_83xx) || defined(CONFIG_PPC_MPC512x)
  337. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8314E, quirk_fsl_pcie_header);
  338. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8314, quirk_fsl_pcie_header);
  339. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8315E, quirk_fsl_pcie_header);
  340. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8315, quirk_fsl_pcie_header);
  341. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8377E, quirk_fsl_pcie_header);
  342. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8377, quirk_fsl_pcie_header);
  343. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8378E, quirk_fsl_pcie_header);
  344. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8378, quirk_fsl_pcie_header);
  345. struct mpc83xx_pcie_priv {
  346. void __iomem *cfg_type0;
  347. void __iomem *cfg_type1;
  348. u32 dev_base;
  349. };
  350. /*
  351. * With the convention of u-boot, the PCIE outbound window 0 serves
  352. * as configuration transactions outbound.
  353. */
  354. #define PEX_OUTWIN0_BAR 0xCA4
  355. #define PEX_OUTWIN0_TAL 0xCA8
  356. #define PEX_OUTWIN0_TAH 0xCAC
  357. static int mpc83xx_pcie_exclude_device(struct pci_bus *bus, unsigned int devfn)
  358. {
  359. struct pci_controller *hose = pci_bus_to_host(bus);
  360. if (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)
  361. return PCIBIOS_DEVICE_NOT_FOUND;
  362. /*
  363. * Workaround for the HW bug: for Type 0 configure transactions the
  364. * PCI-E controller does not check the device number bits and just
  365. * assumes that the device number bits are 0.
  366. */
  367. if (bus->number == hose->first_busno ||
  368. bus->primary == hose->first_busno) {
  369. if (devfn & 0xf8)
  370. return PCIBIOS_DEVICE_NOT_FOUND;
  371. }
  372. if (ppc_md.pci_exclude_device) {
  373. if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
  374. return PCIBIOS_DEVICE_NOT_FOUND;
  375. }
  376. return PCIBIOS_SUCCESSFUL;
  377. }
  378. static void __iomem *mpc83xx_pcie_remap_cfg(struct pci_bus *bus,
  379. unsigned int devfn, int offset)
  380. {
  381. struct pci_controller *hose = pci_bus_to_host(bus);
  382. struct mpc83xx_pcie_priv *pcie = hose->dn->data;
  383. u8 bus_no = bus->number - hose->first_busno;
  384. u32 dev_base = bus_no << 24 | devfn << 16;
  385. int ret;
  386. ret = mpc83xx_pcie_exclude_device(bus, devfn);
  387. if (ret)
  388. return NULL;
  389. offset &= 0xfff;
  390. /* Type 0 */
  391. if (bus->number == hose->first_busno)
  392. return pcie->cfg_type0 + offset;
  393. if (pcie->dev_base == dev_base)
  394. goto mapped;
  395. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, dev_base);
  396. pcie->dev_base = dev_base;
  397. mapped:
  398. return pcie->cfg_type1 + offset;
  399. }
  400. static int mpc83xx_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
  401. int offset, int len, u32 *val)
  402. {
  403. void __iomem *cfg_addr;
  404. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  405. if (!cfg_addr)
  406. return PCIBIOS_DEVICE_NOT_FOUND;
  407. switch (len) {
  408. case 1:
  409. *val = in_8(cfg_addr);
  410. break;
  411. case 2:
  412. *val = in_le16(cfg_addr);
  413. break;
  414. default:
  415. *val = in_le32(cfg_addr);
  416. break;
  417. }
  418. return PCIBIOS_SUCCESSFUL;
  419. }
  420. static int mpc83xx_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
  421. int offset, int len, u32 val)
  422. {
  423. void __iomem *cfg_addr;
  424. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  425. if (!cfg_addr)
  426. return PCIBIOS_DEVICE_NOT_FOUND;
  427. switch (len) {
  428. case 1:
  429. out_8(cfg_addr, val);
  430. break;
  431. case 2:
  432. out_le16(cfg_addr, val);
  433. break;
  434. default:
  435. out_le32(cfg_addr, val);
  436. break;
  437. }
  438. return PCIBIOS_SUCCESSFUL;
  439. }
  440. static struct pci_ops mpc83xx_pcie_ops = {
  441. .read = mpc83xx_pcie_read_config,
  442. .write = mpc83xx_pcie_write_config,
  443. };
  444. static int __init mpc83xx_pcie_setup(struct pci_controller *hose,
  445. struct resource *reg)
  446. {
  447. struct mpc83xx_pcie_priv *pcie;
  448. u32 cfg_bar;
  449. int ret = -ENOMEM;
  450. pcie = zalloc_maybe_bootmem(sizeof(*pcie), GFP_KERNEL);
  451. if (!pcie)
  452. return ret;
  453. pcie->cfg_type0 = ioremap(reg->start, resource_size(reg));
  454. if (!pcie->cfg_type0)
  455. goto err0;
  456. cfg_bar = in_le32(pcie->cfg_type0 + PEX_OUTWIN0_BAR);
  457. if (!cfg_bar) {
  458. /* PCI-E isn't configured. */
  459. ret = -ENODEV;
  460. goto err1;
  461. }
  462. pcie->cfg_type1 = ioremap(cfg_bar, 0x1000);
  463. if (!pcie->cfg_type1)
  464. goto err1;
  465. WARN_ON(hose->dn->data);
  466. hose->dn->data = pcie;
  467. hose->ops = &mpc83xx_pcie_ops;
  468. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAH, 0);
  469. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, 0);
  470. if (fsl_pcie_check_link(hose))
  471. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  472. return 0;
  473. err1:
  474. iounmap(pcie->cfg_type0);
  475. err0:
  476. kfree(pcie);
  477. return ret;
  478. }
  479. int __init mpc83xx_add_bridge(struct device_node *dev)
  480. {
  481. int ret;
  482. int len;
  483. struct pci_controller *hose;
  484. struct resource rsrc_reg;
  485. struct resource rsrc_cfg;
  486. const int *bus_range;
  487. int primary;
  488. if (!of_device_is_available(dev)) {
  489. pr_warning("%s: disabled by the firmware.\n",
  490. dev->full_name);
  491. return -ENODEV;
  492. }
  493. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  494. /* Fetch host bridge registers address */
  495. if (of_address_to_resource(dev, 0, &rsrc_reg)) {
  496. printk(KERN_WARNING "Can't get pci register base!\n");
  497. return -ENOMEM;
  498. }
  499. memset(&rsrc_cfg, 0, sizeof(rsrc_cfg));
  500. if (of_address_to_resource(dev, 1, &rsrc_cfg)) {
  501. printk(KERN_WARNING
  502. "No pci config register base in dev tree, "
  503. "using default\n");
  504. /*
  505. * MPC83xx supports up to two host controllers
  506. * one at 0x8500 has config space registers at 0x8300
  507. * one at 0x8600 has config space registers at 0x8380
  508. */
  509. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  510. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8300;
  511. else if ((rsrc_reg.start & 0xfffff) == 0x8600)
  512. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8380;
  513. }
  514. /*
  515. * Controller at offset 0x8500 is primary
  516. */
  517. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  518. primary = 1;
  519. else
  520. primary = 0;
  521. /* Get bus range if any */
  522. bus_range = of_get_property(dev, "bus-range", &len);
  523. if (bus_range == NULL || len < 2 * sizeof(int)) {
  524. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  525. " bus 0\n", dev->full_name);
  526. }
  527. ppc_pci_add_flags(PPC_PCI_REASSIGN_ALL_BUS);
  528. hose = pcibios_alloc_controller(dev);
  529. if (!hose)
  530. return -ENOMEM;
  531. hose->first_busno = bus_range ? bus_range[0] : 0;
  532. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  533. if (of_device_is_compatible(dev, "fsl,mpc8314-pcie")) {
  534. ret = mpc83xx_pcie_setup(hose, &rsrc_reg);
  535. if (ret)
  536. goto err0;
  537. } else {
  538. setup_indirect_pci(hose, rsrc_cfg.start,
  539. rsrc_cfg.start + 4, 0);
  540. }
  541. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  542. "Firmware bus number: %d->%d\n",
  543. (unsigned long long)rsrc_reg.start, hose->first_busno,
  544. hose->last_busno);
  545. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  546. hose, hose->cfg_addr, hose->cfg_data);
  547. /* Interpret the "ranges" property */
  548. /* This also maps the I/O region and sets isa_io/mem_base */
  549. pci_process_bridge_OF_ranges(hose, dev, primary);
  550. return 0;
  551. err0:
  552. pcibios_free_controller(hose);
  553. return ret;
  554. }
  555. #endif /* CONFIG_PPC_83xx */