2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2003 Christoph Hellwig (hch@lst.de)
7 * Copyright (C) 1999, 2000 Ralf Baechle (ralf@gnu.org)
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/pci.h>
13 #include <asm/sn/arch.h>
14 #include <asm/pci/bridge.h>
15 #include <asm/pci_channel.h>
16 #include <asm/paccess.h>
17 #include <asm/sn/intr.h>
18 #include <asm/sn/sn0/hub.h>
20 extern unsigned int allocate_irqno(void);
23 * Max #PCI busses we can handle; ie, max #PCI bridges.
25 #define MAX_PCI_BUSSES 40
28 * Max #PCI devices (like scsi controllers) we handle on a bus.
30 #define MAX_DEVICES_PER_PCIBUS 8
33 * XXX: No kmalloc available when we do our crosstalk scan,
34 * we should try to move it later in the boot process.
36 static struct bridge_controller bridges[MAX_PCI_BUSSES];
39 * Translate from irq to software PCI bus number and PCI slot.
41 struct bridge_controller *irq_to_bridge[MAX_PCI_BUSSES * MAX_DEVICES_PER_PCIBUS];
42 int irq_to_slot[MAX_PCI_BUSSES * MAX_DEVICES_PER_PCIBUS];
45 * The Bridge ASIC supports both type 0 and type 1 access. Type 1 is
46 * not really documented, so right now I can't write code which uses it.
47 * Therefore we use type 0 accesses for now even though they won't work
48 * correcly for PCI-to-PCI bridges.
50 * The function is complicated by the ultimate brokeness of the IOC3 chip
51 * which is used in SGI systems. The IOC3 can only handle 32-bit PCI
52 * accesses and does only decode parts of it's address space.
55 static int pci_conf0_read_config(struct pci_bus *bus, unsigned int devfn,
56 int where, int size, u32 * value)
58 struct bridge_controller *bc = BRIDGE_CONTROLLER(bus);
59 bridge_t *bridge = bc->base;
60 int slot = PCI_SLOT(devfn);
61 int fn = PCI_FUNC(devfn);
66 addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[PCI_VENDOR_ID];
67 if (get_dbe(cf, (u32 *) addr))
68 return PCIBIOS_DEVICE_NOT_FOUND;
71 * IOC3 is fucked fucked beyond believe ... Don't even give the
72 * generic PCI code a chance to look at it for real ...
74 if (cf == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16)))
77 addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[where ^ (4 - size)];
80 res = get_dbe(*value, (u8 *) addr);
82 res = get_dbe(*value, (u16 *) addr);
84 res = get_dbe(*value, (u32 *) addr);
86 return PCIBIOS_SUCCESSFUL;
91 * IOC3 is fucked fucked beyond believe ... Don't even give the
92 * generic PCI code a chance to look at the wrong register.
94 if ((where >= 0x14 && where < 0x40) || (where >= 0x48)) {
96 return PCIBIOS_SUCCESSFUL;
100 * IOC3 is fucked fucked beyond believe ... Don't try to access
101 * anything but 32-bit words ...
103 addr = &bridge->b_type0_cfg_dev[slot].f[fn].l[where >> 2];
105 if (get_dbe(cf, (u32 *) addr))
106 return PCIBIOS_DEVICE_NOT_FOUND;
108 shift = ((where & 3) << 3);
109 mask = (0xffffffffU >> ((4 - size) << 3));
110 *value = (cf >> shift) & mask;
112 return PCIBIOS_SUCCESSFUL;
115 static int pci_conf0_write_config(struct pci_bus *bus, unsigned int devfn,
116 int where, int size, u32 value)
118 struct bridge_controller *bc = BRIDGE_CONTROLLER(bus);
119 bridge_t *bridge = bc->base;
120 int slot = PCI_SLOT(devfn);
121 int fn = PCI_FUNC(devfn);
123 u32 cf, shift, mask, smask;
126 addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[PCI_VENDOR_ID];
127 if (get_dbe(cf, (u32 *) addr))
128 return PCIBIOS_DEVICE_NOT_FOUND;
131 * IOC3 is fucked fucked beyond believe ... Don't even give the
132 * generic PCI code a chance to look at it for real ...
134 if (cf == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16)))
137 addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[where ^ (4 - size)];
140 res = put_dbe(value, (u8 *) addr);
141 } else if (size == 2) {
142 res = put_dbe(value, (u16 *) addr);
144 res = put_dbe(value, (u32 *) addr);
148 return PCIBIOS_DEVICE_NOT_FOUND;
150 return PCIBIOS_SUCCESSFUL;
155 * IOC3 is fucked fucked beyond believe ... Don't even give the
156 * generic PCI code a chance to touch the wrong register.
158 if ((where >= 0x14 && where < 0x40) || (where >= 0x48))
159 return PCIBIOS_SUCCESSFUL;
162 * IOC3 is fucked fucked beyond believe ... Don't try to access
163 * anything but 32-bit words ...
165 addr = &bridge->b_type0_cfg_dev[slot].f[fn].l[where >> 2];
167 if (get_dbe(cf, (u32 *) addr))
168 return PCIBIOS_DEVICE_NOT_FOUND;
170 shift = ((where & 3) << 3);
171 mask = (0xffffffffU >> ((4 - size) << 3));
172 smask = mask << shift;
174 cf = (cf & ~smask) | ((value & mask) << shift);
175 if (put_dbe(cf, (u32 *) addr))
176 return PCIBIOS_DEVICE_NOT_FOUND;
178 return PCIBIOS_SUCCESSFUL;
181 static struct pci_ops bridge_pci_ops = {
182 .read = pci_conf0_read_config,
183 .write = pci_conf0_write_config,
186 int __init bridge_probe(nasid_t nasid, int widget_id, int masterwid)
188 unsigned long offset = NODE_OFFSET(nasid);
189 struct bridge_controller *bc;
190 static int num_bridges = 0;
194 printk("a bridge\n");
196 /* XXX: kludge alert.. */
198 ioport_resource.end = ~0UL;
200 bc = &bridges[num_bridges++];
202 bc->pc.pci_ops = &bridge_pci_ops;
203 bc->pc.mem_resource = &bc->mem;
204 bc->pc.io_resource = &bc->io;
206 bc->mem.name = "Bridge PCI MEM";
207 bc->pc.mem_offset = offset;
210 bc->mem.flags = IORESOURCE_MEM;
212 bc->io.name = "Bridge IO MEM";
213 bc->pc.io_offset = offset;
216 bc->io.flags = IORESOURCE_IO;
218 bc->irq_cpu = smp_processor_id();
219 bc->widget_id = widget_id;
222 bc->baddr = (u64)masterwid << 60;
223 bc->baddr |= (1UL << 56); /* Barrier set */
226 * point to this bridge
228 bridge = (bridge_t *) RAW_NODE_SWIN_BASE(nasid, widget_id);
231 * Clear all pending interrupts.
233 bridge->b_int_rst_stat = BRIDGE_IRR_ALL_CLR;
236 * Until otherwise set up, assume all interrupts are from slot 0
238 bridge->b_int_device = (u32) 0x0;
241 * swap pio's to pci mem and io space (big windows)
243 bridge->b_wid_control |= BRIDGE_CTRL_IO_SWAP |
244 BRIDGE_CTRL_MEM_SWAP;
247 * Hmm... IRIX sets additional bits in the address which
248 * are documented as reserved in the bridge docs.
250 bridge->b_wid_int_upper = 0x8000 | (masterwid << 16);
251 bridge->b_wid_int_lower = 0x01800090; /* PI_INT_PEND_MOD off*/
252 bridge->b_dir_map = (masterwid << 20); /* DMA */
253 bridge->b_int_enable = 0;
255 for (slot = 0; slot < 8; slot ++)
256 bridge->b_device[slot].reg |= BRIDGE_DEV_SWAP_DIR;
257 bridge->b_widget.w_tflush; /* Flush */
259 bridge->b_wid_tflush; /* wait until Bridge PIO complete */
263 register_pci_controller(&bc->pc);
268 * All observed requests have pin == 1. We could have a global here, that
269 * gets incremented and returned every time - unfortunately, pci_map_irq
270 * may be called on the same device over and over, and need to return the
271 * same value. On O2000, pin can be 0 or 1, and PCI slots can be [0..7].
273 * A given PCI device, in general, should be able to intr any of the cpus
274 * on any one of the hubs connected to its xbow.
276 int __devinit pcibios_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
278 struct bridge_controller *bc = BRIDGE_CONTROLLER(dev->bus);
281 irq = allocate_irqno();
284 * Argh... This API doesn't handle with errors at all ...
287 printk(KERN_ERR "Can't allocate interrupt for PCI device %s\n",
292 irq_to_bridge[irq] = bc;
293 irq_to_slot[irq] = slot;
299 * Device might live on a subordinate PCI bus. XXX Walk up the chain of buses
300 * to find the slot number in sense of the bridge device register.
301 * XXX This also means multiple devices might rely on conflicting bridge
305 static inline void pci_disable_swapping(struct pci_dev *dev)
307 struct bridge_controller *bc = BRIDGE_CONTROLLER(dev->bus);
308 bridge_t *bridge = bc->base;
309 int slot = PCI_SLOT(dev->devfn);
311 /* Turn off byte swapping */
312 bridge->b_device[slot].reg &= ~BRIDGE_DEV_SWAP_DIR;
313 bridge->b_widget.w_tflush; /* Flush */
316 static inline void pci_enable_swapping(struct pci_dev *dev)
318 struct bridge_controller *bc = BRIDGE_CONTROLLER(dev->bus);
319 bridge_t *bridge = bc->base;
320 int slot = PCI_SLOT(dev->devfn);
322 /* Turn on byte swapping */
323 bridge->b_device[slot].reg |= BRIDGE_DEV_SWAP_DIR;
324 bridge->b_widget.w_tflush; /* Flush */
327 static void __init pci_fixup_ioc3(struct pci_dev *d)
329 pci_disable_swapping(d);
332 struct pci_fixup pcibios_fixups[] = {
333 {PCI_FIXUP_HEADER, PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3,