d184fd89a981abd1e4ba8232c5fecb209355719e
[profile/mobile/platform/kernel/u-boot-tm1.git] / property / cmd_cboot.c
1 #include <config.h>
2 #include <common.h>
3 #include <command.h>
4 #include <linux/types.h>
5 #include <linux/keypad.h>
6 #include <linux/key_code.h>
7 #include <boot_mode.h>
8 #include <android_bootimg.h>
9 #include <asm/arch/gpio.h>
10 #include "tizen_misc.h"
11
12 #define COMMAND_MAX 128
13
14 #define DEBUG
15 #ifdef DEBUG
16 #define DBG(fmt...) printf(fmt)
17 #else
18 #define DBG(fmt...) 
19 #endif
20
21 extern int power_button_pressed(void);
22 extern int charger_connected(void);
23 extern int alarm_triggered(void);
24 extern int cali_file_check(void);
25 extern int get_mode_from_gpio();
26 unsigned check_reboot_mode(void);
27 //extern void CHG_LateInit(void);
28 //extern void CHG_LowBatChg(void);
29
30
31 int boot_pwr_check(void)
32 {
33     static int total_cnt = 0;
34     if(!power_button_pressed())
35       total_cnt ++;
36     return total_cnt;
37 }
38 #define mdelay(_ms) udelay(_ms*1000)
39
40 extern CBOOT_FUNC s_boot_func_array[CHECK_BOOTMODE_FUN_NUM] ;
41
42 static unsigned _reboot_mode_check(void)
43 {
44     static unsigned rst_mode = 0, check_times=0;
45     if(!check_times)
46     {
47         rst_mode = check_reboot_mode();
48         check_times++;
49     }
50     DBG("_reboot_mode_check rst_mode=0x%x\n",rst_mode);
51     return rst_mode;
52 }
53
54
55 // 0 get mode from calibration detect
56 boot_mode_enum_type get_mode_from_pctool (void){
57         int ret = CMD_NONE;
58     if(cali_file_check() && !boot_pwr_check())
59         {
60                 ret = pctool_mode_detect();
61                 if(ret < 0)
62                         return CMD_NONE;
63                 else
64                         return ret;
65     }
66         return CMD_NONE;
67 }
68
69 #ifndef CONFIG_MACH_CORI
70 #if !defined(CONFIG_KANAS_W) && !defined(CONFIG_KANAS_TD)
71 boot_mode_enum_type get_mode_from_bat_low (void){
72         while(is_bat_low()) {
73                 if(charger_connected()) {
74                         DBG("cboot:low battery,charging...\n");
75                         mdelay(200);
76                         //CHG_LowBatChg();
77                 }else{
78                         DBG("cboot:low battery and shutdown\n");
79                         return CMD_POWER_DOWN_DEVICE;
80                 }
81         }
82         if(charger_connected()){
83                 //CHG_LateInit();
84         }
85         return CMD_NONE;
86 }
87 #endif
88 #endif
89
90 #ifdef CONFIG_SPRD_SYSDUMP
91 boot_mode_enum_type write_sysdump_before_boot_extend (void){
92     unsigned rst_mode = _reboot_mode_check();
93     write_sysdump_before_boot(rst_mode);
94     return CMD_NONE;
95 }
96 #endif
97
98 // 1 get mode from file
99 boot_mode_enum_type get_mode_from_file_extend (void){
100     switch(get_mode_from_file()){
101         case RECOVERY_MODE:
102             DBG("cboot:get mode from file:recovery\n");
103             return CMD_RECOVERY_MODE;
104         default: 
105         return CMD_NONE;
106     }
107     return CMD_NONE;
108 }
109
110 // 2 get mode from watch dog
111 boot_mode_enum_type get_mode_from_watchdog (void){
112     unsigned rst_mode = _reboot_mode_check();
113     int flag;
114
115     switch(rst_mode){
116         case RECOVERY_MODE:
117             return CMD_RECOVERY_MODE;
118         case FASTBOOT_MODE:
119             return  CMD_FASTBOOT_MODE;
120         case NORMAL_MODE:
121             return CMD_NORMAL_MODE;
122         case WATCHDOG_REBOOT:
123             return CMD_WATCHDOG_REBOOT;
124         case UNKNOW_REBOOT_MODE:
125             return CMD_UNKNOW_REBOOT_MODE;
126         case PANIC_REBOOT:
127             return CMD_PANIC_REBOOT;
128         case AUTODLOADER_REBOOT:
129             return CMD_AUTODLOADER_REBOOT;
130         case SPECIAL_MODE:
131             return CMD_SPECIAL_MODE;
132         case EXT_RSTN_REBOOT_MODE:
133             return CMD_NORMAL_MODE;
134         case IQ_REBOOT_MODE:
135             return CMD_IQ_REBOOT_MODE;
136         case ALARM_MODE:
137         if((flag = alarm_flag_check())){
138             DBG("get_mode_from_watchdog flag=%d\n", flag);
139             if(flag == 1){
140                 return CMD_ALARM_MODE;
141             }
142             else if(flag == 2){
143                 return CMD_NORMAL_MODE;
144             }
145         }
146         default:
147             return CMD_NONE;
148     }
149 }
150
151 // 3 get mode from alarm register
152 boot_mode_enum_type  get_mode_from_alarm_register(void){
153     int flag;
154
155     if(alarm_triggered() && (flag = alarm_flag_check())){
156         DBG("get_mode_from_alarm_register flag=%d\n", flag);
157         if(flag == 1){
158             return CMD_ALARM_MODE;
159         }
160         else if(flag == 2){
161             return CMD_NORMAL_MODE;
162         }
163     }else{
164         return CMD_NONE;
165     }
166 }
167
168 // 4 get mode from charger
169 boot_mode_enum_type  get_mode_from_charger(void){
170     if(charger_connected()){
171         DBG("get mode from charger\n");
172         return CMD_CHARGE_MODE;
173     }else{
174         return CMD_NONE;
175     }
176 }
177     
178 // 5 get mode from keypad
179 boot_mode_enum_type  get_mode_from_keypad(void){
180     uint32_t key_mode = 0;
181     uint32_t key_code = 0;
182     volatile int i;
183
184     if(boot_pwr_check() >= get_pwr_key_cnt()){
185         mdelay(50);
186         DBG("cboot:power key is down");
187         for(i=0; i<10;i++){
188 #ifdef CONFIG_TIZEN
189                         key_code = tizen_check_keypad();
190 #else
191             key_code = board_key_scan();
192 #endif
193             DBG("cboot:key_scan times=%d\n",i);
194             if(key_code != KEY_RESERVED)
195                 break;
196         }
197 #ifdef CONFIG_TIZEN
198                 return key_code;
199 #endif
200         key_mode = check_key_boot(key_code);
201         DBG("cboot:get mode from keypad:0x%x\n",key_code);
202         switch(key_mode){
203             case BOOT_FASTBOOT:
204                 return  CMD_FASTBOOT_MODE;
205             case BOOT_RECOVERY:
206                 return CMD_RECOVERY_MODE;
207             case BOOT_CALIBRATE:
208                 return CMD_ENGTEST_MODE;
209             case BOOT_FACTORYTEST:
210                 return CMD_FACTORYTEST_MODE;
211                         case BOOT_THOR_DOWNLOAD:
212                                 return CMD_THOR_MODE;
213             default:
214                 return CMD_NORMAL_MODE;
215         }
216     }else{
217         return CMD_NONE;
218     }
219 }
220
221 // 6 get mode from gpio
222 boot_mode_enum_type  get_mode_from_gpio_extend(void){
223     if (get_mode_from_gpio()) {
224         DBG("pbint2 triggered, do normal mode\n");
225         return CMD_NORMAL_MODE;
226     }else{
227         return CMD_NONE;
228     }
229 }
230
231 #ifdef CONFIG_TIZEN
232 extern int tizen_reboot_check(void);
233 #endif
234
235 int do_cboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
236 {
237     volatile int i;
238     boot_mode_enum_type bootmode = CMD_NONE;
239     CBOOT_MODE_ENTRY boot_mode_array[20] ={0};
240
241     if(argc > 2)
242         goto usage;
243
244 #ifdef CONFIG_AUTOBOOT
245     normal_mode();
246 #endif
247     for (i=0;  i<CHECK_BOOTMODE_FUN_NUM; i++){
248         if (0 != s_boot_func_array[i]){
249             DBG("cboot: i=%d\n", i);
250             bootmode = s_boot_func_array[i]();
251             if(CMD_NONE == bootmode){
252                 boot_pwr_check();
253                 continue;
254             }else{
255                 break;
256             }
257         }
258 #ifndef CONFIG_TIZEN
259 // get mode from argument
260         else if(2 == argc){
261             DBG("cboot:get mode from argument:%s\n",argv[1]);
262             if(!strcmp(argv[1],"normal")){
263                 bootmode = CMD_NORMAL_MODE;
264                 break;
265             }
266             if(!strcmp(argv[1],"recovery")){
267                 bootmode = CMD_RECOVERY_MODE;
268                 break;
269             }
270             if(!strcmp(argv[1],"fastboot")){
271                 bootmode = CMD_FASTBOOT_MODE;
272                 break;
273             }
274             if(!strcmp(argv[1],"charge")){
275                 bootmode = CMD_CHARGE_MODE;
276                 break;
277             }
278         }else{
279             bootmode = CMD_POWER_DOWN_DEVICE;
280             break;
281         }
282 #endif
283     }
284     DBG("do_cboot:boot mode is %d\n",bootmode);
285
286 #ifdef CONFIG_TIZEN
287         if (bootmode == CMD_NONE || bootmode == CMD_CHARGE_MODE) {
288                 if (bootmode == CMD_NONE)
289                         bootmode = CMD_NORMAL_MODE;
290                 if (tizen_reboot_check()) {
291                         bootmode = CMD_THOR_MODE;
292                         DBG("do_cboot:boot mode is %d\n",bootmode);
293                 }
294
295         }
296 #endif
297
298 #ifdef CONFIG_LCD_LOGO
299         switch (bootmode) {
300         case CMD_THOR_MODE:
301                 draw_image(1);
302                 break;
303         case CMD_NORMAL_MODE:
304                 switch (check_pm_status()) {
305                 case PM_STATE_LPM:
306                         draw_image(2);
307                         break;
308                 case PM_STATE_NORMAL:
309                 default:
310                         draw_image(0);
311                 }
312                 break;
313         default:
314                 draw_image(0);
315         }
316         lcd_display();
317         MMU_DisableIDCM();
318 #endif /* CONFIG_LCD_LOGO */
319
320     cmd_mode_regist(boot_mode_array);
321     if((bootmode > CMD_POWER_DOWN_DEVICE) && (0 != boot_mode_array[bootmode])){
322         /* goto main_loop() if any input is hit from console */
323         if (tstc())
324                 return 0;
325         DBG("do_cboot: enter boot mode\n");
326         boot_mode_array[bootmode]();
327     }else{
328         DBG("do_cboot: power down device\n");
329         power_down_devices();
330         while(1);
331     }
332
333     usage:
334         cmd_usage(cmdtp);
335         return 1;
336 }
337
338 U_BOOT_CMD(
339             cboot, CONFIG_SYS_MAXARGS, 1, do_cboot,
340             "choose boot mode",
341             "mode: \nrecovery, fastboot, dloader, charge, normal, vlx, caliberation.\n"
342             "cboot could enter a mode specified by the mode descriptor.\n"
343             "it also could enter a proper mode automatically depending on "
344             "the environment\n"
345           );