original comment: +Wilson03172004,marked due to this pci host does not support MWI
[linux-2.4.git] / arch / sparc / kernel / ebus.c
1 /* $Id: ebus.c,v 1.18.2.2 2002/01/05 01:12:31 davem Exp $
2  * ebus.c: PCI to EBus bridge device.
3  *
4  * Copyright (C) 1997  Eddie C. Dost  (ecd@skynet.be)
5  *
6  * Adopted for sparc by V. Roganov and G. Raiko.
7  * Fixes for different platforms by Pete Zaitcev.
8  */
9
10 #include <linux/config.h>
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16
17 #include <asm/system.h>
18 #include <asm/page.h>
19 #include <asm/pbm.h>
20 #include <asm/ebus.h>
21 #include <asm/io.h>
22 #include <asm/oplib.h>
23 #include <asm/bpp.h>
24
25 struct linux_ebus *ebus_chain = 0;
26
27 #ifdef CONFIG_SUN_AUXIO
28 extern void auxio_probe(void);
29 #endif
30 extern void rs_init(void);
31
32 /* We are together with pcic.c under CONFIG_PCI. */
33 extern unsigned int pcic_pin_to_irq(unsigned int, char *name);
34
35 /*
36  * IRQ Blacklist
37  * Here we list PROMs and systems that are known to supply crap as IRQ numbers.
38  */
39 struct ebus_device_irq {
40         char *name;
41         unsigned int pin;
42 };
43
44 struct ebus_system_entry {
45         char *esname;
46         struct ebus_device_irq *ipt;
47 };
48
49 static struct ebus_device_irq je1_1[] = {
50         { "8042",                3 },
51         { "SUNW,CS4231",         0 },
52         { "parallel",            0 },
53         { "se",                  2 },
54         { 0, 0 }
55 };
56
57 /*
58  * Gleb's JE1 supplied reasonable pin numbers, but mine did not (OBP 2.32).
59  * Blacklist the sucker... Note that Gleb's system will work.
60  */
61 static struct ebus_system_entry ebus_blacklist[] = {
62         { "SUNW,JavaEngine1", je1_1 },
63         { 0, 0 }
64 };
65
66 static struct ebus_device_irq *ebus_blackp = NULL;
67
68 /*
69  */
70 static inline unsigned long ebus_alloc(size_t size)
71 {
72         return (unsigned long)kmalloc(size, GFP_ATOMIC);
73 }
74
75 /*
76  */
77 int __init ebus_blacklist_irq(char *name)
78 {
79         struct ebus_device_irq *dp;
80
81         if ((dp = ebus_blackp) != NULL) {
82                 for (; dp->name != NULL; dp++) {
83                         if (strcmp(name, dp->name) == 0) {
84                                 return pcic_pin_to_irq(dp->pin, name);
85                         }
86                 }
87         }
88         return 0;
89 }
90
91 void __init fill_ebus_child(int node, struct linux_prom_registers *preg,
92                                 struct linux_ebus_child *dev)
93 {
94         int regs[PROMREG_MAX];
95         int irqs[PROMREG_MAX];
96         char lbuf[128];
97         int i, len;
98
99         dev->prom_node = node;
100         prom_getstring(node, "name", lbuf, sizeof(lbuf));
101         strcpy(dev->prom_name, lbuf);
102
103         len = prom_getproperty(node, "reg", (void *)regs, sizeof(regs));
104         if (len == -1) len = 0;
105         dev->num_addrs = len / sizeof(regs[0]);
106
107         for (i = 0; i < dev->num_addrs; i++) {
108                 if (regs[i] >= dev->parent->num_addrs) {
109                         prom_printf("UGH: property for %s was %d, need < %d\n",
110                                     dev->prom_name, len, dev->parent->num_addrs);
111                         panic(__FUNCTION__);
112                 }
113                 dev->resource[i].start = dev->parent->resource[regs[i]].start; /* XXX resource */
114         }
115
116         for (i = 0; i < PROMINTR_MAX; i++)
117                 dev->irqs[i] = PCI_IRQ_NONE;
118
119         if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_name)) != 0) {
120                 dev->num_irqs = 1;
121         } else if ((len = prom_getproperty(node, "interrupts",
122             (char *)&irqs, sizeof(irqs))) == -1 || len == 0) {
123                 dev->num_irqs = 0;
124                 dev->irqs[0] = 0;
125                 if (dev->parent->num_irqs != 0) {
126                         dev->num_irqs = 1;
127                         dev->irqs[0] = dev->parent->irqs[0];
128 /* P3 */ /* printk("EBUS: dev %s irq %d from parent\n", dev->prom_name, dev->irqs[0]); */
129                 }
130         } else {
131                 dev->num_irqs = len / sizeof(irqs[0]);
132                 if (irqs[0] == 0 || irqs[0] >= 8) {
133                         /*
134                          * XXX Zero is a valid pin number...
135                          * This works as long as Ebus is not wired to INTA#.
136                          */
137                         printk("EBUS: %s got bad irq %d from PROM\n",
138                             dev->prom_name, irqs[0]);
139                         dev->num_irqs = 0;
140                         dev->irqs[0] = 0;
141                 } else {
142                         dev->irqs[0] = pcic_pin_to_irq(irqs[0], dev->prom_name);
143                 }
144         }
145 }
146
147 void __init fill_ebus_device(int node, struct linux_ebus_device *dev)
148 {
149         struct linux_prom_registers regs[PROMREG_MAX];
150         struct linux_ebus_child *child;
151         int irqs[PROMINTR_MAX];
152         char lbuf[128];
153         int i, n, len;
154         unsigned long baseaddr;
155
156         dev->prom_node = node;
157         prom_getstring(node, "name", lbuf, sizeof(lbuf));
158         strcpy(dev->prom_name, lbuf);
159
160         len = prom_getproperty(node, "reg", (void *)regs, sizeof(regs));
161         if (len % sizeof(struct linux_prom_registers)) {
162                 prom_printf("UGH: proplen for %s was %d, need multiple of %d\n",
163                             dev->prom_name, len,
164                             (int)sizeof(struct linux_prom_registers));
165                 panic(__FUNCTION__);
166         }
167         dev->num_addrs = len / sizeof(struct linux_prom_registers);
168
169         for (i = 0; i < dev->num_addrs; i++) {
170                 /*
171                  * XXX Collect JE-1 PROM
172                  * 
173                  * Example - JS-E with 3.11:
174                  *  /ebus
175                  *      regs 
176                  *        0x00000000, 0x0, 0x00000000, 0x0, 0x00000000,
177                  *        0x82000010, 0x0, 0xf0000000, 0x0, 0x01000000,
178                  *        0x82000014, 0x0, 0x38800000, 0x0, 0x00800000,
179                  *      ranges
180                  *        0x00, 0x00000000, 0x02000010, 0x0, 0x0, 0x01000000,
181                  *        0x01, 0x01000000, 0x02000014, 0x0, 0x0, 0x00800000,
182                  *  /ebus/8042
183                  *      regs
184                  *        0x00000001, 0x00300060, 0x00000008,
185                  *        0x00000001, 0x00300060, 0x00000008,
186                  */
187                 n = regs[i].which_io;
188                 if (n >= 4) {
189                         /* XXX This is copied from old JE-1 by Gleb. */
190                         n = (regs[i].which_io - 0x10) >> 2;
191                 } else {
192                         ;
193                 }
194
195 /*
196  * XXX Now as we have regions, why don't we make an on-demand allocation...
197  */
198                 dev->resource[i].start = 0;
199                 if ((baseaddr = dev->bus->self->resource[n].start +
200                     regs[i].phys_addr) != 0) {
201                         /* dev->resource[i].name = dev->prom_name; */
202                         if ((baseaddr = (unsigned long) ioremap(baseaddr,
203                             regs[i].reg_size)) == 0) {
204                                 panic("ebus: unable to remap dev %s",
205                                     dev->prom_name);
206                         }
207                 }
208                 dev->resource[i].start = baseaddr;      /* XXX Unaligned */
209         }
210
211         for (i = 0; i < PROMINTR_MAX; i++)
212                 dev->irqs[i] = PCI_IRQ_NONE;
213
214         if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_name)) != 0) {
215                 dev->num_irqs = 1;
216         } else if ((len = prom_getproperty(node, "interrupts",
217             (char *)&irqs, sizeof(irqs))) == -1 || len == 0) {
218                 dev->num_irqs = 0;
219                 if ((dev->irqs[0] = dev->bus->self->irq) != 0) {
220                          dev->num_irqs = 1;
221 /* P3 */ /* printk("EBUS: child %s irq %d from parent\n", dev->prom_name, dev->irqs[0]); */
222                 }
223         } else {
224                 dev->num_irqs = 1;  /* dev->num_irqs = len / sizeof(irqs[0]); */
225                 if (irqs[0] == 0 || irqs[0] >= 8) {
226                         /* See above for the parent. XXX */
227                         printk("EBUS: %s got bad irq %d from PROM\n",
228                             dev->prom_name, irqs[0]);
229                         dev->num_irqs = 0;
230                         dev->irqs[0] = 0;
231                 } else {
232                         dev->irqs[0] = pcic_pin_to_irq(irqs[0], dev->prom_name);
233                 }
234         }
235
236         if ((node = prom_getchild(node))) {
237                 dev->children = (struct linux_ebus_child *)
238                         ebus_alloc(sizeof(struct linux_ebus_child));
239
240                 child = dev->children;
241                 child->next = 0;
242                 child->parent = dev;
243                 child->bus = dev->bus;
244                 fill_ebus_child(node, &regs[0], child);
245
246                 while ((node = prom_getsibling(node))) {
247                         child->next = (struct linux_ebus_child *)
248                                 ebus_alloc(sizeof(struct linux_ebus_child));
249
250                         child = child->next;
251                         child->next = 0;
252                         child->parent = dev;
253                         child->bus = dev->bus;
254                         fill_ebus_child(node, &regs[0], child);
255                 }
256         }
257 }
258
259 void __init ebus_init(void)
260 {
261         struct linux_prom_pci_registers regs[PROMREG_MAX];
262         struct linux_pbm_info *pbm;
263         struct linux_ebus_device *dev;
264         struct linux_ebus *ebus;
265         struct ebus_system_entry *sp;
266         struct pci_dev *pdev;
267         struct pcidev_cookie *cookie;
268         char lbuf[128];
269         unsigned long addr, *base;
270         unsigned short pci_command;
271         int nd, len, ebusnd;
272         int reg, nreg;
273         int num_ebus = 0;
274
275         if (!pci_present())
276                 return;
277
278         prom_getstring(prom_root_node, "name", lbuf, sizeof(lbuf));
279         for (sp = ebus_blacklist; sp->esname != NULL; sp++) {
280                 if (strcmp(lbuf, sp->esname) == 0) {
281                         ebus_blackp = sp->ipt;
282                         break;
283                 }
284         }
285
286         pdev = pci_find_device(PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_EBUS, 0);
287         if (!pdev) {
288                 return;
289         }
290         cookie = pdev->sysdata;
291         ebusnd = cookie->prom_node;
292
293         ebus_chain = ebus = (struct linux_ebus *)
294                         ebus_alloc(sizeof(struct linux_ebus));
295         ebus->next = 0;
296
297         while (ebusnd) {
298
299                 prom_getstring(ebusnd, "name", lbuf, sizeof(lbuf));
300                 ebus->prom_node = ebusnd;
301                 strcpy(ebus->prom_name, lbuf);
302                 ebus->self = pdev;
303                 ebus->parent = pbm = cookie->pbm;
304
305                 /* Enable BUS Master. */
306                 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
307                 pci_command |= PCI_COMMAND_MASTER;
308                 pci_write_config_word(pdev, PCI_COMMAND, pci_command);
309
310                 len = prom_getproperty(ebusnd, "reg", (void *)regs,
311                                        sizeof(regs));
312                 if (len == 0 || len == -1) {
313                         prom_printf("%s: can't find reg property\n",
314                                     __FUNCTION__);
315                         prom_halt();
316                 }
317                 nreg = len / sizeof(struct linux_prom_pci_registers);
318
319                 base = &ebus->self->resource[0].start;
320                 for (reg = 0; reg < nreg; reg++) {
321                         if (!(regs[reg].which_io & 0x03000000))
322                                 continue;
323
324                         addr = regs[reg].phys_lo;
325                         *base++ = addr;
326                 }
327
328                 nd = prom_getchild(ebusnd);
329                 if (!nd)
330                         goto next_ebus;
331
332                 ebus->devices = (struct linux_ebus_device *)
333                                 ebus_alloc(sizeof(struct linux_ebus_device));
334
335                 dev = ebus->devices;
336                 dev->next = 0;
337                 dev->children = 0;
338                 dev->bus = ebus;
339                 fill_ebus_device(nd, dev);
340
341                 while ((nd = prom_getsibling(nd))) {
342                         dev->next = (struct linux_ebus_device *)
343                                 ebus_alloc(sizeof(struct linux_ebus_device));
344
345                         dev = dev->next;
346                         dev->next = 0;
347                         dev->children = 0;
348                         dev->bus = ebus;
349                         fill_ebus_device(nd, dev);
350                 }
351
352         next_ebus:
353                 pdev = pci_find_device(PCI_VENDOR_ID_SUN,
354                                        PCI_DEVICE_ID_SUN_EBUS, pdev);
355                 if (!pdev)
356                         break;
357
358                 cookie = pdev->sysdata;
359                 ebusnd = cookie->prom_node;
360
361                 ebus->next = (struct linux_ebus *)
362                         ebus_alloc(sizeof(struct linux_ebus));
363                 ebus = ebus->next;
364                 ebus->next = 0;
365                 ++num_ebus;
366         }
367
368         rs_init();
369 #ifdef CONFIG_SUN_AUXIO
370         auxio_probe();
371 #endif
372 }