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/module.h>
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
16 #include <linux/pci.h>
18 #include <asm/system.h>
21 #include <asm/oplib.h>
23 #include <asm/of_device.h>
28 /* EBUS dma library. */
30 #define EBDMA_CSR 0x00UL /* Control/Status */
31 #define EBDMA_ADDR 0x04UL /* DMA Address */
32 #define EBDMA_COUNT 0x08UL /* DMA Count */
34 #define EBDMA_CSR_INT_PEND 0x00000001
35 #define EBDMA_CSR_ERR_PEND 0x00000002
36 #define EBDMA_CSR_DRAIN 0x00000004
37 #define EBDMA_CSR_INT_EN 0x00000010
38 #define EBDMA_CSR_RESET 0x00000080
39 #define EBDMA_CSR_WRITE 0x00000100
40 #define EBDMA_CSR_EN_DMA 0x00000200
41 #define EBDMA_CSR_CYC_PEND 0x00000400
42 #define EBDMA_CSR_DIAG_RD_DONE 0x00000800
43 #define EBDMA_CSR_DIAG_WR_DONE 0x00001000
44 #define EBDMA_CSR_EN_CNT 0x00002000
45 #define EBDMA_CSR_TC 0x00004000
46 #define EBDMA_CSR_DIS_CSR_DRN 0x00010000
47 #define EBDMA_CSR_BURST_SZ_MASK 0x000c0000
48 #define EBDMA_CSR_BURST_SZ_1 0x00080000
49 #define EBDMA_CSR_BURST_SZ_4 0x00000000
50 #define EBDMA_CSR_BURST_SZ_8 0x00040000
51 #define EBDMA_CSR_BURST_SZ_16 0x000c0000
52 #define EBDMA_CSR_DIAG_EN 0x00100000
53 #define EBDMA_CSR_DIS_ERR_PEND 0x00400000
54 #define EBDMA_CSR_TCI_DIS 0x00800000
55 #define EBDMA_CSR_EN_NEXT 0x01000000
56 #define EBDMA_CSR_DMA_ON 0x02000000
57 #define EBDMA_CSR_A_LOADED 0x04000000
58 #define EBDMA_CSR_NA_LOADED 0x08000000
59 #define EBDMA_CSR_DEV_ID_MASK 0xf0000000
61 #define EBUS_DMA_RESET_TIMEOUT 10000
63 static void __ebus_dma_reset(struct ebus_dma_info
*p
, int no_drain
)
68 writel(EBDMA_CSR_RESET
, p
->regs
+ EBDMA_CSR
);
74 for (i
= EBUS_DMA_RESET_TIMEOUT
; i
> 0; i
--) {
75 val
= readl(p
->regs
+ EBDMA_CSR
);
77 if (!(val
& (EBDMA_CSR_DRAIN
| EBDMA_CSR_CYC_PEND
)))
83 static irqreturn_t
ebus_dma_irq(int irq
, void *dev_id
)
85 struct ebus_dma_info
*p
= dev_id
;
89 spin_lock_irqsave(&p
->lock
, flags
);
90 csr
= readl(p
->regs
+ EBDMA_CSR
);
91 writel(csr
, p
->regs
+ EBDMA_CSR
);
92 spin_unlock_irqrestore(&p
->lock
, flags
);
94 if (csr
& EBDMA_CSR_ERR_PEND
) {
95 printk(KERN_CRIT
"ebus_dma(%s): DMA error!\n", p
->name
);
96 p
->callback(p
, EBUS_DMA_EVENT_ERROR
, p
->client_cookie
);
98 } else if (csr
& EBDMA_CSR_INT_PEND
) {
100 (csr
& EBDMA_CSR_TC
) ?
101 EBUS_DMA_EVENT_DMA
: EBUS_DMA_EVENT_DEVICE
,
110 int ebus_dma_register(struct ebus_dma_info
*p
)
116 if (p
->flags
& ~(EBUS_DMA_FLAG_USE_EBDMA_HANDLER
|
117 EBUS_DMA_FLAG_TCI_DISABLE
))
119 if ((p
->flags
& EBUS_DMA_FLAG_USE_EBDMA_HANDLER
) && !p
->callback
)
121 if (!strlen(p
->name
))
124 __ebus_dma_reset(p
, 1);
126 csr
= EBDMA_CSR_BURST_SZ_16
| EBDMA_CSR_EN_CNT
;
128 if (p
->flags
& EBUS_DMA_FLAG_TCI_DISABLE
)
129 csr
|= EBDMA_CSR_TCI_DIS
;
131 writel(csr
, p
->regs
+ EBDMA_CSR
);
135 EXPORT_SYMBOL(ebus_dma_register
);
137 int ebus_dma_irq_enable(struct ebus_dma_info
*p
, int on
)
143 if (p
->flags
& EBUS_DMA_FLAG_USE_EBDMA_HANDLER
) {
144 if (request_irq(p
->irq
, ebus_dma_irq
, IRQF_SHARED
, p
->name
, p
))
148 spin_lock_irqsave(&p
->lock
, flags
);
149 csr
= readl(p
->regs
+ EBDMA_CSR
);
150 csr
|= EBDMA_CSR_INT_EN
;
151 writel(csr
, p
->regs
+ EBDMA_CSR
);
152 spin_unlock_irqrestore(&p
->lock
, flags
);
154 spin_lock_irqsave(&p
->lock
, flags
);
155 csr
= readl(p
->regs
+ EBDMA_CSR
);
156 csr
&= ~EBDMA_CSR_INT_EN
;
157 writel(csr
, p
->regs
+ EBDMA_CSR
);
158 spin_unlock_irqrestore(&p
->lock
, flags
);
160 if (p
->flags
& EBUS_DMA_FLAG_USE_EBDMA_HANDLER
) {
167 EXPORT_SYMBOL(ebus_dma_irq_enable
);
169 void ebus_dma_unregister(struct ebus_dma_info
*p
)
175 spin_lock_irqsave(&p
->lock
, flags
);
176 csr
= readl(p
->regs
+ EBDMA_CSR
);
177 if (csr
& EBDMA_CSR_INT_EN
) {
178 csr
&= ~EBDMA_CSR_INT_EN
;
179 writel(csr
, p
->regs
+ EBDMA_CSR
);
182 spin_unlock_irqrestore(&p
->lock
, flags
);
187 EXPORT_SYMBOL(ebus_dma_unregister
);
189 int ebus_dma_request(struct ebus_dma_info
*p
, dma_addr_t bus_addr
, size_t len
)
195 if (len
>= (1 << 24))
198 spin_lock_irqsave(&p
->lock
, flags
);
199 csr
= readl(p
->regs
+ EBDMA_CSR
);
201 if (!(csr
& EBDMA_CSR_EN_DMA
))
204 if (csr
& EBDMA_CSR_NA_LOADED
)
207 writel(len
, p
->regs
+ EBDMA_COUNT
);
208 writel(bus_addr
, p
->regs
+ EBDMA_ADDR
);
212 spin_unlock_irqrestore(&p
->lock
, flags
);
216 EXPORT_SYMBOL(ebus_dma_request
);
218 void ebus_dma_prepare(struct ebus_dma_info
*p
, int write
)
223 spin_lock_irqsave(&p
->lock
, flags
);
224 __ebus_dma_reset(p
, 0);
226 csr
= (EBDMA_CSR_INT_EN
|
228 EBDMA_CSR_BURST_SZ_16
|
232 csr
|= EBDMA_CSR_WRITE
;
233 if (p
->flags
& EBUS_DMA_FLAG_TCI_DISABLE
)
234 csr
|= EBDMA_CSR_TCI_DIS
;
236 writel(csr
, p
->regs
+ EBDMA_CSR
);
238 spin_unlock_irqrestore(&p
->lock
, flags
);
240 EXPORT_SYMBOL(ebus_dma_prepare
);
242 unsigned int ebus_dma_residue(struct ebus_dma_info
*p
)
244 return readl(p
->regs
+ EBDMA_COUNT
);
246 EXPORT_SYMBOL(ebus_dma_residue
);
248 unsigned int ebus_dma_addr(struct ebus_dma_info
*p
)
250 return readl(p
->regs
+ EBDMA_ADDR
);
252 EXPORT_SYMBOL(ebus_dma_addr
);
254 void ebus_dma_enable(struct ebus_dma_info
*p
, int on
)
259 spin_lock_irqsave(&p
->lock
, flags
);
260 orig_csr
= csr
= readl(p
->regs
+ EBDMA_CSR
);
262 csr
|= EBDMA_CSR_EN_DMA
;
264 csr
&= ~EBDMA_CSR_EN_DMA
;
265 if ((orig_csr
& EBDMA_CSR_EN_DMA
) !=
266 (csr
& EBDMA_CSR_EN_DMA
))
267 writel(csr
, p
->regs
+ EBDMA_CSR
);
268 spin_unlock_irqrestore(&p
->lock
, flags
);
270 EXPORT_SYMBOL(ebus_dma_enable
);
272 struct linux_ebus
*ebus_chain
= NULL
;
274 static inline void *ebus_alloc(size_t size
)
278 mem
= kzalloc(size
, GFP_ATOMIC
);
280 panic("ebus_alloc: out of memory");
284 static void __init
fill_ebus_child(struct device_node
*dp
,
285 struct linux_ebus_child
*dev
,
286 int non_standard_regs
)
288 struct of_device
*op
;
293 printk(" (%s)", dp
->name
);
295 regs
= of_get_property(dp
, "reg", &len
);
299 dev
->num_addrs
= len
/ sizeof(regs
[0]);
301 if (non_standard_regs
) {
302 /* This is to handle reg properties which are not
303 * in the parent relative format. One example are
304 * children of the i2c device on CompactPCI systems.
306 * So, for such devices we just record the property
307 * raw in the child resources.
309 for (i
= 0; i
< dev
->num_addrs
; i
++)
310 dev
->resource
[i
].start
= regs
[i
];
312 for (i
= 0; i
< dev
->num_addrs
; i
++) {
314 if (rnum
>= dev
->parent
->num_addrs
) {
315 prom_printf("UGH: property for %s was %d, need < %d\n",
316 dp
->name
, len
, dev
->parent
->num_addrs
);
319 dev
->resource
[i
].start
= dev
->parent
->resource
[i
].start
;
320 dev
->resource
[i
].end
= dev
->parent
->resource
[i
].end
;
321 dev
->resource
[i
].flags
= IORESOURCE_MEM
;
322 dev
->resource
[i
].name
= dp
->name
;
326 op
= of_find_device_by_node(dp
);
330 dev
->num_irqs
= op
->num_irqs
;
331 for (i
= 0; i
< dev
->num_irqs
; i
++)
332 dev
->irqs
[i
] = op
->irqs
[i
];
335 if (!dev
->num_irqs
) {
337 * Oh, well, some PROMs don't export interrupts
338 * property to children of EBus devices...
340 * Be smart about PS/2 keyboard and mouse.
342 if (!strcmp(dev
->parent
->prom_node
->name
, "8042")) {
343 if (!strcmp(dev
->prom_node
->name
, "kb_ps2")) {
345 dev
->irqs
[0] = dev
->parent
->irqs
[0];
348 dev
->irqs
[0] = dev
->parent
->irqs
[1];
354 static int __init
child_regs_nonstandard(struct linux_ebus_device
*dev
)
356 if (!strcmp(dev
->prom_node
->name
, "i2c") ||
357 !strcmp(dev
->prom_node
->name
, "SUNW,lombus"))
362 static void __init
fill_ebus_device(struct device_node
*dp
, struct linux_ebus_device
*dev
)
364 struct linux_ebus_child
*child
;
365 struct of_device
*op
;
370 printk(" [%s", dp
->name
);
372 op
= of_find_device_by_node(dp
);
377 (void) of_get_property(dp
, "reg", &len
);
378 dev
->num_addrs
= len
/ sizeof(struct linux_prom_registers
);
380 for (i
= 0; i
< dev
->num_addrs
; i
++)
381 memcpy(&dev
->resource
[i
],
383 sizeof(struct resource
));
385 dev
->num_irqs
= op
->num_irqs
;
386 for (i
= 0; i
< dev
->num_irqs
; i
++)
387 dev
->irqs
[i
] = op
->irqs
[i
];
390 dev
->ofdev
.node
= dp
;
391 dev
->ofdev
.dev
.parent
= &dev
->bus
->ofdev
.dev
;
392 dev
->ofdev
.dev
.bus
= &ebus_bus_type
;
393 sprintf(dev
->ofdev
.dev
.bus_id
, "ebus[%08x]", dp
->node
);
395 /* Register with core */
396 if (of_device_register(&dev
->ofdev
) != 0)
397 printk(KERN_DEBUG
"ebus: device registration error for %s!\n",
398 dp
->path_component_name
);
403 dev
->children
= ebus_alloc(sizeof(struct linux_ebus_child
));
405 child
= dev
->children
;
408 child
->bus
= dev
->bus
;
409 fill_ebus_child(dp
, child
,
410 child_regs_nonstandard(dev
));
412 while ((dp
= dp
->sibling
) != NULL
) {
413 child
->next
= ebus_alloc(sizeof(struct linux_ebus_child
));
418 child
->bus
= dev
->bus
;
419 fill_ebus_child(dp
, child
,
420 child_regs_nonstandard(dev
));
426 static struct pci_dev
*find_next_ebus(struct pci_dev
*start
, int *is_rio_p
)
428 struct pci_dev
*pdev
= start
;
430 while ((pdev
= pci_get_device(PCI_VENDOR_ID_SUN
, PCI_ANY_ID
, pdev
)))
431 if (pdev
->device
== PCI_DEVICE_ID_SUN_EBUS
||
432 pdev
->device
== PCI_DEVICE_ID_SUN_RIO_EBUS
)
435 *is_rio_p
= !!(pdev
&& (pdev
->device
== PCI_DEVICE_ID_SUN_RIO_EBUS
));
440 void __init
ebus_init(void)
442 struct linux_ebus_device
*dev
;
443 struct linux_ebus
*ebus
;
444 struct pci_dev
*pdev
;
445 struct device_node
*dp
;
449 pdev
= find_next_ebus(NULL
, &is_rio
);
451 printk("ebus: No EBus's found.\n");
455 dp
= pci_device_to_OF_node(pdev
);
457 ebus_chain
= ebus
= ebus_alloc(sizeof(struct linux_ebus
));
459 ebus
->is_rio
= is_rio
;
462 struct device_node
*child
;
464 /* SUNW,pci-qfe uses four empty ebuses on it.
465 I think we should not consider them here,
466 as they have half of the properties this
467 code expects and once we do PCI hot-plug,
468 we'd have to tweak with the ebus_chain
469 in the runtime after initialization. -jj */
471 pdev
= find_next_ebus(pdev
, &is_rio
);
473 if (ebus
== ebus_chain
) {
475 printk("ebus: No EBus's found.\n");
480 ebus
->is_rio
= is_rio
;
481 dp
= pci_device_to_OF_node(pdev
);
484 printk("ebus%d:", num_ebus
);
486 ebus
->index
= num_ebus
;
487 ebus
->prom_node
= dp
;
490 ebus
->ofdev
.node
= dp
;
491 ebus
->ofdev
.dev
.parent
= &pdev
->dev
;
492 ebus
->ofdev
.dev
.bus
= &ebus_bus_type
;
493 sprintf(ebus
->ofdev
.dev
.bus_id
, "ebus%d", num_ebus
);
495 /* Register with core */
496 if (of_device_register(&ebus
->ofdev
) != 0)
497 printk(KERN_DEBUG
"ebus: device registration error for %s!\n",
498 dp
->path_component_name
);
505 ebus
->devices
= ebus_alloc(sizeof(struct linux_ebus_device
));
509 dev
->children
= NULL
;
511 fill_ebus_device(child
, dev
);
513 while ((child
= child
->sibling
) != NULL
) {
514 dev
->next
= ebus_alloc(sizeof(struct linux_ebus_device
));
518 dev
->children
= NULL
;
520 fill_ebus_device(child
, dev
);
526 pdev
= find_next_ebus(pdev
, &is_rio
);
530 dp
= pci_device_to_OF_node(pdev
);
532 ebus
->next
= ebus_alloc(sizeof(struct linux_ebus
));
535 ebus
->is_rio
= is_rio
;
538 pci_dev_put(pdev
); /* XXX for the case, when ebusnd is 0, is it OK? */