Skip to content

Commit f5cfdfa

Browse files
author
Cruz Monrreal
authored
Merge pull request #8072 from TomoYamanaka/improve_flashiap
Renesas : Improve Flash iap driver
2 parents 2b3f5bd + cb087ed commit f5cfdfa

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

targets/TARGET_RENESAS/TARGET_RZ_A1XX/flash_api.c

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@
1818
#include "mbed_critical.h"
1919

2020
#if DEVICE_FLASH
21+
#include <string.h>
2122
#include "iodefine.h"
2223
#include "spibsc_iobitmask.h"
2324
#include "spibsc.h"
2425
#include "mbed_drv_cfg.h"
2526

2627
/* ---- serial flash command ---- */
28+
#if (FLASH_SIZE > 0x1000000)
29+
#define SPIBSC_OUTPUT_ADDR SPIBSC_OUTPUT_ADDR_32
30+
#define SFLASHCMD_SECTOR_ERASE (0x21u) /* SE4B 4-byte address(1bit) */
31+
#define SFLASHCMD_PAGE_PROGRAM (0x12u) /* PP4B 4-byte address(1bit), data(1bit) */
32+
#else
33+
#define SPIBSC_OUTPUT_ADDR SPIBSC_OUTPUT_ADDR_24
2734
#define SFLASHCMD_SECTOR_ERASE (0x20u) /* SE 3-byte address(1bit) */
2835
#define SFLASHCMD_PAGE_PROGRAM (0x02u) /* PP 3-byte address(1bit), data(1bit) */
36+
#endif
2937
#define SFLASHCMD_READ_STATUS_REG (0x05u) /* RDSR data(1bit) */
3038
#define SFLASHCMD_WRITE_ENABLE (0x06u) /* WREN */
3139
/* ---- serial flash register definitions ---- */
@@ -74,10 +82,6 @@ typedef struct {
7482
uint32_t smwdr[2]; /* write data */
7583
} st_spibsc_spimd_reg_t;
7684

77-
/* SPI Multi-I/O bus address space address definitions */
78-
#define SPIBSC_ADDR_START (0x18000000uL)
79-
#define SPIBSC_ADDR_END (0x1BFFFFFFuL)
80-
8185
typedef struct {
8286
uint32_t b0 : 1 ; /* bit 0 : - (0) */
8387
uint32_t b1 : 1 ; /* bit 1 : - (1) */
@@ -96,9 +100,10 @@ typedef struct {
96100
uint32_t base_addr : 12; /* bit 31-20 : PA[31:20] PA(physical address) bits:bit31-20 */
97101
} mmu_ttbl_desc_section_t;
98102

99-
static mmu_ttbl_desc_section_t desc_tbl[(SPIBSC_ADDR_END >> 20) - (SPIBSC_ADDR_START >> 20) + 1];
103+
static mmu_ttbl_desc_section_t desc_tbl[(FLASH_SIZE >> 20)];
100104
static volatile struct st_spibsc* SPIBSC = &SPIBSC0;
101105
static st_spibsc_spimd_reg_t spimd_reg;
106+
static uint8_t write_tmp_buf[FLASH_PAGE_SIZE];
102107

103108
#if defined(__ICCARM__)
104109
#define RAM_CODE_SEC __ramfunc
@@ -136,24 +141,12 @@ int32_t flash_free(flash_t *obj)
136141

137142
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
138143
{
139-
int32_t ret;
140-
141-
core_util_critical_section_enter();
142-
ret = _sector_erase(address - FLASH_BASE);
143-
core_util_critical_section_exit();
144-
145-
return ret;
144+
return _sector_erase(address - FLASH_BASE);
146145
}
147146

148147
int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size)
149148
{
150-
int32_t ret;
151-
152-
core_util_critical_section_enter();
153-
ret = _page_program(address - FLASH_BASE, data, size);
154-
core_util_critical_section_exit();
155-
156-
return ret;
149+
return _page_program(address - FLASH_BASE, data, size);
157150
}
158151

159152
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
@@ -167,7 +160,7 @@ uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
167160

168161
uint32_t flash_get_page_size(const flash_t *obj)
169162
{
170-
return 1;
163+
return 8;
171164
}
172165

173166
uint32_t flash_get_start_address(const flash_t *obj)
@@ -184,12 +177,14 @@ int32_t _sector_erase(uint32_t addr)
184177
{
185178
int32_t ret;
186179

180+
core_util_critical_section_enter();
187181
spi_mode();
188182

189183
/* ---- Write enable ---- */
190184
ret = write_enable(); /* WREN Command */
191185
if (ret != 0) {
192186
ex_mode();
187+
core_util_critical_section_exit();
193188
return ret;
194189
}
195190

@@ -202,20 +197,22 @@ int32_t _sector_erase(uint32_t addr)
202197
spimd_reg.cmd = SFLASHCMD_SECTOR_ERASE;
203198

204199
/* ---- address ---- */
205-
spimd_reg.ade = SPIBSC_OUTPUT_ADDR_24;
200+
spimd_reg.ade = SPIBSC_OUTPUT_ADDR;
206201
spimd_reg.addre = SPIBSC_SDR_TRANS; /* SDR */
207202
spimd_reg.adb = SPIBSC_1BIT;
208203
spimd_reg.addr = addr;
209204

210205
ret = spibsc_transfer(&spimd_reg);
211206
if (ret != 0) {
212207
ex_mode();
208+
core_util_critical_section_exit();
213209
return ret;
214210
}
215211

216212
ret = busy_wait();
217213

218214
ex_mode();
215+
core_util_critical_section_exit();
219216
return ret;
220217
}
221218

