@@ -301,7 +301,7 @@ int SPINANDBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
301
301
302
302
uint8_t status_reg;
303
303
if (QSPI_STATUS_OK != _qspi_send_general_command (SPINAND_INST_GET_FEATURE, FEATURES_ADDR_STATUS,
304
- NULL , 0 , (char *) &status_reg, 1 )) {
304
+ NULL , 0 , (char *) &status_reg, 1 )) {
305
305
tr_error (" Reading Status Register failed" );
306
306
read_failed = true ;
307
307
status = SPINAND_BD_ERROR_DEVICE_ERROR;
@@ -316,7 +316,7 @@ int SPINANDBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
316
316
}
317
317
} else {
318
318
uint8_t ecc_steps = _ecc_steps;
319
- uint8_t *p = (uint8_t *)_page_buf;
319
+ uint8_t *p = (uint8_t *)_page_buf;
320
320
321
321
if (QSPI_STATUS_OK != _qspi_send_read_command (_read_instruction, (void *)_page_buf, addr & SPINAND_PAGE_MASK, _page_size + _oob_size)) {
322
322
tr_error (" Read Command failed" );
@@ -326,24 +326,23 @@ int SPINANDBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
326
326
}
327
327
328
328
// calculate the software ECC
329
- for (uint8_t i = 0 ; ecc_steps; ecc_steps--, i += _ecc_bytes, p += _ecc_size) {
330
- _bch_calculate_ecc (p, _ecc_calc + i);
329
+ for (uint8_t i = 0 ; ecc_steps; ecc_steps--, i += _ecc_bytes, p += _ecc_size) {
330
+ _bch_calculate_ecc (p, _ecc_calc + i);
331
331
}
332
332
333
333
memcpy (_ecc_code, _page_buf + _page_size + _ecc_layout_pos, _ecc_bytes * _ecc_steps);
334
334
335
- p = (uint8_t *)_page_buf;
335
+ p = (uint8_t *)_page_buf;
336
336
ecc_steps = _ecc_steps;
337
- for (uint8_t i = 0 ; ecc_steps; ecc_steps--, i += _ecc_bytes, p += _ecc_size) {
338
- int res = _bch_correct_data (p, _ecc_code + i, _ecc_calc + i);
339
- if (res < 0 ) {
337
+ for (uint8_t i = 0 ; ecc_steps; ecc_steps--, i += _ecc_bytes, p += _ecc_size) {
338
+ int res = _bch_correct_data (p, _ecc_code + i, _ecc_calc + i);
339
+ if (res < 0 ) {
340
340
tr_error (" Reading data failed" );
341
341
status = SPINAND_BD_ERROR_DEVICE_ERROR;
342
342
read_failed = true ;
343
343
goto exit_point;
344
344
}
345
- }
346
-
345
+ }
347
346
memcpy (buffer, _page_buf + offset, read_bytes);
348
347
}
349
348
@@ -410,27 +409,25 @@ int SPINANDBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t si
410
409
411
410
// prepare data
412
411
memset (_page_buf, 0xff , _page_size + _oob_size);
413
- memcpy (_page_buf + offset, (uint8_t *)buffer, written_bytes);
412
+ memcpy (_page_buf + offset, (uint8_t *)buffer, written_bytes);
414
413
415
414
// calculate the software ECC
416
- for (uint8_t i = 0 ; ecc_steps; ecc_steps--, i += _ecc_bytes, p += _ecc_size) {
417
- _bch_calculate_ecc (p, _ecc_calc + i);
415
+ for (uint8_t i = 0 ; ecc_steps; ecc_steps--, i += _ecc_bytes, p += _ecc_size) {
416
+ _bch_calculate_ecc (p, _ecc_calc + i);
418
417
}
419
418
420
419
// prepare ECC code
421
- memcpy (_page_buf + _page_size + _ecc_layout_pos, _ecc_calc, _ecc_bytes * _ecc_steps);
420
+ memcpy (_page_buf + _page_size + _ecc_layout_pos, _ecc_calc, _ecc_bytes * _ecc_steps);
422
421
423
422
written_bytes = _page_size + _oob_size;
424
423
result = _qspi_send_program_command (_program_instruction, (void *)_page_buf, addr & SPINAND_PAGE_MASK, &written_bytes);
425
- if ((result != QSPI_STATUS_OK)) {
424
+ if ((result != QSPI_STATUS_OK)) {
426
425
tr_error (" Write failed" );
427
426
program_failed = true ;
428
427
status = SPINAND_BD_ERROR_DEVICE_ERROR;
429
428
goto exit_point;
430
429
}
431
- }
432
-
433
-
430
+ }
434
431
435
432
buffer = static_cast <const uint8_t *>(buffer) + chunk;
436
433
addr += SPINAND_PAGE_OFFSET;
@@ -627,7 +624,7 @@ bool SPINANDBlockDevice::_read_otp_onfi()
627
624
tr_error (" Writing Security Register failed" );
628
625
return 0 ;
629
626
}
630
- if (QSPI_STATUS_OK != _qspi_send_read_command (SPINAND_INST_READ_CACHE, onfi_table, 1 << _page_shift, sizeof (onfi_table))) {
627
+ if (QSPI_STATUS_OK != _qspi_send_read_command (SPINAND_INST_READ_CACHE, onfi_table, 1 << _page_shift, sizeof (onfi_table))) {
631
628
tr_error (" Writing Security Register failed" );
632
629
return 0 ;
633
630
}
@@ -641,13 +638,23 @@ bool SPINANDBlockDevice::_read_otp_onfi()
641
638
_block_num = onfi_table[96 ] + (onfi_table[97 ] << 8 );
642
639
_block_size = _page_size * _page_num;
643
640
switch (_page_size) {
644
- case 2048 : _page_shift = 12 ; break ;
645
- case 4096 : _page_shift = 13 ; break ;
641
+ case 2048 :
642
+ _page_shift = 12 ;
643
+ break ;
644
+ case 4096 :
645
+ _page_shift = 13 ;
646
+ break ;
646
647
}
647
648
switch (_page_num) {
648
- case 64 : _block_shift = _page_shift + 6 ; break ;
649
- case 128 : _block_shift = _page_shift + 7 ; break ;
650
- case 256 : _block_shift = _page_shift + 8 ; break ;
649
+ case 64 :
650
+ _block_shift = _page_shift + 6 ;
651
+ break ;
652
+ case 128 :
653
+ _block_shift = _page_shift + 7 ;
654
+ break ;
655
+ case 256 :
656
+ _block_shift = _page_shift + 8 ;
657
+ break ;
651
658
}
652
659
_flash_size = _block_size * _block_num;
653
660
_ecc_bits = onfi_table[112 ];
@@ -666,7 +673,7 @@ bool SPINANDBlockDevice::_read_otp_onfi()
666
673
return 0 ;
667
674
}
668
675
secur_reg &= ~SPINAND_SECURE_BIT_OTP_EN;
669
- _qspi_send_general_command (SPINAND_INST_SET_FEATURE, FEATURES_ADDR_SECURE_OTP,(char *) &secur_reg, 1 , NULL , 0 );
676
+ _qspi_send_general_command (SPINAND_INST_SET_FEATURE, FEATURES_ADDR_SECURE_OTP, (char *) &secur_reg, 1 , NULL , 0 );
670
677
return 1 ;
671
678
}
672
679
@@ -990,38 +997,41 @@ void SPINANDBlockDevice::_bch_init(uint8_t ecc_bits)
990
997
t = (eccbytes * 8 ) / m;
991
998
992
999
_nbc.bch = init_bch (m, t, 0 );
993
- if (!_nbc.bch )
1000
+ if (!_nbc.bch ) {
994
1001
return ;
1002
+ }
995
1003
996
1004
/* verify that eccbytes has the expected value */
997
1005
if (_nbc.bch ->ecc_bytes != eccbytes) {
998
1006
tr_error (" invalid eccbytes %u, should be %u\n " ,
999
- eccbytes, _nbc.bch ->ecc_bytes );
1007
+ eccbytes, _nbc.bch ->ecc_bytes );
1000
1008
return ;
1001
1009
}
1002
1010
1003
1011
_page_buf = (uint8_t *)malloc (_page_size + _oob_size);
1004
1012
_ecc_calc = (uint8_t *)malloc (_ecc_steps * _ecc_bytes);
1005
1013
_ecc_code = (uint8_t *)malloc (_ecc_steps * _ecc_bytes);
1006
- _nbc.eccmask = (unsigned char *)malloc (eccbytes);
1007
- _nbc.errloc = (unsigned int *)malloc (t* sizeof (*_nbc.errloc ));
1014
+ _nbc.eccmask = (unsigned char *)malloc (eccbytes);
1015
+ _nbc.errloc = (unsigned int *)malloc (t * sizeof (*_nbc.errloc ));
1008
1016
if (!_nbc.eccmask || !_nbc.errloc ) {
1009
1017
return ;
1010
1018
}
1011
1019
/*
1012
1020
* compute and store the inverted ecc of an erased ecc block
1013
1021
*/
1014
- erased_page = (unsigned char *)malloc (eccsize);
1015
- if (!erased_page)
1022
+ erased_page = (unsigned char *)malloc (eccsize);
1023
+ if (!erased_page) {
1016
1024
return ;
1025
+ }
1017
1026
memset (_page_buf, 0xff , _page_size + _oob_size);
1018
1027
memset (erased_page, 0xff , eccsize);
1019
1028
memset (_nbc.eccmask , 0 , eccbytes);
1020
1029
encode_bch (_nbc.bch , erased_page, eccsize, _nbc.eccmask );
1021
1030
free (erased_page);
1022
1031
1023
- for (i = 0 ; i < eccbytes; i++)
1032
+ for (i = 0 ; i < eccbytes; i++) {
1024
1033
_nbc.eccmask [i] ^= 0xff ;
1034
+ }
1025
1035
}
1026
1036
1027
1037
void SPINANDBlockDevice::_bch_free ()
@@ -1034,7 +1044,7 @@ void SPINANDBlockDevice::_bch_free()
1034
1044
free (_ecc_code);
1035
1045
}
1036
1046
1037
- int SPINANDBlockDevice::_bch_calculate_ecc (unsigned char * buf, unsigned char * code)
1047
+ int SPINANDBlockDevice::_bch_calculate_ecc (unsigned char * buf, unsigned char * code)
1038
1048
{
1039
1049
unsigned int i;
1040
1050
@@ -1050,21 +1060,22 @@ int SPINANDBlockDevice::_bch_calculate_ecc(unsigned char* buf, unsigned char* co
1050
1060
return 0 ;
1051
1061
}
1052
1062
1053
- int SPINANDBlockDevice::_bch_correct_data (unsigned char * buf, unsigned char * read_ecc, unsigned char * calc_ecc)
1063
+ int SPINANDBlockDevice::_bch_correct_data (unsigned char * buf, unsigned char * read_ecc, unsigned char * calc_ecc)
1054
1064
{
1055
1065
unsigned int *errloc = _nbc.errloc ;
1056
1066
int i, count;
1057
1067
1058
1068
count = decode_bch (_nbc.bch , NULL , _ecc_size, read_ecc, calc_ecc,
1059
- NULL , errloc);
1069
+ NULL , errloc);
1060
1070
if (count > 0 ) {
1061
1071
for (i = 0 ; i < count; i++) {
1062
- if (errloc[i] < (_ecc_size* 8 ))
1072
+ if (errloc[i] < (_ecc_size * 8 )) {
1063
1073
/* error is located in data, correct it */
1064
1074
buf[(errloc[i] >> 3 ) ^ 3 ] ^= (1 << (errloc[i] & 7 ));
1065
- /* else error in ecc, no action needed */
1075
+ }
1066
1076
1067
- tr_error (" corrected bitflip %04x:%d\n " , (errloc[i]>>3 )^3 , errloc[i]&7 );
1077
+ /* else error in ecc, no action needed */
1078
+ tr_error (" corrected bitflip %04x:%d\n " , (errloc[i] >> 3 ) ^ 3 , errloc[i] & 7 );
1068
1079
}
1069
1080
} else if (count < 0 ) {
1070
1081
tr_error (" ecc unrecoverable error\n " );
0 commit comments