@@ -30,6 +30,7 @@ LOG_MODULE_REGISTER(flash_stm32_ospi, CONFIG_FLASH_LOG_LEVEL);
30
30
#define STM32_OSPI_RESET_GPIO DT_INST_NODE_HAS_PROP(0, reset_gpios)
31
31
32
32
#define STM32_OSPI_USE_DMA DT_NODE_HAS_PROP(DT_PARENT(DT_DRV_INST(0)), dmas)
33
+
33
34
#if STM32_OSPI_USE_DMA
34
35
#include <zephyr/drivers/dma/dma_stm32.h>
35
36
#include <zephyr/drivers/dma.h>
@@ -50,6 +51,27 @@ LOG_MODULE_REGISTER(flash_stm32_ospi, CONFIG_FLASH_LOG_LEVEL);
50
51
#define SPI_NOR_WRITEOC_NONE 0xFF
51
52
52
53
#if STM32_OSPI_USE_DMA
54
+ #if CONFIG_DMA_STM32U5
55
+ static const uint32_t table_src_size [] = {
56
+ LL_DMA_SRC_DATAWIDTH_BYTE ,
57
+ LL_DMA_SRC_DATAWIDTH_HALFWORD ,
58
+ LL_DMA_SRC_DATAWIDTH_WORD ,
59
+ };
60
+
61
+ static const uint32_t table_dest_size [] = {
62
+ LL_DMA_DEST_DATAWIDTH_BYTE ,
63
+ LL_DMA_DEST_DATAWIDTH_HALFWORD ,
64
+ LL_DMA_DEST_DATAWIDTH_WORD ,
65
+ };
66
+
67
+ /* Lookup table to set dma priority from the DTS */
68
+ static const uint32_t table_priority [] = {
69
+ LL_DMA_LOW_PRIORITY_LOW_WEIGHT ,
70
+ LL_DMA_LOW_PRIORITY_MID_WEIGHT ,
71
+ LL_DMA_LOW_PRIORITY_HIGH_WEIGHT ,
72
+ LL_DMA_HIGH_PRIORITY ,
73
+ };
74
+ #else
53
75
static const uint32_t table_m_size [] = {
54
76
LL_DMA_MDATAALIGN_BYTE ,
55
77
LL_DMA_MDATAALIGN_HALFWORD ,
@@ -69,6 +91,7 @@ static const uint32_t table_priority[] = {
69
91
DMA_PRIORITY_HIGH ,
70
92
DMA_PRIORITY_VERY_HIGH ,
71
93
};
94
+ #endif /* CONFIG_DMA_STM32U5 */
72
95
73
96
struct stream {
74
97
DMA_TypeDef * reg ;
@@ -126,7 +149,7 @@ struct flash_stm32_ospi_data {
126
149
int cmd_status ;
127
150
#if STM32_OSPI_USE_DMA
128
151
struct stream dma ;
129
- #endif
152
+ #endif /* STM32_OSPI_USE_DMA */
130
153
};
131
154
132
155
static inline void ospi_lock_thread (const struct device * dev )
@@ -227,7 +250,7 @@ static int ospi_write_access(const struct device *dev, OSPI_RegularCmdTypeDef *c
227
250
#endif
228
251
229
252
if (hal_ret != HAL_OK ) {
230
- LOG_ERR ("%d: Failed to read data" , hal_ret );
253
+ LOG_ERR ("%d: Failed to write data" , hal_ret );
231
254
return - EIO ;
232
255
}
233
256
@@ -1053,18 +1076,19 @@ static int flash_stm32_ospi_write(const struct device *dev, off_t addr,
1053
1076
LOG_DBG ("OSPI: write %zu data" , size );
1054
1077
ospi_lock_thread (dev );
1055
1078
1079
+ ret = stm32_ospi_mem_ready (& dev_data -> hospi ,
1080
+ dev_cfg -> data_mode , dev_cfg -> data_rate );
1081
+ if (ret != 0 ) {
1082
+ LOG_ERR ("OSPI: write not ready" );
1083
+ return - EIO ;
1084
+ }
1085
+
1056
1086
while ((size > 0 ) && (ret == 0 )) {
1057
1087
to_write = size ;
1058
-
1059
- ret = stm32_ospi_mem_ready (& dev_data -> hospi ,
1060
- dev_cfg -> data_mode , dev_cfg -> data_rate );
1061
- if (ret != 0 ) {
1062
- break ;
1063
- }
1064
-
1065
1088
ret = stm32_ospi_write_enable (& dev_data -> hospi ,
1066
1089
dev_cfg -> data_mode , dev_cfg -> data_rate );
1067
1090
if (ret != 0 ) {
1091
+ LOG_ERR ("OSPI: write not enabled" );
1068
1092
break ;
1069
1093
}
1070
1094
/* Don't write more than a page. */
@@ -1082,6 +1106,7 @@ static int flash_stm32_ospi_write(const struct device *dev, off_t addr,
1082
1106
1083
1107
ret = ospi_write_access (dev , & cmd_pp , data , to_write );
1084
1108
if (ret != 0 ) {
1109
+ LOG_ERR ("OSPI: write not access" );
1085
1110
break ;
1086
1111
}
1087
1112
@@ -1093,6 +1118,7 @@ static int flash_stm32_ospi_write(const struct device *dev, off_t addr,
1093
1118
ret = stm32_ospi_mem_ready (& dev_data -> hospi ,
1094
1119
dev_cfg -> data_mode , dev_cfg -> data_rate );
1095
1120
if (ret != 0 ) {
1121
+ LOG_ERR ("OSPI: write PP not ready" );
1096
1122
break ;
1097
1123
}
1098
1124
}
@@ -1685,8 +1711,11 @@ static int flash_stm32_ospi_init(const struct device *dev)
1685
1711
/* HACK: This field is used to inform driver that it is overridden */
1686
1712
dma_cfg .linked_channel = STM32_DMA_HAL_OVERRIDE ;
1687
1713
/* Because of the STREAM OFFSET, the DMA channel given here is from 1 - 8 */
1688
- ret = dma_config (dev_data -> dma .dev , dev_data -> dma .channel + 1 , & dma_cfg );
1714
+ ret = dma_config (dev_data -> dma .dev ,
1715
+ (dev_data -> dma .channel + STM32_DMA_STREAM_OFFSET ), & dma_cfg );
1689
1716
if (ret != 0 ) {
1717
+ LOG_ERR ("Failed to configure DMA channel %d" ,
1718
+ dev_data -> dma .channel + STM32_DMA_STREAM_OFFSET );
1690
1719
return ret ;
1691
1720
}
1692
1721
@@ -1698,29 +1727,55 @@ static int flash_stm32_ospi_init(const struct device *dev)
1698
1727
1699
1728
int index = find_lsb_set (dma_cfg .source_data_size ) - 1 ;
1700
1729
1730
+ #if CONFIG_DMA_STM32U5
1731
+ /* Fill the structure for dma init */
1732
+ hdma .Init .BlkHWRequest = DMA_BREQ_SINGLE_BURST ;
1733
+ hdma .Init .SrcInc = DMA_SINC_FIXED ;
1734
+ hdma .Init .DestInc = DMA_DINC_INCREMENTED ;
1735
+ hdma .Init .SrcDataWidth = table_src_size [index ];
1736
+ hdma .Init .DestDataWidth = table_dest_size [index ];
1737
+ hdma .Init .SrcBurstLength = 4 ;
1738
+ hdma .Init .DestBurstLength = 4 ;
1739
+ hdma .Init .TransferAllocatedPort = DMA_SRC_ALLOCATED_PORT0 | DMA_DEST_ALLOCATED_PORT1 ;
1740
+ hdma .Init .TransferEventMode = DMA_TCEM_BLOCK_TRANSFER ;
1741
+ #else
1701
1742
hdma .Init .PeriphDataAlignment = table_p_size [index ];
1702
1743
hdma .Init .MemDataAlignment = table_m_size [index ];
1703
1744
hdma .Init .PeriphInc = DMA_PINC_DISABLE ;
1704
1745
hdma .Init .MemInc = DMA_MINC_ENABLE ;
1746
+ #endif /* CONFIG_DMA_STM32U5 */
1705
1747
hdma .Init .Mode = DMA_NORMAL ;
1706
1748
hdma .Init .Priority = table_priority [dma_cfg .channel_priority ];
1707
1749
hdma .Init .Direction = DMA_PERIPH_TO_MEMORY ;
1750
+ #ifdef CONFIG_DMA_STM32_V1
1751
+ /* TODO: Not tested in this configuration */
1752
+ hdma .Init .Channel = dma_cfg .dma_slot ;
1753
+ hdma .Instance = __LL_DMA_GET_STREAM_INSTANCE (dev_data -> dma .reg ,
1754
+ dev_data -> dma .channel );
1755
+ #else
1708
1756
hdma .Init .Request = dma_cfg .dma_slot ;
1709
- #ifdef CONFIG_DMAMUX_STM32
1757
+ #if CONFIG_DMA_STM32U5
1758
+ hdma .Instance = LL_DMA_GET_CHANNEL_INSTANCE (dev_data -> dma .reg ,
1759
+ dev_data -> dma .channel );
1760
+ #elif defined(CONFIG_DMAMUX_STM32 )
1710
1761
/*
1711
1762
* HAL expects a valid DMA channel (not DMAMUX).
1712
- * The channel is from 0 to 7 because of the STREAM_OFFSET in the dma_stm32 driver
1763
+ * The channel is from 0 to 7 because of the STM32_DMA_STREAM_OFFSET in the dma_stm32 driver
1713
1764
*/
1714
1765
hdma .Instance = __LL_DMA_GET_CHANNEL_INSTANCE (dev_data -> dma .reg ,
1715
1766
dev_data -> dma .channel );
1716
1767
#else
1717
1768
hdma .Instance = __LL_DMA_GET_CHANNEL_INSTANCE (dev_data -> dma .reg ,
1718
1769
dev_data -> dma .channel - 1 );
1719
- #endif /* CONFIG_DMAMUX_STM32 */
1770
+ #endif /* CONFIG_DMA_STM32U5 */
1771
+ #endif /* CONFIG_DMA_STM32_V1 */
1720
1772
1721
1773
/* Initialize DMA HAL */
1722
1774
__HAL_LINKDMA (& dev_data -> hospi , hdma , hdma );
1723
- HAL_DMA_Init (& hdma );
1775
+ if (HAL_DMA_Init (& hdma ) != HAL_OK ) {
1776
+ LOG_ERR ("OSPI DMA Init failed" );
1777
+ return - EIO ;
1778
+ }
1724
1779
LOG_INF ("OSPI with DMA transfer" );
1725
1780
1726
1781
#endif /* STM32_OSPI_USE_DMA */
0 commit comments