1 /*
2  * Copyright (c) 2021 Linaro Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_DRIVERS_DMA_STM32_H_
8 #define ZEPHYR_INCLUDE_DRIVERS_DMA_STM32_H_
9 
10 /* @brief linked_channel value to inform zephyr dma driver that
11  * DMA channel will be handled by HAL
12  */
13 #define STM32_DMA_HAL_OVERRIDE      0x7F
14 
15 /* @brief gives the first DMA channel : 0 or 1 in the register map
16  * when counting channels from 1 to N or from 0 to N-1
17  */
18 #if defined(CONFIG_DMA_STM32U5)
19 /* from DTS the dma stream id is in range 0..N-1 */
20 #define STM32_DMA_STREAM_OFFSET 0
21 #elif !defined(CONFIG_DMA_STM32_V1)
22 /* from DTS the dma stream id is in range 1..N */
23 /* so decrease to set range from 0 from now on */
24 #define STM32_DMA_STREAM_OFFSET 1
25 #elif defined(CONFIG_DMA_STM32_V1) && defined(CONFIG_DMAMUX_STM32)
26 /* typically on the stm32H7 serie, DMA V1 with mux */
27 #define STM32_DMA_STREAM_OFFSET 1
28 #else
29 /* from DTS the dma stream id is in range 0..N-1 */
30 #define STM32_DMA_STREAM_OFFSET 0
31 #endif /* ! CONFIG_DMA_STM32_V1 */
32 
33 /* macro for dma slot (only for dma-v1 or dma-v2 types) */
34 #if DT_HAS_COMPAT_STATUS_OKAY(st_stm32_dma_v2bis)
35 #define STM32_DMA_SLOT(id, dir, slot) 0
36 #else
37 #define STM32_DMA_SLOT(id, dir, slot) DT_INST_DMAS_CELL_BY_NAME(id, dir, slot)
38 #endif
39 
40 #if DT_HAS_COMPAT_STATUS_OKAY(st_stm32_dma_v2) || \
41 	DT_HAS_COMPAT_STATUS_OKAY(st_stm32_dma_v2bis) || \
42 	DT_HAS_COMPAT_STATUS_OKAY(st_stm32_dmamux)
43 #define STM32_DMA_FEATURES(id, dir) 0
44 #else
45 #define STM32_DMA_FEATURES(id, dir)						\
46 		DT_INST_DMAS_CELL_BY_NAME(id, dir, features)
47 #endif
48 
49 #define STM32_DMA_CTLR(id, dir)						\
50 		DT_INST_DMAS_CTLR_BY_NAME(id, dir)
51 #define STM32_DMA_CHANNEL_CONFIG(id, dir)					\
52 		DT_INST_DMAS_CELL_BY_NAME(id, dir, channel_config)
53 
54 /* macros for channel-config */
55 /* direction defined on bits 6-7 */
56 /* 0 -> MEM_TO_MEM, 1 -> MEM_TO_PERIPH, 2 -> PERIPH_TO_MEM */
57 #define STM32_DMA_CONFIG_DIRECTION(config)		((config >> 6) & 0x3)
58 /* periph increment defined on bit 9 as true/false */
59 #define STM32_DMA_CONFIG_PERIPHERAL_ADDR_INC(config)	((config >> 9) & 0x1)
60 /* mem increment defined on bit 10 as true/false */
61 #define STM32_DMA_CONFIG_MEMORY_ADDR_INC(config)	((config >> 10) & 0x1)
62 /* periph data size defined on bits 11-12 */
63 /* 0 -> 1 byte, 1 -> 2 bytes, 2 -> 4 bytes */
64 #define STM32_DMA_CONFIG_PERIPHERAL_DATA_SIZE(config)	\
65 						(1 << ((config >> 11) & 0x3))
66 /* memory data size defined on bits 13, 14 */
67 /* 0 -> 1 byte, 1 -> 2 bytes, 2 -> 4 bytes */
68 #define STM32_DMA_CONFIG_MEMORY_DATA_SIZE(config)	\
69 						(1 << ((config >> 13) & 0x3))
70 /* priority increment offset defined on bit 15 */
71 #define STM32_DMA_CONFIG_PERIPHERAL_INC_FIXED(config)	((config >> 15) & 0x1)
72 /* priority defined on bits 16-17 as 0, 1, 2, 3 */
73 #define STM32_DMA_CONFIG_PRIORITY(config)		((config >> 16) & 0x3)
74 
75 /* macro for features (only for dma-v1) */
76 #if DT_HAS_COMPAT_STATUS_OKAY(st_stm32_dma_v1)
77 #define STM32_DMA_FEATURES_FIFO_THRESHOLD(features)	(features & 0x3)
78 #else
79 #define STM32_DMA_FEATURES_FIFO_THRESHOLD(features)	0
80 #endif
81 
82 #endif /* ZEPHYR_INCLUDE_DRIVERS_DMA_STM32_H_ */
83