Skip to content

Commit c395f5e

Browse files
committed
alif/ospi_flash: Restore XIP settings after erase and write.
Signed-off-by: Damien George <[email protected]>
1 parent df5e4ce commit c395f5e

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

ports/alif/ospi_ext.c

Lines changed: 18 additions & 10 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+
static inline uint32_t ospi_xip_ctrlr0(uint32_t data_len) {
210+
return CTRLR0_IS_MST
211+
| (OCTAL << CTRLR0_SPI_FRF_OFFSET)
212+
| (0 << CTRLR0_SCPOL_OFFSET)
213+
| (0 << CTRLR0_SCPH_OFFSET)
214+
| (0 << CTRLR0_SSTE_OFFSET)
215+
| (TMOD_RO << CTRLR0_TMOD_OFFSET)
216+
| (data_len << CTRLR0_DFS_OFFSET);
217+
}
218+
209219
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) {
210220
spi_disable(ospi_cfg);
211221

212-
uint32_t val = CTRLR0_IS_MST
213-
| (OCTAL << CTRLR0_SPI_FRF_OFFSET)
214-
| (0 << CTRLR0_SCPOL_OFFSET)
215-
| (0 << CTRLR0_SCPH_OFFSET)
216-
| (0 << CTRLR0_SSTE_OFFSET)
217-
| (TMOD_RO << CTRLR0_TMOD_OFFSET)
218-
| (data_len << CTRLR0_DFS_OFFSET);
219-
220-
ospi_writel(ospi_cfg, ctrlr0, val);
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: 15 additions & 1 deletion
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

@@ -398,6 +406,8 @@ int ospi_flash_erase_sector(uint32_t addr) {
398406
ret = ospi_flash_wait_wip0(self);
399407
}
400408

409+
ospi_flash_xip_restore(self);
410+
401411
return ret;
402412
}
403413

@@ -442,6 +452,8 @@ static int ospi_flash_write_page(uint32_t addr, uint32_t len, const uint8_t *src
442452
}
443453

444454
int ospi_flash_write(uint32_t addr, uint32_t len, const uint8_t *src) {
455+
ospi_flash_t *self = &global_flash;
456+
445457
int ret = 0;
446458
uint32_t offset = addr & (PAGE_SIZE - 1);
447459

@@ -460,6 +472,8 @@ int ospi_flash_write(uint32_t addr, uint32_t len, const uint8_t *src) {
460472
offset = 0;
461473
}
462474

475+
ospi_flash_xip_restore(self);
476+
463477
return ret;
464478
}
465479

0 commit comments

Comments
 (0)