1 /* $Id: ebus.c,v 1.64 2001/11/08 04:41:33 davem Exp $
2 * ebus.c: PCI to EBus bridge device.
4 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 1999 David S. Miller (davem@redhat.com)
8 #include <linux/config.h>
9 #include <linux/module.h>
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 #include <linux/interrupt.h>
16 #include <linux/delay.h>
18 #include <asm/system.h>
22 #include <asm/oplib.h>
26 /* EBUS dma library. */
28 #define EBDMA_CSR 0x00UL /* Control/Status */
29 #define EBDMA_ADDR 0x04UL /* DMA Address */
30 #define EBDMA_COUNT 0x08UL /* DMA Count */
32 #define EBDMA_CSR_INT_PEND 0x00000001
33 #define EBDMA_CSR_ERR_PEND 0x00000002
34 #define EBDMA_CSR_DRAIN 0x00000004
35 #define EBDMA_CSR_INT_EN 0x00000010
36 #define EBDMA_CSR_RESET 0x00000080
37 #define EBDMA_CSR_WRITE 0x00000100
38 #define EBDMA_CSR_EN_DMA 0x00000200
39 #define EBDMA_CSR_CYC_PEND 0x00000400
40 #define EBDMA_CSR_DIAG_RD_DONE 0x00000800
41 #define EBDMA_CSR_DIAG_WR_DONE 0x00001000
42 #define EBDMA_CSR_EN_CNT 0x00002000
43 #define EBDMA_CSR_TC 0x00004000
44 #define EBDMA_CSR_DIS_CSR_DRN 0x00010000
45 #define EBDMA_CSR_BURST_SZ_MASK 0x000c0000
46 #define EBDMA_CSR_BURST_SZ_1 0x00080000
47 #define EBDMA_CSR_BURST_SZ_4 0x00000000
48 #define EBDMA_CSR_BURST_SZ_8 0x00040000
49 #define EBDMA_CSR_BURST_SZ_16 0x000c0000
50 #define EBDMA_CSR_DIAG_EN 0x00100000
51 #define EBDMA_CSR_DIS_ERR_PEND 0x00400000
52 #define EBDMA_CSR_TCI_DIS 0x00800000
53 #define EBDMA_CSR_EN_NEXT 0x01000000
54 #define EBDMA_CSR_DMA_ON 0x02000000
55 #define EBDMA_CSR_A_LOADED 0x04000000
56 #define EBDMA_CSR_NA_LOADED 0x08000000
57 #define EBDMA_CSR_DEV_ID_MASK 0xf0000000
59 #define EBUS_DMA_RESET_TIMEOUT 10000
61 static void __ebus_dma_reset(struct ebus_dma_info *p, int no_drain)
66 writel(EBDMA_CSR_RESET, p->regs + EBDMA_CSR);
72 for (i = EBUS_DMA_RESET_TIMEOUT; i > 0; i--) {
73 val = readl(p->regs + EBDMA_CSR);
75 if (!(val & (EBDMA_CSR_DRAIN | EBDMA_CSR_CYC_PEND)))
81 static irqreturn_t ebus_dma_irq(int irq, void *dev_id, struct pt_regs *regs)
83 struct ebus_dma_info *p = dev_id;
87 spin_lock_irqsave(&p->lock, flags);
88 csr = readl(p->regs + EBDMA_CSR);
89 writel(csr, p->regs + EBDMA_CSR);
90 spin_unlock_irqrestore(&p->lock, flags);
92 if (csr & EBDMA_CSR_ERR_PEND) {
93 printk(KERN_CRIT "ebus_dma(%s): DMA error!\n", p->name);
94 p->callback(p, EBUS_DMA_EVENT_ERROR, p->client_cookie);
96 } else if (csr & EBDMA_CSR_INT_PEND) {
98 (csr & EBDMA_CSR_TC) ?
99 EBUS_DMA_EVENT_DMA : EBUS_DMA_EVENT_DEVICE,
108 int ebus_dma_register(struct ebus_dma_info *p)
114 if (p->flags & ~(EBUS_DMA_FLAG_USE_EBDMA_HANDLER |
115 EBUS_DMA_FLAG_TCI_DISABLE))
117 if ((p->flags & EBUS_DMA_FLAG_USE_EBDMA_HANDLER) && !p->callback)
119 if (!strlen(p->name))
122 __ebus_dma_reset(p, 1);
124 csr = EBDMA_CSR_BURST_SZ_16 | EBDMA_CSR_EN_CNT;
126 if (p->flags & EBUS_DMA_FLAG_TCI_DISABLE)
127 csr |= EBDMA_CSR_TCI_DIS;
129 writel(csr, p->regs + EBDMA_CSR);
133 EXPORT_SYMBOL(ebus_dma_register);
135 int ebus_dma_irq_enable(struct ebus_dma_info *p, int on)
141 if (p->flags & EBUS_DMA_FLAG_USE_EBDMA_HANDLER) {
142 if (request_irq(p->irq, ebus_dma_irq, SA_SHIRQ, p->name, p))
146 spin_lock_irqsave(&p->lock, flags);
147 csr = readl(p->regs + EBDMA_CSR);
148 csr |= EBDMA_CSR_INT_EN;
149 writel(csr, p->regs + EBDMA_CSR);
150 spin_unlock_irqrestore(&p->lock, flags);
152 spin_lock_irqsave(&p->lock, flags);
153 csr = readl(p->regs + EBDMA_CSR);
154 csr &= ~EBDMA_CSR_INT_EN;
155 writel(csr, p->regs + EBDMA_CSR);
156 spin_unlock_irqrestore(&p->lock, flags);
158 if (p->flags & EBUS_DMA_FLAG_USE_EBDMA_HANDLER) {
165 EXPORT_SYMBOL(ebus_dma_irq_enable);
167 void ebus_dma_unregister(struct ebus_dma_info *p)
173 spin_lock_irqsave(&p->lock, flags);
174 csr = readl(p->regs + EBDMA_CSR);
175 if (csr & EBDMA_CSR_INT_EN) {
176 csr &= ~EBDMA_CSR_INT_EN;
177 writel(csr, p->regs + EBDMA_CSR);
180 spin_unlock_irqrestore(&p->lock, flags);
185 EXPORT_SYMBOL(ebus_dma_unregister);
187 int ebus_dma_request(struct ebus_dma_info *p, dma_addr_t bus_addr, size_t len)
193 if (len >= (1 << 24))
196 spin_lock_irqsave(&p->lock, flags);
197 csr = readl(p->regs + EBDMA_CSR);
199 if (!(csr & EBDMA_CSR_EN_DMA))
202 if (csr & EBDMA_CSR_NA_LOADED)
205 writel(len, p->regs + EBDMA_COUNT);
206 writel(bus_addr, p->regs + EBDMA_ADDR);
210 spin_unlock_irqrestore(&p->lock, flags);
214 EXPORT_SYMBOL(ebus_dma_request);
216 void ebus_dma_prepare(struct ebus_dma_info *p, int write)
221 spin_lock_irqsave(&p->lock, flags);
222 __ebus_dma_reset(p, 0);
224 csr = (EBDMA_CSR_INT_EN |
226 EBDMA_CSR_BURST_SZ_16 |
230 csr |= EBDMA_CSR_WRITE;
231 if (p->flags & EBUS_DMA_FLAG_TCI_DISABLE)
232 csr |= EBDMA_CSR_TCI_DIS;
234 writel(csr, p->regs + EBDMA_CSR);
236 spin_unlock_irqrestore(&p->lock, flags);
238 EXPORT_SYMBOL(ebus_dma_prepare);
240 unsigned int ebus_dma_residue(struct ebus_dma_info *p)
242 return readl(p->regs + EBDMA_COUNT);
244 EXPORT_SYMBOL(ebus_dma_residue);
246 unsigned int ebus_dma_addr(struct ebus_dma_info *p)
248 return readl(p->regs + EBDMA_ADDR);
250 EXPORT_SYMBOL(ebus_dma_addr);
252 void ebus_dma_enable(struct ebus_dma_info *p, int on)
257 spin_lock_irqsave(&p->lock, flags);
258 orig_csr = csr = readl(p->regs + EBDMA_CSR);
260 csr |= EBDMA_CSR_EN_DMA;
262 csr &= ~EBDMA_CSR_EN_DMA;
263 if ((orig_csr & EBDMA_CSR_EN_DMA) !=
264 (csr & EBDMA_CSR_EN_DMA))
265 writel(csr, p->regs + EBDMA_CSR);
266 spin_unlock_irqrestore(&p->lock, flags);
268 EXPORT_SYMBOL(ebus_dma_enable);
270 struct linux_ebus *ebus_chain = NULL;
272 #ifdef CONFIG_SUN_AUXIO
273 extern void auxio_probe(void);
276 static inline void *ebus_alloc(size_t size)
280 mem = kzalloc(size, GFP_ATOMIC);
282 panic("ebus_alloc: out of memory");
286 static void __init ebus_ranges_init(struct linux_ebus *ebus)
288 struct linux_prom_ebus_ranges *rngs;
291 ebus->num_ebus_ranges = 0;
292 rngs = of_get_property(ebus->prom_node, "ranges", &len);
294 memcpy(ebus->ebus_ranges, rngs, len);
295 ebus->num_ebus_ranges =
296 (len / sizeof(struct linux_prom_ebus_ranges));
300 static void __init ebus_intmap_init(struct linux_ebus *ebus)
302 struct linux_prom_ebus_intmap *imap;
303 struct linux_prom_ebus_intmask *imask;
306 ebus->num_ebus_intmap = 0;
307 imap = of_get_property(ebus->prom_node, "interrupt-map", &len);
311 memcpy(ebus->ebus_intmap, imap, len);
312 ebus->num_ebus_intmap = (len / sizeof(struct linux_prom_ebus_intmap));
314 imask = of_get_property(ebus->prom_node, "interrupt-map-mask", &len);
316 prom_printf("EBUS: can't get interrupt-map-mask\n");
319 memcpy(&ebus->ebus_intmask, imask, sizeof(ebus->ebus_intmask));
322 int __init ebus_intmap_match(struct linux_ebus *ebus,
323 struct linux_prom_registers *reg,
326 unsigned int hi, lo, irq;
329 if (!ebus->num_ebus_intmap)
332 hi = reg->which_io & ebus->ebus_intmask.phys_hi;
333 lo = reg->phys_addr & ebus->ebus_intmask.phys_lo;
334 irq = *interrupt & ebus->ebus_intmask.interrupt;
335 for (i = 0; i < ebus->num_ebus_intmap; i++) {
336 if ((ebus->ebus_intmap[i].phys_hi == hi) &&
337 (ebus->ebus_intmap[i].phys_lo == lo) &&
338 (ebus->ebus_intmap[i].interrupt == irq)) {
339 *interrupt = ebus->ebus_intmap[i].cinterrupt;
346 void __init fill_ebus_child(struct device_node *dp,
347 struct linux_prom_registers *preg,
348 struct linux_ebus_child *dev,
349 int non_standard_regs)
356 printk(" (%s)", dp->name);
358 regs = of_get_property(dp, "reg", &len);
362 dev->num_addrs = len / sizeof(regs[0]);
364 if (non_standard_regs) {
365 /* This is to handle reg properties which are not
366 * in the parent relative format. One example are
367 * children of the i2c device on CompactPCI systems.
369 * So, for such devices we just record the property
370 * raw in the child resources.
372 for (i = 0; i < dev->num_addrs; i++)
373 dev->resource[i].start = regs[i];
375 for (i = 0; i < dev->num_addrs; i++) {
377 if (rnum >= dev->parent->num_addrs) {
378 prom_printf("UGH: property for %s was %d, need < %d\n",
379 dp->name, len, dev->parent->num_addrs);
382 dev->resource[i].start = dev->parent->resource[i].start;
383 dev->resource[i].end = dev->parent->resource[i].end;
384 dev->resource[i].flags = IORESOURCE_MEM;
385 dev->resource[i].name = dp->name;
389 for (i = 0; i < PROMINTR_MAX; i++)
390 dev->irqs[i] = PCI_IRQ_NONE;
392 irqs = of_get_property(dp, "interrupts", &len);
396 * Oh, well, some PROMs don't export interrupts
397 * property to children of EBus devices...
399 * Be smart about PS/2 keyboard and mouse.
401 if (!strcmp(dev->parent->prom_node->name, "8042")) {
402 if (!strcmp(dev->prom_node->name, "kb_ps2")) {
404 dev->irqs[0] = dev->parent->irqs[0];
407 dev->irqs[0] = dev->parent->irqs[1];
411 dev->num_irqs = len / sizeof(irqs[0]);
412 for (i = 0; i < dev->num_irqs; i++) {
413 struct pci_pbm_info *pbm = dev->bus->parent;
414 struct pci_controller_info *p = pbm->parent;
416 if (ebus_intmap_match(dev->bus, preg, &irqs[i]) != -1) {
417 dev->irqs[i] = p->irq_build(pbm,
421 /* If we get a bogus interrupt property, just
422 * record the raw value instead of punting.
424 dev->irqs[i] = irqs[i];
430 static int __init child_regs_nonstandard(struct linux_ebus_device *dev)
432 if (!strcmp(dev->prom_node->name, "i2c") ||
433 !strcmp(dev->prom_node->name, "SUNW,lombus"))
438 void __init fill_ebus_device(struct device_node *dp, struct linux_ebus_device *dev)
440 struct linux_prom_registers *regs;
441 struct linux_ebus_child *child;
447 printk(" [%s", dp->name);
449 regs = of_get_property(dp, "reg", &len);
452 goto probe_interrupts;
455 if (len % sizeof(struct linux_prom_registers)) {
456 prom_printf("UGH: proplen for %s was %d, need multiple of %d\n",
457 dev->prom_node->name, len,
458 (int)sizeof(struct linux_prom_registers));
461 dev->num_addrs = len / sizeof(struct linux_prom_registers);
463 for (i = 0; i < dev->num_addrs; i++) {
464 /* XXX Learn how to interpret ebus ranges... -DaveM */
465 if (regs[i].which_io >= 0x10)
466 n = (regs[i].which_io - 0x10) >> 2;
468 n = regs[i].which_io;
470 dev->resource[i].start = dev->bus->self->resource[n].start;
471 dev->resource[i].start += (unsigned long)regs[i].phys_addr;
472 dev->resource[i].end =
473 (dev->resource[i].start + (unsigned long)regs[i].reg_size - 1UL);
474 dev->resource[i].flags = IORESOURCE_MEM;
475 dev->resource[i].name = dev->prom_node->name;
476 request_resource(&dev->bus->self->resource[n],
481 for (i = 0; i < PROMINTR_MAX; i++)
482 dev->irqs[i] = PCI_IRQ_NONE;
484 irqs = of_get_property(dp, "interrupts", &len);
488 dev->num_irqs = len / sizeof(irqs[0]);
489 for (i = 0; i < dev->num_irqs; i++) {
490 struct pci_pbm_info *pbm = dev->bus->parent;
491 struct pci_controller_info *p = pbm->parent;
493 if (ebus_intmap_match(dev->bus, ®s[0], &irqs[i]) != -1) {
494 dev->irqs[i] = p->irq_build(pbm,
498 /* If we get a bogus interrupt property, just
499 * record the raw value instead of punting.
501 dev->irqs[i] = irqs[i];
509 dev->children = ebus_alloc(sizeof(struct linux_ebus_child));
511 child = dev->children;
514 child->bus = dev->bus;
515 fill_ebus_child(dp, regs, child,
516 child_regs_nonstandard(dev));
518 while ((dp = dp->sibling) != NULL) {
519 child->next = ebus_alloc(sizeof(struct linux_ebus_child));
524 child->bus = dev->bus;
525 fill_ebus_child(dp, regs, child,
526 child_regs_nonstandard(dev));
532 static struct pci_dev *find_next_ebus(struct pci_dev *start, int *is_rio_p)
534 struct pci_dev *pdev = start;
536 while ((pdev = pci_get_device(PCI_VENDOR_ID_SUN, PCI_ANY_ID, pdev)))
537 if (pdev->device == PCI_DEVICE_ID_SUN_EBUS ||
538 pdev->device == PCI_DEVICE_ID_SUN_RIO_EBUS)
541 *is_rio_p = !!(pdev && (pdev->device == PCI_DEVICE_ID_SUN_RIO_EBUS));
546 void __init ebus_init(void)
548 struct pci_pbm_info *pbm;
549 struct linux_ebus_device *dev;
550 struct linux_ebus *ebus;
551 struct pci_dev *pdev;
552 struct pcidev_cookie *cookie;
553 struct device_node *dp;
557 pdev = find_next_ebus(NULL, &is_rio);
559 printk("ebus: No EBus's found.\n");
563 cookie = pdev->sysdata;
564 dp = cookie->prom_node;
566 ebus_chain = ebus = ebus_alloc(sizeof(struct linux_ebus));
568 ebus->is_rio = is_rio;
571 struct device_node *child;
573 /* SUNW,pci-qfe uses four empty ebuses on it.
574 I think we should not consider them here,
575 as they have half of the properties this
576 code expects and once we do PCI hot-plug,
577 we'd have to tweak with the ebus_chain
578 in the runtime after initialization. -jj */
580 pdev = find_next_ebus(pdev, &is_rio);
582 if (ebus == ebus_chain) {
584 printk("ebus: No EBus's found.\n");
589 ebus->is_rio = is_rio;
590 cookie = pdev->sysdata;
591 dp = cookie->prom_node;
594 printk("ebus%d:", num_ebus);
596 ebus->index = num_ebus;
597 ebus->prom_node = dp;
599 ebus->parent = pbm = cookie->pbm;
601 ebus_ranges_init(ebus);
602 ebus_intmap_init(ebus);
608 ebus->devices = ebus_alloc(sizeof(struct linux_ebus_device));
612 dev->children = NULL;
614 fill_ebus_device(child, dev);
616 while ((child = child->sibling) != NULL) {
617 dev->next = ebus_alloc(sizeof(struct linux_ebus_device));
621 dev->children = NULL;
623 fill_ebus_device(child, dev);
629 pdev = find_next_ebus(pdev, &is_rio);
633 cookie = pdev->sysdata;
634 dp = cookie->prom_node;
636 ebus->next = ebus_alloc(sizeof(struct linux_ebus));
639 ebus->is_rio = is_rio;
642 pci_dev_put(pdev); /* XXX for the case, when ebusnd is 0, is it OK? */
644 #ifdef CONFIG_SUN_AUXIO