dmaengine: dma-jz4780: Further residue status fix
[linux-2.6-microblaze.git] / drivers / dma / dma-jz4780.c
1 /*
2  * Ingenic JZ4780 DMA controller
3  *
4  * Copyright (c) 2015 Imagination Technologies
5  * Author: Alex Smith <alex@alex-smith.me.uk>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation;  either version 2 of the  License, or (at your
10  * option) any later version.
11  */
12
13 #include <linux/clk.h>
14 #include <linux/dmapool.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/of_device.h>
20 #include <linux/of_dma.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23
24 #include "dmaengine.h"
25 #include "virt-dma.h"
26
27 /* Global registers. */
28 #define JZ_DMA_REG_DMAC         0x00
29 #define JZ_DMA_REG_DIRQP        0x04
30 #define JZ_DMA_REG_DDR          0x08
31 #define JZ_DMA_REG_DDRS         0x0c
32 #define JZ_DMA_REG_DCKE         0x10
33 #define JZ_DMA_REG_DCKES        0x14
34 #define JZ_DMA_REG_DCKEC        0x18
35 #define JZ_DMA_REG_DMACP        0x1c
36 #define JZ_DMA_REG_DSIRQP       0x20
37 #define JZ_DMA_REG_DSIRQM       0x24
38 #define JZ_DMA_REG_DCIRQP       0x28
39 #define JZ_DMA_REG_DCIRQM       0x2c
40
41 /* Per-channel registers. */
42 #define JZ_DMA_REG_CHAN(n)      (n * 0x20)
43 #define JZ_DMA_REG_DSA          0x00
44 #define JZ_DMA_REG_DTA          0x04
45 #define JZ_DMA_REG_DTC          0x08
46 #define JZ_DMA_REG_DRT          0x0c
47 #define JZ_DMA_REG_DCS          0x10
48 #define JZ_DMA_REG_DCM          0x14
49 #define JZ_DMA_REG_DDA          0x18
50 #define JZ_DMA_REG_DSD          0x1c
51
52 #define JZ_DMA_DMAC_DMAE        BIT(0)
53 #define JZ_DMA_DMAC_AR          BIT(2)
54 #define JZ_DMA_DMAC_HLT         BIT(3)
55 #define JZ_DMA_DMAC_FAIC        BIT(27)
56 #define JZ_DMA_DMAC_FMSC        BIT(31)
57
58 #define JZ_DMA_DRT_AUTO         0x8
59
60 #define JZ_DMA_DCS_CTE          BIT(0)
61 #define JZ_DMA_DCS_HLT          BIT(2)
62 #define JZ_DMA_DCS_TT           BIT(3)
63 #define JZ_DMA_DCS_AR           BIT(4)
64 #define JZ_DMA_DCS_DES8         BIT(30)
65
66 #define JZ_DMA_DCM_LINK         BIT(0)
67 #define JZ_DMA_DCM_TIE          BIT(1)
68 #define JZ_DMA_DCM_STDE         BIT(2)
69 #define JZ_DMA_DCM_TSZ_SHIFT    8
70 #define JZ_DMA_DCM_TSZ_MASK     (0x7 << JZ_DMA_DCM_TSZ_SHIFT)
71 #define JZ_DMA_DCM_DP_SHIFT     12
72 #define JZ_DMA_DCM_SP_SHIFT     14
73 #define JZ_DMA_DCM_DAI          BIT(22)
74 #define JZ_DMA_DCM_SAI          BIT(23)
75
76 #define JZ_DMA_SIZE_4_BYTE      0x0
77 #define JZ_DMA_SIZE_1_BYTE      0x1
78 #define JZ_DMA_SIZE_2_BYTE      0x2
79 #define JZ_DMA_SIZE_16_BYTE     0x3
80 #define JZ_DMA_SIZE_32_BYTE     0x4
81 #define JZ_DMA_SIZE_64_BYTE     0x5
82 #define JZ_DMA_SIZE_128_BYTE    0x6
83
84 #define JZ_DMA_WIDTH_32_BIT     0x0
85 #define JZ_DMA_WIDTH_8_BIT      0x1
86 #define JZ_DMA_WIDTH_16_BIT     0x2
87
88 #define JZ_DMA_BUSWIDTHS        (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE)  | \
89                                  BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
90                                  BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
91
92 #define JZ4780_DMA_CTRL_OFFSET  0x1000
93
94 /* macros for use with jz4780_dma_soc_data.flags */
95 #define JZ_SOC_DATA_ALLOW_LEGACY_DT     BIT(0)
96 #define JZ_SOC_DATA_PROGRAMMABLE_DMA    BIT(1)
97 #define JZ_SOC_DATA_PER_CHAN_PM         BIT(2)
98 #define JZ_SOC_DATA_NO_DCKES_DCKEC      BIT(3)
99
100 /**
101  * struct jz4780_dma_hwdesc - descriptor structure read by the DMA controller.
102  * @dcm: value for the DCM (channel command) register
103  * @dsa: source address
104  * @dta: target address
105  * @dtc: transfer count (number of blocks of the transfer size specified in DCM
106  * to transfer) in the low 24 bits, offset of the next descriptor from the
107  * descriptor base address in the upper 8 bits.
108  */
109 struct jz4780_dma_hwdesc {
110         uint32_t dcm;
111         uint32_t dsa;
112         uint32_t dta;
113         uint32_t dtc;
114 };
115
116 /* Size of allocations for hardware descriptor blocks. */
117 #define JZ_DMA_DESC_BLOCK_SIZE  PAGE_SIZE
118 #define JZ_DMA_MAX_DESC         \
119         (JZ_DMA_DESC_BLOCK_SIZE / sizeof(struct jz4780_dma_hwdesc))
120
121 struct jz4780_dma_desc {
122         struct virt_dma_desc vdesc;
123
124         struct jz4780_dma_hwdesc *desc;
125         dma_addr_t desc_phys;
126         unsigned int count;
127         enum dma_transaction_type type;
128         uint32_t status;
129 };
130
131 struct jz4780_dma_chan {
132         struct virt_dma_chan vchan;
133         unsigned int id;
134         struct dma_pool *desc_pool;
135
136         uint32_t transfer_type;
137         uint32_t transfer_shift;
138         struct dma_slave_config config;
139
140         struct jz4780_dma_desc *desc;
141         unsigned int curr_hwdesc;
142 };
143
144 struct jz4780_dma_soc_data {
145         unsigned int nb_channels;
146         unsigned int transfer_ord_max;
147         unsigned long flags;
148 };
149
150 struct jz4780_dma_dev {
151         struct dma_device dma_device;
152         void __iomem *chn_base;
153         void __iomem *ctrl_base;
154         struct clk *clk;
155         unsigned int irq;
156         const struct jz4780_dma_soc_data *soc_data;
157
158         uint32_t chan_reserved;
159         struct jz4780_dma_chan chan[];
160 };
161
162 struct jz4780_dma_filter_data {
163         struct device_node *of_node;
164         uint32_t transfer_type;
165         int channel;
166 };
167
168 static inline struct jz4780_dma_chan *to_jz4780_dma_chan(struct dma_chan *chan)
169 {
170         return container_of(chan, struct jz4780_dma_chan, vchan.chan);
171 }
172
173 static inline struct jz4780_dma_desc *to_jz4780_dma_desc(
174         struct virt_dma_desc *vdesc)
175 {
176         return container_of(vdesc, struct jz4780_dma_desc, vdesc);
177 }
178
179 static inline struct jz4780_dma_dev *jz4780_dma_chan_parent(
180         struct jz4780_dma_chan *jzchan)
181 {
182         return container_of(jzchan->vchan.chan.device, struct jz4780_dma_dev,
183                             dma_device);
184 }
185
186 static inline uint32_t jz4780_dma_chn_readl(struct jz4780_dma_dev *jzdma,
187         unsigned int chn, unsigned int reg)
188 {
189         return readl(jzdma->chn_base + reg + JZ_DMA_REG_CHAN(chn));
190 }
191
192 static inline void jz4780_dma_chn_writel(struct jz4780_dma_dev *jzdma,
193         unsigned int chn, unsigned int reg, uint32_t val)
194 {
195         writel(val, jzdma->chn_base + reg + JZ_DMA_REG_CHAN(chn));
196 }
197
198 static inline uint32_t jz4780_dma_ctrl_readl(struct jz4780_dma_dev *jzdma,
199         unsigned int reg)
200 {
201         return readl(jzdma->ctrl_base + reg);
202 }
203
204 static inline void jz4780_dma_ctrl_writel(struct jz4780_dma_dev *jzdma,
205         unsigned int reg, uint32_t val)
206 {
207         writel(val, jzdma->ctrl_base + reg);
208 }
209
210 static inline void jz4780_dma_chan_enable(struct jz4780_dma_dev *jzdma,
211         unsigned int chn)
212 {
213         if (jzdma->soc_data->flags & JZ_SOC_DATA_PER_CHAN_PM) {
214                 unsigned int reg;
215
216                 if (jzdma->soc_data->flags & JZ_SOC_DATA_NO_DCKES_DCKEC)
217                         reg = JZ_DMA_REG_DCKE;
218                 else
219                         reg = JZ_DMA_REG_DCKES;
220
221                 jz4780_dma_ctrl_writel(jzdma, reg, BIT(chn));
222         }
223 }
224
225 static inline void jz4780_dma_chan_disable(struct jz4780_dma_dev *jzdma,
226         unsigned int chn)
227 {
228         if ((jzdma->soc_data->flags & JZ_SOC_DATA_PER_CHAN_PM) &&
229                         !(jzdma->soc_data->flags & JZ_SOC_DATA_NO_DCKES_DCKEC))
230                 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DCKEC, BIT(chn));
231 }
232
233 static struct jz4780_dma_desc *jz4780_dma_desc_alloc(
234         struct jz4780_dma_chan *jzchan, unsigned int count,
235         enum dma_transaction_type type)
236 {
237         struct jz4780_dma_desc *desc;
238
239         if (count > JZ_DMA_MAX_DESC)
240                 return NULL;
241
242         desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
243         if (!desc)
244                 return NULL;
245
246         desc->desc = dma_pool_alloc(jzchan->desc_pool, GFP_NOWAIT,
247                                     &desc->desc_phys);
248         if (!desc->desc) {
249                 kfree(desc);
250                 return NULL;
251         }
252
253         desc->count = count;
254         desc->type = type;
255         return desc;
256 }
257
258 static void jz4780_dma_desc_free(struct virt_dma_desc *vdesc)
259 {
260         struct jz4780_dma_desc *desc = to_jz4780_dma_desc(vdesc);
261         struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(vdesc->tx.chan);
262
263         dma_pool_free(jzchan->desc_pool, desc->desc, desc->desc_phys);
264         kfree(desc);
265 }
266
267 static uint32_t jz4780_dma_transfer_size(struct jz4780_dma_chan *jzchan,
268         unsigned long val, uint32_t *shift)
269 {
270         struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
271         int ord = ffs(val) - 1;
272
273         /*
274          * 8 byte transfer sizes unsupported so fall back on 4. If it's larger
275          * than the maximum, just limit it. It is perfectly safe to fall back
276          * in this way since we won't exceed the maximum burst size supported
277          * by the device, the only effect is reduced efficiency. This is better
278          * than refusing to perform the request at all.
279          */
280         if (ord == 3)
281                 ord = 2;
282         else if (ord > jzdma->soc_data->transfer_ord_max)
283                 ord = jzdma->soc_data->transfer_ord_max;
284
285         *shift = ord;
286
287         switch (ord) {
288         case 0:
289                 return JZ_DMA_SIZE_1_BYTE;
290         case 1:
291                 return JZ_DMA_SIZE_2_BYTE;
292         case 2:
293                 return JZ_DMA_SIZE_4_BYTE;
294         case 4:
295                 return JZ_DMA_SIZE_16_BYTE;
296         case 5:
297                 return JZ_DMA_SIZE_32_BYTE;
298         case 6:
299                 return JZ_DMA_SIZE_64_BYTE;
300         default:
301                 return JZ_DMA_SIZE_128_BYTE;
302         }
303 }
304
305 static int jz4780_dma_setup_hwdesc(struct jz4780_dma_chan *jzchan,
306         struct jz4780_dma_hwdesc *desc, dma_addr_t addr, size_t len,
307         enum dma_transfer_direction direction)
308 {
309         struct dma_slave_config *config = &jzchan->config;
310         uint32_t width, maxburst, tsz;
311
312         if (direction == DMA_MEM_TO_DEV) {
313                 desc->dcm = JZ_DMA_DCM_SAI;
314                 desc->dsa = addr;
315                 desc->dta = config->dst_addr;
316
317                 width = config->dst_addr_width;
318                 maxburst = config->dst_maxburst;
319         } else {
320                 desc->dcm = JZ_DMA_DCM_DAI;
321                 desc->dsa = config->src_addr;
322                 desc->dta = addr;
323
324                 width = config->src_addr_width;
325                 maxburst = config->src_maxburst;
326         }
327
328         /*
329          * This calculates the maximum transfer size that can be used with the
330          * given address, length, width and maximum burst size. The address
331          * must be aligned to the transfer size, the total length must be
332          * divisible by the transfer size, and we must not use more than the
333          * maximum burst specified by the user.
334          */
335         tsz = jz4780_dma_transfer_size(jzchan, addr | len | (width * maxburst),
336                                        &jzchan->transfer_shift);
337
338         switch (width) {
339         case DMA_SLAVE_BUSWIDTH_1_BYTE:
340         case DMA_SLAVE_BUSWIDTH_2_BYTES:
341                 break;
342         case DMA_SLAVE_BUSWIDTH_4_BYTES:
343                 width = JZ_DMA_WIDTH_32_BIT;
344                 break;
345         default:
346                 return -EINVAL;
347         }
348
349         desc->dcm |= tsz << JZ_DMA_DCM_TSZ_SHIFT;
350         desc->dcm |= width << JZ_DMA_DCM_SP_SHIFT;
351         desc->dcm |= width << JZ_DMA_DCM_DP_SHIFT;
352
353         desc->dtc = len >> jzchan->transfer_shift;
354         return 0;
355 }
356
357 static struct dma_async_tx_descriptor *jz4780_dma_prep_slave_sg(
358         struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
359         enum dma_transfer_direction direction, unsigned long flags,
360         void *context)
361 {
362         struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
363         struct jz4780_dma_desc *desc;
364         unsigned int i;
365         int err;
366
367         desc = jz4780_dma_desc_alloc(jzchan, sg_len, DMA_SLAVE);
368         if (!desc)
369                 return NULL;
370
371         for (i = 0; i < sg_len; i++) {
372                 err = jz4780_dma_setup_hwdesc(jzchan, &desc->desc[i],
373                                               sg_dma_address(&sgl[i]),
374                                               sg_dma_len(&sgl[i]),
375                                               direction);
376                 if (err < 0) {
377                         jz4780_dma_desc_free(&jzchan->desc->vdesc);
378                         return NULL;
379                 }
380
381                 desc->desc[i].dcm |= JZ_DMA_DCM_TIE;
382
383                 if (i != (sg_len - 1)) {
384                         /* Automatically proceeed to the next descriptor. */
385                         desc->desc[i].dcm |= JZ_DMA_DCM_LINK;
386
387                         /*
388                          * The upper 8 bits of the DTC field in the descriptor
389                          * must be set to (offset from descriptor base of next
390                          * descriptor >> 4).
391                          */
392                         desc->desc[i].dtc |=
393                                 (((i + 1) * sizeof(*desc->desc)) >> 4) << 24;
394                 }
395         }
396
397         return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
398 }
399
400 static struct dma_async_tx_descriptor *jz4780_dma_prep_dma_cyclic(
401         struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
402         size_t period_len, enum dma_transfer_direction direction,
403         unsigned long flags)
404 {
405         struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
406         struct jz4780_dma_desc *desc;
407         unsigned int periods, i;
408         int err;
409
410         if (buf_len % period_len)
411                 return NULL;
412
413         periods = buf_len / period_len;
414
415         desc = jz4780_dma_desc_alloc(jzchan, periods, DMA_CYCLIC);
416         if (!desc)
417                 return NULL;
418
419         for (i = 0; i < periods; i++) {
420                 err = jz4780_dma_setup_hwdesc(jzchan, &desc->desc[i], buf_addr,
421                                               period_len, direction);
422                 if (err < 0) {
423                         jz4780_dma_desc_free(&jzchan->desc->vdesc);
424                         return NULL;
425                 }
426
427                 buf_addr += period_len;
428
429                 /*
430                  * Set the link bit to indicate that the controller should
431                  * automatically proceed to the next descriptor. In
432                  * jz4780_dma_begin(), this will be cleared if we need to issue
433                  * an interrupt after each period.
434                  */
435                 desc->desc[i].dcm |= JZ_DMA_DCM_TIE | JZ_DMA_DCM_LINK;
436
437                 /*
438                  * The upper 8 bits of the DTC field in the descriptor must be
439                  * set to (offset from descriptor base of next descriptor >> 4).
440                  * If this is the last descriptor, link it back to the first,
441                  * i.e. leave offset set to 0, otherwise point to the next one.
442                  */
443                 if (i != (periods - 1)) {
444                         desc->desc[i].dtc |=
445                                 (((i + 1) * sizeof(*desc->desc)) >> 4) << 24;
446                 }
447         }
448
449         return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
450 }
451
452 static struct dma_async_tx_descriptor *jz4780_dma_prep_dma_memcpy(
453         struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
454         size_t len, unsigned long flags)
455 {
456         struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
457         struct jz4780_dma_desc *desc;
458         uint32_t tsz;
459
460         desc = jz4780_dma_desc_alloc(jzchan, 1, DMA_MEMCPY);
461         if (!desc)
462                 return NULL;
463
464         tsz = jz4780_dma_transfer_size(jzchan, dest | src | len,
465                                        &jzchan->transfer_shift);
466
467         jzchan->transfer_type = JZ_DMA_DRT_AUTO;
468
469         desc->desc[0].dsa = src;
470         desc->desc[0].dta = dest;
471         desc->desc[0].dcm = JZ_DMA_DCM_TIE | JZ_DMA_DCM_SAI | JZ_DMA_DCM_DAI |
472                             tsz << JZ_DMA_DCM_TSZ_SHIFT |
473                             JZ_DMA_WIDTH_32_BIT << JZ_DMA_DCM_SP_SHIFT |
474                             JZ_DMA_WIDTH_32_BIT << JZ_DMA_DCM_DP_SHIFT;
475         desc->desc[0].dtc = len >> jzchan->transfer_shift;
476
477         return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
478 }
479
480 static void jz4780_dma_begin(struct jz4780_dma_chan *jzchan)
481 {
482         struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
483         struct virt_dma_desc *vdesc;
484         unsigned int i;
485         dma_addr_t desc_phys;
486
487         if (!jzchan->desc) {
488                 vdesc = vchan_next_desc(&jzchan->vchan);
489                 if (!vdesc)
490                         return;
491
492                 list_del(&vdesc->node);
493
494                 jzchan->desc = to_jz4780_dma_desc(vdesc);
495                 jzchan->curr_hwdesc = 0;
496
497                 if (jzchan->desc->type == DMA_CYCLIC && vdesc->tx.callback) {
498                         /*
499                          * The DMA controller doesn't support triggering an
500                          * interrupt after processing each descriptor, only
501                          * after processing an entire terminated list of
502                          * descriptors. For a cyclic DMA setup the list of
503                          * descriptors is not terminated so we can never get an
504                          * interrupt.
505                          *
506                          * If the user requested a callback for a cyclic DMA
507                          * setup then we workaround this hardware limitation
508                          * here by degrading to a set of unlinked descriptors
509                          * which we will submit in sequence in response to the
510                          * completion of processing the previous descriptor.
511                          */
512                         for (i = 0; i < jzchan->desc->count; i++)
513                                 jzchan->desc->desc[i].dcm &= ~JZ_DMA_DCM_LINK;
514                 }
515         } else {
516                 /*
517                  * There is an existing transfer, therefore this must be one
518                  * for which we unlinked the descriptors above. Advance to the
519                  * next one in the list.
520                  */
521                 jzchan->curr_hwdesc =
522                         (jzchan->curr_hwdesc + 1) % jzchan->desc->count;
523         }
524
525         /* Enable the channel's clock. */
526         jz4780_dma_chan_enable(jzdma, jzchan->id);
527
528         /* Use 4-word descriptors. */
529         jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS, 0);
530
531         /* Set transfer type. */
532         jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DRT,
533                               jzchan->transfer_type);
534
535         /*
536          * Set the transfer count. This is redundant for a descriptor-driven
537          * transfer. However, there can be a delay between the transfer start
538          * time and when DTCn reg contains the new transfer count. Setting
539          * it explicitly ensures residue is computed correctly at all times.
540          */
541         jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DTC,
542                                 jzchan->desc->desc[jzchan->curr_hwdesc].dtc);
543
544         /* Write descriptor address and initiate descriptor fetch. */
545         desc_phys = jzchan->desc->desc_phys +
546                     (jzchan->curr_hwdesc * sizeof(*jzchan->desc->desc));
547         jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DDA, desc_phys);
548         jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DDRS, BIT(jzchan->id));
549
550         /* Enable the channel. */
551         jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS,
552                               JZ_DMA_DCS_CTE);
553 }
554
555 static void jz4780_dma_issue_pending(struct dma_chan *chan)
556 {
557         struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
558         unsigned long flags;
559
560         spin_lock_irqsave(&jzchan->vchan.lock, flags);
561
562         if (vchan_issue_pending(&jzchan->vchan) && !jzchan->desc)
563                 jz4780_dma_begin(jzchan);
564
565         spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
566 }
567
568 static int jz4780_dma_terminate_all(struct dma_chan *chan)
569 {
570         struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
571         struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
572         unsigned long flags;
573         LIST_HEAD(head);
574
575         spin_lock_irqsave(&jzchan->vchan.lock, flags);
576
577         /* Clear the DMA status and stop the transfer. */
578         jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS, 0);
579         if (jzchan->desc) {
580                 vchan_terminate_vdesc(&jzchan->desc->vdesc);
581                 jzchan->desc = NULL;
582         }
583
584         jz4780_dma_chan_disable(jzdma, jzchan->id);
585
586         vchan_get_all_descriptors(&jzchan->vchan, &head);
587
588         spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
589
590         vchan_dma_desc_free_list(&jzchan->vchan, &head);
591         return 0;
592 }
593
594 static void jz4780_dma_synchronize(struct dma_chan *chan)
595 {
596         struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
597         struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
598
599         vchan_synchronize(&jzchan->vchan);
600         jz4780_dma_chan_disable(jzdma, jzchan->id);
601 }
602
603 static int jz4780_dma_config(struct dma_chan *chan,
604         struct dma_slave_config *config)
605 {
606         struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
607
608         if ((config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
609            || (config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES))
610                 return -EINVAL;
611
612         /* Copy the reset of the slave configuration, it is used later. */
613         memcpy(&jzchan->config, config, sizeof(jzchan->config));
614
615         return 0;
616 }
617
618 static size_t jz4780_dma_desc_residue(struct jz4780_dma_chan *jzchan,
619         struct jz4780_dma_desc *desc, unsigned int next_sg)
620 {
621         struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
622         unsigned int count = 0;
623         unsigned int i;
624
625         for (i = next_sg; i < desc->count; i++)
626                 count += desc->desc[i].dtc & GENMASK(23, 0);
627
628         if (next_sg != 0)
629                 count += jz4780_dma_chn_readl(jzdma, jzchan->id,
630                                          JZ_DMA_REG_DTC);
631
632         return count << jzchan->transfer_shift;
633 }
634
635 static enum dma_status jz4780_dma_tx_status(struct dma_chan *chan,
636         dma_cookie_t cookie, struct dma_tx_state *txstate)
637 {
638         struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
639         struct virt_dma_desc *vdesc;
640         enum dma_status status;
641         unsigned long flags;
642
643         status = dma_cookie_status(chan, cookie, txstate);
644         if ((status == DMA_COMPLETE) || (txstate == NULL))
645                 return status;
646
647         spin_lock_irqsave(&jzchan->vchan.lock, flags);
648
649         vdesc = vchan_find_desc(&jzchan->vchan, cookie);
650         if (vdesc) {
651                 /* On the issued list, so hasn't been processed yet */
652                 txstate->residue = jz4780_dma_desc_residue(jzchan,
653                                         to_jz4780_dma_desc(vdesc), 0);
654         } else if (cookie == jzchan->desc->vdesc.tx.cookie) {
655                 txstate->residue = jz4780_dma_desc_residue(jzchan, jzchan->desc,
656                                         jzchan->curr_hwdesc + 1);
657         } else
658                 txstate->residue = 0;
659
660         if (vdesc && jzchan->desc && vdesc == &jzchan->desc->vdesc
661             && jzchan->desc->status & (JZ_DMA_DCS_AR | JZ_DMA_DCS_HLT))
662                 status = DMA_ERROR;
663
664         spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
665         return status;
666 }
667
668 static void jz4780_dma_chan_irq(struct jz4780_dma_dev *jzdma,
669         struct jz4780_dma_chan *jzchan)
670 {
671         uint32_t dcs;
672
673         spin_lock(&jzchan->vchan.lock);
674
675         dcs = jz4780_dma_chn_readl(jzdma, jzchan->id, JZ_DMA_REG_DCS);
676         jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS, 0);
677
678         if (dcs & JZ_DMA_DCS_AR) {
679                 dev_warn(&jzchan->vchan.chan.dev->device,
680                          "address error (DCS=0x%x)\n", dcs);
681         }
682
683         if (dcs & JZ_DMA_DCS_HLT) {
684                 dev_warn(&jzchan->vchan.chan.dev->device,
685                          "channel halt (DCS=0x%x)\n", dcs);
686         }
687
688         if (jzchan->desc) {
689                 jzchan->desc->status = dcs;
690
691                 if ((dcs & (JZ_DMA_DCS_AR | JZ_DMA_DCS_HLT)) == 0) {
692                         if (jzchan->desc->type == DMA_CYCLIC) {
693                                 vchan_cyclic_callback(&jzchan->desc->vdesc);
694                         } else {
695                                 vchan_cookie_complete(&jzchan->desc->vdesc);
696                                 jzchan->desc = NULL;
697                         }
698
699                         jz4780_dma_begin(jzchan);
700                 }
701         } else {
702                 dev_err(&jzchan->vchan.chan.dev->device,
703                         "channel IRQ with no active transfer\n");
704         }
705
706         spin_unlock(&jzchan->vchan.lock);
707 }
708
709 static irqreturn_t jz4780_dma_irq_handler(int irq, void *data)
710 {
711         struct jz4780_dma_dev *jzdma = data;
712         uint32_t pending, dmac;
713         int i;
714
715         pending = jz4780_dma_ctrl_readl(jzdma, JZ_DMA_REG_DIRQP);
716
717         for (i = 0; i < jzdma->soc_data->nb_channels; i++) {
718                 if (!(pending & (1<<i)))
719                         continue;
720
721                 jz4780_dma_chan_irq(jzdma, &jzdma->chan[i]);
722         }
723
724         /* Clear halt and address error status of all channels. */
725         dmac = jz4780_dma_ctrl_readl(jzdma, JZ_DMA_REG_DMAC);
726         dmac &= ~(JZ_DMA_DMAC_HLT | JZ_DMA_DMAC_AR);
727         jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DMAC, dmac);
728
729         /* Clear interrupt pending status. */
730         jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DIRQP, 0);
731
732         return IRQ_HANDLED;
733 }
734
735 static int jz4780_dma_alloc_chan_resources(struct dma_chan *chan)
736 {
737         struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
738
739         jzchan->desc_pool = dma_pool_create(dev_name(&chan->dev->device),
740                                             chan->device->dev,
741                                             JZ_DMA_DESC_BLOCK_SIZE,
742                                             PAGE_SIZE, 0);
743         if (!jzchan->desc_pool) {
744                 dev_err(&chan->dev->device,
745                         "failed to allocate descriptor pool\n");
746                 return -ENOMEM;
747         }
748
749         return 0;
750 }
751
752 static void jz4780_dma_free_chan_resources(struct dma_chan *chan)
753 {
754         struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
755
756         vchan_free_chan_resources(&jzchan->vchan);
757         dma_pool_destroy(jzchan->desc_pool);
758         jzchan->desc_pool = NULL;
759 }
760
761 static bool jz4780_dma_filter_fn(struct dma_chan *chan, void *param)
762 {
763         struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
764         struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
765         struct jz4780_dma_filter_data *data = param;
766
767         if (jzdma->dma_device.dev->of_node != data->of_node)
768                 return false;
769
770         if (data->channel > -1) {
771                 if (data->channel != jzchan->id)
772                         return false;
773         } else if (jzdma->chan_reserved & BIT(jzchan->id)) {
774                 return false;
775         }
776
777         jzchan->transfer_type = data->transfer_type;
778
779         return true;
780 }
781
782 static struct dma_chan *jz4780_of_dma_xlate(struct of_phandle_args *dma_spec,
783         struct of_dma *ofdma)
784 {
785         struct jz4780_dma_dev *jzdma = ofdma->of_dma_data;
786         dma_cap_mask_t mask = jzdma->dma_device.cap_mask;
787         struct jz4780_dma_filter_data data;
788
789         if (dma_spec->args_count != 2)
790                 return NULL;
791
792         data.of_node = ofdma->of_node;
793         data.transfer_type = dma_spec->args[0];
794         data.channel = dma_spec->args[1];
795
796         if (data.channel > -1) {
797                 if (data.channel >= jzdma->soc_data->nb_channels) {
798                         dev_err(jzdma->dma_device.dev,
799                                 "device requested non-existent channel %u\n",
800                                 data.channel);
801                         return NULL;
802                 }
803
804                 /* Can only select a channel marked as reserved. */
805                 if (!(jzdma->chan_reserved & BIT(data.channel))) {
806                         dev_err(jzdma->dma_device.dev,
807                                 "device requested unreserved channel %u\n",
808                                 data.channel);
809                         return NULL;
810                 }
811
812                 jzdma->chan[data.channel].transfer_type = data.transfer_type;
813
814                 return dma_get_slave_channel(
815                         &jzdma->chan[data.channel].vchan.chan);
816         } else {
817                 return dma_request_channel(mask, jz4780_dma_filter_fn, &data);
818         }
819 }
820
821 static int jz4780_dma_probe(struct platform_device *pdev)
822 {
823         struct device *dev = &pdev->dev;
824         const struct jz4780_dma_soc_data *soc_data;
825         struct jz4780_dma_dev *jzdma;
826         struct jz4780_dma_chan *jzchan;
827         struct dma_device *dd;
828         struct resource *res;
829         int i, ret;
830
831         if (!dev->of_node) {
832                 dev_err(dev, "This driver must be probed from devicetree\n");
833                 return -EINVAL;
834         }
835
836         soc_data = device_get_match_data(dev);
837         if (!soc_data)
838                 return -EINVAL;
839
840         jzdma = devm_kzalloc(dev, sizeof(*jzdma)
841                                 + sizeof(*jzdma->chan) * soc_data->nb_channels,
842                                 GFP_KERNEL);
843         if (!jzdma)
844                 return -ENOMEM;
845
846         jzdma->soc_data = soc_data;
847         platform_set_drvdata(pdev, jzdma);
848
849         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
850         if (!res) {
851                 dev_err(dev, "failed to get I/O memory\n");
852                 return -EINVAL;
853         }
854
855         jzdma->chn_base = devm_ioremap_resource(dev, res);
856         if (IS_ERR(jzdma->chn_base))
857                 return PTR_ERR(jzdma->chn_base);
858
859         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
860         if (res) {
861                 jzdma->ctrl_base = devm_ioremap_resource(dev, res);
862                 if (IS_ERR(jzdma->ctrl_base))
863                         return PTR_ERR(jzdma->ctrl_base);
864         } else if (soc_data->flags & JZ_SOC_DATA_ALLOW_LEGACY_DT) {
865                 /*
866                  * On JZ4780, if the second memory resource was not supplied,
867                  * assume we're using an old devicetree, and calculate the
868                  * offset to the control registers.
869                  */
870                 jzdma->ctrl_base = jzdma->chn_base + JZ4780_DMA_CTRL_OFFSET;
871         } else {
872                 dev_err(dev, "failed to get I/O memory\n");
873                 return -EINVAL;
874         }
875
876         ret = platform_get_irq(pdev, 0);
877         if (ret < 0) {
878                 dev_err(dev, "failed to get IRQ: %d\n", ret);
879                 return ret;
880         }
881
882         jzdma->irq = ret;
883
884         ret = request_irq(jzdma->irq, jz4780_dma_irq_handler, 0, dev_name(dev),
885                           jzdma);
886         if (ret) {
887                 dev_err(dev, "failed to request IRQ %u!\n", jzdma->irq);
888                 return ret;
889         }
890
891         jzdma->clk = devm_clk_get(dev, NULL);
892         if (IS_ERR(jzdma->clk)) {
893                 dev_err(dev, "failed to get clock\n");
894                 ret = PTR_ERR(jzdma->clk);
895                 goto err_free_irq;
896         }
897
898         clk_prepare_enable(jzdma->clk);
899
900         /* Property is optional, if it doesn't exist the value will remain 0. */
901         of_property_read_u32_index(dev->of_node, "ingenic,reserved-channels",
902                                    0, &jzdma->chan_reserved);
903
904         dd = &jzdma->dma_device;
905
906         dma_cap_set(DMA_MEMCPY, dd->cap_mask);
907         dma_cap_set(DMA_SLAVE, dd->cap_mask);
908         dma_cap_set(DMA_CYCLIC, dd->cap_mask);
909
910         dd->dev = dev;
911         dd->copy_align = DMAENGINE_ALIGN_4_BYTES;
912         dd->device_alloc_chan_resources = jz4780_dma_alloc_chan_resources;
913         dd->device_free_chan_resources = jz4780_dma_free_chan_resources;
914         dd->device_prep_slave_sg = jz4780_dma_prep_slave_sg;
915         dd->device_prep_dma_cyclic = jz4780_dma_prep_dma_cyclic;
916         dd->device_prep_dma_memcpy = jz4780_dma_prep_dma_memcpy;
917         dd->device_config = jz4780_dma_config;
918         dd->device_terminate_all = jz4780_dma_terminate_all;
919         dd->device_synchronize = jz4780_dma_synchronize;
920         dd->device_tx_status = jz4780_dma_tx_status;
921         dd->device_issue_pending = jz4780_dma_issue_pending;
922         dd->src_addr_widths = JZ_DMA_BUSWIDTHS;
923         dd->dst_addr_widths = JZ_DMA_BUSWIDTHS;
924         dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
925         dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
926
927         /*
928          * Enable DMA controller, mark all channels as not programmable.
929          * Also set the FMSC bit - it increases MSC performance, so it makes
930          * little sense not to enable it.
931          */
932         jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DMAC, JZ_DMA_DMAC_DMAE |
933                                JZ_DMA_DMAC_FAIC | JZ_DMA_DMAC_FMSC);
934
935         if (soc_data->flags & JZ_SOC_DATA_PROGRAMMABLE_DMA)
936                 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DMACP, 0);
937
938         INIT_LIST_HEAD(&dd->channels);
939
940         for (i = 0; i < soc_data->nb_channels; i++) {
941                 jzchan = &jzdma->chan[i];
942                 jzchan->id = i;
943
944                 vchan_init(&jzchan->vchan, dd);
945                 jzchan->vchan.desc_free = jz4780_dma_desc_free;
946         }
947
948         ret = dma_async_device_register(dd);
949         if (ret) {
950                 dev_err(dev, "failed to register device\n");
951                 goto err_disable_clk;
952         }
953
954         /* Register with OF DMA helpers. */
955         ret = of_dma_controller_register(dev->of_node, jz4780_of_dma_xlate,
956                                          jzdma);
957         if (ret) {
958                 dev_err(dev, "failed to register OF DMA controller\n");
959                 goto err_unregister_dev;
960         }
961
962         dev_info(dev, "JZ4780 DMA controller initialised\n");
963         return 0;
964
965 err_unregister_dev:
966         dma_async_device_unregister(dd);
967
968 err_disable_clk:
969         clk_disable_unprepare(jzdma->clk);
970
971 err_free_irq:
972         free_irq(jzdma->irq, jzdma);
973         return ret;
974 }
975
976 static int jz4780_dma_remove(struct platform_device *pdev)
977 {
978         struct jz4780_dma_dev *jzdma = platform_get_drvdata(pdev);
979         int i;
980
981         of_dma_controller_free(pdev->dev.of_node);
982
983         free_irq(jzdma->irq, jzdma);
984
985         for (i = 0; i < jzdma->soc_data->nb_channels; i++)
986                 tasklet_kill(&jzdma->chan[i].vchan.task);
987
988         dma_async_device_unregister(&jzdma->dma_device);
989         return 0;
990 }
991
992 static const struct jz4780_dma_soc_data jz4740_dma_soc_data = {
993         .nb_channels = 6,
994         .transfer_ord_max = 5,
995 };
996
997 static const struct jz4780_dma_soc_data jz4725b_dma_soc_data = {
998         .nb_channels = 6,
999         .transfer_ord_max = 5,
1000         .flags = JZ_SOC_DATA_PER_CHAN_PM | JZ_SOC_DATA_NO_DCKES_DCKEC,
1001 };
1002
1003 static const struct jz4780_dma_soc_data jz4770_dma_soc_data = {
1004         .nb_channels = 6,
1005         .transfer_ord_max = 6,
1006         .flags = JZ_SOC_DATA_PER_CHAN_PM,
1007 };
1008
1009 static const struct jz4780_dma_soc_data jz4780_dma_soc_data = {
1010         .nb_channels = 32,
1011         .transfer_ord_max = 7,
1012         .flags = JZ_SOC_DATA_ALLOW_LEGACY_DT | JZ_SOC_DATA_PROGRAMMABLE_DMA,
1013 };
1014
1015 static const struct of_device_id jz4780_dma_dt_match[] = {
1016         { .compatible = "ingenic,jz4740-dma", .data = &jz4740_dma_soc_data },
1017         { .compatible = "ingenic,jz4725b-dma", .data = &jz4725b_dma_soc_data },
1018         { .compatible = "ingenic,jz4770-dma", .data = &jz4770_dma_soc_data },
1019         { .compatible = "ingenic,jz4780-dma", .data = &jz4780_dma_soc_data },
1020         {},
1021 };
1022 MODULE_DEVICE_TABLE(of, jz4780_dma_dt_match);
1023
1024 static struct platform_driver jz4780_dma_driver = {
1025         .probe          = jz4780_dma_probe,
1026         .remove         = jz4780_dma_remove,
1027         .driver = {
1028                 .name   = "jz4780-dma",
1029                 .of_match_table = of_match_ptr(jz4780_dma_dt_match),
1030         },
1031 };
1032
1033 static int __init jz4780_dma_init(void)
1034 {
1035         return platform_driver_register(&jz4780_dma_driver);
1036 }
1037 subsys_initcall(jz4780_dma_init);
1038
1039 static void __exit jz4780_dma_exit(void)
1040 {
1041         platform_driver_unregister(&jz4780_dma_driver);
1042 }
1043 module_exit(jz4780_dma_exit);
1044
1045 MODULE_AUTHOR("Alex Smith <alex@alex-smith.me.uk>");
1046 MODULE_DESCRIPTION("Ingenic JZ4780 DMA controller driver");
1047 MODULE_LICENSE("GPL");