Skip to content

Commit aecbcdc

Browse files
committed
ports/alif: OSPI flash fixes.
1 parent 03775bd commit aecbcdc

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

ports/alif/ospi_ext.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,22 @@ void ospi_setup_write_ext(ospi_flash_cfg_t *ospi_cfg, bool rxds, uint32_t inst_l
206206
spi_enable(ospi_cfg);
207207
}
208208

209-
void ospi_xip_enter_ext(ospi_flash_cfg_t *ospi_cfg, uint32_t inst_len, uint32_t data_len, uint16_t incr_command, uint16_t wrap_command, uint16_t read_dummy_cycles) {
210-
spi_disable(ospi_cfg);
211-
212-
uint32_t val = CTRLR0_IS_MST
209+
static inline uint32_t ospi_xip_ctrlr0(uint32_t data_len) {
210+
return CTRLR0_IS_MST
213211
| (OCTAL << CTRLR0_SPI_FRF_OFFSET)
214212
| (0 << CTRLR0_SCPOL_OFFSET)
215213
| (0 << CTRLR0_SCPH_OFFSET)
216214
| (0 << CTRLR0_SSTE_OFFSET)
217215
| (TMOD_RO << CTRLR0_TMOD_OFFSET)
218216
| (data_len << CTRLR0_DFS_OFFSET);
217+
}
219218

220-
ospi_writel(ospi_cfg, ctrlr0, val);
219+
void ospi_xip_enter_ext(ospi_flash_cfg_t *ospi_cfg, uint32_t inst_len, uint32_t data_len, uint16_t incr_command, uint16_t wrap_command, uint16_t read_dummy_cycles) {
220+
spi_disable(ospi_cfg);
221+
222+
ospi_writel(ospi_cfg, ctrlr0, ospi_xip_ctrlr0(data_len));
221223

222-
val = (OCTAL << XIP_CTRL_FRF_OFFSET)
224+
uint32_t val = (OCTAL << XIP_CTRL_FRF_OFFSET)
223225
| (0x2 << XIP_CTRL_TRANS_TYPE_OFFSET)
224226
| (ADDR_L32bit << XIP_CTRL_ADDR_L_OFFSET)
225227
| (inst_len << XIP_CTRL_INST_L_OFFSET)
@@ -292,3 +294,9 @@ void ospi_xip_exit_ext(ospi_flash_cfg_t *ospi_cfg, uint32_t inst_len, uint16_t i
292294
ospi_xip_enable(ospi_cfg);
293295
ospi_xip_disable(ospi_cfg);
294296
}
297+
298+
void ospi_xip_restore_ext(ospi_flash_cfg_t *ospi_cfg, uint32_t data_len) {
299+
spi_disable(ospi_cfg);
300+
ospi_writel(ospi_cfg, ctrlr0, ospi_xip_ctrlr0(data_len));
301+
spi_enable(ospi_cfg);
302+
}

ports/alif/ospi_ext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ void ospi_setup_write_ext(ospi_flash_cfg_t *ospi_cfg, bool rxds, uint32_t inst_l
5353

5454
void ospi_xip_enter_ext(ospi_flash_cfg_t *ospi_cfg, uint32_t inst_len, uint32_t data_len, uint16_t incr_command, uint16_t wrap_command, uint16_t read_dummy_cycles);
5555
void ospi_xip_exit_ext(ospi_flash_cfg_t *ospi_cfg, uint32_t inst_len, uint16_t incr_command, uint16_t wrap_command);
56+
void ospi_xip_restore_ext(ospi_flash_cfg_t *ospi_cfg, uint32_t data_len);
5657

5758
#endif // MICROPY_INCLUDED_ALIF_OSPI_EXT_H

ports/alif/ospi_flash.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,9 @@ int ospi_flash_init(void) {
354354
}
355355
}
356356

357-
// Enter XIP mode. It will be disabled during flash read/erase/write.
357+
// Enter XIP mode.
358358
ospi_flash_xip_enter(self);
359+
359360
return 0;
360361
}
361362

@@ -385,6 +386,13 @@ int ospi_flash_xip_exit(ospi_flash_t *self) {
385386
return 0;
386387
}
387388

389+
int ospi_flash_xip_restore(ospi_flash_t *self) {
390+
if (self->xip_active) {
391+
ospi_xip_restore_ext(&self->cfg, self->set->xip_data_len);
392+
}
393+
return 0;
394+
}
395+
388396
/******************************************************************************/
389397
// Top-level read/erase/write functions.
390398

@@ -393,14 +401,13 @@ int ospi_flash_erase_sector(uint32_t addr) {
393401

394402
ospi_flash_write_cmd(self, self->set->write_en);
395403
int ret = ospi_flash_wait_wel1(self);
396-
if (ret < 0) {
397-
return ret;
404+
if (ret == 0) {
405+
ospi_flash_write_cmd_addr(self, self->set->erase_command, OSPI_ADDR_L_32bit, addr);
406+
ret = ospi_flash_wait_wip0(self);
398407
}
399408

400-
ospi_flash_write_cmd_addr(self, self->set->erase_command, OSPI_ADDR_L_32bit, addr);
401-
ret = ospi_flash_wait_wip0(self);
409+
ospi_flash_xip_restore(self);
402410

403-
SCB_InvalidateDCache_by_Addr(global_flash.cfg.xip_base + addr, MICROPY_HW_FLASH_BLOCK_SIZE_BYTES);
404411
return ret;
405412
}
406413

@@ -445,6 +452,8 @@ static int ospi_flash_write_page(uint32_t addr, uint32_t len, const uint8_t *src
445452
}
446453

447454
int ospi_flash_write(uint32_t addr, uint32_t len, const uint8_t *src) {
455+
ospi_flash_t *self = &global_flash;
456+
448457
int ret = 0;
449458
uint32_t offset = addr & (PAGE_SIZE - 1);
450459

@@ -463,7 +472,8 @@ int ospi_flash_write(uint32_t addr, uint32_t len, const uint8_t *src) {
463472
offset = 0;
464473
}
465474

466-
SCB_InvalidateDCache_by_Addr(global_flash.cfg.xip_base + addr, len);
475+
ospi_flash_xip_restore(self);
476+
467477
return ret;
468478
}
469479

0 commit comments

Comments
 (0)