2 * Copyright (C) 2004 Benjamin Herrenschmuidt (benh@kernel.crashing.org),
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
13 #include <linux/kernel.h>
14 #include <linux/pci.h>
15 #include <linux/delay.h>
16 #include <linux/string.h>
17 #include <linux/init.h>
18 #include <linux/bootmem.h>
19 #include <linux/irq.h>
21 #include <asm/sections.h>
24 #include <asm/pci-bridge.h>
25 #include <asm/machdep.h>
26 #include <asm/iommu.h>
27 #include <asm/ppc-pci.h>
32 #define DBG(x...) printk(x)
37 static struct pci_controller
*u3_agp
, *u3_ht
, *u4_pcie
;
39 static int __init
fixup_one_level_bus_range(struct device_node
*node
, int higher
)
41 for (; node
!= 0;node
= node
->sibling
) {
43 const unsigned int *class_code
;
46 /* For PCI<->PCI bridges or CardBus bridges, we go down */
47 class_code
= of_get_property(node
, "class-code", NULL
);
48 if (!class_code
|| ((*class_code
>> 8) != PCI_CLASS_BRIDGE_PCI
&&
49 (*class_code
>> 8) != PCI_CLASS_BRIDGE_CARDBUS
))
51 bus_range
= of_get_property(node
, "bus-range", &len
);
52 if (bus_range
!= NULL
&& len
> 2 * sizeof(int)) {
53 if (bus_range
[1] > higher
)
54 higher
= bus_range
[1];
56 higher
= fixup_one_level_bus_range(node
->child
, higher
);
61 /* This routine fixes the "bus-range" property of all bridges in the
62 * system since they tend to have their "last" member wrong on macs
64 * Note that the bus numbers manipulated here are OF bus numbers, they
65 * are not Linux bus numbers.
67 static void __init
fixup_bus_range(struct device_node
*bridge
)
70 struct property
*prop
;
73 /* Lookup the "bus-range" property for the hose */
74 prop
= of_find_property(bridge
, "bus-range", &len
);
75 if (prop
== NULL
|| prop
->value
== NULL
|| len
< 2 * sizeof(int)) {
76 printk(KERN_WARNING
"Can't get bus-range for %s\n",
80 bus_range
= prop
->value
;
81 bus_range
[1] = fixup_one_level_bus_range(bridge
->child
, bus_range
[1]);
85 static unsigned long u3_agp_cfa0(u8 devfn
, u8 off
)
87 return (1 << (unsigned long)PCI_SLOT(devfn
)) |
88 ((unsigned long)PCI_FUNC(devfn
) << 8) |
89 ((unsigned long)off
& 0xFCUL
);
92 static unsigned long u3_agp_cfa1(u8 bus
, u8 devfn
, u8 off
)
94 return ((unsigned long)bus
<< 16) |
95 ((unsigned long)devfn
<< 8) |
96 ((unsigned long)off
& 0xFCUL
) |
100 static volatile void __iomem
*u3_agp_cfg_access(struct pci_controller
* hose
,
101 u8 bus
, u8 dev_fn
, u8 offset
)
105 if (bus
== hose
->first_busno
) {
106 if (dev_fn
< (11 << 3))
108 caddr
= u3_agp_cfa0(dev_fn
, offset
);
110 caddr
= u3_agp_cfa1(bus
, dev_fn
, offset
);
112 /* Uninorth will return garbage if we don't read back the value ! */
114 out_le32(hose
->cfg_addr
, caddr
);
115 } while (in_le32(hose
->cfg_addr
) != caddr
);
118 return hose
->cfg_data
+ offset
;
121 static int u3_agp_read_config(struct pci_bus
*bus
, unsigned int devfn
,
122 int offset
, int len
, u32
*val
)
124 struct pci_controller
*hose
;
125 volatile void __iomem
*addr
;
127 hose
= pci_bus_to_host(bus
);
129 return PCIBIOS_DEVICE_NOT_FOUND
;
131 addr
= u3_agp_cfg_access(hose
, bus
->number
, devfn
, offset
);
133 return PCIBIOS_DEVICE_NOT_FOUND
;
135 * Note: the caller has already checked that offset is
136 * suitably aligned and that len is 1, 2 or 4.
143 *val
= in_le16(addr
);
146 *val
= in_le32(addr
);
149 return PCIBIOS_SUCCESSFUL
;
152 static int u3_agp_write_config(struct pci_bus
*bus
, unsigned int devfn
,
153 int offset
, int len
, u32 val
)
155 struct pci_controller
*hose
;
156 volatile void __iomem
*addr
;
158 hose
= pci_bus_to_host(bus
);
160 return PCIBIOS_DEVICE_NOT_FOUND
;
162 addr
= u3_agp_cfg_access(hose
, bus
->number
, devfn
, offset
);
164 return PCIBIOS_DEVICE_NOT_FOUND
;
166 * Note: the caller has already checked that offset is
167 * suitably aligned and that len is 1, 2 or 4.
180 return PCIBIOS_SUCCESSFUL
;
183 static struct pci_ops u3_agp_pci_ops
=
185 .read
= u3_agp_read_config
,
186 .write
= u3_agp_write_config
,
189 static unsigned long u3_ht_cfa0(u8 devfn
, u8 off
)
191 return (devfn
<< 8) | off
;
194 static unsigned long u3_ht_cfa1(u8 bus
, u8 devfn
, u8 off
)
196 return u3_ht_cfa0(devfn
, off
) + (bus
<< 16) + 0x01000000UL
;
199 static volatile void __iomem
*u3_ht_cfg_access(struct pci_controller
* hose
,
200 u8 bus
, u8 devfn
, u8 offset
)
202 if (bus
== hose
->first_busno
) {
203 if (PCI_SLOT(devfn
) == 0)
205 return hose
->cfg_data
+ u3_ht_cfa0(devfn
, offset
);
207 return hose
->cfg_data
+ u3_ht_cfa1(bus
, devfn
, offset
);
210 static int u3_ht_read_config(struct pci_bus
*bus
, unsigned int devfn
,
211 int offset
, int len
, u32
*val
)
213 struct pci_controller
*hose
;
214 volatile void __iomem
*addr
;
216 hose
= pci_bus_to_host(bus
);
218 return PCIBIOS_DEVICE_NOT_FOUND
;
221 return PCIBIOS_BAD_REGISTER_NUMBER
;
223 addr
= u3_ht_cfg_access(hose
, bus
->number
, devfn
, offset
);
225 return PCIBIOS_DEVICE_NOT_FOUND
;
228 * Note: the caller has already checked that offset is
229 * suitably aligned and that len is 1, 2 or 4.
236 *val
= in_le16(addr
);
239 *val
= in_le32(addr
);
242 return PCIBIOS_SUCCESSFUL
;
245 static int u3_ht_write_config(struct pci_bus
*bus
, unsigned int devfn
,
246 int offset
, int len
, u32 val
)
248 struct pci_controller
*hose
;
249 volatile void __iomem
*addr
;
251 hose
= pci_bus_to_host(bus
);
253 return PCIBIOS_DEVICE_NOT_FOUND
;
256 return PCIBIOS_BAD_REGISTER_NUMBER
;
258 addr
= u3_ht_cfg_access(hose
, bus
->number
, devfn
, offset
);
260 return PCIBIOS_DEVICE_NOT_FOUND
;
262 * Note: the caller has already checked that offset is
263 * suitably aligned and that len is 1, 2 or 4.
276 return PCIBIOS_SUCCESSFUL
;
279 static struct pci_ops u3_ht_pci_ops
=
281 .read
= u3_ht_read_config
,
282 .write
= u3_ht_write_config
,
285 static unsigned int u4_pcie_cfa0(unsigned int devfn
, unsigned int off
)
287 return (1 << PCI_SLOT(devfn
)) |
288 (PCI_FUNC(devfn
) << 8) |
293 static unsigned int u4_pcie_cfa1(unsigned int bus
, unsigned int devfn
,
302 static volatile void __iomem
*u4_pcie_cfg_access(struct pci_controller
* hose
,
303 u8 bus
, u8 dev_fn
, int offset
)
307 if (bus
== hose
->first_busno
)
308 caddr
= u4_pcie_cfa0(dev_fn
, offset
);
310 caddr
= u4_pcie_cfa1(bus
, dev_fn
, offset
);
312 /* Uninorth will return garbage if we don't read back the value ! */
314 out_le32(hose
->cfg_addr
, caddr
);
315 } while (in_le32(hose
->cfg_addr
) != caddr
);
318 return hose
->cfg_data
+ offset
;
321 static int u4_pcie_read_config(struct pci_bus
*bus
, unsigned int devfn
,
322 int offset
, int len
, u32
*val
)
324 struct pci_controller
*hose
;
325 volatile void __iomem
*addr
;
327 hose
= pci_bus_to_host(bus
);
329 return PCIBIOS_DEVICE_NOT_FOUND
;
330 if (offset
>= 0x1000)
331 return PCIBIOS_BAD_REGISTER_NUMBER
;
332 addr
= u4_pcie_cfg_access(hose
, bus
->number
, devfn
, offset
);
334 return PCIBIOS_DEVICE_NOT_FOUND
;
336 * Note: the caller has already checked that offset is
337 * suitably aligned and that len is 1, 2 or 4.
344 *val
= in_le16(addr
);
347 *val
= in_le32(addr
);
350 return PCIBIOS_SUCCESSFUL
;
352 static int u4_pcie_write_config(struct pci_bus
*bus
, unsigned int devfn
,
353 int offset
, int len
, u32 val
)
355 struct pci_controller
*hose
;
356 volatile void __iomem
*addr
;
358 hose
= pci_bus_to_host(bus
);
360 return PCIBIOS_DEVICE_NOT_FOUND
;
361 if (offset
>= 0x1000)
362 return PCIBIOS_BAD_REGISTER_NUMBER
;
363 addr
= u4_pcie_cfg_access(hose
, bus
->number
, devfn
, offset
);
365 return PCIBIOS_DEVICE_NOT_FOUND
;
367 * Note: the caller has already checked that offset is
368 * suitably aligned and that len is 1, 2 or 4.
381 return PCIBIOS_SUCCESSFUL
;
384 static struct pci_ops u4_pcie_pci_ops
=
386 .read
= u4_pcie_read_config
,
387 .write
= u4_pcie_write_config
,
390 static void __init
setup_u3_agp(struct pci_controller
* hose
)
392 /* On G5, we move AGP up to high bus number so we don't need
393 * to reassign bus numbers for HT. If we ever have P2P bridges
394 * on AGP, we'll have to move pci_assign_all_buses to the
395 * pci_controller structure so we enable it for AGP and not for
397 * We hard code the address because of the different size of
398 * the reg address cell, we shall fix that by killing struct
399 * reg_property and using some accessor functions instead
401 hose
->first_busno
= 0xf0;
402 hose
->last_busno
= 0xff;
403 hose
->ops
= &u3_agp_pci_ops
;
404 hose
->cfg_addr
= ioremap(0xf0000000 + 0x800000, 0x1000);
405 hose
->cfg_data
= ioremap(0xf0000000 + 0xc00000, 0x1000);
410 static void __init
setup_u4_pcie(struct pci_controller
* hose
)
412 /* We currently only implement the "non-atomic" config space, to
413 * be optimised later.
415 hose
->ops
= &u4_pcie_pci_ops
;
416 hose
->cfg_addr
= ioremap(0xf0000000 + 0x800000, 0x1000);
417 hose
->cfg_data
= ioremap(0xf0000000 + 0xc00000, 0x1000);
422 static void __init
setup_u3_ht(struct pci_controller
* hose
)
424 hose
->ops
= &u3_ht_pci_ops
;
426 /* We hard code the address because of the different size of
427 * the reg address cell, we shall fix that by killing struct
428 * reg_property and using some accessor functions instead
430 hose
->cfg_data
= ioremap(0xf2000000, 0x02000000);
432 hose
->first_busno
= 0;
433 hose
->last_busno
= 0xef;
438 static int __init
maple_add_bridge(struct device_node
*dev
)
441 struct pci_controller
*hose
;
443 const int *bus_range
;
446 DBG("Adding PCI host bridge %s\n", dev
->full_name
);
448 bus_range
= of_get_property(dev
, "bus-range", &len
);
449 if (bus_range
== NULL
|| len
< 2 * sizeof(int)) {
450 printk(KERN_WARNING
"Can't get bus-range for %s, assume bus 0\n",
454 hose
= pcibios_alloc_controller(dev
);
457 hose
->first_busno
= bus_range
? bus_range
[0] : 0;
458 hose
->last_busno
= bus_range
? bus_range
[1] : 0xff;
461 if (of_device_is_compatible(dev
, "u3-agp")) {
463 disp_name
= "U3-AGP";
465 } else if (of_device_is_compatible(dev
, "u3-ht")) {
469 } else if (of_device_is_compatible(dev
, "u4-pcie")) {
471 disp_name
= "U4-PCIE";
474 printk(KERN_INFO
"Found %s PCI host bridge. Firmware bus number: %d->%d\n",
475 disp_name
, hose
->first_busno
, hose
->last_busno
);
477 /* Interpret the "ranges" property */
478 /* This also maps the I/O region and sets isa_io/mem_base */
479 pci_process_bridge_OF_ranges(hose
, dev
, primary
);
481 /* Fixup "bus-range" OF property */
482 fixup_bus_range(dev
);
484 /* Check for legacy IOs */
485 isa_bridge_find_early(hose
);
491 void __devinit
maple_pci_irq_fixup(struct pci_dev
*dev
)
493 DBG(" -> maple_pci_irq_fixup\n");
495 /* Fixup IRQ for PCIe host */
496 if (u4_pcie
!= NULL
&& dev
->bus
->number
== 0 &&
497 pci_bus_to_host(dev
->bus
) == u4_pcie
) {
498 printk(KERN_DEBUG
"Fixup U4 PCIe IRQ\n");
499 dev
->irq
= irq_create_mapping(NULL
, 1);
500 if (dev
->irq
!= NO_IRQ
)
501 set_irq_type(dev
->irq
, IRQ_TYPE_LEVEL_LOW
);
504 /* Hide AMD8111 IDE interrupt when in legacy mode so
505 * the driver calls pci_get_legacy_ide_irq()
507 if (dev
->vendor
== PCI_VENDOR_ID_AMD
&&
508 dev
->device
== PCI_DEVICE_ID_AMD_8111_IDE
&&
509 (dev
->class & 5) != 5) {
513 DBG(" <- maple_pci_irq_fixup\n");
516 void __init
maple_pci_init(void)
518 struct device_node
*np
, *root
;
519 struct device_node
*ht
= NULL
;
521 /* Probe root PCI hosts, that is on U3 the AGP host and the
522 * HyperTransport host. That one is actually "kept" around
523 * and actually added last as it's resource management relies
524 * on the AGP resources to have been setup first
526 root
= of_find_node_by_path("/");
528 printk(KERN_CRIT
"maple_find_bridges: can't find root of device tree\n");
531 for (np
= NULL
; (np
= of_get_next_child(root
, np
)) != NULL
;) {
534 if (strcmp(np
->type
, "pci") && strcmp(np
->type
, "ht"))
536 if ((of_device_is_compatible(np
, "u4-pcie") ||
537 of_device_is_compatible(np
, "u3-agp")) &&
538 maple_add_bridge(np
) == 0)
541 if (of_device_is_compatible(np
, "u3-ht")) {
548 /* Now setup the HyperTransport host if we found any
550 if (ht
&& maple_add_bridge(ht
) != 0)
553 /* Setup the linkage between OF nodes and PHBs */
556 /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
557 * assume there is no P2P bridge on the AGP bus, which should be a
558 * safe assumptions hopefully.
561 struct device_node
*np
= u3_agp
->dn
;
562 PCI_DN(np
)->busno
= 0xf0;
563 for (np
= np
->child
; np
; np
= np
->sibling
)
564 PCI_DN(np
)->busno
= 0xf0;
567 /* Tell pci.c to not change any resource allocations. */
571 int maple_pci_get_legacy_ide_irq(struct pci_dev
*pdev
, int channel
)
573 struct device_node
*np
;
574 unsigned int defirq
= channel
? 15 : 14;
577 if (pdev
->vendor
!= PCI_VENDOR_ID_AMD
||
578 pdev
->device
!= PCI_DEVICE_ID_AMD_8111_IDE
)
581 np
= pci_device_to_OF_node(pdev
);
583 printk("Failed to locate OF node for IDE %s\n",
587 irq
= irq_of_parse_and_map(np
, channel
& 0x1);
589 printk("Failed to map onboard IDE interrupt for channel %d\n",
596 /* XXX: To remove once all firmwares are ok */
597 static void fixup_maple_ide(struct pci_dev
* dev
)
599 if (!machine_is(maple
))
602 #if 0 /* Enable this to enable IDE port 0 */
606 pci_read_config_byte(dev
, 0x40, &v
);
608 pci_write_config_byte(dev
, 0x40, v
);
611 #if 0 /* fix bus master base */
612 pci_write_config_dword(dev
, 0x20, 0xcc01);
613 printk("old ide resource: %lx -> %lx \n",
614 dev
->resource
[4].start
, dev
->resource
[4].end
);
615 dev
->resource
[4].start
= 0xcc00;
616 dev
->resource
[4].end
= 0xcc10;
618 #if 0 /* Enable this to fixup IDE sense/polarity of irqs in IO-APICs */
620 struct pci_dev
*apicdev
;
623 apicdev
= pci_get_slot (dev
->bus
, PCI_DEVFN(5,0));
625 printk("IDE Fixup IRQ: Can't find IO-APIC !\n");
627 pci_write_config_byte(apicdev
, 0xf2, 0x10 + 2*14);
628 pci_read_config_dword(apicdev
, 0xf4, &v
);
630 pci_write_config_dword(apicdev
, 0xf4, v
);
631 pci_write_config_byte(apicdev
, 0xf2, 0x10 + 2*15);
632 pci_read_config_dword(apicdev
, 0xf4, &v
);
634 pci_write_config_dword(apicdev
, 0xf4, v
);
635 pci_dev_put(apicdev
);
640 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_8111_IDE
,