fsl_pci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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/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;
  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_P1022E, quirk_fsl_pcie_header);
  341. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1022, quirk_fsl_pcie_header);
  342. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2010E, quirk_fsl_pcie_header);
  343. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2010, quirk_fsl_pcie_header);
  344. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2020E, quirk_fsl_pcie_header);
  345. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2020, quirk_fsl_pcie_header);
  346. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P4040E, quirk_fsl_pcie_header);
  347. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P4040, quirk_fsl_pcie_header);
  348. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P4080E, quirk_fsl_pcie_header);
  349. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P4080, quirk_fsl_pcie_header);
  350. #endif /* CONFIG_FSL_SOC_BOOKE || CONFIG_PPC_86xx */
  351. #if defined(CONFIG_PPC_83xx) || defined(CONFIG_PPC_MPC512x)
  352. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8314E, quirk_fsl_pcie_header);
  353. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8314, quirk_fsl_pcie_header);
  354. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8315E, quirk_fsl_pcie_header);
  355. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8315, quirk_fsl_pcie_header);
  356. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8377E, quirk_fsl_pcie_header);
  357. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8377, quirk_fsl_pcie_header);
  358. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8378E, quirk_fsl_pcie_header);
  359. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8378, quirk_fsl_pcie_header);
  360. struct mpc83xx_pcie_priv {
  361. void __iomem *cfg_type0;
  362. void __iomem *cfg_type1;
  363. u32 dev_base;
  364. };
  365. /*
  366. * With the convention of u-boot, the PCIE outbound window 0 serves
  367. * as configuration transactions outbound.
  368. */
  369. #define PEX_OUTWIN0_BAR 0xCA4
  370. #define PEX_OUTWIN0_TAL 0xCA8
  371. #define PEX_OUTWIN0_TAH 0xCAC
  372. static int mpc83xx_pcie_exclude_device(struct pci_bus *bus, unsigned int devfn)
  373. {
  374. struct pci_controller *hose = pci_bus_to_host(bus);
  375. if (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)
  376. return PCIBIOS_DEVICE_NOT_FOUND;
  377. /*
  378. * Workaround for the HW bug: for Type 0 configure transactions the
  379. * PCI-E controller does not check the device number bits and just
  380. * assumes that the device number bits are 0.
  381. */
  382. if (bus->number == hose->first_busno ||
  383. bus->primary == hose->first_busno) {
  384. if (devfn & 0xf8)
  385. return PCIBIOS_DEVICE_NOT_FOUND;
  386. }
  387. if (ppc_md.pci_exclude_device) {
  388. if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
  389. return PCIBIOS_DEVICE_NOT_FOUND;
  390. }
  391. return PCIBIOS_SUCCESSFUL;
  392. }
  393. static void __iomem *mpc83xx_pcie_remap_cfg(struct pci_bus *bus,
  394. unsigned int devfn, int offset)
  395. {
  396. struct pci_controller *hose = pci_bus_to_host(bus);
  397. struct mpc83xx_pcie_priv *pcie = hose->dn->data;
  398. u32 dev_base = bus->number << 24 | devfn << 16;
  399. int ret;
  400. ret = mpc83xx_pcie_exclude_device(bus, devfn);
  401. if (ret)
  402. return NULL;
  403. offset &= 0xfff;
  404. /* Type 0 */
  405. if (bus->number == hose->first_busno)
  406. return pcie->cfg_type0 + offset;
  407. if (pcie->dev_base == dev_base)
  408. goto mapped;
  409. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, dev_base);
  410. pcie->dev_base = dev_base;
  411. mapped:
  412. return pcie->cfg_type1 + offset;
  413. }
  414. static int mpc83xx_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
  415. int offset, int len, u32 *val)
  416. {
  417. void __iomem *cfg_addr;
  418. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  419. if (!cfg_addr)
  420. return PCIBIOS_DEVICE_NOT_FOUND;
  421. switch (len) {
  422. case 1:
  423. *val = in_8(cfg_addr);
  424. break;
  425. case 2:
  426. *val = in_le16(cfg_addr);
  427. break;
  428. default:
  429. *val = in_le32(cfg_addr);
  430. break;
  431. }
  432. return PCIBIOS_SUCCESSFUL;
  433. }
  434. static int mpc83xx_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
  435. int offset, int len, u32 val)
  436. {
  437. struct pci_controller *hose = pci_bus_to_host(bus);
  438. void __iomem *cfg_addr;
  439. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  440. if (!cfg_addr)
  441. return PCIBIOS_DEVICE_NOT_FOUND;
  442. /* PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS */
  443. if (offset == PCI_PRIMARY_BUS && bus->number == hose->first_busno)
  444. val &= 0xffffff00;
  445. switch (len) {
  446. case 1:
  447. out_8(cfg_addr, val);
  448. break;
  449. case 2:
  450. out_le16(cfg_addr, val);
  451. break;
  452. default:
  453. out_le32(cfg_addr, val);
  454. break;
  455. }
  456. return PCIBIOS_SUCCESSFUL;
  457. }
  458. static struct pci_ops mpc83xx_pcie_ops = {
  459. .read = mpc83xx_pcie_read_config,
  460. .write = mpc83xx_pcie_write_config,
  461. };
  462. static int __init mpc83xx_pcie_setup(struct pci_controller *hose,
  463. struct resource *reg)
  464. {
  465. struct mpc83xx_pcie_priv *pcie;
  466. u32 cfg_bar;
  467. int ret = -ENOMEM;
  468. pcie = zalloc_maybe_bootmem(sizeof(*pcie), GFP_KERNEL);
  469. if (!pcie)
  470. return ret;
  471. pcie->cfg_type0 = ioremap(reg->start, resource_size(reg));
  472. if (!pcie->cfg_type0)
  473. goto err0;
  474. cfg_bar = in_le32(pcie->cfg_type0 + PEX_OUTWIN0_BAR);
  475. if (!cfg_bar) {
  476. /* PCI-E isn't configured. */
  477. ret = -ENODEV;
  478. goto err1;
  479. }
  480. pcie->cfg_type1 = ioremap(cfg_bar, 0x1000);
  481. if (!pcie->cfg_type1)
  482. goto err1;
  483. WARN_ON(hose->dn->data);
  484. hose->dn->data = pcie;
  485. hose->ops = &mpc83xx_pcie_ops;
  486. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAH, 0);
  487. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, 0);
  488. if (fsl_pcie_check_link(hose))
  489. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  490. return 0;
  491. err1:
  492. iounmap(pcie->cfg_type0);
  493. err0:
  494. kfree(pcie);
  495. return ret;
  496. }
  497. int __init mpc83xx_add_bridge(struct device_node *dev)
  498. {
  499. int ret;
  500. int len;
  501. struct pci_controller *hose;
  502. struct resource rsrc_reg;
  503. struct resource rsrc_cfg;
  504. const int *bus_range;
  505. int primary;
  506. if (!of_device_is_available(dev)) {
  507. pr_warning("%s: disabled by the firmware.\n",
  508. dev->full_name);
  509. return -ENODEV;
  510. }
  511. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  512. /* Fetch host bridge registers address */
  513. if (of_address_to_resource(dev, 0, &rsrc_reg)) {
  514. printk(KERN_WARNING "Can't get pci register base!\n");
  515. return -ENOMEM;
  516. }
  517. memset(&rsrc_cfg, 0, sizeof(rsrc_cfg));
  518. if (of_address_to_resource(dev, 1, &rsrc_cfg)) {
  519. printk(KERN_WARNING
  520. "No pci config register base in dev tree, "
  521. "using default\n");
  522. /*
  523. * MPC83xx supports up to two host controllers
  524. * one at 0x8500 has config space registers at 0x8300
  525. * one at 0x8600 has config space registers at 0x8380
  526. */
  527. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  528. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8300;
  529. else if ((rsrc_reg.start & 0xfffff) == 0x8600)
  530. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8380;
  531. }
  532. /*
  533. * Controller at offset 0x8500 is primary
  534. */
  535. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  536. primary = 1;
  537. else
  538. primary = 0;
  539. /* Get bus range if any */
  540. bus_range = of_get_property(dev, "bus-range", &len);
  541. if (bus_range == NULL || len < 2 * sizeof(int)) {
  542. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  543. " bus 0\n", dev->full_name);
  544. }
  545. ppc_pci_add_flags(PPC_PCI_REASSIGN_ALL_BUS);
  546. hose = pcibios_alloc_controller(dev);
  547. if (!hose)
  548. return -ENOMEM;
  549. hose->first_busno = bus_range ? bus_range[0] : 0;
  550. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  551. if (of_device_is_compatible(dev, "fsl,mpc8314-pcie")) {
  552. ret = mpc83xx_pcie_setup(hose, &rsrc_reg);
  553. if (ret)
  554. goto err0;
  555. } else {
  556. setup_indirect_pci(hose, rsrc_cfg.start,
  557. rsrc_cfg.start + 4, 0);
  558. }
  559. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  560. "Firmware bus number: %d->%d\n",
  561. (unsigned long long)rsrc_reg.start, hose->first_busno,
  562. hose->last_busno);
  563. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  564. hose, hose->cfg_addr, hose->cfg_data);
  565. /* Interpret the "ranges" property */
  566. /* This also maps the I/O region and sets isa_io/mem_base */
  567. pci_process_bridge_OF_ranges(hose, dev, primary);
  568. return 0;
  569. err0:
  570. pcibios_free_controller(hose);
  571. return ret;
  572. }
  573. #endif /* CONFIG_PPC_83xx */