fsl_pci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. * MPC83xx/85xx/86xx PCI/PCIE support routing.
  3. *
  4. * Copyright 2007-2009 Freescale Semiconductor, Inc.
  5. * Copyright 2008-2009 MontaVista Software, Inc.
  6. *
  7. * Initial author: Xianghua Xiao <x.xiao@freescale.com>
  8. * Recode: ZHANG WEI <wei.zhang@freescale.com>
  9. * Rewrite the routing for Frescale PCI and PCI Express
  10. * Roy Zang <tie-fei.zang@freescale.com>
  11. * MPC83xx PCI-Express support:
  12. * Tony Li <tony.li@freescale.com>
  13. * Anton Vorontsov <avorontsov@ru.mvista.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the
  17. * Free Software Foundation; either version 2 of the License, or (at your
  18. * option) any later version.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/pci.h>
  22. #include <linux/delay.h>
  23. #include <linux/string.h>
  24. #include <linux/init.h>
  25. #include <linux/bootmem.h>
  26. #include <linux/lmb.h>
  27. #include <linux/log2.h>
  28. #include <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 = lmb_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_MPC8308, quirk_fsl_pcie_header);
  353. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8314E, quirk_fsl_pcie_header);
  354. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8314, quirk_fsl_pcie_header);
  355. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8315E, quirk_fsl_pcie_header);
  356. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8315, quirk_fsl_pcie_header);
  357. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8377E, quirk_fsl_pcie_header);
  358. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8377, quirk_fsl_pcie_header);
  359. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8378E, quirk_fsl_pcie_header);
  360. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8378, quirk_fsl_pcie_header);
  361. struct mpc83xx_pcie_priv {
  362. void __iomem *cfg_type0;
  363. void __iomem *cfg_type1;
  364. u32 dev_base;
  365. };
  366. /*
  367. * With the convention of u-boot, the PCIE outbound window 0 serves
  368. * as configuration transactions outbound.
  369. */
  370. #define PEX_OUTWIN0_BAR 0xCA4
  371. #define PEX_OUTWIN0_TAL 0xCA8
  372. #define PEX_OUTWIN0_TAH 0xCAC
  373. static int mpc83xx_pcie_exclude_device(struct pci_bus *bus, unsigned int devfn)
  374. {
  375. struct pci_controller *hose = pci_bus_to_host(bus);
  376. if (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)
  377. return PCIBIOS_DEVICE_NOT_FOUND;
  378. /*
  379. * Workaround for the HW bug: for Type 0 configure transactions the
  380. * PCI-E controller does not check the device number bits and just
  381. * assumes that the device number bits are 0.
  382. */
  383. if (bus->number == hose->first_busno ||
  384. bus->primary == hose->first_busno) {
  385. if (devfn & 0xf8)
  386. return PCIBIOS_DEVICE_NOT_FOUND;
  387. }
  388. if (ppc_md.pci_exclude_device) {
  389. if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
  390. return PCIBIOS_DEVICE_NOT_FOUND;
  391. }
  392. return PCIBIOS_SUCCESSFUL;
  393. }
  394. static void __iomem *mpc83xx_pcie_remap_cfg(struct pci_bus *bus,
  395. unsigned int devfn, int offset)
  396. {
  397. struct pci_controller *hose = pci_bus_to_host(bus);
  398. struct mpc83xx_pcie_priv *pcie = hose->dn->data;
  399. u32 dev_base = bus->number << 24 | devfn << 16;
  400. int ret;
  401. ret = mpc83xx_pcie_exclude_device(bus, devfn);
  402. if (ret)
  403. return NULL;
  404. offset &= 0xfff;
  405. /* Type 0 */
  406. if (bus->number == hose->first_busno)
  407. return pcie->cfg_type0 + offset;
  408. if (pcie->dev_base == dev_base)
  409. goto mapped;
  410. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, dev_base);
  411. pcie->dev_base = dev_base;
  412. mapped:
  413. return pcie->cfg_type1 + offset;
  414. }
  415. static int mpc83xx_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
  416. int offset, int len, u32 *val)
  417. {
  418. void __iomem *cfg_addr;
  419. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  420. if (!cfg_addr)
  421. return PCIBIOS_DEVICE_NOT_FOUND;
  422. switch (len) {
  423. case 1:
  424. *val = in_8(cfg_addr);
  425. break;
  426. case 2:
  427. *val = in_le16(cfg_addr);
  428. break;
  429. default:
  430. *val = in_le32(cfg_addr);
  431. break;
  432. }
  433. return PCIBIOS_SUCCESSFUL;
  434. }
  435. static int mpc83xx_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
  436. int offset, int len, u32 val)
  437. {
  438. struct pci_controller *hose = pci_bus_to_host(bus);
  439. void __iomem *cfg_addr;
  440. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  441. if (!cfg_addr)
  442. return PCIBIOS_DEVICE_NOT_FOUND;
  443. /* PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS */
  444. if (offset == PCI_PRIMARY_BUS && bus->number == hose->first_busno)
  445. val &= 0xffffff00;
  446. switch (len) {
  447. case 1:
  448. out_8(cfg_addr, val);
  449. break;
  450. case 2:
  451. out_le16(cfg_addr, val);
  452. break;
  453. default:
  454. out_le32(cfg_addr, val);
  455. break;
  456. }
  457. return PCIBIOS_SUCCESSFUL;
  458. }
  459. static struct pci_ops mpc83xx_pcie_ops = {
  460. .read = mpc83xx_pcie_read_config,
  461. .write = mpc83xx_pcie_write_config,
  462. };
  463. static int __init mpc83xx_pcie_setup(struct pci_controller *hose,
  464. struct resource *reg)
  465. {
  466. struct mpc83xx_pcie_priv *pcie;
  467. u32 cfg_bar;
  468. int ret = -ENOMEM;
  469. pcie = zalloc_maybe_bootmem(sizeof(*pcie), GFP_KERNEL);
  470. if (!pcie)
  471. return ret;
  472. pcie->cfg_type0 = ioremap(reg->start, resource_size(reg));
  473. if (!pcie->cfg_type0)
  474. goto err0;
  475. cfg_bar = in_le32(pcie->cfg_type0 + PEX_OUTWIN0_BAR);
  476. if (!cfg_bar) {
  477. /* PCI-E isn't configured. */
  478. ret = -ENODEV;
  479. goto err1;
  480. }
  481. pcie->cfg_type1 = ioremap(cfg_bar, 0x1000);
  482. if (!pcie->cfg_type1)
  483. goto err1;
  484. WARN_ON(hose->dn->data);
  485. hose->dn->data = pcie;
  486. hose->ops = &mpc83xx_pcie_ops;
  487. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAH, 0);
  488. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, 0);
  489. if (fsl_pcie_check_link(hose))
  490. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  491. return 0;
  492. err1:
  493. iounmap(pcie->cfg_type0);
  494. err0:
  495. kfree(pcie);
  496. return ret;
  497. }
  498. int __init mpc83xx_add_bridge(struct device_node *dev)
  499. {
  500. int ret;
  501. int len;
  502. struct pci_controller *hose;
  503. struct resource rsrc_reg;
  504. struct resource rsrc_cfg;
  505. const int *bus_range;
  506. int primary;
  507. if (!of_device_is_available(dev)) {
  508. pr_warning("%s: disabled by the firmware.\n",
  509. dev->full_name);
  510. return -ENODEV;
  511. }
  512. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  513. /* Fetch host bridge registers address */
  514. if (of_address_to_resource(dev, 0, &rsrc_reg)) {
  515. printk(KERN_WARNING "Can't get pci register base!\n");
  516. return -ENOMEM;
  517. }
  518. memset(&rsrc_cfg, 0, sizeof(rsrc_cfg));
  519. if (of_address_to_resource(dev, 1, &rsrc_cfg)) {
  520. printk(KERN_WARNING
  521. "No pci config register base in dev tree, "
  522. "using default\n");
  523. /*
  524. * MPC83xx supports up to two host controllers
  525. * one at 0x8500 has config space registers at 0x8300
  526. * one at 0x8600 has config space registers at 0x8380
  527. */
  528. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  529. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8300;
  530. else if ((rsrc_reg.start & 0xfffff) == 0x8600)
  531. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8380;
  532. }
  533. /*
  534. * Controller at offset 0x8500 is primary
  535. */
  536. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  537. primary = 1;
  538. else
  539. primary = 0;
  540. /* Get bus range if any */
  541. bus_range = of_get_property(dev, "bus-range", &len);
  542. if (bus_range == NULL || len < 2 * sizeof(int)) {
  543. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  544. " bus 0\n", dev->full_name);
  545. }
  546. ppc_pci_add_flags(PPC_PCI_REASSIGN_ALL_BUS);
  547. hose = pcibios_alloc_controller(dev);
  548. if (!hose)
  549. return -ENOMEM;
  550. hose->first_busno = bus_range ? bus_range[0] : 0;
  551. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  552. if (of_device_is_compatible(dev, "fsl,mpc8314-pcie")) {
  553. ret = mpc83xx_pcie_setup(hose, &rsrc_reg);
  554. if (ret)
  555. goto err0;
  556. } else {
  557. setup_indirect_pci(hose, rsrc_cfg.start,
  558. rsrc_cfg.start + 4, 0);
  559. }
  560. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  561. "Firmware bus number: %d->%d\n",
  562. (unsigned long long)rsrc_reg.start, hose->first_busno,
  563. hose->last_busno);
  564. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  565. hose, hose->cfg_addr, hose->cfg_data);
  566. /* Interpret the "ranges" property */
  567. /* This also maps the I/O region and sets isa_io/mem_base */
  568. pci_process_bridge_OF_ranges(hose, dev, primary);
  569. return 0;
  570. err0:
  571. pcibios_free_controller(hose);
  572. return ret;
  573. }
  574. #endif /* CONFIG_PPC_83xx */