summaryrefslogtreecommitdiff
path: root/peripherals/hsmc4/hsmc4_ecc.c
blob: e27655f38bea400e7ef2ff00d46fd4b3f551ad32 (plain)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support 
 * ----------------------------------------------------------------------------
 * Copyright (c) 2008, Atmel Corporation
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ----------------------------------------------------------------------------
 */

//------------------------------------------------------------------------------
//         Headers
//------------------------------------------------------------------------------
#include "hsmc4.h"
#include "hsmc4_ecc.h"
#include <utility/trace.h>
#include <utility/assert.h>

//------------------------------------------------------------------------------
//         Internal function
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
/// Counts and return the number of bits set to '1' in the given byte.
/// \param byte  Byte to count.
//------------------------------------------------------------------------------
static unsigned char CountBitsInByte(unsigned char byte)
{
    unsigned char count = 0;
    while (byte > 0) {

        if (byte & 1) {

            count++;
        }
        byte >>= 1;
    }

    return count;
}
//------------------------------------------------------------------------------
/// Counts and return the number of bits set to '1' in the given hsiao code.
/// \param code  Hsizo code.
//------------------------------------------------------------------------------
static unsigned char CountBitsInCode(unsigned char *code)
{
    return CountBitsInByte(code[0])
           + CountBitsInByte(code[1])
           + CountBitsInByte(code[2]);
}

//------------------------------------------------------------------------------
/// Get all ECC parity and Nparity value.
//------------------------------------------------------------------------------
void HSMC4_EccGetValue(unsigned int *ecc)
{
   ecc[0] = AT91C_BASE_HSMC4->HSMC4_ECCPR0; 
   ecc[1] = AT91C_BASE_HSMC4->HSMC4_ECCPR1; 
   ecc[2] = AT91C_BASE_HSMC4->HSMC4_ECCPR2; 
   ecc[3] = AT91C_BASE_HSMC4->HSMC4_ECCPR3; 
   ecc[4] = AT91C_BASE_HSMC4->HSMC4_ECCPR4; 
   ecc[5] = AT91C_BASE_HSMC4->HSMC4_ECCPR5; 
   ecc[6] = AT91C_BASE_HSMC4->HSMC4_ECCPR6; 
   ecc[7] = AT91C_BASE_HSMC4->HSMC4_ECCPR7; 
   ecc[8] = AT91C_BASE_HSMC4->HSMC4_ECCPR8; 
   ecc[9] = AT91C_BASE_HSMC4->HSMC4_ECCPR9; 
   ecc[10] = AT91C_BASE_HSMC4->HSMC4_ECCPR10; 
   ecc[11] = AT91C_BASE_HSMC4->HSMC4_ECCPR11; 
   ecc[12] = AT91C_BASE_HSMC4->HSMC4_ECCPR12; 
   ecc[13] = AT91C_BASE_HSMC4->HSMC4_ECCPR13; 
   ecc[14] = AT91C_BASE_HSMC4->HSMC4_ECCPR14; 
   ecc[15] = AT91C_BASE_HSMC4->HSMC4_Eccpr15; 
#ifdef DUMP_ECC_PARITY   
      TRACE_INFO("Ecc parity(0-7) %x, %x, %x, %x, %x, %x, %x, %x \n\r",ecc[0],ecc[1],ecc[2],ecc[3],ecc[4],ecc[5],ecc[6],ecc[7]);
      TRACE_INFO("Ecc parity(8-15) %x, %x, %x, %x, %x, %x, %x, %x \n\r",ecc[8],ecc[9],ecc[10],ecc[11],ecc[12],ecc[13],ecc[14],ecc[15]);
#endif   
}

