Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * OMAP DMAengine support |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 8 | #include <linux/delay.h> |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 9 | #include <linux/dmaengine.h> |
| 10 | #include <linux/dma-mapping.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/list.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/omap-dma.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/spinlock.h> |
Jon Hunter | 8d30662 | 2013-02-26 12:27:24 -0600 | [diff] [blame] | 20 | #include <linux/of_dma.h> |
| 21 | #include <linux/of_device.h> |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 22 | |
| 23 | #include "virt-dma.h" |
Tony Lindgren | 7d7e1eb | 2012-08-27 17:43:01 -0700 | [diff] [blame] | 24 | |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 25 | struct omap_dmadev { |
| 26 | struct dma_device ddev; |
| 27 | spinlock_t lock; |
| 28 | struct tasklet_struct task; |
| 29 | struct list_head pending; |
Russell King | 1b416c4 | 2013-11-02 13:00:03 +0000 | [diff] [blame] | 30 | struct omap_system_dma_plat_info *plat; |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | struct omap_chan { |
| 34 | struct virt_dma_chan vc; |
| 35 | struct list_head node; |
Russell King | 1b416c4 | 2013-11-02 13:00:03 +0000 | [diff] [blame] | 36 | struct omap_system_dma_plat_info *plat; |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 37 | |
| 38 | struct dma_slave_config cfg; |
| 39 | unsigned dma_sig; |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 40 | bool cyclic; |
Peter Ujfalusi | 2dcdf57 | 2012-09-14 15:05:45 +0300 | [diff] [blame] | 41 | bool paused; |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 42 | |
| 43 | int dma_ch; |
| 44 | struct omap_desc *desc; |
| 45 | unsigned sgidx; |
| 46 | }; |
| 47 | |
| 48 | struct omap_sg { |
| 49 | dma_addr_t addr; |
| 50 | uint32_t en; /* number of elements (24-bit) */ |
| 51 | uint32_t fn; /* number of frames (16-bit) */ |
| 52 | }; |
| 53 | |
| 54 | struct omap_desc { |
| 55 | struct virt_dma_desc vd; |
| 56 | enum dma_transfer_direction dir; |
| 57 | dma_addr_t dev_addr; |
| 58 | |
Russell King | 7c836bc | 2012-06-18 16:45:19 +0100 | [diff] [blame] | 59 | int16_t fi; /* for OMAP_DMA_SYNC_PACKET */ |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 60 | uint8_t es; /* CSDP_DATA_TYPE_xxx */ |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 61 | uint32_t ccr; /* CCR value */ |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 62 | uint16_t cicr; /* CICR value */ |
Russell King | 2f0d13b | 2013-11-02 18:51:53 +0000 | [diff] [blame] | 63 | uint32_t csdp; /* CSDP value */ |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 64 | |
| 65 | unsigned sglen; |
| 66 | struct omap_sg sg[0]; |
| 67 | }; |
| 68 | |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 69 | enum { |
| 70 | CCR_FS = BIT(5), |
| 71 | CCR_READ_PRIORITY = BIT(6), |
| 72 | CCR_ENABLE = BIT(7), |
| 73 | CCR_AUTO_INIT = BIT(8), /* OMAP1 only */ |
| 74 | CCR_REPEAT = BIT(9), /* OMAP1 only */ |
| 75 | CCR_OMAP31_DISABLE = BIT(10), /* OMAP1 only */ |
| 76 | CCR_SUSPEND_SENSITIVE = BIT(8), /* OMAP2+ only */ |
| 77 | CCR_RD_ACTIVE = BIT(9), /* OMAP2+ only */ |
| 78 | CCR_WR_ACTIVE = BIT(10), /* OMAP2+ only */ |
| 79 | CCR_SRC_AMODE_CONSTANT = 0 << 12, |
| 80 | CCR_SRC_AMODE_POSTINC = 1 << 12, |
| 81 | CCR_SRC_AMODE_SGLIDX = 2 << 12, |
| 82 | CCR_SRC_AMODE_DBLIDX = 3 << 12, |
| 83 | CCR_DST_AMODE_CONSTANT = 0 << 14, |
| 84 | CCR_DST_AMODE_POSTINC = 1 << 14, |
| 85 | CCR_DST_AMODE_SGLIDX = 2 << 14, |
| 86 | CCR_DST_AMODE_DBLIDX = 3 << 14, |
| 87 | CCR_CONSTANT_FILL = BIT(16), |
| 88 | CCR_TRANSPARENT_COPY = BIT(17), |
| 89 | CCR_BS = BIT(18), |
| 90 | CCR_SUPERVISOR = BIT(22), |
| 91 | CCR_PREFETCH = BIT(23), |
| 92 | CCR_TRIGGER_SRC = BIT(24), |
| 93 | CCR_BUFFERING_DISABLE = BIT(25), |
| 94 | CCR_WRITE_PRIORITY = BIT(26), |
| 95 | CCR_SYNC_ELEMENT = 0, |
| 96 | CCR_SYNC_FRAME = CCR_FS, |
| 97 | CCR_SYNC_BLOCK = CCR_BS, |
| 98 | CCR_SYNC_PACKET = CCR_BS | CCR_FS, |
| 99 | |
| 100 | CSDP_DATA_TYPE_8 = 0, |
| 101 | CSDP_DATA_TYPE_16 = 1, |
| 102 | CSDP_DATA_TYPE_32 = 2, |
| 103 | CSDP_SRC_PORT_EMIFF = 0 << 2, /* OMAP1 only */ |
| 104 | CSDP_SRC_PORT_EMIFS = 1 << 2, /* OMAP1 only */ |
| 105 | CSDP_SRC_PORT_OCP_T1 = 2 << 2, /* OMAP1 only */ |
| 106 | CSDP_SRC_PORT_TIPB = 3 << 2, /* OMAP1 only */ |
| 107 | CSDP_SRC_PORT_OCP_T2 = 4 << 2, /* OMAP1 only */ |
| 108 | CSDP_SRC_PORT_MPUI = 5 << 2, /* OMAP1 only */ |
| 109 | CSDP_SRC_PACKED = BIT(6), |
| 110 | CSDP_SRC_BURST_1 = 0 << 7, |
| 111 | CSDP_SRC_BURST_16 = 1 << 7, |
| 112 | CSDP_SRC_BURST_32 = 2 << 7, |
| 113 | CSDP_SRC_BURST_64 = 3 << 7, |
| 114 | CSDP_DST_PORT_EMIFF = 0 << 9, /* OMAP1 only */ |
| 115 | CSDP_DST_PORT_EMIFS = 1 << 9, /* OMAP1 only */ |
| 116 | CSDP_DST_PORT_OCP_T1 = 2 << 9, /* OMAP1 only */ |
| 117 | CSDP_DST_PORT_TIPB = 3 << 9, /* OMAP1 only */ |
| 118 | CSDP_DST_PORT_OCP_T2 = 4 << 9, /* OMAP1 only */ |
| 119 | CSDP_DST_PORT_MPUI = 5 << 9, /* OMAP1 only */ |
| 120 | CSDP_DST_PACKED = BIT(13), |
| 121 | CSDP_DST_BURST_1 = 0 << 14, |
| 122 | CSDP_DST_BURST_16 = 1 << 14, |
| 123 | CSDP_DST_BURST_32 = 2 << 14, |
| 124 | CSDP_DST_BURST_64 = 3 << 14, |
| 125 | |
| 126 | CICR_TOUT_IE = BIT(0), /* OMAP1 only */ |
| 127 | CICR_DROP_IE = BIT(1), |
| 128 | CICR_HALF_IE = BIT(2), |
| 129 | CICR_FRAME_IE = BIT(3), |
| 130 | CICR_LAST_IE = BIT(4), |
| 131 | CICR_BLOCK_IE = BIT(5), |
| 132 | CICR_PKT_IE = BIT(7), /* OMAP2+ only */ |
| 133 | CICR_TRANS_ERR_IE = BIT(8), /* OMAP2+ only */ |
| 134 | CICR_SUPERVISOR_ERR_IE = BIT(10), /* OMAP2+ only */ |
| 135 | CICR_MISALIGNED_ERR_IE = BIT(11), /* OMAP2+ only */ |
| 136 | CICR_DRAIN_IE = BIT(12), /* OMAP2+ only */ |
| 137 | CICR_SUPER_BLOCK_IE = BIT(14), /* OMAP2+ only */ |
| 138 | |
| 139 | CLNK_CTRL_ENABLE_LNK = BIT(15), |
| 140 | }; |
| 141 | |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 142 | static const unsigned es_bytes[] = { |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 143 | [CSDP_DATA_TYPE_8] = 1, |
| 144 | [CSDP_DATA_TYPE_16] = 2, |
| 145 | [CSDP_DATA_TYPE_32] = 4, |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 146 | }; |
| 147 | |
Jon Hunter | 8d30662 | 2013-02-26 12:27:24 -0600 | [diff] [blame] | 148 | static struct of_dma_filter_info omap_dma_info = { |
| 149 | .filter_fn = omap_dma_filter_fn, |
| 150 | }; |
| 151 | |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 152 | static inline struct omap_dmadev *to_omap_dma_dev(struct dma_device *d) |
| 153 | { |
| 154 | return container_of(d, struct omap_dmadev, ddev); |
| 155 | } |
| 156 | |
| 157 | static inline struct omap_chan *to_omap_dma_chan(struct dma_chan *c) |
| 158 | { |
| 159 | return container_of(c, struct omap_chan, vc.chan); |
| 160 | } |
| 161 | |
| 162 | static inline struct omap_desc *to_omap_dma_desc(struct dma_async_tx_descriptor *t) |
| 163 | { |
| 164 | return container_of(t, struct omap_desc, vd.tx); |
| 165 | } |
| 166 | |
| 167 | static void omap_dma_desc_free(struct virt_dma_desc *vd) |
| 168 | { |
| 169 | kfree(container_of(vd, struct omap_desc, vd)); |
| 170 | } |
| 171 | |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 172 | static void omap_dma_start(struct omap_chan *c, struct omap_desc *d) |
| 173 | { |
| 174 | struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device); |
| 175 | uint32_t val; |
| 176 | |
| 177 | if (__dma_omap15xx(od->plat->dma_attr)) |
| 178 | c->plat->dma_write(0, CPC, c->dma_ch); |
| 179 | else |
| 180 | c->plat->dma_write(0, CDAC, c->dma_ch); |
| 181 | |
| 182 | if (!__dma_omap15xx(od->plat->dma_attr) && c->cyclic) { |
| 183 | val = c->plat->dma_read(CLNK_CTRL, c->dma_ch); |
| 184 | |
| 185 | if (dma_omap1()) |
| 186 | val &= ~(1 << 14); |
| 187 | |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 188 | val |= c->dma_ch | CLNK_CTRL_ENABLE_LNK; |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 189 | |
| 190 | c->plat->dma_write(val, CLNK_CTRL, c->dma_ch); |
| 191 | } else if (od->plat->errata & DMA_ERRATA_PARALLEL_CHANNELS) |
| 192 | c->plat->dma_write(c->dma_ch, CLNK_CTRL, c->dma_ch); |
| 193 | |
| 194 | /* Clear CSR */ |
| 195 | if (dma_omap1()) |
| 196 | c->plat->dma_read(CSR, c->dma_ch); |
| 197 | else |
| 198 | c->plat->dma_write(~0, CSR, c->dma_ch); |
| 199 | |
| 200 | /* Enable interrupts */ |
| 201 | c->plat->dma_write(d->cicr, CICR, c->dma_ch); |
| 202 | |
| 203 | val = c->plat->dma_read(CCR, c->dma_ch); |
| 204 | if (od->plat->errata & DMA_ERRATA_IFRAME_BUFFERING) |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 205 | val |= CCR_BUFFERING_DISABLE; |
| 206 | val |= CCR_ENABLE; |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 207 | mb(); |
| 208 | c->plat->dma_write(val, CCR, c->dma_ch); |
| 209 | } |
| 210 | |
| 211 | static void omap_dma_stop(struct omap_chan *c) |
| 212 | { |
| 213 | struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device); |
| 214 | uint32_t val; |
| 215 | |
| 216 | /* disable irq */ |
| 217 | c->plat->dma_write(0, CICR, c->dma_ch); |
| 218 | |
| 219 | /* Clear CSR */ |
| 220 | if (dma_omap1()) |
| 221 | c->plat->dma_read(CSR, c->dma_ch); |
| 222 | else |
| 223 | c->plat->dma_write(~0, CSR, c->dma_ch); |
| 224 | |
| 225 | val = c->plat->dma_read(CCR, c->dma_ch); |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 226 | if (od->plat->errata & DMA_ERRATA_i541 && val & CCR_TRIGGER_SRC) { |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 227 | uint32_t sysconfig; |
| 228 | unsigned i; |
| 229 | |
| 230 | sysconfig = c->plat->dma_read(OCP_SYSCONFIG, c->dma_ch); |
| 231 | val = sysconfig & ~DMA_SYSCONFIG_MIDLEMODE_MASK; |
| 232 | val |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE); |
| 233 | c->plat->dma_write(val, OCP_SYSCONFIG, c->dma_ch); |
| 234 | |
| 235 | val = c->plat->dma_read(CCR, c->dma_ch); |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 236 | val &= ~CCR_ENABLE; |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 237 | c->plat->dma_write(val, CCR, c->dma_ch); |
| 238 | |
| 239 | /* Wait for sDMA FIFO to drain */ |
| 240 | for (i = 0; ; i++) { |
| 241 | val = c->plat->dma_read(CCR, c->dma_ch); |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 242 | if (!(val & (CCR_RD_ACTIVE | CCR_WR_ACTIVE))) |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 243 | break; |
| 244 | |
| 245 | if (i > 100) |
| 246 | break; |
| 247 | |
| 248 | udelay(5); |
| 249 | } |
| 250 | |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 251 | if (val & (CCR_RD_ACTIVE | CCR_WR_ACTIVE)) |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 252 | dev_err(c->vc.chan.device->dev, |
| 253 | "DMA drain did not complete on lch %d\n", |
| 254 | c->dma_ch); |
| 255 | |
| 256 | c->plat->dma_write(sysconfig, OCP_SYSCONFIG, c->dma_ch); |
| 257 | } else { |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 258 | val &= ~CCR_ENABLE; |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 259 | c->plat->dma_write(val, CCR, c->dma_ch); |
| 260 | } |
| 261 | |
| 262 | mb(); |
| 263 | |
| 264 | if (!__dma_omap15xx(od->plat->dma_attr) && c->cyclic) { |
| 265 | val = c->plat->dma_read(CLNK_CTRL, c->dma_ch); |
| 266 | |
| 267 | if (dma_omap1()) |
| 268 | val |= 1 << 14; /* set the STOP_LNK bit */ |
| 269 | else |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 270 | val &= ~CLNK_CTRL_ENABLE_LNK; |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 271 | |
| 272 | c->plat->dma_write(val, CLNK_CTRL, c->dma_ch); |
| 273 | } |
| 274 | } |
| 275 | |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 276 | static void omap_dma_start_sg(struct omap_chan *c, struct omap_desc *d, |
| 277 | unsigned idx) |
| 278 | { |
| 279 | struct omap_sg *sg = d->sg + idx; |
| 280 | |
Russell King | b9e9782 | 2013-11-02 13:26:57 +0000 | [diff] [blame] | 281 | if (d->dir == DMA_DEV_TO_MEM) { |
Russell King | b9e9782 | 2013-11-02 13:26:57 +0000 | [diff] [blame] | 282 | c->plat->dma_write(sg->addr, CDSA, c->dma_ch); |
| 283 | c->plat->dma_write(0, CDEI, c->dma_ch); |
| 284 | c->plat->dma_write(0, CDFI, c->dma_ch); |
| 285 | } else { |
Russell King | b9e9782 | 2013-11-02 13:26:57 +0000 | [diff] [blame] | 286 | c->plat->dma_write(sg->addr, CSSA, c->dma_ch); |
| 287 | c->plat->dma_write(0, CSEI, c->dma_ch); |
| 288 | c->plat->dma_write(0, CSFI, c->dma_ch); |
| 289 | } |
| 290 | |
Russell King | b9e9782 | 2013-11-02 13:26:57 +0000 | [diff] [blame] | 291 | c->plat->dma_write(sg->en, CEN, c->dma_ch); |
| 292 | c->plat->dma_write(sg->fn, CFN, c->dma_ch); |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 293 | |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 294 | omap_dma_start(c, d); |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static void omap_dma_start_desc(struct omap_chan *c) |
| 298 | { |
| 299 | struct virt_dma_desc *vd = vchan_next_desc(&c->vc); |
| 300 | struct omap_desc *d; |
| 301 | |
| 302 | if (!vd) { |
| 303 | c->desc = NULL; |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | list_del(&vd->node); |
| 308 | |
| 309 | c->desc = d = to_omap_dma_desc(&vd->tx); |
| 310 | c->sgidx = 0; |
| 311 | |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 312 | c->plat->dma_write(d->ccr, CCR, c->dma_ch); |
| 313 | if (dma_omap1()) |
| 314 | c->plat->dma_write(d->ccr >> 16, CCR2, c->dma_ch); |
Russell King | b9e9782 | 2013-11-02 13:26:57 +0000 | [diff] [blame] | 315 | |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 316 | if (d->dir == DMA_DEV_TO_MEM) { |
Russell King | b9e9782 | 2013-11-02 13:26:57 +0000 | [diff] [blame] | 317 | c->plat->dma_write(d->dev_addr, CSSA, c->dma_ch); |
| 318 | c->plat->dma_write(0, CSEI, c->dma_ch); |
| 319 | c->plat->dma_write(d->fi, CSFI, c->dma_ch); |
| 320 | } else { |
Russell King | b9e9782 | 2013-11-02 13:26:57 +0000 | [diff] [blame] | 321 | c->plat->dma_write(d->dev_addr, CDSA, c->dma_ch); |
| 322 | c->plat->dma_write(0, CDEI, c->dma_ch); |
| 323 | c->plat->dma_write(d->fi, CDFI, c->dma_ch); |
| 324 | } |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 325 | |
Russell King | 2f0d13b | 2013-11-02 18:51:53 +0000 | [diff] [blame] | 326 | c->plat->dma_write(d->csdp, CSDP, c->dma_ch); |
Russell King | 913a2d0 | 2013-11-02 14:41:42 +0000 | [diff] [blame] | 327 | |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 328 | omap_dma_start_sg(c, d, 0); |
| 329 | } |
| 330 | |
| 331 | static void omap_dma_callback(int ch, u16 status, void *data) |
| 332 | { |
| 333 | struct omap_chan *c = data; |
| 334 | struct omap_desc *d; |
| 335 | unsigned long flags; |
| 336 | |
| 337 | spin_lock_irqsave(&c->vc.lock, flags); |
| 338 | d = c->desc; |
| 339 | if (d) { |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 340 | if (!c->cyclic) { |
| 341 | if (++c->sgidx < d->sglen) { |
| 342 | omap_dma_start_sg(c, d, c->sgidx); |
| 343 | } else { |
| 344 | omap_dma_start_desc(c); |
| 345 | vchan_cookie_complete(&d->vd); |
| 346 | } |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 347 | } else { |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 348 | vchan_cyclic_callback(&d->vd); |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | spin_unlock_irqrestore(&c->vc.lock, flags); |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * This callback schedules all pending channels. We could be more |
| 356 | * clever here by postponing allocation of the real DMA channels to |
| 357 | * this point, and freeing them when our virtual channel becomes idle. |
| 358 | * |
| 359 | * We would then need to deal with 'all channels in-use' |
| 360 | */ |
| 361 | static void omap_dma_sched(unsigned long data) |
| 362 | { |
| 363 | struct omap_dmadev *d = (struct omap_dmadev *)data; |
| 364 | LIST_HEAD(head); |
| 365 | |
| 366 | spin_lock_irq(&d->lock); |
| 367 | list_splice_tail_init(&d->pending, &head); |
| 368 | spin_unlock_irq(&d->lock); |
| 369 | |
| 370 | while (!list_empty(&head)) { |
| 371 | struct omap_chan *c = list_first_entry(&head, |
| 372 | struct omap_chan, node); |
| 373 | |
| 374 | spin_lock_irq(&c->vc.lock); |
| 375 | list_del_init(&c->node); |
| 376 | omap_dma_start_desc(c); |
| 377 | spin_unlock_irq(&c->vc.lock); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | static int omap_dma_alloc_chan_resources(struct dma_chan *chan) |
| 382 | { |
| 383 | struct omap_chan *c = to_omap_dma_chan(chan); |
| 384 | |
Ezequiel Garcia | 9e2f7d8 | 2013-12-19 22:22:29 -0300 | [diff] [blame] | 385 | dev_dbg(c->vc.chan.device->dev, "allocating channel for %u\n", c->dma_sig); |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 386 | |
| 387 | return omap_request_dma(c->dma_sig, "DMA engine", |
| 388 | omap_dma_callback, c, &c->dma_ch); |
| 389 | } |
| 390 | |
| 391 | static void omap_dma_free_chan_resources(struct dma_chan *chan) |
| 392 | { |
| 393 | struct omap_chan *c = to_omap_dma_chan(chan); |
| 394 | |
| 395 | vchan_free_chan_resources(&c->vc); |
| 396 | omap_free_dma(c->dma_ch); |
| 397 | |
Ezequiel Garcia | 9e2f7d8 | 2013-12-19 22:22:29 -0300 | [diff] [blame] | 398 | dev_dbg(c->vc.chan.device->dev, "freeing channel for %u\n", c->dma_sig); |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Russell King | 3850e22 | 2012-06-21 10:37:35 +0100 | [diff] [blame] | 401 | static size_t omap_dma_sg_size(struct omap_sg *sg) |
| 402 | { |
| 403 | return sg->en * sg->fn; |
| 404 | } |
| 405 | |
| 406 | static size_t omap_dma_desc_size(struct omap_desc *d) |
| 407 | { |
| 408 | unsigned i; |
| 409 | size_t size; |
| 410 | |
| 411 | for (size = i = 0; i < d->sglen; i++) |
| 412 | size += omap_dma_sg_size(&d->sg[i]); |
| 413 | |
| 414 | return size * es_bytes[d->es]; |
| 415 | } |
| 416 | |
| 417 | static size_t omap_dma_desc_size_pos(struct omap_desc *d, dma_addr_t addr) |
| 418 | { |
| 419 | unsigned i; |
| 420 | size_t size, es_size = es_bytes[d->es]; |
| 421 | |
| 422 | for (size = i = 0; i < d->sglen; i++) { |
| 423 | size_t this_size = omap_dma_sg_size(&d->sg[i]) * es_size; |
| 424 | |
| 425 | if (size) |
| 426 | size += this_size; |
| 427 | else if (addr >= d->sg[i].addr && |
| 428 | addr < d->sg[i].addr + this_size) |
| 429 | size += d->sg[i].addr + this_size - addr; |
| 430 | } |
| 431 | return size; |
| 432 | } |
| 433 | |
Russell King | 3997cab | 2013-11-02 18:04:17 +0000 | [diff] [blame] | 434 | static dma_addr_t omap_dma_get_src_pos(struct omap_chan *c) |
| 435 | { |
| 436 | struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device); |
| 437 | dma_addr_t addr; |
| 438 | |
| 439 | if (__dma_omap15xx(od->plat->dma_attr)) |
| 440 | addr = c->plat->dma_read(CPC, c->dma_ch); |
| 441 | else |
| 442 | addr = c->plat->dma_read(CSAC, c->dma_ch); |
| 443 | |
| 444 | if (od->plat->errata & DMA_ERRATA_3_3 && addr == 0) |
| 445 | addr = c->plat->dma_read(CSAC, c->dma_ch); |
| 446 | |
| 447 | if (!__dma_omap15xx(od->plat->dma_attr)) { |
| 448 | /* |
| 449 | * CDAC == 0 indicates that the DMA transfer on the channel has |
| 450 | * not been started (no data has been transferred so far). |
| 451 | * Return the programmed source start address in this case. |
| 452 | */ |
| 453 | if (c->plat->dma_read(CDAC, c->dma_ch)) |
| 454 | addr = c->plat->dma_read(CSAC, c->dma_ch); |
| 455 | else |
| 456 | addr = c->plat->dma_read(CSSA, c->dma_ch); |
| 457 | } |
| 458 | |
| 459 | if (dma_omap1()) |
| 460 | addr |= c->plat->dma_read(CSSA, c->dma_ch) & 0xffff0000; |
| 461 | |
| 462 | return addr; |
| 463 | } |
| 464 | |
| 465 | static dma_addr_t omap_dma_get_dst_pos(struct omap_chan *c) |
| 466 | { |
| 467 | struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device); |
| 468 | dma_addr_t addr; |
| 469 | |
| 470 | if (__dma_omap15xx(od->plat->dma_attr)) |
| 471 | addr = c->plat->dma_read(CPC, c->dma_ch); |
| 472 | else |
| 473 | addr = c->plat->dma_read(CDAC, c->dma_ch); |
| 474 | |
| 475 | /* |
| 476 | * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is |
| 477 | * read before the DMA controller finished disabling the channel. |
| 478 | */ |
| 479 | if (!__dma_omap15xx(od->plat->dma_attr) && addr == 0) { |
| 480 | addr = c->plat->dma_read(CDAC, c->dma_ch); |
| 481 | /* |
| 482 | * CDAC == 0 indicates that the DMA transfer on the channel has |
| 483 | * not been started (no data has been transferred so far). |
| 484 | * Return the programmed destination start address in this case. |
| 485 | */ |
| 486 | if (addr == 0) |
| 487 | addr = c->plat->dma_read(CDSA, c->dma_ch); |
| 488 | } |
| 489 | |
| 490 | if (dma_omap1()) |
| 491 | addr |= c->plat->dma_read(CDSA, c->dma_ch) & 0xffff0000; |
| 492 | |
| 493 | return addr; |
| 494 | } |
| 495 | |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 496 | static enum dma_status omap_dma_tx_status(struct dma_chan *chan, |
| 497 | dma_cookie_t cookie, struct dma_tx_state *txstate) |
| 498 | { |
Russell King | 3850e22 | 2012-06-21 10:37:35 +0100 | [diff] [blame] | 499 | struct omap_chan *c = to_omap_dma_chan(chan); |
| 500 | struct virt_dma_desc *vd; |
| 501 | enum dma_status ret; |
| 502 | unsigned long flags; |
| 503 | |
| 504 | ret = dma_cookie_status(chan, cookie, txstate); |
Vinod Koul | 7cce508 | 2013-10-16 20:51:54 +0530 | [diff] [blame] | 505 | if (ret == DMA_COMPLETE || !txstate) |
Russell King | 3850e22 | 2012-06-21 10:37:35 +0100 | [diff] [blame] | 506 | return ret; |
| 507 | |
| 508 | spin_lock_irqsave(&c->vc.lock, flags); |
| 509 | vd = vchan_find_desc(&c->vc, cookie); |
| 510 | if (vd) { |
| 511 | txstate->residue = omap_dma_desc_size(to_omap_dma_desc(&vd->tx)); |
| 512 | } else if (c->desc && c->desc->vd.tx.cookie == cookie) { |
| 513 | struct omap_desc *d = c->desc; |
| 514 | dma_addr_t pos; |
| 515 | |
| 516 | if (d->dir == DMA_MEM_TO_DEV) |
Russell King | 3997cab | 2013-11-02 18:04:17 +0000 | [diff] [blame] | 517 | pos = omap_dma_get_src_pos(c); |
Russell King | 3850e22 | 2012-06-21 10:37:35 +0100 | [diff] [blame] | 518 | else if (d->dir == DMA_DEV_TO_MEM) |
Russell King | 3997cab | 2013-11-02 18:04:17 +0000 | [diff] [blame] | 519 | pos = omap_dma_get_dst_pos(c); |
Russell King | 3850e22 | 2012-06-21 10:37:35 +0100 | [diff] [blame] | 520 | else |
| 521 | pos = 0; |
| 522 | |
| 523 | txstate->residue = omap_dma_desc_size_pos(d, pos); |
| 524 | } else { |
| 525 | txstate->residue = 0; |
| 526 | } |
| 527 | spin_unlock_irqrestore(&c->vc.lock, flags); |
| 528 | |
| 529 | return ret; |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | static void omap_dma_issue_pending(struct dma_chan *chan) |
| 533 | { |
| 534 | struct omap_chan *c = to_omap_dma_chan(chan); |
| 535 | unsigned long flags; |
| 536 | |
| 537 | spin_lock_irqsave(&c->vc.lock, flags); |
| 538 | if (vchan_issue_pending(&c->vc) && !c->desc) { |
Peter Ujfalusi | 7650246 | 2013-04-09 16:33:06 +0200 | [diff] [blame] | 539 | /* |
| 540 | * c->cyclic is used only by audio and in this case the DMA need |
| 541 | * to be started without delay. |
| 542 | */ |
| 543 | if (!c->cyclic) { |
| 544 | struct omap_dmadev *d = to_omap_dma_dev(chan->device); |
| 545 | spin_lock(&d->lock); |
| 546 | if (list_empty(&c->node)) |
| 547 | list_add_tail(&c->node, &d->pending); |
| 548 | spin_unlock(&d->lock); |
| 549 | tasklet_schedule(&d->task); |
| 550 | } else { |
| 551 | omap_dma_start_desc(c); |
| 552 | } |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 553 | } |
| 554 | spin_unlock_irqrestore(&c->vc.lock, flags); |
| 555 | } |
| 556 | |
| 557 | static struct dma_async_tx_descriptor *omap_dma_prep_slave_sg( |
| 558 | struct dma_chan *chan, struct scatterlist *sgl, unsigned sglen, |
| 559 | enum dma_transfer_direction dir, unsigned long tx_flags, void *context) |
| 560 | { |
| 561 | struct omap_chan *c = to_omap_dma_chan(chan); |
| 562 | enum dma_slave_buswidth dev_width; |
| 563 | struct scatterlist *sgent; |
| 564 | struct omap_desc *d; |
| 565 | dma_addr_t dev_addr; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 566 | unsigned i, j = 0, es, en, frame_bytes; |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 567 | u32 burst; |
| 568 | |
| 569 | if (dir == DMA_DEV_TO_MEM) { |
| 570 | dev_addr = c->cfg.src_addr; |
| 571 | dev_width = c->cfg.src_addr_width; |
| 572 | burst = c->cfg.src_maxburst; |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 573 | } else if (dir == DMA_MEM_TO_DEV) { |
| 574 | dev_addr = c->cfg.dst_addr; |
| 575 | dev_width = c->cfg.dst_addr_width; |
| 576 | burst = c->cfg.dst_maxburst; |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 577 | } else { |
| 578 | dev_err(chan->device->dev, "%s: bad direction?\n", __func__); |
| 579 | return NULL; |
| 580 | } |
| 581 | |
| 582 | /* Bus width translates to the element size (ES) */ |
| 583 | switch (dev_width) { |
| 584 | case DMA_SLAVE_BUSWIDTH_1_BYTE: |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 585 | es = CSDP_DATA_TYPE_8; |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 586 | break; |
| 587 | case DMA_SLAVE_BUSWIDTH_2_BYTES: |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 588 | es = CSDP_DATA_TYPE_16; |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 589 | break; |
| 590 | case DMA_SLAVE_BUSWIDTH_4_BYTES: |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 591 | es = CSDP_DATA_TYPE_32; |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 592 | break; |
| 593 | default: /* not reached */ |
| 594 | return NULL; |
| 595 | } |
| 596 | |
| 597 | /* Now allocate and setup the descriptor. */ |
| 598 | d = kzalloc(sizeof(*d) + sglen * sizeof(d->sg[0]), GFP_ATOMIC); |
| 599 | if (!d) |
| 600 | return NULL; |
| 601 | |
| 602 | d->dir = dir; |
| 603 | d->dev_addr = dev_addr; |
| 604 | d->es = es; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 605 | |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 606 | d->ccr = CCR_SYNC_FRAME; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 607 | if (dir == DMA_DEV_TO_MEM) |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 608 | d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_CONSTANT; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 609 | else |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 610 | d->ccr |= CCR_DST_AMODE_CONSTANT | CCR_SRC_AMODE_POSTINC; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 611 | |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 612 | d->cicr = CICR_DROP_IE | CICR_BLOCK_IE; |
Russell King | 2f0d13b | 2013-11-02 18:51:53 +0000 | [diff] [blame] | 613 | d->csdp = es; |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 614 | |
Russell King | 2f0d13b | 2013-11-02 18:51:53 +0000 | [diff] [blame] | 615 | if (dma_omap1()) { |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 616 | if (__dma_omap16xx(od->plat->dma_attr)) { |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 617 | d->ccr |= CCR_OMAP31_DISABLE; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 618 | /* Duplicate what plat-omap/dma.c does */ |
| 619 | d->ccr |= c->dma_ch + 1; |
| 620 | } else { |
| 621 | d->ccr |= c->dma_sig & 0x1f; |
| 622 | } |
| 623 | |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 624 | d->cicr |= CICR_TOUT_IE; |
Russell King | 2f0d13b | 2013-11-02 18:51:53 +0000 | [diff] [blame] | 625 | |
| 626 | if (dir == DMA_DEV_TO_MEM) |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 627 | d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_TIPB; |
Russell King | 2f0d13b | 2013-11-02 18:51:53 +0000 | [diff] [blame] | 628 | else |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 629 | d->csdp |= CSDP_DST_PORT_TIPB | CSDP_SRC_PORT_EMIFF; |
Russell King | 2f0d13b | 2013-11-02 18:51:53 +0000 | [diff] [blame] | 630 | } else { |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 631 | d->ccr |= (c->dma_sig & ~0x1f) << 14; |
| 632 | d->ccr |= c->dma_sig & 0x1f; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 633 | |
| 634 | if (dir == DMA_DEV_TO_MEM) |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 635 | d->ccr |= CCR_TRIGGER_SRC; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 636 | |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 637 | d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE; |
Russell King | 2f0d13b | 2013-11-02 18:51:53 +0000 | [diff] [blame] | 638 | } |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 639 | |
| 640 | /* |
| 641 | * Build our scatterlist entries: each contains the address, |
| 642 | * the number of elements (EN) in each frame, and the number of |
| 643 | * frames (FN). Number of bytes for this entry = ES * EN * FN. |
| 644 | * |
| 645 | * Burst size translates to number of elements with frame sync. |
| 646 | * Note: DMA engine defines burst to be the number of dev-width |
| 647 | * transfers. |
| 648 | */ |
| 649 | en = burst; |
| 650 | frame_bytes = es_bytes[es] * en; |
| 651 | for_each_sg(sgl, sgent, sglen, i) { |
| 652 | d->sg[j].addr = sg_dma_address(sgent); |
| 653 | d->sg[j].en = en; |
| 654 | d->sg[j].fn = sg_dma_len(sgent) / frame_bytes; |
| 655 | j++; |
| 656 | } |
| 657 | |
| 658 | d->sglen = j; |
| 659 | |
| 660 | return vchan_tx_prep(&c->vc, &d->vd, tx_flags); |
| 661 | } |
| 662 | |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 663 | static struct dma_async_tx_descriptor *omap_dma_prep_dma_cyclic( |
| 664 | struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len, |
Peter Ujfalusi | ec8b5e4 | 2012-09-14 15:05:47 +0300 | [diff] [blame] | 665 | size_t period_len, enum dma_transfer_direction dir, unsigned long flags, |
| 666 | void *context) |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 667 | { |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 668 | struct omap_dmadev *od = to_omap_dma_dev(chan->device); |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 669 | struct omap_chan *c = to_omap_dma_chan(chan); |
| 670 | enum dma_slave_buswidth dev_width; |
| 671 | struct omap_desc *d; |
| 672 | dma_addr_t dev_addr; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 673 | unsigned es; |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 674 | u32 burst; |
| 675 | |
| 676 | if (dir == DMA_DEV_TO_MEM) { |
| 677 | dev_addr = c->cfg.src_addr; |
| 678 | dev_width = c->cfg.src_addr_width; |
| 679 | burst = c->cfg.src_maxburst; |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 680 | } else if (dir == DMA_MEM_TO_DEV) { |
| 681 | dev_addr = c->cfg.dst_addr; |
| 682 | dev_width = c->cfg.dst_addr_width; |
| 683 | burst = c->cfg.dst_maxburst; |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 684 | } else { |
| 685 | dev_err(chan->device->dev, "%s: bad direction?\n", __func__); |
| 686 | return NULL; |
| 687 | } |
| 688 | |
| 689 | /* Bus width translates to the element size (ES) */ |
| 690 | switch (dev_width) { |
| 691 | case DMA_SLAVE_BUSWIDTH_1_BYTE: |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 692 | es = CSDP_DATA_TYPE_8; |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 693 | break; |
| 694 | case DMA_SLAVE_BUSWIDTH_2_BYTES: |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 695 | es = CSDP_DATA_TYPE_16; |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 696 | break; |
| 697 | case DMA_SLAVE_BUSWIDTH_4_BYTES: |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 698 | es = CSDP_DATA_TYPE_32; |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 699 | break; |
| 700 | default: /* not reached */ |
| 701 | return NULL; |
| 702 | } |
| 703 | |
| 704 | /* Now allocate and setup the descriptor. */ |
| 705 | d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC); |
| 706 | if (!d) |
| 707 | return NULL; |
| 708 | |
| 709 | d->dir = dir; |
| 710 | d->dev_addr = dev_addr; |
| 711 | d->fi = burst; |
| 712 | d->es = es; |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 713 | d->sg[0].addr = buf_addr; |
| 714 | d->sg[0].en = period_len / es_bytes[es]; |
| 715 | d->sg[0].fn = buf_len / period_len; |
| 716 | d->sglen = 1; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 717 | |
| 718 | d->ccr = 0; |
| 719 | if (__dma_omap15xx(od->plat->dma_attr)) |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 720 | d->ccr = CCR_AUTO_INIT | CCR_REPEAT; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 721 | if (dir == DMA_DEV_TO_MEM) |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 722 | d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_CONSTANT; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 723 | else |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 724 | d->ccr |= CCR_DST_AMODE_CONSTANT | CCR_SRC_AMODE_POSTINC; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 725 | |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 726 | d->cicr = CICR_DROP_IE; |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 727 | if (flags & DMA_PREP_INTERRUPT) |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 728 | d->cicr |= CICR_FRAME_IE; |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 729 | |
Russell King | 2f0d13b | 2013-11-02 18:51:53 +0000 | [diff] [blame] | 730 | d->csdp = es; |
| 731 | |
| 732 | if (dma_omap1()) { |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 733 | if (__dma_omap16xx(od->plat->dma_attr)) { |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 734 | d->ccr |= CCR_OMAP31_DISABLE; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 735 | /* Duplicate what plat-omap/dma.c does */ |
| 736 | d->ccr |= c->dma_ch + 1; |
| 737 | } else { |
| 738 | d->ccr |= c->dma_sig & 0x1f; |
| 739 | } |
| 740 | |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 741 | d->cicr |= CICR_TOUT_IE; |
Russell King | 2f0d13b | 2013-11-02 18:51:53 +0000 | [diff] [blame] | 742 | |
| 743 | if (dir == DMA_DEV_TO_MEM) |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 744 | d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_MPUI; |
Russell King | 2f0d13b | 2013-11-02 18:51:53 +0000 | [diff] [blame] | 745 | else |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 746 | d->csdp |= CSDP_DST_PORT_MPUI | CSDP_SRC_PORT_EMIFF; |
Russell King | 2f0d13b | 2013-11-02 18:51:53 +0000 | [diff] [blame] | 747 | } else { |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 748 | d->ccr |= (c->dma_sig & ~0x1f) << 14; |
| 749 | d->ccr |= c->dma_sig & 0x1f; |
| 750 | |
| 751 | if (burst) |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 752 | d->ccr |= CCR_SYNC_PACKET; |
| 753 | else |
| 754 | d->ccr |= CCR_SYNC_ELEMENT; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 755 | |
| 756 | if (dir == DMA_DEV_TO_MEM) |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 757 | d->ccr |= CCR_TRIGGER_SRC; |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 758 | |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 759 | d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE; |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 760 | |
Russell King | 9043826 | 2013-11-02 19:57:06 +0000 | [diff] [blame^] | 761 | d->csdp |= CSDP_DST_BURST_64 | CSDP_SRC_BURST_64; |
Russell King | 2f0d13b | 2013-11-02 18:51:53 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Russell King | 3ed4d18 | 2013-11-02 19:16:09 +0000 | [diff] [blame] | 764 | c->cyclic = true; |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 765 | |
Peter Ujfalusi | 2dde5b9 | 2012-09-14 15:05:48 +0300 | [diff] [blame] | 766 | return vchan_tx_prep(&c->vc, &d->vd, flags); |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 767 | } |
| 768 | |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 769 | static int omap_dma_slave_config(struct omap_chan *c, struct dma_slave_config *cfg) |
| 770 | { |
| 771 | if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES || |
| 772 | cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES) |
| 773 | return -EINVAL; |
| 774 | |
| 775 | memcpy(&c->cfg, cfg, sizeof(c->cfg)); |
| 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | static int omap_dma_terminate_all(struct omap_chan *c) |
| 781 | { |
| 782 | struct omap_dmadev *d = to_omap_dma_dev(c->vc.chan.device); |
| 783 | unsigned long flags; |
| 784 | LIST_HEAD(head); |
| 785 | |
| 786 | spin_lock_irqsave(&c->vc.lock, flags); |
| 787 | |
| 788 | /* Prevent this channel being scheduled */ |
| 789 | spin_lock(&d->lock); |
| 790 | list_del_init(&c->node); |
| 791 | spin_unlock(&d->lock); |
| 792 | |
| 793 | /* |
| 794 | * Stop DMA activity: we assume the callback will not be called |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 795 | * after omap_dma_stop() returns (even if it does, it will see |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 796 | * c->desc is NULL and exit.) |
| 797 | */ |
| 798 | if (c->desc) { |
| 799 | c->desc = NULL; |
Peter Ujfalusi | 2dcdf57 | 2012-09-14 15:05:45 +0300 | [diff] [blame] | 800 | /* Avoid stopping the dma twice */ |
| 801 | if (!c->paused) |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 802 | omap_dma_stop(c); |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 803 | } |
| 804 | |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 805 | if (c->cyclic) { |
| 806 | c->cyclic = false; |
Peter Ujfalusi | 2dcdf57 | 2012-09-14 15:05:45 +0300 | [diff] [blame] | 807 | c->paused = false; |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 808 | } |
| 809 | |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 810 | vchan_get_all_descriptors(&c->vc, &head); |
| 811 | spin_unlock_irqrestore(&c->vc.lock, flags); |
| 812 | vchan_dma_desc_free_list(&c->vc, &head); |
| 813 | |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | static int omap_dma_pause(struct omap_chan *c) |
| 818 | { |
Peter Ujfalusi | 2dcdf57 | 2012-09-14 15:05:45 +0300 | [diff] [blame] | 819 | /* Pause/Resume only allowed with cyclic mode */ |
| 820 | if (!c->cyclic) |
| 821 | return -EINVAL; |
| 822 | |
| 823 | if (!c->paused) { |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 824 | omap_dma_stop(c); |
Peter Ujfalusi | 2dcdf57 | 2012-09-14 15:05:45 +0300 | [diff] [blame] | 825 | c->paused = true; |
| 826 | } |
| 827 | |
| 828 | return 0; |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | static int omap_dma_resume(struct omap_chan *c) |
| 832 | { |
Peter Ujfalusi | 2dcdf57 | 2012-09-14 15:05:45 +0300 | [diff] [blame] | 833 | /* Pause/Resume only allowed with cyclic mode */ |
| 834 | if (!c->cyclic) |
| 835 | return -EINVAL; |
| 836 | |
| 837 | if (c->paused) { |
Russell King | fa3ad86 | 2013-11-02 17:07:09 +0000 | [diff] [blame] | 838 | omap_dma_start(c, c->desc); |
Peter Ujfalusi | 2dcdf57 | 2012-09-14 15:05:45 +0300 | [diff] [blame] | 839 | c->paused = false; |
| 840 | } |
| 841 | |
| 842 | return 0; |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | static int omap_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, |
| 846 | unsigned long arg) |
| 847 | { |
| 848 | struct omap_chan *c = to_omap_dma_chan(chan); |
| 849 | int ret; |
| 850 | |
| 851 | switch (cmd) { |
| 852 | case DMA_SLAVE_CONFIG: |
| 853 | ret = omap_dma_slave_config(c, (struct dma_slave_config *)arg); |
| 854 | break; |
| 855 | |
| 856 | case DMA_TERMINATE_ALL: |
| 857 | ret = omap_dma_terminate_all(c); |
| 858 | break; |
| 859 | |
| 860 | case DMA_PAUSE: |
| 861 | ret = omap_dma_pause(c); |
| 862 | break; |
| 863 | |
| 864 | case DMA_RESUME: |
| 865 | ret = omap_dma_resume(c); |
| 866 | break; |
| 867 | |
| 868 | default: |
| 869 | ret = -ENXIO; |
| 870 | break; |
| 871 | } |
| 872 | |
| 873 | return ret; |
| 874 | } |
| 875 | |
| 876 | static int omap_dma_chan_init(struct omap_dmadev *od, int dma_sig) |
| 877 | { |
| 878 | struct omap_chan *c; |
| 879 | |
| 880 | c = kzalloc(sizeof(*c), GFP_KERNEL); |
| 881 | if (!c) |
| 882 | return -ENOMEM; |
| 883 | |
Russell King | 1b416c4 | 2013-11-02 13:00:03 +0000 | [diff] [blame] | 884 | c->plat = od->plat; |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 885 | c->dma_sig = dma_sig; |
| 886 | c->vc.desc_free = omap_dma_desc_free; |
| 887 | vchan_init(&c->vc, &od->ddev); |
| 888 | INIT_LIST_HEAD(&c->node); |
| 889 | |
| 890 | od->ddev.chancnt++; |
| 891 | |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | static void omap_dma_free(struct omap_dmadev *od) |
| 896 | { |
| 897 | tasklet_kill(&od->task); |
| 898 | while (!list_empty(&od->ddev.channels)) { |
| 899 | struct omap_chan *c = list_first_entry(&od->ddev.channels, |
| 900 | struct omap_chan, vc.chan.device_node); |
| 901 | |
| 902 | list_del(&c->vc.chan.device_node); |
| 903 | tasklet_kill(&c->vc.task); |
| 904 | kfree(c); |
| 905 | } |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | static int omap_dma_probe(struct platform_device *pdev) |
| 909 | { |
| 910 | struct omap_dmadev *od; |
| 911 | int rc, i; |
| 912 | |
Russell King | 104fce7 | 2013-11-02 12:58:29 +0000 | [diff] [blame] | 913 | od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL); |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 914 | if (!od) |
| 915 | return -ENOMEM; |
| 916 | |
Russell King | 1b416c4 | 2013-11-02 13:00:03 +0000 | [diff] [blame] | 917 | od->plat = omap_get_plat_info(); |
| 918 | if (!od->plat) |
| 919 | return -EPROBE_DEFER; |
| 920 | |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 921 | dma_cap_set(DMA_SLAVE, od->ddev.cap_mask); |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 922 | dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask); |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 923 | od->ddev.device_alloc_chan_resources = omap_dma_alloc_chan_resources; |
| 924 | od->ddev.device_free_chan_resources = omap_dma_free_chan_resources; |
| 925 | od->ddev.device_tx_status = omap_dma_tx_status; |
| 926 | od->ddev.device_issue_pending = omap_dma_issue_pending; |
| 927 | od->ddev.device_prep_slave_sg = omap_dma_prep_slave_sg; |
Russell King | 3a774ea | 2012-06-21 10:40:15 +0100 | [diff] [blame] | 928 | od->ddev.device_prep_dma_cyclic = omap_dma_prep_dma_cyclic; |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 929 | od->ddev.device_control = omap_dma_control; |
| 930 | od->ddev.dev = &pdev->dev; |
| 931 | INIT_LIST_HEAD(&od->ddev.channels); |
| 932 | INIT_LIST_HEAD(&od->pending); |
| 933 | spin_lock_init(&od->lock); |
| 934 | |
| 935 | tasklet_init(&od->task, omap_dma_sched, (unsigned long)od); |
| 936 | |
| 937 | for (i = 0; i < 127; i++) { |
| 938 | rc = omap_dma_chan_init(od, i); |
| 939 | if (rc) { |
| 940 | omap_dma_free(od); |
| 941 | return rc; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | rc = dma_async_device_register(&od->ddev); |
| 946 | if (rc) { |
| 947 | pr_warn("OMAP-DMA: failed to register slave DMA engine device: %d\n", |
| 948 | rc); |
| 949 | omap_dma_free(od); |
Jon Hunter | 8d30662 | 2013-02-26 12:27:24 -0600 | [diff] [blame] | 950 | return rc; |
| 951 | } |
| 952 | |
| 953 | platform_set_drvdata(pdev, od); |
| 954 | |
| 955 | if (pdev->dev.of_node) { |
| 956 | omap_dma_info.dma_cap = od->ddev.cap_mask; |
| 957 | |
| 958 | /* Device-tree DMA controller registration */ |
| 959 | rc = of_dma_controller_register(pdev->dev.of_node, |
| 960 | of_dma_simple_xlate, &omap_dma_info); |
| 961 | if (rc) { |
| 962 | pr_warn("OMAP-DMA: failed to register DMA controller\n"); |
| 963 | dma_async_device_unregister(&od->ddev); |
| 964 | omap_dma_free(od); |
| 965 | } |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | dev_info(&pdev->dev, "OMAP DMA engine driver\n"); |
| 969 | |
| 970 | return rc; |
| 971 | } |
| 972 | |
| 973 | static int omap_dma_remove(struct platform_device *pdev) |
| 974 | { |
| 975 | struct omap_dmadev *od = platform_get_drvdata(pdev); |
| 976 | |
Jon Hunter | 8d30662 | 2013-02-26 12:27:24 -0600 | [diff] [blame] | 977 | if (pdev->dev.of_node) |
| 978 | of_dma_controller_free(pdev->dev.of_node); |
| 979 | |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 980 | dma_async_device_unregister(&od->ddev); |
| 981 | omap_dma_free(od); |
| 982 | |
| 983 | return 0; |
| 984 | } |
| 985 | |
Jon Hunter | 8d30662 | 2013-02-26 12:27:24 -0600 | [diff] [blame] | 986 | static const struct of_device_id omap_dma_match[] = { |
| 987 | { .compatible = "ti,omap2420-sdma", }, |
| 988 | { .compatible = "ti,omap2430-sdma", }, |
| 989 | { .compatible = "ti,omap3430-sdma", }, |
| 990 | { .compatible = "ti,omap3630-sdma", }, |
| 991 | { .compatible = "ti,omap4430-sdma", }, |
| 992 | {}, |
| 993 | }; |
| 994 | MODULE_DEVICE_TABLE(of, omap_dma_match); |
| 995 | |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 996 | static struct platform_driver omap_dma_driver = { |
| 997 | .probe = omap_dma_probe, |
| 998 | .remove = omap_dma_remove, |
| 999 | .driver = { |
| 1000 | .name = "omap-dma-engine", |
| 1001 | .owner = THIS_MODULE, |
Jon Hunter | 8d30662 | 2013-02-26 12:27:24 -0600 | [diff] [blame] | 1002 | .of_match_table = of_match_ptr(omap_dma_match), |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 1003 | }, |
| 1004 | }; |
| 1005 | |
| 1006 | bool omap_dma_filter_fn(struct dma_chan *chan, void *param) |
| 1007 | { |
| 1008 | if (chan->device->dev->driver == &omap_dma_driver.driver) { |
| 1009 | struct omap_chan *c = to_omap_dma_chan(chan); |
| 1010 | unsigned req = *(unsigned *)param; |
| 1011 | |
| 1012 | return req == c->dma_sig; |
| 1013 | } |
| 1014 | return false; |
| 1015 | } |
| 1016 | EXPORT_SYMBOL_GPL(omap_dma_filter_fn); |
| 1017 | |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 1018 | static int omap_dma_init(void) |
| 1019 | { |
Tony Lindgren | be1f948 | 2013-01-11 11:24:19 -0800 | [diff] [blame] | 1020 | return platform_driver_register(&omap_dma_driver); |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 1021 | } |
| 1022 | subsys_initcall(omap_dma_init); |
| 1023 | |
| 1024 | static void __exit omap_dma_exit(void) |
| 1025 | { |
Russell King | 7bedaa5 | 2012-04-13 12:10:24 +0100 | [diff] [blame] | 1026 | platform_driver_unregister(&omap_dma_driver); |
| 1027 | } |
| 1028 | module_exit(omap_dma_exit); |
| 1029 | |
| 1030 | MODULE_AUTHOR("Russell King"); |
| 1031 | MODULE_LICENSE("GPL"); |