1 /*
2 * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdlib.h>
8 #include "spi_flash_chip_generic.h"
9 #include "spi_flash_defs.h"
10 #include "esp_log.h"
11 #include "hal/spi_flash_hal.h"
12
13 /* Driver for MXIC flash chip */
14
spi_flash_chip_mxic_probe(esp_flash_t * chip,uint32_t flash_id)15 esp_err_t spi_flash_chip_mxic_probe(esp_flash_t *chip, uint32_t flash_id)
16 {
17 /* Check manufacturer and product IDs match our desired masks */
18 const uint8_t MFG_ID = 0xC2;
19 if (flash_id >> 16 != MFG_ID) {
20 return ESP_ERR_NOT_FOUND;
21 }
22 if (chip->read_mode >= SPI_FLASH_OPI_FLAG) {
23 // The code here serve for ordinary mxic chip. If opi mode has been selected, go `spi_flash_chip_mxic_opi.c`
24 return ESP_ERR_NOT_FOUND;
25 }
26
27 return ESP_OK;
28 }
29
spi_flash_chip_mxic_detect_size(esp_flash_t * chip,uint32_t * size)30 esp_err_t spi_flash_chip_mxic_detect_size(esp_flash_t *chip, uint32_t *size)
31 {
32 uint32_t id = chip->chip_id;
33 *size = 0;
34
35 /* Can't detect size unless the high byte of the product ID matches the same convention, which is usually 0x40 or
36 * 0xC0 or similar. */
37 if (((id & 0xFFFF) == 0x0000) || ((id & 0xFFFF) == 0xFFFF)) {
38 return ESP_ERR_FLASH_UNSUPPORTED_CHIP;
39 }
40
41 uint32_t mem_density = (id & 0xFF);
42 if (mem_density > 0x30) { // For OPI chips
43 mem_density -= 0x20;
44 }
45
46 *size = (mem_density <= 31) ? (1U << mem_density) : 0;
47 return ESP_OK;
48 }
49
50 esp_err_t spi_flash_chip_issi_set_io_mode(esp_flash_t *chip);
51 esp_err_t spi_flash_chip_issi_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode);
52
53 // Use the same implementation as ISSI chips
54 #define spi_flash_chip_mxic_set_io_mode spi_flash_chip_issi_set_io_mode
55 #define spi_flash_chip_mxic_get_io_mode spi_flash_chip_issi_get_io_mode
56 #define spi_flash_chip_mxic_read_reg spi_flash_chip_generic_read_reg
57
58 static const char chip_name[] = "mxic";
59
spi_flash_chip_mxic_get_caps(esp_flash_t * chip)60 spi_flash_caps_t spi_flash_chip_mxic_get_caps(esp_flash_t *chip)
61 {
62 spi_flash_caps_t caps_flags = 0;
63 // 32-bit-address flash is not supported
64 // flash-suspend is not supported
65 // reading unique id is not supported.
66 return caps_flags;
67 }
68
69 // The mxic chip can use the functions for generic chips except from set read mode and probe,
70 // So we only replace these two functions.
71 const spi_flash_chip_t esp_flash_chip_mxic = {
72 .name = chip_name,
73 .timeout = &spi_flash_chip_generic_timeout,
74 .probe = spi_flash_chip_mxic_probe,
75 .reset = spi_flash_chip_generic_reset,
76 .detect_size = spi_flash_chip_mxic_detect_size,
77 .erase_chip = spi_flash_chip_generic_erase_chip,
78 .erase_sector = spi_flash_chip_generic_erase_sector,
79 .erase_block = spi_flash_chip_generic_erase_block,
80 .sector_size = 4 * 1024,
81 .block_erase_size = 64 * 1024,
82
83 .get_chip_write_protect = spi_flash_chip_generic_get_write_protect,
84 .set_chip_write_protect = spi_flash_chip_generic_set_write_protect,
85
86 .num_protectable_regions = 0,
87 .protectable_regions = NULL,
88 .get_protected_regions = NULL,
89 .set_protected_regions = NULL,
90
91 .read = spi_flash_chip_generic_read,
92 .write = spi_flash_chip_generic_write,
93 .program_page = spi_flash_chip_generic_page_program,
94 .page_size = 256,
95 .write_encrypted = spi_flash_chip_generic_write_encrypted,
96
97 .wait_idle = spi_flash_chip_generic_wait_idle,
98 .set_io_mode = spi_flash_chip_mxic_set_io_mode,
99 .get_io_mode = spi_flash_chip_mxic_get_io_mode,
100
101 .read_reg = spi_flash_chip_mxic_read_reg,
102 .yield = spi_flash_chip_generic_yield,
103 .sus_setup = spi_flash_chip_generic_suspend_cmd_conf,
104 .read_unique_id = spi_flash_chip_generic_read_unique_id_none,
105 .get_chip_caps = spi_flash_chip_mxic_get_caps,
106 .config_host_io_mode = spi_flash_chip_generic_config_host_io_mode,
107 };
108