1 From bd620ec1ca053bab8ce2562968700e6f80e4ff83 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Fri, 6 May 2011 00:10:00 +0200
4 Subject: [PATCH 10/13] MIPS: Lantiq: Add DMA support
6 This patch adds support for the DMA engine found inside the XWAY family of
7 SoCs. The engine has 5 ports and 20 channels.
9 Signed-off-by: John Crispin <blogic@openwrt.org>
10 Signed-off-by: Ralph Hempel <ralph.hempel@lantiq.com>
11 Cc: linux-mips@linux-mips.org
12 Patchwork: https://patchwork.linux-mips.org/patch/2355/
13 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
15 .../mips/include/asm/mach-lantiq/xway/lantiq_soc.h | 3 +-
16 arch/mips/include/asm/mach-lantiq/xway/xway_dma.h | 60 +++++
17 arch/mips/lantiq/xway/Makefile | 2 +-
18 arch/mips/lantiq/xway/devices.h | 1 +
19 arch/mips/lantiq/xway/dma.c | 253 ++++++++++++++++++++
20 5 files changed, 317 insertions(+), 2 deletions(-)
21 create mode 100644 arch/mips/include/asm/mach-lantiq/xway/xway_dma.h
22 create mode 100644 arch/mips/lantiq/xway/dma.c
24 diff --git a/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h b/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h
25 index 343e82c..4827afb 100644
26 --- a/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h
27 +++ b/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h
29 #define LTQ_PPE32_SIZE 0x40000
32 -#define LTQ_DMA_BASE_ADDR 0xBE104100
33 +#define LTQ_DMA_BASE_ADDR 0x1E104100
34 +#define LTQ_DMA_SIZE 0x800
37 #define PCI_CR_BASE_ADDR 0x1E105400
38 diff --git a/arch/mips/include/asm/mach-lantiq/xway/xway_dma.h b/arch/mips/include/asm/mach-lantiq/xway/xway_dma.h
40 index 0000000..872943a
42 +++ b/arch/mips/include/asm/mach-lantiq/xway/xway_dma.h
45 + * This program is free software; you can redistribute it and/or modify it
46 + * under the terms of the GNU General Public License version 2 as published
47 + * by the Free Software Foundation.
49 + * This program is distributed in the hope that it will be useful,
50 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
51 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52 + * GNU General Public License for more details.
54 + * You should have received a copy of the GNU General Public License
55 + * along with this program; if not, write to the Free Software
56 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
58 + * Copyright (C) 2011 John Crispin <blogic@openwrt.org>
64 +#define LTQ_DESC_SIZE 0x08 /* each descriptor is 64bit */
65 +#define LTQ_DESC_NUM 0x40 /* 64 descriptors / channel */
67 +#define LTQ_DMA_OWN BIT(31) /* owner bit */
68 +#define LTQ_DMA_C BIT(30) /* complete bit */
69 +#define LTQ_DMA_SOP BIT(29) /* start of packet */
70 +#define LTQ_DMA_EOP BIT(28) /* end of packet */
71 +#define LTQ_DMA_TX_OFFSET(x) ((x & 0x1f) << 23) /* data bytes offset */
72 +#define LTQ_DMA_RX_OFFSET(x) ((x & 0x7) << 23) /* data bytes offset */
73 +#define LTQ_DMA_SIZE_MASK (0xffff) /* the size field is 16 bit */
75 +struct ltq_dma_desc {
80 +struct ltq_dma_channel {
81 + int nr; /* the channel number */
82 + int irq; /* the mapped irq */
83 + int desc; /* the current descriptor */
84 + struct ltq_dma_desc *desc_base; /* the descriptor base */
85 + int phys; /* physical addr */
93 +extern void ltq_dma_enable_irq(struct ltq_dma_channel *ch);
94 +extern void ltq_dma_disable_irq(struct ltq_dma_channel *ch);
95 +extern void ltq_dma_ack_irq(struct ltq_dma_channel *ch);
96 +extern void ltq_dma_open(struct ltq_dma_channel *ch);
97 +extern void ltq_dma_close(struct ltq_dma_channel *ch);
98 +extern void ltq_dma_alloc_tx(struct ltq_dma_channel *ch);
99 +extern void ltq_dma_alloc_rx(struct ltq_dma_channel *ch);
100 +extern void ltq_dma_free(struct ltq_dma_channel *ch);
101 +extern void ltq_dma_init_port(int p);
104 diff --git a/arch/mips/lantiq/xway/Makefile b/arch/mips/lantiq/xway/Makefile
105 index 6b5e07e..c517f2e 100644
106 --- a/arch/mips/lantiq/xway/Makefile
107 +++ b/arch/mips/lantiq/xway/Makefile
109 -obj-y := pmu.o ebu.o reset.o gpio.o gpio_stp.o gpio_ebu.o devices.o
110 +obj-y := pmu.o ebu.o reset.o gpio.o gpio_stp.o gpio_ebu.o devices.o dma.o
112 obj-$(CONFIG_SOC_XWAY) += clk-xway.o prom-xway.o setup-xway.o
113 obj-$(CONFIG_SOC_AMAZON_SE) += clk-ase.o prom-ase.o setup-ase.o
114 diff --git a/arch/mips/lantiq/xway/devices.h b/arch/mips/lantiq/xway/devices.h
115 index 51f56b5..d573084 100644
116 --- a/arch/mips/lantiq/xway/devices.h
117 +++ b/arch/mips/lantiq/xway/devices.h
119 #define _LTQ_DEVICES_XWAY_H__
121 #include "../devices.h"
122 +#include <linux/phy.h>
124 extern void ltq_register_gpio(void);
125 extern void ltq_register_gpio_stp(void);
126 diff --git a/arch/mips/lantiq/xway/dma.c b/arch/mips/lantiq/xway/dma.c
128 index 0000000..4278a45
130 +++ b/arch/mips/lantiq/xway/dma.c
133 + * This program is free software; you can redistribute it and/or modify it
134 + * under the terms of the GNU General Public License version 2 as published
135 + * by the Free Software Foundation.
137 + * This program is distributed in the hope that it will be useful,
138 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
139 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
140 + * GNU General Public License for more details.
142 + * You should have received a copy of the GNU General Public License
143 + * along with this program; if not, write to the Free Software
144 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
146 + * Copyright (C) 2011 John Crispin <blogic@openwrt.org>
149 +#include <linux/init.h>
150 +#include <linux/platform_device.h>
151 +#include <linux/io.h>
152 +#include <linux/dma-mapping.h>
154 +#include <lantiq_soc.h>
155 +#include <xway_dma.h>
157 +#define LTQ_DMA_CTRL 0x10
158 +#define LTQ_DMA_CPOLL 0x14
159 +#define LTQ_DMA_CS 0x18
160 +#define LTQ_DMA_CCTRL 0x1C
161 +#define LTQ_DMA_CDBA 0x20
162 +#define LTQ_DMA_CDLEN 0x24
163 +#define LTQ_DMA_CIS 0x28
164 +#define LTQ_DMA_CIE 0x2C
165 +#define LTQ_DMA_PS 0x40
166 +#define LTQ_DMA_PCTRL 0x44
167 +#define LTQ_DMA_IRNEN 0xf4
169 +#define DMA_DESCPT BIT(3) /* descriptor complete irq */
170 +#define DMA_TX BIT(8) /* TX channel direction */
171 +#define DMA_CHAN_ON BIT(0) /* channel on / off bit */
172 +#define DMA_PDEN BIT(6) /* enable packet drop */
173 +#define DMA_CHAN_RST BIT(1) /* channel on / off bit */
174 +#define DMA_RESET BIT(0) /* channel on / off bit */
175 +#define DMA_IRQ_ACK 0x7e /* IRQ status register */
176 +#define DMA_POLL BIT(31) /* turn on channel polling */
177 +#define DMA_CLK_DIV4 BIT(6) /* polling clock divider */
178 +#define DMA_2W_BURST BIT(1) /* 2 word burst length */
179 +#define DMA_MAX_CHANNEL 20 /* the soc has 20 channels */
180 +#define DMA_ETOP_ENDIANESS (0xf << 8) /* endianess swap etop channels */
181 +#define DMA_WEIGHT (BIT(17) | BIT(16)) /* default channel wheight */
183 +#define ltq_dma_r32(x) ltq_r32(ltq_dma_membase + (x))
184 +#define ltq_dma_w32(x, y) ltq_w32(x, ltq_dma_membase + (y))
185 +#define ltq_dma_w32_mask(x, y, z) ltq_w32_mask(x, y, \
186 + ltq_dma_membase + (z))
188 +static struct resource ltq_dma_resource = {
190 + .start = LTQ_DMA_BASE_ADDR,
191 + .end = LTQ_DMA_BASE_ADDR + LTQ_DMA_SIZE - 1,
192 + .flags = IORESOURCE_MEM,
195 +static void __iomem *ltq_dma_membase;
198 +ltq_dma_enable_irq(struct ltq_dma_channel *ch)
200 + unsigned long flags;
202 + local_irq_save(flags);
203 + ltq_dma_w32(ch->nr, LTQ_DMA_CS);
204 + ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
205 + local_irq_restore(flags);
207 +EXPORT_SYMBOL_GPL(ltq_dma_enable_irq);
210 +ltq_dma_disable_irq(struct ltq_dma_channel *ch)
212 + unsigned long flags;
214 + local_irq_save(flags);
215 + ltq_dma_w32(ch->nr, LTQ_DMA_CS);
216 + ltq_dma_w32_mask(1 << ch->nr, 0, LTQ_DMA_IRNEN);
217 + local_irq_restore(flags);
219 +EXPORT_SYMBOL_GPL(ltq_dma_disable_irq);
222 +ltq_dma_ack_irq(struct ltq_dma_channel *ch)
224 + unsigned long flags;
226 + local_irq_save(flags);
227 + ltq_dma_w32(ch->nr, LTQ_DMA_CS);
228 + ltq_dma_w32(DMA_IRQ_ACK, LTQ_DMA_CIS);
229 + local_irq_restore(flags);
231 +EXPORT_SYMBOL_GPL(ltq_dma_ack_irq);
234 +ltq_dma_open(struct ltq_dma_channel *ch)
236 + unsigned long flag;
238 + local_irq_save(flag);
239 + ltq_dma_w32(ch->nr, LTQ_DMA_CS);
240 + ltq_dma_w32_mask(0, DMA_CHAN_ON, LTQ_DMA_CCTRL);
241 + ltq_dma_enable_irq(ch);
242 + local_irq_restore(flag);
244 +EXPORT_SYMBOL_GPL(ltq_dma_open);
247 +ltq_dma_close(struct ltq_dma_channel *ch)
249 + unsigned long flag;
251 + local_irq_save(flag);
252 + ltq_dma_w32(ch->nr, LTQ_DMA_CS);
253 + ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
254 + ltq_dma_disable_irq(ch);
255 + local_irq_restore(flag);
257 +EXPORT_SYMBOL_GPL(ltq_dma_close);
260 +ltq_dma_alloc(struct ltq_dma_channel *ch)
262 + unsigned long flags;
265 + ch->desc_base = dma_alloc_coherent(NULL,
266 + LTQ_DESC_NUM * LTQ_DESC_SIZE,
267 + &ch->phys, GFP_ATOMIC);
268 + memset(ch->desc_base, 0, LTQ_DESC_NUM * LTQ_DESC_SIZE);
270 + local_irq_save(flags);
271 + ltq_dma_w32(ch->nr, LTQ_DMA_CS);
272 + ltq_dma_w32(ch->phys, LTQ_DMA_CDBA);
273 + ltq_dma_w32(LTQ_DESC_NUM, LTQ_DMA_CDLEN);
274 + ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
276 + ltq_dma_w32_mask(0, DMA_CHAN_RST, LTQ_DMA_CCTRL);
277 + while (ltq_dma_r32(LTQ_DMA_CCTRL) & DMA_CHAN_RST)
279 + local_irq_restore(flags);
283 +ltq_dma_alloc_tx(struct ltq_dma_channel *ch)
285 + unsigned long flags;
289 + local_irq_save(flags);
290 + ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE);
291 + ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
292 + ltq_dma_w32(DMA_WEIGHT | DMA_TX, LTQ_DMA_CCTRL);
293 + local_irq_restore(flags);
295 +EXPORT_SYMBOL_GPL(ltq_dma_alloc_tx);
298 +ltq_dma_alloc_rx(struct ltq_dma_channel *ch)
300 + unsigned long flags;
304 + local_irq_save(flags);
305 + ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE);
306 + ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
307 + ltq_dma_w32(DMA_WEIGHT, LTQ_DMA_CCTRL);
308 + local_irq_restore(flags);
310 +EXPORT_SYMBOL_GPL(ltq_dma_alloc_rx);
313 +ltq_dma_free(struct ltq_dma_channel *ch)
315 + if (!ch->desc_base)
318 + dma_free_coherent(NULL, LTQ_DESC_NUM * LTQ_DESC_SIZE,
319 + ch->desc_base, ch->phys);
321 +EXPORT_SYMBOL_GPL(ltq_dma_free);
324 +ltq_dma_init_port(int p)
326 + ltq_dma_w32(p, LTQ_DMA_PS);
328 + case DMA_PORT_ETOP:
330 + * Tell the DMA engine to swap the endianess of data frames and
331 + * drop packets if the channel arbitration fails.
333 + ltq_dma_w32_mask(0, DMA_ETOP_ENDIANESS | DMA_PDEN,
338 + ltq_dma_w32((DMA_2W_BURST << 4) | (DMA_2W_BURST << 2),
346 +EXPORT_SYMBOL_GPL(ltq_dma_init_port);
353 + /* insert and request the memory region */
354 + if (insert_resource(&iomem_resource, <q_dma_resource) < 0)
355 + panic("Failed to insert dma memory\n");
357 + if (request_mem_region(ltq_dma_resource.start,
358 + resource_size(<q_dma_resource), "dma") < 0)
359 + panic("Failed to request dma memory\n");
361 + /* remap dma register range */
362 + ltq_dma_membase = ioremap_nocache(ltq_dma_resource.start,
363 + resource_size(<q_dma_resource));
364 + if (!ltq_dma_membase)
365 + panic("Failed to remap dma memory\n");
367 + /* power up and reset the dma engine */
368 + ltq_pmu_enable(PMU_DMA);
369 + ltq_dma_w32_mask(0, DMA_RESET, LTQ_DMA_CTRL);
371 + /* disable all interrupts */
372 + ltq_dma_w32(0, LTQ_DMA_IRNEN);
374 + /* reset/configure each channel */
375 + for (i = 0; i < DMA_MAX_CHANNEL; i++) {
376 + ltq_dma_w32(i, LTQ_DMA_CS);
377 + ltq_dma_w32(DMA_CHAN_RST, LTQ_DMA_CCTRL);
378 + ltq_dma_w32(DMA_POLL | DMA_CLK_DIV4, LTQ_DMA_CPOLL);
379 + ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
384 +postcore_initcall(ltq_dma_init);