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 
52     // Configure the host, and return
53     err = spi_flash_chip_generic_config_host_io_mode(chip, REGION_32BIT(address, length));
54 
55     if (err == ESP_ERR_NOT_SUPPORTED) {
56         ESP_LOGE(TAG, "configure host io mode failed - unsupported");
57         return err;
58     }
59 
60     while (err == ESP_OK && length > 0) {
61         memset(temp_buffer, 0xFF, sizeof(temp_buffer));
62         uint32_t read_len = chip->host->driver->read_data_slicer(chip->host, address, length, &align_address, page_size);
63         uint32_t left_off = address - align_address;
64         uint32_t data_len = MIN(align_address + read_len, address + length) - address;
65         err = chip->host->driver->read(chip->host, temp_buffer, align_address, read_len);
66 
67         memcpy(buffer, temp_buffer + left_off, data_len);
68 
69         address += data_len;
70         buffer = (void *)((intptr_t)buffer + data_len);
71         length = length - data_len;
72     }
73 
74     return err;
75 }
76 
spi_flash_chip_winbond_page_program(esp_flash_t * chip,const void * buffer,uint32_t address,uint32_t length)77 esp_err_t spi_flash_chip_winbond_page_program(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
78 {
79     esp_err_t err;
80 
81     err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
82 
83     if (err == ESP_OK) {
84         // Perform the actual Page Program command
85         err = spi_flash_command_winbond_program_4B(chip, buffer, address, length);
86         if (err != ESP_OK) {
87             return err;
88         }
89 
90         err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->page_program_timeout);
91     }
92     return err;
93 }
94 
spi_flash_chip_winbond_erase_sector(esp_flash_t * chip,uint32_t start_address)95 esp_err_t spi_flash_chip_winbond_erase_sector(esp_flash_t *chip, uint32_t start_address)
96 {
97     esp_err_t err = chip->chip_drv->set_chip_write_protect(chip, false);
98     if (err == ESP_OK) {
99         err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
100     }
101 
102     if (err == ESP_OK) {
103         err = spi_flash_command_winbond_erase_sector_4B(chip, start_address);
104         if (err != ESP_OK) {
105             return err;
106         }
107         //to save time, flush cache here
108         if (chip->host->driver->flush_cache) {
109             err = chip->host->driver->flush_cache(chip->host, start_address, chip->chip_drv->sector_size);
110             if (err != ESP_OK) {
111                 return err;
112             }
113         }
114         err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->sector_erase_timeout);
115     }
116     return err;
117 }
118 
spi_flash_chip_winbond_erase_block(esp_flash_t * chip,uint32_t start_address)119 esp_err_t spi_flash_chip_winbond_erase_block(esp_flash_t *chip, uint32_t start_address)
120 {
121     esp_err_t err = chip->chip_drv->set_chip_write_protect(chip, false);
122     if (err == ESP_OK) {
123         err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
124     }
125 
126     if (err == ESP_OK) {
127         err = spi_flash_command_erase_block_4B(chip, start_address);
128         if (err != ESP_OK) {
129             return err;
130         }
131         //to save time, flush cache here
132         if (chip->host->driver->flush_cache) {
133             err = chip->host->driver->flush_cache(chip->host, start_address, chip->chip_drv->block_erase_size);
134             if (err != ESP_OK) {
135                 return err;
136             }
137         }
138         err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->block_erase_timeout);
139     }
140     return err;
141 }
142 
143 static const char chip_name[] = "winbond";
144 
145 // The issi chip can use the functions for generic chips except from set read mode and probe,
146 // So we only replace these two functions.
147 const spi_flash_chip_t esp_flash_chip_winbond = {
148     .name = chip_name,
149     .timeout = &spi_flash_chip_generic_timeout,
150     .probe = spi_flash_chip_winbond_probe,
151     .reset = spi_flash_chip_generic_reset,
152     .detect_size = spi_flash_chip_generic_detect_size,
153     .erase_chip = spi_flash_chip_generic_erase_chip,
154     .erase_sector = spi_flash_chip_winbond_erase_sector,
155     .erase_block = spi_flash_chip_winbond_erase_block,
156     .sector_size = 4 * 1024,
157     .block_erase_size = 64 * 1024,
158 
159     .get_chip_write_protect = spi_flash_chip_generic_get_write_protect,
160     .set_chip_write_protect = spi_flash_chip_generic_set_write_protect,
161 
162     .num_protectable_regions = 0,
163     .protectable_regions = NULL,
164     .get_protected_regions = NULL,
165     .set_protected_regions = NULL,
166 
167     .read = spi_flash_chip_winbond_read,
168     .write = spi_flash_chip_generic_write,
169     .program_page = spi_flash_chip_winbond_page_program,
170     .page_size = 256,
171     .write_encrypted = spi_flash_chip_generic_write_encrypted,
172 
173     .wait_idle = spi_flash_chip_generic_wait_idle,
174     .set_io_mode = spi_flash_chip_generic_set_io_mode,
175     .get_io_mode = spi_flash_chip_generic_get_io_mode,
176 
177     .read_reg = spi_flash_chip_generic_read_reg,
178     .yield = spi_flash_chip_generic_yield,
179     .sus_setup = spi_flash_chip_generic_suspend_cmd_conf,
180 };
181 
182 
spi_flash_command_winbond_program_4B(esp_flash_t * chip,const void * buffer,uint32_t address,uint32_t length)183 static esp_err_t spi_flash_command_winbond_program_4B(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
184 {
185     bool addr_4b = ADDR_32BIT(address);
186     spi_flash_trans_t t = {
187         .command = (addr_4b? CMD_PROGRAM_PAGE_4B: CMD_PROGRAM_PAGE),
188         .address_bitlen = (addr_4b? 32: 24),
189         .address = address,
190         .mosi_len = length,
191         .mosi_data = buffer,
192     };
193     return chip->host->driver->common_command(chip->host, &t);
194 }
195 
spi_flash_command_winbond_erase_sector_4B(esp_flash_t * chip,uint32_t start_address)196 esp_err_t spi_flash_command_winbond_erase_sector_4B(esp_flash_t *chip, uint32_t start_address)
197 {
198     bool addr_4b = ADDR_32BIT(start_address);
199     spi_flash_trans_t t = {
200         .command = (addr_4b? CMD_SECTOR_ERASE_4B: CMD_SECTOR_ERASE),
201         .address_bitlen = (addr_4b? 32: 24),
202         .address = start_address,
203     };
204     return chip->host->driver->common_command(chip->host, &t);
205 }
206 
spi_flash_command_erase_block_4B(esp_flash_t * chip,uint32_t start_address)207 esp_err_t spi_flash_command_erase_block_4B(esp_flash_t *chip, uint32_t start_address)
208 {
209     bool addr_4b = ADDR_32BIT(start_address);
210     spi_flash_trans_t t = {
211         .command = (addr_4b? CMD_LARGE_BLOCK_ERASE_4B: CMD_LARGE_BLOCK_ERASE),
212         .address_bitlen = (addr_4b? 32: 24),
213         .address = start_address,
214     };
215     return chip->host->driver->common_command(chip->host, &t);
216 }
217