fsl_pci.c 11 KB

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