tizen: bootmode: enable uart console with home key
[profile/mobile/platform/kernel/u-boot-tm1.git] / property / alarm_mode.c
1 #include <config.h>
2 #include <common.h>
3 #include <linux/types.h>
4 #include <asm/arch/bits.h>
5 #include <linux/string.h>
6 #include <android_bootimg.h>
7 #include <linux/mtd/mtd.h>
8 #include <linux/mtd/nand.h>
9 #include <nand.h>
10 #include <android_boot.h>
11 #include <environment.h>
12 #include <jffs2/jffs2.h>
13 #include <boot_mode.h>
14
15
16 #define msleep(a) udelay(a * 1000)
17 #define PROD_PART "prodnv"
18
19 extern int do_fs_file_read(char *mpart, char *filenm, void *buf, int len);
20 extern unsigned int get_alarm_lead_set(void);
21 extern void sprd_rtc_init(void);
22 extern void sprd_rtc_set_alarm_sec(unsigned long secs);
23 extern unsigned long sprd_rtc_get_alarm_sec(void);
24 extern unsigned long sprd_rtc_get_sec(void);
25 extern int sprd_is_poweroff_alarm(void);
26
27 int alarm_file_check(char *time_buf)
28 {
29         if(!do_fs_file_read(PROD_PART, "/alarm_flag", time_buf,200)){
30                 return 1;
31         }else{
32                 return -1;
33         }
34 }
35
36 int poweron_file_check(char *time_buf)
37 {
38         if(!do_fs_file_read(PROD_PART, "/poweron_timeinmillis", time_buf,200)){
39                 return 1;
40         }else{
41                 return -1;
42         }
43 }
44
45 void alarm_mode(void)
46 {
47     printf("%s\n", __func__);
48
49 #if BOOT_NATIVE_LINUX
50     vlx_nand_boot(BOOT_PART, CONFIG_BOOTARGS " androidboot.mode=alarm", BACKLIGHT_OFF);
51 #else
52     vlx_nand_boot(BOOT_PART, "androidboot.mode=alarm", BACKLIGHT_OFF);
53 #endif
54 }
55
56
57 static void uboot_rtc_set_alarm_sec(unsigned long secs)
58 {
59    int i=0;
60    unsigned long read_secs;
61    sprd_rtc_set_alarm_sec(secs);
62    msleep(150);
63    do {
64       if(i!=0){
65            sprd_rtc_set_alarm_sec(secs);
66             msleep(150);
67        }
68       read_secs = sprd_rtc_get_alarm_sec();
69       msleep(1);
70       i++;
71    }while(read_secs != secs && i < 100);
72 }
73
74 #if 0
75 int alarm_flag_check(void)
76 {
77         if (sprd_is_poweroff_alarm())
78                 return 1;
79         else
80                 return 2;
81 }
82 #endif
83
84 int alarm_flag_check(void)
85 {
86         int ret = -1,ret1 = -1;
87         long time = 0;
88         long time1 = 0;
89         unsigned long time_rtc= 0;
90         unsigned long time_rtc1= 0;
91         unsigned long now_rtc = 0;
92         int time_lead = 0;
93         char time_buf[200]={0};
94         char time_buf1[200]={0};
95
96         memset(time_buf,0x0,200);
97         memset(time_buf1,0x0,200);
98
99         ret = alarm_file_check(time_buf);
100         if(ret > 0){
101                 printf("file: alarm_flag exist\n");
102                 printf("time get %s", time_buf);
103                 time = simple_strtol(time_buf, NULL, 10);
104                 time_rtc = time;
105         }
106         ret1 = poweron_file_check(time_buf1);
107         if(ret1 > 0){
108                 printf("file: poweron_timeinmillis exist\n");
109                 printf("time get %s", time_buf1);
110                 time1 = simple_strtol(time_buf1, NULL, 10);
111                 time_rtc1 = time1;
112         }
113         sprd_rtc_init();
114         now_rtc = sprd_rtc_get_sec();
115         printf("now rtc %lu\n", now_rtc);
116         time_lead = get_alarm_lead_set();
117         time = time - now_rtc;
118         time1 = time1 - now_rtc;
119         if((time < time_lead +180) && (time > time_lead -10))
120         {
121                 if((time1 < time_lead +180) && (time1 > time_lead -10)&& (time -time1>50))
122                         ret = 2;
123                 else
124                         ret = 1;
125         }
126         else if((time1 < time_lead +180) && (time1 > time_lead -10))
127                 ret =2;
128         else{
129                 if (ret == -1&& ret1== -1) {
130                         printf("file: all not found\n");
131                 }else if(ret1== -1){
132                         uboot_rtc_set_alarm_sec(time_rtc);
133                 }else if(ret== -1){
134                         uboot_rtc_set_alarm_sec(time_rtc1);
135                 }else{
136                         if(time_rtc1 > time_rtc){
137                                 uboot_rtc_set_alarm_sec(time_rtc);
138                         }else{
139                                 uboot_rtc_set_alarm_sec(time_rtc1);
140                         }
141                 }
142                 ret = 0;
143         }
144
145         return ret;
146 }