1 // Copyright 2015-2021 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 "spi_flash_chip_generic.h"
17 #include "spi_flash_chip_gd.h"
18 #include "spi_flash_defs.h"
19 
spi_flash_chip_boya_probe(esp_flash_t * chip,uint32_t flash_id)20 esp_err_t spi_flash_chip_boya_probe(esp_flash_t *chip, uint32_t flash_id)
21 {
22     /* Check manufacturer and product IDs match our desired masks */
23     const uint8_t MFG_ID = 0x68;
24     if (flash_id >> 16 != MFG_ID) {
25         return ESP_ERR_NOT_FOUND;
26     }
27 
28     const uint16_t FLASH_ID_MASK = 0xFF00;
29     const uint16_t FLASH_ID_VALUE = 0x4000;
30     if ((flash_id & FLASH_ID_MASK) != FLASH_ID_VALUE) {
31         return ESP_ERR_NOT_FOUND;
32     }
33 
34     return ESP_OK;
35 }
36 
spi_flash_chip_boya_get_caps(esp_flash_t * chip)37 spi_flash_caps_t spi_flash_chip_boya_get_caps(esp_flash_t *chip)
38 {
39     spi_flash_caps_t caps_flags = 0;
40     // 32-bit-address flash is not supported
41     // flash-suspend is not supported
42     // flash read unique id.
43     caps_flags |= SPI_FLASH_CHIP_CAP_UNIQUE_ID;
44     return caps_flags;
45 }
46 
47 static const char chip_name[] = "boya";
48 
49 // The BOYA chip can use the functions for generic chips except from set read mode and probe,
50 // So we only replace these two functions.
51 const spi_flash_chip_t esp_flash_chip_boya = {
52     .name = chip_name,
53     .timeout = &spi_flash_chip_generic_timeout,
54     .probe = spi_flash_chip_boya_probe,
55     .reset = spi_flash_chip_generic_reset,
56     .detect_size = spi_flash_chip_generic_detect_size,
57     .erase_chip = spi_flash_chip_generic_erase_chip,
58     .erase_sector = spi_flash_chip_generic_erase_sector,
59     .erase_block = spi_flash_chip_generic_erase_block,
60     .sector_size = 4 * 1024,
61     .block_erase_size = 64 * 1024,
62 
63     .get_chip_write_protect = spi_flash_chip_generic_get_write_protect,
64     .set_chip_write_protect = spi_flash_chip_generic_set_write_protect,
65 
66     .num_protectable_regions = 0,
67     .protectable_regions = NULL,
68     .get_protected_regions = NULL,
69     .set_protected_regions = NULL,
70 
71     .read = spi_flash_chip_generic_read,
72     .write = spi_flash_chip_generic_write,
73     .program_page = spi_flash_chip_generic_page_program,
74     .page_size = 256,
75     .write_encrypted = spi_flash_chip_generic_write_encrypted,
76 
77     .wait_idle = spi_flash_chip_generic_wait_idle,
78     .set_io_mode = spi_flash_chip_generic_set_io_mode,
79     .get_io_mode = spi_flash_chip_generic_get_io_mode,
80 
81     .read_reg = spi_flash_chip_generic_read_reg,
82     .yield = spi_flash_chip_generic_yield,
83     .sus_setup = spi_flash_chip_generic_suspend_cmd_conf,
84     .read_unique_id = spi_flash_chip_generic_read_unique_id,
85     .get_chip_caps = spi_flash_chip_boya_get_caps,
86     .config_host_io_mode = spi_flash_chip_generic_config_host_io_mode,
87 };
88