fsl_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * MPC83xx/85xx/86xx PCI/PCIE support routing.
  3. *
  4. * Copyright 2007,2008 Freescale Semiconductor, Inc
  5. *
  6. * Initial author: Xianghua Xiao <x.xiao@freescale.com>
  7. * Recode: ZHANG WEI <wei.zhang@freescale.com>
  8. * Rewrite the routing for Frescale PCI and PCI Express
  9. * Roy Zang <tie-fei.zang@freescale.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/pci.h>
  18. #include <linux/delay.h>
  19. #include <linux/string.h>
  20. #include <linux/init.h>
  21. #include <linux/bootmem.h>
  22. #include <asm/io.h>
  23. #include <asm/prom.h>
  24. #include <asm/pci-bridge.h>
  25. #include <asm/machdep.h>
  26. #include <sysdev/fsl_soc.h>
  27. #include <sysdev/fsl_pci.h>
  28. #if defined(CONFIG_PPC_85xx) || defined(CONFIG_PPC_86xx)
  29. static int __init setup_one_atmu(struct ccsr_pci __iomem *pci,
  30. unsigned int index, const struct resource *res,
  31. resource_size_t offset)
  32. {
  33. resource_size_t pci_addr = res->start - offset;
  34. resource_size_t phys_addr = res->start;
  35. resource_size_t size = res->end - res->start + 1;
  36. u32 flags = 0x80044000; /* enable & mem R/W */
  37. unsigned int i;
  38. pr_debug("PCI MEM resource start 0x%016llx, size 0x%016llx.\n",
  39. (u64)res->start, (u64)size);
  40. if (res->flags & IORESOURCE_PREFETCH)
  41. flags |= 0x10000000; /* enable relaxed ordering */
  42. for (i = 0; size > 0; i++) {
  43. unsigned int bits = min(__ilog2(size),
  44. __ffs(pci_addr | phys_addr));
  45. if (index + i >= 5)
  46. return -1;
  47. out_be32(&pci->pow[index + i].potar, pci_addr >> 12);
  48. out_be32(&pci->pow[index + i].potear, (u64)pci_addr >> 44);
  49. out_be32(&pci->pow[index + i].powbar, phys_addr >> 12);
  50. out_be32(&pci->pow[index + i].powar, flags | (bits - 1));
  51. pci_addr += (resource_size_t)1U << bits;
  52. phys_addr += (resource_size_t)1U << bits;
  53. size -= (resource_size_t)1U << bits;
  54. }
  55. return i;
  56. }
  57. /* atmu setup for fsl pci/pcie controller */
  58. static void __init setup_pci_atmu(struct pci_controller *hose,
  59. struct resource *rsrc)
  60. {
  61. struct ccsr_pci __iomem *pci;
  62. int i, j, n;
  63. pr_debug("PCI memory map start 0x%016llx, size 0x%016llx\n",
  64. (u64)rsrc->start, (u64)rsrc->end - (u64)rsrc->start + 1);
  65. pci = ioremap(rsrc->start, rsrc->end - rsrc->start + 1);
  66. if (!pci) {
  67. dev_err(hose->parent, "Unable to map ATMU registers\n");
  68. return;
  69. }
  70. /* Disable all windows (except powar0 since it's ignored) */
  71. for(i = 1; i < 5; i++)
  72. out_be32(&pci->pow[i].powar, 0);
  73. for(i = 0; i < 3; i++)
  74. out_be32(&pci->piw[i].piwar, 0);
  75. /* Setup outbound MEM window */
  76. for(i = 0, j = 1; i < 3; i++) {
  77. if (!(hose->mem_resources[i].flags & IORESOURCE_MEM))
  78. continue;
  79. n = setup_one_atmu(pci, j, &hose->mem_resources[i],
  80. hose->pci_mem_offset);
  81. if (n < 0 || j >= 5) {
  82. pr_err("Ran out of outbound PCI ATMUs for resource %d!\n", i);
  83. hose->mem_resources[i].flags |= IORESOURCE_DISABLED;
  84. } else
  85. j += n;
  86. }
  87. /* Setup outbound IO window */
  88. if (hose->io_resource.flags & IORESOURCE_IO) {
  89. if (j >= 5) {
  90. pr_err("Ran out of outbound PCI ATMUs for IO resource\n");
  91. } else {
  92. pr_debug("PCI IO resource start 0x%016llx, size 0x%016llx, "
  93. "phy base 0x%016llx.\n",
  94. (u64)hose->io_resource.start,
  95. (u64)hose->io_resource.end - (u64)hose->io_resource.start + 1,
  96. (u64)hose->io_base_phys);
  97. out_be32(&pci->pow[j].potar, (hose->io_resource.start >> 12));
  98. out_be32(&pci->pow[j].potear, 0);
  99. out_be32(&pci->pow[j].powbar, (hose->io_base_phys >> 12));
  100. /* Enable, IO R/W */
  101. out_be32(&pci->pow[j].powar, 0x80088000
  102. | (__ilog2(hose->io_resource.end
  103. - hose->io_resource.start + 1) - 1));
  104. }
  105. }
  106. /* Setup 2G inbound Memory Window @ 1 */
  107. out_be32(&pci->piw[2].pitar, 0x00000000);
  108. out_be32(&pci->piw[2].piwbar,0x00000000);
  109. out_be32(&pci->piw[2].piwar, PIWAR_2G);
  110. iounmap(pci);
  111. }
  112. static void __init setup_pci_cmd(struct pci_controller *hose)
  113. {
  114. u16 cmd;
  115. int cap_x;
  116. early_read_config_word(hose, 0, 0, PCI_COMMAND, &cmd);
  117. cmd |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY
  118. | PCI_COMMAND_IO;
  119. early_write_config_word(hose, 0, 0, PCI_COMMAND, cmd);
  120. cap_x = early_find_capability(hose, 0, 0, PCI_CAP_ID_PCIX);
  121. if (cap_x) {
  122. int pci_x_cmd = cap_x + PCI_X_CMD;
  123. cmd = PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ
  124. | PCI_X_CMD_ERO | PCI_X_CMD_DPERR_E;
  125. early_write_config_word(hose, 0, 0, pci_x_cmd, cmd);
  126. } else {
  127. early_write_config_byte(hose, 0, 0, PCI_LATENCY_TIMER, 0x80);
  128. }
  129. }
  130. static void __init setup_pci_pcsrbar(struct pci_controller *hose)
  131. {
  132. #ifdef CONFIG_PCI_MSI
  133. phys_addr_t immr_base;
  134. immr_base = get_immrbase();
  135. early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, immr_base);
  136. #endif
  137. }
  138. static int fsl_pcie_bus_fixup;
  139. static void __init quirk_fsl_pcie_header(struct pci_dev *dev)
  140. {
  141. /* if we aren't a PCIe don't bother */
  142. if (!pci_find_capability(dev, PCI_CAP_ID_EXP))
  143. return ;
  144. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  145. fsl_pcie_bus_fixup = 1;
  146. return ;
  147. }
  148. static int __init fsl_pcie_check_link(struct pci_controller *hose)
  149. {
  150. u32 val;
  151. early_read_config_dword(hose, 0, 0, PCIE_LTSSM, &val);
  152. if (val < PCIE_LTSSM_L0)
  153. return 1;
  154. return 0;
  155. }
  156. void fsl_pcibios_fixup_bus(struct pci_bus *bus)
  157. {
  158. struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
  159. int i;
  160. if ((bus->parent == hose->bus) &&
  161. ((fsl_pcie_bus_fixup &&
  162. early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) ||
  163. (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)))
  164. {
  165. for (i = 0; i < 4; ++i) {
  166. struct resource *res = bus->resource[i];
  167. struct resource *par = bus->parent->resource[i];
  168. if (res) {
  169. res->start = 0;
  170. res->end = 0;
  171. res->flags = 0;
  172. }
  173. if (res && par) {
  174. res->start = par->start;
  175. res->end = par->end;
  176. res->flags = par->flags;
  177. }
  178. }
  179. }
  180. }
  181. int __init fsl_add_bridge(struct device_node *dev, int is_primary)
  182. {
  183. int len;
  184. struct pci_controller *hose;
  185. struct resource rsrc;
  186. const int *bus_range;
  187. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  188. /* Fetch host bridge registers address */
  189. if (of_address_to_resource(dev, 0, &rsrc)) {
  190. printk(KERN_WARNING "Can't get pci register base!");
  191. return -ENOMEM;
  192. }
  193. /* Get bus range if any */
  194. bus_range = of_get_property(dev, "bus-range", &len);
  195. if (bus_range == NULL || len < 2 * sizeof(int))
  196. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  197. " bus 0\n", dev->full_name);
  198. ppc_pci_add_flags(PPC_PCI_REASSIGN_ALL_BUS);
  199. hose = pcibios_alloc_controller(dev);
  200. if (!hose)
  201. return -ENOMEM;
  202. hose->first_busno = bus_range ? bus_range[0] : 0x0;
  203. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  204. setup_indirect_pci(hose, rsrc.start, rsrc.start + 0x4,
  205. PPC_INDIRECT_TYPE_BIG_ENDIAN);
  206. setup_pci_cmd(hose);
  207. /* check PCI express link status */
  208. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  209. hose->indirect_type |= PPC_INDIRECT_TYPE_EXT_REG |
  210. PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS;
  211. if (fsl_pcie_check_link(hose))
  212. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  213. }
  214. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  215. "Firmware bus number: %d->%d\n",
  216. (unsigned long long)rsrc.start, hose->first_busno,
  217. hose->last_busno);
  218. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  219. hose, hose->cfg_addr, hose->cfg_data);
  220. /* Interpret the "ranges" property */
  221. /* This also maps the I/O region and sets isa_io/mem_base */
  222. pci_process_bridge_OF_ranges(hose, dev, is_primary);
  223. /* Setup PEX window registers */
  224. setup_pci_atmu(hose, &rsrc);
  225. /* Setup PEXCSRBAR */
  226. setup_pci_pcsrbar(hose);
  227. return 0;
  228. }
  229. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8548E, quirk_fsl_pcie_header);
  230. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8548, quirk_fsl_pcie_header);
  231. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8543E, quirk_fsl_pcie_header);
  232. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8543, quirk_fsl_pcie_header);
  233. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8547E, quirk_fsl_pcie_header);
  234. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8545E, quirk_fsl_pcie_header);
  235. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8545, quirk_fsl_pcie_header);
  236. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8568E, quirk_fsl_pcie_header);
  237. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8568, quirk_fsl_pcie_header);
  238. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8567E, quirk_fsl_pcie_header);
  239. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8567, quirk_fsl_pcie_header);
  240. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8533E, quirk_fsl_pcie_header);
  241. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8533, quirk_fsl_pcie_header);
  242. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8544E, quirk_fsl_pcie_header);
  243. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8544, quirk_fsl_pcie_header);
  244. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8572E, quirk_fsl_pcie_header);
  245. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8572, quirk_fsl_pcie_header);
  246. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8536E, quirk_fsl_pcie_header);
  247. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8536, quirk_fsl_pcie_header);
  248. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8641, quirk_fsl_pcie_header);
  249. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8641D, quirk_fsl_pcie_header);
  250. DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8610, quirk_fsl_pcie_header);
  251. #endif /* CONFIG_PPC_85xx || CONFIG_PPC_86xx */
  252. #if defined(CONFIG_PPC_83xx) || defined(CONFIG_PPC_MPC512x)
  253. int __init mpc83xx_add_bridge(struct device_node *dev)
  254. {
  255. int len;
  256. struct pci_controller *hose;
  257. struct resource rsrc_reg;
  258. struct resource rsrc_cfg;
  259. const int *bus_range;
  260. int primary;
  261. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  262. /* Fetch host bridge registers address */
  263. if (of_address_to_resource(dev, 0, &rsrc_reg)) {
  264. printk(KERN_WARNING "Can't get pci register base!\n");
  265. return -ENOMEM;
  266. }
  267. memset(&rsrc_cfg, 0, sizeof(rsrc_cfg));
  268. if (of_address_to_resource(dev, 1, &rsrc_cfg)) {
  269. printk(KERN_WARNING
  270. "No pci config register base in dev tree, "
  271. "using default\n");
  272. /*
  273. * MPC83xx supports up to two host controllers
  274. * one at 0x8500 has config space registers at 0x8300
  275. * one at 0x8600 has config space registers at 0x8380
  276. */
  277. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  278. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8300;
  279. else if ((rsrc_reg.start & 0xfffff) == 0x8600)
  280. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8380;
  281. }
  282. /*
  283. * Controller at offset 0x8500 is primary
  284. */
  285. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  286. primary = 1;
  287. else
  288. primary = 0;
  289. /* Get bus range if any */
  290. bus_range = of_get_property(dev, "bus-range", &len);
  291. if (bus_range == NULL || len < 2 * sizeof(int)) {
  292. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  293. " bus 0\n", dev->full_name);
  294. }
  295. ppc_pci_add_flags(PPC_PCI_REASSIGN_ALL_BUS);
  296. hose = pcibios_alloc_controller(dev);
  297. if (!hose)
  298. return -ENOMEM;
  299. hose->first_busno = bus_range ? bus_range[0] : 0;
  300. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  301. setup_indirect_pci(hose, rsrc_cfg.start, rsrc_cfg.start + 4, 0);
  302. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  303. "Firmware bus number: %d->%d\n",
  304. (unsigned long long)rsrc_reg.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, primary);
  311. return 0;
  312. }
  313. #endif /* CONFIG_PPC_83xx */