1 |
70 |
zero_gravi |
// #################################################################################################
|
2 |
|
|
// # << NEORV32: neorv32_xip.c - Execute In Place (XIP) Module HW Driver (Source) >> #
|
3 |
|
|
// # ********************************************************************************************* #
|
4 |
|
|
// # BSD 3-Clause License #
|
5 |
|
|
// # #
|
6 |
|
|
// # Copyright (c) 2022, Stephan Nolting. All rights reserved. #
|
7 |
|
|
// # #
|
8 |
|
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
9 |
|
|
// # permitted provided that the following conditions are met: #
|
10 |
|
|
// # #
|
11 |
|
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
12 |
|
|
// # conditions and the following disclaimer. #
|
13 |
|
|
// # #
|
14 |
|
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
15 |
|
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
16 |
|
|
// # provided with the distribution. #
|
17 |
|
|
// # #
|
18 |
|
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
19 |
|
|
// # endorse or promote products derived from this software without specific prior written #
|
20 |
|
|
// # permission. #
|
21 |
|
|
// # #
|
22 |
|
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
23 |
|
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
24 |
|
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
25 |
|
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
26 |
|
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
27 |
|
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
28 |
|
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
29 |
|
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
30 |
|
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
31 |
|
|
// # ********************************************************************************************* #
|
32 |
|
|
// # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
33 |
|
|
// #################################################################################################
|
34 |
|
|
|
35 |
|
|
|
36 |
|
|
/**********************************************************************//**
|
37 |
|
|
* @file neorv32_xip.c
|
38 |
|
|
* @brief Execute in place module (XIP) HW driver source file.
|
39 |
|
|
*
|
40 |
|
|
* @note These functions should only be used if the XIP module was synthesized (IO_XIP_EN = true).
|
41 |
|
|
**************************************************************************/
|
42 |
|
|
|
43 |
|
|
#include "neorv32.h"
|
44 |
|
|
#include "neorv32_xip.h"
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
/**********************************************************************//**
|
48 |
|
|
* Check if XIP module was synthesized.
|
49 |
|
|
*
|
50 |
|
|
* @return 0 if XIP was not synthesized, 1 if XIP is available.
|
51 |
|
|
**************************************************************************/
|
52 |
|
|
int neorv32_xip_available(void) {
|
53 |
|
|
|
54 |
|
|
if (NEORV32_SYSINFO.SOC & (1 << SYSINFO_SOC_IO_XIP)) {
|
55 |
|
|
return 1;
|
56 |
|
|
}
|
57 |
|
|
else {
|
58 |
|
|
return 0;
|
59 |
|
|
}
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
/**********************************************************************//**
|
64 |
|
|
* Configure XIP module: configure SPI properties.
|
65 |
|
|
*
|
66 |
|
|
* @warning This will reset the XIP module overriding the CTRL register.
|
67 |
|
|
* @note This function will also send 64 dummy clocks via the SPI port (with chip-select disabled).
|
68 |
|
|
*
|
69 |
|
|
* @param[in] prsc SPI clock prescaler select (0..7).
|
70 |
|
|
* @param[in] cpol SPI clock polarity (0/1).
|
71 |
|
|
* @param[in] cpha SPI clock phase(0/1).
|
72 |
|
|
* @param[in] rd_cmd SPI flash read command.
|
73 |
|
|
* @return 0 if configuration is OK, 1 if configuration error.
|
74 |
|
|
**************************************************************************/
|
75 |
|
|
int neorv32_xip_init(uint8_t prsc, uint8_t cpol, uint8_t cpha, uint8_t rd_cmd) {
|
76 |
|
|
|
77 |
|
|
// configuration check
|
78 |
|
|
if ((prsc > 7) || (cpol > 1) || (cpha > 1)) {
|
79 |
|
|
return 1;
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
// reset module
|
83 |
|
|
NEORV32_XIP.CTRL = 0;
|
84 |
|
|
|
85 |
|
|
uint32_t ctrl = 0;
|
86 |
|
|
|
87 |
|
|
ctrl |= ((uint32_t)(1 )) << XIP_CTRL_EN; // enable module
|
88 |
|
|
ctrl |= ((uint32_t)(prsc & 0x07)) << XIP_CTRL_PRSC0;
|
89 |
|
|
ctrl |= ((uint32_t)(cpol & 0x01)) << XIP_CTRL_CPOL;
|
90 |
|
|
ctrl |= ((uint32_t)(cpha & 0x01)) << XIP_CTRL_CPHA;
|
91 |
|
|
ctrl |= ((uint32_t)(8 )) << XIP_CTRL_SPI_NBYTES_LSB; // set 8 bytes transfer size as default
|
92 |
|
|
ctrl |= ((uint32_t)(rd_cmd & 0xff)) << XIP_CTRL_RD_CMD_LSB;
|
93 |
|
|
|
94 |
|
|
NEORV32_XIP.CTRL = ctrl;
|
95 |
|
|
|
96 |
|
|
// send 64 dummy clocks
|
97 |
|
|
NEORV32_XIP.DATA_LO = 0;
|
98 |
|
|
NEORV32_XIP.DATA_HI = 0; // trigger SPI transfer
|
99 |
|
|
|
100 |
|
|
// wait for transfer to complete
|
101 |
|
|
while(NEORV32_XIP.CTRL & (1 << XIP_CTRL_PHY_BUSY));
|
102 |
|
|
|
103 |
|
|
NEORV32_XIP.CTRL |= 1 << XIP_CTRL_SPI_CSEN; // finally enable SPI chip-select
|
104 |
|
|
|
105 |
|
|
return 0;
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
/**********************************************************************//**
|
110 |
|
|
* Enable XIP mode (to allow CPU to _transparently_ fetch instructions).
|
111 |
|
|
*
|
112 |
|
|
* @warning This function is blocking until the XIP mode is ready.
|
113 |
|
|
*
|
114 |
|
|
* @param[in] abytes Number of address bytes used to access the SPI flash (1,2,3,4).
|
115 |
|
|
* @param[in] page_base XIP memory page base address (top 4 address bits, 0..15).
|
116 |
|
|
* @return 0 if XIP configuration is OK, 1 if configuration error.
|
117 |
|
|
**************************************************************************/
|
118 |
|
|
int neorv32_xip_start(uint8_t abytes, uint32_t page_base) {
|
119 |
|
|
|
120 |
|
|
if ((abytes < 1) || (abytes > 4)) {
|
121 |
|
|
return 1;
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
if (page_base & 0x0FFFFFFF) {
|
125 |
|
|
return 1;
|
126 |
|
|
}
|
127 |
|
|
page_base >>= 28;
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
uint32_t ctrl = NEORV32_XIP.CTRL;
|
131 |
|
|
|
132 |
|
|
// address bytes send to SPI flash
|
133 |
|
|
ctrl &= ~(3 << XIP_CTRL_XIP_ABYTES_LSB); // clear old configuration
|
134 |
|
|
ctrl |= ((uint32_t)(abytes-1)) << XIP_CTRL_XIP_ABYTES_LSB; // set new configuration
|
135 |
|
|
|
136 |
|
|
// total number of bytes to transfer via SPI
|
137 |
|
|
// 'abytes' address bytes + 1 command byte + 4 bytes RX data (one 32-bit word)
|
138 |
|
|
ctrl &= ~(0xF << XIP_CTRL_SPI_NBYTES_LSB); // clear old configuration
|
139 |
|
|
ctrl |= ((uint32_t)(abytes+1+4)) << XIP_CTRL_SPI_NBYTES_LSB; // set new configuration
|
140 |
|
|
|
141 |
|
|
// XIP memory page
|
142 |
|
|
ctrl &= ~(0xF << XIP_CTRL_PAGE_LSB); // clear old configuration
|
143 |
|
|
ctrl |= ((uint32_t)(page_base & 0xf)) << XIP_CTRL_PAGE_LSB; // set new configuration
|
144 |
|
|
|
145 |
|
|
ctrl |= 1 << XIP_CTRL_XIP_EN; // enable XIP mode
|
146 |
|
|
|
147 |
|
|
NEORV32_XIP.CTRL = ctrl;
|
148 |
|
|
|
149 |
|
|
return 0;
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
/**********************************************************************//**
|
154 |
|
|
* Enable high-speed SPI mode (running at half of the processor clock).
|
155 |
|
|
*
|
156 |
|
|
* @note High-speed SPI mode ignores the programmed clock prescaler configuration.
|
157 |
|
|
**************************************************************************/
|
158 |
|
|
void neorv32_xip_highspeed_enable(void) {
|
159 |
|
|
|
160 |
|
|
NEORV32_XIP.CTRL |= 1 << XIP_CTRL_HIGHSPEED;
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
/**********************************************************************//**
|
165 |
|
|
* Disable high-speed SPI mode.
|
166 |
|
|
*
|
167 |
|
|
* @note High-speed SPI mode ignores the programmed clock prescaler configuration.
|
168 |
|
|
**************************************************************************/
|
169 |
|
|
void neorv32_xip_highspeed_disable(void) {
|
170 |
|
|
|
171 |
|
|
NEORV32_XIP.CTRL &= ~(1 << XIP_CTRL_HIGHSPEED);
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
/**********************************************************************//**
|
176 |
|
|
* Direct SPI access to the XIP flash.
|
177 |
|
|
*
|
178 |
|
|
* @warning This function can only be used BEFORE the XIP-mode is activated!
|
179 |
|
|
* @note This function is blocking.
|
180 |
|
|
*
|
181 |
|
|
* @param[in] nbytes Number of bytes to transfer (1..8).
|
182 |
|
|
* @param[in,out] rtx_data Pointer to 64-bit TX/RX data (MSB-aligned for sending, LSB-aligned for receiving (only 32-bit)).
|
183 |
|
|
* @return 0 if valid transfer, 1 if transfer configuration error.
|
184 |
|
|
**************************************************************************/
|
185 |
|
|
int neorv32_xip_spi_trans(uint8_t nbytes, uint64_t *rtx_data) {
|
186 |
|
|
|
187 |
|
|
if ((nbytes == 0) || (nbytes > 8)) {
|
188 |
|
|
return 1;
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
// configure number of bytes to transfer
|
192 |
|
|
uint32_t ctrl = NEORV32_XIP.CTRL;
|
193 |
|
|
ctrl &= ~(0xF << XIP_CTRL_SPI_NBYTES_LSB); // clear old configuration
|
194 |
|
|
ctrl |= nbytes << XIP_CTRL_SPI_NBYTES_LSB; // set new configuration
|
195 |
|
|
NEORV32_XIP.CTRL = ctrl;
|
196 |
|
|
|
197 |
|
|
union {
|
198 |
|
|
uint64_t uint64;
|
199 |
71 |
zero_gravi |
uint32_t uint32[sizeof(uint64_t)/sizeof(uint32_t)];
|
200 |
70 |
zero_gravi |
} data;
|
201 |
|
|
|
202 |
|
|
data.uint64 = *rtx_data;
|
203 |
|
|
NEORV32_XIP.DATA_LO = data.uint32[0];
|
204 |
|
|
NEORV32_XIP.DATA_HI = data.uint32[1]; // trigger SPI transfer
|
205 |
|
|
|
206 |
|
|
// wait for transfer to complete
|
207 |
|
|
while(NEORV32_XIP.CTRL & (1 << XIP_CTRL_PHY_BUSY));
|
208 |
|
|
|
209 |
|
|
data.uint32[0] = NEORV32_XIP.DATA_LO;
|
210 |
|
|
data.uint32[1] = 0; // RX data is always 32-bit
|
211 |
|
|
*rtx_data = data.uint64;
|
212 |
|
|
|
213 |
|
|
return 0;
|
214 |
|
|
}
|
215 |
|
|
|