//------------------------------------------------------------------------------
//         Exported functions
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
/// Configures ECC mode in HSMC4 peripheral as specified. 
/// \param type  Type of correction.
/// \param pageSize  Page size of NAND flash device.
//------------------------------------------------------------------------------
void HSMC4_EccConfigure(unsigned int type, unsigned int pageSize)
{
    // Software Reset ECC.
    AT91C_BASE_HSMC4->HSMC4_ECCCR = (0x1 <<  1) ;
    AT91C_BASE_HSMC4->HSMC4_ECCCMD = type | pageSize;
}

//------------------------------------------------------------------------------
/// Returns type of ECC correction setting.
//------------------------------------------------------------------------------
unsigned int HSMC4_GetEccCorrectoinType(void)
{
    return ((AT91C_BASE_HSMC4->HSMC4_ECCCMD)& AT91C_ECC_TYPCORRECT);
}

//------------------------------------------------------------------------------
/// Returns ECC status by giving ecc number
/// \param eccNumber  ecc parity number from 0 to 15.
//------------------------------------------------------------------------------
unsigned char HSMC4_GetEccStatus(unsigned char eccNumber)
{
    unsigned int status;
    if (eccNumber < 8){
        status = AT91C_BASE_HSMC4->HSMC4_ECCSR1;
    } 
    else {
        status = AT91C_BASE_HSMC4->HSMC4_ECCSR2;
        eccNumber -=8;
    }
    return ((status >> (eccNumber * 4)) & 0x07);
}
//------------------------------------------------------------------------------
/// Verifies 4-bytes hsiao codes for a data block whose size is a page Size
/// word. Page words block is verified between the given HSIAO code 
/// generated by hardware and original HSIAO codes store has been previously stored.
/// Returns 0 if the data is correct, Hsiao_ERROR_SINGLEBIT if one or more
/// block(s) have had a single bit corrected, or either Hsiao_ERROR_ECC
/// or Hsiao_ERROR_MULTIPLEBITS.
/// \param data  Data buffer to verify.
/// \param originalCode  Original codes.
/// \param verifyCode  codes to be verified.
//------------------------------------------------------------------------------
static unsigned char HSMC4_VerifyPageOf8bitHsiao(
    unsigned char *data,
    const unsigned char *originalCode,
    const unsigned char *verifyCode)
{
    unsigned char correctionCode[4];
    unsigned char bitCount;
    // Xor both codes together
    correctionCode[0] = verifyCode[0] ^ originalCode[0];
    correctionCode[1] = verifyCode[1] ^ originalCode[1];
    correctionCode[2] = verifyCode[2] ^ originalCode[2];
    correctionCode[3] = verifyCode[3] ^ originalCode[3];
    TRACE_DEBUG("Correction code = %02X %02X %02X %02X\n\r",
                correctionCode[0], correctionCode[1], correctionCode[2], correctionCode[3]);
    // If all bytes are 0, there is no error
    if ((correctionCode[0] == 0)
        && (correctionCode[1] == 0)
        && (correctionCode[2] == 0)
        && (correctionCode[3] == 0)) {

        return 0;
    }
     // If there is a single bit error, there are 15 bits set to 1
    bitCount = CountBitsInByte(correctionCode[0]) + 
               CountBitsInByte(correctionCode[1]) + 
               CountBitsInByte(correctionCode[2]) + 
               CountBitsInByte(correctionCode[3]);
    if (bitCount == 15) {
        // Get byte and bit indexes
        unsigned short byte = (correctionCode[0] & 0xf0) >> 4;
        byte |= (correctionCode[1] & 0xff) << 4;
        unsigned char bit = correctionCode[0] & 0x0f;
        // Correct bit
        TRACE_INFO("Correcting byte #%d at bit %d\n\r", byte, bit);
        data[byte] ^= (1 << bit);

        return Hsiao_ERROR_SINGLEBIT;
    }
   
    // Check if ECC has been corrupted
    if (bitCount == 1) {
        return Hsiao_ERROR_ECC;
    }
    // Otherwise, this is a multi-bit error
    else {
        return Hsiao_ERROR_MULTIPLEBITS;
    }
}

