1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Renesas RZ/G2L DMA Controller Driver
4 *
5 * Based on imx-dma.c
6 *
7 * Copyright (C) 2021 Renesas Electronics Corp.
8 * Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
9 * Copyright 2012 Javier Martin, Vista Silicon <javier.martin@vista-silicon.com>
10 */
11
12 #include <linux/bitfield.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/dmaengine.h>
15 #include <linux/interrupt.h>
16 #include <linux/iopoll.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/of_dma.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26
27 #include "../dmaengine.h"
28 #include "../virt-dma.h"
29
30 enum rz_dmac_prep_type {
31 RZ_DMAC_DESC_MEMCPY,
32 RZ_DMAC_DESC_SLAVE_SG,
33 };
34
35 struct rz_lmdesc {
36 u32 header;
37 u32 sa;
38 u32 da;
39 u32 tb;
40 u32 chcfg;
41 u32 chitvl;
42 u32 chext;
43 u32 nxla;
44 };
45
46 struct rz_dmac_desc {
47 struct virt_dma_desc vd;
48 dma_addr_t src;
49 dma_addr_t dest;
50 size_t len;
51 struct list_head node;
52 enum dma_transfer_direction direction;
53 enum rz_dmac_prep_type type;
54 /* For slave sg */
55 struct scatterlist *sg;
56 unsigned int sgcount;
57 };
58
59 #define to_rz_dmac_desc(d) container_of(d, struct rz_dmac_desc, vd)
60
61 struct rz_dmac_chan {
62 struct virt_dma_chan vc;
63 void __iomem *ch_base;
64 void __iomem *ch_cmn_base;
65 unsigned int index;
66 int irq;
67 struct rz_dmac_desc *desc;
68 int descs_allocated;
69
70 enum dma_slave_buswidth src_word_size;
71 enum dma_slave_buswidth dst_word_size;
72 dma_addr_t src_per_address;
73 dma_addr_t dst_per_address;
74
75 u32 chcfg;
76 u32 chctrl;
77 int mid_rid;
78
79 struct list_head ld_free;
80 struct list_head ld_queue;
81 struct list_head ld_active;
82
83 struct {
84 struct rz_lmdesc *base;
85 struct rz_lmdesc *head;
86 struct rz_lmdesc *tail;
87 dma_addr_t base_dma;
88 } lmdesc;
89 };
90
91 #define to_rz_dmac_chan(c) container_of(c, struct rz_dmac_chan, vc.chan)
92
93 struct rz_dmac {
94 struct dma_device engine;
95 struct device *dev;
96 void __iomem *base;
97 void __iomem *ext_base;
98
99 unsigned int n_channels;
100 struct rz_dmac_chan *channels;
101
102 DECLARE_BITMAP(modules, 1024);
103 };
104
105 #define to_rz_dmac(d) container_of(d, struct rz_dmac, engine)
106
107 /*
108 * -----------------------------------------------------------------------------
109 * Registers
110 */
111
112 #define CHSTAT 0x0024
113 #define CHCTRL 0x0028
114 #define CHCFG 0x002c
115 #define NXLA 0x0038
116
117 #define DCTRL 0x0000
118
119 #define EACH_CHANNEL_OFFSET 0x0040
120 #define CHANNEL_0_7_OFFSET 0x0000
121 #define CHANNEL_0_7_COMMON_BASE 0x0300
122 #define CHANNEL_8_15_OFFSET 0x0400
123 #define CHANNEL_8_15_COMMON_BASE 0x0700
124
125 #define CHSTAT_ER BIT(4)
126 #define CHSTAT_EN BIT(0)
127
128 #define CHCTRL_CLRINTMSK BIT(17)
129 #define CHCTRL_CLRSUS BIT(9)
130 #define CHCTRL_CLRTC BIT(6)
131 #define CHCTRL_CLREND BIT(5)
132 #define CHCTRL_CLRRQ BIT(4)
133 #define CHCTRL_SWRST BIT(3)
134 #define CHCTRL_STG BIT(2)
135 #define CHCTRL_CLREN BIT(1)
136 #define CHCTRL_SETEN BIT(0)
137 #define CHCTRL_DEFAULT (CHCTRL_CLRINTMSK | CHCTRL_CLRSUS | \
138 CHCTRL_CLRTC | CHCTRL_CLREND | \
139 CHCTRL_CLRRQ | CHCTRL_SWRST | \
140 CHCTRL_CLREN)
141
142 #define CHCFG_DMS BIT(31)
143 #define CHCFG_DEM BIT(24)
144 #define CHCFG_DAD BIT(21)
145 #define CHCFG_SAD BIT(20)
146 #define CHCFG_REQD BIT(3)
147 #define CHCFG_SEL(bits) ((bits) & 0x07)
148 #define CHCFG_MEM_COPY (0x80400008)
149 #define CHCFG_FILL_DDS_MASK GENMASK(19, 16)
150 #define CHCFG_FILL_SDS_MASK GENMASK(15, 12)
151 #define CHCFG_FILL_TM(a) (((a) & BIT(5)) << 22)
152 #define CHCFG_FILL_AM(a) (((a) & GENMASK(4, 2)) << 6)
153 #define CHCFG_FILL_LVL(a) (((a) & BIT(1)) << 5)
154 #define CHCFG_FILL_HIEN(a) (((a) & BIT(0)) << 5)
155
156 #define MID_RID_MASK GENMASK(9, 0)
157 #define CHCFG_MASK GENMASK(15, 10)
158 #define CHCFG_DS_INVALID 0xFF
159 #define DCTRL_LVINT BIT(1)
160 #define DCTRL_PR BIT(0)
161 #define DCTRL_DEFAULT (DCTRL_LVINT | DCTRL_PR)
162
163 /* LINK MODE DESCRIPTOR */
164 #define HEADER_LV BIT(0)
165
166 #define RZ_DMAC_MAX_CHAN_DESCRIPTORS 16
167 #define RZ_DMAC_MAX_CHANNELS 16
168 #define DMAC_NR_LMDESC 64
169
170 /*
171 * -----------------------------------------------------------------------------
172 * Device access
173 */
174
rz_dmac_writel(struct rz_dmac * dmac,unsigned int val,unsigned int offset)175 static void rz_dmac_writel(struct rz_dmac *dmac, unsigned int val,
176 unsigned int offset)
177 {
178 writel(val, dmac->base + offset);
179 }
180
rz_dmac_ext_writel(struct rz_dmac * dmac,unsigned int val,unsigned int offset)181 static void rz_dmac_ext_writel(struct rz_dmac *dmac, unsigned int val,
182 unsigned int offset)
183 {
184 writel(val, dmac->ext_base + offset);
185 }
186
rz_dmac_ext_readl(struct rz_dmac * dmac,unsigned int offset)187 static u32 rz_dmac_ext_readl(struct rz_dmac *dmac, unsigned int offset)
188 {
189 return readl(dmac->ext_base + offset);
190 }
191
rz_dmac_ch_writel(struct rz_dmac_chan * channel,unsigned int val,unsigned int offset,int which)192 static void rz_dmac_ch_writel(struct rz_dmac_chan *channel, unsigned int val,
193 unsigned int offset, int which)
194 {
195 if (which)
196 writel(val, channel->ch_base + offset);
197 else
198 writel(val, channel->ch_cmn_base + offset);
199 }
200
rz_dmac_ch_readl(struct rz_dmac_chan * channel,unsigned int offset,int which)201 static u32 rz_dmac_ch_readl(struct rz_dmac_chan *channel,
202 unsigned int offset, int which)
203 {
204 if (which)
205 return readl(channel->ch_base + offset);
206 else
207 return readl(channel->ch_cmn_base + offset);
208 }
209
210 /*
211 * -----------------------------------------------------------------------------
212 * Initialization
213 */
214
rz_lmdesc_setup(struct rz_dmac_chan * channel,struct rz_lmdesc * lmdesc)215 static void rz_lmdesc_setup(struct rz_dmac_chan *channel,
216 struct rz_lmdesc *lmdesc)
217 {
218 u32 nxla;
219
220 channel->lmdesc.base = lmdesc;
221 channel->lmdesc.head = lmdesc;
222 channel->lmdesc.tail = lmdesc;
223 nxla = channel->lmdesc.base_dma;
224 while (lmdesc < (channel->lmdesc.base + (DMAC_NR_LMDESC - 1))) {
225 lmdesc->header = 0;
226 nxla += sizeof(*lmdesc);
227 lmdesc->nxla = nxla;
228 lmdesc++;
229 }
230
231 lmdesc->header = 0;
232 lmdesc->nxla = channel->lmdesc.base_dma;
233 }
234
235 /*
236 * -----------------------------------------------------------------------------
237 * Descriptors preparation
238 */
239
rz_dmac_lmdesc_recycle(struct rz_dmac_chan * channel)240 static void rz_dmac_lmdesc_recycle(struct rz_dmac_chan *channel)
241 {
242 struct rz_lmdesc *lmdesc = channel->lmdesc.head;
243
244 while (!(lmdesc->header & HEADER_LV)) {
245 lmdesc->header = 0;
246 lmdesc++;
247 if (lmdesc >= (channel->lmdesc.base + DMAC_NR_LMDESC))
248 lmdesc = channel->lmdesc.base;
249 }
250 channel->lmdesc.head = lmdesc;
251 }
252
rz_dmac_enable_hw(struct rz_dmac_chan * channel)253 static void rz_dmac_enable_hw(struct rz_dmac_chan *channel)
254 {
255 struct dma_chan *chan = &channel->vc.chan;
256 struct rz_dmac *dmac = to_rz_dmac(chan->device);
257 unsigned long flags;
258 u32 nxla;
259 u32 chctrl;
260 u32 chstat;
261
262 dev_dbg(dmac->dev, "%s channel %d\n", __func__, channel->index);
263
264 local_irq_save(flags);
265
266 rz_dmac_lmdesc_recycle(channel);
267
268 nxla = channel->lmdesc.base_dma +
269 (sizeof(struct rz_lmdesc) * (channel->lmdesc.head -
270 channel->lmdesc.base));
271
272 chstat = rz_dmac_ch_readl(channel, CHSTAT, 1);
273 if (!(chstat & CHSTAT_EN)) {
274 chctrl = (channel->chctrl | CHCTRL_SETEN);
275 rz_dmac_ch_writel(channel, nxla, NXLA, 1);
276 rz_dmac_ch_writel(channel, channel->chcfg, CHCFG, 1);
277 rz_dmac_ch_writel(channel, CHCTRL_SWRST, CHCTRL, 1);
278 rz_dmac_ch_writel(channel, chctrl, CHCTRL, 1);
279 }
280
281 local_irq_restore(flags);
282 }
283
rz_dmac_disable_hw(struct rz_dmac_chan * channel)284 static void rz_dmac_disable_hw(struct rz_dmac_chan *channel)
285 {
286 struct dma_chan *chan = &channel->vc.chan;
287 struct rz_dmac *dmac = to_rz_dmac(chan->device);
288 unsigned long flags;
289
290 dev_dbg(dmac->dev, "%s channel %d\n", __func__, channel->index);
291
292 local_irq_save(flags);
293 rz_dmac_ch_writel(channel, CHCTRL_DEFAULT, CHCTRL, 1);
294 local_irq_restore(flags);
295 }
296
rz_dmac_set_dmars_register(struct rz_dmac * dmac,int nr,u32 dmars)297 static void rz_dmac_set_dmars_register(struct rz_dmac *dmac, int nr, u32 dmars)
298 {
299 u32 dmars_offset = (nr / 2) * 4;
300 u32 shift = (nr % 2) * 16;
301 u32 dmars32;
302
303 dmars32 = rz_dmac_ext_readl(dmac, dmars_offset);
304 dmars32 &= ~(0xffff << shift);
305 dmars32 |= dmars << shift;
306
307 rz_dmac_ext_writel(dmac, dmars32, dmars_offset);
308 }
309
rz_dmac_prepare_desc_for_memcpy(struct rz_dmac_chan * channel)310 static void rz_dmac_prepare_desc_for_memcpy(struct rz_dmac_chan *channel)
311 {
312 struct dma_chan *chan = &channel->vc.chan;
313 struct rz_dmac *dmac = to_rz_dmac(chan->device);
314 struct rz_lmdesc *lmdesc = channel->lmdesc.tail;
315 struct rz_dmac_desc *d = channel->desc;
316 u32 chcfg = CHCFG_MEM_COPY;
317
318 /* prepare descriptor */
319 lmdesc->sa = d->src;
320 lmdesc->da = d->dest;
321 lmdesc->tb = d->len;
322 lmdesc->chcfg = chcfg;
323 lmdesc->chitvl = 0;
324 lmdesc->chext = 0;
325 lmdesc->header = HEADER_LV;
326
327 rz_dmac_set_dmars_register(dmac, channel->index, 0);
328
329 channel->chcfg = chcfg;
330 channel->chctrl = CHCTRL_STG | CHCTRL_SETEN;
331 }
332
rz_dmac_prepare_descs_for_slave_sg(struct rz_dmac_chan * channel)333 static void rz_dmac_prepare_descs_for_slave_sg(struct rz_dmac_chan *channel)
334 {
335 struct dma_chan *chan = &channel->vc.chan;
336 struct rz_dmac *dmac = to_rz_dmac(chan->device);
337 struct rz_dmac_desc *d = channel->desc;
338 struct scatterlist *sg, *sgl = d->sg;
339 struct rz_lmdesc *lmdesc;
340 unsigned int i, sg_len = d->sgcount;
341
342 channel->chcfg |= CHCFG_SEL(channel->index) | CHCFG_DEM | CHCFG_DMS;
343
344 if (d->direction == DMA_DEV_TO_MEM) {
345 channel->chcfg |= CHCFG_SAD;
346 channel->chcfg &= ~CHCFG_REQD;
347 } else {
348 channel->chcfg |= CHCFG_DAD | CHCFG_REQD;
349 }
350
351 lmdesc = channel->lmdesc.tail;
352
353 for (i = 0, sg = sgl; i < sg_len; i++, sg = sg_next(sg)) {
354 if (d->direction == DMA_DEV_TO_MEM) {
355 lmdesc->sa = channel->src_per_address;
356 lmdesc->da = sg_dma_address(sg);
357 } else {
358 lmdesc->sa = sg_dma_address(sg);
359 lmdesc->da = channel->dst_per_address;
360 }
361
362 lmdesc->tb = sg_dma_len(sg);
363 lmdesc->chitvl = 0;
364 lmdesc->chext = 0;
365 if (i == (sg_len - 1)) {
366 lmdesc->chcfg = (channel->chcfg & ~CHCFG_DEM);
367 lmdesc->header = HEADER_LV;
368 } else {
369 lmdesc->chcfg = channel->chcfg;
370 lmdesc->header = HEADER_LV;
371 }
372 if (++lmdesc >= (channel->lmdesc.base + DMAC_NR_LMDESC))
373 lmdesc = channel->lmdesc.base;
374 }
375
376 channel->lmdesc.tail = lmdesc;
377
378 rz_dmac_set_dmars_register(dmac, channel->index, channel->mid_rid);
379 channel->chctrl = CHCTRL_SETEN;
380 }
381
rz_dmac_xfer_desc(struct rz_dmac_chan * chan)382 static int rz_dmac_xfer_desc(struct rz_dmac_chan *chan)
383 {
384 struct rz_dmac_desc *d = chan->desc;
385 struct virt_dma_desc *vd;
386
387 vd = vchan_next_desc(&chan->vc);
388 if (!vd)
389 return 0;
390
391 list_del(&vd->node);
392
393 switch (d->type) {
394 case RZ_DMAC_DESC_MEMCPY:
395 rz_dmac_prepare_desc_for_memcpy(chan);
396 break;
397
398 case RZ_DMAC_DESC_SLAVE_SG:
399 rz_dmac_prepare_descs_for_slave_sg(chan);
400 break;
401
402 default:
403 return -EINVAL;
404 }
405
406 rz_dmac_enable_hw(chan);
407
408 return 0;
409 }
410
411 /*
412 * -----------------------------------------------------------------------------
413 * DMA engine operations
414 */
415
rz_dmac_alloc_chan_resources(struct dma_chan * chan)416 static int rz_dmac_alloc_chan_resources(struct dma_chan *chan)
417 {
418 struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
419
420 while (channel->descs_allocated < RZ_DMAC_MAX_CHAN_DESCRIPTORS) {
421 struct rz_dmac_desc *desc;
422
423 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
424 if (!desc)
425 break;
426
427 list_add_tail(&desc->node, &channel->ld_free);
428 channel->descs_allocated++;
429 }
430
431 if (!channel->descs_allocated)
432 return -ENOMEM;
433
434 return channel->descs_allocated;
435 }
436
rz_dmac_free_chan_resources(struct dma_chan * chan)437 static void rz_dmac_free_chan_resources(struct dma_chan *chan)
438 {
439 struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
440 struct rz_dmac *dmac = to_rz_dmac(chan->device);
441 struct rz_lmdesc *lmdesc = channel->lmdesc.base;
442 struct rz_dmac_desc *desc, *_desc;
443 unsigned long flags;
444 unsigned int i;
445
446 spin_lock_irqsave(&channel->vc.lock, flags);
447
448 for (i = 0; i < DMAC_NR_LMDESC; i++)
449 lmdesc[i].header = 0;
450
451 rz_dmac_disable_hw(channel);
452 list_splice_tail_init(&channel->ld_active, &channel->ld_free);
453 list_splice_tail_init(&channel->ld_queue, &channel->ld_free);
454
455 if (channel->mid_rid >= 0) {
456 clear_bit(channel->mid_rid, dmac->modules);
457 channel->mid_rid = -EINVAL;
458 }
459
460 spin_unlock_irqrestore(&channel->vc.lock, flags);
461
462 list_for_each_entry_safe(desc, _desc, &channel->ld_free, node) {
463 kfree(desc);
464 channel->descs_allocated--;
465 }
466
467 INIT_LIST_HEAD(&channel->ld_free);
468 vchan_free_chan_resources(&channel->vc);
469 }
470
471 static struct dma_async_tx_descriptor *
rz_dmac_prep_dma_memcpy(struct dma_chan * chan,dma_addr_t dest,dma_addr_t src,size_t len,unsigned long flags)472 rz_dmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
473 size_t len, unsigned long flags)
474 {
475 struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
476 struct rz_dmac *dmac = to_rz_dmac(chan->device);
477 struct rz_dmac_desc *desc;
478
479 dev_dbg(dmac->dev, "%s channel: %d src=0x%pad dst=0x%pad len=%zu\n",
480 __func__, channel->index, &src, &dest, len);
481
482 if (list_empty(&channel->ld_free))
483 return NULL;
484
485 desc = list_first_entry(&channel->ld_free, struct rz_dmac_desc, node);
486
487 desc->type = RZ_DMAC_DESC_MEMCPY;
488 desc->src = src;
489 desc->dest = dest;
490 desc->len = len;
491 desc->direction = DMA_MEM_TO_MEM;
492
493 list_move_tail(channel->ld_free.next, &channel->ld_queue);
494 return vchan_tx_prep(&channel->vc, &desc->vd, flags);
495 }
496
497 static struct dma_async_tx_descriptor *
rz_dmac_prep_slave_sg(struct dma_chan * chan,struct scatterlist * sgl,unsigned int sg_len,enum dma_transfer_direction direction,unsigned long flags,void * context)498 rz_dmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
499 unsigned int sg_len,
500 enum dma_transfer_direction direction,
501 unsigned long flags, void *context)
502 {
503 struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
504 struct rz_dmac_desc *desc;
505 struct scatterlist *sg;
506 int dma_length = 0;
507 int i = 0;
508
509 if (list_empty(&channel->ld_free))
510 return NULL;
511
512 desc = list_first_entry(&channel->ld_free, struct rz_dmac_desc, node);
513
514 for_each_sg(sgl, sg, sg_len, i) {
515 dma_length += sg_dma_len(sg);
516 }
517
518 desc->type = RZ_DMAC_DESC_SLAVE_SG;
519 desc->sg = sgl;
520 desc->sgcount = sg_len;
521 desc->len = dma_length;
522 desc->direction = direction;
523
524 if (direction == DMA_DEV_TO_MEM)
525 desc->src = channel->src_per_address;
526 else
527 desc->dest = channel->dst_per_address;
528
529 list_move_tail(channel->ld_free.next, &channel->ld_queue);
530 return vchan_tx_prep(&channel->vc, &desc->vd, flags);
531 }
532
rz_dmac_terminate_all(struct dma_chan * chan)533 static int rz_dmac_terminate_all(struct dma_chan *chan)
534 {
535 struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
536 unsigned long flags;
537 LIST_HEAD(head);
538
539 rz_dmac_disable_hw(channel);
540 spin_lock_irqsave(&channel->vc.lock, flags);
541 list_splice_tail_init(&channel->ld_active, &channel->ld_free);
542 list_splice_tail_init(&channel->ld_queue, &channel->ld_free);
543 spin_unlock_irqrestore(&channel->vc.lock, flags);
544 vchan_get_all_descriptors(&channel->vc, &head);
545 vchan_dma_desc_free_list(&channel->vc, &head);
546
547 return 0;
548 }
549
rz_dmac_issue_pending(struct dma_chan * chan)550 static void rz_dmac_issue_pending(struct dma_chan *chan)
551 {
552 struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
553 struct rz_dmac *dmac = to_rz_dmac(chan->device);
554 struct rz_dmac_desc *desc;
555 unsigned long flags;
556
557 spin_lock_irqsave(&channel->vc.lock, flags);
558
559 if (!list_empty(&channel->ld_queue)) {
560 desc = list_first_entry(&channel->ld_queue,
561 struct rz_dmac_desc, node);
562 channel->desc = desc;
563 if (vchan_issue_pending(&channel->vc)) {
564 if (rz_dmac_xfer_desc(channel) < 0)
565 dev_warn(dmac->dev, "ch: %d couldn't issue DMA xfer\n",
566 channel->index);
567 else
568 list_move_tail(channel->ld_queue.next,
569 &channel->ld_active);
570 }
571 }
572
573 spin_unlock_irqrestore(&channel->vc.lock, flags);
574 }
575
rz_dmac_ds_to_val_mapping(enum dma_slave_buswidth ds)576 static u8 rz_dmac_ds_to_val_mapping(enum dma_slave_buswidth ds)
577 {
578 u8 i;
579 static const enum dma_slave_buswidth ds_lut[] = {
580 DMA_SLAVE_BUSWIDTH_1_BYTE,
581 DMA_SLAVE_BUSWIDTH_2_BYTES,
582 DMA_SLAVE_BUSWIDTH_4_BYTES,
583 DMA_SLAVE_BUSWIDTH_8_BYTES,
584 DMA_SLAVE_BUSWIDTH_16_BYTES,
585 DMA_SLAVE_BUSWIDTH_32_BYTES,
586 DMA_SLAVE_BUSWIDTH_64_BYTES,
587 DMA_SLAVE_BUSWIDTH_128_BYTES,
588 };
589
590 for (i = 0; i < ARRAY_SIZE(ds_lut); i++) {
591 if (ds_lut[i] == ds)
592 return i;
593 }
594
595 return CHCFG_DS_INVALID;
596 }
597
rz_dmac_config(struct dma_chan * chan,struct dma_slave_config * config)598 static int rz_dmac_config(struct dma_chan *chan,
599 struct dma_slave_config *config)
600 {
601 struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
602 u32 val;
603
604 channel->src_per_address = config->src_addr;
605 channel->src_word_size = config->src_addr_width;
606 channel->dst_per_address = config->dst_addr;
607 channel->dst_word_size = config->dst_addr_width;
608
609 val = rz_dmac_ds_to_val_mapping(config->dst_addr_width);
610 if (val == CHCFG_DS_INVALID)
611 return -EINVAL;
612
613 channel->chcfg &= ~CHCFG_FILL_DDS_MASK;
614 channel->chcfg |= FIELD_PREP(CHCFG_FILL_DDS_MASK, val);
615
616 val = rz_dmac_ds_to_val_mapping(config->src_addr_width);
617 if (val == CHCFG_DS_INVALID)
618 return -EINVAL;
619
620 channel->chcfg &= ~CHCFG_FILL_SDS_MASK;
621 channel->chcfg |= FIELD_PREP(CHCFG_FILL_SDS_MASK, val);
622
623 return 0;
624 }
625
rz_dmac_virt_desc_free(struct virt_dma_desc * vd)626 static void rz_dmac_virt_desc_free(struct virt_dma_desc *vd)
627 {
628 /*
629 * Place holder
630 * Descriptor allocation is done during alloc_chan_resources and
631 * get freed during free_chan_resources.
632 * list is used to manage the descriptors and avoid any memory
633 * allocation/free during DMA read/write.
634 */
635 }
636
rz_dmac_device_synchronize(struct dma_chan * chan)637 static void rz_dmac_device_synchronize(struct dma_chan *chan)
638 {
639 struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
640 struct rz_dmac *dmac = to_rz_dmac(chan->device);
641 u32 chstat;
642 int ret;
643
644 ret = read_poll_timeout(rz_dmac_ch_readl, chstat, !(chstat & CHSTAT_EN),
645 100, 100000, false, channel, CHSTAT, 1);
646 if (ret < 0)
647 dev_warn(dmac->dev, "DMA Timeout");
648
649 rz_dmac_set_dmars_register(dmac, channel->index, 0);
650 }
651
652 /*
653 * -----------------------------------------------------------------------------
654 * IRQ handling
655 */
656
rz_dmac_irq_handle_channel(struct rz_dmac_chan * channel)657 static void rz_dmac_irq_handle_channel(struct rz_dmac_chan *channel)
658 {
659 struct dma_chan *chan = &channel->vc.chan;
660 struct rz_dmac *dmac = to_rz_dmac(chan->device);
661 u32 chstat, chctrl;
662
663 chstat = rz_dmac_ch_readl(channel, CHSTAT, 1);
664 if (chstat & CHSTAT_ER) {
665 dev_err(dmac->dev, "DMAC err CHSTAT_%d = %08X\n",
666 channel->index, chstat);
667 rz_dmac_ch_writel(channel, CHCTRL_DEFAULT, CHCTRL, 1);
668 goto done;
669 }
670
671 chctrl = rz_dmac_ch_readl(channel, CHCTRL, 1);
672 rz_dmac_ch_writel(channel, chctrl | CHCTRL_CLREND, CHCTRL, 1);
673 done:
674 return;
675 }
676
rz_dmac_irq_handler(int irq,void * dev_id)677 static irqreturn_t rz_dmac_irq_handler(int irq, void *dev_id)
678 {
679 struct rz_dmac_chan *channel = dev_id;
680
681 if (channel) {
682 rz_dmac_irq_handle_channel(channel);
683 return IRQ_WAKE_THREAD;
684 }
685 /* handle DMAERR irq */
686 return IRQ_HANDLED;
687 }
688
rz_dmac_irq_handler_thread(int irq,void * dev_id)689 static irqreturn_t rz_dmac_irq_handler_thread(int irq, void *dev_id)
690 {
691 struct rz_dmac_chan *channel = dev_id;
692 struct rz_dmac_desc *desc = NULL;
693 unsigned long flags;
694
695 spin_lock_irqsave(&channel->vc.lock, flags);
696
697 if (list_empty(&channel->ld_active)) {
698 /* Someone might have called terminate all */
699 goto out;
700 }
701
702 desc = list_first_entry(&channel->ld_active, struct rz_dmac_desc, node);
703 vchan_cookie_complete(&desc->vd);
704 list_move_tail(channel->ld_active.next, &channel->ld_free);
705 if (!list_empty(&channel->ld_queue)) {
706 desc = list_first_entry(&channel->ld_queue, struct rz_dmac_desc,
707 node);
708 channel->desc = desc;
709 if (rz_dmac_xfer_desc(channel) == 0)
710 list_move_tail(channel->ld_queue.next, &channel->ld_active);
711 }
712 out:
713 spin_unlock_irqrestore(&channel->vc.lock, flags);
714
715 return IRQ_HANDLED;
716 }
717
718 /*
719 * -----------------------------------------------------------------------------
720 * OF xlate and channel filter
721 */
722
rz_dmac_chan_filter(struct dma_chan * chan,void * arg)723 static bool rz_dmac_chan_filter(struct dma_chan *chan, void *arg)
724 {
725 struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
726 struct rz_dmac *dmac = to_rz_dmac(chan->device);
727 struct of_phandle_args *dma_spec = arg;
728 u32 ch_cfg;
729
730 channel->mid_rid = dma_spec->args[0] & MID_RID_MASK;
731 ch_cfg = (dma_spec->args[0] & CHCFG_MASK) >> 10;
732 channel->chcfg = CHCFG_FILL_TM(ch_cfg) | CHCFG_FILL_AM(ch_cfg) |
733 CHCFG_FILL_LVL(ch_cfg) | CHCFG_FILL_HIEN(ch_cfg);
734
735 return !test_and_set_bit(channel->mid_rid, dmac->modules);
736 }
737
rz_dmac_of_xlate(struct of_phandle_args * dma_spec,struct of_dma * ofdma)738 static struct dma_chan *rz_dmac_of_xlate(struct of_phandle_args *dma_spec,
739 struct of_dma *ofdma)
740 {
741 dma_cap_mask_t mask;
742
743 if (dma_spec->args_count != 1)
744 return NULL;
745
746 /* Only slave DMA channels can be allocated via DT */
747 dma_cap_zero(mask);
748 dma_cap_set(DMA_SLAVE, mask);
749
750 return dma_request_channel(mask, rz_dmac_chan_filter, dma_spec);
751 }
752
753 /*
754 * -----------------------------------------------------------------------------
755 * Probe and remove
756 */
757
rz_dmac_chan_probe(struct rz_dmac * dmac,struct rz_dmac_chan * channel,unsigned int index)758 static int rz_dmac_chan_probe(struct rz_dmac *dmac,
759 struct rz_dmac_chan *channel,
760 unsigned int index)
761 {
762 struct platform_device *pdev = to_platform_device(dmac->dev);
763 struct rz_lmdesc *lmdesc;
764 char pdev_irqname[5];
765 char *irqname;
766 int ret;
767
768 channel->index = index;
769 channel->mid_rid = -EINVAL;
770
771 /* Request the channel interrupt. */
772 sprintf(pdev_irqname, "ch%u", index);
773 channel->irq = platform_get_irq_byname(pdev, pdev_irqname);
774 if (channel->irq < 0)
775 return channel->irq;
776
777 irqname = devm_kasprintf(dmac->dev, GFP_KERNEL, "%s:%u",
778 dev_name(dmac->dev), index);
779 if (!irqname)
780 return -ENOMEM;
781
782 ret = devm_request_threaded_irq(dmac->dev, channel->irq,
783 rz_dmac_irq_handler,
784 rz_dmac_irq_handler_thread, 0,
785 irqname, channel);
786 if (ret) {
787 dev_err(dmac->dev, "failed to request IRQ %u (%d)\n",
788 channel->irq, ret);
789 return ret;
790 }
791
792 /* Set io base address for each channel */
793 if (index < 8) {
794 channel->ch_base = dmac->base + CHANNEL_0_7_OFFSET +
795 EACH_CHANNEL_OFFSET * index;
796 channel->ch_cmn_base = dmac->base + CHANNEL_0_7_COMMON_BASE;
797 } else {
798 channel->ch_base = dmac->base + CHANNEL_8_15_OFFSET +
799 EACH_CHANNEL_OFFSET * (index - 8);
800 channel->ch_cmn_base = dmac->base + CHANNEL_8_15_COMMON_BASE;
801 }
802
803 /* Allocate descriptors */
804 lmdesc = dma_alloc_coherent(&pdev->dev,
805 sizeof(struct rz_lmdesc) * DMAC_NR_LMDESC,
806 &channel->lmdesc.base_dma, GFP_KERNEL);
807 if (!lmdesc) {
808 dev_err(&pdev->dev, "Can't allocate memory (lmdesc)\n");
809 return -ENOMEM;
810 }
811 rz_lmdesc_setup(channel, lmdesc);
812
813 /* Initialize register for each channel */
814 rz_dmac_ch_writel(channel, CHCTRL_DEFAULT, CHCTRL, 1);
815
816 channel->vc.desc_free = rz_dmac_virt_desc_free;
817 vchan_init(&channel->vc, &dmac->engine);
818 INIT_LIST_HEAD(&channel->ld_queue);
819 INIT_LIST_HEAD(&channel->ld_free);
820 INIT_LIST_HEAD(&channel->ld_active);
821
822 return 0;
823 }
824
rz_dmac_parse_of(struct device * dev,struct rz_dmac * dmac)825 static int rz_dmac_parse_of(struct device *dev, struct rz_dmac *dmac)
826 {
827 struct device_node *np = dev->of_node;
828 int ret;
829
830 ret = of_property_read_u32(np, "dma-channels", &dmac->n_channels);
831 if (ret < 0) {
832 dev_err(dev, "unable to read dma-channels property\n");
833 return ret;
834 }
835
836 if (!dmac->n_channels || dmac->n_channels > RZ_DMAC_MAX_CHANNELS) {
837 dev_err(dev, "invalid number of channels %u\n", dmac->n_channels);
838 return -EINVAL;
839 }
840
841 return 0;
842 }
843
rz_dmac_probe(struct platform_device * pdev)844 static int rz_dmac_probe(struct platform_device *pdev)
845 {
846 const char *irqname = "error";
847 struct dma_device *engine;
848 struct rz_dmac *dmac;
849 int channel_num;
850 unsigned int i;
851 int ret;
852 int irq;
853
854 dmac = devm_kzalloc(&pdev->dev, sizeof(*dmac), GFP_KERNEL);
855 if (!dmac)
856 return -ENOMEM;
857
858 dmac->dev = &pdev->dev;
859 platform_set_drvdata(pdev, dmac);
860
861 ret = rz_dmac_parse_of(&pdev->dev, dmac);
862 if (ret < 0)
863 return ret;
864
865 dmac->channels = devm_kcalloc(&pdev->dev, dmac->n_channels,
866 sizeof(*dmac->channels), GFP_KERNEL);
867 if (!dmac->channels)
868 return -ENOMEM;
869
870 /* Request resources */
871 dmac->base = devm_platform_ioremap_resource(pdev, 0);
872 if (IS_ERR(dmac->base))
873 return PTR_ERR(dmac->base);
874
875 dmac->ext_base = devm_platform_ioremap_resource(pdev, 1);
876 if (IS_ERR(dmac->ext_base))
877 return PTR_ERR(dmac->ext_base);
878
879 /* Register interrupt handler for error */
880 irq = platform_get_irq_byname(pdev, irqname);
881 if (irq < 0)
882 return irq;
883
884 ret = devm_request_irq(&pdev->dev, irq, rz_dmac_irq_handler, 0,
885 irqname, NULL);
886 if (ret) {
887 dev_err(&pdev->dev, "failed to request IRQ %u (%d)\n",
888 irq, ret);
889 return ret;
890 }
891
892 /* Initialize the channels. */
893 INIT_LIST_HEAD(&dmac->engine.channels);
894
895 pm_runtime_enable(&pdev->dev);
896 ret = pm_runtime_resume_and_get(&pdev->dev);
897 if (ret < 0) {
898 dev_err(&pdev->dev, "pm_runtime_resume_and_get failed\n");
899 goto err_pm_disable;
900 }
901
902 for (i = 0; i < dmac->n_channels; i++) {
903 ret = rz_dmac_chan_probe(dmac, &dmac->channels[i], i);
904 if (ret < 0)
905 goto err;
906 }
907
908 /* Register the DMAC as a DMA provider for DT. */
909 ret = of_dma_controller_register(pdev->dev.of_node, rz_dmac_of_xlate,
910 NULL);
911 if (ret < 0)
912 goto err;
913
914 /* Register the DMA engine device. */
915 engine = &dmac->engine;
916 dma_cap_set(DMA_SLAVE, engine->cap_mask);
917 dma_cap_set(DMA_MEMCPY, engine->cap_mask);
918 rz_dmac_writel(dmac, DCTRL_DEFAULT, CHANNEL_0_7_COMMON_BASE + DCTRL);
919 rz_dmac_writel(dmac, DCTRL_DEFAULT, CHANNEL_8_15_COMMON_BASE + DCTRL);
920
921 engine->dev = &pdev->dev;
922
923 engine->device_alloc_chan_resources = rz_dmac_alloc_chan_resources;
924 engine->device_free_chan_resources = rz_dmac_free_chan_resources;
925 engine->device_tx_status = dma_cookie_status;
926 engine->device_prep_slave_sg = rz_dmac_prep_slave_sg;
927 engine->device_prep_dma_memcpy = rz_dmac_prep_dma_memcpy;
928 engine->device_config = rz_dmac_config;
929 engine->device_terminate_all = rz_dmac_terminate_all;
930 engine->device_issue_pending = rz_dmac_issue_pending;
931 engine->device_synchronize = rz_dmac_device_synchronize;
932
933 engine->copy_align = DMAENGINE_ALIGN_1_BYTE;
934 dma_set_max_seg_size(engine->dev, U32_MAX);
935
936 ret = dma_async_device_register(engine);
937 if (ret < 0) {
938 dev_err(&pdev->dev, "unable to register\n");
939 goto dma_register_err;
940 }
941 return 0;
942
943 dma_register_err:
944 of_dma_controller_free(pdev->dev.of_node);
945 err:
946 channel_num = i ? i - 1 : 0;
947 for (i = 0; i < channel_num; i++) {
948 struct rz_dmac_chan *channel = &dmac->channels[i];
949
950 dma_free_coherent(&pdev->dev,
951 sizeof(struct rz_lmdesc) * DMAC_NR_LMDESC,
952 channel->lmdesc.base,
953 channel->lmdesc.base_dma);
954 }
955
956 pm_runtime_put(&pdev->dev);
957 err_pm_disable:
958 pm_runtime_disable(&pdev->dev);
959
960 return ret;
961 }
962
rz_dmac_remove(struct platform_device * pdev)963 static int rz_dmac_remove(struct platform_device *pdev)
964 {
965 struct rz_dmac *dmac = platform_get_drvdata(pdev);
966 unsigned int i;
967
968 for (i = 0; i < dmac->n_channels; i++) {
969 struct rz_dmac_chan *channel = &dmac->channels[i];
970
971 dma_free_coherent(&pdev->dev,
972 sizeof(struct rz_lmdesc) * DMAC_NR_LMDESC,
973 channel->lmdesc.base,
974 channel->lmdesc.base_dma);
975 }
976 of_dma_controller_free(pdev->dev.of_node);
977 dma_async_device_unregister(&dmac->engine);
978 pm_runtime_put(&pdev->dev);
979 pm_runtime_disable(&pdev->dev);
980
981 return 0;
982 }
983
984 static const struct of_device_id of_rz_dmac_match[] = {
985 { .compatible = "renesas,rz-dmac", },
986 { /* Sentinel */ }
987 };
988 MODULE_DEVICE_TABLE(of, of_rz_dmac_match);
989
990 static struct platform_driver rz_dmac_driver = {
991 .driver = {
992 .name = "rz-dmac",
993 .of_match_table = of_rz_dmac_match,
994 },
995 .probe = rz_dmac_probe,
996 .remove = rz_dmac_remove,
997 };
998
999 module_platform_driver(rz_dmac_driver);
1000
1001 MODULE_DESCRIPTION("Renesas RZ/G2L DMA Controller Driver");
1002 MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");
1003 MODULE_LICENSE("GPL v2");
1004