xref: /openbmc/linux/arch/sh/drivers/dma/dmabrg.c (revision fc467a2623029976899261d6d379779c950ddcba)
1*fc467a26SManuel Lauss /*
2*fc467a26SManuel Lauss  * SH7760 DMABRG IRQ handling
3*fc467a26SManuel Lauss  *
4*fc467a26SManuel Lauss  * (c) 2007 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
5*fc467a26SManuel Lauss  *  licensed under the GPLv2.
6*fc467a26SManuel Lauss  *
7*fc467a26SManuel Lauss  */
8*fc467a26SManuel Lauss 
9*fc467a26SManuel Lauss #include <linux/interrupt.h>
10*fc467a26SManuel Lauss #include <linux/kernel.h>
11*fc467a26SManuel Lauss #include <asm/dma.h>
12*fc467a26SManuel Lauss #include <asm/dmabrg.h>
13*fc467a26SManuel Lauss #include <asm/io.h>
14*fc467a26SManuel Lauss 
15*fc467a26SManuel Lauss /*
16*fc467a26SManuel Lauss  * The DMABRG is a special DMA unit within the SH7760. It does transfers
17*fc467a26SManuel Lauss  * from USB-SRAM/Audio units to main memory (and also the LCDC; but that
18*fc467a26SManuel Lauss  * part is sensibly placed  in the LCDC  registers and requires no irqs)
19*fc467a26SManuel Lauss  * It has 3 IRQ lines which trigger 10 events, and works independently
20*fc467a26SManuel Lauss  * from the traditional SH DMAC (although it blocks usage of DMAC 0)
21*fc467a26SManuel Lauss  *
22*fc467a26SManuel Lauss  * BRGIRQID   | component | dir | meaning      | source
23*fc467a26SManuel Lauss  * -----------------------------------------------------
24*fc467a26SManuel Lauss  *     0      | USB-DMA   | ... | xfer done    | DMABRGI1
25*fc467a26SManuel Lauss  *     1      | USB-UAE   | ... | USB addr err.| DMABRGI0
26*fc467a26SManuel Lauss  *     2      | HAC0/SSI0 | play| all done     | DMABRGI1
27*fc467a26SManuel Lauss  *     3      | HAC0/SSI0 | play| half done    | DMABRGI2
28*fc467a26SManuel Lauss  *     4      | HAC0/SSI0 | rec | all done     | DMABRGI1
29*fc467a26SManuel Lauss  *     5      | HAC0/SSI0 | rec | half done    | DMABRGI2
30*fc467a26SManuel Lauss  *     6      | HAC1/SSI1 | play| all done     | DMABRGI1
31*fc467a26SManuel Lauss  *     7      | HAC1/SSI1 | play| half done    | DMABRGI2
32*fc467a26SManuel Lauss  *     8      | HAC1/SSI1 | rec | all done     | DMABRGI1
33*fc467a26SManuel Lauss  *     9      | HAC1/SSI1 | rec | half done    | DMABRGI2
34*fc467a26SManuel Lauss  *
35*fc467a26SManuel Lauss  * all can be enabled/disabled in the DMABRGCR register,
36*fc467a26SManuel Lauss  * as well as checked if they occured.
37*fc467a26SManuel Lauss  *
38*fc467a26SManuel Lauss  * DMABRGI0 services  USB  DMA  Address  errors,  but it still must be
39*fc467a26SManuel Lauss  * enabled/acked in the DMABRGCR register.  USB-DMA complete indicator
40*fc467a26SManuel Lauss  * is grouped together with the audio buffer end indicators, too bad...
41*fc467a26SManuel Lauss  *
42*fc467a26SManuel Lauss  * DMABRGCR:	Bits 31-24: audio-dma ENABLE flags,
43*fc467a26SManuel Lauss  *		Bits 23-16: audio-dma STATUS flags,
44*fc467a26SManuel Lauss  *		Bits  9-8:  USB error/xfer ENABLE,
45*fc467a26SManuel Lauss  *		Bits  1-0:  USB error/xfer STATUS.
46*fc467a26SManuel Lauss  *	Ack an IRQ by writing 0 to the STATUS flag.
47*fc467a26SManuel Lauss  *	Mask IRQ by writing 0 to ENABLE flag.
48*fc467a26SManuel Lauss  *
49*fc467a26SManuel Lauss  * Usage is almost like with any other IRQ:
50*fc467a26SManuel Lauss  *  dmabrg_request_irq(BRGIRQID, handler, data)
51*fc467a26SManuel Lauss  *  dmabrg_free_irq(BRGIRQID)
52*fc467a26SManuel Lauss  *
53*fc467a26SManuel Lauss  * handler prototype:  void brgirqhandler(void *data)
54*fc467a26SManuel Lauss  */
55*fc467a26SManuel Lauss 
56*fc467a26SManuel Lauss #define DMARSRA		0xfe090000
57*fc467a26SManuel Lauss #define DMAOR		0xffa00040
58*fc467a26SManuel Lauss #define DMACHCR0	0xffa0000c
59*fc467a26SManuel Lauss #define DMABRGCR	0xfe3c0000
60*fc467a26SManuel Lauss 
61*fc467a26SManuel Lauss #define DMAOR_BRG	0x0000c000
62*fc467a26SManuel Lauss #define DMAOR_DMEN	0x00000001
63*fc467a26SManuel Lauss 
64*fc467a26SManuel Lauss #define DMABRGI0	68
65*fc467a26SManuel Lauss #define DMABRGI1	69
66*fc467a26SManuel Lauss #define DMABRGI2	70
67*fc467a26SManuel Lauss 
68*fc467a26SManuel Lauss struct dmabrg_handler {
69*fc467a26SManuel Lauss 	void (*handler)(void *);
70*fc467a26SManuel Lauss 	void *data;
71*fc467a26SManuel Lauss } *dmabrg_handlers;
72*fc467a26SManuel Lauss 
73*fc467a26SManuel Lauss static inline void dmabrg_call_handler(int i)
74*fc467a26SManuel Lauss {
75*fc467a26SManuel Lauss 	dmabrg_handlers[i].handler(dmabrg_handlers[i].data);
76*fc467a26SManuel Lauss }
77*fc467a26SManuel Lauss 
78*fc467a26SManuel Lauss /*
79*fc467a26SManuel Lauss  * main DMABRG irq handler. It acks irqs and then
80*fc467a26SManuel Lauss  * handles every set and unmasked bit sequentially.
81*fc467a26SManuel Lauss  * No locking and no validity checks; it should be
82*fc467a26SManuel Lauss  * as fast as possible (audio!)
83*fc467a26SManuel Lauss  */
84*fc467a26SManuel Lauss static irqreturn_t dmabrg_irq(int irq, void *data)
85*fc467a26SManuel Lauss {
86*fc467a26SManuel Lauss 	unsigned long dcr;
87*fc467a26SManuel Lauss 	unsigned int i;
88*fc467a26SManuel Lauss 
89*fc467a26SManuel Lauss 	dcr = ctrl_inl(DMABRGCR);
90*fc467a26SManuel Lauss 	ctrl_outl(dcr & ~0x00ff0003, DMABRGCR);	/* ack all */
91*fc467a26SManuel Lauss 	dcr &= dcr >> 8;	/* ignore masked */
92*fc467a26SManuel Lauss 
93*fc467a26SManuel Lauss 	/* USB stuff, get it out of the way first */
94*fc467a26SManuel Lauss 	if (dcr & 1)
95*fc467a26SManuel Lauss 		dmabrg_call_handler(DMABRGIRQ_USBDMA);
96*fc467a26SManuel Lauss 	if (dcr & 2)
97*fc467a26SManuel Lauss 		dmabrg_call_handler(DMABRGIRQ_USBDMAERR);
98*fc467a26SManuel Lauss 
99*fc467a26SManuel Lauss 	/* Audio */
100*fc467a26SManuel Lauss 	dcr >>= 16;
101*fc467a26SManuel Lauss 	while (dcr) {
102*fc467a26SManuel Lauss 		i = __ffs(dcr);
103*fc467a26SManuel Lauss 		dcr &= dcr - 1;
104*fc467a26SManuel Lauss 		dmabrg_call_handler(i + DMABRGIRQ_A0TXF);
105*fc467a26SManuel Lauss 	}
106*fc467a26SManuel Lauss 	return IRQ_HANDLED;
107*fc467a26SManuel Lauss }
108*fc467a26SManuel Lauss 
109*fc467a26SManuel Lauss static void dmabrg_disable_irq(unsigned int dmairq)
110*fc467a26SManuel Lauss {
111*fc467a26SManuel Lauss 	unsigned long dcr;
112*fc467a26SManuel Lauss 	dcr = ctrl_inl(DMABRGCR);
113*fc467a26SManuel Lauss 	dcr &= ~(1 << ((dmairq > 1) ? dmairq + 22 : dmairq + 8));
114*fc467a26SManuel Lauss 	ctrl_outl(dcr, DMABRGCR);
115*fc467a26SManuel Lauss }
116*fc467a26SManuel Lauss 
117*fc467a26SManuel Lauss static void dmabrg_enable_irq(unsigned int dmairq)
118*fc467a26SManuel Lauss {
119*fc467a26SManuel Lauss 	unsigned long dcr;
120*fc467a26SManuel Lauss 	dcr = ctrl_inl(DMABRGCR);
121*fc467a26SManuel Lauss 	dcr |= (1 << ((dmairq > 1) ? dmairq + 22 : dmairq + 8));
122*fc467a26SManuel Lauss 	ctrl_outl(dcr, DMABRGCR);
123*fc467a26SManuel Lauss }
124*fc467a26SManuel Lauss 
125*fc467a26SManuel Lauss int dmabrg_request_irq(unsigned int dmairq, void(*handler)(void*),
126*fc467a26SManuel Lauss 		       void *data)
127*fc467a26SManuel Lauss {
128*fc467a26SManuel Lauss 	if ((dmairq > 9) || !handler)
129*fc467a26SManuel Lauss 		return -ENOENT;
130*fc467a26SManuel Lauss 	if (dmabrg_handlers[dmairq].handler)
131*fc467a26SManuel Lauss 		return -EBUSY;
132*fc467a26SManuel Lauss 
133*fc467a26SManuel Lauss 	dmabrg_handlers[dmairq].handler = handler;
134*fc467a26SManuel Lauss 	dmabrg_handlers[dmairq].data = data;
135*fc467a26SManuel Lauss 
136*fc467a26SManuel Lauss 	dmabrg_enable_irq(dmairq);
137*fc467a26SManuel Lauss 	return 0;
138*fc467a26SManuel Lauss }
139*fc467a26SManuel Lauss EXPORT_SYMBOL_GPL(dmabrg_request_irq);
140*fc467a26SManuel Lauss 
141*fc467a26SManuel Lauss void dmabrg_free_irq(unsigned int dmairq)
142*fc467a26SManuel Lauss {
143*fc467a26SManuel Lauss 	if (likely(dmairq < 10)) {
144*fc467a26SManuel Lauss 		dmabrg_disable_irq(dmairq);
145*fc467a26SManuel Lauss 		dmabrg_handlers[dmairq].handler = NULL;
146*fc467a26SManuel Lauss 		dmabrg_handlers[dmairq].data = NULL;
147*fc467a26SManuel Lauss 	}
148*fc467a26SManuel Lauss }
149*fc467a26SManuel Lauss EXPORT_SYMBOL_GPL(dmabrg_free_irq);
150*fc467a26SManuel Lauss 
151*fc467a26SManuel Lauss static int __init dmabrg_init(void)
152*fc467a26SManuel Lauss {
153*fc467a26SManuel Lauss 	unsigned long or;
154*fc467a26SManuel Lauss 	int ret;
155*fc467a26SManuel Lauss 
156*fc467a26SManuel Lauss 	dmabrg_handlers = kzalloc(10 * sizeof(struct dmabrg_handler),
157*fc467a26SManuel Lauss 				  GFP_KERNEL);
158*fc467a26SManuel Lauss 	if (!dmabrg_handlers)
159*fc467a26SManuel Lauss 		return -ENOMEM;
160*fc467a26SManuel Lauss 
161*fc467a26SManuel Lauss #ifdef CONFIG_SH_DMA
162*fc467a26SManuel Lauss 	/* request DMAC channel 0 before anyone else can get it */
163*fc467a26SManuel Lauss 	ret = request_dma(0, "DMAC 0 (DMABRG)");
164*fc467a26SManuel Lauss 	if (ret < 0)
165*fc467a26SManuel Lauss 		printk(KERN_INFO "DMABRG: DMAC ch0 not reserved!\n");
166*fc467a26SManuel Lauss #endif
167*fc467a26SManuel Lauss 
168*fc467a26SManuel Lauss 	ctrl_outl(0, DMABRGCR);
169*fc467a26SManuel Lauss 	ctrl_outl(0, DMACHCR0);
170*fc467a26SManuel Lauss 	ctrl_outl(0x94000000, DMARSRA);	/* enable DMABRG in DMAC 0 */
171*fc467a26SManuel Lauss 
172*fc467a26SManuel Lauss 	/* enable DMABRG mode, enable the DMAC */
173*fc467a26SManuel Lauss 	or = ctrl_inl(DMAOR);
174*fc467a26SManuel Lauss 	ctrl_outl(or | DMAOR_BRG | DMAOR_DMEN, DMAOR);
175*fc467a26SManuel Lauss 
176*fc467a26SManuel Lauss 	ret = request_irq(DMABRGI0, dmabrg_irq, IRQF_DISABLED,
177*fc467a26SManuel Lauss 			"DMABRG USB address error", NULL);
178*fc467a26SManuel Lauss 	if (ret)
179*fc467a26SManuel Lauss 		goto out0;
180*fc467a26SManuel Lauss 
181*fc467a26SManuel Lauss 	ret = request_irq(DMABRGI1, dmabrg_irq, IRQF_DISABLED,
182*fc467a26SManuel Lauss 			"DMABRG Transfer End", NULL);
183*fc467a26SManuel Lauss 	if (ret)
184*fc467a26SManuel Lauss 		goto out1;
185*fc467a26SManuel Lauss 
186*fc467a26SManuel Lauss 	ret = request_irq(DMABRGI2, dmabrg_irq, IRQF_DISABLED,
187*fc467a26SManuel Lauss 			"DMABRG Transfer Half", NULL);
188*fc467a26SManuel Lauss 	if (ret == 0)
189*fc467a26SManuel Lauss 		return ret;
190*fc467a26SManuel Lauss 
191*fc467a26SManuel Lauss 	free_irq(DMABRGI1, 0);
192*fc467a26SManuel Lauss out1:	free_irq(DMABRGI0, 0);
193*fc467a26SManuel Lauss out0:	kfree(dmabrg_handlers);
194*fc467a26SManuel Lauss 	return ret;
195*fc467a26SManuel Lauss }
196*fc467a26SManuel Lauss subsys_initcall(dmabrg_init);
197