Skip to content

Commit 42dad4a

Browse files
superna9999galak
authored andcommitted
flash: Rework and add flash device support for STM32L4x SoCs
The STM32L4x SoCs embeds a slightly different embedded flash controller from the STM32F4x SoCs. This particular controller has the following properties : - Up to 2 512KiB banks divided in 2KiB pages - Flash can be accessed in any sizes - Flash must be written in 64bit aligned 64bit double-words The drivers/flash/flash_stm32f4x.c is refactored into a new common drivers/flash/flash_stm32.c and drivers/flash/flash_stm32l4x.c is created with the STM32L4x specific functions. To ease the refactoring and keep common functions, the STM32L4x flash headers are slightly modified to match the hardware reference naming and solve compilation issues. Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
1 parent ad9cca0 commit 42dad4a

File tree

10 files changed

+494
-220
lines changed

10 files changed

+494
-220
lines changed

arch/arm/soc/st_stm32/stm32f4/flash_map.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ struct stm32f4x_flash_sector {
2020
.end = offset + bytes - 1, \
2121
}
2222

23-
__attribute__((unused))
24-
struct stm32f4x_flash_sector stm32f4xx_sectors[] = {
23+
__unused
24+
static struct stm32f4x_flash_sector stm32f4xx_sectors[] = {
2525
STM32F4X_FLASH_SECTOR(0x00000, KB(16)),
2626
STM32F4X_FLASH_SECTOR(0x04000, KB(16)),
2727
STM32F4X_FLASH_SECTOR(0x08000, KB(16)),
@@ -41,7 +41,8 @@ struct stm32f4x_flash_sector stm32f4xx_sectors[] = {
4141
#define STM32F4X_FLASH_END \
4242
(stm32f4xx_sectors[ARRAY_SIZE(stm32f4xx_sectors) - 1].end)
4343

44-
int stm32f4x_get_sector(off_t offset)
44+
__unused
45+
static int stm32f4x_get_sector(off_t offset)
4546
{
4647
int i;
4748

arch/arm/soc/st_stm32/stm32f4/flash_registers.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ union __flash_acr {
3232
/* 3.8.7 Embedded flash registers */
3333
struct stm32f4x_flash {
3434
volatile union __flash_acr acr;
35-
volatile u32_t key;
36-
volatile u32_t optkey;
37-
volatile u32_t status;
38-
volatile u32_t ctrl;
39-
volatile u32_t optctrl;
35+
volatile u32_t keyr;
36+
volatile u32_t optkeyr;
37+
volatile u32_t sr;
38+
volatile u32_t cr;
39+
volatile u32_t optcr;
4040
};
4141

4242
#endif /* _STM32F4X_FLASHREGISTERS_H_ */

arch/arm/soc/st_stm32/stm32l4/flash_registers.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,29 @@ union __ef_acr {
3535

3636
/* FLASH register map */
3737
struct stm32l4x_flash {
38-
union __ef_acr acr;
39-
u32_t pdkeyr;
40-
u32_t keyr;
41-
u32_t optkeyr;
42-
u32_t sr;
43-
u32_t cr;
44-
u32_t eccr;
45-
u32_t rsvd_0;
46-
u32_t optr;
47-
u32_t pcrop1sr;
48-
u32_t pcrop1er;
49-
u32_t wrp1ar;
50-
u32_t wrp1br;
51-
u32_t rsvd_2[4];
38+
volatile union __ef_acr acr;
39+
volatile u32_t pdkeyr;
40+
volatile u32_t keyr;
41+
volatile u32_t optkeyr;
42+
volatile u32_t sr;
43+
volatile u32_t cr;
44+
volatile u32_t eccr;
45+
volatile u32_t rsvd_0;
46+
volatile u32_t optr;
47+
volatile u32_t pcrop1sr;
48+
volatile u32_t pcrop1er;
49+
volatile u32_t wrp1ar;
50+
volatile u32_t wrp1br;
51+
volatile u32_t rsvd_2[4];
5252

5353
/*
5454
* The registers below are only present on STM32L4x2, STM32L4x5,
5555
* STM32L4x6.
5656
*/
57-
u32_t pcrop2sr;
58-
u32_t pcrop2er;
59-
u32_t wrp2ar;
60-
u32_t wrp2br;
57+
volatile u32_t pcrop2sr;
58+
volatile u32_t pcrop2er;
59+
volatile u32_t wrp2ar;
60+
volatile u32_t wrp2br;
6161
};
6262

6363
#endif /* _STM32L4X_FLASH_REGISTERS_H_ */

drivers/flash/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,4 @@ config SOC_FLASH_MCUX_DEV_NAME
148148
help
149149
Specify the device name for the flash driver.
150150

151-
source "drivers/flash/Kconfig.stm32fxx"
151+
source "drivers/flash/Kconfig.stm32"

drivers/flash/Kconfig.stm32fxx renamed to drivers/flash/Kconfig.stm32

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# Kconfig - ST Microelectronics STM32F3x MCUs Flash driver config
1+
# Kconfig - ST Microelectronics STM32 MCUs Flash driver config
22
#
33
# Copyright (c) 2016 RnDity Sp. z o.o.
4+
# Copyright (c) 2017 BayLibre, SAS
45
#
56
# SPDX-License-Identifier: Apache-2.0
67
#
@@ -10,10 +11,10 @@ if FLASH && SOC_FAMILY_STM32
1011
menuconfig SOC_FLASH_STM32
1112
bool
1213
prompt "STM32 flash driver"
13-
depends on (SOC_SERIES_STM32F3X || SOC_SERIES_STM32F4X)
14+
depends on (SOC_SERIES_STM32F3X || SOC_SERIES_STM32F4X || SOC_SERIES_STM32L4X)
1415
default y
1516
help
16-
Enable STM32F3x OR STM32F4x series flash driver.
17+
Enable STM32F3x, STM32F4x OR STM32L4x series flash driver.
1718

1819
config SOC_FLASH_STM32_DEV_NAME
1920
string "STM32 flash device name"

drivers/flash/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ obj-$(CONFIG_SOC_FLASH_STM32) += flash_stm32f3x_priv.o
99
endif
1010

1111
ifeq ($(CONFIG_SOC_SERIES_STM32F4X),y)
12-
obj-$(CONFIG_SOC_FLASH_STM32) += flash_stm32f4x.o
12+
obj-$(CONFIG_SOC_FLASH_STM32) += flash_stm32.o flash_stm32f4x.o
13+
endif
14+
15+
ifeq ($(CONFIG_SOC_SERIES_STM32L4X),y)
16+
obj-$(CONFIG_SOC_FLASH_STM32) += flash_stm32.o flash_stm32l4x.o
1317
endif

drivers/flash/flash_stm32.c

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
/*
2+
* Copyright (c) 2017 Linaro Limited
3+
* Copyright (c) 2017 BayLibre, SAS.
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include <kernel.h>
9+
#include <device.h>
10+
#include <string.h>
11+
#include <flash.h>
12+
#include <init.h>
13+
#include <soc.h>
14+
15+
#include <flash_stm32.h>
16+
17+
#define STM32_FLASH_TIMEOUT ((u32_t) 0x000B0000)
18+
19+
int flash_stm32_check_status(struct flash_stm32_priv *p)
20+
{
21+
#if defined(CONFIG_SOC_SERIES_STM32F4X)
22+
struct stm32f4x_flash *regs = p->regs;
23+
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
24+
struct stm32l4x_flash *regs = p->regs;
25+
#endif
26+
27+
u32_t const error =
28+
FLASH_FLAG_WRPERR |
29+
FLASH_FLAG_PGAERR |
30+
#if defined(FLASH_FLAG_RDERR)
31+
FLASH_FLAG_RDERR |
32+
#endif
33+
#if defined(FLASH_FLAG_PGPERR)
34+
FLASH_FLAG_PGPERR |
35+
#endif
36+
FLASH_FLAG_PGSERR |
37+
FLASH_FLAG_OPERR;
38+
39+
if (regs->sr & error) {
40+
return -EIO;
41+
}
42+
43+
return 0;
44+
}
45+
46+
int flash_stm32_wait_flash_idle(struct flash_stm32_priv *p)
47+
{
48+
#if defined(CONFIG_SOC_SERIES_STM32F4X)
49+
struct stm32f4x_flash *regs = p->regs;
50+
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
51+
struct stm32l4x_flash *regs = p->regs;
52+
#endif
53+
u32_t timeout = STM32_FLASH_TIMEOUT;
54+
int rc;
55+
56+
rc = flash_stm32_check_status(p);
57+
if (rc < 0) {
58+
return -EIO;
59+
}
60+
61+
while ((regs->sr & FLASH_SR_BSY) && timeout) {
62+
timeout--;
63+
}
64+
65+
if (!timeout) {
66+
return -EIO;
67+
}
68+
69+
return 0;
70+
}
71+
72+
void flash_stm32_flush_caches(struct flash_stm32_priv *p)
73+
{
74+
#if defined(CONFIG_SOC_SERIES_STM32F4X)
75+
struct stm32f4x_flash *regs = p->regs;
76+
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
77+
struct stm32l4x_flash *regs = p->regs;
78+
#endif
79+
80+
if (regs->acr.val & FLASH_ACR_ICEN) {
81+
regs->acr.val &= ~FLASH_ACR_ICEN;
82+
regs->acr.val |= FLASH_ACR_ICRST;
83+
regs->acr.val &= ~FLASH_ACR_ICRST;
84+
regs->acr.val |= FLASH_ACR_ICEN;
85+
}
86+
87+
if (regs->acr.val & FLASH_ACR_DCEN) {
88+
regs->acr.val &= ~FLASH_ACR_DCEN;
89+
regs->acr.val |= FLASH_ACR_DCRST;
90+
regs->acr.val &= ~FLASH_ACR_DCRST;
91+
regs->acr.val |= FLASH_ACR_DCEN;
92+
}
93+
}
94+
95+
static int flash_stm32_read(struct device *dev, off_t offset, void *data,
96+
size_t len)
97+
{
98+
if (!flash_stm32_valid_range(offset, len)) {
99+
return -EINVAL;
100+
}
101+
102+
if (!len) {
103+
return 0;
104+
}
105+
106+
memcpy(data, (void *) CONFIG_FLASH_BASE_ADDRESS + offset, len);
107+
108+
return 0;
109+
}
110+
111+
int flash_stm32_erase(struct device *dev, off_t offset, size_t len)
112+
{
113+
struct flash_stm32_priv *p = dev->driver_data;
114+
int rc;
115+
116+
if (!flash_stm32_valid_range(offset, len)) {
117+
return -EINVAL;
118+
}
119+
120+
if (!len) {
121+
return 0;
122+
}
123+
124+
k_sem_take(&p->sem, K_FOREVER);
125+
126+
rc = flash_stm32_block_erase_loop(offset, len, p);
127+
128+
flash_stm32_flush_caches(p);
129+
130+
k_sem_give(&p->sem);
131+
132+
return rc;
133+
}
134+
135+
int flash_stm32_write(struct device *dev, off_t offset,
136+
const void *data, size_t len)
137+
{
138+
struct flash_stm32_priv *p = dev->driver_data;
139+
int rc;
140+
141+
if (!flash_stm32_valid_range(offset, len)) {
142+
return -EINVAL;
143+
}
144+
145+
if (!len) {
146+
return 0;
147+
}
148+
149+
k_sem_take(&p->sem, K_FOREVER);
150+
151+
rc = flash_stm32_write_range(offset, data, len, p);
152+
153+
k_sem_give(&p->sem);
154+
155+
return rc;
156+
}
157+
158+
static int flash_stm32_write_protection(struct device *dev, bool enable)
159+
{
160+
struct flash_stm32_priv *p = dev->driver_data;
161+
#if defined(CONFIG_SOC_SERIES_STM32F4X)
162+
struct stm32f4x_flash *regs = p->regs;
163+
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
164+
struct stm32l4x_flash *regs = p->regs;
165+
#endif
166+
int rc = 0;
167+
168+
k_sem_take(&p->sem, K_FOREVER);
169+
170+
if (enable) {
171+
rc = flash_stm32_wait_flash_idle(p);
172+
if (rc) {
173+
k_sem_give(&p->sem);
174+
return rc;
175+
}
176+
regs->cr |= FLASH_CR_LOCK;
177+
} else {
178+
if (regs->cr & FLASH_CR_LOCK) {
179+
regs->keyr = FLASH_KEY1;
180+
regs->keyr = FLASH_KEY2;
181+
}
182+
}
183+
184+
k_sem_give(&p->sem);
185+
186+
return rc;
187+
}
188+
189+
static struct flash_stm32_priv flash_data = {
190+
#if defined(CONFIG_SOC_SERIES_STM32F4X)
191+
.regs = (struct stm32f4x_flash *) FLASH_R_BASE,
192+
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
193+
.regs = (struct stm32l4x_flash *) FLASH_R_BASE,
194+
.pclken = { .bus = STM32_CLOCK_BUS_AHB1,
195+
.enr = LL_AHB1_GRP1_PERIPH_FLASH },
196+
#endif
197+
};
198+
199+
static const struct flash_driver_api flash_stm32_api = {
200+
.write_protection = flash_stm32_write_protection,
201+
.erase = flash_stm32_erase,
202+
.write = flash_stm32_write,
203+
.read = flash_stm32_read,
204+
};
205+
206+
static int stm32_flash_init(struct device *dev)
207+
{
208+
struct flash_stm32_priv *p = dev->driver_data;
209+
#if defined(CONFIG_SOC_SERIES_STM32L4X)
210+
struct device *clk = device_get_binding(STM32_CLOCK_CONTROL_NAME);
211+
212+
/* enable clock */
213+
clock_control_on(clk, (clock_control_subsys_t *)&p->pclken);
214+
#endif
215+
216+
k_sem_init(&p->sem, 1, 1);
217+
218+
return flash_stm32_write_protection(dev, false);
219+
}
220+
221+
DEVICE_AND_API_INIT(stm32_flash, CONFIG_SOC_FLASH_STM32_DEV_NAME,
222+
stm32_flash_init, &flash_data, NULL, POST_KERNEL,
223+
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &flash_stm32_api);
224+

0 commit comments

Comments
 (0)