Debian Codegraph @

ports/renesas-ra/ra/ra_flash.c

raw
1 /*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2021 Renesas Electronics Corporation
5 * Copyright (c) 2023 Vekatech Ltd.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26 #include <stdbool.h>
27 #include "py/mpconfig.h"
28 #include "hal_data.h"
29 #include "ra_config.h"
30 #include "ra_utils.h"
31 #include "ra_flash.h"
32
33 #if !defined(USE_FSP_FLASH)
34 #define USE_FSP_FLASH
35 #endif
36
37 #if defined(USE_FSP_FLASH)
38
39 #if defined(__GNUC__)
40 #pragma GCC diagnostic ignored "-Wunused-parameter"
41 #pragma GCC diagnostic ignored "-Wmissing-declarations"
42 #endif
43
44 /* Flags, set from Callback function */
45 static volatile _Bool g_b_flash_event_not_blank = false;
46 static volatile _Bool g_b_flash_event_blank = false;
47 static volatile _Bool g_b_flash_event_erase_complete = false;
48 static volatile _Bool g_b_flash_event_write_complete = false;
49
50 static uint8_t flash_buf[FLASH_BUF_SIZE] __attribute__((aligned(2)));
51
52 #ifdef USE_FSP_QSPI
53 #if MICROPY_HW_HAS_QSPI_FLASH
54 static bool r_qspi_get_QE(void) {
55 /* Enter direct communication mode */
56 R_QSPI->SFMCMD = 1U;
57
58 R_QSPI->SFMCOM = 0x35;
59 bool sts = (R_QSPI->SFMCOM >> 1) & 1;
60
61 /* Close the SPI bus cycle. Reference section 39.10.3 "Generating the SPI Bus Cycle during Direct Communication"
62 * in the RA6M3 manual R01UH0886EJ0100. */
63 R_QSPI->SFMCMD = 1U;
64
65 /* Exit direct communication mode */
66 R_QSPI->SFMCMD = 0U;
67
68 return sts;
69 }
70
71 static void r_qspi_set_QE(qspi_instance_ctrl_t *p_instance_ctrl) {
72 /* Enter direct communication mode */
73 R_QSPI->SFMCMD = 1U;
74
75 /* Enable write. */
76 R_QSPI->SFMCOM = p_instance_ctrl->p_cfg->write_enable_command;
77
78 /* Close the SPI bus cycle. Reference section 39.10.3 "Generating the SPI Bus Cycle during Direct Communication"
79 * in the RA6M3 manual R01UH0886EJ0100. */
80 R_QSPI->SFMCMD = 1U;
81
82 R_QSPI->SFMCOM = 0x31;
83 R_QSPI->SFMCOM = 0x02;
84
85 /* Close the SPI bus cycle. Reference section 39.10.3 "Generating the SPI Bus Cycle during Direct Communication"
86 * in the RA6M3 manual R01UH0886EJ0100. */
87 R_QSPI->SFMCMD = 1U;
88
89 /* Exit direct communication mode */
90 R_QSPI->SFMCMD = 0U;
91 }
92
93 static void r_qspi_wait_WIP(qspi_instance_ctrl_t *p_instance_ctrl) {
94 /* Enter direct communication mode */
95 R_QSPI->SFMCMD = 1U;
96
97 R_QSPI->SFMCOM = p_instance_ctrl->p_cfg->status_command;
98 while ((R_QSPI->SFMCOM >> p_instance_ctrl->p_cfg->write_status_bit) & 1) {
99 ;
100 }
101
102 /* Close the SPI bus cycle. Reference section 39.10.3 "Generating the SPI Bus Cycle during Direct Communication"
103 * in the RA6M3 manual R01UH0886EJ0100. */
104 R_QSPI->SFMCMD = 1U;
105
106 /* Exit direct communication mode */
107 R_QSPI->SFMCMD = 0U;
108 }
109
110 static fsp_err_t R_QSPI_QuadEnable(spi_flash_ctrl_t *p_ctrl) {
111 qspi_instance_ctrl_t *p_instance_ctrl = (qspi_instance_ctrl_t *)p_ctrl;
112
113 #if QSPI_CFG_PARAM_CHECKING_ENABLE
114 FSP_ASSERT(NULL != p_instance_ctrl);
115 FSP_ASSERT(NULL != p_status);
116 FSP_ERROR_RETURN(QSPI_PRV_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN);
117
118 /* Do not enter direct communication mode from XIP mode. Reference note in section 39.10.2 "Using Direct
119 * Communication Mode" in the RA6M3 manual R01UH0886EJ0100. */
120 FSP_ERROR_RETURN(0U == R_QSPI->SFMSDC_b.SFMXST, FSP_ERR_INVALID_MODE);
121 #endif
122
123 /* Read device status. */
124 if (!r_qspi_get_QE()) {
125 r_qspi_set_QE(p_instance_ctrl);
126 }
127
128 return FSP_SUCCESS;
129 }
130
131 static fsp_err_t R_QSPI_Wait_WIP(spi_flash_ctrl_t *p_ctrl) {
132 qspi_instance_ctrl_t *p_instance_ctrl = (qspi_instance_ctrl_t *)p_ctrl;
133
134 #if QSPI_CFG_PARAM_CHECKING_ENABLE
135 FSP_ASSERT(NULL != p_instance_ctrl);
136 FSP_ASSERT(NULL != p_status);
137 FSP_ERROR_RETURN(QSPI_PRV_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN);
138
139 /* Do not enter direct communication mode from XIP mode. Reference note in section 39.10.2 "Using Direct
140 * Communication Mode" in the RA6M3 manual R01UH0886EJ0100. */
141 FSP_ERROR_RETURN(0U == R_QSPI->SFMSDC_b.SFMXST, FSP_ERR_INVALID_MODE);
142 #endif
143
144 /* Wait WIP flag to go 0 */
145 r_qspi_wait_WIP(p_instance_ctrl);
146
147 return FSP_SUCCESS;
148 }
149
150 static bool lmemprob(void *dst, size_t len) {
151 uint8_t *p;
152 for (p = (uint8_t *)dst; ((len > 0) && (p[0] == 0xFF)); len--) {
153 p++;
154 }
155
156 return len? false : true;
157 }
158 #endif
159 #endif
160
161 void *FLASH_SECTION lmemset(void *dst, int c, size_t len) {
162 char *p;
163 for (p = (char *)dst; len > 0; len--) {
164 *(p++) = (char)c;
165 }
166 return (void *)dst;
167 }
168
169 void *FLASH_SECTION lmemcpy(void *dst, const void *src, size_t len) {
170 char *d = (char *)dst;
171 const char *s = (const char *)src;
172 for (; len > 0; len--) {
173 *(d++) = *(s++);
174 }
175 return (void *)dst;
176 }
177
178 int FLASH_SECTION lmemcmp(const void *p1, const void *p2, size_t len) {
179 unsigned char *a, *b;
180 size_t i;
181
182 a = (unsigned char *)p1;
183 b = (unsigned char *)p2;
184 for (i = 0; i < len; i++) {
185 if (*a != *b) {
186 return (*a < *b) ? -1 : 1;
187 }
188 a++;
189 b++;
190 }
191 return (int)0;
192 }
193
194 #if defined(RA4M1) | defined(RA4M3) | defined(RA4W1) | MICROPY_HW_HAS_QSPI_FLASH
195
196 uint32_t FLASH_SECTION sector_size(uint32_t addr) {
197 return FLASH_SECTOR_SIZE;
198 }
199
200 uint32_t FLASH_SECTION sector_start(uint32_t addr) {
201 return addr & ~(FLASH_SECTOR_SIZE - 1);
202 }
203
204 uint32_t FLASH_SECTION sector_index(uint32_t addr) {
205 return (addr - 0x00000000) / FLASH_SECTOR_SIZE;
206 }
207
208 #elif defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
209
210 #define REGION1_SECTOR_SIZE 0x8000 // 32K
211 #define REGION1_SECTOR_MAX 14
212 #define REGION0_SECTOR_SIZE 0x2000 // 8K
213 #define REGION0_SECTOR_MAX 8
214
215 uint32_t FLASH_SECTION sector_size(uint32_t addr) {
216 if (addr < 0x00010000) {
217 return REGION0_SECTOR_SIZE;
218 } else {
219 return REGION1_SECTOR_SIZE;
220 }
221 }
222
223 uint32_t FLASH_SECTION sector_start(uint32_t addr) {
224 if (addr < 0x00010000) {
225 return addr & ~(REGION0_SECTOR_SIZE - 1);
226 } else {
227 return addr & ~(REGION1_SECTOR_SIZE - 1);
228 }
229 }
230
231 uint32_t FLASH_SECTION sector_index(uint32_t addr) {
232 if (addr < 0x00010000) {
233 return (addr - 0x00000000) / REGION0_SECTOR_SIZE;
234 } else {
235 return ((addr - 0x00010000) / REGION1_SECTOR_SIZE) + REGION0_SECTOR_SIZE;
236 }
237 }
238
239 #else
240 #error "CMSIS MCU Series is not specified."
241 #endif
242
243 bool internal_flash_read(uint8_t *addr, uint32_t NumBytes, uint8_t *pSectorBuff) {
244 CHIP_WORD *startaddr = (CHIP_WORD *)addr;
245 CHIP_WORD *endaddr = (CHIP_WORD *)(addr + NumBytes);
246 while (startaddr < endaddr) {
247 *pSectorBuff++ = *startaddr++;
248 }
249 return true;
250 }
251
252 bool internal_flash_write(uint8_t *addr, uint32_t NumBytes, uint8_t *pSectorBuff, bool ReadModifyWrite) {
253 return internal_flash_writex(addr, NumBytes, pSectorBuff, ReadModifyWrite, true);
254 }
255
256 bool internal_flash_writex(uint8_t *addr, uint32_t NumBytes, uint8_t *pSectorBuff, bool ReadModifyWrite, bool fIncrementDataPtr) {
257 fsp_err_t err = FSP_SUCCESS;
258 bool flag;
259 uint32_t count;
260 uint8_t *buf_addr = (uint8_t *)&flash_buf[0];
261 uint32_t startaddr = (uint32_t)addr & FLASH_BUF_ADDR_MASK;
262 uint32_t offset = (uint32_t)addr & FLASH_BUF_OFF_MASK;
263 uint32_t endaddr = (uint32_t)addr + NumBytes;
264 uint32_t error_code = 0;
265 while (startaddr < endaddr) {
266 // copy from dst rom addr to flash buffer to keep current data
267 lmemcpy(flash_buf, (void *)startaddr, FLASH_BUF_SIZE);
268 // memset(flash_buf, 0xff, FLASH_BUF_SIZE);
269 if (NumBytes + offset > FLASH_BUF_SIZE) {
270 count = FLASH_BUF_SIZE - offset;
271 NumBytes -= count;
272 } else {
273 count = NumBytes;
274 }
275 // overwrite data from src addr to flash buffer
276 if (fIncrementDataPtr) {
277 lmemcpy(flash_buf + offset, pSectorBuff, count);
278 } else {
279 lmemset(flash_buf + offset, (int)*pSectorBuff, count);
280 }
281 g_b_flash_event_write_complete = false;
282 uint8_t *flash_addr = (uint8_t *)((uint32_t)startaddr & FLASH_BUF_ADDR_MASK);
283 #if MICROPY_HW_HAS_QSPI_FLASH
284 for (uint16_t idx = 0; ((err == FSP_SUCCESS) && (idx < FLASH_SECTOR_SIZE)); idx += FLASH_PAGE_SIZE)
285 {
286 err = R_QSPI_Write(&g_qspi0_ctrl, &buf_addr[idx], &flash_addr[idx], FLASH_PAGE_SIZE);
287 err = R_QSPI_Wait_WIP(&g_qspi0_ctrl);
288 }
289 #else
290 uint32_t state = ra_disable_irq();
291 #if defined(RA4M1) | defined(RA4M3) | defined(RA4W1)
292 err = R_FLASH_LP_Write(&g_flash0_ctrl, (uint32_t const)buf_addr, (uint32_t)flash_addr, FLASH_SECTOR_SIZE);
293 #elif defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
294 err = R_FLASH_HP_Write(&g_flash0_ctrl, (uint32_t const)buf_addr, (uint32_t)flash_addr, FLASH_SECTOR_SIZE);
295 #else
296 #error "CMSIS MCU Series is not specified."
297 #endif
298 ra_enable_irq(state);
299 #endif
300 if (FSP_SUCCESS != err) {
301 error_code = 1;
302 goto WriteX_exit;
303 }
304 if (fIncrementDataPtr) {
305 flag = (lmemcmp((void *)(startaddr + offset), flash_buf + offset, count) == 0);
306 if (!flag) {
307 error_code = 2;
308 break;
309 }
310 }
311 offset = 0;
312 startaddr += FLASH_BUF_SIZE;
313 pSectorBuff += count;
314 }
315 WriteX_exit:
316 if (error_code == 0) {
317 return true;
318 }
319 return false;
320 }
321
322 bool internal_flash_memset(uint8_t *addr, uint8_t Data, uint32_t NumBytes) {
323 return true;
324 }
325
326 bool internal_flash_isblockerased(uint8_t *addr, uint32_t BlockLength) {
327 g_b_flash_event_not_blank = false;
328 g_b_flash_event_blank = false;
329 #if MICROPY_HW_HAS_QSPI_FLASH
330 return lmemprob((uint8_t *)((uint32_t)addr & FLASH_BUF_ADDR_MASK), FLASH_SECTOR_SIZE);
331 #else
332 fsp_err_t err = FSP_SUCCESS;
333 flash_result_t blankCheck = FLASH_RESULT_BLANK;
334 uint32_t state = ra_disable_irq();
335 #if defined(RA4M1) | defined(RA4M3) | defined(RA4W1)
336 err = R_FLASH_LP_BlankCheck(&g_flash0_ctrl, (uint32_t const)((uint32_t)addr & FLASH_BUF_ADDR_MASK), FLASH_SECTOR_SIZE, &blankCheck);
337 #elif defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
338 err = R_FLASH_HP_BlankCheck(&g_flash0_ctrl, (uint32_t const)((uint32_t)addr & FLASH_BUF_ADDR_MASK), FLASH_SECTOR_SIZE, &blankCheck);
339 #else
340 #error "CMSIS MCU Series is not specified."
341 #endif
342 ra_enable_irq(state);
343 if (err == FSP_SUCCESS) {
344 if (FLASH_RESULT_BLANK == blankCheck) {
345 return true;
346 } else {
347 return false;
348 }
349 } else {
350 return false;
351 }
352 #endif
353 }
354
355 bool internal_flash_eraseblock(uint8_t *addr) {
356 uint32_t error_code = 0;
357 fsp_err_t err = FSP_SUCCESS;
358 g_b_flash_event_erase_complete = false;
359 #if MICROPY_HW_HAS_QSPI_FLASH
360 if (!lmemprob((uint8_t *)((uint32_t)addr & FLASH_BUF_ADDR_MASK), FLASH_SECTOR_SIZE)) {
361 err = R_QSPI_Erase(&g_qspi0_ctrl, (uint8_t *const)addr, FLASH_SECTOR_SIZE);
362 err = R_QSPI_Wait_WIP(&g_qspi0_ctrl);
363 }
364 #else
365 uint32_t state = ra_disable_irq();
366 #if defined(RA4M1) | defined(RA4M3) | defined(RA4W1)
367 err = R_FLASH_LP_Erase(&g_flash0_ctrl, (uint32_t const)addr, 1);
368 #elif defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
369 err = R_FLASH_HP_Erase(&g_flash0_ctrl, (uint32_t const)addr, 1);
370 #else
371 #error "CMSIS MCU Series is not specified."
372 #endif
373 ra_enable_irq(state);
374 #endif
375 if (err == FSP_SUCCESS) {
376 error_code = 0;
377 } else {
378 error_code = 1;
379 }
380 if (error_code == 0) {
381 return true;
382 }
383 return false;
384 }
385
386 /* Callback function */
387 void callback_flash(flash_callback_args_t *p_args) {
388 /* TODO: add your own code here */
389 switch (p_args->event) {
390 case FLASH_EVENT_NOT_BLANK:
391 g_b_flash_event_not_blank = true;
392 break;
393 case FLASH_EVENT_BLANK:
394 g_b_flash_event_blank = true;
395 break;
396 case FLASH_EVENT_ERASE_COMPLETE:
397 g_b_flash_event_erase_complete = true;
398 break;
399 case FLASH_EVENT_WRITE_COMPLETE:
400 g_b_flash_event_write_complete = true;
401 break;
402 default:
403 /* Do nothing */
404 break;
405 }
406 }
407
408 bool internal_flash_init(void) {
409 fsp_err_t err = FSP_SUCCESS;
410 #if MICROPY_HW_HAS_QSPI_FLASH
411 err = R_QSPI_Open(&g_qspi0_ctrl, &g_qspi0_cfg);
412 if (err == FSP_SUCCESS) {
413 R_QSPI_QuadEnable(&g_qspi0_ctrl);
414 return true;
415 } else {
416 return false;
417 }
418 #else
419 #if defined(RA4M1) | defined(RA4M3) | defined(RA4W1)
420 err = R_FLASH_LP_Open(&g_flash0_ctrl, &g_flash0_cfg);
421 #elif defined(RA6M1) | defined(RA6M2) | defined(RA6M3) | defined(RA6M5)
422 err = R_FLASH_HP_Open(&g_flash0_ctrl, &g_flash0_cfg);
423 #else
424 #error "CMSIS MCU Series is not specified."
425 #endif
426 if (err == FSP_SUCCESS) {
427 return true;
428 } else {
429 return false;
430 }
431 #endif
432 }
433
434 #else
435
436 // ToDo: implementation
437
438 #endif