145051539SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2dfec1a82SJohn Crispin /*
3dfec1a82SJohn Crispin *
497b92108SJohn Crispin * Copyright (C) 2011 John Crispin <john@phrozen.org>
5dfec1a82SJohn Crispin */
6dfec1a82SJohn Crispin
7dfec1a82SJohn Crispin #include <linux/init.h>
8dfec1a82SJohn Crispin #include <linux/platform_device.h>
9dfec1a82SJohn Crispin #include <linux/io.h>
10dfec1a82SJohn Crispin #include <linux/dma-mapping.h>
1126dd3e4fSPaul Gortmaker #include <linux/export.h>
1298e58b01SHauke Mehrtens #include <linux/spinlock.h>
13ddd4eecaSJohn Crispin #include <linux/clk.h>
14c12aa581SAleksander Jan Bajkowski #include <linux/delay.h>
157c390a7eSThierry Reding #include <linux/err.h>
1695af1df6SMarc Zyngier #include <linux/of.h>
17dfec1a82SJohn Crispin
18dfec1a82SJohn Crispin #include <lantiq_soc.h>
19dfec1a82SJohn Crispin #include <xway_dma.h>
20dfec1a82SJohn Crispin
21b8b3acbeSJohn Crispin #define LTQ_DMA_ID 0x08
22dfec1a82SJohn Crispin #define LTQ_DMA_CTRL 0x10
23dfec1a82SJohn Crispin #define LTQ_DMA_CPOLL 0x14
24dfec1a82SJohn Crispin #define LTQ_DMA_CS 0x18
25dfec1a82SJohn Crispin #define LTQ_DMA_CCTRL 0x1C
26dfec1a82SJohn Crispin #define LTQ_DMA_CDBA 0x20
27dfec1a82SJohn Crispin #define LTQ_DMA_CDLEN 0x24
28dfec1a82SJohn Crispin #define LTQ_DMA_CIS 0x28
29dfec1a82SJohn Crispin #define LTQ_DMA_CIE 0x2C
30dfec1a82SJohn Crispin #define LTQ_DMA_PS 0x40
31dfec1a82SJohn Crispin #define LTQ_DMA_PCTRL 0x44
32dfec1a82SJohn Crispin #define LTQ_DMA_IRNEN 0xf4
33dfec1a82SJohn Crispin
345ca9ce2bSAleksander Jan Bajkowski #define DMA_ID_CHNR GENMASK(26, 20) /* channel number */
35dfec1a82SJohn Crispin #define DMA_DESCPT BIT(3) /* descriptor complete irq */
36dfec1a82SJohn Crispin #define DMA_TX BIT(8) /* TX channel direction */
37dfec1a82SJohn Crispin #define DMA_CHAN_ON BIT(0) /* channel on / off bit */
38dfec1a82SJohn Crispin #define DMA_PDEN BIT(6) /* enable packet drop */
39dfec1a82SJohn Crispin #define DMA_CHAN_RST BIT(1) /* channel on / off bit */
40dfec1a82SJohn Crispin #define DMA_RESET BIT(0) /* channel on / off bit */
41dfec1a82SJohn Crispin #define DMA_IRQ_ACK 0x7e /* IRQ status register */
42dfec1a82SJohn Crispin #define DMA_POLL BIT(31) /* turn on channel polling */
43dfec1a82SJohn Crispin #define DMA_CLK_DIV4 BIT(6) /* polling clock divider */
445ad74d39SAleksander Jan Bajkowski #define DMA_PCTRL_2W_BURST 0x1 /* 2 word burst length */
455ad74d39SAleksander Jan Bajkowski #define DMA_PCTRL_4W_BURST 0x2 /* 4 word burst length */
465ad74d39SAleksander Jan Bajkowski #define DMA_PCTRL_8W_BURST 0x3 /* 8 word burst length */
475ad74d39SAleksander Jan Bajkowski #define DMA_TX_BURST_SHIFT 4 /* tx burst shift */
485ad74d39SAleksander Jan Bajkowski #define DMA_RX_BURST_SHIFT 2 /* rx burst shift */
49d08be0dbSMasanari Iida #define DMA_ETOP_ENDIANNESS (0xf << 8) /* endianness swap etop channels */
50dfec1a82SJohn Crispin #define DMA_WEIGHT (BIT(17) | BIT(16)) /* default channel wheight */
51dfec1a82SJohn Crispin
52dfec1a82SJohn Crispin #define ltq_dma_r32(x) ltq_r32(ltq_dma_membase + (x))
53dfec1a82SJohn Crispin #define ltq_dma_w32(x, y) ltq_w32(x, ltq_dma_membase + (y))
54dfec1a82SJohn Crispin #define ltq_dma_w32_mask(x, y, z) ltq_w32_mask(x, y, \
55dfec1a82SJohn Crispin ltq_dma_membase + (z))
56dfec1a82SJohn Crispin
57dfec1a82SJohn Crispin static void __iomem *ltq_dma_membase;
5898e58b01SHauke Mehrtens static DEFINE_SPINLOCK(ltq_dma_lock);
59dfec1a82SJohn Crispin
60dfec1a82SJohn Crispin void
ltq_dma_enable_irq(struct ltq_dma_channel * ch)61dfec1a82SJohn Crispin ltq_dma_enable_irq(struct ltq_dma_channel *ch)
62dfec1a82SJohn Crispin {
63dfec1a82SJohn Crispin unsigned long flags;
64dfec1a82SJohn Crispin
6598e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags);
66dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS);
67dfec1a82SJohn Crispin ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
6898e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags);
69dfec1a82SJohn Crispin }
70dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_enable_irq);
71dfec1a82SJohn Crispin
72dfec1a82SJohn Crispin void
ltq_dma_disable_irq(struct ltq_dma_channel * ch)73dfec1a82SJohn Crispin ltq_dma_disable_irq(struct ltq_dma_channel *ch)
74dfec1a82SJohn Crispin {
75dfec1a82SJohn Crispin unsigned long flags;
76dfec1a82SJohn Crispin
7798e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags);
78dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS);
79dfec1a82SJohn Crispin ltq_dma_w32_mask(1 << ch->nr, 0, LTQ_DMA_IRNEN);
8098e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags);
81dfec1a82SJohn Crispin }
82dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_disable_irq);
83dfec1a82SJohn Crispin
84dfec1a82SJohn Crispin void
ltq_dma_ack_irq(struct ltq_dma_channel * ch)85dfec1a82SJohn Crispin ltq_dma_ack_irq(struct ltq_dma_channel *ch)
86dfec1a82SJohn Crispin {
87dfec1a82SJohn Crispin unsigned long flags;
88dfec1a82SJohn Crispin
8998e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags);
90dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS);
91dfec1a82SJohn Crispin ltq_dma_w32(DMA_IRQ_ACK, LTQ_DMA_CIS);
9298e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags);
93dfec1a82SJohn Crispin }
94dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_ack_irq);
95dfec1a82SJohn Crispin
96dfec1a82SJohn Crispin void
ltq_dma_open(struct ltq_dma_channel * ch)97dfec1a82SJohn Crispin ltq_dma_open(struct ltq_dma_channel *ch)
98dfec1a82SJohn Crispin {
99dfec1a82SJohn Crispin unsigned long flag;
100dfec1a82SJohn Crispin
10198e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flag);
102dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS);
103dfec1a82SJohn Crispin ltq_dma_w32_mask(0, DMA_CHAN_ON, LTQ_DMA_CCTRL);
10498e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flag);
105dfec1a82SJohn Crispin }
106dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_open);
107dfec1a82SJohn Crispin
108dfec1a82SJohn Crispin void
ltq_dma_close(struct ltq_dma_channel * ch)109dfec1a82SJohn Crispin ltq_dma_close(struct ltq_dma_channel *ch)
110dfec1a82SJohn Crispin {
111dfec1a82SJohn Crispin unsigned long flag;
112dfec1a82SJohn Crispin
11398e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flag);
114dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS);
115dfec1a82SJohn Crispin ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
11698e58b01SHauke Mehrtens ltq_dma_w32_mask(1 << ch->nr, 0, LTQ_DMA_IRNEN);
11798e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flag);
118dfec1a82SJohn Crispin }
119dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_close);
120dfec1a82SJohn Crispin
121dfec1a82SJohn Crispin static void
ltq_dma_alloc(struct ltq_dma_channel * ch)122dfec1a82SJohn Crispin ltq_dma_alloc(struct ltq_dma_channel *ch)
123dfec1a82SJohn Crispin {
124dfec1a82SJohn Crispin unsigned long flags;
125dfec1a82SJohn Crispin
126dfec1a82SJohn Crispin ch->desc = 0;
127750afb08SLuis Chamberlain ch->desc_base = dma_alloc_coherent(ch->dev,
128dfec1a82SJohn Crispin LTQ_DESC_NUM * LTQ_DESC_SIZE,
129dfec1a82SJohn Crispin &ch->phys, GFP_ATOMIC);
130dfec1a82SJohn Crispin
13198e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags);
132dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS);
133dfec1a82SJohn Crispin ltq_dma_w32(ch->phys, LTQ_DMA_CDBA);
134dfec1a82SJohn Crispin ltq_dma_w32(LTQ_DESC_NUM, LTQ_DMA_CDLEN);
135dfec1a82SJohn Crispin ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
136dfec1a82SJohn Crispin wmb();
137dfec1a82SJohn Crispin ltq_dma_w32_mask(0, DMA_CHAN_RST, LTQ_DMA_CCTRL);
138dfec1a82SJohn Crispin while (ltq_dma_r32(LTQ_DMA_CCTRL) & DMA_CHAN_RST)
139dfec1a82SJohn Crispin ;
14098e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags);
141dfec1a82SJohn Crispin }
142dfec1a82SJohn Crispin
143dfec1a82SJohn Crispin void
ltq_dma_alloc_tx(struct ltq_dma_channel * ch)144dfec1a82SJohn Crispin ltq_dma_alloc_tx(struct ltq_dma_channel *ch)
145dfec1a82SJohn Crispin {
146dfec1a82SJohn Crispin unsigned long flags;
147dfec1a82SJohn Crispin
148dfec1a82SJohn Crispin ltq_dma_alloc(ch);
149dfec1a82SJohn Crispin
15098e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags);
151dfec1a82SJohn Crispin ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE);
152dfec1a82SJohn Crispin ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
153dfec1a82SJohn Crispin ltq_dma_w32(DMA_WEIGHT | DMA_TX, LTQ_DMA_CCTRL);
15498e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags);
155dfec1a82SJohn Crispin }
156dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_alloc_tx);
157dfec1a82SJohn Crispin
158dfec1a82SJohn Crispin void
ltq_dma_alloc_rx(struct ltq_dma_channel * ch)159dfec1a82SJohn Crispin ltq_dma_alloc_rx(struct ltq_dma_channel *ch)
160dfec1a82SJohn Crispin {
161dfec1a82SJohn Crispin unsigned long flags;
162dfec1a82SJohn Crispin
163dfec1a82SJohn Crispin ltq_dma_alloc(ch);
164dfec1a82SJohn Crispin
16598e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags);
166dfec1a82SJohn Crispin ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE);
167dfec1a82SJohn Crispin ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
168dfec1a82SJohn Crispin ltq_dma_w32(DMA_WEIGHT, LTQ_DMA_CCTRL);
16998e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags);
170dfec1a82SJohn Crispin }
171dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_alloc_rx);
172dfec1a82SJohn Crispin
173dfec1a82SJohn Crispin void
ltq_dma_free(struct ltq_dma_channel * ch)174dfec1a82SJohn Crispin ltq_dma_free(struct ltq_dma_channel *ch)
175dfec1a82SJohn Crispin {
176dfec1a82SJohn Crispin if (!ch->desc_base)
177dfec1a82SJohn Crispin return;
178dfec1a82SJohn Crispin ltq_dma_close(ch);
1792d946e5bSHauke Mehrtens dma_free_coherent(ch->dev, LTQ_DESC_NUM * LTQ_DESC_SIZE,
180dfec1a82SJohn Crispin ch->desc_base, ch->phys);
181dfec1a82SJohn Crispin }
182dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_free);
183dfec1a82SJohn Crispin
184dfec1a82SJohn Crispin void
ltq_dma_init_port(int p,int tx_burst,int rx_burst)18549293bbcSAleksander Jan Bajkowski ltq_dma_init_port(int p, int tx_burst, int rx_burst)
186dfec1a82SJohn Crispin {
187dfec1a82SJohn Crispin ltq_dma_w32(p, LTQ_DMA_PS);
188dfec1a82SJohn Crispin switch (p) {
189dfec1a82SJohn Crispin case DMA_PORT_ETOP:
190dfec1a82SJohn Crispin /*
191d08be0dbSMasanari Iida * Tell the DMA engine to swap the endianness of data frames and
192dfec1a82SJohn Crispin * drop packets if the channel arbitration fails.
193dfec1a82SJohn Crispin */
19449293bbcSAleksander Jan Bajkowski ltq_dma_w32_mask(0, (DMA_ETOP_ENDIANNESS | DMA_PDEN),
195dfec1a82SJohn Crispin LTQ_DMA_PCTRL);
196dfec1a82SJohn Crispin break;
197dfec1a82SJohn Crispin
19849293bbcSAleksander Jan Bajkowski default:
19949293bbcSAleksander Jan Bajkowski break;
20049293bbcSAleksander Jan Bajkowski }
20149293bbcSAleksander Jan Bajkowski
20249293bbcSAleksander Jan Bajkowski switch (rx_burst) {
20349293bbcSAleksander Jan Bajkowski case 8:
20449293bbcSAleksander Jan Bajkowski ltq_dma_w32_mask(0x0c, (DMA_PCTRL_8W_BURST << DMA_RX_BURST_SHIFT),
205dfec1a82SJohn Crispin LTQ_DMA_PCTRL);
206dfec1a82SJohn Crispin break;
20749293bbcSAleksander Jan Bajkowski case 4:
20849293bbcSAleksander Jan Bajkowski ltq_dma_w32_mask(0x0c, (DMA_PCTRL_4W_BURST << DMA_RX_BURST_SHIFT),
20949293bbcSAleksander Jan Bajkowski LTQ_DMA_PCTRL);
21049293bbcSAleksander Jan Bajkowski break;
21149293bbcSAleksander Jan Bajkowski case 2:
21249293bbcSAleksander Jan Bajkowski ltq_dma_w32_mask(0x0c, (DMA_PCTRL_2W_BURST << DMA_RX_BURST_SHIFT),
21349293bbcSAleksander Jan Bajkowski LTQ_DMA_PCTRL);
21449293bbcSAleksander Jan Bajkowski break;
21549293bbcSAleksander Jan Bajkowski default:
21649293bbcSAleksander Jan Bajkowski break;
21749293bbcSAleksander Jan Bajkowski }
218dfec1a82SJohn Crispin
21949293bbcSAleksander Jan Bajkowski switch (tx_burst) {
22049293bbcSAleksander Jan Bajkowski case 8:
22149293bbcSAleksander Jan Bajkowski ltq_dma_w32_mask(0x30, (DMA_PCTRL_8W_BURST << DMA_TX_BURST_SHIFT),
22249293bbcSAleksander Jan Bajkowski LTQ_DMA_PCTRL);
22349293bbcSAleksander Jan Bajkowski break;
22449293bbcSAleksander Jan Bajkowski case 4:
22549293bbcSAleksander Jan Bajkowski ltq_dma_w32_mask(0x30, (DMA_PCTRL_4W_BURST << DMA_TX_BURST_SHIFT),
22649293bbcSAleksander Jan Bajkowski LTQ_DMA_PCTRL);
22749293bbcSAleksander Jan Bajkowski break;
22849293bbcSAleksander Jan Bajkowski case 2:
22949293bbcSAleksander Jan Bajkowski ltq_dma_w32_mask(0x30, (DMA_PCTRL_2W_BURST << DMA_TX_BURST_SHIFT),
23049293bbcSAleksander Jan Bajkowski LTQ_DMA_PCTRL);
23149293bbcSAleksander Jan Bajkowski break;
232dfec1a82SJohn Crispin default:
233dfec1a82SJohn Crispin break;
234dfec1a82SJohn Crispin }
235dfec1a82SJohn Crispin }
236dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_init_port);
237dfec1a82SJohn Crispin
23828eb0e46SGreg Kroah-Hartman static int
ltq_dma_init(struct platform_device * pdev)239ddd4eecaSJohn Crispin ltq_dma_init(struct platform_device *pdev)
240dfec1a82SJohn Crispin {
241ddd4eecaSJohn Crispin struct clk *clk;
2425ca9ce2bSAleksander Jan Bajkowski unsigned int id, nchannels;
243dfec1a82SJohn Crispin int i;
244dfec1a82SJohn Crispin
245*00c11b40SYe Xingchen ltq_dma_membase = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
2467c390a7eSThierry Reding if (IS_ERR(ltq_dma_membase))
247ddd4eecaSJohn Crispin panic("Failed to remap dma resource");
248dfec1a82SJohn Crispin
249dfec1a82SJohn Crispin /* power up and reset the dma engine */
250ddd4eecaSJohn Crispin clk = clk_get(&pdev->dev, NULL);
251ddd4eecaSJohn Crispin if (IS_ERR(clk))
252ddd4eecaSJohn Crispin panic("Failed to get dma clock");
253ddd4eecaSJohn Crispin
254ddd4eecaSJohn Crispin clk_enable(clk);
255dfec1a82SJohn Crispin ltq_dma_w32_mask(0, DMA_RESET, LTQ_DMA_CTRL);
256dfec1a82SJohn Crispin
257c12aa581SAleksander Jan Bajkowski usleep_range(1, 10);
258c12aa581SAleksander Jan Bajkowski
259dfec1a82SJohn Crispin /* disable all interrupts */
260dfec1a82SJohn Crispin ltq_dma_w32(0, LTQ_DMA_IRNEN);
261dfec1a82SJohn Crispin
262dfec1a82SJohn Crispin /* reset/configure each channel */
2635ca9ce2bSAleksander Jan Bajkowski id = ltq_dma_r32(LTQ_DMA_ID);
2645ca9ce2bSAleksander Jan Bajkowski nchannels = ((id & DMA_ID_CHNR) >> 20);
2655ca9ce2bSAleksander Jan Bajkowski for (i = 0; i < nchannels; i++) {
266dfec1a82SJohn Crispin ltq_dma_w32(i, LTQ_DMA_CS);
267dfec1a82SJohn Crispin ltq_dma_w32(DMA_CHAN_RST, LTQ_DMA_CCTRL);
268dfec1a82SJohn Crispin ltq_dma_w32(DMA_POLL | DMA_CLK_DIV4, LTQ_DMA_CPOLL);
269dfec1a82SJohn Crispin ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
270dfec1a82SJohn Crispin }
271b8b3acbeSJohn Crispin
272b8b3acbeSJohn Crispin dev_info(&pdev->dev,
273b8b3acbeSJohn Crispin "Init done - hw rev: %X, ports: %d, channels: %d\n",
2745ca9ce2bSAleksander Jan Bajkowski id & 0x1f, (id >> 16) & 0xf, nchannels);
275b8b3acbeSJohn Crispin
276dfec1a82SJohn Crispin return 0;
277dfec1a82SJohn Crispin }
278dfec1a82SJohn Crispin
279ddd4eecaSJohn Crispin static const struct of_device_id dma_match[] = {
280ddd4eecaSJohn Crispin { .compatible = "lantiq,dma-xway" },
281ddd4eecaSJohn Crispin {},
282ddd4eecaSJohn Crispin };
283ddd4eecaSJohn Crispin
284ddd4eecaSJohn Crispin static struct platform_driver dma_driver = {
285ddd4eecaSJohn Crispin .probe = ltq_dma_init,
286ddd4eecaSJohn Crispin .driver = {
287ddd4eecaSJohn Crispin .name = "dma-xway",
288ddd4eecaSJohn Crispin .of_match_table = dma_match,
289ddd4eecaSJohn Crispin },
290ddd4eecaSJohn Crispin };
291ddd4eecaSJohn Crispin
292ddd4eecaSJohn Crispin int __init
dma_init(void)293ddd4eecaSJohn Crispin dma_init(void)
294ddd4eecaSJohn Crispin {
295ddd4eecaSJohn Crispin return platform_driver_register(&dma_driver);
296ddd4eecaSJohn Crispin }
297ddd4eecaSJohn Crispin
298ddd4eecaSJohn Crispin postcore_initcall(dma_init);
299