[processor-sdk/performance-audio-sr.git] / pdk_k2g_1_0_1_0_eng / packages / ti / board / diag / norflash / src / norflash_test.c
1 /*
2 * Copyright (c) 2015, 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 spi_nor_test.c
37 *
38 * \brief This file contains spi nor test functions.
39 *
40 ******************************************************************************/
42 #include "norflash_test.h"
44 #include <ti/drv/gpio/GPIO.h>
45 #include <ti/csl/soc.h>
46 #include <ti/drv/gpio/soc/GPIO_soc.h>
48 /* Port and pin number mask for flash hold.
49 Bits 7-0: Pin number and Bits 15-8: Port number */
50 #define GPIO_FLASH_HOLD (0x0129)
52 /* GPIO Driver board specific pin configuration structure */
53 GPIO_PinConfig gpioPinConfigs[] = {
54 GPIO_FLASH_HOLD | GPIO_CFG_OUTPUT
55 };
57 /* GPIO Driver call back functions */
58 GPIO_CallbackFxn gpioCallbackFunctions[] = {
59 NULL
60 };
62 /* GPIO Driver configuration structure */
63 GPIO_v0_Config GPIO_v0_config = {
64 gpioPinConfigs,
65 gpioCallbackFunctions,
66 sizeof(gpioPinConfigs) / sizeof(GPIO_PinConfig),
67 sizeof(gpioCallbackFunctions) / sizeof(GPIO_CallbackFxn),
68 0,
69 };
71 /**
72 * \brief Function to configure SPI NOR flash hold signal
73 *
74 *
75 * \return None
76 */
77 static void configFlashHold(void)
78 {
79 GPIO_init();
80 GPIO_write(0, 1);
81 }
83 /**
84 * \brief Executes SPI NOR flash chip detect test
85 *
86 * \param p_device [IN] NOR flash device handle
87 * \param testArgs [IN] Test arguments
88 *
89 * \return
90 * \n TEST_PASS - Test Passed
91 * \n TEST_FAIL - Test Failed
92 */
93 static TEST_STATUS spi_nor_chip_detect_test(PLATFORM_DEVICE_info *p_device,
94 void *testArgs)
95 {
96 spiNorTestArgs_t *args = (spiNorTestArgs_t *)testArgs;
98 p_device = platform_device_open(args->devId, 0);
99 if (p_device == NULL)
100 {
101 UART_printf("SPI NOR Flash Test: Could Not Open NOR Device; errno = 0x%x \n", platform_errno);
102 return (TEST_FAIL);
103 }
105 UART_printf("Device Id - 0x%X\n", p_device->device_id);
106 UART_printf("Manufacturer Id - 0x%X\n", p_device->manufacturer_id);
107 UART_printf("Device Width - %d\n", p_device->width);
108 UART_printf("Block Count - %d\n", p_device->block_count);
109 UART_printf("Page Count - %d\n", p_device->page_count);
110 UART_printf("Page Size - %d\n", p_device->page_size);
112 platform_device_close(p_device->handle);
114 return (TEST_PASS);
115 }
117 /**
118 * \brief Executes SPI NOR flash memory read/write test
119 *
120 * \param p_device [IN] NOR flash device handle
121 * \param testArgs [IN] Test arguments
122 *
123 * \return
124 * \n TEST_PASS - Test Passed
125 * \n TEST_FAIL - Test Failed
126 */
127 static TEST_STATUS spi_nor_memory_access_test(PLATFORM_DEVICE_info *p_device,
128 void *testArgs)
129 {
130 spiNorTestArgs_t *args = (spiNorTestArgs_t *)testArgs;
131 Platform_STATUS status;
132 TEST_STATUS testStatus = TEST_FAIL;
134 uint32_t address;
135 uint8_t *norWrBuf;
136 uint8_t *norRdBuf;
137 #ifdef ENABLE_MEMORY_BACKUP
138 uint8_t *norBkpBuf;
139 #endif
141 norWrBuf = malloc(args->blockLen);
142 norRdBuf = malloc(args->blockLen);
144 if((norWrBuf == NULL) ||
145 (norRdBuf == NULL))
146 {
147 UART_printf("SPI NOR Flash Test: Buffer Allocation Failed!\n");
148 return (TEST_FAIL);
149 }
151 #ifdef ENABLE_MEMORY_BACKUP
152 norBkpBuf = malloc(args->blockLen);
153 if(norBkpBuf == NULL)
154 {
155 UART_printf("SPI NOR Flash Test: Buffer Allocation Failed!\n");
156 free(norWrBuf);
157 free(norRdBuf);
158 return (TEST_FAIL);
159 }
160 #endif
162 /* Initialize the test pattern */
163 memset(norWrBuf, args->testPattern, args->blockLen);
164 memset(norRdBuf, 0, args->blockLen);
166 address = ((args->sectNum) * (SPI_NOR_SECTOR_SIZE));
168 #ifdef ENABLE_MEMORY_BACKUP
169 status = platform_device_read(p_device->handle, address,
170 norBkpBuf, args->blockLen);
171 if(status != Platform_EOK)
172 {
173 UART_printf("SPI NOR Flash Test: Read Failed errno = 0x%x\n", platform_errno);
174 goto free;
175 }
176 #endif
178 status = platform_device_write(p_device->handle, address,
179 norWrBuf, args->blockLen);
180 if(status != Platform_EOK)
181 {
182 UART_printf("SPI NOR Flash Test: Write Test Data Failed errno = 0x%x\n", platform_errno);
183 goto free;
184 }
186 status = platform_device_read(p_device->handle, address,
187 norRdBuf, args->blockLen);
188 if(status != Platform_EOK)
189 {
190 UART_printf("SPI NOR Flash Test: Read Test Data Failed errno = 0x%x\n", platform_errno);
191 goto free;
192 }
194 if (memcmp(norWrBuf, norRdBuf, args->blockLen) != 0)
195 {
196 UART_printf("SPI NOR Flash Test: Data Verification Failed\n");
197 testStatus = TEST_FAIL;
198 }
199 else
200 {
201 UART_printf("SPI NOR Flash Test: Data Verification Passed\n");
202 testStatus = TEST_PASS;
203 }
205 #ifdef ENABLE_MEMORY_BACKUP
206 status = platform_device_erase_block (p_device->handle, args->sectNum);
207 if(status != Platform_EOK)
208 {
209 UART_printf("SPI NOR Flash Test: Erase Failed errno = 0x%x\n",
210 platform_errno);
211 testStatus = TEST_FAIL;
212 }
214 status = platform_device_write(p_device->handle, address,
215 norBkpBuf, args->blockLen);
216 if(status != Platform_EOK)
217 {
218 UART_printf("SPI NOR Flash Test: Write Back Original Data Failed errno = 0x%x\n",
219 platform_errno);
220 testStatus = TEST_FAIL;
221 }
222 #endif
224 free:
225 free(norWrBuf);
226 free(norRdBuf);
227 #ifdef ENABLE_MEMORY_BACKUP
228 free(norBkpBuf);
229 #endif
231 return (testStatus);
232 }
234 /**
235 * \brief Executes SPI NOR flash memory erase test
236 *
237 * \param p_device [IN] NOR flash device handle
238 * \param testArgs [IN] Test arguments
239 *
240 * \return
241 * \n TEST_PASS - Test Passed
242 * \n TEST_FAIL - Test Failed
243 */
244 static TEST_STATUS spi_nor_erase_test(PLATFORM_DEVICE_info *p_device,
245 void *testArgs)
246 {
247 spiNorTestArgs_t *args = (spiNorTestArgs_t *)testArgs;
248 Platform_STATUS status;
249 TEST_STATUS testStatus = TEST_FAIL;
251 uint32_t index;
252 uint32_t address;
253 uint8_t *norRdBuf;
254 #ifdef ENABLE_MEMORY_BACKUP
255 uint8_t *norBkpBuf;
256 #endif
258 norRdBuf = malloc(SPI_NOR_SECTOR_SIZE);
259 if(norRdBuf == NULL)
260 {
261 UART_printf("SPI NOR Flash Test: Buffer Allocation Failed!\n");
262 return (testStatus);
263 }
265 #ifdef ENABLE_MEMORY_BACKUP
266 norBkpBuf = malloc(SPI_NOR_SECTOR_SIZE);
267 if(norBkpBuf == NULL)
268 {
269 UART_printf("SPI NOR Flash Test: Buffer Allocation Failed!\n");
270 free(norRdBuf);
271 return (testStatus);
272 }
273 #endif
275 memset(norRdBuf, 0, SPI_NOR_SECTOR_SIZE);
277 address = ((args->sectNum) * (SPI_NOR_SECTOR_SIZE));
279 #ifdef ENABLE_MEMORY_BACKUP
280 status = platform_device_read(p_device->handle, address,
281 norBkpBuf, SPI_NOR_SECTOR_SIZE);
282 if(status != Platform_EOK)
283 {
284 UART_printf("SPI NOR Flash Test: Read Failed errno = 0x%x\n", platform_errno);
285 goto free;
286 }
287 #endif
289 status = platform_device_erase_block (p_device->handle, args->sectNum);
290 if(status != Platform_EOK)
291 {
292 UART_printf("SPI NOR Flash Test: Erase Failed for Sector %d errno = 0x%x\n",
293 args->sectNum, platform_errno);
294 goto free;
295 }
297 status = platform_device_read(p_device->handle, address,
298 norRdBuf, SPI_NOR_SECTOR_SIZE);
299 if(status != Platform_EOK)
300 {
301 UART_printf("SPI NOR Flash Test: Read Test Data Failed errno = 0x%x\n", platform_errno);
302 goto free;
303 }
305 for (index = 0; index < SPI_NOR_SECTOR_SIZE; index++)
306 {
307 if(norRdBuf[index] != 0xFF)
308 {
309 UART_printf("SPI NOR Flash Test: Erase Data Verification Failed\n");
310 testStatus = TEST_FAIL;
311 break;
312 }
313 }
315 if(index == SPI_NOR_SECTOR_SIZE)
316 {
317 UART_printf("SPI NOR Flash Test: Erase Data Verification Passed\n");
318 testStatus = TEST_PASS;
319 }
321 #ifdef ENABLE_MEMORY_BACKUP
322 status = platform_device_write(p_device->handle, address,
323 norBkpBuf, SPI_NOR_SECTOR_SIZE);
324 if(status != Platform_EOK)
325 {
326 UART_printf("SPI NOR Flash Test: Write Back Original Data Failed errno = 0x%x\n",
327 platform_errno);
328 testStatus = TEST_FAIL;
329 }
330 #endif
332 free:
333 free(norRdBuf);
334 #ifdef ENABLE_MEMORY_BACKUP
335 free(norBkpBuf);
336 #endif
338 return (testStatus);
339 }
341 /**
342 * \brief Executes SPI NOR flash tests
343 *
344 * \param testArgs [IN] Test arguments
345 *
346 * \return
347 * \n TEST_PASS - Test Passed
348 * \n TEST_FAIL - Test Failed
349 */
350 static TEST_STATUS run_spi_nor_test(void *testArgs)
351 {
352 PLATFORM_DEVICE_info *p_device = NULL;
353 spiNorTestArgs_t *args = (spiNorTestArgs_t *)testArgs;
354 TEST_STATUS testStatus = TEST_FAIL;
356 UART_printf("\nRunning SPI NOR Flash Chip Detect Test\n");
357 testStatus = spi_nor_chip_detect_test(p_device, args);
358 if(testStatus != TEST_PASS)
359 {
360 UART_printf("\nSPI NOR Flash Chip Detect Test Failed\n");
361 goto TEST_END;
362 }
363 else
364 {
365 UART_printf("\nSPI NOR Flash Chip Detect Test Passed\n");
366 }
368 p_device = platform_device_open(args->devId, 0);
369 if (p_device == NULL)
370 {
371 UART_printf("SPI NOR Flash Test: Could Not Open NOR Device; errno = 0x%x \n",
372 platform_errno);
373 return (TEST_FAIL);
374 }
376 UART_printf("\nRunning SPI NOR Flash Block Erase Test\n");
377 testStatus = spi_nor_erase_test(p_device, args);
378 if(testStatus != TEST_PASS)
379 {
380 UART_printf("\nSPI NOR Flash Block Erase Test Failed\n");
381 goto TEST_END;
382 }
383 else
384 {
385 UART_printf("\nSPI NOR Flash Block Erase Test Passed\n");
386 }
388 UART_printf("\nRunning SPI NOR Flash Memory Access Test - Test Pattern 1\n");
389 args->testPattern = NOR_FLASH_TEST_PATTERN1;
390 testStatus = spi_nor_memory_access_test(p_device, args);
391 if(testStatus != TEST_PASS)
392 {
393 UART_printf("\nSPI NOR Flash Memory Access Test Failed\n");
394 goto TEST_END;
395 }
396 else
397 {
398 UART_printf("\nSPI NOR Flash Memory Access Test Passed\n");
399 }
401 UART_printf("\nRunning SPI NOR Flash Memory Access Test - Test Pattern 2\n");
402 args->testPattern = NOR_FLASH_TEST_PATTERN2;
403 testStatus = spi_nor_memory_access_test(p_device, args);
404 if(testStatus != TEST_PASS)
405 {
406 UART_printf("\nSPI NOR Flash Memory Access Test Failed\n");
407 goto TEST_END;
408 }
409 else
410 {
411 UART_printf("\nSPI NOR Flash Memory Access Test Passed\n");
412 }
414 TEST_END:
415 platform_device_close(p_device->handle);
417 return (testStatus);
418 }
420 /**
421 * \brief This function performs spi nor test
422 *
423 * \param testArgs - Test arguments
424 *
425 * \return
426 * \n TEST_PASS - Test Passed
427 * \n TEST_FAIL - Test Failed
428 *
429 */
430 TEST_STATUS spiNorTest(void *testArgs)
431 {
432 TEST_STATUS testStatus;
434 spiNorTestArgs_t *args = (spiNorTestArgs_t *)testArgs;
436 UART_printf("\n********************************\n");
437 UART_printf( " SPI NOR Flash Test \n");
438 UART_printf( "********************************\n");
440 if(args == NULL)
441 {
442 UART_printf("Invalid Test Arguments!\n");
443 UART_printf("Aborting the Test!!\n");
444 return (TEST_FAIL);
445 }
447 if(args->autoRun == FALSE)
448 {
449 /* Read test inputs from user if needed */
450 }
452 /* Disable flash hold signal to get the access to SPI flash */
453 configFlashHold();
455 testStatus = run_spi_nor_test(args);
456 if(testStatus != TEST_PASS)
457 {
458 UART_printf("\nSPI NOR Flash Test Failed!\n");
459 }
460 else
461 {
462 UART_printf("\nSPI NOR Flash Test Passed!\n");
463 }
465 UART_printf("\nSPI NOR Flash Tests Completed!!\n");
466 UART_printf("\n-----------------X-----------------\n\n\n");
468 return (testStatus);
470 } // spiNorTest
472 /**
473 * \brief Invokes spi nor test functions
474 *
475 */
476 int main(void)
477 {
478 TEST_STATUS testStatus;
479 spiNorTestArgs_t testArgs;
480 Board_initCfg boardCfg;
482 testArgs.autoRun = 0;
483 testArgs.blockLen = SPI_NOR_SECTOR_SIZE;
484 testArgs.sectNum = 0;
485 testArgs.devId = PLATFORM_DEVID_NORN25Q128A13ESF40F;
486 testArgs.testPattern = 0xAA;
488 #ifdef PDK_RAW_BOOT
489 boardCfg = BOARD_INIT_PINMUX_CONFIG | BOARD_INIT_UART_STDIO;
490 #else
491 boardCfg = BOARD_INIT_UART_STDIO;
492 #endif
493 Board_init(boardCfg);
495 /* Invoke SPI NOR Test */
496 testStatus = spiNorTest(&testArgs);
497 if(testStatus != TEST_PASS)
498 {
499 return (-1);
500 }
502 return (0);
503 }
505 /* Nothing past this point */