Skip to content

Commit 602bc86

Browse files
iabdalkaderdpgeorge
authored andcommitted
alif/ospi_flash: Use OSPI in XIP mode only.
The OSPI controller supports concurrent direct/XIP accesses, there's no need to disable XIP on direct access. In addition to improving the performance, this change lays the groundwork for supporting access by the HP and HE cores simultaneously. Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent df06bf9 commit 602bc86

2 files changed

Lines changed: 12 additions & 31 deletions

File tree

ports/alif/ospi_flash.c

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ typedef struct _ospi_flash_t {
5555

5656
static ospi_flash_t global_flash;
5757

58-
static int ospi_flash_xip_enter(ospi_flash_t *self);
59-
static int ospi_flash_xip_exit(ospi_flash_t *self);
60-
6158
/******************************************************************************/
6259
// Generic SPI-flash helper functions.
6360

@@ -188,6 +185,7 @@ int ospi_flash_mx_init(ospi_flash_t *self) {
188185
break;
189186
}
190187
}
188+
191189
ospi_flash_wren_spi(self);
192190
uint8_t buf0[6] = {cmd_wrcr2, 0, 0, 3, 0, ddc_value};
193191
ospi_spi_transfer(&self->cfg, sizeof(buf0), buf0, buf0);
@@ -355,7 +353,7 @@ uintptr_t ospi_flash_get_xip_base(void) {
355353
return (uintptr_t)self->cfg.xip_base;
356354
}
357355

358-
static int ospi_flash_xip_enter(ospi_flash_t *self) {
356+
int ospi_flash_xip_enter(ospi_flash_t *self) {
359357
if (!self->xip_active) {
360358
uint32_t irq_state = disable_irq();
361359
self->xip_active = true;
@@ -366,7 +364,7 @@ static int ospi_flash_xip_enter(ospi_flash_t *self) {
366364
return 0;
367365
}
368366

369-
static int ospi_flash_xip_exit(ospi_flash_t *self) {
367+
int ospi_flash_xip_exit(ospi_flash_t *self) {
370368
if (self->xip_active) {
371369
uint32_t irq_state = disable_irq();
372370
ospi_xip_exit_ext(&self->cfg, self->set->inst_len, self->set->read_command, self->set->read_command);
@@ -382,43 +380,23 @@ static int ospi_flash_xip_exit(ospi_flash_t *self) {
382380
int ospi_flash_erase_sector(uint32_t addr) {
383381
ospi_flash_t *self = &global_flash;
384382

385-
ospi_flash_xip_exit(self);
386-
387383
ospi_flash_write_cmd(self, self->set->write_en);
388384
int ret = ospi_flash_wait_wel1(self);
389385
if (ret < 0) {
390-
ospi_flash_xip_enter(self);
391386
return ret;
392387
}
393388

394389
ospi_flash_write_cmd_addr(self, self->set->erase_command, OSPI_ADDR_L_32bit, addr);
395390
ret = ospi_flash_wait_wip0(self);
396391

397-
ospi_flash_xip_enter(self);
392+
SCB_InvalidateDCache_by_Addr(global_flash.cfg.xip_base + addr, MICROPY_HW_FLASH_BLOCK_SIZE_BYTES);
398393
return ret;
399394
}
400395

401396
int ospi_flash_read(uint32_t addr, uint32_t len, uint8_t *dest) {
402-
// OSPI FIFO is limited to 256 bytes. Need DMA to get a longer read.
403-
// Note that direct reading is much faster than using XIP memory-mapped read.
404397
ospi_flash_t *self = &global_flash;
405-
406-
ospi_flash_xip_exit(self);
407-
408-
while (len) {
409-
uint32_t l = len / 4;
410-
if (l > 256) {
411-
l = 256;
412-
}
413-
ospi_setup_read_ext(&self->cfg, self->set->rxds, self->set->inst_len, OSPI_ADDR_L_32bit, OSPI_DATA_L_32bit, l, self->set->read_dummy_cycles);
414-
ospi_push(&self->cfg, self->set->read_command);
415-
ospi_recv_blocking_32bit_data(&self->cfg, addr, (uint32_t *)dest);
416-
addr += l * 4;
417-
len -= l * 4;
418-
dest += l * 4;
419-
}
420-
421-
ospi_flash_xip_enter(self);
398+
// Perform an XIP read (memcpy)
399+
memcpy(dest, (void *)self->cfg.xip_base + addr, len);
422400
return 0;
423401
}
424402

@@ -448,10 +426,9 @@ static int ospi_flash_write_page(uint32_t addr, uint32_t len, const uint8_t *src
448426
}
449427

450428
int ospi_flash_write(uint32_t addr, uint32_t len, const uint8_t *src) {
451-
ospi_flash_xip_exit(&global_flash);
452-
453429
int ret = 0;
454430
uint32_t offset = addr & (PAGE_SIZE - 1);
431+
455432
while (len) {
456433
size_t rest = PAGE_SIZE - offset;
457434
if (rest > len) {
@@ -467,7 +444,7 @@ int ospi_flash_write(uint32_t addr, uint32_t len, const uint8_t *src) {
467444
offset = 0;
468445
}
469446

470-
ospi_flash_xip_enter(&global_flash);
447+
SCB_InvalidateDCache_by_Addr(global_flash.cfg.xip_base + addr, len);
471448
return ret;
472449
}
473450

ports/alif/ospi_flash.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ uint8_t ospi_flash_mx_read_cr2(struct _ospi_flash_t *self, uint32_t addr);
8989
int ospi_flash_mx_write_cr(struct _ospi_flash_t *self, uint8_t value);
9090
int ospi_flash_mx_write_cr2(struct _ospi_flash_t *self, uint32_t addr, uint8_t value);
9191

92+
// XIP control
93+
int ospi_flash_xip_enter(struct _ospi_flash_t *self);
94+
int ospi_flash_xip_exit(struct _ospi_flash_t *self);
95+
9296
// SPI flash interface.
9397
int ospi_flash_init(void);
9498
uintptr_t ospi_flash_get_xip_base(void);

0 commit comments

Comments
 (0)