1 // Copyright 2015-2020 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 "esp_log.h"
19 #include "spi_flash_chip_generic.h"
20 #include "spi_flash_defs.h"
21 
22 
23 #define REGION_32BIT(start, len)    ((start) + (len) > (1<<24))
24 #define ADDR_32BIT(addr)            (addr >= (1<<24))
25 
26 
27 static const char TAG[] = "chip_wb";
28 
29 /* Driver for Winbond flash chip */
30 static esp_err_t spi_flash_command_winbond_program_4B(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
31 static esp_err_t spi_flash_command_winbond_erase_sector_4B(esp_flash_t *chip, uint32_t start_address);
32 static esp_err_t spi_flash_command_erase_block_4B(esp_flash_t *chip, uint32_t start_address);
33 
spi_flash_chip_winbond_probe(esp_flash_t * chip,uint32_t flash_id)34 esp_err_t spi_flash_chip_winbond_probe(esp_flash_t *chip, uint32_t flash_id)
35 {
36     /* Check manufacturer and product IDs match our desired masks */
37     const uint8_t MFG_ID = 0xEF;
38     if (flash_id >> 16 != MFG_ID) {
39         return ESP_ERR_NOT_FOUND;
40     }
41 
42     return ESP_OK;
43 }
44 
spi_flash_chip_winbond_read(esp_flash_t * chip,void * buffer,uint32_t address,uint32_t length)45 esp_err_t spi_flash_chip_winbond_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
46 {
47     esp_err_t err = ESP_OK;
48     const uint32_t page_size = chip->chip_drv->page_size;
49     uint32_t align_address;
50     uint8_t temp_buffer[64]; //spiflash hal max length of read no longer than 64byte
51     uint32_t config_io_flags = 0;
52 
53     // Configure the host, and return
54     if (REGION_32BIT(address, length)) {
55         config_io_flags |= SPI_FLASH_CONFIG_IO_MODE_32B_ADDR;
56     }
57     err = chip->chip_drv->config_host_io_mode(chip, config_io_flags);
58 
59     if (err == ESP_ERR_NOT_SUPPORTED) {
60         ESP_LOGE(TAG, "configure host io mode failed - unsupported");
61         return err;
62     }
63 
64     while (err == ESP_OK && length > 0) {
65         memset(temp_buffer, 0xFF, sizeof(temp_buffer));
66         uint32_t read_len = chip->host->driver->read_data_slicer(chip->host, address, length, &align_address, page_size);
67         uint32_t left_off = address - align_address;
68         uint32_t data_len = MIN(align_address + read_len, address + length) - address;
69         err = chip->host->driver->read(chip->host, temp_buffer, align_address, read_len);
70 
71         memcpy(buffer, temp_buffer + left_off, data_len);
72 
73         address += data_len;
74         buffer = (void *)((intptr_t)buffer + data_len);
75         length = length - data_len;
76     }
77 
78     return err;
79 }
80 
spi_flash_chip_winbond_page_program(esp_flash_t * chip,const void * buffer,uint32_t address,uint32_t length)81 esp_err_t spi_flash_chip_winbond_page_program(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
82 {
83     esp_err_t err;
84 
85     err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
86 
87     if (err == ESP_OK) {
88         // Perform the actual Page Program command
89         err = spi_flash_command_winbond_program_4B(chip, buffer, address, length);
90         if (err != ESP_OK) {
91             return err;
92         }
93 
94         err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->page_program_timeout);
95     }
96     return err;
97 }
98 
spi_flash_chip_winbond_erase_sector(esp_flash_t * chip,uint32_t start_address)99 esp_err_t spi_flash_chip_winbond_erase_sector(esp_flash_t *chip, uint32_t start_address)
100 {
101     esp_err_t err = chip->chip_drv->set_chip_write_protect(chip, false);
102     if (err == ESP_OK) {
103         err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
104     }
105 
106     if (err == ESP_OK) {
107         err = spi_flash_command_winbond_erase_sector_4B(chip, start_address);
108         if (err != ESP_OK) {
109             return err;
110         }
111         //to save time, flush cache here
112         if (chip->host->driver->flush_cache) {
113             err = chip->host->driver->flush_cache(chip->host, start_address, chip->chip_drv->sector_size);
114             if (err != ESP_OK) {
115                 return err;
116             }
117         }
118         err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->sector_erase_timeout);
119     }
120     return err;
121 }
122 
spi_flash_chip_winbond_erase_block(esp_flash_t * chip,uint32_t start_address)123 esp_err_t spi_flash_chip_winbond_erase_block(esp_flash_t *chip, uint32_t start_address)
124 {
125     esp_err_t err = chip->chip_drv->set_chip_write_protect(chip, false);
126     if (err == ESP_OK) {
127         err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
128     }
129 
130     if (err == ESP_OK) {
131         err = spi_flash_command_erase_block_4B(chip, start_address);
132         if (err != ESP_OK) {
133             return err;
134         }
135         //to save time, flush cache here
136         if (chip->host->driver->flush_cache) {
137             err = chip->host->driver->flush_cache(chip->host, start_address, chip->chip_drv->block_erase_size);
138             if (err != ESP_OK) {
139                 return err;
140             }
141         }
142         err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->block_erase_timeout);
143     }
144     return err;
145 }
146 
spi_flash_chip_winbond_get_caps(esp_flash_t * chip)147 spi_flash_caps_t spi_flash_chip_winbond_get_caps(esp_flash_t *chip)
148 {
149     spi_flash_caps_t caps_flags = 0;
150     // 32M-bits address support
151     if ((chip->chip_id & 0xFF) >= 0x19) {
152         caps_flags |= SPI_FLASH_CHIP_CAP_32MB_SUPPORT;
153     }
154     // flash-suspend is not supported
155     // flash read unique id.
156     caps_flags |= SPI_FLASH_CHIP_CAP_UNIQUE_ID;
157     return caps_flags;
158 }
159 
160 static const char chip_name[] = "winbond";
161 
162 // The issi chip can use the functions for generic chips except from set read mode and probe,
163 // So we only replace these two functions.
164 const spi_flash_chip_t esp_flash_chip_winbond = {
165     .name = chip_name,
166     .timeout = &spi_flash_chip_generic_timeout,
167     .probe = spi_flash_chip_winbond_probe,
168     .reset = spi_flash_chip_generic_reset,
169     .detect_size = spi_flash_chip_generic_detect_size,
170     .erase_chip = spi_flash_chip_generic_erase_chip,
171     .erase_sector = spi_flash_chip_winbond_erase_sector,
172     .erase_block = spi_flash_chip_winbond_erase_block,
173     .sector_size = 4 * 1024,
174     .block_erase_size = 64 * 1024,
175 
176     .get_chip_write_protect = spi_flash_chip_generic_get_write_protect,
177     .set_chip_write_protect = spi_flash_chip_generic_set_write_protect,
178 
179     .num_protectable_regions = 0,
180     .protectable_regions = NULL,
181     .get_protected_regions = NULL,
182     .set_protected_regions = NULL,
183 
184     .read = spi_flash_chip_winbond_read,
185     .write = spi_flash_chip_generic_write,
186     .program_page = spi_flash_chip_winbond_page_program,
187     .page_size = 256,
188     .write_encrypted = spi_flash_chip_generic_write_encrypted,
189 
190     .wait_idle = spi_flash_chip_generic_wait_idle,
191     .set_io_mode = spi_flash_chip_generic_set_io_mode,
192     .get_io_mode = spi_flash_chip_generic_get_io_mode,
193 
194     .read_reg = spi_flash_chip_generic_read_reg,
195     .yield = spi_flash_chip_generic_yield,
196     .sus_setup = spi_flash_chip_generic_suspend_cmd_conf,
197     .read_unique_id = spi_flash_chip_generic_read_unique_id,
198     .get_chip_caps = spi_flash_chip_winbond_get_caps,
199     .config_host_io_mode = spi_flash_chip_generic_config_host_io_mode,
200 };
201 
202 
spi_flash_command_winbond_program_4B(esp_flash_t * chip,const void * buffer,uint32_t address,uint32_t length)203 static esp_err_t spi_flash_command_winbond_program_4B(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
204 {
205     bool addr_4b = ADDR_32BIT(address);
206     spi_flash_trans_t t = {
207         .command = (addr_4b? CMD_PROGRAM_PAGE_4B: CMD_PROGRAM_PAGE),
208         .address_bitlen = (addr_4b? 32: 24),
209         .address = address,
210         .mosi_len = length,
211         .mosi_data = buffer,
212     };
213     return chip->host->driver->common_command(chip->host, &t);
214 }
215 
spi_flash_command_winbond_erase_sector_4B(esp_flash_t * chip,uint32_t start_address)216 esp_err_t spi_flash_command_winbond_erase_sector_4B(esp_flash_t *chip, uint32_t start_address)
217 {
218     bool addr_4b = ADDR_32BIT(start_address);
219     spi_flash_trans_t t = {
220         .command = (addr_4b? CMD_SECTOR_ERASE_4B: CMD_SECTOR_ERASE),
221         .address_bitlen = (addr_4b? 32: 24),
222         .address = start_address,
223     };
224     return chip->host->driver->common_command(chip->host, &t);
225 }
226 
spi_flash_command_erase_block_4B(esp_flash_t * chip,uint32_t start_address)227 esp_err_t spi_flash_command_erase_block_4B(esp_flash_t *chip, uint32_t start_address)
228 {
229     bool addr_4b = ADDR_32BIT(start_address);
230     spi_flash_trans_t t = {
231         .command = (addr_4b? CMD_LARGE_BLOCK_ERASE_4B: CMD_LARGE_BLOCK_ERASE),
232         .address_bitlen = (addr_4b? 32: 24),
233         .address = start_address,
234     };
235     return chip->host->driver->common_command(chip->host, &t);
236 }
237