Skip to content

Commit 30f98b9

Browse files
committed
drivers/flash: stm32wb: Add driver for stm32wb
Add flash driver for stm32wb Signed-off-by: Erwan Gouriou <[email protected]>
1 parent 1c8b1e9 commit 30f98b9

File tree

9 files changed

+314
-3
lines changed

9 files changed

+314
-3
lines changed

drivers/flash/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ if(CONFIG_CLOCK_CONTROL_STM32_CUBE)
2424
zephyr_sources_ifdef(CONFIG_SOC_SERIES_STM32F4X flash_stm32f4x.c)
2525
zephyr_sources_ifdef(CONFIG_SOC_SERIES_STM32F7X flash_stm32f7x.c)
2626
zephyr_sources_ifdef(CONFIG_SOC_SERIES_STM32L4X flash_stm32l4x.c)
27+
zephyr_sources_ifdef(CONFIG_SOC_SERIES_STM32WBX flash_stm32wbx.c)
2728
endif()
2829

2930
zephyr_include_directories_ifdef(

drivers/flash/Kconfig.stm32

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,23 @@ if SOC_FAMILY_STM32
1010

1111
menuconfig SOC_FLASH_STM32
1212
bool "STM32 flash driver"
13-
depends on (SOC_SERIES_STM32F0X || SOC_SERIES_STM32F3X || SOC_SERIES_STM32F4X || SOC_SERIES_STM32F7X || SOC_SERIES_STM32L4X)
13+
depends on (SOC_SERIES_STM32F0X || SOC_SERIES_STM32F3X || SOC_SERIES_STM32F4X || SOC_SERIES_STM32F7X || SOC_SERIES_STM32L4X || SOC_SERIES_STM32WBX)
1414
select FLASH_HAS_DRIVER_ENABLED
1515
default y
1616
select FLASH_PAGE_LAYOUT if SOC_SERIES_STM32F0X
1717
select FLASH_PAGE_LAYOUT if SOC_SERIES_STM32F3X
1818
select FLASH_PAGE_LAYOUT if SOC_SERIES_STM32F4X
1919
select FLASH_PAGE_LAYOUT if SOC_SERIES_STM32F7X
2020
select FLASH_PAGE_LAYOUT if SOC_SERIES_STM32L4X
21+
select FLASH_PAGE_LAYOUT if SOC_SERIES_STM32WBX
2122
select FLASH_HAS_PAGE_LAYOUT if SOC_SERIES_STM32F0X
2223
select FLASH_HAS_PAGE_LAYOUT if SOC_SERIES_STM32F3X
2324
select FLASH_HAS_PAGE_LAYOUT if SOC_SERIES_STM32F4X
2425
select FLASH_HAS_PAGE_LAYOUT if SOC_SERIES_STM32F7X
2526
select FLASH_HAS_PAGE_LAYOUT if SOC_SERIES_STM32L4X
27+
select FLASH_HAS_PAGE_LAYOUT if SOC_SERIES_STM32WBX
2628
help
27-
Enable STM32F0x, STM32F3x, STM32F4x, STM32F7x OR STM32L4x series flash driver.
29+
Enable STM32F0x, STM32F3x, STM32F4x, STM32F7x, STM32L4x or
30+
STM32WBx series flash driver.
2831

2932
endif

drivers/flash/flash_stm32.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
/* STM32L4: maximum erase time of 24.47ms for a 2K sector */
3030
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
3131
#define STM32_FLASH_TIMEOUT (K_MSEC(25))
32+
/* STM32WB: maximum erase time of 24.5ms for a 4K sector */
33+
#elif defined(CONFIG_SOC_SERIES_STM32WBX)
34+
#define STM32_FLASH_TIMEOUT (K_MSEC(25))
3235
#endif
3336

3437
/*
@@ -46,6 +49,7 @@ static inline void flash_stm32_sem_give(struct device *dev)
4649
k_sem_give(&FLASH_STM32_PRIV(dev)->sem);
4750
}
4851

52+
#if !defined(CONFIG_SOC_SERIES_STM32WBX)
4953
static int flash_stm32_check_status(struct device *dev)
5054
{
5155
u32_t const error =
@@ -75,6 +79,7 @@ static int flash_stm32_check_status(struct device *dev)
7579

7680
return 0;
7781
}
82+
#endif /* CONFIG_SOC_SERIES_STM32WBX */
7883

7984
int flash_stm32_wait_flash_idle(struct device *dev)
8085
{
@@ -102,13 +107,17 @@ static void flash_stm32_flush_caches(struct device *dev,
102107
ARG_UNUSED(dev);
103108
ARG_UNUSED(offset);
104109
ARG_UNUSED(len);
105-
#elif defined(CONFIG_SOC_SERIES_STM32F4X) || defined(CONFIG_SOC_SERIES_STM32L4X)
110+
#elif defined(CONFIG_SOC_SERIES_STM32F4X) || \
111+
defined(CONFIG_SOC_SERIES_STM32L4X) || \
112+
defined(CONFIG_SOC_SERIES_STM32WBX)
106113
ARG_UNUSED(offset);
107114
ARG_UNUSED(len);
108115
#if defined(CONFIG_SOC_SERIES_STM32F4X)
109116
struct stm32f4x_flash *regs = FLASH_STM32_REGS(dev);
110117
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
111118
struct stm32l4x_flash *regs = FLASH_STM32_REGS(dev);
119+
#elif defined(CONFIG_SOC_SERIES_STM32WBX)
120+
struct stm32wbx_flash *regs = FLASH_STM32_REGS(dev);
112121
#endif
113122
if (regs->acr.val & FLASH_ACR_DCEN) {
114123
regs->acr.val &= ~FLASH_ACR_DCEN;
@@ -195,6 +204,8 @@ static int flash_stm32_write_protection(struct device *dev, bool enable)
195204
struct stm32f3x_flash *regs = FLASH_STM32_REGS(dev);
196205
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
197206
struct stm32l4x_flash *regs = FLASH_STM32_REGS(dev);
207+
#elif defined(CONFIG_SOC_SERIES_STM32WBX)
208+
struct stm32wbx_flash *regs = FLASH_STM32_REGS(dev);
198209
#endif
199210
int rc = 0;
200211

@@ -236,6 +247,8 @@ static struct flash_stm32_priv flash_data = {
236247
.regs = (struct stm32l4x_flash *) DT_FLASH_DEV_BASE_ADDRESS,
237248
.pclken = { .bus = STM32_CLOCK_BUS_AHB1,
238249
.enr = LL_AHB1_GRP1_PERIPH_FLASH },
250+
#elif defined(CONFIG_SOC_SERIES_STM32WBX)
251+
.regs = (struct stm32wbx_flash *) DT_FLASH_DEV_BASE_ADDRESS,
239252
#endif
240253
};
241254

drivers/flash/flash_stm32.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ struct flash_stm32_priv {
3434
struct stm32l4x_flash *regs;
3535
/* clock subsystem driving this peripheral */
3636
struct stm32_pclken pclken;
37+
#elif defined(CONFIG_SOC_SERIES_STM32WBX)
38+
struct stm32wbx_flash *regs;
3739
#endif
3840
struct k_sem sem;
3941
};
@@ -64,6 +66,10 @@ int flash_stm32_block_erase_loop(struct device *dev, unsigned int offset,
6466

6567
int flash_stm32_wait_flash_idle(struct device *dev);
6668

69+
#ifdef CONFIG_SOC_SERIES_STM32WBX
70+
int flash_stm32_check_status(struct device *dev);
71+
#endif /* CONFIG_SOC_SERIES_STM32WBX */
72+
6773
#ifdef CONFIG_FLASH_PAGE_LAYOUT
6874
void flash_stm32_page_layout(struct device *dev,
6975
const struct flash_pages_layout **layout,

drivers/flash/flash_stm32wbx.c

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/*
2+
* Copyright (c) 2019 Linaro Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define LOG_DOMAIN flash_stm32wb
8+
#define LOG_LEVEL CONFIG_FLASH_LOG_LEVEL
9+
#include <logging/log.h>
10+
LOG_MODULE_REGISTER(LOG_DOMAIN);
11+
12+
#include <kernel.h>
13+
#include <device.h>
14+
#include <string.h>
15+
#include <flash.h>
16+
#include <init.h>
17+
#include <soc.h>
18+
#include <misc/__assert.h>
19+
20+
#include "flash_stm32.h"
21+
22+
#define STM32WBX_PAGE_SHIFT 12
23+
24+
/* offset and len must be aligned on 8 for write,
25+
* positive and not beyond end of flash
26+
*/
27+
bool flash_stm32_valid_range(struct device *dev, off_t offset, u32_t len,
28+
bool write)
29+
{
30+
return (!write || (offset % 8 == 0 && len % 8 == 0U)) &&
31+
flash_stm32_range_exists(dev, offset, len);
32+
}
33+
34+
/*
35+
* Up to 255 4K pages
36+
*/
37+
static u32_t get_page(off_t offset)
38+
{
39+
return offset >> STM32WBX_PAGE_SHIFT;
40+
}
41+
42+
static int write_dword(struct device *dev, off_t offset, u64_t val)
43+
{
44+
volatile u32_t *flash = (u32_t *)(offset + CONFIG_FLASH_BASE_ADDRESS);
45+
struct stm32wbx_flash *regs = FLASH_STM32_REGS(dev);
46+
u32_t tmp;
47+
int ret, rc;
48+
49+
/* if the control register is locked, do not fail silently */
50+
if (regs->cr & FLASH_CR_LOCK) {
51+
rc = -EIO;
52+
}
53+
54+
/* Check if this double word is erased */
55+
if (flash[0] != 0xFFFFFFFFUL ||
56+
flash[1] != 0xFFFFFFFFUL) {
57+
return -EIO;
58+
}
59+
60+
ret = flash_stm32_check_status(dev);
61+
if (ret < 0) {
62+
return -EIO;
63+
}
64+
65+
/* Set the PG bit */
66+
regs->cr |= FLASH_CR_PG;
67+
68+
/* Flush the register write */
69+
tmp = regs->cr;
70+
71+
/* Perform the data write operation at the desired memory address */
72+
flash[0] = (u32_t)val;
73+
flash[1] = (u32_t)(val >> 32);
74+
75+
/* Wait until the BSY bit is cleared */
76+
rc = flash_stm32_wait_flash_idle(dev);
77+
78+
/* Clear the PG bit */
79+
regs->cr &= (~FLASH_CR_PG);
80+
81+
return 0;
82+
}
83+
84+
static int erase_page(struct device *dev, u32_t page)
85+
{
86+
struct stm32wbx_flash *regs = FLASH_STM32_REGS(dev);
87+
int rc;
88+
89+
/* if the control register is locked, do not fail silently */
90+
if (regs->cr & FLASH_CR_LOCK) {
91+
return -EIO;
92+
}
93+
94+
/* Check that no Flash memory operation is ongoing */
95+
rc = flash_stm32_wait_flash_idle(dev);
96+
if (rc < 0) {
97+
return rc;
98+
}
99+
100+
/* Check erase operation allowed */
101+
if (regs->cr & FLASH_SR_PESD) {
102+
return -EBUSY;
103+
}
104+
105+
/* Proceed to erase the page */
106+
regs->cr |= FLASH_CR_PER;
107+
regs->cr &= ~FLASH_CR_PNB_Msk;
108+
regs->cr |= page << FLASH_CR_PNB_Pos;
109+
110+
regs->cr |= FLASH_CR_STRT;
111+
112+
/* Wait for the BSY bit */
113+
rc = flash_stm32_wait_flash_idle(dev);
114+
115+
regs->cr &= (~FLASH_TYPEERASE_PAGES);
116+
117+
return rc;
118+
}
119+
120+
int flash_stm32_block_erase_loop(struct device *dev, unsigned int offset,
121+
unsigned int len)
122+
{
123+
int i, rc = 0;
124+
125+
i = get_page(offset);
126+
for (; i <= get_page(offset + len - 1) ; ++i) {
127+
rc = erase_page(dev, i);
128+
if (rc < 0) {
129+
break;
130+
}
131+
}
132+
133+
return rc;
134+
}
135+
136+
int flash_stm32_write_range(struct device *dev, unsigned int offset,
137+
const void *data, unsigned int len)
138+
{
139+
int i, rc = 0;
140+
141+
for (i = 0; i < len; i += 8, offset += 8U) {
142+
rc = write_dword(dev, offset,
143+
UNALIGNED_GET((const u64_t *) data + (i >> 3)));
144+
if (rc < 0) {
145+
return rc;
146+
}
147+
}
148+
149+
return rc;
150+
}
151+
152+
void flash_stm32_page_layout(struct device *dev,
153+
const struct flash_pages_layout **layout,
154+
size_t *layout_size)
155+
{
156+
static struct flash_pages_layout stm32wb_flash_layout = {
157+
.pages_count = 0,
158+
.pages_size = 0,
159+
};
160+
161+
ARG_UNUSED(dev);
162+
163+
if (stm32wb_flash_layout.pages_count == 0) {
164+
stm32wb_flash_layout.pages_count = FLASH_SIZE / FLASH_PAGE_SIZE;
165+
stm32wb_flash_layout.pages_size = FLASH_PAGE_SIZE;
166+
}
167+
168+
*layout = &stm32wb_flash_layout;
169+
*layout_size = 1;
170+
}
171+
172+
int flash_stm32_check_status(struct device *dev)
173+
{
174+
struct stm32wbx_flash *regs = FLASH_STM32_REGS(dev);
175+
u32_t error = 0;
176+
177+
/* Save Flash errors */
178+
error = (regs->sr & FLASH_FLAG_SR_ERROR);
179+
error |= (regs->eccr & FLASH_FLAG_ECCC);
180+
181+
/* Clear systematic Option and Enginneering bits validity error */
182+
if (error & FLASH_FLAG_OPTVERR) {
183+
regs->sr |= FLASH_FLAG_SR_ERROR;
184+
return 0;
185+
}
186+
187+
if (error) {
188+
return -EIO;
189+
}
190+
191+
return 0;
192+
}

dts/arm/st/wb/stm32wb.dtsi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
flash0: flash@8000000 {
3939
compatible = "soc-nv-flash";
4040
label = "FLASH_STM32";
41+
42+
write-block-size = <8>;
43+
erase-block-size = <4096>;
4144
};
4245
};
4346

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: STM32 WB Flash Controller
3+
version: 0.1
4+
5+
description: >
6+
This binding gives a base representation of the STM32 wb Flash Controller
7+
8+
inherits:
9+
!include flash-controller.yaml
10+
11+
properties:
12+
compatible:
13+
constraint: "st,stm32wb-flash-controller"
14+
15+
single-bank:
16+
type: boolean
17+
description: dual-bank mode not enabled (page erase 4096k)
18+
generation: define
19+
category: optional
20+
21+
dual-bank:
22+
type: boolean
23+
description: dual-bank mode enabled (page erase 2048k)
24+
generation: define
25+
category: optional
26+
...

soc/arm/st_stm32/stm32wb/dts_fixup.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,8 @@
8080
#define DT_UART_STM32_LPUART_1_CLOCK_BUS DT_ST_STM32_LPUART_40008000_CLOCK_BUS
8181
#define DT_UART_STM32_LPUART_1_HW_FLOW_CONTROL DT_ST_STM32_LPUART_40008000_HW_FLOW_CONTROL
8282

83+
#define DT_FLASH_DEV_BASE_ADDRESS DT_ST_STM32WB_FLASH_CONTROLLER_58004000_BASE_ADDRESS
84+
#define DT_FLASH_DEV_NAME DT_ST_STM32WB_FLASH_CONTROLLER_58004000_LABEL
85+
86+
8387
/* End of SoC Level DTS fixup file */

0 commit comments

Comments
 (0)