fsl_pci.c 11 KB

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