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