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