-
Notifications
You must be signed in to change notification settings - Fork 60
/
lx_nand_flash_256byte_ecc_check.c
185 lines (150 loc) · 7.76 KB
/
lx_nand_flash_256byte_ecc_check.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** LevelX Component */
/** */
/** NAND Flash */
/** */
/**************************************************************************/
/**************************************************************************/
#define LX_SOURCE_CODE
/* Disable ThreadX error checking. */
#ifndef LX_DISABLE_ERROR_CHECKING
#define LX_DISABLE_ERROR_CHECKING
#endif
/* Include necessary system files. */
#include "lx_api.h"
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _lx_nand_flash_256byte_ecc_check PORTABLE C */
/* 6.1.7 */
/* AUTHOR */
/* */
/* William E. Lamie, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function checks 256 bytes of a NAND flash and ECC and */
/* attempts to correct any single bit errors. */
/* */
/* INPUT */
/* */
/* page_buffer Page buffer */
/* ecc_buffer Returned ECC buffer */
/* */
/* OUTPUT */
/* */
/* return status */
/* */
/* CALLS */
/* */
/* _lx_nand_flash_256byte_ecc_compute Compute ECC for 256 bytes */
/* */
/* CALLED BY */
/* */
/* _lx_nand_flash_page_ecc_check NAND page check */
/* */
/* RELEASE HISTORY */
/* */
/* DATE NAME DESCRIPTION */
/* */
/* 05-19-2020 William E. Lamie Initial Version 6.0 */
/* 09-30-2020 William E. Lamie Modified comment(s), */
/* resulting in version 6.1 */
/* 06-02-2021 Bhupendra Naphade Modified comment(s), */
/* resulting in version 6.1.7 */
/* */
/**************************************************************************/
UINT _lx_nand_flash_256byte_ecc_check(UCHAR *page_buffer, UCHAR *ecc_buffer)
{
INT i, j;
UCHAR mask;
UCHAR new_ecc_buffer[3];
UCHAR ecc_errors[3];
INT error_count;
USHORT *data;
USHORT byte;
USHORT bit;
ULONG correction_code;
/* Clear the error count. */
ecc_errors[0] = 0;
ecc_errors[1] = 0;
ecc_errors[2] = 0;
/* Calculate a new ECC for the 256 byte buffer. */
_lx_nand_flash_256byte_ecc_compute(page_buffer, new_ecc_buffer);
/* Clear error count. */
error_count = 0;
/* Loop through the ECC bytes to determine if there is an error in the page. */
for (i = 0; i < 3; i++)
{
/* Check for differences in the ECCs. */
ecc_errors[i] = new_ecc_buffer[i] ^ ecc_buffer[i];
/* Are there any errors? */
if (ecc_errors[i])
{
/* Accumulate the count of set bits. */
mask = 1;
for (j = 0; j < 8; j++)
{
/* Is this bit set? */
if (ecc_errors[i] & mask)
{
/* Yes, increment the count. */
error_count++;
}
/* Move mask to next bit. */
mask = (UCHAR) ((mask << 1) & 0xFF);
}
}
}
/* Determine if there are any errors. */
if (error_count == 0)
{
/* Everything is okay, return success. */
return(LX_SUCCESS);
}
/* Was a correctable error discovered? */
else if (error_count == 11)
{
/* Initialize bit and byte offset values. */
bit = 0;
byte = 0;
/* Setup the data pointer. */
data = (USHORT *) page_buffer;
/* Calculate the 32-bit correction code. */
correction_code = (ULONG) (ecc_errors[2] << 16) | (ULONG)(ecc_errors[1] << 8) | (ULONG)ecc_errors[0];
/* Unpack the correction code. */
byte = (USHORT) ((byte | ((correction_code >> (21+2)) & 1) << 6) & 0xFFFF);
byte = (USHORT) ((byte | ((correction_code >> (19+2)) & 1) << 5) & 0xFFFF);
byte = (USHORT) ((byte | ((correction_code >> (17+2)) & 1) << 4) & 0xFFFF);
byte = (USHORT) ((byte | ((correction_code >> (15+2)) & 1) << 3) & 0xFFFF);
byte = (USHORT) ((byte | ((correction_code >> (13+2)) & 1) << 2) & 0xFFFF);
byte = (USHORT) ((byte | ((correction_code >> (11+2)) & 1) << 1) & 0xFFFF);
byte = (USHORT) ((byte | ((correction_code >> (9+2)) & 1) << 0) & 0xFFFF);
bit = (USHORT) ((bit | ((correction_code >> (7+2)) & 1) << 3) & 0xFFFF);
bit = (USHORT) ((bit | ((correction_code >> (5+2)) & 1) << 2) & 0xFFFF);
bit = (USHORT) ((bit | ((correction_code >> (3+2)) & 1) << 1) & 0xFFFF);
bit = (USHORT) ((bit | ((correction_code >> (1+2)) & 1) << 0) & 0xFFFF);
/* Fix the error. */
data[byte] = (USHORT) ((data[byte] ^ (1 << bit)) & 0xFFFF);
/* Return an error corrected status. */
return(LX_NAND_ERROR_CORRECTED);
}
/* Otherwise, an unrecoverable ECC or data error is present. */
else
{
/* Return an error. */
return(LX_NAND_ERROR_NOT_CORRECTED);
}
}