@@ -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>
@@ -37,86 +38,6 @@ LOG_MODULE_REGISTER(LOG_DOMAIN);
37
38
#define ICACHE_DISABLE_TIMEOUT_VALUE 1U /* 1ms */
38
39
#define ICACHE_INVALIDATE_TIMEOUT_VALUE 1U /* 1ms */
39
40
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
41
/* Macro to check if the flash is Dual bank or not */
121
42
#if defined(CONFIG_SOC_SERIES_STM32H5X )
122
43
#define stm32_flash_has_2_banks (flash_device ) true
@@ -302,19 +223,16 @@ int flash_stm32_block_erase_loop(const struct device *dev,
302
223
{
303
224
unsigned int address = offset ;
304
225
int rc = 0 ;
305
- bool icache_enabled = LL_ICACHE_IsEnabled ();
306
226
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
- }
227
+ /* Disable icache, this will start the invalidation procedure.
228
+ * All changes(erase/write) to flash memory should happen when
229
+ * i-cache is disabled. A write to flash performed without
230
+ * disabling i-cache will set ERRF error flag in SR register.
231
+ */
232
+
233
+ bool cache_enabled = LL_ICACHE_IsEnabled ();
234
+
235
+ sys_cache_instr_disable ();
318
236
319
237
for (; address <= offset + len - 1 ; address += FLASH_PAGE_SIZE ) {
320
238
rc = erase_page (dev , address );
@@ -323,16 +241,8 @@ int flash_stm32_block_erase_loop(const struct device *dev,
323
241
}
324
242
}
325
243
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 ();
244
+ if (cache_enabled ) {
245
+ sys_cache_instr_enable ();
336
246
}
337
247
338
248
return rc ;
@@ -342,19 +252,16 @@ int flash_stm32_write_range(const struct device *dev, unsigned int offset,
342
252
const void * data , unsigned int len )
343
253
{
344
254
int i , rc = 0 ;
345
- bool icache_enabled = LL_ICACHE_IsEnabled ();
346
255
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
- }
256
+ /* Disable icache, this will start the invalidation procedure.
257
+ * All changes(erase/write) to flash memory should happen when
258
+ * i-cache is disabled. A write to flash performed without
259
+ * disabling i-cache will set ERRF error flag in SR register.
260
+ */
261
+
262
+ bool cache_enabled = LL_ICACHE_IsEnabled ();
263
+
264
+ sys_cache_instr_disable ();
358
265
359
266
for (i = 0 ; i < len ; i += FLASH_STM32_WRITE_BLOCK_SIZE ) {
360
267
rc = write_nwords (dev , offset + i , ((const uint32_t * ) data + (i >>2 )),
@@ -364,22 +271,8 @@ int flash_stm32_write_range(const struct device *dev, unsigned int offset,
364
271
}
365
272
}
366
273
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 ();
274
+ if (cache_enabled ){
275
+ sys_cache_instr_enable ();
383
276
}
384
277
385
278
return rc ;
0 commit comments