1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** LevelX Component                                                      */
17 /**                                                                       */
18 /**   NAND Flash                                                          */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define LX_SOURCE_CODE
24 
25 
26 /* Disable ThreadX error checking.  */
27 
28 #ifndef LX_DISABLE_ERROR_CHECKING
29 #define LX_DISABLE_ERROR_CHECKING
30 #endif
31 
32 
33 /* Include necessary system files.  */
34 
35 #include "lx_api.h"
36 
37 
38 /**************************************************************************/
39 /*                                                                        */
40 /*  FUNCTION                                               RELEASE        */
41 /*                                                                        */
42 /*    _lx_nand_flash_page_ecc_check                       PORTABLE C      */
43 /*                                                           6.1.7        */
44 /*  AUTHOR                                                                */
45 /*                                                                        */
46 /*    William E. Lamie, Microsoft Corporation                             */
47 /*                                                                        */
48 /*  DESCRIPTION                                                           */
49 /*                                                                        */
50 /*    This function checks the NAND page and ECC for errors and           */
51 /*    attempts to correct 1 bit errors.                                   */
52 /*                                                                        */
53 /*  INPUT                                                                 */
54 /*                                                                        */
55 /*    nand_flash                            NAND flash instance           */
56 /*    page_buffer                           Page buffer                   */
57 /*    ecc_buffer                            Returned ECC buffer           */
58 /*                                                                        */
59 /*  OUTPUT                                                                */
60 /*                                                                        */
61 /*    return status                                                       */
62 /*                                                                        */
63 /*  CALLS                                                                 */
64 /*                                                                        */
65 /*    _lx_nand_flash_256byte_ecc_check      Check 256 bytes and ECC       */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    NAND flash driver                                                   */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
76 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
79 /*                                            resulting in version 6.1.7  */
80 /*                                                                        */
81 /**************************************************************************/
_lx_nand_flash_page_ecc_check(LX_NAND_FLASH * nand_flash,UCHAR * page_buffer,UCHAR * ecc_buffer)82 UINT  _lx_nand_flash_page_ecc_check(LX_NAND_FLASH *nand_flash, UCHAR *page_buffer, UCHAR *ecc_buffer)
83 {
84 
85 UINT    bytes_checked;
86 UINT    status;
87 UINT    return_status =  LX_SUCCESS;
88 
89 
90     /* Loop to check the entire NAND flash page.  */
91     bytes_checked =  0;
92     while (bytes_checked < nand_flash -> lx_nand_flash_bytes_per_page)
93     {
94 
95         /* Check this 256 byte piece of the NAND page.  */
96         status =  _lx_nand_flash_256byte_ecc_check(page_buffer, ecc_buffer);
97 
98         /* Determine if there was an error.  */
99         if (status != LX_SUCCESS)
100         {
101 
102             /* Determine if a non-correctable error is present.  */
103             if (status == LX_NAND_ERROR_NOT_CORRECTED)
104             {
105 
106                 /* Always return a non-correctable error, if present.  */
107                 return_status =  LX_NAND_ERROR_NOT_CORRECTED;
108                 break;
109             }
110 
111             /* A single-bit error was corrected, return this status
112                if there is no LX_ERROR status already detected.  */
113             else if (return_status == LX_SUCCESS)
114             {
115 
116                 /* Return a notice that the single bit error was corrected.  */
117                 return_status =  LX_NAND_ERROR_CORRECTED;
118             }
119         }
120 
121         /* Move to the next 256 byte portion of the page.  */
122         bytes_checked =  bytes_checked + 256;
123 
124         /* Move the page buffer forward.  */
125         page_buffer =  page_buffer + 256;
126 
127         /* Move the ECC buffer forward, note there are 3 bytes of ECC per page. */
128         ecc_buffer =   ecc_buffer + 3;
129     }
130 
131     /* Return status.  */
132     return(return_status);
133 }
134 
135