[processor-sdk/performance-audio-sr.git] / pdk_k2g_1_0_1 / packages / ti / board / diag / nand / src / nand_test.c
1 /*
2 * Copyright (c) 2016, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
34 /**
35 *
36 * \file nand_test.c
37 *
38 * \brief This file contains nand test functions.
39 *
40 ******************************************************************************/
42 #include "nand_test.h"
44 #include <ti/drv/uart/UART_stdio.h>
46 #include "board.h"
47 #include "board_cfg.h"
49 /**
50 * \brief Executes NAND flash chip detect test
51 *
52 * \param p_device [IN] NAND flash device handle
53 * \param testArgs [IN] Test arguments
54 *
55 * \return
56 * \n TEST_PASS - Test Passed
57 * \n TEST_FAIL - Test Failed
58 */
59 static TEST_STATUS nand_chip_detect_test(PLATFORM_DEVICE_info *p_device,
60 void *testArgs)
61 {
62 nandTestArgs_t *args = (nandTestArgs_t *)testArgs;
64 p_device = platform_device_open(args->devId, 0);
65 if (p_device == NULL)
66 {
67 UART_printf("NAND Flash Test: Could Not Open NAND Device; errno = 0x%x \n",
68 platform_errno);
69 return (TEST_FAIL);
70 }
72 UART_printf("Device Id - 0x%X\n", p_device->device_id);
73 UART_printf("Manufacturer Id - 0x%X\n", p_device->manufacturer_id);
74 UART_printf("Device Width - %d\n", p_device->width);
75 UART_printf("Block Count - %d\n", p_device->block_count);
76 UART_printf("Page Count - %d\n", p_device->page_count);
77 UART_printf("Page Size - %d\n", p_device->page_size);
78 UART_printf("Spare Area Size - %d\n", p_device->spare_size);
79 UART_printf("Column Address - %d\n", p_device->column);
81 platform_device_close(p_device->handle);
83 return (TEST_PASS);
84 }
86 /**
87 * \brief Executes NAND flash memory read/write test
88 *
89 * \param p_device [IN] NAND flash device handle
90 * \param testArgs [IN] Test arguments
91 *
92 * \return
93 * \n TEST_PASS - Test Passed
94 * \n TEST_FAIL - Test Failed
95 */
96 static TEST_STATUS nand_memory_access_test(PLATFORM_DEVICE_info *p_device,
97 void *testArgs)
98 {
99 Platform_STATUS status;
100 TEST_STATUS testStatus = TEST_FAIL;
101 nandTestArgs_t *args = (nandTestArgs_t *)testArgs;
102 uint32_t offset;
103 uint8_t *nandWrBuf = NULL;
104 uint8_t *nandRdBuf = NULL;
105 #ifdef ENABLE_MEMORY_BACKUP
106 uint8_t *nandBkpBuf = NULL;
107 #endif
109 /* Create buffers */
110 nandWrBuf = (uint8_t *) malloc(args->blockLen);
111 nandRdBuf = (uint8_t *) malloc(args->blockLen);
112 if ((nandWrBuf == NULL) ||
113 (nandRdBuf == NULL))
114 {
115 UART_printf("NAND Flash Test: Buffer Allocation Failed!\n");
116 goto NAND_ACCESS_TEST_DONE;
117 }
119 #ifdef ENABLE_MEMORY_BACKUP
120 nandBkpBuf = (uint8_t *) malloc(args->blockLen);
121 if (nandBkpBuf == NULL)
122 {
123 UART_printf("NAND Flash Test: Buffer Allocation Failed!\n");
124 goto NAND_ACCESS_TEST_DONE;
125 }
126 #endif
128 /* Get the NAND offset to the block */
129 status = platform_blocknpage_to_offset(p_device->handle, &offset,
130 args->blockNum, 0);
131 if(status != Platform_EOK)
132 {
133 UART_printf("NAND Flash Test: Block Number to Offset Conversion Failed - ");
134 UART_printf("Check the Block Number!!\n");
135 goto NAND_ACCESS_TEST_DONE;
136 }
138 #ifdef ENABLE_MEMORY_BACKUP
139 status = platform_device_read(p_device->handle, offset,
140 nandBkpBuf, args->blockLen);
141 if(status != Platform_EOK)
142 {
143 UART_printf("NAND Flash Test: Unable to Read Block %d Page %d errno = 0x%x\n",
144 args->blockNum, 0, platform_errno);
145 goto NAND_ACCESS_TEST_DONE;
146 }
147 #endif
149 /* Erase our test block so we can write to it */
150 status = platform_device_erase_block(p_device->handle, args->blockNum);
151 if(status != Platform_EOK)
152 {
153 UART_printf("NAND Flash Test: Unable to Erase (%d) Block %d errno = 0x%x\n",
154 args->blockNum, platform_errno);
155 goto NAND_ACCESS_TEST_DONE;
156 }
158 /* Fill in a test pattern */
159 memset(nandWrBuf, args->testPattern, args->blockLen);
160 memset(nandRdBuf, 0, args->blockLen);
162 /* Write the test pattern to the test block */
163 status = platform_device_write(p_device->handle, offset,
164 nandWrBuf, args->blockLen);
165 if (status != Platform_EOK)
166 {
167 UART_printf("NAND Flash Test: Unable to Write (%d) Block %d Page %d errno = 0x%x \n",
168 args->blockNum, 0, platform_errno);
169 goto NAND_ACCESS_TEST_DONE;
170 }
172 status = platform_device_read(p_device->handle, offset,
173 nandRdBuf, args->blockLen);
174 if (status != Platform_EOK)
175 {
176 UART_printf("NAND Flash Test: Unable to Read Block %d Page %d errno = 0x%x\n",
177 args->blockNum, 0, platform_errno);
178 goto NAND_ACCESS_TEST_DONE;
179 }
181 /* Compare to see if what we wrote is what we read */
182 if(memcmp(nandWrBuf, nandRdBuf, args->blockLen))
183 {
184 testStatus = TEST_FAIL;
185 UART_printf("NAND Flash Test: Data Verification Failed\n");
186 }
187 else
188 {
189 testStatus = TEST_PASS;
190 UART_printf("NAND Flash Test: Data Verification Passed\n");
191 }
193 #ifdef ENABLE_MEMORY_BACKUP
194 /* Restore the original buffer */
195 status = platform_device_erase_block(p_device->handle, args->blockNum);
196 if (status != Platform_EOK)
197 {
198 testStatus = TEST_FAIL;
199 UART_printf("NAND Flash Test: Unable to Erase Block %d errno = 0x%x \n",
200 args->blockNum, platform_errno);
201 goto NAND_ACCESS_TEST_DONE;
202 }
204 status = platform_device_write(p_device->handle, offset,
205 nandBkpBuf, args->blockLen);
206 if (status != Platform_EOK)
207 {
208 testStatus = TEST_FAIL;
209 UART_printf("NAND Flash Test: Unable to Write Back Original Data errno = 0x%x \n",
210 platform_errno);
211 goto NAND_ACCESS_TEST_DONE;
212 }
213 #endif
215 NAND_ACCESS_TEST_DONE:
217 if (nandWrBuf)
218 {
219 free(nandWrBuf);
220 }
222 if (nandRdBuf)
223 {
224 free(nandRdBuf);
225 }
227 #ifdef ENABLE_MEMORY_BACKUP
228 if (nandBkpBuf)
229 {
230 free(nandBkpBuf);
231 }
232 #endif
234 return (testStatus);
235 }
237 /**
238 * \brief Executes NAND flash memory erase test
239 *
240 * \param p_device [IN] NAND flash device handle
241 * \param testArgs [IN] Test arguments
242 *
243 * \return
244 * \n TEST_PASS - Test Passed
245 * \n TEST_FAIL - Test Failed
246 */
247 static TEST_STATUS nand_erase_test(PLATFORM_DEVICE_info *p_device,
248 void *testArgs)
249 {
250 nandTestArgs_t *args = (nandTestArgs_t *)testArgs;
251 Platform_STATUS status;
252 TEST_STATUS testStatus = TEST_FAIL;
254 uint32_t index;
255 uint32_t offset;
256 uint32_t blockSize;
257 uint8_t *nandRdBuf;
258 #ifdef ENABLE_MEMORY_BACKUP
259 uint8_t *nandBkpBuf;
260 #endif
262 blockSize = (BYTES_PER_PAGE * PAGES_PER_BLOCK);
264 nandRdBuf = malloc(blockSize);
265 if(nandRdBuf == NULL)
266 {
267 UART_printf("NAND Flash Test: Buffer Allocation Failed!\n");
268 goto NAND_ERASE_TEST_DONE;
269 }
271 #ifdef ENABLE_MEMORY_BACKUP
272 nandBkpBuf = malloc(blockSize);
273 if(nandBkpBuf == NULL)
274 {
275 UART_printf("NAND Flash Test: Buffer Allocation Failed!\n");
276 goto NAND_ERASE_TEST_DONE;
277 }
278 #endif
280 memset(nandRdBuf, 0, blockSize);
282 status = platform_blocknpage_to_offset(p_device->handle, &offset,
283 args->blockNum, 0);
284 if(status != Platform_EOK)
285 {
286 UART_printf("NAND Flash Test: Block Number to Offset Conversion Failed - ");
287 UART_printf("Check the Block Number!!\n");
288 goto NAND_ERASE_TEST_DONE;
289 }
291 #ifdef ENABLE_MEMORY_BACKUP
292 status = platform_device_read(p_device->handle, offset,
293 nandBkpBuf, blockSize);
294 if(status != Platform_EOK)
295 {
296 UART_printf("NAND Flash Test: Read Failed errno = 0x%x\n",
297 platform_errno);
298 goto NAND_ERASE_TEST_DONE;
299 }
300 #endif
302 status = platform_device_erase_block (p_device->handle, args->blockNum);
303 if(status != Platform_EOK)
304 {
305 UART_printf("NAND Flash Test: Erase Failed for NAND Block %d errno = 0x%x\n",
306 args->blockNum, platform_errno);
307 goto NAND_ERASE_TEST_DONE;
308 }
310 status = platform_device_read(p_device->handle, offset,
311 nandRdBuf, blockSize);
312 if(status != Platform_EOK)
313 {
314 UART_printf("NAND Flash Test: Reading Erased Block Failed errno = 0x%x\n",
315 platform_errno);
316 goto NAND_ERASE_TEST_DONE;
317 }
319 for (index = 0; index < blockSize; index++)
320 {
321 if(nandRdBuf[index] != 0xFF)
322 {
323 UART_printf("NAND Flash Test: Erase Data Verification Failed\n");
324 testStatus = TEST_FAIL;
325 break;
326 }
327 }
329 if(index == blockSize)
330 {
331 UART_printf("NAND Flash Test: Erase Data Verification Passed\n");
332 testStatus = TEST_PASS;
333 }
335 #ifdef ENABLE_MEMORY_BACKUP
336 status = platform_device_write(p_device->handle, offset,
337 nandBkpBuf, blockSize);
338 if(status != Platform_EOK)
339 {
340 UART_printf("NAND Flash Test: Write Back Original Data Failed errno = 0x%x\n",
341 platform_errno);
342 testStatus = TEST_FAIL;
343 }
344 #endif
346 NAND_ERASE_TEST_DONE:
347 if(nandRdBuf)
348 {
349 free(nandRdBuf);
350 }
352 #ifdef ENABLE_MEMORY_BACKUP
353 if(nandBkpBuf)
354 {
355 free(nandBkpBuf);
356 }
357 #endif
359 return (testStatus);
360 }
362 /**
363 * \brief Executes NAND flash tests
364 *
365 * \param testArgs [IN] Test arguments
366 *
367 * \return
368 * \n TEST_PASS - Test Passed
369 * \n TEST_FAIL - Test Failed
370 */
371 static TEST_STATUS run_nand_test(void *testArgs)
372 {
373 PLATFORM_DEVICE_info *p_device = NULL;
374 nandTestArgs_t *args = (nandTestArgs_t *)testArgs;
375 TEST_STATUS testStatus = TEST_FAIL;
377 UART_printf("\nRunning NAND Flash Chip Detect Test\n");
379 testStatus = nand_chip_detect_test(p_device, args);
380 if(testStatus != TEST_PASS)
381 {
382 UART_printf("\nNAND Flash Chip Detect Test Failed\n");
383 return (testStatus);
384 }
385 else
386 {
387 UART_printf("\nNAND Flash Chip Detect Test Passed\n");
388 }
390 p_device = platform_device_open(args->devId, 0);
391 if (p_device == NULL)
392 {
393 UART_printf("NAND Flash Test: Could Not Open NAND Device; errno = 0x%x \n",
394 platform_errno);
395 return (TEST_FAIL);
396 }
398 UART_printf("\nRunning NAND Flash Block Erase Test\n");
400 testStatus = nand_erase_test(p_device, args);
401 if(testStatus != TEST_PASS)
402 {
403 UART_printf("\nNAND Flash Block Erase Test Failed\n");
404 goto END_TEST;
405 }
406 else
407 {
408 UART_printf("\nNAND Flash Block Erase Test Passed\n");
409 }
411 UART_printf("\nRunning NAND Flash Memory Access Test - Test Pattern 1\n");
412 args->testPattern = NAND_FLASH_TEST_PATTERN1;
414 testStatus = nand_memory_access_test(p_device, args);
415 if(testStatus != TEST_PASS)
416 {
417 UART_printf("\nNAND Flash Memory Access Test Failed\n");
418 goto END_TEST;
419 }
420 else
421 {
422 UART_printf("\nNAND Flash Memory Access Test Passed\n");
423 }
425 UART_printf("\nRunning NAND Flash Memory Access Test - Test Pattern 2\n");
426 args->testPattern = NAND_FLASH_TEST_PATTERN2;
428 testStatus = nand_memory_access_test(p_device, args);
429 if(testStatus != TEST_PASS)
430 {
431 UART_printf("\nNAND Flash Memory Access Test Failed\n");
432 goto END_TEST;
433 }
434 else
435 {
436 UART_printf("\nNAND Flash Memory Access Test Passed\n");
437 }
439 END_TEST:
440 platform_device_close(p_device->handle);
442 return (testStatus);
443 }
445 /**
446 * \brief This function performs nand test
447 *
448 * \param testArgs - Test arguments
449 *
450 * \return
451 * \n TEST_PASS - Test Passed
452 * \n TEST_FAIL - Test Failed
453 *
454 */
455 TEST_STATUS nandTest(void *testArgs)
456 {
457 TEST_STATUS testStatus = TEST_PASS;
458 nandTestArgs_t *args = (nandTestArgs_t *)testArgs;
460 UART_printf("\n***********************\n");
461 UART_printf( " NAND Test \n");
462 UART_printf( "***********************\n");
464 if(args == NULL)
465 {
466 UART_printf("Invalid Test Arguments!\n");
467 UART_printf("Aborting the Test!!\n");
468 return (TEST_FAIL);
469 }
471 if(args->autoRun == FALSE)
472 {
473 /* Read test inputs from user if needed */
474 }
476 testStatus = run_nand_test(args);
477 if(testStatus != TEST_PASS)
478 {
479 UART_printf("\nNAND Flash Test Failed!\n");
480 }
481 else
482 {
483 UART_printf("\nNAND Flash Test Passed!\n");
484 }
486 UART_printf("\nNAND Flash Tests Completed!!\n");
487 UART_printf("\n-----------------X-----------------\n\n\n");
489 return (testStatus);
491 } // nandTest
493 /**
494 * \brief Invokes nand test functions
495 *
496 */
497 int main(void)
498 {
499 TEST_STATUS testStatus;
500 Platform_STATUS status;
501 nandTestArgs_t testArgs;
502 Board_initCfg boardCfg;
504 testArgs.autoRun = 0;
505 testArgs.blockLen = BYTES_PER_PAGE;
506 testArgs.blockNum = 0;
507 testArgs.devId = PLATFORM_DEVID_MT29F2G16ABAFA;
508 testArgs.testPattern = 0xAA;
510 #ifdef PDK_RAW_BOOT
511 boardCfg = BOARD_INIT_PINMUX_CONFIG |
512 BOARD_INIT_UART_STDIO;
513 #else
514 boardCfg = BOARD_INIT_UART_STDIO;
515 #endif
516 Board_init(boardCfg);
518 /* Invoke NAND flash Test */
519 testStatus = nandTest(&testArgs);
520 if(testStatus != TEST_PASS)
521 {
522 return (-1);
523 }
525 return (0);
526 }
528 /* Nothing past this point */