xref: /openbmc/linux/arch/mips/alchemy/common/dma.c (revision 26dd3e4f)
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>
3426dd3e4fSPaul 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 
dump_au1000_dma_channel(unsigned int dmanr)134e8c7c482SRalf Baechle void dump_au1000_dma_channel(unsigned int dmanr)
135e8c7c482SRalf Baechle {
136e8c7c482SRalf Baechle 	struct dma_chan *chan;
137e8c7c482SRalf Baechle 
138e8c7c482SRalf Baechle 	if (dmanr >= NUM_AU1000_DMA_CHANNELS)
139e8c7c482SRalf Baechle 		return;
140e8c7c482SRalf Baechle 	chan = &au1000_dma_table[dmanr];
141e8c7c482SRalf Baechle 
142e8c7c482SRalf Baechle 	printk(KERN_INFO "Au1000 DMA%d Register Dump:\n", dmanr);
143e8c7c482SRalf Baechle 	printk(KERN_INFO "  mode = 0x%08x\n",
1442f73bfbeSManuel Lauss 	       __raw_readl(chan->io + DMA_MODE_SET));
145e8c7c482SRalf Baechle 	printk(KERN_INFO "  addr = 0x%08x\n",
1462f73bfbeSManuel Lauss 	       __raw_readl(chan->io + DMA_PERIPHERAL_ADDR));
147e8c7c482SRalf Baechle 	printk(KERN_INFO "  start0 = 0x%08x\n",
1482f73bfbeSManuel Lauss 	       __raw_readl(chan->io + DMA_BUFFER0_START));
149e8c7c482SRalf Baechle 	printk(KERN_INFO "  start1 = 0x%08x\n",
1502f73bfbeSManuel Lauss 	       __raw_readl(chan->io + DMA_BUFFER1_START));
151e8c7c482SRalf Baechle 	printk(KERN_INFO "  count0 = 0x%08x\n",
1522f73bfbeSManuel Lauss 	       __raw_readl(chan->io + DMA_BUFFER0_COUNT));
153e8c7c482SRalf Baechle 	printk(KERN_INFO "  count1 = 0x%08x\n",
1542f73bfbeSManuel Lauss 	       __raw_readl(chan->io + DMA_BUFFER1_COUNT));
155e8c7c482SRalf Baechle }
156e8c7c482SRalf Baechle 
157e8c7c482SRalf Baechle /*
158e8c7c482SRalf Baechle  * Finds a free channel, and binds the requested device to it.
159e8c7c482SRalf Baechle  * Returns the allocated channel number, or negative on error.
160e8c7c482SRalf Baechle  * Requests the DMA done IRQ if irqhandler != NULL.
161e8c7c482SRalf Baechle  */
request_au1000_dma(int dev_id,const char * dev_str,irq_handler_t irqhandler,unsigned long irqflags,void * irq_dev_id)162e8c7c482SRalf Baechle int request_au1000_dma(int dev_id, const char *dev_str,
163e8c7c482SRalf Baechle 		       irq_handler_t irqhandler,
164e8c7c482SRalf Baechle 		       unsigned long irqflags,
165e8c7c482SRalf Baechle 		       void *irq_dev_id)
166e8c7c482SRalf Baechle {
167e8c7c482SRalf Baechle 	struct dma_chan *chan;
168e8c7c482SRalf Baechle 	const struct dma_dev *dev;
169e8c7c482SRalf Baechle 	int i, ret;
170e8c7c482SRalf Baechle 
171f2e442fdSManuel Lauss 	if (alchemy_get_cputype() == ALCHEMY_CPU_AU1100) {
172e8c7c482SRalf Baechle 		if (dev_id < 0 || dev_id >= (DMA_NUM_DEV + DMA_NUM_DEV_BANK2))
173e8c7c482SRalf Baechle 			return -EINVAL;
174f2e442fdSManuel Lauss 	} else {
175e8c7c482SRalf Baechle 		if (dev_id < 0 || dev_id >= DMA_NUM_DEV)
176e8c7c482SRalf Baechle 			return -EINVAL;
177f2e442fdSManuel Lauss 	}
178e8c7c482SRalf Baechle 
179e8c7c482SRalf Baechle 	for (i = 0; i < NUM_AU1000_DMA_CHANNELS; i++)
180e8c7c482SRalf Baechle 		if (au1000_dma_table[i].dev_id < 0)
181e8c7c482SRalf Baechle 			break;
182e8c7c482SRalf Baechle 
183e8c7c482SRalf Baechle 	if (i == NUM_AU1000_DMA_CHANNELS)
184e8c7c482SRalf Baechle 		return -ENODEV;
185e8c7c482SRalf Baechle 
186e8c7c482SRalf Baechle 	chan = &au1000_dma_table[i];
187e8c7c482SRalf Baechle 
188e8c7c482SRalf Baechle 	if (dev_id >= DMA_NUM_DEV) {
189e8c7c482SRalf Baechle 		dev_id -= DMA_NUM_DEV;
190e8c7c482SRalf Baechle 		dev = &dma_dev_table_bank2[dev_id];
191e8c7c482SRalf Baechle 	} else
192e8c7c482SRalf Baechle 		dev = &dma_dev_table[dev_id];
193e8c7c482SRalf Baechle 
194e8c7c482SRalf Baechle 	if (irqhandler) {
195e8c7c482SRalf Baechle 		chan->irq_dev = irq_dev_id;
196e8c7c482SRalf Baechle 		ret = request_irq(chan->irq, irqhandler, irqflags, dev_str,
197e8c7c482SRalf Baechle 				  chan->irq_dev);
198e8c7c482SRalf Baechle 		if (ret) {
199e8c7c482SRalf Baechle 			chan->irq_dev = NULL;
200e8c7c482SRalf Baechle 			return ret;
201e8c7c482SRalf Baechle 		}
202e8c7c482SRalf Baechle 	} else {
203e8c7c482SRalf Baechle 		chan->irq_dev = NULL;
204e8c7c482SRalf Baechle 	}
205e8c7c482SRalf Baechle 
206e8c7c482SRalf Baechle 	/* fill it in */
2072f73bfbeSManuel Lauss 	chan->io = (void __iomem *)(KSEG1ADDR(AU1000_DMA_PHYS_ADDR) +
2082f73bfbeSManuel Lauss 			i * DMA_CHANNEL_LEN);
209e8c7c482SRalf Baechle 	chan->dev_id = dev_id;
210e8c7c482SRalf Baechle 	chan->dev_str = dev_str;
211e8c7c482SRalf Baechle 	chan->fifo_addr = dev->fifo_addr;
212e8c7c482SRalf Baechle 	chan->mode = dev->dma_mode;
213e8c7c482SRalf Baechle 
214e8c7c482SRalf Baechle 	/* initialize the channel before returning */
215e8c7c482SRalf Baechle 	init_dma(i);
216e8c7c482SRalf Baechle 
217e8c7c482SRalf Baechle 	return i;
218e8c7c482SRalf Baechle }
219e8c7c482SRalf Baechle EXPORT_SYMBOL(request_au1000_dma);
220e8c7c482SRalf Baechle 
free_au1000_dma(unsigned int dmanr)221e8c7c482SRalf Baechle void free_au1000_dma(unsigned int dmanr)
222e8c7c482SRalf Baechle {
223e8c7c482SRalf Baechle 	struct dma_chan *chan = get_dma_chan(dmanr);
224e8c7c482SRalf Baechle 
225e8c7c482SRalf Baechle 	if (!chan) {
226e8c7c482SRalf Baechle 		printk(KERN_ERR "Error trying to free DMA%d\n", dmanr);
227e8c7c482SRalf Baechle 		return;
228e8c7c482SRalf Baechle 	}
229e8c7c482SRalf Baechle 
230e8c7c482SRalf Baechle 	disable_dma(dmanr);
23178814465SManuel Lauss 	if (chan->irq_dev)
232e8c7c482SRalf Baechle 		free_irq(chan->irq, chan->irq_dev);
233e8c7c482SRalf Baechle 
234e8c7c482SRalf Baechle 	chan->irq_dev = NULL;
235e8c7c482SRalf Baechle 	chan->dev_id = -1;
236e8c7c482SRalf Baechle }
237e8c7c482SRalf Baechle EXPORT_SYMBOL(free_au1000_dma);
238e8c7c482SRalf Baechle 
au1000_dma_init(void)23978814465SManuel Lauss static int __init au1000_dma_init(void)
24078814465SManuel Lauss {
24178814465SManuel Lauss 	int base, i;
24278814465SManuel Lauss 
24378814465SManuel Lauss 	switch (alchemy_get_cputype()) {
24478814465SManuel Lauss 	case ALCHEMY_CPU_AU1000:
24578814465SManuel Lauss 		base = AU1000_DMA_INT_BASE;
24678814465SManuel Lauss 		break;
24778814465SManuel Lauss 	case ALCHEMY_CPU_AU1500:
24878814465SManuel Lauss 		base = AU1500_DMA_INT_BASE;
24978814465SManuel Lauss 		break;
25078814465SManuel Lauss 	case ALCHEMY_CPU_AU1100:
25178814465SManuel Lauss 		base = AU1100_DMA_INT_BASE;
25278814465SManuel Lauss 		break;
25378814465SManuel Lauss 	default:
25478814465SManuel Lauss 		goto out;
25578814465SManuel Lauss 	}
25678814465SManuel Lauss 
25778814465SManuel Lauss 	for (i = 0; i < NUM_AU1000_DMA_CHANNELS; i++)
25878814465SManuel Lauss 		au1000_dma_table[i].irq = base + i;
25978814465SManuel Lauss 
26078814465SManuel Lauss 	printk(KERN_INFO "Alchemy DMA initialized\n");
26178814465SManuel Lauss 
26278814465SManuel Lauss out:
26378814465SManuel Lauss 	return 0;
26478814465SManuel Lauss }
26578814465SManuel Lauss arch_initcall(au1000_dma_init);
266