-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathECC_Check.cpp
325 lines (298 loc) · 10.9 KB
/
ECC_Check.cpp
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// ECC_Check.cpp: implementation of the CECC_Check class.
//
//////////////////////////////////////////////////////////////////////
#include "ECC_Check.h"
/*****************************************************************************/
/* */
/* NAME */
/* make_ecc_512 */
/* DESCRIPTION */
/* This function generates 3 byte ECC for 512 byte data. */
/* (Software ECC) */
/* PARAMETERS */
/* ecc_buf the location where ECC should be stored */
/* data_buf given data */
/* RETURN VALUES */
/* none */
/* */
/*****************************************************************************/
#if (XMODE == 8)
void make_ecc_512(uint8_t * ecc_buf, uint8_t * data_buf)
#else
void make_ecc_512(uint16_t * ecc_buf, uint16_t * data_buf)
#endif
{
uint32_t i, ALIGN_FACTOR;
uint32_t tmp;
uint32_t uiparity = 0;
uint32_t parityCol, ecc = 0;
uint32_t parityCol4321 = 0, parityCol4343 = 0, parityCol4242 = 0, parityColTot = 0;
uint32_t *Data;
uint32_t Xorbit=0;
ALIGN_FACTOR = (uint32_t)data_buf % 4 ;
Data = (uint32_t *)(data_buf + ALIGN_FACTOR);
for( i = 0; i < 16; i++)
{
parityCol = *Data++;
tmp = *Data++; parityCol ^= tmp; parityCol4242 ^= tmp;
tmp = *Data++; parityCol ^= tmp; parityCol4343 ^= tmp;
tmp = *Data++; parityCol ^= tmp; parityCol4343 ^= tmp; parityCol4242 ^= tmp;
tmp = *Data++; parityCol ^= tmp; parityCol4321 ^= tmp;
tmp = *Data++; parityCol ^= tmp; parityCol4242 ^= tmp; parityCol4321 ^= tmp;
tmp = *Data++; parityCol ^= tmp; parityCol4343 ^= tmp; parityCol4321 ^= tmp;
tmp = *Data++; parityCol ^= tmp; parityCol4242 ^= tmp; parityCol4343 ^= tmp; parityCol4321 ^= tmp;
parityColTot ^= parityCol;
tmp = (parityCol >> 16) ^ parityCol;
tmp = (tmp >> 8) ^ tmp;
tmp = (tmp >> 4) ^ tmp;
tmp = ((tmp >> 2) ^ tmp) & 0x03;
if ((tmp == 0x01) || (tmp == 0x02))
{
uiparity ^= i;
Xorbit ^= 0x01;
}
}
#if (XMODE == 8)
tmp = (parityCol4321 >> 16) ^ parityCol4321;
tmp = (tmp << 8) ^ tmp;
tmp = (tmp >> 4) ^ tmp;
tmp = (tmp >> 2) ^ tmp;
ecc |= ((tmp << 1) ^ tmp) & 0x200; // p128
#else
tmp = (parityCol4321 >> 16) ^ parityCol4321;
tmp = (tmp >> 8) ^ tmp;
tmp = (tmp << 4) ^ tmp;
tmp = (tmp << 2) ^ tmp;
ecc |= ((tmp << 1) ^ tmp) & 0x80; // p128
#endif
#if (XMODE == 8)
tmp = (parityCol4343 >> 16) ^ parityCol4343;
tmp = (tmp >> 8) ^ tmp;
tmp = (tmp << 4) ^ tmp;
tmp = (tmp << 2) ^ tmp;
ecc |= ((tmp << 1) ^ tmp) & 0x80; // p64
#else
tmp = (parityCol4343 >> 16) ^ parityCol4343;
tmp = (tmp >> 8) ^ tmp;
tmp = (tmp << 4) ^ tmp;
tmp = (tmp >> 2) ^ tmp;
ecc |= ((tmp << 1) ^ tmp) & 0x20; // p64
#endif
#if (XMODE == 8)
tmp = (parityCol4242 >> 16) ^ parityCol4242;
tmp = (tmp >> 8) ^ tmp;
tmp = (tmp << 4) ^ tmp;
tmp = (tmp >> 2) ^ tmp;
ecc |= ((tmp << 1) ^ tmp) & 0x20; // p32
#else
tmp = (parityCol4242 >> 16) ^ parityCol4242;
tmp = (tmp >> 8) ^ tmp;
tmp = (tmp >> 4) ^ tmp;
tmp = (tmp << 2) ^ tmp;
ecc |= ((tmp << 1) ^ tmp) & 0x08; // p32
#endif
#if (XMODE == 8)
tmp = parityColTot & 0xFFFF0000;
tmp = tmp >> 16;
tmp = (tmp >> 8) ^ tmp;
tmp = (tmp >> 4) ^ tmp;
tmp = (tmp << 2) ^ tmp;
ecc |= ((tmp << 1) ^ tmp) & 0x08; // p16
#else
tmp = parityColTot & 0xFFFF0000;
tmp = tmp >> 16;
tmp = (tmp >> 8) ^ tmp;
tmp = (tmp >> 4) ^ tmp;
tmp = (tmp >> 2) ^ tmp;
ecc |= ((tmp << 1) ^ tmp) & 0x02; // p16
#endif
#if (XMODE == 8)
tmp = parityColTot & 0xFF00FF00;
tmp = (tmp >> 16) ^ tmp;
tmp = (tmp >> 8);
tmp = (tmp >> 4) ^ tmp;
tmp = (tmp >> 2) ^ tmp;
ecc |= ((tmp << 1) ^ tmp) & 0x02; // p8
#else
tmp = parityColTot & 0xFF00FF00;
tmp = (tmp << 16) ^ tmp;
tmp = (tmp >> 8);
tmp = (tmp << 4) ^ tmp;
tmp = (tmp << 2) ^ tmp;
ecc |= ((tmp << 1) ^ tmp) & 0x800000; // p8
#endif
#if (XMODE == 8)
tmp = parityColTot & 0xF0F0F0F0 ;
tmp = (tmp << 16) ^ tmp;
tmp = (tmp >> 8) ^ tmp;
tmp = (tmp << 2) ^ tmp;
ecc |= ((tmp << 1) ^ tmp) & 0x800000; // p4
#else
tmp = parityColTot & 0xF0F0F0F0 ;
tmp = (tmp << 16) ^ tmp;
tmp = (tmp >> 8) ^ tmp;
tmp = (tmp >> 2) ^ tmp;
ecc |= ((tmp << 1) ^ tmp) & 0x200000; // p4
#endif
#if (XMODE == 8)
tmp = parityColTot & 0xCCCCCCCC ;
tmp = (tmp << 16) ^ tmp;
tmp = (tmp >> 8) ^ tmp;
tmp = (tmp << 4) ^ tmp;
tmp = (tmp >> 2);
ecc |= ((tmp << 1) ^ tmp) & 0x200000; // p2
#else
tmp = parityColTot & 0xCCCCCCCC ;
tmp = (tmp << 16) ^ tmp;
tmp = (tmp >> 8) ^ tmp;
tmp = (tmp >> 4) ^ tmp;
ecc |= ((tmp << 1) ^ tmp) & 0x80000; // p2
#endif
#if (XMODE == 8)
tmp = parityColTot & 0xAAAAAAAA ;
tmp = (tmp << 16) ^ tmp;
tmp = (tmp >> 8) ^ tmp;
tmp = (tmp >> 4) ^ tmp;
tmp = (tmp << 2) ^ tmp;
ecc |= (tmp & 0x80000); // p1
#else
tmp = parityColTot & 0xAAAAAAAA ;
tmp = (tmp << 16) ^ tmp;
tmp = (tmp >> 8) ^ tmp;
tmp = (tmp >> 4) ^ tmp;
tmp = (tmp >> 2) ^ tmp;
ecc |= (tmp & 0x20000); // p1
#endif
#if (XMODE == 8)
ecc |= (uiparity & 0x01) <<11;
ecc |= (uiparity & 0x02) <<12;
ecc |= (uiparity & 0x04) <<13;
ecc |= (uiparity & 0x08) <<14;
#else
ecc |= (uiparity & 0x01) <<9;
ecc |= (uiparity & 0x02) <<10;
ecc |= (uiparity & 0x04) <<11;
ecc |= (uiparity & 0x08) <<12;
#endif
if (Xorbit)
{
ecc |= (ecc ^ 0x00AAAAAA)>>1;
}
else
{
ecc |= (ecc >> 1);
}
#if (XMODE == 8)
ecc = ~ecc;
*(ecc_buf + 2) = (uint8_t) (ecc >> 16);
*(ecc_buf + 1) = (uint8_t) (ecc >> 8);
*(ecc_buf + 0) = (uint8_t) (ecc);
#else // X16
ecc = ( ~ecc ) | 0xFF000000;
*(ecc_buf + 1) = (uint16_t) (ecc >> 16);
*(ecc_buf + 0) = (uint16_t) (ecc);
#endif
}
/*****************************************************************************/
/* */
/* NAME */
/* compare_ecc_512 */
/* DESCRIPTION */
/* This function compares two ECCs and indicates if there is an error. */
/* PARAMETERS */
/* ecc_data1 one ECC to be compared */
/* ecc_data2 the other ECC to be compared */
/* page_data content of data page */
/* offset where the error occurred */
/* corrected correct data */
/* RETURN VALUES */
/* Upon successful completion, compare_ecc returns SSR_SUCCESS. */
/* Otherwise, corresponding error code is returned. */
/* */
/*****************************************************************************/
#if (XMODE == 8)
eccdiff_t compare_ecc_512(uint8_t *iEccdata1, uint8_t *iEccdata2,
uint8_t *pPagedata, int32_t *pOffset, uint8_t *pCorrected)
#else // X16
eccdiff_t compare_ecc_512(uint16_t *iEccdata1, uint16_t *iEccdata2,
uint16_t *pPagedata, int32_t *pOffset, uint16_t *pCorrected)
#endif
{
uint32_t iCompecc = 0, iEccsum = 0;
uint32_t iFindbyte = 0;
uint32_t iIndex;
uint32_t nT1 = 0, nT2 =0;
#if (XMODE == 8)
uint8_t iNewvalue;
uint8_t iFindbit = 0;
uint8_t *pEcc1 = (uint8_t *)iEccdata1;
uint8_t *pEcc2 = (uint8_t *)iEccdata2;
for ( iIndex = 0; iIndex <2; iIndex++)
{
nT1 ^= (((*pEcc1) >> iIndex) & 0x01);
nT2 ^= (((*pEcc2) >> iIndex) & 0x01);
}
for (iIndex = 0; iIndex < 3; iIndex++)
iCompecc |= ((~(*pEcc1++) ^ ~(*pEcc2++)) << iIndex * 8);
for(iIndex = 0; iIndex < 24; iIndex++) {
iEccsum += ((iCompecc >> iIndex) & 0x01);
}
#else // X16
uint16_t iNewvalue;
uint16_t iFindbit = 0;
uint16_t *pEcc1 = (uint16_t *)iEccdata1;
uint16_t *pEcc2 = (uint16_t *)iEccdata2;
for ( iIndex = 0; iIndex <2; iIndex++)
{
nT1 ^= (((*pEcc1) >> iIndex) & 0x01);
nT2 ^= (((*pEcc2) >> iIndex) & 0x01);
}
for (iIndex = 0; iIndex < 2; iIndex++) // 2 word of ECC data
iCompecc |= (((~*pEcc1++) ^ (~*pEcc2++)) << iIndex * 16);
for(iIndex = 0; iIndex < 24; iIndex++) {
iEccsum += ((iCompecc >> iIndex) & 0x01);
}
#endif
switch (iEccsum) {
case 0 :
//printf("RESULT : no error\n");
return ECC_NO_ERROR;
case 1 :
//printf("RESULT : ECC code 1 bit fail\n");
return ECC_ECC_ERROR;
case 12 :
if (nT1 != nT2)
{
#if (XMODE == 8)
iFindbyte = ((iCompecc >> 17 & 1) << 8) + ((iCompecc >> 15 & 1) << 7) + ((iCompecc >> 13 & 1) << 6)
+ ((iCompecc >> 11 & 1) << 5) + ((iCompecc >> 9 & 1) << 4) + ((iCompecc >> 7 & 1) << 3)
+ ((iCompecc >> 5 & 1) << 2) + ((iCompecc >> 3 & 1) << 1) + (iCompecc >> 1 & 1);
iFindbit = (uint8_t)(((iCompecc >> 23 & 1) << 2) + ((iCompecc >> 21 & 1) << 1) + (iCompecc >> 19 & 1));
iNewvalue = (uint8_t)(pPagedata[iFindbyte] ^ (1 << iFindbit));
#else // CASE_X16
iFindbyte = ((iCompecc >> 15 & 1) << 7) + ((iCompecc >> 13 & 1) << 6)
+ ((iCompecc >> 11 & 1) << 5) + ((iCompecc >> 9 & 1) << 4) + ((iCompecc >> 7 & 1) << 3)
+ ((iCompecc >> 5 & 1) << 2) + ((iCompecc >> 3 & 1) << 1) + (iCompecc >> 1 & 1) ;
iFindbit = (uint16_t)(((iCompecc >> 23 & 1) << 3) + ((iCompecc >> 21 & 1) << 2) + ((iCompecc >> 19 & 1) << 1)
+ (iCompecc >> 17 & 1) );
iNewvalue = (uint16_t)(pPagedata[iFindbyte] ^ (1 << iFindbit));
#endif
//printf("iCompecc = %d\n",iCompecc);
//printf("RESULT : one bit error\r\n");
//printf("byte = %d, bit = %d\r\n", iFindbyte, iFindbit);
//printf("corrupted = %x, corrected = %x\r\n", pPagedata[iFindbyte], iNewvalue);
if (pOffset != NULL) {
*pOffset = iFindbyte;
}
if (pCorrected != NULL) {
*pCorrected = iNewvalue;
}
return ECC_CORRECTABLE_ERROR;
}
else
return ECC_UNCORRECTABLE_ERROR;
default :
//printf("RESULT : unrecoverable error\n");
return ECC_UNCORRECTABLE_ERROR;
}
}