Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 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> |
Dong Aisheng | 90c9abc | 2012-05-04 20:12:17 +0800 | [diff] [blame] | 25 | #include <linux/module.h> |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 26 | #include <linux/stmp_device.h> |
Dong Aisheng | 90c9abc | 2012-05-04 20:12:17 +0800 | [diff] [blame] | 27 | #include <linux/of.h> |
| 28 | #include <linux/of_device.h> |
Shawn Guo | d84f638 | 2013-02-26 09:42:09 +0800 | [diff] [blame] | 29 | #include <linux/of_dma.h> |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 30 | #include <linux/list.h> |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 31 | |
| 32 | #include <asm/irq.h> |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 33 | |
Russell King - ARM Linux | d2ebfb3 | 2012-03-06 22:34:26 +0000 | [diff] [blame] | 34 | #include "dmaengine.h" |
| 35 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 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 | |
Shawn Guo | 8c92013 | 2012-05-10 06:23:26 +0800 | [diff] [blame] | 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) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 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) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 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 |
Shawn Guo | bb11fb6 | 2012-05-07 14:14:08 +0800 | [diff] [blame] | 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) |
Markus Pargmann | 7b11304 | 2013-10-29 08:47:46 +0100 | [diff] [blame] | 61 | #define HW_APBHX_CHn_BAR(d, n) \ |
| 62 | (((dma_is_apbh(d) && apbh_is_old(d)) ? 0x070 : 0x130) + (n) * 0x70) |
Markus Pargmann | 702e94d | 2013-10-29 08:47:47 +0100 | [diff] [blame] | 63 | #define HW_APBX_CHn_DEBUG1(d, n) (0x150 + (n) * 0x70) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 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 | |
| 98 | struct 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 | |
Marek Vasut | 5e97fa9 | 2012-09-04 06:04:25 +0200 | [diff] [blame] | 108 | #define CCW_BLOCK_SIZE (4 * PAGE_SIZE) |
| 109 | #define NUM_CCW (int)(CCW_BLOCK_SIZE / sizeof(struct mxs_dma_ccw)) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 110 | |
| 111 | struct 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; |
Fabio Estevam | f2ad699 | 2013-01-07 23:48:39 -0200 | [diff] [blame] | 116 | unsigned int chan_irq; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 117 | struct mxs_dma_ccw *ccw; |
| 118 | dma_addr_t ccw_phys; |
Lothar Waßmann | 6d23ea4 | 2011-12-08 09:15:43 +0100 | [diff] [blame] | 119 | int desc_count; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 120 | enum dma_status status; |
| 121 | unsigned int flags; |
Markus Pargmann | 2dcbdce | 2013-10-29 08:47:49 +0100 | [diff] [blame] | 122 | bool reset; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 123 | #define MXS_DMA_SG_LOOP (1 << 0) |
Markus Pargmann | 2dcbdce | 2013-10-29 08:47:49 +0100 | [diff] [blame] | 124 | #define MXS_DMA_USE_SEMAPHORE (1 << 1) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | #define MXS_DMA_CHANNELS 16 |
| 128 | #define MXS_DMA_CHANNELS_MASK 0xffff |
| 129 | |
Shawn Guo | 8c92013 | 2012-05-10 06:23:26 +0800 | [diff] [blame] | 130 | enum mxs_dma_devtype { |
| 131 | MXS_DMA_APBH, |
| 132 | MXS_DMA_APBX, |
| 133 | }; |
| 134 | |
| 135 | enum mxs_dma_id { |
| 136 | IMX23_DMA, |
| 137 | IMX28_DMA, |
| 138 | }; |
| 139 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 140 | struct mxs_dma_engine { |
Shawn Guo | 8c92013 | 2012-05-10 06:23:26 +0800 | [diff] [blame] | 141 | enum mxs_dma_id dev_id; |
| 142 | enum mxs_dma_devtype type; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 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]; |
Shawn Guo | d84f638 | 2013-02-26 09:42:09 +0800 | [diff] [blame] | 148 | struct platform_device *pdev; |
| 149 | unsigned int nr_channels; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 150 | }; |
| 151 | |
Shawn Guo | 8c92013 | 2012-05-10 06:23:26 +0800 | [diff] [blame] | 152 | struct mxs_dma_type { |
| 153 | enum mxs_dma_id id; |
| 154 | enum mxs_dma_devtype type; |
| 155 | }; |
| 156 | |
| 157 | static 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 | |
Krzysztof Kozlowski | 0d85050 | 2015-05-02 00:57:47 +0900 | [diff] [blame] | 173 | static const struct platform_device_id mxs_dma_ids[] = { |
Shawn Guo | 8c92013 | 2012-05-10 06:23:26 +0800 | [diff] [blame] | 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 | |
Dong Aisheng | 90c9abc | 2012-05-04 20:12:17 +0800 | [diff] [blame] | 191 | static 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 | }; |
| 198 | MODULE_DEVICE_TABLE(of, mxs_dma_dt_ids); |
| 199 | |
Shawn Guo | 8c92013 | 2012-05-10 06:23:26 +0800 | [diff] [blame] | 200 | static 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 | |
Maxime Ripard | 5c9d2e3 | 2014-11-17 14:42:26 +0100 | [diff] [blame] | 205 | static void mxs_dma_reset_chan(struct dma_chan *chan) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 206 | { |
Maxime Ripard | 5c9d2e3 | 2014-11-17 14:42:26 +0100 | [diff] [blame] | 207 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 208 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
| 209 | int chan_id = mxs_chan->chan.chan_id; |
| 210 | |
Markus Pargmann | 2dcbdce | 2013-10-29 08:47:49 +0100 | [diff] [blame] | 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)) { |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 222 | writel(1 << (chan_id + BP_APBH_CTRL0_RESET_CHANNEL), |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 223 | mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET); |
Markus Pargmann | 702e94d | 2013-10-29 08:47:47 +0100 | [diff] [blame] | 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 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 247 | writel(1 << (chan_id + BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL), |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 248 | mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET); |
Markus Pargmann | 702e94d | 2013-10-29 08:47:47 +0100 | [diff] [blame] | 249 | } |
Markus Pargmann | bb3660f | 2013-10-29 08:47:48 +0100 | [diff] [blame] | 250 | |
| 251 | mxs_chan->status = DMA_COMPLETE; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 252 | } |
| 253 | |
Maxime Ripard | 5c9d2e3 | 2014-11-17 14:42:26 +0100 | [diff] [blame] | 254 | static void mxs_dma_enable_chan(struct dma_chan *chan) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 255 | { |
Maxime Ripard | 5c9d2e3 | 2014-11-17 14:42:26 +0100 | [diff] [blame] | 256 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 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, |
Shawn Guo | bb11fb6 | 2012-05-07 14:14:08 +0800 | [diff] [blame] | 262 | mxs_dma->base + HW_APBHX_CHn_NXTCMDAR(mxs_dma, chan_id)); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 263 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 264 | /* write 1 to SEMA to kick off the channel */ |
Markus Pargmann | 2dcbdce | 2013-10-29 08:47:49 +0100 | [diff] [blame] | 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; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 275 | } |
| 276 | |
Maxime Ripard | 5c9d2e3 | 2014-11-17 14:42:26 +0100 | [diff] [blame] | 277 | static void mxs_dma_disable_chan(struct dma_chan *chan) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 278 | { |
Maxime Ripard | 5c9d2e3 | 2014-11-17 14:42:26 +0100 | [diff] [blame] | 279 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
| 280 | |
Vinod Koul | 2737583 | 2013-10-16 20:51:30 +0530 | [diff] [blame] | 281 | mxs_chan->status = DMA_COMPLETE; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 282 | } |
| 283 | |
Vinod Koul | a29c395 | 2014-12-08 11:24:09 +0530 | [diff] [blame] | 284 | static int mxs_dma_pause_chan(struct dma_chan *chan) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 285 | { |
Maxime Ripard | 5c9d2e3 | 2014-11-17 14:42:26 +0100 | [diff] [blame] | 286 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 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 */ |
Shawn Guo | bb11fb6 | 2012-05-07 14:14:08 +0800 | [diff] [blame] | 291 | if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma)) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 292 | writel(1 << chan_id, |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 293 | mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 294 | else |
| 295 | writel(1 << chan_id, |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 296 | mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 297 | |
| 298 | mxs_chan->status = DMA_PAUSED; |
Vinod Koul | a29c395 | 2014-12-08 11:24:09 +0530 | [diff] [blame] | 299 | return 0; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 300 | } |
| 301 | |
Vinod Koul | a29c395 | 2014-12-08 11:24:09 +0530 | [diff] [blame] | 302 | static int mxs_dma_resume_chan(struct dma_chan *chan) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 303 | { |
Maxime Ripard | 5c9d2e3 | 2014-11-17 14:42:26 +0100 | [diff] [blame] | 304 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 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 */ |
Shawn Guo | bb11fb6 | 2012-05-07 14:14:08 +0800 | [diff] [blame] | 309 | if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma)) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 310 | writel(1 << chan_id, |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 311 | mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_CLR); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 312 | else |
| 313 | writel(1 << chan_id, |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 314 | mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_CLR); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 315 | |
| 316 | mxs_chan->status = DMA_IN_PROGRESS; |
Vinod Koul | a29c395 | 2014-12-08 11:24:09 +0530 | [diff] [blame] | 317 | return 0; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 318 | } |
| 319 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 320 | static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx) |
| 321 | { |
Russell King - ARM Linux | 884485e | 2012-03-06 22:34:46 +0000 | [diff] [blame] | 322 | return dma_cookie_assign(tx); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static void mxs_dma_tasklet(unsigned long data) |
| 326 | { |
| 327 | struct mxs_dma_chan *mxs_chan = (struct mxs_dma_chan *) data; |
| 328 | |
Dave Jiang | 064370c | 2016-07-20 13:12:18 -0700 | [diff] [blame] | 329 | dmaengine_desc_get_callback_invoke(&mxs_chan->desc, NULL); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 330 | } |
| 331 | |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 332 | static 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 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 343 | static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id) |
| 344 | { |
| 345 | struct mxs_dma_engine *mxs_dma = dev_id; |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 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; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 353 | |
| 354 | /* completion status */ |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 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); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 361 | |
| 362 | /* error status */ |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 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); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 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 |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 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. |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 383 | */ |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 384 | err -= err & completed; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 385 | |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 386 | mxs_chan = &mxs_dma->mxs_chans[chan]; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 387 | |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 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; |
Vinod Koul | e0cad7a | 2014-12-07 23:07:38 +0530 | [diff] [blame] | 393 | mxs_dma_reset_chan(&mxs_chan->chan); |
Markus Pargmann | bb3660f | 2013-10-29 08:47:48 +0100 | [diff] [blame] | 394 | } else if (mxs_chan->status != DMA_COMPLETE) { |
Markus Pargmann | 2dcbdce | 2013-10-29 08:47:49 +0100 | [diff] [blame] | 395 | if (mxs_chan->flags & MXS_DMA_SG_LOOP) { |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 396 | mxs_chan->status = DMA_IN_PROGRESS; |
Markus Pargmann | 2dcbdce | 2013-10-29 08:47:49 +0100 | [diff] [blame] | 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 { |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 401 | mxs_chan->status = DMA_COMPLETE; |
Markus Pargmann | 2dcbdce | 2013-10-29 08:47:49 +0100 | [diff] [blame] | 402 | } |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 403 | } |
| 404 | |
Markus Pargmann | 2dcbdce | 2013-10-29 08:47:49 +0100 | [diff] [blame] | 405 | if (mxs_chan->status == DMA_COMPLETE) { |
| 406 | if (mxs_chan->reset) |
| 407 | return IRQ_HANDLED; |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 408 | dma_cookie_complete(&mxs_chan->desc); |
Markus Pargmann | 2dcbdce | 2013-10-29 08:47:49 +0100 | [diff] [blame] | 409 | } |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 410 | |
| 411 | /* schedule tasklet on this channel */ |
| 412 | tasklet_schedule(&mxs_chan->tasklet); |
| 413 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 414 | return IRQ_HANDLED; |
| 415 | } |
| 416 | |
| 417 | static int mxs_dma_alloc_chan_resources(struct dma_chan *chan) |
| 418 | { |
| 419 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 420 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
| 421 | int ret; |
| 422 | |
Joe Perches | 9f92d22 | 2014-06-15 13:37:35 -0700 | [diff] [blame] | 423 | mxs_chan->ccw = dma_zalloc_coherent(mxs_dma->dma_device.dev, |
| 424 | CCW_BLOCK_SIZE, |
| 425 | &mxs_chan->ccw_phys, GFP_KERNEL); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 426 | if (!mxs_chan->ccw) { |
| 427 | ret = -ENOMEM; |
| 428 | goto err_alloc; |
| 429 | } |
| 430 | |
Shawn Guo | 95bfea1 | 2011-06-30 16:06:33 +0800 | [diff] [blame] | 431 | if (mxs_chan->chan_irq != NO_IRQ) { |
| 432 | ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler, |
| 433 | 0, "mxs-dma", mxs_dma); |
| 434 | if (ret) |
| 435 | goto err_irq; |
| 436 | } |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 437 | |
Shawn Guo | 759a2e3 | 2011-12-20 13:54:00 +0800 | [diff] [blame] | 438 | ret = clk_prepare_enable(mxs_dma->clk); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 439 | if (ret) |
| 440 | goto err_clk; |
| 441 | |
Maxime Ripard | 5c9d2e3 | 2014-11-17 14:42:26 +0100 | [diff] [blame] | 442 | mxs_dma_reset_chan(chan); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 443 | |
| 444 | dma_async_tx_descriptor_init(&mxs_chan->desc, chan); |
| 445 | mxs_chan->desc.tx_submit = mxs_dma_tx_submit; |
| 446 | |
| 447 | /* the descriptor is ready */ |
| 448 | async_tx_ack(&mxs_chan->desc); |
| 449 | |
| 450 | return 0; |
| 451 | |
| 452 | err_clk: |
| 453 | free_irq(mxs_chan->chan_irq, mxs_dma); |
| 454 | err_irq: |
Marek Vasut | 5e97fa9 | 2012-09-04 06:04:25 +0200 | [diff] [blame] | 455 | dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE, |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 456 | mxs_chan->ccw, mxs_chan->ccw_phys); |
| 457 | err_alloc: |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | static void mxs_dma_free_chan_resources(struct dma_chan *chan) |
| 462 | { |
| 463 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
| 464 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
| 465 | |
Maxime Ripard | 5c9d2e3 | 2014-11-17 14:42:26 +0100 | [diff] [blame] | 466 | mxs_dma_disable_chan(chan); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 467 | |
| 468 | free_irq(mxs_chan->chan_irq, mxs_dma); |
| 469 | |
Marek Vasut | 5e97fa9 | 2012-09-04 06:04:25 +0200 | [diff] [blame] | 470 | dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE, |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 471 | mxs_chan->ccw, mxs_chan->ccw_phys); |
| 472 | |
Shawn Guo | 759a2e3 | 2011-12-20 13:54:00 +0800 | [diff] [blame] | 473 | clk_disable_unprepare(mxs_dma->clk); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 474 | } |
| 475 | |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 476 | /* |
| 477 | * How to use the flags for ->device_prep_slave_sg() : |
| 478 | * [1] If there is only one DMA command in the DMA chain, the code should be: |
| 479 | * ...... |
| 480 | * ->device_prep_slave_sg(DMA_CTRL_ACK); |
| 481 | * ...... |
| 482 | * [2] If there are two DMA commands in the DMA chain, the code should be |
| 483 | * ...... |
| 484 | * ->device_prep_slave_sg(0); |
| 485 | * ...... |
| 486 | * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 487 | * ...... |
| 488 | * [3] If there are more than two DMA commands in the DMA chain, the code |
| 489 | * should be: |
| 490 | * ...... |
| 491 | * ->device_prep_slave_sg(0); // First |
| 492 | * ...... |
| 493 | * ->device_prep_slave_sg(DMA_PREP_INTERRUPT [| DMA_CTRL_ACK]); |
| 494 | * ...... |
| 495 | * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK); // Last |
| 496 | * ...... |
| 497 | */ |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 498 | static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg( |
| 499 | struct dma_chan *chan, struct scatterlist *sgl, |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 500 | unsigned int sg_len, enum dma_transfer_direction direction, |
Linus Torvalds | 623ff77 | 2012-03-30 17:31:56 -0700 | [diff] [blame] | 501 | unsigned long flags, void *context) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 502 | { |
| 503 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
| 504 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
| 505 | struct mxs_dma_ccw *ccw; |
| 506 | struct scatterlist *sg; |
Fabio Estevam | f2ad699 | 2013-01-07 23:48:39 -0200 | [diff] [blame] | 507 | u32 i, j; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 508 | u32 *pio; |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 509 | bool append = flags & DMA_PREP_INTERRUPT; |
Lothar Waßmann | 6d23ea4 | 2011-12-08 09:15:43 +0100 | [diff] [blame] | 510 | int idx = append ? mxs_chan->desc_count : 0; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 511 | |
| 512 | if (mxs_chan->status == DMA_IN_PROGRESS && !append) |
| 513 | return NULL; |
| 514 | |
| 515 | if (sg_len + (append ? idx : 0) > NUM_CCW) { |
| 516 | dev_err(mxs_dma->dma_device.dev, |
| 517 | "maximum number of sg exceeded: %d > %d\n", |
| 518 | sg_len, NUM_CCW); |
| 519 | goto err_out; |
| 520 | } |
| 521 | |
| 522 | mxs_chan->status = DMA_IN_PROGRESS; |
| 523 | mxs_chan->flags = 0; |
| 524 | |
| 525 | /* |
| 526 | * If the sg is prepared with append flag set, the sg |
| 527 | * will be appended to the last prepared sg. |
| 528 | */ |
| 529 | if (append) { |
| 530 | BUG_ON(idx < 1); |
| 531 | ccw = &mxs_chan->ccw[idx - 1]; |
| 532 | ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx; |
| 533 | ccw->bits |= CCW_CHAIN; |
| 534 | ccw->bits &= ~CCW_IRQ; |
| 535 | ccw->bits &= ~CCW_DEC_SEM; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 536 | } else { |
| 537 | idx = 0; |
| 538 | } |
| 539 | |
Shawn Guo | 62268ce | 2011-12-13 23:48:03 +0800 | [diff] [blame] | 540 | if (direction == DMA_TRANS_NONE) { |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 541 | ccw = &mxs_chan->ccw[idx++]; |
| 542 | pio = (u32 *) sgl; |
| 543 | |
| 544 | for (j = 0; j < sg_len;) |
| 545 | ccw->pio_words[j++] = *pio++; |
| 546 | |
| 547 | ccw->bits = 0; |
| 548 | ccw->bits |= CCW_IRQ; |
| 549 | ccw->bits |= CCW_DEC_SEM; |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 550 | if (flags & DMA_CTRL_ACK) |
| 551 | ccw->bits |= CCW_WAIT4END; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 552 | ccw->bits |= CCW_HALT_ON_TERM; |
| 553 | ccw->bits |= CCW_TERM_FLUSH; |
| 554 | ccw->bits |= BF_CCW(sg_len, PIO_NUM); |
| 555 | ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND); |
| 556 | } else { |
| 557 | for_each_sg(sgl, sg, sg_len, i) { |
Lars-Peter Clausen | fdaf9c4 | 2012-04-25 20:50:52 +0200 | [diff] [blame] | 558 | if (sg_dma_len(sg) > MAX_XFER_BYTES) { |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 559 | dev_err(mxs_dma->dma_device.dev, "maximum bytes for sg entry exceeded: %d > %d\n", |
Lars-Peter Clausen | fdaf9c4 | 2012-04-25 20:50:52 +0200 | [diff] [blame] | 560 | sg_dma_len(sg), MAX_XFER_BYTES); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 561 | goto err_out; |
| 562 | } |
| 563 | |
| 564 | ccw = &mxs_chan->ccw[idx++]; |
| 565 | |
| 566 | ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx; |
| 567 | ccw->bufaddr = sg->dma_address; |
Lars-Peter Clausen | fdaf9c4 | 2012-04-25 20:50:52 +0200 | [diff] [blame] | 568 | ccw->xfer_bytes = sg_dma_len(sg); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 569 | |
| 570 | ccw->bits = 0; |
| 571 | ccw->bits |= CCW_CHAIN; |
| 572 | ccw->bits |= CCW_HALT_ON_TERM; |
| 573 | ccw->bits |= CCW_TERM_FLUSH; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 574 | ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ? |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 575 | MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, |
| 576 | COMMAND); |
| 577 | |
| 578 | if (i + 1 == sg_len) { |
| 579 | ccw->bits &= ~CCW_CHAIN; |
| 580 | ccw->bits |= CCW_IRQ; |
| 581 | ccw->bits |= CCW_DEC_SEM; |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 582 | if (flags & DMA_CTRL_ACK) |
| 583 | ccw->bits |= CCW_WAIT4END; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | } |
Lothar Waßmann | 6d23ea4 | 2011-12-08 09:15:43 +0100 | [diff] [blame] | 587 | mxs_chan->desc_count = idx; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 588 | |
| 589 | return &mxs_chan->desc; |
| 590 | |
| 591 | err_out: |
| 592 | mxs_chan->status = DMA_ERROR; |
| 593 | return NULL; |
| 594 | } |
| 595 | |
| 596 | static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic( |
| 597 | struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len, |
Alexandre Bounine | 185ecb5 | 2012-03-08 15:35:13 -0500 | [diff] [blame] | 598 | size_t period_len, enum dma_transfer_direction direction, |
Laurent Pinchart | 31c1e5a | 2014-08-01 12:20:10 +0200 | [diff] [blame] | 599 | unsigned long flags) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 600 | { |
| 601 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
| 602 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
Fabio Estevam | f2ad699 | 2013-01-07 23:48:39 -0200 | [diff] [blame] | 603 | u32 num_periods = buf_len / period_len; |
| 604 | u32 i = 0, buf = 0; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 605 | |
| 606 | if (mxs_chan->status == DMA_IN_PROGRESS) |
| 607 | return NULL; |
| 608 | |
| 609 | mxs_chan->status = DMA_IN_PROGRESS; |
| 610 | mxs_chan->flags |= MXS_DMA_SG_LOOP; |
Markus Pargmann | 2dcbdce | 2013-10-29 08:47:49 +0100 | [diff] [blame] | 611 | mxs_chan->flags |= MXS_DMA_USE_SEMAPHORE; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 612 | |
| 613 | if (num_periods > NUM_CCW) { |
| 614 | dev_err(mxs_dma->dma_device.dev, |
| 615 | "maximum number of sg exceeded: %d > %d\n", |
| 616 | num_periods, NUM_CCW); |
| 617 | goto err_out; |
| 618 | } |
| 619 | |
| 620 | if (period_len > MAX_XFER_BYTES) { |
| 621 | dev_err(mxs_dma->dma_device.dev, |
| 622 | "maximum period size exceeded: %d > %d\n", |
| 623 | period_len, MAX_XFER_BYTES); |
| 624 | goto err_out; |
| 625 | } |
| 626 | |
| 627 | while (buf < buf_len) { |
| 628 | struct mxs_dma_ccw *ccw = &mxs_chan->ccw[i]; |
| 629 | |
| 630 | if (i + 1 == num_periods) |
| 631 | ccw->next = mxs_chan->ccw_phys; |
| 632 | else |
| 633 | ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * (i + 1); |
| 634 | |
| 635 | ccw->bufaddr = dma_addr; |
| 636 | ccw->xfer_bytes = period_len; |
| 637 | |
| 638 | ccw->bits = 0; |
| 639 | ccw->bits |= CCW_CHAIN; |
| 640 | ccw->bits |= CCW_IRQ; |
| 641 | ccw->bits |= CCW_HALT_ON_TERM; |
| 642 | ccw->bits |= CCW_TERM_FLUSH; |
Markus Pargmann | 2dcbdce | 2013-10-29 08:47:49 +0100 | [diff] [blame] | 643 | ccw->bits |= CCW_DEC_SEM; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 644 | ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ? |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 645 | MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, COMMAND); |
| 646 | |
| 647 | dma_addr += period_len; |
| 648 | buf += period_len; |
| 649 | |
| 650 | i++; |
| 651 | } |
Lothar Waßmann | 6d23ea4 | 2011-12-08 09:15:43 +0100 | [diff] [blame] | 652 | mxs_chan->desc_count = i; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 653 | |
| 654 | return &mxs_chan->desc; |
| 655 | |
| 656 | err_out: |
| 657 | mxs_chan->status = DMA_ERROR; |
| 658 | return NULL; |
| 659 | } |
| 660 | |
Maxime Ripard | 5c9d2e3 | 2014-11-17 14:42:26 +0100 | [diff] [blame] | 661 | static int mxs_dma_terminate_all(struct dma_chan *chan) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 662 | { |
Maxime Ripard | 5c9d2e3 | 2014-11-17 14:42:26 +0100 | [diff] [blame] | 663 | mxs_dma_reset_chan(chan); |
| 664 | mxs_dma_disable_chan(chan); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 665 | |
Maxime Ripard | 5c9d2e3 | 2014-11-17 14:42:26 +0100 | [diff] [blame] | 666 | return 0; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | static enum dma_status mxs_dma_tx_status(struct dma_chan *chan, |
| 670 | dma_cookie_t cookie, struct dma_tx_state *txstate) |
| 671 | { |
| 672 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
Markus Pargmann | 7b11304 | 2013-10-29 08:47:46 +0100 | [diff] [blame] | 673 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
| 674 | u32 residue = 0; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 675 | |
Markus Pargmann | 7b11304 | 2013-10-29 08:47:46 +0100 | [diff] [blame] | 676 | if (mxs_chan->status == DMA_IN_PROGRESS && |
| 677 | mxs_chan->flags & MXS_DMA_SG_LOOP) { |
| 678 | struct mxs_dma_ccw *last_ccw; |
| 679 | u32 bar; |
| 680 | |
| 681 | last_ccw = &mxs_chan->ccw[mxs_chan->desc_count - 1]; |
| 682 | residue = last_ccw->xfer_bytes + last_ccw->bufaddr; |
| 683 | |
| 684 | bar = readl(mxs_dma->base + |
| 685 | HW_APBHX_CHn_BAR(mxs_dma, chan->chan_id)); |
| 686 | residue -= bar; |
| 687 | } |
| 688 | |
| 689 | dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie, |
| 690 | residue); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 691 | |
| 692 | return mxs_chan->status; |
| 693 | } |
| 694 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 695 | static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma) |
| 696 | { |
| 697 | int ret; |
| 698 | |
Shawn Guo | 759a2e3 | 2011-12-20 13:54:00 +0800 | [diff] [blame] | 699 | ret = clk_prepare_enable(mxs_dma->clk); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 700 | if (ret) |
Lothar Waßmann | feb397d | 2011-12-08 09:15:42 +0100 | [diff] [blame] | 701 | return ret; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 702 | |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 703 | ret = stmp_reset_block(mxs_dma->base); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 704 | if (ret) |
| 705 | goto err_out; |
| 706 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 707 | /* enable apbh burst */ |
Shawn Guo | bb11fb6 | 2012-05-07 14:14:08 +0800 | [diff] [blame] | 708 | if (dma_is_apbh(mxs_dma)) { |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 709 | writel(BM_APBH_CTRL0_APB_BURST_EN, |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 710 | mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 711 | writel(BM_APBH_CTRL0_APB_BURST8_EN, |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 712 | mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* enable irq for all the channels */ |
| 716 | writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS, |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 717 | mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_SET); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 718 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 719 | err_out: |
Linus Torvalds | 57f2685 | 2012-01-17 18:40:24 -0800 | [diff] [blame] | 720 | clk_disable_unprepare(mxs_dma->clk); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 721 | return ret; |
| 722 | } |
| 723 | |
Shawn Guo | d84f638 | 2013-02-26 09:42:09 +0800 | [diff] [blame] | 724 | struct mxs_dma_filter_param { |
| 725 | struct device_node *of_node; |
| 726 | unsigned int chan_id; |
| 727 | }; |
| 728 | |
| 729 | static bool mxs_dma_filter_fn(struct dma_chan *chan, void *fn_param) |
| 730 | { |
| 731 | struct mxs_dma_filter_param *param = fn_param; |
| 732 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
| 733 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
| 734 | int chan_irq; |
| 735 | |
| 736 | if (mxs_dma->dma_device.dev->of_node != param->of_node) |
| 737 | return false; |
| 738 | |
| 739 | if (chan->chan_id != param->chan_id) |
| 740 | return false; |
| 741 | |
| 742 | chan_irq = platform_get_irq(mxs_dma->pdev, param->chan_id); |
| 743 | if (chan_irq < 0) |
| 744 | return false; |
| 745 | |
| 746 | mxs_chan->chan_irq = chan_irq; |
| 747 | |
| 748 | return true; |
| 749 | } |
| 750 | |
Fabio Estevam | 3208b37 | 2013-05-24 16:37:27 -0300 | [diff] [blame] | 751 | static struct dma_chan *mxs_dma_xlate(struct of_phandle_args *dma_spec, |
Shawn Guo | d84f638 | 2013-02-26 09:42:09 +0800 | [diff] [blame] | 752 | struct of_dma *ofdma) |
| 753 | { |
| 754 | struct mxs_dma_engine *mxs_dma = ofdma->of_dma_data; |
| 755 | dma_cap_mask_t mask = mxs_dma->dma_device.cap_mask; |
| 756 | struct mxs_dma_filter_param param; |
| 757 | |
| 758 | if (dma_spec->args_count != 1) |
| 759 | return NULL; |
| 760 | |
| 761 | param.of_node = ofdma->of_node; |
| 762 | param.chan_id = dma_spec->args[0]; |
| 763 | |
| 764 | if (param.chan_id >= mxs_dma->nr_channels) |
| 765 | return NULL; |
| 766 | |
| 767 | return dma_request_channel(mask, mxs_dma_filter_fn, ¶m); |
| 768 | } |
| 769 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 770 | static int __init mxs_dma_probe(struct platform_device *pdev) |
| 771 | { |
Shawn Guo | d84f638 | 2013-02-26 09:42:09 +0800 | [diff] [blame] | 772 | struct device_node *np = pdev->dev.of_node; |
Dong Aisheng | 90c9abc | 2012-05-04 20:12:17 +0800 | [diff] [blame] | 773 | const struct platform_device_id *id_entry; |
| 774 | const struct of_device_id *of_id; |
| 775 | const struct mxs_dma_type *dma_type; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 776 | struct mxs_dma_engine *mxs_dma; |
| 777 | struct resource *iores; |
| 778 | int ret, i; |
| 779 | |
Shawn Guo | aaa2051 | 2013-02-25 14:57:26 +0800 | [diff] [blame] | 780 | mxs_dma = devm_kzalloc(&pdev->dev, sizeof(*mxs_dma), GFP_KERNEL); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 781 | if (!mxs_dma) |
| 782 | return -ENOMEM; |
| 783 | |
Shawn Guo | d84f638 | 2013-02-26 09:42:09 +0800 | [diff] [blame] | 784 | ret = of_property_read_u32(np, "dma-channels", &mxs_dma->nr_channels); |
| 785 | if (ret) { |
| 786 | dev_err(&pdev->dev, "failed to read dma-channels\n"); |
| 787 | return ret; |
| 788 | } |
| 789 | |
Dong Aisheng | 90c9abc | 2012-05-04 20:12:17 +0800 | [diff] [blame] | 790 | of_id = of_match_device(mxs_dma_dt_ids, &pdev->dev); |
| 791 | if (of_id) |
| 792 | id_entry = of_id->data; |
| 793 | else |
| 794 | id_entry = platform_get_device_id(pdev); |
| 795 | |
| 796 | dma_type = (struct mxs_dma_type *)id_entry->driver_data; |
Shawn Guo | 8c92013 | 2012-05-10 06:23:26 +0800 | [diff] [blame] | 797 | mxs_dma->type = dma_type->type; |
Dong Aisheng | 90c9abc | 2012-05-04 20:12:17 +0800 | [diff] [blame] | 798 | mxs_dma->dev_id = dma_type->id; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 799 | |
| 800 | iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Shawn Guo | aaa2051 | 2013-02-25 14:57:26 +0800 | [diff] [blame] | 801 | mxs_dma->base = devm_ioremap_resource(&pdev->dev, iores); |
| 802 | if (IS_ERR(mxs_dma->base)) |
| 803 | return PTR_ERR(mxs_dma->base); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 804 | |
Shawn Guo | aaa2051 | 2013-02-25 14:57:26 +0800 | [diff] [blame] | 805 | mxs_dma->clk = devm_clk_get(&pdev->dev, NULL); |
| 806 | if (IS_ERR(mxs_dma->clk)) |
| 807 | return PTR_ERR(mxs_dma->clk); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 808 | |
| 809 | dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask); |
| 810 | dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask); |
| 811 | |
| 812 | INIT_LIST_HEAD(&mxs_dma->dma_device.channels); |
| 813 | |
| 814 | /* Initialize channel parameters */ |
| 815 | for (i = 0; i < MXS_DMA_CHANNELS; i++) { |
| 816 | struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i]; |
| 817 | |
| 818 | mxs_chan->mxs_dma = mxs_dma; |
| 819 | mxs_chan->chan.device = &mxs_dma->dma_device; |
Russell King - ARM Linux | 8ac6954 | 2012-03-06 22:36:27 +0000 | [diff] [blame] | 820 | dma_cookie_init(&mxs_chan->chan); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 821 | |
| 822 | tasklet_init(&mxs_chan->tasklet, mxs_dma_tasklet, |
| 823 | (unsigned long) mxs_chan); |
| 824 | |
| 825 | |
| 826 | /* Add the channel to mxs_chan list */ |
| 827 | list_add_tail(&mxs_chan->chan.device_node, |
| 828 | &mxs_dma->dma_device.channels); |
| 829 | } |
| 830 | |
| 831 | ret = mxs_dma_init(mxs_dma); |
| 832 | if (ret) |
Shawn Guo | aaa2051 | 2013-02-25 14:57:26 +0800 | [diff] [blame] | 833 | return ret; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 834 | |
Shawn Guo | d84f638 | 2013-02-26 09:42:09 +0800 | [diff] [blame] | 835 | mxs_dma->pdev = pdev; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 836 | mxs_dma->dma_device.dev = &pdev->dev; |
| 837 | |
| 838 | /* mxs_dma gets 65535 bytes maximum sg size */ |
| 839 | mxs_dma->dma_device.dev->dma_parms = &mxs_dma->dma_parms; |
| 840 | dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES); |
| 841 | |
| 842 | mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources; |
| 843 | mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources; |
| 844 | mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status; |
| 845 | mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg; |
| 846 | mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic; |
Maxime Ripard | 5c9d2e3 | 2014-11-17 14:42:26 +0100 | [diff] [blame] | 847 | mxs_dma->dma_device.device_pause = mxs_dma_pause_chan; |
| 848 | mxs_dma->dma_device.device_resume = mxs_dma_resume_chan; |
| 849 | mxs_dma->dma_device.device_terminate_all = mxs_dma_terminate_all; |
Fabio Estevam | ef9d2a9 | 2014-12-29 15:21:19 -0200 | [diff] [blame] | 850 | mxs_dma->dma_device.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); |
| 851 | mxs_dma->dma_device.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); |
| 852 | mxs_dma->dma_device.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV); |
| 853 | mxs_dma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST; |
Maxime Ripard | 5c9d2e3 | 2014-11-17 14:42:26 +0100 | [diff] [blame] | 854 | mxs_dma->dma_device.device_issue_pending = mxs_dma_enable_chan; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 855 | |
| 856 | ret = dma_async_device_register(&mxs_dma->dma_device); |
| 857 | if (ret) { |
| 858 | dev_err(mxs_dma->dma_device.dev, "unable to register\n"); |
Shawn Guo | aaa2051 | 2013-02-25 14:57:26 +0800 | [diff] [blame] | 859 | return ret; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 860 | } |
| 861 | |
Shawn Guo | d84f638 | 2013-02-26 09:42:09 +0800 | [diff] [blame] | 862 | ret = of_dma_controller_register(np, mxs_dma_xlate, mxs_dma); |
| 863 | if (ret) { |
| 864 | dev_err(mxs_dma->dma_device.dev, |
| 865 | "failed to register controller\n"); |
| 866 | dma_async_device_unregister(&mxs_dma->dma_device); |
| 867 | } |
| 868 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 869 | dev_info(mxs_dma->dma_device.dev, "initialized\n"); |
| 870 | |
| 871 | return 0; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 872 | } |
| 873 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 874 | static struct platform_driver mxs_dma_driver = { |
| 875 | .driver = { |
| 876 | .name = "mxs-dma", |
Dong Aisheng | 90c9abc | 2012-05-04 20:12:17 +0800 | [diff] [blame] | 877 | .of_match_table = mxs_dma_dt_ids, |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 878 | }, |
Shawn Guo | 8c92013 | 2012-05-10 06:23:26 +0800 | [diff] [blame] | 879 | .id_table = mxs_dma_ids, |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 880 | }; |
| 881 | |
| 882 | static int __init mxs_dma_module_init(void) |
| 883 | { |
| 884 | return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe); |
| 885 | } |
| 886 | subsys_initcall(mxs_dma_module_init); |