1 /*
2  * Copyright (c) 2019 Song Qiang <songqiang1304521@gmail.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef DMA_STM32_H_
8 #define DMA_STM32_H_
9 
10 #include <soc.h>
11 #include <stm32_ll_dma.h>
12 #include <zephyr/drivers/dma.h>
13 #include <zephyr/drivers/clock_control/stm32_clock_control.h>
14 
15 /* Maximum data sent in single transfer (Bytes) */
16 #define DMA_STM32_MAX_DATA_ITEMS	0xffff
17 
18 struct dma_stm32_stream {
19 	uint32_t direction;
20 #ifdef CONFIG_DMAMUX_STM32
21 	int mux_channel; /* stores the dmamux channel */
22 #endif /* CONFIG_DMAMUX_STM32 */
23 	bool source_periph;
24 	bool hal_override;
25 	volatile bool busy;
26 	uint32_t src_size;
27 	uint32_t dst_size;
28 	void *user_data; /* holds the client data */
29 	dma_callback_t dma_callback;
30 	bool cyclic;
31 };
32 
33 struct dma_stm32_data {
34 		struct dma_context dma_ctx;
35 };
36 
37 struct dma_stm32_config {
38 	struct stm32_pclken pclken;
39 	void (*config_irq)(const struct device *dev);
40 	bool support_m2m;
41 	uint32_t base;
42 	uint32_t max_streams;
43 #ifdef CONFIG_DMAMUX_STM32
44 	uint8_t offset; /* position in the list of dmamux channel list */
45 #endif
46 	struct dma_stm32_stream *streams;
47 };
48 
49 uint32_t dma_stm32_id_to_stream(uint32_t id);
50 #if !defined(CONFIG_DMAMUX_STM32)
51 uint32_t dma_stm32_slot_to_channel(uint32_t id);
52 #endif
53 
54 typedef void (*dma_stm32_clear_flag_func)(DMA_TypeDef *DMAx);
55 #if !defined(CONFIG_SOC_SERIES_STM32G0X)
56 typedef uint32_t (*dma_stm32_check_flag_func)(DMA_TypeDef *DMAx);
57 #else
58 typedef uint32_t (*dma_stm32_check_flag_func)(const DMA_TypeDef *DMAx);
59 #endif
60 
61 bool dma_stm32_is_tc_active(DMA_TypeDef *DMAx, uint32_t id);
62 void dma_stm32_clear_tc(DMA_TypeDef *DMAx, uint32_t id);
63 bool dma_stm32_is_ht_active(DMA_TypeDef *DMAx, uint32_t id);
64 void dma_stm32_clear_ht(DMA_TypeDef *DMAx, uint32_t id);
65 bool dma_stm32_is_te_active(DMA_TypeDef *DMAx, uint32_t id);
66 void dma_stm32_clear_te(DMA_TypeDef *DMAx, uint32_t id);
67 
68 #ifdef CONFIG_DMA_STM32_V1
69 bool dma_stm32_is_dme_active(DMA_TypeDef *DMAx, uint32_t id);
70 void dma_stm32_clear_dme(DMA_TypeDef *DMAx, uint32_t id);
71 bool dma_stm32_is_fe_active(DMA_TypeDef *DMAx, uint32_t id);
72 void dma_stm32_clear_fe(DMA_TypeDef *DMAx, uint32_t id);
73 #endif
74 
75 #ifdef CONFIG_DMA_STM32_V2
76 bool dma_stm32_is_gi_active(DMA_TypeDef *DMAx, uint32_t id);
77 void dma_stm32_clear_gi(DMA_TypeDef *DMAx, uint32_t id);
78 #endif
79 
80 bool stm32_dma_is_irq_active(DMA_TypeDef *dma, uint32_t id);
81 bool stm32_dma_is_ht_irq_active(DMA_TypeDef *dma, uint32_t id);
82 bool stm32_dma_is_tc_irq_active(DMA_TypeDef *dma, uint32_t id);
83 
84 void stm32_dma_dump_stream_irq(DMA_TypeDef *dma, uint32_t id);
85 void stm32_dma_clear_stream_irq(DMA_TypeDef *dma, uint32_t id);
86 bool stm32_dma_is_irq_happened(DMA_TypeDef *dma, uint32_t id);
87 bool stm32_dma_is_unexpected_irq_happened(DMA_TypeDef *dma, uint32_t id);
88 void stm32_dma_enable_stream(DMA_TypeDef *dma, uint32_t id);
89 bool stm32_dma_is_enabled_stream(DMA_TypeDef *dma, uint32_t id);
90 int stm32_dma_disable_stream(DMA_TypeDef *dma, uint32_t id);
91 
92 #if !defined(CONFIG_DMAMUX_STM32)
93 void stm32_dma_config_channel_function(DMA_TypeDef *dma, uint32_t id,
94 						uint32_t slot);
95 #endif
96 
97 #ifdef CONFIG_DMA_STM32_V1
98 void stm32_dma_disable_fifo_irq(DMA_TypeDef *dma, uint32_t id);
99 bool stm32_dma_check_fifo_mburst(LL_DMA_InitTypeDef *DMAx);
100 uint32_t stm32_dma_get_fifo_threshold(uint16_t fifo_mode_control);
101 uint32_t stm32_dma_get_mburst(struct dma_config *config, bool source_periph);
102 uint32_t stm32_dma_get_pburst(struct dma_config *config, bool source_periph);
103 #endif
104 
105 #ifdef CONFIG_DMAMUX_STM32
106 /* dma_stm32_ api functions are exported to the dmamux_stm32 */
107 #define DMA_STM32_EXPORT_API
108 int dma_stm32_configure(const struct device *dev, uint32_t id,
109 				struct dma_config *config);
110 int dma_stm32_reload(const struct device *dev, uint32_t id,
111 			uint32_t src, uint32_t dst, size_t size);
112 int dma_stm32_start(const struct device *dev, uint32_t id);
113 int dma_stm32_stop(const struct device *dev, uint32_t id);
114 int dma_stm32_get_status(const struct device *dev, uint32_t id,
115 				struct dma_status *stat);
116 #else
117 #define DMA_STM32_EXPORT_API static
118 #endif /* CONFIG_DMAMUX_STM32 */
119 
120 #endif /* DMA_STM32_H_*/
121