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