fsl_pci.c 22 KB

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