@@ -222,6 +222,11 @@ enum stm32_dma3_port_data_width {
222222#define STM32_DMA3_DT_PFREQ BIT(9) /* CTR2_PFREQ */
223223#define STM32_DMA3_DT_TCEM GENMASK(13, 12) /* CTR2_TCEM */
224224
225+ /* struct stm32_dma3_chan .config_set bitfield */
226+ #define STM32_DMA3_CFG_SET_DT BIT(0)
227+ #define STM32_DMA3_CFG_SET_DMA BIT(1)
228+ #define STM32_DMA3_CFG_SET_BOTH (STM32_DMA3_CFG_SET_DT | STM32_DMA3_CFG_SET_DMA)
229+
225230#define STM32_DMA3_MAX_BLOCK_SIZE ALIGN_DOWN(CBR1_BNDT, 64)
226231#define port_is_ahb (maxdw ) ({ typeof(maxdw) (_maxdw) = (maxdw); \
227232 ((_maxdw) != DW_INVALID) && ((_maxdw) == DW_32); })
@@ -281,6 +286,7 @@ struct stm32_dma3_chan {
281286 bool semaphore_mode ;
282287 struct stm32_dma3_dt_conf dt_config ;
283288 struct dma_slave_config dma_config ;
289+ u8 config_set ;
284290 struct dma_pool * lli_pool ;
285291 struct stm32_dma3_swdesc * swdesc ;
286292 enum ctr2_tcem tcem ;
@@ -548,7 +554,7 @@ static int stm32_dma3_chan_prep_hw(struct stm32_dma3_chan *chan, enum dma_transf
548554{
549555 struct stm32_dma3_ddata * ddata = to_stm32_dma3_ddata (chan );
550556 struct dma_device dma_device = ddata -> dma_dev ;
551- u32 sdw , ddw , sbl_max , dbl_max , tcem ;
557+ u32 sdw , ddw , sbl_max , dbl_max , tcem , init_dw , init_bl_max ;
552558 u32 _ctr1 = 0 , _ctr2 = 0 ;
553559 u32 ch_conf = chan -> dt_config .ch_conf ;
554560 u32 tr_conf = chan -> dt_config .tr_conf ;
@@ -667,6 +673,49 @@ static int stm32_dma3_chan_prep_hw(struct stm32_dma3_chan *chan, enum dma_transf
667673
668674 break ;
669675
676+ case DMA_MEM_TO_MEM :
677+ /* Set source (memory) data width and burst */
678+ init_dw = sdw ;
679+ init_bl_max = sbl_max ;
680+ sdw = stm32_dma3_get_max_dw (chan -> max_burst , sap_max_dw , len , src_addr );
681+ sbl_max = stm32_dma3_get_max_burst (len , sdw , chan -> max_burst );
682+ if (chan -> config_set & STM32_DMA3_CFG_SET_DMA ) {
683+ sdw = min_t (u32 , init_dw , sdw );
684+ sbl_max = min_t (u32 , init_bl_max ,
685+ stm32_dma3_get_max_burst (len , sdw , chan -> max_burst ));
686+ }
687+
688+ /* Set destination (memory) data width and burst */
689+ init_dw = ddw ;
690+ init_bl_max = dbl_max ;
691+ ddw = stm32_dma3_get_max_dw (chan -> max_burst , dap_max_dw , len , dst_addr );
692+ dbl_max = stm32_dma3_get_max_burst (len , ddw , chan -> max_burst );
693+ if (chan -> config_set & STM32_DMA3_CFG_SET_DMA ) {
694+ ddw = min_t (u32 , init_dw , ddw );
695+ dbl_max = min_t (u32 , init_bl_max ,
696+ stm32_dma3_get_max_burst (len , ddw , chan -> max_burst ));
697+ }
698+
699+ _ctr1 |= FIELD_PREP (CTR1_SDW_LOG2 , ilog2 (sdw ));
700+ _ctr1 |= FIELD_PREP (CTR1_SBL_1 , sbl_max - 1 );
701+ _ctr1 |= FIELD_PREP (CTR1_DDW_LOG2 , ilog2 (ddw ));
702+ _ctr1 |= FIELD_PREP (CTR1_DBL_1 , dbl_max - 1 );
703+
704+ if (ddw != sdw ) {
705+ _ctr1 |= FIELD_PREP (CTR1_PAM , CTR1_PAM_PACK_UNPACK );
706+ /* Should never reach this case as ddw is clamped down */
707+ if (len & (ddw - 1 )) {
708+ dev_err (chan2dev (chan ),
709+ "Packing mode is enabled and len is not multiple of ddw" );
710+ return - EINVAL ;
711+ }
712+ }
713+
714+ /* CTR2_REQSEL/DREQ/BREQ/PFREQ are ignored with CTR2_SWREQ=1 */
715+ _ctr2 |= CTR2_SWREQ ;
716+
717+ break ;
718+
670719 default :
671720 dev_err (chan2dev (chan ), "Direction %s not supported\n" ,
672721 dmaengine_get_direction_text (dir ));
@@ -936,6 +985,82 @@ static void stm32_dma3_free_chan_resources(struct dma_chan *c)
936985 /* Reset configuration */
937986 memset (& chan -> dt_config , 0 , sizeof (chan -> dt_config ));
938987 memset (& chan -> dma_config , 0 , sizeof (chan -> dma_config ));
988+ chan -> config_set = 0 ;
989+ }
990+
991+ static void stm32_dma3_init_chan_config_for_memcpy (struct stm32_dma3_chan * chan ,
992+ dma_addr_t dst , dma_addr_t src )
993+ {
994+ struct stm32_dma3_ddata * ddata = to_stm32_dma3_ddata (chan );
995+ u32 dw = get_chan_max_dw (ddata -> ports_max_dw [0 ], chan -> max_burst ); /* port 0 by default */
996+ u32 burst = chan -> max_burst / dw ;
997+
998+ /* Initialize dt_config if channel not pre-configured through DT */
999+ if (!(chan -> config_set & STM32_DMA3_CFG_SET_DT )) {
1000+ chan -> dt_config .ch_conf = FIELD_PREP (STM32_DMA3_DT_PRIO , CCR_PRIO_VERY_HIGH );
1001+ chan -> dt_config .ch_conf |= FIELD_PREP (STM32_DMA3_DT_FIFO , chan -> fifo_size );
1002+ chan -> dt_config .tr_conf = STM32_DMA3_DT_SINC | STM32_DMA3_DT_DINC ;
1003+ chan -> dt_config .tr_conf |= FIELD_PREP (STM32_DMA3_DT_TCEM , CTR2_TCEM_CHANNEL );
1004+ }
1005+
1006+ /* Initialize dma_config if dmaengine_slave_config() not used */
1007+ if (!(chan -> config_set & STM32_DMA3_CFG_SET_DMA )) {
1008+ chan -> dma_config .src_addr_width = dw ;
1009+ chan -> dma_config .dst_addr_width = dw ;
1010+ chan -> dma_config .src_maxburst = burst ;
1011+ chan -> dma_config .dst_maxburst = burst ;
1012+ chan -> dma_config .src_addr = src ;
1013+ chan -> dma_config .dst_addr = dst ;
1014+ }
1015+ }
1016+
1017+ static struct dma_async_tx_descriptor * stm32_dma3_prep_dma_memcpy (struct dma_chan * c ,
1018+ dma_addr_t dst , dma_addr_t src ,
1019+ size_t len , unsigned long flags )
1020+ {
1021+ struct stm32_dma3_chan * chan = to_stm32_dma3_chan (c );
1022+ struct stm32_dma3_swdesc * swdesc ;
1023+ size_t next_size , offset ;
1024+ u32 count , i , ctr1 , ctr2 ;
1025+
1026+ count = DIV_ROUND_UP (len , STM32_DMA3_MAX_BLOCK_SIZE );
1027+
1028+ swdesc = stm32_dma3_chan_desc_alloc (chan , count );
1029+ if (!swdesc )
1030+ return NULL ;
1031+
1032+ if (chan -> config_set != STM32_DMA3_CFG_SET_BOTH )
1033+ stm32_dma3_init_chan_config_for_memcpy (chan , dst , src );
1034+
1035+ for (i = 0 , offset = 0 ; offset < len ; i ++ , offset += next_size ) {
1036+ size_t remaining ;
1037+ int ret ;
1038+
1039+ remaining = len - offset ;
1040+ next_size = min_t (size_t , remaining , STM32_DMA3_MAX_BLOCK_SIZE );
1041+
1042+ ret = stm32_dma3_chan_prep_hw (chan , DMA_MEM_TO_MEM , & swdesc -> ccr , & ctr1 , & ctr2 ,
1043+ src + offset , dst + offset , next_size );
1044+ if (ret )
1045+ goto err_desc_free ;
1046+
1047+ stm32_dma3_chan_prep_hwdesc (chan , swdesc , i , src + offset , dst + offset , next_size ,
1048+ ctr1 , ctr2 , next_size == remaining , false);
1049+ }
1050+
1051+ /* Enable Errors interrupts */
1052+ swdesc -> ccr |= CCR_USEIE | CCR_ULEIE | CCR_DTEIE ;
1053+ /* Enable Transfer state interrupts */
1054+ swdesc -> ccr |= CCR_TCIE ;
1055+
1056+ swdesc -> cyclic = false;
1057+
1058+ return vchan_tx_prep (& chan -> vchan , & swdesc -> vdesc , flags );
1059+
1060+ err_desc_free :
1061+ stm32_dma3_chan_desc_free (chan , swdesc );
1062+
1063+ return NULL ;
9391064}
9401065
9411066static struct dma_async_tx_descriptor * stm32_dma3_prep_slave_sg (struct dma_chan * c ,
@@ -1119,6 +1244,7 @@ static int stm32_dma3_config(struct dma_chan *c, struct dma_slave_config *config
11191244 struct stm32_dma3_chan * chan = to_stm32_dma3_chan (c );
11201245
11211246 memcpy (& chan -> dma_config , config , sizeof (* config ));
1247+ chan -> config_set |= STM32_DMA3_CFG_SET_DMA ;
11221248
11231249 return 0 ;
11241250}
@@ -1233,6 +1359,7 @@ static struct dma_chan *stm32_dma3_of_xlate(struct of_phandle_args *dma_spec, st
12331359
12341360 chan = to_stm32_dma3_chan (c );
12351361 chan -> dt_config = conf ;
1362+ chan -> config_set |= STM32_DMA3_CFG_SET_DT ;
12361363
12371364 return c ;
12381365}
@@ -1331,6 +1458,7 @@ static int stm32_dma3_probe(struct platform_device *pdev)
13311458 dma_cap_set (DMA_SLAVE , dma_dev -> cap_mask );
13321459 dma_cap_set (DMA_PRIVATE , dma_dev -> cap_mask );
13331460 dma_cap_set (DMA_CYCLIC , dma_dev -> cap_mask );
1461+ dma_cap_set (DMA_MEMCPY , dma_dev -> cap_mask );
13341462 dma_dev -> dev = & pdev -> dev ;
13351463 /*
13361464 * This controller supports up to 8-byte buswidth depending on the port used and the
@@ -1352,6 +1480,7 @@ static int stm32_dma3_probe(struct platform_device *pdev)
13521480 dma_dev -> residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR ;
13531481 dma_dev -> device_alloc_chan_resources = stm32_dma3_alloc_chan_resources ;
13541482 dma_dev -> device_free_chan_resources = stm32_dma3_free_chan_resources ;
1483+ dma_dev -> device_prep_dma_memcpy = stm32_dma3_prep_dma_memcpy ;
13551484 dma_dev -> device_prep_slave_sg = stm32_dma3_prep_slave_sg ;
13561485 dma_dev -> device_prep_dma_cyclic = stm32_dma3_prep_dma_cyclic ;
13571486 dma_dev -> device_caps = stm32_dma3_caps ;
0 commit comments