File: /usr/src/linux/arch/sparc/kernel/ebus.c
1 /* $Id: ebus.c,v 1.17 2001/08/06 13:12:57 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 if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_name)) != 0) {
117 dev->num_irqs = 1;
118 } else if ((len = prom_getproperty(node, "interrupts",
119 (char *)&irqs, sizeof(irqs))) == -1 || len == 0) {
120 dev->num_irqs = 0;
121 dev->irqs[0] = 0;
122 if (dev->parent->num_irqs != 0) {
123 dev->num_irqs = 1;
124 dev->irqs[0] = dev->parent->irqs[0];
125 /* P3 */ /* printk("EBUS: dev %s irq %d from parent\n", dev->prom_name, dev->irqs[0]); */
126 }
127 } else {
128 dev->num_irqs = len / sizeof(irqs[0]);
129 if (irqs[0] == 0 || irqs[0] >= 8) {
130 /*
131 * XXX Zero is a valid pin number...
132 * This works as long as Ebus is not wired to INTA#.
133 */
134 printk("EBUS: %s got bad irq %d from PROM\n",
135 dev->prom_name, irqs[0]);
136 dev->num_irqs = 0;
137 dev->irqs[0] = 0;
138 } else {
139 dev->irqs[0] = pcic_pin_to_irq(irqs[0], dev->prom_name);
140 }
141 }
142 }
143
144 void __init fill_ebus_device(int node, struct linux_ebus_device *dev)
145 {
146 struct linux_prom_registers regs[PROMREG_MAX];
147 struct linux_ebus_child *child;
148 int irqs[PROMINTR_MAX];
149 char lbuf[128];
150 int i, n, len;
151 unsigned long baseaddr;
152
153 dev->prom_node = node;
154 prom_getstring(node, "name", lbuf, sizeof(lbuf));
155 strcpy(dev->prom_name, lbuf);
156
157 len = prom_getproperty(node, "reg", (void *)regs, sizeof(regs));
158 if (len % sizeof(struct linux_prom_registers)) {
159 prom_printf("UGH: proplen for %s was %d, need multiple of %d\n",
160 dev->prom_name, len,
161 (int)sizeof(struct linux_prom_registers));
162 panic(__FUNCTION__);
163 }
164 dev->num_addrs = len / sizeof(struct linux_prom_registers);
165
166 for (i = 0; i < dev->num_addrs; i++) {
167 /*
168 * XXX Collect JE-1 PROM
169 *
170 * Example - JS-E with 3.11:
171 * /ebus
172 * regs
173 * 0x00000000, 0x0, 0x00000000, 0x0, 0x00000000,
174 * 0x82000010, 0x0, 0xf0000000, 0x0, 0x01000000,
175 * 0x82000014, 0x0, 0x38800000, 0x0, 0x00800000,
176 * ranges
177 * 0x00, 0x00000000, 0x02000010, 0x0, 0x0, 0x01000000,
178 * 0x01, 0x01000000, 0x02000014, 0x0, 0x0, 0x00800000,
179 * /ebus/8042
180 * regs
181 * 0x00000001, 0x00300060, 0x00000008,
182 * 0x00000001, 0x00300060, 0x00000008,
183 */
184 n = regs[i].which_io;
185 if (n >= 4) {
186 /* XXX This is copied from old JE-1 by Gleb. */
187 n = (regs[i].which_io - 0x10) >> 2;
188 } else {
189 ;
190 }
191
192 /*
193 * XXX Now as we have regions, why don't we make an on-demand allocation...
194 */
195 dev->resource[i].start = 0;
196 if ((baseaddr = dev->bus->self->resource[n].start +
197 regs[i].phys_addr) != 0) {
198 /* dev->resource[i].name = dev->prom_name; */
199 if ((baseaddr = (unsigned long) ioremap(baseaddr,
200 regs[i].reg_size)) == 0) {
201 panic("ebus: unable to remap dev %s",
202 dev->prom_name);
203 }
204 }
205 dev->resource[i].start = baseaddr; /* XXX Unaligned */
206 }
207
208 if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_name)) != 0) {
209 dev->num_irqs = 1;
210 } else if ((len = prom_getproperty(node, "interrupts",
211 (char *)&irqs, sizeof(irqs))) == -1 || len == 0) {
212 dev->num_irqs = 0;
213 if ((dev->irqs[0] = dev->bus->self->irq) != 0) {
214 dev->num_irqs = 1;
215 /* P3 */ /* printk("EBUS: child %s irq %d from parent\n", dev->prom_name, dev->irqs[0]); */
216 }
217 } else {
218 dev->num_irqs = 1; /* dev->num_irqs = len / sizeof(irqs[0]); */
219 if (irqs[0] == 0 || irqs[0] >= 8) {
220 /* See above for the parent. XXX */
221 printk("EBUS: %s got bad irq %d from PROM\n",
222 dev->prom_name, irqs[0]);
223 dev->num_irqs = 0;
224 dev->irqs[0] = 0;
225 } else {
226 dev->irqs[0] = pcic_pin_to_irq(irqs[0], dev->prom_name);
227 }
228 }
229
230 if ((node = prom_getchild(node))) {
231 dev->children = (struct linux_ebus_child *)
232 ebus_alloc(sizeof(struct linux_ebus_child));
233
234 child = dev->children;
235 child->next = 0;
236 child->parent = dev;
237 child->bus = dev->bus;
238 fill_ebus_child(node, ®s[0], child);
239
240 while ((node = prom_getsibling(node))) {
241 child->next = (struct linux_ebus_child *)
242 ebus_alloc(sizeof(struct linux_ebus_child));
243
244 child = child->next;
245 child->next = 0;
246 child->parent = dev;
247 child->bus = dev->bus;
248 fill_ebus_child(node, ®s[0], child);
249 }
250 }
251 }
252
253 void __init ebus_init(void)
254 {
255 struct linux_prom_pci_registers regs[PROMREG_MAX];
256 struct linux_pbm_info *pbm;
257 struct linux_ebus_device *dev;
258 struct linux_ebus *ebus;
259 struct ebus_system_entry *sp;
260 struct pci_dev *pdev;
261 struct pcidev_cookie *cookie;
262 char lbuf[128];
263 unsigned long addr, *base;
264 unsigned short pci_command;
265 int nd, len, ebusnd;
266 int reg, nreg;
267 int num_ebus = 0;
268
269 if (!pci_present())
270 return;
271
272 prom_getstring(prom_root_node, "name", lbuf, sizeof(lbuf));
273 for (sp = ebus_blacklist; sp->esname != NULL; sp++) {
274 if (strcmp(lbuf, sp->esname) == 0) {
275 ebus_blackp = sp->ipt;
276 break;
277 }
278 }
279
280 pdev = pci_find_device(PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_EBUS, 0);
281 if (!pdev) {
282 return;
283 }
284 cookie = pdev->sysdata;
285 ebusnd = cookie->prom_node;
286
287 ebus_chain = ebus = (struct linux_ebus *)
288 ebus_alloc(sizeof(struct linux_ebus));
289 ebus->next = 0;
290
291 while (ebusnd) {
292
293 prom_getstring(ebusnd, "name", lbuf, sizeof(lbuf));
294 ebus->prom_node = ebusnd;
295 strcpy(ebus->prom_name, lbuf);
296 ebus->self = pdev;
297 ebus->parent = pbm = cookie->pbm;
298
299 /* Enable BUS Master. */
300 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
301 pci_command |= PCI_COMMAND_MASTER;
302 pci_write_config_word(pdev, PCI_COMMAND, pci_command);
303
304 len = prom_getproperty(ebusnd, "reg", (void *)regs,
305 sizeof(regs));
306 if (len == 0 || len == -1) {
307 prom_printf("%s: can't find reg property\n",
308 __FUNCTION__);
309 prom_halt();
310 }
311 nreg = len / sizeof(struct linux_prom_pci_registers);
312
313 base = &ebus->self->resource[0].start;
314 for (reg = 0; reg < nreg; reg++) {
315 if (!(regs[reg].which_io & 0x03000000))
316 continue;
317
318 addr = regs[reg].phys_lo;
319 *base++ = addr;
320 }
321
322 nd = prom_getchild(ebusnd);
323 if (!nd)
324 goto next_ebus;
325
326 ebus->devices = (struct linux_ebus_device *)
327 ebus_alloc(sizeof(struct linux_ebus_device));
328
329 dev = ebus->devices;
330 dev->next = 0;
331 dev->children = 0;
332 dev->bus = ebus;
333 fill_ebus_device(nd, dev);
334
335 while ((nd = prom_getsibling(nd))) {
336 dev->next = (struct linux_ebus_device *)
337 ebus_alloc(sizeof(struct linux_ebus_device));
338
339 dev = dev->next;
340 dev->next = 0;
341 dev->children = 0;
342 dev->bus = ebus;
343 fill_ebus_device(nd, dev);
344 }
345
346 next_ebus:
347 pdev = pci_find_device(PCI_VENDOR_ID_SUN,
348 PCI_DEVICE_ID_SUN_EBUS, pdev);
349 if (!pdev)
350 break;
351
352 cookie = pdev->sysdata;
353 ebusnd = cookie->prom_node;
354
355 ebus->next = (struct linux_ebus *)
356 ebus_alloc(sizeof(struct linux_ebus));
357 ebus = ebus->next;
358 ebus->next = 0;
359 ++num_ebus;
360 }
361
362 rs_init();
363 #ifdef CONFIG_SUN_AUXIO
364 auxio_probe();
365 #endif
366 }
367