# BRCM_VERSION=3
[bcm963xx.git] / kernel / linux / 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 extern unsigned int allocate_irqno(void);
21
22 /*
23  * Max #PCI busses we can handle; ie, max #PCI bridges.
24  */
25 #define MAX_PCI_BUSSES          40
26
27 /*
28  * Max #PCI devices (like scsi controllers) we handle on a bus.
29  */
30 #define MAX_DEVICES_PER_PCIBUS  8
31
32 /*
33  * XXX: No kmalloc available when we do our crosstalk scan,
34  *      we should try to move it later in the boot process.
35  */
36 static struct bridge_controller bridges[MAX_PCI_BUSSES];
37
38 /*
39  * Translate from irq to software PCI bus number and PCI slot.
40  */
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];
43
44 /*
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.
49  *
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.
53  */
54
55 static int pci_conf0_read_config(struct pci_bus *bus, unsigned int devfn,
56                                  int where, int size, u32 * value)
57 {
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);
62         volatile void *addr;
63         u32 cf, shift, mask;
64         int res;
65
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;
69
70         /*
71          * IOC3 is fucked fucked beyond believe ...  Don't even give the
72          * generic PCI code a chance to look at it for real ...
73          */
74         if (cf == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16)))
75                 goto oh_my_gawd;
76
77         addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[where ^ (4 - size)];
78
79         if (size == 1)
80                 res = get_dbe(*value, (u8 *) addr);
81         else if (size == 2)
82                 res = get_dbe(*value, (u16 *) addr);
83         else
84                 res = get_dbe(*value, (u32 *) addr);
85
86         return PCIBIOS_SUCCESSFUL;
87
88 oh_my_gawd:
89
90         /*
91          * IOC3 is fucked fucked beyond believe ...  Don't even give the
92          * generic PCI code a chance to look at the wrong register.
93          */
94         if ((where >= 0x14 && where < 0x40) || (where >= 0x48)) {
95                 *value = 0;
96                 return PCIBIOS_SUCCESSFUL;
97         }
98
99         /*
100          * IOC3 is fucked fucked beyond believe ...  Don't try to access
101          * anything but 32-bit words ...
102          */
103         addr = &bridge->b_type0_cfg_dev[slot].f[fn].l[where >> 2];
104
105         if (get_dbe(cf, (u32 *) addr))
106                 return PCIBIOS_DEVICE_NOT_FOUND;
107
108         shift = ((where & 3) << 3);
109         mask = (0xffffffffU >> ((4 - size) << 3));
110         *value = (cf >> shift) & mask;
111
112         return PCIBIOS_SUCCESSFUL;
113 }
114
115 static int pci_conf0_write_config(struct pci_bus *bus, unsigned int devfn,
116                                   int where, int size, u32 value)
117 {
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);
122         volatile void *addr;
123         u32 cf, shift, mask, smask;
124         int res;
125
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;
129
130         /*
131          * IOC3 is fucked fucked beyond believe ...  Don't even give the
132          * generic PCI code a chance to look at it for real ...
133          */
134         if (cf == (PCI_VENDOR_ID_SGI | (PCI_DEVICE_ID_SGI_IOC3 << 16)))
135                 goto oh_my_gawd;
136
137         addr = &bridge->b_type0_cfg_dev[slot].f[fn].c[where ^ (4 - size)];
138
139         if (size == 1) {
140                 res = put_dbe(value, (u8 *) addr);
141         } else if (size == 2) {
142                 res = put_dbe(value, (u16 *) addr);
143         } else {
144                 res = put_dbe(value, (u32 *) addr);
145         }
146
147         if (res)
148                 return PCIBIOS_DEVICE_NOT_FOUND;
149
150         return PCIBIOS_SUCCESSFUL;
151
152 oh_my_gawd:
153
154         /*
155          * IOC3 is fucked fucked beyond believe ...  Don't even give the
156          * generic PCI code a chance to touch the wrong register.
157          */
158         if ((where >= 0x14 && where < 0x40) || (where >= 0x48))
159                 return PCIBIOS_SUCCESSFUL;
160
161         /*
162          * IOC3 is fucked fucked beyond believe ...  Don't try to access
163          * anything but 32-bit words ...
164          */
165         addr = &bridge->b_type0_cfg_dev[slot].f[fn].l[where >> 2];
166
167         if (get_dbe(cf, (u32 *) addr))
168                 return PCIBIOS_DEVICE_NOT_FOUND;
169
170         shift = ((where & 3) << 3);
171         mask = (0xffffffffU >> ((4 - size) << 3));
172         smask = mask << shift;
173
174         cf = (cf & ~smask) | ((value & mask) << shift);
175         if (put_dbe(cf, (u32 *) addr))
176                 return PCIBIOS_DEVICE_NOT_FOUND;
177
178         return PCIBIOS_SUCCESSFUL;
179 }
180
181 static struct pci_ops bridge_pci_ops = {
182         .read = pci_conf0_read_config,
183         .write = pci_conf0_write_config,
184 };
185
186 int __init bridge_probe(nasid_t nasid, int widget_id, int masterwid)
187 {
188         unsigned long offset = NODE_OFFSET(nasid);
189         struct bridge_controller *bc;
190         static int num_bridges = 0;
191         bridge_t *bridge;
192         int slot;
193
194         printk("a bridge\n");
195
196         /* XXX: kludge alert.. */
197         if (!num_bridges)
198                 ioport_resource.end = ~0UL;
199
200         bc = &bridges[num_bridges++];
201
202         bc->pc.pci_ops          = &bridge_pci_ops;
203         bc->pc.mem_resource     = &bc->mem;
204         bc->pc.io_resource      = &bc->io;
205
206         bc->mem.name            = "Bridge PCI MEM";
207         bc->pc.mem_offset       = offset;
208         bc->mem.start           = 0;
209         bc->mem.end             = ~0UL;
210         bc->mem.flags           = IORESOURCE_MEM;
211
212         bc->io.name             = "Bridge IO MEM";
213         bc->pc.io_offset        = offset;
214         bc->io.start            = 0UL;
215         bc->io.end              = ~0UL;
216         bc->io.flags            = IORESOURCE_IO;
217
218         bc->irq_cpu = smp_processor_id();
219         bc->widget_id = widget_id;
220         bc->nasid = nasid;
221
222         bc->baddr = (u64)masterwid << 60;
223         bc->baddr |= (1UL << 56);       /* Barrier set */
224
225         /*
226          * point to this bridge
227          */
228         bridge = (bridge_t *) RAW_NODE_SWIN_BASE(nasid, widget_id);
229
230         /*
231          * Clear all pending interrupts.
232          */
233         bridge->b_int_rst_stat = BRIDGE_IRR_ALL_CLR;
234
235         /*
236          * Until otherwise set up, assume all interrupts are from slot 0
237          */
238         bridge->b_int_device = (u32) 0x0;
239
240         /*
241          * swap pio's to pci mem and io space (big windows)
242          */
243         bridge->b_wid_control |= BRIDGE_CTRL_IO_SWAP |
244                                  BRIDGE_CTRL_MEM_SWAP;
245
246         /*
247          * Hmm...  IRIX sets additional bits in the address which
248          * are documented as reserved in the bridge docs.
249          */
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;
254
255         for (slot = 0; slot < 8; slot ++)
256                 bridge->b_device[slot].reg |= BRIDGE_DEV_SWAP_DIR;
257         bridge->b_widget.w_tflush;      /* Flush */
258
259         bridge->b_wid_tflush;     /* wait until Bridge PIO complete */
260
261         bc->base = bridge;
262
263         register_pci_controller(&bc->pc);
264         return 0;
265 }
266
267 /*
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].
272  *
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.
275  */
276 int __devinit pcibios_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
277 {
278         struct bridge_controller *bc = BRIDGE_CONTROLLER(dev->bus);
279         int irq;
280
281         irq = allocate_irqno();
282
283         /*
284          * Argh...  This API doesn't handle with errors at all ...
285          */
286         if (irq == -1) {
287                 printk(KERN_ERR "Can't allocate interrupt for PCI device %s\n",
288                        pci_name(dev));
289                 return -1;
290         }
291
292         irq_to_bridge[irq] = bc;
293         irq_to_slot[irq] = slot;
294
295         return irq;
296 }
297
298 /*
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
302  * settings.
303  */
304
305 static inline void pci_disable_swapping(struct pci_dev *dev)
306 {
307         struct bridge_controller *bc = BRIDGE_CONTROLLER(dev->bus);
308         bridge_t *bridge = bc->base;
309         int slot = PCI_SLOT(dev->devfn);
310
311         /* Turn off byte swapping */
312         bridge->b_device[slot].reg &= ~BRIDGE_DEV_SWAP_DIR;
313         bridge->b_widget.w_tflush;      /* Flush */
314 }
315
316 static inline void pci_enable_swapping(struct pci_dev *dev)
317 {
318         struct bridge_controller *bc = BRIDGE_CONTROLLER(dev->bus);
319         bridge_t *bridge = bc->base;
320         int slot = PCI_SLOT(dev->devfn);
321
322         /* Turn on byte swapping */
323         bridge->b_device[slot].reg |= BRIDGE_DEV_SWAP_DIR;
324         bridge->b_widget.w_tflush;      /* Flush */
325 }
326
327 static void __init pci_fixup_ioc3(struct pci_dev *d)
328 {
329         pci_disable_swapping(d);
330 }
331
332 struct pci_fixup pcibios_fixups[] = {
333         {PCI_FIXUP_HEADER, PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3,
334          pci_fixup_ioc3},
335         {0}
336 };