2 * MPC83xx/85xx/86xx PCI/PCIE support routing.
4 * Copyright 2007,2008 Freescale Semiconductor, Inc
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>
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.
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>
25 #include <asm/pci-bridge.h>
26 #include <asm/machdep.h>
27 #include <sysdev/fsl_soc.h>
28 #include <sysdev/fsl_pci.h>
30 #if defined(CONFIG_PPC_85xx) || defined(CONFIG_PPC_86xx)
31 static int __init setup_one_atmu(struct ccsr_pci __iomem *pci,
32 unsigned int index, const struct resource *res,
33 resource_size_t offset)
35 resource_size_t pci_addr = res->start - offset;
36 resource_size_t phys_addr = res->start;
37 resource_size_t size = res->end - res->start + 1;
38 u32 flags = 0x80044000; /* enable & mem R/W */
41 pr_debug("PCI MEM resource start 0x%016llx, size 0x%016llx.\n",
42 (u64)res->start, (u64)size);
44 for (i = 0; size > 0; i++) {
45 unsigned int bits = min(__ilog2(size),
46 __ffs(pci_addr | phys_addr));
51 out_be32(&pci->pow[index + i].potar, pci_addr >> 12);
52 out_be32(&pci->pow[index + i].potear, (u64)pci_addr >> 44);
53 out_be32(&pci->pow[index + i].powbar, phys_addr >> 12);
54 out_be32(&pci->pow[index + i].powar, flags | (bits - 1));
56 pci_addr += (resource_size_t)1U << bits;
57 phys_addr += (resource_size_t)1U << bits;
58 size -= (resource_size_t)1U << bits;
64 /* atmu setup for fsl pci/pcie controller */
65 static void __init setup_pci_atmu(struct pci_controller *hose,
66 struct resource *rsrc)
68 struct ccsr_pci __iomem *pci;
71 pr_debug("PCI memory map start 0x%016llx, size 0x%016llx\n",
72 (u64)rsrc->start, (u64)rsrc->end - (u64)rsrc->start + 1);
73 pci = ioremap(rsrc->start, rsrc->end - rsrc->start + 1);
75 dev_err(hose->parent, "Unable to map ATMU registers\n");
79 /* Disable all windows (except powar0 since it's ignored) */
80 for(i = 1; i < 5; i++)
81 out_be32(&pci->pow[i].powar, 0);
82 for(i = 0; i < 3; i++)
83 out_be32(&pci->piw[i].piwar, 0);
85 /* Setup outbound MEM window */
86 for(i = 0, j = 1; i < 3; i++) {
87 if (!(hose->mem_resources[i].flags & IORESOURCE_MEM))
90 n = setup_one_atmu(pci, j, &hose->mem_resources[i],
91 hose->pci_mem_offset);
93 if (n < 0 || j >= 5) {
94 pr_err("Ran out of outbound PCI ATMUs for resource %d!\n", i);
95 hose->mem_resources[i].flags |= IORESOURCE_DISABLED;
100 /* Setup outbound IO window */
101 if (hose->io_resource.flags & IORESOURCE_IO) {
103 pr_err("Ran out of outbound PCI ATMUs for IO resource\n");
105 pr_debug("PCI IO resource start 0x%016llx, size 0x%016llx, "
106 "phy base 0x%016llx.\n",
107 (u64)hose->io_resource.start,
108 (u64)hose->io_resource.end - (u64)hose->io_resource.start + 1,
109 (u64)hose->io_base_phys);
110 out_be32(&pci->pow[j].potar, (hose->io_resource.start >> 12));
111 out_be32(&pci->pow[j].potear, 0);
112 out_be32(&pci->pow[j].powbar, (hose->io_base_phys >> 12));
114 out_be32(&pci->pow[j].powar, 0x80088000
115 | (__ilog2(hose->io_resource.end
116 - hose->io_resource.start + 1) - 1));
120 /* Setup 2G inbound Memory Window @ 1 */
121 out_be32(&pci->piw[2].pitar, 0x00000000);
122 out_be32(&pci->piw[2].piwbar,0x00000000);
123 out_be32(&pci->piw[2].piwar, PIWAR_2G);
128 static void __init setup_pci_cmd(struct pci_controller *hose)
133 early_read_config_word(hose, 0, 0, PCI_COMMAND, &cmd);
134 cmd |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY
136 early_write_config_word(hose, 0, 0, PCI_COMMAND, cmd);
138 cap_x = early_find_capability(hose, 0, 0, PCI_CAP_ID_PCIX);
140 int pci_x_cmd = cap_x + PCI_X_CMD;
141 cmd = PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ
142 | PCI_X_CMD_ERO | PCI_X_CMD_DPERR_E;
143 early_write_config_word(hose, 0, 0, pci_x_cmd, cmd);
145 early_write_config_byte(hose, 0, 0, PCI_LATENCY_TIMER, 0x80);
149 static void __init setup_pci_pcsrbar(struct pci_controller *hose)
151 #ifdef CONFIG_PCI_MSI
152 phys_addr_t immr_base;
154 immr_base = get_immrbase();
155 early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, immr_base);
159 static int fsl_pcie_bus_fixup;
161 static void __init quirk_fsl_pcie_header(struct pci_dev *dev)
163 /* if we aren't a PCIe don't bother */
164 if (!pci_find_capability(dev, PCI_CAP_ID_EXP))
167 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
168 fsl_pcie_bus_fixup = 1;
172 static int __init fsl_pcie_check_link(struct pci_controller *hose)
175 early_read_config_dword(hose, 0, 0, PCIE_LTSSM, &val);
176 if (val < PCIE_LTSSM_L0)
181 void fsl_pcibios_fixup_bus(struct pci_bus *bus)
183 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
186 if ((bus->parent == hose->bus) &&
187 ((fsl_pcie_bus_fixup &&
188 early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) ||
189 (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)))
191 for (i = 0; i < 4; ++i) {
192 struct resource *res = bus->resource[i];
193 struct resource *par = bus->parent->resource[i];
200 res->start = par->start;
202 res->flags = par->flags;
208 int __init fsl_add_bridge(struct device_node *dev, int is_primary)
211 struct pci_controller *hose;
212 struct resource rsrc;
213 const int *bus_range;
215 pr_debug("Adding PCI host bridge %s\n", dev->full_name);
217 /* Fetch host bridge registers address */
218 if (of_address_to_resource(dev, 0, &rsrc)) {
219 printk(KERN_WARNING "Can't get pci register base!");
223 /* Get bus range if any */
224 bus_range = of_get_property(dev, "bus-range", &len);
225 if (bus_range == NULL || len < 2 * sizeof(int))
226 printk(KERN_WARNING "Can't get bus-range for %s, assume"
227 " bus 0\n", dev->full_name);
229 ppc_pci_add_flags(PPC_PCI_REASSIGN_ALL_BUS);
230 hose = pcibios_alloc_controller(dev);
234 hose->first_busno = bus_range ? bus_range[0] : 0x0;
235 hose->last_busno = bus_range ? bus_range[1] : 0xff;
237 setup_indirect_pci(hose, rsrc.start, rsrc.start + 0x4,
238 PPC_INDIRECT_TYPE_BIG_ENDIAN);
241 /* check PCI express link status */
242 if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
243 hose->indirect_type |= PPC_INDIRECT_TYPE_EXT_REG |
244 PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS;
245 if (fsl_pcie_check_link(hose))
246 hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
249 printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
250 "Firmware bus number: %d->%d\n",
251 (unsigned long long)rsrc.start, hose->first_busno,
254 pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
255 hose, hose->cfg_addr, hose->cfg_data);
257 /* Interpret the "ranges" property */
258 /* This also maps the I/O region and sets isa_io/mem_base */
259 pci_process_bridge_OF_ranges(hose, dev, is_primary);
261 /* Setup PEX window registers */
262 setup_pci_atmu(hose, &rsrc);
264 /* Setup PEXCSRBAR */
265 setup_pci_pcsrbar(hose);
269 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8548E, quirk_fsl_pcie_header);
270 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8548, quirk_fsl_pcie_header);
271 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8543E, quirk_fsl_pcie_header);
272 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8543, quirk_fsl_pcie_header);
273 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8547E, quirk_fsl_pcie_header);
274 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8545E, quirk_fsl_pcie_header);
275 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8545, quirk_fsl_pcie_header);
276 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8568E, quirk_fsl_pcie_header);
277 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8568, quirk_fsl_pcie_header);
278 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8567E, quirk_fsl_pcie_header);
279 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8567, quirk_fsl_pcie_header);
280 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8533E, quirk_fsl_pcie_header);
281 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8533, quirk_fsl_pcie_header);
282 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8544E, quirk_fsl_pcie_header);
283 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8544, quirk_fsl_pcie_header);
284 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8572E, quirk_fsl_pcie_header);
285 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8572, quirk_fsl_pcie_header);
286 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8536E, quirk_fsl_pcie_header);
287 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8536, quirk_fsl_pcie_header);
288 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8641, quirk_fsl_pcie_header);
289 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8641D, quirk_fsl_pcie_header);
290 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8610, quirk_fsl_pcie_header);
291 #endif /* CONFIG_PPC_85xx || CONFIG_PPC_86xx */
293 #if defined(CONFIG_PPC_83xx) || defined(CONFIG_PPC_MPC512x)
294 int __init mpc83xx_add_bridge(struct device_node *dev)
297 struct pci_controller *hose;
298 struct resource rsrc_reg;
299 struct resource rsrc_cfg;
300 const int *bus_range;
303 pr_debug("Adding PCI host bridge %s\n", dev->full_name);
305 /* Fetch host bridge registers address */
306 if (of_address_to_resource(dev, 0, &rsrc_reg)) {
307 printk(KERN_WARNING "Can't get pci register base!\n");
311 memset(&rsrc_cfg, 0, sizeof(rsrc_cfg));
313 if (of_address_to_resource(dev, 1, &rsrc_cfg)) {
315 "No pci config register base in dev tree, "
318 * MPC83xx supports up to two host controllers
319 * one at 0x8500 has config space registers at 0x8300
320 * one at 0x8600 has config space registers at 0x8380
322 if ((rsrc_reg.start & 0xfffff) == 0x8500)
323 rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8300;
324 else if ((rsrc_reg.start & 0xfffff) == 0x8600)
325 rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8380;
328 * Controller at offset 0x8500 is primary
330 if ((rsrc_reg.start & 0xfffff) == 0x8500)
335 /* Get bus range if any */
336 bus_range = of_get_property(dev, "bus-range", &len);
337 if (bus_range == NULL || len < 2 * sizeof(int)) {
338 printk(KERN_WARNING "Can't get bus-range for %s, assume"
339 " bus 0\n", dev->full_name);
342 ppc_pci_add_flags(PPC_PCI_REASSIGN_ALL_BUS);
343 hose = pcibios_alloc_controller(dev);
347 hose->first_busno = bus_range ? bus_range[0] : 0;
348 hose->last_busno = bus_range ? bus_range[1] : 0xff;
350 setup_indirect_pci(hose, rsrc_cfg.start, rsrc_cfg.start + 4, 0);
352 printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
353 "Firmware bus number: %d->%d\n",
354 (unsigned long long)rsrc_reg.start, hose->first_busno,
357 pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
358 hose, hose->cfg_addr, hose->cfg_data);
360 /* Interpret the "ranges" property */
361 /* This also maps the I/O region and sets isa_io/mem_base */
362 pci_process_bridge_OF_ranges(hose, dev, primary);
366 #endif /* CONFIG_PPC_83xx */