Thomas Gleixner | af873fc | 2019-05-28 09:57:21 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Driver for STM32 DMA controller |
| 4 | * |
| 5 | * Inspired by dma-jz4740.c and tegra20-apb-dma.c |
| 6 | * |
| 7 | * Copyright (C) M'boumba Cedric Madianga 2015 |
| 8 | * Author: M'boumba Cedric Madianga <cedric.madianga@gmail.com> |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 9 | * Pierre-Yves Mordret <pierre-yves.mordret@st.com> |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 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> |
Amelie Delaunay | 409ffc4 | 2020-01-29 16:36:27 +0100 | [diff] [blame^] | 18 | #include <linux/iopoll.h> |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 19 | #include <linux/jiffies.h> |
| 20 | #include <linux/list.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/of_device.h> |
| 24 | #include <linux/of_dma.h> |
| 25 | #include <linux/platform_device.h> |
Pierre-Yves MORDRET | 48bc73b | 2019-01-03 11:17:08 +0100 | [diff] [blame] | 26 | #include <linux/pm_runtime.h> |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 27 | #include <linux/reset.h> |
| 28 | #include <linux/sched.h> |
| 29 | #include <linux/slab.h> |
| 30 | |
| 31 | #include "virt-dma.h" |
| 32 | |
| 33 | #define STM32_DMA_LISR 0x0000 /* DMA Low Int Status Reg */ |
| 34 | #define STM32_DMA_HISR 0x0004 /* DMA High Int Status Reg */ |
| 35 | #define STM32_DMA_LIFCR 0x0008 /* DMA Low Int Flag Clear Reg */ |
| 36 | #define STM32_DMA_HIFCR 0x000c /* DMA High Int Flag Clear Reg */ |
| 37 | #define STM32_DMA_TCI BIT(5) /* Transfer Complete Interrupt */ |
Pierre Yves MORDRET | c2d86b1 | 2018-03-13 17:42:03 +0100 | [diff] [blame] | 38 | #define STM32_DMA_HTI BIT(4) /* Half Transfer Interrupt */ |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 39 | #define STM32_DMA_TEI BIT(3) /* Transfer Error Interrupt */ |
| 40 | #define STM32_DMA_DMEI BIT(2) /* Direct Mode Error Interrupt */ |
| 41 | #define STM32_DMA_FEI BIT(0) /* FIFO Error Interrupt */ |
Pierre Yves MORDRET | 9df3bd5 | 2018-03-13 17:42:05 +0100 | [diff] [blame] | 42 | #define STM32_DMA_MASKI (STM32_DMA_TCI \ |
| 43 | | STM32_DMA_TEI \ |
| 44 | | STM32_DMA_DMEI \ |
| 45 | | STM32_DMA_FEI) |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 46 | |
| 47 | /* DMA Stream x Configuration Register */ |
| 48 | #define STM32_DMA_SCR(x) (0x0010 + 0x18 * (x)) /* x = 0..7 */ |
| 49 | #define STM32_DMA_SCR_REQ(n) ((n & 0x7) << 25) |
| 50 | #define STM32_DMA_SCR_MBURST_MASK GENMASK(24, 23) |
| 51 | #define STM32_DMA_SCR_MBURST(n) ((n & 0x3) << 23) |
| 52 | #define STM32_DMA_SCR_PBURST_MASK GENMASK(22, 21) |
| 53 | #define STM32_DMA_SCR_PBURST(n) ((n & 0x3) << 21) |
| 54 | #define STM32_DMA_SCR_PL_MASK GENMASK(17, 16) |
| 55 | #define STM32_DMA_SCR_PL(n) ((n & 0x3) << 16) |
| 56 | #define STM32_DMA_SCR_MSIZE_MASK GENMASK(14, 13) |
| 57 | #define STM32_DMA_SCR_MSIZE(n) ((n & 0x3) << 13) |
| 58 | #define STM32_DMA_SCR_PSIZE_MASK GENMASK(12, 11) |
| 59 | #define STM32_DMA_SCR_PSIZE(n) ((n & 0x3) << 11) |
| 60 | #define STM32_DMA_SCR_PSIZE_GET(n) ((n & STM32_DMA_SCR_PSIZE_MASK) >> 11) |
| 61 | #define STM32_DMA_SCR_DIR_MASK GENMASK(7, 6) |
| 62 | #define STM32_DMA_SCR_DIR(n) ((n & 0x3) << 6) |
| 63 | #define STM32_DMA_SCR_CT BIT(19) /* Target in double buffer */ |
| 64 | #define STM32_DMA_SCR_DBM BIT(18) /* Double Buffer Mode */ |
| 65 | #define STM32_DMA_SCR_PINCOS BIT(15) /* Peripheral inc offset size */ |
| 66 | #define STM32_DMA_SCR_MINC BIT(10) /* Memory increment mode */ |
| 67 | #define STM32_DMA_SCR_PINC BIT(9) /* Peripheral increment mode */ |
| 68 | #define STM32_DMA_SCR_CIRC BIT(8) /* Circular mode */ |
| 69 | #define STM32_DMA_SCR_PFCTRL BIT(5) /* Peripheral Flow Controller */ |
Pierre Yves MORDRET | 249d553 | 2018-03-13 17:42:07 +0100 | [diff] [blame] | 70 | #define STM32_DMA_SCR_TCIE BIT(4) /* Transfer Complete Int Enable |
| 71 | */ |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 72 | #define STM32_DMA_SCR_TEIE BIT(2) /* Transfer Error Int Enable */ |
| 73 | #define STM32_DMA_SCR_DMEIE BIT(1) /* Direct Mode Err Int Enable */ |
| 74 | #define STM32_DMA_SCR_EN BIT(0) /* Stream Enable */ |
| 75 | #define STM32_DMA_SCR_CFG_MASK (STM32_DMA_SCR_PINC \ |
| 76 | | STM32_DMA_SCR_MINC \ |
| 77 | | STM32_DMA_SCR_PINCOS \ |
| 78 | | STM32_DMA_SCR_PL_MASK) |
| 79 | #define STM32_DMA_SCR_IRQ_MASK (STM32_DMA_SCR_TCIE \ |
| 80 | | STM32_DMA_SCR_TEIE \ |
| 81 | | STM32_DMA_SCR_DMEIE) |
| 82 | |
| 83 | /* DMA Stream x number of data register */ |
| 84 | #define STM32_DMA_SNDTR(x) (0x0014 + 0x18 * (x)) |
| 85 | |
| 86 | /* DMA stream peripheral address register */ |
| 87 | #define STM32_DMA_SPAR(x) (0x0018 + 0x18 * (x)) |
| 88 | |
| 89 | /* DMA stream x memory 0 address register */ |
| 90 | #define STM32_DMA_SM0AR(x) (0x001c + 0x18 * (x)) |
| 91 | |
| 92 | /* DMA stream x memory 1 address register */ |
| 93 | #define STM32_DMA_SM1AR(x) (0x0020 + 0x18 * (x)) |
| 94 | |
| 95 | /* DMA stream x FIFO control register */ |
| 96 | #define STM32_DMA_SFCR(x) (0x0024 + 0x18 * (x)) |
| 97 | #define STM32_DMA_SFCR_FTH_MASK GENMASK(1, 0) |
| 98 | #define STM32_DMA_SFCR_FTH(n) (n & STM32_DMA_SFCR_FTH_MASK) |
| 99 | #define STM32_DMA_SFCR_FEIE BIT(7) /* FIFO error interrupt enable */ |
| 100 | #define STM32_DMA_SFCR_DMDIS BIT(2) /* Direct mode disable */ |
| 101 | #define STM32_DMA_SFCR_MASK (STM32_DMA_SFCR_FEIE \ |
| 102 | | STM32_DMA_SFCR_DMDIS) |
| 103 | |
| 104 | /* DMA direction */ |
| 105 | #define STM32_DMA_DEV_TO_MEM 0x00 |
| 106 | #define STM32_DMA_MEM_TO_DEV 0x01 |
| 107 | #define STM32_DMA_MEM_TO_MEM 0x02 |
| 108 | |
| 109 | /* DMA priority level */ |
| 110 | #define STM32_DMA_PRIORITY_LOW 0x00 |
| 111 | #define STM32_DMA_PRIORITY_MEDIUM 0x01 |
| 112 | #define STM32_DMA_PRIORITY_HIGH 0x02 |
| 113 | #define STM32_DMA_PRIORITY_VERY_HIGH 0x03 |
| 114 | |
| 115 | /* DMA FIFO threshold selection */ |
| 116 | #define STM32_DMA_FIFO_THRESHOLD_1QUARTERFULL 0x00 |
| 117 | #define STM32_DMA_FIFO_THRESHOLD_HALFFULL 0x01 |
| 118 | #define STM32_DMA_FIFO_THRESHOLD_3QUARTERSFULL 0x02 |
| 119 | #define STM32_DMA_FIFO_THRESHOLD_FULL 0x03 |
| 120 | |
| 121 | #define STM32_DMA_MAX_DATA_ITEMS 0xffff |
Pierre Yves MORDRET | 80a7695 | 2018-03-13 17:42:04 +0100 | [diff] [blame] | 122 | /* |
| 123 | * Valid transfer starts from @0 to @0xFFFE leading to unaligned scatter |
| 124 | * gather at boundary. Thus it's safer to round down this value on FIFO |
| 125 | * size (16 Bytes) |
| 126 | */ |
| 127 | #define STM32_DMA_ALIGNED_MAX_DATA_ITEMS \ |
| 128 | ALIGN_DOWN(STM32_DMA_MAX_DATA_ITEMS, 16) |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 129 | #define STM32_DMA_MAX_CHANNELS 0x08 |
| 130 | #define STM32_DMA_MAX_REQUEST_ID 0x08 |
| 131 | #define STM32_DMA_MAX_DATA_PARAM 0x03 |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 132 | #define STM32_DMA_FIFO_SIZE 16 /* FIFO is 16 bytes */ |
| 133 | #define STM32_DMA_MIN_BURST 4 |
M'boumba Cedric Madianga | 276b004 | 2016-12-13 14:40:51 +0100 | [diff] [blame] | 134 | #define STM32_DMA_MAX_BURST 16 |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 135 | |
Pierre Yves MORDRET | 951f44c | 2018-03-13 17:42:01 +0100 | [diff] [blame] | 136 | /* DMA Features */ |
| 137 | #define STM32_DMA_THRESHOLD_FTR_MASK GENMASK(1, 0) |
| 138 | #define STM32_DMA_THRESHOLD_FTR_GET(n) ((n) & STM32_DMA_THRESHOLD_FTR_MASK) |
| 139 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 140 | enum stm32_dma_width { |
| 141 | STM32_DMA_BYTE, |
| 142 | STM32_DMA_HALF_WORD, |
| 143 | STM32_DMA_WORD, |
| 144 | }; |
| 145 | |
| 146 | enum stm32_dma_burst_size { |
| 147 | STM32_DMA_BURST_SINGLE, |
| 148 | STM32_DMA_BURST_INCR4, |
| 149 | STM32_DMA_BURST_INCR8, |
| 150 | STM32_DMA_BURST_INCR16, |
| 151 | }; |
| 152 | |
Pierre Yves MORDRET | 951f44c | 2018-03-13 17:42:01 +0100 | [diff] [blame] | 153 | /** |
| 154 | * struct stm32_dma_cfg - STM32 DMA custom configuration |
| 155 | * @channel_id: channel ID |
| 156 | * @request_line: DMA request |
| 157 | * @stream_config: 32bit mask specifying the DMA channel configuration |
| 158 | * @features: 32bit mask specifying the DMA Feature list |
| 159 | */ |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 160 | struct stm32_dma_cfg { |
| 161 | u32 channel_id; |
| 162 | u32 request_line; |
| 163 | u32 stream_config; |
Pierre Yves MORDRET | 951f44c | 2018-03-13 17:42:01 +0100 | [diff] [blame] | 164 | u32 features; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | struct stm32_dma_chan_reg { |
| 168 | u32 dma_lisr; |
| 169 | u32 dma_hisr; |
| 170 | u32 dma_lifcr; |
| 171 | u32 dma_hifcr; |
| 172 | u32 dma_scr; |
| 173 | u32 dma_sndtr; |
| 174 | u32 dma_spar; |
| 175 | u32 dma_sm0ar; |
| 176 | u32 dma_sm1ar; |
| 177 | u32 dma_sfcr; |
| 178 | }; |
| 179 | |
| 180 | struct stm32_dma_sg_req { |
| 181 | u32 len; |
| 182 | struct stm32_dma_chan_reg chan_reg; |
| 183 | }; |
| 184 | |
| 185 | struct stm32_dma_desc { |
| 186 | struct virt_dma_desc vdesc; |
| 187 | bool cyclic; |
| 188 | u32 num_sgs; |
| 189 | struct stm32_dma_sg_req sg_req[]; |
| 190 | }; |
| 191 | |
| 192 | struct stm32_dma_chan { |
| 193 | struct virt_dma_chan vchan; |
| 194 | bool config_init; |
| 195 | bool busy; |
| 196 | u32 id; |
| 197 | u32 irq; |
| 198 | struct stm32_dma_desc *desc; |
| 199 | u32 next_sg; |
| 200 | struct dma_slave_config dma_sconfig; |
| 201 | struct stm32_dma_chan_reg chan_reg; |
Pierre Yves MORDRET | 951f44c | 2018-03-13 17:42:01 +0100 | [diff] [blame] | 202 | u32 threshold; |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 203 | u32 mem_burst; |
| 204 | u32 mem_width; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | struct stm32_dma_device { |
| 208 | struct dma_device ddev; |
| 209 | void __iomem *base; |
| 210 | struct clk *clk; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 211 | bool mem2mem; |
| 212 | struct stm32_dma_chan chan[STM32_DMA_MAX_CHANNELS]; |
| 213 | }; |
| 214 | |
| 215 | static struct stm32_dma_device *stm32_dma_get_dev(struct stm32_dma_chan *chan) |
| 216 | { |
| 217 | return container_of(chan->vchan.chan.device, struct stm32_dma_device, |
| 218 | ddev); |
| 219 | } |
| 220 | |
| 221 | static struct stm32_dma_chan *to_stm32_dma_chan(struct dma_chan *c) |
| 222 | { |
| 223 | return container_of(c, struct stm32_dma_chan, vchan.chan); |
| 224 | } |
| 225 | |
| 226 | static struct stm32_dma_desc *to_stm32_dma_desc(struct virt_dma_desc *vdesc) |
| 227 | { |
| 228 | return container_of(vdesc, struct stm32_dma_desc, vdesc); |
| 229 | } |
| 230 | |
| 231 | static struct device *chan2dev(struct stm32_dma_chan *chan) |
| 232 | { |
| 233 | return &chan->vchan.chan.dev->device; |
| 234 | } |
| 235 | |
| 236 | static u32 stm32_dma_read(struct stm32_dma_device *dmadev, u32 reg) |
| 237 | { |
| 238 | return readl_relaxed(dmadev->base + reg); |
| 239 | } |
| 240 | |
| 241 | static void stm32_dma_write(struct stm32_dma_device *dmadev, u32 reg, u32 val) |
| 242 | { |
| 243 | writel_relaxed(val, dmadev->base + reg); |
| 244 | } |
| 245 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 246 | static int stm32_dma_get_width(struct stm32_dma_chan *chan, |
| 247 | enum dma_slave_buswidth width) |
| 248 | { |
| 249 | switch (width) { |
| 250 | case DMA_SLAVE_BUSWIDTH_1_BYTE: |
| 251 | return STM32_DMA_BYTE; |
| 252 | case DMA_SLAVE_BUSWIDTH_2_BYTES: |
| 253 | return STM32_DMA_HALF_WORD; |
| 254 | case DMA_SLAVE_BUSWIDTH_4_BYTES: |
| 255 | return STM32_DMA_WORD; |
| 256 | default: |
| 257 | dev_err(chan2dev(chan), "Dma bus width not supported\n"); |
| 258 | return -EINVAL; |
| 259 | } |
| 260 | } |
| 261 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 262 | static enum dma_slave_buswidth stm32_dma_get_max_width(u32 buf_len, |
| 263 | u32 threshold) |
| 264 | { |
| 265 | enum dma_slave_buswidth max_width; |
| 266 | |
| 267 | if (threshold == STM32_DMA_FIFO_THRESHOLD_FULL) |
| 268 | max_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 269 | else |
| 270 | max_width = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 271 | |
| 272 | while ((buf_len < max_width || buf_len % max_width) && |
| 273 | max_width > DMA_SLAVE_BUSWIDTH_1_BYTE) |
| 274 | max_width = max_width >> 1; |
| 275 | |
| 276 | return max_width; |
| 277 | } |
| 278 | |
| 279 | static bool stm32_dma_fifo_threshold_is_allowed(u32 burst, u32 threshold, |
| 280 | enum dma_slave_buswidth width) |
| 281 | { |
| 282 | u32 remaining; |
| 283 | |
| 284 | if (width != DMA_SLAVE_BUSWIDTH_UNDEFINED) { |
| 285 | if (burst != 0) { |
| 286 | /* |
| 287 | * If number of beats fit in several whole bursts |
| 288 | * this configuration is allowed. |
| 289 | */ |
| 290 | remaining = ((STM32_DMA_FIFO_SIZE / width) * |
| 291 | (threshold + 1) / 4) % burst; |
| 292 | |
| 293 | if (remaining == 0) |
| 294 | return true; |
| 295 | } else { |
| 296 | return true; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | static bool stm32_dma_is_burst_possible(u32 buf_len, u32 threshold) |
| 304 | { |
Pierre-Yves MORDRET | cc832dc | 2018-09-11 09:31:16 +0200 | [diff] [blame] | 305 | /* |
| 306 | * Buffer or period length has to be aligned on FIFO depth. |
| 307 | * Otherwise bytes may be stuck within FIFO at buffer or period |
| 308 | * length. |
| 309 | */ |
| 310 | return ((buf_len % ((threshold + 1) * 4)) == 0); |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static u32 stm32_dma_get_best_burst(u32 buf_len, u32 max_burst, u32 threshold, |
| 314 | enum dma_slave_buswidth width) |
| 315 | { |
| 316 | u32 best_burst = max_burst; |
| 317 | |
| 318 | if (best_burst == 1 || !stm32_dma_is_burst_possible(buf_len, threshold)) |
| 319 | return 0; |
| 320 | |
| 321 | while ((buf_len < best_burst * width && best_burst > 1) || |
| 322 | !stm32_dma_fifo_threshold_is_allowed(best_burst, threshold, |
| 323 | width)) { |
| 324 | if (best_burst > STM32_DMA_MIN_BURST) |
| 325 | best_burst = best_burst >> 1; |
| 326 | else |
| 327 | best_burst = 0; |
| 328 | } |
| 329 | |
| 330 | return best_burst; |
| 331 | } |
| 332 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 333 | static int stm32_dma_get_burst(struct stm32_dma_chan *chan, u32 maxburst) |
| 334 | { |
| 335 | switch (maxburst) { |
| 336 | case 0: |
| 337 | case 1: |
| 338 | return STM32_DMA_BURST_SINGLE; |
| 339 | case 4: |
| 340 | return STM32_DMA_BURST_INCR4; |
| 341 | case 8: |
| 342 | return STM32_DMA_BURST_INCR8; |
| 343 | case 16: |
| 344 | return STM32_DMA_BURST_INCR16; |
| 345 | default: |
| 346 | dev_err(chan2dev(chan), "Dma burst size not supported\n"); |
| 347 | return -EINVAL; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | static void stm32_dma_set_fifo_config(struct stm32_dma_chan *chan, |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 352 | u32 src_burst, u32 dst_burst) |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 353 | { |
| 354 | chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_MASK; |
| 355 | chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_DMEIE; |
| 356 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 357 | if (!src_burst && !dst_burst) { |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 358 | /* Using direct mode */ |
| 359 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_DMEIE; |
| 360 | } else { |
| 361 | /* Using FIFO mode */ |
| 362 | chan->chan_reg.dma_sfcr |= STM32_DMA_SFCR_MASK; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | static int stm32_dma_slave_config(struct dma_chan *c, |
| 367 | struct dma_slave_config *config) |
| 368 | { |
| 369 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 370 | |
| 371 | memcpy(&chan->dma_sconfig, config, sizeof(*config)); |
| 372 | |
| 373 | chan->config_init = true; |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static u32 stm32_dma_irq_status(struct stm32_dma_chan *chan) |
| 379 | { |
| 380 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 381 | u32 flags, dma_isr; |
| 382 | |
| 383 | /* |
| 384 | * Read "flags" from DMA_xISR register corresponding to the selected |
| 385 | * DMA channel at the correct bit offset inside that register. |
| 386 | * |
| 387 | * If (ch % 4) is 2 or 3, left shift the mask by 16 bits. |
| 388 | * If (ch % 4) is 1 or 3, additionally left shift the mask by 6 bits. |
| 389 | */ |
| 390 | |
| 391 | if (chan->id & 4) |
| 392 | dma_isr = stm32_dma_read(dmadev, STM32_DMA_HISR); |
| 393 | else |
| 394 | dma_isr = stm32_dma_read(dmadev, STM32_DMA_LISR); |
| 395 | |
| 396 | flags = dma_isr >> (((chan->id & 2) << 3) | ((chan->id & 1) * 6)); |
| 397 | |
Pierre Yves MORDRET | 9df3bd5 | 2018-03-13 17:42:05 +0100 | [diff] [blame] | 398 | return flags & STM32_DMA_MASKI; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static void stm32_dma_irq_clear(struct stm32_dma_chan *chan, u32 flags) |
| 402 | { |
| 403 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 404 | u32 dma_ifcr; |
| 405 | |
| 406 | /* |
| 407 | * Write "flags" to the DMA_xIFCR register corresponding to the selected |
| 408 | * DMA channel at the correct bit offset inside that register. |
| 409 | * |
| 410 | * If (ch % 4) is 2 or 3, left shift the mask by 16 bits. |
| 411 | * If (ch % 4) is 1 or 3, additionally left shift the mask by 6 bits. |
| 412 | */ |
Pierre Yves MORDRET | 9df3bd5 | 2018-03-13 17:42:05 +0100 | [diff] [blame] | 413 | flags &= STM32_DMA_MASKI; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 414 | dma_ifcr = flags << (((chan->id & 2) << 3) | ((chan->id & 1) * 6)); |
| 415 | |
| 416 | if (chan->id & 4) |
| 417 | stm32_dma_write(dmadev, STM32_DMA_HIFCR, dma_ifcr); |
| 418 | else |
| 419 | stm32_dma_write(dmadev, STM32_DMA_LIFCR, dma_ifcr); |
| 420 | } |
| 421 | |
| 422 | static int stm32_dma_disable_chan(struct stm32_dma_chan *chan) |
| 423 | { |
| 424 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
Amelie Delaunay | 409ffc4 | 2020-01-29 16:36:27 +0100 | [diff] [blame^] | 425 | u32 dma_scr, id, reg; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 426 | |
| 427 | id = chan->id; |
Amelie Delaunay | 409ffc4 | 2020-01-29 16:36:27 +0100 | [diff] [blame^] | 428 | reg = STM32_DMA_SCR(id); |
| 429 | dma_scr = stm32_dma_read(dmadev, reg); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 430 | |
| 431 | if (dma_scr & STM32_DMA_SCR_EN) { |
| 432 | dma_scr &= ~STM32_DMA_SCR_EN; |
Amelie Delaunay | 409ffc4 | 2020-01-29 16:36:27 +0100 | [diff] [blame^] | 433 | stm32_dma_write(dmadev, reg, dma_scr); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 434 | |
Amelie Delaunay | 409ffc4 | 2020-01-29 16:36:27 +0100 | [diff] [blame^] | 435 | return readl_relaxed_poll_timeout_atomic(dmadev->base + reg, |
| 436 | dma_scr, !(dma_scr & STM32_DMA_SCR_EN), |
| 437 | 10, 1000000); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | static void stm32_dma_stop(struct stm32_dma_chan *chan) |
| 444 | { |
| 445 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 446 | u32 dma_scr, dma_sfcr, status; |
| 447 | int ret; |
| 448 | |
| 449 | /* Disable interrupts */ |
| 450 | dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id)); |
| 451 | dma_scr &= ~STM32_DMA_SCR_IRQ_MASK; |
| 452 | stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), dma_scr); |
| 453 | dma_sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id)); |
| 454 | dma_sfcr &= ~STM32_DMA_SFCR_FEIE; |
| 455 | stm32_dma_write(dmadev, STM32_DMA_SFCR(chan->id), dma_sfcr); |
| 456 | |
| 457 | /* Disable DMA */ |
| 458 | ret = stm32_dma_disable_chan(chan); |
| 459 | if (ret < 0) |
| 460 | return; |
| 461 | |
| 462 | /* Clear interrupt status if it is there */ |
| 463 | status = stm32_dma_irq_status(chan); |
| 464 | if (status) { |
| 465 | dev_dbg(chan2dev(chan), "%s(): clearing interrupt: 0x%08x\n", |
| 466 | __func__, status); |
| 467 | stm32_dma_irq_clear(chan, status); |
| 468 | } |
| 469 | |
| 470 | chan->busy = false; |
| 471 | } |
| 472 | |
| 473 | static int stm32_dma_terminate_all(struct dma_chan *c) |
| 474 | { |
| 475 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 476 | unsigned long flags; |
| 477 | LIST_HEAD(head); |
| 478 | |
| 479 | spin_lock_irqsave(&chan->vchan.lock, flags); |
| 480 | |
| 481 | if (chan->busy) { |
| 482 | stm32_dma_stop(chan); |
| 483 | chan->desc = NULL; |
| 484 | } |
| 485 | |
| 486 | vchan_get_all_descriptors(&chan->vchan, &head); |
| 487 | spin_unlock_irqrestore(&chan->vchan.lock, flags); |
| 488 | vchan_dma_desc_free_list(&chan->vchan, &head); |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
M'boumba Cedric Madianga | dc80867 | 2016-12-13 14:40:50 +0100 | [diff] [blame] | 493 | static void stm32_dma_synchronize(struct dma_chan *c) |
| 494 | { |
| 495 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 496 | |
| 497 | vchan_synchronize(&chan->vchan); |
| 498 | } |
| 499 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 500 | static void stm32_dma_dump_reg(struct stm32_dma_chan *chan) |
| 501 | { |
| 502 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 503 | u32 scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id)); |
| 504 | u32 ndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id)); |
| 505 | u32 spar = stm32_dma_read(dmadev, STM32_DMA_SPAR(chan->id)); |
| 506 | u32 sm0ar = stm32_dma_read(dmadev, STM32_DMA_SM0AR(chan->id)); |
| 507 | u32 sm1ar = stm32_dma_read(dmadev, STM32_DMA_SM1AR(chan->id)); |
| 508 | u32 sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id)); |
| 509 | |
| 510 | dev_dbg(chan2dev(chan), "SCR: 0x%08x\n", scr); |
| 511 | dev_dbg(chan2dev(chan), "NDTR: 0x%08x\n", ndtr); |
| 512 | dev_dbg(chan2dev(chan), "SPAR: 0x%08x\n", spar); |
| 513 | dev_dbg(chan2dev(chan), "SM0AR: 0x%08x\n", sm0ar); |
| 514 | dev_dbg(chan2dev(chan), "SM1AR: 0x%08x\n", sm1ar); |
| 515 | dev_dbg(chan2dev(chan), "SFCR: 0x%08x\n", sfcr); |
| 516 | } |
| 517 | |
Pierre Yves MORDRET | e57cb3b | 2018-03-13 17:42:06 +0100 | [diff] [blame] | 518 | static void stm32_dma_configure_next_sg(struct stm32_dma_chan *chan); |
| 519 | |
M'boumba Cedric Madianga | 8d1b76f | 2016-12-13 14:40:47 +0100 | [diff] [blame] | 520 | 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] | 521 | { |
| 522 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 523 | struct virt_dma_desc *vdesc; |
| 524 | struct stm32_dma_sg_req *sg_req; |
| 525 | struct stm32_dma_chan_reg *reg; |
| 526 | u32 status; |
| 527 | int ret; |
| 528 | |
| 529 | ret = stm32_dma_disable_chan(chan); |
| 530 | if (ret < 0) |
M'boumba Cedric Madianga | 8d1b76f | 2016-12-13 14:40:47 +0100 | [diff] [blame] | 531 | return; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 532 | |
| 533 | if (!chan->desc) { |
| 534 | vdesc = vchan_next_desc(&chan->vchan); |
| 535 | if (!vdesc) |
M'boumba Cedric Madianga | 8d1b76f | 2016-12-13 14:40:47 +0100 | [diff] [blame] | 536 | return; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 537 | |
| 538 | chan->desc = to_stm32_dma_desc(vdesc); |
| 539 | chan->next_sg = 0; |
| 540 | } |
| 541 | |
| 542 | if (chan->next_sg == chan->desc->num_sgs) |
| 543 | chan->next_sg = 0; |
| 544 | |
| 545 | sg_req = &chan->desc->sg_req[chan->next_sg]; |
| 546 | reg = &sg_req->chan_reg; |
| 547 | |
Pierre-Yves MORDRET | 22a0bb2 | 2020-01-29 16:36:24 +0100 | [diff] [blame] | 548 | reg->dma_scr &= ~STM32_DMA_SCR_EN; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 549 | stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), reg->dma_scr); |
| 550 | stm32_dma_write(dmadev, STM32_DMA_SPAR(chan->id), reg->dma_spar); |
| 551 | stm32_dma_write(dmadev, STM32_DMA_SM0AR(chan->id), reg->dma_sm0ar); |
| 552 | stm32_dma_write(dmadev, STM32_DMA_SFCR(chan->id), reg->dma_sfcr); |
| 553 | stm32_dma_write(dmadev, STM32_DMA_SM1AR(chan->id), reg->dma_sm1ar); |
| 554 | stm32_dma_write(dmadev, STM32_DMA_SNDTR(chan->id), reg->dma_sndtr); |
| 555 | |
| 556 | chan->next_sg++; |
| 557 | |
| 558 | /* Clear interrupt status if it is there */ |
| 559 | status = stm32_dma_irq_status(chan); |
| 560 | if (status) |
| 561 | stm32_dma_irq_clear(chan, status); |
| 562 | |
Pierre Yves MORDRET | e57cb3b | 2018-03-13 17:42:06 +0100 | [diff] [blame] | 563 | if (chan->desc->cyclic) |
| 564 | stm32_dma_configure_next_sg(chan); |
| 565 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 566 | stm32_dma_dump_reg(chan); |
| 567 | |
| 568 | /* Start DMA */ |
| 569 | reg->dma_scr |= STM32_DMA_SCR_EN; |
| 570 | stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), reg->dma_scr); |
| 571 | |
| 572 | chan->busy = true; |
| 573 | |
Benjamin Gaignard | 90ec93c | 2018-07-06 15:02:20 +0200 | [diff] [blame] | 574 | dev_dbg(chan2dev(chan), "vchan %pK: started\n", &chan->vchan); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | static void stm32_dma_configure_next_sg(struct stm32_dma_chan *chan) |
| 578 | { |
| 579 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 580 | struct stm32_dma_sg_req *sg_req; |
| 581 | u32 dma_scr, dma_sm0ar, dma_sm1ar, id; |
| 582 | |
| 583 | id = chan->id; |
| 584 | dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id)); |
| 585 | |
| 586 | if (dma_scr & STM32_DMA_SCR_DBM) { |
| 587 | if (chan->next_sg == chan->desc->num_sgs) |
| 588 | chan->next_sg = 0; |
| 589 | |
| 590 | sg_req = &chan->desc->sg_req[chan->next_sg]; |
| 591 | |
| 592 | if (dma_scr & STM32_DMA_SCR_CT) { |
| 593 | dma_sm0ar = sg_req->chan_reg.dma_sm0ar; |
| 594 | stm32_dma_write(dmadev, STM32_DMA_SM0AR(id), dma_sm0ar); |
| 595 | dev_dbg(chan2dev(chan), "CT=1 <=> SM0AR: 0x%08x\n", |
| 596 | stm32_dma_read(dmadev, STM32_DMA_SM0AR(id))); |
| 597 | } else { |
| 598 | dma_sm1ar = sg_req->chan_reg.dma_sm1ar; |
| 599 | stm32_dma_write(dmadev, STM32_DMA_SM1AR(id), dma_sm1ar); |
| 600 | dev_dbg(chan2dev(chan), "CT=0 <=> SM1AR: 0x%08x\n", |
| 601 | stm32_dma_read(dmadev, STM32_DMA_SM1AR(id))); |
| 602 | } |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
| 606 | static void stm32_dma_handle_chan_done(struct stm32_dma_chan *chan) |
| 607 | { |
| 608 | if (chan->desc) { |
| 609 | if (chan->desc->cyclic) { |
| 610 | vchan_cyclic_callback(&chan->desc->vdesc); |
M'boumba Cedric Madianga | 2b12c558 | 2016-12-13 14:40:48 +0100 | [diff] [blame] | 611 | chan->next_sg++; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 612 | stm32_dma_configure_next_sg(chan); |
| 613 | } else { |
| 614 | chan->busy = false; |
| 615 | if (chan->next_sg == chan->desc->num_sgs) { |
| 616 | list_del(&chan->desc->vdesc.node); |
| 617 | vchan_cookie_complete(&chan->desc->vdesc); |
| 618 | chan->desc = NULL; |
| 619 | } |
| 620 | stm32_dma_start_transfer(chan); |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | static irqreturn_t stm32_dma_chan_irq(int irq, void *devid) |
| 626 | { |
| 627 | struct stm32_dma_chan *chan = devid; |
| 628 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
Pierre-Yves MORDRET | ca4c72c | 2019-01-03 11:17:29 +0100 | [diff] [blame] | 629 | u32 status, scr, sfcr; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 630 | |
| 631 | spin_lock(&chan->vchan.lock); |
| 632 | |
| 633 | status = stm32_dma_irq_status(chan); |
| 634 | scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id)); |
Pierre-Yves MORDRET | ca4c72c | 2019-01-03 11:17:29 +0100 | [diff] [blame] | 635 | sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id)); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 636 | |
Pierre Yves MORDRET | c2d86b1 | 2018-03-13 17:42:03 +0100 | [diff] [blame] | 637 | if (status & STM32_DMA_TCI) { |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 638 | stm32_dma_irq_clear(chan, STM32_DMA_TCI); |
Pierre Yves MORDRET | c2d86b1 | 2018-03-13 17:42:03 +0100 | [diff] [blame] | 639 | if (scr & STM32_DMA_SCR_TCIE) |
| 640 | stm32_dma_handle_chan_done(chan); |
| 641 | status &= ~STM32_DMA_TCI; |
| 642 | } |
| 643 | if (status & STM32_DMA_HTI) { |
| 644 | stm32_dma_irq_clear(chan, STM32_DMA_HTI); |
| 645 | status &= ~STM32_DMA_HTI; |
| 646 | } |
| 647 | if (status & STM32_DMA_FEI) { |
| 648 | stm32_dma_irq_clear(chan, STM32_DMA_FEI); |
| 649 | status &= ~STM32_DMA_FEI; |
Pierre-Yves MORDRET | ca4c72c | 2019-01-03 11:17:29 +0100 | [diff] [blame] | 650 | if (sfcr & STM32_DMA_SFCR_FEIE) { |
| 651 | if (!(scr & STM32_DMA_SCR_EN)) |
| 652 | dev_err(chan2dev(chan), "FIFO Error\n"); |
| 653 | else |
| 654 | dev_dbg(chan2dev(chan), "FIFO over/underrun\n"); |
| 655 | } |
Pierre Yves MORDRET | c2d86b1 | 2018-03-13 17:42:03 +0100 | [diff] [blame] | 656 | } |
| 657 | if (status) { |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 658 | stm32_dma_irq_clear(chan, status); |
| 659 | dev_err(chan2dev(chan), "DMA error: status=0x%08x\n", status); |
Pierre Yves MORDRET | c2d86b1 | 2018-03-13 17:42:03 +0100 | [diff] [blame] | 660 | if (!(scr & STM32_DMA_SCR_EN)) |
| 661 | dev_err(chan2dev(chan), "chan disabled by HW\n"); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | spin_unlock(&chan->vchan.lock); |
| 665 | |
| 666 | return IRQ_HANDLED; |
| 667 | } |
| 668 | |
| 669 | static void stm32_dma_issue_pending(struct dma_chan *c) |
| 670 | { |
| 671 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 672 | unsigned long flags; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 673 | |
| 674 | spin_lock_irqsave(&chan->vchan.lock, flags); |
M'boumba Cedric Madianga | 8d1b76f | 2016-12-13 14:40:47 +0100 | [diff] [blame] | 675 | if (vchan_issue_pending(&chan->vchan) && !chan->desc && !chan->busy) { |
Benjamin Gaignard | 90ec93c | 2018-07-06 15:02:20 +0200 | [diff] [blame] | 676 | dev_dbg(chan2dev(chan), "vchan %pK: issued\n", &chan->vchan); |
M'boumba Cedric Madianga | 8d1b76f | 2016-12-13 14:40:47 +0100 | [diff] [blame] | 677 | stm32_dma_start_transfer(chan); |
Pierre Yves MORDRET | e57cb3b | 2018-03-13 17:42:06 +0100 | [diff] [blame] | 678 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 679 | } |
| 680 | spin_unlock_irqrestore(&chan->vchan.lock, flags); |
| 681 | } |
| 682 | |
| 683 | static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan, |
| 684 | enum dma_transfer_direction direction, |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 685 | enum dma_slave_buswidth *buswidth, |
| 686 | u32 buf_len) |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 687 | { |
| 688 | enum dma_slave_buswidth src_addr_width, dst_addr_width; |
| 689 | int src_bus_width, dst_bus_width; |
| 690 | int src_burst_size, dst_burst_size; |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 691 | u32 src_maxburst, dst_maxburst, src_best_burst, dst_best_burst; |
| 692 | u32 dma_scr, threshold; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 693 | |
| 694 | src_addr_width = chan->dma_sconfig.src_addr_width; |
| 695 | dst_addr_width = chan->dma_sconfig.dst_addr_width; |
| 696 | src_maxburst = chan->dma_sconfig.src_maxburst; |
| 697 | dst_maxburst = chan->dma_sconfig.dst_maxburst; |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 698 | threshold = chan->threshold; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 699 | |
| 700 | switch (direction) { |
| 701 | case DMA_MEM_TO_DEV: |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 702 | /* Set device data size */ |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 703 | dst_bus_width = stm32_dma_get_width(chan, dst_addr_width); |
| 704 | if (dst_bus_width < 0) |
| 705 | return dst_bus_width; |
| 706 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 707 | /* Set device burst size */ |
| 708 | dst_best_burst = stm32_dma_get_best_burst(buf_len, |
| 709 | dst_maxburst, |
| 710 | threshold, |
| 711 | dst_addr_width); |
| 712 | |
| 713 | dst_burst_size = stm32_dma_get_burst(chan, dst_best_burst); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 714 | if (dst_burst_size < 0) |
| 715 | return dst_burst_size; |
| 716 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 717 | /* Set memory data size */ |
| 718 | src_addr_width = stm32_dma_get_max_width(buf_len, threshold); |
| 719 | chan->mem_width = src_addr_width; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 720 | src_bus_width = stm32_dma_get_width(chan, src_addr_width); |
| 721 | if (src_bus_width < 0) |
| 722 | return src_bus_width; |
| 723 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 724 | /* Set memory burst size */ |
| 725 | src_maxburst = STM32_DMA_MAX_BURST; |
| 726 | src_best_burst = stm32_dma_get_best_burst(buf_len, |
| 727 | src_maxburst, |
| 728 | threshold, |
| 729 | src_addr_width); |
| 730 | src_burst_size = stm32_dma_get_burst(chan, src_best_burst); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 731 | if (src_burst_size < 0) |
| 732 | return src_burst_size; |
| 733 | |
| 734 | dma_scr = STM32_DMA_SCR_DIR(STM32_DMA_MEM_TO_DEV) | |
| 735 | STM32_DMA_SCR_PSIZE(dst_bus_width) | |
| 736 | STM32_DMA_SCR_MSIZE(src_bus_width) | |
| 737 | STM32_DMA_SCR_PBURST(dst_burst_size) | |
| 738 | STM32_DMA_SCR_MBURST(src_burst_size); |
| 739 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 740 | /* Set FIFO threshold */ |
| 741 | chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_FTH_MASK; |
| 742 | chan->chan_reg.dma_sfcr |= STM32_DMA_SFCR_FTH(threshold); |
| 743 | |
| 744 | /* Set peripheral address */ |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 745 | chan->chan_reg.dma_spar = chan->dma_sconfig.dst_addr; |
| 746 | *buswidth = dst_addr_width; |
| 747 | break; |
| 748 | |
| 749 | case DMA_DEV_TO_MEM: |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 750 | /* Set device data size */ |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 751 | src_bus_width = stm32_dma_get_width(chan, src_addr_width); |
| 752 | if (src_bus_width < 0) |
| 753 | return src_bus_width; |
| 754 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 755 | /* Set device burst size */ |
| 756 | src_best_burst = stm32_dma_get_best_burst(buf_len, |
| 757 | src_maxburst, |
| 758 | threshold, |
| 759 | src_addr_width); |
| 760 | chan->mem_burst = src_best_burst; |
| 761 | src_burst_size = stm32_dma_get_burst(chan, src_best_burst); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 762 | if (src_burst_size < 0) |
| 763 | return src_burst_size; |
| 764 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 765 | /* Set memory data size */ |
| 766 | dst_addr_width = stm32_dma_get_max_width(buf_len, threshold); |
| 767 | chan->mem_width = dst_addr_width; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 768 | dst_bus_width = stm32_dma_get_width(chan, dst_addr_width); |
| 769 | if (dst_bus_width < 0) |
| 770 | return dst_bus_width; |
| 771 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 772 | /* Set memory burst size */ |
| 773 | dst_maxburst = STM32_DMA_MAX_BURST; |
| 774 | dst_best_burst = stm32_dma_get_best_burst(buf_len, |
| 775 | dst_maxburst, |
| 776 | threshold, |
| 777 | dst_addr_width); |
| 778 | chan->mem_burst = dst_best_burst; |
| 779 | dst_burst_size = stm32_dma_get_burst(chan, dst_best_burst); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 780 | if (dst_burst_size < 0) |
| 781 | return dst_burst_size; |
| 782 | |
| 783 | dma_scr = STM32_DMA_SCR_DIR(STM32_DMA_DEV_TO_MEM) | |
| 784 | STM32_DMA_SCR_PSIZE(src_bus_width) | |
| 785 | STM32_DMA_SCR_MSIZE(dst_bus_width) | |
| 786 | STM32_DMA_SCR_PBURST(src_burst_size) | |
| 787 | STM32_DMA_SCR_MBURST(dst_burst_size); |
| 788 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 789 | /* Set FIFO threshold */ |
| 790 | chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_FTH_MASK; |
| 791 | chan->chan_reg.dma_sfcr |= STM32_DMA_SFCR_FTH(threshold); |
| 792 | |
| 793 | /* Set peripheral address */ |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 794 | chan->chan_reg.dma_spar = chan->dma_sconfig.src_addr; |
| 795 | *buswidth = chan->dma_sconfig.src_addr_width; |
| 796 | break; |
| 797 | |
| 798 | default: |
| 799 | dev_err(chan2dev(chan), "Dma direction is not supported\n"); |
| 800 | return -EINVAL; |
| 801 | } |
| 802 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 803 | stm32_dma_set_fifo_config(chan, src_best_burst, dst_best_burst); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 804 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 805 | /* Set DMA control register */ |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 806 | chan->chan_reg.dma_scr &= ~(STM32_DMA_SCR_DIR_MASK | |
| 807 | STM32_DMA_SCR_PSIZE_MASK | STM32_DMA_SCR_MSIZE_MASK | |
| 808 | STM32_DMA_SCR_PBURST_MASK | STM32_DMA_SCR_MBURST_MASK); |
| 809 | chan->chan_reg.dma_scr |= dma_scr; |
| 810 | |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | static void stm32_dma_clear_reg(struct stm32_dma_chan_reg *regs) |
| 815 | { |
| 816 | memset(regs, 0, sizeof(struct stm32_dma_chan_reg)); |
| 817 | } |
| 818 | |
| 819 | static struct dma_async_tx_descriptor *stm32_dma_prep_slave_sg( |
| 820 | struct dma_chan *c, struct scatterlist *sgl, |
| 821 | u32 sg_len, enum dma_transfer_direction direction, |
| 822 | unsigned long flags, void *context) |
| 823 | { |
| 824 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 825 | struct stm32_dma_desc *desc; |
| 826 | struct scatterlist *sg; |
| 827 | enum dma_slave_buswidth buswidth; |
| 828 | u32 nb_data_items; |
| 829 | int i, ret; |
| 830 | |
| 831 | if (!chan->config_init) { |
| 832 | dev_err(chan2dev(chan), "dma channel is not configured\n"); |
| 833 | return NULL; |
| 834 | } |
| 835 | |
| 836 | if (sg_len < 1) { |
| 837 | dev_err(chan2dev(chan), "Invalid segment length %d\n", sg_len); |
| 838 | return NULL; |
| 839 | } |
| 840 | |
Gustavo A. R. Silva | 402096c | 2019-08-30 11:14:23 -0500 | [diff] [blame] | 841 | desc = kzalloc(struct_size(desc, sg_req, sg_len), GFP_NOWAIT); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 842 | if (!desc) |
| 843 | return NULL; |
| 844 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 845 | /* Set peripheral flow controller */ |
| 846 | if (chan->dma_sconfig.device_fc) |
| 847 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_PFCTRL; |
| 848 | else |
| 849 | chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL; |
| 850 | |
| 851 | for_each_sg(sgl, sg, sg_len, i) { |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 852 | ret = stm32_dma_set_xfer_param(chan, direction, &buswidth, |
| 853 | sg_dma_len(sg)); |
| 854 | if (ret < 0) |
| 855 | goto err; |
| 856 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 857 | desc->sg_req[i].len = sg_dma_len(sg); |
| 858 | |
| 859 | nb_data_items = desc->sg_req[i].len / buswidth; |
Pierre Yves MORDRET | 80a7695 | 2018-03-13 17:42:04 +0100 | [diff] [blame] | 860 | if (nb_data_items > STM32_DMA_ALIGNED_MAX_DATA_ITEMS) { |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 861 | dev_err(chan2dev(chan), "nb items not supported\n"); |
| 862 | goto err; |
| 863 | } |
| 864 | |
| 865 | stm32_dma_clear_reg(&desc->sg_req[i].chan_reg); |
| 866 | desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr; |
| 867 | desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr; |
| 868 | desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar; |
| 869 | desc->sg_req[i].chan_reg.dma_sm0ar = sg_dma_address(sg); |
| 870 | desc->sg_req[i].chan_reg.dma_sm1ar = sg_dma_address(sg); |
| 871 | desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items; |
| 872 | } |
| 873 | |
| 874 | desc->num_sgs = sg_len; |
| 875 | desc->cyclic = false; |
| 876 | |
| 877 | return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags); |
| 878 | |
| 879 | err: |
| 880 | kfree(desc); |
| 881 | return NULL; |
| 882 | } |
| 883 | |
| 884 | static struct dma_async_tx_descriptor *stm32_dma_prep_dma_cyclic( |
| 885 | struct dma_chan *c, dma_addr_t buf_addr, size_t buf_len, |
| 886 | size_t period_len, enum dma_transfer_direction direction, |
| 887 | unsigned long flags) |
| 888 | { |
| 889 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 890 | struct stm32_dma_desc *desc; |
| 891 | enum dma_slave_buswidth buswidth; |
| 892 | u32 num_periods, nb_data_items; |
| 893 | int i, ret; |
| 894 | |
| 895 | if (!buf_len || !period_len) { |
| 896 | dev_err(chan2dev(chan), "Invalid buffer/period len\n"); |
| 897 | return NULL; |
| 898 | } |
| 899 | |
| 900 | if (!chan->config_init) { |
| 901 | dev_err(chan2dev(chan), "dma channel is not configured\n"); |
| 902 | return NULL; |
| 903 | } |
| 904 | |
| 905 | if (buf_len % period_len) { |
| 906 | dev_err(chan2dev(chan), "buf_len not multiple of period_len\n"); |
| 907 | return NULL; |
| 908 | } |
| 909 | |
| 910 | /* |
| 911 | * We allow to take more number of requests till DMA is |
| 912 | * not started. The driver will loop over all requests. |
| 913 | * Once DMA is started then new requests can be queued only after |
| 914 | * terminating the DMA. |
| 915 | */ |
| 916 | if (chan->busy) { |
| 917 | dev_err(chan2dev(chan), "Request not allowed when dma busy\n"); |
| 918 | return NULL; |
| 919 | } |
| 920 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 921 | ret = stm32_dma_set_xfer_param(chan, direction, &buswidth, period_len); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 922 | if (ret < 0) |
| 923 | return NULL; |
| 924 | |
| 925 | nb_data_items = period_len / buswidth; |
Pierre Yves MORDRET | 80a7695 | 2018-03-13 17:42:04 +0100 | [diff] [blame] | 926 | if (nb_data_items > STM32_DMA_ALIGNED_MAX_DATA_ITEMS) { |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 927 | dev_err(chan2dev(chan), "number of items not supported\n"); |
| 928 | return NULL; |
| 929 | } |
| 930 | |
| 931 | /* Enable Circular mode or double buffer mode */ |
| 932 | if (buf_len == period_len) |
| 933 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_CIRC; |
| 934 | else |
| 935 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_DBM; |
| 936 | |
| 937 | /* Clear periph ctrl if client set it */ |
| 938 | chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL; |
| 939 | |
| 940 | num_periods = buf_len / period_len; |
| 941 | |
Gustavo A. R. Silva | 402096c | 2019-08-30 11:14:23 -0500 | [diff] [blame] | 942 | desc = kzalloc(struct_size(desc, sg_req, num_periods), GFP_NOWAIT); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 943 | if (!desc) |
| 944 | return NULL; |
| 945 | |
| 946 | for (i = 0; i < num_periods; i++) { |
| 947 | desc->sg_req[i].len = period_len; |
| 948 | |
| 949 | stm32_dma_clear_reg(&desc->sg_req[i].chan_reg); |
| 950 | desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr; |
| 951 | desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr; |
| 952 | desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar; |
| 953 | desc->sg_req[i].chan_reg.dma_sm0ar = buf_addr; |
| 954 | desc->sg_req[i].chan_reg.dma_sm1ar = buf_addr; |
| 955 | desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items; |
| 956 | buf_addr += period_len; |
| 957 | } |
| 958 | |
| 959 | desc->num_sgs = num_periods; |
| 960 | desc->cyclic = true; |
| 961 | |
| 962 | return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags); |
| 963 | } |
| 964 | |
| 965 | static struct dma_async_tx_descriptor *stm32_dma_prep_dma_memcpy( |
| 966 | struct dma_chan *c, dma_addr_t dest, |
| 967 | dma_addr_t src, size_t len, unsigned long flags) |
| 968 | { |
| 969 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 970 | enum dma_slave_buswidth max_width; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 971 | struct stm32_dma_desc *desc; |
| 972 | size_t xfer_count, offset; |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 973 | u32 num_sgs, best_burst, dma_burst, threshold; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 974 | int i; |
| 975 | |
Pierre Yves MORDRET | 80a7695 | 2018-03-13 17:42:04 +0100 | [diff] [blame] | 976 | num_sgs = DIV_ROUND_UP(len, STM32_DMA_ALIGNED_MAX_DATA_ITEMS); |
Gustavo A. R. Silva | 402096c | 2019-08-30 11:14:23 -0500 | [diff] [blame] | 977 | desc = kzalloc(struct_size(desc, sg_req, num_sgs), GFP_NOWAIT); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 978 | if (!desc) |
| 979 | return NULL; |
| 980 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 981 | threshold = chan->threshold; |
| 982 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 983 | for (offset = 0, i = 0; offset < len; offset += xfer_count, i++) { |
| 984 | xfer_count = min_t(size_t, len - offset, |
Pierre Yves MORDRET | 80a7695 | 2018-03-13 17:42:04 +0100 | [diff] [blame] | 985 | STM32_DMA_ALIGNED_MAX_DATA_ITEMS); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 986 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 987 | /* Compute best burst size */ |
| 988 | max_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 989 | best_burst = stm32_dma_get_best_burst(len, STM32_DMA_MAX_BURST, |
| 990 | threshold, max_width); |
| 991 | dma_burst = stm32_dma_get_burst(chan, best_burst); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 992 | |
| 993 | stm32_dma_clear_reg(&desc->sg_req[i].chan_reg); |
| 994 | desc->sg_req[i].chan_reg.dma_scr = |
| 995 | STM32_DMA_SCR_DIR(STM32_DMA_MEM_TO_MEM) | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 996 | STM32_DMA_SCR_PBURST(dma_burst) | |
| 997 | STM32_DMA_SCR_MBURST(dma_burst) | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 998 | STM32_DMA_SCR_MINC | |
| 999 | STM32_DMA_SCR_PINC | |
| 1000 | STM32_DMA_SCR_TCIE | |
| 1001 | STM32_DMA_SCR_TEIE; |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 1002 | desc->sg_req[i].chan_reg.dma_sfcr |= STM32_DMA_SFCR_MASK; |
| 1003 | desc->sg_req[i].chan_reg.dma_sfcr |= |
| 1004 | STM32_DMA_SFCR_FTH(threshold); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1005 | desc->sg_req[i].chan_reg.dma_spar = src + offset; |
| 1006 | desc->sg_req[i].chan_reg.dma_sm0ar = dest + offset; |
| 1007 | desc->sg_req[i].chan_reg.dma_sndtr = xfer_count; |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 1008 | desc->sg_req[i].len = xfer_count; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | desc->num_sgs = num_sgs; |
| 1012 | desc->cyclic = false; |
| 1013 | |
| 1014 | return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags); |
| 1015 | } |
| 1016 | |
M'boumba Cedric Madianga | 2b12c558 | 2016-12-13 14:40:48 +0100 | [diff] [blame] | 1017 | static u32 stm32_dma_get_remaining_bytes(struct stm32_dma_chan *chan) |
| 1018 | { |
| 1019 | u32 dma_scr, width, ndtr; |
| 1020 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 1021 | |
| 1022 | dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id)); |
| 1023 | width = STM32_DMA_SCR_PSIZE_GET(dma_scr); |
| 1024 | ndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id)); |
| 1025 | |
| 1026 | return ndtr << width; |
| 1027 | } |
| 1028 | |
Arnaud Pouliquen | 2a4885a | 2019-05-02 11:28:42 +0200 | [diff] [blame] | 1029 | /** |
| 1030 | * stm32_dma_is_current_sg - check that expected sg_req is currently transferred |
| 1031 | * @chan: dma channel |
| 1032 | * |
| 1033 | * This function called when IRQ are disable, checks that the hardware has not |
| 1034 | * switched on the next transfer in double buffer mode. The test is done by |
| 1035 | * comparing the next_sg memory address with the hardware related register |
| 1036 | * (based on CT bit value). |
| 1037 | * |
| 1038 | * Returns true if expected current transfer is still running or double |
| 1039 | * buffer mode is not activated. |
| 1040 | */ |
| 1041 | static bool stm32_dma_is_current_sg(struct stm32_dma_chan *chan) |
| 1042 | { |
| 1043 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 1044 | struct stm32_dma_sg_req *sg_req; |
| 1045 | u32 dma_scr, dma_smar, id; |
| 1046 | |
| 1047 | id = chan->id; |
| 1048 | dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id)); |
| 1049 | |
| 1050 | if (!(dma_scr & STM32_DMA_SCR_DBM)) |
| 1051 | return true; |
| 1052 | |
| 1053 | sg_req = &chan->desc->sg_req[chan->next_sg]; |
| 1054 | |
| 1055 | if (dma_scr & STM32_DMA_SCR_CT) { |
| 1056 | dma_smar = stm32_dma_read(dmadev, STM32_DMA_SM0AR(id)); |
| 1057 | return (dma_smar == sg_req->chan_reg.dma_sm0ar); |
| 1058 | } |
| 1059 | |
| 1060 | dma_smar = stm32_dma_read(dmadev, STM32_DMA_SM1AR(id)); |
| 1061 | |
| 1062 | return (dma_smar == sg_req->chan_reg.dma_sm1ar); |
| 1063 | } |
| 1064 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1065 | static size_t stm32_dma_desc_residue(struct stm32_dma_chan *chan, |
| 1066 | struct stm32_dma_desc *desc, |
| 1067 | u32 next_sg) |
| 1068 | { |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 1069 | u32 modulo, burst_size; |
Arnaud Pouliquen | 2a4885a | 2019-05-02 11:28:42 +0200 | [diff] [blame] | 1070 | u32 residue; |
| 1071 | u32 n_sg = next_sg; |
| 1072 | struct stm32_dma_sg_req *sg_req = &chan->desc->sg_req[chan->next_sg]; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1073 | int i; |
| 1074 | |
M'boumba Cedric Madianga | 2b12c558 | 2016-12-13 14:40:48 +0100 | [diff] [blame] | 1075 | /* |
Arnaud Pouliquen | 2a4885a | 2019-05-02 11:28:42 +0200 | [diff] [blame] | 1076 | * Calculate the residue means compute the descriptors |
| 1077 | * information: |
| 1078 | * - the sg_req currently transferred |
| 1079 | * - the Hardware remaining position in this sg (NDTR bits field). |
| 1080 | * |
| 1081 | * A race condition may occur if DMA is running in cyclic or double |
| 1082 | * buffer mode, since the DMA register are automatically reloaded at end |
| 1083 | * of period transfer. The hardware may have switched to the next |
| 1084 | * transfer (CT bit updated) just before the position (SxNDTR reg) is |
| 1085 | * read. |
| 1086 | * In this case the SxNDTR reg could (or not) correspond to the new |
| 1087 | * transfer position, and not the expected one. |
| 1088 | * The strategy implemented in the stm32 driver is to: |
| 1089 | * - read the SxNDTR register |
| 1090 | * - crosscheck that hardware is still in current transfer. |
| 1091 | * In case of switch, we can assume that the DMA is at the beginning of |
| 1092 | * the next transfer. So we approximate the residue in consequence, by |
| 1093 | * pointing on the beginning of next transfer. |
| 1094 | * |
| 1095 | * This race condition doesn't apply for none cyclic mode, as double |
| 1096 | * buffer is not used. In such situation registers are updated by the |
| 1097 | * software. |
M'boumba Cedric Madianga | 2b12c558 | 2016-12-13 14:40:48 +0100 | [diff] [blame] | 1098 | */ |
Arnaud Pouliquen | 2a4885a | 2019-05-02 11:28:42 +0200 | [diff] [blame] | 1099 | |
| 1100 | residue = stm32_dma_get_remaining_bytes(chan); |
| 1101 | |
| 1102 | if (!stm32_dma_is_current_sg(chan)) { |
| 1103 | n_sg++; |
| 1104 | if (n_sg == chan->desc->num_sgs) |
| 1105 | n_sg = 0; |
| 1106 | residue = sg_req->len; |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 1107 | } |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1108 | |
M'boumba Cedric Madianga | 2b12c558 | 2016-12-13 14:40:48 +0100 | [diff] [blame] | 1109 | /* |
Arnaud Pouliquen | 2a4885a | 2019-05-02 11:28:42 +0200 | [diff] [blame] | 1110 | * In cyclic mode, for the last period, residue = remaining bytes |
| 1111 | * from NDTR, |
| 1112 | * else for all other periods in cyclic mode, and in sg mode, |
| 1113 | * residue = remaining bytes from NDTR + remaining |
| 1114 | * periods/sg to be transferred |
M'boumba Cedric Madianga | 2b12c558 | 2016-12-13 14:40:48 +0100 | [diff] [blame] | 1115 | */ |
Arnaud Pouliquen | 2a4885a | 2019-05-02 11:28:42 +0200 | [diff] [blame] | 1116 | if (!chan->desc->cyclic || n_sg != 0) |
| 1117 | for (i = n_sg; i < desc->num_sgs; i++) |
| 1118 | residue += desc->sg_req[i].len; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1119 | |
Pierre Yves MORDRET | a2b6103 | 2018-03-13 17:42:02 +0100 | [diff] [blame] | 1120 | if (!chan->mem_burst) |
| 1121 | return residue; |
| 1122 | |
| 1123 | burst_size = chan->mem_burst * chan->mem_width; |
| 1124 | modulo = residue % burst_size; |
| 1125 | if (modulo) |
| 1126 | residue = residue - modulo + burst_size; |
| 1127 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1128 | return residue; |
| 1129 | } |
| 1130 | |
| 1131 | static enum dma_status stm32_dma_tx_status(struct dma_chan *c, |
| 1132 | dma_cookie_t cookie, |
| 1133 | struct dma_tx_state *state) |
| 1134 | { |
| 1135 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 1136 | struct virt_dma_desc *vdesc; |
| 1137 | enum dma_status status; |
| 1138 | unsigned long flags; |
M'boumba Cedric Madianga | 57b5a32 | 2016-12-13 14:40:46 +0100 | [diff] [blame] | 1139 | u32 residue = 0; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1140 | |
| 1141 | status = dma_cookie_status(c, cookie, state); |
Pierre Yves MORDRET | 249d553 | 2018-03-13 17:42:07 +0100 | [diff] [blame] | 1142 | if (status == DMA_COMPLETE || !state) |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1143 | return status; |
| 1144 | |
| 1145 | spin_lock_irqsave(&chan->vchan.lock, flags); |
| 1146 | vdesc = vchan_find_desc(&chan->vchan, cookie); |
M'boumba Cedric Madianga | 57b5a32 | 2016-12-13 14:40:46 +0100 | [diff] [blame] | 1147 | if (chan->desc && cookie == chan->desc->vdesc.tx.cookie) |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1148 | residue = stm32_dma_desc_residue(chan, chan->desc, |
| 1149 | chan->next_sg); |
M'boumba Cedric Madianga | 57b5a32 | 2016-12-13 14:40:46 +0100 | [diff] [blame] | 1150 | else if (vdesc) |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1151 | residue = stm32_dma_desc_residue(chan, |
| 1152 | to_stm32_dma_desc(vdesc), 0); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1153 | dma_set_residue(state, residue); |
| 1154 | |
| 1155 | spin_unlock_irqrestore(&chan->vchan.lock, flags); |
| 1156 | |
| 1157 | return status; |
| 1158 | } |
| 1159 | |
| 1160 | static int stm32_dma_alloc_chan_resources(struct dma_chan *c) |
| 1161 | { |
| 1162 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 1163 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 1164 | int ret; |
| 1165 | |
| 1166 | chan->config_init = false; |
Pierre-Yves MORDRET | 48bc73b | 2019-01-03 11:17:08 +0100 | [diff] [blame] | 1167 | |
| 1168 | ret = pm_runtime_get_sync(dmadev->ddev.dev); |
| 1169 | if (ret < 0) |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1170 | return ret; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1171 | |
| 1172 | ret = stm32_dma_disable_chan(chan); |
| 1173 | if (ret < 0) |
Pierre-Yves MORDRET | 48bc73b | 2019-01-03 11:17:08 +0100 | [diff] [blame] | 1174 | pm_runtime_put(dmadev->ddev.dev); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1175 | |
| 1176 | return ret; |
| 1177 | } |
| 1178 | |
| 1179 | static void stm32_dma_free_chan_resources(struct dma_chan *c) |
| 1180 | { |
| 1181 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
| 1182 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
| 1183 | unsigned long flags; |
| 1184 | |
| 1185 | dev_dbg(chan2dev(chan), "Freeing channel %d\n", chan->id); |
| 1186 | |
| 1187 | if (chan->busy) { |
| 1188 | spin_lock_irqsave(&chan->vchan.lock, flags); |
| 1189 | stm32_dma_stop(chan); |
| 1190 | chan->desc = NULL; |
| 1191 | spin_unlock_irqrestore(&chan->vchan.lock, flags); |
| 1192 | } |
| 1193 | |
Pierre-Yves MORDRET | 48bc73b | 2019-01-03 11:17:08 +0100 | [diff] [blame] | 1194 | pm_runtime_put(dmadev->ddev.dev); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1195 | |
| 1196 | vchan_free_chan_resources(to_virt_chan(c)); |
| 1197 | } |
| 1198 | |
| 1199 | static void stm32_dma_desc_free(struct virt_dma_desc *vdesc) |
| 1200 | { |
| 1201 | kfree(container_of(vdesc, struct stm32_dma_desc, vdesc)); |
| 1202 | } |
| 1203 | |
Vinod Koul | e97adb4 | 2016-09-02 15:59:10 +0530 | [diff] [blame] | 1204 | static void stm32_dma_set_config(struct stm32_dma_chan *chan, |
Pierre Yves MORDRET | 249d553 | 2018-03-13 17:42:07 +0100 | [diff] [blame] | 1205 | struct stm32_dma_cfg *cfg) |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1206 | { |
| 1207 | stm32_dma_clear_reg(&chan->chan_reg); |
| 1208 | |
| 1209 | chan->chan_reg.dma_scr = cfg->stream_config & STM32_DMA_SCR_CFG_MASK; |
| 1210 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_REQ(cfg->request_line); |
| 1211 | |
| 1212 | /* Enable Interrupts */ |
| 1213 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_TEIE | STM32_DMA_SCR_TCIE; |
| 1214 | |
Pierre Yves MORDRET | 951f44c | 2018-03-13 17:42:01 +0100 | [diff] [blame] | 1215 | chan->threshold = STM32_DMA_THRESHOLD_FTR_GET(cfg->features); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | static struct dma_chan *stm32_dma_of_xlate(struct of_phandle_args *dma_spec, |
| 1219 | struct of_dma *ofdma) |
| 1220 | { |
| 1221 | struct stm32_dma_device *dmadev = ofdma->of_dma_data; |
M'boumba Cedric Madianga | 5df4eb4 | 2017-01-05 09:09:40 +0100 | [diff] [blame] | 1222 | struct device *dev = dmadev->ddev.dev; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1223 | struct stm32_dma_cfg cfg; |
| 1224 | struct stm32_dma_chan *chan; |
| 1225 | struct dma_chan *c; |
| 1226 | |
M'boumba Cedric Madianga | 5df4eb4 | 2017-01-05 09:09:40 +0100 | [diff] [blame] | 1227 | if (dma_spec->args_count < 4) { |
| 1228 | dev_err(dev, "Bad number of cells\n"); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1229 | return NULL; |
M'boumba Cedric Madianga | 5df4eb4 | 2017-01-05 09:09:40 +0100 | [diff] [blame] | 1230 | } |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1231 | |
| 1232 | cfg.channel_id = dma_spec->args[0]; |
| 1233 | cfg.request_line = dma_spec->args[1]; |
| 1234 | cfg.stream_config = dma_spec->args[2]; |
Pierre Yves MORDRET | 951f44c | 2018-03-13 17:42:01 +0100 | [diff] [blame] | 1235 | cfg.features = dma_spec->args[3]; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1236 | |
Pierre Yves MORDRET | 249d553 | 2018-03-13 17:42:07 +0100 | [diff] [blame] | 1237 | if (cfg.channel_id >= STM32_DMA_MAX_CHANNELS || |
| 1238 | cfg.request_line >= STM32_DMA_MAX_REQUEST_ID) { |
M'boumba Cedric Madianga | 5df4eb4 | 2017-01-05 09:09:40 +0100 | [diff] [blame] | 1239 | dev_err(dev, "Bad channel and/or request id\n"); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1240 | return NULL; |
M'boumba Cedric Madianga | 5df4eb4 | 2017-01-05 09:09:40 +0100 | [diff] [blame] | 1241 | } |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1242 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1243 | chan = &dmadev->chan[cfg.channel_id]; |
| 1244 | |
| 1245 | c = dma_get_slave_channel(&chan->vchan.chan); |
M'boumba Cedric Madianga | 5df4eb4 | 2017-01-05 09:09:40 +0100 | [diff] [blame] | 1246 | if (!c) { |
Colin Ian King | 041cf7e | 2017-02-21 18:30:45 +0000 | [diff] [blame] | 1247 | dev_err(dev, "No more channels available\n"); |
M'boumba Cedric Madianga | 5df4eb4 | 2017-01-05 09:09:40 +0100 | [diff] [blame] | 1248 | return NULL; |
| 1249 | } |
| 1250 | |
| 1251 | stm32_dma_set_config(chan, &cfg); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1252 | |
| 1253 | return c; |
| 1254 | } |
| 1255 | |
| 1256 | static const struct of_device_id stm32_dma_of_match[] = { |
| 1257 | { .compatible = "st,stm32-dma", }, |
| 1258 | { /* sentinel */ }, |
| 1259 | }; |
| 1260 | MODULE_DEVICE_TABLE(of, stm32_dma_of_match); |
| 1261 | |
| 1262 | static int stm32_dma_probe(struct platform_device *pdev) |
| 1263 | { |
| 1264 | struct stm32_dma_chan *chan; |
| 1265 | struct stm32_dma_device *dmadev; |
| 1266 | struct dma_device *dd; |
| 1267 | const struct of_device_id *match; |
| 1268 | struct resource *res; |
Etienne Carriere | 8cf1e0f | 2020-01-29 16:36:22 +0100 | [diff] [blame] | 1269 | struct reset_control *rst; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1270 | int i, ret; |
| 1271 | |
| 1272 | match = of_match_device(stm32_dma_of_match, &pdev->dev); |
| 1273 | if (!match) { |
| 1274 | dev_err(&pdev->dev, "Error: No device match found\n"); |
| 1275 | return -ENODEV; |
| 1276 | } |
| 1277 | |
| 1278 | dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev), GFP_KERNEL); |
| 1279 | if (!dmadev) |
| 1280 | return -ENOMEM; |
| 1281 | |
| 1282 | dd = &dmadev->ddev; |
| 1283 | |
| 1284 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1285 | dmadev->base = devm_ioremap_resource(&pdev->dev, res); |
| 1286 | if (IS_ERR(dmadev->base)) |
| 1287 | return PTR_ERR(dmadev->base); |
| 1288 | |
| 1289 | dmadev->clk = devm_clk_get(&pdev->dev, NULL); |
| 1290 | if (IS_ERR(dmadev->clk)) { |
Etienne Carriere | 615eee2 | 2020-01-29 16:36:23 +0100 | [diff] [blame] | 1291 | ret = PTR_ERR(dmadev->clk); |
| 1292 | if (ret != -EPROBE_DEFER) |
| 1293 | dev_err(&pdev->dev, "Can't get clock\n"); |
| 1294 | return ret; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1295 | } |
| 1296 | |
Pierre-Yves MORDRET | 48bc73b | 2019-01-03 11:17:08 +0100 | [diff] [blame] | 1297 | ret = clk_prepare_enable(dmadev->clk); |
| 1298 | if (ret < 0) { |
| 1299 | dev_err(&pdev->dev, "clk_prep_enable error: %d\n", ret); |
| 1300 | return ret; |
| 1301 | } |
| 1302 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1303 | dmadev->mem2mem = of_property_read_bool(pdev->dev.of_node, |
| 1304 | "st,mem2mem"); |
| 1305 | |
Etienne Carriere | 8cf1e0f | 2020-01-29 16:36:22 +0100 | [diff] [blame] | 1306 | rst = devm_reset_control_get(&pdev->dev, NULL); |
Etienne Carriere | 615eee2 | 2020-01-29 16:36:23 +0100 | [diff] [blame] | 1307 | if (IS_ERR(rst)) { |
| 1308 | ret = PTR_ERR(rst); |
| 1309 | if (ret == -EPROBE_DEFER) |
| 1310 | goto clk_free; |
| 1311 | } else { |
Etienne Carriere | 8cf1e0f | 2020-01-29 16:36:22 +0100 | [diff] [blame] | 1312 | reset_control_assert(rst); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1313 | udelay(2); |
Etienne Carriere | 8cf1e0f | 2020-01-29 16:36:22 +0100 | [diff] [blame] | 1314 | reset_control_deassert(rst); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1315 | } |
| 1316 | |
Amelie Delaunay | d7a9e42 | 2020-01-29 16:36:25 +0100 | [diff] [blame] | 1317 | dma_set_max_seg_size(&pdev->dev, STM32_DMA_ALIGNED_MAX_DATA_ITEMS); |
| 1318 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1319 | dma_cap_set(DMA_SLAVE, dd->cap_mask); |
| 1320 | dma_cap_set(DMA_PRIVATE, dd->cap_mask); |
| 1321 | dma_cap_set(DMA_CYCLIC, dd->cap_mask); |
| 1322 | dd->device_alloc_chan_resources = stm32_dma_alloc_chan_resources; |
| 1323 | dd->device_free_chan_resources = stm32_dma_free_chan_resources; |
| 1324 | dd->device_tx_status = stm32_dma_tx_status; |
| 1325 | dd->device_issue_pending = stm32_dma_issue_pending; |
| 1326 | dd->device_prep_slave_sg = stm32_dma_prep_slave_sg; |
| 1327 | dd->device_prep_dma_cyclic = stm32_dma_prep_dma_cyclic; |
| 1328 | dd->device_config = stm32_dma_slave_config; |
| 1329 | dd->device_terminate_all = stm32_dma_terminate_all; |
M'boumba Cedric Madianga | dc80867 | 2016-12-13 14:40:50 +0100 | [diff] [blame] | 1330 | dd->device_synchronize = stm32_dma_synchronize; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1331 | dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | |
| 1332 | BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | |
| 1333 | BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); |
| 1334 | dd->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | |
| 1335 | BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | |
| 1336 | BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); |
| 1337 | dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV); |
| 1338 | dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST; |
Amelie Delaunay | 32ce108 | 2020-01-29 16:36:26 +0100 | [diff] [blame] | 1339 | dd->copy_align = DMAENGINE_ALIGN_32_BYTES; |
M'boumba Cedric Madianga | 276b004 | 2016-12-13 14:40:51 +0100 | [diff] [blame] | 1340 | dd->max_burst = STM32_DMA_MAX_BURST; |
Pierre-Yves MORDRET | 22a0bb2 | 2020-01-29 16:36:24 +0100 | [diff] [blame] | 1341 | dd->descriptor_reuse = true; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1342 | dd->dev = &pdev->dev; |
| 1343 | INIT_LIST_HEAD(&dd->channels); |
| 1344 | |
| 1345 | if (dmadev->mem2mem) { |
| 1346 | dma_cap_set(DMA_MEMCPY, dd->cap_mask); |
| 1347 | dd->device_prep_dma_memcpy = stm32_dma_prep_dma_memcpy; |
| 1348 | dd->directions |= BIT(DMA_MEM_TO_MEM); |
| 1349 | } |
| 1350 | |
| 1351 | for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) { |
| 1352 | chan = &dmadev->chan[i]; |
| 1353 | chan->id = i; |
| 1354 | chan->vchan.desc_free = stm32_dma_desc_free; |
| 1355 | vchan_init(&chan->vchan, dd); |
| 1356 | } |
| 1357 | |
| 1358 | ret = dma_async_device_register(dd); |
| 1359 | if (ret) |
Pierre-Yves MORDRET | 48bc73b | 2019-01-03 11:17:08 +0100 | [diff] [blame] | 1360 | goto clk_free; |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1361 | |
| 1362 | for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) { |
| 1363 | chan = &dmadev->chan[i]; |
Vinod Koul | c6504be | 2019-04-26 22:30:27 +0530 | [diff] [blame] | 1364 | ret = platform_get_irq(pdev, i); |
Stephen Boyd | e17be6e | 2019-07-30 11:15:10 -0700 | [diff] [blame] | 1365 | if (ret < 0) |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1366 | goto err_unregister; |
Vinod Koul | c6504be | 2019-04-26 22:30:27 +0530 | [diff] [blame] | 1367 | chan->irq = ret; |
| 1368 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1369 | ret = devm_request_irq(&pdev->dev, chan->irq, |
| 1370 | stm32_dma_chan_irq, 0, |
| 1371 | dev_name(chan2dev(chan)), chan); |
| 1372 | if (ret) { |
| 1373 | dev_err(&pdev->dev, |
| 1374 | "request_irq failed with err %d channel %d\n", |
| 1375 | ret, i); |
| 1376 | goto err_unregister; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | ret = of_dma_controller_register(pdev->dev.of_node, |
| 1381 | stm32_dma_of_xlate, dmadev); |
| 1382 | if (ret < 0) { |
| 1383 | dev_err(&pdev->dev, |
| 1384 | "STM32 DMA DMA OF registration failed %d\n", ret); |
| 1385 | goto err_unregister; |
| 1386 | } |
| 1387 | |
| 1388 | platform_set_drvdata(pdev, dmadev); |
| 1389 | |
Pierre-Yves MORDRET | 48bc73b | 2019-01-03 11:17:08 +0100 | [diff] [blame] | 1390 | pm_runtime_set_active(&pdev->dev); |
| 1391 | pm_runtime_enable(&pdev->dev); |
| 1392 | pm_runtime_get_noresume(&pdev->dev); |
| 1393 | pm_runtime_put(&pdev->dev); |
| 1394 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1395 | dev_info(&pdev->dev, "STM32 DMA driver registered\n"); |
| 1396 | |
| 1397 | return 0; |
| 1398 | |
| 1399 | err_unregister: |
| 1400 | dma_async_device_unregister(dd); |
Pierre-Yves MORDRET | 48bc73b | 2019-01-03 11:17:08 +0100 | [diff] [blame] | 1401 | clk_free: |
| 1402 | clk_disable_unprepare(dmadev->clk); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1403 | |
| 1404 | return ret; |
| 1405 | } |
| 1406 | |
Pierre-Yves MORDRET | 48bc73b | 2019-01-03 11:17:08 +0100 | [diff] [blame] | 1407 | #ifdef CONFIG_PM |
| 1408 | static int stm32_dma_runtime_suspend(struct device *dev) |
| 1409 | { |
| 1410 | struct stm32_dma_device *dmadev = dev_get_drvdata(dev); |
| 1411 | |
| 1412 | clk_disable_unprepare(dmadev->clk); |
| 1413 | |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
| 1417 | static int stm32_dma_runtime_resume(struct device *dev) |
| 1418 | { |
| 1419 | struct stm32_dma_device *dmadev = dev_get_drvdata(dev); |
| 1420 | int ret; |
| 1421 | |
| 1422 | ret = clk_prepare_enable(dmadev->clk); |
| 1423 | if (ret) { |
| 1424 | dev_err(dev, "failed to prepare_enable clock\n"); |
| 1425 | return ret; |
| 1426 | } |
| 1427 | |
| 1428 | return 0; |
| 1429 | } |
| 1430 | #endif |
| 1431 | |
Pierre-Yves MORDRET | 05f8740 | 2020-01-29 16:36:21 +0100 | [diff] [blame] | 1432 | #ifdef CONFIG_PM_SLEEP |
| 1433 | static int stm32_dma_suspend(struct device *dev) |
| 1434 | { |
| 1435 | struct stm32_dma_device *dmadev = dev_get_drvdata(dev); |
| 1436 | int id, ret, scr; |
| 1437 | |
| 1438 | ret = pm_runtime_get_sync(dev); |
| 1439 | if (ret < 0) |
| 1440 | return ret; |
| 1441 | |
| 1442 | for (id = 0; id < STM32_DMA_MAX_CHANNELS; id++) { |
| 1443 | scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id)); |
| 1444 | if (scr & STM32_DMA_SCR_EN) { |
| 1445 | dev_warn(dev, "Suspend is prevented by Chan %i\n", id); |
| 1446 | return -EBUSY; |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | pm_runtime_put_sync(dev); |
| 1451 | |
| 1452 | pm_runtime_force_suspend(dev); |
| 1453 | |
| 1454 | return 0; |
| 1455 | } |
| 1456 | |
| 1457 | static int stm32_dma_resume(struct device *dev) |
| 1458 | { |
| 1459 | return pm_runtime_force_resume(dev); |
| 1460 | } |
| 1461 | #endif |
| 1462 | |
Pierre-Yves MORDRET | 48bc73b | 2019-01-03 11:17:08 +0100 | [diff] [blame] | 1463 | static const struct dev_pm_ops stm32_dma_pm_ops = { |
Pierre-Yves MORDRET | 05f8740 | 2020-01-29 16:36:21 +0100 | [diff] [blame] | 1464 | SET_SYSTEM_SLEEP_PM_OPS(stm32_dma_suspend, stm32_dma_resume) |
Pierre-Yves MORDRET | 48bc73b | 2019-01-03 11:17:08 +0100 | [diff] [blame] | 1465 | SET_RUNTIME_PM_OPS(stm32_dma_runtime_suspend, |
| 1466 | stm32_dma_runtime_resume, NULL) |
| 1467 | }; |
| 1468 | |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1469 | static struct platform_driver stm32_dma_driver = { |
| 1470 | .driver = { |
| 1471 | .name = "stm32-dma", |
| 1472 | .of_match_table = stm32_dma_of_match, |
Pierre-Yves MORDRET | 48bc73b | 2019-01-03 11:17:08 +0100 | [diff] [blame] | 1473 | .pm = &stm32_dma_pm_ops, |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1474 | }, |
Etienne Carriere | 615eee2 | 2020-01-29 16:36:23 +0100 | [diff] [blame] | 1475 | .probe = stm32_dma_probe, |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1476 | }; |
| 1477 | |
| 1478 | static int __init stm32_dma_init(void) |
| 1479 | { |
Etienne Carriere | 615eee2 | 2020-01-29 16:36:23 +0100 | [diff] [blame] | 1480 | return platform_driver_register(&stm32_dma_driver); |
M'boumba Cedric Madianga | d8b4683 | 2015-10-16 15:59:14 +0200 | [diff] [blame] | 1481 | } |
| 1482 | subsys_initcall(stm32_dma_init); |