RTEMS 6.1-rc4
Loading...
Searching...
No Matches
zynq-qspi-flash.h
1/*
2 * Copyright (C) 2024 Contemporary Software
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
17 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 * POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#if !defined(_ZYNQ_QSPI_FLASH_H_)
27#define _ZYNQ_QSPI_FLASH_H_
28
29#include <stdbool.h>
30#include <stdlib.h>
31
32/*
33 * Driver configuration.
34 */
35#define ZQSPI_FLASH_4BYTE_ADDRESSING 1
36#define ZQSPI_FLASH_FAST_READ 1
37
38#define ZQSPI_FLASH_COMMAND_OFFSET (0) /* FLASH instruction */
39#define ZQSPI_FLASH_ADDRESS_1_OFFSET (1) /* Bits 31-24 of the address */
40#define ZQSPI_FLASH_ADDRESS_2_OFFSET (2) /* Bits 23-16 of the address */
41#define ZQSPI_FLASH_ADDRESS_3_OFFSET (3) /* Bits 16-8 of the address */
42#if ZQSPI_FLASH_4BYTE_ADDRESSING
43 #define ZQSPI_FLASH_ADDRESS_4_OFFSET (4) /* Bits 8-0 of the address */
44 #define ZQSPI_FLASH_DATA_OFFSET (5) /* Start of Data for Read/Write */
45#else
46 #define ZQSPI_FLASH_DATA_OFFSET (4) /* Start of Data for Read/Write */
47#endif
48#define ZQSPI_FLASH_SPI_MAX_PADDING (4) /* Maximum amount of padding. */
49
50#define ZQSPI_FLASH_TX_TRANS 0
51#define ZQSPI_FLASH_RX_TRANS 1
52
53#define ZQSPI_FLASH_BUFFER_SIZE 0x1008
54
55#define ZQPSI_ZYNQ_QSPI_IRQ 51
56
57#define ZQSPI_TIMEOUT_US 5000000U
58#define ZQSPI_TIMEOUT_TICKS (ZQSPI_TIMEOUT_US \
59 / rtems_configuration_get_microseconds_per_tick())
60
61
62/*
63 * Zynq QSPI Flash driver errors.
64 */
65typedef enum
66{
67 ZQSPI_FLASH_NO_ERROR = 0,
68 ZQSPI_FLASH_NOT_OPEN,
69 ZQSPI_FLASH_ALREADY_OPEN,
70 ZQSPI_FLASH_NO_MEMORY,
71 ZQSPI_FLASH_4BYTE_ADDR_NOT_SUPPORTED,
72 ZQSPI_FLASH_BUFFER_OVERFLOW,
73 ZQSPI_FLASH_BUFFER_UNDERFLOW,
74 ZQSPI_FLASH_BAD_ADDRESS,
75 ZQSPI_FLASH_NOT_BLANK,
76 ZQSPI_FLASH_ERASE_FAILURE,
77 ZQSPI_FLASH_READ_ONLY,
78 ZQSPI_FLASH_WRITE_LATCH_CLEAR_FAIL,
79 ZQSPI_FLASH_WRITE_LOCK_FAIL,
80 ZQSPI_FLASH_WRITE_ACROSS_SECTION,
81 ZQSPI_FLASH_WRITE_ERASE_CMD_FAIL,
82 ZQSPI_FLASH_LOCK_FAIL,
83 ZQSPI_FLASH_INVALID_DEVICE,
84 ZQPSI_FLASH_TRANSFER_FAILED,
85 ZQSPI_FLASH_RTEMS_INTR
86} zqspi_error;
87
88/*
89 * A transfer buffer.
90 */
91typedef struct
92{
93 size_t size;
94 size_t length;
95 size_t padding;
96 size_t in;
97 size_t out;
98 uint32_t trans_dir;
99 uint32_t command_len;
100 uint8_t *buffer;
101 uint32_t *tx_data;
102 uint32_t *rx_data;
103 size_t tx_length;
104 size_t rx_length;
105 size_t sending;
106 bool start;
108
109/*
110 * A flash driver instance
111 */
112typedef struct
113{
115 bool initialised;
116 uint32_t jedec_id;
117 uint64_t flash_size;
118 uint32_t flash_read_dummies;
119 uint32_t flash_erase_sector_size;
120 uint32_t flash_page_size;
121} zqspiflash;
122
130zqspi_error zqspi_init(zqspiflash *driver);
131
137void zqspi_close(zqspiflash *driver);
138
149zqspi_error zqspi_read(
150 zqspiflash *driver,
151 uint32_t address,
152 void* buffer,
153 size_t length
154);
155
166zqspi_error zqspi_write(
167 zqspiflash *driver,
168 uint32_t address,
169 const void* buffer,
170 size_t length
171);
172
182zqspi_error zqspi_erase(
183 zqspiflash *driver,
184 uint32_t address,
185 size_t length
186);
187
197zqspi_error zqspi_blank(
198 zqspiflash *driver,
199 uint32_t address,
200 size_t length
201);
202
211zqspi_error zqspi_erase_sector(
212 zqspiflash *driver,
213 uint32_t address
214);
215
225zqspi_error zqspi_write_sector(
226 zqspiflash *driver,
227 uint32_t address,
228 const void* buffer,
229 size_t length
230);
231
239zqspi_error zqspi_erase_device(zqspiflash *driver);
240
250zqspi_error zqspi_readid(zqspiflash *driver, uint32_t *jedec_id);
251
259size_t zqspi_device_size(zqspiflash *driver);
260
268size_t zqspi_device_sector_erase_size(zqspiflash *driver);
269
276void zqspi_transfer_trace(
277 const char* message,
278 const zqspi_transfer_buffer* transfer
279);
280
287void qspi_reg_write(uint32_t reg, uint32_t value);
288
296uint32_t qspi_reg_read(uint32_t reg);
297
306zqspi_error zqspi_transfer_buffer_skip(
307 zqspi_transfer_buffer* transfer,
308 const size_t size
309);
310
311#endif /* _ZYNQ_QSPI_FLASH_H_ */
Definition: media-server.c:46
Definition: zynq-qspi-flash.h:92
Definition: zynq-qspi-flash.h:113