Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 1 | /* |
| 2 | * TI EDMA DMA engine driver |
| 3 | * |
| 4 | * Copyright 2012 Texas Instruments |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation version 2. |
| 9 | * |
| 10 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any |
| 11 | * kind, whether express or implied; without even the implied warranty |
| 12 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/dmaengine.h> |
| 17 | #include <linux/dma-mapping.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/list.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/spinlock.h> |
| 26 | |
Matt Porter | 3ad7a42 | 2013-03-06 11:15:31 -0500 | [diff] [blame] | 27 | #include <linux/platform_data/edma.h> |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 28 | |
| 29 | #include "dmaengine.h" |
| 30 | #include "virt-dma.h" |
| 31 | |
| 32 | /* |
| 33 | * This will go away when the private EDMA API is folded |
| 34 | * into this driver and the platform device(s) are |
| 35 | * instantiated in the arch code. We can only get away |
| 36 | * with this simplification because DA8XX may not be built |
| 37 | * in the same kernel image with other DaVinci parts. This |
| 38 | * avoids having to sprinkle dmaengine driver platform devices |
| 39 | * and data throughout all the existing board files. |
| 40 | */ |
| 41 | #ifdef CONFIG_ARCH_DAVINCI_DA8XX |
| 42 | #define EDMA_CTLRS 2 |
| 43 | #define EDMA_CHANS 32 |
| 44 | #else |
| 45 | #define EDMA_CTLRS 1 |
| 46 | #define EDMA_CHANS 64 |
| 47 | #endif /* CONFIG_ARCH_DAVINCI_DA8XX */ |
| 48 | |
Joel Fernandes | 2abd5f1 | 2013-09-23 18:05:15 -0500 | [diff] [blame] | 49 | /* |
| 50 | * Max of 20 segments per channel to conserve PaRAM slots |
| 51 | * Also note that MAX_NR_SG should be atleast the no.of periods |
| 52 | * that are required for ASoC, otherwise DMA prep calls will |
| 53 | * fail. Today davinci-pcm is the only user of this driver and |
| 54 | * requires atleast 17 slots, so we setup the default to 20. |
| 55 | */ |
| 56 | #define MAX_NR_SG 20 |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 57 | #define EDMA_MAX_SLOTS MAX_NR_SG |
| 58 | #define EDMA_DESCRIPTORS 16 |
| 59 | |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 60 | struct edma_pset { |
Thomas Gleixner | c2da234 | 2014-04-28 14:29:57 -0500 | [diff] [blame^] | 61 | u32 len; |
| 62 | dma_addr_t addr; |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 63 | struct edmacc_param param; |
| 64 | }; |
| 65 | |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 66 | struct edma_desc { |
| 67 | struct virt_dma_desc vdesc; |
| 68 | struct list_head node; |
Thomas Gleixner | c2da234 | 2014-04-28 14:29:57 -0500 | [diff] [blame^] | 69 | enum dma_transfer_direction direction; |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 70 | int cyclic; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 71 | int absync; |
| 72 | int pset_nr; |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 73 | int processed; |
Thomas Gleixner | b6205c3 | 2014-04-28 14:18:45 -0500 | [diff] [blame] | 74 | u32 residue; |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 75 | struct edma_pset pset[0]; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | struct edma_cc; |
| 79 | |
| 80 | struct edma_chan { |
| 81 | struct virt_dma_chan vchan; |
| 82 | struct list_head node; |
| 83 | struct edma_desc *edesc; |
| 84 | struct edma_cc *ecc; |
| 85 | int ch_num; |
| 86 | bool alloced; |
| 87 | int slot[EDMA_MAX_SLOTS]; |
Joel Fernandes | c5f4799 | 2013-08-29 18:05:43 -0500 | [diff] [blame] | 88 | int missed; |
Matt Porter | 661f7cb | 2013-01-10 13:41:04 -0500 | [diff] [blame] | 89 | struct dma_slave_config cfg; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | struct edma_cc { |
| 93 | int ctlr; |
| 94 | struct dma_device dma_slave; |
| 95 | struct edma_chan slave_chans[EDMA_CHANS]; |
| 96 | int num_slave_chans; |
| 97 | int dummy_slot; |
| 98 | }; |
| 99 | |
| 100 | static inline struct edma_cc *to_edma_cc(struct dma_device *d) |
| 101 | { |
| 102 | return container_of(d, struct edma_cc, dma_slave); |
| 103 | } |
| 104 | |
| 105 | static inline struct edma_chan *to_edma_chan(struct dma_chan *c) |
| 106 | { |
| 107 | return container_of(c, struct edma_chan, vchan.chan); |
| 108 | } |
| 109 | |
| 110 | static inline struct edma_desc |
| 111 | *to_edma_desc(struct dma_async_tx_descriptor *tx) |
| 112 | { |
| 113 | return container_of(tx, struct edma_desc, vdesc.tx); |
| 114 | } |
| 115 | |
| 116 | static void edma_desc_free(struct virt_dma_desc *vdesc) |
| 117 | { |
| 118 | kfree(container_of(vdesc, struct edma_desc, vdesc)); |
| 119 | } |
| 120 | |
| 121 | /* Dispatch a queued descriptor to the controller (caller holds lock) */ |
| 122 | static void edma_execute(struct edma_chan *echan) |
| 123 | { |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 124 | struct virt_dma_desc *vdesc; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 125 | struct edma_desc *edesc; |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 126 | struct device *dev = echan->vchan.chan.device->dev; |
| 127 | int i, j, left, nslots; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 128 | |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 129 | /* If either we processed all psets or we're still not started */ |
| 130 | if (!echan->edesc || |
| 131 | echan->edesc->pset_nr == echan->edesc->processed) { |
| 132 | /* Get next vdesc */ |
| 133 | vdesc = vchan_next_desc(&echan->vchan); |
| 134 | if (!vdesc) { |
| 135 | echan->edesc = NULL; |
| 136 | return; |
| 137 | } |
| 138 | list_del(&vdesc->node); |
| 139 | echan->edesc = to_edma_desc(&vdesc->tx); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 142 | edesc = echan->edesc; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 143 | |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 144 | /* Find out how many left */ |
| 145 | left = edesc->pset_nr - edesc->processed; |
| 146 | nslots = min(MAX_NR_SG, left); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 147 | |
| 148 | /* Write descriptor PaRAM set(s) */ |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 149 | for (i = 0; i < nslots; i++) { |
| 150 | j = i + edesc->processed; |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 151 | edma_write_slot(echan->slot[i], &edesc->pset[j].param); |
Peter Ujfalusi | 83bb312 | 2014-04-14 14:42:02 +0300 | [diff] [blame] | 152 | dev_vdbg(echan->vchan.chan.device->dev, |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 153 | "\n pset[%d]:\n" |
| 154 | " chnum\t%d\n" |
| 155 | " slot\t%d\n" |
| 156 | " opt\t%08x\n" |
| 157 | " src\t%08x\n" |
| 158 | " dst\t%08x\n" |
| 159 | " abcnt\t%08x\n" |
| 160 | " ccnt\t%08x\n" |
| 161 | " bidx\t%08x\n" |
| 162 | " cidx\t%08x\n" |
| 163 | " lkrld\t%08x\n", |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 164 | j, echan->ch_num, echan->slot[i], |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 165 | edesc->pset[j].param.opt, |
| 166 | edesc->pset[j].param.src, |
| 167 | edesc->pset[j].param.dst, |
| 168 | edesc->pset[j].param.a_b_cnt, |
| 169 | edesc->pset[j].param.ccnt, |
| 170 | edesc->pset[j].param.src_dst_bidx, |
| 171 | edesc->pset[j].param.src_dst_cidx, |
| 172 | edesc->pset[j].param.link_bcntrld); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 173 | /* Link to the previous slot if not the last set */ |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 174 | if (i != (nslots - 1)) |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 175 | edma_link(echan->slot[i], echan->slot[i+1]); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 176 | } |
| 177 | |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 178 | edesc->processed += nslots; |
| 179 | |
Joel Fernandes | b267b3b | 2013-08-29 18:05:44 -0500 | [diff] [blame] | 180 | /* |
| 181 | * If this is either the last set in a set of SG-list transactions |
| 182 | * then setup a link to the dummy slot, this results in all future |
| 183 | * events being absorbed and that's OK because we're done |
| 184 | */ |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 185 | if (edesc->processed == edesc->pset_nr) { |
| 186 | if (edesc->cyclic) |
| 187 | edma_link(echan->slot[nslots-1], echan->slot[1]); |
| 188 | else |
| 189 | edma_link(echan->slot[nslots-1], |
| 190 | echan->ecc->dummy_slot); |
| 191 | } |
Joel Fernandes | b267b3b | 2013-08-29 18:05:44 -0500 | [diff] [blame] | 192 | |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 193 | if (edesc->processed <= MAX_NR_SG) { |
Peter Ujfalusi | 9aac909 | 2014-04-24 10:29:50 +0300 | [diff] [blame] | 194 | dev_dbg(dev, "first transfer starting on channel %d\n", |
| 195 | echan->ch_num); |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 196 | edma_start(echan->ch_num); |
Sekhar Nori | 5fc68a6 | 2014-03-19 11:25:50 +0530 | [diff] [blame] | 197 | } else { |
| 198 | dev_dbg(dev, "chan: %d: completed %d elements, resuming\n", |
| 199 | echan->ch_num, edesc->processed); |
| 200 | edma_resume(echan->ch_num); |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 201 | } |
Joel Fernandes | c5f4799 | 2013-08-29 18:05:43 -0500 | [diff] [blame] | 202 | |
| 203 | /* |
| 204 | * This happens due to setup times between intermediate transfers |
| 205 | * in long SG lists which have to be broken up into transfers of |
| 206 | * MAX_NR_SG |
| 207 | */ |
| 208 | if (echan->missed) { |
Peter Ujfalusi | 9aac909 | 2014-04-24 10:29:50 +0300 | [diff] [blame] | 209 | dev_dbg(dev, "missed event on channel %d\n", echan->ch_num); |
Joel Fernandes | c5f4799 | 2013-08-29 18:05:43 -0500 | [diff] [blame] | 210 | edma_clean_channel(echan->ch_num); |
| 211 | edma_stop(echan->ch_num); |
| 212 | edma_start(echan->ch_num); |
| 213 | edma_trigger_channel(echan->ch_num); |
| 214 | echan->missed = 0; |
| 215 | } |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static int edma_terminate_all(struct edma_chan *echan) |
| 219 | { |
| 220 | unsigned long flags; |
| 221 | LIST_HEAD(head); |
| 222 | |
| 223 | spin_lock_irqsave(&echan->vchan.lock, flags); |
| 224 | |
| 225 | /* |
| 226 | * Stop DMA activity: we assume the callback will not be called |
| 227 | * after edma_dma() returns (even if it does, it will see |
| 228 | * echan->edesc is NULL and exit.) |
| 229 | */ |
| 230 | if (echan->edesc) { |
| 231 | echan->edesc = NULL; |
| 232 | edma_stop(echan->ch_num); |
| 233 | } |
| 234 | |
| 235 | vchan_get_all_descriptors(&echan->vchan, &head); |
| 236 | spin_unlock_irqrestore(&echan->vchan.lock, flags); |
| 237 | vchan_dma_desc_free_list(&echan->vchan, &head); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 242 | static int edma_slave_config(struct edma_chan *echan, |
Matt Porter | 661f7cb | 2013-01-10 13:41:04 -0500 | [diff] [blame] | 243 | struct dma_slave_config *cfg) |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 244 | { |
Matt Porter | 661f7cb | 2013-01-10 13:41:04 -0500 | [diff] [blame] | 245 | if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES || |
| 246 | cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES) |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 247 | return -EINVAL; |
| 248 | |
Matt Porter | 661f7cb | 2013-01-10 13:41:04 -0500 | [diff] [blame] | 249 | memcpy(&echan->cfg, cfg, sizeof(echan->cfg)); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
Peter Ujfalusi | 72c7b67 | 2014-04-14 14:41:59 +0300 | [diff] [blame] | 254 | static int edma_dma_pause(struct edma_chan *echan) |
| 255 | { |
| 256 | /* Pause/Resume only allowed with cyclic mode */ |
| 257 | if (!echan->edesc->cyclic) |
| 258 | return -EINVAL; |
| 259 | |
| 260 | edma_pause(echan->ch_num); |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static int edma_dma_resume(struct edma_chan *echan) |
| 265 | { |
| 266 | /* Pause/Resume only allowed with cyclic mode */ |
| 267 | if (!echan->edesc->cyclic) |
| 268 | return -EINVAL; |
| 269 | |
| 270 | edma_resume(echan->ch_num); |
| 271 | return 0; |
| 272 | } |
| 273 | |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 274 | static int edma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, |
| 275 | unsigned long arg) |
| 276 | { |
| 277 | int ret = 0; |
| 278 | struct dma_slave_config *config; |
| 279 | struct edma_chan *echan = to_edma_chan(chan); |
| 280 | |
| 281 | switch (cmd) { |
| 282 | case DMA_TERMINATE_ALL: |
| 283 | edma_terminate_all(echan); |
| 284 | break; |
| 285 | case DMA_SLAVE_CONFIG: |
| 286 | config = (struct dma_slave_config *)arg; |
| 287 | ret = edma_slave_config(echan, config); |
| 288 | break; |
Peter Ujfalusi | 72c7b67 | 2014-04-14 14:41:59 +0300 | [diff] [blame] | 289 | case DMA_PAUSE: |
| 290 | ret = edma_dma_pause(echan); |
| 291 | break; |
| 292 | |
| 293 | case DMA_RESUME: |
| 294 | ret = edma_dma_resume(echan); |
| 295 | break; |
| 296 | |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 297 | default: |
| 298 | ret = -ENOSYS; |
| 299 | } |
| 300 | |
| 301 | return ret; |
| 302 | } |
| 303 | |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 304 | /* |
| 305 | * A PaRAM set configuration abstraction used by other modes |
| 306 | * @chan: Channel who's PaRAM set we're configuring |
| 307 | * @pset: PaRAM set to initialize and setup. |
| 308 | * @src_addr: Source address of the DMA |
| 309 | * @dst_addr: Destination address of the DMA |
| 310 | * @burst: In units of dev_width, how much to send |
| 311 | * @dev_width: How much is the dev_width |
| 312 | * @dma_length: Total length of the DMA transfer |
| 313 | * @direction: Direction of the transfer |
| 314 | */ |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 315 | static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset, |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 316 | dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst, |
| 317 | enum dma_slave_buswidth dev_width, unsigned int dma_length, |
| 318 | enum dma_transfer_direction direction) |
| 319 | { |
| 320 | struct edma_chan *echan = to_edma_chan(chan); |
| 321 | struct device *dev = chan->device->dev; |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 322 | struct edmacc_param *param = &epset->param; |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 323 | int acnt, bcnt, ccnt, cidx; |
| 324 | int src_bidx, dst_bidx, src_cidx, dst_cidx; |
| 325 | int absync; |
| 326 | |
| 327 | acnt = dev_width; |
Peter Ujfalusi | b2b617d | 2014-04-14 14:41:58 +0300 | [diff] [blame] | 328 | |
| 329 | /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */ |
| 330 | if (!burst) |
| 331 | burst = 1; |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 332 | /* |
| 333 | * If the maxburst is equal to the fifo width, use |
| 334 | * A-synced transfers. This allows for large contiguous |
| 335 | * buffer transfers using only one PaRAM set. |
| 336 | */ |
| 337 | if (burst == 1) { |
| 338 | /* |
| 339 | * For the A-sync case, bcnt and ccnt are the remainder |
| 340 | * and quotient respectively of the division of: |
| 341 | * (dma_length / acnt) by (SZ_64K -1). This is so |
| 342 | * that in case bcnt over flows, we have ccnt to use. |
| 343 | * Note: In A-sync tranfer only, bcntrld is used, but it |
| 344 | * only applies for sg_dma_len(sg) >= SZ_64K. |
| 345 | * In this case, the best way adopted is- bccnt for the |
| 346 | * first frame will be the remainder below. Then for |
| 347 | * every successive frame, bcnt will be SZ_64K-1. This |
| 348 | * is assured as bcntrld = 0xffff in end of function. |
| 349 | */ |
| 350 | absync = false; |
| 351 | ccnt = dma_length / acnt / (SZ_64K - 1); |
| 352 | bcnt = dma_length / acnt - ccnt * (SZ_64K - 1); |
| 353 | /* |
| 354 | * If bcnt is non-zero, we have a remainder and hence an |
| 355 | * extra frame to transfer, so increment ccnt. |
| 356 | */ |
| 357 | if (bcnt) |
| 358 | ccnt++; |
| 359 | else |
| 360 | bcnt = SZ_64K - 1; |
| 361 | cidx = acnt; |
| 362 | } else { |
| 363 | /* |
| 364 | * If maxburst is greater than the fifo address_width, |
| 365 | * use AB-synced transfers where A count is the fifo |
| 366 | * address_width and B count is the maxburst. In this |
| 367 | * case, we are limited to transfers of C count frames |
| 368 | * of (address_width * maxburst) where C count is limited |
| 369 | * to SZ_64K-1. This places an upper bound on the length |
| 370 | * of an SG segment that can be handled. |
| 371 | */ |
| 372 | absync = true; |
| 373 | bcnt = burst; |
| 374 | ccnt = dma_length / (acnt * bcnt); |
| 375 | if (ccnt > (SZ_64K - 1)) { |
| 376 | dev_err(dev, "Exceeded max SG segment size\n"); |
| 377 | return -EINVAL; |
| 378 | } |
| 379 | cidx = acnt * bcnt; |
| 380 | } |
| 381 | |
Thomas Gleixner | c2da234 | 2014-04-28 14:29:57 -0500 | [diff] [blame^] | 382 | epset->len = dma_length; |
| 383 | |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 384 | if (direction == DMA_MEM_TO_DEV) { |
| 385 | src_bidx = acnt; |
| 386 | src_cidx = cidx; |
| 387 | dst_bidx = 0; |
| 388 | dst_cidx = 0; |
Thomas Gleixner | c2da234 | 2014-04-28 14:29:57 -0500 | [diff] [blame^] | 389 | epset->addr = src_addr; |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 390 | } else if (direction == DMA_DEV_TO_MEM) { |
| 391 | src_bidx = 0; |
| 392 | src_cidx = 0; |
| 393 | dst_bidx = acnt; |
| 394 | dst_cidx = cidx; |
Thomas Gleixner | c2da234 | 2014-04-28 14:29:57 -0500 | [diff] [blame^] | 395 | epset->addr = dst_addr; |
Joel Fernandes | 8cc3e30 | 2014-04-18 21:50:33 -0500 | [diff] [blame] | 396 | } else if (direction == DMA_MEM_TO_MEM) { |
| 397 | src_bidx = acnt; |
| 398 | src_cidx = cidx; |
| 399 | dst_bidx = acnt; |
| 400 | dst_cidx = cidx; |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 401 | } else { |
| 402 | dev_err(dev, "%s: direction not implemented yet\n", __func__); |
| 403 | return -EINVAL; |
| 404 | } |
| 405 | |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 406 | param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num)); |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 407 | /* Configure A or AB synchronized transfers */ |
| 408 | if (absync) |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 409 | param->opt |= SYNCDIM; |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 410 | |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 411 | param->src = src_addr; |
| 412 | param->dst = dst_addr; |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 413 | |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 414 | param->src_dst_bidx = (dst_bidx << 16) | src_bidx; |
| 415 | param->src_dst_cidx = (dst_cidx << 16) | src_cidx; |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 416 | |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 417 | param->a_b_cnt = bcnt << 16 | acnt; |
| 418 | param->ccnt = ccnt; |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 419 | /* |
| 420 | * Only time when (bcntrld) auto reload is required is for |
| 421 | * A-sync case, and in this case, a requirement of reload value |
| 422 | * of SZ_64K-1 only is assured. 'link' is initially set to NULL |
| 423 | * and then later will be populated by edma_execute. |
| 424 | */ |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 425 | param->link_bcntrld = 0xffffffff; |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 426 | return absync; |
| 427 | } |
| 428 | |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 429 | static struct dma_async_tx_descriptor *edma_prep_slave_sg( |
| 430 | struct dma_chan *chan, struct scatterlist *sgl, |
| 431 | unsigned int sg_len, enum dma_transfer_direction direction, |
| 432 | unsigned long tx_flags, void *context) |
| 433 | { |
| 434 | struct edma_chan *echan = to_edma_chan(chan); |
| 435 | struct device *dev = chan->device->dev; |
| 436 | struct edma_desc *edesc; |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 437 | dma_addr_t src_addr = 0, dst_addr = 0; |
Matt Porter | 661f7cb | 2013-01-10 13:41:04 -0500 | [diff] [blame] | 438 | enum dma_slave_buswidth dev_width; |
| 439 | u32 burst; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 440 | struct scatterlist *sg; |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 441 | int i, nslots, ret; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 442 | |
| 443 | if (unlikely(!echan || !sgl || !sg_len)) |
| 444 | return NULL; |
| 445 | |
Matt Porter | 661f7cb | 2013-01-10 13:41:04 -0500 | [diff] [blame] | 446 | if (direction == DMA_DEV_TO_MEM) { |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 447 | src_addr = echan->cfg.src_addr; |
Matt Porter | 661f7cb | 2013-01-10 13:41:04 -0500 | [diff] [blame] | 448 | dev_width = echan->cfg.src_addr_width; |
| 449 | burst = echan->cfg.src_maxburst; |
| 450 | } else if (direction == DMA_MEM_TO_DEV) { |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 451 | dst_addr = echan->cfg.dst_addr; |
Matt Porter | 661f7cb | 2013-01-10 13:41:04 -0500 | [diff] [blame] | 452 | dev_width = echan->cfg.dst_addr_width; |
| 453 | burst = echan->cfg.dst_maxburst; |
| 454 | } else { |
Peter Ujfalusi | e6fad59 | 2014-04-14 14:42:05 +0300 | [diff] [blame] | 455 | dev_err(dev, "%s: bad direction: %d\n", __func__, direction); |
Matt Porter | 661f7cb | 2013-01-10 13:41:04 -0500 | [diff] [blame] | 456 | return NULL; |
| 457 | } |
| 458 | |
| 459 | if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) { |
Peter Ujfalusi | c594c89 | 2014-04-14 14:42:03 +0300 | [diff] [blame] | 460 | dev_err(dev, "%s: Undefined slave buswidth\n", __func__); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 461 | return NULL; |
| 462 | } |
| 463 | |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 464 | edesc = kzalloc(sizeof(*edesc) + sg_len * |
| 465 | sizeof(edesc->pset[0]), GFP_ATOMIC); |
| 466 | if (!edesc) { |
Peter Ujfalusi | c594c89 | 2014-04-14 14:42:03 +0300 | [diff] [blame] | 467 | dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 468 | return NULL; |
| 469 | } |
| 470 | |
| 471 | edesc->pset_nr = sg_len; |
Thomas Gleixner | b6205c3 | 2014-04-28 14:18:45 -0500 | [diff] [blame] | 472 | edesc->residue = 0; |
Thomas Gleixner | c2da234 | 2014-04-28 14:29:57 -0500 | [diff] [blame^] | 473 | edesc->direction = direction; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 474 | |
Joel Fernandes | 6fbe24d | 2013-08-29 18:05:40 -0500 | [diff] [blame] | 475 | /* Allocate a PaRAM slot, if needed */ |
| 476 | nslots = min_t(unsigned, MAX_NR_SG, sg_len); |
| 477 | |
| 478 | for (i = 0; i < nslots; i++) { |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 479 | if (echan->slot[i] < 0) { |
| 480 | echan->slot[i] = |
| 481 | edma_alloc_slot(EDMA_CTLR(echan->ch_num), |
| 482 | EDMA_SLOT_ANY); |
| 483 | if (echan->slot[i] < 0) { |
Valentin Ilie | 4b6271a | 2013-10-24 16:14:22 +0300 | [diff] [blame] | 484 | kfree(edesc); |
Peter Ujfalusi | c594c89 | 2014-04-14 14:42:03 +0300 | [diff] [blame] | 485 | dev_err(dev, "%s: Failed to allocate slot\n", |
| 486 | __func__); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 487 | return NULL; |
| 488 | } |
| 489 | } |
Joel Fernandes | 6fbe24d | 2013-08-29 18:05:40 -0500 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /* Configure PaRAM sets for each SG */ |
| 493 | for_each_sg(sgl, sg, sg_len, i) { |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 494 | /* Get address for each SG */ |
| 495 | if (direction == DMA_DEV_TO_MEM) |
| 496 | dst_addr = sg_dma_address(sg); |
| 497 | else |
| 498 | src_addr = sg_dma_address(sg); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 499 | |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 500 | ret = edma_config_pset(chan, &edesc->pset[i], src_addr, |
| 501 | dst_addr, burst, dev_width, |
| 502 | sg_dma_len(sg), direction); |
Vinod Koul | b967aec | 2013-10-30 13:07:18 +0530 | [diff] [blame] | 503 | if (ret < 0) { |
| 504 | kfree(edesc); |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 505 | return NULL; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 506 | } |
| 507 | |
Joel Fernandes | fd00903 | 2013-09-23 18:05:13 -0500 | [diff] [blame] | 508 | edesc->absync = ret; |
Thomas Gleixner | b6205c3 | 2014-04-28 14:18:45 -0500 | [diff] [blame] | 509 | edesc->residue += sg_dma_len(sg); |
Joel Fernandes | 6fbe24d | 2013-08-29 18:05:40 -0500 | [diff] [blame] | 510 | |
| 511 | /* If this is the last in a current SG set of transactions, |
| 512 | enable interrupts so that next set is processed */ |
| 513 | if (!((i+1) % MAX_NR_SG)) |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 514 | edesc->pset[i].param.opt |= TCINTEN; |
Joel Fernandes | 6fbe24d | 2013-08-29 18:05:40 -0500 | [diff] [blame] | 515 | |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 516 | /* If this is the last set, enable completion interrupt flag */ |
| 517 | if (i == sg_len - 1) |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 518 | edesc->pset[i].param.opt |= TCINTEN; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 519 | } |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 520 | |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 521 | return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags); |
| 522 | } |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 523 | |
Joel Fernandes | 8cc3e30 | 2014-04-18 21:50:33 -0500 | [diff] [blame] | 524 | struct dma_async_tx_descriptor *edma_prep_dma_memcpy( |
| 525 | struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, |
| 526 | size_t len, unsigned long tx_flags) |
| 527 | { |
| 528 | int ret; |
| 529 | struct edma_desc *edesc; |
| 530 | struct device *dev = chan->device->dev; |
| 531 | struct edma_chan *echan = to_edma_chan(chan); |
| 532 | |
| 533 | if (unlikely(!echan || !len)) |
| 534 | return NULL; |
| 535 | |
| 536 | edesc = kzalloc(sizeof(*edesc) + sizeof(edesc->pset[0]), GFP_ATOMIC); |
| 537 | if (!edesc) { |
| 538 | dev_dbg(dev, "Failed to allocate a descriptor\n"); |
| 539 | return NULL; |
| 540 | } |
| 541 | |
| 542 | edesc->pset_nr = 1; |
| 543 | |
| 544 | ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1, |
| 545 | DMA_SLAVE_BUSWIDTH_4_BYTES, len, DMA_MEM_TO_MEM); |
| 546 | if (ret < 0) |
| 547 | return NULL; |
| 548 | |
| 549 | edesc->absync = ret; |
| 550 | |
| 551 | /* |
| 552 | * Enable intermediate transfer chaining to re-trigger channel |
| 553 | * on completion of every TR, and enable transfer-completion |
| 554 | * interrupt on completion of the whole transfer. |
| 555 | */ |
| 556 | edesc->pset[0].opt |= ITCCHEN; |
| 557 | edesc->pset[0].opt |= TCINTEN; |
| 558 | |
| 559 | return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags); |
| 560 | } |
| 561 | |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 562 | static struct dma_async_tx_descriptor *edma_prep_dma_cyclic( |
| 563 | struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len, |
| 564 | size_t period_len, enum dma_transfer_direction direction, |
| 565 | unsigned long tx_flags, void *context) |
| 566 | { |
| 567 | struct edma_chan *echan = to_edma_chan(chan); |
| 568 | struct device *dev = chan->device->dev; |
| 569 | struct edma_desc *edesc; |
| 570 | dma_addr_t src_addr, dst_addr; |
| 571 | enum dma_slave_buswidth dev_width; |
| 572 | u32 burst; |
| 573 | int i, ret, nslots; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 574 | |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 575 | if (unlikely(!echan || !buf_len || !period_len)) |
| 576 | return NULL; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 577 | |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 578 | if (direction == DMA_DEV_TO_MEM) { |
| 579 | src_addr = echan->cfg.src_addr; |
| 580 | dst_addr = buf_addr; |
| 581 | dev_width = echan->cfg.src_addr_width; |
| 582 | burst = echan->cfg.src_maxburst; |
| 583 | } else if (direction == DMA_MEM_TO_DEV) { |
| 584 | src_addr = buf_addr; |
| 585 | dst_addr = echan->cfg.dst_addr; |
| 586 | dev_width = echan->cfg.dst_addr_width; |
| 587 | burst = echan->cfg.dst_maxburst; |
| 588 | } else { |
Peter Ujfalusi | e6fad59 | 2014-04-14 14:42:05 +0300 | [diff] [blame] | 589 | dev_err(dev, "%s: bad direction: %d\n", __func__, direction); |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 590 | return NULL; |
| 591 | } |
| 592 | |
| 593 | if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) { |
Peter Ujfalusi | c594c89 | 2014-04-14 14:42:03 +0300 | [diff] [blame] | 594 | dev_err(dev, "%s: Undefined slave buswidth\n", __func__); |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 595 | return NULL; |
| 596 | } |
| 597 | |
| 598 | if (unlikely(buf_len % period_len)) { |
| 599 | dev_err(dev, "Period should be multiple of Buffer length\n"); |
| 600 | return NULL; |
| 601 | } |
| 602 | |
| 603 | nslots = (buf_len / period_len) + 1; |
| 604 | |
| 605 | /* |
| 606 | * Cyclic DMA users such as audio cannot tolerate delays introduced |
| 607 | * by cases where the number of periods is more than the maximum |
| 608 | * number of SGs the EDMA driver can handle at a time. For DMA types |
| 609 | * such as Slave SGs, such delays are tolerable and synchronized, |
| 610 | * but the synchronization is difficult to achieve with Cyclic and |
| 611 | * cannot be guaranteed, so we error out early. |
| 612 | */ |
| 613 | if (nslots > MAX_NR_SG) |
| 614 | return NULL; |
| 615 | |
| 616 | edesc = kzalloc(sizeof(*edesc) + nslots * |
| 617 | sizeof(edesc->pset[0]), GFP_ATOMIC); |
| 618 | if (!edesc) { |
Peter Ujfalusi | c594c89 | 2014-04-14 14:42:03 +0300 | [diff] [blame] | 619 | dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__); |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 620 | return NULL; |
| 621 | } |
| 622 | |
| 623 | edesc->cyclic = 1; |
| 624 | edesc->pset_nr = nslots; |
Thomas Gleixner | b6205c3 | 2014-04-28 14:18:45 -0500 | [diff] [blame] | 625 | edesc->residue = buf_len; |
Thomas Gleixner | c2da234 | 2014-04-28 14:29:57 -0500 | [diff] [blame^] | 626 | edesc->direction = direction; |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 627 | |
Peter Ujfalusi | 83bb312 | 2014-04-14 14:42:02 +0300 | [diff] [blame] | 628 | dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n", |
| 629 | __func__, echan->ch_num, nslots, period_len, buf_len); |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 630 | |
| 631 | for (i = 0; i < nslots; i++) { |
| 632 | /* Allocate a PaRAM slot, if needed */ |
| 633 | if (echan->slot[i] < 0) { |
| 634 | echan->slot[i] = |
| 635 | edma_alloc_slot(EDMA_CTLR(echan->ch_num), |
| 636 | EDMA_SLOT_ANY); |
| 637 | if (echan->slot[i] < 0) { |
Christian Engelmayer | e3ddc97 | 2013-12-30 20:48:39 +0100 | [diff] [blame] | 638 | kfree(edesc); |
Peter Ujfalusi | c594c89 | 2014-04-14 14:42:03 +0300 | [diff] [blame] | 639 | dev_err(dev, "%s: Failed to allocate slot\n", |
| 640 | __func__); |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 641 | return NULL; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | if (i == nslots - 1) { |
| 646 | memcpy(&edesc->pset[i], &edesc->pset[0], |
| 647 | sizeof(edesc->pset[0])); |
| 648 | break; |
| 649 | } |
| 650 | |
| 651 | ret = edma_config_pset(chan, &edesc->pset[i], src_addr, |
| 652 | dst_addr, burst, dev_width, period_len, |
| 653 | direction); |
Christian Engelmayer | e3ddc97 | 2013-12-30 20:48:39 +0100 | [diff] [blame] | 654 | if (ret < 0) { |
| 655 | kfree(edesc); |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 656 | return NULL; |
Christian Engelmayer | e3ddc97 | 2013-12-30 20:48:39 +0100 | [diff] [blame] | 657 | } |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 658 | |
| 659 | if (direction == DMA_DEV_TO_MEM) |
| 660 | dst_addr += period_len; |
| 661 | else |
| 662 | src_addr += period_len; |
| 663 | |
Peter Ujfalusi | 83bb312 | 2014-04-14 14:42:02 +0300 | [diff] [blame] | 664 | dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i); |
| 665 | dev_vdbg(dev, |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 666 | "\n pset[%d]:\n" |
| 667 | " chnum\t%d\n" |
| 668 | " slot\t%d\n" |
| 669 | " opt\t%08x\n" |
| 670 | " src\t%08x\n" |
| 671 | " dst\t%08x\n" |
| 672 | " abcnt\t%08x\n" |
| 673 | " ccnt\t%08x\n" |
| 674 | " bidx\t%08x\n" |
| 675 | " cidx\t%08x\n" |
| 676 | " lkrld\t%08x\n", |
| 677 | i, echan->ch_num, echan->slot[i], |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 678 | edesc->pset[i].param.opt, |
| 679 | edesc->pset[i].param.src, |
| 680 | edesc->pset[i].param.dst, |
| 681 | edesc->pset[i].param.a_b_cnt, |
| 682 | edesc->pset[i].param.ccnt, |
| 683 | edesc->pset[i].param.src_dst_bidx, |
| 684 | edesc->pset[i].param.src_dst_cidx, |
| 685 | edesc->pset[i].param.link_bcntrld); |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 686 | |
| 687 | edesc->absync = ret; |
| 688 | |
| 689 | /* |
| 690 | * Enable interrupts for every period because callback |
| 691 | * has to be called for every period. |
| 692 | */ |
Thomas Gleixner | b5088ad | 2014-04-28 14:23:55 -0500 | [diff] [blame] | 693 | edesc->pset[i].param.opt |= TCINTEN; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags); |
| 697 | } |
| 698 | |
| 699 | static void edma_callback(unsigned ch_num, u16 ch_status, void *data) |
| 700 | { |
| 701 | struct edma_chan *echan = data; |
| 702 | struct device *dev = echan->vchan.chan.device->dev; |
| 703 | struct edma_desc *edesc; |
Joel Fernandes | c5f4799 | 2013-08-29 18:05:43 -0500 | [diff] [blame] | 704 | struct edmacc_param p; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 705 | |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 706 | edesc = echan->edesc; |
| 707 | |
| 708 | /* Pause the channel for non-cyclic */ |
| 709 | if (!edesc || (edesc && !edesc->cyclic)) |
| 710 | edma_pause(echan->ch_num); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 711 | |
| 712 | switch (ch_status) { |
Vinod Koul | db60d8d | 2013-10-30 18:22:30 +0530 | [diff] [blame] | 713 | case EDMA_DMA_COMPLETE: |
Joel Fernandes | 406efb1 | 2014-04-17 00:58:33 -0500 | [diff] [blame] | 714 | spin_lock(&echan->vchan.lock); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 715 | |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 716 | if (edesc) { |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 717 | if (edesc->cyclic) { |
| 718 | vchan_cyclic_callback(&edesc->vdesc); |
| 719 | } else if (edesc->processed == edesc->pset_nr) { |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 720 | dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num); |
Thomas Gleixner | b6205c3 | 2014-04-28 14:18:45 -0500 | [diff] [blame] | 721 | edesc->residue = 0; |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 722 | edma_stop(echan->ch_num); |
| 723 | vchan_cookie_complete(&edesc->vdesc); |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 724 | edma_execute(echan); |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 725 | } else { |
| 726 | dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num); |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 727 | edma_execute(echan); |
Joel Fernandes | 5340706 | 2013-09-03 10:02:46 -0500 | [diff] [blame] | 728 | } |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 729 | } |
| 730 | |
Joel Fernandes | 406efb1 | 2014-04-17 00:58:33 -0500 | [diff] [blame] | 731 | spin_unlock(&echan->vchan.lock); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 732 | |
| 733 | break; |
Vinod Koul | db60d8d | 2013-10-30 18:22:30 +0530 | [diff] [blame] | 734 | case EDMA_DMA_CC_ERROR: |
Joel Fernandes | 406efb1 | 2014-04-17 00:58:33 -0500 | [diff] [blame] | 735 | spin_lock(&echan->vchan.lock); |
Joel Fernandes | c5f4799 | 2013-08-29 18:05:43 -0500 | [diff] [blame] | 736 | |
| 737 | edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p); |
| 738 | |
| 739 | /* |
| 740 | * Issue later based on missed flag which will be sure |
| 741 | * to happen as: |
| 742 | * (1) we finished transmitting an intermediate slot and |
| 743 | * edma_execute is coming up. |
| 744 | * (2) or we finished current transfer and issue will |
| 745 | * call edma_execute. |
| 746 | * |
| 747 | * Important note: issuing can be dangerous here and |
| 748 | * lead to some nasty recursion when we are in a NULL |
| 749 | * slot. So we avoid doing so and set the missed flag. |
| 750 | */ |
| 751 | if (p.a_b_cnt == 0 && p.ccnt == 0) { |
| 752 | dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n"); |
| 753 | echan->missed = 1; |
| 754 | } else { |
| 755 | /* |
| 756 | * The slot is already programmed but the event got |
| 757 | * missed, so its safe to issue it here. |
| 758 | */ |
| 759 | dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n"); |
| 760 | edma_clean_channel(echan->ch_num); |
| 761 | edma_stop(echan->ch_num); |
| 762 | edma_start(echan->ch_num); |
| 763 | edma_trigger_channel(echan->ch_num); |
| 764 | } |
| 765 | |
Joel Fernandes | 406efb1 | 2014-04-17 00:58:33 -0500 | [diff] [blame] | 766 | spin_unlock(&echan->vchan.lock); |
Joel Fernandes | c5f4799 | 2013-08-29 18:05:43 -0500 | [diff] [blame] | 767 | |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 768 | break; |
| 769 | default: |
| 770 | break; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | /* Alloc channel resources */ |
| 775 | static int edma_alloc_chan_resources(struct dma_chan *chan) |
| 776 | { |
| 777 | struct edma_chan *echan = to_edma_chan(chan); |
| 778 | struct device *dev = chan->device->dev; |
| 779 | int ret; |
| 780 | int a_ch_num; |
| 781 | LIST_HEAD(descs); |
| 782 | |
| 783 | a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback, |
| 784 | chan, EVENTQ_DEFAULT); |
| 785 | |
| 786 | if (a_ch_num < 0) { |
| 787 | ret = -ENODEV; |
| 788 | goto err_no_chan; |
| 789 | } |
| 790 | |
| 791 | if (a_ch_num != echan->ch_num) { |
| 792 | dev_err(dev, "failed to allocate requested channel %u:%u\n", |
| 793 | EDMA_CTLR(echan->ch_num), |
| 794 | EDMA_CHAN_SLOT(echan->ch_num)); |
| 795 | ret = -ENODEV; |
| 796 | goto err_wrong_chan; |
| 797 | } |
| 798 | |
| 799 | echan->alloced = true; |
| 800 | echan->slot[0] = echan->ch_num; |
| 801 | |
Peter Ujfalusi | 9aac909 | 2014-04-24 10:29:50 +0300 | [diff] [blame] | 802 | dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num, |
Ezequiel Garcia | 0e772c6 | 2013-12-13 11:06:18 -0300 | [diff] [blame] | 803 | EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num)); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 804 | |
| 805 | return 0; |
| 806 | |
| 807 | err_wrong_chan: |
| 808 | edma_free_channel(a_ch_num); |
| 809 | err_no_chan: |
| 810 | return ret; |
| 811 | } |
| 812 | |
| 813 | /* Free channel resources */ |
| 814 | static void edma_free_chan_resources(struct dma_chan *chan) |
| 815 | { |
| 816 | struct edma_chan *echan = to_edma_chan(chan); |
| 817 | struct device *dev = chan->device->dev; |
| 818 | int i; |
| 819 | |
| 820 | /* Terminate transfers */ |
| 821 | edma_stop(echan->ch_num); |
| 822 | |
| 823 | vchan_free_chan_resources(&echan->vchan); |
| 824 | |
| 825 | /* Free EDMA PaRAM slots */ |
| 826 | for (i = 1; i < EDMA_MAX_SLOTS; i++) { |
| 827 | if (echan->slot[i] >= 0) { |
| 828 | edma_free_slot(echan->slot[i]); |
| 829 | echan->slot[i] = -1; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | /* Free EDMA channel */ |
| 834 | if (echan->alloced) { |
| 835 | edma_free_channel(echan->ch_num); |
| 836 | echan->alloced = false; |
| 837 | } |
| 838 | |
Ezequiel Garcia | 0e772c6 | 2013-12-13 11:06:18 -0300 | [diff] [blame] | 839 | dev_dbg(dev, "freeing channel for %u\n", echan->ch_num); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /* Send pending descriptor to hardware */ |
| 843 | static void edma_issue_pending(struct dma_chan *chan) |
| 844 | { |
| 845 | struct edma_chan *echan = to_edma_chan(chan); |
| 846 | unsigned long flags; |
| 847 | |
| 848 | spin_lock_irqsave(&echan->vchan.lock, flags); |
| 849 | if (vchan_issue_pending(&echan->vchan) && !echan->edesc) |
| 850 | edma_execute(echan); |
| 851 | spin_unlock_irqrestore(&echan->vchan.lock, flags); |
| 852 | } |
| 853 | |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 854 | /* Check request completion status */ |
| 855 | static enum dma_status edma_tx_status(struct dma_chan *chan, |
| 856 | dma_cookie_t cookie, |
| 857 | struct dma_tx_state *txstate) |
| 858 | { |
| 859 | struct edma_chan *echan = to_edma_chan(chan); |
| 860 | struct virt_dma_desc *vdesc; |
| 861 | enum dma_status ret; |
| 862 | unsigned long flags; |
| 863 | |
| 864 | ret = dma_cookie_status(chan, cookie, txstate); |
Vinod Koul | 9d386ec | 2013-10-16 13:42:15 +0530 | [diff] [blame] | 865 | if (ret == DMA_COMPLETE || !txstate) |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 866 | return ret; |
| 867 | |
| 868 | spin_lock_irqsave(&echan->vchan.lock, flags); |
Thomas Gleixner | de13593 | 2014-04-28 14:19:51 -0500 | [diff] [blame] | 869 | if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie) |
Thomas Gleixner | b6205c3 | 2014-04-28 14:18:45 -0500 | [diff] [blame] | 870 | txstate->residue = echan->edesc->residue; |
Thomas Gleixner | de13593 | 2014-04-28 14:19:51 -0500 | [diff] [blame] | 871 | else if ((vdesc = vchan_find_desc(&echan->vchan, cookie))) |
| 872 | txstate->residue = to_edma_desc(&vdesc->tx)->residue; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 873 | spin_unlock_irqrestore(&echan->vchan.lock, flags); |
| 874 | |
| 875 | return ret; |
| 876 | } |
| 877 | |
| 878 | static void __init edma_chan_init(struct edma_cc *ecc, |
| 879 | struct dma_device *dma, |
| 880 | struct edma_chan *echans) |
| 881 | { |
| 882 | int i, j; |
| 883 | |
| 884 | for (i = 0; i < EDMA_CHANS; i++) { |
| 885 | struct edma_chan *echan = &echans[i]; |
| 886 | echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i); |
| 887 | echan->ecc = ecc; |
| 888 | echan->vchan.desc_free = edma_desc_free; |
| 889 | |
| 890 | vchan_init(&echan->vchan, dma); |
| 891 | |
| 892 | INIT_LIST_HEAD(&echan->node); |
| 893 | for (j = 0; j < EDMA_MAX_SLOTS; j++) |
| 894 | echan->slot[j] = -1; |
| 895 | } |
| 896 | } |
| 897 | |
Peter Ujfalusi | 2c88ee6 | 2014-04-14 14:42:01 +0300 | [diff] [blame] | 898 | #define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \ |
| 899 | BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \ |
| 900 | BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)) |
| 901 | |
| 902 | static int edma_dma_device_slave_caps(struct dma_chan *dchan, |
| 903 | struct dma_slave_caps *caps) |
| 904 | { |
| 905 | caps->src_addr_widths = EDMA_DMA_BUSWIDTHS; |
| 906 | caps->dstn_addr_widths = EDMA_DMA_BUSWIDTHS; |
| 907 | caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV); |
| 908 | caps->cmd_pause = true; |
| 909 | caps->cmd_terminate = true; |
| 910 | caps->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR; |
| 911 | |
| 912 | return 0; |
| 913 | } |
| 914 | |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 915 | static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma, |
| 916 | struct device *dev) |
| 917 | { |
| 918 | dma->device_prep_slave_sg = edma_prep_slave_sg; |
Joel Fernandes | 50a9c70 | 2013-10-31 16:31:23 -0500 | [diff] [blame] | 919 | dma->device_prep_dma_cyclic = edma_prep_dma_cyclic; |
Joel Fernandes | 8cc3e30 | 2014-04-18 21:50:33 -0500 | [diff] [blame] | 920 | dma->device_prep_dma_memcpy = edma_prep_dma_memcpy; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 921 | dma->device_alloc_chan_resources = edma_alloc_chan_resources; |
| 922 | dma->device_free_chan_resources = edma_free_chan_resources; |
| 923 | dma->device_issue_pending = edma_issue_pending; |
| 924 | dma->device_tx_status = edma_tx_status; |
| 925 | dma->device_control = edma_control; |
Peter Ujfalusi | 2c88ee6 | 2014-04-14 14:42:01 +0300 | [diff] [blame] | 926 | dma->device_slave_caps = edma_dma_device_slave_caps; |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 927 | dma->dev = dev; |
| 928 | |
Joel Fernandes | 8cc3e30 | 2014-04-18 21:50:33 -0500 | [diff] [blame] | 929 | /* |
| 930 | * code using dma memcpy must make sure alignment of |
| 931 | * length is at dma->copy_align boundary. |
| 932 | */ |
| 933 | dma->copy_align = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 934 | |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 935 | INIT_LIST_HEAD(&dma->channels); |
| 936 | } |
| 937 | |
Bill Pemberton | 463a1f8 | 2012-11-19 13:22:55 -0500 | [diff] [blame] | 938 | static int edma_probe(struct platform_device *pdev) |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 939 | { |
| 940 | struct edma_cc *ecc; |
| 941 | int ret; |
| 942 | |
Russell King | 94cb0e7 | 2013-06-27 13:45:16 +0100 | [diff] [blame] | 943 | ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); |
| 944 | if (ret) |
| 945 | return ret; |
| 946 | |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 947 | ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL); |
| 948 | if (!ecc) { |
| 949 | dev_err(&pdev->dev, "Can't allocate controller\n"); |
| 950 | return -ENOMEM; |
| 951 | } |
| 952 | |
| 953 | ecc->ctlr = pdev->id; |
| 954 | ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY); |
| 955 | if (ecc->dummy_slot < 0) { |
| 956 | dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n"); |
| 957 | return -EIO; |
| 958 | } |
| 959 | |
| 960 | dma_cap_zero(ecc->dma_slave.cap_mask); |
| 961 | dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask); |
Peter Ujfalusi | 232b223d | 2014-04-14 14:42:00 +0300 | [diff] [blame] | 962 | dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask); |
Joel Fernandes | 8cc3e30 | 2014-04-18 21:50:33 -0500 | [diff] [blame] | 963 | dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 964 | |
| 965 | edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev); |
| 966 | |
| 967 | edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans); |
| 968 | |
| 969 | ret = dma_async_device_register(&ecc->dma_slave); |
| 970 | if (ret) |
| 971 | goto err_reg1; |
| 972 | |
| 973 | platform_set_drvdata(pdev, ecc); |
| 974 | |
| 975 | dev_info(&pdev->dev, "TI EDMA DMA engine driver\n"); |
| 976 | |
| 977 | return 0; |
| 978 | |
| 979 | err_reg1: |
| 980 | edma_free_slot(ecc->dummy_slot); |
| 981 | return ret; |
| 982 | } |
| 983 | |
Greg Kroah-Hartman | 4bf27b8 | 2012-12-21 15:09:59 -0800 | [diff] [blame] | 984 | static int edma_remove(struct platform_device *pdev) |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 985 | { |
| 986 | struct device *dev = &pdev->dev; |
| 987 | struct edma_cc *ecc = dev_get_drvdata(dev); |
| 988 | |
| 989 | dma_async_device_unregister(&ecc->dma_slave); |
| 990 | edma_free_slot(ecc->dummy_slot); |
| 991 | |
| 992 | return 0; |
| 993 | } |
| 994 | |
| 995 | static struct platform_driver edma_driver = { |
| 996 | .probe = edma_probe, |
Bill Pemberton | a7d6e3e | 2012-11-19 13:20:04 -0500 | [diff] [blame] | 997 | .remove = edma_remove, |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 998 | .driver = { |
| 999 | .name = "edma-dma-engine", |
| 1000 | .owner = THIS_MODULE, |
| 1001 | }, |
| 1002 | }; |
| 1003 | |
| 1004 | bool edma_filter_fn(struct dma_chan *chan, void *param) |
| 1005 | { |
| 1006 | if (chan->device->dev->driver == &edma_driver.driver) { |
| 1007 | struct edma_chan *echan = to_edma_chan(chan); |
| 1008 | unsigned ch_req = *(unsigned *)param; |
| 1009 | return ch_req == echan->ch_num; |
| 1010 | } |
| 1011 | return false; |
| 1012 | } |
| 1013 | EXPORT_SYMBOL(edma_filter_fn); |
| 1014 | |
| 1015 | static struct platform_device *pdev0, *pdev1; |
| 1016 | |
| 1017 | static const struct platform_device_info edma_dev_info0 = { |
| 1018 | .name = "edma-dma-engine", |
| 1019 | .id = 0, |
Russell King | 94cb0e7 | 2013-06-27 13:45:16 +0100 | [diff] [blame] | 1020 | .dma_mask = DMA_BIT_MASK(32), |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 1021 | }; |
| 1022 | |
| 1023 | static const struct platform_device_info edma_dev_info1 = { |
| 1024 | .name = "edma-dma-engine", |
| 1025 | .id = 1, |
Russell King | 94cb0e7 | 2013-06-27 13:45:16 +0100 | [diff] [blame] | 1026 | .dma_mask = DMA_BIT_MASK(32), |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 1027 | }; |
| 1028 | |
| 1029 | static int edma_init(void) |
| 1030 | { |
| 1031 | int ret = platform_driver_register(&edma_driver); |
| 1032 | |
| 1033 | if (ret == 0) { |
| 1034 | pdev0 = platform_device_register_full(&edma_dev_info0); |
| 1035 | if (IS_ERR(pdev0)) { |
| 1036 | platform_driver_unregister(&edma_driver); |
| 1037 | ret = PTR_ERR(pdev0); |
| 1038 | goto out; |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | if (EDMA_CTLRS == 2) { |
| 1043 | pdev1 = platform_device_register_full(&edma_dev_info1); |
| 1044 | if (IS_ERR(pdev1)) { |
| 1045 | platform_driver_unregister(&edma_driver); |
| 1046 | platform_device_unregister(pdev0); |
| 1047 | ret = PTR_ERR(pdev1); |
| 1048 | } |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | out: |
| 1052 | return ret; |
| 1053 | } |
| 1054 | subsys_initcall(edma_init); |
| 1055 | |
| 1056 | static void __exit edma_exit(void) |
| 1057 | { |
| 1058 | platform_device_unregister(pdev0); |
| 1059 | if (pdev1) |
| 1060 | platform_device_unregister(pdev1); |
| 1061 | platform_driver_unregister(&edma_driver); |
| 1062 | } |
| 1063 | module_exit(edma_exit); |
| 1064 | |
Josh Boyer | d71505b | 2013-09-04 10:32:50 -0400 | [diff] [blame] | 1065 | MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>"); |
Matt Porter | c2dde5f | 2012-08-22 21:09:34 -0400 | [diff] [blame] | 1066 | MODULE_DESCRIPTION("TI EDMA DMA engine driver"); |
| 1067 | MODULE_LICENSE("GPL v2"); |