1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/param.h> // For MIN/MAX
18 #include "spi_flash_chip_generic.h"
19 #include "spi_flash_defs.h"
20 #include "esp_log.h"
21 #include "esp_attr.h"
22
23 typedef struct flash_chip_dummy {
24 uint8_t dio_dummy_bitlen;
25 uint8_t qio_dummy_bitlen;
26 uint8_t qout_dummy_bitlen;
27 uint8_t dout_dummy_bitlen;
28 uint8_t fastrd_dummy_bitlen;
29 uint8_t slowrd_dummy_bitlen;
30 } flash_chip_dummy_t;
31
32 // These parameters can be placed in the ROM. For now we use the code in IDF.
33 DRAM_ATTR const static flash_chip_dummy_t default_flash_chip_dummy = {
34 .dio_dummy_bitlen = SPI_FLASH_DIO_DUMMY_BITLEN,
35 .qio_dummy_bitlen = SPI_FLASH_QIO_DUMMY_BITLEN,
36 .qout_dummy_bitlen = SPI_FLASH_QOUT_DUMMY_BITLEN,
37 .dout_dummy_bitlen = SPI_FLASH_DOUT_DUMMY_BITLEN,
38 .fastrd_dummy_bitlen = SPI_FLASH_FASTRD_DUMMY_BITLEN,
39 .slowrd_dummy_bitlen = SPI_FLASH_SLOWRD_DUMMY_BITLEN,
40 };
41
42 DRAM_ATTR flash_chip_dummy_t *rom_flash_chip_dummy = (flash_chip_dummy_t *)&default_flash_chip_dummy;
43
44 #define SPI_FLASH_DEFAULT_IDLE_TIMEOUT_MS 200
45 #define SPI_FLASH_GENERIC_CHIP_ERASE_TIMEOUT_MS 4000
46 #define SPI_FLASH_GENERIC_SECTOR_ERASE_TIMEOUT_MS 600 //according to GD25Q127(125°) + 100ms
47 #define SPI_FLASH_GENERIC_BLOCK_ERASE_TIMEOUT_MS 4100 //according to GD25Q127(125°) + 100ms
48 #define SPI_FLASH_GENERIC_PAGE_PROGRAM_TIMEOUT_MS 500
49
50 #define HOST_DELAY_INTERVAL_US 1
51 #define CHIP_WAIT_IDLE_INTERVAL_US 20
52
53 const DRAM_ATTR flash_chip_op_timeout_t spi_flash_chip_generic_timeout = {
54 .idle_timeout = SPI_FLASH_DEFAULT_IDLE_TIMEOUT_MS * 1000,
55 .chip_erase_timeout = SPI_FLASH_GENERIC_CHIP_ERASE_TIMEOUT_MS * 1000,
56 .block_erase_timeout = SPI_FLASH_GENERIC_BLOCK_ERASE_TIMEOUT_MS * 1000,
57 .sector_erase_timeout = SPI_FLASH_GENERIC_SECTOR_ERASE_TIMEOUT_MS * 1000,
58 .page_program_timeout = SPI_FLASH_GENERIC_PAGE_PROGRAM_TIMEOUT_MS * 1000,
59 };
60
61 static const char TAG[] = "chip_generic";
62
63 #ifndef CONFIG_SPI_FLASH_ROM_IMPL
64
spi_flash_chip_generic_probe(esp_flash_t * chip,uint32_t flash_id)65 esp_err_t spi_flash_chip_generic_probe(esp_flash_t *chip, uint32_t flash_id)
66 {
67 // This is the catch-all probe function, claim the chip always if nothing
68 // else has claimed it yet.
69 return ESP_OK;
70 }
71
spi_flash_chip_generic_reset(esp_flash_t * chip)72 esp_err_t spi_flash_chip_generic_reset(esp_flash_t *chip)
73 {
74 //this is written following the winbond spec..
75 spi_flash_trans_t t;
76 t = (spi_flash_trans_t) {
77 .command = CMD_RST_EN,
78 };
79 esp_err_t err = chip->host->driver->common_command(chip->host, &t);
80 if (err != ESP_OK) {
81 return err;
82 }
83
84 t = (spi_flash_trans_t) {
85 .command = CMD_RST_DEV,
86 };
87 err = chip->host->driver->common_command(chip->host, &t);
88 if (err != ESP_OK) {
89 return err;
90 }
91
92 err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
93 return err;
94 }
95
spi_flash_chip_generic_detect_size(esp_flash_t * chip,uint32_t * size)96 esp_err_t spi_flash_chip_generic_detect_size(esp_flash_t *chip, uint32_t *size)
97 {
98 uint32_t id = chip->chip_id;
99 *size = 0;
100
101 /* Can't detect size unless the high byte of the product ID matches the same convention, which is usually 0x40 or
102 * 0xC0 or similar. */
103 if (((id & 0xFFFF) == 0x0000) || ((id & 0xFFFF) == 0xFFFF)) {
104 return ESP_ERR_FLASH_UNSUPPORTED_CHIP;
105 }
106
107 *size = 1 << (id & 0xFF);
108 return ESP_OK;
109 }
110
111
spi_flash_chip_generic_erase_chip(esp_flash_t * chip)112 esp_err_t spi_flash_chip_generic_erase_chip(esp_flash_t *chip)
113 {
114 esp_err_t err;
115
116 err = chip->chip_drv->set_chip_write_protect(chip, false);
117 if (err == ESP_OK) {
118 err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
119 }
120 //The chip didn't accept the previous write command. Ignore this in preparation stage.
121 if (err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED) {
122 chip->host->driver->erase_chip(chip->host);
123 chip->busy = 1;
124 #ifdef CONFIG_SPI_FLASH_CHECK_ERASE_TIMEOUT_DISABLED
125 err = chip->chip_drv->wait_idle(chip, ESP_FLASH_CHIP_GENERIC_NO_TIMEOUT);
126 #else
127 err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->chip_erase_timeout);
128 #endif
129 }
130 // Ensure WEL is 0, even if the erase failed.
131 if (err == ESP_ERR_NOT_SUPPORTED) {
132 err = chip->chip_drv->set_chip_write_protect(chip, true);
133 }
134
135 return err;
136 }
137
spi_flash_chip_generic_erase_sector(esp_flash_t * chip,uint32_t start_address)138 esp_err_t spi_flash_chip_generic_erase_sector(esp_flash_t *chip, uint32_t start_address)
139 {
140 esp_err_t err = chip->chip_drv->set_chip_write_protect(chip, false);
141 if (err == ESP_OK) {
142 err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
143 }
144 //The chip didn't accept the previous write command. Ignore this in preparationstage.
145 if (err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED) {
146 chip->host->driver->erase_sector(chip->host, start_address);
147 chip->busy = 1;
148 #ifdef CONFIG_SPI_FLASH_CHECK_ERASE_TIMEOUT_DISABLED
149 err = chip->chip_drv->wait_idle(chip, ESP_FLASH_CHIP_GENERIC_NO_TIMEOUT);
150 #else
151 err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->sector_erase_timeout);
152 #endif
153 }
154 // Ensure WEL is 0, even if the erase failed.
155 if (err == ESP_ERR_NOT_SUPPORTED) {
156 err = chip->chip_drv->set_chip_write_protect(chip, true);
157 }
158
159 return err;
160 }
161
spi_flash_chip_generic_erase_block(esp_flash_t * chip,uint32_t start_address)162 esp_err_t spi_flash_chip_generic_erase_block(esp_flash_t *chip, uint32_t start_address)
163 {
164 esp_err_t err = chip->chip_drv->set_chip_write_protect(chip, false);
165 if (err == ESP_OK) {
166 err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
167 }
168 //The chip didn't accept the previous write command. Ignore this in preparationstage.
169 if (err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED) {
170 chip->host->driver->erase_block(chip->host, start_address);
171 chip->busy = 1;
172 #ifdef CONFIG_SPI_FLASH_CHECK_ERASE_TIMEOUT_DISABLED
173 err = chip->chip_drv->wait_idle(chip, ESP_FLASH_CHIP_GENERIC_NO_TIMEOUT);
174 #else
175 err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->block_erase_timeout);
176 #endif
177 }
178 // Ensure WEL is 0, even if the erase failed.
179 if (err == ESP_ERR_NOT_SUPPORTED) {
180 err = chip->chip_drv->set_chip_write_protect(chip, true);
181 }
182
183 return err;
184 }
185
spi_flash_chip_generic_read(esp_flash_t * chip,void * buffer,uint32_t address,uint32_t length)186 esp_err_t spi_flash_chip_generic_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
187 {
188 esp_err_t err = ESP_OK;
189 const uint32_t page_size = chip->chip_drv->page_size;
190 uint32_t align_address;
191 uint8_t temp_buffer[64]; //spiflash hal max length of read no longer than 64byte
192
193 // Configure the host, and return
194 err = spi_flash_chip_generic_config_host_io_mode(chip, false);
195
196 if (err == ESP_ERR_NOT_SUPPORTED) {
197 ESP_LOGE(TAG, "configure host io mode failed - unsupported");
198 return err;
199 }
200
201 while (err == ESP_OK && length > 0) {
202 memset(temp_buffer, 0xFF, sizeof(temp_buffer));
203 uint32_t read_len = chip->host->driver->read_data_slicer(chip->host, address, length, &align_address, page_size);
204 uint32_t left_off = address - align_address;
205 uint32_t data_len = MIN(align_address + read_len, address + length) - address;
206 err = chip->host->driver->read(chip->host, temp_buffer, align_address, read_len);
207
208 memcpy(buffer, temp_buffer + left_off, data_len);
209
210 address += data_len;
211 buffer = (void *)((intptr_t)buffer + data_len);
212 length = length - data_len;
213 }
214
215 return err;
216 }
217
spi_flash_chip_generic_page_program(esp_flash_t * chip,const void * buffer,uint32_t address,uint32_t length)218 esp_err_t spi_flash_chip_generic_page_program(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
219 {
220 esp_err_t err;
221
222 err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
223 //The chip didn't accept the previous write command. Ignore this in preparationstage.
224 if (err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED) {
225 // Perform the actual Page Program command
226 chip->host->driver->program_page(chip->host, buffer, address, length);
227 chip->busy = 1;
228
229 err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->page_program_timeout);
230 }
231 // Ensure WEL is 0, even if the page program failed.
232 if (err == ESP_ERR_NOT_SUPPORTED) {
233 err = chip->chip_drv->set_chip_write_protect(chip, true);
234 }
235 return err;
236 }
237
spi_flash_chip_generic_write(esp_flash_t * chip,const void * buffer,uint32_t address,uint32_t length)238 esp_err_t spi_flash_chip_generic_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
239 {
240 esp_err_t err = ESP_OK;
241 const uint32_t page_size = chip->chip_drv->page_size;
242 uint32_t align_address;
243 uint8_t temp_buffer[64]; //spiflash hal max length of write no longer than 64byte
244
245 while (err == ESP_OK && length > 0) {
246 memset(temp_buffer, 0xFF, sizeof(temp_buffer));
247 uint32_t page_len = chip->host->driver->write_data_slicer(chip->host, address, length, &align_address, page_size);
248 uint32_t left_off = address - align_address;
249 uint32_t write_len = MIN(align_address + page_len, address + length) - address;
250 memcpy(temp_buffer + left_off, buffer, write_len);
251
252 err = chip->chip_drv->set_chip_write_protect(chip, false);
253 if (err == ESP_OK && length > 0) {
254 err = chip->chip_drv->program_page(chip, temp_buffer, align_address, page_len);
255
256 address += write_len;
257 buffer = (void *)((intptr_t)buffer + write_len);
258 length -= write_len;
259 }
260 }
261 // The caller is responsible to do host->driver->flush_cache, because this function may be
262 // called in small pieces. Frequency call of flush cache will do harm to the performance.
263 return err;
264 }
265
spi_flash_chip_generic_write_encrypted(esp_flash_t * chip,const void * buffer,uint32_t address,uint32_t length)266 esp_err_t spi_flash_chip_generic_write_encrypted(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
267 {
268 return ESP_ERR_FLASH_UNSUPPORTED_HOST; // TODO
269 }
270
spi_flash_chip_generic_set_write_protect(esp_flash_t * chip,bool write_protect)271 esp_err_t spi_flash_chip_generic_set_write_protect(esp_flash_t *chip, bool write_protect)
272 {
273 esp_err_t err = ESP_OK;
274
275 err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
276 //The chip didn't accept the previous write command. Ignore this in preparationstage.
277 if (err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED) {
278 chip->host->driver->set_write_protect(chip->host, write_protect);
279 }
280
281 bool wp_read;
282 err = chip->chip_drv->get_chip_write_protect(chip, &wp_read);
283 if (err == ESP_OK && wp_read != write_protect) {
284 // WREN flag has not been set!
285 err = ESP_ERR_NOT_FOUND;
286 }
287 return err;
288 }
289
spi_flash_chip_generic_get_write_protect(esp_flash_t * chip,bool * out_write_protect)290 esp_err_t spi_flash_chip_generic_get_write_protect(esp_flash_t *chip, bool *out_write_protect)
291 {
292 esp_err_t err = ESP_OK;
293 uint32_t status;
294 assert(out_write_protect!=NULL);
295 err = chip->chip_drv->read_reg(chip, SPI_FLASH_REG_STATUS, &status);
296 if (err != ESP_OK) {
297 return err;
298 }
299
300 *out_write_protect = ((status & SR_WREN) == 0);
301 return err;
302 }
303
spi_flash_chip_generic_read_reg(esp_flash_t * chip,spi_flash_register_t reg_id,uint32_t * out_reg)304 esp_err_t spi_flash_chip_generic_read_reg(esp_flash_t* chip, spi_flash_register_t reg_id, uint32_t* out_reg)
305 {
306 return chip->host->driver->read_status(chip->host, (uint8_t*)out_reg);
307 }
308
spi_flash_chip_generic_yield(esp_flash_t * chip,uint32_t wip)309 esp_err_t spi_flash_chip_generic_yield(esp_flash_t* chip, uint32_t wip)
310 {
311 esp_err_t err = ESP_OK;
312 uint32_t flags = wip? 1: 0; //check_yield() and yield() impls should not issue suspend/resume if this flag is zero
313
314 if (chip->os_func->check_yield) {
315 uint32_t request;
316 //According to the implementation, the check_yield() function may block, poll, delay or do nothing but return
317 err = chip->os_func->check_yield(chip->os_func_data, flags, &request);
318 if (err == ESP_OK) {
319 if (err == ESP_OK && (request & SPI_FLASH_YIELD_REQ_YIELD) != 0) {
320 uint32_t status;
321 //According to the implementation, the yield() function may block until something happen
322 err = chip->os_func->yield(chip->os_func_data, &status);
323 }
324 } else if (err == ESP_ERR_TIMEOUT) {
325 err = ESP_OK;
326 } else {
327 abort();
328 }
329 }
330 return err;
331 }
332
spi_flash_chip_generic_wait_idle(esp_flash_t * chip,uint32_t timeout_us)333 esp_err_t spi_flash_chip_generic_wait_idle(esp_flash_t *chip, uint32_t timeout_us)
334 {
335 bool timeout_en = (timeout_us != ESP_FLASH_CHIP_GENERIC_NO_TIMEOUT);
336 if (timeout_us == ESP_FLASH_CHIP_GENERIC_NO_TIMEOUT) {
337 timeout_us = 0;// In order to go into while
338 }
339 timeout_us++; // allow at least one pass before timeout, last one has no sleep cycle
340
341 uint8_t status = 0;
342 const int interval = CHIP_WAIT_IDLE_INTERVAL_US;
343 while (timeout_us > 0) {
344 while (!chip->host->driver->host_status(chip->host) && timeout_us > 0) {
345
346 #if HOST_DELAY_INTERVAL_US > 0
347 if (timeout_us > 1) {
348 int delay = MIN(HOST_DELAY_INTERVAL_US, timeout_us);
349 chip->os_func->delay_us(chip->os_func_data, delay);
350 timeout_us -= delay;
351 }
352 #endif
353 }
354
355 uint32_t read;
356 esp_err_t err = chip->chip_drv->read_reg(chip, SPI_FLASH_REG_STATUS, &read);
357 if (err != ESP_OK) {
358 return err;
359 }
360 status = read;
361
362 if ((status & SR_WIP) == 0) { // Verify write in progress is complete
363 if (chip->busy == 1) {
364 chip->busy = 0;
365 if ((status & SR_WREN) != 0) { // The previous command is not accepted, leaving the WEL still set.
366 return ESP_ERR_NOT_SUPPORTED;
367 }
368 }
369 break;
370 }
371 if (timeout_us > 0 && interval > 0) {
372 int delay = MIN(interval, timeout_us);
373 chip->os_func->delay_us(chip->os_func_data, delay);
374 if (timeout_en) {
375 timeout_us -= delay;
376 }
377 }
378 }
379 return (timeout_us > 0) ? ESP_OK : ESP_ERR_TIMEOUT;
380 }
381
spi_flash_chip_generic_config_host_io_mode(esp_flash_t * chip,bool addr_32bit)382 esp_err_t spi_flash_chip_generic_config_host_io_mode(esp_flash_t *chip, bool addr_32bit)
383 {
384 uint32_t dummy_cyclelen_base;
385 uint32_t addr_bitlen;
386 uint32_t read_command;
387 bool conf_required = false;
388 esp_flash_io_mode_t read_mode = chip->read_mode;
389
390 switch (read_mode & 0xFFFF) {
391 case SPI_FLASH_QIO:
392 //for QIO mode, the 4 bit right after the address are used for continuous mode, should be set to 0 to avoid that.
393 addr_bitlen = SPI_FLASH_QIO_ADDR_BITLEN;
394 dummy_cyclelen_base = rom_flash_chip_dummy->qio_dummy_bitlen;
395 read_command = (addr_32bit? CMD_FASTRD_QIO_4B: CMD_FASTRD_QIO);
396 conf_required = true;
397 break;
398 case SPI_FLASH_QOUT:
399 addr_bitlen = SPI_FLASH_QOUT_ADDR_BITLEN;
400 dummy_cyclelen_base = rom_flash_chip_dummy->qout_dummy_bitlen;
401 read_command = (addr_32bit? CMD_FASTRD_QUAD_4B: CMD_FASTRD_QUAD);
402 break;
403 case SPI_FLASH_DIO:
404 //for DIO mode, the 4 bit right after the address are used for continuous mode, should be set to 0 to avoid that.
405 addr_bitlen = SPI_FLASH_DIO_ADDR_BITLEN;
406 dummy_cyclelen_base = rom_flash_chip_dummy->dio_dummy_bitlen;
407 read_command = (addr_32bit? CMD_FASTRD_DIO_4B: CMD_FASTRD_DIO);
408 conf_required = true;
409 break;
410 case SPI_FLASH_DOUT:
411 addr_bitlen = SPI_FLASH_DOUT_ADDR_BITLEN;
412 dummy_cyclelen_base = rom_flash_chip_dummy->dout_dummy_bitlen;
413 read_command = (addr_32bit? CMD_FASTRD_DUAL_4B: CMD_FASTRD_DUAL);
414 break;
415 case SPI_FLASH_FASTRD:
416 addr_bitlen = SPI_FLASH_FASTRD_ADDR_BITLEN;
417 dummy_cyclelen_base = rom_flash_chip_dummy->fastrd_dummy_bitlen;
418 read_command = (addr_32bit? CMD_FASTRD_4B: CMD_FASTRD);
419 break;
420 case SPI_FLASH_SLOWRD:
421 addr_bitlen = SPI_FLASH_SLOWRD_ADDR_BITLEN;
422 dummy_cyclelen_base = rom_flash_chip_dummy->slowrd_dummy_bitlen;
423 read_command = (addr_32bit? CMD_READ_4B: CMD_READ);
424 break;
425 default:
426 return ESP_ERR_FLASH_NOT_INITIALISED;
427 }
428 //For W25Q256 chip, the only difference between 4-Byte address command and 3-Byte version is the command value and the address bit length.
429 if (addr_32bit) {
430 addr_bitlen += 8;
431 }
432
433 if (conf_required) {
434 read_mode |= SPI_FLASH_CONFIG_CONF_BITS;
435 }
436
437 return chip->host->driver->configure_host_io_mode(chip->host, read_command, addr_bitlen, dummy_cyclelen_base, read_mode);
438 }
439
spi_flash_chip_generic_get_io_mode(esp_flash_t * chip,esp_flash_io_mode_t * out_io_mode)440 esp_err_t spi_flash_chip_generic_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode)
441 {
442 // On "generic" chips, this involves checking
443 // bit 1 (QE) of RDSR2 (35h) result
444 // (it works this way on GigaDevice & Fudan Micro chips, probably others...)
445 const uint8_t BIT_QE = 1 << 1;
446 uint32_t sr;
447 esp_err_t ret = spi_flash_common_read_status_8b_rdsr2(chip, &sr);
448 if (ret == ESP_OK) {
449 *out_io_mode = ((sr & BIT_QE)? SPI_FLASH_QOUT: 0);
450 }
451 return ret;
452 }
453
spi_flash_chip_generic_set_io_mode(esp_flash_t * chip)454 esp_err_t spi_flash_chip_generic_set_io_mode(esp_flash_t *chip)
455 {
456 // On "generic" chips, this involves checking
457 // bit 9 (QE) of RDSR (05h) result
458 const uint32_t BIT_QE = 1 << 9;
459 return spi_flash_common_set_io_mode(chip,
460 spi_flash_common_write_status_16b_wrsr,
461 spi_flash_common_read_status_16b_rdsr_rdsr2,
462 BIT_QE);
463 }
464 #endif // CONFIG_SPI_FLASH_ROM_IMPL
465
466 static const char chip_name[] = "generic";
467
468 const spi_flash_chip_t esp_flash_chip_generic = {
469 .name = chip_name,
470 .timeout = &spi_flash_chip_generic_timeout,
471 .probe = spi_flash_chip_generic_probe,
472 .reset = spi_flash_chip_generic_reset,
473 .detect_size = spi_flash_chip_generic_detect_size,
474 .erase_chip = spi_flash_chip_generic_erase_chip,
475 .erase_sector = spi_flash_chip_generic_erase_sector,
476 .erase_block = spi_flash_chip_generic_erase_block,
477 .sector_size = 4 * 1024,
478 .block_erase_size = 64 * 1024,
479
480 // TODO: figure out if generic chip-wide protection bits exist across some manufacturers
481 .get_chip_write_protect = spi_flash_chip_generic_get_write_protect,
482 .set_chip_write_protect = spi_flash_chip_generic_set_write_protect,
483
484 // Chip write protection regions do not appear to be standardised
485 // at all, this is implemented in chip-specific drivers only.
486 .num_protectable_regions = 0,
487 .protectable_regions = NULL,
488 .get_protected_regions = NULL,
489 .set_protected_regions = NULL,
490
491 .read = spi_flash_chip_generic_read,
492 .write = spi_flash_chip_generic_write,
493 .program_page = spi_flash_chip_generic_page_program,
494 .page_size = 256,
495 .write_encrypted = spi_flash_chip_generic_write_encrypted,
496
497 .wait_idle = spi_flash_chip_generic_wait_idle,
498 .set_io_mode = spi_flash_chip_generic_set_io_mode,
499 .get_io_mode = spi_flash_chip_generic_get_io_mode,
500
501 .read_reg = spi_flash_chip_generic_read_reg,
502 .yield = spi_flash_chip_generic_yield,
503 .sus_setup = spi_flash_chip_generic_suspend_cmd_conf,
504 };
505
506 #ifndef CONFIG_SPI_FLASH_ROM_IMPL
507 /*******************************************************************************
508 * Utility functions
509 ******************************************************************************/
510
spi_flash_common_read_qe_sr(esp_flash_t * chip,uint8_t qe_rdsr_command,uint8_t qe_sr_bitwidth,uint32_t * sr)511 static esp_err_t spi_flash_common_read_qe_sr(esp_flash_t *chip, uint8_t qe_rdsr_command, uint8_t qe_sr_bitwidth, uint32_t *sr)
512 {
513 uint32_t sr_buf = 0;
514 spi_flash_trans_t t = {
515 .command = qe_rdsr_command,
516 .miso_data = (uint8_t*) &sr_buf,
517 .miso_len = qe_sr_bitwidth / 8,
518 };
519 esp_err_t ret = chip->host->driver->common_command(chip->host, &t);
520 *sr = sr_buf;
521 return ret;
522 }
523
spi_flash_common_write_qe_sr(esp_flash_t * chip,uint8_t qe_wrsr_command,uint8_t qe_sr_bitwidth,uint32_t qe)524 static esp_err_t spi_flash_common_write_qe_sr(esp_flash_t *chip, uint8_t qe_wrsr_command, uint8_t qe_sr_bitwidth, uint32_t qe)
525 {
526 spi_flash_trans_t t = {
527 .command = qe_wrsr_command,
528 .mosi_data = ((uint8_t*) &qe),
529 .mosi_len = qe_sr_bitwidth / 8,
530 .miso_len = 0,
531 };
532 return chip->host->driver->common_command(chip->host, &t);
533 }
534
spi_flash_common_read_status_16b_rdsr_rdsr2(esp_flash_t * chip,uint32_t * out_sr)535 esp_err_t spi_flash_common_read_status_16b_rdsr_rdsr2(esp_flash_t* chip, uint32_t* out_sr)
536 {
537 uint32_t sr, sr2;
538 esp_err_t ret = spi_flash_common_read_qe_sr(chip, CMD_RDSR2, 8, &sr2);
539 if (ret == ESP_OK) {
540 ret = spi_flash_common_read_qe_sr(chip, CMD_RDSR, 8, &sr);
541 }
542 if (ret == ESP_OK) {
543 *out_sr = (sr & 0xff) | ((sr2 & 0xff) << 8);
544 }
545 return ret;
546 }
547
spi_flash_common_read_status_8b_rdsr2(esp_flash_t * chip,uint32_t * out_sr)548 esp_err_t spi_flash_common_read_status_8b_rdsr2(esp_flash_t* chip, uint32_t* out_sr)
549 {
550 return spi_flash_common_read_qe_sr(chip, CMD_RDSR2, 8, out_sr);
551 }
552
spi_flash_common_read_status_8b_rdsr(esp_flash_t * chip,uint32_t * out_sr)553 esp_err_t spi_flash_common_read_status_8b_rdsr(esp_flash_t* chip, uint32_t* out_sr)
554 {
555 return spi_flash_common_read_qe_sr(chip, CMD_RDSR, 8, out_sr);
556 }
557
spi_flash_common_write_status_16b_wrsr(esp_flash_t * chip,uint32_t sr)558 esp_err_t spi_flash_common_write_status_16b_wrsr(esp_flash_t* chip, uint32_t sr)
559 {
560 return spi_flash_common_write_qe_sr(chip, CMD_WRSR, 16, sr);
561 }
562
spi_flash_common_write_status_8b_wrsr(esp_flash_t * chip,uint32_t sr)563 esp_err_t spi_flash_common_write_status_8b_wrsr(esp_flash_t* chip, uint32_t sr)
564 {
565 return spi_flash_common_write_qe_sr(chip, CMD_WRSR, 8, sr);
566 }
567
spi_flash_common_write_status_8b_wrsr2(esp_flash_t * chip,uint32_t sr)568 esp_err_t spi_flash_common_write_status_8b_wrsr2(esp_flash_t* chip, uint32_t sr)
569 {
570 return spi_flash_common_write_qe_sr(chip, CMD_WRSR2, 8, sr);
571 }
572
spi_flash_common_set_io_mode(esp_flash_t * chip,esp_flash_wrsr_func_t wrsr_func,esp_flash_rdsr_func_t rdsr_func,uint32_t qe_sr_bit)573 esp_err_t spi_flash_common_set_io_mode(esp_flash_t *chip, esp_flash_wrsr_func_t wrsr_func, esp_flash_rdsr_func_t rdsr_func, uint32_t qe_sr_bit)
574 {
575 esp_err_t ret = ESP_OK;
576 const bool is_quad_mode = esp_flash_is_quad_mode(chip);
577 bool update_config = false;
578 /*
579 * By default, we don't clear the QE bit even the flash mode is not QIO or QOUT. Force clearing
580 * QE bit by the generic chip driver (command 01H with 2 bytes) may cause the output of some
581 * chips (MXIC) no longer valid.
582 * Enable this option when testing a new flash chip for clearing of QE.
583 */
584 const bool force_check = false;
585
586 bool need_check = is_quad_mode || force_check;
587
588 uint32_t sr_update;
589 if (need_check) {
590 // Ensure quad modes are enabled, using the Quad Enable parameters supplied.
591 uint32_t sr;
592 ret = (*rdsr_func)(chip, &sr);
593 if (ret != ESP_OK) {
594 return ret;
595 }
596 ESP_EARLY_LOGD(TAG, "set_io_mode: status before 0x%x", sr);
597 if (is_quad_mode) {
598 sr_update = sr | qe_sr_bit;
599 } else {
600 sr_update = sr & (~qe_sr_bit);
601 }
602 ESP_EARLY_LOGV(TAG, "set_io_mode: status update 0x%x", sr_update);
603 if (sr != sr_update) {
604 update_config = true;
605 }
606 }
607
608 if (update_config) {
609 //some chips needs the write protect to be disabled before writing to Status Register
610 chip->chip_drv->set_chip_write_protect(chip, false);
611
612 ret = (*wrsr_func)(chip, sr_update);
613 if (ret != ESP_OK) {
614 chip->chip_drv->set_chip_write_protect(chip, true);
615 return ret;
616 }
617
618 ret = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
619 if (ret == ESP_ERR_NOT_SUPPORTED) {
620 chip->chip_drv->set_chip_write_protect(chip, true);
621 }
622 /* This function is the fallback approach, so we give it higher tolerance.
623 * When the previous WRSR is rejected by the flash,
624 * the result of this function is determined by the result -whether the value of RDSR meets the expectation.
625 */
626 if (ret != ESP_OK && ret != ESP_ERR_NOT_SUPPORTED) {
627 return ret;
628 }
629
630 /* Check the new QE bit has stayed set */
631 uint32_t sr;
632 ret = (*rdsr_func)(chip, &sr);
633 if (ret != ESP_OK) {
634 return ret;
635 }
636 ESP_EARLY_LOGD(TAG, "set_io_mode: status after 0x%x", sr);
637 if (sr != sr_update) {
638 ret = ESP_ERR_FLASH_NO_RESPONSE;
639 }
640 }
641 return ret;
642 }
643
644 #endif // !CONFIG_SPI_FLASH_ROM_IMPL
645
spi_flash_chip_generic_suspend_cmd_conf(esp_flash_t * chip)646 esp_err_t spi_flash_chip_generic_suspend_cmd_conf(esp_flash_t *chip)
647 {
648 // Only XMC support auto-suspend
649 if (chip->chip_id >> 16 != 0x20) {
650 ESP_EARLY_LOGE(TAG, "The flash you use doesn't support auto suspend, only \'XMC\' is supported");
651 return ESP_ERR_NOT_SUPPORTED;
652 }
653 spi_flash_sus_cmd_conf sus_conf = {
654 .sus_mask = 0x80,
655 .cmd_rdsr = CMD_RDSR2,
656 .sus_cmd = CMD_SUSPEND,
657 .res_cmd = CMD_RESUME,
658 };
659
660 return chip->host->driver->sus_setup(chip->host, &sus_conf);
661 }
662