//------------------------------------------------------------------------------
/// Verifies 3-bytes hsiao codes for a data block whose size is multiple of
/// 256 bytes. Each 256-bytes block is verified between the given HSIAO code 
/// generated by hardware and original HSIAO codes store has been previously stored.
/// Returns 0 if the data is correct, Hsiao_ERROR_SINGLEBIT if one or more
/// block(s) have had a single bit corrected, or either Hsiao_ERROR_ECC
/// or Hsiao_ERROR_MULTIPLEBITS.
/// \param data  Data buffer to verify.
/// \param size  Size of the data in words.
/// \param originalCode  Original codes.
/// \param verifyCode  codes to be verified.
//------------------------------------------------------------------------------
static unsigned char HSMC4_Verify256x8bitHsiao(
    unsigned char *data,
    unsigned int size,
    const unsigned char *originalCode,
    const unsigned char *verifyCode)
{
    unsigned char correctionCode[3];
    unsigned int position = 0;
    unsigned char byte;
    unsigned char bit;
    unsigned char error = 0;
    
    TRACE_DEBUG("HSMC4_Verify512x8bitHsiao()\n\r");
    while (position < size) {
        // Xor both codes together
        correctionCode[0] = verifyCode[0] ^ originalCode[0];
        correctionCode[1] = verifyCode[1] ^ originalCode[1];
        correctionCode[2] = verifyCode[2] ^ originalCode[2];
        TRACE_DEBUG("Correction code = %02X %02X %02X\n\r",
            correctionCode[0], correctionCode[1], correctionCode[2]);
            
        // If all bytes are 0, there is no error
        if ( correctionCode[0] || correctionCode[1] || correctionCode[2]) {
            // If there is a single bit error, there are 11 bits set to 1
            if (CountBitsInCode(correctionCode) == 11) {
                // Get byte and bit indexes
                byte = (correctionCode[0] & 0xf8) >> 3;
                byte |= (correctionCode[1] & 0x07) << 5;
                bit = correctionCode[0] & 0x07;
                // Correct bit
                TRACE_INFO("Correcting byte #%d at bit %d\n\r", (position + byte), bit);
                data[byte] ^= (1 << bit);
                error = Hsiao_ERROR_SINGLEBIT;
            }
            // Check if ECC has been corrupted
            else if (CountBitsInCode(correctionCode) == 1) {
                return Hsiao_ERROR_ECC;
            }
            else {
                // Otherwise, this is a multi-bit error
                return Hsiao_ERROR_MULTIPLEBITS;
            }
        }
        data += 256;
        originalCode += 3;
        verifyCode += 3;
        position += 256;
    }
    return error;
}

