Skip to content

Commit 2b0ebfd

Browse files
author
Henrik Lindblom
committed
drivers: stm32: use cache peripheral driver
Closes #71268 Signed-off-by: Henrik Lindblom <[email protected]>
1 parent c7a2e73 commit 2b0ebfd

File tree

5 files changed

+50
-276
lines changed

5 files changed

+50
-276
lines changed

drivers/flash/flash_stm32l5x.c

+21-133
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ LOG_MODULE_REGISTER(LOG_DOMAIN);
1111

1212
#include <zephyr/kernel.h>
1313
#include <zephyr/device.h>
14+
#include <zephyr/cache.h>
1415
#include <string.h>
1516
#include <zephyr/drivers/flash.h>
1617
#include <zephyr/init.h>
1718
#include <soc.h>
18-
#include <stm32_ll_icache.h>
1919
#include <stm32_ll_system.h>
2020

2121
#include "flash_stm32.h"
@@ -37,86 +37,6 @@ LOG_MODULE_REGISTER(LOG_DOMAIN);
3737
#define ICACHE_DISABLE_TIMEOUT_VALUE 1U /* 1ms */
3838
#define ICACHE_INVALIDATE_TIMEOUT_VALUE 1U /* 1ms */
3939

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-
12040
/* Macro to check if the flash is Dual bank or not */
12141
#if defined(CONFIG_SOC_SERIES_STM32H5X)
12242
#define stm32_flash_has_2_banks(flash_device) true
@@ -302,19 +222,15 @@ int flash_stm32_block_erase_loop(const struct device *dev,
302222
{
303223
unsigned int address = offset;
304224
int rc = 0;
305-
bool icache_enabled = LL_ICACHE_IsEnabled();
306225

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-
}
226+
/* Disable icache, this will start the invalidation procedure.
227+
* All changes(erase/write) to flash memory should happen when
228+
* i-cache is disabled. A write to flash performed without
229+
* disabling i-cache will set ERRF error flag in SR register.
230+
*/
231+
232+
LOG_DBG("I-cache Disable");
233+
sys_cache_instr_disable();
318234

319235
for (; address <= offset + len - 1 ; address += FLASH_PAGE_SIZE) {
320236
rc = erase_page(dev, address);
@@ -323,17 +239,8 @@ int flash_stm32_block_erase_loop(const struct device *dev,
323239
}
324240
}
325241

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();
336-
}
242+
LOG_DBG("I-cache Enable");
243+
sys_cache_instr_enable();
337244

338245
return rc;
339246
}
@@ -342,19 +249,15 @@ int flash_stm32_write_range(const struct device *dev, unsigned int offset,
342249
const void *data, unsigned int len)
343250
{
344251
int i, rc = 0;
345-
bool icache_enabled = LL_ICACHE_IsEnabled();
346252

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+
LOG_DBG("I-cache Disable");
260+
sys_cache_instr_disable();
358261

359262
for (i = 0; i < len; i += FLASH_STM32_WRITE_BLOCK_SIZE) {
360263
rc = write_nwords(dev, offset + i, ((const uint32_t *) data + (i>>2)),
@@ -364,23 +267,8 @@ int flash_stm32_write_range(const struct device *dev, unsigned int offset,
364267
}
365268
}
366269

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();
383-
}
270+
LOG_DBG("I-cache Enable");
271+
sys_cache_instr_enable();
384272

385273
return rc;
386274
}

0 commit comments

Comments
 (0)