1 /*
2 * Copyright (c) 2017 Linaro Limited
3 * Copyright (c) 2017 BayLibre, SAS.
4 * Copyright (c) 2019 Centaur Analytics, Inc
5 * Copyright (c) 2023 Google Inc
6 *
7 * SPDX-License-Identifier: Apache-2.0
8 */
9
10 #include <zephyr/kernel.h>
11 #include <zephyr/device.h>
12
13 #define DT_DRV_COMPAT st_stm32_flash_controller
14
15 #include <string.h>
16 #include <zephyr/drivers/flash.h>
17 #include <zephyr/drivers/flash/stm32_flash_api_extensions.h>
18 #include <zephyr/init.h>
19 #include <soc.h>
20 #include <stm32_ll_bus.h>
21 #include <stm32_ll_rcc.h>
22 #include <zephyr/logging/log.h>
23
24 #include "flash_stm32.h"
25 #include "stm32_hsem.h"
26
27 LOG_MODULE_REGISTER(flash_stm32, CONFIG_FLASH_LOG_LEVEL);
28
29 /* Let's wait for double the max erase time to be sure that the operation is
30 * completed.
31 */
32 #define STM32_FLASH_TIMEOUT \
33 (2 * DT_PROP(DT_INST(0, st_stm32_nv_flash), max_erase_time))
34
35 static const struct flash_parameters flash_stm32_parameters = {
36 .write_block_size = FLASH_STM32_WRITE_BLOCK_SIZE,
37 /* Some SoCs (L0/L1) use an EEPROM under the hood. Distinguish
38 * between them based on the presence of the PECR register. */
39 #if defined(FLASH_PECR_ERASE)
40 .erase_value = 0,
41 #else
42 .erase_value = 0xff,
43 #endif
44 };
45
46 static int flash_stm32_write_protection(const struct device *dev, bool enable);
47
flash_stm32_valid_range(const struct device * dev,off_t offset,uint32_t len,bool write)48 bool __weak flash_stm32_valid_range(const struct device *dev, off_t offset,
49 uint32_t len, bool write)
50 {
51 if (write && !flash_stm32_valid_write(offset, len)) {
52 return false;
53 }
54 return flash_stm32_range_exists(dev, offset, len);
55 }
56
flash_stm32_check_configuration(void)57 int __weak flash_stm32_check_configuration(void)
58 {
59 return 0;
60 }
61
62 #if defined(CONFIG_MULTITHREADING)
63 /*
64 * This is named flash_stm32_sem_take instead of flash_stm32_lock (and
65 * similarly for flash_stm32_sem_give) to avoid confusion with locking
66 * actual flash pages.
67 */
_flash_stm32_sem_take(const struct device * dev)68 static inline void _flash_stm32_sem_take(const struct device *dev)
69 {
70 k_sem_take(&FLASH_STM32_PRIV(dev)->sem, K_FOREVER);
71 z_stm32_hsem_lock(CFG_HW_FLASH_SEMID, HSEM_LOCK_WAIT_FOREVER);
72 }
73
_flash_stm32_sem_give(const struct device * dev)74 static inline void _flash_stm32_sem_give(const struct device *dev)
75 {
76 z_stm32_hsem_unlock(CFG_HW_FLASH_SEMID);
77 k_sem_give(&FLASH_STM32_PRIV(dev)->sem);
78 }
79
80 #define flash_stm32_sem_init(dev) k_sem_init(&FLASH_STM32_PRIV(dev)->sem, 1, 1)
81 #define flash_stm32_sem_take(dev) _flash_stm32_sem_take(dev)
82 #define flash_stm32_sem_give(dev) _flash_stm32_sem_give(dev)
83 #else
84 #define flash_stm32_sem_init(dev)
85 #define flash_stm32_sem_take(dev)
86 #define flash_stm32_sem_give(dev)
87 #endif
88
89 #if !defined(CONFIG_SOC_SERIES_STM32WBX)
flash_stm32_check_status(const struct device * dev)90 static int flash_stm32_check_status(const struct device *dev)
91 {
92
93 if (FLASH_STM32_REGS(dev)->FLASH_STM32_SR & FLASH_STM32_SR_ERRORS) {
94 LOG_DBG("Status: 0x%08lx",
95 FLASH_STM32_REGS(dev)->FLASH_STM32_SR &
96 FLASH_STM32_SR_ERRORS);
97 /* Clear errors to unblock usage of the flash */
98 FLASH_STM32_REGS(dev)->FLASH_STM32_SR = FLASH_STM32_REGS(dev)->FLASH_STM32_SR &
99 FLASH_STM32_SR_ERRORS;
100 return -EIO;
101 }
102
103 return 0;
104 }
105 #endif /* CONFIG_SOC_SERIES_STM32WBX */
106
flash_stm32_wait_flash_idle(const struct device * dev)107 int flash_stm32_wait_flash_idle(const struct device *dev)
108 {
109 int64_t timeout_time = k_uptime_get() + STM32_FLASH_TIMEOUT;
110 int rc;
111 uint32_t busy_flags;
112
113 rc = flash_stm32_check_status(dev);
114 if (rc < 0) {
115 return -EIO;
116 }
117
118 busy_flags = FLASH_STM32_SR_BUSY;
119
120 /* Some Series can't modify FLASH_CR reg while CFGBSY is set. Wait as well */
121 #if defined(FLASH_STM32_SR_CFGBSY)
122 busy_flags |= FLASH_STM32_SR_CFGBSY;
123 #endif
124
125 while ((FLASH_STM32_REGS(dev)->FLASH_STM32_SR & busy_flags)) {
126 if (k_uptime_get() > timeout_time) {
127 LOG_ERR("Timeout! val: %d", STM32_FLASH_TIMEOUT);
128 return -EIO;
129 }
130 }
131
132 return 0;
133 }
134
flash_stm32_flush_caches(const struct device * dev,off_t offset,size_t len)135 static void flash_stm32_flush_caches(const struct device *dev,
136 off_t offset, size_t len)
137 {
138 #if defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32F3X) || \
139 defined(CONFIG_SOC_SERIES_STM32G0X) || defined(CONFIG_SOC_SERIES_STM32L5X) || \
140 defined(CONFIG_SOC_SERIES_STM32U5X) || defined(CONFIG_SOC_SERIES_STM32H5X)
141 ARG_UNUSED(dev);
142 ARG_UNUSED(offset);
143 ARG_UNUSED(len);
144 #elif defined(CONFIG_SOC_SERIES_STM32F4X) || \
145 defined(CONFIG_SOC_SERIES_STM32L4X) || \
146 defined(CONFIG_SOC_SERIES_STM32WBX) || \
147 defined(CONFIG_SOC_SERIES_STM32G4X)
148 ARG_UNUSED(offset);
149 ARG_UNUSED(len);
150
151 FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
152
153 if (regs->ACR & FLASH_ACR_DCEN) {
154 regs->ACR &= ~FLASH_ACR_DCEN;
155 regs->ACR |= FLASH_ACR_DCRST;
156 regs->ACR &= ~FLASH_ACR_DCRST;
157 regs->ACR |= FLASH_ACR_DCEN;
158 }
159 #elif defined(CONFIG_SOC_SERIES_STM32F7X)
160 SCB_InvalidateDCache_by_Addr((uint32_t *)(FLASH_STM32_BASE_ADDRESS
161 + offset), len);
162 #endif
163 }
164
flash_stm32_read(const struct device * dev,off_t offset,void * data,size_t len)165 static int flash_stm32_read(const struct device *dev, off_t offset,
166 void *data,
167 size_t len)
168 {
169 if (!flash_stm32_valid_range(dev, offset, len, false)) {
170 LOG_ERR("Read range invalid. Offset: %ld, len: %zu",
171 (long int) offset, len);
172 return -EINVAL;
173 }
174
175 if (!len) {
176 return 0;
177 }
178
179 LOG_DBG("Read offset: %ld, len: %zu", (long int) offset, len);
180
181 memcpy(data, (uint8_t *) FLASH_STM32_BASE_ADDRESS + offset, len);
182
183 return 0;
184 }
185
flash_stm32_erase(const struct device * dev,off_t offset,size_t len)186 static int flash_stm32_erase(const struct device *dev, off_t offset,
187 size_t len)
188 {
189 int rc;
190
191 if (!flash_stm32_valid_range(dev, offset, len, true)) {
192 LOG_ERR("Erase range invalid. Offset: %ld, len: %zu",
193 (long int) offset, len);
194 return -EINVAL;
195 }
196
197 if (!len) {
198 return 0;
199 }
200
201 flash_stm32_sem_take(dev);
202
203 LOG_DBG("Erase offset: %ld, len: %zu", (long int) offset, len);
204
205 rc = flash_stm32_write_protection(dev, false);
206 if (rc == 0) {
207 rc = flash_stm32_block_erase_loop(dev, offset, len);
208 }
209
210 flash_stm32_flush_caches(dev, offset, len);
211
212 int rc2 = flash_stm32_write_protection(dev, true);
213
214 if (!rc) {
215 rc = rc2;
216 }
217
218 flash_stm32_sem_give(dev);
219
220 return rc;
221 }
222
flash_stm32_write(const struct device * dev,off_t offset,const void * data,size_t len)223 static int flash_stm32_write(const struct device *dev, off_t offset,
224 const void *data, size_t len)
225 {
226 int rc;
227
228 if (!flash_stm32_valid_range(dev, offset, len, true)) {
229 LOG_ERR("Write range invalid. Offset: %ld, len: %zu",
230 (long int) offset, len);
231 return -EINVAL;
232 }
233
234 if (!len) {
235 return 0;
236 }
237
238 flash_stm32_sem_take(dev);
239
240 LOG_DBG("Write offset: %ld, len: %zu", (long int) offset, len);
241
242 rc = flash_stm32_write_protection(dev, false);
243 if (rc == 0) {
244 rc = flash_stm32_write_range(dev, offset, data, len);
245 }
246
247 int rc2 = flash_stm32_write_protection(dev, true);
248
249 if (!rc) {
250 rc = rc2;
251 }
252
253 flash_stm32_sem_give(dev);
254
255 return rc;
256 }
257
flash_stm32_write_protection(const struct device * dev,bool enable)258 static int flash_stm32_write_protection(const struct device *dev, bool enable)
259 {
260 FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
261
262 int rc = 0;
263
264 if (enable) {
265 rc = flash_stm32_wait_flash_idle(dev);
266 if (rc) {
267 flash_stm32_sem_give(dev);
268 return rc;
269 }
270 }
271
272 #if defined(FLASH_SECURITY_NS)
273 if (enable) {
274 regs->NSCR |= FLASH_STM32_NSLOCK;
275 } else {
276 if (regs->NSCR & FLASH_STM32_NSLOCK) {
277 regs->NSKEYR = FLASH_KEY1;
278 regs->NSKEYR = FLASH_KEY2;
279 }
280 }
281 #elif defined(FLASH_CR_LOCK)
282 if (enable) {
283 regs->CR |= FLASH_CR_LOCK;
284 } else {
285 if (regs->CR & FLASH_CR_LOCK) {
286 regs->KEYR = FLASH_KEY1;
287 regs->KEYR = FLASH_KEY2;
288 }
289 }
290 #else
291 if (enable) {
292 regs->PECR |= FLASH_PECR_PRGLOCK;
293 regs->PECR |= FLASH_PECR_PELOCK;
294 } else {
295 if (regs->PECR & FLASH_PECR_PRGLOCK) {
296 LOG_DBG("Disabling write protection");
297 regs->PEKEYR = FLASH_PEKEY1;
298 regs->PEKEYR = FLASH_PEKEY2;
299 regs->PRGKEYR = FLASH_PRGKEY1;
300 regs->PRGKEYR = FLASH_PRGKEY2;
301 }
302 if (FLASH->PECR & FLASH_PECR_PRGLOCK) {
303 LOG_ERR("Unlock failed");
304 rc = -EIO;
305 }
306 }
307 #endif /* FLASH_SECURITY_NS */
308
309 if (enable) {
310 LOG_DBG("Enable write protection");
311 } else {
312 LOG_DBG("Disable write protection");
313 }
314
315 return rc;
316 }
317
flash_stm32_option_bytes_lock(const struct device * dev,bool enable)318 int flash_stm32_option_bytes_lock(const struct device *dev, bool enable)
319 {
320 FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
321
322 #if defined(FLASH_OPTCR_OPTLOCK) /* F2, F4, F7 and H7 */
323 if (enable) {
324 regs->OPTCR |= FLASH_OPTCR_OPTLOCK;
325 } else if (regs->OPTCR & FLASH_OPTCR_OPTLOCK) {
326 regs->OPTKEYR = FLASH_OPT_KEY1;
327 regs->OPTKEYR = FLASH_OPT_KEY2;
328 }
329 #else
330 int rc;
331
332 /* Unlock CR/PECR/NSCR register if needed. */
333 if (!enable) {
334 rc = flash_stm32_write_protection(dev, false);
335 if (rc) {
336 return rc;
337 }
338 }
339 #if defined(FLASH_CR_OPTWRE) /* F0, F1 and F3 */
340 if (enable) {
341 regs->CR &= ~FLASH_CR_OPTWRE;
342 } else if (!(regs->CR & FLASH_CR_OPTWRE)) {
343 regs->OPTKEYR = FLASH_OPTKEY1;
344 regs->OPTKEYR = FLASH_OPTKEY2;
345 }
346 #elif defined(FLASH_CR_OPTLOCK) /* G0, G4, L4, WB and WL */
347 if (enable) {
348 regs->CR |= FLASH_CR_OPTLOCK;
349 } else if (regs->CR & FLASH_CR_OPTLOCK) {
350 regs->OPTKEYR = FLASH_OPTKEY1;
351 regs->OPTKEYR = FLASH_OPTKEY2;
352 }
353 #elif defined(FLASH_PECR_OPTLOCK) /* L0 and L1 */
354 if (enable) {
355 regs->PECR |= FLASH_PECR_OPTLOCK;
356 } else if (regs->PECR & FLASH_PECR_OPTLOCK) {
357 regs->OPTKEYR = FLASH_OPTKEY1;
358 regs->OPTKEYR = FLASH_OPTKEY2;
359 }
360 #elif defined(FLASH_NSCR_OPTLOCK) /* L5 and U5 */
361 if (enable) {
362 regs->NSCR |= FLASH_NSCR_OPTLOCK;
363 } else if (regs->NSCR & FLASH_NSCR_OPTLOCK) {
364 regs->OPTKEYR = FLASH_OPTKEY1;
365 regs->OPTKEYR = FLASH_OPTKEY2;
366 }
367 #elif defined(FLASH_NSCR1_OPTLOCK) /* WBA */
368 if (enable) {
369 regs->NSCR1 |= FLASH_NSCR1_OPTLOCK;
370 } else if (regs->NSCR1 & FLASH_NSCR1_OPTLOCK) {
371 regs->OPTKEYR = FLASH_OPTKEY1;
372 regs->OPTKEYR = FLASH_OPTKEY2;
373 }
374 #endif
375 /* Lock CR/PECR/NSCR register if needed. */
376 if (enable) {
377 rc = flash_stm32_write_protection(dev, true);
378 if (rc) {
379 return rc;
380 }
381 }
382 #endif
383
384 if (enable) {
385 LOG_DBG("Option bytes locked");
386 } else {
387 LOG_DBG("Option bytes unlocked");
388 }
389
390 return 0;
391 }
392
393 #if defined(CONFIG_FLASH_EX_OP_ENABLED) && defined(CONFIG_FLASH_STM32_BLOCK_REGISTERS)
flash_stm32_control_register_disable(const struct device * dev)394 static int flash_stm32_control_register_disable(const struct device *dev)
395 {
396 FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
397
398 #if defined(FLASH_CR_LOCK) /* F0, F1, F2, F3, F4, F7, L4, G0, G4, H7, WB, WL \
399 */
400 /*
401 * Access to control register can be disabled by writing wrong key to
402 * the key register. Option register will remain disabled until reset.
403 * Writing wrong key causes a bus fault, so we need to set FAULTMASK to
404 * disable faults, and clear bus fault pending bit before enabling them
405 * again.
406 */
407 regs->CR |= FLASH_CR_LOCK;
408
409 __set_FAULTMASK(1);
410 regs->KEYR = 0xffffffff;
411
412 /* Clear Bus Fault pending bit */
413 SCB->SHCSR &= ~SCB_SHCSR_BUSFAULTPENDED_Msk;
414 __set_FAULTMASK(0);
415
416 return 0;
417 #else
418 ARG_UNUSED(regs);
419
420 return -ENOTSUP;
421 #endif
422 }
423
flash_stm32_option_bytes_disable(const struct device * dev)424 static int flash_stm32_option_bytes_disable(const struct device *dev)
425 {
426 FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
427
428 #if defined(FLASH_OPTCR_OPTLOCK) /* F2, F4, F7 and H7 */
429 /*
430 * Access to option register can be disabled by writing wrong key to
431 * the key register. Option register will remain disabled until reset.
432 * Writing wrong key causes a bus fault, so we need to set FAULTMASK to
433 * disable faults, and clear bus fault pending bit before enabling them
434 * again.
435 */
436 regs->OPTCR |= FLASH_OPTCR_OPTLOCK;
437
438 __set_FAULTMASK(1);
439 regs->OPTKEYR = 0xffffffff;
440
441 /* Clear Bus Fault pending bit */
442 SCB->SHCSR &= ~SCB_SHCSR_BUSFAULTPENDED_Msk;
443 __set_FAULTMASK(0);
444
445 return 0;
446 #else
447 ARG_UNUSED(regs);
448
449 return -ENOTSUP;
450 #endif
451 }
452 #endif /* CONFIG_FLASH_STM32_BLOCK_REGISTERS */
453
454 static const struct flash_parameters *
flash_stm32_get_parameters(const struct device * dev)455 flash_stm32_get_parameters(const struct device *dev)
456 {
457 ARG_UNUSED(dev);
458
459 return &flash_stm32_parameters;
460 }
461
462 #ifdef CONFIG_FLASH_EX_OP_ENABLED
flash_stm32_ex_op(const struct device * dev,uint16_t code,const uintptr_t in,void * out)463 static int flash_stm32_ex_op(const struct device *dev, uint16_t code,
464 const uintptr_t in, void *out)
465 {
466 int rv = -ENOTSUP;
467
468 flash_stm32_sem_take(dev);
469
470 switch (code) {
471 #if defined(CONFIG_FLASH_STM32_WRITE_PROTECT)
472 case FLASH_STM32_EX_OP_SECTOR_WP:
473 rv = flash_stm32_ex_op_sector_wp(dev, in, out);
474 break;
475 #endif /* CONFIG_FLASH_STM32_WRITE_PROTECT */
476 #if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION)
477 case FLASH_STM32_EX_OP_RDP:
478 rv = flash_stm32_ex_op_rdp(dev, in, out);
479 break;
480 #endif /* CONFIG_FLASH_STM32_READOUT_PROTECTION */
481 #if defined(CONFIG_FLASH_STM32_BLOCK_REGISTERS)
482 case FLASH_STM32_EX_OP_BLOCK_OPTION_REG:
483 rv = flash_stm32_option_bytes_disable(dev);
484 break;
485 case FLASH_STM32_EX_OP_BLOCK_CONTROL_REG:
486 rv = flash_stm32_control_register_disable(dev);
487 break;
488 #endif /* CONFIG_FLASH_STM32_BLOCK_REGISTERS */
489 }
490
491 flash_stm32_sem_give(dev);
492
493 return rv;
494 }
495 #endif
496
497 static struct flash_stm32_priv flash_data = {
498 .regs = (FLASH_TypeDef *) DT_INST_REG_ADDR(0),
499 /* Getting clocks information from device tree description depending
500 * on the presence of 'clocks' property.
501 */
502 #if DT_INST_NODE_HAS_PROP(0, clocks)
503 .pclken = {
504 .enr = DT_INST_CLOCKS_CELL(0, bits),
505 .bus = DT_INST_CLOCKS_CELL(0, bus),
506 }
507 #endif
508 };
509
510 static const struct flash_driver_api flash_stm32_api = {
511 .erase = flash_stm32_erase,
512 .write = flash_stm32_write,
513 .read = flash_stm32_read,
514 .get_parameters = flash_stm32_get_parameters,
515 #ifdef CONFIG_FLASH_PAGE_LAYOUT
516 .page_layout = flash_stm32_page_layout,
517 #endif
518 #ifdef CONFIG_FLASH_EX_OP_ENABLED
519 .ex_op = flash_stm32_ex_op,
520 #endif
521 };
522
stm32_flash_init(const struct device * dev)523 static int stm32_flash_init(const struct device *dev)
524 {
525 int rc;
526 /* Below is applicable to F0, F1, F3, G0, G4, L1, L4, L5, U5 & WB55 series.
527 * For F2, F4, F7 & H7 series, this is not applicable.
528 */
529 #if DT_INST_NODE_HAS_PROP(0, clocks)
530 struct flash_stm32_priv *p = FLASH_STM32_PRIV(dev);
531 const struct device *const clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
532
533 /*
534 * On STM32 F0, F1, F3 & L1 series, flash interface clock source is
535 * always HSI, so statically enable HSI here.
536 */
537 #if defined(CONFIG_SOC_SERIES_STM32F0X) || \
538 defined(CONFIG_SOC_SERIES_STM32F1X) || \
539 defined(CONFIG_SOC_SERIES_STM32F3X) || \
540 defined(CONFIG_SOC_SERIES_STM32L1X)
541 LL_RCC_HSI_Enable();
542
543 while (!LL_RCC_HSI_IsReady()) {
544 }
545 #endif
546
547 if (!device_is_ready(clk)) {
548 LOG_ERR("clock control device not ready");
549 return -ENODEV;
550 }
551
552 /* enable clock */
553 if (clock_control_on(clk, (clock_control_subsys_t)&p->pclken) != 0) {
554 LOG_ERR("Failed to enable clock");
555 return -EIO;
556 }
557 #endif
558
559 #ifdef CONFIG_SOC_SERIES_STM32WBX
560 LL_AHB3_GRP1_EnableClock(LL_AHB3_GRP1_PERIPH_HSEM);
561 #endif /* CONFIG_SOC_SERIES_STM32WBX */
562
563 flash_stm32_sem_init(dev);
564
565 LOG_DBG("Flash @0x%x initialized. BS: %zu",
566 FLASH_STM32_BASE_ADDRESS,
567 flash_stm32_parameters.write_block_size);
568
569 /* Check Flash configuration */
570 rc = flash_stm32_check_configuration();
571 if (rc < 0) {
572 return rc;
573 }
574
575 #if ((CONFIG_FLASH_LOG_LEVEL >= LOG_LEVEL_DBG) && CONFIG_FLASH_PAGE_LAYOUT)
576 const struct flash_pages_layout *layout;
577 size_t layout_size;
578
579 flash_stm32_page_layout(dev, &layout, &layout_size);
580 for (size_t i = 0; i < layout_size; i++) {
581 LOG_DBG("Block %zu: bs: %zu count: %zu", i,
582 layout[i].pages_size, layout[i].pages_count);
583 }
584 #endif
585
586 return 0;
587 }
588
589 DEVICE_DT_INST_DEFINE(0, stm32_flash_init, NULL,
590 &flash_data, NULL, POST_KERNEL,
591 CONFIG_FLASH_INIT_PRIORITY, &flash_stm32_api);
592