27
27
/* ---- serial flash command ---- */
28
28
#if (FLASH_SIZE > 0x1000000 )
29
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) */
30
+ #if DEVICE_QSPIx2
31
+ #define SFLASHCMD_SECTOR_ERASE (0xDCu) /* SE4B 4-byte address(1bit) */
32
+ #define SFLASHCMD_PAGE_PROGRAM (0x34u) /* QPP4B 4-byte address(1bit), data(4bit) */
33
+ #else
34
+ #define SFLASHCMD_SECTOR_ERASE (0x21u) /* SE4B 4-byte address(1bit) */
35
+ #define SFLASHCMD_PAGE_PROGRAM (0x12u) /* PP4B 4-byte address(1bit), data(1bit) */
36
+ #endif
32
37
#else
33
38
#define SPIBSC_OUTPUT_ADDR SPIBSC_OUTPUT_ADDR_24
34
- #define SFLASHCMD_SECTOR_ERASE (0x20u) /* SE 3-byte address(1bit) */
35
- #define SFLASHCMD_PAGE_PROGRAM (0x02u) /* PP 3-byte address(1bit), data(1bit) */
39
+ #if DEVICE_QSPIx2
40
+ #define SFLASHCMD_SECTOR_ERASE (0xD8u) /* SE 3-byte address(1bit) */
41
+ #define SFLASHCMD_PAGE_PROGRAM (0x32u) /* PP 3-byte address(1bit), data(4bit) */
42
+ #else
43
+ #define SFLASHCMD_SECTOR_ERASE (0x20u) /* SE 3-byte address(1bit) */
44
+ #define SFLASHCMD_PAGE_PROGRAM (0x02u) /* PP 3-byte address(1bit), data(1bit) */
45
+ #endif
36
46
#endif
37
47
#define SFLASHCMD_READ_STATUS_REG (0x05u) /* RDSR data(1bit) */
38
48
#define SFLASHCMD_WRITE_ENABLE (0x06u) /* WREN */
39
49
/* ---- serial flash register definitions ---- */
50
+ #if DEVICE_QSPIx2
51
+ #define STREG_BUSY_BIT (0x0101u) /* SR.[0]BUSY Erase/Write In Progress (RO) */
52
+ #define SPIBSC_BUS_MODE SPIBSC_CMNCR_BSZ_DUAL
53
+ #define CHIP_SIZE (FLASH_SIZE + FLASH_SECTOR_SIZE)
54
+ #define CHIP_BASE (FLASH_BASE - FLASH_SECTOR_SIZE)
55
+ #else
40
56
#define STREG_BUSY_BIT (0x01u) /* SR.[0]BUSY Erase/Write In Progress (RO) */
57
+ #define SPIBSC_BUS_MODE SPIBSC_CMNCR_BSZ_SINGLE
58
+ #define CHIP_SIZE FLASH_SIZE
59
+ #define CHIP_BASE FLASH_BASE
60
+ #endif
41
61
42
62
/* Definition of the base address for the MMU translation table */
43
63
#if defined(__CC_ARM ) || defined(__ARMCC_VERSION ) || defined(__GNUC__ )
@@ -100,7 +120,7 @@ typedef struct {
100
120
uint32_t base_addr : 12 ; /* bit 31-20 : PA[31:20] PA(physical address) bits:bit31-20 */
101
121
} mmu_ttbl_desc_section_t ;
102
122
103
- static mmu_ttbl_desc_section_t desc_tbl [(FLASH_SIZE >> 20 )];
123
+ static mmu_ttbl_desc_section_t desc_tbl [(CHIP_SIZE >> 20 )];
104
124
static volatile struct st_spibsc * SPIBSC = & SPIBSC0 ;
105
125
static st_spibsc_spimd_reg_t spimd_reg ;
106
126
static uint8_t write_tmp_buf [FLASH_PAGE_SIZE ];
@@ -117,7 +137,7 @@ RAM_CODE_SEC int32_t _page_program(uint32_t addr, const uint8_t * buf, int32_t s
117
137
118
138
static RAM_CODE_SEC int32_t write_enable (void );
119
139
static RAM_CODE_SEC int32_t busy_wait (void );
120
- static RAM_CODE_SEC int32_t read_register (uint8_t cmd , uint8_t * status );
140
+ static RAM_CODE_SEC int32_t read_register (uint8_t cmd , uint16_t * status );
121
141
static RAM_CODE_SEC int32_t data_send (uint32_t bit_width , uint32_t spbssl_level , const uint8_t * buf , int32_t size );
122
142
static RAM_CODE_SEC void spi_mode (void );
123
143
static RAM_CODE_SEC void ex_mode (void );
@@ -141,12 +161,12 @@ int32_t flash_free(flash_t *obj)
141
161
142
162
int32_t flash_erase_sector (flash_t * obj , uint32_t address )
143
163
{
144
- return _sector_erase (address - FLASH_BASE );
164
+ return _sector_erase (address - CHIP_BASE );
145
165
}
146
166
147
167
int32_t flash_program_page (flash_t * obj , uint32_t address , const uint8_t * data , uint32_t size )
148
168
{
149
- return _page_program (address - FLASH_BASE , data , size );
169
+ return _page_program (address - CHIP_BASE , data , size );
150
170
}
151
171
152
172
uint32_t flash_get_sector_size (const flash_t * obj , uint32_t address )
@@ -160,7 +180,7 @@ uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
160
180
161
181
uint32_t flash_get_page_size (const flash_t * obj )
162
182
{
163
- return 8 ;
183
+ return ( SPIBSC_BUS_MODE == SPIBSC_CMNCR_BSZ_DUAL )? 32 : 8 ;
164
184
}
165
185
166
186
uint32_t flash_get_start_address (const flash_t * obj )
@@ -177,6 +197,11 @@ int32_t _sector_erase(uint32_t addr)
177
197
{
178
198
int32_t ret ;
179
199
200
+ #if DEVICE_QSPIx2
201
+ if (addr < FLASH_SECTOR_SIZE )
202
+ return -1 ; /* Do not mess around with the 1-st sector (Bootloader) */
203
+ #endif
204
+
180
205
core_util_critical_section_enter ();
181
206
spi_mode ();
182
207
@@ -200,7 +225,7 @@ int32_t _sector_erase(uint32_t addr)
200
225
spimd_reg .ade = SPIBSC_OUTPUT_ADDR ;
201
226
spimd_reg .addre = SPIBSC_SDR_TRANS ; /* SDR */
202
227
spimd_reg .adb = SPIBSC_1BIT ;
203
- spimd_reg .addr = addr ;
228
+ spimd_reg .addr = ( SPIBSC_BUS_MODE == SPIBSC_CMNCR_BSZ_DUAL )? addr >> 1 : addr ;
204
229
205
230
ret = spibsc_transfer (& spimd_reg );
206
231
if (ret != 0 ) {
@@ -223,6 +248,11 @@ int32_t _page_program(uint32_t addr, const uint8_t * buf, int32_t size)
223
248
int32_t remainder ;
224
249
int32_t idx = 0 ;
225
250
251
+ #if DEVICE_QSPIx2
252
+ if (addr < FLASH_SECTOR_SIZE )
253
+ return -1 ; /* Do not mess around with the 1-st sector (Bootloader) */
254
+ #endif
255
+
226
256
while (size > 0 ) {
227
257
if (size > FLASH_PAGE_SIZE ) {
228
258
program_size = FLASH_PAGE_SIZE ;
@@ -259,7 +289,7 @@ int32_t _page_program(uint32_t addr, const uint8_t * buf, int32_t size)
259
289
spimd_reg .ade = SPIBSC_OUTPUT_ADDR ;
260
290
spimd_reg .addre = SPIBSC_SDR_TRANS ; /* SDR */
261
291
spimd_reg .adb = SPIBSC_1BIT ;
262
- spimd_reg .addr = addr ;
292
+ spimd_reg .addr = ( SPIBSC_BUS_MODE == SPIBSC_CMNCR_BSZ_DUAL )? addr >> 1 : addr ;
263
293
264
294
/* ---- Others ---- */
265
295
spimd_reg .sslkp = SPIBSC_SPISSL_KEEP ; /* SPBSSL level */
@@ -272,7 +302,7 @@ int32_t _page_program(uint32_t addr, const uint8_t * buf, int32_t size)
272
302
}
273
303
274
304
/* ----------- 2. Data ---------------*/
275
- ret = data_send (SPIBSC_1BIT , SPIBSC_SPISSL_NEGATE , write_tmp_buf , program_size );
305
+ ret = data_send (( SPIBSC_BUS_MODE == SPIBSC_CMNCR_BSZ_DUAL )? SPIBSC_4BIT : SPIBSC_1BIT , SPIBSC_SPISSL_NEGATE , write_tmp_buf , program_size );
276
306
if (ret != 0 ) {
277
307
ex_mode ();
278
308
core_util_critical_section_exit ();
@@ -317,7 +347,7 @@ static int32_t write_enable(void)
317
347
static int32_t busy_wait (void )
318
348
{
319
349
int32_t ret ;
320
- uint8_t st_reg ;
350
+ uint16_t st_reg ;
321
351
322
352
while (1 ) {
323
353
ret = read_register (SFLASHCMD_READ_STATUS_REG , & st_reg );
@@ -332,7 +362,7 @@ static int32_t busy_wait(void)
332
362
return ret ;
333
363
}
334
364
335
- static int32_t read_register (uint8_t cmd , uint8_t * status )
365
+ static int32_t read_register (uint8_t cmd , uint16_t * status )
336
366
{
337
367
int32_t ret ;
338
368
@@ -358,7 +388,7 @@ static int32_t read_register(uint8_t cmd, uint8_t * status)
358
388
359
389
ret = spibsc_transfer (& spimd_reg );
360
390
if (ret == 0 ) {
361
- * status = (uint8_t )(spimd_reg .smrdr [0 ]); /* Data[7:0] */
391
+ * status = (SPIBSC_BUS_MODE == SPIBSC_CMNCR_BSZ_DUAL )? ( uint16_t )( spimd_reg . smrdr [ 0 ]) : ( uint8_t )(spimd_reg .smrdr [0 ]); /* Data[15:8] / Data[ 7:0] */
362
392
}
363
393
364
394
return ret ;
@@ -383,6 +413,19 @@ static int32_t data_send(uint32_t bit_width, uint32_t spbssl_level, const uint8_
383
413
spimd_reg .spidb = bit_width ;
384
414
spimd_reg .spidre = SPIBSC_SDR_TRANS ; /* SDR */
385
415
416
+ #if (SPIBSC_BUS_MODE == SPIBSC_CMNCR_BSZ_DUAL )
417
+ if (((uint32_t )size & 0x7 ) == 0 ) {
418
+ spimd_reg .spide = SPIBSC_OUTPUT_SPID_32 ; /* Enable(64bit) */
419
+ unit = 8 ;
420
+ } else if (((uint32_t )size & 0x3 ) == 0 ) {
421
+ spimd_reg .spide = SPIBSC_OUTPUT_SPID_32 ; /* Enable(32bit) */
422
+ unit = 4 ;
423
+ } else if (((uint32_t )size & 0x1 ) == 0 ) {
424
+ spimd_reg .spide = SPIBSC_OUTPUT_SPID_16 ; /* Enable(16bit) */
425
+ unit = 2 ;
426
+ } else
427
+ return -1 ;
428
+ #else
386
429
if (((uint32_t )size & 0x3 ) == 0 ) {
387
430
spimd_reg .spide = SPIBSC_OUTPUT_SPID_32 ; /* Enable(32bit) */
388
431
unit = 4 ;
@@ -393,6 +436,7 @@ static int32_t data_send(uint32_t bit_width, uint32_t spbssl_level, const uint8_
393
436
spimd_reg .spide = SPIBSC_OUTPUT_SPID_8 ; /* Enable(8bit) */
394
437
unit = 1 ;
395
438
}
439
+ #endif
396
440
397
441
while (size > 0 ) {
398
442
if (unit == 1 ) {
@@ -404,6 +448,11 @@ static int32_t data_send(uint32_t bit_width, uint32_t spbssl_level, const uint8_
404
448
} else if (unit == 4 ) {
405
449
buf_l = (uint32_t * )buf ;
406
450
spimd_reg .smwdr [0 ] = (uint32_t )(((uint32_t )(* buf_l )) & 0xfffffffful );
451
+ } else if (unit == 8 ) {
452
+ buf_l = (uint32_t * )buf ;
453
+ spimd_reg .smwdr [1 ] = (uint32_t )(((uint32_t )(* buf_l )) & 0xfffffffful );
454
+ buf_l ++ ;
455
+ spimd_reg .smwdr [0 ] = (uint32_t )(((uint32_t )(* buf_l )) & 0xfffffffful );
407
456
} else {
408
457
/* Do Nothing */
409
458
}
@@ -701,6 +750,9 @@ static void change_mmu_ttbl_spibsc(uint32_t type)
701
750
desc .XN = 0x1u ; /* XN = 1 (Execute never) */
702
751
} else { /* Xip */
703
752
desc = desc_tbl [index - (FLASH_BASE >> 20 )];
753
+ #if (FLASH_SIZE > 0x01000000 )
754
+ __NOP (); /* prevents '__aeabi_memcpy4' function insertion by the ARM compiler, which sabotages the RAM_CODE execution and respectively rises the DAbt_Handler exeption */
755
+ #endif
704
756
}
705
757
/* Write descriptor back to translation table */
706
758
table [index ] = desc ;
0 commit comments