Loading...
1/*
2 * Copyright 2011 Freescale Semiconductor, Inc. All Rights Reserved.
3 *
4 * Refer to drivers/dma/imx-sdma.c
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/init.h>
12#include <linux/types.h>
13#include <linux/mm.h>
14#include <linux/interrupt.h>
15#include <linux/clk.h>
16#include <linux/wait.h>
17#include <linux/sched.h>
18#include <linux/semaphore.h>
19#include <linux/device.h>
20#include <linux/dma-mapping.h>
21#include <linux/slab.h>
22#include <linux/platform_device.h>
23#include <linux/dmaengine.h>
24#include <linux/delay.h>
25
26#include <asm/irq.h>
27#include <mach/mxs.h>
28#include <mach/dma.h>
29#include <mach/common.h>
30
31/*
32 * NOTE: The term "PIO" throughout the mxs-dma implementation means
33 * PIO mode of mxs apbh-dma and apbx-dma. With this working mode,
34 * dma can program the controller registers of peripheral devices.
35 */
36
37#define MXS_DMA_APBH 0
38#define MXS_DMA_APBX 1
39#define dma_is_apbh() (mxs_dma->dev_id == MXS_DMA_APBH)
40
41#define APBH_VERSION_LATEST 3
42#define apbh_is_old() (mxs_dma->version < APBH_VERSION_LATEST)
43
44#define HW_APBHX_CTRL0 0x000
45#define BM_APBH_CTRL0_APB_BURST8_EN (1 << 29)
46#define BM_APBH_CTRL0_APB_BURST_EN (1 << 28)
47#define BP_APBH_CTRL0_CLKGATE_CHANNEL 8
48#define BP_APBH_CTRL0_RESET_CHANNEL 16
49#define HW_APBHX_CTRL1 0x010
50#define HW_APBHX_CTRL2 0x020
51#define HW_APBHX_CHANNEL_CTRL 0x030
52#define BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL 16
53#define HW_APBH_VERSION (cpu_is_mx23() ? 0x3f0 : 0x800)
54#define HW_APBX_VERSION 0x800
55#define BP_APBHX_VERSION_MAJOR 24
56#define HW_APBHX_CHn_NXTCMDAR(n) \
57 (((dma_is_apbh() && apbh_is_old()) ? 0x050 : 0x110) + (n) * 0x70)
58#define HW_APBHX_CHn_SEMA(n) \
59 (((dma_is_apbh() && apbh_is_old()) ? 0x080 : 0x140) + (n) * 0x70)
60
61/*
62 * ccw bits definitions
63 *
64 * COMMAND: 0..1 (2)
65 * CHAIN: 2 (1)
66 * IRQ: 3 (1)
67 * NAND_LOCK: 4 (1) - not implemented
68 * NAND_WAIT4READY: 5 (1) - not implemented
69 * DEC_SEM: 6 (1)
70 * WAIT4END: 7 (1)
71 * HALT_ON_TERMINATE: 8 (1)
72 * TERMINATE_FLUSH: 9 (1)
73 * RESERVED: 10..11 (2)
74 * PIO_NUM: 12..15 (4)
75 */
76#define BP_CCW_COMMAND 0
77#define BM_CCW_COMMAND (3 << 0)
78#define CCW_CHAIN (1 << 2)
79#define CCW_IRQ (1 << 3)
80#define CCW_DEC_SEM (1 << 6)
81#define CCW_WAIT4END (1 << 7)
82#define CCW_HALT_ON_TERM (1 << 8)
83#define CCW_TERM_FLUSH (1 << 9)
84#define BP_CCW_PIO_NUM 12
85#define BM_CCW_PIO_NUM (0xf << 12)
86
87#define BF_CCW(value, field) (((value) << BP_CCW_##field) & BM_CCW_##field)
88
89#define MXS_DMA_CMD_NO_XFER 0
90#define MXS_DMA_CMD_WRITE 1
91#define MXS_DMA_CMD_READ 2
92#define MXS_DMA_CMD_DMA_SENSE 3 /* not implemented */
93
94struct mxs_dma_ccw {
95 u32 next;
96 u16 bits;
97 u16 xfer_bytes;
98#define MAX_XFER_BYTES 0xff00
99 u32 bufaddr;
100#define MXS_PIO_WORDS 16
101 u32 pio_words[MXS_PIO_WORDS];
102};
103
104#define NUM_CCW (int)(PAGE_SIZE / sizeof(struct mxs_dma_ccw))
105
106struct mxs_dma_chan {
107 struct mxs_dma_engine *mxs_dma;
108 struct dma_chan chan;
109 struct dma_async_tx_descriptor desc;
110 struct tasklet_struct tasklet;
111 int chan_irq;
112 struct mxs_dma_ccw *ccw;
113 dma_addr_t ccw_phys;
114 dma_cookie_t last_completed;
115 enum dma_status status;
116 unsigned int flags;
117#define MXS_DMA_SG_LOOP (1 << 0)
118};
119
120#define MXS_DMA_CHANNELS 16
121#define MXS_DMA_CHANNELS_MASK 0xffff
122
123struct mxs_dma_engine {
124 int dev_id;
125 unsigned int version;
126 void __iomem *base;
127 struct clk *clk;
128 struct dma_device dma_device;
129 struct device_dma_parameters dma_parms;
130 struct mxs_dma_chan mxs_chans[MXS_DMA_CHANNELS];
131};
132
133static void mxs_dma_reset_chan(struct mxs_dma_chan *mxs_chan)
134{
135 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
136 int chan_id = mxs_chan->chan.chan_id;
137
138 if (dma_is_apbh() && apbh_is_old())
139 writel(1 << (chan_id + BP_APBH_CTRL0_RESET_CHANNEL),
140 mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
141 else
142 writel(1 << (chan_id + BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL),
143 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + MXS_SET_ADDR);
144}
145
146static void mxs_dma_enable_chan(struct mxs_dma_chan *mxs_chan)
147{
148 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
149 int chan_id = mxs_chan->chan.chan_id;
150
151 /* set cmd_addr up */
152 writel(mxs_chan->ccw_phys,
153 mxs_dma->base + HW_APBHX_CHn_NXTCMDAR(chan_id));
154
155 /* enable apbh channel clock */
156 if (dma_is_apbh()) {
157 if (apbh_is_old())
158 writel(1 << (chan_id + BP_APBH_CTRL0_CLKGATE_CHANNEL),
159 mxs_dma->base + HW_APBHX_CTRL0 + MXS_CLR_ADDR);
160 else
161 writel(1 << chan_id,
162 mxs_dma->base + HW_APBHX_CTRL0 + MXS_CLR_ADDR);
163 }
164
165 /* write 1 to SEMA to kick off the channel */
166 writel(1, mxs_dma->base + HW_APBHX_CHn_SEMA(chan_id));
167}
168
169static void mxs_dma_disable_chan(struct mxs_dma_chan *mxs_chan)
170{
171 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
172 int chan_id = mxs_chan->chan.chan_id;
173
174 /* disable apbh channel clock */
175 if (dma_is_apbh()) {
176 if (apbh_is_old())
177 writel(1 << (chan_id + BP_APBH_CTRL0_CLKGATE_CHANNEL),
178 mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
179 else
180 writel(1 << chan_id,
181 mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
182 }
183
184 mxs_chan->status = DMA_SUCCESS;
185}
186
187static void mxs_dma_pause_chan(struct mxs_dma_chan *mxs_chan)
188{
189 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
190 int chan_id = mxs_chan->chan.chan_id;
191
192 /* freeze the channel */
193 if (dma_is_apbh() && apbh_is_old())
194 writel(1 << chan_id,
195 mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
196 else
197 writel(1 << chan_id,
198 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + MXS_SET_ADDR);
199
200 mxs_chan->status = DMA_PAUSED;
201}
202
203static void mxs_dma_resume_chan(struct mxs_dma_chan *mxs_chan)
204{
205 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
206 int chan_id = mxs_chan->chan.chan_id;
207
208 /* unfreeze the channel */
209 if (dma_is_apbh() && apbh_is_old())
210 writel(1 << chan_id,
211 mxs_dma->base + HW_APBHX_CTRL0 + MXS_CLR_ADDR);
212 else
213 writel(1 << chan_id,
214 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + MXS_CLR_ADDR);
215
216 mxs_chan->status = DMA_IN_PROGRESS;
217}
218
219static dma_cookie_t mxs_dma_assign_cookie(struct mxs_dma_chan *mxs_chan)
220{
221 dma_cookie_t cookie = mxs_chan->chan.cookie;
222
223 if (++cookie < 0)
224 cookie = 1;
225
226 mxs_chan->chan.cookie = cookie;
227 mxs_chan->desc.cookie = cookie;
228
229 return cookie;
230}
231
232static struct mxs_dma_chan *to_mxs_dma_chan(struct dma_chan *chan)
233{
234 return container_of(chan, struct mxs_dma_chan, chan);
235}
236
237static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx)
238{
239 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(tx->chan);
240
241 mxs_dma_enable_chan(mxs_chan);
242
243 return mxs_dma_assign_cookie(mxs_chan);
244}
245
246static void mxs_dma_tasklet(unsigned long data)
247{
248 struct mxs_dma_chan *mxs_chan = (struct mxs_dma_chan *) data;
249
250 if (mxs_chan->desc.callback)
251 mxs_chan->desc.callback(mxs_chan->desc.callback_param);
252}
253
254static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id)
255{
256 struct mxs_dma_engine *mxs_dma = dev_id;
257 u32 stat1, stat2;
258
259 /* completion status */
260 stat1 = readl(mxs_dma->base + HW_APBHX_CTRL1);
261 stat1 &= MXS_DMA_CHANNELS_MASK;
262 writel(stat1, mxs_dma->base + HW_APBHX_CTRL1 + MXS_CLR_ADDR);
263
264 /* error status */
265 stat2 = readl(mxs_dma->base + HW_APBHX_CTRL2);
266 writel(stat2, mxs_dma->base + HW_APBHX_CTRL2 + MXS_CLR_ADDR);
267
268 /*
269 * When both completion and error of termination bits set at the
270 * same time, we do not take it as an error. IOW, it only becomes
271 * an error we need to handler here in case of ether it's (1) an bus
272 * error or (2) a termination error with no completion.
273 */
274 stat2 = ((stat2 >> MXS_DMA_CHANNELS) & stat2) | /* (1) */
275 (~(stat2 >> MXS_DMA_CHANNELS) & stat2 & ~stat1); /* (2) */
276
277 /* combine error and completion status for checking */
278 stat1 = (stat2 << MXS_DMA_CHANNELS) | stat1;
279 while (stat1) {
280 int channel = fls(stat1) - 1;
281 struct mxs_dma_chan *mxs_chan =
282 &mxs_dma->mxs_chans[channel % MXS_DMA_CHANNELS];
283
284 if (channel >= MXS_DMA_CHANNELS) {
285 dev_dbg(mxs_dma->dma_device.dev,
286 "%s: error in channel %d\n", __func__,
287 channel - MXS_DMA_CHANNELS);
288 mxs_chan->status = DMA_ERROR;
289 mxs_dma_reset_chan(mxs_chan);
290 } else {
291 if (mxs_chan->flags & MXS_DMA_SG_LOOP)
292 mxs_chan->status = DMA_IN_PROGRESS;
293 else
294 mxs_chan->status = DMA_SUCCESS;
295 }
296
297 stat1 &= ~(1 << channel);
298
299 if (mxs_chan->status == DMA_SUCCESS)
300 mxs_chan->last_completed = mxs_chan->desc.cookie;
301
302 /* schedule tasklet on this channel */
303 tasklet_schedule(&mxs_chan->tasklet);
304 }
305
306 return IRQ_HANDLED;
307}
308
309static int mxs_dma_alloc_chan_resources(struct dma_chan *chan)
310{
311 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
312 struct mxs_dma_data *data = chan->private;
313 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
314 int ret;
315
316 if (!data)
317 return -EINVAL;
318
319 mxs_chan->chan_irq = data->chan_irq;
320
321 mxs_chan->ccw = dma_alloc_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
322 &mxs_chan->ccw_phys, GFP_KERNEL);
323 if (!mxs_chan->ccw) {
324 ret = -ENOMEM;
325 goto err_alloc;
326 }
327
328 memset(mxs_chan->ccw, 0, PAGE_SIZE);
329
330 if (mxs_chan->chan_irq != NO_IRQ) {
331 ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler,
332 0, "mxs-dma", mxs_dma);
333 if (ret)
334 goto err_irq;
335 }
336
337 ret = clk_enable(mxs_dma->clk);
338 if (ret)
339 goto err_clk;
340
341 mxs_dma_reset_chan(mxs_chan);
342
343 dma_async_tx_descriptor_init(&mxs_chan->desc, chan);
344 mxs_chan->desc.tx_submit = mxs_dma_tx_submit;
345
346 /* the descriptor is ready */
347 async_tx_ack(&mxs_chan->desc);
348
349 return 0;
350
351err_clk:
352 free_irq(mxs_chan->chan_irq, mxs_dma);
353err_irq:
354 dma_free_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
355 mxs_chan->ccw, mxs_chan->ccw_phys);
356err_alloc:
357 return ret;
358}
359
360static void mxs_dma_free_chan_resources(struct dma_chan *chan)
361{
362 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
363 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
364
365 mxs_dma_disable_chan(mxs_chan);
366
367 free_irq(mxs_chan->chan_irq, mxs_dma);
368
369 dma_free_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
370 mxs_chan->ccw, mxs_chan->ccw_phys);
371
372 clk_disable(mxs_dma->clk);
373}
374
375static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg(
376 struct dma_chan *chan, struct scatterlist *sgl,
377 unsigned int sg_len, enum dma_data_direction direction,
378 unsigned long append)
379{
380 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
381 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
382 struct mxs_dma_ccw *ccw;
383 struct scatterlist *sg;
384 int i, j;
385 u32 *pio;
386 static int idx;
387
388 if (mxs_chan->status == DMA_IN_PROGRESS && !append)
389 return NULL;
390
391 if (sg_len + (append ? idx : 0) > NUM_CCW) {
392 dev_err(mxs_dma->dma_device.dev,
393 "maximum number of sg exceeded: %d > %d\n",
394 sg_len, NUM_CCW);
395 goto err_out;
396 }
397
398 mxs_chan->status = DMA_IN_PROGRESS;
399 mxs_chan->flags = 0;
400
401 /*
402 * If the sg is prepared with append flag set, the sg
403 * will be appended to the last prepared sg.
404 */
405 if (append) {
406 BUG_ON(idx < 1);
407 ccw = &mxs_chan->ccw[idx - 1];
408 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
409 ccw->bits |= CCW_CHAIN;
410 ccw->bits &= ~CCW_IRQ;
411 ccw->bits &= ~CCW_DEC_SEM;
412 ccw->bits &= ~CCW_WAIT4END;
413 } else {
414 idx = 0;
415 }
416
417 if (direction == DMA_NONE) {
418 ccw = &mxs_chan->ccw[idx++];
419 pio = (u32 *) sgl;
420
421 for (j = 0; j < sg_len;)
422 ccw->pio_words[j++] = *pio++;
423
424 ccw->bits = 0;
425 ccw->bits |= CCW_IRQ;
426 ccw->bits |= CCW_DEC_SEM;
427 ccw->bits |= CCW_WAIT4END;
428 ccw->bits |= CCW_HALT_ON_TERM;
429 ccw->bits |= CCW_TERM_FLUSH;
430 ccw->bits |= BF_CCW(sg_len, PIO_NUM);
431 ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND);
432 } else {
433 for_each_sg(sgl, sg, sg_len, i) {
434 if (sg->length > MAX_XFER_BYTES) {
435 dev_err(mxs_dma->dma_device.dev, "maximum bytes for sg entry exceeded: %d > %d\n",
436 sg->length, MAX_XFER_BYTES);
437 goto err_out;
438 }
439
440 ccw = &mxs_chan->ccw[idx++];
441
442 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
443 ccw->bufaddr = sg->dma_address;
444 ccw->xfer_bytes = sg->length;
445
446 ccw->bits = 0;
447 ccw->bits |= CCW_CHAIN;
448 ccw->bits |= CCW_HALT_ON_TERM;
449 ccw->bits |= CCW_TERM_FLUSH;
450 ccw->bits |= BF_CCW(direction == DMA_FROM_DEVICE ?
451 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ,
452 COMMAND);
453
454 if (i + 1 == sg_len) {
455 ccw->bits &= ~CCW_CHAIN;
456 ccw->bits |= CCW_IRQ;
457 ccw->bits |= CCW_DEC_SEM;
458 ccw->bits |= CCW_WAIT4END;
459 }
460 }
461 }
462
463 return &mxs_chan->desc;
464
465err_out:
466 mxs_chan->status = DMA_ERROR;
467 return NULL;
468}
469
470static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic(
471 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
472 size_t period_len, enum dma_data_direction direction)
473{
474 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
475 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
476 int num_periods = buf_len / period_len;
477 int i = 0, buf = 0;
478
479 if (mxs_chan->status == DMA_IN_PROGRESS)
480 return NULL;
481
482 mxs_chan->status = DMA_IN_PROGRESS;
483 mxs_chan->flags |= MXS_DMA_SG_LOOP;
484
485 if (num_periods > NUM_CCW) {
486 dev_err(mxs_dma->dma_device.dev,
487 "maximum number of sg exceeded: %d > %d\n",
488 num_periods, NUM_CCW);
489 goto err_out;
490 }
491
492 if (period_len > MAX_XFER_BYTES) {
493 dev_err(mxs_dma->dma_device.dev,
494 "maximum period size exceeded: %d > %d\n",
495 period_len, MAX_XFER_BYTES);
496 goto err_out;
497 }
498
499 while (buf < buf_len) {
500 struct mxs_dma_ccw *ccw = &mxs_chan->ccw[i];
501
502 if (i + 1 == num_periods)
503 ccw->next = mxs_chan->ccw_phys;
504 else
505 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * (i + 1);
506
507 ccw->bufaddr = dma_addr;
508 ccw->xfer_bytes = period_len;
509
510 ccw->bits = 0;
511 ccw->bits |= CCW_CHAIN;
512 ccw->bits |= CCW_IRQ;
513 ccw->bits |= CCW_HALT_ON_TERM;
514 ccw->bits |= CCW_TERM_FLUSH;
515 ccw->bits |= BF_CCW(direction == DMA_FROM_DEVICE ?
516 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, COMMAND);
517
518 dma_addr += period_len;
519 buf += period_len;
520
521 i++;
522 }
523
524 return &mxs_chan->desc;
525
526err_out:
527 mxs_chan->status = DMA_ERROR;
528 return NULL;
529}
530
531static int mxs_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
532 unsigned long arg)
533{
534 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
535 int ret = 0;
536
537 switch (cmd) {
538 case DMA_TERMINATE_ALL:
539 mxs_dma_disable_chan(mxs_chan);
540 mxs_dma_reset_chan(mxs_chan);
541 break;
542 case DMA_PAUSE:
543 mxs_dma_pause_chan(mxs_chan);
544 break;
545 case DMA_RESUME:
546 mxs_dma_resume_chan(mxs_chan);
547 break;
548 default:
549 ret = -ENOSYS;
550 }
551
552 return ret;
553}
554
555static enum dma_status mxs_dma_tx_status(struct dma_chan *chan,
556 dma_cookie_t cookie, struct dma_tx_state *txstate)
557{
558 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
559 dma_cookie_t last_used;
560
561 last_used = chan->cookie;
562 dma_set_tx_state(txstate, mxs_chan->last_completed, last_used, 0);
563
564 return mxs_chan->status;
565}
566
567static void mxs_dma_issue_pending(struct dma_chan *chan)
568{
569 /*
570 * Nothing to do. We only have a single descriptor.
571 */
572}
573
574static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma)
575{
576 int ret;
577
578 ret = clk_enable(mxs_dma->clk);
579 if (ret)
580 goto err_out;
581
582 ret = mxs_reset_block(mxs_dma->base);
583 if (ret)
584 goto err_out;
585
586 /* only major version matters */
587 mxs_dma->version = readl(mxs_dma->base +
588 ((mxs_dma->dev_id == MXS_DMA_APBX) ?
589 HW_APBX_VERSION : HW_APBH_VERSION)) >>
590 BP_APBHX_VERSION_MAJOR;
591
592 /* enable apbh burst */
593 if (dma_is_apbh()) {
594 writel(BM_APBH_CTRL0_APB_BURST_EN,
595 mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
596 writel(BM_APBH_CTRL0_APB_BURST8_EN,
597 mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
598 }
599
600 /* enable irq for all the channels */
601 writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS,
602 mxs_dma->base + HW_APBHX_CTRL1 + MXS_SET_ADDR);
603
604 clk_disable(mxs_dma->clk);
605
606 return 0;
607
608err_out:
609 return ret;
610}
611
612static int __init mxs_dma_probe(struct platform_device *pdev)
613{
614 const struct platform_device_id *id_entry =
615 platform_get_device_id(pdev);
616 struct mxs_dma_engine *mxs_dma;
617 struct resource *iores;
618 int ret, i;
619
620 mxs_dma = kzalloc(sizeof(*mxs_dma), GFP_KERNEL);
621 if (!mxs_dma)
622 return -ENOMEM;
623
624 mxs_dma->dev_id = id_entry->driver_data;
625
626 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
627
628 if (!request_mem_region(iores->start, resource_size(iores),
629 pdev->name)) {
630 ret = -EBUSY;
631 goto err_request_region;
632 }
633
634 mxs_dma->base = ioremap(iores->start, resource_size(iores));
635 if (!mxs_dma->base) {
636 ret = -ENOMEM;
637 goto err_ioremap;
638 }
639
640 mxs_dma->clk = clk_get(&pdev->dev, NULL);
641 if (IS_ERR(mxs_dma->clk)) {
642 ret = PTR_ERR(mxs_dma->clk);
643 goto err_clk;
644 }
645
646 dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask);
647 dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask);
648
649 INIT_LIST_HEAD(&mxs_dma->dma_device.channels);
650
651 /* Initialize channel parameters */
652 for (i = 0; i < MXS_DMA_CHANNELS; i++) {
653 struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i];
654
655 mxs_chan->mxs_dma = mxs_dma;
656 mxs_chan->chan.device = &mxs_dma->dma_device;
657
658 tasklet_init(&mxs_chan->tasklet, mxs_dma_tasklet,
659 (unsigned long) mxs_chan);
660
661
662 /* Add the channel to mxs_chan list */
663 list_add_tail(&mxs_chan->chan.device_node,
664 &mxs_dma->dma_device.channels);
665 }
666
667 ret = mxs_dma_init(mxs_dma);
668 if (ret)
669 goto err_init;
670
671 mxs_dma->dma_device.dev = &pdev->dev;
672
673 /* mxs_dma gets 65535 bytes maximum sg size */
674 mxs_dma->dma_device.dev->dma_parms = &mxs_dma->dma_parms;
675 dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES);
676
677 mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources;
678 mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources;
679 mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status;
680 mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg;
681 mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic;
682 mxs_dma->dma_device.device_control = mxs_dma_control;
683 mxs_dma->dma_device.device_issue_pending = mxs_dma_issue_pending;
684
685 ret = dma_async_device_register(&mxs_dma->dma_device);
686 if (ret) {
687 dev_err(mxs_dma->dma_device.dev, "unable to register\n");
688 goto err_init;
689 }
690
691 dev_info(mxs_dma->dma_device.dev, "initialized\n");
692
693 return 0;
694
695err_init:
696 clk_put(mxs_dma->clk);
697err_clk:
698 iounmap(mxs_dma->base);
699err_ioremap:
700 release_mem_region(iores->start, resource_size(iores));
701err_request_region:
702 kfree(mxs_dma);
703 return ret;
704}
705
706static struct platform_device_id mxs_dma_type[] = {
707 {
708 .name = "mxs-dma-apbh",
709 .driver_data = MXS_DMA_APBH,
710 }, {
711 .name = "mxs-dma-apbx",
712 .driver_data = MXS_DMA_APBX,
713 }, {
714 /* end of list */
715 }
716};
717
718static struct platform_driver mxs_dma_driver = {
719 .driver = {
720 .name = "mxs-dma",
721 },
722 .id_table = mxs_dma_type,
723};
724
725static int __init mxs_dma_module_init(void)
726{
727 return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe);
728}
729subsys_initcall(mxs_dma_module_init);
1/*
2 * Copyright 2011 Freescale Semiconductor, Inc. All Rights Reserved.
3 *
4 * Refer to drivers/dma/imx-sdma.c
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/init.h>
12#include <linux/types.h>
13#include <linux/mm.h>
14#include <linux/interrupt.h>
15#include <linux/clk.h>
16#include <linux/wait.h>
17#include <linux/sched.h>
18#include <linux/semaphore.h>
19#include <linux/device.h>
20#include <linux/dma-mapping.h>
21#include <linux/slab.h>
22#include <linux/platform_device.h>
23#include <linux/dmaengine.h>
24#include <linux/delay.h>
25#include <linux/module.h>
26#include <linux/stmp_device.h>
27#include <linux/of.h>
28#include <linux/of_device.h>
29#include <linux/of_dma.h>
30#include <linux/list.h>
31
32#include <asm/irq.h>
33
34#include "dmaengine.h"
35
36/*
37 * NOTE: The term "PIO" throughout the mxs-dma implementation means
38 * PIO mode of mxs apbh-dma and apbx-dma. With this working mode,
39 * dma can program the controller registers of peripheral devices.
40 */
41
42#define dma_is_apbh(mxs_dma) ((mxs_dma)->type == MXS_DMA_APBH)
43#define apbh_is_old(mxs_dma) ((mxs_dma)->dev_id == IMX23_DMA)
44
45#define HW_APBHX_CTRL0 0x000
46#define BM_APBH_CTRL0_APB_BURST8_EN (1 << 29)
47#define BM_APBH_CTRL0_APB_BURST_EN (1 << 28)
48#define BP_APBH_CTRL0_RESET_CHANNEL 16
49#define HW_APBHX_CTRL1 0x010
50#define HW_APBHX_CTRL2 0x020
51#define HW_APBHX_CHANNEL_CTRL 0x030
52#define BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL 16
53/*
54 * The offset of NXTCMDAR register is different per both dma type and version,
55 * while stride for each channel is all the same 0x70.
56 */
57#define HW_APBHX_CHn_NXTCMDAR(d, n) \
58 (((dma_is_apbh(d) && apbh_is_old(d)) ? 0x050 : 0x110) + (n) * 0x70)
59#define HW_APBHX_CHn_SEMA(d, n) \
60 (((dma_is_apbh(d) && apbh_is_old(d)) ? 0x080 : 0x140) + (n) * 0x70)
61#define HW_APBHX_CHn_BAR(d, n) \
62 (((dma_is_apbh(d) && apbh_is_old(d)) ? 0x070 : 0x130) + (n) * 0x70)
63#define HW_APBX_CHn_DEBUG1(d, n) (0x150 + (n) * 0x70)
64
65/*
66 * ccw bits definitions
67 *
68 * COMMAND: 0..1 (2)
69 * CHAIN: 2 (1)
70 * IRQ: 3 (1)
71 * NAND_LOCK: 4 (1) - not implemented
72 * NAND_WAIT4READY: 5 (1) - not implemented
73 * DEC_SEM: 6 (1)
74 * WAIT4END: 7 (1)
75 * HALT_ON_TERMINATE: 8 (1)
76 * TERMINATE_FLUSH: 9 (1)
77 * RESERVED: 10..11 (2)
78 * PIO_NUM: 12..15 (4)
79 */
80#define BP_CCW_COMMAND 0
81#define BM_CCW_COMMAND (3 << 0)
82#define CCW_CHAIN (1 << 2)
83#define CCW_IRQ (1 << 3)
84#define CCW_DEC_SEM (1 << 6)
85#define CCW_WAIT4END (1 << 7)
86#define CCW_HALT_ON_TERM (1 << 8)
87#define CCW_TERM_FLUSH (1 << 9)
88#define BP_CCW_PIO_NUM 12
89#define BM_CCW_PIO_NUM (0xf << 12)
90
91#define BF_CCW(value, field) (((value) << BP_CCW_##field) & BM_CCW_##field)
92
93#define MXS_DMA_CMD_NO_XFER 0
94#define MXS_DMA_CMD_WRITE 1
95#define MXS_DMA_CMD_READ 2
96#define MXS_DMA_CMD_DMA_SENSE 3 /* not implemented */
97
98struct mxs_dma_ccw {
99 u32 next;
100 u16 bits;
101 u16 xfer_bytes;
102#define MAX_XFER_BYTES 0xff00
103 u32 bufaddr;
104#define MXS_PIO_WORDS 16
105 u32 pio_words[MXS_PIO_WORDS];
106};
107
108#define CCW_BLOCK_SIZE (4 * PAGE_SIZE)
109#define NUM_CCW (int)(CCW_BLOCK_SIZE / sizeof(struct mxs_dma_ccw))
110
111struct mxs_dma_chan {
112 struct mxs_dma_engine *mxs_dma;
113 struct dma_chan chan;
114 struct dma_async_tx_descriptor desc;
115 struct tasklet_struct tasklet;
116 unsigned int chan_irq;
117 struct mxs_dma_ccw *ccw;
118 dma_addr_t ccw_phys;
119 int desc_count;
120 enum dma_status status;
121 unsigned int flags;
122 bool reset;
123#define MXS_DMA_SG_LOOP (1 << 0)
124#define MXS_DMA_USE_SEMAPHORE (1 << 1)
125};
126
127#define MXS_DMA_CHANNELS 16
128#define MXS_DMA_CHANNELS_MASK 0xffff
129
130enum mxs_dma_devtype {
131 MXS_DMA_APBH,
132 MXS_DMA_APBX,
133};
134
135enum mxs_dma_id {
136 IMX23_DMA,
137 IMX28_DMA,
138};
139
140struct mxs_dma_engine {
141 enum mxs_dma_id dev_id;
142 enum mxs_dma_devtype type;
143 void __iomem *base;
144 struct clk *clk;
145 struct dma_device dma_device;
146 struct device_dma_parameters dma_parms;
147 struct mxs_dma_chan mxs_chans[MXS_DMA_CHANNELS];
148 struct platform_device *pdev;
149 unsigned int nr_channels;
150};
151
152struct mxs_dma_type {
153 enum mxs_dma_id id;
154 enum mxs_dma_devtype type;
155};
156
157static struct mxs_dma_type mxs_dma_types[] = {
158 {
159 .id = IMX23_DMA,
160 .type = MXS_DMA_APBH,
161 }, {
162 .id = IMX23_DMA,
163 .type = MXS_DMA_APBX,
164 }, {
165 .id = IMX28_DMA,
166 .type = MXS_DMA_APBH,
167 }, {
168 .id = IMX28_DMA,
169 .type = MXS_DMA_APBX,
170 }
171};
172
173static const struct platform_device_id mxs_dma_ids[] = {
174 {
175 .name = "imx23-dma-apbh",
176 .driver_data = (kernel_ulong_t) &mxs_dma_types[0],
177 }, {
178 .name = "imx23-dma-apbx",
179 .driver_data = (kernel_ulong_t) &mxs_dma_types[1],
180 }, {
181 .name = "imx28-dma-apbh",
182 .driver_data = (kernel_ulong_t) &mxs_dma_types[2],
183 }, {
184 .name = "imx28-dma-apbx",
185 .driver_data = (kernel_ulong_t) &mxs_dma_types[3],
186 }, {
187 /* end of list */
188 }
189};
190
191static const struct of_device_id mxs_dma_dt_ids[] = {
192 { .compatible = "fsl,imx23-dma-apbh", .data = &mxs_dma_ids[0], },
193 { .compatible = "fsl,imx23-dma-apbx", .data = &mxs_dma_ids[1], },
194 { .compatible = "fsl,imx28-dma-apbh", .data = &mxs_dma_ids[2], },
195 { .compatible = "fsl,imx28-dma-apbx", .data = &mxs_dma_ids[3], },
196 { /* sentinel */ }
197};
198MODULE_DEVICE_TABLE(of, mxs_dma_dt_ids);
199
200static struct mxs_dma_chan *to_mxs_dma_chan(struct dma_chan *chan)
201{
202 return container_of(chan, struct mxs_dma_chan, chan);
203}
204
205static void mxs_dma_reset_chan(struct dma_chan *chan)
206{
207 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
208 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
209 int chan_id = mxs_chan->chan.chan_id;
210
211 /*
212 * mxs dma channel resets can cause a channel stall. To recover from a
213 * channel stall, we have to reset the whole DMA engine. To avoid this,
214 * we use cyclic DMA with semaphores, that are enhanced in
215 * mxs_dma_int_handler. To reset the channel, we can simply stop writing
216 * into the semaphore counter.
217 */
218 if (mxs_chan->flags & MXS_DMA_USE_SEMAPHORE &&
219 mxs_chan->flags & MXS_DMA_SG_LOOP) {
220 mxs_chan->reset = true;
221 } else if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma)) {
222 writel(1 << (chan_id + BP_APBH_CTRL0_RESET_CHANNEL),
223 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
224 } else {
225 unsigned long elapsed = 0;
226 const unsigned long max_wait = 50000; /* 50ms */
227 void __iomem *reg_dbg1 = mxs_dma->base +
228 HW_APBX_CHn_DEBUG1(mxs_dma, chan_id);
229
230 /*
231 * On i.MX28 APBX, the DMA channel can stop working if we reset
232 * the channel while it is in READ_FLUSH (0x08) state.
233 * We wait here until we leave the state. Then we trigger the
234 * reset. Waiting a maximum of 50ms, the kernel shouldn't crash
235 * because of this.
236 */
237 while ((readl(reg_dbg1) & 0xf) == 0x8 && elapsed < max_wait) {
238 udelay(100);
239 elapsed += 100;
240 }
241
242 if (elapsed >= max_wait)
243 dev_err(&mxs_chan->mxs_dma->pdev->dev,
244 "Failed waiting for the DMA channel %d to leave state READ_FLUSH, trying to reset channel in READ_FLUSH state now\n",
245 chan_id);
246
247 writel(1 << (chan_id + BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL),
248 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
249 }
250
251 mxs_chan->status = DMA_COMPLETE;
252}
253
254static void mxs_dma_enable_chan(struct dma_chan *chan)
255{
256 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
257 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
258 int chan_id = mxs_chan->chan.chan_id;
259
260 /* set cmd_addr up */
261 writel(mxs_chan->ccw_phys,
262 mxs_dma->base + HW_APBHX_CHn_NXTCMDAR(mxs_dma, chan_id));
263
264 /* write 1 to SEMA to kick off the channel */
265 if (mxs_chan->flags & MXS_DMA_USE_SEMAPHORE &&
266 mxs_chan->flags & MXS_DMA_SG_LOOP) {
267 /* A cyclic DMA consists of at least 2 segments, so initialize
268 * the semaphore with 2 so we have enough time to add 1 to the
269 * semaphore if we need to */
270 writel(2, mxs_dma->base + HW_APBHX_CHn_SEMA(mxs_dma, chan_id));
271 } else {
272 writel(1, mxs_dma->base + HW_APBHX_CHn_SEMA(mxs_dma, chan_id));
273 }
274 mxs_chan->reset = false;
275}
276
277static void mxs_dma_disable_chan(struct dma_chan *chan)
278{
279 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
280
281 mxs_chan->status = DMA_COMPLETE;
282}
283
284static int mxs_dma_pause_chan(struct dma_chan *chan)
285{
286 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
287 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
288 int chan_id = mxs_chan->chan.chan_id;
289
290 /* freeze the channel */
291 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
292 writel(1 << chan_id,
293 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
294 else
295 writel(1 << chan_id,
296 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
297
298 mxs_chan->status = DMA_PAUSED;
299 return 0;
300}
301
302static int mxs_dma_resume_chan(struct dma_chan *chan)
303{
304 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
305 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
306 int chan_id = mxs_chan->chan.chan_id;
307
308 /* unfreeze the channel */
309 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
310 writel(1 << chan_id,
311 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_CLR);
312 else
313 writel(1 << chan_id,
314 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_CLR);
315
316 mxs_chan->status = DMA_IN_PROGRESS;
317 return 0;
318}
319
320static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx)
321{
322 return dma_cookie_assign(tx);
323}
324
325static void mxs_dma_tasklet(unsigned long data)
326{
327 struct mxs_dma_chan *mxs_chan = (struct mxs_dma_chan *) data;
328
329 dmaengine_desc_get_callback_invoke(&mxs_chan->desc, NULL);
330}
331
332static int mxs_dma_irq_to_chan(struct mxs_dma_engine *mxs_dma, int irq)
333{
334 int i;
335
336 for (i = 0; i != mxs_dma->nr_channels; ++i)
337 if (mxs_dma->mxs_chans[i].chan_irq == irq)
338 return i;
339
340 return -EINVAL;
341}
342
343static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id)
344{
345 struct mxs_dma_engine *mxs_dma = dev_id;
346 struct mxs_dma_chan *mxs_chan;
347 u32 completed;
348 u32 err;
349 int chan = mxs_dma_irq_to_chan(mxs_dma, irq);
350
351 if (chan < 0)
352 return IRQ_NONE;
353
354 /* completion status */
355 completed = readl(mxs_dma->base + HW_APBHX_CTRL1);
356 completed = (completed >> chan) & 0x1;
357
358 /* Clear interrupt */
359 writel((1 << chan),
360 mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_CLR);
361
362 /* error status */
363 err = readl(mxs_dma->base + HW_APBHX_CTRL2);
364 err &= (1 << (MXS_DMA_CHANNELS + chan)) | (1 << chan);
365
366 /*
367 * error status bit is in the upper 16 bits, error irq bit in the lower
368 * 16 bits. We transform it into a simpler error code:
369 * err: 0x00 = no error, 0x01 = TERMINATION, 0x02 = BUS_ERROR
370 */
371 err = (err >> (MXS_DMA_CHANNELS + chan)) + (err >> chan);
372
373 /* Clear error irq */
374 writel((1 << chan),
375 mxs_dma->base + HW_APBHX_CTRL2 + STMP_OFFSET_REG_CLR);
376
377 /*
378 * When both completion and error of termination bits set at the
379 * same time, we do not take it as an error. IOW, it only becomes
380 * an error we need to handle here in case of either it's a bus
381 * error or a termination error with no completion. 0x01 is termination
382 * error, so we can subtract err & completed to get the real error case.
383 */
384 err -= err & completed;
385
386 mxs_chan = &mxs_dma->mxs_chans[chan];
387
388 if (err) {
389 dev_dbg(mxs_dma->dma_device.dev,
390 "%s: error in channel %d\n", __func__,
391 chan);
392 mxs_chan->status = DMA_ERROR;
393 mxs_dma_reset_chan(&mxs_chan->chan);
394 } else if (mxs_chan->status != DMA_COMPLETE) {
395 if (mxs_chan->flags & MXS_DMA_SG_LOOP) {
396 mxs_chan->status = DMA_IN_PROGRESS;
397 if (mxs_chan->flags & MXS_DMA_USE_SEMAPHORE)
398 writel(1, mxs_dma->base +
399 HW_APBHX_CHn_SEMA(mxs_dma, chan));
400 } else {
401 mxs_chan->status = DMA_COMPLETE;
402 }
403 }
404
405 if (mxs_chan->status == DMA_COMPLETE) {
406 if (mxs_chan->reset)
407 return IRQ_HANDLED;
408 dma_cookie_complete(&mxs_chan->desc);
409 }
410
411 /* schedule tasklet on this channel */
412 tasklet_schedule(&mxs_chan->tasklet);
413
414 return IRQ_HANDLED;
415}
416
417static int mxs_dma_alloc_chan_resources(struct dma_chan *chan)
418{
419 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
420 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
421 int ret;
422
423 mxs_chan->ccw = dma_zalloc_coherent(mxs_dma->dma_device.dev,
424 CCW_BLOCK_SIZE,
425 &mxs_chan->ccw_phys, GFP_KERNEL);
426 if (!mxs_chan->ccw) {
427 ret = -ENOMEM;
428 goto err_alloc;
429 }
430
431 ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler,
432 0, "mxs-dma", mxs_dma);
433 if (ret)
434 goto err_irq;
435
436 ret = clk_prepare_enable(mxs_dma->clk);
437 if (ret)
438 goto err_clk;
439
440 mxs_dma_reset_chan(chan);
441
442 dma_async_tx_descriptor_init(&mxs_chan->desc, chan);
443 mxs_chan->desc.tx_submit = mxs_dma_tx_submit;
444
445 /* the descriptor is ready */
446 async_tx_ack(&mxs_chan->desc);
447
448 return 0;
449
450err_clk:
451 free_irq(mxs_chan->chan_irq, mxs_dma);
452err_irq:
453 dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE,
454 mxs_chan->ccw, mxs_chan->ccw_phys);
455err_alloc:
456 return ret;
457}
458
459static void mxs_dma_free_chan_resources(struct dma_chan *chan)
460{
461 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
462 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
463
464 mxs_dma_disable_chan(chan);
465
466 free_irq(mxs_chan->chan_irq, mxs_dma);
467
468 dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE,
469 mxs_chan->ccw, mxs_chan->ccw_phys);
470
471 clk_disable_unprepare(mxs_dma->clk);
472}
473
474/*
475 * How to use the flags for ->device_prep_slave_sg() :
476 * [1] If there is only one DMA command in the DMA chain, the code should be:
477 * ......
478 * ->device_prep_slave_sg(DMA_CTRL_ACK);
479 * ......
480 * [2] If there are two DMA commands in the DMA chain, the code should be
481 * ......
482 * ->device_prep_slave_sg(0);
483 * ......
484 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
485 * ......
486 * [3] If there are more than two DMA commands in the DMA chain, the code
487 * should be:
488 * ......
489 * ->device_prep_slave_sg(0); // First
490 * ......
491 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT [| DMA_CTRL_ACK]);
492 * ......
493 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK); // Last
494 * ......
495 */
496static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg(
497 struct dma_chan *chan, struct scatterlist *sgl,
498 unsigned int sg_len, enum dma_transfer_direction direction,
499 unsigned long flags, void *context)
500{
501 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
502 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
503 struct mxs_dma_ccw *ccw;
504 struct scatterlist *sg;
505 u32 i, j;
506 u32 *pio;
507 bool append = flags & DMA_PREP_INTERRUPT;
508 int idx = append ? mxs_chan->desc_count : 0;
509
510 if (mxs_chan->status == DMA_IN_PROGRESS && !append)
511 return NULL;
512
513 if (sg_len + (append ? idx : 0) > NUM_CCW) {
514 dev_err(mxs_dma->dma_device.dev,
515 "maximum number of sg exceeded: %d > %d\n",
516 sg_len, NUM_CCW);
517 goto err_out;
518 }
519
520 mxs_chan->status = DMA_IN_PROGRESS;
521 mxs_chan->flags = 0;
522
523 /*
524 * If the sg is prepared with append flag set, the sg
525 * will be appended to the last prepared sg.
526 */
527 if (append) {
528 BUG_ON(idx < 1);
529 ccw = &mxs_chan->ccw[idx - 1];
530 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
531 ccw->bits |= CCW_CHAIN;
532 ccw->bits &= ~CCW_IRQ;
533 ccw->bits &= ~CCW_DEC_SEM;
534 } else {
535 idx = 0;
536 }
537
538 if (direction == DMA_TRANS_NONE) {
539 ccw = &mxs_chan->ccw[idx++];
540 pio = (u32 *) sgl;
541
542 for (j = 0; j < sg_len;)
543 ccw->pio_words[j++] = *pio++;
544
545 ccw->bits = 0;
546 ccw->bits |= CCW_IRQ;
547 ccw->bits |= CCW_DEC_SEM;
548 if (flags & DMA_CTRL_ACK)
549 ccw->bits |= CCW_WAIT4END;
550 ccw->bits |= CCW_HALT_ON_TERM;
551 ccw->bits |= CCW_TERM_FLUSH;
552 ccw->bits |= BF_CCW(sg_len, PIO_NUM);
553 ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND);
554 } else {
555 for_each_sg(sgl, sg, sg_len, i) {
556 if (sg_dma_len(sg) > MAX_XFER_BYTES) {
557 dev_err(mxs_dma->dma_device.dev, "maximum bytes for sg entry exceeded: %d > %d\n",
558 sg_dma_len(sg), MAX_XFER_BYTES);
559 goto err_out;
560 }
561
562 ccw = &mxs_chan->ccw[idx++];
563
564 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
565 ccw->bufaddr = sg->dma_address;
566 ccw->xfer_bytes = sg_dma_len(sg);
567
568 ccw->bits = 0;
569 ccw->bits |= CCW_CHAIN;
570 ccw->bits |= CCW_HALT_ON_TERM;
571 ccw->bits |= CCW_TERM_FLUSH;
572 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
573 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ,
574 COMMAND);
575
576 if (i + 1 == sg_len) {
577 ccw->bits &= ~CCW_CHAIN;
578 ccw->bits |= CCW_IRQ;
579 ccw->bits |= CCW_DEC_SEM;
580 if (flags & DMA_CTRL_ACK)
581 ccw->bits |= CCW_WAIT4END;
582 }
583 }
584 }
585 mxs_chan->desc_count = idx;
586
587 return &mxs_chan->desc;
588
589err_out:
590 mxs_chan->status = DMA_ERROR;
591 return NULL;
592}
593
594static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic(
595 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
596 size_t period_len, enum dma_transfer_direction direction,
597 unsigned long flags)
598{
599 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
600 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
601 u32 num_periods = buf_len / period_len;
602 u32 i = 0, buf = 0;
603
604 if (mxs_chan->status == DMA_IN_PROGRESS)
605 return NULL;
606
607 mxs_chan->status = DMA_IN_PROGRESS;
608 mxs_chan->flags |= MXS_DMA_SG_LOOP;
609 mxs_chan->flags |= MXS_DMA_USE_SEMAPHORE;
610
611 if (num_periods > NUM_CCW) {
612 dev_err(mxs_dma->dma_device.dev,
613 "maximum number of sg exceeded: %d > %d\n",
614 num_periods, NUM_CCW);
615 goto err_out;
616 }
617
618 if (period_len > MAX_XFER_BYTES) {
619 dev_err(mxs_dma->dma_device.dev,
620 "maximum period size exceeded: %d > %d\n",
621 period_len, MAX_XFER_BYTES);
622 goto err_out;
623 }
624
625 while (buf < buf_len) {
626 struct mxs_dma_ccw *ccw = &mxs_chan->ccw[i];
627
628 if (i + 1 == num_periods)
629 ccw->next = mxs_chan->ccw_phys;
630 else
631 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * (i + 1);
632
633 ccw->bufaddr = dma_addr;
634 ccw->xfer_bytes = period_len;
635
636 ccw->bits = 0;
637 ccw->bits |= CCW_CHAIN;
638 ccw->bits |= CCW_IRQ;
639 ccw->bits |= CCW_HALT_ON_TERM;
640 ccw->bits |= CCW_TERM_FLUSH;
641 ccw->bits |= CCW_DEC_SEM;
642 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
643 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, COMMAND);
644
645 dma_addr += period_len;
646 buf += period_len;
647
648 i++;
649 }
650 mxs_chan->desc_count = i;
651
652 return &mxs_chan->desc;
653
654err_out:
655 mxs_chan->status = DMA_ERROR;
656 return NULL;
657}
658
659static int mxs_dma_terminate_all(struct dma_chan *chan)
660{
661 mxs_dma_reset_chan(chan);
662 mxs_dma_disable_chan(chan);
663
664 return 0;
665}
666
667static enum dma_status mxs_dma_tx_status(struct dma_chan *chan,
668 dma_cookie_t cookie, struct dma_tx_state *txstate)
669{
670 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
671 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
672 u32 residue = 0;
673
674 if (mxs_chan->status == DMA_IN_PROGRESS &&
675 mxs_chan->flags & MXS_DMA_SG_LOOP) {
676 struct mxs_dma_ccw *last_ccw;
677 u32 bar;
678
679 last_ccw = &mxs_chan->ccw[mxs_chan->desc_count - 1];
680 residue = last_ccw->xfer_bytes + last_ccw->bufaddr;
681
682 bar = readl(mxs_dma->base +
683 HW_APBHX_CHn_BAR(mxs_dma, chan->chan_id));
684 residue -= bar;
685 }
686
687 dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
688 residue);
689
690 return mxs_chan->status;
691}
692
693static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma)
694{
695 int ret;
696
697 ret = clk_prepare_enable(mxs_dma->clk);
698 if (ret)
699 return ret;
700
701 ret = stmp_reset_block(mxs_dma->base);
702 if (ret)
703 goto err_out;
704
705 /* enable apbh burst */
706 if (dma_is_apbh(mxs_dma)) {
707 writel(BM_APBH_CTRL0_APB_BURST_EN,
708 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
709 writel(BM_APBH_CTRL0_APB_BURST8_EN,
710 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
711 }
712
713 /* enable irq for all the channels */
714 writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS,
715 mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_SET);
716
717err_out:
718 clk_disable_unprepare(mxs_dma->clk);
719 return ret;
720}
721
722struct mxs_dma_filter_param {
723 struct device_node *of_node;
724 unsigned int chan_id;
725};
726
727static bool mxs_dma_filter_fn(struct dma_chan *chan, void *fn_param)
728{
729 struct mxs_dma_filter_param *param = fn_param;
730 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
731 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
732 int chan_irq;
733
734 if (mxs_dma->dma_device.dev->of_node != param->of_node)
735 return false;
736
737 if (chan->chan_id != param->chan_id)
738 return false;
739
740 chan_irq = platform_get_irq(mxs_dma->pdev, param->chan_id);
741 if (chan_irq < 0)
742 return false;
743
744 mxs_chan->chan_irq = chan_irq;
745
746 return true;
747}
748
749static struct dma_chan *mxs_dma_xlate(struct of_phandle_args *dma_spec,
750 struct of_dma *ofdma)
751{
752 struct mxs_dma_engine *mxs_dma = ofdma->of_dma_data;
753 dma_cap_mask_t mask = mxs_dma->dma_device.cap_mask;
754 struct mxs_dma_filter_param param;
755
756 if (dma_spec->args_count != 1)
757 return NULL;
758
759 param.of_node = ofdma->of_node;
760 param.chan_id = dma_spec->args[0];
761
762 if (param.chan_id >= mxs_dma->nr_channels)
763 return NULL;
764
765 return dma_request_channel(mask, mxs_dma_filter_fn, ¶m);
766}
767
768static int __init mxs_dma_probe(struct platform_device *pdev)
769{
770 struct device_node *np = pdev->dev.of_node;
771 const struct platform_device_id *id_entry;
772 const struct of_device_id *of_id;
773 const struct mxs_dma_type *dma_type;
774 struct mxs_dma_engine *mxs_dma;
775 struct resource *iores;
776 int ret, i;
777
778 mxs_dma = devm_kzalloc(&pdev->dev, sizeof(*mxs_dma), GFP_KERNEL);
779 if (!mxs_dma)
780 return -ENOMEM;
781
782 ret = of_property_read_u32(np, "dma-channels", &mxs_dma->nr_channels);
783 if (ret) {
784 dev_err(&pdev->dev, "failed to read dma-channels\n");
785 return ret;
786 }
787
788 of_id = of_match_device(mxs_dma_dt_ids, &pdev->dev);
789 if (of_id)
790 id_entry = of_id->data;
791 else
792 id_entry = platform_get_device_id(pdev);
793
794 dma_type = (struct mxs_dma_type *)id_entry->driver_data;
795 mxs_dma->type = dma_type->type;
796 mxs_dma->dev_id = dma_type->id;
797
798 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
799 mxs_dma->base = devm_ioremap_resource(&pdev->dev, iores);
800 if (IS_ERR(mxs_dma->base))
801 return PTR_ERR(mxs_dma->base);
802
803 mxs_dma->clk = devm_clk_get(&pdev->dev, NULL);
804 if (IS_ERR(mxs_dma->clk))
805 return PTR_ERR(mxs_dma->clk);
806
807 dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask);
808 dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask);
809
810 INIT_LIST_HEAD(&mxs_dma->dma_device.channels);
811
812 /* Initialize channel parameters */
813 for (i = 0; i < MXS_DMA_CHANNELS; i++) {
814 struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i];
815
816 mxs_chan->mxs_dma = mxs_dma;
817 mxs_chan->chan.device = &mxs_dma->dma_device;
818 dma_cookie_init(&mxs_chan->chan);
819
820 tasklet_init(&mxs_chan->tasklet, mxs_dma_tasklet,
821 (unsigned long) mxs_chan);
822
823
824 /* Add the channel to mxs_chan list */
825 list_add_tail(&mxs_chan->chan.device_node,
826 &mxs_dma->dma_device.channels);
827 }
828
829 ret = mxs_dma_init(mxs_dma);
830 if (ret)
831 return ret;
832
833 mxs_dma->pdev = pdev;
834 mxs_dma->dma_device.dev = &pdev->dev;
835
836 /* mxs_dma gets 65535 bytes maximum sg size */
837 mxs_dma->dma_device.dev->dma_parms = &mxs_dma->dma_parms;
838 dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES);
839
840 mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources;
841 mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources;
842 mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status;
843 mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg;
844 mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic;
845 mxs_dma->dma_device.device_pause = mxs_dma_pause_chan;
846 mxs_dma->dma_device.device_resume = mxs_dma_resume_chan;
847 mxs_dma->dma_device.device_terminate_all = mxs_dma_terminate_all;
848 mxs_dma->dma_device.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
849 mxs_dma->dma_device.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
850 mxs_dma->dma_device.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
851 mxs_dma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
852 mxs_dma->dma_device.device_issue_pending = mxs_dma_enable_chan;
853
854 ret = dma_async_device_register(&mxs_dma->dma_device);
855 if (ret) {
856 dev_err(mxs_dma->dma_device.dev, "unable to register\n");
857 return ret;
858 }
859
860 ret = of_dma_controller_register(np, mxs_dma_xlate, mxs_dma);
861 if (ret) {
862 dev_err(mxs_dma->dma_device.dev,
863 "failed to register controller\n");
864 dma_async_device_unregister(&mxs_dma->dma_device);
865 }
866
867 dev_info(mxs_dma->dma_device.dev, "initialized\n");
868
869 return 0;
870}
871
872static struct platform_driver mxs_dma_driver = {
873 .driver = {
874 .name = "mxs-dma",
875 .of_match_table = mxs_dma_dt_ids,
876 },
877 .id_table = mxs_dma_ids,
878};
879
880static int __init mxs_dma_module_init(void)
881{
882 return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe);
883}
884subsys_initcall(mxs_dma_module_init);