//------------------------------------------------------------------------------
/// Verifies 3-bytes hsiao codes for a data block whose size is multiple of
/// 512 bytes. Each 512-bytes block is verified between the given HSIAO code 
/// generated by hardware and original HSIAO codes store has been previously stored.
/// Returns 0 if the data is correct, Hsiao_ERROR_SINGLEBIT if one or more
/// block(s) have had a single bit corrected, or either Hsiao_ERROR_ECC
/// or Hsiao_ERROR_MULTIPLEBITS.
/// \param data  Data buffer to verify.
/// \param size  Size of the data in words.
/// \param originalCode  Original codes.
/// \param verifyCode  codes to be verified.
//------------------------------------------------------------------------------
static unsigned char HSMC4_Verify512x8bitHsiao(
    unsigned char *data,
    unsigned int size,
    const unsigned char *originalCode,
    const unsigned char *verifyCode)
{
    unsigned char correctionCode[3];
    unsigned int position = 0;
    unsigned short byte;
    unsigned char bit;
    unsigned char error = 0;
    
    TRACE_DEBUG("HSMC4_Verify512x8bitHsiao()\n\r");
    while (position < size) {
        // Xor both codes together
        correctionCode[0] = verifyCode[0] ^ originalCode[0];
        correctionCode[1] = verifyCode[1] ^ originalCode[1];
        correctionCode[2] = verifyCode[2] ^ originalCode[2];
        TRACE_DEBUG("Correction code = %02X %02X %02X\n\r",
            correctionCode[0], correctionCode[1], correctionCode[2]);
            
        // If all bytes are 0, there is no error
        if ( correctionCode[0] || correctionCode[1] || correctionCode[2]) {
            // If there is a single bit error, there are 11 bits set to 1
            if (CountBitsInCode(correctionCode) == 12) {
                // Get byte and bit indexes
                byte = (correctionCode[0] & 0xf8) >> 3;
                byte |= (correctionCode[1] & 0x0f) << 5;
                bit = correctionCode[0] & 0x07;
                // Correct bit
                TRACE_INFO("Correcting byte #%d at bit %d\n\r", (position + byte), bit);
                data[byte] ^= (1 << bit);
                error = Hsiao_ERROR_SINGLEBIT;
            }
            // Check if ECC has been corrupted
            else if (CountBitsInCode(correctionCode) == 1) {
                return Hsiao_ERROR_ECC;
            }
            else {
                // Otherwise, this is a multi-bit error
                return Hsiao_ERROR_MULTIPLEBITS;
            }
        }
        data += 512;
        originalCode += 3;
        verifyCode += 3;
        position += 512;
    }
    return error;
}

//------------------------------------------------------------------------------
/// Verifies 4-bytes hsiao codes for a data block whose size is a page Size
/// word. Page words block is verified between the given HSIAO code 
/// generated by hardware and original HSIAO codes store has been previously stored.
/// Returns 0 if the data is correct, Hsiao_ERROR_SINGLEBIT if one or more
/// block(s) have had a single bit corrected, or either Hsiao_ERROR_ECC
/// or Hsiao_ERROR_MULTIPLEBITS.
/// \param data  Data buffer to verify.
/// \param originalCode  Original codes.
/// \param verifyCode  codes to be verified.
//------------------------------------------------------------------------------
static unsigned char HSMC4_VerifyPageOf16bitHsiao(
    unsigned short *data,
    const unsigned char *originalCode,
    const unsigned char *verifyCode)
{
    unsigned char correctionCode[4];
    unsigned char bitCount;
    // Xor both codes together
    correctionCode[0] = verifyCode[0] ^ originalCode[0];
    correctionCode[1] = verifyCode[1] ^ originalCode[1];
    correctionCode[2] = verifyCode[2] ^ originalCode[2];
    correctionCode[3] = verifyCode[3] ^ originalCode[3];
    TRACE_DEBUG("Correction code = %02X %02X %02X %02X\n\r",
                correctionCode[0], correctionCode[1], correctionCode[2], correctionCode[3]);
    // If all bytes are 0, there is no error
    if ((correctionCode[0] == 0)
        && (correctionCode[1] == 0)
        && (correctionCode[2] == 0)
        && (correctionCode[3] == 0)) {

        return 0;
    }
     // If there is a single bit error, there are 11 bits set to 1
    bitCount = CountBitsInByte(correctionCode[0]) + 
               CountBitsInByte(correctionCode[1]) + 
               CountBitsInByte(correctionCode[2]) + 
               CountBitsInByte(correctionCode[3]);
    printf("bitCount = %d \n\r",bitCount);
    if (bitCount == 12) {
        // Get byte and bit indexes
        unsigned char word = (correctionCode[0] & 0xf0) >> 4;
        word |= (correctionCode[1] & 0xff) << 4;
        unsigned char bit = correctionCode[0] & 0x0f;
        // Correct bit
        TRACE_INFO("Correcting word #%d at bit %d\n\r", word, bit);
        data[word] ^= (1 << bit);

        return Hsiao_ERROR_SINGLEBIT;
    }
   
    // Check if ECC has been corrupted
    if (bitCount == 1) {
        return Hsiao_ERROR_ECC;
    }
    // Otherwise, this is a multi-bit error
    else {
        return Hsiao_ERROR_MULTIPLEBITS;
    }
}

