add other tools utilities and examples
[keystone-rtos/mcsdk-tools.git] / post / src / post.c
1 /******************************************************************************
2  * Copyright (c) 2011 Texas Instruments Incorporated - http://www.ti.com
3  * 
4  *  Redistribution and use in source and binary forms, with or without 
5  *  modification, are permitted provided that the following conditions 
6  *  are met:
7  *
8  *    Redistributions of source code must retain the above copyright 
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  *    Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the 
13  *    documentation and/or other materials provided with the   
14  *    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 
21  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
22  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
24  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
25  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
26  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
29  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
30  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * 
32  *****************************************************************************/
34 #include <string.h>
35 #include "platform.h"
36 #include "types.h"
37 #include "post.h"
39 /* The version string */
40 #pragma DATA_SECTION(post_version, ".version")
41 #pragma DATA_ALIGN(post_version, 16)
42 char post_version[] = POST_VERSION;
44 /******************************************************************************
45  * Function:    post_display_led_error  
46  ******************************************************************************/
47 void 
48 post_display_led_error
49 (
50     POST_TEST_ID     test_id
51 )
52 {
53     uint8_t     led_status[POST_MAX_NUM_LED];
54     uint32_t    i;
55         
56     memset(led_status, POST_LED_ON, POST_MAX_NUM_LED);
57     while (TRUE)
58     {
59         for (i = 0; i < POST_MAX_NUM_LED; i++)
60         {
61             if (post_led_status[test_id][i] == POST_LED_BLINK)
62             {
63                 if (led_status[i] == POST_LED_ON)
64                 {
65                     led_status[i] = POST_LED_OFF;
66                 }
67                 else
68                 {
69                     led_status[i] = POST_LED_ON;
70                 }
71                 platform_led(i, (PLATFORM_LED_OP)led_status[i], PLATFORM_USER_LED_CLASS);
72             }
73             else
74             {
75                 platform_led(i, (PLATFORM_LED_OP)post_led_status[test_id][i], PLATFORM_USER_LED_CLASS);
76             }
77         }
78         platform_delay(POST_LED_BLINK_DELAY);
79         /* POST in the while(1) loop to display the LED error status */
80     }
81 }
83 /******************************************************************************
84  * Function:    post_write_uart  
85  ******************************************************************************/
86 Bool 
87 post_write_uart
88 (
89     char*      msg
90 )
91 {
92     uint32_t i;
93     uint32_t msg_len = strlen(msg);
95     /* Write the message to the UART */
96     for (i = 0; i < msg_len; i++)
97     {
98         if (platform_uart_write(msg[i]) != Platform_EOK)
99         {
100             return FALSE;
101         }
102     }
104     return TRUE;
107 /******************************************************************************
108  * Function:    post_display_status  
109  ******************************************************************************/
110 void 
111 post_display_status
113     POST_TEST_ID        test_id,
114     Bool                test_passed
117     uint32_t    i;
118     char        *msg;
119     char        msg1[40] = "\r\n\r\nPOST ";
120     char        msg2[] = " test passed!";
121     char        msg3[] = " test failed!";
123     msg = strcat(msg1, post_status[test_id]);
124     switch (test_id)
125     {
126     case POST_TEST_IN_PROGRESS:
127     case POST_TEST_COMPLETE:
128         /* Form the POST status message to write to the UART */
129         if (post_write_uart(msg) != TRUE)
130         {
131             post_display_led_error(POST_TEST_UART);   /* Never return from this function */
132         }
134         for (i = 0; i < POST_MAX_NUM_LED; i++)
135         {
136             if (platform_led(i, (PLATFORM_LED_OP)post_led_status[test_id][i], PLATFORM_USER_LED_CLASS) != Platform_EOK)
137             {
138                 post_write_uart("POST LED test failed \r\n");
139             }
140         }
141         break;
143     default:
144         /* Form the POST status message to write to the UART */
145         if (test_passed)
146         {
147             msg = strcat(msg, msg2);
148             if (post_write_uart(msg) != TRUE)
149             {
150                 post_display_led_error(POST_TEST_UART);   /* Never return from this function */
151             }
152         }
153         else
154         {
155             msg = strcat(msg, msg3);
156             if (post_write_uart(msg) != TRUE)
157             {
158                 post_display_led_error(POST_TEST_UART);   /* Never return from this function */
159             }      
160             post_display_led_error(test_id);  /* Never return from this function */
161         }
162         break;
163     }
166 /******************************************************************************
167  * Function:    post_test_external_memory  
168  ******************************************************************************/
169 Bool 
170 post_test_external_memory
172     void
175     Bool    test_passed = TRUE;
177     if(platform_external_memory_test(0, 0) != Platform_EOK) 
178     {
179         test_passed = FALSE;
180     }
182     return test_passed;
185 /******************************************************************************
186  * Function:    post_test_eeprom  
187  ******************************************************************************/
188 Bool 
189 post_test_eeprom
191     void
194     uint8_t                 test_buf[POST_EEPROM_TEST_READ_LENGTH];
195     Bool                    test_passed = TRUE;
196     PLATFORM_DEVICE_info    *p_device;
198     p_device = platform_device_open(POST_EEPROM_TEST_DEVICE_ID, 0);
199     if (p_device == NULL) 
200     {
201         return FALSE;
202     }
204     if(platform_device_read(p_device->handle, 
205                             POST_EEPROM_TEST_READ_ADDRESS, 
206                             test_buf, 
207                             POST_EEPROM_TEST_READ_LENGTH) != Platform_EOK) 
208     {
209         test_passed = FALSE;
210     }
212     platform_device_close(p_device->handle);
213     return test_passed;
216 /******************************************************************************
217  * Function:    post_test_nand  
218  ******************************************************************************/
219 Bool 
220 post_test_nand
222     void
225     uint8_t                 test_buf[POST_NAND_TEST_READ_LENGTH];
226     Bool                    test_passed = TRUE;
227     uint32_t                addr;
228     PLATFORM_DEVICE_info    *p_device;
230     p_device = platform_device_open(POST_NAND_TEST_DEVICE_ID, 0);
231     if (p_device == NULL) 
232     {
233         return FALSE;
234     }
236     addr = (POST_NAND_TEST_READ_BLOCK_NUM * p_device->page_count + POST_NAND_TEST_READ_PAGE_NUM) * p_device->page_size;
237     if(platform_device_read(p_device->handle, 
238                             addr, 
239                             test_buf, 
240                             POST_NAND_TEST_READ_LENGTH) != Platform_EOK) 
241     {
242         test_passed = FALSE;
243     }
245     platform_device_close(p_device->handle);
246     return test_passed;
249 /******************************************************************************
250  * Function:    post_test_nor 
251  ******************************************************************************/
252 Bool 
253 post_test_nor
255     void
258     uint8_t                 test_buf[POST_NOR_TEST_READ_LENGTH];
259     Bool                    test_passed = TRUE;
260     PLATFORM_DEVICE_info    *p_device;
262     p_device = platform_device_open(POST_NOR_TEST_DEVICE_ID, 0);
263     if (p_device == NULL) 
264     {
265         return FALSE;
266     }
268     if(platform_device_read(p_device->handle, 
269                             POST_NOR_TEST_READ_ADDR, 
270                             test_buf, 
271                             POST_NOR_TEST_READ_LENGTH) != Platform_EOK) 
272     {
273         test_passed = FALSE;
274     }
276     platform_device_close(p_device->handle);
277     return test_passed;
280 /******************************************************************************
281  * Function:    main function for POST
282  ******************************************************************************/
283 void 
284 main
286     void
289     platform_init_flags     init_flags;
290     platform_init_config    init_config;
291     POST_TEST_ID            test_id = POST_TEST_IN_PROGRESS;
292     Bool                    test_passed = TRUE;
293     uint32_t                reset_type;
294     int32_t                 i;
295     char                    msg[9];
296     uint8_t                 mac_addr[6];
298     /* Turn on all the platform initialize flags */
299     memset(&init_config, 0, sizeof(platform_init_config));
300     memset(&init_flags, 0x01, sizeof(platform_init_flags));
302     /* Initialize the platform */
303     if (platform_init(&init_flags, &init_config) != Platform_EOK)
304     {
305         switch (platform_errno)
306         {
307         case PLATFORM_ERRNO_PLL_SETUP:
308             test_id = POST_TEST_PLL_INIT;
309             break;
310         case PLATFORM_ERRNO_NAND:
311             test_id = POST_TEST_NAND_INIT;
312             break;
313         case PLATFORM_ERRNO_NOR:
314             test_id = POST_TEST_NOR_INIT;
315             break;
316         default:
317             test_id = POST_TEST_GENERAL;
318             break;
319         }
320         test_passed = FALSE;
321     }
323     platform_uart_init();
324     platform_uart_set_baudrate(POST_UART_BAUDRATE);
325     if (test_id != POST_TEST_PLL_INIT)
326     {
327         if (post_write_uart(POST_DEVICE) != TRUE)
328         {
329             post_display_led_error(POST_TEST_UART);   /* Never return from this function */
330         }
331         post_write_uart(POST_EVM_VERSION_MSG);
332         post_write_uart(post_version);
334         /*post_write_uart("\r\n\r\nPOST booting from I2C 0x50 ... ");*/
336         platform_get_macaddr(PLATFORM_MAC_TYPE_EFUSE, mac_addr);
337         msg[2] = ' ';
338         msg[3] = 0;
339         post_write_uart("\r\n\r\nEFUSE MAC ID is: ");
340         for (i = 0; i < 6; i++)
341         {
342             msg[0] = (char)(mac_addr[i]>>4);
343             if (msg[0] < 0xa)
344             {
345                 msg[0] += '0';
346             }
347             else
348             {
349                 msg[0] = msg[0] - 0xa + 'A';
350             }
351             msg[1] = (char)(mac_addr[i]&0xf);
352             if (msg[1] < 0xa)
353             {
354                 msg[1] += '0';
355             }
356             else
357             {
358                 msg[1] = msg[1] - 0xa + 'A';
359             }
360             post_write_uart(msg);
361         }
362     }
364     /* Display test in progress UART/LED status or init error */
365     post_display_status(test_id, test_passed);
367     test_passed = post_test_external_memory();
368     post_display_status(POST_TEST_DDR, test_passed);
370     test_passed = post_test_eeprom();
371     post_display_status(POST_TEST_EEPROM, test_passed);
373     test_passed = post_test_nand();
374     post_display_status(POST_TEST_NAND, test_passed);
376     test_passed = post_test_nor();
377     post_display_status(POST_TEST_NOR, test_passed);
379     post_display_status(POST_TEST_COMPLETE, TRUE);
381     /* Read the PLL Reset Type Status register and display on UART */
382     reset_type = PLL_CTRL_REG_RSTYPE;
383     for (i = 7; i >= 0; i--)
384     {
385         msg[i] = (char)(reset_type & 0xf) + '0';
386         reset_type = reset_type >> 4;
387     }
388     msg[8] = 0;
389     post_write_uart("\r\n\r\nPLL Reset Type Status Register: 0x");
390     post_write_uart(msg);