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 
69 
flash_stm32_sem_take(const struct device * dev)70 static inline void flash_stm32_sem_take(const struct device *dev)
71 {
72 	k_sem_take(&FLASH_STM32_PRIV(dev)->sem, K_FOREVER);
73 }
74 
flash_stm32_sem_give(const struct device * dev)75 static inline void flash_stm32_sem_give(const struct device *dev)
76 {
77 	k_sem_give(&FLASH_STM32_PRIV(dev)->sem);
78 }
79 
flash_stm32_read(const struct device * dev,off_t offset,void * data,size_t len)80 static int flash_stm32_read(const struct device *dev, off_t offset,
81 			    void *data,
82 			    size_t len)
83 {
84 	if (!flash_stm32_valid_range(dev, offset, len, false)) {
85 		LOG_ERR("Read range invalid. Offset: %p, len: %zu",
86 			(void *) offset, len);
87 		return -EINVAL;
88 	}
89 
90 	if (!len) {
91 		return 0;
92 	}
93 
94 	flash_stm32_sem_take(dev);
95 
96 	memcpy(data, (uint8_t *) FLASH_STM32_BASE_ADDRESS + offset, len);
97 
98 	flash_stm32_sem_give(dev);
99 
100 	return 0;
101 }
102 
flash_stm32_erase(const struct device * dev,off_t offset,size_t len)103 static int flash_stm32_erase(const struct device *dev, off_t offset,
104 			     size_t len)
105 {
106 	int rc;
107 	int sect_num = (len / FLASH_PAGE_SIZE) + 1;
108 
109 	if (!flash_stm32_valid_range(dev, offset, len, true)) {
110 		LOG_ERR("Erase range invalid. Offset: %p, len: %zu",
111 			(void *)offset, len);
112 		return -EINVAL;
113 	}
114 
115 	if (!len) {
116 		return 0;
117 	}
118 
119 	flash_stm32_sem_take(dev);
120 
121 	LOG_DBG("Erase offset: %p, page: %ld, len: %zu, sect num: %d",
122 		(void *)offset, offset / FLASH_PAGE_SIZE, len, sect_num);
123 
124 	rc = FM_Erase(offset / FLASH_PAGE_SIZE, sect_num, &cb_ptr);
125 	if (rc == 0) {
126 		k_sem_take(&flash_busy, K_FOREVER);
127 	} else {
128 		LOG_DBG("Erase operation rejected. err = %d", rc);
129 	}
130 
131 	flash_stm32_sem_give(dev);
132 
133 	return rc;
134 }
135 
flash_stm32_write(const struct device * dev,off_t offset,const void * data,size_t len)136 static int flash_stm32_write(const struct device *dev, off_t offset,
137 			     const void *data, size_t len)
138 {
139 	int rc;
140 
141 	if (!flash_stm32_valid_range(dev, offset, len, true)) {
142 		LOG_ERR("Write range invalid. Offset: %p, len: %zu",
143 			(void *)offset, len);
144 		return -EINVAL;
145 	}
146 
147 	if (!len) {
148 		return 0;
149 	}
150 
151 	flash_stm32_sem_take(dev);
152 
153 	LOG_DBG("Write offset: %p, len: %zu", (void *)offset, len);
154 
155 	rc = FM_Write((uint32_t *)data,
156 		      (uint32_t *)(FLASH_STM32_BASE_ADDRESS + offset),
157 		      (int32_t)len/4, &cb_ptr);
158 	if (rc == 0) {
159 		k_sem_take(&flash_busy, K_FOREVER);
160 	} else {
161 		LOG_DBG("Write operation rejected. err = %d", rc);
162 	}
163 
164 	flash_stm32_sem_give(dev);
165 
166 	return rc;
167 }
168 
169 static const struct flash_parameters *
flash_stm32_get_parameters(const struct device * dev)170 			flash_stm32_get_parameters(const struct device *dev)
171 {
172 	ARG_UNUSED(dev);
173 
174 	return &flash_stm32_parameters;
175 }
176 
177 static struct flash_stm32_priv flash_data = {
178 	.regs = (FLASH_TypeDef *) DT_INST_REG_ADDR(0),
179 };
180 
flash_stm32wba_page_layout(const struct device * dev,const struct flash_pages_layout ** layout,size_t * layout_size)181 void flash_stm32wba_page_layout(const struct device *dev,
182 				const struct flash_pages_layout **layout,
183 				size_t *layout_size)
184 {
185 	static struct flash_pages_layout stm32wba_flash_layout = {
186 		.pages_count = 0,
187 		.pages_size = 0,
188 	};
189 
190 	ARG_UNUSED(dev);
191 
192 	if (stm32wba_flash_layout.pages_count == 0) {
193 		stm32wba_flash_layout.pages_count = FLASH_SIZE / FLASH_PAGE_SIZE;
194 		stm32wba_flash_layout.pages_size = FLASH_PAGE_SIZE;
195 	}
196 
197 	*layout = &stm32wba_flash_layout;
198 	*layout_size = 1;
199 }
200 
201 static const struct flash_driver_api flash_stm32_api = {
202 	.erase = flash_stm32_erase,
203 	.write = flash_stm32_write,
204 	.read = flash_stm32_read,
205 	.get_parameters = flash_stm32_get_parameters,
206 #ifdef CONFIG_FLASH_PAGE_LAYOUT
207 	.page_layout = flash_stm32wba_page_layout,
208 #endif
209 };
210 
stm32_flash_init(const struct device * dev)211 static int stm32_flash_init(const struct device *dev)
212 {
213 	k_sem_init(&FLASH_STM32_PRIV(dev)->sem, 1, 1);
214 
215 	LOG_DBG("Flash initialized. BS: %zu",
216 		flash_stm32_parameters.write_block_size);
217 
218 	k_work_init(&fm_work, &FM_BackgroundProcess_Entry);
219 
220 	/* Enable flash driver system flag */
221 	FD_SetStatus(FD_FLASHACCESS_RFTS, LL_FLASH_DISABLE);
222 	FD_SetStatus(FD_FLASHACCESS_RFTS_BYPASS, LL_FLASH_ENABLE);
223 	FD_SetStatus(FD_FLASHACCESS_SYSTEM, LL_FLASH_ENABLE);
224 
225 #if ((CONFIG_FLASH_LOG_LEVEL >= LOG_LEVEL_DBG) && CONFIG_FLASH_PAGE_LAYOUT)
226 	const struct flash_pages_layout *layout;
227 	size_t layout_size;
228 
229 	flash_stm32wba_page_layout(dev, &layout, &layout_size);
230 	for (size_t i = 0; i < layout_size; i++) {
231 		LOG_DBG("Block %zu: bs: %zu count: %zu", i,
232 			layout[i].pages_size, layout[i].pages_count);
233 	}
234 #endif
235 
236 	return 0;
237 }
238 
239 DEVICE_DT_INST_DEFINE(0, stm32_flash_init, NULL,
240 		    &flash_data, NULL, POST_KERNEL,
241 		    CONFIG_FLASH_INIT_PRIORITY, &flash_stm32_api);
242