M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Driver for STM32 DMA controller |
| 3 | * |
| 4 | * Inspired by dma-jz4740.c and tegra20-apb-dma.c |
| 5 | * |
| 6 | * Copyright (C) M'boumba Cedric Madianga 2015 |
| 7 | * Author: M'boumba Cedric Madianga <cedric.madianga@gmail.com> |
| 8 | * |
| 9 | * License terms: GNU General Public License (GPL), version 2 |
| 10 | */ |
| 11 | |
| 12 | #include <linux/clk.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/dmaengine.h> |
| 15 | #include <linux/dma-mapping.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/jiffies.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/of.h> |
| 22 | #include <linux/of_device.h> |
| 23 | #include <linux/of_dma.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/reset.h> |
| 26 | #include <linux/sched.h> |
| 27 | #include <linux/slab.h> |
| 28 | |
| 29 | #include "virt-dma.h" |
| 30 | |
| 31 | #define STM32_DMA_LISR 0x0000 /* DMA Low Int Status Reg */ |
| 32 | #define STM32_DMA_HISR 0x0004 /* DMA High Int Status Reg */ |
| 33 | #define STM32_DMA_LIFCR 0x0008 /* DMA Low Int Flag Clear Reg */ |
| 34 | #define STM32_DMA_HIFCR 0x000c /* DMA High Int Flag Clear Reg */ |
| 35 | #define STM32_DMA_TCI BIT(5) /* Transfer Complete Interrupt */ |
| 36 | #define STM32_DMA_TEI BIT(3) /* Transfer Error Interrupt */ |
| 37 | #define STM32_DMA_DMEI BIT(2) /* Direct Mode Error Interrupt */ |
| 38 | #define STM32_DMA_FEI BIT(0) /* FIFO Error Interrupt */ |
| 39 | |
| 40 | /* DMA Stream x Configuration Register */ |
| 41 | #define STM32_DMA_SCR(x) (0x0010 + 0x18 * (x)) /* x = 0..7 */ |
| 42 | #define STM32_DMA_SCR_REQ(n) ((n & 0x7) << 25) |
| 43 | #define STM32_DMA_SCR_MBURST_MASK GENMASK(24, 23) |
| 44 | #define STM32_DMA_SCR_MBURST(n) ((n & 0x3) << 23) |
| 45 | #define STM32_DMA_SCR_PBURST_MASK GENMASK(22, 21) |
| 46 | #define STM32_DMA_SCR_PBURST(n) ((n & 0x3) << 21) |
| 47 | #define STM32_DMA_SCR_PL_MASK GENMASK(17, 16) |
| 48 | #define STM32_DMA_SCR_PL(n) ((n & 0x3) << 16) |
| 49 | #define STM32_DMA_SCR_MSIZE_MASK GENMASK(14, 13) |
| 50 | #define STM32_DMA_SCR_MSIZE(n) ((n & 0x3) << 13) |
| 51 | #define STM32_DMA_SCR_PSIZE_MASK GENMASK(12, 11) |
| 52 | #define STM32_DMA_SCR_PSIZE(n) ((n & 0x3) << 11) |
| 53 | #define STM32_DMA_SCR_PSIZE_GET(n) ((n & STM32_DMA_SCR_PSIZE_MASK) >> 11) |
| 54 | #define STM32_DMA_SCR_DIR_MASK GENMASK(7, 6) |
| 55 | #define STM32_DMA_SCR_DIR(n) ((n & 0x3) << 6) |
| 56 | #define STM32_DMA_SCR_CT BIT(19) /* Target in double buffer */ |
| 57 | #define STM32_DMA_SCR_DBM BIT(18) /* Double Buffer Mode */ |
| 58 | #define STM32_DMA_SCR_PINCOS BIT(15) /* Peripheral inc offset size */ |
| 59 | #define STM32_DMA_SCR_MINC BIT(10) /* Memory increment mode */ |
| 60 | #define STM32_DMA_SCR_PINC BIT(9) /* Peripheral increment mode */ |
| 61 | #define STM32_DMA_SCR_CIRC BIT(8) /* Circular mode */ |
| 62 | #define STM32_DMA_SCR_PFCTRL BIT(5) /* Peripheral Flow Controller */ |
| 63 | #define STM32_DMA_SCR_TCIE BIT(4) /* Transfer Cplete Int Enable*/ |
| 64 | #define STM32_DMA_SCR_TEIE BIT(2) /* Transfer Error Int Enable */ |
| 65 | #define STM32_DMA_SCR_DMEIE BIT(1) /* Direct Mode Err Int Enable */ |
| 66 | #define STM32_DMA_SCR_EN BIT(0) /* Stream Enable */ |
| 67 | #define STM32_DMA_SCR_CFG_MASK (STM32_DMA_SCR_PINC \ |
| 68 | | STM32_DMA_SCR_MINC \ |
| 69 | | STM32_DMA_SCR_PINCOS \ |
| 70 | | STM32_DMA_SCR_PL_MASK) |
| 71 | #define STM32_DMA_SCR_IRQ_MASK (STM32_DMA_SCR_TCIE \ |
| 72 | | STM32_DMA_SCR_TEIE \ |
| 73 | | STM32_DMA_SCR_DMEIE) |
| 74 | |
| 75 | /* DMA Stream x number of data register */ |
| 76 | #define STM32_DMA_SNDTR(x) (0x0014 + 0x18 * (x)) |
| 77 | |
| 78 | /* DMA stream peripheral address register */ |
| 79 | #define STM32_DMA_SPAR(x) (0x0018 + 0x18 * (x)) |
| 80 | |
| 81 | /* DMA stream x memory 0 address register */ |
| 82 | #define STM32_DMA_SM0AR(x) (0x001c + 0x18 * (x)) |
| 83 | |
| 84 | /* DMA stream x memory 1 address register */ |
| 85 | #define STM32_DMA_SM1AR(x) (0x0020 + 0x18 * (x)) |
| 86 | |
| 87 | /* DMA stream x FIFO control register */ |
| 88 | #define STM32_DMA_SFCR(x) (0x0024 + 0x18 * (x)) |
| 89 | #define STM32_DMA_SFCR_FTH_MASK GENMASK(1, 0) |
| 90 | #define STM32_DMA_SFCR_FTH(n) (n & STM32_DMA_SFCR_FTH_MASK) |
| 91 | #define STM32_DMA_SFCR_FEIE BIT(7) /* FIFO error interrupt enable */ |
| 92 | #define STM32_DMA_SFCR_DMDIS BIT(2) /* Direct mode disable */ |
| 93 | #define STM32_DMA_SFCR_MASK (STM32_DMA_SFCR_FEIE \ |
| 94 | | STM32_DMA_SFCR_DMDIS) |
| 95 | |
| 96 | /* DMA direction */ |
| 97 | #define STM32_DMA_DEV_TO_MEM 0x00 |
| 98 | #define STM32_DMA_MEM_TO_DEV 0x01 |
| 99 | #define STM32_DMA_MEM_TO_MEM 0x02 |
| 100 | |
| 101 | /* DMA priority level */ |
| 102 | #define STM32_DMA_PRIORITY_LOW 0x00 |
| 103 | #define STM32_DMA_PRIORITY_MEDIUM 0x01 |
| 104 | #define STM32_DMA_PRIORITY_HIGH 0x02 |
| 105 | #define STM32_DMA_PRIORITY_VERY_HIGH 0x03 |
| 106 | |
| 107 | /* DMA FIFO threshold selection */ |
| 108 | #define STM32_DMA_FIFO_THRESHOLD_1QUARTERFULL 0x00 |
| 109 | #define STM32_DMA_FIFO_THRESHOLD_HALFFULL 0x01 |
| 110 | #define STM32_DMA_FIFO_THRESHOLD_3QUARTERSFULL 0x02 |
| 111 | #define STM32_DMA_FIFO_THRESHOLD_FULL 0x03 |
| 112 | |
| 113 | #define STM32_DMA_MAX_DATA_ITEMS 0xffff |
| 114 | #define STM32_DMA_MAX_CHANNELS 0x08 |
| 115 | #define STM32_DMA_MAX_REQUEST_ID 0x08 |
| 116 | #define STM32_DMA_MAX_DATA_PARAM 0x03 |
M'boumba Cedric Madianga | 276b004 | 2016-12-13 14:40:51 +0100 | [diff] [blame^] | 117 | #define STM32_DMA_MAX_BURST 16 |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 118 | |
| 119 | enum stm32_dma_width { |
| 120 | STM32_DMA_BYTE, |
| 121 | STM32_DMA_HALF_WORD, |
| 122 | STM32_DMA_WORD, |
| 123 | }; |
| 124 | |
| 125 | enum stm32_dma_burst_size { |
| 126 | STM32_DMA_BURST_SINGLE, |
| 127 | STM32_DMA_BURST_INCR4, |
| 128 | STM32_DMA_BURST_INCR8, |
| 129 | STM32_DMA_BURST_INCR16, |
| 130 | }; |
| 131 | |
| 132 | struct stm32_dma_cfg { |
| 133 | u32 channel_id; |
| 134 | u32 request_line; |
| 135 | u32 stream_config; |
| 136 | u32 threshold; |
| 137 | }; |
| 138 | |
| 139 | struct stm32_dma_chan_reg { |
| 140 | u32 dma_lisr; |
| 141 | u32 dma_hisr; |
| 142 | u32 dma_lifcr; |
| 143 | u32 dma_hifcr; |
| 144 | u32 dma_scr; |
| 145 | u32 dma_sndtr; |
| 146 | u32 dma_spar; |
| 147 | u32 dma_sm0ar; |
| 148 | u32 dma_sm1ar; |
| 149 | u32 dma_sfcr; |
| 150 | }; |
| 151 | |
| 152 | struct stm32_dma_sg_req { |
| 153 | u32 len; |
| 154 | struct stm32_dma_chan_reg chan_reg; |
| 155 | }; |
| 156 | |
| 157 | struct stm32_dma_desc { |
| 158 | struct virt_dma_desc vdesc; |
| 159 | bool cyclic; |
| 160 | u32 num_sgs; |
| 161 | struct stm32_dma_sg_req sg_req[]; |
| 162 | }; |
| 163 | |
| 164 | struct stm32_dma_chan { |
| 165 | struct virt_dma_chan vchan; |
| 166 | bool config_init; |
| 167 | bool busy; |
| 168 | u32 id; |
| 169 | u32 irq; |
| 170 | struct stm32_dma_desc *desc; |
| 171 | u32 next_sg; |
| 172 | struct dma_slave_config dma_sconfig; |
| 173 | struct stm32_dma_chan_reg chan_reg; |
| 174 | }; |
| 175 | |
| 176 | struct stm32_dma_device { |
| 177 | struct dma_device ddev; |
| 178 | void __iomem *base; |
| 179 | struct clk *clk; |
| 180 | struct reset_control *rst; |
| 181 | bool mem2mem; |
| 182 | struct stm32_dma_chan chan[STM32_DMA_MAX_CHANNELS]; |
| 183 | }; |
| 184 | |
| 185 | static struct stm32_dma_device *stm32_dma_get_dev(struct stm32_dma_chan *chan) |
| 186 | { |
| 187 | return container_of(chan->vchan.chan.device, struct stm32_dma_device, |
| 188 | ddev); |
| 189 | } |
| 190 | |
| 191 | static struct stm32_dma_chan *to_stm32_dma_chan(struct dma_chan *c) |
| 192 | { |
| 193 | return container_of(c, struct stm32_dma_chan, vchan.chan); |
| 194 | } |
| 195 | |
| 196 | static struct stm32_dma_desc *to_stm32_dma_desc(struct virt_dma_desc *vdesc) |
| 197 | { |
| 198 | return container_of(vdesc, struct stm32_dma_desc, vdesc); |
| 199 | } |
| 200 | |
| 201 | static struct device *chan2dev(struct stm32_dma_chan *chan) |
| 202 | { |
| 203 | return &chan->vchan.chan.dev->device; |
| 204 | } |
| 205 | |
| 206 | static u32 stm32_dma_read(struct stm32_dma_device *dmadev, u32 reg) |
| 207 | { |
| 208 | return readl_relaxed(dmadev->base + reg); |
| 209 | } |
| 210 | |
| 211 | static void stm32_dma_write(struct stm32_dma_device *dmadev, u32 reg, u32 val) |
| 212 | { |
| 213 | writel_relaxed(val, dmadev->base + reg); |
| 214 | } |
| 215 | |
| 216 | static struct stm32_dma_desc *stm32_dma_alloc_desc(u32 num_sgs) |
| 217 | { |
| 218 | return kzalloc(sizeof(struct stm32_dma_desc) + |
| 219 | sizeof(struct stm32_dma_sg_req) * num_sgs, GFP_NOWAIT); |
| 220 | } |
| 221 | |
| 222 | static int stm32_dma_get_width(struct stm32_dma_chan *chan, |
| 223 | enum dma_slave_buswidth width) |
| 224 | { |
| 225 | switch (width) { |
| 226 | case DMA_SLAVE_BUSWIDTH_1_BYTE: |
| 227 | return STM32_DMA_BYTE; |
| 228 | case DMA_SLAVE_BUSWIDTH_2_BYTES: |
| 229 | return STM32_DMA_HALF_WORD; |
| 230 | case DMA_SLAVE_BUSWIDTH_4_BYTES: |
| 231 | return STM32_DMA_WORD; |
| 232 | default: |
| 233 | dev_err(chan2dev(chan), "Dma bus width not supported\n"); |
| 234 | return -EINVAL; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | static int stm32_dma_get_burst(struct stm32_dma_chan *chan, u32 maxburst) |
| 239 | { |
| 240 | switch (maxburst) { |
| 241 | case 0: |
| 242 | case 1: |
| 243 | return STM32_DMA_BURST_SINGLE; |
| 244 | case 4: |
| 245 | return STM32_DMA_BURST_INCR4; |
| 246 | case 8: |
| 247 | return STM32_DMA_BURST_INCR8; |
| 248 | case 16: |
| 249 | return STM32_DMA_BURST_INCR16; |
| 250 | default: |
| 251 | dev_err(chan2dev(chan), "Dma burst size not supported\n"); |
| 252 | return -EINVAL; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | static void stm32_dma_set_fifo_config(struct stm32_dma_chan *chan, |
| 257 | u32 src_maxburst, u32 dst_maxburst) |
| 258 | { |
| 259 | chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_MASK; |
| 260 | chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_DMEIE; |
| 261 | |
| 262 | if ((!src_maxburst) && (!dst_maxburst)) { |
| 263 | /* Using direct mode */ |
| 264 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_DMEIE; |
| 265 | } else { |
| 266 | /* Using FIFO mode */ |
| 267 | chan->chan_reg.dma_sfcr |= STM32_DMA_SFCR_MASK; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | static int stm32_dma_slave_config(struct dma_chan *c, |
| 272 | struct dma_slave_config *config) |
| 273 | { |
| 274 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 275 | |
| 276 | memcpy(&chan->dma_sconfig, config, sizeof(*config)); |
| 277 | |
| 278 | chan->config_init = true; |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static u32 stm32_dma_irq_status(struct stm32_dma_chan *chan) |
| 284 | { |
| 285 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 286 | u32 flags, dma_isr; |
| 287 | |
| 288 | /* |
| 289 | * Read "flags" from DMA_xISR register corresponding to the selected |
| 290 | * DMA channel at the correct bit offset inside that register. |
| 291 | * |
| 292 | * If (ch % 4) is 2 or 3, left shift the mask by 16 bits. |
| 293 | * If (ch % 4) is 1 or 3, additionally left shift the mask by 6 bits. |
| 294 | */ |
| 295 | |
| 296 | if (chan->id & 4) |
| 297 | dma_isr = stm32_dma_read(dmadev, STM32_DMA_HISR); |
| 298 | else |
| 299 | dma_isr = stm32_dma_read(dmadev, STM32_DMA_LISR); |
| 300 | |
| 301 | flags = dma_isr >> (((chan->id & 2) << 3) | ((chan->id & 1) * 6)); |
| 302 | |
| 303 | return flags; |
| 304 | } |
| 305 | |
| 306 | static void stm32_dma_irq_clear(struct stm32_dma_chan *chan, u32 flags) |
| 307 | { |
| 308 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 309 | u32 dma_ifcr; |
| 310 | |
| 311 | /* |
| 312 | * Write "flags" to the DMA_xIFCR register corresponding to the selected |
| 313 | * DMA channel at the correct bit offset inside that register. |
| 314 | * |
| 315 | * If (ch % 4) is 2 or 3, left shift the mask by 16 bits. |
| 316 | * If (ch % 4) is 1 or 3, additionally left shift the mask by 6 bits. |
| 317 | */ |
| 318 | dma_ifcr = flags << (((chan->id & 2) << 3) | ((chan->id & 1) * 6)); |
| 319 | |
| 320 | if (chan->id & 4) |
| 321 | stm32_dma_write(dmadev, STM32_DMA_HIFCR, dma_ifcr); |
| 322 | else |
| 323 | stm32_dma_write(dmadev, STM32_DMA_LIFCR, dma_ifcr); |
| 324 | } |
| 325 | |
| 326 | static int stm32_dma_disable_chan(struct stm32_dma_chan *chan) |
| 327 | { |
| 328 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 329 | unsigned long timeout = jiffies + msecs_to_jiffies(5000); |
| 330 | u32 dma_scr, id; |
| 331 | |
| 332 | id = chan->id; |
| 333 | dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id)); |
| 334 | |
| 335 | if (dma_scr & STM32_DMA_SCR_EN) { |
| 336 | dma_scr &= ~STM32_DMA_SCR_EN; |
| 337 | stm32_dma_write(dmadev, STM32_DMA_SCR(id), dma_scr); |
| 338 | |
| 339 | do { |
| 340 | dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id)); |
| 341 | dma_scr &= STM32_DMA_SCR_EN; |
| 342 | if (!dma_scr) |
| 343 | break; |
| 344 | |
| 345 | if (time_after_eq(jiffies, timeout)) { |
| 346 | dev_err(chan2dev(chan), "%s: timeout!\n", |
| 347 | __func__); |
| 348 | return -EBUSY; |
| 349 | } |
| 350 | cond_resched(); |
| 351 | } while (1); |
| 352 | } |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static void stm32_dma_stop(struct stm32_dma_chan *chan) |
| 358 | { |
| 359 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 360 | u32 dma_scr, dma_sfcr, status; |
| 361 | int ret; |
| 362 | |
| 363 | /* Disable interrupts */ |
| 364 | dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id)); |
| 365 | dma_scr &= ~STM32_DMA_SCR_IRQ_MASK; |
| 366 | stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), dma_scr); |
| 367 | dma_sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id)); |
| 368 | dma_sfcr &= ~STM32_DMA_SFCR_FEIE; |
| 369 | stm32_dma_write(dmadev, STM32_DMA_SFCR(chan->id), dma_sfcr); |
| 370 | |
| 371 | /* Disable DMA */ |
| 372 | ret = stm32_dma_disable_chan(chan); |
| 373 | if (ret < 0) |
| 374 | return; |
| 375 | |
| 376 | /* Clear interrupt status if it is there */ |
| 377 | status = stm32_dma_irq_status(chan); |
| 378 | if (status) { |
| 379 | dev_dbg(chan2dev(chan), "%s(): clearing interrupt: 0x%08x\n", |
| 380 | __func__, status); |
| 381 | stm32_dma_irq_clear(chan, status); |
| 382 | } |
| 383 | |
| 384 | chan->busy = false; |
| 385 | } |
| 386 | |
| 387 | static int stm32_dma_terminate_all(struct dma_chan *c) |
| 388 | { |
| 389 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 390 | unsigned long flags; |
| 391 | LIST_HEAD(head); |
| 392 | |
| 393 | spin_lock_irqsave(&chan->vchan.lock, flags); |
| 394 | |
| 395 | if (chan->busy) { |
| 396 | stm32_dma_stop(chan); |
| 397 | chan->desc = NULL; |
| 398 | } |
| 399 | |
| 400 | vchan_get_all_descriptors(&chan->vchan, &head); |
| 401 | spin_unlock_irqrestore(&chan->vchan.lock, flags); |
| 402 | vchan_dma_desc_free_list(&chan->vchan, &head); |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
M'boumba Cedric Madianga | dc80867 | 2016-12-13 14:40:50 +0100 | [diff] [blame] | 407 | static void stm32_dma_synchronize(struct dma_chan *c) |
| 408 | { |
| 409 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 410 | |
| 411 | vchan_synchronize(&chan->vchan); |
| 412 | } |
| 413 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 414 | static void stm32_dma_dump_reg(struct stm32_dma_chan *chan) |
| 415 | { |
| 416 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 417 | u32 scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id)); |
| 418 | u32 ndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id)); |
| 419 | u32 spar = stm32_dma_read(dmadev, STM32_DMA_SPAR(chan->id)); |
| 420 | u32 sm0ar = stm32_dma_read(dmadev, STM32_DMA_SM0AR(chan->id)); |
| 421 | u32 sm1ar = stm32_dma_read(dmadev, STM32_DMA_SM1AR(chan->id)); |
| 422 | u32 sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id)); |
| 423 | |
| 424 | dev_dbg(chan2dev(chan), "SCR: 0x%08x\n", scr); |
| 425 | dev_dbg(chan2dev(chan), "NDTR: 0x%08x\n", ndtr); |
| 426 | dev_dbg(chan2dev(chan), "SPAR: 0x%08x\n", spar); |
| 427 | dev_dbg(chan2dev(chan), "SM0AR: 0x%08x\n", sm0ar); |
| 428 | dev_dbg(chan2dev(chan), "SM1AR: 0x%08x\n", sm1ar); |
| 429 | dev_dbg(chan2dev(chan), "SFCR: 0x%08x\n", sfcr); |
| 430 | } |
| 431 | |
M'boumba Cedric Madianga | 8d1b76f | 2016-12-13 14:40:47 +0100 | [diff] [blame] | 432 | static void stm32_dma_start_transfer(struct stm32_dma_chan *chan) |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 433 | { |
| 434 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 435 | struct virt_dma_desc *vdesc; |
| 436 | struct stm32_dma_sg_req *sg_req; |
| 437 | struct stm32_dma_chan_reg *reg; |
| 438 | u32 status; |
| 439 | int ret; |
| 440 | |
| 441 | ret = stm32_dma_disable_chan(chan); |
| 442 | if (ret < 0) |
M'boumba Cedric Madianga | 8d1b76f | 2016-12-13 14:40:47 +0100 | [diff] [blame] | 443 | return; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 444 | |
| 445 | if (!chan->desc) { |
| 446 | vdesc = vchan_next_desc(&chan->vchan); |
| 447 | if (!vdesc) |
M'boumba Cedric Madianga | 8d1b76f | 2016-12-13 14:40:47 +0100 | [diff] [blame] | 448 | return; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 449 | |
| 450 | chan->desc = to_stm32_dma_desc(vdesc); |
| 451 | chan->next_sg = 0; |
| 452 | } |
| 453 | |
| 454 | if (chan->next_sg == chan->desc->num_sgs) |
| 455 | chan->next_sg = 0; |
| 456 | |
| 457 | sg_req = &chan->desc->sg_req[chan->next_sg]; |
| 458 | reg = &sg_req->chan_reg; |
| 459 | |
| 460 | stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), reg->dma_scr); |
| 461 | stm32_dma_write(dmadev, STM32_DMA_SPAR(chan->id), reg->dma_spar); |
| 462 | stm32_dma_write(dmadev, STM32_DMA_SM0AR(chan->id), reg->dma_sm0ar); |
| 463 | stm32_dma_write(dmadev, STM32_DMA_SFCR(chan->id), reg->dma_sfcr); |
| 464 | stm32_dma_write(dmadev, STM32_DMA_SM1AR(chan->id), reg->dma_sm1ar); |
| 465 | stm32_dma_write(dmadev, STM32_DMA_SNDTR(chan->id), reg->dma_sndtr); |
| 466 | |
| 467 | chan->next_sg++; |
| 468 | |
| 469 | /* Clear interrupt status if it is there */ |
| 470 | status = stm32_dma_irq_status(chan); |
| 471 | if (status) |
| 472 | stm32_dma_irq_clear(chan, status); |
| 473 | |
| 474 | stm32_dma_dump_reg(chan); |
| 475 | |
| 476 | /* Start DMA */ |
| 477 | reg->dma_scr |= STM32_DMA_SCR_EN; |
| 478 | stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), reg->dma_scr); |
| 479 | |
| 480 | chan->busy = true; |
| 481 | |
M'boumba Cedric Madianga | 8d1b76f | 2016-12-13 14:40:47 +0100 | [diff] [blame] | 482 | dev_dbg(chan2dev(chan), "vchan %p: started\n", &chan->vchan); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static void stm32_dma_configure_next_sg(struct stm32_dma_chan *chan) |
| 486 | { |
| 487 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 488 | struct stm32_dma_sg_req *sg_req; |
| 489 | u32 dma_scr, dma_sm0ar, dma_sm1ar, id; |
| 490 | |
| 491 | id = chan->id; |
| 492 | dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id)); |
| 493 | |
| 494 | if (dma_scr & STM32_DMA_SCR_DBM) { |
| 495 | if (chan->next_sg == chan->desc->num_sgs) |
| 496 | chan->next_sg = 0; |
| 497 | |
| 498 | sg_req = &chan->desc->sg_req[chan->next_sg]; |
| 499 | |
| 500 | if (dma_scr & STM32_DMA_SCR_CT) { |
| 501 | dma_sm0ar = sg_req->chan_reg.dma_sm0ar; |
| 502 | stm32_dma_write(dmadev, STM32_DMA_SM0AR(id), dma_sm0ar); |
| 503 | dev_dbg(chan2dev(chan), "CT=1 <=> SM0AR: 0x%08x\n", |
| 504 | stm32_dma_read(dmadev, STM32_DMA_SM0AR(id))); |
| 505 | } else { |
| 506 | dma_sm1ar = sg_req->chan_reg.dma_sm1ar; |
| 507 | stm32_dma_write(dmadev, STM32_DMA_SM1AR(id), dma_sm1ar); |
| 508 | dev_dbg(chan2dev(chan), "CT=0 <=> SM1AR: 0x%08x\n", |
| 509 | stm32_dma_read(dmadev, STM32_DMA_SM1AR(id))); |
| 510 | } |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
| 514 | static void stm32_dma_handle_chan_done(struct stm32_dma_chan *chan) |
| 515 | { |
| 516 | if (chan->desc) { |
| 517 | if (chan->desc->cyclic) { |
| 518 | vchan_cyclic_callback(&chan->desc->vdesc); |
M'boumba Cedric Madianga | 2b12c558 | 2016-12-13 14:40:48 +0100 | [diff] [blame] | 519 | chan->next_sg++; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 520 | stm32_dma_configure_next_sg(chan); |
| 521 | } else { |
| 522 | chan->busy = false; |
| 523 | if (chan->next_sg == chan->desc->num_sgs) { |
| 524 | list_del(&chan->desc->vdesc.node); |
| 525 | vchan_cookie_complete(&chan->desc->vdesc); |
| 526 | chan->desc = NULL; |
| 527 | } |
| 528 | stm32_dma_start_transfer(chan); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | static irqreturn_t stm32_dma_chan_irq(int irq, void *devid) |
| 534 | { |
| 535 | struct stm32_dma_chan *chan = devid; |
| 536 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
Vinod Koul | 1bc4f06 | 2016-12-09 15:24:12 +0530 | [diff] [blame] | 537 | u32 status, scr; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 538 | |
| 539 | spin_lock(&chan->vchan.lock); |
| 540 | |
| 541 | status = stm32_dma_irq_status(chan); |
| 542 | scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id)); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 543 | |
| 544 | if ((status & STM32_DMA_TCI) && (scr & STM32_DMA_SCR_TCIE)) { |
| 545 | stm32_dma_irq_clear(chan, STM32_DMA_TCI); |
| 546 | stm32_dma_handle_chan_done(chan); |
| 547 | |
| 548 | } else { |
| 549 | stm32_dma_irq_clear(chan, status); |
| 550 | dev_err(chan2dev(chan), "DMA error: status=0x%08x\n", status); |
| 551 | } |
| 552 | |
| 553 | spin_unlock(&chan->vchan.lock); |
| 554 | |
| 555 | return IRQ_HANDLED; |
| 556 | } |
| 557 | |
| 558 | static void stm32_dma_issue_pending(struct dma_chan *c) |
| 559 | { |
| 560 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 561 | unsigned long flags; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 562 | |
| 563 | spin_lock_irqsave(&chan->vchan.lock, flags); |
M'boumba Cedric Madianga | 8d1b76f | 2016-12-13 14:40:47 +0100 | [diff] [blame] | 564 | if (vchan_issue_pending(&chan->vchan) && !chan->desc && !chan->busy) { |
| 565 | dev_dbg(chan2dev(chan), "vchan %p: issued\n", &chan->vchan); |
| 566 | stm32_dma_start_transfer(chan); |
| 567 | if (chan->desc->cyclic) |
| 568 | stm32_dma_configure_next_sg(chan); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 569 | } |
| 570 | spin_unlock_irqrestore(&chan->vchan.lock, flags); |
| 571 | } |
| 572 | |
| 573 | static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan, |
| 574 | enum dma_transfer_direction direction, |
| 575 | enum dma_slave_buswidth *buswidth) |
| 576 | { |
| 577 | enum dma_slave_buswidth src_addr_width, dst_addr_width; |
| 578 | int src_bus_width, dst_bus_width; |
| 579 | int src_burst_size, dst_burst_size; |
| 580 | u32 src_maxburst, dst_maxburst; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 581 | u32 dma_scr = 0; |
| 582 | |
| 583 | src_addr_width = chan->dma_sconfig.src_addr_width; |
| 584 | dst_addr_width = chan->dma_sconfig.dst_addr_width; |
| 585 | src_maxburst = chan->dma_sconfig.src_maxburst; |
| 586 | dst_maxburst = chan->dma_sconfig.dst_maxburst; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 587 | |
| 588 | switch (direction) { |
| 589 | case DMA_MEM_TO_DEV: |
| 590 | dst_bus_width = stm32_dma_get_width(chan, dst_addr_width); |
| 591 | if (dst_bus_width < 0) |
| 592 | return dst_bus_width; |
| 593 | |
| 594 | dst_burst_size = stm32_dma_get_burst(chan, dst_maxburst); |
| 595 | if (dst_burst_size < 0) |
| 596 | return dst_burst_size; |
| 597 | |
| 598 | if (!src_addr_width) |
| 599 | src_addr_width = dst_addr_width; |
| 600 | |
| 601 | src_bus_width = stm32_dma_get_width(chan, src_addr_width); |
| 602 | if (src_bus_width < 0) |
| 603 | return src_bus_width; |
| 604 | |
| 605 | src_burst_size = stm32_dma_get_burst(chan, src_maxburst); |
| 606 | if (src_burst_size < 0) |
| 607 | return src_burst_size; |
| 608 | |
| 609 | dma_scr = STM32_DMA_SCR_DIR(STM32_DMA_MEM_TO_DEV) | |
| 610 | STM32_DMA_SCR_PSIZE(dst_bus_width) | |
| 611 | STM32_DMA_SCR_MSIZE(src_bus_width) | |
| 612 | STM32_DMA_SCR_PBURST(dst_burst_size) | |
| 613 | STM32_DMA_SCR_MBURST(src_burst_size); |
| 614 | |
| 615 | chan->chan_reg.dma_spar = chan->dma_sconfig.dst_addr; |
| 616 | *buswidth = dst_addr_width; |
| 617 | break; |
| 618 | |
| 619 | case DMA_DEV_TO_MEM: |
| 620 | src_bus_width = stm32_dma_get_width(chan, src_addr_width); |
| 621 | if (src_bus_width < 0) |
| 622 | return src_bus_width; |
| 623 | |
| 624 | src_burst_size = stm32_dma_get_burst(chan, src_maxburst); |
| 625 | if (src_burst_size < 0) |
| 626 | return src_burst_size; |
| 627 | |
| 628 | if (!dst_addr_width) |
| 629 | dst_addr_width = src_addr_width; |
| 630 | |
| 631 | dst_bus_width = stm32_dma_get_width(chan, dst_addr_width); |
| 632 | if (dst_bus_width < 0) |
| 633 | return dst_bus_width; |
| 634 | |
| 635 | dst_burst_size = stm32_dma_get_burst(chan, dst_maxburst); |
| 636 | if (dst_burst_size < 0) |
| 637 | return dst_burst_size; |
| 638 | |
| 639 | dma_scr = STM32_DMA_SCR_DIR(STM32_DMA_DEV_TO_MEM) | |
| 640 | STM32_DMA_SCR_PSIZE(src_bus_width) | |
| 641 | STM32_DMA_SCR_MSIZE(dst_bus_width) | |
| 642 | STM32_DMA_SCR_PBURST(src_burst_size) | |
| 643 | STM32_DMA_SCR_MBURST(dst_burst_size); |
| 644 | |
| 645 | chan->chan_reg.dma_spar = chan->dma_sconfig.src_addr; |
| 646 | *buswidth = chan->dma_sconfig.src_addr_width; |
| 647 | break; |
| 648 | |
| 649 | default: |
| 650 | dev_err(chan2dev(chan), "Dma direction is not supported\n"); |
| 651 | return -EINVAL; |
| 652 | } |
| 653 | |
| 654 | stm32_dma_set_fifo_config(chan, src_maxburst, dst_maxburst); |
| 655 | |
| 656 | chan->chan_reg.dma_scr &= ~(STM32_DMA_SCR_DIR_MASK | |
| 657 | STM32_DMA_SCR_PSIZE_MASK | STM32_DMA_SCR_MSIZE_MASK | |
| 658 | STM32_DMA_SCR_PBURST_MASK | STM32_DMA_SCR_MBURST_MASK); |
| 659 | chan->chan_reg.dma_scr |= dma_scr; |
| 660 | |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | static void stm32_dma_clear_reg(struct stm32_dma_chan_reg *regs) |
| 665 | { |
| 666 | memset(regs, 0, sizeof(struct stm32_dma_chan_reg)); |
| 667 | } |
| 668 | |
| 669 | static struct dma_async_tx_descriptor *stm32_dma_prep_slave_sg( |
| 670 | struct dma_chan *c, struct scatterlist *sgl, |
| 671 | u32 sg_len, enum dma_transfer_direction direction, |
| 672 | unsigned long flags, void *context) |
| 673 | { |
| 674 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 675 | struct stm32_dma_desc *desc; |
| 676 | struct scatterlist *sg; |
| 677 | enum dma_slave_buswidth buswidth; |
| 678 | u32 nb_data_items; |
| 679 | int i, ret; |
| 680 | |
| 681 | if (!chan->config_init) { |
| 682 | dev_err(chan2dev(chan), "dma channel is not configured\n"); |
| 683 | return NULL; |
| 684 | } |
| 685 | |
| 686 | if (sg_len < 1) { |
| 687 | dev_err(chan2dev(chan), "Invalid segment length %d\n", sg_len); |
| 688 | return NULL; |
| 689 | } |
| 690 | |
| 691 | desc = stm32_dma_alloc_desc(sg_len); |
| 692 | if (!desc) |
| 693 | return NULL; |
| 694 | |
| 695 | ret = stm32_dma_set_xfer_param(chan, direction, &buswidth); |
| 696 | if (ret < 0) |
| 697 | goto err; |
| 698 | |
| 699 | /* Set peripheral flow controller */ |
| 700 | if (chan->dma_sconfig.device_fc) |
| 701 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_PFCTRL; |
| 702 | else |
| 703 | chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL; |
| 704 | |
| 705 | for_each_sg(sgl, sg, sg_len, i) { |
| 706 | desc->sg_req[i].len = sg_dma_len(sg); |
| 707 | |
| 708 | nb_data_items = desc->sg_req[i].len / buswidth; |
| 709 | if (nb_data_items > STM32_DMA_MAX_DATA_ITEMS) { |
| 710 | dev_err(chan2dev(chan), "nb items not supported\n"); |
| 711 | goto err; |
| 712 | } |
| 713 | |
| 714 | stm32_dma_clear_reg(&desc->sg_req[i].chan_reg); |
| 715 | desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr; |
| 716 | desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr; |
| 717 | desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar; |
| 718 | desc->sg_req[i].chan_reg.dma_sm0ar = sg_dma_address(sg); |
| 719 | desc->sg_req[i].chan_reg.dma_sm1ar = sg_dma_address(sg); |
| 720 | desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items; |
| 721 | } |
| 722 | |
| 723 | desc->num_sgs = sg_len; |
| 724 | desc->cyclic = false; |
| 725 | |
| 726 | return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags); |
| 727 | |
| 728 | err: |
| 729 | kfree(desc); |
| 730 | return NULL; |
| 731 | } |
| 732 | |
| 733 | static struct dma_async_tx_descriptor *stm32_dma_prep_dma_cyclic( |
| 734 | struct dma_chan *c, dma_addr_t buf_addr, size_t buf_len, |
| 735 | size_t period_len, enum dma_transfer_direction direction, |
| 736 | unsigned long flags) |
| 737 | { |
| 738 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 739 | struct stm32_dma_desc *desc; |
| 740 | enum dma_slave_buswidth buswidth; |
| 741 | u32 num_periods, nb_data_items; |
| 742 | int i, ret; |
| 743 | |
| 744 | if (!buf_len || !period_len) { |
| 745 | dev_err(chan2dev(chan), "Invalid buffer/period len\n"); |
| 746 | return NULL; |
| 747 | } |
| 748 | |
| 749 | if (!chan->config_init) { |
| 750 | dev_err(chan2dev(chan), "dma channel is not configured\n"); |
| 751 | return NULL; |
| 752 | } |
| 753 | |
| 754 | if (buf_len % period_len) { |
| 755 | dev_err(chan2dev(chan), "buf_len not multiple of period_len\n"); |
| 756 | return NULL; |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | * We allow to take more number of requests till DMA is |
| 761 | * not started. The driver will loop over all requests. |
| 762 | * Once DMA is started then new requests can be queued only after |
| 763 | * terminating the DMA. |
| 764 | */ |
| 765 | if (chan->busy) { |
| 766 | dev_err(chan2dev(chan), "Request not allowed when dma busy\n"); |
| 767 | return NULL; |
| 768 | } |
| 769 | |
| 770 | ret = stm32_dma_set_xfer_param(chan, direction, &buswidth); |
| 771 | if (ret < 0) |
| 772 | return NULL; |
| 773 | |
| 774 | nb_data_items = period_len / buswidth; |
| 775 | if (nb_data_items > STM32_DMA_MAX_DATA_ITEMS) { |
| 776 | dev_err(chan2dev(chan), "number of items not supported\n"); |
| 777 | return NULL; |
| 778 | } |
| 779 | |
| 780 | /* Enable Circular mode or double buffer mode */ |
| 781 | if (buf_len == period_len) |
| 782 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_CIRC; |
| 783 | else |
| 784 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_DBM; |
| 785 | |
| 786 | /* Clear periph ctrl if client set it */ |
| 787 | chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL; |
| 788 | |
| 789 | num_periods = buf_len / period_len; |
| 790 | |
| 791 | desc = stm32_dma_alloc_desc(num_periods); |
| 792 | if (!desc) |
| 793 | return NULL; |
| 794 | |
| 795 | for (i = 0; i < num_periods; i++) { |
| 796 | desc->sg_req[i].len = period_len; |
| 797 | |
| 798 | stm32_dma_clear_reg(&desc->sg_req[i].chan_reg); |
| 799 | desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr; |
| 800 | desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr; |
| 801 | desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar; |
| 802 | desc->sg_req[i].chan_reg.dma_sm0ar = buf_addr; |
| 803 | desc->sg_req[i].chan_reg.dma_sm1ar = buf_addr; |
| 804 | desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items; |
| 805 | buf_addr += period_len; |
| 806 | } |
| 807 | |
| 808 | desc->num_sgs = num_periods; |
| 809 | desc->cyclic = true; |
| 810 | |
| 811 | return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags); |
| 812 | } |
| 813 | |
| 814 | static struct dma_async_tx_descriptor *stm32_dma_prep_dma_memcpy( |
| 815 | struct dma_chan *c, dma_addr_t dest, |
| 816 | dma_addr_t src, size_t len, unsigned long flags) |
| 817 | { |
| 818 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 819 | u32 num_sgs; |
| 820 | struct stm32_dma_desc *desc; |
| 821 | size_t xfer_count, offset; |
| 822 | int i; |
| 823 | |
| 824 | num_sgs = DIV_ROUND_UP(len, STM32_DMA_MAX_DATA_ITEMS); |
| 825 | desc = stm32_dma_alloc_desc(num_sgs); |
| 826 | if (!desc) |
| 827 | return NULL; |
| 828 | |
| 829 | for (offset = 0, i = 0; offset < len; offset += xfer_count, i++) { |
| 830 | xfer_count = min_t(size_t, len - offset, |
| 831 | STM32_DMA_MAX_DATA_ITEMS); |
| 832 | |
| 833 | desc->sg_req[i].len = xfer_count; |
| 834 | |
| 835 | stm32_dma_clear_reg(&desc->sg_req[i].chan_reg); |
| 836 | desc->sg_req[i].chan_reg.dma_scr = |
| 837 | STM32_DMA_SCR_DIR(STM32_DMA_MEM_TO_MEM) | |
| 838 | STM32_DMA_SCR_MINC | |
| 839 | STM32_DMA_SCR_PINC | |
| 840 | STM32_DMA_SCR_TCIE | |
| 841 | STM32_DMA_SCR_TEIE; |
| 842 | desc->sg_req[i].chan_reg.dma_sfcr = STM32_DMA_SFCR_DMDIS | |
| 843 | STM32_DMA_SFCR_FTH(STM32_DMA_FIFO_THRESHOLD_FULL) | |
| 844 | STM32_DMA_SFCR_FEIE; |
| 845 | desc->sg_req[i].chan_reg.dma_spar = src + offset; |
| 846 | desc->sg_req[i].chan_reg.dma_sm0ar = dest + offset; |
| 847 | desc->sg_req[i].chan_reg.dma_sndtr = xfer_count; |
| 848 | } |
| 849 | |
| 850 | desc->num_sgs = num_sgs; |
| 851 | desc->cyclic = false; |
| 852 | |
| 853 | return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags); |
| 854 | } |
| 855 | |
M'boumba Cedric Madianga | 2b12c558 | 2016-12-13 14:40:48 +0100 | [diff] [blame] | 856 | static u32 stm32_dma_get_remaining_bytes(struct stm32_dma_chan *chan) |
| 857 | { |
| 858 | u32 dma_scr, width, ndtr; |
| 859 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 860 | |
| 861 | dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id)); |
| 862 | width = STM32_DMA_SCR_PSIZE_GET(dma_scr); |
| 863 | ndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id)); |
| 864 | |
| 865 | return ndtr << width; |
| 866 | } |
| 867 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 868 | static size_t stm32_dma_desc_residue(struct stm32_dma_chan *chan, |
| 869 | struct stm32_dma_desc *desc, |
| 870 | u32 next_sg) |
| 871 | { |
M'boumba Cedric Madianga | 2b12c558 | 2016-12-13 14:40:48 +0100 | [diff] [blame] | 872 | u32 residue = 0; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 873 | int i; |
| 874 | |
M'boumba Cedric Madianga | 2b12c558 | 2016-12-13 14:40:48 +0100 | [diff] [blame] | 875 | /* |
| 876 | * In cyclic mode, for the last period, residue = remaining bytes from |
| 877 | * NDTR |
| 878 | */ |
| 879 | if (chan->desc->cyclic && next_sg == 0) |
| 880 | return stm32_dma_get_remaining_bytes(chan); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 881 | |
M'boumba Cedric Madianga | 2b12c558 | 2016-12-13 14:40:48 +0100 | [diff] [blame] | 882 | /* |
| 883 | * For all other periods in cyclic mode, and in sg mode, |
| 884 | * residue = remaining bytes from NDTR + remaining periods/sg to be |
| 885 | * transferred |
| 886 | */ |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 887 | for (i = next_sg; i < desc->num_sgs; i++) |
| 888 | residue += desc->sg_req[i].len; |
M'boumba Cedric Madianga | 2b12c558 | 2016-12-13 14:40:48 +0100 | [diff] [blame] | 889 | residue += stm32_dma_get_remaining_bytes(chan); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 890 | |
| 891 | return residue; |
| 892 | } |
| 893 | |
| 894 | static enum dma_status stm32_dma_tx_status(struct dma_chan *c, |
| 895 | dma_cookie_t cookie, |
| 896 | struct dma_tx_state *state) |
| 897 | { |
| 898 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 899 | struct virt_dma_desc *vdesc; |
| 900 | enum dma_status status; |
| 901 | unsigned long flags; |
| 902 | u32 residue; |
| 903 | |
| 904 | status = dma_cookie_status(c, cookie, state); |
| 905 | if ((status == DMA_COMPLETE) || (!state)) |
| 906 | return status; |
| 907 | |
| 908 | spin_lock_irqsave(&chan->vchan.lock, flags); |
| 909 | vdesc = vchan_find_desc(&chan->vchan, cookie); |
| 910 | if (cookie == chan->desc->vdesc.tx.cookie) { |
| 911 | residue = stm32_dma_desc_residue(chan, chan->desc, |
| 912 | chan->next_sg); |
| 913 | } else if (vdesc) { |
| 914 | residue = stm32_dma_desc_residue(chan, |
| 915 | to_stm32_dma_desc(vdesc), 0); |
| 916 | } else { |
| 917 | residue = 0; |
| 918 | } |
| 919 | |
| 920 | dma_set_residue(state, residue); |
| 921 | |
| 922 | spin_unlock_irqrestore(&chan->vchan.lock, flags); |
| 923 | |
| 924 | return status; |
| 925 | } |
| 926 | |
| 927 | static int stm32_dma_alloc_chan_resources(struct dma_chan *c) |
| 928 | { |
| 929 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 930 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 931 | int ret; |
| 932 | |
| 933 | chan->config_init = false; |
| 934 | ret = clk_prepare_enable(dmadev->clk); |
| 935 | if (ret < 0) { |
| 936 | dev_err(chan2dev(chan), "clk_prepare_enable failed: %d\n", ret); |
| 937 | return ret; |
| 938 | } |
| 939 | |
| 940 | ret = stm32_dma_disable_chan(chan); |
| 941 | if (ret < 0) |
| 942 | clk_disable_unprepare(dmadev->clk); |
| 943 | |
| 944 | return ret; |
| 945 | } |
| 946 | |
| 947 | static void stm32_dma_free_chan_resources(struct dma_chan *c) |
| 948 | { |
| 949 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 950 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 951 | unsigned long flags; |
| 952 | |
| 953 | dev_dbg(chan2dev(chan), "Freeing channel %d\n", chan->id); |
| 954 | |
| 955 | if (chan->busy) { |
| 956 | spin_lock_irqsave(&chan->vchan.lock, flags); |
| 957 | stm32_dma_stop(chan); |
| 958 | chan->desc = NULL; |
| 959 | spin_unlock_irqrestore(&chan->vchan.lock, flags); |
| 960 | } |
| 961 | |
| 962 | clk_disable_unprepare(dmadev->clk); |
| 963 | |
| 964 | vchan_free_chan_resources(to_virt_chan(c)); |
| 965 | } |
| 966 | |
| 967 | static void stm32_dma_desc_free(struct virt_dma_desc *vdesc) |
| 968 | { |
| 969 | kfree(container_of(vdesc, struct stm32_dma_desc, vdesc)); |
| 970 | } |
| 971 | |
Vinod Koul | e97adb4 | 2016-09-02 15:59:10 +0530 | [diff] [blame] | 972 | static void stm32_dma_set_config(struct stm32_dma_chan *chan, |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 973 | struct stm32_dma_cfg *cfg) |
| 974 | { |
| 975 | stm32_dma_clear_reg(&chan->chan_reg); |
| 976 | |
| 977 | chan->chan_reg.dma_scr = cfg->stream_config & STM32_DMA_SCR_CFG_MASK; |
| 978 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_REQ(cfg->request_line); |
| 979 | |
| 980 | /* Enable Interrupts */ |
| 981 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_TEIE | STM32_DMA_SCR_TCIE; |
| 982 | |
| 983 | chan->chan_reg.dma_sfcr = cfg->threshold & STM32_DMA_SFCR_FTH_MASK; |
| 984 | } |
| 985 | |
| 986 | static struct dma_chan *stm32_dma_of_xlate(struct of_phandle_args *dma_spec, |
| 987 | struct of_dma *ofdma) |
| 988 | { |
| 989 | struct stm32_dma_device *dmadev = ofdma->of_dma_data; |
| 990 | struct stm32_dma_cfg cfg; |
| 991 | struct stm32_dma_chan *chan; |
| 992 | struct dma_chan *c; |
| 993 | |
| 994 | if (dma_spec->args_count < 3) |
| 995 | return NULL; |
| 996 | |
| 997 | cfg.channel_id = dma_spec->args[0]; |
| 998 | cfg.request_line = dma_spec->args[1]; |
| 999 | cfg.stream_config = dma_spec->args[2]; |
| 1000 | cfg.threshold = 0; |
| 1001 | |
| 1002 | if ((cfg.channel_id >= STM32_DMA_MAX_CHANNELS) || (cfg.request_line >= |
| 1003 | STM32_DMA_MAX_REQUEST_ID)) |
| 1004 | return NULL; |
| 1005 | |
| 1006 | if (dma_spec->args_count > 3) |
| 1007 | cfg.threshold = dma_spec->args[3]; |
| 1008 | |
| 1009 | chan = &dmadev->chan[cfg.channel_id]; |
| 1010 | |
| 1011 | c = dma_get_slave_channel(&chan->vchan.chan); |
| 1012 | if (c) |
| 1013 | stm32_dma_set_config(chan, &cfg); |
| 1014 | |
| 1015 | return c; |
| 1016 | } |
| 1017 | |
| 1018 | static const struct of_device_id stm32_dma_of_match[] = { |
| 1019 | { .compatible = "st,stm32-dma", }, |
| 1020 | { /* sentinel */ }, |
| 1021 | }; |
| 1022 | MODULE_DEVICE_TABLE(of, stm32_dma_of_match); |
| 1023 | |
| 1024 | static int stm32_dma_probe(struct platform_device *pdev) |
| 1025 | { |
| 1026 | struct stm32_dma_chan *chan; |
| 1027 | struct stm32_dma_device *dmadev; |
| 1028 | struct dma_device *dd; |
| 1029 | const struct of_device_id *match; |
| 1030 | struct resource *res; |
| 1031 | int i, ret; |
| 1032 | |
| 1033 | match = of_match_device(stm32_dma_of_match, &pdev->dev); |
| 1034 | if (!match) { |
| 1035 | dev_err(&pdev->dev, "Error: No device match found\n"); |
| 1036 | return -ENODEV; |
| 1037 | } |
| 1038 | |
| 1039 | dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev), GFP_KERNEL); |
| 1040 | if (!dmadev) |
| 1041 | return -ENOMEM; |
| 1042 | |
| 1043 | dd = &dmadev->ddev; |
| 1044 | |
| 1045 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1046 | dmadev->base = devm_ioremap_resource(&pdev->dev, res); |
| 1047 | if (IS_ERR(dmadev->base)) |
| 1048 | return PTR_ERR(dmadev->base); |
| 1049 | |
| 1050 | dmadev->clk = devm_clk_get(&pdev->dev, NULL); |
| 1051 | if (IS_ERR(dmadev->clk)) { |
| 1052 | dev_err(&pdev->dev, "Error: Missing controller clock\n"); |
| 1053 | return PTR_ERR(dmadev->clk); |
| 1054 | } |
| 1055 | |
| 1056 | dmadev->mem2mem = of_property_read_bool(pdev->dev.of_node, |
| 1057 | "st,mem2mem"); |
| 1058 | |
| 1059 | dmadev->rst = devm_reset_control_get(&pdev->dev, NULL); |
| 1060 | if (!IS_ERR(dmadev->rst)) { |
| 1061 | reset_control_assert(dmadev->rst); |
| 1062 | udelay(2); |
| 1063 | reset_control_deassert(dmadev->rst); |
| 1064 | } |
| 1065 | |
| 1066 | dma_cap_set(DMA_SLAVE, dd->cap_mask); |
| 1067 | dma_cap_set(DMA_PRIVATE, dd->cap_mask); |
| 1068 | dma_cap_set(DMA_CYCLIC, dd->cap_mask); |
| 1069 | dd->device_alloc_chan_resources = stm32_dma_alloc_chan_resources; |
| 1070 | dd->device_free_chan_resources = stm32_dma_free_chan_resources; |
| 1071 | dd->device_tx_status = stm32_dma_tx_status; |
| 1072 | dd->device_issue_pending = stm32_dma_issue_pending; |
| 1073 | dd->device_prep_slave_sg = stm32_dma_prep_slave_sg; |
| 1074 | dd->device_prep_dma_cyclic = stm32_dma_prep_dma_cyclic; |
| 1075 | dd->device_config = stm32_dma_slave_config; |
| 1076 | dd->device_terminate_all = stm32_dma_terminate_all; |
M'boumba Cedric Madianga | dc80867 | 2016-12-13 14:40:50 +0100 | [diff] [blame] | 1077 | dd->device_synchronize = stm32_dma_synchronize; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1078 | dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | |
| 1079 | BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | |
| 1080 | BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); |
| 1081 | dd->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | |
| 1082 | BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | |
| 1083 | BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); |
| 1084 | dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV); |
| 1085 | dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST; |
M'boumba Cedric Madianga | 276b004 | 2016-12-13 14:40:51 +0100 | [diff] [blame^] | 1086 | dd->max_burst = STM32_DMA_MAX_BURST; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1087 | dd->dev = &pdev->dev; |
| 1088 | INIT_LIST_HEAD(&dd->channels); |
| 1089 | |
| 1090 | if (dmadev->mem2mem) { |
| 1091 | dma_cap_set(DMA_MEMCPY, dd->cap_mask); |
| 1092 | dd->device_prep_dma_memcpy = stm32_dma_prep_dma_memcpy; |
| 1093 | dd->directions |= BIT(DMA_MEM_TO_MEM); |
| 1094 | } |
| 1095 | |
| 1096 | for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) { |
| 1097 | chan = &dmadev->chan[i]; |
| 1098 | chan->id = i; |
| 1099 | chan->vchan.desc_free = stm32_dma_desc_free; |
| 1100 | vchan_init(&chan->vchan, dd); |
| 1101 | } |
| 1102 | |
| 1103 | ret = dma_async_device_register(dd); |
| 1104 | if (ret) |
| 1105 | return ret; |
| 1106 | |
| 1107 | for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) { |
| 1108 | chan = &dmadev->chan[i]; |
| 1109 | res = platform_get_resource(pdev, IORESOURCE_IRQ, i); |
| 1110 | if (!res) { |
| 1111 | ret = -EINVAL; |
| 1112 | dev_err(&pdev->dev, "No irq resource for chan %d\n", i); |
| 1113 | goto err_unregister; |
| 1114 | } |
| 1115 | chan->irq = res->start; |
| 1116 | ret = devm_request_irq(&pdev->dev, chan->irq, |
| 1117 | stm32_dma_chan_irq, 0, |
| 1118 | dev_name(chan2dev(chan)), chan); |
| 1119 | if (ret) { |
| 1120 | dev_err(&pdev->dev, |
| 1121 | "request_irq failed with err %d channel %d\n", |
| 1122 | ret, i); |
| 1123 | goto err_unregister; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | ret = of_dma_controller_register(pdev->dev.of_node, |
| 1128 | stm32_dma_of_xlate, dmadev); |
| 1129 | if (ret < 0) { |
| 1130 | dev_err(&pdev->dev, |
| 1131 | "STM32 DMA DMA OF registration failed %d\n", ret); |
| 1132 | goto err_unregister; |
| 1133 | } |
| 1134 | |
| 1135 | platform_set_drvdata(pdev, dmadev); |
| 1136 | |
| 1137 | dev_info(&pdev->dev, "STM32 DMA driver registered\n"); |
| 1138 | |
| 1139 | return 0; |
| 1140 | |
| 1141 | err_unregister: |
| 1142 | dma_async_device_unregister(dd); |
| 1143 | |
| 1144 | return ret; |
| 1145 | } |
| 1146 | |
| 1147 | static struct platform_driver stm32_dma_driver = { |
| 1148 | .driver = { |
| 1149 | .name = "stm32-dma", |
| 1150 | .of_match_table = stm32_dma_of_match, |
| 1151 | }, |
| 1152 | }; |
| 1153 | |
| 1154 | static int __init stm32_dma_init(void) |
| 1155 | { |
| 1156 | return platform_driver_probe(&stm32_dma_driver, stm32_dma_probe); |
| 1157 | } |
| 1158 | subsys_initcall(stm32_dma_init); |