ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / pci / pci-ip27.c
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.
5  *
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.
9  */
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>
19
20 /*
21  * Max #PCI busses we can handle; ie, max #PCI bridges.
22  */
23 #define MAX_PCI_BUSSES          40
24
25 /*
26  * Max #PCI devices (like scsi controllers) we handle on a bus.
27  */
28 #define MAX_DEVICES_PER_PCIBUS  8
29
30 /*
31  * XXX: No kmalloc available when we do our crosstalk scan,
32  *      we should try to move it later in the boot process.
33  */
34 static struct bridge_controller bridges[MAX_PCI_BUSSES];
35
36 /*
37  * Translate from irq to software PCI bus number and PCI slot.
38  */
39 struct bridge_controller *irq_to_bridge[MAX_PCI_BUSSES * MAX_DEVICES_PER_PCIBUS];
40 int irq_to_slot[MAX_PCI_BUSSES * MAX_DEVICES_PER_PCIBUS];
41
42 /*
43  * The Bridge ASIC supports both type 0 and type 1 access.  Type 1 is
44  * not really documented, so right now I can't write code which uses it.
45  * Therefore we use type 0 accesses for now even though they won't work
46  * correcly for PCI-to-PCI bridges.
47  *
48  * The function is complicated by the ultimate brokeness of the IOC3 chip
49  * which is used in SGI systems.  The IOC3 can only handle 32-bit PCI
50  * accesses and does only decode parts of it's address space.
51  */
52
53 static int pci_conf0_read_config(struct pci_bus *bus, unsigned int devfn,
54                                  int where, int size, u32 * value)
55 {
56         struct bridge_controller *bc = BRIDGE_CONTROLLER(bus);
57         bridge_t *bridge = bc->base;
58         int slot = PCI_SLOT(devfn);
59         int fn = PCI_FUNC(devfn);
60         volatile void *addr;
61         u32 cf, shift, mask;
62         int res;
63
64         addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[PCI_VENDOR_ID];
65         if (get_dbe(cf, (u32 *) addr))
66                 return PCIBIOS_DEVICE_NOT_FOUND;
67
68         /*
69          * IOC3 is fucked fucked beyond believe ...  Don't even give the
70          * generic PCI code a chance to look at it for real ...
71          */
72         if (cf == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16)))
73                 goto oh_my_gawd;
74
75         addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[where ^ (4 - size)];
76
77         if (size == 1)
78                 res = get_dbe(*value, (u8 *) addr);
79         else if (size == 2)
80                 res = get_dbe(*value, (u16 *) addr);
81         else
82                 res = get_dbe(*value, (u32 *) addr);
83
84         return PCIBIOS_SUCCESSFUL;
85
86 oh_my_gawd:
87
88         /*
89          * IOC3 is fucked fucked beyond believe ...  Don't even give the
90          * generic PCI code a chance to look at the wrong register.
91          */
92         if ((where >= 0x14 && where < 0x40) || (where >= 0x48)) {
93                 *value = 0;
94                 return PCIBIOS_SUCCESSFUL;
95         }
96
97         /*
98          * IOC3 is fucked fucked beyond believe ...  Don't try to access
99          * anything but 32-bit words ...
100          */
101         addr = &bridge->b_type0_cfg_dev[slot].f[fn].l[where >> 2];
102
103         if (get_dbe(cf, (u32 *) addr))
104                 return PCIBIOS_DEVICE_NOT_FOUND;
105
106         shift = ((where & 3) << 3);
107         mask = (0xffffffffU >> ((4 - size) << 3));
108         *value = (cf >> shift) & mask;
109
110         return PCIBIOS_SUCCESSFUL;
111 }
112
113 static int pci_conf0_write_config(struct pci_bus *bus, unsigned int devfn,
114                                   int where, int size, u32 value)
115 {
116         struct bridge_controller *bc = BRIDGE_CONTROLLER(bus);
117         bridge_t *bridge = bc->base;
118         int slot = PCI_SLOT(devfn);
119         int fn = PCI_FUNC(devfn);
120         volatile void *addr;
121         u32 cf, shift, mask, smask;
122         int res;
123
124         addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[PCI_VENDOR_ID];
125         if (get_dbe(cf, (u32 *) addr))
126                 return PCIBIOS_DEVICE_NOT_FOUND;
127
128         /*
129          * IOC3 is fucked fucked beyond believe ...  Don't even give the
130          * generic PCI code a chance to look at it for real ...
131          */
132         if (cf == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16)))
133                 goto oh_my_gawd;
134
135         addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[where ^ (4 - size)];
136
137         if (size == 1) {
138                 res = put_dbe(value, (u8 *) addr);
139         } else if (size == 2) {
140                 res = put_dbe(value, (u16 *) addr);
141         } else {
142                 res = put_dbe(value, (u32 *) addr);
143         }
144
145         if (res)
146                 return PCIBIOS_DEVICE_NOT_FOUND;
147
148         return PCIBIOS_SUCCESSFUL;
149
150 oh_my_gawd:
151
152         /*
153          * IOC3 is fucked fucked beyond believe ...  Don't even give the
154          * generic PCI code a chance to touch the wrong register.
155          */
156         if ((where >= 0x14 && where < 0x40) || (where >= 0x48))
157                 return PCIBIOS_SUCCESSFUL;
158
159         /*
160          * IOC3 is fucked fucked beyond believe ...  Don't try to access
161          * anything but 32-bit words ...
162          */
163         addr = &bridge->b_type0_cfg_dev[slot].f[fn].l[where >> 2];
164
165         if (get_dbe(cf, (u32 *) addr))
166                 return PCIBIOS_DEVICE_NOT_FOUND;
167
168         shift = ((where & 3) << 3);
169         mask = (0xffffffffU >> ((4 - size) << 3));
170         smask = mask << shift;
171
172         cf = (cf & ~smask) | ((value & mask) << shift);
173         if (put_dbe(cf, (u32 *) addr))
174                 return PCIBIOS_DEVICE_NOT_FOUND;
175
176         return PCIBIOS_SUCCESSFUL;
177 }
178
179 static struct pci_ops bridge_pci_ops = {
180         .read = pci_conf0_read_config,
181         .write = pci_conf0_write_config,
182 };
183
184 int __init bridge_probe(nasid_t nasid, int widget_id, int masterwid)
185 {
186         struct bridge_controller *bc;
187         bridge_t *bridge;
188         static int num_bridges = 0;
189
190         printk("a bridge\n");
191
192         /* XXX: kludge alert.. */
193         if (!num_bridges)
194                 ioport_resource.end = ~0UL;
195
196         bc = &bridges[num_bridges++];
197
198         bc->pc.pci_ops          = &bridge_pci_ops;
199         bc->pc.mem_resource     = &bc->mem;
200         bc->pc.io_resource      = &bc->io;
201
202         bc->mem.name            = "Bridge PCI MEM";
203         bc->pc.mem_offset       = 0;
204         bc->mem.start           = 0;
205         bc->mem.end             = ~0UL;
206         bc->mem.flags           = IORESOURCE_MEM;
207
208         bc->io.name             = "Bridge IO MEM";
209         bc->io.start            = 0UL;
210         bc->pc.io_offset        = 0UL;
211         bc->io.end              = ~0UL;
212         bc->io.flags            = IORESOURCE_IO;
213
214         bc->irq_cpu = smp_processor_id();
215         bc->widget_id = widget_id;
216         bc->nasid = nasid;
217
218         bc->baddr = (u64)masterwid << 60;
219         bc->baddr |= (1UL << 56);       /* Barrier set */
220
221         /*
222          * point to this bridge
223          */
224         bridge = (bridge_t *) RAW_NODE_SWIN_BASE(nasid, widget_id);
225
226         /*
227          * Clear all pending interrupts.
228          */
229         bridge->b_int_rst_stat = BRIDGE_IRR_ALL_CLR;
230
231         /*
232          * Until otherwise set up, assume all interrupts are from slot 0
233          */
234         bridge->b_int_device = (u32) 0x0;
235
236         /*
237          * swap pio's to pci mem and io space (big windows)
238          */
239         bridge->b_wid_control |= BRIDGE_CTRL_IO_SWAP |
240                                  BRIDGE_CTRL_MEM_SWAP;
241
242         /*
243          * Hmm...  IRIX sets additional bits in the address which
244          * are documented as reserved in the bridge docs.
245          */
246         bridge->b_wid_int_upper = 0x8000 | (masterwid << 16);
247         bridge->b_wid_int_lower = 0x01800090;   /* PI_INT_PEND_MOD off*/
248         bridge->b_dir_map = (masterwid << 20);  /* DMA */
249         bridge->b_int_enable = 0;
250
251         bridge->b_wid_tflush;     /* wait until Bridge PIO complete */
252
253         bc->base = bridge;
254
255         register_pci_controller(&bc->pc);
256         return 0;
257 }
258
259 /*
260  * All observed requests have pin == 1. We could have a global here, that
261  * gets incremented and returned every time - unfortunately, pci_map_irq
262  * may be called on the same device over and over, and need to return the
263  * same value. On O2000, pin can be 0 or 1, and PCI slots can be [0..7].
264  *
265  * A given PCI device, in general, should be able to intr any of the cpus
266  * on any one of the hubs connected to its xbow.
267  */
268 int __devinit pcibios_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
269 {
270         struct bridge_controller *bc = BRIDGE_CONTROLLER(dev->bus);
271         int irq;
272
273         irq = allocate_irqno();
274
275         /*
276          * Argh...  This API doesn't handle with errors at all ...
277          */
278         if (irq == -1) {
279                 printk(KERN_ERR "Can't allocate interrupt for PCI device %s\n",
280                        pci_name(dev));
281                 return -1;
282         }
283
284         irq_to_bridge[irq] = bc;
285         irq_to_slot[irq] = slot;
286
287         return irq;
288 }
289
290 /*
291  * Device might live on a subordinate PCI bus.  XXX Walk up the chain of buses
292  * to find the slot number in sense of the bridge device register.
293  * XXX This also means multiple devices might rely on conflicting bridge
294  * settings.
295  */
296
297 static void __init pci_disable_swapping(struct pci_dev *dev)
298 {
299         struct bridge_controller *bc = BRIDGE_CONTROLLER(dev->bus);
300         bridge_t *bridge = bc->base;
301         int slot = PCI_SLOT(dev->devfn);
302
303         /* Turn off byte swapping */
304         bridge->b_device[slot].reg &= ~BRIDGE_DEV_SWAP_DIR;
305         bridge->b_widget.w_tflush;      /* Flush */
306 }
307
308 static void __init pci_enable_swapping(struct pci_dev *dev)
309 {
310         struct bridge_controller *bc = BRIDGE_CONTROLLER(dev->bus);
311         bridge_t *bridge = bc->base;
312         int slot = PCI_SLOT(dev->devfn);
313
314         /* Turn on byte swapping */
315         bridge->b_device[slot].reg |= BRIDGE_DEV_SWAP_DIR;
316         bridge->b_widget.w_tflush;      /* Flush */
317 }
318
319 static void __init pci_fixup_ioc3(struct pci_dev *d)
320 {
321         struct bridge_controller *bc = BRIDGE_CONTROLLER(d->bus);
322         unsigned long offset = NODE_OFFSET(bc->nasid);
323
324         printk("PCI: Fixing base addresses for IOC3 device %s\n", pci_name(d));
325
326         d->resource[0].start |= offset;
327         d->resource[0].end |= offset;
328
329         pci_disable_swapping(d);
330 }
331
332 static void __init pci_fixup_isp1020(struct pci_dev *d)
333 {
334         struct bridge_controller *bc = BRIDGE_CONTROLLER(d->bus);
335         unsigned short command;
336
337         d->resource[0].start |= (unsigned long) bc->nasid << 32;
338         printk("PCI: Fixing isp1020 in [bus:slot.fn] %s\n", pci_name(d));
339
340         /*
341          * Configure device to allow bus mastering, i/o and memory mapping.
342          * Older qlogicisp driver expects to have the IO space enable
343          * bit set. Things stop working if we program the controllers as not
344          * having PCI_COMMAND_MEMORY, so we have to fudge the mem_flags.
345          */
346         pci_set_master(d);
347         pci_read_config_word(d, PCI_COMMAND, &command);
348         command |= PCI_COMMAND_MEMORY;
349         command |= PCI_COMMAND_IO;
350         pci_write_config_word(d, PCI_COMMAND, command);
351         d->resource[1].flags |= 1;
352
353         pci_enable_swapping(d);
354 }
355
356 static void __init pci_fixup_isp2x00(struct pci_dev *d)
357 {
358         struct bridge_controller *bc = BRIDGE_CONTROLLER(d->bus);
359         bridge_t *bridge = bc->base;
360         bridgereg_t devreg;
361         int i;
362         int slot = PCI_SLOT(d->devfn);
363         unsigned int start;
364         unsigned short command;
365
366         printk("PCI: Fixing isp2x00 in [bus:slot.fn] %s\n", pci_name(d));
367
368         /* set the resource struct for this device */
369         start = (u32) (u64) bridge;     /* yes, we want to lose the upper 32 bits here */
370         start |= BRIDGE_DEVIO(slot);
371
372         d->resource[0].start = start;
373         d->resource[0].end = d->resource[0].start + 0xff;
374         d->resource[0].flags = IORESOURCE_IO;
375
376         d->resource[1].start = start;
377         d->resource[1].end = d->resource[0].start + 0xfff;
378         d->resource[1].flags = IORESOURCE_MEM;
379
380         /*
381          * set the bridge device(x) reg for this device
382          */
383         devreg = bridge->b_device[slot].reg;
384         /* point device(x) to it appropriate small window */
385         devreg &= ~BRIDGE_DEV_OFF_MASK;
386         devreg |= (start >> 20) & BRIDGE_DEV_OFF_MASK;
387         bridge->b_device[slot].reg = devreg;
388
389         pci_enable_swapping(d);
390
391         /* set card's base addr reg */
392         //pci_write_config_dword(d, PCI_BASE_ADDRESS_0, 0x500001);
393         //pci_write_config_dword(d, PCI_BASE_ADDRESS_1, 0x8b00000);
394         //pci_write_config_dword(d, PCI_ROM_ADDRESS, 0x8b20000);
395
396         /* I got these from booting irix on system... */
397         pci_write_config_dword(d, PCI_BASE_ADDRESS_0, 0x200001);
398         //pci_write_config_dword(d, PCI_BASE_ADDRESS_1, 0xf800000);
399         pci_write_config_dword(d, PCI_ROM_ADDRESS, 0x10200000);
400
401         pci_write_config_dword(d, PCI_BASE_ADDRESS_1, start);
402         //pci_write_config_dword(d, PCI_ROM_ADDRESS, (start | 0x20000));
403
404         /* set cache line size */
405         pci_write_config_dword(d, PCI_CACHE_LINE_SIZE, 0xf080);
406
407         /* set pci bus timeout */
408         bridge->b_bus_timeout |= BRIDGE_BUS_PCI_RETRY_HLD(0x3);
409         bridge->b_wid_tflush;
410         printk("PCI: bridge bus timeout= 0x%x \n", bridge->b_bus_timeout);
411
412         /* set host error field */
413         bridge->b_int_host_err = 0x44;
414         bridge->b_wid_tflush;
415
416         bridge->b_wid_tflush;   /* wait until Bridge PIO complete */
417         for (i = 0; i < 8; i++)
418                 printk("PCI: device(%d)= 0x%x\n", i,
419                        bridge->b_device[i].reg);
420
421         /* configure device to allow bus mastering, i/o and memory mapping */
422         pci_set_master(d);
423         pci_read_config_word(d, PCI_COMMAND, &command);
424         command |= PCI_COMMAND_MEMORY;
425         command |= PCI_COMMAND_IO;
426         pci_write_config_word(d, PCI_COMMAND, command);
427         /*d->resource[1].flags |= 1; */
428 }
429
430 struct pci_fixup pcibios_fixups[] = {
431         {PCI_FIXUP_HEADER, PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3,
432          pci_fixup_ioc3},
433         {PCI_FIXUP_HEADER, PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1020,
434          pci_fixup_isp1020},
435         {PCI_FIXUP_HEADER, PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100,
436          pci_fixup_isp2x00},
437         {PCI_FIXUP_HEADER, PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200,
438          pci_fixup_isp2x00},
439         {0}
440 };