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; |
| 122 | #define MXS_DMA_SG_LOOP (1 << 0) |
| 123 | }; |
| 124 | |
| 125 | #define MXS_DMA_CHANNELS 16 |
| 126 | #define MXS_DMA_CHANNELS_MASK 0xffff |
| 127 | |
Shawn Guo | 8c92013 | 2012-05-10 06:23:26 +0800 | [diff] [blame] | 128 | enum mxs_dma_devtype { |
| 129 | MXS_DMA_APBH, |
| 130 | MXS_DMA_APBX, |
| 131 | }; |
| 132 | |
| 133 | enum mxs_dma_id { |
| 134 | IMX23_DMA, |
| 135 | IMX28_DMA, |
| 136 | }; |
| 137 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 138 | struct mxs_dma_engine { |
Shawn Guo | 8c92013 | 2012-05-10 06:23:26 +0800 | [diff] [blame] | 139 | enum mxs_dma_id dev_id; |
| 140 | enum mxs_dma_devtype type; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 141 | void __iomem *base; |
| 142 | struct clk *clk; |
| 143 | struct dma_device dma_device; |
| 144 | struct device_dma_parameters dma_parms; |
| 145 | struct mxs_dma_chan mxs_chans[MXS_DMA_CHANNELS]; |
Shawn Guo | d84f638 | 2013-02-26 09:42:09 +0800 | [diff] [blame] | 146 | struct platform_device *pdev; |
| 147 | unsigned int nr_channels; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 148 | }; |
| 149 | |
Shawn Guo | 8c92013 | 2012-05-10 06:23:26 +0800 | [diff] [blame] | 150 | struct mxs_dma_type { |
| 151 | enum mxs_dma_id id; |
| 152 | enum mxs_dma_devtype type; |
| 153 | }; |
| 154 | |
| 155 | static struct mxs_dma_type mxs_dma_types[] = { |
| 156 | { |
| 157 | .id = IMX23_DMA, |
| 158 | .type = MXS_DMA_APBH, |
| 159 | }, { |
| 160 | .id = IMX23_DMA, |
| 161 | .type = MXS_DMA_APBX, |
| 162 | }, { |
| 163 | .id = IMX28_DMA, |
| 164 | .type = MXS_DMA_APBH, |
| 165 | }, { |
| 166 | .id = IMX28_DMA, |
| 167 | .type = MXS_DMA_APBX, |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | static struct platform_device_id mxs_dma_ids[] = { |
| 172 | { |
| 173 | .name = "imx23-dma-apbh", |
| 174 | .driver_data = (kernel_ulong_t) &mxs_dma_types[0], |
| 175 | }, { |
| 176 | .name = "imx23-dma-apbx", |
| 177 | .driver_data = (kernel_ulong_t) &mxs_dma_types[1], |
| 178 | }, { |
| 179 | .name = "imx28-dma-apbh", |
| 180 | .driver_data = (kernel_ulong_t) &mxs_dma_types[2], |
| 181 | }, { |
| 182 | .name = "imx28-dma-apbx", |
| 183 | .driver_data = (kernel_ulong_t) &mxs_dma_types[3], |
| 184 | }, { |
| 185 | /* end of list */ |
| 186 | } |
| 187 | }; |
| 188 | |
Dong Aisheng | 90c9abc | 2012-05-04 20:12:17 +0800 | [diff] [blame] | 189 | static const struct of_device_id mxs_dma_dt_ids[] = { |
| 190 | { .compatible = "fsl,imx23-dma-apbh", .data = &mxs_dma_ids[0], }, |
| 191 | { .compatible = "fsl,imx23-dma-apbx", .data = &mxs_dma_ids[1], }, |
| 192 | { .compatible = "fsl,imx28-dma-apbh", .data = &mxs_dma_ids[2], }, |
| 193 | { .compatible = "fsl,imx28-dma-apbx", .data = &mxs_dma_ids[3], }, |
| 194 | { /* sentinel */ } |
| 195 | }; |
| 196 | MODULE_DEVICE_TABLE(of, mxs_dma_dt_ids); |
| 197 | |
Shawn Guo | 8c92013 | 2012-05-10 06:23:26 +0800 | [diff] [blame] | 198 | static struct mxs_dma_chan *to_mxs_dma_chan(struct dma_chan *chan) |
| 199 | { |
| 200 | return container_of(chan, struct mxs_dma_chan, chan); |
| 201 | } |
| 202 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 203 | static void mxs_dma_reset_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 | |
Markus Pargmann | 702e94d | 2013-10-29 08:47:47 +0100 | [diff] [blame^] | 208 | if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma)) { |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 209 | writel(1 << (chan_id + BP_APBH_CTRL0_RESET_CHANNEL), |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 210 | mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET); |
Markus Pargmann | 702e94d | 2013-10-29 08:47:47 +0100 | [diff] [blame^] | 211 | } else { |
| 212 | unsigned long elapsed = 0; |
| 213 | const unsigned long max_wait = 50000; /* 50ms */ |
| 214 | void __iomem *reg_dbg1 = mxs_dma->base + |
| 215 | HW_APBX_CHn_DEBUG1(mxs_dma, chan_id); |
| 216 | |
| 217 | /* |
| 218 | * On i.MX28 APBX, the DMA channel can stop working if we reset |
| 219 | * the channel while it is in READ_FLUSH (0x08) state. |
| 220 | * We wait here until we leave the state. Then we trigger the |
| 221 | * reset. Waiting a maximum of 50ms, the kernel shouldn't crash |
| 222 | * because of this. |
| 223 | */ |
| 224 | while ((readl(reg_dbg1) & 0xf) == 0x8 && elapsed < max_wait) { |
| 225 | udelay(100); |
| 226 | elapsed += 100; |
| 227 | } |
| 228 | |
| 229 | if (elapsed >= max_wait) |
| 230 | dev_err(&mxs_chan->mxs_dma->pdev->dev, |
| 231 | "Failed waiting for the DMA channel %d to leave state READ_FLUSH, trying to reset channel in READ_FLUSH state now\n", |
| 232 | chan_id); |
| 233 | |
| 234 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 235 | writel(1 << (chan_id + BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL), |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 236 | mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET); |
Markus Pargmann | 702e94d | 2013-10-29 08:47:47 +0100 | [diff] [blame^] | 237 | } |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static void mxs_dma_enable_chan(struct mxs_dma_chan *mxs_chan) |
| 241 | { |
| 242 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
| 243 | int chan_id = mxs_chan->chan.chan_id; |
| 244 | |
| 245 | /* set cmd_addr up */ |
| 246 | writel(mxs_chan->ccw_phys, |
Shawn Guo | bb11fb6 | 2012-05-07 14:14:08 +0800 | [diff] [blame] | 247 | mxs_dma->base + HW_APBHX_CHn_NXTCMDAR(mxs_dma, chan_id)); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 248 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 249 | /* write 1 to SEMA to kick off the channel */ |
Shawn Guo | bb11fb6 | 2012-05-07 14:14:08 +0800 | [diff] [blame] | 250 | writel(1, mxs_dma->base + HW_APBHX_CHn_SEMA(mxs_dma, chan_id)); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static void mxs_dma_disable_chan(struct mxs_dma_chan *mxs_chan) |
| 254 | { |
Vinod Koul | 2737583 | 2013-10-16 20:51:30 +0530 | [diff] [blame] | 255 | mxs_chan->status = DMA_COMPLETE; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static void mxs_dma_pause_chan(struct mxs_dma_chan *mxs_chan) |
| 259 | { |
| 260 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
| 261 | int chan_id = mxs_chan->chan.chan_id; |
| 262 | |
| 263 | /* freeze the channel */ |
Shawn Guo | bb11fb6 | 2012-05-07 14:14:08 +0800 | [diff] [blame] | 264 | if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma)) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 265 | writel(1 << chan_id, |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 266 | mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 267 | else |
| 268 | writel(1 << chan_id, |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 269 | mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 270 | |
| 271 | mxs_chan->status = DMA_PAUSED; |
| 272 | } |
| 273 | |
| 274 | static void mxs_dma_resume_chan(struct mxs_dma_chan *mxs_chan) |
| 275 | { |
| 276 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
| 277 | int chan_id = mxs_chan->chan.chan_id; |
| 278 | |
| 279 | /* unfreeze the channel */ |
Shawn Guo | bb11fb6 | 2012-05-07 14:14:08 +0800 | [diff] [blame] | 280 | if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma)) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 281 | writel(1 << chan_id, |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 282 | mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_CLR); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 283 | else |
| 284 | writel(1 << chan_id, |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 285 | mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_CLR); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 286 | |
| 287 | mxs_chan->status = DMA_IN_PROGRESS; |
| 288 | } |
| 289 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 290 | static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx) |
| 291 | { |
Russell King - ARM Linux | 884485e | 2012-03-06 22:34:46 +0000 | [diff] [blame] | 292 | return dma_cookie_assign(tx); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static void mxs_dma_tasklet(unsigned long data) |
| 296 | { |
| 297 | struct mxs_dma_chan *mxs_chan = (struct mxs_dma_chan *) data; |
| 298 | |
| 299 | if (mxs_chan->desc.callback) |
| 300 | mxs_chan->desc.callback(mxs_chan->desc.callback_param); |
| 301 | } |
| 302 | |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 303 | static int mxs_dma_irq_to_chan(struct mxs_dma_engine *mxs_dma, int irq) |
| 304 | { |
| 305 | int i; |
| 306 | |
| 307 | for (i = 0; i != mxs_dma->nr_channels; ++i) |
| 308 | if (mxs_dma->mxs_chans[i].chan_irq == irq) |
| 309 | return i; |
| 310 | |
| 311 | return -EINVAL; |
| 312 | } |
| 313 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 314 | static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id) |
| 315 | { |
| 316 | struct mxs_dma_engine *mxs_dma = dev_id; |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 317 | struct mxs_dma_chan *mxs_chan; |
| 318 | u32 completed; |
| 319 | u32 err; |
| 320 | int chan = mxs_dma_irq_to_chan(mxs_dma, irq); |
| 321 | |
| 322 | if (chan < 0) |
| 323 | return IRQ_NONE; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 324 | |
| 325 | /* completion status */ |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 326 | completed = readl(mxs_dma->base + HW_APBHX_CTRL1); |
| 327 | completed = (completed >> chan) & 0x1; |
| 328 | |
| 329 | /* Clear interrupt */ |
| 330 | writel((1 << chan), |
| 331 | mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_CLR); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 332 | |
| 333 | /* error status */ |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 334 | err = readl(mxs_dma->base + HW_APBHX_CTRL2); |
| 335 | err &= (1 << (MXS_DMA_CHANNELS + chan)) | (1 << chan); |
| 336 | |
| 337 | /* |
| 338 | * error status bit is in the upper 16 bits, error irq bit in the lower |
| 339 | * 16 bits. We transform it into a simpler error code: |
| 340 | * err: 0x00 = no error, 0x01 = TERMINATION, 0x02 = BUS_ERROR |
| 341 | */ |
| 342 | err = (err >> (MXS_DMA_CHANNELS + chan)) + (err >> chan); |
| 343 | |
| 344 | /* Clear error irq */ |
| 345 | writel((1 << chan), |
| 346 | mxs_dma->base + HW_APBHX_CTRL2 + STMP_OFFSET_REG_CLR); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 347 | |
| 348 | /* |
| 349 | * When both completion and error of termination bits set at the |
| 350 | * 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] | 351 | * an error we need to handle here in case of either it's a bus |
| 352 | * error or a termination error with no completion. 0x01 is termination |
| 353 | * 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] | 354 | */ |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 355 | err -= err & completed; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 356 | |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 357 | mxs_chan = &mxs_dma->mxs_chans[chan]; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 358 | |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 359 | if (err) { |
| 360 | dev_dbg(mxs_dma->dma_device.dev, |
| 361 | "%s: error in channel %d\n", __func__, |
| 362 | chan); |
| 363 | mxs_chan->status = DMA_ERROR; |
| 364 | mxs_dma_reset_chan(mxs_chan); |
| 365 | } else { |
| 366 | if (mxs_chan->flags & MXS_DMA_SG_LOOP) |
| 367 | mxs_chan->status = DMA_IN_PROGRESS; |
| 368 | else |
| 369 | mxs_chan->status = DMA_COMPLETE; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 370 | } |
| 371 | |
Markus Pargmann | b2d6398 | 2013-10-29 08:47:45 +0100 | [diff] [blame] | 372 | if (mxs_chan->status == DMA_COMPLETE) |
| 373 | dma_cookie_complete(&mxs_chan->desc); |
| 374 | |
| 375 | /* schedule tasklet on this channel */ |
| 376 | tasklet_schedule(&mxs_chan->tasklet); |
| 377 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 378 | return IRQ_HANDLED; |
| 379 | } |
| 380 | |
| 381 | static int mxs_dma_alloc_chan_resources(struct dma_chan *chan) |
| 382 | { |
| 383 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 384 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
| 385 | int ret; |
| 386 | |
Marek Vasut | 5e97fa9 | 2012-09-04 06:04:25 +0200 | [diff] [blame] | 387 | mxs_chan->ccw = dma_alloc_coherent(mxs_dma->dma_device.dev, |
| 388 | CCW_BLOCK_SIZE, &mxs_chan->ccw_phys, |
| 389 | GFP_KERNEL); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 390 | if (!mxs_chan->ccw) { |
| 391 | ret = -ENOMEM; |
| 392 | goto err_alloc; |
| 393 | } |
| 394 | |
Marek Vasut | 5e97fa9 | 2012-09-04 06:04:25 +0200 | [diff] [blame] | 395 | memset(mxs_chan->ccw, 0, CCW_BLOCK_SIZE); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 396 | |
Shawn Guo | 95bfea1 | 2011-06-30 16:06:33 +0800 | [diff] [blame] | 397 | if (mxs_chan->chan_irq != NO_IRQ) { |
| 398 | ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler, |
| 399 | 0, "mxs-dma", mxs_dma); |
| 400 | if (ret) |
| 401 | goto err_irq; |
| 402 | } |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 403 | |
Shawn Guo | 759a2e3 | 2011-12-20 13:54:00 +0800 | [diff] [blame] | 404 | ret = clk_prepare_enable(mxs_dma->clk); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 405 | if (ret) |
| 406 | goto err_clk; |
| 407 | |
| 408 | mxs_dma_reset_chan(mxs_chan); |
| 409 | |
| 410 | dma_async_tx_descriptor_init(&mxs_chan->desc, chan); |
| 411 | mxs_chan->desc.tx_submit = mxs_dma_tx_submit; |
| 412 | |
| 413 | /* the descriptor is ready */ |
| 414 | async_tx_ack(&mxs_chan->desc); |
| 415 | |
| 416 | return 0; |
| 417 | |
| 418 | err_clk: |
| 419 | free_irq(mxs_chan->chan_irq, mxs_dma); |
| 420 | err_irq: |
Marek Vasut | 5e97fa9 | 2012-09-04 06:04:25 +0200 | [diff] [blame] | 421 | dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE, |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 422 | mxs_chan->ccw, mxs_chan->ccw_phys); |
| 423 | err_alloc: |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | static void mxs_dma_free_chan_resources(struct dma_chan *chan) |
| 428 | { |
| 429 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
| 430 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
| 431 | |
| 432 | mxs_dma_disable_chan(mxs_chan); |
| 433 | |
| 434 | free_irq(mxs_chan->chan_irq, mxs_dma); |
| 435 | |
Marek Vasut | 5e97fa9 | 2012-09-04 06:04:25 +0200 | [diff] [blame] | 436 | dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE, |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 437 | mxs_chan->ccw, mxs_chan->ccw_phys); |
| 438 | |
Shawn Guo | 759a2e3 | 2011-12-20 13:54:00 +0800 | [diff] [blame] | 439 | clk_disable_unprepare(mxs_dma->clk); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 440 | } |
| 441 | |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 442 | /* |
| 443 | * How to use the flags for ->device_prep_slave_sg() : |
| 444 | * [1] If there is only one DMA command in the DMA chain, the code should be: |
| 445 | * ...... |
| 446 | * ->device_prep_slave_sg(DMA_CTRL_ACK); |
| 447 | * ...... |
| 448 | * [2] If there are two DMA commands in the DMA chain, the code should be |
| 449 | * ...... |
| 450 | * ->device_prep_slave_sg(0); |
| 451 | * ...... |
| 452 | * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 453 | * ...... |
| 454 | * [3] If there are more than two DMA commands in the DMA chain, the code |
| 455 | * should be: |
| 456 | * ...... |
| 457 | * ->device_prep_slave_sg(0); // First |
| 458 | * ...... |
| 459 | * ->device_prep_slave_sg(DMA_PREP_INTERRUPT [| DMA_CTRL_ACK]); |
| 460 | * ...... |
| 461 | * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK); // Last |
| 462 | * ...... |
| 463 | */ |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 464 | static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg( |
| 465 | struct dma_chan *chan, struct scatterlist *sgl, |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 466 | unsigned int sg_len, enum dma_transfer_direction direction, |
Linus Torvalds | 623ff77 | 2012-03-30 17:31:56 -0700 | [diff] [blame] | 467 | unsigned long flags, void *context) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 468 | { |
| 469 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
| 470 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
| 471 | struct mxs_dma_ccw *ccw; |
| 472 | struct scatterlist *sg; |
Fabio Estevam | f2ad699 | 2013-01-07 23:48:39 -0200 | [diff] [blame] | 473 | u32 i, j; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 474 | u32 *pio; |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 475 | bool append = flags & DMA_PREP_INTERRUPT; |
Lothar Waßmann | 6d23ea4 | 2011-12-08 09:15:43 +0100 | [diff] [blame] | 476 | int idx = append ? mxs_chan->desc_count : 0; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 477 | |
| 478 | if (mxs_chan->status == DMA_IN_PROGRESS && !append) |
| 479 | return NULL; |
| 480 | |
| 481 | if (sg_len + (append ? idx : 0) > NUM_CCW) { |
| 482 | dev_err(mxs_dma->dma_device.dev, |
| 483 | "maximum number of sg exceeded: %d > %d\n", |
| 484 | sg_len, NUM_CCW); |
| 485 | goto err_out; |
| 486 | } |
| 487 | |
| 488 | mxs_chan->status = DMA_IN_PROGRESS; |
| 489 | mxs_chan->flags = 0; |
| 490 | |
| 491 | /* |
| 492 | * If the sg is prepared with append flag set, the sg |
| 493 | * will be appended to the last prepared sg. |
| 494 | */ |
| 495 | if (append) { |
| 496 | BUG_ON(idx < 1); |
| 497 | ccw = &mxs_chan->ccw[idx - 1]; |
| 498 | ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx; |
| 499 | ccw->bits |= CCW_CHAIN; |
| 500 | ccw->bits &= ~CCW_IRQ; |
| 501 | ccw->bits &= ~CCW_DEC_SEM; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 502 | } else { |
| 503 | idx = 0; |
| 504 | } |
| 505 | |
Shawn Guo | 62268ce | 2011-12-13 23:48:03 +0800 | [diff] [blame] | 506 | if (direction == DMA_TRANS_NONE) { |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 507 | ccw = &mxs_chan->ccw[idx++]; |
| 508 | pio = (u32 *) sgl; |
| 509 | |
| 510 | for (j = 0; j < sg_len;) |
| 511 | ccw->pio_words[j++] = *pio++; |
| 512 | |
| 513 | ccw->bits = 0; |
| 514 | ccw->bits |= CCW_IRQ; |
| 515 | ccw->bits |= CCW_DEC_SEM; |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 516 | if (flags & DMA_CTRL_ACK) |
| 517 | ccw->bits |= CCW_WAIT4END; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 518 | ccw->bits |= CCW_HALT_ON_TERM; |
| 519 | ccw->bits |= CCW_TERM_FLUSH; |
| 520 | ccw->bits |= BF_CCW(sg_len, PIO_NUM); |
| 521 | ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND); |
| 522 | } else { |
| 523 | for_each_sg(sgl, sg, sg_len, i) { |
Lars-Peter Clausen | fdaf9c4 | 2012-04-25 20:50:52 +0200 | [diff] [blame] | 524 | if (sg_dma_len(sg) > MAX_XFER_BYTES) { |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 525 | 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] | 526 | sg_dma_len(sg), MAX_XFER_BYTES); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 527 | goto err_out; |
| 528 | } |
| 529 | |
| 530 | ccw = &mxs_chan->ccw[idx++]; |
| 531 | |
| 532 | ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx; |
| 533 | ccw->bufaddr = sg->dma_address; |
Lars-Peter Clausen | fdaf9c4 | 2012-04-25 20:50:52 +0200 | [diff] [blame] | 534 | ccw->xfer_bytes = sg_dma_len(sg); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 535 | |
| 536 | ccw->bits = 0; |
| 537 | ccw->bits |= CCW_CHAIN; |
| 538 | ccw->bits |= CCW_HALT_ON_TERM; |
| 539 | ccw->bits |= CCW_TERM_FLUSH; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 540 | ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ? |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 541 | MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, |
| 542 | COMMAND); |
| 543 | |
| 544 | if (i + 1 == sg_len) { |
| 545 | ccw->bits &= ~CCW_CHAIN; |
| 546 | ccw->bits |= CCW_IRQ; |
| 547 | ccw->bits |= CCW_DEC_SEM; |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 548 | if (flags & DMA_CTRL_ACK) |
| 549 | ccw->bits |= CCW_WAIT4END; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | } |
Lothar Waßmann | 6d23ea4 | 2011-12-08 09:15:43 +0100 | [diff] [blame] | 553 | mxs_chan->desc_count = idx; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 554 | |
| 555 | return &mxs_chan->desc; |
| 556 | |
| 557 | err_out: |
| 558 | mxs_chan->status = DMA_ERROR; |
| 559 | return NULL; |
| 560 | } |
| 561 | |
| 562 | static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic( |
| 563 | 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] | 564 | size_t period_len, enum dma_transfer_direction direction, |
Peter Ujfalusi | ec8b5e4 | 2012-09-14 15:05:47 +0300 | [diff] [blame] | 565 | unsigned long flags, void *context) |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 566 | { |
| 567 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
| 568 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
Fabio Estevam | f2ad699 | 2013-01-07 23:48:39 -0200 | [diff] [blame] | 569 | u32 num_periods = buf_len / period_len; |
| 570 | u32 i = 0, buf = 0; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 571 | |
| 572 | if (mxs_chan->status == DMA_IN_PROGRESS) |
| 573 | return NULL; |
| 574 | |
| 575 | mxs_chan->status = DMA_IN_PROGRESS; |
| 576 | mxs_chan->flags |= MXS_DMA_SG_LOOP; |
| 577 | |
| 578 | if (num_periods > NUM_CCW) { |
| 579 | dev_err(mxs_dma->dma_device.dev, |
| 580 | "maximum number of sg exceeded: %d > %d\n", |
| 581 | num_periods, NUM_CCW); |
| 582 | goto err_out; |
| 583 | } |
| 584 | |
| 585 | if (period_len > MAX_XFER_BYTES) { |
| 586 | dev_err(mxs_dma->dma_device.dev, |
| 587 | "maximum period size exceeded: %d > %d\n", |
| 588 | period_len, MAX_XFER_BYTES); |
| 589 | goto err_out; |
| 590 | } |
| 591 | |
| 592 | while (buf < buf_len) { |
| 593 | struct mxs_dma_ccw *ccw = &mxs_chan->ccw[i]; |
| 594 | |
| 595 | if (i + 1 == num_periods) |
| 596 | ccw->next = mxs_chan->ccw_phys; |
| 597 | else |
| 598 | ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * (i + 1); |
| 599 | |
| 600 | ccw->bufaddr = dma_addr; |
| 601 | ccw->xfer_bytes = period_len; |
| 602 | |
| 603 | ccw->bits = 0; |
| 604 | ccw->bits |= CCW_CHAIN; |
| 605 | ccw->bits |= CCW_IRQ; |
| 606 | ccw->bits |= CCW_HALT_ON_TERM; |
| 607 | ccw->bits |= CCW_TERM_FLUSH; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 608 | ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ? |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 609 | MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, COMMAND); |
| 610 | |
| 611 | dma_addr += period_len; |
| 612 | buf += period_len; |
| 613 | |
| 614 | i++; |
| 615 | } |
Lothar Waßmann | 6d23ea4 | 2011-12-08 09:15:43 +0100 | [diff] [blame] | 616 | mxs_chan->desc_count = i; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 617 | |
| 618 | return &mxs_chan->desc; |
| 619 | |
| 620 | err_out: |
| 621 | mxs_chan->status = DMA_ERROR; |
| 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | static int mxs_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, |
| 626 | unsigned long arg) |
| 627 | { |
| 628 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
| 629 | int ret = 0; |
| 630 | |
| 631 | switch (cmd) { |
| 632 | case DMA_TERMINATE_ALL: |
Dong Aisheng | a62bae9 | 2011-07-19 12:09:56 +0800 | [diff] [blame] | 633 | mxs_dma_reset_chan(mxs_chan); |
Lothar Waßmann | 7ad7a34 | 2011-12-08 09:15:44 +0100 | [diff] [blame] | 634 | mxs_dma_disable_chan(mxs_chan); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 635 | break; |
| 636 | case DMA_PAUSE: |
| 637 | mxs_dma_pause_chan(mxs_chan); |
| 638 | break; |
| 639 | case DMA_RESUME: |
| 640 | mxs_dma_resume_chan(mxs_chan); |
| 641 | break; |
| 642 | default: |
| 643 | ret = -ENOSYS; |
| 644 | } |
| 645 | |
| 646 | return ret; |
| 647 | } |
| 648 | |
| 649 | static enum dma_status mxs_dma_tx_status(struct dma_chan *chan, |
| 650 | dma_cookie_t cookie, struct dma_tx_state *txstate) |
| 651 | { |
| 652 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
Markus Pargmann | 7b11304 | 2013-10-29 08:47:46 +0100 | [diff] [blame] | 653 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
| 654 | u32 residue = 0; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 655 | |
Markus Pargmann | 7b11304 | 2013-10-29 08:47:46 +0100 | [diff] [blame] | 656 | if (mxs_chan->status == DMA_IN_PROGRESS && |
| 657 | mxs_chan->flags & MXS_DMA_SG_LOOP) { |
| 658 | struct mxs_dma_ccw *last_ccw; |
| 659 | u32 bar; |
| 660 | |
| 661 | last_ccw = &mxs_chan->ccw[mxs_chan->desc_count - 1]; |
| 662 | residue = last_ccw->xfer_bytes + last_ccw->bufaddr; |
| 663 | |
| 664 | bar = readl(mxs_dma->base + |
| 665 | HW_APBHX_CHn_BAR(mxs_dma, chan->chan_id)); |
| 666 | residue -= bar; |
| 667 | } |
| 668 | |
| 669 | dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie, |
| 670 | residue); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 671 | |
| 672 | return mxs_chan->status; |
| 673 | } |
| 674 | |
| 675 | static void mxs_dma_issue_pending(struct dma_chan *chan) |
| 676 | { |
Shawn Guo | d04525e | 2012-04-11 13:29:31 +0800 | [diff] [blame] | 677 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
| 678 | |
| 679 | mxs_dma_enable_chan(mxs_chan); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma) |
| 683 | { |
| 684 | int ret; |
| 685 | |
Shawn Guo | 759a2e3 | 2011-12-20 13:54:00 +0800 | [diff] [blame] | 686 | ret = clk_prepare_enable(mxs_dma->clk); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 687 | if (ret) |
Lothar Waßmann | feb397d | 2011-12-08 09:15:42 +0100 | [diff] [blame] | 688 | return ret; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 689 | |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 690 | ret = stmp_reset_block(mxs_dma->base); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 691 | if (ret) |
| 692 | goto err_out; |
| 693 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 694 | /* enable apbh burst */ |
Shawn Guo | bb11fb6 | 2012-05-07 14:14:08 +0800 | [diff] [blame] | 695 | if (dma_is_apbh(mxs_dma)) { |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 696 | writel(BM_APBH_CTRL0_APB_BURST_EN, |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 697 | mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 698 | writel(BM_APBH_CTRL0_APB_BURST8_EN, |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 699 | mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /* enable irq for all the channels */ |
| 703 | writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS, |
Dong Aisheng | f5b7efc | 2012-05-04 20:12:15 +0800 | [diff] [blame] | 704 | mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_SET); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 705 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 706 | err_out: |
Linus Torvalds | 57f2685 | 2012-01-17 18:40:24 -0800 | [diff] [blame] | 707 | clk_disable_unprepare(mxs_dma->clk); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 708 | return ret; |
| 709 | } |
| 710 | |
Shawn Guo | d84f638 | 2013-02-26 09:42:09 +0800 | [diff] [blame] | 711 | struct mxs_dma_filter_param { |
| 712 | struct device_node *of_node; |
| 713 | unsigned int chan_id; |
| 714 | }; |
| 715 | |
| 716 | static bool mxs_dma_filter_fn(struct dma_chan *chan, void *fn_param) |
| 717 | { |
| 718 | struct mxs_dma_filter_param *param = fn_param; |
| 719 | struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan); |
| 720 | struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma; |
| 721 | int chan_irq; |
| 722 | |
| 723 | if (mxs_dma->dma_device.dev->of_node != param->of_node) |
| 724 | return false; |
| 725 | |
| 726 | if (chan->chan_id != param->chan_id) |
| 727 | return false; |
| 728 | |
| 729 | chan_irq = platform_get_irq(mxs_dma->pdev, param->chan_id); |
| 730 | if (chan_irq < 0) |
| 731 | return false; |
| 732 | |
| 733 | mxs_chan->chan_irq = chan_irq; |
| 734 | |
| 735 | return true; |
| 736 | } |
| 737 | |
Fabio Estevam | 3208b37 | 2013-05-24 16:37:27 -0300 | [diff] [blame] | 738 | 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] | 739 | struct of_dma *ofdma) |
| 740 | { |
| 741 | struct mxs_dma_engine *mxs_dma = ofdma->of_dma_data; |
| 742 | dma_cap_mask_t mask = mxs_dma->dma_device.cap_mask; |
| 743 | struct mxs_dma_filter_param param; |
| 744 | |
| 745 | if (dma_spec->args_count != 1) |
| 746 | return NULL; |
| 747 | |
| 748 | param.of_node = ofdma->of_node; |
| 749 | param.chan_id = dma_spec->args[0]; |
| 750 | |
| 751 | if (param.chan_id >= mxs_dma->nr_channels) |
| 752 | return NULL; |
| 753 | |
| 754 | return dma_request_channel(mask, mxs_dma_filter_fn, ¶m); |
| 755 | } |
| 756 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 757 | static int __init mxs_dma_probe(struct platform_device *pdev) |
| 758 | { |
Shawn Guo | d84f638 | 2013-02-26 09:42:09 +0800 | [diff] [blame] | 759 | struct device_node *np = pdev->dev.of_node; |
Dong Aisheng | 90c9abc | 2012-05-04 20:12:17 +0800 | [diff] [blame] | 760 | const struct platform_device_id *id_entry; |
| 761 | const struct of_device_id *of_id; |
| 762 | const struct mxs_dma_type *dma_type; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 763 | struct mxs_dma_engine *mxs_dma; |
| 764 | struct resource *iores; |
| 765 | int ret, i; |
| 766 | |
Shawn Guo | aaa2051 | 2013-02-25 14:57:26 +0800 | [diff] [blame] | 767 | mxs_dma = devm_kzalloc(&pdev->dev, sizeof(*mxs_dma), GFP_KERNEL); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 768 | if (!mxs_dma) |
| 769 | return -ENOMEM; |
| 770 | |
Shawn Guo | d84f638 | 2013-02-26 09:42:09 +0800 | [diff] [blame] | 771 | ret = of_property_read_u32(np, "dma-channels", &mxs_dma->nr_channels); |
| 772 | if (ret) { |
| 773 | dev_err(&pdev->dev, "failed to read dma-channels\n"); |
| 774 | return ret; |
| 775 | } |
| 776 | |
Dong Aisheng | 90c9abc | 2012-05-04 20:12:17 +0800 | [diff] [blame] | 777 | of_id = of_match_device(mxs_dma_dt_ids, &pdev->dev); |
| 778 | if (of_id) |
| 779 | id_entry = of_id->data; |
| 780 | else |
| 781 | id_entry = platform_get_device_id(pdev); |
| 782 | |
| 783 | dma_type = (struct mxs_dma_type *)id_entry->driver_data; |
Shawn Guo | 8c92013 | 2012-05-10 06:23:26 +0800 | [diff] [blame] | 784 | mxs_dma->type = dma_type->type; |
Dong Aisheng | 90c9abc | 2012-05-04 20:12:17 +0800 | [diff] [blame] | 785 | mxs_dma->dev_id = dma_type->id; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 786 | |
| 787 | iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Shawn Guo | aaa2051 | 2013-02-25 14:57:26 +0800 | [diff] [blame] | 788 | mxs_dma->base = devm_ioremap_resource(&pdev->dev, iores); |
| 789 | if (IS_ERR(mxs_dma->base)) |
| 790 | return PTR_ERR(mxs_dma->base); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 791 | |
Shawn Guo | aaa2051 | 2013-02-25 14:57:26 +0800 | [diff] [blame] | 792 | mxs_dma->clk = devm_clk_get(&pdev->dev, NULL); |
| 793 | if (IS_ERR(mxs_dma->clk)) |
| 794 | return PTR_ERR(mxs_dma->clk); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 795 | |
| 796 | dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask); |
| 797 | dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask); |
| 798 | |
| 799 | INIT_LIST_HEAD(&mxs_dma->dma_device.channels); |
| 800 | |
| 801 | /* Initialize channel parameters */ |
| 802 | for (i = 0; i < MXS_DMA_CHANNELS; i++) { |
| 803 | struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i]; |
| 804 | |
| 805 | mxs_chan->mxs_dma = mxs_dma; |
| 806 | mxs_chan->chan.device = &mxs_dma->dma_device; |
Russell King - ARM Linux | 8ac6954 | 2012-03-06 22:36:27 +0000 | [diff] [blame] | 807 | dma_cookie_init(&mxs_chan->chan); |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 808 | |
| 809 | tasklet_init(&mxs_chan->tasklet, mxs_dma_tasklet, |
| 810 | (unsigned long) mxs_chan); |
| 811 | |
| 812 | |
| 813 | /* Add the channel to mxs_chan list */ |
| 814 | list_add_tail(&mxs_chan->chan.device_node, |
| 815 | &mxs_dma->dma_device.channels); |
| 816 | } |
| 817 | |
| 818 | ret = mxs_dma_init(mxs_dma); |
| 819 | if (ret) |
Shawn Guo | aaa2051 | 2013-02-25 14:57:26 +0800 | [diff] [blame] | 820 | return ret; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 821 | |
Shawn Guo | d84f638 | 2013-02-26 09:42:09 +0800 | [diff] [blame] | 822 | mxs_dma->pdev = pdev; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 823 | mxs_dma->dma_device.dev = &pdev->dev; |
| 824 | |
| 825 | /* mxs_dma gets 65535 bytes maximum sg size */ |
| 826 | mxs_dma->dma_device.dev->dma_parms = &mxs_dma->dma_parms; |
| 827 | dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES); |
| 828 | |
| 829 | mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources; |
| 830 | mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources; |
| 831 | mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status; |
| 832 | mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg; |
| 833 | mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic; |
| 834 | mxs_dma->dma_device.device_control = mxs_dma_control; |
| 835 | mxs_dma->dma_device.device_issue_pending = mxs_dma_issue_pending; |
| 836 | |
| 837 | ret = dma_async_device_register(&mxs_dma->dma_device); |
| 838 | if (ret) { |
| 839 | dev_err(mxs_dma->dma_device.dev, "unable to register\n"); |
Shawn Guo | aaa2051 | 2013-02-25 14:57:26 +0800 | [diff] [blame] | 840 | return ret; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 841 | } |
| 842 | |
Shawn Guo | d84f638 | 2013-02-26 09:42:09 +0800 | [diff] [blame] | 843 | ret = of_dma_controller_register(np, mxs_dma_xlate, mxs_dma); |
| 844 | if (ret) { |
| 845 | dev_err(mxs_dma->dma_device.dev, |
| 846 | "failed to register controller\n"); |
| 847 | dma_async_device_unregister(&mxs_dma->dma_device); |
| 848 | } |
| 849 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 850 | dev_info(mxs_dma->dma_device.dev, "initialized\n"); |
| 851 | |
| 852 | return 0; |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 853 | } |
| 854 | |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 855 | static struct platform_driver mxs_dma_driver = { |
| 856 | .driver = { |
| 857 | .name = "mxs-dma", |
Dong Aisheng | 90c9abc | 2012-05-04 20:12:17 +0800 | [diff] [blame] | 858 | .of_match_table = mxs_dma_dt_ids, |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 859 | }, |
Shawn Guo | 8c92013 | 2012-05-10 06:23:26 +0800 | [diff] [blame] | 860 | .id_table = mxs_dma_ids, |
Shawn Guo | a580b8c | 2011-02-27 00:47:42 +0800 | [diff] [blame] | 861 | }; |
| 862 | |
| 863 | static int __init mxs_dma_module_init(void) |
| 864 | { |
| 865 | return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe); |
| 866 | } |
| 867 | subsys_initcall(mxs_dma_module_init); |