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>
17 #include <lantiq_soc.h>
20 #define LTQ_DMA_ID 0x08
21 #define LTQ_DMA_CTRL 0x10
22 #define LTQ_DMA_CPOLL 0x14
23 #define LTQ_DMA_CS 0x18
24 #define LTQ_DMA_CCTRL 0x1C
25 #define LTQ_DMA_CDBA 0x20
26 #define LTQ_DMA_CDLEN 0x24
27 #define LTQ_DMA_CIS 0x28
28 #define LTQ_DMA_CIE 0x2C
29 #define LTQ_DMA_PS 0x40
30 #define LTQ_DMA_PCTRL 0x44
31 #define LTQ_DMA_IRNEN 0xf4
33 #define DMA_ID_CHNR GENMASK(26, 20) /* channel number */
34 #define DMA_DESCPT BIT(3) /* descriptor complete irq */
35 #define DMA_TX BIT(8) /* TX channel direction */
36 #define DMA_CHAN_ON BIT(0) /* channel on / off bit */
37 #define DMA_PDEN BIT(6) /* enable packet drop */
38 #define DMA_CHAN_RST BIT(1) /* channel on / off bit */
39 #define DMA_RESET BIT(0) /* channel on / off bit */
40 #define DMA_IRQ_ACK 0x7e /* IRQ status register */
41 #define DMA_POLL BIT(31) /* turn on channel polling */
42 #define DMA_CLK_DIV4 BIT(6) /* polling clock divider */
43 #define DMA_PCTRL_2W_BURST 0x1 /* 2 word burst length */
44 #define DMA_PCTRL_4W_BURST 0x2 /* 4 word burst length */
45 #define DMA_PCTRL_8W_BURST 0x3 /* 8 word burst length */
46 #define DMA_TX_BURST_SHIFT 4 /* tx burst shift */
47 #define DMA_RX_BURST_SHIFT 2 /* rx burst shift */
48 #define DMA_ETOP_ENDIANNESS (0xf << 8) /* endianness swap etop channels */
49 #define DMA_WEIGHT (BIT(17) | BIT(16)) /* default channel wheight */
51 #define ltq_dma_r32(x) ltq_r32(ltq_dma_membase + (x))
52 #define ltq_dma_w32(x, y) ltq_w32(x, ltq_dma_membase + (y))
53 #define ltq_dma_w32_mask(x, y, z) ltq_w32_mask(x, y, \
54 ltq_dma_membase + (z))
56 static void __iomem *ltq_dma_membase;
57 static DEFINE_SPINLOCK(ltq_dma_lock);
60 ltq_dma_enable_irq(struct ltq_dma_channel *ch)
64 spin_lock_irqsave(<q_dma_lock, flags);
65 ltq_dma_w32(ch->nr, LTQ_DMA_CS);
66 ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
67 spin_unlock_irqrestore(<q_dma_lock, flags);
69 EXPORT_SYMBOL_GPL(ltq_dma_enable_irq);
72 ltq_dma_disable_irq(struct ltq_dma_channel *ch)
76 spin_lock_irqsave(<q_dma_lock, flags);
77 ltq_dma_w32(ch->nr, LTQ_DMA_CS);
78 ltq_dma_w32_mask(1 << ch->nr, 0, LTQ_DMA_IRNEN);
79 spin_unlock_irqrestore(<q_dma_lock, flags);
81 EXPORT_SYMBOL_GPL(ltq_dma_disable_irq);
84 ltq_dma_ack_irq(struct ltq_dma_channel *ch)
88 spin_lock_irqsave(<q_dma_lock, flags);
89 ltq_dma_w32(ch->nr, LTQ_DMA_CS);
90 ltq_dma_w32(DMA_IRQ_ACK, LTQ_DMA_CIS);
91 spin_unlock_irqrestore(<q_dma_lock, flags);
93 EXPORT_SYMBOL_GPL(ltq_dma_ack_irq);
96 ltq_dma_open(struct ltq_dma_channel *ch)
100 spin_lock_irqsave(<q_dma_lock, flag);
101 ltq_dma_w32(ch->nr, LTQ_DMA_CS);
102 ltq_dma_w32_mask(0, DMA_CHAN_ON, LTQ_DMA_CCTRL);
103 spin_unlock_irqrestore(<q_dma_lock, flag);
105 EXPORT_SYMBOL_GPL(ltq_dma_open);
108 ltq_dma_close(struct ltq_dma_channel *ch)
112 spin_lock_irqsave(<q_dma_lock, flag);
113 ltq_dma_w32(ch->nr, LTQ_DMA_CS);
114 ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
115 ltq_dma_w32_mask(1 << ch->nr, 0, LTQ_DMA_IRNEN);
116 spin_unlock_irqrestore(<q_dma_lock, flag);
118 EXPORT_SYMBOL_GPL(ltq_dma_close);
121 ltq_dma_alloc(struct ltq_dma_channel *ch)
126 ch->desc_base = dma_alloc_coherent(ch->dev,
127 LTQ_DESC_NUM * LTQ_DESC_SIZE,
128 &ch->phys, GFP_ATOMIC);
130 spin_lock_irqsave(<q_dma_lock, flags);
131 ltq_dma_w32(ch->nr, LTQ_DMA_CS);
132 ltq_dma_w32(ch->phys, LTQ_DMA_CDBA);
133 ltq_dma_w32(LTQ_DESC_NUM, LTQ_DMA_CDLEN);
134 ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
136 ltq_dma_w32_mask(0, DMA_CHAN_RST, LTQ_DMA_CCTRL);
137 while (ltq_dma_r32(LTQ_DMA_CCTRL) & DMA_CHAN_RST)
139 spin_unlock_irqrestore(<q_dma_lock, flags);
143 ltq_dma_alloc_tx(struct ltq_dma_channel *ch)
149 spin_lock_irqsave(<q_dma_lock, flags);
150 ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE);
151 ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
152 ltq_dma_w32(DMA_WEIGHT | DMA_TX, LTQ_DMA_CCTRL);
153 spin_unlock_irqrestore(<q_dma_lock, flags);
155 EXPORT_SYMBOL_GPL(ltq_dma_alloc_tx);
158 ltq_dma_alloc_rx(struct ltq_dma_channel *ch)
164 spin_lock_irqsave(<q_dma_lock, flags);
165 ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE);
166 ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
167 ltq_dma_w32(DMA_WEIGHT, LTQ_DMA_CCTRL);
168 spin_unlock_irqrestore(<q_dma_lock, flags);
170 EXPORT_SYMBOL_GPL(ltq_dma_alloc_rx);
173 ltq_dma_free(struct ltq_dma_channel *ch)
178 dma_free_coherent(ch->dev, LTQ_DESC_NUM * LTQ_DESC_SIZE,
179 ch->desc_base, ch->phys);
181 EXPORT_SYMBOL_GPL(ltq_dma_free);
184 ltq_dma_init_port(int p)
186 ltq_dma_w32(p, LTQ_DMA_PS);
190 * Tell the DMA engine to swap the endianness of data frames and
191 * drop packets if the channel arbitration fails.
193 ltq_dma_w32_mask(0, DMA_ETOP_ENDIANNESS | DMA_PDEN,
198 ltq_dma_w32((DMA_PCTRL_2W_BURST << DMA_TX_BURST_SHIFT) |
199 (DMA_PCTRL_2W_BURST << DMA_RX_BURST_SHIFT),
207 EXPORT_SYMBOL_GPL(ltq_dma_init_port);
210 ltq_dma_init(struct platform_device *pdev)
213 struct resource *res;
214 unsigned int id, nchannels;
217 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
218 ltq_dma_membase = devm_ioremap_resource(&pdev->dev, res);
219 if (IS_ERR(ltq_dma_membase))
220 panic("Failed to remap dma resource");
222 /* power up and reset the dma engine */
223 clk = clk_get(&pdev->dev, NULL);
225 panic("Failed to get dma clock");
228 ltq_dma_w32_mask(0, DMA_RESET, LTQ_DMA_CTRL);
232 /* disable all interrupts */
233 ltq_dma_w32(0, LTQ_DMA_IRNEN);
235 /* reset/configure each channel */
236 id = ltq_dma_r32(LTQ_DMA_ID);
237 nchannels = ((id & DMA_ID_CHNR) >> 20);
238 for (i = 0; i < nchannels; i++) {
239 ltq_dma_w32(i, LTQ_DMA_CS);
240 ltq_dma_w32(DMA_CHAN_RST, LTQ_DMA_CCTRL);
241 ltq_dma_w32(DMA_POLL | DMA_CLK_DIV4, LTQ_DMA_CPOLL);
242 ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
246 "Init done - hw rev: %X, ports: %d, channels: %d\n",
247 id & 0x1f, (id >> 16) & 0xf, nchannels);
252 static const struct of_device_id dma_match[] = {
253 { .compatible = "lantiq,dma-xway" },
257 static struct platform_driver dma_driver = {
258 .probe = ltq_dma_init,
261 .of_match_table = dma_match,
268 return platform_driver_register(&dma_driver);
271 postcore_initcall(dma_init);