fsl_pci.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. if (!of_device_is_available(dev)) {
  276. pr_warning("%s: disabled\n", dev->full_name);
  277. return -ENODEV;
  278. }
  279. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  280. /* Fetch host bridge registers address */
  281. if (of_address_to_resource(dev, 0, &rsrc)) {
  282. printk(KERN_WARNING "Can't get pci register base!");
  283. return -ENOMEM;
  284. }
  285. /* Get bus range if any */
  286. bus_range = of_get_property(dev, "bus-range", &len);
  287. if (bus_range == NULL || len < 2 * sizeof(int))
  288. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  289. " bus 0\n", dev->full_name);
  290. ppc_pci_add_flags(PPC_PCI_REASSIGN_ALL_BUS);
  291. hose = pcibios_alloc_controller(dev);
  292. if (!hose)
  293. return -ENOMEM;
  294. hose->first_busno = bus_range ? bus_range[0] : 0x0;
  295. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  296. setup_indirect_pci(hose, rsrc.start, rsrc.start + 0x4,
  297. PPC_INDIRECT_TYPE_BIG_ENDIAN);
  298. setup_pci_cmd(hose);
  299. /* check PCI express link status */
  300. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  301. hose->indirect_type |= PPC_INDIRECT_TYPE_EXT_REG |
  302. PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS;
  303. if (fsl_pcie_check_link(hose))
  304. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  305. }
  306. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  307. "Firmware bus number: %d->%d\n",
  308. (unsigned long long)rsrc.start, hose->first_busno,
  309. hose->last_busno);
  310. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  311. hose, hose->cfg_addr, hose->cfg_data);
  312. /* Interpret the "ranges" property */
  313. /* This also maps the I/O region and sets isa_io/mem_base */
  314. pci_process_bridge_OF_ranges(hose, dev, is_primary);
  315. /* Setup PEX window registers */
  316. setup_pci_atmu(hose, &rsrc);
  317. return 0;
  318. }
  319. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8548E, quirk_fsl_pcie_header);
  320. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8548, quirk_fsl_pcie_header);
  321. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8543E, quirk_fsl_pcie_header);
  322. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8543, quirk_fsl_pcie_header);
  323. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8547E, quirk_fsl_pcie_header);
  324. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8545E, quirk_fsl_pcie_header);
  325. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8545, quirk_fsl_pcie_header);
  326. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8569E, quirk_fsl_pcie_header);
  327. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8569, quirk_fsl_pcie_header);
  328. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8568E, quirk_fsl_pcie_header);
  329. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8568, quirk_fsl_pcie_header);
  330. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8567E, quirk_fsl_pcie_header);
  331. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8567, quirk_fsl_pcie_header);
  332. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8533E, quirk_fsl_pcie_header);
  333. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8533, quirk_fsl_pcie_header);
  334. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8544E, quirk_fsl_pcie_header);
  335. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8544, quirk_fsl_pcie_header);
  336. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8572E, quirk_fsl_pcie_header);
  337. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8572, quirk_fsl_pcie_header);
  338. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8536E, quirk_fsl_pcie_header);
  339. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8536, quirk_fsl_pcie_header);
  340. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8641, quirk_fsl_pcie_header);
  341. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8641D, quirk_fsl_pcie_header);
  342. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8610, quirk_fsl_pcie_header);
  343. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1011E, quirk_fsl_pcie_header);
  344. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1011, quirk_fsl_pcie_header);
  345. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1013E, quirk_fsl_pcie_header);
  346. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1013, quirk_fsl_pcie_header);
  347. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1020E, quirk_fsl_pcie_header);
  348. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1020, quirk_fsl_pcie_header);
  349. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1021E, quirk_fsl_pcie_header);
  350. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1021, quirk_fsl_pcie_header);
  351. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1022E, quirk_fsl_pcie_header);
  352. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1022, quirk_fsl_pcie_header);
  353. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2010E, quirk_fsl_pcie_header);
  354. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2010, quirk_fsl_pcie_header);
  355. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2020E, quirk_fsl_pcie_header);
  356. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2020, quirk_fsl_pcie_header);
  357. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2040E, quirk_fsl_pcie_header);
  358. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2040, quirk_fsl_pcie_header);
  359. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P3041E, quirk_fsl_pcie_header);
  360. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P3041, quirk_fsl_pcie_header);
  361. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P4040E, quirk_fsl_pcie_header);
  362. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P4040, quirk_fsl_pcie_header);
  363. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P4080E, quirk_fsl_pcie_header);
  364. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P4080, quirk_fsl_pcie_header);
  365. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P5010E, quirk_fsl_pcie_header);
  366. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P5010, quirk_fsl_pcie_header);
  367. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P5020E, quirk_fsl_pcie_header);
  368. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P5020, quirk_fsl_pcie_header);
  369. #endif /* CONFIG_FSL_SOC_BOOKE || CONFIG_PPC_86xx */
  370. #if defined(CONFIG_PPC_83xx) || defined(CONFIG_PPC_MPC512x)
  371. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8308, quirk_fsl_pcie_header);
  372. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8314E, quirk_fsl_pcie_header);
  373. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8314, quirk_fsl_pcie_header);
  374. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8315E, quirk_fsl_pcie_header);
  375. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8315, quirk_fsl_pcie_header);
  376. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8377E, quirk_fsl_pcie_header);
  377. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8377, quirk_fsl_pcie_header);
  378. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8378E, quirk_fsl_pcie_header);
  379. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8378, quirk_fsl_pcie_header);
  380. struct mpc83xx_pcie_priv {
  381. void __iomem *cfg_type0;
  382. void __iomem *cfg_type1;
  383. u32 dev_base;
  384. };
  385. struct pex_inbound_window {
  386. u32 ar;
  387. u32 tar;
  388. u32 barl;
  389. u32 barh;
  390. };
  391. /*
  392. * With the convention of u-boot, the PCIE outbound window 0 serves
  393. * as configuration transactions outbound.
  394. */
  395. #define PEX_OUTWIN0_BAR 0xCA4
  396. #define PEX_OUTWIN0_TAL 0xCA8
  397. #define PEX_OUTWIN0_TAH 0xCAC
  398. #define PEX_RC_INWIN_BASE 0xE60
  399. #define PEX_RCIWARn_EN 0x1
  400. static int mpc83xx_pcie_exclude_device(struct pci_bus *bus, unsigned int devfn)
  401. {
  402. struct pci_controller *hose = pci_bus_to_host(bus);
  403. if (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)
  404. return PCIBIOS_DEVICE_NOT_FOUND;
  405. /*
  406. * Workaround for the HW bug: for Type 0 configure transactions the
  407. * PCI-E controller does not check the device number bits and just
  408. * assumes that the device number bits are 0.
  409. */
  410. if (bus->number == hose->first_busno ||
  411. bus->primary == hose->first_busno) {
  412. if (devfn & 0xf8)
  413. return PCIBIOS_DEVICE_NOT_FOUND;
  414. }
  415. if (ppc_md.pci_exclude_device) {
  416. if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
  417. return PCIBIOS_DEVICE_NOT_FOUND;
  418. }
  419. return PCIBIOS_SUCCESSFUL;
  420. }
  421. static void __iomem *mpc83xx_pcie_remap_cfg(struct pci_bus *bus,
  422. unsigned int devfn, int offset)
  423. {
  424. struct pci_controller *hose = pci_bus_to_host(bus);
  425. struct mpc83xx_pcie_priv *pcie = hose->dn->data;
  426. u32 dev_base = bus->number << 24 | devfn << 16;
  427. int ret;
  428. ret = mpc83xx_pcie_exclude_device(bus, devfn);
  429. if (ret)
  430. return NULL;
  431. offset &= 0xfff;
  432. /* Type 0 */
  433. if (bus->number == hose->first_busno)
  434. return pcie->cfg_type0 + offset;
  435. if (pcie->dev_base == dev_base)
  436. goto mapped;
  437. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, dev_base);
  438. pcie->dev_base = dev_base;
  439. mapped:
  440. return pcie->cfg_type1 + offset;
  441. }
  442. static int mpc83xx_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
  443. int offset, int len, u32 *val)
  444. {
  445. void __iomem *cfg_addr;
  446. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  447. if (!cfg_addr)
  448. return PCIBIOS_DEVICE_NOT_FOUND;
  449. switch (len) {
  450. case 1:
  451. *val = in_8(cfg_addr);
  452. break;
  453. case 2:
  454. *val = in_le16(cfg_addr);
  455. break;
  456. default:
  457. *val = in_le32(cfg_addr);
  458. break;
  459. }
  460. return PCIBIOS_SUCCESSFUL;
  461. }
  462. static int mpc83xx_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
  463. int offset, int len, u32 val)
  464. {
  465. struct pci_controller *hose = pci_bus_to_host(bus);
  466. void __iomem *cfg_addr;
  467. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  468. if (!cfg_addr)
  469. return PCIBIOS_DEVICE_NOT_FOUND;
  470. /* PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS */
  471. if (offset == PCI_PRIMARY_BUS && bus->number == hose->first_busno)
  472. val &= 0xffffff00;
  473. switch (len) {
  474. case 1:
  475. out_8(cfg_addr, val);
  476. break;
  477. case 2:
  478. out_le16(cfg_addr, val);
  479. break;
  480. default:
  481. out_le32(cfg_addr, val);
  482. break;
  483. }
  484. return PCIBIOS_SUCCESSFUL;
  485. }
  486. static struct pci_ops mpc83xx_pcie_ops = {
  487. .read = mpc83xx_pcie_read_config,
  488. .write = mpc83xx_pcie_write_config,
  489. };
  490. static int __init mpc83xx_pcie_setup(struct pci_controller *hose,
  491. struct resource *reg)
  492. {
  493. struct mpc83xx_pcie_priv *pcie;
  494. u32 cfg_bar;
  495. int ret = -ENOMEM;
  496. pcie = zalloc_maybe_bootmem(sizeof(*pcie), GFP_KERNEL);
  497. if (!pcie)
  498. return ret;
  499. pcie->cfg_type0 = ioremap(reg->start, resource_size(reg));
  500. if (!pcie->cfg_type0)
  501. goto err0;
  502. cfg_bar = in_le32(pcie->cfg_type0 + PEX_OUTWIN0_BAR);
  503. if (!cfg_bar) {
  504. /* PCI-E isn't configured. */
  505. ret = -ENODEV;
  506. goto err1;
  507. }
  508. pcie->cfg_type1 = ioremap(cfg_bar, 0x1000);
  509. if (!pcie->cfg_type1)
  510. goto err1;
  511. WARN_ON(hose->dn->data);
  512. hose->dn->data = pcie;
  513. hose->ops = &mpc83xx_pcie_ops;
  514. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAH, 0);
  515. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, 0);
  516. if (fsl_pcie_check_link(hose))
  517. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  518. return 0;
  519. err1:
  520. iounmap(pcie->cfg_type0);
  521. err0:
  522. kfree(pcie);
  523. return ret;
  524. }
  525. int __init mpc83xx_add_bridge(struct device_node *dev)
  526. {
  527. int ret;
  528. int len;
  529. struct pci_controller *hose;
  530. struct resource rsrc_reg;
  531. struct resource rsrc_cfg;
  532. const int *bus_range;
  533. int primary;
  534. is_mpc83xx_pci = 1;
  535. if (!of_device_is_available(dev)) {
  536. pr_warning("%s: disabled by the firmware.\n",
  537. dev->full_name);
  538. return -ENODEV;
  539. }
  540. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  541. /* Fetch host bridge registers address */
  542. if (of_address_to_resource(dev, 0, &rsrc_reg)) {
  543. printk(KERN_WARNING "Can't get pci register base!\n");
  544. return -ENOMEM;
  545. }
  546. memset(&rsrc_cfg, 0, sizeof(rsrc_cfg));
  547. if (of_address_to_resource(dev, 1, &rsrc_cfg)) {
  548. printk(KERN_WARNING
  549. "No pci config register base in dev tree, "
  550. "using default\n");
  551. /*
  552. * MPC83xx supports up to two host controllers
  553. * one at 0x8500 has config space registers at 0x8300
  554. * one at 0x8600 has config space registers at 0x8380
  555. */
  556. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  557. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8300;
  558. else if ((rsrc_reg.start & 0xfffff) == 0x8600)
  559. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8380;
  560. }
  561. /*
  562. * Controller at offset 0x8500 is primary
  563. */
  564. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  565. primary = 1;
  566. else
  567. primary = 0;
  568. /* Get bus range if any */
  569. bus_range = of_get_property(dev, "bus-range", &len);
  570. if (bus_range == NULL || len < 2 * sizeof(int)) {
  571. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  572. " bus 0\n", dev->full_name);
  573. }
  574. ppc_pci_add_flags(PPC_PCI_REASSIGN_ALL_BUS);
  575. hose = pcibios_alloc_controller(dev);
  576. if (!hose)
  577. return -ENOMEM;
  578. hose->first_busno = bus_range ? bus_range[0] : 0;
  579. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  580. if (of_device_is_compatible(dev, "fsl,mpc8314-pcie")) {
  581. ret = mpc83xx_pcie_setup(hose, &rsrc_reg);
  582. if (ret)
  583. goto err0;
  584. } else {
  585. setup_indirect_pci(hose, rsrc_cfg.start,
  586. rsrc_cfg.start + 4, 0);
  587. }
  588. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  589. "Firmware bus number: %d->%d\n",
  590. (unsigned long long)rsrc_reg.start, hose->first_busno,
  591. hose->last_busno);
  592. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  593. hose, hose->cfg_addr, hose->cfg_data);
  594. /* Interpret the "ranges" property */
  595. /* This also maps the I/O region and sets isa_io/mem_base */
  596. pci_process_bridge_OF_ranges(hose, dev, primary);
  597. return 0;
  598. err0:
  599. pcibios_free_controller(hose);
  600. return ret;
  601. }
  602. #endif /* CONFIG_PPC_83xx */
  603. u64 fsl_pci_immrbar_base(struct pci_controller *hose)
  604. {
  605. #ifdef CONFIG_PPC_83xx
  606. if (is_mpc83xx_pci) {
  607. struct mpc83xx_pcie_priv *pcie = hose->dn->data;
  608. struct pex_inbound_window *in;
  609. int i;
  610. /* Walk the Root Complex Inbound windows to match IMMR base */
  611. in = pcie->cfg_type0 + PEX_RC_INWIN_BASE;
  612. for (i = 0; i < 4; i++) {
  613. /* not enabled, skip */
  614. if (!in_le32(&in[i].ar) & PEX_RCIWARn_EN)
  615. continue;
  616. if (get_immrbase() == in_le32(&in[i].tar))
  617. return (u64)in_le32(&in[i].barh) << 32 |
  618. in_le32(&in[i].barl);
  619. }
  620. printk(KERN_WARNING "could not find PCI BAR matching IMMR\n");
  621. }
  622. #endif
  623. #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
  624. if (!is_mpc83xx_pci) {
  625. u32 base;
  626. pci_bus_read_config_dword(hose->bus,
  627. PCI_DEVFN(0, 0), PCI_BASE_ADDRESS_0, &base);
  628. return base;
  629. }
  630. #endif
  631. return 0;
  632. }