1 From ced95a5a9d7a3ba168e8518d9b5004c9a0cad1fe Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 19 Jun 2010 04:08:14 +0000
4 Subject: [PATCH] MIPS: JZ4740: Add DMA support.
6 Add support for DMA transfers on JZ4740 SoCs.
8 Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
9 Cc: linux-mips@linux-mips.org
10 Cc: linux-kernel@vger.kernel.org
11 Patchwork: https://patchwork.linux-mips.org/patch/1401/
12 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
14 arch/mips/include/asm/mach-jz4740/dma.h | 90 ++++++++++
15 arch/mips/jz4740/dma.c | 289 +++++++++++++++++++++++++++++++
16 2 files changed, 379 insertions(+), 0 deletions(-)
17 create mode 100644 arch/mips/include/asm/mach-jz4740/dma.h
18 create mode 100644 arch/mips/jz4740/dma.c
21 +++ b/arch/mips/include/asm/mach-jz4740/dma.h
24 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
25 + * JZ7420/JZ4740 DMA definitions
27 + * This program is free software; you can redistribute it and/or modify it
28 + * under the terms of the GNU General Public License as published by the
29 + * Free Software Foundation; either version 2 of the License, or (at your
30 + * option) any later version.
32 + * You should have received a copy of the GNU General Public License along
33 + * with this program; if not, write to the Free Software Foundation, Inc.,
34 + * 675 Mass Ave, Cambridge, MA 02139, USA.
38 +#ifndef __ASM_MACH_JZ4740_DMA_H__
39 +#define __ASM_MACH_JZ4740_DMA_H__
41 +struct jz4740_dma_chan;
43 +enum jz4740_dma_request_type {
44 + JZ4740_DMA_TYPE_AUTO_REQUEST = 8,
45 + JZ4740_DMA_TYPE_UART_TRANSMIT = 20,
46 + JZ4740_DMA_TYPE_UART_RECEIVE = 21,
47 + JZ4740_DMA_TYPE_SPI_TRANSMIT = 22,
48 + JZ4740_DMA_TYPE_SPI_RECEIVE = 23,
49 + JZ4740_DMA_TYPE_AIC_TRANSMIT = 24,
50 + JZ4740_DMA_TYPE_AIC_RECEIVE = 25,
51 + JZ4740_DMA_TYPE_MMC_TRANSMIT = 26,
52 + JZ4740_DMA_TYPE_MMC_RECEIVE = 27,
53 + JZ4740_DMA_TYPE_TCU = 28,
54 + JZ4740_DMA_TYPE_SADC = 29,
55 + JZ4740_DMA_TYPE_SLCD = 30,
58 +enum jz4740_dma_width {
59 + JZ4740_DMA_WIDTH_32BIT = 0,
60 + JZ4740_DMA_WIDTH_8BIT = 1,
61 + JZ4740_DMA_WIDTH_16BIT = 2,
64 +enum jz4740_dma_transfer_size {
65 + JZ4740_DMA_TRANSFER_SIZE_4BYTE = 0,
66 + JZ4740_DMA_TRANSFER_SIZE_1BYTE = 1,
67 + JZ4740_DMA_TRANSFER_SIZE_2BYTE = 2,
68 + JZ4740_DMA_TRANSFER_SIZE_16BYTE = 3,
69 + JZ4740_DMA_TRANSFER_SIZE_32BYTE = 4,
72 +enum jz4740_dma_flags {
73 + JZ4740_DMA_SRC_AUTOINC = 0x2,
74 + JZ4740_DMA_DST_AUTOINC = 0x1,
77 +enum jz4740_dma_mode {
78 + JZ4740_DMA_MODE_SINGLE = 0,
79 + JZ4740_DMA_MODE_BLOCK = 1,
82 +struct jz4740_dma_config {
83 + enum jz4740_dma_width src_width;
84 + enum jz4740_dma_width dst_width;
85 + enum jz4740_dma_transfer_size transfer_size;
86 + enum jz4740_dma_request_type request_type;
87 + enum jz4740_dma_flags flags;
88 + enum jz4740_dma_mode mode;
91 +typedef void (*jz4740_dma_complete_callback_t)(struct jz4740_dma_chan *, int, void *);
93 +struct jz4740_dma_chan *jz4740_dma_request(void *dev, const char *name);
94 +void jz4740_dma_free(struct jz4740_dma_chan *dma);
96 +void jz4740_dma_configure(struct jz4740_dma_chan *dma,
97 + const struct jz4740_dma_config *config);
100 +void jz4740_dma_enable(struct jz4740_dma_chan *dma);
101 +void jz4740_dma_disable(struct jz4740_dma_chan *dma);
103 +void jz4740_dma_set_src_addr(struct jz4740_dma_chan *dma, dma_addr_t src);
104 +void jz4740_dma_set_dst_addr(struct jz4740_dma_chan *dma, dma_addr_t dst);
105 +void jz4740_dma_set_transfer_count(struct jz4740_dma_chan *dma, uint32_t count);
107 +uint32_t jz4740_dma_get_residue(const struct jz4740_dma_chan *dma);
109 +void jz4740_dma_set_complete_cb(struct jz4740_dma_chan *dma,
110 + jz4740_dma_complete_callback_t cb);
112 +#endif /* __ASM_JZ4740_DMA_H__ */
114 +++ b/arch/mips/jz4740/dma.c
117 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
118 + * JZ4740 SoC DMA support
120 + * This program is free software; you can redistribute it and/or modify it
121 + * under the terms of the GNU General Public License as published by the
122 + * Free Software Foundation; either version 2 of the License, or (at your
123 + * option) any later version.
125 + * You should have received a copy of the GNU General Public License along
126 + * with this program; if not, write to the Free Software Foundation, Inc.,
127 + * 675 Mass Ave, Cambridge, MA 02139, USA.
131 +#include <linux/kernel.h>
132 +#include <linux/module.h>
133 +#include <linux/spinlock.h>
134 +#include <linux/interrupt.h>
136 +#include <linux/dma-mapping.h>
137 +#include <asm/mach-jz4740/dma.h>
138 +#include <asm/mach-jz4740/base.h>
140 +#define JZ_REG_DMA_SRC_ADDR(x) (0x00 + (x) * 0x20)
141 +#define JZ_REG_DMA_DST_ADDR(x) (0x04 + (x) * 0x20)
142 +#define JZ_REG_DMA_TRANSFER_COUNT(x) (0x08 + (x) * 0x20)
143 +#define JZ_REG_DMA_REQ_TYPE(x) (0x0C + (x) * 0x20)
144 +#define JZ_REG_DMA_STATUS_CTRL(x) (0x10 + (x) * 0x20)
145 +#define JZ_REG_DMA_CMD(x) (0x14 + (x) * 0x20)
146 +#define JZ_REG_DMA_DESC_ADDR(x) (0x18 + (x) * 0x20)
148 +#define JZ_REG_DMA_CTRL 0x300
149 +#define JZ_REG_DMA_IRQ 0x304
150 +#define JZ_REG_DMA_DOORBELL 0x308
151 +#define JZ_REG_DMA_DOORBELL_SET 0x30C
153 +#define JZ_DMA_STATUS_CTRL_NO_DESC BIT(31)
154 +#define JZ_DMA_STATUS_CTRL_DESC_INV BIT(6)
155 +#define JZ_DMA_STATUS_CTRL_ADDR_ERR BIT(4)
156 +#define JZ_DMA_STATUS_CTRL_TRANSFER_DONE BIT(3)
157 +#define JZ_DMA_STATUS_CTRL_HALT BIT(2)
158 +#define JZ_DMA_STATUS_CTRL_COUNT_TERMINATE BIT(1)
159 +#define JZ_DMA_STATUS_CTRL_ENABLE BIT(0)
161 +#define JZ_DMA_CMD_SRC_INC BIT(23)
162 +#define JZ_DMA_CMD_DST_INC BIT(22)
163 +#define JZ_DMA_CMD_RDIL_MASK (0xf << 16)
164 +#define JZ_DMA_CMD_SRC_WIDTH_MASK (0x3 << 14)
165 +#define JZ_DMA_CMD_DST_WIDTH_MASK (0x3 << 12)
166 +#define JZ_DMA_CMD_INTERVAL_LENGTH_MASK (0x7 << 8)
167 +#define JZ_DMA_CMD_BLOCK_MODE BIT(7)
168 +#define JZ_DMA_CMD_DESC_VALID BIT(4)
169 +#define JZ_DMA_CMD_DESC_VALID_MODE BIT(3)
170 +#define JZ_DMA_CMD_VALID_IRQ_ENABLE BIT(2)
171 +#define JZ_DMA_CMD_TRANSFER_IRQ_ENABLE BIT(1)
172 +#define JZ_DMA_CMD_LINK_ENABLE BIT(0)
174 +#define JZ_DMA_CMD_FLAGS_OFFSET 22
175 +#define JZ_DMA_CMD_RDIL_OFFSET 16
176 +#define JZ_DMA_CMD_SRC_WIDTH_OFFSET 14
177 +#define JZ_DMA_CMD_DST_WIDTH_OFFSET 12
178 +#define JZ_DMA_CMD_TRANSFER_SIZE_OFFSET 8
179 +#define JZ_DMA_CMD_MODE_OFFSET 7
181 +#define JZ_DMA_CTRL_PRIORITY_MASK (0x3 << 8)
182 +#define JZ_DMA_CTRL_HALT BIT(3)
183 +#define JZ_DMA_CTRL_ADDRESS_ERROR BIT(2)
184 +#define JZ_DMA_CTRL_ENABLE BIT(0)
187 +static void __iomem *jz4740_dma_base;
188 +static spinlock_t jz4740_dma_lock;
190 +static inline uint32_t jz4740_dma_read(size_t reg)
192 + return readl(jz4740_dma_base + reg);
195 +static inline void jz4740_dma_write(size_t reg, uint32_t val)
197 + writel(val, jz4740_dma_base + reg);
200 +static inline void jz4740_dma_write_mask(size_t reg, uint32_t val, uint32_t mask)
203 + val2 = jz4740_dma_read(reg);
206 + jz4740_dma_write(reg, val2);
209 +struct jz4740_dma_chan {
214 + enum jz4740_dma_flags flags;
215 + uint32_t transfer_shift;
217 + jz4740_dma_complete_callback_t complete_cb;
222 +#define JZ4740_DMA_CHANNEL(_id) { .id = _id }
224 +struct jz4740_dma_chan jz4740_dma_channels[] = {
225 + JZ4740_DMA_CHANNEL(0),
226 + JZ4740_DMA_CHANNEL(1),
227 + JZ4740_DMA_CHANNEL(2),
228 + JZ4740_DMA_CHANNEL(3),
229 + JZ4740_DMA_CHANNEL(4),
230 + JZ4740_DMA_CHANNEL(5),
233 +struct jz4740_dma_chan *jz4740_dma_request(void *dev, const char *name)
236 + struct jz4740_dma_chan *dma = NULL;
238 + spin_lock(&jz4740_dma_lock);
240 + for (i = 0; i < ARRAY_SIZE(jz4740_dma_channels); ++i) {
241 + if (!jz4740_dma_channels[i].used) {
242 + dma = &jz4740_dma_channels[i];
248 + spin_unlock(&jz4740_dma_lock);
258 +EXPORT_SYMBOL_GPL(jz4740_dma_request);
260 +void jz4740_dma_configure(struct jz4740_dma_chan *dma,
261 + const struct jz4740_dma_config *config)
265 + switch (config->transfer_size) {
266 + case JZ4740_DMA_TRANSFER_SIZE_2BYTE:
267 + dma->transfer_shift = 1;
269 + case JZ4740_DMA_TRANSFER_SIZE_4BYTE:
270 + dma->transfer_shift = 2;
272 + case JZ4740_DMA_TRANSFER_SIZE_16BYTE:
273 + dma->transfer_shift = 4;
275 + case JZ4740_DMA_TRANSFER_SIZE_32BYTE:
276 + dma->transfer_shift = 5;
279 + dma->transfer_shift = 0;
283 + cmd = config->flags << JZ_DMA_CMD_FLAGS_OFFSET;
284 + cmd |= config->src_width << JZ_DMA_CMD_SRC_WIDTH_OFFSET;
285 + cmd |= config->dst_width << JZ_DMA_CMD_DST_WIDTH_OFFSET;
286 + cmd |= config->transfer_size << JZ_DMA_CMD_TRANSFER_SIZE_OFFSET;
287 + cmd |= config->mode << JZ_DMA_CMD_MODE_OFFSET;
288 + cmd |= JZ_DMA_CMD_TRANSFER_IRQ_ENABLE;
290 + jz4740_dma_write(JZ_REG_DMA_CMD(dma->id), cmd);
291 + jz4740_dma_write(JZ_REG_DMA_STATUS_CTRL(dma->id), 0);
292 + jz4740_dma_write(JZ_REG_DMA_REQ_TYPE(dma->id), config->request_type);
294 +EXPORT_SYMBOL_GPL(jz4740_dma_configure);
296 +void jz4740_dma_set_src_addr(struct jz4740_dma_chan *dma, dma_addr_t src)
298 + jz4740_dma_write(JZ_REG_DMA_SRC_ADDR(dma->id), src);
300 +EXPORT_SYMBOL_GPL(jz4740_dma_set_src_addr);
302 +void jz4740_dma_set_dst_addr(struct jz4740_dma_chan *dma, dma_addr_t dst)
304 + jz4740_dma_write(JZ_REG_DMA_DST_ADDR(dma->id), dst);
306 +EXPORT_SYMBOL_GPL(jz4740_dma_set_dst_addr);
308 +void jz4740_dma_set_transfer_count(struct jz4740_dma_chan *dma, uint32_t count)
310 + count >>= dma->transfer_shift;
311 + jz4740_dma_write(JZ_REG_DMA_TRANSFER_COUNT(dma->id), count);
313 +EXPORT_SYMBOL_GPL(jz4740_dma_set_transfer_count);
315 +void jz4740_dma_set_complete_cb(struct jz4740_dma_chan *dma,
316 + jz4740_dma_complete_callback_t cb)
318 + dma->complete_cb = cb;
320 +EXPORT_SYMBOL_GPL(jz4740_dma_set_complete_cb);
322 +void jz4740_dma_free(struct jz4740_dma_chan *dma)
325 + dma->complete_cb = NULL;
328 +EXPORT_SYMBOL_GPL(jz4740_dma_free);
330 +void jz4740_dma_enable(struct jz4740_dma_chan *dma)
332 + jz4740_dma_write_mask(JZ_REG_DMA_STATUS_CTRL(dma->id),
333 + JZ_DMA_STATUS_CTRL_NO_DESC | JZ_DMA_STATUS_CTRL_ENABLE,
334 + JZ_DMA_STATUS_CTRL_HALT | JZ_DMA_STATUS_CTRL_NO_DESC |
335 + JZ_DMA_STATUS_CTRL_ENABLE);
337 + jz4740_dma_write_mask(JZ_REG_DMA_CTRL,
338 + JZ_DMA_CTRL_ENABLE,
339 + JZ_DMA_CTRL_HALT | JZ_DMA_CTRL_ENABLE);
341 +EXPORT_SYMBOL_GPL(jz4740_dma_enable);
343 +void jz4740_dma_disable(struct jz4740_dma_chan *dma)
345 + jz4740_dma_write_mask(JZ_REG_DMA_STATUS_CTRL(dma->id), 0,
346 + JZ_DMA_STATUS_CTRL_ENABLE);
348 +EXPORT_SYMBOL_GPL(jz4740_dma_disable);
350 +uint32_t jz4740_dma_get_residue(const struct jz4740_dma_chan *dma)
353 + residue = jz4740_dma_read(JZ_REG_DMA_TRANSFER_COUNT(dma->id));
354 + return residue << dma->transfer_shift;
356 +EXPORT_SYMBOL_GPL(jz4740_dma_get_residue);
358 +static void jz4740_dma_chan_irq(struct jz4740_dma_chan *dma)
362 + status = jz4740_dma_read(JZ_REG_DMA_STATUS_CTRL(dma->id));
364 + jz4740_dma_write_mask(JZ_REG_DMA_STATUS_CTRL(dma->id), 0,
365 + JZ_DMA_STATUS_CTRL_ENABLE | JZ_DMA_STATUS_CTRL_TRANSFER_DONE);
367 + if (dma->complete_cb)
368 + dma->complete_cb(dma, 0, dma->dev);
371 +static irqreturn_t jz4740_dma_irq(int irq, void *dev_id)
373 + uint32_t irq_status;
376 + irq_status = readl(jz4740_dma_base + JZ_REG_DMA_IRQ);
378 + for (i = 0; i < 6; ++i) {
379 + if (irq_status & (1 << i))
380 + jz4740_dma_chan_irq(&jz4740_dma_channels[i]);
383 + return IRQ_HANDLED;
386 +static int jz4740_dma_init(void)
390 + jz4740_dma_base = ioremap(JZ4740_DMAC_BASE_ADDR, 0x400);
392 + if (!jz4740_dma_base)
395 + spin_lock_init(&jz4740_dma_lock);
397 + ret = request_irq(JZ4740_IRQ_DMAC, jz4740_dma_irq, 0, "DMA", NULL);
400 + printk(KERN_ERR "JZ4740 DMA: Failed to request irq: %d\n", ret);
404 +arch_initcall(jz4740_dma_init);