Feed the mess through indent.
[linux-2.6/linux-mips.git] / arch / mips / pci / pci-ip27.c
blob42453760ab567b53b7ac936d33080dc275d92001
1 /*
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
4 * for more details.
6 * Copyright (C) 1999, 2000 Ralf Baechle (ralf@gnu.org)
7 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
8 */
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/pci.h>
12 #include <asm/sn/arch.h>
13 #include <asm/pci/bridge.h>
14 #include <asm/paccess.h>
15 #include <asm/sn/sn0/ip27.h>
16 #include <asm/sn/sn0/hub.h>
19 * Max #PCI busses we can handle; ie, max #PCI bridges.
21 #define MAX_PCI_BUSSES 40
24 * Max #PCI devices (like scsi controllers) we handle on a bus.
26 #define MAX_DEVICES_PER_PCIBUS 8
29 * No locking needed until PCI initialization is done parallely.
31 int irqstore[MAX_PCI_BUSSES][MAX_DEVICES_PER_PCIBUS];
32 int lastirq = BASE_PCI_IRQ;
35 * Translate from irq to software PCI bus number and PCI slot.
37 int irq_to_bus[MAX_PCI_BUSSES * MAX_DEVICES_PER_PCIBUS];
38 int irq_to_slot[MAX_PCI_BUSSES * MAX_DEVICES_PER_PCIBUS];
41 * The Bridge ASIC supports both type 0 and type 1 access. Type 1 is
42 * not really documented, so right now I can't write code which uses it.
43 * Therefore we use type 0 accesses for now even though they won't work
44 * correcly for PCI-to-PCI bridges.
46 #define CF0_READ_PCI_CFG(bus,devfn,where,value,bm,mask) \
47 do { \
48 bridge_t *bridge; \
49 int slot = PCI_SLOT(devfn); \
50 int fn = PCI_FUNC(devfn); \
51 volatile u32 *addr; \
52 u32 cf, __bit; \
53 unsigned int bus_id = (unsigned) bus->number; \
55 bridge = (bridge_t *) NODE_SWIN_BASE(bus_to_nid[bus_id], \
56 bus_to_wid[bus_id]); \
58 __bit = (((where) & (bm)) << 3); \
59 addr = &bridge->b_type0_cfg_dev[slot].f[fn].l[where >> 2]; \
60 if (get_dbe(cf, addr)) \
61 return PCIBIOS_DEVICE_NOT_FOUND; \
62 *value = (cf >> __bit) & (mask); \
63 return PCIBIOS_SUCCESSFUL; \
64 } while (0)
66 static int pci_conf0_read_config(struct pci_bus *bus, unsigned int devfn,
67 int where, int size, u32 * value)
69 u32 vprod;
71 CF0_READ_PCI_CFG(bus, devfn, PCI_VENDOR_ID, &vprod, 0, 0xffffffff);
72 if (vprod == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16))
73 && ((where >= 0x14 && where < 0x40) || (where >= 0x48))) {
74 *value = 0;
75 return PCIBIOS_SUCCESSFUL;
78 if (size == 1)
79 CF0_READ_PCI_CFG(bus, devfn, where, (u8 *) value, 3, 0xff);
80 else if (size == 2)
81 CF0_READ_PCI_CFG(bus, devfn, where, (u16 *) value, 2,
82 0xffff);
83 else
84 CF0_READ_PCI_CFG(bus, devfn, where, (u32 *) value, 0,
85 0xffffffff);
88 #define CF0_WRITE_PCI_CFG(bus,devfn,where,value,bm,mask) \
89 do { \
90 bridge_t *bridge; \
91 int slot = PCI_SLOT(devfn); \
92 int fn = PCI_FUNC(devfn); \
93 volatile u32 *addr; \
94 u32 cf, __bit; \
95 unsigned int bus_id = (unsigned) bus->number; \
97 bridge = (bridge_t *) NODE_SWIN_BASE(bus_to_nid[bus_id], \
98 bus_to_wid[bus_id]); \
100 __bit = (((where) & (bm)) << 3); \
101 addr = &bridge->b_type0_cfg_dev[slot].f[fn].l[where >> 2]; \
102 if (get_dbe(cf, addr)) \
103 return PCIBIOS_DEVICE_NOT_FOUND; \
104 cf &= (~mask); \
105 cf |= (value); \
106 put_dbe(cf, addr); \
107 return PCIBIOS_SUCCESSFUL; \
108 } while (0)
110 static int pci_conf0_write_config(struct pci_bus *bus, unsigned int devfn,
111 int where, int size, u32 value)
113 u32 vprod;
115 CF0_READ_PCI_CFG(bus, devfn, PCI_VENDOR_ID, &vprod, 0, 0xffffffff);
116 if (vprod == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16))
117 && ((where >= 0x14 && where < 0x40) || (where >= 0x48))) {
118 return PCIBIOS_SUCCESSFUL;
121 if (size == 1)
122 CF0_WRITE_PCI_CFG(bus, devfn, where, (u8) value, 3, 0xff);
123 else if (size == 2)
124 CF0_WRITE_PCI_CFG(bus, devfn, where, (u16) value, 2,
125 0xffff);
126 else
127 CF0_WRITE_PCI_CFG(bus, devfn, where, (u32) value, 0,
128 0xffffffff);
131 static struct pci_ops bridge_pci_ops = {
132 .read = pci_conf0_read_config,
133 .write = pci_conf0_write_config,
136 static int __init pcibios_init(void)
138 struct pci_ops *ops = &bridge_pci_ops;
139 int i;
141 ioport_resource.end = ~0UL;
143 for (i = 0; i < num_bridges; i++) {
144 printk("PCI: Probing PCI hardware on host bus %2d.\n", i);
145 pci_scan_bus(i, ops, NULL);
148 return 0;
151 subsys_initcall(pcibios_init);
153 static inline u8 bridge_swizzle(u8 pin, u8 slot)
155 return (((pin - 1) + slot) % 4) + 1;
158 static u8 __devinit pci_swizzle(struct pci_dev *dev, u8 * pinp)
160 u8 pin = *pinp;
162 while (dev->bus->self) { /* Move up the chain of bridges. */
163 pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
164 dev = dev->bus->self;
166 *pinp = pin;
168 return PCI_SLOT(dev->devfn);
172 * All observed requests have pin == 1. We could have a global here, that
173 * gets incremented and returned every time - unfortunately, pci_map_irq
174 * may be called on the same device over and over, and need to return the
175 * same value. On O2000, pin can be 0 or 1, and PCI slots can be [0..7].
177 * A given PCI device, in general, should be able to intr any of the cpus
178 * on any one of the hubs connected to its xbow.
180 static int __devinit pci_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
182 if ((dev->bus->number >= MAX_PCI_BUSSES)
183 || (pin != 1)
184 || (slot >= MAX_DEVICES_PER_PCIBUS))
185 panic("Increase supported PCI busses %d,%d,%d",
186 dev->bus->number, slot, pin);
189 * Already assigned? Then return previously assigned value ...
191 if (irqstore[dev->bus->number][slot])
192 return irqstore[dev->bus->number][slot];
194 irq_to_bus[lastirq] = dev->bus->number;
195 irq_to_slot[lastirq] = slot;
196 irqstore[dev->bus->number][slot] = lastirq;
197 lastirq++;
198 return lastirq - 1;
201 void __init pcibios_update_irq(struct pci_dev *dev, int irq)
203 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
206 void __devinit pcibios_fixup_bus(struct pci_bus *b)
208 pci_fixup_irqs(pci_swizzle, pci_map_irq);
211 int pcibios_enable_device(struct pci_dev *dev, int mask)
213 /* Not needed, since we enable all devices at startup. */
214 return 0;
217 void pcibios_align_resource(void *data, struct resource *res,
218 unsigned long size, unsigned long align)
222 unsigned int pcibios_assign_all_busses(void)
224 return 0;
227 char *__devinit pcibios_setup(char *str)
229 /* Nothing to do for now. */
231 return str;
235 * Device might live on a subordinate PCI bus. XXX Walk up the chain of buses
236 * to find the slot number in sense of the bridge device register.
237 * XXX This also means multiple devices might rely on conflicting bridge
238 * settings.
241 static void __init pci_disable_swapping(struct pci_dev *dev)
243 unsigned int bus_id = (unsigned) dev->bus->number;
244 bridge_t *bridge = (bridge_t *) NODE_SWIN_BASE(bus_to_nid[bus_id],
245 bus_to_wid[bus_id]);
246 int slot = PCI_SLOT(dev->devfn);
248 /* Turn off byte swapping */
249 bridge->b_device[slot].reg &= ~BRIDGE_DEV_SWAP_DIR;
250 bridge->b_widget.w_tflush; /* Flush */
253 static void __init pci_enable_swapping(struct pci_dev *dev)
255 unsigned int bus_id = (unsigned) dev->bus->number;
256 bridge_t *bridge = (bridge_t *) NODE_SWIN_BASE(bus_to_nid[bus_id],
257 bus_to_wid[bus_id]);
258 int slot = PCI_SLOT(dev->devfn);
260 /* Turn on byte swapping */
261 bridge->b_device[slot].reg |= BRIDGE_DEV_SWAP_DIR;
262 bridge->b_widget.w_tflush; /* Flush */
265 static void __init pci_fixup_ioc3(struct pci_dev *d)
267 unsigned long bus_id = (unsigned) d->bus->number;
269 printk("PCI: Fixing base addresses for IOC3 device %s\n",
270 d->slot_name);
272 d->resource[0].start |= NODE_OFFSET(bus_to_nid[bus_id]);
273 d->resource[0].end |= NODE_OFFSET(bus_to_nid[bus_id]);
275 pci_disable_swapping(d);
278 static void __init pci_fixup_isp1020(struct pci_dev *d)
280 unsigned short command;
282 d->resource[0].start |=
283 ((unsigned long) (bus_to_nid[d->bus->number]) << 32);
284 printk("PCI: Fixing isp1020 in [bus:slot.fn] %s\n", d->slot_name);
287 * Configure device to allow bus mastering, i/o and memory mapping.
288 * Older qlogicisp driver expects to have the IO space enable
289 * bit set. Things stop working if we program the controllers as not
290 * having PCI_COMMAND_MEMORY, so we have to fudge the mem_flags.
293 pci_set_master(d);
294 pci_read_config_word(d, PCI_COMMAND, &command);
295 command |= PCI_COMMAND_MEMORY;
296 command |= PCI_COMMAND_IO;
297 pci_write_config_word(d, PCI_COMMAND, command);
298 d->resource[1].flags |= 1;
300 pci_enable_swapping(d);
303 static void __init pci_fixup_isp2x00(struct pci_dev *d)
305 unsigned int bus_id = (unsigned) d->bus->number;
306 bridge_t *bridge = (bridge_t *) NODE_SWIN_BASE(bus_to_nid[bus_id],
307 bus_to_wid[bus_id]);
308 bridgereg_t devreg;
309 int i;
310 int slot = PCI_SLOT(d->devfn);
311 unsigned int start;
312 unsigned short command;
314 printk("PCI: Fixing isp2x00 in [bus:slot.fn] %s\n", d->slot_name);
316 /* set the resource struct for this device */
317 start = (u32) (u64) bridge; /* yes, we want to lose the upper 32 bits here */
318 start |= BRIDGE_DEVIO(slot);
320 d->resource[0].start = start;
321 d->resource[0].end = d->resource[0].start + 0xff;
322 d->resource[0].flags = IORESOURCE_IO;
324 d->resource[1].start = start;
325 d->resource[1].end = d->resource[0].start + 0xfff;
326 d->resource[1].flags = IORESOURCE_MEM;
329 * set the bridge device(x) reg for this device
331 devreg = bridge->b_device[slot].reg;
332 /* point device(x) to it appropriate small window */
333 devreg &= ~BRIDGE_DEV_OFF_MASK;
334 devreg |= (start >> 20) & BRIDGE_DEV_OFF_MASK;
335 bridge->b_device[slot].reg = devreg;
337 pci_enable_swapping(d);
339 /* set card's base addr reg */
340 //pci_write_config_dword(d, PCI_BASE_ADDRESS_0, 0x500001);
341 //pci_write_config_dword(d, PCI_BASE_ADDRESS_1, 0x8b00000);
342 //pci_write_config_dword(d, PCI_ROM_ADDRESS, 0x8b20000);
344 /* I got these from booting irix on system... */
345 pci_write_config_dword(d, PCI_BASE_ADDRESS_0, 0x200001);
346 //pci_write_config_dword(d, PCI_BASE_ADDRESS_1, 0xf800000);
347 pci_write_config_dword(d, PCI_ROM_ADDRESS, 0x10200000);
349 pci_write_config_dword(d, PCI_BASE_ADDRESS_1, start);
350 //pci_write_config_dword(d, PCI_ROM_ADDRESS, (start | 0x20000));
352 /* set cache line size */
353 pci_write_config_dword(d, PCI_CACHE_LINE_SIZE, 0xf080);
355 /* set pci bus timeout */
356 bridge->b_bus_timeout |= BRIDGE_BUS_PCI_RETRY_HLD(0x3);
357 bridge->b_wid_tflush;
358 printk("PCI: bridge bus timeout= 0x%x \n", bridge->b_bus_timeout);
360 /* set host error field */
361 bridge->b_int_host_err = 0x44;
362 bridge->b_wid_tflush;
364 bridge->b_wid_tflush; /* wait until Bridge PIO complete */
365 for (i = 0; i < 8; i++)
366 printk("PCI: device(%d)= 0x%x\n", i,
367 bridge->b_device[i].reg);
369 /* configure device to allow bus mastering, i/o and memory mapping */
370 pci_set_master(d);
371 pci_read_config_word(d, PCI_COMMAND, &command);
372 command |= PCI_COMMAND_MEMORY;
373 command |= PCI_COMMAND_IO;
374 pci_write_config_word(d, PCI_COMMAND, command);
375 /*d->resource[1].flags |= 1; */
378 struct pci_fixup pcibios_fixups[] = {
379 {PCI_FIXUP_HEADER, PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3,
380 pci_fixup_ioc3},
381 {PCI_FIXUP_HEADER, PCI_VENDOR_ID_QLOGIC,
382 PCI_DEVICE_ID_QLOGIC_ISP1020,
383 pci_fixup_isp1020},
384 {PCI_FIXUP_HEADER, PCI_VENDOR_ID_QLOGIC,
385 PCI_DEVICE_ID_QLOGIC_ISP2100,
386 pci_fixup_isp2x00},
387 {PCI_FIXUP_HEADER, PCI_VENDOR_ID_QLOGIC,
388 PCI_DEVICE_ID_QLOGIC_ISP2200,
389 pci_fixup_isp2x00},