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] = "
269 "{.start=%llx, .end=%llx, .flags=%llx}\n",
270 (unsigned long long)res
->start
,
271 (unsigned long long)res
->end
,
272 (unsigned long long)res
->flags
);
273 out_be32(&pci_regs
->iw0btar
,
274 MPC52xx_PCI_IWBTAR_TRANSLATION(res
->start
, res
->start
,
275 res
->end
- res
->start
+ 1));
276 iwcr0
= MPC52xx_PCI_IWCR_ENABLE
| MPC52xx_PCI_IWCR_MEM
;
277 if (res
->flags
& IORESOURCE_PREFETCH
)
278 iwcr0
|= MPC52xx_PCI_IWCR_READ_MULTI
;
280 iwcr0
|= MPC52xx_PCI_IWCR_READ
;
283 res
= &hose
->mem_resources
[1];
285 pr_debug("mem_resource[1] = {.start=%x, .end=%x, .flags=%lx}\n",
286 res
->start
, res
->end
, res
->flags
);
287 out_be32(&pci_regs
->iw1btar
,
288 MPC52xx_PCI_IWBTAR_TRANSLATION(res
->start
, res
->start
,
289 res
->end
- res
->start
+ 1));
290 iwcr1
= MPC52xx_PCI_IWCR_ENABLE
| MPC52xx_PCI_IWCR_MEM
;
291 if (res
->flags
& IORESOURCE_PREFETCH
)
292 iwcr1
|= MPC52xx_PCI_IWCR_READ_MULTI
;
294 iwcr1
|= MPC52xx_PCI_IWCR_READ
;
298 res
= &hose
->io_resource
;
300 printk(KERN_ERR
"%s: Didn't find IO resources\n", __FILE__
);
303 pr_debug(".io_resource={.start=%llx,.end=%llx,.flags=%llx} "
304 ".io_base_phys=0x%p\n",
305 (unsigned long long)res
->start
,
306 (unsigned long long)res
->end
,
307 (unsigned long long)res
->flags
, (void*)hose
->io_base_phys
);
308 out_be32(&pci_regs
->iw2btar
,
309 MPC52xx_PCI_IWBTAR_TRANSLATION(hose
->io_base_phys
,
311 res
->end
- res
->start
+ 1));
312 iwcr2
= MPC52xx_PCI_IWCR_ENABLE
| MPC52xx_PCI_IWCR_IO
;
314 /* Set all the IWCR fields at once; they're in the same reg */
315 out_be32(&pci_regs
->iwcr
, MPC52xx_PCI_IWCR_PACK(iwcr0
, iwcr1
, iwcr2
));
317 out_be32(&pci_regs
->tbatr0
,
318 MPC52xx_PCI_TBATR_ENABLE
| MPC52xx_PCI_TARGET_IO
);
319 out_be32(&pci_regs
->tbatr1
,
320 MPC52xx_PCI_TBATR_ENABLE
| MPC52xx_PCI_TARGET_MEM
);
322 out_be32(&pci_regs
->tcr
, MPC52xx_PCI_TCR_LD
| MPC52xx_PCI_TCR_WCT8
);
324 tmp
= in_be32(&pci_regs
->gscr
);
326 /* Reset the exteral bus ( internal PCI controller is NOT resetted ) */
327 /* Not necessary and can be a bad thing if for example the bootloader
328 is displaying a splash screen or ... Just left here for
329 documentation purpose if anyone need it */
330 out_be32(&pci_regs
->gscr
, tmp
| MPC52xx_PCI_GSCR_PR
);
334 /* Make sure the PCI bridge is out of reset */
335 out_be32(&pci_regs
->gscr
, tmp
& ~MPC52xx_PCI_GSCR_PR
);
339 mpc52xx_pci_fixup_resources(struct pci_dev
*dev
)
343 pr_debug("mpc52xx_pci_fixup_resources() %.4x:%.4x\n",
344 dev
->vendor
, dev
->device
);
346 /* We don't rely on boot loader for PCI and resets all
348 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
349 struct resource
*res
= &dev
->resource
[i
];
350 if (res
->end
> res
->start
) { /* Only valid resources */
351 res
->end
-= res
->start
;
353 res
->flags
|= IORESOURCE_UNSET
;
357 /* The PCI Host bridge of MPC52xx has a prefetch memory resource
358 fixed to 1Gb. Doesn't fit in the resource system so we remove it */
359 if ( (dev
->vendor
== PCI_VENDOR_ID_MOTOROLA
) &&
360 ( dev
->device
== PCI_DEVICE_ID_MOTOROLA_MPC5200
361 || dev
->device
== PCI_DEVICE_ID_MOTOROLA_MPC5200B
) ) {
362 struct resource
*res
= &dev
->resource
[1];
363 res
->start
= res
->end
= res
->flags
= 0;
368 mpc52xx_add_bridge(struct device_node
*node
)
371 struct mpc52xx_pci __iomem
*pci_regs
;
372 struct pci_controller
*hose
;
373 const int *bus_range
;
374 struct resource rsrc
;
376 pr_debug("Adding MPC52xx PCI host bridge %s\n", node
->full_name
);
378 ppc_pci_flags
|= PPC_PCI_REASSIGN_ALL_BUS
;
380 if (of_address_to_resource(node
, 0, &rsrc
) != 0) {
381 printk(KERN_ERR
"Can't get %s resources\n", node
->full_name
);
385 bus_range
= of_get_property(node
, "bus-range", &len
);
386 if (bus_range
== NULL
|| len
< 2 * sizeof(int)) {
387 printk(KERN_WARNING
"Can't get %s bus-range, assume bus 0\n",
392 /* There are some PCI quirks on the 52xx, register the hook to
394 ppc_md
.pcibios_fixup_resources
= mpc52xx_pci_fixup_resources
;
396 /* Alloc and initialize the pci controller. Values in the device
397 * tree are needed to configure the 52xx PCI controller. Rather
398 * than parse the tree here, let pci_process_bridge_OF_ranges()
399 * do it for us and extract the values after the fact */
400 hose
= pcibios_alloc_controller(node
);
404 hose
->first_busno
= bus_range
? bus_range
[0] : 0;
405 hose
->last_busno
= bus_range
? bus_range
[1] : 0xff;
407 hose
->ops
= &mpc52xx_pci_ops
;
409 pci_regs
= ioremap(rsrc
.start
, rsrc
.end
- rsrc
.start
+ 1);
413 pci_process_bridge_OF_ranges(hose
, node
, 1);
415 /* Finish setting up PCI using values obtained by
416 * pci_proces_bridge_OF_ranges */
417 mpc52xx_pci_setup(hose
, pci_regs
);
422 void __init
mpc52xx_setup_pci(void)
424 struct device_node
*pci
;
426 pci
= of_find_matching_node(NULL
, mpc52xx_pci_ids
);
430 mpc52xx_add_bridge(pci
);