fsl_pci.c 22 KB

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