1 /*
2 * Copyright (c) 2019 Richard Osterloh <richard.osterloh@gmail.com>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define LOG_DOMAIN flash_stm32g4
8 #define LOG_LEVEL CONFIG_FLASH_LOG_LEVEL
9 #include <zephyr/logging/log.h>
10 LOG_MODULE_REGISTER(LOG_DOMAIN);
11
12 #include <zephyr/kernel.h>
13 #include <zephyr/device.h>
14 #include <string.h>
15 #include <zephyr/drivers/flash.h>
16 #include <zephyr/sys/barrier.h>
17 #include <zephyr/init.h>
18 #include <soc.h>
19 #include <stm32_ll_system.h>
20
21 #include "flash_stm32.h"
22
23 #define STM32G4_SERIES_MAX_FLASH 512
24 #define BANK2_OFFSET (KB(STM32G4_SERIES_MAX_FLASH) / 2)
25
26 /*
27 * offset and len must be aligned on 8 for write,
28 * positive and not beyond end of flash
29 */
flash_stm32_valid_range(const struct device * dev,off_t offset,uint32_t len,bool write)30 bool flash_stm32_valid_range(const struct device *dev, off_t offset,
31 uint32_t len,
32 bool write)
33 {
34
35 #if defined(FLASH_STM32_DBANK) && (CONFIG_FLASH_SIZE < STM32G4_SERIES_MAX_FLASH)
36 /*
37 * In case of bank1/2 discontinuity, the range should not
38 * start before bank2 and end beyond bank1 at the same time.
39 * Locations beyond bank2 are caught by flash_stm32_range_exists.
40 */
41 if ((offset < BANK2_OFFSET) && (offset + len > FLASH_SIZE / 2)) {
42 return 0;
43 }
44 #endif
45
46 if (write && !flash_stm32_valid_write(offset, len)) {
47 return false;
48 }
49 return flash_stm32_range_exists(dev, offset, len);
50 }
51
flush_cache(FLASH_TypeDef * regs)52 static inline void flush_cache(FLASH_TypeDef *regs)
53 {
54 if (regs->ACR & FLASH_ACR_DCEN) {
55 regs->ACR &= ~FLASH_ACR_DCEN;
56 /* Datasheet: DCRST: Data cache reset
57 * This bit can be written only when the data cache is disabled
58 */
59 regs->ACR |= FLASH_ACR_DCRST;
60 regs->ACR &= ~FLASH_ACR_DCRST;
61 regs->ACR |= FLASH_ACR_DCEN;
62 }
63
64 if (regs->ACR & FLASH_ACR_ICEN) {
65 regs->ACR &= ~FLASH_ACR_ICEN;
66 /* Datasheet: ICRST: Instruction cache reset :
67 * This bit can be written only when the instruction cache
68 * is disabled
69 */
70 regs->ACR |= FLASH_ACR_ICRST;
71 regs->ACR &= ~FLASH_ACR_ICRST;
72 regs->ACR |= FLASH_ACR_ICEN;
73 }
74 }
75
write_dword(const struct device * dev,off_t offset,uint64_t val)76 static int write_dword(const struct device *dev, off_t offset, uint64_t val)
77 {
78 volatile uint32_t *flash = (uint32_t *)(offset + FLASH_STM32_BASE_ADDRESS);
79 FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
80 #if defined(FLASH_STM32_DBANK)
81 bool dcache_enabled = false;
82 #endif /* FLASH_STM32_DBANK */
83 uint32_t tmp;
84 int rc;
85
86 /* if the control register is locked, do not fail silently */
87 if (regs->CR & FLASH_CR_LOCK) {
88 LOG_ERR("CR locked");
89 return -EIO;
90 }
91
92 /* Check that no Flash main memory operation is ongoing */
93 rc = flash_stm32_wait_flash_idle(dev);
94 if (rc < 0) {
95 return rc;
96 }
97
98 /* Check if this double word is erased and value isn't 0.
99 *
100 * It is allowed to write only zeros over an already written dword
101 * See 3.3.7 in reference manual.
102 */
103 if ((flash[0] != 0xFFFFFFFFUL ||
104 flash[1] != 0xFFFFFFFFUL) && val != 0UL) {
105 LOG_ERR("Word at offs %ld not erased", (long)offset);
106 return -EIO;
107 }
108
109 #if defined(FLASH_STM32_DBANK)
110 /*
111 * Disable the data cache to avoid the silicon errata ES0430 Rev 7 2.2.2:
112 * "Data cache might be corrupted during Flash memory read-while-write operation"
113 */
114 if (regs->ACR & FLASH_ACR_DCEN) {
115 dcache_enabled = true;
116 regs->ACR &= (~FLASH_ACR_DCEN);
117 }
118 #endif /* FLASH_STM32_DBANK */
119
120 /* Set the PG bit */
121 regs->CR |= FLASH_CR_PG;
122
123 /* Flush the register write */
124 tmp = regs->CR;
125
126 /* Perform the data write operation at the desired memory address */
127 flash[0] = (uint32_t)val;
128 flash[1] = (uint32_t)(val >> 32);
129
130 /* Wait until the BSY bit is cleared */
131 rc = flash_stm32_wait_flash_idle(dev);
132
133 /* Clear the PG bit */
134 regs->CR &= (~FLASH_CR_PG);
135
136 #if defined(FLASH_STM32_DBANK)
137 /* Reset/enable the data cache if previously enabled */
138 if (dcache_enabled) {
139 regs->ACR |= FLASH_ACR_DCRST;
140 regs->ACR &= (~FLASH_ACR_DCRST);
141 regs->ACR |= FLASH_ACR_DCEN;
142 }
143 #endif /* FLASH_STM32_DBANK */
144
145 return rc;
146 }
147
erase_page(const struct device * dev,unsigned int offset)148 static int erase_page(const struct device *dev, unsigned int offset)
149 {
150 FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
151 uint32_t tmp;
152 int rc;
153 int page;
154
155 /* if the control register is locked, do not fail silently */
156 if (regs->CR & FLASH_CR_LOCK) {
157 LOG_ERR("CR locked");
158 return -EIO;
159 }
160
161 /* Check that no Flash memory operation is ongoing */
162 rc = flash_stm32_wait_flash_idle(dev);
163 if (rc < 0) {
164 return rc;
165 }
166
167 #if defined(FLASH_STM32_DBANK)
168 bool bank_swap;
169 /* Check whether bank1/2 are swapped */
170 bank_swap = (LL_SYSCFG_GetFlashBankMode() == LL_SYSCFG_BANKMODE_BANK2);
171
172 if ((offset < (FLASH_SIZE / 2)) && !bank_swap) {
173 /* The pages to be erased is in bank 1 */
174 regs->CR &= ~FLASH_CR_BKER_Msk;
175 page = offset / FLASH_PAGE_SIZE;
176 LOG_DBG("Erase page %d on bank 1", page);
177 } else if ((offset >= BANK2_OFFSET) && bank_swap) {
178 /* The pages to be erased is in bank 1 */
179 regs->CR &= ~FLASH_CR_BKER_Msk;
180 page = (offset - BANK2_OFFSET) / FLASH_PAGE_SIZE;
181 LOG_DBG("Erase page %d on bank 1", page);
182 } else if ((offset < (FLASH_SIZE / 2)) && bank_swap) {
183 /* The pages to be erased is in bank 2 */
184 regs->CR |= FLASH_CR_BKER;
185 page = offset / FLASH_PAGE_SIZE;
186 LOG_DBG("Erase page %d on bank 2", page);
187 } else if ((offset >= BANK2_OFFSET) && !bank_swap) {
188 /* The pages to be erased is in bank 2 */
189 regs->CR |= FLASH_CR_BKER;
190 page = (offset - BANK2_OFFSET) / FLASH_PAGE_SIZE;
191 LOG_DBG("Erase page %d on bank 2", page);
192 } else {
193 LOG_ERR("Offset %d does not exist", offset);
194 return -EINVAL;
195 }
196 #else
197 page = offset / FLASH_PAGE_SIZE;
198 LOG_DBG("Erase page %d", page);
199 #endif
200
201 /* Set the PER bit and select the page you wish to erase */
202 regs->CR |= FLASH_CR_PER;
203 regs->CR &= ~FLASH_CR_PNB_Msk;
204 regs->CR |= (page << FLASH_CR_PNB_Pos);
205
206 /* Set the STRT bit */
207 regs->CR |= FLASH_CR_STRT;
208
209 /* flush the register write */
210 tmp = regs->CR;
211
212 /* Wait for the BSY bit */
213 rc = flash_stm32_wait_flash_idle(dev);
214
215 flush_cache(regs);
216
217 #ifdef FLASH_STM32_DBANK
218 regs->CR &= ~(FLASH_CR_PER | FLASH_CR_BKER);
219 #else
220 regs->CR &= ~(FLASH_CR_PER);
221 #endif
222
223 return rc;
224 }
225
flash_stm32_block_erase_loop(const struct device * dev,unsigned int offset,unsigned int len)226 int flash_stm32_block_erase_loop(const struct device *dev,
227 unsigned int offset,
228 unsigned int len)
229 {
230 unsigned int address = offset;
231 int rc = 0;
232
233 for (; address <= offset + len - 1 ; address += FLASH_PAGE_SIZE) {
234 rc = erase_page(dev, address);
235 if (rc < 0) {
236 break;
237 }
238 }
239
240 return rc;
241 }
242
flash_stm32_write_range(const struct device * dev,unsigned int offset,const void * data,unsigned int len)243 int flash_stm32_write_range(const struct device *dev, unsigned int offset,
244 const void *data, unsigned int len)
245 {
246 int i, rc = 0;
247
248 for (i = 0; i < len; i += 8, offset += 8) {
249 rc = write_dword(dev, offset, ((const uint64_t *) data)[i>>3]);
250 if (rc < 0) {
251 return rc;
252 }
253 }
254
255 return rc;
256 }
257
write_optb(const struct device * dev,uint32_t mask,uint32_t value)258 static __unused int write_optb(const struct device *dev, uint32_t mask,
259 uint32_t value)
260 {
261 FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
262 int rc;
263
264 if (regs->CR & FLASH_CR_OPTLOCK) {
265 return -EIO;
266 }
267
268 if ((regs->OPTR & mask) == value) {
269 return 0;
270 }
271
272 rc = flash_stm32_wait_flash_idle(dev);
273 if (rc < 0) {
274 return rc;
275 }
276
277 regs->OPTR = (regs->OPTR & ~mask) | value;
278 regs->CR |= FLASH_CR_OPTSTRT;
279
280 /* Make sure previous write is completed. */
281 barrier_dsync_fence_full();
282
283 rc = flash_stm32_wait_flash_idle(dev);
284 if (rc < 0) {
285 return rc;
286 }
287
288 return 0;
289 }
290
291 #if defined(CONFIG_FLASH_STM32_WRITE_PROTECT)
292
293 /*
294 * Remark for future development implementing Write Protection for the L4 parts:
295 *
296 * STM32L4 allows for 2 write protected memory areas, c.f. FLASH_WEP1AR, FLASH_WRP1BR
297 * which are defined by their start and end pages.
298 *
299 * Other STM32 parts (i.e. F4 series) uses bitmask to select sectors.
300 *
301 * To implement Write Protection for L4 one should thus add a new EX_OP like
302 * FLASH_STM32_EX_OP_SECTOR_WP_RANGED in stm32_flash_api_extensions.h
303 */
304
305 #endif /* CONFIG_FLASH_STM32_WRITE_PROTECT */
306
307 #if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION)
flash_stm32_update_rdp(const struct device * dev,bool enable,bool permanent)308 int flash_stm32_update_rdp(const struct device *dev, bool enable,
309 bool permanent)
310 {
311 FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
312 uint8_t current_level, target_level;
313
314 current_level =
315 (regs->OPTR & FLASH_OPTR_RDP_Msk) >> FLASH_OPTR_RDP_Pos;
316 target_level = current_level;
317
318 /*
319 * 0xAA = RDP level 0 (no protection)
320 * 0xCC = RDP level 2 (permanent protection)
321 * others = RDP level 1 (protection active)
322 */
323 switch (current_level) {
324 case FLASH_STM32_RDP2:
325 if (!enable || !permanent) {
326 LOG_ERR("RDP level 2 is permanent and can't be changed!");
327 return -ENOTSUP;
328 }
329 break;
330 case FLASH_STM32_RDP0:
331 if (enable) {
332 target_level = FLASH_STM32_RDP1;
333 if (permanent) {
334 #if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION_PERMANENT_ALLOW)
335 target_level = FLASH_STM32_RDP2;
336 #else
337 LOG_ERR("Permanent readout protection (RDP "
338 "level 0 -> 2) not allowed");
339 return -ENOTSUP;
340 #endif
341 }
342 }
343 break;
344 default: /* FLASH_STM32_RDP1 */
345 if (enable && permanent) {
346 #if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION_PERMANENT_ALLOW)
347 target_level = FLASH_STM32_RDP2;
348 #else
349 LOG_ERR("Permanent readout protection (RDP "
350 "level 1 -> 2) not allowed");
351 return -ENOTSUP;
352 #endif
353 }
354 if (!enable) {
355 #if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION_DISABLE_ALLOW)
356 target_level = FLASH_STM32_RDP0;
357 #else
358 LOG_ERR("Disabling readout protection (RDP "
359 "level 1 -> 0) not allowed");
360 return -EACCES;
361 #endif
362 }
363 }
364
365 /* Update RDP level if needed */
366 if (current_level != target_level) {
367 LOG_INF("RDP changed from 0x%02x to 0x%02x", current_level,
368 target_level);
369
370 write_optb(dev, FLASH_OPTR_RDP_Msk,
371 (uint32_t)target_level << FLASH_OPTR_RDP_Pos);
372 }
373 return 0;
374 }
375
flash_stm32_get_rdp(const struct device * dev,bool * enabled,bool * permanent)376 int flash_stm32_get_rdp(const struct device *dev, bool *enabled,
377 bool *permanent)
378 {
379 FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
380 uint8_t current_level;
381
382 current_level =
383 (regs->OPTR & FLASH_OPTR_RDP_Msk) >> FLASH_OPTR_RDP_Pos;
384
385 /*
386 * 0xAA = RDP level 0 (no protection)
387 * 0xCC = RDP level 2 (permanent protection)
388 * others = RDP level 1 (protection active)
389 */
390 switch (current_level) {
391 case FLASH_STM32_RDP2:
392 *enabled = true;
393 *permanent = true;
394 break;
395 case FLASH_STM32_RDP0:
396 *enabled = false;
397 *permanent = false;
398 break;
399 default: /* FLASH_STM32_RDP1 */
400 *enabled = true;
401 *permanent = false;
402 }
403 return 0;
404 }
405 #endif /* CONFIG_FLASH_STM32_READOUT_PROTECTION */
406
flash_stm32_page_layout(const struct device * dev,const struct flash_pages_layout ** layout,size_t * layout_size)407 void flash_stm32_page_layout(const struct device *dev,
408 const struct flash_pages_layout **layout,
409 size_t *layout_size)
410 {
411 ARG_UNUSED(dev);
412
413 #if defined(FLASH_STM32_DBANK) && (CONFIG_FLASH_SIZE < STM32G4_SERIES_MAX_FLASH)
414 #define PAGES_PER_BANK ((FLASH_SIZE / FLASH_PAGE_SIZE) / 2)
415 static struct flash_pages_layout stm32g4_flash_layout[3];
416
417 if (stm32g4_flash_layout[0].pages_count == 0) {
418 /* Bank1 */
419 stm32g4_flash_layout[0].pages_count = PAGES_PER_BANK;
420 stm32g4_flash_layout[0].pages_size = FLASH_PAGE_SIZE;
421 /* Dummy page corresponding to discontinuity between bank1/2 */
422 stm32g4_flash_layout[1].pages_count = 1;
423 stm32g4_flash_layout[1].pages_size = BANK2_OFFSET
424 - (PAGES_PER_BANK * FLASH_PAGE_SIZE);
425 /* Bank2 */
426 stm32g4_flash_layout[2].pages_count = PAGES_PER_BANK;
427 stm32g4_flash_layout[2].pages_size = FLASH_PAGE_SIZE;
428 }
429 #else
430 static struct flash_pages_layout stm32g4_flash_layout[1];
431
432 if (stm32g4_flash_layout[0].pages_count == 0) {
433 stm32g4_flash_layout[0].pages_count = FLASH_SIZE
434 / FLASH_PAGE_SIZE;
435 stm32g4_flash_layout[0].pages_size = FLASH_PAGE_SIZE;
436 }
437 #endif
438
439 *layout = stm32g4_flash_layout;
440 *layout_size = ARRAY_SIZE(stm32g4_flash_layout);
441 }
442
443 /* Override weak function */
flash_stm32_check_configuration(void)444 int flash_stm32_check_configuration(void)
445 {
446 #if defined(FLASH_STM32_DBANK)
447 if (READ_BIT(FLASH->OPTR, FLASH_STM32_DBANK) == 0U) {
448 /* Single bank not supported when dualbank is possible */
449 LOG_ERR("Single bank configuration not supported");
450 return -ENOTSUP;
451 }
452 #endif
453 return 0;
454 }
455