//------------------------------------------------------------------------------
/// Verifies 3-bytes hsiao codes for a data block whose size is multiple of
/// 256 word.Each 256-words block is verified between the given HSIAO code 
/// generated by hardware and original HSIAO codes store has been previously stored.
/// Returns 0 if the data is correct, Hsiao_ERROR_SINGLEBIT if one or more
/// block(s) have had a single bit corrected, or either Hsiao_ERROR_ECC
/// or Hsiao_ERROR_MULTIPLEBITS.
/// \param data  Data buffer to verify.
/// \param size  Size of the data in words.
/// \param originalCode  Original codes.
/// \param verifyCode  codes to be verified.
//------------------------------------------------------------------------------
static unsigned char HSMC4_Verify256x16bitHsiao(
    unsigned short *data,
    unsigned int size,
    const unsigned char *originalCode,
    const unsigned char *verifyCode
    )
{
    unsigned char correctionCode[3];
    unsigned int position = 0;
    unsigned char word;
    unsigned char bit;
    unsigned char error = 0;
    
    TRACE_DEBUG("HSMC4_Verify512x8bitHsiao()\n\r");
    while (position < size) {
        // Xor both codes together
        correctionCode[0] = verifyCode[0] ^ originalCode[0];
        correctionCode[1] = verifyCode[1] ^ originalCode[1];
        correctionCode[2] = verifyCode[2] ^ originalCode[2];
        TRACE_DEBUG("Correction code = %02X %02X %02X\n\r",
            correctionCode[0], correctionCode[1], correctionCode[2]);
            
        // If all bytes are 0, there is no error
        if ( correctionCode[0] || correctionCode[1] || correctionCode[2]) {
            // If there is a single bit error, there are 11 bits set to 1
            if (CountBitsInCode(correctionCode) == 12) {
                // Get word and bit indexes
                word = (correctionCode[0] & 0xf0) >> 4;
                word |= (correctionCode[1] & 0x0f) << 4;
                bit = correctionCode[0] & 0x0f;
                // Correct bit
                TRACE_INFO("Correcting word #%d at bit %d\n\r", (position + word), bit);
                data[word] ^= (1 << bit);
                error = Hsiao_ERROR_SINGLEBIT;
            }
            // Check if ECC has been corrupted
            else if (CountBitsInCode(correctionCode) == 1) {
                return Hsiao_ERROR_ECC;
            }
            else {
                // Otherwise, this is a multi-bit error
                return Hsiao_ERROR_MULTIPLEBITS;
            }
        }
        data += 256;
        originalCode += 3;
        verifyCode += 3;
        position += 256;
    }
    return error;
}

