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