1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 #include "hal/spi_flash_hal.h"
9 
10 /** Default configuration for the memspi (high speed version) */
11 #define ESP_FLASH_DEFAULT_HOST_DRIVER()  (spi_flash_host_driver_t) { \
12         .dev_config = spi_flash_hal_device_config, \
13         .common_command = spi_flash_hal_common_command, \
14         .read_id = memspi_host_read_id_hs, \
15         .erase_chip = spi_flash_hal_erase_chip, \
16         .erase_sector = spi_flash_hal_erase_sector, \
17         .erase_block = spi_flash_hal_erase_block, \
18         .read_status = memspi_host_read_status_hs, \
19         .set_write_protect = spi_flash_hal_set_write_protect, \
20         .supports_direct_write = spi_flash_hal_supports_direct_write, \
21         .supports_direct_read = spi_flash_hal_supports_direct_read, \
22         .program_page = spi_flash_hal_program_page, \
23         .write_data_slicer = memspi_host_write_data_slicer, \
24         .read = spi_flash_hal_read, \
25         .read_data_slicer = memspi_host_read_data_slicer, \
26         .host_status = spi_flash_hal_check_status, \
27         .configure_host_io_mode = spi_flash_hal_configure_host_io_mode, \
28         .poll_cmd_done = spi_flash_hal_poll_cmd_done, \
29         .flush_cache = memspi_host_flush_cache, \
30         .check_suspend = NULL, \
31         .resume = spi_flash_hal_resume, \
32         .suspend = spi_flash_hal_suspend,\
33         .sus_setup = spi_flash_hal_setup_read_suspend,\
34 }
35 
36 /// configuration for the memspi host
37 typedef spi_flash_hal_config_t memspi_host_config_t;
38 /// context for the memspi host
39 typedef spi_flash_hal_context_t memspi_host_inst_t;
40 
41 /**
42  * Initialize the memory SPI host.
43  *
44  * @param host Pointer to the host structure.
45  * @param cfg Pointer to configuration structure
46  *
47  * @return always return ESP_OK
48  */
49 esp_err_t memspi_host_init_pointers(memspi_host_inst_t *host, const memspi_host_config_t *cfg);
50 
51 /*******************************************************************************
52  * NOTICE
53  * Rest part of this file are part of the HAL layer
54  * The HAL is not public api, don't use in application code.
55  * See readme.md in hal/include/hal/readme.md
56  ******************************************************************************/
57 
58 /**
59  * @brief Read the Status Register read from RDSR (05h).
60  *
61  * High speed implementation of RDID through memspi interface relying on the
62  * ``common_command``.
63  *
64  * @param host The driver context.
65  * @param id Output of the read ID from the slave.
66  *
67  * @return
68  *  - ESP_OK: if success
69  *  - ESP_ERR_FLASH_NO_RESPONSE: if no response from chip
70  *  - or other cases from ``spi_hal_common_command``
71  */
72 esp_err_t memspi_host_read_id_hs(spi_flash_host_inst_t *host, uint32_t *id);
73 
74 /**
75  * High speed implementation of RDSR through memspi interface relying on the
76  * ``common_command``.
77  *
78  * @param host The driver context.
79  * @param id Output of the read ID from the slave.
80  *
81  * @return
82  *  - ESP_OK: if success
83  *  - or other cases from ``spi_hal_common_command``
84  */
85 esp_err_t memspi_host_read_status_hs(spi_flash_host_inst_t *host, uint8_t *out_sr);
86 
87 /**
88  * Flush the cache (if needed) after the contents are modified.
89  *
90  * @param host The driver context.
91  * @param addr Start address of the modified region
92  * @param size Size of the region modified.
93  *
94  * @return always ESP_OK.
95  */
96 esp_err_t memspi_host_flush_cache(spi_flash_host_inst_t *host, uint32_t addr, uint32_t size);
97 
98 /**
99  *  Erase contents of entire chip.
100  *
101  * @param host The driver context.
102  */
103 void memspi_host_erase_chip(spi_flash_host_inst_t *host);
104 
105 /**
106  *  Erase a sector starting from a given address. For 24bit address only.
107  *
108  * @param host The driver context.
109  * @param start_address Starting address of the sector.
110  */
111 void memspi_host_erase_sector(spi_flash_host_inst_t *host, uint32_t start_address);
112 
113 /**
114  *  Erase a block starting from a given address. For 24bit address only.
115  *
116  * @param host The driver context.
117  * @param start_address Starting address of the block.
118  */
119 void memspi_host_erase_block(spi_flash_host_inst_t *host, uint32_t start_address);
120 
121 /**
122  * Program a page with contents of a buffer. For 24bit address only.
123  *
124  * @param host The driver context.
125  * @param buffer Buffer which contains the data to be flashed.
126  * @param address Starting address of where to flash the data.
127  * @param length The number of bytes to flash.
128  */
129 void memspi_host_program_page(spi_flash_host_inst_t *host, const void *buffer, uint32_t address, uint32_t length);
130 
131 /**
132  * Set ability to write to chip.
133  *
134  * @param host The driver context.
135  * @param wp Enable or disable write protect (true - enable, false - disable).
136  */
137 esp_err_t memspi_host_set_write_protect(spi_flash_host_inst_t *host, bool wp);
138 
139 /**
140  * Read data to buffer.
141  *
142  * @param host The driver context.
143  * @param buffer Buffer which contains the data to be read.
144  * @param address Starting address of where to read the data.
145  * @param length The number of bytes to read.
146  */
147 esp_err_t memspi_host_read(spi_flash_host_inst_t *host, void *buffer, uint32_t address, uint32_t read_len);
148 
149 /**
150  * @brief Slicer for read data used in non-encrypted regions. This slicer does nothing but
151  *        limit the length to the maximum size the host supports.
152  *
153  * @param address Flash address to read
154  * @param len Length to read
155  * @param align_address Output of the address to read, should be equal to the input `address`
156  * @param page_size Physical SPI flash page size
157  *
158  * @return Length that can actually be read in one `read` call in `spi_flash_host_driver_t`.
159  */
160 int memspi_host_read_data_slicer(spi_flash_host_inst_t *host, uint32_t address, uint32_t len, uint32_t *align_address, uint32_t page_size);
161 
162 /**
163  * @brief Slicer for write data used in non-encrypted regions. This slicer limit the length to the
164  *        maximum size the host supports, and truncate if the write data lie accross the page boundary
165  *        (256 bytes)
166  *
167  * @param address Flash address to write
168  * @param len Length to write
169  * @param align_address Output of the address to write, should be equal to the input `address`
170  * @param page_size Physical SPI flash page size
171  *
172  * @return Length that can actually be written in one `program_page` call in `spi_flash_host_driver_t`.
173  */
174 int memspi_host_write_data_slicer(spi_flash_host_inst_t *host, uint32_t address, uint32_t len, uint32_t *align_address, uint32_t page_size);
175