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