@@ -11,6 +11,7 @@ LOG_MODULE_REGISTER(LOG_DOMAIN);
11
11
12
12
#include <zephyr/kernel.h>
13
13
#include <zephyr/device.h>
14
+ #include <zephyr/cache.h>
14
15
#include <string.h>
15
16
#include <zephyr/drivers/flash.h>
16
17
#include <zephyr/init.h>
@@ -34,89 +35,6 @@ LOG_MODULE_REGISTER(LOG_DOMAIN);
34
35
35
36
#define BANK2_OFFSET (KB(STM32_SERIES_MAX_FLASH) / 2)
36
37
37
- #define ICACHE_DISABLE_TIMEOUT_VALUE 1U /* 1ms */
38
- #define ICACHE_INVALIDATE_TIMEOUT_VALUE 1U /* 1ms */
39
-
40
- static int stm32_icache_disable (void )
41
- {
42
- int status = 0 ;
43
- uint32_t tickstart ;
44
-
45
- LOG_DBG ("I-cache Disable" );
46
- /* Clear BSYENDF flag first and then disable the instruction cache
47
- * that starts a cache invalidation procedure
48
- */
49
- CLEAR_BIT (ICACHE -> FCR , ICACHE_FCR_CBSYENDF );
50
-
51
- LL_ICACHE_Disable ();
52
-
53
- /* Get tick */
54
- tickstart = k_uptime_get_32 ();
55
-
56
- /* Wait for instruction cache to get disabled */
57
- while (LL_ICACHE_IsEnabled ()) {
58
- if ((k_uptime_get_32 () - tickstart ) >
59
- ICACHE_DISABLE_TIMEOUT_VALUE ) {
60
- /* New check to avoid false timeout detection in case
61
- * of preemption.
62
- */
63
- if (LL_ICACHE_IsEnabled ()) {
64
- status = - ETIMEDOUT ;
65
- break ;
66
- }
67
- }
68
- }
69
-
70
- return status ;
71
- }
72
-
73
- static void stm32_icache_enable (void )
74
- {
75
- LOG_DBG ("I-cache Enable" );
76
- LL_ICACHE_Enable ();
77
- }
78
-
79
- static int icache_wait_for_invalidate_complete (void )
80
- {
81
- int status = - EIO ;
82
- uint32_t tickstart ;
83
-
84
- /* Check if ongoing invalidation operation */
85
- if (LL_ICACHE_IsActiveFlag_BUSY ()) {
86
- /* Get tick */
87
- tickstart = k_uptime_get_32 ();
88
-
89
- /* Wait for end of cache invalidation */
90
- while (!LL_ICACHE_IsActiveFlag_BSYEND ()) {
91
- if ((k_uptime_get_32 () - tickstart ) >
92
- ICACHE_INVALIDATE_TIMEOUT_VALUE ) {
93
- break ;
94
- }
95
- }
96
- }
97
-
98
- /* Clear any pending flags */
99
- if (LL_ICACHE_IsActiveFlag_BSYEND ()) {
100
- LOG_DBG ("I-cache Invalidation complete" );
101
-
102
- LL_ICACHE_ClearFlag_BSYEND ();
103
- status = 0 ;
104
- } else {
105
- LOG_ERR ("I-cache Invalidation timeout" );
106
-
107
- status = - ETIMEDOUT ;
108
- }
109
-
110
- if (LL_ICACHE_IsActiveFlag_ERR ()) {
111
- LOG_ERR ("I-cache error" );
112
-
113
- LL_ICACHE_ClearFlag_ERR ();
114
- status = - EIO ;
115
- }
116
-
117
- return status ;
118
- }
119
-
120
38
/* Macro to check if the flash is Dual bank or not */
121
39
#if defined(CONFIG_SOC_SERIES_STM32H5X )
122
40
#define stm32_flash_has_2_banks (flash_device ) true
@@ -302,19 +220,16 @@ int flash_stm32_block_erase_loop(const struct device *dev,
302
220
{
303
221
unsigned int address = offset ;
304
222
int rc = 0 ;
305
- bool icache_enabled = LL_ICACHE_IsEnabled ();
306
223
307
- if (icache_enabled ) {
308
- /* Disable icache, this will start the invalidation procedure.
309
- * All changes(erase/write) to flash memory should happen when
310
- * i-cache is disabled. A write to flash performed without
311
- * disabling i-cache will set ERRF error flag in SR register.
312
- */
313
- rc = stm32_icache_disable ();
314
- if (rc != 0 ) {
315
- return rc ;
316
- }
317
- }
224
+ /* Disable icache, this will start the invalidation procedure.
225
+ * All changes(erase/write) to flash memory should happen when
226
+ * i-cache is disabled. A write to flash performed without
227
+ * disabling i-cache will set ERRF error flag in SR register.
228
+ */
229
+
230
+ bool cache_enabled = LL_ICACHE_IsEnabled ();
231
+
232
+ sys_cache_instr_disable ();
318
233
319
234
for (; address <= offset + len - 1 ; address += FLASH_PAGE_SIZE ) {
320
235
rc = erase_page (dev , address );
@@ -323,16 +238,8 @@ int flash_stm32_block_erase_loop(const struct device *dev,
323
238
}
324
239
}
325
240
326
- if (icache_enabled ) {
327
- /* Since i-cache was disabled, this would start the
328
- * invalidation procedure, so wait for completion.
329
- */
330
- rc = icache_wait_for_invalidate_complete ();
331
-
332
- /* I-cache should be enabled only after the
333
- * invalidation is complete.
334
- */
335
- stm32_icache_enable ();
241
+ if (cache_enabled ) {
242
+ sys_cache_instr_enable ();
336
243
}
337
244
338
245
return rc ;
@@ -342,19 +249,16 @@ int flash_stm32_write_range(const struct device *dev, unsigned int offset,
342
249
const void * data , unsigned int len )
343
250
{
344
251
int i , rc = 0 ;
345
- bool icache_enabled = LL_ICACHE_IsEnabled ();
346
252
347
- if (icache_enabled ) {
348
- /* Disable icache, this will start the invalidation procedure.
349
- * All changes(erase/write) to flash memory should happen when
350
- * i-cache is disabled. A write to flash performed without
351
- * disabling i-cache will set ERRF error flag in SR register.
352
- */
353
- rc = stm32_icache_disable ();
354
- if (rc != 0 ) {
355
- return rc ;
356
- }
357
- }
253
+ /* Disable icache, this will start the invalidation procedure.
254
+ * All changes(erase/write) to flash memory should happen when
255
+ * i-cache is disabled. A write to flash performed without
256
+ * disabling i-cache will set ERRF error flag in SR register.
257
+ */
258
+
259
+ bool cache_enabled = LL_ICACHE_IsEnabled ();
260
+
261
+ sys_cache_instr_disable ();
358
262
359
263
for (i = 0 ; i < len ; i += FLASH_STM32_WRITE_BLOCK_SIZE ) {
360
264
rc = write_nwords (dev , offset + i , ((const uint32_t * ) data + (i >>2 )),
@@ -364,22 +268,8 @@ int flash_stm32_write_range(const struct device *dev, unsigned int offset,
364
268
}
365
269
}
366
270
367
- if (icache_enabled ) {
368
- int rc2 ;
369
-
370
- /* Since i-cache was disabled, this would start the
371
- * invalidation procedure, so wait for completion.
372
- */
373
- rc2 = icache_wait_for_invalidate_complete ();
374
-
375
- if (!rc ) {
376
- rc = rc2 ;
377
- }
378
-
379
- /* I-cache should be enabled only after the
380
- * invalidation is complete.
381
- */
382
- stm32_icache_enable ();
271
+ if (cache_enabled ) {
272
+ sys_cache_instr_enable ();
383
273
}
384
274
385
275
return rc ;
0 commit comments