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