1/* $Id: ebus.c,v 1.1.1.1 2007/08/03 18:52:17 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/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>
22#include <asm/prom.h>
23#include <asm/bpp.h>
24
25struct linux_ebus *ebus_chain = NULL;
26
27/* We are together with pcic.c under CONFIG_PCI. */
28extern unsigned int pcic_pin_to_irq(unsigned int, const char *name);
29
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 },
49	{ NULL, 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 */
56static struct ebus_system_entry ebus_blacklist[] = {
57	{ "SUNW,JavaEngine1", je1_1 },
58	{ NULL, NULL }
59};
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 */
72int __init ebus_blacklist_irq(const 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
86void __init fill_ebus_child(struct device_node *dp,
87			    struct linux_ebus_child *dev)
88{
89	const int *regs;
90	const int *irqs;
91	int i, len;
92
93	dev->prom_node = dp;
94	regs = of_get_property(dp, "reg", &len);
95	if (!regs)
96		len = 0;
97	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",
102				    dev->prom_node->name, len,
103				    dev->parent->num_addrs);
104			panic(__FUNCTION__);
105		}
106
107		dev->resource[i].start =
108			dev->parent->resource[regs[i]].start;
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_node->name)) != 0) {
115		dev->num_irqs = 1;
116	} else {
117		irqs = of_get_property(dp, "interrupts", &len);
118		if (!irqs) {
119			dev->num_irqs = 0;
120			dev->irqs[0] = 0;
121			if (dev->parent->num_irqs != 0) {
122				dev->num_irqs = 1;
123				dev->irqs[0] = dev->parent->irqs[0];
124			}
125		} else {
126			dev->num_irqs = len / sizeof(irqs[0]);
127			if (irqs[0] == 0 || irqs[0] >= 8) {
128				printk("EBUS: %s got bad irq %d from PROM\n",
129				       dev->prom_node->name, irqs[0]);
130				dev->num_irqs = 0;
131				dev->irqs[0] = 0;
132			} else {
133				dev->irqs[0] =
134					pcic_pin_to_irq(irqs[0],
135							dev->prom_node->name);
136			}
137		}
138	}
139}
140
141void __init fill_ebus_device(struct device_node *dp, struct linux_ebus_device *dev)
142{
143	const struct linux_prom_registers *regs;
144	struct linux_ebus_child *child;
145	const int *irqs;
146	int i, n, len;
147	unsigned long baseaddr;
148
149	dev->prom_node = dp;
150
151	regs = of_get_property(dp, "reg", &len);
152	if (len % sizeof(struct linux_prom_registers)) {
153		prom_printf("UGH: proplen for %s was %d, need multiple of %d\n",
154			    dev->prom_node->name, len,
155			    (int)sizeof(struct linux_prom_registers));
156		panic(__FUNCTION__);
157	}
158	dev->num_addrs = len / sizeof(struct linux_prom_registers);
159
160	for (i = 0; i < dev->num_addrs; i++) {
161		n = regs[i].which_io;
162		if (n >= 4) {
163			n = (regs[i].which_io - 0x10) >> 2;
164		} else {
165			;
166		}
167
168		dev->resource[i].start = 0;
169		if ((baseaddr = dev->bus->self->resource[n].start +
170		    regs[i].phys_addr) != 0) {
171			/* dev->resource[i].name = dev->prom_name; */
172			if ((baseaddr = (unsigned long) ioremap(baseaddr,
173			    regs[i].reg_size)) == 0) {
174				panic("ebus: unable to remap dev %s",
175				      dev->prom_node->name);
176			}
177		}
178		dev->resource[i].start = baseaddr;
179	}
180
181	for (i = 0; i < PROMINTR_MAX; i++)
182		dev->irqs[i] = PCI_IRQ_NONE;
183
184	if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_node->name)) != 0) {
185		dev->num_irqs = 1;
186	} else {
187		irqs = of_get_property(dp, "interrupts", &len);
188		if (!irqs) {
189			dev->num_irqs = 0;
190			if ((dev->irqs[0] = dev->bus->self->irq) != 0) {
191				dev->num_irqs = 1;
192/* P3 */ /* printk("EBUS: child %s irq %d from parent\n", dev->prom_name, dev->irqs[0]); */
193			}
194		} else {
195			dev->num_irqs = 1;  /* dev->num_irqs = len / sizeof(irqs[0]); */
196			if (irqs[0] == 0 || irqs[0] >= 8) {
197				printk("EBUS: %s got bad irq %d from PROM\n",
198				       dev->prom_node->name, irqs[0]);
199				dev->num_irqs = 0;
200				dev->irqs[0] = 0;
201			} else {
202				dev->irqs[0] =
203					pcic_pin_to_irq(irqs[0],
204							dev->prom_node->name);
205			}
206		}
207	}
208
209	dev->ofdev.node = dp;
210	dev->ofdev.dev.parent = &dev->bus->ofdev.dev;
211	dev->ofdev.dev.bus = &ebus_bus_type;
212	sprintf(dev->ofdev.dev.bus_id, "ebus[%08x]", dp->node);
213
214	/* Register with core */
215	if (of_device_register(&dev->ofdev) != 0)
216		printk(KERN_DEBUG "ebus: device registration error for %s!\n",
217		       dp->path_component_name);
218
219	if ((dp = dp->child) != NULL) {
220		dev->children = (struct linux_ebus_child *)
221			ebus_alloc(sizeof(struct linux_ebus_child));
222
223		child = dev->children;
224		child->next = NULL;
225		child->parent = dev;
226		child->bus = dev->bus;
227		fill_ebus_child(dp, child);
228
229		while ((dp = dp->sibling) != NULL) {
230			child->next = (struct linux_ebus_child *)
231				ebus_alloc(sizeof(struct linux_ebus_child));
232
233			child = child->next;
234			child->next = NULL;
235			child->parent = dev;
236			child->bus = dev->bus;
237			fill_ebus_child(dp, child);
238		}
239	}
240}
241
242void __init ebus_init(void)
243{
244	const struct linux_prom_pci_registers *regs;
245	struct linux_pbm_info *pbm;
246	struct linux_ebus_device *dev;
247	struct linux_ebus *ebus;
248	struct ebus_system_entry *sp;
249	struct pci_dev *pdev;
250	struct pcidev_cookie *cookie;
251	struct device_node *dp;
252	struct resource *p;
253	unsigned short pci_command;
254	int len, reg, nreg;
255	int num_ebus = 0;
256
257	dp = of_find_node_by_path("/");
258	for (sp = ebus_blacklist; sp->esname != NULL; sp++) {
259		if (strcmp(dp->name, sp->esname) == 0) {
260			ebus_blackp = sp->ipt;
261			break;
262		}
263	}
264
265	pdev = pci_get_device(PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_EBUS, NULL);
266	if (!pdev)
267		return;
268
269	cookie = pdev->sysdata;
270	dp = cookie->prom_node;
271
272	ebus_chain = ebus = (struct linux_ebus *)
273			ebus_alloc(sizeof(struct linux_ebus));
274	ebus->next = NULL;
275
276	while (dp) {
277		struct device_node *nd;
278
279		ebus->prom_node = dp;
280		ebus->self = pdev;
281		ebus->parent = pbm = cookie->pbm;
282
283		/* Enable BUS Master. */
284		pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
285		pci_command |= PCI_COMMAND_MASTER;
286		pci_write_config_word(pdev, PCI_COMMAND, pci_command);
287
288		regs = of_get_property(dp, "reg", &len);
289		if (!regs) {
290			prom_printf("%s: can't find reg property\n",
291				    __FUNCTION__);
292			prom_halt();
293		}
294		nreg = len / sizeof(struct linux_prom_pci_registers);
295
296		p = &ebus->self->resource[0];
297		for (reg = 0; reg < nreg; reg++) {
298			if (!(regs[reg].which_io & 0x03000000))
299				continue;
300
301			(p++)->start = regs[reg].phys_lo;
302		}
303
304		ebus->ofdev.node = dp;
305		ebus->ofdev.dev.parent = &pdev->dev;
306		ebus->ofdev.dev.bus = &ebus_bus_type;
307		sprintf(ebus->ofdev.dev.bus_id, "ebus%d", num_ebus);
308
309		/* Register with core */
310		if (of_device_register(&ebus->ofdev) != 0)
311			printk(KERN_DEBUG "ebus: device registration error for %s!\n",
312			       dp->path_component_name);
313
314
315		nd = dp->child;
316		if (!nd)
317			goto next_ebus;
318
319		ebus->devices = (struct linux_ebus_device *)
320				ebus_alloc(sizeof(struct linux_ebus_device));
321
322		dev = ebus->devices;
323		dev->next = NULL;
324		dev->children = NULL;
325		dev->bus = ebus;
326		fill_ebus_device(nd, dev);
327
328		while ((nd = nd->sibling) != NULL) {
329			dev->next = (struct linux_ebus_device *)
330				ebus_alloc(sizeof(struct linux_ebus_device));
331
332			dev = dev->next;
333			dev->next = NULL;
334			dev->children = NULL;
335			dev->bus = ebus;
336			fill_ebus_device(nd, dev);
337		}
338
339	next_ebus:
340		pdev = pci_get_device(PCI_VENDOR_ID_SUN,
341				       PCI_DEVICE_ID_SUN_EBUS, pdev);
342		if (!pdev)
343			break;
344
345		cookie = pdev->sysdata;
346		dp = cookie->prom_node;
347
348		ebus->next = (struct linux_ebus *)
349			ebus_alloc(sizeof(struct linux_ebus));
350		ebus = ebus->next;
351		ebus->next = NULL;
352		++num_ebus;
353	}
354	if (pdev)
355		pci_dev_put(pdev);
356}
357