Skip to content

Commit db57018

Browse files
Oliver Joosoliver-joos
authored andcommitted
drivers/memory/spiflash: Implement QSPI for ISSI flash chips.
Signed-off-by: Oliver Joos <[email protected]>
1 parent 1dedb65 commit db57018

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

drivers/memory/spiflash.c

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -180,29 +180,43 @@ void mp_spiflash_init(mp_spiflash_t *self) {
180180
// Ensure SPI flash is out of sleep mode
181181
mp_spiflash_deepsleep_internal(self, 0);
182182

183-
#if defined(CHECK_DEVID)
184-
// Validate device id
183+
// Read JEDEC ID:
184+
// "ISSI IS25LP256" = 0x0019609d (ISSI = 0x9d)
185+
// "Winbond W25Q256" = 0x001940ef (Winbond = 0xef)
185186
uint32_t devid;
186187
int ret = mp_spiflash_read_cmd(self, CMD_RD_DEVID, 3, &devid);
187-
if (ret != 0 || devid != CHECK_DEVID) {
188+
if (ret != 0) {
188189
mp_spiflash_release_bus(self);
189190
return;
190191
}
191-
#endif
192192

193193
if (self->config->bus_kind == MP_SPIFLASH_BUS_QSPI) {
194194
// Set QE bit
195-
uint32_t sr = 0, cr = 0;
196-
int ret = mp_spiflash_read_cmd(self, CMD_RDSR, 1, &sr);
197-
if (ret == 0) {
198-
ret = mp_spiflash_read_cmd(self, CMD_RDCR, 1, &cr);
199-
}
200-
uint32_t data = (sr & 0xff) | (cr & 0xff) << 8;
201-
if (ret == 0 && !(data & (QSPI_QE_MASK << 8))) {
202-
data |= QSPI_QE_MASK << 8;
203-
mp_spiflash_write_cmd(self, CMD_WREN);
204-
mp_spiflash_write_cmd_data(self, CMD_WRSR, 2, data);
205-
mp_spiflash_wait_wip0(self);
195+
if ((devid & 0xff) == 0x9d) {
196+
// Manufacturer ISSI has QE in bit 6 of a 1-byte status register
197+
uint32_t sr = 0;
198+
int ret = mp_spiflash_read_cmd(self, CMD_RDSR, 1, &sr);
199+
uint32_t data = (sr & 0xff);
200+
if (ret == 0 && !(data & (1 << 6))) {
201+
data |= 1 << 6;
202+
mp_spiflash_write_cmd(self, CMD_WREN);
203+
mp_spiflash_write_cmd_data(self, CMD_WRSR, 1, data);
204+
mp_spiflash_wait_wip0(self);
205+
}
206+
} else {
207+
// MicroPython default writes QE to bit 9 of a 2-byte status register (e.g. Winbond)
208+
uint32_t sr = 0, cr = 0;
209+
int ret = mp_spiflash_read_cmd(self, CMD_RDSR, 1, &sr);
210+
if (ret == 0) {
211+
ret = mp_spiflash_read_cmd(self, CMD_RDCR, 1, &cr);
212+
}
213+
uint32_t data = (sr & 0xff) | (cr & 0xff) << 8;
214+
if (ret == 0 && !(data & (QSPI_QE_MASK << 8))) {
215+
data |= QSPI_QE_MASK << 8;
216+
mp_spiflash_write_cmd(self, CMD_WREN);
217+
mp_spiflash_write_cmd_data(self, CMD_WRSR, 2, data);
218+
mp_spiflash_wait_wip0(self);
219+
}
206220
}
207221
}
208222

0 commit comments

Comments
 (0)