1 /*
2 * Copyright (c) 2023 STMicroelectronics
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/init.h>
10 #include <zephyr/drivers/flash.h>
11
12 #define DT_DRV_COMPAT st_stm32wba_flash_controller
13
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_REGISTER(flash_stm32wba, CONFIG_FLASH_LOG_LEVEL);
16
17 #include "flash_stm32.h"
18 #include "flash_manager.h"
19 #include "flash_driver.h"
20
21 /* Let's wait for double the max erase time to be sure that the operation is
22 * completed.
23 */
24 #define STM32_FLASH_TIMEOUT \
25 (2 * DT_PROP(DT_INST(0, st_stm32_nv_flash), max_erase_time))
26
27 extern struct k_work_q ble_ctlr_work_q;
28 struct k_work fm_work;
29
30 static const struct flash_parameters flash_stm32_parameters = {
31 .write_block_size = FLASH_STM32_WRITE_BLOCK_SIZE,
32 .erase_value = 0xff,
33 };
34
35 K_SEM_DEFINE(flash_busy, 0, 1);
36
flash_callback(FM_FlashOp_Status_t status)37 static void flash_callback(FM_FlashOp_Status_t status)
38 {
39 LOG_DBG("%d", status);
40
41 k_sem_give(&flash_busy);
42 }
43
44 struct FM_CallbackNode cb_ptr = {
45 .Callback = flash_callback
46 };
47
FM_ProcessRequest(void)48 void FM_ProcessRequest(void)
49 {
50 k_work_submit_to_queue(&ble_ctlr_work_q, &fm_work);
51 }
52
FM_BackgroundProcess_Entry(struct k_work * work)53 void FM_BackgroundProcess_Entry(struct k_work *work)
54 {
55 ARG_UNUSED(work);
56
57 FM_BackgroundProcess();
58 }
59
flash_stm32_valid_range(const struct device * dev,off_t offset,uint32_t len,bool write)60 bool flash_stm32_valid_range(const struct device *dev, off_t offset,
61 uint32_t len, bool write)
62 {
63 if (write && !flash_stm32_valid_write(offset, len)) {
64 return false;
65 }
66 return flash_stm32_range_exists(dev, offset, len);
67 }
68
flash_stm32_read(const struct device * dev,off_t offset,void * data,size_t len)69 static int flash_stm32_read(const struct device *dev, off_t offset,
70 void *data,
71 size_t len)
72 {
73 if (!flash_stm32_valid_range(dev, offset, len, false)) {
74 LOG_ERR("Read range invalid. Offset: %p, len: %zu",
75 (void *) offset, len);
76 return -EINVAL;
77 }
78
79 if (!len) {
80 return 0;
81 }
82
83 flash_stm32_sem_take(dev);
84
85 memcpy(data, (uint8_t *) FLASH_STM32_BASE_ADDRESS + offset, len);
86
87 flash_stm32_sem_give(dev);
88
89 return 0;
90 }
91
flash_stm32_erase(const struct device * dev,off_t offset,size_t len)92 static int flash_stm32_erase(const struct device *dev, off_t offset,
93 size_t len)
94 {
95 int rc;
96 int sect_num = (len / FLASH_PAGE_SIZE) + 1;
97
98 if (!flash_stm32_valid_range(dev, offset, len, true)) {
99 LOG_ERR("Erase range invalid. Offset: %p, len: %zu",
100 (void *)offset, len);
101 return -EINVAL;
102 }
103
104 if (!len) {
105 return 0;
106 }
107
108 flash_stm32_sem_take(dev);
109
110 LOG_DBG("Erase offset: %p, page: %ld, len: %zu, sect num: %d",
111 (void *)offset, offset / FLASH_PAGE_SIZE, len, sect_num);
112
113 rc = FM_Erase(offset / FLASH_PAGE_SIZE, sect_num, &cb_ptr);
114 if (rc == 0) {
115 k_sem_take(&flash_busy, K_FOREVER);
116 } else {
117 LOG_DBG("Erase operation rejected. err = %d", rc);
118 }
119
120 flash_stm32_sem_give(dev);
121
122 return rc;
123 }
124
flash_stm32_write(const struct device * dev,off_t offset,const void * data,size_t len)125 static int flash_stm32_write(const struct device *dev, off_t offset,
126 const void *data, size_t len)
127 {
128 int rc;
129
130 if (!flash_stm32_valid_range(dev, offset, len, true)) {
131 LOG_ERR("Write range invalid. Offset: %p, len: %zu",
132 (void *)offset, len);
133 return -EINVAL;
134 }
135
136 if (!len) {
137 return 0;
138 }
139
140 flash_stm32_sem_take(dev);
141
142 LOG_DBG("Write offset: %p, len: %zu", (void *)offset, len);
143
144 rc = FM_Write((uint32_t *)data,
145 (uint32_t *)(FLASH_STM32_BASE_ADDRESS + offset),
146 (int32_t)len/4, &cb_ptr);
147 if (rc == 0) {
148 k_sem_take(&flash_busy, K_FOREVER);
149 } else {
150 LOG_DBG("Write operation rejected. err = %d", rc);
151 }
152
153 flash_stm32_sem_give(dev);
154
155 return rc;
156 }
157
158 static const struct flash_parameters *
flash_stm32_get_parameters(const struct device * dev)159 flash_stm32_get_parameters(const struct device *dev)
160 {
161 ARG_UNUSED(dev);
162
163 return &flash_stm32_parameters;
164 }
165
166 static struct flash_stm32_priv flash_data = {
167 .regs = (FLASH_TypeDef *) DT_INST_REG_ADDR(0),
168 };
169
flash_stm32wba_page_layout(const struct device * dev,const struct flash_pages_layout ** layout,size_t * layout_size)170 void flash_stm32wba_page_layout(const struct device *dev,
171 const struct flash_pages_layout **layout,
172 size_t *layout_size)
173 {
174 static struct flash_pages_layout stm32wba_flash_layout = {
175 .pages_count = 0,
176 .pages_size = 0,
177 };
178
179 ARG_UNUSED(dev);
180
181 if (stm32wba_flash_layout.pages_count == 0) {
182 stm32wba_flash_layout.pages_count = FLASH_SIZE / FLASH_PAGE_SIZE;
183 stm32wba_flash_layout.pages_size = FLASH_PAGE_SIZE;
184 }
185
186 *layout = &stm32wba_flash_layout;
187 *layout_size = 1;
188 }
189
190 static DEVICE_API(flash, flash_stm32_api) = {
191 .erase = flash_stm32_erase,
192 .write = flash_stm32_write,
193 .read = flash_stm32_read,
194 .get_parameters = flash_stm32_get_parameters,
195 #ifdef CONFIG_FLASH_PAGE_LAYOUT
196 .page_layout = flash_stm32wba_page_layout,
197 #endif
198 };
199
stm32_flash_init(const struct device * dev)200 static int stm32_flash_init(const struct device *dev)
201 {
202 k_sem_init(&FLASH_STM32_PRIV(dev)->sem, 1, 1);
203
204 LOG_DBG("Flash initialized. BS: %zu",
205 flash_stm32_parameters.write_block_size);
206
207 k_work_init(&fm_work, &FM_BackgroundProcess_Entry);
208
209 /* Enable flash driver system flag */
210 FD_SetStatus(FD_FLASHACCESS_RFTS, LL_FLASH_DISABLE);
211 FD_SetStatus(FD_FLASHACCESS_RFTS_BYPASS, LL_FLASH_ENABLE);
212 FD_SetStatus(FD_FLASHACCESS_SYSTEM, LL_FLASH_ENABLE);
213
214 #if ((CONFIG_FLASH_LOG_LEVEL >= LOG_LEVEL_DBG) && CONFIG_FLASH_PAGE_LAYOUT)
215 const struct flash_pages_layout *layout;
216 size_t layout_size;
217
218 flash_stm32wba_page_layout(dev, &layout, &layout_size);
219 for (size_t i = 0; i < layout_size; i++) {
220 LOG_DBG("Block %zu: bs: %zu count: %zu", i,
221 layout[i].pages_size, layout[i].pages_count);
222 }
223 #endif
224
225 return 0;
226 }
227
228 DEVICE_DT_INST_DEFINE(0, stm32_flash_init, NULL,
229 &flash_data, NULL, POST_KERNEL,
230 CONFIG_FLASH_INIT_PRIORITY, &flash_stm32_api);
231