1
6
7
14
15
21
22
23
24
25
26
27
28
29
32
33
37
38
39
40
41
42
43
44
45
46
48
51
52
53
54
55
56
64
65
66
67
68
69
70
71
79
80
81
82
85
86
87
88
96
97
98
99
100
101
102
109
110
111
112
120
121
122
126
127
128
129
130
131
132
133
134
135
136
137
144
145
148
149
150
151
152
153
154
155
156
157
158
159
160
163
164
165
166
167
168
169
170
171
172
173
176
177
178
179
182
183
184
185
186
187
188
189
190
191
194
195
196
197
198
199
200
201
202
205
206
207
212
213
214
215
216
217
218
219
220
221
224
225
226
227
228
229
230
231
234
235
236
241
242
243
244
245
246
247
248
249
250
253
254
255
256
257
258
259
260
261
262
263
264
265
272
273
274
275
276
277
278
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
306
307
308
309
310
311
312
313
314
315
316
317
318
325
326
329
330
331
344
348
349
350
351
352
359
360
361
362
363
364
365
366
367
368
369
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* ... */
#include <stdlib.h>
#include "spi_flash_chip_generic.h"
#include "spi_flash_defs.h"
#include "esp_log.h"
#include "string.h"
#include <sys/param.h>
#include "hal/spi_flash_hal.h"7 includes
#define CMD_OPI_FLASH_MXIC(cmd) ((((~(cmd) & 0xff) << 8)) | ((cmd) & 0xff))
#define CMD_OPI_FLASH_MXIC_CHIP_ERASE 0x9F60
#define CMD_OPI_FLASH_MXIC_READ_STR 0x13EC
#define CMD_OPI_FLASH_MXIC_READ_DTR 0x11EE
#define CMD_OPI_FLASH_MXIC_RDCR2 0x8E71
#define CMD_OPI_FLASH_MXIC_WRCR2 0x8D726 defines
static const char chip_name[] = "mxic (opi)";
esp_err_t spi_flash_chip_mxic_opi_probe(esp_flash_t *chip, uint32_t flash_id)
{
const uint8_t MFG_ID = 0xC2;
if (flash_id >> 16 != MFG_ID) {
return ESP_ERR_NOT_FOUND;
}{...}
if (chip->read_mode < SPI_FLASH_OPI_FLAG) {
return ESP_ERR_NOT_FOUND;
}{...}
return ESP_OK;
}{ ... }
esp_err_t spi_flash_chip_mxic_opi_detect_size(esp_flash_t *chip, uint32_t *size)
{
uint32_t id = chip->chip_id;
*size = 0;
/* ... */
if (((id & 0xFFFF) == 0x0000) || ((id & 0xFFFF) == 0xFFFF)) {
return ESP_ERR_FLASH_UNSUPPORTED_CHIP;
}{...}
*size = 1 << ((id & 0xFF) - 0x20);
return ESP_OK;
}{ ... }
spi_flash_caps_t spi_flash_chip_mxic_opi_get_caps(esp_flash_t *chip)
{
spi_flash_caps_t caps_flags = 0;
caps_flags |= SPI_FLASH_CHIP_CAP_32MB_SUPPORT;
return caps_flags;
}{ ... }
esp_err_t spi_flash_chip_mxic_opi_set_write_protect(esp_flash_t *chip, bool write_protect)
{
esp_err_t err = ESP_OK;
err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
spi_flash_trans_t t = {};
if (err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED) {
if(write_protect) {
t.command = CMD_OPI_FLASH_MXIC(CMD_WRDI);
}{...} else {
t.command = CMD_OPI_FLASH_MXIC(CMD_WREN);
}{...}
err = chip->host->driver->common_command(chip->host, &t);
}{...}
bool wp_read;
err = chip->chip_drv->get_chip_write_protect(chip, &wp_read);
if (err == ESP_OK && wp_read != write_protect) {
err = ESP_ERR_NOT_FOUND;
}{...}
return err;
}{ ... }
static void spi_flash_chip_mxic_opi_get_data_length_zoom(esp_flash_io_mode_t io_mode, uint32_t *length_zoom)
{
/* ... */
assert((io_mode == SPI_FLASH_OPI_STR) || (io_mode == SPI_FLASH_OPI_DTR));
*length_zoom = (io_mode == SPI_FLASH_OPI_STR) ? 1 : 2;
}{ ... }
esp_err_t spi_flash_chip_mxic_opi_read_id(esp_flash_t *chip, uint32_t* out_chip_id)
{
uint64_t id_buf = 0;
uint32_t length_zoom;
spi_flash_chip_mxic_opi_get_data_length_zoom(chip->read_mode, &length_zoom);
spi_flash_trans_t t = {
.command = CMD_OPI_FLASH_MXIC(CMD_RDID),
.miso_len = 3 * length_zoom,
.dummy_bitlen = 4 * length_zoom,
.address_bitlen = 32,
.miso_data = ((uint8_t*) &id_buf),
}{...};
chip->host->driver->common_command(chip->host, &t);
if(chip->read_mode == SPI_FLASH_OPI_DTR) {
ESP_EARLY_LOGV(chip_name, "raw_chip_id: %llx\n", id_buf);
id_buf = (id_buf & 0xff) | ((id_buf & 0xff0000) >> 8) | ((id_buf & 0xff00000000) >> 16);
}{...} else {
ESP_EARLY_LOGV(chip_name, "raw_chip_id: %X\n", id_buf);
}{...}
uint32_t raw_flash_id = __builtin_bswap32(id_buf);
if (raw_flash_id == 0xFFFFFF || raw_flash_id == 0) {
ESP_EARLY_LOGE(chip_name, "no response\n");
return ESP_ERR_FLASH_NO_RESPONSE;
}{...}
*out_chip_id = (raw_flash_id >> 8);
ESP_EARLY_LOGV(chip_name, "chip_id: %X\n", *out_chip_id);
return ESP_OK;
}{ ... }
esp_err_t spi_flash_chip_mxic_opi_read_reg(esp_flash_t *chip, spi_flash_register_t reg_id, uint32_t* out_reg)
{
uint32_t stat_buf = 0;
uint32_t length_zoom;
spi_flash_chip_mxic_opi_get_data_length_zoom(chip->read_mode, &length_zoom);
spi_flash_trans_t t = {
.command = CMD_OPI_FLASH_MXIC(CMD_RDSR),
.miso_data = ((uint8_t*) &stat_buf),
.miso_len = 1 * length_zoom,
.address_bitlen = 32,
.dummy_bitlen = 4 * length_zoom,
}{...};
esp_err_t err = chip->host->driver->common_command(chip->host, &t);
if (err != ESP_OK) {
return err;
}{...}
*out_reg = (stat_buf & 0xff);
return ESP_OK;
}{ ... }
esp_err_t spi_flash_chip_mxic_opi_get_write_protect(esp_flash_t *chip, bool *out_write_protected)
{
esp_err_t err = ESP_OK;
uint32_t status;
assert(out_write_protected!=NULL);
err = chip->chip_drv->read_reg(chip, SPI_FLASH_REG_STATUS, &status);
if (err != ESP_OK) {
return err;
}{...}
*out_write_protected = ((status & SR_WREN) == 0);
return err;
}{ ... }
esp_err_t spi_flash_chip_mxic_opi_erase_chip(esp_flash_t *chip)
{
esp_err_t err;
err = chip->chip_drv->set_chip_write_protect(chip, false);
if (err == ESP_OK) {
err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
}{...}
if (err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED) {
spi_flash_trans_t t = {
.command = CMD_OPI_FLASH_MXIC_CHIP_ERASE,
}{...};
err = chip->host->driver->common_command(chip->host, &t);
chip->busy = 1;
#ifdef CONFIG_SPI_FLASH_CHECK_ERASE_TIMEOUT_DISABLED
err = chip->chip_drv->wait_idle(chip, ESP_FLASH_CHIP_GENERIC_NO_TIMEOUT);
#else
err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->chip_erase_timeout);
#endif
}{...}
if (err == ESP_ERR_NOT_SUPPORTED) {
chip->chip_drv->set_chip_write_protect(chip, true);
}{...}
return err;
}{ ... }
esp_err_t spi_flash_chip_mxic_opi_erase_sector(esp_flash_t *chip, uint32_t start_address)
{
esp_err_t err = chip->chip_drv->set_chip_write_protect(chip, false);
if (err == ESP_OK) {
err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
}{...}
if (err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED) {
spi_flash_trans_t t = {
.command = CMD_OPI_FLASH_MXIC(CMD_SECTOR_ERASE_4B),
.address_bitlen = 32,
.address = start_address,
}{...};
err = chip->host->driver->common_command(chip->host, &t);
chip->busy = 1;
#ifdef CONFIG_SPI_FLASH_CHECK_ERASE_TIMEOUT_DISABLED
err = chip->chip_drv->wait_idle(chip, ESP_FLASH_CHIP_GENERIC_NO_TIMEOUT);
#else
err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->sector_erase_timeout);
#endif
}{...}
if (err == ESP_ERR_NOT_SUPPORTED) {
err = chip->chip_drv->set_chip_write_protect(chip, true);
}{...}
return err;
}{ ... }
esp_err_t spi_flash_chip_mxic_opi_erase_block(esp_flash_t *chip, uint32_t start_address)
{
esp_err_t err = chip->chip_drv->set_chip_write_protect(chip, false);
if (err == ESP_OK) {
err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
}{...}
if (err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED) {
spi_flash_trans_t t = {
.command = CMD_OPI_FLASH_MXIC(CMD_LARGE_BLOCK_ERASE_4B),
.address_bitlen = 32,
.address = start_address,
}{...};
err = chip->host->driver->common_command(chip->host, &t);
chip->busy = 1;
#ifdef CONFIG_SPI_FLASH_CHECK_ERASE_TIMEOUT_DISABLED
err = chip->chip_drv->wait_idle(chip, ESP_FLASH_CHIP_GENERIC_NO_TIMEOUT);
#else
err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->block_erase_timeout);
#endif
}{...}
if (err == ESP_ERR_NOT_SUPPORTED) {
err = chip->chip_drv->set_chip_write_protect(chip, true);
}{...}
return err;
}{ ... }
esp_err_t spi_flash_chip_mxic_opi_page_program(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
{
esp_err_t err;
err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
if (err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED) {
spi_flash_trans_t t = {
.command = CMD_OPI_FLASH_MXIC(CMD_PROGRAM_PAGE_4B),
.address_bitlen = 32,
.address = address,
.mosi_len = length,
.mosi_data = buffer,
}{...};
chip->host->driver->common_command(chip->host, &t);
chip->busy = 1;
err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->page_program_timeout);
}{...}
if (err == ESP_ERR_NOT_SUPPORTED) {
err = chip->chip_drv->set_chip_write_protect(chip, true);
}{...}
return err;
}{ ... }
esp_err_t spi_flash_chip_mxic_opi_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
{
esp_err_t err = ESP_OK;
const uint32_t page_size = chip->chip_drv->page_size;
uint32_t align_address;
uint8_t temp_buffer[64];
while (err == ESP_OK && length > 0) {
memset(temp_buffer, 0xFF, sizeof(temp_buffer));
uint32_t page_len = chip->host->driver->write_data_slicer(chip->host, address, length, &align_address, page_size);
uint32_t left_off = address - align_address;
uint32_t write_len = MIN(align_address + page_len, address + length) - address;
memcpy(temp_buffer + left_off, buffer, write_len);
err = chip->chip_drv->set_chip_write_protect(chip, false);
if (err == ESP_OK && length > 0) {
err = chip->chip_drv->program_page(chip, temp_buffer, align_address, page_len);
address += write_len;
buffer = (void *)((intptr_t)buffer + write_len);
length -= write_len;
}{...}
}{...}
return err;
}{ ... }
esp_err_t spi_flash_chip_mxic_opi_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode)
{
uint32_t stat_buf = 0;
uint32_t length_zoom;
spi_flash_chip_mxic_opi_get_data_length_zoom(chip->read_mode, &length_zoom);
spi_flash_trans_t t = {
.command = CMD_OPI_FLASH_MXIC_RDCR2,
.dummy_bitlen = 4 * length_zoom,
.miso_data = ((uint8_t*) &stat_buf),
.miso_len = 1 * length_zoom,
.address_bitlen = 32,
}{...};
esp_err_t err = chip->host->driver->common_command(chip->host, &t);
if (err != ESP_OK) {
return err;
}{...}
switch (stat_buf & 0xff)
{
case 0x1:
*out_io_mode = SPI_FLASH_OPI_STR;
break;...
case 0x2:
*out_io_mode = SPI_FLASH_OPI_DTR;
break;...
default:
*out_io_mode = 0;
break;...
}{...}
if (*out_io_mode != chip->read_mode) {
*out_io_mode = 0;
}{...}
return ESP_OK;
}{ ... }
esp_err_t spi_flash_chip_xmic_opi_set_io_mode(esp_flash_t *chip)
{
return ESP_OK;
}{ ... }
esp_err_t spi_flash_chip_xmic_opi_config_host_io_mode(esp_flash_t *chip, uint32_t flags)
{
uint32_t dummy_cyclelen_base;
uint32_t addr_bitlen;
uint32_t read_command;
esp_flash_io_mode_t read_mode = chip->read_mode;
switch (read_mode & 0xFFFF) {
case SPI_FLASH_OPI_STR:
addr_bitlen = SPI_FLASH_OPISTR_ADDR_BITLEN;
dummy_cyclelen_base = SPI_FLASH_OPISTR_DUMMY_BITLEN;
read_command = CMD_OPI_FLASH_MXIC_READ_STR;
break;...
case SPI_FLASH_OPI_DTR:
addr_bitlen = SPI_FLASH_OPIDTR_ADDR_BITLEN;
dummy_cyclelen_base = SPI_FLASH_OPIDTR_DUMMY_BITLEN;
read_command = CMD_OPI_FLASH_MXIC_READ_DTR;
break;...
default:
return ESP_ERR_FLASH_NOT_INITIALISED;...
}{...}
return chip->host->driver->configure_host_io_mode(chip->host, read_command, addr_bitlen, dummy_cyclelen_base, read_mode);
}{ ... }
const spi_flash_chip_t esp_flash_chip_mxic_opi = {
.name = chip_name,
.timeout = &spi_flash_chip_generic_timeout,
.probe = spi_flash_chip_mxic_opi_probe,
.reset = spi_flash_chip_generic_reset,
.detect_size = spi_flash_chip_mxic_opi_detect_size,
.erase_chip = spi_flash_chip_mxic_opi_erase_chip,
.erase_sector = spi_flash_chip_mxic_opi_erase_sector,
.erase_block = spi_flash_chip_mxic_opi_erase_block,
.sector_size = 4 * 1024,
.block_erase_size = 64 * 1024,
.get_chip_write_protect = spi_flash_chip_mxic_opi_get_write_protect,
.set_chip_write_protect = spi_flash_chip_mxic_opi_set_write_protect,
.num_protectable_regions = 0,
.protectable_regions = NULL,
.get_protected_regions = NULL,
.set_protected_regions = NULL,
.read = spi_flash_chip_generic_read,
.write = spi_flash_chip_mxic_opi_write,
.program_page = spi_flash_chip_mxic_opi_page_program,
.page_size = 256,
.write_encrypted = spi_flash_chip_generic_write_encrypted,
.wait_idle = spi_flash_chip_generic_wait_idle,
.set_io_mode = spi_flash_chip_xmic_opi_set_io_mode,
.get_io_mode = spi_flash_chip_mxic_opi_get_io_mode,
.read_id = spi_flash_chip_mxic_opi_read_id,
.read_reg = spi_flash_chip_mxic_opi_read_reg,
.yield = spi_flash_chip_generic_yield,
.sus_setup = spi_flash_chip_generic_suspend_cmd_conf,
.read_unique_id = spi_flash_chip_generic_read_unique_id_none,
.get_chip_caps = spi_flash_chip_mxic_opi_get_caps,
.config_host_io_mode = spi_flash_chip_xmic_opi_config_host_io_mode,
}{...};