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