//------------------------------------------------------------------------------
/// Verifies hsiao codes for a data block. The block is verified between the given 
/// HSIAO code generated by hardware and original HSIAO codes store has been 
/// previously stored.
/// Returns 0 if the data is correct, Hsiao_ERROR_SINGLEBIT if one or more
/// block(s) have had a single bit corrected, or either Hsiao_ERROR_ECC
/// or Hsiao_ERROR_MULTIPLEBITS.
/// \param data  Data buffer to verify.
/// \param size  Size of the data in words.
/// \param originalCode  Original codes.
/// \param verifyCode  codes to be verified.
/// \param dataPath 8bit/16bit data path.
//------------------------------------------------------------------------------
unsigned char HSMC4_VerifyHsiao(
    unsigned char *data,
    unsigned int size,
    const unsigned char *originalCode,
    const unsigned char *verifyCode,
    unsigned char dataPath)
{
    unsigned char correctionType;
    unsigned char error = 0;
    correctionType = HSMC4_GetEccCorrectoinType();
    // For 16-bit data path
    if (dataPath == 16) {
        switch (correctionType){
            case AT91C_ECC_TYPCORRECT_ONE_PER_PAGE:
                error = HSMC4_VerifyPageOf16bitHsiao((unsigned short*)data, originalCode, verifyCode);
                break;
            case AT91C_ECC_TYPCORRECT_ONE_EVERY_256_BYTES:
                error= HSMC4_Verify256x16bitHsiao((unsigned short*)data, size / 2, originalCode, verifyCode);
                break;
            case AT91C_ECC_TYPCORRECT_ONE_EVERY_512_BYTES:
                TRACE_WARNING("16-bit 512 per page correction not yet implement! \n\r");
                break;
        }
    }
    // For 8-bit data path
    else {
        switch (correctionType){
            case AT91C_ECC_TYPCORRECT_ONE_PER_PAGE:
                error = HSMC4_VerifyPageOf8bitHsiao(data, originalCode, verifyCode);
                break;
            case AT91C_ECC_TYPCORRECT_ONE_EVERY_256_BYTES:
                 error = HSMC4_Verify256x8bitHsiao(data, size, originalCode, verifyCode);
                 break;
            case AT91C_ECC_TYPCORRECT_ONE_EVERY_512_BYTES:
                error = HSMC4_Verify512x8bitHsiao(data, size, originalCode, verifyCode);
                break;
        }
    }
    return error;
}

//------------------------------------------------------------------------------
/// Get 32-bit ECC code for 16-bit data path NAND flash.
/// 32-bit ECC is generated in order to perform one bit correction 
/// for a page in page 512/1024/2048/4096 for 16-bit words
/// \param size  Data size in bytes.
/// \param code  Codes buffer.
//------------------------------------------------------------------------------
void HSMC4_Get24bitPerPageEcc(unsigned int pageDataSize, unsigned char *code)
{
    unsigned int eccParity;
    unsigned int eccNparity;
    unsigned int ecc[16];
    // Get Parity value.
    HSMC4_EccGetValue(ecc);
     
    //  ----   P16384'P8192'P4096'P2048'  P1024'P512'P256' ---  4th. Ecc Byte to store
    /// P128'  P64'   P32'  P16'  P8'     P4'   P2'  P1'   ---  3rd. Ecc Byte to store
    //  ----   P16384 P8192 P4096 P2048   P1024 P512 P256  ---  2nd. Ecc Byte to store
    //  P128   P64    P32   P16   P8      P4    P2   P1    ---  1st. Ecc Byte to store
    
    // Invert codes (linux compatibility)    
    eccParity = ~(ecc[0]);
    eccNparity = ~(ecc[1]);
    TRACE_DEBUG("ecc Parity is 0x%08x, ecc Nparity is 0x%08x \n\r", eccParity, eccNparity);
    code[0] = eccParity & 0xff;
    code[1] = (eccParity >> 8 )& 0xff;
    code[2] = eccNparity & 0xff;
    code[3] = (eccNparity >> 8 )& 0xff;
}

//------------------------------------------------------------------------------
/// Get 24-bit ECC code for 8-bit data path NAND flash.
/// 24-bit ECC is generated in order to perform one bit correction 
/// for 256 byte in page 512/1024/2048/4096 for 8-bit words
/// \param size  Data size in bytes.
/// \param code  Codes buffer.
//------------------------------------------------------------------------------
void HSMC4_Get24bitPer256Ecc(unsigned int pageDataSize, unsigned char *code)
{
    unsigned char i;
    unsigned char numEcc;
    unsigned int eccParity;
    unsigned int ecc[16];
    HSMC4_EccGetValue(ecc);
    numEcc = pageDataSize / 256;
  
    //  P2048' P1024' P512' P256' P128'   P64'  P32' P16'  ---  3rd. Ecc Byte to store
    //  P8'    P4'    P2'   P1'   P2048   P1024 P512 P256  ---  2nd. Ecc Byte to store
    //  P128   P64    P32   P16   P8      P4    P2   P1    ---  1st. Ecc Byte to store
    for (i = 0; i < numEcc; i++) {
        // Get Parity and NParity value.
        eccParity = ecc[i];
        // Invert codes (linux compatibility)
        eccParity = ~eccParity; 
        TRACE_DEBUG("ecc Parity%d is 0x%08x \n\r", i, eccParity);
        code[i * 3] = eccParity & 0xff;
        code[i * 3 + 1] = (eccParity >> 8) & 0xff;
        code[i * 3 + 2] = (eccParity >> 16) & 0xff;
    }
}

