treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / dma / milbeaut-xdmac.c
blobab3d2f39537835c20c6605322905d1be3ba17cba
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (C) 2019 Linaro Ltd.
4 // Copyright (C) 2019 Socionext Inc.
6 #include <linux/bits.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/dmaengine.h>
9 #include <linux/interrupt.h>
10 #include <linux/iopoll.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <linux/of_dma.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/types.h>
17 #include <linux/bitfield.h>
19 #include "virt-dma.h"
21 /* global register */
22 #define M10V_XDACS 0x00
24 /* channel local register */
25 #define M10V_XDTBC 0x10
26 #define M10V_XDSSA 0x14
27 #define M10V_XDDSA 0x18
28 #define M10V_XDSAC 0x1C
29 #define M10V_XDDAC 0x20
30 #define M10V_XDDCC 0x24
31 #define M10V_XDDES 0x28
32 #define M10V_XDDPC 0x2C
33 #define M10V_XDDSD 0x30
35 #define M10V_XDACS_XE BIT(28)
37 #define M10V_DEFBS 0x3
38 #define M10V_DEFBL 0xf
40 #define M10V_XDSAC_SBS GENMASK(17, 16)
41 #define M10V_XDSAC_SBL GENMASK(11, 8)
43 #define M10V_XDDAC_DBS GENMASK(17, 16)
44 #define M10V_XDDAC_DBL GENMASK(11, 8)
46 #define M10V_XDDES_CE BIT(28)
47 #define M10V_XDDES_SE BIT(24)
48 #define M10V_XDDES_SA BIT(15)
49 #define M10V_XDDES_TF GENMASK(23, 20)
50 #define M10V_XDDES_EI BIT(1)
51 #define M10V_XDDES_TI BIT(0)
53 #define M10V_XDDSD_IS_MASK GENMASK(3, 0)
54 #define M10V_XDDSD_IS_NORMAL 0x8
56 #define MLB_XDMAC_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
57 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
58 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
59 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
61 struct milbeaut_xdmac_desc {
62 struct virt_dma_desc vd;
63 size_t len;
64 dma_addr_t src;
65 dma_addr_t dst;
68 struct milbeaut_xdmac_chan {
69 struct virt_dma_chan vc;
70 struct milbeaut_xdmac_desc *md;
71 void __iomem *reg_ch_base;
74 struct milbeaut_xdmac_device {
75 struct dma_device ddev;
76 void __iomem *reg_base;
77 struct milbeaut_xdmac_chan channels[0];
80 static struct milbeaut_xdmac_chan *
81 to_milbeaut_xdmac_chan(struct virt_dma_chan *vc)
83 return container_of(vc, struct milbeaut_xdmac_chan, vc);
86 static struct milbeaut_xdmac_desc *
87 to_milbeaut_xdmac_desc(struct virt_dma_desc *vd)
89 return container_of(vd, struct milbeaut_xdmac_desc, vd);
92 /* mc->vc.lock must be held by caller */
93 static struct milbeaut_xdmac_desc *
94 milbeaut_xdmac_next_desc(struct milbeaut_xdmac_chan *mc)
96 struct virt_dma_desc *vd;
98 vd = vchan_next_desc(&mc->vc);
99 if (!vd) {
100 mc->md = NULL;
101 return NULL;
104 list_del(&vd->node);
106 mc->md = to_milbeaut_xdmac_desc(vd);
108 return mc->md;
111 /* mc->vc.lock must be held by caller */
112 static void milbeaut_chan_start(struct milbeaut_xdmac_chan *mc,
113 struct milbeaut_xdmac_desc *md)
115 u32 val;
117 /* Setup the channel */
118 val = md->len - 1;
119 writel_relaxed(val, mc->reg_ch_base + M10V_XDTBC);
121 val = md->src;
122 writel_relaxed(val, mc->reg_ch_base + M10V_XDSSA);
124 val = md->dst;
125 writel_relaxed(val, mc->reg_ch_base + M10V_XDDSA);
127 val = readl_relaxed(mc->reg_ch_base + M10V_XDSAC);
128 val &= ~(M10V_XDSAC_SBS | M10V_XDSAC_SBL);
129 val |= FIELD_PREP(M10V_XDSAC_SBS, M10V_DEFBS) |
130 FIELD_PREP(M10V_XDSAC_SBL, M10V_DEFBL);
131 writel_relaxed(val, mc->reg_ch_base + M10V_XDSAC);
133 val = readl_relaxed(mc->reg_ch_base + M10V_XDDAC);
134 val &= ~(M10V_XDDAC_DBS | M10V_XDDAC_DBL);
135 val |= FIELD_PREP(M10V_XDDAC_DBS, M10V_DEFBS) |
136 FIELD_PREP(M10V_XDDAC_DBL, M10V_DEFBL);
137 writel_relaxed(val, mc->reg_ch_base + M10V_XDDAC);
139 /* Start the channel */
140 val = readl_relaxed(mc->reg_ch_base + M10V_XDDES);
141 val &= ~(M10V_XDDES_CE | M10V_XDDES_SE | M10V_XDDES_TF |
142 M10V_XDDES_EI | M10V_XDDES_TI);
143 val |= FIELD_PREP(M10V_XDDES_CE, 1) | FIELD_PREP(M10V_XDDES_SE, 1) |
144 FIELD_PREP(M10V_XDDES_TF, 1) | FIELD_PREP(M10V_XDDES_EI, 1) |
145 FIELD_PREP(M10V_XDDES_TI, 1);
146 writel_relaxed(val, mc->reg_ch_base + M10V_XDDES);
149 /* mc->vc.lock must be held by caller */
150 static void milbeaut_xdmac_start(struct milbeaut_xdmac_chan *mc)
152 struct milbeaut_xdmac_desc *md;
154 md = milbeaut_xdmac_next_desc(mc);
155 if (md)
156 milbeaut_chan_start(mc, md);
159 static irqreturn_t milbeaut_xdmac_interrupt(int irq, void *dev_id)
161 struct milbeaut_xdmac_chan *mc = dev_id;
162 struct milbeaut_xdmac_desc *md;
163 unsigned long flags;
164 u32 val;
166 spin_lock_irqsave(&mc->vc.lock, flags);
168 /* Ack and Stop */
169 val = FIELD_PREP(M10V_XDDSD_IS_MASK, 0x0);
170 writel_relaxed(val, mc->reg_ch_base + M10V_XDDSD);
172 md = mc->md;
173 if (!md)
174 goto out;
176 vchan_cookie_complete(&md->vd);
178 milbeaut_xdmac_start(mc);
179 out:
180 spin_unlock_irqrestore(&mc->vc.lock, flags);
181 return IRQ_HANDLED;
184 static void milbeaut_xdmac_free_chan_resources(struct dma_chan *chan)
186 vchan_free_chan_resources(to_virt_chan(chan));
189 static struct dma_async_tx_descriptor *
190 milbeaut_xdmac_prep_memcpy(struct dma_chan *chan, dma_addr_t dst,
191 dma_addr_t src, size_t len, unsigned long flags)
193 struct virt_dma_chan *vc = to_virt_chan(chan);
194 struct milbeaut_xdmac_desc *md;
196 md = kzalloc(sizeof(*md), GFP_NOWAIT);
197 if (!md)
198 return NULL;
200 md->len = len;
201 md->src = src;
202 md->dst = dst;
204 return vchan_tx_prep(vc, &md->vd, flags);
207 static int milbeaut_xdmac_terminate_all(struct dma_chan *chan)
209 struct virt_dma_chan *vc = to_virt_chan(chan);
210 struct milbeaut_xdmac_chan *mc = to_milbeaut_xdmac_chan(vc);
211 unsigned long flags;
212 u32 val;
214 LIST_HEAD(head);
216 spin_lock_irqsave(&vc->lock, flags);
218 /* Halt the channel */
219 val = readl(mc->reg_ch_base + M10V_XDDES);
220 val &= ~M10V_XDDES_CE;
221 val |= FIELD_PREP(M10V_XDDES_CE, 0);
222 writel(val, mc->reg_ch_base + M10V_XDDES);
224 if (mc->md) {
225 vchan_terminate_vdesc(&mc->md->vd);
226 mc->md = NULL;
229 vchan_get_all_descriptors(vc, &head);
231 spin_unlock_irqrestore(&vc->lock, flags);
233 vchan_dma_desc_free_list(vc, &head);
235 return 0;
238 static void milbeaut_xdmac_synchronize(struct dma_chan *chan)
240 vchan_synchronize(to_virt_chan(chan));
243 static void milbeaut_xdmac_issue_pending(struct dma_chan *chan)
245 struct virt_dma_chan *vc = to_virt_chan(chan);
246 struct milbeaut_xdmac_chan *mc = to_milbeaut_xdmac_chan(vc);
247 unsigned long flags;
249 spin_lock_irqsave(&vc->lock, flags);
251 if (vchan_issue_pending(vc) && !mc->md)
252 milbeaut_xdmac_start(mc);
254 spin_unlock_irqrestore(&vc->lock, flags);
257 static void milbeaut_xdmac_desc_free(struct virt_dma_desc *vd)
259 kfree(to_milbeaut_xdmac_desc(vd));
262 static int milbeaut_xdmac_chan_init(struct platform_device *pdev,
263 struct milbeaut_xdmac_device *mdev,
264 int chan_id)
266 struct device *dev = &pdev->dev;
267 struct milbeaut_xdmac_chan *mc = &mdev->channels[chan_id];
268 char *irq_name;
269 int irq, ret;
271 irq = platform_get_irq(pdev, chan_id);
272 if (irq < 0)
273 return irq;
275 irq_name = devm_kasprintf(dev, GFP_KERNEL, "milbeaut-xdmac-%d",
276 chan_id);
277 if (!irq_name)
278 return -ENOMEM;
280 ret = devm_request_irq(dev, irq, milbeaut_xdmac_interrupt,
281 IRQF_SHARED, irq_name, mc);
282 if (ret)
283 return ret;
285 mc->reg_ch_base = mdev->reg_base + chan_id * 0x30;
287 mc->vc.desc_free = milbeaut_xdmac_desc_free;
288 vchan_init(&mc->vc, &mdev->ddev);
290 return 0;
293 static void enable_xdmac(struct milbeaut_xdmac_device *mdev)
295 unsigned int val;
297 val = readl(mdev->reg_base + M10V_XDACS);
298 val |= M10V_XDACS_XE;
299 writel(val, mdev->reg_base + M10V_XDACS);
302 static void disable_xdmac(struct milbeaut_xdmac_device *mdev)
304 unsigned int val;
306 val = readl(mdev->reg_base + M10V_XDACS);
307 val &= ~M10V_XDACS_XE;
308 writel(val, mdev->reg_base + M10V_XDACS);
311 static int milbeaut_xdmac_probe(struct platform_device *pdev)
313 struct device *dev = &pdev->dev;
314 struct milbeaut_xdmac_device *mdev;
315 struct dma_device *ddev;
316 int nr_chans, ret, i;
318 nr_chans = platform_irq_count(pdev);
319 if (nr_chans < 0)
320 return nr_chans;
322 mdev = devm_kzalloc(dev, struct_size(mdev, channels, nr_chans),
323 GFP_KERNEL);
324 if (!mdev)
325 return -ENOMEM;
327 mdev->reg_base = devm_platform_ioremap_resource(pdev, 0);
328 if (IS_ERR(mdev->reg_base))
329 return PTR_ERR(mdev->reg_base);
331 ddev = &mdev->ddev;
332 ddev->dev = dev;
333 dma_cap_set(DMA_MEMCPY, ddev->cap_mask);
334 ddev->src_addr_widths = MLB_XDMAC_BUSWIDTHS;
335 ddev->dst_addr_widths = MLB_XDMAC_BUSWIDTHS;
336 ddev->device_free_chan_resources = milbeaut_xdmac_free_chan_resources;
337 ddev->device_prep_dma_memcpy = milbeaut_xdmac_prep_memcpy;
338 ddev->device_terminate_all = milbeaut_xdmac_terminate_all;
339 ddev->device_synchronize = milbeaut_xdmac_synchronize;
340 ddev->device_tx_status = dma_cookie_status;
341 ddev->device_issue_pending = milbeaut_xdmac_issue_pending;
342 INIT_LIST_HEAD(&ddev->channels);
344 for (i = 0; i < nr_chans; i++) {
345 ret = milbeaut_xdmac_chan_init(pdev, mdev, i);
346 if (ret)
347 return ret;
350 enable_xdmac(mdev);
352 ret = dma_async_device_register(ddev);
353 if (ret)
354 return ret;
356 ret = of_dma_controller_register(dev->of_node,
357 of_dma_simple_xlate, mdev);
358 if (ret)
359 goto unregister_dmac;
361 platform_set_drvdata(pdev, mdev);
363 return 0;
365 unregister_dmac:
366 dma_async_device_unregister(ddev);
367 return ret;
370 static int milbeaut_xdmac_remove(struct platform_device *pdev)
372 struct milbeaut_xdmac_device *mdev = platform_get_drvdata(pdev);
373 struct dma_chan *chan;
374 int ret;
377 * Before reaching here, almost all descriptors have been freed by the
378 * ->device_free_chan_resources() hook. However, each channel might
379 * be still holding one descriptor that was on-flight at that moment.
380 * Terminate it to make sure this hardware is no longer running. Then,
381 * free the channel resources once again to avoid memory leak.
383 list_for_each_entry(chan, &mdev->ddev.channels, device_node) {
384 ret = dmaengine_terminate_sync(chan);
385 if (ret)
386 return ret;
387 milbeaut_xdmac_free_chan_resources(chan);
390 of_dma_controller_free(pdev->dev.of_node);
391 dma_async_device_unregister(&mdev->ddev);
393 disable_xdmac(mdev);
395 return 0;
398 static const struct of_device_id milbeaut_xdmac_match[] = {
399 { .compatible = "socionext,milbeaut-m10v-xdmac" },
400 { /* sentinel */ }
402 MODULE_DEVICE_TABLE(of, milbeaut_xdmac_match);
404 static struct platform_driver milbeaut_xdmac_driver = {
405 .probe = milbeaut_xdmac_probe,
406 .remove = milbeaut_xdmac_remove,
407 .driver = {
408 .name = "milbeaut-m10v-xdmac",
409 .of_match_table = milbeaut_xdmac_match,
412 module_platform_driver(milbeaut_xdmac_driver);
414 MODULE_DESCRIPTION("Milbeaut XDMAC DmaEngine driver");
415 MODULE_LICENSE("GPL v2");