|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 EXALT Technologies. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include "spi_nor.h" |
| 8 | + |
| 9 | +#ifndef ZEPHYR_DRIVERS_MSPI_STM32_H_ |
| 10 | +#define ZEPHYR_DRIVERS_MSPI_STM32_H_ |
| 11 | +/* Macro to check if any xspi device has a domain clock or more */ |
| 12 | +#define MSPI_STM32_DOMAIN_CLOCK_INST_SUPPORT(inst) DT_CLOCKS_HAS_IDX(DT_INST_PARENT(inst), 1) || |
| 13 | +#define MSPI_STM32_INST_DEV_DOMAIN_CLOCK_SUPPORT \ |
| 14 | + (DT_INST_FOREACH_STATUS_OKAY(MSPI_STM32_DOMAIN_CLOCK_INST_SUPPORT) 0) |
| 15 | + |
| 16 | +/* This symbol takes the value 1 if device instance has a domain clock in its dts */ |
| 17 | +#if MSPI_STM32_INST_DEV_DOMAIN_CLOCK_SUPPORT |
| 18 | +#define MSPI_STM32_DOMAIN_CLOCK_SUPPORT 1 |
| 19 | +#else |
| 20 | +#define MSPI_STM32_DOMAIN_CLOCK_SUPPORT 0 |
| 21 | +#endif |
| 22 | + |
| 23 | +#define MSPI_STM32_FIFO_THRESHOLD 4U |
| 24 | +#define MSPI_MAX_FREQ 250000000 |
| 25 | +#define MSPI_MAX_DEVICE 2 |
| 26 | + |
| 27 | +#if defined(CONFIG_SOC_SERIES_STM32U5X) |
| 28 | +/* Valid range is [1, 256] */ |
| 29 | +#define MSPI_STM32_CLOCK_PRESCALER_MIN 1U |
| 30 | +#define MSPI_STM32_CLOCK_PRESCALER_MAX 256U |
| 31 | +#define MSPI_STM32_CLOCK_COMPUTE(bus_freq, prescaler) ((bus_freq) / (prescaler)) |
| 32 | +#else |
| 33 | +/* Valid range is [0, 255] */ |
| 34 | +#define MSPI_STM32_CLOCK_PRESCALER_MIN 0U |
| 35 | +#define MSPI_STM32_CLOCK_PRESCALER_MAX 255U |
| 36 | +#define MSPI_STM32_CLOCK_COMPUTE(bus_freq, prescaler) ((bus_freq) / ((prescaler) + 1U)) |
| 37 | +#endif |
| 38 | + |
| 39 | +#define MSPI_STM32_WRITE_REG_MAX_TIME 40U |
| 40 | +#define MSPI_STM32_MAX_FREQ 48000000 |
| 41 | + |
| 42 | + |
| 43 | +typedef void (*irq_config_func_t)(void); |
| 44 | + |
| 45 | +#ifndef DMA_LOW_PRIORITY_LOW_WEIGHT |
| 46 | +#define DMA_LOW_PRIORITY_LOW_WEIGHT DMA_PRIORITY_LOW |
| 47 | +#endif |
| 48 | + |
| 49 | +#ifndef DMA_LOW_PRIORITY_MID_WEIGHT |
| 50 | +#define DMA_LOW_PRIORITY_MID_WEIGHT DMA_PRIORITY_MEDIUM |
| 51 | +#endif |
| 52 | + |
| 53 | +#ifndef DMA_LOW_PRIORITY_HIGH_WEIGHT |
| 54 | +#define DMA_LOW_PRIORITY_HIGH_WEIGHT DMA_PRIORITY_HIGH |
| 55 | +#endif |
| 56 | + |
| 57 | +#ifndef DMA_HIGH_PRIORITY |
| 58 | +#define DMA_HIGH_PRIORITY DMA_PRIORITY_VERY_HIGH |
| 59 | +#endif |
| 60 | + |
| 61 | +enum mspi_access_mode { |
| 62 | + MSPI_ACCESS_ASYNC = 1, |
| 63 | + MSPI_ACCESS_SYNC = 2, |
| 64 | + MSPI_ACCESS_DMA = 3 |
| 65 | +}; |
| 66 | + |
| 67 | +struct mspi_context { |
| 68 | + struct mspi_xfer xfer; |
| 69 | + int packets_left; |
| 70 | + struct k_sem lock; |
| 71 | +}; |
| 72 | + |
| 73 | +struct mspi_stm32_conf { |
| 74 | + bool use_dma; |
| 75 | + size_t pclk_len; |
| 76 | + irq_config_func_t irq_config; |
| 77 | + struct mspi_cfg mspicfg; |
| 78 | + const struct stm32_pclken *pclken; |
| 79 | + const struct pinctrl_dev_config *pcfg; |
| 80 | +}; |
| 81 | + |
| 82 | +/* Lookup table to set dma priority from the DTS */ |
| 83 | +static const uint32_t table_priority[] = { |
| 84 | + DMA_LOW_PRIORITY_LOW_WEIGHT, |
| 85 | + DMA_LOW_PRIORITY_MID_WEIGHT, |
| 86 | + DMA_LOW_PRIORITY_HIGH_WEIGHT, |
| 87 | + DMA_HIGH_PRIORITY, |
| 88 | +}; |
| 89 | + |
| 90 | +/* Lookup table to set dma channel direction from the DTS */ |
| 91 | +static const uint32_t table_direction[] = { |
| 92 | + DMA_MEMORY_TO_MEMORY, |
| 93 | + DMA_MEMORY_TO_PERIPH, |
| 94 | + DMA_PERIPH_TO_MEMORY, |
| 95 | +}; |
| 96 | + |
| 97 | +static const uint32_t table_src_size[] = { |
| 98 | + #ifndef LL_DMA_SRC_DATAWIDTH_BYTE |
| 99 | + #define LL_DMA_SRC_DATAWIDTH_BYTE LL_DMA_MDATAALIGN_BYTE |
| 100 | + #endif |
| 101 | + #ifndef LL_DMA_SRC_DATAWIDTH_HALFWORD |
| 102 | + #define LL_DMA_SRC_DATAWIDTH_HALFWORD LL_DMA_MDATAALIGN_HALFWORD |
| 103 | + #endif |
| 104 | + #ifndef LL_DMA_SRC_DATAWIDTH_WORD |
| 105 | + #define LL_DMA_SRC_DATAWIDTH_WORD LL_DMA_MDATAALIGN_WORD |
| 106 | + #endif |
| 107 | +}; |
| 108 | + |
| 109 | +static const uint32_t table_dest_size[] = { |
| 110 | + #ifndef LL_DMA_DEST_DATAWIDTH_BYTE |
| 111 | + #define LL_DMA_DEST_DATAWIDTH_BYTE LL_DMA_PDATAALIGN_BYTE |
| 112 | + #endif |
| 113 | + #ifndef LL_DMA_DEST_DATAWIDTH_HALFWORD |
| 114 | + #define LL_DMA_DEST_DATAWIDTH_HALFWORD LL_DMA_PDATAALIGN_HALFWORD |
| 115 | + #endif |
| 116 | + #ifndef LL_DMA_DEST_DATAWIDTH_WORD |
| 117 | + #define LL_DMA_DEST_DATAWIDTH_WORD LL_DMA_PDATAALIGN_WORD |
| 118 | + #endif |
| 119 | +}; |
| 120 | + |
| 121 | +struct stream { |
| 122 | + DMA_TypeDef *reg; |
| 123 | + const struct device *dev; |
| 124 | + uint32_t channel; |
| 125 | + struct dma_config cfg; |
| 126 | + uint8_t priority; |
| 127 | + bool src_addr_increment; |
| 128 | + bool dst_addr_increment; |
| 129 | +}; |
| 130 | + |
| 131 | +union hmspi_handle { |
| 132 | +#ifdef CONFIG_MSPI_STM32_XSPI |
| 133 | + XSPI_HandleTypeDef xspi; |
| 134 | +#endif |
| 135 | +#ifdef CONFIG_MSPI_STM32_OSPI |
| 136 | + OSPI_HandleTypeDef ospi; |
| 137 | +#endif |
| 138 | +#ifdef CONFIG_MSPI_STM32_QSPI |
| 139 | + QSPI_HandleTypeDef qspi; |
| 140 | +#endif |
| 141 | +}; |
| 142 | + |
| 143 | +/* mspi data includes the controller specific config variable */ |
| 144 | +struct mspi_stm32_data { |
| 145 | + union hmspi_handle hmspi; |
| 146 | + uint32_t memmap_base_addr; |
| 147 | + struct mspi_context ctx; |
| 148 | + struct mspi_dev_id *dev_id; |
| 149 | + struct k_mutex lock; |
| 150 | + struct k_sem sync; |
| 151 | + struct mspi_dev_cfg dev_cfg; |
| 152 | + struct mspi_xip_cfg xip_cfg; |
| 153 | + struct stream dma_tx; |
| 154 | + struct stream dma_rx; |
| 155 | + struct stream dma; |
| 156 | +}; |
| 157 | + |
| 158 | +#endif /* ZEPHYR_DRIVERS_MSPI_STM32_H_ */ |
0 commit comments