xref: /linux/arch/mips/alchemy/common/dma.c (revision 8617d7d6298f54dfef4038281863270b5864fe83)
1e8c7c482SRalf Baechle /*
2e8c7c482SRalf Baechle  *
3e8c7c482SRalf Baechle  * BRIEF MODULE DESCRIPTION
4e8c7c482SRalf Baechle  *      A DMA channel allocator for Au1x00. API is modeled loosely off of
5e8c7c482SRalf Baechle  *      linux/kernel/dma.c.
6e8c7c482SRalf Baechle  *
7e8c7c482SRalf Baechle  * Copyright 2000, 2008 MontaVista Software Inc.
8e8c7c482SRalf Baechle  * Author: MontaVista Software, Inc. <source@mvista.com>
9e8c7c482SRalf Baechle  * Copyright (C) 2005 Ralf Baechle (ralf@linux-mips.org)
10e8c7c482SRalf Baechle  *
11e8c7c482SRalf Baechle  *  This program is free software; you can redistribute  it and/or modify it
12e8c7c482SRalf Baechle  *  under  the terms of  the GNU General  Public License as published by the
13e8c7c482SRalf Baechle  *  Free Software Foundation;  either version 2 of the  License, or (at your
14e8c7c482SRalf Baechle  *  option) any later version.
15e8c7c482SRalf Baechle  *
16e8c7c482SRalf Baechle  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
17e8c7c482SRalf Baechle  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
18e8c7c482SRalf Baechle  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
19e8c7c482SRalf Baechle  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
20e8c7c482SRalf Baechle  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21e8c7c482SRalf Baechle  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
22e8c7c482SRalf Baechle  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23e8c7c482SRalf Baechle  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
24e8c7c482SRalf Baechle  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25e8c7c482SRalf Baechle  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26e8c7c482SRalf Baechle  *
27e8c7c482SRalf Baechle  *  You should have received a copy of the  GNU General Public License along
28e8c7c482SRalf Baechle  *  with this program; if not, write  to the Free Software Foundation, Inc.,
29e8c7c482SRalf Baechle  *  675 Mass Ave, Cambridge, MA 02139, USA.
30e8c7c482SRalf Baechle  *
31e8c7c482SRalf Baechle  */
3278814465SManuel Lauss 
3378814465SManuel Lauss #include <linux/init.h>
34*26dd3e4fSPaul Gortmaker #include <linux/export.h>
35e8c7c482SRalf Baechle #include <linux/kernel.h>
36e8c7c482SRalf Baechle #include <linux/errno.h>
37e8c7c482SRalf Baechle #include <linux/spinlock.h>
38e8c7c482SRalf Baechle #include <linux/interrupt.h>
39e8c7c482SRalf Baechle 
40e8c7c482SRalf Baechle #include <asm/mach-au1x00/au1000.h>
41e8c7c482SRalf Baechle #include <asm/mach-au1x00/au1000_dma.h>
42e8c7c482SRalf Baechle 
43e8c7c482SRalf Baechle /*
44e8c7c482SRalf Baechle  * A note on resource allocation:
45e8c7c482SRalf Baechle  *
46e8c7c482SRalf Baechle  * All drivers needing DMA channels, should allocate and release them
47e8c7c482SRalf Baechle  * through the public routines `request_dma()' and `free_dma()'.
48e8c7c482SRalf Baechle  *
49e8c7c482SRalf Baechle  * In order to avoid problems, all processes should allocate resources in
50e8c7c482SRalf Baechle  * the same sequence and release them in the reverse order.
51e8c7c482SRalf Baechle  *
52e8c7c482SRalf Baechle  * So, when allocating DMAs and IRQs, first allocate the DMA, then the IRQ.
53e8c7c482SRalf Baechle  * When releasing them, first release the IRQ, then release the DMA. The
54e8c7c482SRalf Baechle  * main reason for this order is that, if you are requesting the DMA buffer
55e8c7c482SRalf Baechle  * done interrupt, you won't know the irq number until the DMA channel is
56e8c7c482SRalf Baechle  * returned from request_dma.
57e8c7c482SRalf Baechle  */
58e8c7c482SRalf Baechle 
595d4ddcb4SManuel Lauss /* DMA Channel register block spacing */
605d4ddcb4SManuel Lauss #define DMA_CHANNEL_LEN		0x00000100
615d4ddcb4SManuel Lauss 
62e8c7c482SRalf Baechle DEFINE_SPINLOCK(au1000_dma_spin_lock);
63e8c7c482SRalf Baechle 
64e8c7c482SRalf Baechle struct dma_chan au1000_dma_table[NUM_AU1000_DMA_CHANNELS] = {
65e8c7c482SRalf Baechle       {.dev_id = -1,},
66e8c7c482SRalf Baechle       {.dev_id = -1,},
67e8c7c482SRalf Baechle       {.dev_id = -1,},
68e8c7c482SRalf Baechle       {.dev_id = -1,},
69e8c7c482SRalf Baechle       {.dev_id = -1,},
70e8c7c482SRalf Baechle       {.dev_id = -1,},
71e8c7c482SRalf Baechle       {.dev_id = -1,},
72e8c7c482SRalf Baechle       {.dev_id = -1,}
73e8c7c482SRalf Baechle };
74e8c7c482SRalf Baechle EXPORT_SYMBOL(au1000_dma_table);
75e8c7c482SRalf Baechle 
76e8c7c482SRalf Baechle /* Device FIFO addresses and default DMA modes */
77e8c7c482SRalf Baechle static const struct dma_dev {
78e8c7c482SRalf Baechle 	unsigned int fifo_addr;
79e8c7c482SRalf Baechle 	unsigned int dma_mode;
80e8c7c482SRalf Baechle } dma_dev_table[DMA_NUM_DEV] = {
815d4ddcb4SManuel Lauss 	{ AU1000_UART0_PHYS_ADDR + 0x04, DMA_DW8 },		/* UART0_TX */
825d4ddcb4SManuel Lauss 	{ AU1000_UART0_PHYS_ADDR + 0x00, DMA_DW8 | DMA_DR },	/* UART0_RX */
835d4ddcb4SManuel Lauss 	{ 0, 0 },	/* DMA_REQ0 */
845d4ddcb4SManuel Lauss 	{ 0, 0 },	/* DMA_REQ1 */
855d4ddcb4SManuel Lauss 	{ AU1000_AC97_PHYS_ADDR + 0x08, DMA_DW16 },		/* AC97 TX c */
865d4ddcb4SManuel Lauss 	{ AU1000_AC97_PHYS_ADDR + 0x08, DMA_DW16 | DMA_DR },	/* AC97 RX c */
875d4ddcb4SManuel Lauss 	{ AU1000_UART3_PHYS_ADDR + 0x04, DMA_DW8 | DMA_NC },	/* UART3_TX */
885d4ddcb4SManuel Lauss 	{ AU1000_UART3_PHYS_ADDR + 0x00, DMA_DW8 | DMA_NC | DMA_DR }, /* UART3_RX */
89ce6bc922SManuel Lauss 	{ AU1000_USB_UDC_PHYS_ADDR + 0x00, DMA_DW8 | DMA_NC | DMA_DR }, /* EP0RD */
90ce6bc922SManuel Lauss 	{ AU1000_USB_UDC_PHYS_ADDR + 0x04, DMA_DW8 | DMA_NC }, /* EP0WR */
91ce6bc922SManuel Lauss 	{ AU1000_USB_UDC_PHYS_ADDR + 0x08, DMA_DW8 | DMA_NC }, /* EP2WR */
92ce6bc922SManuel Lauss 	{ AU1000_USB_UDC_PHYS_ADDR + 0x0c, DMA_DW8 | DMA_NC }, /* EP3WR */
93ce6bc922SManuel Lauss 	{ AU1000_USB_UDC_PHYS_ADDR + 0x10, DMA_DW8 | DMA_NC | DMA_DR }, /* EP4RD */
94ce6bc922SManuel Lauss 	{ AU1000_USB_UDC_PHYS_ADDR + 0x14, DMA_DW8 | DMA_NC | DMA_DR }, /* EP5RD */
955d4ddcb4SManuel Lauss 	/* on Au1500, these 2 are DMA_REQ2/3 (GPIO208/209) instead! */
965d4ddcb4SManuel Lauss 	{ AU1000_I2S_PHYS_ADDR + 0x00, DMA_DW32 | DMA_NC},	/* I2S TX */
975d4ddcb4SManuel Lauss 	{ AU1000_I2S_PHYS_ADDR + 0x00, DMA_DW32 | DMA_NC | DMA_DR}, /* I2S RX */
98e8c7c482SRalf Baechle };
99e8c7c482SRalf Baechle 
au1000_dma_read_proc(char * buf,char ** start,off_t fpos,int length,int * eof,void * data)100e8c7c482SRalf Baechle int au1000_dma_read_proc(char *buf, char **start, off_t fpos,
101e8c7c482SRalf Baechle 			 int length, int *eof, void *data)
102e8c7c482SRalf Baechle {
103e8c7c482SRalf Baechle 	int i, len = 0;
104e8c7c482SRalf Baechle 	struct dma_chan *chan;
105e8c7c482SRalf Baechle 
106e8c7c482SRalf Baechle 	for (i = 0; i < NUM_AU1000_DMA_CHANNELS; i++) {
107e8c7c482SRalf Baechle 		chan = get_dma_chan(i);
108e8c7c482SRalf Baechle 		if (chan != NULL)
109e8c7c482SRalf Baechle 			len += sprintf(buf + len, "%2d: %s\n",
110e8c7c482SRalf Baechle 				       i, chan->dev_str);
111e8c7c482SRalf Baechle 	}
112e8c7c482SRalf Baechle 
113e8c7c482SRalf Baechle 	if (fpos >= len) {
114e8c7c482SRalf Baechle 		*start = buf;
115e8c7c482SRalf Baechle 		*eof = 1;
116e8c7c482SRalf Baechle 		return 0;
117e8c7c482SRalf Baechle 	}
118e8c7c482SRalf Baechle 	*start = buf + fpos;
119e8c7c482SRalf Baechle 	len -= fpos;
120e8c7c482SRalf Baechle 	if (len > length)
121e8c7c482SRalf Baechle 		return length;
122e8c7c482SRalf Baechle 	*eof = 1;
123e8c7c482SRalf Baechle 	return len;
124e8c7c482SRalf Baechle }
125e8c7c482SRalf Baechle 
126e8c7c482SRalf Baechle /* Device FIFO addresses and default DMA modes - 2nd bank */
127e8c7c482SRalf Baechle static const struct dma_dev dma_dev_table_bank2[DMA_NUM_DEV_BANK2] = {
1285d4ddcb4SManuel Lauss 	{ AU1100_SD0_PHYS_ADDR + 0x00, DMA_DS | DMA_DW8 },		/* coherent */
1295d4ddcb4SManuel Lauss 	{ AU1100_SD0_PHYS_ADDR + 0x04, DMA_DS | DMA_DW8 | DMA_DR },	/* coherent */
1305d4ddcb4SManuel Lauss 	{ AU1100_SD1_PHYS_ADDR + 0x00, DMA_DS | DMA_DW8 },		/* coherent */
1315d4ddcb4SManuel Lauss 	{ AU1100_SD1_PHYS_ADDR + 0x04, DMA_DS | DMA_DW8 | DMA_DR }	/* coherent */
132e8c7c482SRalf Baechle };
133e8c7c482SRalf Baechle 
134e8c7c482SRalf Baechle /*
135e8c7c482SRalf Baechle  * Finds a free channel, and binds the requested device to it.
136e8c7c482SRalf Baechle  * Returns the allocated channel number, or negative on error.
137e8c7c482SRalf Baechle  * Requests the DMA done IRQ if irqhandler != NULL.
138e8c7c482SRalf Baechle  */
request_au1000_dma(int dev_id,const char * dev_str,irq_handler_t irqhandler,unsigned long irqflags,void * irq_dev_id)139e8c7c482SRalf Baechle int request_au1000_dma(int dev_id, const char *dev_str,
140e8c7c482SRalf Baechle 		       irq_handler_t irqhandler,
141e8c7c482SRalf Baechle 		       unsigned long irqflags,
142e8c7c482SRalf Baechle 		       void *irq_dev_id)
143e8c7c482SRalf Baechle {
144e8c7c482SRalf Baechle 	struct dma_chan *chan;
145e8c7c482SRalf Baechle 	const struct dma_dev *dev;
146e8c7c482SRalf Baechle 	int i, ret;
147e8c7c482SRalf Baechle 
148f2e442fdSManuel Lauss 	if (alchemy_get_cputype() == ALCHEMY_CPU_AU1100) {
149e8c7c482SRalf Baechle 		if (dev_id < 0 || dev_id >= (DMA_NUM_DEV + DMA_NUM_DEV_BANK2))
150e8c7c482SRalf Baechle 			return -EINVAL;
151f2e442fdSManuel Lauss 	} else {
152e8c7c482SRalf Baechle 		if (dev_id < 0 || dev_id >= DMA_NUM_DEV)
153e8c7c482SRalf Baechle 			return -EINVAL;
154f2e442fdSManuel Lauss 	}
155e8c7c482SRalf Baechle 
156e8c7c482SRalf Baechle 	for (i = 0; i < NUM_AU1000_DMA_CHANNELS; i++)
157e8c7c482SRalf Baechle 		if (au1000_dma_table[i].dev_id < 0)
158e8c7c482SRalf Baechle 			break;
159e8c7c482SRalf Baechle 
160e8c7c482SRalf Baechle 	if (i == NUM_AU1000_DMA_CHANNELS)
161e8c7c482SRalf Baechle 		return -ENODEV;
162e8c7c482SRalf Baechle 
163e8c7c482SRalf Baechle 	chan = &au1000_dma_table[i];
164e8c7c482SRalf Baechle 
165e8c7c482SRalf Baechle 	if (dev_id >= DMA_NUM_DEV) {
166e8c7c482SRalf Baechle 		dev_id -= DMA_NUM_DEV;
167e8c7c482SRalf Baechle 		dev = &dma_dev_table_bank2[dev_id];
168e8c7c482SRalf Baechle 	} else
169e8c7c482SRalf Baechle 		dev = &dma_dev_table[dev_id];
170e8c7c482SRalf Baechle 
171e8c7c482SRalf Baechle 	if (irqhandler) {
172e8c7c482SRalf Baechle 		chan->irq_dev = irq_dev_id;
173e8c7c482SRalf Baechle 		ret = request_irq(chan->irq, irqhandler, irqflags, dev_str,
174e8c7c482SRalf Baechle 				  chan->irq_dev);
175e8c7c482SRalf Baechle 		if (ret) {
176e8c7c482SRalf Baechle 			chan->irq_dev = NULL;
177e8c7c482SRalf Baechle 			return ret;
178e8c7c482SRalf Baechle 		}
179e8c7c482SRalf Baechle 	} else {
180e8c7c482SRalf Baechle 		chan->irq_dev = NULL;
181e8c7c482SRalf Baechle 	}
182e8c7c482SRalf Baechle 
183e8c7c482SRalf Baechle 	/* fill it in */
1842f73bfbeSManuel Lauss 	chan->io = (void __iomem *)(KSEG1ADDR(AU1000_DMA_PHYS_ADDR) +
1852f73bfbeSManuel Lauss 			i * DMA_CHANNEL_LEN);
186e8c7c482SRalf Baechle 	chan->dev_id = dev_id;
187e8c7c482SRalf Baechle 	chan->dev_str = dev_str;
188e8c7c482SRalf Baechle 	chan->fifo_addr = dev->fifo_addr;
189e8c7c482SRalf Baechle 	chan->mode = dev->dma_mode;
190e8c7c482SRalf Baechle 
191e8c7c482SRalf Baechle 	/* initialize the channel before returning */
192e8c7c482SRalf Baechle 	init_dma(i);
193e8c7c482SRalf Baechle 
194e8c7c482SRalf Baechle 	return i;
195e8c7c482SRalf Baechle }
196e8c7c482SRalf Baechle EXPORT_SYMBOL(request_au1000_dma);
197e8c7c482SRalf Baechle 
free_au1000_dma(unsigned int dmanr)198e8c7c482SRalf Baechle void free_au1000_dma(unsigned int dmanr)
199e8c7c482SRalf Baechle {
200e8c7c482SRalf Baechle 	struct dma_chan *chan = get_dma_chan(dmanr);
201e8c7c482SRalf Baechle 
202e8c7c482SRalf Baechle 	if (!chan) {
203e8c7c482SRalf Baechle 		printk(KERN_ERR "Error trying to free DMA%d\n", dmanr);
204e8c7c482SRalf Baechle 		return;
205e8c7c482SRalf Baechle 	}
206e8c7c482SRalf Baechle 
207e8c7c482SRalf Baechle 	disable_dma(dmanr);
20878814465SManuel Lauss 	if (chan->irq_dev)
209e8c7c482SRalf Baechle 		free_irq(chan->irq, chan->irq_dev);
210e8c7c482SRalf Baechle 
211e8c7c482SRalf Baechle 	chan->irq_dev = NULL;
212e8c7c482SRalf Baechle 	chan->dev_id = -1;
213e8c7c482SRalf Baechle }
214e8c7c482SRalf Baechle EXPORT_SYMBOL(free_au1000_dma);
215e8c7c482SRalf Baechle 
au1000_dma_init(void)21678814465SManuel Lauss static int __init au1000_dma_init(void)
21778814465SManuel Lauss {
21878814465SManuel Lauss 	int base, i;
21978814465SManuel Lauss 
22078814465SManuel Lauss 	switch (alchemy_get_cputype()) {
22178814465SManuel Lauss 	case ALCHEMY_CPU_AU1000:
22278814465SManuel Lauss 		base = AU1000_DMA_INT_BASE;
22378814465SManuel Lauss 		break;
22478814465SManuel Lauss 	case ALCHEMY_CPU_AU1500:
22578814465SManuel Lauss 		base = AU1500_DMA_INT_BASE;
22678814465SManuel Lauss 		break;
22778814465SManuel Lauss 	case ALCHEMY_CPU_AU1100:
22878814465SManuel Lauss 		base = AU1100_DMA_INT_BASE;
22978814465SManuel Lauss 		break;
23078814465SManuel Lauss 	default:
23178814465SManuel Lauss 		goto out;
23278814465SManuel Lauss 	}
23378814465SManuel Lauss 
23478814465SManuel Lauss 	for (i = 0; i < NUM_AU1000_DMA_CHANNELS; i++)
23578814465SManuel Lauss 		au1000_dma_table[i].irq = base + i;
23678814465SManuel Lauss 
23778814465SManuel Lauss 	printk(KERN_INFO "Alchemy DMA initialized\n");
23878814465SManuel Lauss 
23978814465SManuel Lauss out:
24078814465SManuel Lauss 	return 0;
24178814465SManuel Lauss }
24278814465SManuel Lauss arch_initcall(au1000_dma_init);
243