[processor-sdk/performance-audio-sr.git] / psdk_cust / pdk_k2g_1_0_1_2_eng / packages / ti / board / diag / framework / src / framework.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 */
33 /* ========================================================================== */
34 /* Include Files */
35 /* ========================================================================== */
38 #if defined(SOC_AM335x) || defined(SOC_AM437x)
39 #include "diag_mmc.h"
40 #else
41 #include <string.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <stdbool.h>
45 #include <ti/csl/tistdtypes.h>
46 #include <ti/fs/fatfs/ff.h>
47 #include <ti/csl/csl_a15.h>
49 #include <ti/drv/uart/UART_stdio.h>
50 #include <ti/boot/sbl/board/src/sbl_image_copy.h>
51 #include <ti/boot/sbl/src/rprc/sbl_rprc_parse.h>
52 #endif
54 #include "framework.h"
56 /* Diagnostic commands */
57 DIAG_cmdEntry_t DIAG_cmdTbl[] =
58 {
59 {"help", &DIAG_printMenu},
60 {"run", &DIAG_runTest},
61 {"status", &DIAG_printStatus},
62 {NULL, NULL}
63 };
65 /* Diagnostic applications */
66 DIAG_testApp_t DIAG_appTbl[DIAG_APP_MAX_NUM];
67 uint32_t DIAG_numApp;
69 static DIR DIAG_dir;
70 static FILINFO DIAG_fileInfo;
72 /**
73 * \brief This API Invalidates Instruction cache.
74 *
75 * \param None.
76 *
77 * \return None.
78 *
79 **/
80 void CP15ICacheFlush(void)
81 {
82 asm (" mov r0, #0\n\t"
83 " mcr p15, #0, r0, c7, c5, #0\n\t");
84 }
86 void DIAG_printMenu()
87 {
88 UART_printf("Command options:\n");
89 UART_printf("\thelp - displays this help menu again\n");
90 UART_printf("\trun - run a diagnostic application\n");
91 UART_printf("\tstatus - prints the test status\n");
92 }
94 void DIAG_printStatus()
95 {
96 int i, j;
97 UART_printf("Diagnostic Test Status:\n");
98 UART_printf("--------------------------------------------------------------\n");
99 UART_printf("ID\tName\t\t\t\tPass\t# of times Ran\n");
100 for (i = 0; i<DIAG_numApp; i++)
101 {
102 UART_printf("%d\t%s",
103 i+1,
104 DIAG_appTbl[i].appName);
106 /* note: 32 is 4 tabs between "Name" and "Pass". This is just for display */
107 for (j = 0; j<(32 - strlen(DIAG_appTbl[i].appName)); j++)
108 {
109 UART_printf(" ");
110 }
111 UART_printf("%s\t%d\n",
112 (DIAG_appTbl[i].appPassed == 1) ? "Yes":"No",
113 DIAG_appTbl[i].appRan);
114 }
115 UART_printf("\n");
116 }
118 void DIAG_parseApps()
119 {
120 FRESULT fresult;
121 char *dir = "0:";
122 int i;
123 TCHAR lfname[_MAX_LFN];
125 for (i = 0; i<DIAG_APP_MAX_NUM; i++)
126 {
127 DIAG_appTbl[i].appName[0] = '\0';
128 DIAG_appTbl[i].appPassed = 0;
129 DIAG_appTbl[i].appRan = 0;
130 }
132 DIAG_numApp = 0;
133 fresult = f_opendir(&DIAG_dir, dir);
134 while (fresult == FR_OK)
135 {
136 DIAG_fileInfo.lfname = lfname;
137 DIAG_fileInfo.lfsize = _MAX_LFN - 1;
138 fresult = f_readdir(&DIAG_dir, &DIAG_fileInfo);
139 if (fresult == FR_OK)
140 {
141 if(DIAG_fileInfo.fname[0] == '\0') break;
142 else
143 {
144 if (DIAG_fileInfo.lfname[0])
145 {
146 if (strstr(DIAG_fileInfo.lfname, DIAG_APP_IDENTIFIER) != NULL)
147 {
148 strcpy(DIAG_appTbl[DIAG_numApp].appName, DIAG_fileInfo.lfname);
149 DIAG_numApp++;
150 }
151 }
152 else
153 {
154 if (strstr(DIAG_fileInfo.fname, DIAG_APP_IDENTIFIER) != NULL)
155 {
156 strcpy(DIAG_appTbl[DIAG_numApp].appName, DIAG_fileInfo.fname);
157 DIAG_numApp++;
158 }
159 }
160 }
161 }
162 else
163 {
164 UART_printf("Failed in f_readdir()\n");
165 }
166 if (DIAG_numApp == DIAG_APP_MAX_NUM)
167 {
168 UART_printf("Warning: max number of diagnostic applications reached\n");
169 break;
170 }
171 }
172 #if defined(SOC_K2G) || defined(SOC_AM572x) || defined(SOC_AM571x)
173 fp_readData = &DIAG_fread;
174 fp_seek = &DIAG_fseek;
175 #endif
176 }
178 int DIAG_checkCmd(char *cmd)
179 {
180 int index;
181 for (index = 0; DIAG_cmdTbl[index].cmdName != NULL; index++)
182 {
183 if (strcmp(cmd, DIAG_cmdTbl[index].cmdName) == 0) break;
184 }
185 if (DIAG_cmdTbl[index].cmdName == NULL) index = -1;
186 return index;
187 }
189 void DIAG_runTest()
190 {
191 int32_t retVal;
192 FIL fp;
193 FRESULT fresult;
194 int n;
195 char buf[10];
196 sblEntryPoint_t pEntry;
198 void (*func_ptr)(int);
199 register int result asm("r9");
201 DIAG_printStatus();
202 UART_printf("Select test number (1 - %d): ", DIAG_numApp);
203 UART_scanFmt("%s\n", buf);
204 n = atoi(buf);
206 if ( (n<1) || (n>(DIAG_numApp+1)) )
207 {
208 UART_printf("Invalid number %d! Please select from 1 to %d.\n", n, DIAG_numApp);
209 return;
210 }
211 else
212 {
213 fresult = f_open(&fp, DIAG_appTbl[n-1].appName, (uint8_t) FA_READ);
214 if (fresult != FR_OK)
215 {
216 UART_printf("Failed to open %s\n", DIAG_appTbl[n-1].appName);
217 return;
218 }
219 else
220 {
221 UART_printf("Parsing %s\n", DIAG_appTbl[n-1].appName);
222 retVal = SBL_MulticoreImageParse((void *) &fp, 0, &pEntry);
224 f_close(&fp);
225 CP15ICacheFlush();
226 if (retVal != E_PASS)
227 {
228 UART_printf("Error in parsing image\n");
229 return;
230 }
231 else
232 {
233 if (pEntry.entryPoint_MPU_CPU0 == 0)
234 {
235 UART_printf("Unable to get entry point\n");
236 return;
237 }
238 else
239 {
240 UART_printf("Running %s\n", DIAG_appTbl[n-1].appName);
241 DIAG_appTbl[n-1].appRan++;
242 #if defined(SOC_K2G) || defined(SOC_AM572x) || defined(SOC_AM571x)
243 /* Clean the data Cache. */
244 CSL_a15WbAllDataCache();
245 #endif
246 func_ptr = (void *) pEntry.entryPoint_MPU_CPU0;
247 (*func_ptr)(DIAG_VAL);
248 if (result == 0)
249 DIAG_appTbl[n-1].appPassed = 1;
250 else
251 DIAG_appTbl[n-1].appPassed = 0;
253 UART_printf("Finished running %s, result %s\n",
254 DIAG_appTbl[n-1].appName,
255 (DIAG_appTbl[n-1].appPassed == 1)? "passed!" : "failed!");
256 }
257 }
258 }
259 }
260 }
262 void DIAG_main()
263 {
264 char cmd[255];
265 bool diag_continue = true;
266 int index = 0;
267 void (*fnPtr) ();
268 UART_printf("\nDIAGNOSTIC TEST FRAMEWORK\n");
270 DIAG_parseApps();
271 DIAG_printMenu();
273 while (diag_continue)
274 {
275 UART_printf("[Diag Menu]: ");
276 UART_scanFmt("%s", cmd);
277 index = DIAG_checkCmd(cmd);
278 if (index >= 0)
279 {
280 fnPtr = DIAG_cmdTbl[index].cmdFn;
281 (*fnPtr)();
282 }
283 }
284 }
286 int32_t DIAG_fread(void *buff, void *fileptr, uint32_t size)
287 {
288 FIL *fp = (FIL *) (fileptr);
289 uint32_t i = 0;
290 uint32_t bytes_read = 0;
291 uint32_t Max_read = 0x400U;
292 FRESULT fresult = FR_OK;
294 for (i = 0; i < (size / Max_read); ++i)
295 {
296 fresult = f_read(fp, buff, Max_read, &bytes_read);
297 buff += bytes_read;
298 if (fresult != FR_OK)
299 {
300 break;
301 }
302 }
303 if (fresult == FR_OK)
304 {
305 fresult = f_read(fp, buff, (uint16_t) (size % Max_read), &bytes_read);
306 }
308 return fresult;
309 }
311 void DIAG_fseek(void *fileptr, uint32_t location)
312 {
313 FIL *fp = (FIL *) (fileptr);
314 f_lseek(fp, location);
315 }