//------------------------------------------------------------------------------
/// Get 24-bit ECC code for 8-bit data path NAND flash.
/// 24-bit ECC is generated in order to perform one bit correction 
/// for 512 byte in page 512/1024/2048/4096 for 8-bit words
/// \param size  Data size in bytes.
/// \param code  Codes buffer.
//------------------------------------------------------------------------------
void HSMC4_Get24bitPer512Ecc(unsigned int pageDataSize, unsigned char *code)
{
    unsigned char i;
    unsigned char numEcc;
    unsigned int eccParity;
    unsigned int ecc[16];
    HSMC4_EccGetValue(ecc);
    numEcc = pageDataSize / 512;
  
    //  P2048' P1024' P512' P256' P128'   P64'  P32' P16'  ---  3rd. Ecc Byte to store
    //  P8'    P4'    P2'   P1'   P2048   P1024 P512 P256  ---  2nd. Ecc Byte to store
    //  P128   P64    P32   P16   P8      P4    P2   P1    ---  1st. Ecc Byte to store
    for (i = 0; i < numEcc; i++) {
        // Get Parity and NParity value.
        eccParity = ecc[i];
        // Invert codes (linux compatibility)
        eccParity = ~eccParity; 
        TRACE_DEBUG("ecc Parity%d is 0x%08x \n\r", i, eccParity);
        code[i * 3] = eccParity & 0xff;
        code[i * 3 + 1] = (eccParity >> 8) & 0xff;
        code[i * 3 + 2] = (eccParity >> 16) & 0xff;
    }
}

//------------------------------------------------------------------------------
/// Get 32-bit ECC code for 16-bit data path NAND flash.
/// 32-bit ECC is generated in order to perform one bit correction 
/// for 256 word in page 512/1024/2048/4096 for 16-bit words
/// \param size  Data size in bytes.
/// \param code  Codes buffer.
//------------------------------------------------------------------------------
void HSMC4_Get32bitPer256Ecc(unsigned int pageDataSize, unsigned char *code)
{
    unsigned char i;
    unsigned char numEcc;
    unsigned int eccParity;
    unsigned int eccNparity;
    unsigned int ecc[16];
    HSMC4_EccGetValue(ecc);
    numEcc = pageDataSize / 256;

    //  P2048' P1024' P512' P256' P128'   P64'  P32' P16'  ---  3rd. Ecc Byte to store
    //  P8'    P4'    P2'   P1'   P2048   P1024 P512 P256  ---  2nd. Ecc Byte to store
    //  P128   P64    P32   P16   P8      P4    P2   P1    ---  1st. Ecc Byte to store
    for (i = 0; i < numEcc; i+= 2) {
        // Get Parity value.
        eccParity = ecc[i];
        // Invert codes (linux compatibility)
        eccParity = ~eccParity; 
        // Get NParity value.
        eccNparity = ecc[i + 1];
        eccNparity = ~eccNparity;
        TRACE_DEBUG("ecc Parity%d is 0x%08x, ecc Nparity%d is 0x%08x \n\r", i, eccParity, i, eccNparity);
        code[i * 3] = eccParity & 0xff;
        code[i * 3 + 1] = ((eccParity >> 8) & 0x0f ) | ((eccNparity & 0x0f) << 4);
        code[i * 3 + 2] = (eccNparity >> 4) & 0xff;

        code[(i + 1) * 3] = (eccParity >> 16) & 0xff;
        code[(i + 1) * 3 + 1] = ((eccParity >> 24) & 0x0f ) | (((eccNparity >> 16)& 0x0f) << 4);
        code[(i + 1) * 3 + 2] =  (eccNparity >> 20) & 0xff;

    }
}

