blob: ac352eb6dff3beb4753cefc885078c87284c053e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $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
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/kernel.h>
11#include <linux/types.h>
12#include <linux/init.h>
13#include <linux/slab.h>
14#include <linux/string.h>
15
16#include <asm/system.h>
17#include <asm/page.h>
18#include <asm/pbm.h>
19#include <asm/ebus.h>
20#include <asm/io.h>
21#include <asm/oplib.h>
David S. Miller942a6bd2006-06-23 15:53:31 -070022#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/bpp.h>
24
Al Viroe4fe3422005-12-04 18:48:45 -050025struct linux_ebus *ebus_chain = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/* We are together with pcic.c under CONFIG_PCI. */
Stephen Rothwellee5ac9d2007-04-26 00:03:53 -070028extern unsigned int pcic_pin_to_irq(unsigned int, const char *name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
31 * IRQ Blacklist
32 * Here we list PROMs and systems that are known to supply crap as IRQ numbers.
33 */
34struct ebus_device_irq {
35 char *name;
36 unsigned int pin;
37};
38
39struct ebus_system_entry {
40 char *esname;
41 struct ebus_device_irq *ipt;
42};
43
44static struct ebus_device_irq je1_1[] = {
45 { "8042", 3 },
46 { "SUNW,CS4231", 0 },
47 { "parallel", 0 },
48 { "se", 2 },
Al Viroe4fe3422005-12-04 18:48:45 -050049 { NULL, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
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 */
56static struct ebus_system_entry ebus_blacklist[] = {
57 { "SUNW,JavaEngine1", je1_1 },
Al Viroe4fe3422005-12-04 18:48:45 -050058 { NULL, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
61static struct ebus_device_irq *ebus_blackp = NULL;
62
63/*
64 */
65static inline unsigned long ebus_alloc(size_t size)
66{
67 return (unsigned long)kmalloc(size, GFP_ATOMIC);
68}
69
70/*
71 */
Stephen Rothwellee5ac9d2007-04-26 00:03:53 -070072int __init ebus_blacklist_irq(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
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
David S. Miller942a6bd2006-06-23 15:53:31 -070086void __init fill_ebus_child(struct device_node *dp,
87 struct linux_ebus_child *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Stephen Rothwell8271f042007-03-29 00:47:23 -070089 const int *regs;
90 const int *irqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 int i, len;
92
David S. Miller942a6bd2006-06-23 15:53:31 -070093 dev->prom_node = dp;
94 regs = of_get_property(dp, "reg", &len);
95 if (!regs)
96 len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 dev->num_addrs = len / sizeof(regs[0]);
98
99 for (i = 0; i < dev->num_addrs; i++) {
100 if (regs[i] >= dev->parent->num_addrs) {
101 prom_printf("UGH: property for %s was %d, need < %d\n",
David S. Miller942a6bd2006-06-23 15:53:31 -0700102 dev->prom_node->name, len,
103 dev->parent->num_addrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 panic(__FUNCTION__);
105 }
David S. Miller942a6bd2006-06-23 15:53:31 -0700106
107 /* XXX resource */
108 dev->resource[i].start =
109 dev->parent->resource[regs[i]].start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111
112 for (i = 0; i < PROMINTR_MAX; i++)
113 dev->irqs[i] = PCI_IRQ_NONE;
114
David S. Miller942a6bd2006-06-23 15:53:31 -0700115 if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_node->name)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 dev->num_irqs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 } else {
David S. Miller942a6bd2006-06-23 15:53:31 -0700118 irqs = of_get_property(dp, "interrupts", &len);
119 if (!irqs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 dev->num_irqs = 0;
121 dev->irqs[0] = 0;
David S. Miller942a6bd2006-06-23 15:53:31 -0700122 if (dev->parent->num_irqs != 0) {
123 dev->num_irqs = 1;
124 dev->irqs[0] = dev->parent->irqs[0];
125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 } else {
David S. Miller942a6bd2006-06-23 15:53:31 -0700127 dev->num_irqs = len / sizeof(irqs[0]);
128 if (irqs[0] == 0 || irqs[0] >= 8) {
129 /*
130 * XXX Zero is a valid pin number...
131 * This works as long as Ebus is not wired
132 * to INTA#.
133 */
134 printk("EBUS: %s got bad irq %d from PROM\n",
135 dev->prom_node->name, irqs[0]);
136 dev->num_irqs = 0;
137 dev->irqs[0] = 0;
138 } else {
139 dev->irqs[0] =
140 pcic_pin_to_irq(irqs[0],
141 dev->prom_node->name);
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144 }
145}
146
David S. Miller942a6bd2006-06-23 15:53:31 -0700147void __init fill_ebus_device(struct device_node *dp, struct linux_ebus_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Stephen Rothwell8271f042007-03-29 00:47:23 -0700149 const struct linux_prom_registers *regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct linux_ebus_child *child;
David S. Miller3d6e4702007-07-18 22:03:25 -0700151 struct dev_archdata *sd;
Stephen Rothwell8271f042007-03-29 00:47:23 -0700152 const int *irqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int i, n, len;
154 unsigned long baseaddr;
155
David S. Miller942a6bd2006-06-23 15:53:31 -0700156 dev->prom_node = dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
David S. Miller942a6bd2006-06-23 15:53:31 -0700158 regs = of_get_property(dp, "reg", &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (len % sizeof(struct linux_prom_registers)) {
160 prom_printf("UGH: proplen for %s was %d, need multiple of %d\n",
David S. Miller942a6bd2006-06-23 15:53:31 -0700161 dev->prom_node->name, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 (int)sizeof(struct linux_prom_registers));
163 panic(__FUNCTION__);
164 }
165 dev->num_addrs = len / sizeof(struct linux_prom_registers);
166
167 for (i = 0; i < dev->num_addrs; i++) {
168 /*
169 * XXX Collect JE-1 PROM
170 *
171 * Example - JS-E with 3.11:
172 * /ebus
173 * regs
174 * 0x00000000, 0x0, 0x00000000, 0x0, 0x00000000,
175 * 0x82000010, 0x0, 0xf0000000, 0x0, 0x01000000,
176 * 0x82000014, 0x0, 0x38800000, 0x0, 0x00800000,
177 * ranges
178 * 0x00, 0x00000000, 0x02000010, 0x0, 0x0, 0x01000000,
179 * 0x01, 0x01000000, 0x02000014, 0x0, 0x0, 0x00800000,
180 * /ebus/8042
181 * regs
182 * 0x00000001, 0x00300060, 0x00000008,
183 * 0x00000001, 0x00300060, 0x00000008,
184 */
185 n = regs[i].which_io;
186 if (n >= 4) {
187 /* XXX This is copied from old JE-1 by Gleb. */
188 n = (regs[i].which_io - 0x10) >> 2;
189 } else {
190 ;
191 }
192
193/*
194 * XXX Now as we have regions, why don't we make an on-demand allocation...
195 */
196 dev->resource[i].start = 0;
197 if ((baseaddr = dev->bus->self->resource[n].start +
198 regs[i].phys_addr) != 0) {
199 /* dev->resource[i].name = dev->prom_name; */
200 if ((baseaddr = (unsigned long) ioremap(baseaddr,
201 regs[i].reg_size)) == 0) {
202 panic("ebus: unable to remap dev %s",
David S. Miller942a6bd2006-06-23 15:53:31 -0700203 dev->prom_node->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205 }
206 dev->resource[i].start = baseaddr; /* XXX Unaligned */
207 }
208
209 for (i = 0; i < PROMINTR_MAX; i++)
210 dev->irqs[i] = PCI_IRQ_NONE;
211
David S. Miller942a6bd2006-06-23 15:53:31 -0700212 if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_node->name)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 dev->num_irqs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 } else {
David S. Miller942a6bd2006-06-23 15:53:31 -0700215 irqs = of_get_property(dp, "interrupts", &len);
216 if (!irqs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 dev->num_irqs = 0;
David S. Miller942a6bd2006-06-23 15:53:31 -0700218 if ((dev->irqs[0] = dev->bus->self->irq) != 0) {
219 dev->num_irqs = 1;
220/* P3 */ /* printk("EBUS: child %s irq %d from parent\n", dev->prom_name, dev->irqs[0]); */
221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 } else {
David S. Miller942a6bd2006-06-23 15:53:31 -0700223 dev->num_irqs = 1; /* dev->num_irqs = len / sizeof(irqs[0]); */
224 if (irqs[0] == 0 || irqs[0] >= 8) {
225 /* See above for the parent. XXX */
226 printk("EBUS: %s got bad irq %d from PROM\n",
227 dev->prom_node->name, irqs[0]);
228 dev->num_irqs = 0;
229 dev->irqs[0] = 0;
230 } else {
231 dev->irqs[0] =
232 pcic_pin_to_irq(irqs[0],
233 dev->prom_node->name);
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 }
237
David S. Miller3d6e4702007-07-18 22:03:25 -0700238 sd = &dev->ofdev.dev.archdata;
239 sd->prom_node = dp;
240 sd->op = &dev->ofdev;
241
David S. Millerfd531432006-06-23 15:55:17 -0700242 dev->ofdev.node = dp;
243 dev->ofdev.dev.parent = &dev->bus->ofdev.dev;
244 dev->ofdev.dev.bus = &ebus_bus_type;
David S. Millerf5ef9d12006-10-27 01:03:31 -0700245 sprintf(dev->ofdev.dev.bus_id, "ebus[%08x]", dp->node);
David S. Millerfd531432006-06-23 15:55:17 -0700246
247 /* Register with core */
248 if (of_device_register(&dev->ofdev) != 0)
249 printk(KERN_DEBUG "ebus: device registration error for %s!\n",
David S. Millerf5ef9d12006-10-27 01:03:31 -0700250 dp->path_component_name);
David S. Millerfd531432006-06-23 15:55:17 -0700251
David S. Miller942a6bd2006-06-23 15:53:31 -0700252 if ((dp = dp->child) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 dev->children = (struct linux_ebus_child *)
254 ebus_alloc(sizeof(struct linux_ebus_child));
255
256 child = dev->children;
Al Viroe4fe3422005-12-04 18:48:45 -0500257 child->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 child->parent = dev;
259 child->bus = dev->bus;
David S. Miller942a6bd2006-06-23 15:53:31 -0700260 fill_ebus_child(dp, child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
David S. Miller942a6bd2006-06-23 15:53:31 -0700262 while ((dp = dp->sibling) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 child->next = (struct linux_ebus_child *)
264 ebus_alloc(sizeof(struct linux_ebus_child));
265
266 child = child->next;
Al Viroe4fe3422005-12-04 18:48:45 -0500267 child->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 child->parent = dev;
269 child->bus = dev->bus;
David S. Miller942a6bd2006-06-23 15:53:31 -0700270 fill_ebus_child(dp, child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 }
273}
274
275void __init ebus_init(void)
276{
Stephen Rothwell8271f042007-03-29 00:47:23 -0700277 const struct linux_prom_pci_registers *regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct linux_pbm_info *pbm;
279 struct linux_ebus_device *dev;
280 struct linux_ebus *ebus;
281 struct ebus_system_entry *sp;
282 struct pci_dev *pdev;
283 struct pcidev_cookie *cookie;
David S. Miller942a6bd2006-06-23 15:53:31 -0700284 struct device_node *dp;
Al Virocc9bd992006-09-23 01:18:41 +0100285 struct resource *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 unsigned short pci_command;
David S. Miller942a6bd2006-06-23 15:53:31 -0700287 int len, reg, nreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int num_ebus = 0;
289
David S. Miller942a6bd2006-06-23 15:53:31 -0700290 dp = of_find_node_by_path("/");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 for (sp = ebus_blacklist; sp->esname != NULL; sp++) {
David S. Miller942a6bd2006-06-23 15:53:31 -0700292 if (strcmp(dp->name, sp->esname) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 ebus_blackp = sp->ipt;
294 break;
295 }
296 }
297
Al Viroe4fe3422005-12-04 18:48:45 -0500298 pdev = pci_get_device(PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_EBUS, NULL);
David S. Miller942a6bd2006-06-23 15:53:31 -0700299 if (!pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return;
David S. Miller942a6bd2006-06-23 15:53:31 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 cookie = pdev->sysdata;
David S. Miller942a6bd2006-06-23 15:53:31 -0700303 dp = cookie->prom_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 ebus_chain = ebus = (struct linux_ebus *)
306 ebus_alloc(sizeof(struct linux_ebus));
Al Viroe4fe3422005-12-04 18:48:45 -0500307 ebus->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
David S. Miller942a6bd2006-06-23 15:53:31 -0700309 while (dp) {
310 struct device_node *nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
David S. Miller942a6bd2006-06-23 15:53:31 -0700312 ebus->prom_node = dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 ebus->self = pdev;
314 ebus->parent = pbm = cookie->pbm;
315
316 /* Enable BUS Master. */
317 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
318 pci_command |= PCI_COMMAND_MASTER;
319 pci_write_config_word(pdev, PCI_COMMAND, pci_command);
320
David S. Miller942a6bd2006-06-23 15:53:31 -0700321 regs = of_get_property(dp, "reg", &len);
322 if (!regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 prom_printf("%s: can't find reg property\n",
324 __FUNCTION__);
325 prom_halt();
326 }
327 nreg = len / sizeof(struct linux_prom_pci_registers);
328
Al Virocc9bd992006-09-23 01:18:41 +0100329 p = &ebus->self->resource[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 for (reg = 0; reg < nreg; reg++) {
331 if (!(regs[reg].which_io & 0x03000000))
332 continue;
333
Al Virocc9bd992006-09-23 01:18:41 +0100334 (p++)->start = regs[reg].phys_lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
David S. Millerfd531432006-06-23 15:55:17 -0700337 ebus->ofdev.node = dp;
338 ebus->ofdev.dev.parent = &pdev->dev;
339 ebus->ofdev.dev.bus = &ebus_bus_type;
David S. Millerf5ef9d12006-10-27 01:03:31 -0700340 sprintf(ebus->ofdev.dev.bus_id, "ebus%d", num_ebus);
David S. Millerfd531432006-06-23 15:55:17 -0700341
342 /* Register with core */
343 if (of_device_register(&ebus->ofdev) != 0)
344 printk(KERN_DEBUG "ebus: device registration error for %s!\n",
David S. Millerf5ef9d12006-10-27 01:03:31 -0700345 dp->path_component_name);
David S. Millerfd531432006-06-23 15:55:17 -0700346
347
David S. Miller942a6bd2006-06-23 15:53:31 -0700348 nd = dp->child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (!nd)
350 goto next_ebus;
351
352 ebus->devices = (struct linux_ebus_device *)
353 ebus_alloc(sizeof(struct linux_ebus_device));
354
355 dev = ebus->devices;
Al Viroe4fe3422005-12-04 18:48:45 -0500356 dev->next = NULL;
357 dev->children = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 dev->bus = ebus;
359 fill_ebus_device(nd, dev);
360
David S. Miller942a6bd2006-06-23 15:53:31 -0700361 while ((nd = nd->sibling) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 dev->next = (struct linux_ebus_device *)
363 ebus_alloc(sizeof(struct linux_ebus_device));
364
365 dev = dev->next;
Al Viroe4fe3422005-12-04 18:48:45 -0500366 dev->next = NULL;
367 dev->children = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 dev->bus = ebus;
369 fill_ebus_device(nd, dev);
370 }
371
372 next_ebus:
373 pdev = pci_get_device(PCI_VENDOR_ID_SUN,
374 PCI_DEVICE_ID_SUN_EBUS, pdev);
375 if (!pdev)
376 break;
377
378 cookie = pdev->sysdata;
David S. Miller942a6bd2006-06-23 15:53:31 -0700379 dp = cookie->prom_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 ebus->next = (struct linux_ebus *)
382 ebus_alloc(sizeof(struct linux_ebus));
383 ebus = ebus->next;
Al Viroe4fe3422005-12-04 18:48:45 -0500384 ebus->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 ++num_ebus;
386 }
387 if (pdev)
388 pci_dev_put(pdev);
389}