1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2011 John Crispin <john@phrozen.org>
7 #include <linux/init.h>
8 #include <linux/platform_device.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/export.h>
12 #include <linux/spinlock.h>
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
18 #include <lantiq_soc.h>
21 #define LTQ_DMA_ID 0x08
22 #define LTQ_DMA_CTRL 0x10
23 #define LTQ_DMA_CPOLL 0x14
24 #define LTQ_DMA_CS 0x18
25 #define LTQ_DMA_CCTRL 0x1C
26 #define LTQ_DMA_CDBA 0x20
27 #define LTQ_DMA_CDLEN 0x24
28 #define LTQ_DMA_CIS 0x28
29 #define LTQ_DMA_CIE 0x2C
30 #define LTQ_DMA_PS 0x40
31 #define LTQ_DMA_PCTRL 0x44
32 #define LTQ_DMA_IRNEN 0xf4
34 #define DMA_ID_CHNR GENMASK(26, 20) /* channel number */
35 #define DMA_DESCPT BIT(3) /* descriptor complete irq */
36 #define DMA_TX BIT(8) /* TX channel direction */
37 #define DMA_CHAN_ON BIT(0) /* channel on / off bit */
38 #define DMA_PDEN BIT(6) /* enable packet drop */
39 #define DMA_CHAN_RST BIT(1) /* channel on / off bit */
40 #define DMA_RESET BIT(0) /* channel on / off bit */
41 #define DMA_IRQ_ACK 0x7e /* IRQ status register */
42 #define DMA_POLL BIT(31) /* turn on channel polling */
43 #define DMA_CLK_DIV4 BIT(6) /* polling clock divider */
44 #define DMA_PCTRL_2W_BURST 0x1 /* 2 word burst length */
45 #define DMA_PCTRL_4W_BURST 0x2 /* 4 word burst length */
46 #define DMA_PCTRL_8W_BURST 0x3 /* 8 word burst length */
47 #define DMA_TX_BURST_SHIFT 4 /* tx burst shift */
48 #define DMA_RX_BURST_SHIFT 2 /* rx burst shift */
49 #define DMA_ETOP_ENDIANNESS (0xf << 8) /* endianness swap etop channels */
50 #define DMA_WEIGHT (BIT(17) | BIT(16)) /* default channel wheight */
52 #define ltq_dma_r32(x) ltq_r32(ltq_dma_membase + (x))
53 #define ltq_dma_w32(x, y) ltq_w32(x, ltq_dma_membase + (y))
54 #define ltq_dma_w32_mask(x, y, z) ltq_w32_mask(x, y, \
55 ltq_dma_membase + (z))
57 static void __iomem
*ltq_dma_membase
;
58 static DEFINE_SPINLOCK(ltq_dma_lock
);
61 ltq_dma_enable_irq(struct ltq_dma_channel
*ch
)
65 spin_lock_irqsave(<q_dma_lock
, flags
);
66 ltq_dma_w32(ch
->nr
, LTQ_DMA_CS
);
67 ltq_dma_w32_mask(0, 1 << ch
->nr
, LTQ_DMA_IRNEN
);
68 spin_unlock_irqrestore(<q_dma_lock
, flags
);
70 EXPORT_SYMBOL_GPL(ltq_dma_enable_irq
);
73 ltq_dma_disable_irq(struct ltq_dma_channel
*ch
)
77 spin_lock_irqsave(<q_dma_lock
, flags
);
78 ltq_dma_w32(ch
->nr
, LTQ_DMA_CS
);
79 ltq_dma_w32_mask(1 << ch
->nr
, 0, LTQ_DMA_IRNEN
);
80 spin_unlock_irqrestore(<q_dma_lock
, flags
);
82 EXPORT_SYMBOL_GPL(ltq_dma_disable_irq
);
85 ltq_dma_ack_irq(struct ltq_dma_channel
*ch
)
89 spin_lock_irqsave(<q_dma_lock
, flags
);
90 ltq_dma_w32(ch
->nr
, LTQ_DMA_CS
);
91 ltq_dma_w32(DMA_IRQ_ACK
, LTQ_DMA_CIS
);
92 spin_unlock_irqrestore(<q_dma_lock
, flags
);
94 EXPORT_SYMBOL_GPL(ltq_dma_ack_irq
);
97 ltq_dma_open(struct ltq_dma_channel
*ch
)
101 spin_lock_irqsave(<q_dma_lock
, flag
);
102 ltq_dma_w32(ch
->nr
, LTQ_DMA_CS
);
103 ltq_dma_w32_mask(0, DMA_CHAN_ON
, LTQ_DMA_CCTRL
);
104 spin_unlock_irqrestore(<q_dma_lock
, flag
);
106 EXPORT_SYMBOL_GPL(ltq_dma_open
);
109 ltq_dma_close(struct ltq_dma_channel
*ch
)
113 spin_lock_irqsave(<q_dma_lock
, flag
);
114 ltq_dma_w32(ch
->nr
, LTQ_DMA_CS
);
115 ltq_dma_w32_mask(DMA_CHAN_ON
, 0, LTQ_DMA_CCTRL
);
116 ltq_dma_w32_mask(1 << ch
->nr
, 0, LTQ_DMA_IRNEN
);
117 spin_unlock_irqrestore(<q_dma_lock
, flag
);
119 EXPORT_SYMBOL_GPL(ltq_dma_close
);
122 ltq_dma_alloc(struct ltq_dma_channel
*ch
)
127 ch
->desc_base
= dma_alloc_coherent(ch
->dev
,
128 LTQ_DESC_NUM
* LTQ_DESC_SIZE
,
129 &ch
->phys
, GFP_ATOMIC
);
131 spin_lock_irqsave(<q_dma_lock
, flags
);
132 ltq_dma_w32(ch
->nr
, LTQ_DMA_CS
);
133 ltq_dma_w32(ch
->phys
, LTQ_DMA_CDBA
);
134 ltq_dma_w32(LTQ_DESC_NUM
, LTQ_DMA_CDLEN
);
135 ltq_dma_w32_mask(DMA_CHAN_ON
, 0, LTQ_DMA_CCTRL
);
137 ltq_dma_w32_mask(0, DMA_CHAN_RST
, LTQ_DMA_CCTRL
);
138 while (ltq_dma_r32(LTQ_DMA_CCTRL
) & DMA_CHAN_RST
)
140 spin_unlock_irqrestore(<q_dma_lock
, flags
);
144 ltq_dma_alloc_tx(struct ltq_dma_channel
*ch
)
150 spin_lock_irqsave(<q_dma_lock
, flags
);
151 ltq_dma_w32(DMA_DESCPT
, LTQ_DMA_CIE
);
152 ltq_dma_w32_mask(0, 1 << ch
->nr
, LTQ_DMA_IRNEN
);
153 ltq_dma_w32(DMA_WEIGHT
| DMA_TX
, LTQ_DMA_CCTRL
);
154 spin_unlock_irqrestore(<q_dma_lock
, flags
);
156 EXPORT_SYMBOL_GPL(ltq_dma_alloc_tx
);
159 ltq_dma_alloc_rx(struct ltq_dma_channel
*ch
)
165 spin_lock_irqsave(<q_dma_lock
, flags
);
166 ltq_dma_w32(DMA_DESCPT
, LTQ_DMA_CIE
);
167 ltq_dma_w32_mask(0, 1 << ch
->nr
, LTQ_DMA_IRNEN
);
168 ltq_dma_w32(DMA_WEIGHT
, LTQ_DMA_CCTRL
);
169 spin_unlock_irqrestore(<q_dma_lock
, flags
);
171 EXPORT_SYMBOL_GPL(ltq_dma_alloc_rx
);
174 ltq_dma_free(struct ltq_dma_channel
*ch
)
179 dma_free_coherent(ch
->dev
, LTQ_DESC_NUM
* LTQ_DESC_SIZE
,
180 ch
->desc_base
, ch
->phys
);
182 EXPORT_SYMBOL_GPL(ltq_dma_free
);
185 ltq_dma_init_port(int p
, int tx_burst
, int rx_burst
)
187 ltq_dma_w32(p
, LTQ_DMA_PS
);
191 * Tell the DMA engine to swap the endianness of data frames and
192 * drop packets if the channel arbitration fails.
194 ltq_dma_w32_mask(0, (DMA_ETOP_ENDIANNESS
| DMA_PDEN
),
204 ltq_dma_w32_mask(0x0c, (DMA_PCTRL_8W_BURST
<< DMA_RX_BURST_SHIFT
),
208 ltq_dma_w32_mask(0x0c, (DMA_PCTRL_4W_BURST
<< DMA_RX_BURST_SHIFT
),
212 ltq_dma_w32_mask(0x0c, (DMA_PCTRL_2W_BURST
<< DMA_RX_BURST_SHIFT
),
221 ltq_dma_w32_mask(0x30, (DMA_PCTRL_8W_BURST
<< DMA_TX_BURST_SHIFT
),
225 ltq_dma_w32_mask(0x30, (DMA_PCTRL_4W_BURST
<< DMA_TX_BURST_SHIFT
),
229 ltq_dma_w32_mask(0x30, (DMA_PCTRL_2W_BURST
<< DMA_TX_BURST_SHIFT
),
236 EXPORT_SYMBOL_GPL(ltq_dma_init_port
);
239 ltq_dma_init(struct platform_device
*pdev
)
242 unsigned int id
, nchannels
;
245 ltq_dma_membase
= devm_platform_get_and_ioremap_resource(pdev
, 0, NULL
);
246 if (IS_ERR(ltq_dma_membase
))
247 panic("Failed to remap dma resource");
249 /* power up and reset the dma engine */
250 clk
= clk_get(&pdev
->dev
, NULL
);
252 panic("Failed to get dma clock");
255 ltq_dma_w32_mask(0, DMA_RESET
, LTQ_DMA_CTRL
);
259 /* disable all interrupts */
260 ltq_dma_w32(0, LTQ_DMA_IRNEN
);
262 /* reset/configure each channel */
263 id
= ltq_dma_r32(LTQ_DMA_ID
);
264 nchannels
= ((id
& DMA_ID_CHNR
) >> 20);
265 for (i
= 0; i
< nchannels
; i
++) {
266 ltq_dma_w32(i
, LTQ_DMA_CS
);
267 ltq_dma_w32(DMA_CHAN_RST
, LTQ_DMA_CCTRL
);
268 ltq_dma_w32(DMA_POLL
| DMA_CLK_DIV4
, LTQ_DMA_CPOLL
);
269 ltq_dma_w32_mask(DMA_CHAN_ON
, 0, LTQ_DMA_CCTRL
);
273 "Init done - hw rev: %X, ports: %d, channels: %d\n",
274 id
& 0x1f, (id
>> 16) & 0xf, nchannels
);
279 static const struct of_device_id dma_match
[] = {
280 { .compatible
= "lantiq,dma-xway" },
284 static struct platform_driver dma_driver
= {
285 .probe
= ltq_dma_init
,
288 .of_match_table
= dma_match
,
295 return platform_driver_register(&dma_driver
);
298 postcore_initcall(dma_init
);