1 /*
2  * Copyright (c) 2023 Jeroen van Dooren, Nobleo Technology
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef BDMA_STM32_H_
8 #define BDMA_STM32_H_
9 
10 #include <soc.h>
11 #include <stm32_ll_bdma.h>
12 #include <zephyr/drivers/dma.h>
13 #include <zephyr/drivers/clock_control/stm32_clock_control.h>
14 #include <zephyr/drivers/dma/dma_stm32.h>
15 
16 /* Maximum data sent in single transfer (Bytes) */
17 #define BDMA_STM32_MAX_DATA_ITEMS	0xffff
18 
19 struct bdma_stm32_channel {
20 	uint32_t direction;
21 #ifdef CONFIG_DMAMUX_STM32
22 	int mux_channel; /* stores the dmamux channel */
23 #endif /* CONFIG_DMAMUX_STM32 */
24 	bool source_periph;
25 	bool hal_override;
26 	volatile bool busy;
27 	uint32_t src_size;
28 	uint32_t dst_size;
29 	void *user_data; /* holds the client data */
30 	dma_callback_t bdma_callback;
31 	bool cyclic;
32 };
33 
34 struct bdma_stm32_data {
35 	struct dma_context dma_ctx;
36 };
37 
38 struct bdma_stm32_config {
39 	struct stm32_pclken pclken;
40 	void (*config_irq)(const struct device *dev);
41 	bool support_m2m;
42 	uint32_t base;
43 	uint32_t max_channels;
44 #ifdef CONFIG_DMAMUX_STM32
45 	uint8_t offset; /* position in the list of bdmamux channel list */
46 #endif
47 	struct bdma_stm32_channel *channels;
48 };
49 
50 uint32_t bdma_stm32_id_to_channel(uint32_t id);
51 #if !defined(CONFIG_DMAMUX_STM32)
52 uint32_t bdma_stm32_slot_to_channel(uint32_t id);
53 #endif
54 
55 typedef void (*bdma_stm32_clear_flag_func)(BDMA_TypeDef *DMAx);
56 typedef uint32_t (*bdma_stm32_check_flag_func)(const BDMA_TypeDef *DMAx);
57 
58 bool bdma_stm32_is_gi_active(BDMA_TypeDef *DMAx, uint32_t id);
59 void bdma_stm32_clear_gi(BDMA_TypeDef *DMAx, uint32_t id);
60 
61 void bdma_stm32_clear_tc(BDMA_TypeDef *DMAx, uint32_t id);
62 void bdma_stm32_clear_ht(BDMA_TypeDef *DMAx, uint32_t id);
63 bool bdma_stm32_is_te_active(BDMA_TypeDef *DMAx, uint32_t id);
64 void bdma_stm32_clear_te(BDMA_TypeDef *DMAx, uint32_t id);
65 
66 bool stm32_bdma_is_irq_active(BDMA_TypeDef *dma, uint32_t id);
67 bool stm32_bdma_is_ht_irq_active(BDMA_TypeDef *ma, uint32_t id);
68 bool stm32_bdma_is_tc_irq_active(BDMA_TypeDef *ma, uint32_t id);
69 
70 void stm32_bdma_dump_channel_irq(BDMA_TypeDef *dma, uint32_t id);
71 void stm32_bdma_clear_channel_irq(BDMA_TypeDef *dma, uint32_t id);
72 bool stm32_bdma_is_irq_happened(BDMA_TypeDef *dma, uint32_t id);
73 void stm32_bdma_enable_channel(BDMA_TypeDef *dma, uint32_t id);
74 int stm32_bdma_disable_channel(BDMA_TypeDef *dma, uint32_t id);
75 
76 #if !defined(CONFIG_DMAMUX_STM32)
77 void stm32_dma_config_channel_function(BDMA_TypeDef *dma, uint32_t id,
78 						uint32_t slot);
79 #endif
80 
81 #ifdef CONFIG_DMAMUX_STM32
82 /* bdma_stm32_ api functions are exported to the bdmamux_stm32 */
83 #define BDMA_STM32_EXPORT_API
84 int bdma_stm32_configure(const struct device *dev, uint32_t id,
85 				struct dma_config *config);
86 int bdma_stm32_reload(const struct device *dev, uint32_t id,
87 			uint32_t src, uint32_t dst, size_t size);
88 int bdma_stm32_start(const struct device *dev, uint32_t id);
89 int bdma_stm32_stop(const struct device *dev, uint32_t id);
90 int bdma_stm32_get_status(const struct device *dev, uint32_t id,
91 				struct dma_status *stat);
92 #else
93 #define BDMA_STM32_EXPORT_API static
94 #endif /* CONFIG_DMAMUX_STM32 */
95 
96 #endif /* BDMA_STM32_H_*/
97