blob: c69504aa638f59418a3bf08040cabb57fc85767f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: ebus.c,v 1.64 2001/11/08 04:41:33 davem Exp $
2 * ebus.c: PCI to EBus bridge device.
3 *
4 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 1999 David S. Miller (davem@redhat.com)
6 */
7
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>
17
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>
25
26/* EBUS dma library. */
27
28#define EBDMA_CSR 0x00UL /* Control/Status */
29#define EBDMA_ADDR 0x04UL /* DMA Address */
30#define EBDMA_COUNT 0x08UL /* DMA Count */
31
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
58
59#define EBUS_DMA_RESET_TIMEOUT 10000
60
61static void __ebus_dma_reset(struct ebus_dma_info *p, int no_drain)
62{
63 int i;
64 u32 val = 0;
65
66 writel(EBDMA_CSR_RESET, p->regs + EBDMA_CSR);
67 udelay(1);
68
69 if (no_drain)
70 return;
71
72 for (i = EBUS_DMA_RESET_TIMEOUT; i > 0; i--) {
73 val = readl(p->regs + EBDMA_CSR);
74
75 if (!(val & (EBDMA_CSR_DRAIN | EBDMA_CSR_CYC_PEND)))
76 break;
77 udelay(10);
78 }
79}
80
81static irqreturn_t ebus_dma_irq(int irq, void *dev_id, struct pt_regs *regs)
82{
83 struct ebus_dma_info *p = dev_id;
84 unsigned long flags;
85 u32 csr = 0;
86
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);
91
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;
102 }
103
104 return IRQ_NONE;
105
106}
107
108int ebus_dma_register(struct ebus_dma_info *p)
109{
110 u32 csr;
111
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;
121
122 __ebus_dma_reset(p, 1);
123
124 csr = EBDMA_CSR_BURST_SZ_16 | EBDMA_CSR_EN_CNT;
125
126 if (p->flags & EBUS_DMA_FLAG_TCI_DISABLE)
127 csr |= EBDMA_CSR_TCI_DIS;
128
129 writel(csr, p->regs + EBDMA_CSR);
130
131 return 0;
132}
133EXPORT_SYMBOL(ebus_dma_register);
134
135int ebus_dma_irq_enable(struct ebus_dma_info *p, int on)
136{
137 unsigned long flags;
138 u32 csr;
139
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;
144 }
145
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);
157
158 if (p->flags & EBUS_DMA_FLAG_USE_EBDMA_HANDLER) {
159 free_irq(p->irq, p);
160 }
161 }
162
163 return 0;
164}
165EXPORT_SYMBOL(ebus_dma_irq_enable);
166
167void ebus_dma_unregister(struct ebus_dma_info *p)
168{
169 unsigned long flags;
170 u32 csr;
171 int irq_on = 0;
172
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;
179 }
180 spin_unlock_irqrestore(&p->lock, flags);
181
182 if (irq_on)
183 free_irq(p->irq, p);
184}
185EXPORT_SYMBOL(ebus_dma_unregister);
186
187int ebus_dma_request(struct ebus_dma_info *p, dma_addr_t bus_addr, size_t len)
188{
189 unsigned long flags;
190 u32 csr;
191 int err;
192
193 if (len >= (1 << 24))
194 return -EINVAL;
195
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;
204
205 writel(len, p->regs + EBDMA_COUNT);
206 writel(bus_addr, p->regs + EBDMA_ADDR);
207 err = 0;
208
209out:
210 spin_unlock_irqrestore(&p->lock, flags);
211
212 return err;
213}
214EXPORT_SYMBOL(ebus_dma_request);
215
216void ebus_dma_prepare(struct ebus_dma_info *p, int write)
217{
218 unsigned long flags;
219 u32 csr;
220
221 spin_lock_irqsave(&p->lock, flags);
222 __ebus_dma_reset(p, 0);
223
224 csr = (EBDMA_CSR_INT_EN |
225 EBDMA_CSR_EN_CNT |
226 EBDMA_CSR_BURST_SZ_16 |
227 EBDMA_CSR_EN_NEXT);
228
229 if (write)
230 csr |= EBDMA_CSR_WRITE;
231 if (p->flags & EBUS_DMA_FLAG_TCI_DISABLE)
232 csr |= EBDMA_CSR_TCI_DIS;
233
234 writel(csr, p->regs + EBDMA_CSR);
235
236 spin_unlock_irqrestore(&p->lock, flags);
237}
238EXPORT_SYMBOL(ebus_dma_prepare);
239
240unsigned int ebus_dma_residue(struct ebus_dma_info *p)
241{
242 return readl(p->regs + EBDMA_COUNT);
243}
244EXPORT_SYMBOL(ebus_dma_residue);
245
246unsigned int ebus_dma_addr(struct ebus_dma_info *p)
247{
248 return readl(p->regs + EBDMA_ADDR);
249}
250EXPORT_SYMBOL(ebus_dma_addr);
251
252void ebus_dma_enable(struct ebus_dma_info *p, int on)
253{
254 unsigned long flags;
255 u32 orig_csr, csr;
256
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);
267}
268EXPORT_SYMBOL(ebus_dma_enable);
269
270struct linux_ebus *ebus_chain = NULL;
271
272#ifdef CONFIG_SUN_AUXIO
273extern void auxio_probe(void);
274#endif
275
276static inline void *ebus_alloc(size_t size)
277{
278 void *mem;
279
Eric Sesterhenn91329832006-03-06 13:48:40 -0800280 mem = kzalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (!mem)
282 panic("ebus_alloc: out of memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return mem;
284}
285
286static void __init ebus_ranges_init(struct linux_ebus *ebus)
287{
288 int success;
289
290 ebus->num_ebus_ranges = 0;
291 success = prom_getproperty(ebus->prom_node, "ranges",
292 (char *)ebus->ebus_ranges,
293 sizeof(ebus->ebus_ranges));
294 if (success != -1)
295 ebus->num_ebus_ranges = (success/sizeof(struct linux_prom_ebus_ranges));
296}
297
298static void __init ebus_intmap_init(struct linux_ebus *ebus)
299{
300 int success;
301
302 ebus->num_ebus_intmap = 0;
303 success = prom_getproperty(ebus->prom_node, "interrupt-map",
304 (char *)ebus->ebus_intmap,
305 sizeof(ebus->ebus_intmap));
306 if (success == -1)
307 return;
308
309 ebus->num_ebus_intmap = (success/sizeof(struct linux_prom_ebus_intmap));
310
311 success = prom_getproperty(ebus->prom_node, "interrupt-map-mask",
312 (char *)&ebus->ebus_intmask,
313 sizeof(ebus->ebus_intmask));
314 if (success == -1) {
315 prom_printf("%s: can't get interrupt-map-mask\n", __FUNCTION__);
316 prom_halt();
317 }
318}
319
320int __init ebus_intmap_match(struct linux_ebus *ebus,
321 struct linux_prom_registers *reg,
322 int *interrupt)
323{
324 unsigned int hi, lo, irq;
325 int i;
326
327 if (!ebus->num_ebus_intmap)
328 return 0;
329
330 hi = reg->which_io & ebus->ebus_intmask.phys_hi;
331 lo = reg->phys_addr & ebus->ebus_intmask.phys_lo;
332 irq = *interrupt & ebus->ebus_intmask.interrupt;
333 for (i = 0; i < ebus->num_ebus_intmap; i++) {
334 if ((ebus->ebus_intmap[i].phys_hi == hi) &&
335 (ebus->ebus_intmap[i].phys_lo == lo) &&
336 (ebus->ebus_intmap[i].interrupt == irq)) {
337 *interrupt = ebus->ebus_intmap[i].cinterrupt;
338 return 0;
339 }
340 }
341 return -1;
342}
343
344void __init fill_ebus_child(int node, struct linux_prom_registers *preg,
345 struct linux_ebus_child *dev, int non_standard_regs)
346{
347 int regs[PROMREG_MAX];
348 int irqs[PROMREG_MAX];
349 int i, len;
350
351 dev->prom_node = node;
352 prom_getstring(node, "name", dev->prom_name, sizeof(dev->prom_name));
353 printk(" (%s)", dev->prom_name);
354
355 len = prom_getproperty(node, "reg", (void *)regs, sizeof(regs));
356 dev->num_addrs = len / sizeof(regs[0]);
357
358 if (non_standard_regs) {
359 /* This is to handle reg properties which are not
360 * in the parent relative format. One example are
361 * children of the i2c device on CompactPCI systems.
362 *
363 * So, for such devices we just record the property
364 * raw in the child resources.
365 */
366 for (i = 0; i < dev->num_addrs; i++)
367 dev->resource[i].start = regs[i];
368 } else {
369 for (i = 0; i < dev->num_addrs; i++) {
370 int rnum = regs[i];
371 if (rnum >= dev->parent->num_addrs) {
372 prom_printf("UGH: property for %s was %d, need < %d\n",
373 dev->prom_name, len, dev->parent->num_addrs);
374 panic(__FUNCTION__);
375 }
376 dev->resource[i].start = dev->parent->resource[i].start;
377 dev->resource[i].end = dev->parent->resource[i].end;
378 dev->resource[i].flags = IORESOURCE_MEM;
379 dev->resource[i].name = dev->prom_name;
380 }
381 }
382
383 for (i = 0; i < PROMINTR_MAX; i++)
384 dev->irqs[i] = PCI_IRQ_NONE;
385
386 len = prom_getproperty(node, "interrupts", (char *)&irqs, sizeof(irqs));
387 if ((len == -1) || (len == 0)) {
388 dev->num_irqs = 0;
389 /*
390 * Oh, well, some PROMs don't export interrupts
391 * property to children of EBus devices...
392 *
393 * Be smart about PS/2 keyboard and mouse.
394 */
395 if (!strcmp(dev->parent->prom_name, "8042")) {
396 if (!strcmp(dev->prom_name, "kb_ps2")) {
397 dev->num_irqs = 1;
398 dev->irqs[0] = dev->parent->irqs[0];
399 } else {
400 dev->num_irqs = 1;
401 dev->irqs[0] = dev->parent->irqs[1];
402 }
403 }
404 } else {
405 dev->num_irqs = len / sizeof(irqs[0]);
406 for (i = 0; i < dev->num_irqs; i++) {
407 struct pci_pbm_info *pbm = dev->bus->parent;
408 struct pci_controller_info *p = pbm->parent;
409
410 if (ebus_intmap_match(dev->bus, preg, &irqs[i]) != -1) {
411 dev->irqs[i] = p->irq_build(pbm,
412 dev->bus->self,
413 irqs[i]);
414 } else {
415 /* If we get a bogus interrupt property, just
416 * record the raw value instead of punting.
417 */
418 dev->irqs[i] = irqs[i];
419 }
420 }
421 }
422}
423
424static int __init child_regs_nonstandard(struct linux_ebus_device *dev)
425{
426 if (!strcmp(dev->prom_name, "i2c") ||
427 !strcmp(dev->prom_name, "SUNW,lombus"))
428 return 1;
429 return 0;
430}
431
432void __init fill_ebus_device(int node, struct linux_ebus_device *dev)
433{
434 struct linux_prom_registers regs[PROMREG_MAX];
435 struct linux_ebus_child *child;
436 int irqs[PROMINTR_MAX];
437 int i, n, len;
438
439 dev->prom_node = node;
440 prom_getstring(node, "name", dev->prom_name, sizeof(dev->prom_name));
441 printk(" [%s", dev->prom_name);
442
443 len = prom_getproperty(node, "reg", (void *)regs, sizeof(regs));
444 if (len == -1) {
445 dev->num_addrs = 0;
446 goto probe_interrupts;
447 }
448
449 if (len % sizeof(struct linux_prom_registers)) {
450 prom_printf("UGH: proplen for %s was %d, need multiple of %d\n",
451 dev->prom_name, len,
452 (int)sizeof(struct linux_prom_registers));
453 prom_halt();
454 }
455 dev->num_addrs = len / sizeof(struct linux_prom_registers);
456
457 for (i = 0; i < dev->num_addrs; i++) {
458 /* XXX Learn how to interpret ebus ranges... -DaveM */
459 if (regs[i].which_io >= 0x10)
460 n = (regs[i].which_io - 0x10) >> 2;
461 else
462 n = regs[i].which_io;
463
464 dev->resource[i].start = dev->bus->self->resource[n].start;
465 dev->resource[i].start += (unsigned long)regs[i].phys_addr;
466 dev->resource[i].end =
467 (dev->resource[i].start + (unsigned long)regs[i].reg_size - 1UL);
468 dev->resource[i].flags = IORESOURCE_MEM;
469 dev->resource[i].name = dev->prom_name;
470 request_resource(&dev->bus->self->resource[n],
471 &dev->resource[i]);
472 }
473
474probe_interrupts:
475 for (i = 0; i < PROMINTR_MAX; i++)
476 dev->irqs[i] = PCI_IRQ_NONE;
477
478 len = prom_getproperty(node, "interrupts", (char *)&irqs, sizeof(irqs));
479 if ((len == -1) || (len == 0)) {
480 dev->num_irqs = 0;
481 } else {
482 dev->num_irqs = len / sizeof(irqs[0]);
483 for (i = 0; i < dev->num_irqs; i++) {
484 struct pci_pbm_info *pbm = dev->bus->parent;
485 struct pci_controller_info *p = pbm->parent;
486
487 if (ebus_intmap_match(dev->bus, &regs[0], &irqs[i]) != -1) {
488 dev->irqs[i] = p->irq_build(pbm,
489 dev->bus->self,
490 irqs[i]);
491 } else {
492 /* If we get a bogus interrupt property, just
493 * record the raw value instead of punting.
494 */
495 dev->irqs[i] = irqs[i];
496 }
497 }
498 }
499
500 if ((node = prom_getchild(node))) {
501 printk(" ->");
502 dev->children = ebus_alloc(sizeof(struct linux_ebus_child));
503
504 child = dev->children;
505 child->next = NULL;
506 child->parent = dev;
507 child->bus = dev->bus;
508 fill_ebus_child(node, &regs[0],
509 child, child_regs_nonstandard(dev));
510
511 while ((node = prom_getsibling(node)) != 0) {
512 child->next = ebus_alloc(sizeof(struct linux_ebus_child));
513
514 child = child->next;
515 child->next = NULL;
516 child->parent = dev;
517 child->bus = dev->bus;
518 fill_ebus_child(node, &regs[0],
519 child, child_regs_nonstandard(dev));
520 }
521 }
522 printk("]");
523}
524
525static struct pci_dev *find_next_ebus(struct pci_dev *start, int *is_rio_p)
526{
527 struct pci_dev *pdev = start;
528
Jiri Slabyd08fa1a2005-11-06 23:39:35 -0800529 while ((pdev = pci_get_device(PCI_VENDOR_ID_SUN, PCI_ANY_ID, pdev)))
530 if (pdev->device == PCI_DEVICE_ID_SUN_EBUS ||
531 pdev->device == PCI_DEVICE_ID_SUN_RIO_EBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Jiri Slabyd08fa1a2005-11-06 23:39:35 -0800534 *is_rio_p = !!(pdev && (pdev->device == PCI_DEVICE_ID_SUN_RIO_EBUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 return pdev;
537}
538
539void __init ebus_init(void)
540{
541 struct pci_pbm_info *pbm;
542 struct linux_ebus_device *dev;
543 struct linux_ebus *ebus;
544 struct pci_dev *pdev;
545 struct pcidev_cookie *cookie;
546 int nd, ebusnd, is_rio;
547 int num_ebus = 0;
548
549 pdev = find_next_ebus(NULL, &is_rio);
550 if (!pdev) {
551 printk("ebus: No EBus's found.\n");
552 return;
553 }
554
555 cookie = pdev->sysdata;
556 ebusnd = cookie->prom_node;
557
558 ebus_chain = ebus = ebus_alloc(sizeof(struct linux_ebus));
559 ebus->next = NULL;
560 ebus->is_rio = is_rio;
561
562 while (ebusnd) {
563 /* SUNW,pci-qfe uses four empty ebuses on it.
564 I think we should not consider them here,
565 as they have half of the properties this
566 code expects and once we do PCI hot-plug,
567 we'd have to tweak with the ebus_chain
568 in the runtime after initialization. -jj */
569 if (!prom_getchild (ebusnd)) {
570 pdev = find_next_ebus(pdev, &is_rio);
571 if (!pdev) {
572 if (ebus == ebus_chain) {
573 ebus_chain = NULL;
574 printk("ebus: No EBus's found.\n");
575 return;
576 }
577 break;
578 }
579 ebus->is_rio = is_rio;
580 cookie = pdev->sysdata;
581 ebusnd = cookie->prom_node;
582 continue;
583 }
584 printk("ebus%d:", num_ebus);
585
586 prom_getstring(ebusnd, "name", ebus->prom_name, sizeof(ebus->prom_name));
587 ebus->index = num_ebus;
588 ebus->prom_node = ebusnd;
589 ebus->self = pdev;
590 ebus->parent = pbm = cookie->pbm;
591
592 ebus_ranges_init(ebus);
593 ebus_intmap_init(ebus);
594
595 nd = prom_getchild(ebusnd);
596 if (!nd)
597 goto next_ebus;
598
599 ebus->devices = ebus_alloc(sizeof(struct linux_ebus_device));
600
601 dev = ebus->devices;
602 dev->next = NULL;
603 dev->children = NULL;
604 dev->bus = ebus;
605 fill_ebus_device(nd, dev);
606
607 while ((nd = prom_getsibling(nd)) != 0) {
608 dev->next = ebus_alloc(sizeof(struct linux_ebus_device));
609
610 dev = dev->next;
611 dev->next = NULL;
612 dev->children = NULL;
613 dev->bus = ebus;
614 fill_ebus_device(nd, dev);
615 }
616
617 next_ebus:
618 printk("\n");
619
620 pdev = find_next_ebus(pdev, &is_rio);
621 if (!pdev)
622 break;
623
624 cookie = pdev->sysdata;
625 ebusnd = cookie->prom_node;
626
627 ebus->next = ebus_alloc(sizeof(struct linux_ebus));
628 ebus = ebus->next;
629 ebus->next = NULL;
630 ebus->is_rio = is_rio;
631 ++num_ebus;
632 }
Jiri Slabyd08fa1a2005-11-06 23:39:35 -0800633 pci_dev_put(pdev); /* XXX for the case, when ebusnd is 0, is it OK? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635#ifdef CONFIG_SUN_AUXIO
636 auxio_probe();
637#endif
638}