@@ -226,8 +223,6 @@ int32_t _page_program(uint32_t addr, const uint8_t * buf, int32_t size)
226223
int32_t remainder;
227224
int32_t idx = 0;
228225

229-
spi_mode();
230-
231226
while (size > 0) {
232227
if (size > FLASH_PAGE_SIZE) {
233228
program_size = FLASH_PAGE_SIZE;
@@ -239,10 +234,15 @@ int32_t _page_program(uint32_t addr, const uint8_t * buf, int32_t size)
239234
program_size = remainder;
240235
}
241236

237+
core_util_critical_section_enter();
238+
memcpy(write_tmp_buf, &buf[idx], program_size);
239+
spi_mode();
240+
242241
/* ---- Write enable ---- */
243242
ret = write_enable(); /* WREN Command */
244243
if (ret != 0) {
245244
ex_mode();
245+
core_util_critical_section_exit();
246246
return ret;
247247
}
248248

@@ -256,7 +256,7 @@ int32_t _page_program(uint32_t addr, const uint8_t * buf, int32_t size)
256256
spimd_reg.cmd = SFLASHCMD_PAGE_PROGRAM;
257257

258258
/* ---- address ---- */
259-
spimd_reg.ade = SPIBSC_OUTPUT_ADDR_24;
259+
spimd_reg.ade = SPIBSC_OUTPUT_ADDR;
260260
spimd_reg.addre = SPIBSC_SDR_TRANS; /* SDR */
261261
spimd_reg.adb = SPIBSC_1BIT;
262262
spimd_reg.addr = addr;
@@ -267,28 +267,33 @@ int32_t _page_program(uint32_t addr, const uint8_t * buf, int32_t size)
267267
ret = spibsc_transfer(&spimd_reg); /* Command,Address */
268268
if (ret != 0) {
269269
ex_mode();
270+
core_util_critical_section_exit();
270271
return ret;
271272
}
272273

273274
/* ----------- 2. Data ---------------*/
274-
ret = data_send(SPIBSC_1BIT, SPIBSC_SPISSL_NEGATE, &buf[idx], program_size);
275+
ret = data_send(SPIBSC_1BIT, SPIBSC_SPISSL_NEGATE, write_tmp_buf, program_size);
275276
if (ret != 0) {
276277
ex_mode();
278+
core_util_critical_section_exit();
277279
return ret;
278280
}
279281

280282
ret = busy_wait();
281283
if (ret != 0) {
282284
ex_mode();
285+
core_util_critical_section_exit();
283286
return ret;
284287
}
285288

289+
ex_mode();
290+
core_util_critical_section_exit();
291+
286292
addr += program_size;
287293
idx += program_size;
288294
size -= program_size;
289295
}
290296

291-
ex_mode();
292297
return ret;
293298
}
294299

@@ -686,16 +691,16 @@ static void change_mmu_ttbl_spibsc(uint32_t type)
686691
mmu_ttbl_desc_section_t * table = (mmu_ttbl_desc_section_t *)TTB;
687692

688693
/* ==== Modify SPI Multi-I/O bus space settings in the MMU translation table ==== */
689-
for (index = (SPIBSC_ADDR_START >> 20); index <= (SPIBSC_ADDR_END >> 20); index++) {
694+
for (index = (FLASH_BASE >> 20); index < ((FLASH_BASE + FLASH_SIZE) >> 20); index++) {
690695
/* Modify memory attribute descriptor */
691696
if (type == 0) { /* Spi */
692697
desc = table[index];
693-
desc_tbl[index - (SPIBSC_ADDR_START >> 20)] = desc;
698+
desc_tbl[index - (FLASH_BASE >> 20)] = desc;
694699
desc.AP1_0 = 0x0u; /* AP[2:0] = b'000 (No access) */
695700
desc.AP2 = 0x0u;
696701
desc.XN = 0x1u; /* XN = 1 (Execute never) */
697702
} else { /* Xip */
698-
desc = desc_tbl[index - (SPIBSC_ADDR_START >> 20)];
703+
desc = desc_tbl[index - (FLASH_BASE >> 20)];
699704
}
700705
/* Write descriptor back to translation table */
701706
table[index] = desc;

0 commit comments

Comments
 (0)