42453760ab567b53b7ac936d33080dc275d92001
[linux-flexiantxendom0-3.2.10.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) 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>
17
18 /*
19  * Max #PCI busses we can handle; ie, max #PCI bridges.
20  */
21 #define MAX_PCI_BUSSES          40
22
23 /*
24  * Max #PCI devices (like scsi controllers) we handle on a bus.
25  */
26 #define MAX_DEVICES_PER_PCIBUS  8
27
28 /*
29  * No locking needed until PCI initialization is done parallely.
30  */
31 int irqstore[MAX_PCI_BUSSES][MAX_DEVICES_PER_PCIBUS];
32 int lastirq = BASE_PCI_IRQ;
33
34 /*
35  * Translate from irq to software PCI bus number and PCI slot.
36  */
37 int irq_to_bus[MAX_PCI_BUSSES * MAX_DEVICES_PER_PCIBUS];
38 int irq_to_slot[MAX_PCI_BUSSES * MAX_DEVICES_PER_PCIBUS];
39
40 /*
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.
45  */
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;                   \
54                                                                         \
55         bridge = (bridge_t *) NODE_SWIN_BASE(bus_to_nid[bus_id],        \
56                                              bus_to_wid[bus_id]);       \
57                                                                         \
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)
65
66 static int pci_conf0_read_config(struct pci_bus *bus, unsigned int devfn,
67                                  int where, int size, u32 * value)
68 {
69         u32 vprod;
70
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;
76         }
77
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);
86 }
87
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;                   \
96                                                                         \
97         bridge = (bridge_t *) NODE_SWIN_BASE(bus_to_nid[bus_id],        \
98                                              bus_to_wid[bus_id]);       \
99                                                                         \
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)
109
110 static int pci_conf0_write_config(struct pci_bus *bus, unsigned int devfn,
111                                   int where, int size, u32 value)
112 {
113         u32 vprod;
114
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;
119         }
120
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);
129 }
130
131 static struct pci_ops bridge_pci_ops = {
132         .read = pci_conf0_read_config,
133         .write = pci_conf0_write_config,
134 };
135
136 static int __init pcibios_init(void)
137 {
138         struct pci_ops *ops = &bridge_pci_ops;
139         int i;
140
141         ioport_resource.end = ~0UL;
142
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);
146         }
147
148         return 0;
149 }
150
151 subsys_initcall(pcibios_init);
152
153 static inline u8 bridge_swizzle(u8 pin, u8 slot)
154 {
155         return (((pin - 1) + slot) % 4) + 1;
156 }
157
158 static u8 __devinit pci_swizzle(struct pci_dev *dev, u8 * pinp)
159 {
160         u8 pin = *pinp;
161
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;
165         }
166         *pinp = pin;
167
168         return PCI_SLOT(dev->devfn);
169 }
170
171 /*
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].
176  *
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.
179  */
180 static int __devinit pci_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
181 {
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);
187
188         /*
189          * Already assigned? Then return previously assigned value ...
190          */
191         if (irqstore[dev->bus->number][slot])
192                 return irqstore[dev->bus->number][slot];
193
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;
199 }
200
201 void __init pcibios_update_irq(struct pci_dev *dev, int irq)
202 {
203         pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
204 }
205
206 void __devinit pcibios_fixup_bus(struct pci_bus *b)
207 {
208         pci_fixup_irqs(pci_swizzle, pci_map_irq);
209 }
210
211 int pcibios_enable_device(struct pci_dev *dev, int mask)
212 {
213         /* Not needed, since we enable all devices at startup.  */
214         return 0;
215 }
216
217 void pcibios_align_resource(void *data, struct resource *res,
218                             unsigned long size, unsigned long align)
219 {
220 }
221
222 unsigned int pcibios_assign_all_busses(void)
223 {
224         return 0;
225 }
226
227 char *__devinit pcibios_setup(char *str)
228 {
229         /* Nothing to do for now.  */
230
231         return str;
232 }
233
234 /*
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.
239  */
240
241 static void __init pci_disable_swapping(struct pci_dev *dev)
242 {
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);
247
248         /* Turn off byte swapping */
249         bridge->b_device[slot].reg &= ~BRIDGE_DEV_SWAP_DIR;
250         bridge->b_widget.w_tflush;      /* Flush */
251 }
252
253 static void __init pci_enable_swapping(struct pci_dev *dev)
254 {
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);
259
260         /* Turn on byte swapping */
261         bridge->b_device[slot].reg |= BRIDGE_DEV_SWAP_DIR;
262         bridge->b_widget.w_tflush;      /* Flush */
263 }
264
265 static void __init pci_fixup_ioc3(struct pci_dev *d)
266 {
267         unsigned long bus_id = (unsigned) d->bus->number;
268
269         printk("PCI: Fixing base addresses for IOC3 device %s\n",
270                d->slot_name);
271
272         d->resource[0].start |= NODE_OFFSET(bus_to_nid[bus_id]);
273         d->resource[0].end |= NODE_OFFSET(bus_to_nid[bus_id]);
274
275         pci_disable_swapping(d);
276 }
277
278 static void __init pci_fixup_isp1020(struct pci_dev *d)
279 {
280         unsigned short command;
281
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);
285
286         /*
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.
291          */
292
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;
299
300         pci_enable_swapping(d);
301 }
302
303 static void __init pci_fixup_isp2x00(struct pci_dev *d)
304 {
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;
313
314         printk("PCI: Fixing isp2x00 in [bus:slot.fn] %s\n", d->slot_name);
315
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);
319
320         d->resource[0].start = start;
321         d->resource[0].end = d->resource[0].start + 0xff;
322         d->resource[0].flags = IORESOURCE_IO;
323
324         d->resource[1].start = start;
325         d->resource[1].end = d->resource[0].start + 0xfff;
326         d->resource[1].flags = IORESOURCE_MEM;
327
328         /*
329          * set the bridge device(x) reg for this device
330          */
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;
336
337         pci_enable_swapping(d);
338
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);
343
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);
348
349         pci_write_config_dword(d, PCI_BASE_ADDRESS_1, start);
350         //pci_write_config_dword(d, PCI_ROM_ADDRESS, (start | 0x20000));
351
352         /* set cache line size */
353         pci_write_config_dword(d, PCI_CACHE_LINE_SIZE, 0xf080);
354
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);
359
360         /* set host error field */
361         bridge->b_int_host_err = 0x44;
362         bridge->b_wid_tflush;
363
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);
368
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; */
376 }
377
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},
390         {0}
391 };