//------------------------------------------------------------------------------
/// Get 32-bit ECC code for 16-bit data path NAND flash.
/// 32-bit ECC is generated in order to perform one bit correction 
/// for a page in page 512/1024/2048/4096 for 16-bit words
/// \param size  Data size in bytes.
/// \param code  Codes buffer.
//------------------------------------------------------------------------------
void HSMC4_Get32bitPerPageEcc(unsigned int pageDataSize, unsigned char *code)
{
    unsigned int eccParity;
    unsigned int eccNparity;
    unsigned int ecc[16];
    // Get Parity value.
    HSMC4_EccGetValue(ecc);
     
    //  ----   P16384'P8192'P4096'P2048'  P1024'P512'P256' ---  4th. Ecc Byte to store
    /// P128'  P64'   P32'  P16'  P8'     P4'   P2'  P1'   ---  3rd. Ecc Byte to store
    //  ----   P16384 P8192 P4096 P2048   P1024 P512 P256  ---  2nd. Ecc Byte to store
    //  P128   P64    P32   P16   P8      P4    P2   P1    ---  1st. Ecc Byte to store
    
    // Invert codes (linux compatibility)    
    eccParity = ~(ecc[0]);
    eccNparity = ~(ecc[1]);
    TRACE_DEBUG("ecc Parity%d is 0x%08x, ecc Nparity%d is 0x%08x \n\r", eccParity, eccNparity);
    code[0] = eccParity & 0xff;
    code[1] = (eccParity >> 8 )& 0xff;
    code[2] = eccNparity & 0xff;
    code[3] = (eccNparity >> 8 )& 0xff;
}

//------------------------------------------------------------------------------
/// Get ECC code for 8bit/16-bit data path NAND flash by giving data path.
/// 24-bit or 32-bit ECC is generated in order to perform one bit correction 
/// for a page in page 512/1024/2048/4096.
/// \param size  Data size in bytes.
/// \param code  Codes buffer.
/// \param dataPath 8bit/16bit data path.
//------------------------------------------------------------------------------
void HSMC4_GetEccParity(unsigned int pageDataSize, unsigned char *code, unsigned char dataPath)
{
    unsigned char correctionType;
    correctionType = HSMC4_GetEccCorrectoinType();
    // For 16-bit data path
    if (dataPath == 16) {
        switch (correctionType){
            case AT91C_ECC_TYPCORRECT_ONE_PER_PAGE:
                HSMC4_Get32bitPerPageEcc(pageDataSize, code);
                break;
            case AT91C_ECC_TYPCORRECT_ONE_EVERY_256_BYTES:
                HSMC4_Get32bitPer256Ecc(pageDataSize, code);
                break;
            case AT91C_ECC_TYPCORRECT_ONE_EVERY_512_BYTES:
                TRACE_WARNING("16 bit  512 byte correct not yet implement! \n\r");
                break;
        }
    }
    // For 8-bit data path
    else {
        switch (correctionType){
            case AT91C_ECC_TYPCORRECT_ONE_PER_PAGE:
                HSMC4_Get24bitPerPageEcc(pageDataSize, code);
                break;
            case AT91C_ECC_TYPCORRECT_ONE_EVERY_256_BYTES:
                 HSMC4_Get24bitPer256Ecc(pageDataSize, code);
                 break;
            case AT91C_ECC_TYPCORRECT_ONE_EVERY_512_BYTES:
                HSMC4_Get24bitPer512Ecc(pageDataSize, code);
                break;
        }
    }
}
personal git repositories of Harald Welte. Your mileage may vary