[PATCH] PCI: pci_find_device remove (sparc64/kernel/ebus.c)
[linux-2.6/linux-mips.git] / arch / sparc64 / kernel / ebus.c
blob7991e919d8ab6801605f040c1b16f2df1277975f
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)
6 */
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>
19 #include <asm/page.h>
20 #include <asm/pbm.h>
21 #include <asm/ebus.h>
22 #include <asm/oplib.h>
23 #include <asm/bpp.h>
24 #include <asm/irq.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)
63 int i;
64 u32 val = 0;
66 writel(EBDMA_CSR_RESET, p->regs + EBDMA_CSR);
67 udelay(1);
69 if (no_drain)
70 return;
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)))
76 break;
77 udelay(10);
81 static irqreturn_t ebus_dma_irq(int irq, void *dev_id, struct pt_regs *regs)
83 struct ebus_dma_info *p = dev_id;
84 unsigned long flags;
85 u32 csr = 0;
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);
95 return IRQ_HANDLED;
96 } else if (csr & EBDMA_CSR_INT_PEND) {
97 p->callback(p,
98 (csr & EBDMA_CSR_TC) ?
99 EBUS_DMA_EVENT_DMA : EBUS_DMA_EVENT_DEVICE,
100 p->client_cookie);
101 return IRQ_HANDLED;
104 return IRQ_NONE;
108 int ebus_dma_register(struct ebus_dma_info *p)
110 u32 csr;
112 if (!p->regs)
113 return -EINVAL;
114 if (p->flags & ~(EBUS_DMA_FLAG_USE_EBDMA_HANDLER |
115 EBUS_DMA_FLAG_TCI_DISABLE))
116 return -EINVAL;
117 if ((p->flags & EBUS_DMA_FLAG_USE_EBDMA_HANDLER) && !p->callback)
118 return -EINVAL;
119 if (!strlen(p->name))
120 return -EINVAL;
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);
131 return 0;
133 EXPORT_SYMBOL(ebus_dma_register);
135 int ebus_dma_irq_enable(struct ebus_dma_info *p, int on)
137 unsigned long flags;
138 u32 csr;
140 if (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))
143 return -EBUSY;
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);
151 } else {
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) {
159 free_irq(p->irq, p);
163 return 0;
165 EXPORT_SYMBOL(ebus_dma_irq_enable);
167 void ebus_dma_unregister(struct ebus_dma_info *p)
169 unsigned long flags;
170 u32 csr;
171 int irq_on = 0;
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);
178 irq_on = 1;
180 spin_unlock_irqrestore(&p->lock, flags);
182 if (irq_on)
183 free_irq(p->irq, p);
185 EXPORT_SYMBOL(ebus_dma_unregister);
187 int ebus_dma_request(struct ebus_dma_info *p, dma_addr_t bus_addr, size_t len)
189 unsigned long flags;
190 u32 csr;
191 int err;
193 if (len >= (1 << 24))
194 return -EINVAL;
196 spin_lock_irqsave(&p->lock, flags);
197 csr = readl(p->regs + EBDMA_CSR);
198 err = -EINVAL;
199 if (!(csr & EBDMA_CSR_EN_DMA))
200 goto out;
201 err = -EBUSY;
202 if (csr & EBDMA_CSR_NA_LOADED)
203 goto out;
205 writel(len, p->regs + EBDMA_COUNT);
206 writel(bus_addr, p->regs + EBDMA_ADDR);
207 err = 0;
209 out:
210 spin_unlock_irqrestore(&p->lock, flags);
212 return err;
214 EXPORT_SYMBOL(ebus_dma_request);
216 void ebus_dma_prepare(struct ebus_dma_info *p, int write)
218 unsigned long flags;
219 u32 csr;
221 spin_lock_irqsave(&p->lock, flags);
222 __ebus_dma_reset(p, 0);
224 csr = (EBDMA_CSR_INT_EN |
225 EBDMA_CSR_EN_CNT |
226 EBDMA_CSR_BURST_SZ_16 |
227 EBDMA_CSR_EN_NEXT);
229 if (write)
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)
254 unsigned long flags;
255 u32 orig_csr, csr;
257 spin_lock_irqsave(&p->lock, flags);
258 orig_csr = csr = readl(p->regs + EBDMA_CSR);
259 if (on)
260 csr |= EBDMA_CSR_EN_DMA;
261 else
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);
274 #endif
276 static inline void *ebus_alloc(size_t size)
278 void *mem;
280 mem = kmalloc(size, GFP_ATOMIC);
281 if (!mem)
282 panic("ebus_alloc: out of memory");
283 memset((char *)mem, 0, size);
284 return mem;
287 static void __init ebus_ranges_init(struct linux_ebus *ebus)
289 int success;
291 ebus->num_ebus_ranges = 0;
292 success = prom_getproperty(ebus->prom_node, "ranges",
293 (char *)ebus->ebus_ranges,
294 sizeof(ebus->ebus_ranges));
295 if (success != -1)
296 ebus->num_ebus_ranges = (success/sizeof(struct linux_prom_ebus_ranges));
299 static void __init ebus_intmap_init(struct linux_ebus *ebus)
301 int success;
303 ebus->num_ebus_intmap = 0;
304 success = prom_getproperty(ebus->prom_node, "interrupt-map",
305 (char *)ebus->ebus_intmap,
306 sizeof(ebus->ebus_intmap));
307 if (success == -1)
308 return;
310 ebus->num_ebus_intmap = (success/sizeof(struct linux_prom_ebus_intmap));
312 success = prom_getproperty(ebus->prom_node, "interrupt-map-mask",
313 (char *)&ebus->ebus_intmask,
314 sizeof(ebus->ebus_intmask));
315 if (success == -1) {
316 prom_printf("%s: can't get interrupt-map-mask\n", __FUNCTION__);
317 prom_halt();
321 int __init ebus_intmap_match(struct linux_ebus *ebus,
322 struct linux_prom_registers *reg,
323 int *interrupt)
325 unsigned int hi, lo, irq;
326 int i;
328 if (!ebus->num_ebus_intmap)
329 return 0;
331 hi = reg->which_io & ebus->ebus_intmask.phys_hi;
332 lo = reg->phys_addr & ebus->ebus_intmask.phys_lo;
333 irq = *interrupt & ebus->ebus_intmask.interrupt;
334 for (i = 0; i < ebus->num_ebus_intmap; i++) {
335 if ((ebus->ebus_intmap[i].phys_hi == hi) &&
336 (ebus->ebus_intmap[i].phys_lo == lo) &&
337 (ebus->ebus_intmap[i].interrupt == irq)) {
338 *interrupt = ebus->ebus_intmap[i].cinterrupt;
339 return 0;
342 return -1;
345 void __init fill_ebus_child(int node, struct linux_prom_registers *preg,
346 struct linux_ebus_child *dev, int non_standard_regs)
348 int regs[PROMREG_MAX];
349 int irqs[PROMREG_MAX];
350 int i, len;
352 dev->prom_node = node;
353 prom_getstring(node, "name", dev->prom_name, sizeof(dev->prom_name));
354 printk(" (%s)", dev->prom_name);
356 len = prom_getproperty(node, "reg", (void *)regs, sizeof(regs));
357 dev->num_addrs = len / sizeof(regs[0]);
359 if (non_standard_regs) {
360 /* This is to handle reg properties which are not
361 * in the parent relative format. One example are
362 * children of the i2c device on CompactPCI systems.
364 * So, for such devices we just record the property
365 * raw in the child resources.
367 for (i = 0; i < dev->num_addrs; i++)
368 dev->resource[i].start = regs[i];
369 } else {
370 for (i = 0; i < dev->num_addrs; i++) {
371 int rnum = regs[i];
372 if (rnum >= dev->parent->num_addrs) {
373 prom_printf("UGH: property for %s was %d, need < %d\n",
374 dev->prom_name, len, dev->parent->num_addrs);
375 panic(__FUNCTION__);
377 dev->resource[i].start = dev->parent->resource[i].start;
378 dev->resource[i].end = dev->parent->resource[i].end;
379 dev->resource[i].flags = IORESOURCE_MEM;
380 dev->resource[i].name = dev->prom_name;
384 for (i = 0; i < PROMINTR_MAX; i++)
385 dev->irqs[i] = PCI_IRQ_NONE;
387 len = prom_getproperty(node, "interrupts", (char *)&irqs, sizeof(irqs));
388 if ((len == -1) || (len == 0)) {
389 dev->num_irqs = 0;
391 * Oh, well, some PROMs don't export interrupts
392 * property to children of EBus devices...
394 * Be smart about PS/2 keyboard and mouse.
396 if (!strcmp(dev->parent->prom_name, "8042")) {
397 if (!strcmp(dev->prom_name, "kb_ps2")) {
398 dev->num_irqs = 1;
399 dev->irqs[0] = dev->parent->irqs[0];
400 } else {
401 dev->num_irqs = 1;
402 dev->irqs[0] = dev->parent->irqs[1];
405 } else {
406 dev->num_irqs = len / sizeof(irqs[0]);
407 for (i = 0; i < dev->num_irqs; i++) {
408 struct pci_pbm_info *pbm = dev->bus->parent;
409 struct pci_controller_info *p = pbm->parent;
411 if (ebus_intmap_match(dev->bus, preg, &irqs[i]) != -1) {
412 dev->irqs[i] = p->irq_build(pbm,
413 dev->bus->self,
414 irqs[i]);
415 } else {
416 /* If we get a bogus interrupt property, just
417 * record the raw value instead of punting.
419 dev->irqs[i] = irqs[i];
425 static int __init child_regs_nonstandard(struct linux_ebus_device *dev)
427 if (!strcmp(dev->prom_name, "i2c") ||
428 !strcmp(dev->prom_name, "SUNW,lombus"))
429 return 1;
430 return 0;
433 void __init fill_ebus_device(int node, struct linux_ebus_device *dev)
435 struct linux_prom_registers regs[PROMREG_MAX];
436 struct linux_ebus_child *child;
437 int irqs[PROMINTR_MAX];
438 int i, n, len;
440 dev->prom_node = node;
441 prom_getstring(node, "name", dev->prom_name, sizeof(dev->prom_name));
442 printk(" [%s", dev->prom_name);
444 len = prom_getproperty(node, "reg", (void *)regs, sizeof(regs));
445 if (len == -1) {
446 dev->num_addrs = 0;
447 goto probe_interrupts;
450 if (len % sizeof(struct linux_prom_registers)) {
451 prom_printf("UGH: proplen for %s was %d, need multiple of %d\n",
452 dev->prom_name, len,
453 (int)sizeof(struct linux_prom_registers));
454 prom_halt();
456 dev->num_addrs = len / sizeof(struct linux_prom_registers);
458 for (i = 0; i < dev->num_addrs; i++) {
459 /* XXX Learn how to interpret ebus ranges... -DaveM */
460 if (regs[i].which_io >= 0x10)
461 n = (regs[i].which_io - 0x10) >> 2;
462 else
463 n = regs[i].which_io;
465 dev->resource[i].start = dev->bus->self->resource[n].start;
466 dev->resource[i].start += (unsigned long)regs[i].phys_addr;
467 dev->resource[i].end =
468 (dev->resource[i].start + (unsigned long)regs[i].reg_size - 1UL);
469 dev->resource[i].flags = IORESOURCE_MEM;
470 dev->resource[i].name = dev->prom_name;
471 request_resource(&dev->bus->self->resource[n],
472 &dev->resource[i]);
475 probe_interrupts:
476 for (i = 0; i < PROMINTR_MAX; i++)
477 dev->irqs[i] = PCI_IRQ_NONE;
479 len = prom_getproperty(node, "interrupts", (char *)&irqs, sizeof(irqs));
480 if ((len == -1) || (len == 0)) {
481 dev->num_irqs = 0;
482 } else {
483 dev->num_irqs = len / sizeof(irqs[0]);
484 for (i = 0; i < dev->num_irqs; i++) {
485 struct pci_pbm_info *pbm = dev->bus->parent;
486 struct pci_controller_info *p = pbm->parent;
488 if (ebus_intmap_match(dev->bus, &regs[0], &irqs[i]) != -1) {
489 dev->irqs[i] = p->irq_build(pbm,
490 dev->bus->self,
491 irqs[i]);
492 } else {
493 /* If we get a bogus interrupt property, just
494 * record the raw value instead of punting.
496 dev->irqs[i] = irqs[i];
501 if ((node = prom_getchild(node))) {
502 printk(" ->");
503 dev->children = ebus_alloc(sizeof(struct linux_ebus_child));
505 child = dev->children;
506 child->next = NULL;
507 child->parent = dev;
508 child->bus = dev->bus;
509 fill_ebus_child(node, &regs[0],
510 child, child_regs_nonstandard(dev));
512 while ((node = prom_getsibling(node)) != 0) {
513 child->next = ebus_alloc(sizeof(struct linux_ebus_child));
515 child = child->next;
516 child->next = NULL;
517 child->parent = dev;
518 child->bus = dev->bus;
519 fill_ebus_child(node, &regs[0],
520 child, child_regs_nonstandard(dev));
523 printk("]");
526 static struct pci_dev *find_next_ebus(struct pci_dev *start, int *is_rio_p)
528 struct pci_dev *pdev = start;
530 while ((pdev = pci_get_device(PCI_VENDOR_ID_SUN, PCI_ANY_ID, pdev)))
531 if (pdev->device == PCI_DEVICE_ID_SUN_EBUS ||
532 pdev->device == PCI_DEVICE_ID_SUN_RIO_EBUS)
533 break;
535 *is_rio_p = !!(pdev && (pdev->device == PCI_DEVICE_ID_SUN_RIO_EBUS));
537 return pdev;
540 void __init ebus_init(void)
542 struct pci_pbm_info *pbm;
543 struct linux_ebus_device *dev;
544 struct linux_ebus *ebus;
545 struct pci_dev *pdev;
546 struct pcidev_cookie *cookie;
547 int nd, ebusnd, is_rio;
548 int num_ebus = 0;
550 pdev = find_next_ebus(NULL, &is_rio);
551 if (!pdev) {
552 printk("ebus: No EBus's found.\n");
553 return;
556 cookie = pdev->sysdata;
557 ebusnd = cookie->prom_node;
559 ebus_chain = ebus = ebus_alloc(sizeof(struct linux_ebus));
560 ebus->next = NULL;
561 ebus->is_rio = is_rio;
563 while (ebusnd) {
564 /* SUNW,pci-qfe uses four empty ebuses on it.
565 I think we should not consider them here,
566 as they have half of the properties this
567 code expects and once we do PCI hot-plug,
568 we'd have to tweak with the ebus_chain
569 in the runtime after initialization. -jj */
570 if (!prom_getchild (ebusnd)) {
571 pdev = find_next_ebus(pdev, &is_rio);
572 if (!pdev) {
573 if (ebus == ebus_chain) {
574 ebus_chain = NULL;
575 printk("ebus: No EBus's found.\n");
576 return;
578 break;
580 ebus->is_rio = is_rio;
581 cookie = pdev->sysdata;
582 ebusnd = cookie->prom_node;
583 continue;
585 printk("ebus%d:", num_ebus);
587 prom_getstring(ebusnd, "name", ebus->prom_name, sizeof(ebus->prom_name));
588 ebus->index = num_ebus;
589 ebus->prom_node = ebusnd;
590 ebus->self = pdev;
591 ebus->parent = pbm = cookie->pbm;
593 ebus_ranges_init(ebus);
594 ebus_intmap_init(ebus);
596 nd = prom_getchild(ebusnd);
597 if (!nd)
598 goto next_ebus;
600 ebus->devices = ebus_alloc(sizeof(struct linux_ebus_device));
602 dev = ebus->devices;
603 dev->next = NULL;
604 dev->children = NULL;
605 dev->bus = ebus;
606 fill_ebus_device(nd, dev);
608 while ((nd = prom_getsibling(nd)) != 0) {
609 dev->next = ebus_alloc(sizeof(struct linux_ebus_device));
611 dev = dev->next;
612 dev->next = NULL;
613 dev->children = NULL;
614 dev->bus = ebus;
615 fill_ebus_device(nd, dev);
618 next_ebus:
619 printk("\n");
621 pdev = find_next_ebus(pdev, &is_rio);
622 if (!pdev)
623 break;
625 cookie = pdev->sysdata;
626 ebusnd = cookie->prom_node;
628 ebus->next = ebus_alloc(sizeof(struct linux_ebus));
629 ebus = ebus->next;
630 ebus->next = NULL;
631 ebus->is_rio = is_rio;
632 ++num_ebus;
634 pci_dev_put(pdev); /* XXX for the case, when ebusnd is 0, is it OK? */
636 #ifdef CONFIG_SUN_AUXIO
637 auxio_probe();
638 #endif