Skip to content

Commit ecaec62

Browse files
authored
Merge pull request #2 from nuvoton-CS00/yachen0
Update zephyr base
2 parents 72f51b3 + dbce59a commit ecaec62

File tree

12 files changed

+889
-74
lines changed

12 files changed

+889
-74
lines changed

drivers/flash/flash_npcm_nor.c

Lines changed: 92 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
#include <zephyr/logging/log.h>
2424
LOG_MODULE_REGISTER(flash_npcm_nor, CONFIG_FLASH_LOG_LEVEL);
2525

26+
#ifdef CONFIG_XIP
27+
#include <zephyr/linker/linker-defs.h>
28+
#define RAMFUNC __attribute__ ((section(".ramfunc")))
29+
#else
30+
#define RAMFUNC
31+
#endif
32+
2633
#define BLOCK_64K_SIZE KB(64)
2734
#define BLOCK_4K_SIZE KB(4)
2835
#define MAPPED_ADDR_NOT_SUPPORT 0xffffffff
@@ -61,22 +68,29 @@ static const struct flash_parameters flash_npcm_parameters = {
6168
DT_INST_STRING_TOKEN(inst, quad_enable_requirements))), \
6269
((JESD216_DW15_QER_VAL_NONE)))
6370

64-
static inline bool is_within_region(off_t addr, size_t size, off_t region_start,
71+
RAMFUNC static inline bool is_within_region(off_t addr, size_t size, off_t region_start,
6572
size_t region_size)
6673
{
6774
return (addr >= region_start &&
6875
(addr < (region_start + region_size)) &&
6976
((addr + size) <= (region_start + region_size)));
7077
}
7178

72-
static int flash_npcm_transceive(const struct device *dev, struct npcm_transceive_cfg *cfg,
79+
RAMFUNC static int flash_npcm_transceive(const struct device *dev, struct npcm_transceive_cfg *cfg,
7380
uint32_t flags)
7481
{
7582
const struct flash_npcm_nor_config *config = dev->config;
7683
struct flash_npcm_nor_data *data = dev->data;
7784
struct npcm_qspi_data *qspi_data = config->qspi_bus->data;
7885
int ret;
7986

87+
#ifdef CONFIG_XIP
88+
unsigned int key = 0;
89+
90+
/* Disable IRQ */
91+
key = irq_lock();
92+
#endif
93+
8094
/* Lock SPI bus and configure it if needed */
8195
qspi_data->qspi_ops->lock_configure(config->qspi_bus, &config->qspi_cfg,
8296
data->operation);
@@ -87,18 +101,22 @@ static int flash_npcm_transceive(const struct device *dev, struct npcm_transceiv
87101
/* Unlock SPI bus */
88102
qspi_data->qspi_ops->unlock(config->qspi_bus);
89103

104+
#ifdef CONFIG_XIP
105+
irq_unlock(key);
106+
#endif
107+
90108
return ret;
91109
}
92110

93111
/* NPCM functions for SPI NOR flash */
94-
static int flash_npcm_transceive_cmd_only(const struct device *dev, uint8_t opcode)
112+
RAMFUNC static int flash_npcm_transceive_cmd_only(const struct device *dev, uint8_t opcode)
95113
{
96114
struct npcm_transceive_cfg cfg = { .opcode = opcode};
97115

98116
return flash_npcm_transceive(dev, &cfg, 0); /* opcode only */
99117
}
100118

101-
static int flash_npcm_transceive_cmd_by_addr(const struct device *dev, uint8_t opcode,
119+
RAMFUNC static int flash_npcm_transceive_cmd_by_addr(const struct device *dev, uint8_t opcode,
102120
uint32_t addr)
103121
{
104122
struct npcm_transceive_cfg cfg = { .opcode = opcode};
@@ -107,7 +125,7 @@ static int flash_npcm_transceive_cmd_by_addr(const struct device *dev, uint8_t o
107125
return flash_npcm_transceive(dev, &cfg, NPCM_TRANSCEIVE_ACCESS_ADDR);
108126
}
109127

110-
static int flash_npcm_transceive_read_by_addr(const struct device *dev, uint8_t opcode,
128+
RAMFUNC static int flash_npcm_transceive_read_by_addr(const struct device *dev, uint8_t opcode,
111129
uint8_t *dst, const size_t size, uint32_t addr)
112130
{
113131
struct npcm_transceive_cfg cfg = { .opcode = opcode,
@@ -119,7 +137,7 @@ static int flash_npcm_transceive_read_by_addr(const struct device *dev, uint8_t
119137
NPCM_TRANSCEIVE_ACCESS_ADDR);
120138
}
121139

122-
static int flash_npcm_transceive_read(const struct device *dev, uint8_t opcode,
140+
RAMFUNC static int flash_npcm_transceive_read(const struct device *dev, uint8_t opcode,
123141
uint8_t *dst, const size_t size)
124142
{
125143
struct npcm_transceive_cfg cfg = { .opcode = opcode,
@@ -129,7 +147,7 @@ static int flash_npcm_transceive_read(const struct device *dev, uint8_t opcode,
129147
return flash_npcm_transceive(dev, &cfg, NPCM_TRANSCEIVE_ACCESS_READ);
130148
}
131149

132-
static int flash_npcm_transceive_write(const struct device *dev, uint8_t opcode,
150+
RAMFUNC static int flash_npcm_transceive_write(const struct device *dev, uint8_t opcode,
133151
uint8_t *src, const size_t size)
134152
{
135153
struct npcm_transceive_cfg cfg = { .opcode = opcode,
@@ -139,7 +157,7 @@ static int flash_npcm_transceive_write(const struct device *dev, uint8_t opcode,
139157
return flash_npcm_transceive(dev, &cfg, NPCM_TRANSCEIVE_ACCESS_WRITE);
140158
}
141159

142-
static int flash_npcm_transceive_write_by_addr(const struct device *dev, uint8_t opcode,
160+
RAMFUNC static int flash_npcm_transceive_write_by_addr(const struct device *dev, uint8_t opcode,
143161
uint8_t *src, const size_t size, uint32_t addr)
144162
{
145163
struct npcm_transceive_cfg cfg = { .opcode = opcode,
@@ -152,7 +170,7 @@ static int flash_npcm_transceive_write_by_addr(const struct device *dev, uint8_t
152170
}
153171

154172
/* Local SPI NOR flash functions */
155-
static int flash_npcm_nor_wait_until_ready(const struct device *dev)
173+
RAMFUNC static int flash_npcm_nor_wait_until_ready(const struct device *dev)
156174
{
157175
int ret;
158176
uint8_t reg;
@@ -172,36 +190,51 @@ static int flash_npcm_nor_wait_until_ready(const struct device *dev)
172190
return -EBUSY;
173191
}
174192

175-
static int flash_npcm_nor_read_status_regs(const struct device *dev, uint8_t *sts_reg)
193+
RAMFUNC static int flash_npcm_nor_read_status_regs(const struct device *dev, uint8_t *sts_reg)
176194
{
177195
int ret = flash_npcm_transceive_read(dev, SPI_NOR_CMD_RDSR, sts_reg, 1);
178196

179197
if (ret != 0) {
180198
return ret;
181199
}
200+
182201
return flash_npcm_transceive_read(dev, SPI_NOR_CMD_RDSR2, sts_reg + 1, 1);
183202
}
184203

185-
static int flash_npcm_nor_write_status_regs(const struct device *dev, uint8_t *sts_reg)
204+
RAMFUNC static int flash_npcm_nor_write_status_regs(const struct device *dev, uint8_t *sts_reg)
186205
{
187206
int ret;
188207

208+
#ifdef CONFIG_XIP
209+
unsigned int key = 0;
210+
211+
key = irq_lock();
212+
#endif
213+
189214
ret = flash_npcm_transceive_cmd_only(dev, SPI_NOR_CMD_WREN);
190215
if (ret != 0) {
191-
return ret;
216+
goto exit;
192217
}
193218

194219
ret = flash_npcm_transceive_write(dev, SPI_NOR_CMD_WRSR, sts_reg, 2);
195220
if (ret != 0) {
196-
return ret;
221+
goto exit;
197222
}
198223

199-
return flash_npcm_nor_wait_until_ready(dev);
224+
ret = flash_npcm_nor_wait_until_ready(dev);
225+
226+
exit:
227+
228+
#ifdef CONFIG_XIP
229+
key = irq_lock();
230+
#endif
231+
232+
return ret;
200233
}
201234

202235
/* Flash API functions */
203236
#if defined(CONFIG_FLASH_JESD216_API)
204-
static int flash_npcm_nor_read_jedec_id(const struct device *dev, uint8_t *id)
237+
RAMFUNC static int flash_npcm_nor_read_jedec_id(const struct device *dev, uint8_t *id)
205238
{
206239
if (id == NULL) {
207240
return -EINVAL;
@@ -210,7 +243,7 @@ static int flash_npcm_nor_read_jedec_id(const struct device *dev, uint8_t *id)
210243
return flash_npcm_transceive_read(dev, SPI_NOR_CMD_RDID, id, SPI_NOR_MAX_ID_LEN);
211244
}
212245

213-
static int flash_npcm_nor_read_sfdp(const struct device *dev, off_t addr,
246+
RAMFUNC static int flash_npcm_nor_read_sfdp(const struct device *dev, off_t addr,
214247
void *data, size_t size)
215248
{
216249
uint8_t sfdp_addr[4];
@@ -234,7 +267,7 @@ static int flash_npcm_nor_read_sfdp(const struct device *dev, off_t addr,
234267
#endif /* CONFIG_FLASH_JESD216_API */
235268

236269
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
237-
static void flash_npcm_nor_pages_layout(const struct device *dev,
270+
RAMFUNC static void flash_npcm_nor_pages_layout(const struct device *dev,
238271
const struct flash_pages_layout **layout,
239272
size_t *layout_size)
240273
{
@@ -245,7 +278,7 @@ static void flash_npcm_nor_pages_layout(const struct device *dev,
245278
}
246279
#endif /* CONFIG_FLASH_PAGE_LAYOUT */
247280

248-
static int flash_npcm_nor_read(const struct device *dev, off_t addr,
281+
RAMFUNC static int flash_npcm_nor_read(const struct device *dev, off_t addr,
249282
void *data, size_t size)
250283
{
251284
const struct flash_npcm_nor_config *config = dev->config;
@@ -275,11 +308,15 @@ static int flash_npcm_nor_read(const struct device *dev, off_t addr,
275308
return 0;
276309
}
277310

278-
static int flash_npcm_nor_erase(const struct device *dev, off_t addr, size_t size)
311+
RAMFUNC static int flash_npcm_nor_erase(const struct device *dev, off_t addr, size_t size)
279312
{
280313
const struct flash_npcm_nor_config *config = dev->config;
281314
int ret = 0;
282315

316+
#ifdef CONFIG_XIP
317+
unsigned int key = 0;
318+
#endif
319+
283320
/* Out of the region of nor flash device? */
284321
if (!is_within_region(addr, size, 0, config->flash_size)) {
285322
LOG_ERR("Addr %ld, size %d are out of range", addr, size);
@@ -298,6 +335,15 @@ static int flash_npcm_nor_erase(const struct device *dev, off_t addr, size_t siz
298335
return -EINVAL;
299336
}
300337

338+
#ifdef CONFIG_XIP
339+
/* if execute in the place, disable IRQ to avoid interrupt and context
340+
* switch, to make sure always execute code in the RAM(SRAM) when use
341+
* spim driver. npcm4xx only have one core, we don't need use lock to
342+
* handle race condition.
343+
*/
344+
key = irq_lock();
345+
#endif
346+
301347
/* Select erase opcode by size */
302348
if (size == config->flash_size) {
303349
flash_npcm_transceive_cmd_only(dev, SPI_NOR_CMD_WREN);
@@ -324,17 +370,28 @@ static int flash_npcm_nor_erase(const struct device *dev, off_t addr, size_t siz
324370
}
325371
}
326372

373+
#ifdef CONFIG_XIP
374+
irq_unlock(key);
375+
#endif
376+
327377
return ret;
328378
}
329379

330-
static int flash_npcm_nor_write(const struct device *dev, off_t addr,
380+
uintptr_t npcm4xx_vector_table_save(void);
381+
void npcm4xx_vector_table_restore(uintptr_t vtor);
382+
383+
RAMFUNC static int flash_npcm_nor_write(const struct device *dev, off_t addr,
331384
const void *data, size_t size)
332385
{
333386
const struct flash_npcm_nor_config *config = dev->config;
334387
uint8_t *tx_buf = (uint8_t *)data;
335388
int ret = 0;
336389
size_t sz_write;
337390

391+
#ifdef CONFIG_XIP
392+
unsigned int key = 0;
393+
#endif
394+
338395
/* Out of the region of nor flash device? */
339396
if (!is_within_region(addr, size, 0, config->flash_size)) {
340397
return -EINVAL;
@@ -355,6 +412,10 @@ static int flash_npcm_nor_write(const struct device *dev, off_t addr,
355412
sz_write -= (addr + sz_write) & (SPI_NOR_PAGE_SIZE - 1);
356413
}
357414

415+
#ifdef CONFIG_XIP
416+
key = irq_lock();
417+
#endif
418+
358419
while (size > 0) {
359420
/* Start to write */
360421
flash_npcm_transceive_cmd_only(dev, SPI_NOR_CMD_WREN);
@@ -381,10 +442,14 @@ static int flash_npcm_nor_write(const struct device *dev, off_t addr,
381442
}
382443
}
383444

445+
#ifdef CONFIG_XIP
446+
irq_unlock(key);
447+
#endif
448+
384449
return ret;
385450
}
386451

387-
static const struct flash_parameters *
452+
RAMFUNC static const struct flash_parameters *
388453
flash_npcm_nor_get_parameters(const struct device *dev)
389454
{
390455
ARG_UNUSED(dev);
@@ -393,7 +458,7 @@ flash_npcm_nor_get_parameters(const struct device *dev)
393458
};
394459

395460
#ifdef CONFIG_FLASH_EX_OP_ENABLED
396-
static int flash_npcm_nor_ex_exec_transceive(const struct device *dev,
461+
RAMFUNC static int flash_npcm_nor_ex_exec_transceive(const struct device *dev,
397462
const struct npcm_ex_ops_transceive_in *op_in,
398463
const struct npcm_ex_ops_transceive_out *op_out)
399464
{
@@ -426,7 +491,7 @@ static int flash_npcm_nor_ex_exec_transceive(const struct device *dev,
426491
return flash_npcm_transceive(dev, &cfg, flag);
427492
}
428493

429-
static int flash_npcm_nor_ex_set_spi_spec(const struct device *dev,
494+
RAMFUNC static int flash_npcm_nor_ex_set_spi_spec(const struct device *dev,
430495
const struct npcm_ex_ops_qspi_oper_in *op_in)
431496
{
432497
struct flash_npcm_nor_data *data = dev->data;
@@ -440,7 +505,7 @@ static int flash_npcm_nor_ex_set_spi_spec(const struct device *dev,
440505
return 0;
441506
}
442507

443-
static int flash_npcm_nor_ex_get_spi_spec(const struct device *dev,
508+
RAMFUNC static int flash_npcm_nor_ex_get_spi_spec(const struct device *dev,
444509
struct npcm_ex_ops_qspi_oper_out *op_out)
445510
{
446511
struct flash_npcm_nor_data *data = dev->data;
@@ -449,7 +514,7 @@ static int flash_npcm_nor_ex_get_spi_spec(const struct device *dev,
449514
return 0;
450515
}
451516

452-
static int flash_npcm_nor_ex_op(const struct device *dev, uint16_t code,
517+
RAMFUNC static int flash_npcm_nor_ex_op(const struct device *dev, uint16_t code,
453518
const uintptr_t in, void *out)
454519
{
455520
#ifdef CONFIG_USERSPACE
@@ -542,7 +607,7 @@ static const struct flash_driver_api flash_npcm_nor_driver_api = {
542607
#endif
543608
};
544609

545-
static int flash_npcm_nor_init(const struct device *dev)
610+
RAMFUNC static int flash_npcm_nor_init(const struct device *dev)
546611
{
547612
const struct flash_npcm_nor_config *config = dev->config;
548613
int ret;
@@ -611,7 +676,7 @@ BUILD_ASSERT(DT_INST_QUAD_EN_PROP_OR(n) == JESD216_DW15_QER_NONE || \
611676
DT_INST_STRING_TOKEN(n, rd_mode) == NPCM_RD_MODE_FAST_DUAL, \
612677
"Fast Dual IO read must be selected in Quad mode"); \
613678
PINCTRL_DT_INST_DEFINE(n); \
614-
static const struct flash_npcm_nor_config flash_npcm_nor_config_##n = { \
679+
static struct flash_npcm_nor_config flash_npcm_nor_config_##n = { \
615680
.qspi_bus = DEVICE_DT_GET(DT_PARENT(DT_DRV_INST(n))), \
616681
.mapped_addr = DT_INST_PROP_OR(n, mapped_addr, MAPPED_ADDR_NOT_SUPPORT),\
617682
.flash_size = DT_INST_PROP(n, size) / 8, \

0 commit comments

Comments
 (0)