logo: remove meaningless packed attributte
[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 void sprd_rtc_init(void);
20 extern void sprd_rtc_set_alarm_sec(unsigned long secs);
21 extern unsigned long sprd_rtc_get_alarm_sec(void);
22 extern unsigned long sprd_rtc_get_sec(void);
23 extern int sprd_is_poweroff_alarm(void);
24
25 int alarm_file_check(char *time_buf)
26 {
27         if(!do_fs_file_read(PROD_PART, "/alarm_flag", time_buf,200)){
28                 return 1;
29         }else{
30                 return -1;
31         }
32 }
33
34 int poweron_file_check(char *time_buf)
35 {
36         if(!do_fs_file_read(PROD_PART, "/poweron_timeinmillis", time_buf,200)){
37                 return 1;
38         }else{
39                 return -1;
40         }
41 }
42
43 void alarm_mode(void)
44 {
45     printf("%s\n", __func__);
46
47 #if BOOT_NATIVE_LINUX
48     vlx_nand_boot(BOOT_PART, CONFIG_BOOTARGS " androidboot.mode=alarm", BACKLIGHT_OFF);
49 #else
50     vlx_nand_boot(BOOT_PART, "androidboot.mode=alarm", BACKLIGHT_OFF);
51 #endif
52 }
53
54
55 static void uboot_rtc_set_alarm_sec(unsigned long secs)
56 {
57    int i=0;
58    unsigned long read_secs;
59    sprd_rtc_set_alarm_sec(secs);
60    msleep(150);
61    do {
62       if(i!=0){
63            sprd_rtc_set_alarm_sec(secs);
64             msleep(150);
65        }
66       read_secs = sprd_rtc_get_alarm_sec();
67       msleep(1);
68       i++;
69    }while(read_secs != secs && i < 100);
70 }
71
72 #if 0
73 int alarm_flag_check(void)
74 {
75         if (sprd_is_poweroff_alarm())
76                 return 1;
77         else
78                 return 2;
79 }
80 #endif
81
82 int alarm_flag_check(void)
83 {
84         int ret = -1,ret1 = -1;
85         long time = 0;
86         long time1 = 0;
87         unsigned long time_rtc= 0;
88         unsigned long time_rtc1= 0;
89         unsigned long now_rtc = 0;
90         int time_lead = 0;
91         char time_buf[200]={0};
92         char time_buf1[200]={0};
93
94         memset(time_buf,0x0,200);
95         memset(time_buf1,0x0,200);
96
97         ret = alarm_file_check(time_buf);
98         if(ret > 0){
99                 printf("file: alarm_flag exist\n");
100                 printf("time get %s", time_buf);
101                 time = simple_strtol(time_buf, NULL, 10);
102                 time_rtc = time;
103         }
104         ret1 = poweron_file_check(time_buf1);
105         if(ret1 > 0){
106                 printf("file: poweron_timeinmillis exist\n");
107                 printf("time get %s", time_buf1);
108                 time1 = simple_strtol(time_buf1, NULL, 10);
109                 time_rtc1 = time1;
110         }
111         sprd_rtc_init();
112         now_rtc = sprd_rtc_get_sec();
113         printf("now rtc %lu\n", now_rtc);
114         time_lead = get_alarm_lead_set();
115         time = time - now_rtc;
116         time1 = time1 - now_rtc;
117         if((time < time_lead +180) && (time > time_lead -10))
118         {
119                 if((time1 < time_lead +180) && (time1 > time_lead -10)&& (time -time1>50))
120                         ret = 2;
121                 else
122                         ret = 1;
123         }
124         else if((time1 < time_lead +180) && (time1 > time_lead -10))
125                 ret =2;
126         else{
127                 if (ret == -1&& ret1== -1) {
128                         printf("file: all not found\n");
129                 }else if(ret1== -1){
130                         uboot_rtc_set_alarm_sec(time_rtc);
131                 }else if(ret== -1){
132                         uboot_rtc_set_alarm_sec(time_rtc1);
133                 }else{
134                         if(time_rtc1 > time_rtc){
135                                 uboot_rtc_set_alarm_sec(time_rtc);
136                         }else{
137                                 uboot_rtc_set_alarm_sec(time_rtc1);
138                         }
139                 }
140                 ret = 0;
141         }
142
143         return ret;
144 }