Skip to content

Commit 6866fbf

Browse files
committed
modify SPINANDBlockDevice.cpp
1 parent f1589ac commit 6866fbf

File tree

1 file changed

+49
-38
lines changed

1 file changed

+49
-38
lines changed

storage/blockdevice/COMPONENT_SPINAND/source/SPINANDBlockDevice.cpp

+49-38
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ int SPINANDBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
301301

302302
uint8_t status_reg;
303303
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)) {
305305
tr_error("Reading Status Register failed");
306306
read_failed = true;
307307
status = SPINAND_BD_ERROR_DEVICE_ERROR;
@@ -316,7 +316,7 @@ int SPINANDBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
316316
}
317317
} else {
318318
uint8_t ecc_steps = _ecc_steps;
319-
uint8_t *p = (uint8_t*)_page_buf;
319+
uint8_t *p = (uint8_t *)_page_buf;
320320

321321
if (QSPI_STATUS_OK != _qspi_send_read_command(_read_instruction, (void *)_page_buf, addr & SPINAND_PAGE_MASK, _page_size + _oob_size)) {
322322
tr_error("Read Command failed");
@@ -326,24 +326,23 @@ int SPINANDBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
326326
}
327327

328328
// 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);
331331
}
332332

333333
memcpy(_ecc_code, _page_buf + _page_size + _ecc_layout_pos, _ecc_bytes * _ecc_steps);
334334

335-
p = (uint8_t*)_page_buf;
335+
p = (uint8_t *)_page_buf;
336336
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) {
340340
tr_error("Reading data failed");
341341
status = SPINAND_BD_ERROR_DEVICE_ERROR;
342342
read_failed = true;
343343
goto exit_point;
344344
}
345-
}
346-
345+
}
347346
memcpy(buffer, _page_buf + offset, read_bytes);
348347
}
349348

@@ -410,27 +409,25 @@ int SPINANDBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t si
410409

411410
// prepare data
412411
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);
414413

415414
// 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);
418417
}
419418

420419
// 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);
422421

423422
written_bytes = _page_size + _oob_size;
424423
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)) {
426425
tr_error("Write failed");
427426
program_failed = true;
428427
status = SPINAND_BD_ERROR_DEVICE_ERROR;
429428
goto exit_point;
430429
}
431-
}
432-
433-
430+
}
434431

435432
buffer = static_cast<const uint8_t *>(buffer) + chunk;
436433
addr += SPINAND_PAGE_OFFSET;
@@ -627,7 +624,7 @@ bool SPINANDBlockDevice::_read_otp_onfi()
627624
tr_error("Writing Security Register failed");
628625
return 0;
629626
}
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))) {
631628
tr_error("Writing Security Register failed");
632629
return 0;
633630
}
@@ -641,13 +638,23 @@ bool SPINANDBlockDevice::_read_otp_onfi()
641638
_block_num = onfi_table[96] + (onfi_table[97] << 8);
642639
_block_size = _page_size * _page_num;
643640
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;
646647
}
647648
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;
651658
}
652659
_flash_size = _block_size * _block_num;
653660
_ecc_bits = onfi_table[112];
@@ -666,7 +673,7 @@ bool SPINANDBlockDevice::_read_otp_onfi()
666673
return 0;
667674
}
668675
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);
670677
return 1;
671678
}
672679

@@ -990,38 +997,41 @@ void SPINANDBlockDevice::_bch_init(uint8_t ecc_bits)
990997
t = (eccbytes * 8) / m;
991998

992999
_nbc.bch = init_bch(m, t, 0);
993-
if (!_nbc.bch)
1000+
if (!_nbc.bch) {
9941001
return;
1002+
}
9951003

9961004
/* verify that eccbytes has the expected value */
9971005
if (_nbc.bch->ecc_bytes != eccbytes) {
9981006
tr_error("invalid eccbytes %u, should be %u\n",
999-
eccbytes, _nbc.bch->ecc_bytes);
1007+
eccbytes, _nbc.bch->ecc_bytes);
10001008
return;
10011009
}
10021010

10031011
_page_buf = (uint8_t *)malloc(_page_size + _oob_size);
10041012
_ecc_calc = (uint8_t *)malloc(_ecc_steps * _ecc_bytes);
10051013
_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));
10081016
if (!_nbc.eccmask || !_nbc.errloc) {
10091017
return;
10101018
}
10111019
/*
10121020
* compute and store the inverted ecc of an erased ecc block
10131021
*/
1014-
erased_page = (unsigned char*)malloc(eccsize);
1015-
if (!erased_page)
1022+
erased_page = (unsigned char *)malloc(eccsize);
1023+
if (!erased_page) {
10161024
return;
1025+
}
10171026
memset(_page_buf, 0xff, _page_size + _oob_size);
10181027
memset(erased_page, 0xff, eccsize);
10191028
memset(_nbc.eccmask, 0, eccbytes);
10201029
encode_bch(_nbc.bch, erased_page, eccsize, _nbc.eccmask);
10211030
free(erased_page);
10221031

1023-
for (i = 0; i < eccbytes; i++)
1032+
for (i = 0; i < eccbytes; i++) {
10241033
_nbc.eccmask[i] ^= 0xff;
1034+
}
10251035
}
10261036

10271037
void SPINANDBlockDevice::_bch_free()
@@ -1034,7 +1044,7 @@ void SPINANDBlockDevice::_bch_free()
10341044
free(_ecc_code);
10351045
}
10361046

1037-
int SPINANDBlockDevice::_bch_calculate_ecc(unsigned char* buf, unsigned char* code)
1047+
int SPINANDBlockDevice::_bch_calculate_ecc(unsigned char *buf, unsigned char *code)
10381048
{
10391049
unsigned int i;
10401050

@@ -1050,21 +1060,22 @@ int SPINANDBlockDevice::_bch_calculate_ecc(unsigned char* buf, unsigned char* co
10501060
return 0;
10511061
}
10521062

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)
10541064
{
10551065
unsigned int *errloc = _nbc.errloc;
10561066
int i, count;
10571067

10581068
count = decode_bch(_nbc.bch, NULL, _ecc_size, read_ecc, calc_ecc,
1059-
NULL, errloc);
1069+
NULL, errloc);
10601070
if (count > 0) {
10611071
for (i = 0; i < count; i++) {
1062-
if (errloc[i] < (_ecc_size*8))
1072+
if (errloc[i] < (_ecc_size * 8)) {
10631073
/* error is located in data, correct it */
10641074
buf[(errloc[i] >> 3) ^ 3] ^= (1 << (errloc[i] & 7));
1065-
/* else error in ecc, no action needed */
1075+
}
10661076

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);
10681079
}
10691080
} else if (count < 0) {
10701081
tr_error("ecc unrecoverable error\n");

0 commit comments

Comments
 (0)