1 /*
2  * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #pragma once
7 
8 #include <stdint.h>
9 #include "esp_err.h"
10 #include "esp_attr.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 typedef unsigned (*bootloader_flash_read_status_fn_t)(void);
17 typedef void (*bootloader_flash_write_status_fn_t)(unsigned);
18 
19 typedef struct __attribute__((packed))
20 {
21     const char *manufacturer;
22     uint8_t mfg_id; /* 8-bit JEDEC manufacturer ID */
23     uint16_t flash_id; /* 16-bit JEDEC flash chip ID */
24     uint16_t id_mask; /* Bits to match on in flash chip ID */
25     bootloader_flash_read_status_fn_t read_status_fn;
26     bootloader_flash_write_status_fn_t write_status_fn;
27     uint8_t status_qio_bit;
28 } bootloader_qio_info_t;
29 
30 /**
31  * @brief Read 8 bit status using RDSR command
32  *
33  * @return Value of SR1.
34  */
35 unsigned bootloader_read_status_8b_rdsr(void);
36 
37 /**
38  * @brief Read 8 bit status (second byte) using RDSR2 command
39  *
40  * @return Value of SR2
41  */
42 unsigned bootloader_read_status_8b_rdsr2(void);
43 
44 /**
45  * @brief Read 8 bit status (third byte) using RDSR3 command
46  *
47  * @return Value of SR3
48  */
49 unsigned bootloader_read_status_8b_rdsr3(void);
50 
51 /**
52  * @brief Read 16 bit status using RDSR & RDSR2 (low and high bytes)
53  *
54  * @return Value of SR2#SR1.
55  */
56 unsigned bootloader_read_status_16b_rdsr_rdsr2(void);
57 
58 /**
59  * @brief Write 8 bit status using WRSR
60  */
61 void bootloader_write_status_8b_wrsr(unsigned new_status);
62 
63 /**
64  * @brief Write 8 bit status (second byte) using WRSR2.
65  */
66 void bootloader_write_status_8b_wrsr2(unsigned new_status);
67 
68 /**
69  * @brief Write 8 bit status (third byte) using WRSR3.
70  */
71 void bootloader_write_status_8b_wrsr3(unsigned new_status);
72 
73 /**
74  * @brief Write 16 bit status using WRSR, (both write SR1 and SR2)
75  */
76 void bootloader_write_status_16b_wrsr(unsigned new_status);
77 
78 /**
79  * @brief Read 8 bit status of XM25QU64A.
80  *
81  * @return Value of 8 bit SR.
82  */
83 unsigned bootloader_read_status_8b_xmc25qu64a(void);
84 
85 /**
86  * @brief Write 8 bit status for XM25QU64A
87  */
88 void bootloader_write_status_8b_xmc25qu64a(unsigned new_status);
89 
90 /* Array of known flash chips and data to enable Quad I/O mode
91 
92    Manufacturer & flash ID can be tested by running "esptool.py
93    flash_id"
94 
95    If manufacturer ID matches, and flash ID ORed with flash ID mask
96    matches, enable_qio_mode() will execute "Read Cmd", test if bit
97    number "QIE Bit" is set, and if not set it will call "Write Cmd"
98    with this bit set.
99 
100    Searching of this table stops when the first match is found.
101  */
102 extern const bootloader_qio_info_t __attribute__((weak)) bootloader_flash_qe_support_list[];
103 
104 /**
105   * @brief Unlock Flash write protect.
106   *        Please do not call this function in SDK.
107   *
108   * @note This can be overridden because it's attribute weak.
109   */
110 esp_err_t __attribute__((weak)) bootloader_flash_unlock(void);
111 
112 #if CONFIG_BOOTLOADER_CACHE_32BIT_ADDR_OCTAL_FLASH
113 /**
114  * @brief Enable 32bits address flash(larger than 16MB) can map to cache.
115  *
116  * @param flash_mode SPI flash working mode.
117  *
118  * @note This can be overridden because it's attribute weak.
119  */
120 void __attribute__((weak)) bootloader_flash_32bits_address_map_enable(esp_rom_spiflash_read_mode_t flash_mode);
121 #endif
122 
123 #ifdef __cplusplus
124 }
125 #endif
126