diff options
author | Vinod Koul <vinod.koul@intel.com> | 2018-04-09 23:25:58 -0400 |
---|---|---|
committer | Vinod Koul <vinod.koul@intel.com> | 2018-04-09 23:25:58 -0400 |
commit | 2ffb850e23a943acfbeda62599397c863cdd854c (patch) | |
tree | 8745a5531ed800d0788d3314ff2a5d0221cf4f8f | |
parent | 3a7b854d625816166c0faa3846810fee23f4b3d3 (diff) | |
parent | 9df3bd5520038225ceb3927021e6ea811c000c5b (diff) |
Merge branch 'topic/stm' into for-linus
-rw-r--r-- | Documentation/devicetree/bindings/dma/stm32-dma.txt | 6 | ||||
-rw-r--r-- | drivers/dma/stm32-dma.c | 287 |
2 files changed, 243 insertions, 50 deletions
diff --git a/Documentation/devicetree/bindings/dma/stm32-dma.txt b/Documentation/devicetree/bindings/dma/stm32-dma.txt index 0b55718bf889..c5f519097204 100644 --- a/Documentation/devicetree/bindings/dma/stm32-dma.txt +++ b/Documentation/devicetree/bindings/dma/stm32-dma.txt | |||
@@ -62,14 +62,14 @@ channel: a phandle to the DMA controller plus the following four integer cells: | |||
62 | 0x1: medium | 62 | 0x1: medium |
63 | 0x2: high | 63 | 0x2: high |
64 | 0x3: very high | 64 | 0x3: very high |
65 | 4. A 32bit mask specifying the DMA FIFO threshold configuration which are device | 65 | 4. A 32bit bitfield value specifying DMA features which are device dependent: |
66 | dependent: | 66 | -bit 0-1: DMA FIFO threshold selection |
67 | -bit 0-1: Fifo threshold | ||
68 | 0x0: 1/4 full FIFO | 67 | 0x0: 1/4 full FIFO |
69 | 0x1: 1/2 full FIFO | 68 | 0x1: 1/2 full FIFO |
70 | 0x2: 3/4 full FIFO | 69 | 0x2: 3/4 full FIFO |
71 | 0x3: full FIFO | 70 | 0x3: full FIFO |
72 | 71 | ||
72 | |||
73 | Example: | 73 | Example: |
74 | 74 | ||
75 | usart1: serial@40011000 { | 75 | usart1: serial@40011000 { |
diff --git a/drivers/dma/stm32-dma.c b/drivers/dma/stm32-dma.c index 786fc8fcc38e..8c5807362a25 100644 --- a/drivers/dma/stm32-dma.c +++ b/drivers/dma/stm32-dma.c | |||
@@ -5,6 +5,7 @@ | |||
5 | * | 5 | * |
6 | * Copyright (C) M'boumba Cedric Madianga 2015 | 6 | * Copyright (C) M'boumba Cedric Madianga 2015 |
7 | * Author: M'boumba Cedric Madianga <cedric.madianga@gmail.com> | 7 | * Author: M'boumba Cedric Madianga <cedric.madianga@gmail.com> |
8 | * Pierre-Yves Mordret <pierre-yves.mordret@st.com> | ||
8 | * | 9 | * |
9 | * License terms: GNU General Public License (GPL), version 2 | 10 | * License terms: GNU General Public License (GPL), version 2 |
10 | */ | 11 | */ |
@@ -33,9 +34,14 @@ | |||
33 | #define STM32_DMA_LIFCR 0x0008 /* DMA Low Int Flag Clear Reg */ | 34 | #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_HIFCR 0x000c /* DMA High Int Flag Clear Reg */ |
35 | #define STM32_DMA_TCI BIT(5) /* Transfer Complete Interrupt */ | 36 | #define STM32_DMA_TCI BIT(5) /* Transfer Complete Interrupt */ |
37 | #define STM32_DMA_HTI BIT(4) /* Half Transfer Interrupt */ | ||
36 | #define STM32_DMA_TEI BIT(3) /* Transfer Error Interrupt */ | 38 | #define STM32_DMA_TEI BIT(3) /* Transfer Error Interrupt */ |
37 | #define STM32_DMA_DMEI BIT(2) /* Direct Mode Error Interrupt */ | 39 | #define STM32_DMA_DMEI BIT(2) /* Direct Mode Error Interrupt */ |
38 | #define STM32_DMA_FEI BIT(0) /* FIFO Error Interrupt */ | 40 | #define STM32_DMA_FEI BIT(0) /* FIFO Error Interrupt */ |
41 | #define STM32_DMA_MASKI (STM32_DMA_TCI \ | ||
42 | | STM32_DMA_TEI \ | ||
43 | | STM32_DMA_DMEI \ | ||
44 | | STM32_DMA_FEI) | ||
39 | 45 | ||
40 | /* DMA Stream x Configuration Register */ | 46 | /* DMA Stream x Configuration Register */ |
41 | #define STM32_DMA_SCR(x) (0x0010 + 0x18 * (x)) /* x = 0..7 */ | 47 | #define STM32_DMA_SCR(x) (0x0010 + 0x18 * (x)) /* x = 0..7 */ |
@@ -60,7 +66,8 @@ | |||
60 | #define STM32_DMA_SCR_PINC BIT(9) /* Peripheral increment mode */ | 66 | #define STM32_DMA_SCR_PINC BIT(9) /* Peripheral increment mode */ |
61 | #define STM32_DMA_SCR_CIRC BIT(8) /* Circular mode */ | 67 | #define STM32_DMA_SCR_CIRC BIT(8) /* Circular mode */ |
62 | #define STM32_DMA_SCR_PFCTRL BIT(5) /* Peripheral Flow Controller */ | 68 | #define STM32_DMA_SCR_PFCTRL BIT(5) /* Peripheral Flow Controller */ |
63 | #define STM32_DMA_SCR_TCIE BIT(4) /* Transfer Cplete Int Enable*/ | 69 | #define STM32_DMA_SCR_TCIE BIT(4) /* Transfer Complete Int Enable |
70 | */ | ||
64 | #define STM32_DMA_SCR_TEIE BIT(2) /* Transfer Error Int Enable */ | 71 | #define STM32_DMA_SCR_TEIE BIT(2) /* Transfer Error Int Enable */ |
65 | #define STM32_DMA_SCR_DMEIE BIT(1) /* Direct Mode Err Int Enable */ | 72 | #define STM32_DMA_SCR_DMEIE BIT(1) /* Direct Mode Err Int Enable */ |
66 | #define STM32_DMA_SCR_EN BIT(0) /* Stream Enable */ | 73 | #define STM32_DMA_SCR_EN BIT(0) /* Stream Enable */ |
@@ -111,11 +118,24 @@ | |||
111 | #define STM32_DMA_FIFO_THRESHOLD_FULL 0x03 | 118 | #define STM32_DMA_FIFO_THRESHOLD_FULL 0x03 |
112 | 119 | ||
113 | #define STM32_DMA_MAX_DATA_ITEMS 0xffff | 120 | #define STM32_DMA_MAX_DATA_ITEMS 0xffff |
121 | /* | ||
122 | * Valid transfer starts from @0 to @0xFFFE leading to unaligned scatter | ||
123 | * gather at boundary. Thus it's safer to round down this value on FIFO | ||
124 | * size (16 Bytes) | ||
125 | */ | ||
126 | #define STM32_DMA_ALIGNED_MAX_DATA_ITEMS \ | ||
127 | ALIGN_DOWN(STM32_DMA_MAX_DATA_ITEMS, 16) | ||
114 | #define STM32_DMA_MAX_CHANNELS 0x08 | 128 | #define STM32_DMA_MAX_CHANNELS 0x08 |
115 | #define STM32_DMA_MAX_REQUEST_ID 0x08 | 129 | #define STM32_DMA_MAX_REQUEST_ID 0x08 |
116 | #define STM32_DMA_MAX_DATA_PARAM 0x03 | 130 | #define STM32_DMA_MAX_DATA_PARAM 0x03 |
131 | #define STM32_DMA_FIFO_SIZE 16 /* FIFO is 16 bytes */ | ||
132 | #define STM32_DMA_MIN_BURST 4 | ||
117 | #define STM32_DMA_MAX_BURST 16 | 133 | #define STM32_DMA_MAX_BURST 16 |
118 | 134 | ||
135 | /* DMA Features */ | ||
136 | #define STM32_DMA_THRESHOLD_FTR_MASK GENMASK(1, 0) | ||
137 | #define STM32_DMA_THRESHOLD_FTR_GET(n) ((n) & STM32_DMA_THRESHOLD_FTR_MASK) | ||
138 | |||
119 | enum stm32_dma_width { | 139 | enum stm32_dma_width { |
120 | STM32_DMA_BYTE, | 140 | STM32_DMA_BYTE, |
121 | STM32_DMA_HALF_WORD, | 141 | STM32_DMA_HALF_WORD, |
@@ -129,11 +149,18 @@ enum stm32_dma_burst_size { | |||
129 | STM32_DMA_BURST_INCR16, | 149 | STM32_DMA_BURST_INCR16, |
130 | }; | 150 | }; |
131 | 151 | ||
152 | /** | ||
153 | * struct stm32_dma_cfg - STM32 DMA custom configuration | ||
154 | * @channel_id: channel ID | ||
155 | * @request_line: DMA request | ||
156 | * @stream_config: 32bit mask specifying the DMA channel configuration | ||
157 | * @features: 32bit mask specifying the DMA Feature list | ||
158 | */ | ||
132 | struct stm32_dma_cfg { | 159 | struct stm32_dma_cfg { |
133 | u32 channel_id; | 160 | u32 channel_id; |
134 | u32 request_line; | 161 | u32 request_line; |
135 | u32 stream_config; | 162 | u32 stream_config; |
136 | u32 threshold; | 163 | u32 features; |
137 | }; | 164 | }; |
138 | 165 | ||
139 | struct stm32_dma_chan_reg { | 166 | struct stm32_dma_chan_reg { |
@@ -171,6 +198,9 @@ struct stm32_dma_chan { | |||
171 | u32 next_sg; | 198 | u32 next_sg; |
172 | struct dma_slave_config dma_sconfig; | 199 | struct dma_slave_config dma_sconfig; |
173 | struct stm32_dma_chan_reg chan_reg; | 200 | struct stm32_dma_chan_reg chan_reg; |
201 | u32 threshold; | ||
202 | u32 mem_burst; | ||
203 | u32 mem_width; | ||
174 | }; | 204 | }; |
175 | 205 | ||
176 | struct stm32_dma_device { | 206 | struct stm32_dma_device { |
@@ -235,6 +265,85 @@ static int stm32_dma_get_width(struct stm32_dma_chan *chan, | |||
235 | } | 265 | } |
236 | } | 266 | } |
237 | 267 | ||
268 | static enum dma_slave_buswidth stm32_dma_get_max_width(u32 buf_len, | ||
269 | u32 threshold) | ||
270 | { | ||
271 | enum dma_slave_buswidth max_width; | ||
272 | |||
273 | if (threshold == STM32_DMA_FIFO_THRESHOLD_FULL) | ||
274 | max_width = DMA_SLAVE_BUSWIDTH_4_BYTES; | ||
275 | else | ||
276 | max_width = DMA_SLAVE_BUSWIDTH_2_BYTES; | ||
277 | |||
278 | while ((buf_len < max_width || buf_len % max_width) && | ||
279 | max_width > DMA_SLAVE_BUSWIDTH_1_BYTE) | ||
280 | max_width = max_width >> 1; | ||
281 | |||
282 | return max_width; | ||
283 | } | ||
284 | |||
285 | static bool stm32_dma_fifo_threshold_is_allowed(u32 burst, u32 threshold, | ||
286 | enum dma_slave_buswidth width) | ||
287 | { | ||
288 | u32 remaining; | ||
289 | |||
290 | if (width != DMA_SLAVE_BUSWIDTH_UNDEFINED) { | ||
291 | if (burst != 0) { | ||
292 | /* | ||
293 | * If number of beats fit in several whole bursts | ||
294 | * this configuration is allowed. | ||
295 | */ | ||
296 | remaining = ((STM32_DMA_FIFO_SIZE / width) * | ||
297 | (threshold + 1) / 4) % burst; | ||
298 | |||
299 | if (remaining == 0) | ||
300 | return true; | ||
301 | } else { | ||
302 | return true; | ||
303 | } | ||
304 | } | ||
305 | |||
306 | return false; | ||
307 | } | ||
308 | |||
309 | static bool stm32_dma_is_burst_possible(u32 buf_len, u32 threshold) | ||
310 | { | ||
311 | switch (threshold) { | ||
312 | case STM32_DMA_FIFO_THRESHOLD_FULL: | ||
313 | if (buf_len >= STM32_DMA_MAX_BURST) | ||
314 | return true; | ||
315 | else | ||
316 | return false; | ||
317 | case STM32_DMA_FIFO_THRESHOLD_HALFFULL: | ||
318 | if (buf_len >= STM32_DMA_MAX_BURST / 2) | ||
319 | return true; | ||
320 | else | ||
321 | return false; | ||
322 | default: | ||
323 | return false; | ||
324 | } | ||
325 | } | ||
326 | |||
327 | static u32 stm32_dma_get_best_burst(u32 buf_len, u32 max_burst, u32 threshold, | ||
328 | enum dma_slave_buswidth width) | ||
329 | { | ||
330 | u32 best_burst = max_burst; | ||
331 | |||
332 | if (best_burst == 1 || !stm32_dma_is_burst_possible(buf_len, threshold)) | ||
333 | return 0; | ||
334 | |||
335 | while ((buf_len < best_burst * width && best_burst > 1) || | ||
336 | !stm32_dma_fifo_threshold_is_allowed(best_burst, threshold, | ||
337 | width)) { | ||
338 | if (best_burst > STM32_DMA_MIN_BURST) | ||
339 | best_burst = best_burst >> 1; | ||
340 | else | ||
341 | best_burst = 0; | ||
342 | } | ||
343 | |||
344 | return best_burst; | ||
345 | } | ||
346 | |||
238 | static int stm32_dma_get_burst(struct stm32_dma_chan *chan, u32 maxburst) | 347 | static int stm32_dma_get_burst(struct stm32_dma_chan *chan, u32 maxburst) |
239 | { | 348 | { |
240 | switch (maxburst) { | 349 | switch (maxburst) { |
@@ -254,12 +363,12 @@ static int stm32_dma_get_burst(struct stm32_dma_chan *chan, u32 maxburst) | |||
254 | } | 363 | } |
255 | 364 | ||
256 | static void stm32_dma_set_fifo_config(struct stm32_dma_chan *chan, | 365 | static void stm32_dma_set_fifo_config(struct stm32_dma_chan *chan, |
257 | u32 src_maxburst, u32 dst_maxburst) | 366 | u32 src_burst, u32 dst_burst) |
258 | { | 367 | { |
259 | chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_MASK; | 368 | chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_MASK; |
260 | chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_DMEIE; | 369 | chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_DMEIE; |
261 | 370 | ||
262 | if ((!src_maxburst) && (!dst_maxburst)) { | 371 | if (!src_burst && !dst_burst) { |
263 | /* Using direct mode */ | 372 | /* Using direct mode */ |
264 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_DMEIE; | 373 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_DMEIE; |
265 | } else { | 374 | } else { |
@@ -300,7 +409,7 @@ static u32 stm32_dma_irq_status(struct stm32_dma_chan *chan) | |||
300 | 409 | ||
301 | flags = dma_isr >> (((chan->id & 2) << 3) | ((chan->id & 1) * 6)); | 410 | flags = dma_isr >> (((chan->id & 2) << 3) | ((chan->id & 1) * 6)); |
302 | 411 | ||
303 | return flags; | 412 | return flags & STM32_DMA_MASKI; |
304 | } | 413 | } |
305 | 414 | ||
306 | static void stm32_dma_irq_clear(struct stm32_dma_chan *chan, u32 flags) | 415 | static void stm32_dma_irq_clear(struct stm32_dma_chan *chan, u32 flags) |
@@ -315,6 +424,7 @@ static void stm32_dma_irq_clear(struct stm32_dma_chan *chan, u32 flags) | |||
315 | * If (ch % 4) is 2 or 3, left shift the mask by 16 bits. | 424 | * 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. | 425 | * If (ch % 4) is 1 or 3, additionally left shift the mask by 6 bits. |
317 | */ | 426 | */ |
427 | flags &= STM32_DMA_MASKI; | ||
318 | dma_ifcr = flags << (((chan->id & 2) << 3) | ((chan->id & 1) * 6)); | 428 | dma_ifcr = flags << (((chan->id & 2) << 3) | ((chan->id & 1) * 6)); |
319 | 429 | ||
320 | if (chan->id & 4) | 430 | if (chan->id & 4) |
@@ -429,6 +539,8 @@ static void stm32_dma_dump_reg(struct stm32_dma_chan *chan) | |||
429 | dev_dbg(chan2dev(chan), "SFCR: 0x%08x\n", sfcr); | 539 | dev_dbg(chan2dev(chan), "SFCR: 0x%08x\n", sfcr); |
430 | } | 540 | } |
431 | 541 | ||
542 | static void stm32_dma_configure_next_sg(struct stm32_dma_chan *chan); | ||
543 | |||
432 | static void stm32_dma_start_transfer(struct stm32_dma_chan *chan) | 544 | static void stm32_dma_start_transfer(struct stm32_dma_chan *chan) |
433 | { | 545 | { |
434 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); | 546 | struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); |
@@ -471,6 +583,9 @@ static void stm32_dma_start_transfer(struct stm32_dma_chan *chan) | |||
471 | if (status) | 583 | if (status) |
472 | stm32_dma_irq_clear(chan, status); | 584 | stm32_dma_irq_clear(chan, status); |
473 | 585 | ||
586 | if (chan->desc->cyclic) | ||
587 | stm32_dma_configure_next_sg(chan); | ||
588 | |||
474 | stm32_dma_dump_reg(chan); | 589 | stm32_dma_dump_reg(chan); |
475 | 590 | ||
476 | /* Start DMA */ | 591 | /* Start DMA */ |
@@ -541,13 +656,29 @@ static irqreturn_t stm32_dma_chan_irq(int irq, void *devid) | |||
541 | status = stm32_dma_irq_status(chan); | 656 | status = stm32_dma_irq_status(chan); |
542 | scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id)); | 657 | scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id)); |
543 | 658 | ||
544 | if ((status & STM32_DMA_TCI) && (scr & STM32_DMA_SCR_TCIE)) { | 659 | if (status & STM32_DMA_TCI) { |
545 | stm32_dma_irq_clear(chan, STM32_DMA_TCI); | 660 | stm32_dma_irq_clear(chan, STM32_DMA_TCI); |
546 | stm32_dma_handle_chan_done(chan); | 661 | if (scr & STM32_DMA_SCR_TCIE) |
547 | 662 | stm32_dma_handle_chan_done(chan); | |
548 | } else { | 663 | status &= ~STM32_DMA_TCI; |
664 | } | ||
665 | if (status & STM32_DMA_HTI) { | ||
666 | stm32_dma_irq_clear(chan, STM32_DMA_HTI); | ||
667 | status &= ~STM32_DMA_HTI; | ||
668 | } | ||
669 | if (status & STM32_DMA_FEI) { | ||
670 | stm32_dma_irq_clear(chan, STM32_DMA_FEI); | ||
671 | status &= ~STM32_DMA_FEI; | ||
672 | if (!(scr & STM32_DMA_SCR_EN)) | ||
673 | dev_err(chan2dev(chan), "FIFO Error\n"); | ||
674 | else | ||
675 | dev_dbg(chan2dev(chan), "FIFO over/underrun\n"); | ||
676 | } | ||
677 | if (status) { | ||
549 | stm32_dma_irq_clear(chan, status); | 678 | stm32_dma_irq_clear(chan, status); |
550 | dev_err(chan2dev(chan), "DMA error: status=0x%08x\n", status); | 679 | dev_err(chan2dev(chan), "DMA error: status=0x%08x\n", status); |
680 | if (!(scr & STM32_DMA_SCR_EN)) | ||
681 | dev_err(chan2dev(chan), "chan disabled by HW\n"); | ||
551 | } | 682 | } |
552 | 683 | ||
553 | spin_unlock(&chan->vchan.lock); | 684 | spin_unlock(&chan->vchan.lock); |
@@ -564,45 +695,59 @@ static void stm32_dma_issue_pending(struct dma_chan *c) | |||
564 | if (vchan_issue_pending(&chan->vchan) && !chan->desc && !chan->busy) { | 695 | if (vchan_issue_pending(&chan->vchan) && !chan->desc && !chan->busy) { |
565 | dev_dbg(chan2dev(chan), "vchan %p: issued\n", &chan->vchan); | 696 | dev_dbg(chan2dev(chan), "vchan %p: issued\n", &chan->vchan); |
566 | stm32_dma_start_transfer(chan); | 697 | stm32_dma_start_transfer(chan); |
567 | if (chan->desc->cyclic) | 698 | |
568 | stm32_dma_configure_next_sg(chan); | ||
569 | } | 699 | } |
570 | spin_unlock_irqrestore(&chan->vchan.lock, flags); | 700 | spin_unlock_irqrestore(&chan->vchan.lock, flags); |
571 | } | 701 | } |
572 | 702 | ||
573 | static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan, | 703 | static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan, |
574 | enum dma_transfer_direction direction, | 704 | enum dma_transfer_direction direction, |
575 | enum dma_slave_buswidth *buswidth) | 705 | enum dma_slave_buswidth *buswidth, |
706 | u32 buf_len) | ||
576 | { | 707 | { |
577 | enum dma_slave_buswidth src_addr_width, dst_addr_width; | 708 | enum dma_slave_buswidth src_addr_width, dst_addr_width; |
578 | int src_bus_width, dst_bus_width; | 709 | int src_bus_width, dst_bus_width; |
579 | int src_burst_size, dst_burst_size; | 710 | int src_burst_size, dst_burst_size; |
580 | u32 src_maxburst, dst_maxburst; | 711 | u32 src_maxburst, dst_maxburst, src_best_burst, dst_best_burst; |
581 | u32 dma_scr = 0; | 712 | u32 dma_scr, threshold; |
582 | 713 | ||
583 | src_addr_width = chan->dma_sconfig.src_addr_width; | 714 | src_addr_width = chan->dma_sconfig.src_addr_width; |
584 | dst_addr_width = chan->dma_sconfig.dst_addr_width; | 715 | dst_addr_width = chan->dma_sconfig.dst_addr_width; |
585 | src_maxburst = chan->dma_sconfig.src_maxburst; | 716 | src_maxburst = chan->dma_sconfig.src_maxburst; |
586 | dst_maxburst = chan->dma_sconfig.dst_maxburst; | 717 | dst_maxburst = chan->dma_sconfig.dst_maxburst; |
718 | threshold = chan->threshold; | ||
587 | 719 | ||
588 | switch (direction) { | 720 | switch (direction) { |
589 | case DMA_MEM_TO_DEV: | 721 | case DMA_MEM_TO_DEV: |
722 | /* Set device data size */ | ||
590 | dst_bus_width = stm32_dma_get_width(chan, dst_addr_width); | 723 | dst_bus_width = stm32_dma_get_width(chan, dst_addr_width); |
591 | if (dst_bus_width < 0) | 724 | if (dst_bus_width < 0) |
592 | return dst_bus_width; | 725 | return dst_bus_width; |
593 | 726 | ||
594 | dst_burst_size = stm32_dma_get_burst(chan, dst_maxburst); | 727 | /* Set device burst size */ |
728 | dst_best_burst = stm32_dma_get_best_burst(buf_len, | ||
729 | dst_maxburst, | ||
730 | threshold, | ||
731 | dst_addr_width); | ||
732 | |||
733 | dst_burst_size = stm32_dma_get_burst(chan, dst_best_burst); | ||
595 | if (dst_burst_size < 0) | 734 | if (dst_burst_size < 0) |
596 | return dst_burst_size; | 735 | return dst_burst_size; |
597 | 736 | ||
598 | if (!src_addr_width) | 737 | /* Set memory data size */ |
599 | src_addr_width = dst_addr_width; | 738 | src_addr_width = stm32_dma_get_max_width(buf_len, threshold); |
600 | 739 | chan->mem_width = src_addr_width; | |
601 | src_bus_width = stm32_dma_get_width(chan, src_addr_width); | 740 | src_bus_width = stm32_dma_get_width(chan, src_addr_width); |
602 | if (src_bus_width < 0) | 741 | if (src_bus_width < 0) |
603 | return src_bus_width; | 742 | return src_bus_width; |
604 | 743 | ||
605 | src_burst_size = stm32_dma_get_burst(chan, src_maxburst); | 744 | /* Set memory burst size */ |
745 | src_maxburst = STM32_DMA_MAX_BURST; | ||
746 | src_best_burst = stm32_dma_get_best_burst(buf_len, | ||
747 | src_maxburst, | ||
748 | threshold, | ||
749 | src_addr_width); | ||
750 | src_burst_size = stm32_dma_get_burst(chan, src_best_burst); | ||
606 | if (src_burst_size < 0) | 751 | if (src_burst_size < 0) |
607 | return src_burst_size; | 752 | return src_burst_size; |
608 | 753 | ||
@@ -612,27 +757,46 @@ static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan, | |||
612 | STM32_DMA_SCR_PBURST(dst_burst_size) | | 757 | STM32_DMA_SCR_PBURST(dst_burst_size) | |
613 | STM32_DMA_SCR_MBURST(src_burst_size); | 758 | STM32_DMA_SCR_MBURST(src_burst_size); |
614 | 759 | ||
760 | /* Set FIFO threshold */ | ||
761 | chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_FTH_MASK; | ||
762 | chan->chan_reg.dma_sfcr |= STM32_DMA_SFCR_FTH(threshold); | ||
763 | |||
764 | /* Set peripheral address */ | ||
615 | chan->chan_reg.dma_spar = chan->dma_sconfig.dst_addr; | 765 | chan->chan_reg.dma_spar = chan->dma_sconfig.dst_addr; |
616 | *buswidth = dst_addr_width; | 766 | *buswidth = dst_addr_width; |
617 | break; | 767 | break; |
618 | 768 | ||
619 | case DMA_DEV_TO_MEM: | 769 | case DMA_DEV_TO_MEM: |
770 | /* Set device data size */ | ||
620 | src_bus_width = stm32_dma_get_width(chan, src_addr_width); | 771 | src_bus_width = stm32_dma_get_width(chan, src_addr_width); |
621 | if (src_bus_width < 0) | 772 | if (src_bus_width < 0) |
622 | return src_bus_width; | 773 | return src_bus_width; |
623 | 774 | ||
624 | src_burst_size = stm32_dma_get_burst(chan, src_maxburst); | 775 | /* Set device burst size */ |
776 | src_best_burst = stm32_dma_get_best_burst(buf_len, | ||
777 | src_maxburst, | ||
778 | threshold, | ||
779 | src_addr_width); | ||
780 | chan->mem_burst = src_best_burst; | ||
781 | src_burst_size = stm32_dma_get_burst(chan, src_best_burst); | ||
625 | if (src_burst_size < 0) | 782 | if (src_burst_size < 0) |
626 | return src_burst_size; | 783 | return src_burst_size; |
627 | 784 | ||
628 | if (!dst_addr_width) | 785 | /* Set memory data size */ |
629 | dst_addr_width = src_addr_width; | 786 | dst_addr_width = stm32_dma_get_max_width(buf_len, threshold); |
630 | 787 | chan->mem_width = dst_addr_width; | |
631 | dst_bus_width = stm32_dma_get_width(chan, dst_addr_width); | 788 | dst_bus_width = stm32_dma_get_width(chan, dst_addr_width); |
632 | if (dst_bus_width < 0) | 789 | if (dst_bus_width < 0) |
633 | return dst_bus_width; | 790 | return dst_bus_width; |
634 | 791 | ||
635 | dst_burst_size = stm32_dma_get_burst(chan, dst_maxburst); | 792 | /* Set memory burst size */ |
793 | dst_maxburst = STM32_DMA_MAX_BURST; | ||
794 | dst_best_burst = stm32_dma_get_best_burst(buf_len, | ||
795 | dst_maxburst, | ||
796 | threshold, | ||
797 | dst_addr_width); | ||
798 | chan->mem_burst = dst_best_burst; | ||
799 | dst_burst_size = stm32_dma_get_burst(chan, dst_best_burst); | ||
636 | if (dst_burst_size < 0) | 800 | if (dst_burst_size < 0) |
637 | return dst_burst_size; | 801 | return dst_burst_size; |
638 | 802 | ||
@@ -642,6 +806,11 @@ static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan, | |||
642 | STM32_DMA_SCR_PBURST(src_burst_size) | | 806 | STM32_DMA_SCR_PBURST(src_burst_size) | |
643 | STM32_DMA_SCR_MBURST(dst_burst_size); | 807 | STM32_DMA_SCR_MBURST(dst_burst_size); |
644 | 808 | ||
809 | /* Set FIFO threshold */ | ||
810 | chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_FTH_MASK; | ||
811 | chan->chan_reg.dma_sfcr |= STM32_DMA_SFCR_FTH(threshold); | ||
812 | |||
813 | /* Set peripheral address */ | ||
645 | chan->chan_reg.dma_spar = chan->dma_sconfig.src_addr; | 814 | chan->chan_reg.dma_spar = chan->dma_sconfig.src_addr; |
646 | *buswidth = chan->dma_sconfig.src_addr_width; | 815 | *buswidth = chan->dma_sconfig.src_addr_width; |
647 | break; | 816 | break; |
@@ -651,8 +820,9 @@ static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan, | |||
651 | return -EINVAL; | 820 | return -EINVAL; |
652 | } | 821 | } |
653 | 822 | ||
654 | stm32_dma_set_fifo_config(chan, src_maxburst, dst_maxburst); | 823 | stm32_dma_set_fifo_config(chan, src_best_burst, dst_best_burst); |
655 | 824 | ||
825 | /* Set DMA control register */ | ||
656 | chan->chan_reg.dma_scr &= ~(STM32_DMA_SCR_DIR_MASK | | 826 | chan->chan_reg.dma_scr &= ~(STM32_DMA_SCR_DIR_MASK | |
657 | STM32_DMA_SCR_PSIZE_MASK | STM32_DMA_SCR_MSIZE_MASK | | 827 | STM32_DMA_SCR_PSIZE_MASK | STM32_DMA_SCR_MSIZE_MASK | |
658 | STM32_DMA_SCR_PBURST_MASK | STM32_DMA_SCR_MBURST_MASK); | 828 | STM32_DMA_SCR_PBURST_MASK | STM32_DMA_SCR_MBURST_MASK); |
@@ -692,10 +862,6 @@ static struct dma_async_tx_descriptor *stm32_dma_prep_slave_sg( | |||
692 | if (!desc) | 862 | if (!desc) |
693 | return NULL; | 863 | return NULL; |
694 | 864 | ||
695 | ret = stm32_dma_set_xfer_param(chan, direction, &buswidth); | ||
696 | if (ret < 0) | ||
697 | goto err; | ||
698 | |||
699 | /* Set peripheral flow controller */ | 865 | /* Set peripheral flow controller */ |
700 | if (chan->dma_sconfig.device_fc) | 866 | if (chan->dma_sconfig.device_fc) |
701 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_PFCTRL; | 867 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_PFCTRL; |
@@ -703,10 +869,15 @@ static struct dma_async_tx_descriptor *stm32_dma_prep_slave_sg( | |||
703 | chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL; | 869 | chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL; |
704 | 870 | ||
705 | for_each_sg(sgl, sg, sg_len, i) { | 871 | for_each_sg(sgl, sg, sg_len, i) { |
872 | ret = stm32_dma_set_xfer_param(chan, direction, &buswidth, | ||
873 | sg_dma_len(sg)); | ||
874 | if (ret < 0) | ||
875 | goto err; | ||
876 | |||
706 | desc->sg_req[i].len = sg_dma_len(sg); | 877 | desc->sg_req[i].len = sg_dma_len(sg); |
707 | 878 | ||
708 | nb_data_items = desc->sg_req[i].len / buswidth; | 879 | nb_data_items = desc->sg_req[i].len / buswidth; |
709 | if (nb_data_items > STM32_DMA_MAX_DATA_ITEMS) { | 880 | if (nb_data_items > STM32_DMA_ALIGNED_MAX_DATA_ITEMS) { |
710 | dev_err(chan2dev(chan), "nb items not supported\n"); | 881 | dev_err(chan2dev(chan), "nb items not supported\n"); |
711 | goto err; | 882 | goto err; |
712 | } | 883 | } |
@@ -767,12 +938,12 @@ static struct dma_async_tx_descriptor *stm32_dma_prep_dma_cyclic( | |||
767 | return NULL; | 938 | return NULL; |
768 | } | 939 | } |
769 | 940 | ||
770 | ret = stm32_dma_set_xfer_param(chan, direction, &buswidth); | 941 | ret = stm32_dma_set_xfer_param(chan, direction, &buswidth, period_len); |
771 | if (ret < 0) | 942 | if (ret < 0) |
772 | return NULL; | 943 | return NULL; |
773 | 944 | ||
774 | nb_data_items = period_len / buswidth; | 945 | nb_data_items = period_len / buswidth; |
775 | if (nb_data_items > STM32_DMA_MAX_DATA_ITEMS) { | 946 | if (nb_data_items > STM32_DMA_ALIGNED_MAX_DATA_ITEMS) { |
776 | dev_err(chan2dev(chan), "number of items not supported\n"); | 947 | dev_err(chan2dev(chan), "number of items not supported\n"); |
777 | return NULL; | 948 | return NULL; |
778 | } | 949 | } |
@@ -816,35 +987,45 @@ static struct dma_async_tx_descriptor *stm32_dma_prep_dma_memcpy( | |||
816 | dma_addr_t src, size_t len, unsigned long flags) | 987 | dma_addr_t src, size_t len, unsigned long flags) |
817 | { | 988 | { |
818 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); | 989 | struct stm32_dma_chan *chan = to_stm32_dma_chan(c); |
819 | u32 num_sgs; | 990 | enum dma_slave_buswidth max_width; |
820 | struct stm32_dma_desc *desc; | 991 | struct stm32_dma_desc *desc; |
821 | size_t xfer_count, offset; | 992 | size_t xfer_count, offset; |
993 | u32 num_sgs, best_burst, dma_burst, threshold; | ||
822 | int i; | 994 | int i; |
823 | 995 | ||
824 | num_sgs = DIV_ROUND_UP(len, STM32_DMA_MAX_DATA_ITEMS); | 996 | num_sgs = DIV_ROUND_UP(len, STM32_DMA_ALIGNED_MAX_DATA_ITEMS); |
825 | desc = stm32_dma_alloc_desc(num_sgs); | 997 | desc = stm32_dma_alloc_desc(num_sgs); |
826 | if (!desc) | 998 | if (!desc) |
827 | return NULL; | 999 | return NULL; |
828 | 1000 | ||
1001 | threshold = chan->threshold; | ||
1002 | |||
829 | for (offset = 0, i = 0; offset < len; offset += xfer_count, i++) { | 1003 | for (offset = 0, i = 0; offset < len; offset += xfer_count, i++) { |
830 | xfer_count = min_t(size_t, len - offset, | 1004 | xfer_count = min_t(size_t, len - offset, |
831 | STM32_DMA_MAX_DATA_ITEMS); | 1005 | STM32_DMA_ALIGNED_MAX_DATA_ITEMS); |
832 | 1006 | ||
833 | desc->sg_req[i].len = xfer_count; | 1007 | /* Compute best burst size */ |
1008 | max_width = DMA_SLAVE_BUSWIDTH_1_BYTE; | ||
1009 | best_burst = stm32_dma_get_best_burst(len, STM32_DMA_MAX_BURST, | ||
1010 | threshold, max_width); | ||
1011 | dma_burst = stm32_dma_get_burst(chan, best_burst); | ||
834 | 1012 | ||
835 | stm32_dma_clear_reg(&desc->sg_req[i].chan_reg); | 1013 | stm32_dma_clear_reg(&desc->sg_req[i].chan_reg); |
836 | desc->sg_req[i].chan_reg.dma_scr = | 1014 | desc->sg_req[i].chan_reg.dma_scr = |
837 | STM32_DMA_SCR_DIR(STM32_DMA_MEM_TO_MEM) | | 1015 | STM32_DMA_SCR_DIR(STM32_DMA_MEM_TO_MEM) | |
1016 | STM32_DMA_SCR_PBURST(dma_burst) | | ||
1017 | STM32_DMA_SCR_MBURST(dma_burst) | | ||
838 | STM32_DMA_SCR_MINC | | 1018 | STM32_DMA_SCR_MINC | |
839 | STM32_DMA_SCR_PINC | | 1019 | STM32_DMA_SCR_PINC | |
840 | STM32_DMA_SCR_TCIE | | 1020 | STM32_DMA_SCR_TCIE | |
841 | STM32_DMA_SCR_TEIE; | 1021 | STM32_DMA_SCR_TEIE; |
842 | desc->sg_req[i].chan_reg.dma_sfcr = STM32_DMA_SFCR_DMDIS | | 1022 | desc->sg_req[i].chan_reg.dma_sfcr |= STM32_DMA_SFCR_MASK; |
843 | STM32_DMA_SFCR_FTH(STM32_DMA_FIFO_THRESHOLD_FULL) | | 1023 | desc->sg_req[i].chan_reg.dma_sfcr |= |
844 | STM32_DMA_SFCR_FEIE; | 1024 | STM32_DMA_SFCR_FTH(threshold); |
845 | desc->sg_req[i].chan_reg.dma_spar = src + offset; | 1025 | desc->sg_req[i].chan_reg.dma_spar = src + offset; |
846 | desc->sg_req[i].chan_reg.dma_sm0ar = dest + offset; | 1026 | desc->sg_req[i].chan_reg.dma_sm0ar = dest + offset; |
847 | desc->sg_req[i].chan_reg.dma_sndtr = xfer_count; | 1027 | desc->sg_req[i].chan_reg.dma_sndtr = xfer_count; |
1028 | desc->sg_req[i].len = xfer_count; | ||
848 | } | 1029 | } |
849 | 1030 | ||
850 | desc->num_sgs = num_sgs; | 1031 | desc->num_sgs = num_sgs; |
@@ -869,6 +1050,7 @@ static size_t stm32_dma_desc_residue(struct stm32_dma_chan *chan, | |||
869 | struct stm32_dma_desc *desc, | 1050 | struct stm32_dma_desc *desc, |
870 | u32 next_sg) | 1051 | u32 next_sg) |
871 | { | 1052 | { |
1053 | u32 modulo, burst_size; | ||
872 | u32 residue = 0; | 1054 | u32 residue = 0; |
873 | int i; | 1055 | int i; |
874 | 1056 | ||
@@ -876,8 +1058,10 @@ static size_t stm32_dma_desc_residue(struct stm32_dma_chan *chan, | |||
876 | * In cyclic mode, for the last period, residue = remaining bytes from | 1058 | * In cyclic mode, for the last period, residue = remaining bytes from |
877 | * NDTR | 1059 | * NDTR |
878 | */ | 1060 | */ |
879 | if (chan->desc->cyclic && next_sg == 0) | 1061 | if (chan->desc->cyclic && next_sg == 0) { |
880 | return stm32_dma_get_remaining_bytes(chan); | 1062 | residue = stm32_dma_get_remaining_bytes(chan); |
1063 | goto end; | ||
1064 | } | ||
881 | 1065 | ||
882 | /* | 1066 | /* |
883 | * For all other periods in cyclic mode, and in sg mode, | 1067 | * For all other periods in cyclic mode, and in sg mode, |
@@ -888,6 +1072,15 @@ static size_t stm32_dma_desc_residue(struct stm32_dma_chan *chan, | |||
888 | residue += desc->sg_req[i].len; | 1072 | residue += desc->sg_req[i].len; |
889 | residue += stm32_dma_get_remaining_bytes(chan); | 1073 | residue += stm32_dma_get_remaining_bytes(chan); |
890 | 1074 | ||
1075 | end: | ||
1076 | if (!chan->mem_burst) | ||
1077 | return residue; | ||
1078 | |||
1079 | burst_size = chan->mem_burst * chan->mem_width; | ||
1080 | modulo = residue % burst_size; | ||
1081 | if (modulo) | ||
1082 | residue = residue - modulo + burst_size; | ||
1083 | |||
891 | return residue; | 1084 | return residue; |
892 | } | 1085 | } |
893 | 1086 | ||
@@ -902,7 +1095,7 @@ static enum dma_status stm32_dma_tx_status(struct dma_chan *c, | |||
902 | u32 residue = 0; | 1095 | u32 residue = 0; |
903 | 1096 | ||
904 | status = dma_cookie_status(c, cookie, state); | 1097 | status = dma_cookie_status(c, cookie, state); |
905 | if ((status == DMA_COMPLETE) || (!state)) | 1098 | if (status == DMA_COMPLETE || !state) |
906 | return status; | 1099 | return status; |
907 | 1100 | ||
908 | spin_lock_irqsave(&chan->vchan.lock, flags); | 1101 | spin_lock_irqsave(&chan->vchan.lock, flags); |
@@ -966,7 +1159,7 @@ static void stm32_dma_desc_free(struct virt_dma_desc *vdesc) | |||
966 | } | 1159 | } |
967 | 1160 | ||
968 | static void stm32_dma_set_config(struct stm32_dma_chan *chan, | 1161 | static void stm32_dma_set_config(struct stm32_dma_chan *chan, |
969 | struct stm32_dma_cfg *cfg) | 1162 | struct stm32_dma_cfg *cfg) |
970 | { | 1163 | { |
971 | stm32_dma_clear_reg(&chan->chan_reg); | 1164 | stm32_dma_clear_reg(&chan->chan_reg); |
972 | 1165 | ||
@@ -976,7 +1169,7 @@ static void stm32_dma_set_config(struct stm32_dma_chan *chan, | |||
976 | /* Enable Interrupts */ | 1169 | /* Enable Interrupts */ |
977 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_TEIE | STM32_DMA_SCR_TCIE; | 1170 | chan->chan_reg.dma_scr |= STM32_DMA_SCR_TEIE | STM32_DMA_SCR_TCIE; |
978 | 1171 | ||
979 | chan->chan_reg.dma_sfcr = cfg->threshold & STM32_DMA_SFCR_FTH_MASK; | 1172 | chan->threshold = STM32_DMA_THRESHOLD_FTR_GET(cfg->features); |
980 | } | 1173 | } |
981 | 1174 | ||
982 | static struct dma_chan *stm32_dma_of_xlate(struct of_phandle_args *dma_spec, | 1175 | static struct dma_chan *stm32_dma_of_xlate(struct of_phandle_args *dma_spec, |
@@ -996,10 +1189,10 @@ static struct dma_chan *stm32_dma_of_xlate(struct of_phandle_args *dma_spec, | |||
996 | cfg.channel_id = dma_spec->args[0]; | 1189 | cfg.channel_id = dma_spec->args[0]; |
997 | cfg.request_line = dma_spec->args[1]; | 1190 | cfg.request_line = dma_spec->args[1]; |
998 | cfg.stream_config = dma_spec->args[2]; | 1191 | cfg.stream_config = dma_spec->args[2]; |
999 | cfg.threshold = dma_spec->args[3]; | 1192 | cfg.features = dma_spec->args[3]; |
1000 | 1193 | ||
1001 | if ((cfg.channel_id >= STM32_DMA_MAX_CHANNELS) || | 1194 | if (cfg.channel_id >= STM32_DMA_MAX_CHANNELS || |
1002 | (cfg.request_line >= STM32_DMA_MAX_REQUEST_ID)) { | 1195 | cfg.request_line >= STM32_DMA_MAX_REQUEST_ID) { |
1003 | dev_err(dev, "Bad channel and/or request id\n"); | 1196 | dev_err(dev, "Bad channel and/or request id\n"); |
1004 | return NULL; | 1197 | return NULL; |
1005 | } | 1198 | } |