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 |
|
|
* @author Stephan Nolting
|
39 |
|
|
* @brief Execute in place module (XIP) HW driver source file.
|
40 |
|
|
*
|
41 |
|
|
* @note These functions should only be used if the XIP module was synthesized (IO_XIP_EN = true).
|
42 |
|
|
**************************************************************************/
|
43 |
|
|
|
44 |
|
|
#include "neorv32.h"
|
45 |
|
|
#include "neorv32_xip.h"
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
/**********************************************************************//**
|
49 |
|
|
* Check if XIP module was synthesized.
|
50 |
|
|
*
|
51 |
|
|
* @return 0 if XIP was not synthesized, 1 if XIP is available.
|
52 |
|
|
**************************************************************************/
|
53 |
|
|
int neorv32_xip_available(void) {
|
54 |
|
|
|
55 |
|
|
if (NEORV32_SYSINFO.SOC & (1 << SYSINFO_SOC_IO_XIP)) {
|
56 |
|
|
return 1;
|
57 |
|
|
}
|
58 |
|
|
else {
|
59 |
|
|
return 0;
|
60 |
|
|
}
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
/**********************************************************************//**
|
65 |
|
|
* Configure XIP module: configure SPI properties.
|
66 |
|
|
*
|
67 |
|
|
* @warning This will reset the XIP module overriding the CTRL register.
|
68 |
|
|
* @note This function will also send 64 dummy clocks via the SPI port (with chip-select disabled).
|
69 |
|
|
*
|
70 |
|
|
* @param[in] prsc SPI clock prescaler select (0..7).
|
71 |
|
|
* @param[in] cpol SPI clock polarity (0/1).
|
72 |
|
|
* @param[in] cpha SPI clock phase(0/1).
|
73 |
|
|
* @param[in] rd_cmd SPI flash read command.
|
74 |
|
|
* @return 0 if configuration is OK, 1 if configuration error.
|
75 |
|
|
**************************************************************************/
|
76 |
|
|
int neorv32_xip_init(uint8_t prsc, uint8_t cpol, uint8_t cpha, uint8_t rd_cmd) {
|
77 |
|
|
|
78 |
|
|
// configuration check
|
79 |
|
|
if ((prsc > 7) || (cpol > 1) || (cpha > 1)) {
|
80 |
|
|
return 1;
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
// reset module
|
84 |
|
|
NEORV32_XIP.CTRL = 0;
|
85 |
|
|
|
86 |
|
|
uint32_t ctrl = 0;
|
87 |
|
|
|
88 |
|
|
ctrl |= ((uint32_t)(1 )) << XIP_CTRL_EN; // enable module
|
89 |
|
|
ctrl |= ((uint32_t)(prsc & 0x07)) << XIP_CTRL_PRSC0;
|
90 |
|
|
ctrl |= ((uint32_t)(cpol & 0x01)) << XIP_CTRL_CPOL;
|
91 |
|
|
ctrl |= ((uint32_t)(cpha & 0x01)) << XIP_CTRL_CPHA;
|
92 |
|
|
ctrl |= ((uint32_t)(8 )) << XIP_CTRL_SPI_NBYTES_LSB; // set 8 bytes transfer size as default
|
93 |
|
|
ctrl |= ((uint32_t)(rd_cmd & 0xff)) << XIP_CTRL_RD_CMD_LSB;
|
94 |
|
|
|
95 |
|
|
NEORV32_XIP.CTRL = ctrl;
|
96 |
|
|
|
97 |
|
|
// send 64 dummy clocks
|
98 |
|
|
NEORV32_XIP.DATA_LO = 0;
|
99 |
|
|
NEORV32_XIP.DATA_HI = 0; // trigger SPI transfer
|
100 |
|
|
|
101 |
|
|
// wait for transfer to complete
|
102 |
|
|
while(NEORV32_XIP.CTRL & (1 << XIP_CTRL_PHY_BUSY));
|
103 |
|
|
|
104 |
|
|
NEORV32_XIP.CTRL |= 1 << XIP_CTRL_SPI_CSEN; // finally enable SPI chip-select
|
105 |
|
|
|
106 |
|
|
return 0;
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
/**********************************************************************//**
|
111 |
|
|
* Enable XIP mode (to allow CPU to _transparently_ fetch instructions).
|
112 |
|
|
*
|
113 |
|
|
* @warning This function is blocking until the XIP mode is ready.
|
114 |
|
|
*
|
115 |
|
|
* @param[in] abytes Number of address bytes used to access the SPI flash (1,2,3,4).
|
116 |
|
|
* @param[in] page_base XIP memory page base address (top 4 address bits, 0..15).
|
117 |
|
|
* @return 0 if XIP configuration is OK, 1 if configuration error.
|
118 |
|
|
**************************************************************************/
|
119 |
|
|
int neorv32_xip_start(uint8_t abytes, uint32_t page_base) {
|
120 |
|
|
|
121 |
|
|
if ((abytes < 1) || (abytes > 4)) {
|
122 |
|
|
return 1;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
if (page_base & 0x0FFFFFFF) {
|
126 |
|
|
return 1;
|
127 |
|
|
}
|
128 |
|
|
page_base >>= 28;
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
uint32_t ctrl = NEORV32_XIP.CTRL;
|
132 |
|
|
|
133 |
|
|
// address bytes send to SPI flash
|
134 |
|
|
ctrl &= ~(3 << XIP_CTRL_XIP_ABYTES_LSB); // clear old configuration
|
135 |
|
|
ctrl |= ((uint32_t)(abytes-1)) << XIP_CTRL_XIP_ABYTES_LSB; // set new configuration
|
136 |
|
|
|
137 |
|
|
// total number of bytes to transfer via SPI
|
138 |
|
|
// 'abytes' address bytes + 1 command byte + 4 bytes RX data (one 32-bit word)
|
139 |
|
|
ctrl &= ~(0xF << XIP_CTRL_SPI_NBYTES_LSB); // clear old configuration
|
140 |
|
|
ctrl |= ((uint32_t)(abytes+1+4)) << XIP_CTRL_SPI_NBYTES_LSB; // set new configuration
|
141 |
|
|
|
142 |
|
|
// XIP memory page
|
143 |
|
|
ctrl &= ~(0xF << XIP_CTRL_PAGE_LSB); // clear old configuration
|
144 |
|
|
ctrl |= ((uint32_t)(page_base & 0xf)) << XIP_CTRL_PAGE_LSB; // set new configuration
|
145 |
|
|
|
146 |
|
|
ctrl |= 1 << XIP_CTRL_XIP_EN; // enable XIP mode
|
147 |
|
|
|
148 |
|
|
NEORV32_XIP.CTRL = ctrl;
|
149 |
|
|
|
150 |
|
|
return 0;
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
/**********************************************************************//**
|
155 |
|
|
* Enable high-speed SPI mode (running at half of the processor clock).
|
156 |
|
|
*
|
157 |
|
|
* @note High-speed SPI mode ignores the programmed clock prescaler configuration.
|
158 |
|
|
**************************************************************************/
|
159 |
|
|
void neorv32_xip_highspeed_enable(void) {
|
160 |
|
|
|
161 |
|
|
NEORV32_XIP.CTRL |= 1 << XIP_CTRL_HIGHSPEED;
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
/**********************************************************************//**
|
166 |
|
|
* Disable high-speed SPI mode.
|
167 |
|
|
*
|
168 |
|
|
* @note High-speed SPI mode ignores the programmed clock prescaler configuration.
|
169 |
|
|
**************************************************************************/
|
170 |
|
|
void neorv32_xip_highspeed_disable(void) {
|
171 |
|
|
|
172 |
|
|
NEORV32_XIP.CTRL &= ~(1 << XIP_CTRL_HIGHSPEED);
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
/**********************************************************************//**
|
177 |
|
|
* Direct SPI access to the XIP flash.
|
178 |
|
|
*
|
179 |
|
|
* @warning This function can only be used BEFORE the XIP-mode is activated!
|
180 |
|
|
* @note This function is blocking.
|
181 |
|
|
*
|
182 |
|
|
* @param[in] nbytes Number of bytes to transfer (1..8).
|
183 |
|
|
* @param[in,out] rtx_data Pointer to 64-bit TX/RX data (MSB-aligned for sending, LSB-aligned for receiving (only 32-bit)).
|
184 |
|
|
* @return 0 if valid transfer, 1 if transfer configuration error.
|
185 |
|
|
**************************************************************************/
|
186 |
|
|
int neorv32_xip_spi_trans(uint8_t nbytes, uint64_t *rtx_data) {
|
187 |
|
|
|
188 |
|
|
if ((nbytes == 0) || (nbytes > 8)) {
|
189 |
|
|
return 1;
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
// configure number of bytes to transfer
|
193 |
|
|
uint32_t ctrl = NEORV32_XIP.CTRL;
|
194 |
|
|
ctrl &= ~(0xF << XIP_CTRL_SPI_NBYTES_LSB); // clear old configuration
|
195 |
|
|
ctrl |= nbytes << XIP_CTRL_SPI_NBYTES_LSB; // set new configuration
|
196 |
|
|
NEORV32_XIP.CTRL = ctrl;
|
197 |
|
|
|
198 |
|
|
union {
|
199 |
|
|
uint64_t uint64;
|
200 |
71 |
zero_gravi |
uint32_t uint32[sizeof(uint64_t)/sizeof(uint32_t)];
|
201 |
70 |
zero_gravi |
} data;
|
202 |
|
|
|
203 |
|
|
data.uint64 = *rtx_data;
|
204 |
|
|
NEORV32_XIP.DATA_LO = data.uint32[0];
|
205 |
|
|
NEORV32_XIP.DATA_HI = data.uint32[1]; // trigger SPI transfer
|
206 |
|
|
|
207 |
|
|
// wait for transfer to complete
|
208 |
|
|
while(NEORV32_XIP.CTRL & (1 << XIP_CTRL_PHY_BUSY));
|
209 |
|
|
|
210 |
|
|
data.uint32[0] = NEORV32_XIP.DATA_LO;
|
211 |
|
|
data.uint32[1] = 0; // RX data is always 32-bit
|
212 |
|
|
*rtx_data = data.uint64;
|
213 |
|
|
|
214 |
|
|
return 0;
|
215 |
|
|
}
|
216 |
|
|
|