aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/dma/stm32-dma.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/dma/stm32-dma.c')
-rw-r--r--drivers/dma/stm32-dma.c88
1 files changed, 59 insertions, 29 deletions
diff --git a/drivers/dma/stm32-dma.c b/drivers/dma/stm32-dma.c
index 3056ce7f8c69..49f86cabcfec 100644
--- a/drivers/dma/stm32-dma.c
+++ b/drivers/dma/stm32-dma.c
@@ -114,6 +114,7 @@
114#define STM32_DMA_MAX_CHANNELS 0x08 114#define STM32_DMA_MAX_CHANNELS 0x08
115#define STM32_DMA_MAX_REQUEST_ID 0x08 115#define STM32_DMA_MAX_REQUEST_ID 0x08
116#define STM32_DMA_MAX_DATA_PARAM 0x03 116#define STM32_DMA_MAX_DATA_PARAM 0x03
117#define STM32_DMA_MAX_BURST 16
117 118
118enum stm32_dma_width { 119enum stm32_dma_width {
119 STM32_DMA_BYTE, 120 STM32_DMA_BYTE,
@@ -403,6 +404,13 @@ static int stm32_dma_terminate_all(struct dma_chan *c)
403 return 0; 404 return 0;
404} 405}
405 406
407static void stm32_dma_synchronize(struct dma_chan *c)
408{
409 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
410
411 vchan_synchronize(&chan->vchan);
412}
413
406static void stm32_dma_dump_reg(struct stm32_dma_chan *chan) 414static void stm32_dma_dump_reg(struct stm32_dma_chan *chan)
407{ 415{
408 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); 416 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
@@ -421,7 +429,7 @@ static void stm32_dma_dump_reg(struct stm32_dma_chan *chan)
421 dev_dbg(chan2dev(chan), "SFCR: 0x%08x\n", sfcr); 429 dev_dbg(chan2dev(chan), "SFCR: 0x%08x\n", sfcr);
422} 430}
423 431
424static int stm32_dma_start_transfer(struct stm32_dma_chan *chan) 432static void stm32_dma_start_transfer(struct stm32_dma_chan *chan)
425{ 433{
426 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); 434 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
427 struct virt_dma_desc *vdesc; 435 struct virt_dma_desc *vdesc;
@@ -432,12 +440,12 @@ static int stm32_dma_start_transfer(struct stm32_dma_chan *chan)
432 440
433 ret = stm32_dma_disable_chan(chan); 441 ret = stm32_dma_disable_chan(chan);
434 if (ret < 0) 442 if (ret < 0)
435 return ret; 443 return;
436 444
437 if (!chan->desc) { 445 if (!chan->desc) {
438 vdesc = vchan_next_desc(&chan->vchan); 446 vdesc = vchan_next_desc(&chan->vchan);
439 if (!vdesc) 447 if (!vdesc)
440 return -EPERM; 448 return;
441 449
442 chan->desc = to_stm32_dma_desc(vdesc); 450 chan->desc = to_stm32_dma_desc(vdesc);
443 chan->next_sg = 0; 451 chan->next_sg = 0;
@@ -471,7 +479,7 @@ static int stm32_dma_start_transfer(struct stm32_dma_chan *chan)
471 479
472 chan->busy = true; 480 chan->busy = true;
473 481
474 return 0; 482 dev_dbg(chan2dev(chan), "vchan %p: started\n", &chan->vchan);
475} 483}
476 484
477static void stm32_dma_configure_next_sg(struct stm32_dma_chan *chan) 485static void stm32_dma_configure_next_sg(struct stm32_dma_chan *chan)
@@ -500,8 +508,6 @@ static void stm32_dma_configure_next_sg(struct stm32_dma_chan *chan)
500 dev_dbg(chan2dev(chan), "CT=0 <=> SM1AR: 0x%08x\n", 508 dev_dbg(chan2dev(chan), "CT=0 <=> SM1AR: 0x%08x\n",
501 stm32_dma_read(dmadev, STM32_DMA_SM1AR(id))); 509 stm32_dma_read(dmadev, STM32_DMA_SM1AR(id)));
502 } 510 }
503
504 chan->next_sg++;
505 } 511 }
506} 512}
507 513
@@ -510,6 +516,7 @@ static void stm32_dma_handle_chan_done(struct stm32_dma_chan *chan)
510 if (chan->desc) { 516 if (chan->desc) {
511 if (chan->desc->cyclic) { 517 if (chan->desc->cyclic) {
512 vchan_cyclic_callback(&chan->desc->vdesc); 518 vchan_cyclic_callback(&chan->desc->vdesc);
519 chan->next_sg++;
513 stm32_dma_configure_next_sg(chan); 520 stm32_dma_configure_next_sg(chan);
514 } else { 521 } else {
515 chan->busy = false; 522 chan->busy = false;
@@ -552,15 +559,13 @@ static void stm32_dma_issue_pending(struct dma_chan *c)
552{ 559{
553 struct stm32_dma_chan *chan = to_stm32_dma_chan(c); 560 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
554 unsigned long flags; 561 unsigned long flags;
555 int ret;
556 562
557 spin_lock_irqsave(&chan->vchan.lock, flags); 563 spin_lock_irqsave(&chan->vchan.lock, flags);
558 if (!chan->busy) { 564 if (vchan_issue_pending(&chan->vchan) && !chan->desc && !chan->busy) {
559 if (vchan_issue_pending(&chan->vchan) && !chan->desc) { 565 dev_dbg(chan2dev(chan), "vchan %p: issued\n", &chan->vchan);
560 ret = stm32_dma_start_transfer(chan); 566 stm32_dma_start_transfer(chan);
561 if ((!ret) && (chan->desc->cyclic)) 567 if (chan->desc->cyclic)
562 stm32_dma_configure_next_sg(chan); 568 stm32_dma_configure_next_sg(chan);
563 }
564 } 569 }
565 spin_unlock_irqrestore(&chan->vchan.lock, flags); 570 spin_unlock_irqrestore(&chan->vchan.lock, flags);
566} 571}
@@ -848,26 +853,40 @@ static struct dma_async_tx_descriptor *stm32_dma_prep_dma_memcpy(
848 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags); 853 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
849} 854}
850 855
856static u32 stm32_dma_get_remaining_bytes(struct stm32_dma_chan *chan)
857{
858 u32 dma_scr, width, ndtr;
859 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
860
861 dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
862 width = STM32_DMA_SCR_PSIZE_GET(dma_scr);
863 ndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id));
864
865 return ndtr << width;
866}
867
851static size_t stm32_dma_desc_residue(struct stm32_dma_chan *chan, 868static size_t stm32_dma_desc_residue(struct stm32_dma_chan *chan,
852 struct stm32_dma_desc *desc, 869 struct stm32_dma_desc *desc,
853 u32 next_sg) 870 u32 next_sg)
854{ 871{
855 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan); 872 u32 residue = 0;
856 u32 dma_scr, width, residue, count;
857 int i; 873 int i;
858 874
859 residue = 0; 875 /*
876 * In cyclic mode, for the last period, residue = remaining bytes from
877 * NDTR
878 */
879 if (chan->desc->cyclic && next_sg == 0)
880 return stm32_dma_get_remaining_bytes(chan);
860 881
882 /*
883 * For all other periods in cyclic mode, and in sg mode,
884 * residue = remaining bytes from NDTR + remaining periods/sg to be
885 * transferred
886 */
861 for (i = next_sg; i < desc->num_sgs; i++) 887 for (i = next_sg; i < desc->num_sgs; i++)
862 residue += desc->sg_req[i].len; 888 residue += desc->sg_req[i].len;
863 889 residue += stm32_dma_get_remaining_bytes(chan);
864 if (next_sg != 0) {
865 dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
866 width = STM32_DMA_SCR_PSIZE_GET(dma_scr);
867 count = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id));
868
869 residue += count << width;
870 }
871 890
872 return residue; 891 return residue;
873} 892}
@@ -964,27 +983,36 @@ static struct dma_chan *stm32_dma_of_xlate(struct of_phandle_args *dma_spec,
964 struct of_dma *ofdma) 983 struct of_dma *ofdma)
965{ 984{
966 struct stm32_dma_device *dmadev = ofdma->of_dma_data; 985 struct stm32_dma_device *dmadev = ofdma->of_dma_data;
986 struct device *dev = dmadev->ddev.dev;
967 struct stm32_dma_cfg cfg; 987 struct stm32_dma_cfg cfg;
968 struct stm32_dma_chan *chan; 988 struct stm32_dma_chan *chan;
969 struct dma_chan *c; 989 struct dma_chan *c;
970 990
971 if (dma_spec->args_count < 4) 991 if (dma_spec->args_count < 4) {
992 dev_err(dev, "Bad number of cells\n");
972 return NULL; 993 return NULL;
994 }
973 995
974 cfg.channel_id = dma_spec->args[0]; 996 cfg.channel_id = dma_spec->args[0];
975 cfg.request_line = dma_spec->args[1]; 997 cfg.request_line = dma_spec->args[1];
976 cfg.stream_config = dma_spec->args[2]; 998 cfg.stream_config = dma_spec->args[2];
977 cfg.threshold = dma_spec->args[3]; 999 cfg.threshold = dma_spec->args[3];
978 1000
979 if ((cfg.channel_id >= STM32_DMA_MAX_CHANNELS) || (cfg.request_line >= 1001 if ((cfg.channel_id >= STM32_DMA_MAX_CHANNELS) ||
980 STM32_DMA_MAX_REQUEST_ID)) 1002 (cfg.request_line >= STM32_DMA_MAX_REQUEST_ID)) {
1003 dev_err(dev, "Bad channel and/or request id\n");
981 return NULL; 1004 return NULL;
1005 }
982 1006
983 chan = &dmadev->chan[cfg.channel_id]; 1007 chan = &dmadev->chan[cfg.channel_id];
984 1008
985 c = dma_get_slave_channel(&chan->vchan.chan); 1009 c = dma_get_slave_channel(&chan->vchan.chan);
986 if (c) 1010 if (!c) {
987 stm32_dma_set_config(chan, &cfg); 1011 dev_err(dev, "No more channel avalaible\n");
1012 return NULL;
1013 }
1014
1015 stm32_dma_set_config(chan, &cfg);
988 1016
989 return c; 1017 return c;
990} 1018}
@@ -1048,6 +1076,7 @@ static int stm32_dma_probe(struct platform_device *pdev)
1048 dd->device_prep_dma_cyclic = stm32_dma_prep_dma_cyclic; 1076 dd->device_prep_dma_cyclic = stm32_dma_prep_dma_cyclic;
1049 dd->device_config = stm32_dma_slave_config; 1077 dd->device_config = stm32_dma_slave_config;
1050 dd->device_terminate_all = stm32_dma_terminate_all; 1078 dd->device_terminate_all = stm32_dma_terminate_all;
1079 dd->device_synchronize = stm32_dma_synchronize;
1051 dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | 1080 dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1052 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | 1081 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1053 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); 1082 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
@@ -1056,6 +1085,7 @@ static int stm32_dma_probe(struct platform_device *pdev)
1056 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); 1085 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1057 dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV); 1086 dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1058 dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST; 1087 dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1088 dd->max_burst = STM32_DMA_MAX_BURST;
1059 dd->dev = &pdev->dev; 1089 dd->dev = &pdev->dev;
1060 INIT_LIST_HEAD(&dd->channels); 1090 INIT_LIST_HEAD(&dd->channels);
1061 1091