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 };
31 
32 struct dma_stm32_data {
33 		struct dma_context dma_ctx;
34 };
35 
36 struct dma_stm32_config {
37 	struct stm32_pclken pclken;
38 	void (*config_irq)(const struct device *dev);
39 	bool support_m2m;
40 	uint32_t base;
41 	uint32_t max_streams;
42 #ifdef CONFIG_DMAMUX_STM32
43 	uint8_t offset; /* position in the list of dmamux channel list */
44 #endif
45 	struct dma_stm32_stream *streams;
46 };
47 
48 uint32_t dma_stm32_id_to_stream(uint32_t id);
49 #if !defined(CONFIG_DMAMUX_STM32)
50 uint32_t dma_stm32_slot_to_channel(uint32_t id);
51 #endif
52 
53 typedef void (*dma_stm32_clear_flag_func)(DMA_TypeDef *DMAx);
54 typedef uint32_t (*dma_stm32_check_flag_func)(DMA_TypeDef *DMAx);
55 
56 bool dma_stm32_is_tc_active(DMA_TypeDef *DMAx, uint32_t id);
57 void dma_stm32_clear_tc(DMA_TypeDef *DMAx, uint32_t id);
58 bool dma_stm32_is_ht_active(DMA_TypeDef *DMAx, uint32_t id);
59 void dma_stm32_clear_ht(DMA_TypeDef *DMAx, uint32_t id);
60 bool dma_stm32_is_te_active(DMA_TypeDef *DMAx, uint32_t id);
61 void dma_stm32_clear_te(DMA_TypeDef *DMAx, uint32_t id);
62 
63 #ifdef CONFIG_DMA_STM32_V1
64 bool dma_stm32_is_dme_active(DMA_TypeDef *DMAx, uint32_t id);
65 void dma_stm32_clear_dme(DMA_TypeDef *DMAx, uint32_t id);
66 bool dma_stm32_is_fe_active(DMA_TypeDef *DMAx, uint32_t id);
67 void dma_stm32_clear_fe(DMA_TypeDef *DMAx, uint32_t id);
68 #endif
69 
70 #ifdef CONFIG_DMA_STM32_V2
71 bool dma_stm32_is_gi_active(DMA_TypeDef *DMAx, uint32_t id);
72 void dma_stm32_clear_gi(DMA_TypeDef *DMAx, uint32_t id);
73 #endif
74 
75 bool stm32_dma_is_irq_active(DMA_TypeDef *dma, uint32_t id);
76 bool stm32_dma_is_ht_irq_active(DMA_TypeDef *dma, uint32_t id);
77 bool stm32_dma_is_tc_irq_active(DMA_TypeDef *dma, uint32_t id);
78 
79 void stm32_dma_dump_stream_irq(DMA_TypeDef *dma, uint32_t id);
80 void stm32_dma_clear_stream_irq(DMA_TypeDef *dma, uint32_t id);
81 bool stm32_dma_is_irq_happened(DMA_TypeDef *dma, uint32_t id);
82 bool stm32_dma_is_unexpected_irq_happened(DMA_TypeDef *dma, uint32_t id);
83 void stm32_dma_enable_stream(DMA_TypeDef *dma, uint32_t id);
84 bool stm32_dma_is_enabled_stream(DMA_TypeDef *dma, uint32_t id);
85 int stm32_dma_disable_stream(DMA_TypeDef *dma, uint32_t id);
86 
87 #if !defined(CONFIG_DMAMUX_STM32)
88 void stm32_dma_config_channel_function(DMA_TypeDef *dma, uint32_t id,
89 						uint32_t slot);
90 #endif
91 
92 #ifdef CONFIG_DMA_STM32_V1
93 void stm32_dma_disable_fifo_irq(DMA_TypeDef *dma, uint32_t id);
94 bool stm32_dma_check_fifo_mburst(LL_DMA_InitTypeDef *DMAx);
95 uint32_t stm32_dma_get_fifo_threshold(uint16_t fifo_mode_control);
96 uint32_t stm32_dma_get_mburst(struct dma_config *config, bool source_periph);
97 uint32_t stm32_dma_get_pburst(struct dma_config *config, bool source_periph);
98 #endif
99 
100 #ifdef CONFIG_DMAMUX_STM32
101 /* dma_stm32_ api functions are exported to the dmamux_stm32 */
102 #define DMA_STM32_EXPORT_API
103 int dma_stm32_configure(const struct device *dev, uint32_t id,
104 				struct dma_config *config);
105 int dma_stm32_reload(const struct device *dev, uint32_t id,
106 			uint32_t src, uint32_t dst, size_t size);
107 int dma_stm32_start(const struct device *dev, uint32_t id);
108 int dma_stm32_stop(const struct device *dev, uint32_t id);
109 int dma_stm32_get_status(const struct device *dev, uint32_t id,
110 				struct dma_status *stat);
111 #else
112 #define DMA_STM32_EXPORT_API static
113 #endif /* CONFIG_DMAMUX_STM32 */
114 
115 #endif /* DMA_STM32_H_*/
116