1 /*
2  * SPDX-FileCopyrightText: 2016 Cesanta Software Limited
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  *
6  * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
7  */
8 
9 /* ROM function prototypes for functions in ROM which are
10    called by the flasher stubs.
11 */
12 #pragma once
13 
14 #include <stdint.h>
15 #include "soc_support.h"
16 
17 int uart_rx_one_char(uint8_t *ch);
18 uint8_t uart_rx_one_char_block();
19 int uart_tx_one_char(char ch);
20 
21 #if ESP32C6 || ESP32H2
22 /* uart_tx_one_char doesn't send data to USB device serial, needs to be replaced */
23 int uart_tx_one_char2(char ch);
24 #define uart_tx_one_char(ch) uart_tx_one_char2(ch)
25 #endif // ESP32C6 || ESP32H2
26 
27 void uart_div_modify(uint32_t uart_no, uint32_t baud_div);
28 
29 void ets_delay_us(uint32_t us);
30 
31 typedef enum { SPI_FLASH_RESULT_OK = 0,
32                SPI_FLASH_RESULT_ERR = 1,
33                SPI_FLASH_RESULT_TIMEOUT = 2 } SpiFlashOpResult;
34 
35 SpiFlashOpResult SPILock();
36 SpiFlashOpResult SPIUnlock();
37 SpiFlashOpResult SPIRead(uint32_t addr, void *dst, uint32_t size);
38 SpiFlashOpResult SPIWrite(uint32_t addr, const uint8_t *src, uint32_t size);
39 SpiFlashOpResult SPIEraseChip();
40 SpiFlashOpResult SPIEraseBlock(uint32_t block_num);
41 SpiFlashOpResult SPIEraseSector(uint32_t sector_num);
42 uint32_t SPI_read_status();
43 uint32_t Wait_SPI_Idle();
44 void spi_flash_attach();
45 
46 void SelectSpiFunction();
47 uint32_t SPIParamCfg(uint32_t deviceId, uint32_t chip_size, uint32_t block_size, uint32_t sector_size, uint32_t page_size, uint32_t status_mask);
48 
49 void ets_isr_mask(uint32_t ints);
50 void ets_isr_unmask(uint32_t ints);
51 void ets_set_user_start(void (*user_start_fn)());
52 
53 void software_reset();
54 void software_reset_cpu(int cpu_no);
55 
56 #ifdef ESP32C2  // ESP32C2 ROM uses mbedtls_md5
57 struct MD5Context {  // Called mbedtls_md5_context in ROM
58     uint32_t total[2];        // number of bytes processed
59     uint32_t state[4];        // intermediate digest state
60     unsigned char buffer[64]; // data block being processed
61 };
62 
63 int mbedtls_md5_starts_ret(struct MD5Context *ctx);
64 int mbedtls_md5_update_ret(struct MD5Context *ctx, const unsigned char *input, size_t ilen);
65 int mbedtls_md5_finish_ret(struct MD5Context *ctx, unsigned char digest[16]);
66 
67 #define MD5Init(ctx) mbedtls_md5_starts_ret(ctx)
68 #define MD5Update(ctx, buf, n) mbedtls_md5_update_ret(ctx, buf, n)
69 #define MD5Final(digest, ctx) mbedtls_md5_finish_ret(ctx, digest)
70 #else  // not ESP32C2
71 struct MD5Context {
72     uint32_t buf[4];
73     uint32_t bits[2];
74     uint8_t in[64];
75 };
76 
77 void MD5Init(struct MD5Context *ctx);
78 void MD5Update(struct MD5Context *ctx, void *buf, uint32_t len);
79 void MD5Final(uint8_t digest[16], struct MD5Context *ctx);
80 #endif // not ESP32C2
81 
82 typedef struct {
83     uint32_t device_id;
84     uint32_t chip_size;    // chip size in bytes
85     uint32_t block_size;
86     uint32_t sector_size;
87     uint32_t page_size;
88     uint32_t status_mask;
89 } esp_rom_spiflash_chip_t;
90 
91 
92 typedef void (*int_handler_t)(void *arg);
93 int_handler_t ets_isr_attach(uint32_t int_num, int_handler_t handler,
94                              void *arg);
95 /* Some ESP32-onwards ROM functions */
96 #if ESP32_OR_LATER
97 void uart_tx_flush(int uart);
98 uint32_t ets_efuse_get_spiconfig(void);
99 
100 #if ESP32
101 SpiFlashOpResult esp_rom_spiflash_write_encrypted(uint32_t addr, const uint8_t *src, uint32_t size);
102 #else
103 void SPI_Write_Encrypt_Enable();
104 void SPI_Write_Encrypt_Disable();
105 SpiFlashOpResult SPI_Encrypt_Write(uint32_t flash_addr, const void* data, uint32_t len);
106 #endif
107 
108 #if ESP32S2_OR_LATER
109 uint32_t GetSecurityInfoProc(int* pMsg, int* pnErr, uint8_t *buf);  // pMsg and pnErr unused in ROM
110 SpiFlashOpResult SPI_read_status_high(esp_rom_spiflash_chip_t *spi, uint32_t *status);
111 #else
112 /* Note: On ESP32 this was a static function whose first argument was elided by the
113    compiler. */
114 SpiFlashOpResult SPI_read_status_high(uint32_t *status);
115 #endif
116 
117 SpiFlashOpResult SPI_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value);
118 
119 void intr_matrix_set(int cpu_no, uint32_t module_num, uint32_t intr_num);
120 #endif /* ESP32_OR_LATER */
121 
122 /* RISC-V-only ROM functions */
123 #if IS_RISCV
124 void esprv_intc_int_set_priority(int intr_num, int priority);
125 #endif // IS_RISCV
126 
127 /* USB-OTG and USB-JTAG-Serial imports */
128 #ifdef WITH_USB_OTG
129 #define ACM_BYTES_PER_TX   64
130 #define ACM_STATUS_LINESTATE_CHANGED   -1
131 #define ACM_STATUS_RX                  -4
132 #define LINE_CTRL_BAUD_RATE   (1 << 0)
133 #define LINE_CTRL_RTS         (1 << 1)
134 #define LINE_CTRL_DTR         (1 << 2)
135 #define LINE_CTRL_DCD         (1 << 3)
136 #define LINE_CTRL_DSR         (1 << 4)
137 #define USBDC_PERSIST_ENA  (1<<31)
138 void usb_dw_isr_handler(void* arg);
139 typedef void cdc_acm_device;
140 extern cdc_acm_device *uart_acm_dev;
141 typedef void(*uart_irq_callback_t)(cdc_acm_device *dev, int status);
142 void cdc_acm_irq_callback_set(cdc_acm_device *dev, uart_irq_callback_t cb);
143 void cdc_acm_irq_rx_enable(cdc_acm_device *dev);
144 void cdc_acm_irq_rx_disable(cdc_acm_device *dev);
145 int cdc_acm_fifo_read(cdc_acm_device *dev, uint8_t *rx_data, const int size);
146 int cdc_acm_fifo_fill(cdc_acm_device *dev, const uint8_t *tx_data, int len);
147 int cdc_acm_line_ctrl_get(cdc_acm_device *dev, uint32_t ctrl, uint32_t *val);
148 int cdc_acm_rx_fifo_cnt(cdc_acm_device *dev);
149 void cdc_acm_irq_state_enable(cdc_acm_device *dev);
150 void usb_dc_check_poll_for_interrupts(void);
151 void chip_usb_set_persist_flags(uint32_t flags);
152 int usb_dc_prepare_persist(void);
153 #endif // WITH_USB_OTG
154 
155 #if WITH_USB_JTAG_SERIAL || WITH_USB_OTG
156 typedef struct {
157     uint8_t *pRcvMsgBuff;
158     uint8_t *pWritePos;
159     uint8_t *pReadPos;
160     uint8_t  TrigLvl;
161     int BuffState;
162 } RcvMsgBuff;
163 
164 typedef struct {
165     int     baud_rate;
166     int     data_bits;
167     int     exist_parity;
168     int     parity;
169     int     stop_bits;
170     int     flow_ctrl;
171     uint8_t buff_uart_no;
172     RcvMsgBuff     rcv_buff;
173     int     rcv_state;
174     int     received;
175 } UartDevice;
176 
177 UartDevice * GetUartDevice();
178 #endif // WITH_USB_JTAG_SERIAL || WITH_USB_OTG
179 
180 #if defined(ESP32S3)
181 #define BIT(nr)                 (1UL << (nr))
182 #define ESP_ROM_OPIFLASH_SEL_CS0     (BIT(0))
183 
184 typedef enum {
185     SPI_FLASH_QIO_MODE = 0,
186     SPI_FLASH_QOUT_MODE,
187     SPI_FLASH_DIO_MODE,
188     SPI_FLASH_DOUT_MODE,
189     SPI_FLASH_FASTRD_MODE,
190     SPI_FLASH_SLOWRD_MODE,
191     SPI_FLASH_OPI_STR_MODE,
192     SPI_FLASH_OPI_DTR_MODE,
193     SPI_FLASH_OOUT_MODE,
194     SPI_FLASH_OIO_STR_MODE,
195     SPI_FLASH_OIO_DTR_MODE,
196     SPI_FLASH_QPI_MODE,
197 } SpiFlashRdMode;
198 
199 typedef enum {
200     ESP_ROM_SPIFLASH_RESULT_OK,
201     ESP_ROM_SPIFLASH_RESULT_ERR,
202     ESP_ROM_SPIFLASH_RESULT_TIMEOUT
203 } esp_rom_spiflash_result_t;
204 
205 #define CMD_RDID                    0x9F
206 #define CMD_RDSR                    0x05
207 #define CMD_WREN                    0x06
208 #define CMD_SECTOR_ERASE            0x20
209 #define CMD_SECTOR_ERASE_4B         0x21
210 #define CMD_FSTRD4B                 0x0C
211 #define CMD_LARGE_BLOCK_ERASE       0xD8
212 #define CMD_LARGE_BLOCK_ERASE_4B    0xDC
213 #define CMD_PROGRAM_PAGE_4B         0x12
214 
215 #define OPIFLASH_DRIVER() {   \
216     .rdid = {              \
217         .mode = SPI_FLASH_FASTRD_MODE, \
218         .cmd_bit_len = 8, \
219         .cmd = CMD_RDID, \
220         .addr = 0, \
221         .addr_bit_len = 0, \
222         .dummy_bit_len = 0, \
223         .data_bit_len = 24, \
224         .cs_sel = 0x1, \
225         .is_pe = 0, \
226     }, \
227     .rdsr = { \
228         .mode = SPI_FLASH_FASTRD_MODE, \
229         .cmd_bit_len = 8, \
230         .cmd = CMD_RDSR, \
231         .addr = 0, \
232         .addr_bit_len = 0, \
233         .dummy_bit_len = 0, \
234         .data_bit_len = 8, \
235         .cs_sel = 0x1, \
236         .is_pe = 0, \
237     }, \
238     .wren = { \
239         .mode = SPI_FLASH_FASTRD_MODE, \
240         .cmd_bit_len = 8, \
241         .cmd = CMD_WREN, \
242         .addr = 0, \
243         .addr_bit_len = 0, \
244         .dummy_bit_len = 0, \
245         .data_bit_len = 0, \
246         .cs_sel = 0x1, \
247         .is_pe = 0, \
248     }, \
249     .se = { \
250         .mode = SPI_FLASH_FASTRD_MODE, \
251         .cmd_bit_len = 8, \
252         .cmd = CMD_SECTOR_ERASE_4B, \
253         .addr = 0, \
254         .addr_bit_len = 32, \
255         .dummy_bit_len = 0, \
256         .data_bit_len = 0, \
257         .cs_sel = 0x1, \
258         .is_pe = 1, \
259     }, \
260     .be64k = { \
261         .mode = SPI_FLASH_FASTRD_MODE, \
262         .cmd_bit_len = 8, \
263         .cmd = CMD_LARGE_BLOCK_ERASE_4B, \
264         .addr = 0, \
265         .addr_bit_len = 32, \
266         .dummy_bit_len = 0, \
267         .data_bit_len = 0, \
268         .cs_sel = 0x1, \
269         .is_pe = 1, \
270     }, \
271     .read = { \
272         .mode = SPI_FLASH_FASTRD_MODE, \
273         .cmd_bit_len = 8, \
274         .cmd = CMD_FSTRD4B, \
275         .addr = 0, \
276         .addr_bit_len = 32, \
277         .dummy_bit_len = 0, \
278         .data_bit_len = 0, \
279         .cs_sel = 0x1, \
280         .is_pe = 0, \
281     }, \
282     .pp = { \
283         .mode = SPI_FLASH_FASTRD_MODE, \
284         .cmd_bit_len = 8, \
285         .cmd = CMD_PROGRAM_PAGE_4B, \
286         .addr = 0, \
287         .addr_bit_len = 32, \
288         .dummy_bit_len = 0, \
289         .data_bit_len = 0, \
290         .cs_sel = 0x1, \
291         .is_pe = 1, \
292     }, \
293     .cache_rd_cmd = { \
294         .addr_bit_len = 32, \
295         .dummy_bit_len = 20*2, \
296         .cmd = CMD_FSTRD4B, \
297         .cmd_bit_len = 16, \
298         .var_dummy_en = 1, \
299     } \
300 }
301 
302 #ifndef ESP32S3BETA2
303 typedef struct {
304     uint8_t mode;
305     uint8_t cmd_bit_len;
306     uint16_t cmd;
307     uint32_t addr;
308     uint8_t addr_bit_len;
309     uint8_t dummy_bit_len;
310     uint8_t data_bit_len;
311     uint8_t cs_sel: 4;
312     uint8_t is_pe: 4;
313 } esp_rom_opiflash_cmd_t;
314 
315 typedef struct {
316     uint8_t addr_bit_len;
317     uint8_t dummy_bit_len;
318     uint16_t cmd;
319     uint8_t cmd_bit_len;
320     uint8_t var_dummy_en;
321 } esp_rom_opiflash_spi0rd_t;
322 
323 typedef struct {
324     esp_rom_opiflash_cmd_t rdid;
325     esp_rom_opiflash_cmd_t rdsr;
326     esp_rom_opiflash_cmd_t wren;
327     esp_rom_opiflash_cmd_t se;
328     esp_rom_opiflash_cmd_t be64k;
329     esp_rom_opiflash_cmd_t read;
330     esp_rom_opiflash_cmd_t pp;
331     esp_rom_opiflash_spi0rd_t cache_rd_cmd;
332 } esp_rom_opiflash_def_t;
333 
334 void esp_rom_opiflash_legacy_driver_init(const esp_rom_opiflash_def_t *flash_cmd_def);
335 bool ets_efuse_flash_octal_mode(void);
336 #endif //ESP32S3BETA2
337 
338 void esp_rom_opiflash_exec_cmd(int spi_num, SpiFlashRdMode mode,
339     uint32_t cmd, int cmd_bit_len,
340     uint32_t addr, int addr_bit_len,
341     int dummy_bits,
342     uint8_t* mosi_data, int mosi_bit_len,
343     uint8_t* miso_data, int miso_bit_len,
344     uint32_t cs_mask,
345     bool is_write_erase_operation);
346 
347 esp_rom_spiflash_result_t esp_rom_opiflash_wait_idle();
348 esp_rom_spiflash_result_t esp_rom_opiflash_wren();
349 esp_rom_spiflash_result_t esp_rom_opiflash_erase_sector(uint32_t sector_num);
350 esp_rom_spiflash_result_t esp_rom_opiflash_erase_block_64k(uint32_t block_num);
351 SpiFlashOpResult SPI_write_enable(esp_rom_spiflash_chip_t *spi);
352 #endif // ESP32S3
353