-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
soc_flash_nrf_rram.c
362 lines (288 loc) · 8.91 KB
/
soc_flash_nrf_rram.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT nordic_rram_controller
#include <string.h>
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/flash.h>
#include <zephyr/logging/log.h>
#include <zephyr/irq.h>
#include <zephyr/sys/barrier.h>
#include <hal/nrf_rramc.h>
#include <zephyr/../../drivers/flash/soc_flash_nrf.h>
/* Note that it is supported to compile this driver for both secure
* and non-secure images, but non-secure images cannot call
* nrf_rramc_config_set because NRF_RRAMC_NS does not exist.
*
* Instead, when TF-M boots, it will configure RRAMC with this static
* configuration:
*
* nrf_rramc_config_t config = {
* .mode_write = true,
* .write_buff_size = WRITE_BUFFER_SIZE
* };
*
* nrf_rramc_ready_next_timeout_t params = {
* .value = CONFIG_NRF_RRAM_READYNEXT_TIMEOUT_VALUE,
* .enable = true,
* };
*
* For more details see NCSDK-26982.
*/
LOG_MODULE_REGISTER(flash_nrf_rram, CONFIG_FLASH_LOG_LEVEL);
#define RRAM DT_INST(0, soc_nv_flash)
#define RRAM_START DT_REG_ADDR(RRAM)
#define RRAM_SIZE DT_REG_SIZE(RRAM)
#define PAGE_SIZE DT_PROP(RRAM, erase_block_size)
#define PAGE_COUNT ((RRAM_SIZE) / (PAGE_SIZE))
#define WRITE_BLOCK_SIZE_FROM_DT DT_PROP(RRAM, write_block_size)
#define ERASE_VALUE 0xFF
#ifdef CONFIG_MULTITHREADING
static struct k_sem sem_lock;
#define SYNC_INIT() k_sem_init(&sem_lock, 1, 1)
#define SYNC_LOCK() k_sem_take(&sem_lock, K_FOREVER)
#define SYNC_UNLOCK() k_sem_give(&sem_lock)
#else
#define SYNC_INIT()
#define SYNC_LOCK()
#define SYNC_UNLOCK()
#endif /* CONFIG_MULTITHREADING */
#if CONFIG_NRF_RRAM_WRITE_BUFFER_SIZE > 0
#define WRITE_BUFFER_ENABLE 1
#define WRITE_BUFFER_SIZE CONFIG_NRF_RRAM_WRITE_BUFFER_SIZE
#define WRITE_LINE_SIZE 16 /* In bytes, one line is 128 bits. */
#define WRITE_BUFFER_MAX_SIZE (WRITE_BUFFER_SIZE * WRITE_LINE_SIZE)
BUILD_ASSERT((PAGE_SIZE % (WRITE_LINE_SIZE) == 0), "erase-block-size must be a multiple of 16");
BUILD_ASSERT((WRITE_BLOCK_SIZE_FROM_DT % (WRITE_LINE_SIZE) == 0),
"if NRF_RRAM_WRITE_BUFFER_SIZE > 0, then write-block-size must be a multiple of 16");
#else
#define WRITE_BUFFER_ENABLE 0
#define WRITE_BUFFER_SIZE 0
#define WRITE_LINE_SIZE WRITE_BLOCK_SIZE_FROM_DT
#define WRITE_BUFFER_MAX_SIZE 16 /* In bytes, one line is 128 bits. */
BUILD_ASSERT((PAGE_SIZE % (WRITE_LINE_SIZE) == 0),
"erase-block-size must be a multiple of write-block-size");
#endif
#ifndef CONFIG_SOC_FLASH_NRF_RADIO_SYNC_NONE
#if (WRITE_BUFFER_SIZE < 2)
#define FLASH_SLOT_WRITE 500
#elif (WRITE_BUFFER_SIZE < 4)
#define FLASH_SLOT_WRITE 1000
#elif (WRITE_BUFFER_SIZE < 9)
#define FLASH_SLOT_WRITE 2000
#elif (WRITE_BUFFER_SIZE < 17)
#define FLASH_SLOT_WRITE 4000
#else
#define FLASH_SLOT_WRITE 8000 /* longest write takes 7107 us */
#endif
static int write_op(void *context); /* instance of flash_op_handler_t */
static int write_synchronously(off_t addr, const void *data, size_t len);
#endif /* !CONFIG_SOC_FLASH_NRF_RADIO_SYNC_NONE */
static inline bool is_within_bounds(off_t addr, size_t len, off_t boundary_start,
size_t boundary_size)
{
return (addr >= boundary_start && (addr < (boundary_start + boundary_size)) &&
(len <= (boundary_start + boundary_size - addr)));
}
#if WRITE_BUFFER_ENABLE
static void commit_changes(off_t addr, size_t len)
{
#if !defined(CONFIG_TRUSTED_EXECUTION_NONSECURE)
if (nrf_rramc_empty_buffer_check(NRF_RRAMC)) {
/* The internal write-buffer has been committed to RRAM and is now empty. */
return;
}
#endif
if ((len % (WRITE_BUFFER_MAX_SIZE)) == 0) {
/* Our last operation was buffer size-aligned, so we're done. */
return;
}
#if !defined(CONFIG_TRUSTED_EXECUTION_NONSECURE)
ARG_UNUSED(addr);
nrf_rramc_task_trigger(NRF_RRAMC, NRF_RRAMC_TASK_COMMIT_WRITEBUF);
#else
/*
* When the commit task is unavailable we need to get creative to
* ensure this is committed.
*
* According to the PS the buffer is committed when "There is a
* read operation from a 128-bit word line in the buffer that has
* already been written to".
*
* So we read the last byte that has been written to trigger this
* commit.
*
* If this approach proves to be problematic, e.g. for writes to
* write-only memory, then one would have to rely on
* READYNEXTTIMEOUT to eventually commit the write.
*/
volatile uint8_t dummy_read = *(volatile uint8_t *)(addr + len - 1);
ARG_UNUSED(dummy_read);
#endif
barrier_dmem_fence_full();
}
#endif
static void rram_write(off_t addr, const void *data, size_t len)
{
#if !defined(CONFIG_TRUSTED_EXECUTION_NONSECURE)
nrf_rramc_config_t config = {.mode_write = true, .write_buff_size = WRITE_BUFFER_SIZE};
nrf_rramc_config_set(NRF_RRAMC, &config);
#endif
if (data) {
memcpy((void *)addr, data, len);
} else {
memset((void *)addr, ERASE_VALUE, len);
}
barrier_dmem_fence_full(); /* Barrier following our last write. */
#if WRITE_BUFFER_ENABLE
commit_changes(addr, len);
#endif
#if !defined(CONFIG_TRUSTED_EXECUTION_NONSECURE)
config.mode_write = false;
nrf_rramc_config_set(NRF_RRAMC, &config);
#endif
}
#ifndef CONFIG_SOC_FLASH_NRF_RADIO_SYNC_NONE
static void shift_write_context(uint32_t shift, struct flash_context *w_ctx)
{
w_ctx->flash_addr += shift;
/* NULL data_addr => erase emulation request*/
if (w_ctx->data_addr) {
w_ctx->data_addr += shift;
}
w_ctx->len -= shift;
}
static int write_op(void *context)
{
struct flash_context *w_ctx = context;
size_t len;
uint32_t i = 0U;
if (w_ctx->enable_time_limit) {
nrf_flash_sync_get_timestamp_begin();
}
while (w_ctx->len > 0) {
len = (WRITE_BUFFER_MAX_SIZE < w_ctx->len) ? WRITE_BUFFER_MAX_SIZE : w_ctx->len;
rram_write(w_ctx->flash_addr, (const void *)w_ctx->data_addr, len);
shift_write_context(len, w_ctx);
if (w_ctx->len > 0) {
i++;
if (w_ctx->enable_time_limit) {
if (nrf_flash_sync_check_time_limit(i)) {
return FLASH_OP_ONGOING;
}
}
}
}
return FLASH_OP_DONE;
}
static int write_synchronously(off_t addr, const void *data, size_t len)
{
struct flash_context context = {
.data_addr = (uint32_t)data,
.flash_addr = addr,
.len = len,
.enable_time_limit = 1 /* enable time limit */
};
struct flash_op_desc flash_op_desc = {.handler = write_op, .context = &context};
nrf_flash_sync_set_context(FLASH_SLOT_WRITE);
return nrf_flash_sync_exe(&flash_op_desc);
}
#endif /* !CONFIG_SOC_FLASH_NRF_RADIO_SYNC_NONE */
static int nrf_write(off_t addr, const void *data, size_t len)
{
int ret = 0;
if (!is_within_bounds(addr, len, 0, RRAM_SIZE)) {
return -EINVAL;
}
addr += RRAM_START;
if (!len) {
return 0;
}
LOG_DBG("Write: %p:%zu", (void *)addr, len);
SYNC_LOCK();
#ifndef CONFIG_SOC_FLASH_NRF_RADIO_SYNC_NONE
if (nrf_flash_sync_is_required()) {
ret = write_synchronously(addr, data, len);
} else
#endif /* !CONFIG_SOC_FLASH_NRF_RADIO_SYNC_NONE */
{
rram_write(addr, data, len);
}
SYNC_UNLOCK();
return ret;
}
static int nrf_rram_read(const struct device *dev, off_t addr, void *data, size_t len)
{
ARG_UNUSED(dev);
if (!is_within_bounds(addr, len, 0, RRAM_SIZE)) {
return -EINVAL;
}
addr += RRAM_START;
memcpy(data, (void *)addr, len);
return 0;
}
static int nrf_rram_write(const struct device *dev, off_t addr, const void *data, size_t len)
{
ARG_UNUSED(dev);
if (data == NULL) {
return -EINVAL;
}
return nrf_write(addr, data, len);
}
static int nrf_rram_erase(const struct device *dev, off_t addr, size_t len)
{
ARG_UNUSED(dev);
return nrf_write(addr, NULL, len);
}
static const struct flash_parameters *nrf_rram_get_parameters(const struct device *dev)
{
ARG_UNUSED(dev);
static const struct flash_parameters parameters = {
.write_block_size = WRITE_LINE_SIZE,
.erase_value = ERASE_VALUE,
};
return ¶meters;
}
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
static void nrf_rram_page_layout(const struct device *dev, const struct flash_pages_layout **layout,
size_t *layout_size)
{
ARG_UNUSED(dev);
static const struct flash_pages_layout pages_layout = {
.pages_count = PAGE_COUNT,
.pages_size = PAGE_SIZE,
};
*layout = &pages_layout;
*layout_size = 1;
}
#endif
static const struct flash_driver_api nrf_rram_api = {
.read = nrf_rram_read,
.write = nrf_rram_write,
.erase = nrf_rram_erase,
.get_parameters = nrf_rram_get_parameters,
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
.page_layout = nrf_rram_page_layout,
#endif
};
static int nrf_rram_init(const struct device *dev)
{
ARG_UNUSED(dev);
SYNC_INIT();
#ifndef CONFIG_SOC_FLASH_NRF_RADIO_SYNC_NONE
nrf_flash_sync_init();
#endif /* !CONFIG_SOC_FLASH_NRF_RADIO_SYNC_NONE */
#if !defined(CONFIG_TRUSTED_EXECUTION_NONSECURE) && CONFIG_NRF_RRAM_READYNEXT_TIMEOUT_VALUE > 0
nrf_rramc_ready_next_timeout_t params = {
.value = CONFIG_NRF_RRAM_READYNEXT_TIMEOUT_VALUE,
.enable = true,
};
nrf_rramc_ready_next_timeout_set(NRF_RRAMC, ¶ms);
#endif
return 0;
}
DEVICE_DT_INST_DEFINE(0, nrf_rram_init, NULL, NULL, NULL, POST_KERNEL, CONFIG_FLASH_INIT_PRIORITY,
&nrf_rram_api);