Skip to content

Commit 0d1d09b

Browse files
committed
add ondie and software ECC code
1 parent c37cd51 commit 0d1d09b

File tree

4 files changed

+1867
-31
lines changed

4 files changed

+1867
-31
lines changed

Diff for: storage/blockdevice/COMPONENT_SPINAND/include/SPINAND/SPINANDBlockDevice.h

+23-1
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ class SPINANDBlockDevice : public mbed::BlockDevice {
272272
/* Flash Configuration Functions */
273273
/*********************************/
274274

275+
// Read OTP ONFI parameters
276+
bool _read_otp_onfi();
277+
275278
// Quad Enable in Security Register
276279
int _set_quad_enable();
277280

@@ -283,7 +286,10 @@ class SPINANDBlockDevice : public mbed::BlockDevice {
283286

284287
// Wait on status register until write not-in-progress
285288
bool _is_mem_ready();
286-
289+
void _bch_init(uint8_t ecc_bits);
290+
void _bch_free();
291+
int _bch_calculate_ecc(unsigned char *buf, unsigned char *code);
292+
int _bch_correct_data(unsigned char *buf, unsigned char *read_ecc, unsigned char *calc_ecc);
287293
private:
288294

289295
// QSPI Driver Object
@@ -320,6 +326,22 @@ class SPINANDBlockDevice : public mbed::BlockDevice {
320326

321327
uint32_t _init_ref_count;
322328
bool _is_initialized;
329+
char _name[32];
330+
uint32_t _page_size, _block_size, _flash_size;
331+
uint8_t _page_shift, _block_shift;
332+
uint16_t _block_num, _page_num, _oob_size;
333+
uint8_t _ecc_bits, _ecc_bytes, _ecc_steps, _ecc_layout_pos;
334+
uint32_t _ecc_size;
335+
uint8_t *_ecc_calc;
336+
uint8_t *_ecc_code;
337+
uint8_t *_page_buf;
338+
339+
struct nand_bch_control {
340+
struct bch_control *bch;
341+
unsigned int *errloc;
342+
unsigned char *eccmask;
343+
};
344+
struct nand_bch_control _nbc;
323345
};
324346

325347
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Generic binary BCH encoding/decoding library
3+
*
4+
* This program is free software; you can redistribute it and/or modify it
5+
* under the terms of the GNU General Public License version 2 as published by
6+
* the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful, but WITHOUT
9+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11+
* more details.
12+
*
13+
* You should have received a copy of the GNU General Public License along with
14+
* this program; if not, write to the Free Software Foundation, Inc., 51
15+
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16+
*
17+
* Copyright © 2011 Parrot S.A.
18+
*
19+
* Author: Ivan Djelic <[email protected]>
20+
*
21+
* Description:
22+
*
23+
* This library provides runtime configurable encoding/decoding of binary
24+
* Bose-Chaudhuri-Hocquenghem (BCH) codes.
25+
*/
26+
/* mbed Microcontroller Library
27+
* Copyright (c) 2022 ARM Limited
28+
* SPDX-License-Identifier: Apache-2.0
29+
*
30+
* Licensed under the Apache License, Version 2.0 (the "License");
31+
* you may not use this file except in compliance with the License.
32+
* You may obtain a copy of the License at
33+
*
34+
* http://www.apache.org/licenses/LICENSE-2.0
35+
*
36+
* Unless required by applicable law or agreed to in writing, software
37+
* distributed under the License is distributed on an "AS IS" BASIS,
38+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39+
* See the License for the specific language governing permissions and
40+
* limitations under the License.
41+
*/
42+
#ifndef _BCH_H
43+
#define _BCH_H
44+
#ifdef __cplusplus
45+
extern "C" {
46+
#endif
47+
#include <stdint.h>
48+
//typedef unsigned char uint8_t;
49+
//typedef unsigned short uint16_t;
50+
//typedef unsigned int uint32_t;
51+
52+
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
53+
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
54+
/**
55+
* struct bch_control - BCH control structure
56+
* @m: Galois field order
57+
* @n: maximum codeword size in bits (= 2^m-1)
58+
* @t: error correction capability in bits
59+
* @ecc_bits: ecc exact size in bits, i.e. generator polynomial degree (<=m*t)
60+
* @ecc_bytes: ecc max size (m*t bits) in bytes
61+
* @a_pow_tab: Galois field GF(2^m) exponentiation lookup table
62+
* @a_log_tab: Galois field GF(2^m) log lookup table
63+
* @mod8_tab: remainder generator polynomial lookup tables
64+
* @ecc_buf: ecc parity words buffer
65+
* @ecc_buf2: ecc parity words buffer
66+
* @xi_tab: GF(2^m) base for solving degree 2 polynomial roots
67+
* @syn: syndrome buffer
68+
* @cache: log-based polynomial representation buffer
69+
* @elp: error locator polynomial
70+
* @poly_2t: temporary polynomials of degree 2t
71+
*/
72+
struct bch_control {
73+
unsigned int m;
74+
unsigned int n;
75+
unsigned int t;
76+
unsigned int ecc_bits;
77+
unsigned int ecc_bytes;
78+
/* private: */
79+
uint16_t *a_pow_tab;
80+
uint16_t *a_log_tab;
81+
uint32_t *mod8_tab;
82+
uint32_t *ecc_buf;
83+
uint32_t *ecc_buf2;
84+
unsigned int *xi_tab;
85+
unsigned int *syn;
86+
int *cache;
87+
struct gf_poly *elp;
88+
struct gf_poly *poly_2t[4];
89+
};
90+
91+
struct bch_control *init_bch(int m, int t, unsigned int prim_poly);
92+
93+
void free_bch(struct bch_control *bch);
94+
95+
void encode_bch(struct bch_control *bch, const uint8_t *data,
96+
unsigned int len, uint8_t *ecc);
97+
98+
int decode_bch(struct bch_control *bch, const uint8_t *data, unsigned int len,
99+
const uint8_t *recv_ecc, const uint8_t *calc_ecc,
100+
const unsigned int *syn, unsigned int *errloc);
101+
int fls(int x);
102+
#ifdef _X86_
103+
#define cpu_to_be32(x) ((((x)&0xff)<<24) | (((x)&0xff00)<<8) | (((x)>>8)&0xff00) | (((x)>>24)&0xff))
104+
#else
105+
#define cpu_to_be32(x) x
106+
#endif
107+
#ifdef __cplusplus
108+
}
109+
#endif
110+
#endif /* _BCH_H */

0 commit comments

Comments
 (0)