Skip to content

Commit 8807f8d

Browse files
iabdalkaderdpgeorge
authored andcommitted
alif/ospi_flash: Configure dummy cycles.
The default dummy cycles may not match the actual flash frequency supported by a certain board. For example, the MX chip uses 20 dummy cycles by default which supports up to 200MHz DDR, but the maximum frequency supported by the AE3 board is 50MHz DDR. So the dummy cycles for this board can be as low as 6. It's important to set the correct dummy cycles, as it results in doubling the XIP read speed, in the case of the AE3 board. Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent aec0300 commit 8807f8d

3 files changed

Lines changed: 38 additions & 17 deletions

File tree

ports/alif/ospi_flash.c

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,22 +146,27 @@ static uint32_t ospi_flash_read_id(ospi_flash_t *self) {
146146
/******************************************************************************/
147147
// Functions specific to ISSI flash chips.
148148

149-
int ospi_flash_issi_octal_switch(ospi_flash_t *self) {
150-
// Switch SPI flash to Octal DDR mode.
149+
int ospi_flash_issi_init(ospi_flash_t *self) {
151150
const uint8_t cmd_wrvol = 0x81;
151+
152+
// Configure dummy cycles.
153+
ospi_flash_wren_spi(self);
154+
uint8_t buf0[5] = {cmd_wrvol, 0, 0, 1, self->set->read_dummy_cycles};
155+
ospi_spi_transfer(&self->cfg, sizeof(buf0), buf0, buf0);
156+
157+
// Switch SPI flash to Octal DDR mode.
152158
const uint8_t issi_mode_octal_ddr_dqs = 0xe7;
153159
ospi_flash_wren_spi(self);
154-
uint8_t buf[5] = {cmd_wrvol, 0, 0, 0, issi_mode_octal_ddr_dqs};
155-
ospi_spi_transfer(&self->cfg, sizeof(buf), buf, buf);
160+
uint8_t buf1[5] = {cmd_wrvol, 0, 0, 0, issi_mode_octal_ddr_dqs};
161+
ospi_spi_transfer(&self->cfg, sizeof(buf1), buf1, buf1);
156162
self->cfg.ddr_en = 1;
157163
return 0;
158164
}
159165

160166
/******************************************************************************/
161167
// Functions specific to MX flash chips.
162168

163-
int ospi_flash_mx_octal_switch(ospi_flash_t *self) {
164-
// Switch SPI flash to Octal SDR or DDR mode (SOPI or DOPI) by writing to CR2.
169+
int ospi_flash_mx_init(ospi_flash_t *self) {
165170
const uint8_t cmd_wrcr2 = 0x72;
166171
const uint8_t mx_mode_enable_sopi = 0x01;
167172
const uint8_t mx_mode_enable_dopi = 0x02;
@@ -171,9 +176,26 @@ int ospi_flash_mx_octal_switch(ospi_flash_t *self) {
171176
} else {
172177
mx_mode = mx_mode_enable_sopi;
173178
}
179+
180+
// Configure dummy cycles.
181+
uint8_t ddc_value = 0;
182+
const uint8_t ospi_flash_mx_ddc[][2] = {
183+
{20, 0}, {18, 1}, {16, 2}, {14, 3}, {12, 4}, {10, 5}, {8, 6}, {6, 7}
184+
};
185+
for (size_t i = 0; i < MP_ARRAY_SIZE(ospi_flash_mx_ddc); i++) {
186+
if (self->set->read_dummy_cycles == ospi_flash_mx_ddc[i][0]) {
187+
ddc_value = ospi_flash_mx_ddc[i][1];
188+
break;
189+
}
190+
}
174191
ospi_flash_wren_spi(self);
175-
uint8_t buf[6] = {cmd_wrcr2, 0, 0, 0, 0, mx_mode};
176-
ospi_spi_transfer(&self->cfg, sizeof(buf), buf, buf);
192+
uint8_t buf0[6] = {cmd_wrcr2, 0, 0, 3, 0, ddc_value};
193+
ospi_spi_transfer(&self->cfg, sizeof(buf0), buf0, buf0);
194+
195+
// Switch SPI flash to Octal SDR or DDR mode.
196+
ospi_flash_wren_spi(self);
197+
uint8_t buf1[6] = {cmd_wrcr2, 0, 0, 0, 0, mx_mode};
198+
ospi_spi_transfer(&self->cfg, sizeof(buf1), buf1, buf1);
177199
if (self->set->octal_mode == OSPI_FLASH_OCTAL_MODE_DDD) {
178200
self->cfg.ddr_en = 1;
179201
} else {
@@ -310,8 +332,8 @@ int ospi_flash_init(void) {
310332
ospi_init(&self->cfg);
311333

312334
// Switch to octal mode if needed.
313-
if (set->octal_switch != NULL) {
314-
set->octal_switch(self);
335+
if (set->flash_init != NULL) {
336+
set->flash_init(self);
315337

316338
// Check the device ID after switching mode.
317339
if (ospi_flash_read_id(self) != set->jedec_id) {
@@ -321,7 +343,6 @@ int ospi_flash_init(void) {
321343

322344
// Enter XIP mode. It will be disabled during flash read/erase/write.
323345
ospi_flash_xip_enter(self);
324-
325346
return 0;
326347
}
327348

ports/alif/ospi_flash.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ typedef struct _ospi_pin_settings_t {
5757
typedef struct _ospi_flash_settings_t {
5858
uint32_t jedec_id;
5959
uint32_t freq_hz;
60-
int (*octal_switch)(struct _ospi_flash_t *);
60+
int (*flash_init)(struct _ospi_flash_t *);
6161
uint8_t octal_mode;
6262
bool rxds;
6363
uint8_t inst_len;
@@ -79,10 +79,10 @@ extern const ospi_flash_settings_t ospi_flash_settings[];
7979
extern const size_t ospi_flash_settings_len;
8080

8181
// Functions specific to ISSI flash chips.
82-
int ospi_flash_issi_octal_switch(struct _ospi_flash_t *self);
82+
int ospi_flash_issi_init(struct _ospi_flash_t *self);
8383

8484
// Functions specific to MX flash chips.
85-
int ospi_flash_mx_octal_switch(struct _ospi_flash_t *self);
85+
int ospi_flash_mx_init(struct _ospi_flash_t *self);
8686
uint8_t ospi_flash_mx_read_cr(struct _ospi_flash_t *self);
8787
uint8_t ospi_flash_mx_read_cr2(struct _ospi_flash_t *self, uint32_t addr);
8888
int ospi_flash_mx_write_cr(struct _ospi_flash_t *self, uint8_t value);

ports/alif/ospi_flash_settings.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
// Macronix MX25
3333
#define OSPI_FLASH_SETTINGS_MX25 \
34-
.octal_switch = ospi_flash_mx_octal_switch, \
34+
.flash_init = ospi_flash_mx_init, \
3535
.octal_mode = OSPI_FLASH_OCTAL_MODE_DDD, \
3636
.rxds = true, \
3737
.inst_len = OSPI_INST_L_16bit, \
@@ -47,7 +47,7 @@
4747

4848
// Everspin EM.
4949
#define OSPI_FLASH_SETTINGS_EM \
50-
.octal_switch = ospi_flash_issi_octal_switch, \
50+
.flash_init = ospi_flash_issi_init, \
5151
.octal_mode = OSPI_FLASH_OCTAL_MODE_DDD, \
5252
.rxds = false, \
5353
.inst_len = OSPI_INST_L_8bit, \
@@ -63,7 +63,7 @@
6363

6464
// ISSI IS25.
6565
#define OSPI_FLASH_SETTINGS_IS25 \
66-
.octal_switch = ospi_flash_issi_octal_switch, \
66+
.flash_init = ospi_flash_issi_init, \
6767
.octal_mode = OSPI_FLASH_OCTAL_MODE_DDD, \
6868
.rxds = true, \
6969
.inst_len = OSPI_INST_L_8bit, \

0 commit comments

Comments
 (0)