2 * PCI code for the Freescale MPC52xx embedded CPU.
4 * Copyright (C) 2006 Secret Lab Technologies Ltd.
5 * Grant Likely <grant.likely@secretlab.ca>
6 * Copyright (C) 2004 Sylvain Munaut <tnt@246tNt.com>
8 * This file is licensed under the terms of the GNU General Public License
9 * version 2. This program is licensed "as is" without any warranty of any
10 * kind, whether express or implied.
16 #include <asm/mpc52xx.h>
17 #include <asm/delay.h>
18 #include <asm/machdep.h>
19 #include <linux/kernel.h>
22 /* ======================================================================== */
23 /* PCI windows config */
24 /* ======================================================================== */
26 #define MPC52xx_PCI_TARGET_IO 0xf0000000
27 #define MPC52xx_PCI_TARGET_MEM 0x00000000
30 /* ======================================================================== */
31 /* Structures mapping & Defines for PCI Unit */
32 /* ======================================================================== */
34 #define MPC52xx_PCI_GSCR_BM 0x40000000
35 #define MPC52xx_PCI_GSCR_PE 0x20000000
36 #define MPC52xx_PCI_GSCR_SE 0x10000000
37 #define MPC52xx_PCI_GSCR_XLB2PCI_MASK 0x07000000
38 #define MPC52xx_PCI_GSCR_XLB2PCI_SHIFT 24
39 #define MPC52xx_PCI_GSCR_IPG2PCI_MASK 0x00070000
40 #define MPC52xx_PCI_GSCR_IPG2PCI_SHIFT 16
41 #define MPC52xx_PCI_GSCR_BME 0x00004000
42 #define MPC52xx_PCI_GSCR_PEE 0x00002000
43 #define MPC52xx_PCI_GSCR_SEE 0x00001000
44 #define MPC52xx_PCI_GSCR_PR 0x00000001
47 #define MPC52xx_PCI_IWBTAR_TRANSLATION(proc_ad,pci_ad,size) \
48 ( ( (proc_ad) & 0xff000000 ) | \
49 ( (((size) - 1) >> 8) & 0x00ff0000 ) | \
50 ( ((pci_ad) >> 16) & 0x0000ff00 ) )
52 #define MPC52xx_PCI_IWCR_PACK(win0,win1,win2) (((win0) << 24) | \
56 #define MPC52xx_PCI_IWCR_DISABLE 0x0
57 #define MPC52xx_PCI_IWCR_ENABLE 0x1
58 #define MPC52xx_PCI_IWCR_READ 0x0
59 #define MPC52xx_PCI_IWCR_READ_LINE 0x2
60 #define MPC52xx_PCI_IWCR_READ_MULTI 0x4
61 #define MPC52xx_PCI_IWCR_MEM 0x0
62 #define MPC52xx_PCI_IWCR_IO 0x8
64 #define MPC52xx_PCI_TCR_P 0x01000000
65 #define MPC52xx_PCI_TCR_LD 0x00010000
66 #define MPC52xx_PCI_TCR_WCT8 0x00000008
68 #define MPC52xx_PCI_TBATR_DISABLE 0x0
69 #define MPC52xx_PCI_TBATR_ENABLE 0x1
72 u32 idr; /* PCI + 0x00 */
73 u32 scr; /* PCI + 0x04 */
74 u32 ccrir; /* PCI + 0x08 */
75 u32 cr1; /* PCI + 0x0C */
76 u32 bar0; /* PCI + 0x10 */
77 u32 bar1; /* PCI + 0x14 */
78 u8 reserved1[16]; /* PCI + 0x18 */
79 u32 ccpr; /* PCI + 0x28 */
80 u32 sid; /* PCI + 0x2C */
81 u32 erbar; /* PCI + 0x30 */
82 u32 cpr; /* PCI + 0x34 */
83 u8 reserved2[4]; /* PCI + 0x38 */
84 u32 cr2; /* PCI + 0x3C */
85 u8 reserved3[32]; /* PCI + 0x40 */
86 u32 gscr; /* PCI + 0x60 */
87 u32 tbatr0; /* PCI + 0x64 */
88 u32 tbatr1; /* PCI + 0x68 */
89 u32 tcr; /* PCI + 0x6C */
90 u32 iw0btar; /* PCI + 0x70 */
91 u32 iw1btar; /* PCI + 0x74 */
92 u32 iw2btar; /* PCI + 0x78 */
93 u8 reserved4[4]; /* PCI + 0x7C */
94 u32 iwcr; /* PCI + 0x80 */
95 u32 icr; /* PCI + 0x84 */
96 u32 isr; /* PCI + 0x88 */
97 u32 arb; /* PCI + 0x8C */
98 u8 reserved5[104]; /* PCI + 0x90 */
99 u32 car; /* PCI + 0xF8 */
100 u8 reserved6[4]; /* PCI + 0xFC */
103 /* MPC5200 device tree match tables */
104 const struct of_device_id mpc52xx_pci_ids[] __initdata = {
105 { .type = "pci", .compatible = "fsl,mpc5200-pci", },
106 { .type = "pci", .compatible = "mpc5200-pci", },
110 /* ======================================================================== */
111 /* PCI configuration acess */
112 /* ======================================================================== */
115 mpc52xx_pci_read_config(struct pci_bus *bus, unsigned int devfn,
116 int offset, int len, u32 *val)
118 struct pci_controller *hose = bus->sysdata;
121 if (ppc_md.pci_exclude_device)
122 if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
123 return PCIBIOS_DEVICE_NOT_FOUND;
125 out_be32(hose->cfg_addr,
127 (bus->number << 16) |
132 #if defined(CONFIG_PPC_MPC5200_BUGFIX)
134 /* workaround for the bug 435 of the MPC5200 (L25R);
135 * Don't do 32 bits config access during type-1 cycles */
138 value = in_8(((u8 __iomem *)hose->cfg_data) +
142 value = in_le16(((u16 __iomem *)hose->cfg_data) +
147 value = in_le16((u16 __iomem *)hose->cfg_data) |
148 (in_le16(((u16 __iomem *)hose->cfg_data) + 1) << 16);
155 value = in_le32(hose->cfg_data);
158 value >>= ((offset & 0x3) << 3);
159 value &= 0xffffffff >> (32 - (len << 3));
165 out_be32(hose->cfg_addr, 0);
168 return PCIBIOS_SUCCESSFUL;
172 mpc52xx_pci_write_config(struct pci_bus *bus, unsigned int devfn,
173 int offset, int len, u32 val)
175 struct pci_controller *hose = bus->sysdata;
178 if (ppc_md.pci_exclude_device)
179 if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
180 return PCIBIOS_DEVICE_NOT_FOUND;
182 out_be32(hose->cfg_addr,
184 (bus->number << 16) |
189 #if defined(CONFIG_PPC_MPC5200_BUGFIX)
191 /* workaround for the bug 435 of the MPC5200 (L25R);
192 * Don't do 32 bits config access during type-1 cycles */
195 out_8(((u8 __iomem *)hose->cfg_data) +
199 out_le16(((u16 __iomem *)hose->cfg_data) +
200 ((offset>>1) & 1), val);
204 out_le16((u16 __iomem *)hose->cfg_data,
206 out_le16(((u16 __iomem *)hose->cfg_data) + 1,
215 value = in_le32(hose->cfg_data);
217 offset = (offset & 0x3) << 3;
218 mask = (0xffffffff >> (32 - (len << 3)));
222 val = value | ((val << offset) & mask);
225 out_le32(hose->cfg_data, val);
229 out_be32(hose->cfg_addr, 0);
232 return PCIBIOS_SUCCESSFUL;
235 static struct pci_ops mpc52xx_pci_ops = {
236 .read = mpc52xx_pci_read_config,
237 .write = mpc52xx_pci_write_config
241 /* ======================================================================== */
243 /* ======================================================================== */
246 mpc52xx_pci_setup(struct pci_controller *hose,
247 struct mpc52xx_pci __iomem *pci_regs)
249 struct resource *res;
251 int iwcr0 = 0, iwcr1 = 0, iwcr2 = 0;
253 pr_debug("mpc52xx_pci_setup(hose=%p, pci_regs=%p)\n", hose, pci_regs);
255 /* pci_process_bridge_OF_ranges() found all our addresses for us;
256 * now store them in the right places */
257 hose->cfg_addr = &pci_regs->car;
258 hose->cfg_data = hose->io_base_virt;
261 tmp = in_be32(&pci_regs->scr);
262 tmp |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
263 out_be32(&pci_regs->scr, tmp);
266 res = &hose->mem_resources[0];
268 pr_debug("mem_resource[0] = {.start=%x, .end=%x, .flags=%lx}\n",
269 res->start, res->end, res->flags);
270 out_be32(&pci_regs->iw0btar,
271 MPC52xx_PCI_IWBTAR_TRANSLATION(res->start, res->start,
272 res->end - res->start + 1));
273 iwcr0 = MPC52xx_PCI_IWCR_ENABLE | MPC52xx_PCI_IWCR_MEM;
274 if (res->flags & IORESOURCE_PREFETCH)
275 iwcr0 |= MPC52xx_PCI_IWCR_READ_MULTI;
277 iwcr0 |= MPC52xx_PCI_IWCR_READ;
280 res = &hose->mem_resources[1];
282 pr_debug("mem_resource[1] = {.start=%x, .end=%x, .flags=%lx}\n",
283 res->start, res->end, res->flags);
284 out_be32(&pci_regs->iw1btar,
285 MPC52xx_PCI_IWBTAR_TRANSLATION(res->start, res->start,
286 res->end - res->start + 1));
287 iwcr1 = MPC52xx_PCI_IWCR_ENABLE | MPC52xx_PCI_IWCR_MEM;
288 if (res->flags & IORESOURCE_PREFETCH)
289 iwcr1 |= MPC52xx_PCI_IWCR_READ_MULTI;
291 iwcr1 |= MPC52xx_PCI_IWCR_READ;
295 res = &hose->io_resource;
297 printk(KERN_ERR "%s: Didn't find IO resources\n", __FILE__);
300 pr_debug(".io_resource={.start=%x,.end=%x,.flags=%lx} "
301 ".io_base_phys=0x%p\n",
302 res->start, res->end, res->flags, (void*)hose->io_base_phys);
303 out_be32(&pci_regs->iw2btar,
304 MPC52xx_PCI_IWBTAR_TRANSLATION(hose->io_base_phys,
306 res->end - res->start + 1));
307 iwcr2 = MPC52xx_PCI_IWCR_ENABLE | MPC52xx_PCI_IWCR_IO;
309 /* Set all the IWCR fields at once; they're in the same reg */
310 out_be32(&pci_regs->iwcr, MPC52xx_PCI_IWCR_PACK(iwcr0, iwcr1, iwcr2));
312 out_be32(&pci_regs->tbatr0,
313 MPC52xx_PCI_TBATR_ENABLE | MPC52xx_PCI_TARGET_IO );
314 out_be32(&pci_regs->tbatr1,
315 MPC52xx_PCI_TBATR_ENABLE | MPC52xx_PCI_TARGET_MEM );
317 out_be32(&pci_regs->tcr, MPC52xx_PCI_TCR_LD | MPC52xx_PCI_TCR_WCT8);
319 tmp = in_be32(&pci_regs->gscr);
321 /* Reset the exteral bus ( internal PCI controller is NOT resetted ) */
322 /* Not necessary and can be a bad thing if for example the bootloader
323 is displaying a splash screen or ... Just left here for
324 documentation purpose if anyone need it */
325 out_be32(&pci_regs->gscr, tmp | MPC52xx_PCI_GSCR_PR);
329 /* Make sure the PCI bridge is out of reset */
330 out_be32(&pci_regs->gscr, tmp & ~MPC52xx_PCI_GSCR_PR);
334 mpc52xx_pci_fixup_resources(struct pci_dev *dev)
338 pr_debug("mpc52xx_pci_fixup_resources() %.4x:%.4x\n",
339 dev->vendor, dev->device);
341 /* We don't rely on boot loader for PCI and resets all
343 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
344 struct resource *res = &dev->resource[i];
345 if (res->end > res->start) { /* Only valid resources */
346 res->end -= res->start;
348 res->flags |= IORESOURCE_UNSET;
352 /* The PCI Host bridge of MPC52xx has a prefetch memory resource
353 fixed to 1Gb. Doesn't fit in the resource system so we remove it */
354 if ( (dev->vendor == PCI_VENDOR_ID_MOTOROLA) &&
355 ( dev->device == PCI_DEVICE_ID_MOTOROLA_MPC5200
356 || dev->device == PCI_DEVICE_ID_MOTOROLA_MPC5200B) ) {
357 struct resource *res = &dev->resource[1];
358 res->start = res->end = res->flags = 0;
363 mpc52xx_add_bridge(struct device_node *node)
366 struct mpc52xx_pci __iomem *pci_regs;
367 struct pci_controller *hose;
368 const int *bus_range;
369 struct resource rsrc;
371 pr_debug("Adding MPC52xx PCI host bridge %s\n", node->full_name);
373 ppc_pci_flags |= PPC_PCI_REASSIGN_ALL_BUS;
375 if (of_address_to_resource(node, 0, &rsrc) != 0) {
376 printk(KERN_ERR "Can't get %s resources\n", node->full_name);
380 bus_range = of_get_property(node, "bus-range", &len);
381 if (bus_range == NULL || len < 2 * sizeof(int)) {
382 printk(KERN_WARNING "Can't get %s bus-range, assume bus 0\n",
387 /* There are some PCI quirks on the 52xx, register the hook to
389 ppc_md.pcibios_fixup_resources = mpc52xx_pci_fixup_resources;
391 /* Alloc and initialize the pci controller. Values in the device
392 * tree are needed to configure the 52xx PCI controller. Rather
393 * than parse the tree here, let pci_process_bridge_OF_ranges()
394 * do it for us and extract the values after the fact */
395 hose = pcibios_alloc_controller(node);
399 hose->first_busno = bus_range ? bus_range[0] : 0;
400 hose->last_busno = bus_range ? bus_range[1] : 0xff;
402 hose->ops = &mpc52xx_pci_ops;
404 pci_regs = ioremap(rsrc.start, rsrc.end - rsrc.start + 1);
408 pci_process_bridge_OF_ranges(hose, node, 1);
410 /* Finish setting up PCI using values obtained by
411 * pci_proces_bridge_OF_ranges */
412 mpc52xx_pci_setup(hose, pci_regs);
417 void __init mpc52xx_setup_pci(void)
419 struct device_node *pci;
421 pci = of_find_matching_node(NULL, mpc52xx_pci_ids);
425 mpc52xx_add_bridge(pci);