summaryrefslogtreecommitdiffstats
path: root/firmware/drivers/rtc/rtc_as3514.c
blob: fe2921b433da772e2c18de27c6432112399b9892 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2007 by Barry Wardell
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdbool.h>
#include "config.h"
#include "rtc.h"
#include "as3514.h"
#include "ascodec.h"

/* AMS Sansas start counting from Jan 1st 1970 instead of 1980 (not as3525v2) */
#if (CONFIG_CPU==AS3525)
#define SECS_ADJUST 315532800   /* seconds between 1970-1-1 and 1980-1-1 */
#elif (CONFIG_CPU==AS3525v2)
#define SECS_ADJUST 315532800 - (2*365*24*3600) - 26*(24*3600) + 7*3600 + 25*60
#else
#define SECS_ADJUST 0
#endif

#define MINUTE_SECONDS      60
#define HOUR_SECONDS        3600
#define DAY_SECONDS         86400
#define WEEK_SECONDS        604800
#define YEAR_SECONDS        31536000
#define LEAP_YEAR_SECONDS   31622400

/* Days in each month */
static unsigned int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

static inline bool is_leapyear(int year)
{
    if( ((year%4)==0) && (((year%100)!=0) || ((year%400)==0)) )
        return true;
    else
        return false;
}

#ifdef HAVE_RTC_ALARM /* as3543 */
static int wakeup_h;
static int wakeup_m;
static bool alarm_enabled = false;

void rtc_set_alarm(int h, int m)
{
    wakeup_h = h;
    wakeup_m = m;
}

void rtc_get_alarm(int *h, int *m)
{
    *h = wakeup_h;
    *m = wakeup_m;
}

void rtc_alarm_poweroff(void)
{
    if(!alarm_enabled)
        return;

    struct tm tm;
    rtc_read_datetime(&tm);
    int hours = wakeup_h - tm.tm_hour;
    int mins  = wakeup_m - tm.tm_min;
    if(mins < 0)
    {
        mins += 60;
        hours -= 1;
    }
    if(hours < 0)
        hours += 24;

    uint32_t seconds = hours*3600 + mins*60;
    if(seconds == 0)
        seconds = 24*3600;

    seconds -= tm.tm_sec;

    disable_irq();

    ascodec_write_pmu(0x1a, 4, 0x0); // In_Cntr : disable hearbeat source

    ascodec_write(AS3543_WAKEUP, seconds);
    seconds >>= 8;
    ascodec_write(AS3543_WAKEUP, seconds);
    seconds >>= 8;
    seconds |= 1<<7; /* enable bit */
    ascodec_write(AS3543_WAKEUP, seconds);

    /* write our watermark : desired time of wake up */
    ascodec_write(AS3543_WAKEUP, wakeup_h);
    ascodec_write(AS3543_WAKEUP, wakeup_m);

    ascodec_write(AS3514_SYSTEM, (1<<3) | (1<<0)); // enable hearbeat watchdog

    while(1);
}

bool rtc_enable_alarm(bool enable)
{
    return alarm_enabled = enable;
}

bool rtc_check_alarm_started(bool release_alarm)
{
    (void) release_alarm;

    /* was it an alarm that triggered power on ? */
    bool alarm_start = false;

    /* 3 first reads give the 23 bits counter and enable bit */
    ascodec_read(AS3543_WAKEUP); /* bits  7:0  */
    ascodec_read(AS3543_WAKEUP); /* bits 15:8  */
    if(ascodec_read(AS3543_WAKEUP) & (1<<7)) /* enable bit */
    {
        alarm_enabled = true;

        /* subsequent reads give the 16 bytes static SRAM */
        wakeup_h = ascodec_read(AS3543_WAKEUP);
        wakeup_m = ascodec_read(AS3543_WAKEUP);

        struct tm tm;
        rtc_read_datetime(&tm);

        /* do we wake up at the programmed time, or for another reason ? */
        if(wakeup_h == tm.tm_hour && wakeup_m == tm.tm_min)
            alarm_start = true;
    }

    /* disable alarm */
    ascodec_write(AS3543_WAKEUP, 0); /* bits  7:0  */
    ascodec_write(AS3543_WAKEUP, 0); /* bits 15:8  */
    ascodec_write(AS3543_WAKEUP, 0); /* bits 22:16 + enable bit */

    return alarm_start;
}

bool rtc_check_alarm_flag(void)
{
    /* We don't need to do anything special if it has already fired */
    return false;
}
#endif /* HAVE_RTC_ALARM */

void rtc_init(void)
{
}

int rtc_read_datetime(struct tm *tm)
{
    char tmp[4];
    int i, year, mday, hour, min;
    unsigned int seconds;

    /* RTC_AS3514's slave address is 0x46*/
    for (i = 0; i < 4; i++){
        tmp[i] = ascodec_read(AS3514_RTC_0 + i);
    }
    seconds = tmp[0] + (tmp[1]<<8) + (tmp[2]<<16) + (tmp[3]<<24);
    seconds -= SECS_ADJUST;

    /* Convert seconds since Jan-1-1980 to format compatible with
     * get_time() from firmware/common/timefuncs.c */

    /* weekday */
    tm->tm_wday = ((seconds % WEEK_SECONDS) / DAY_SECONDS + 2) % 7;

    /* Year */
    year = 1980;
    while (seconds >= LEAP_YEAR_SECONDS)
    {
        if (is_leapyear(year)){
            seconds -= LEAP_YEAR_SECONDS;
        } else {
            seconds -= YEAR_SECONDS;
        }

        year++;
    }

    if (is_leapyear(year)) {
        days_in_month[1] = 29;
    } else {
        days_in_month[1] = 28;
        if(seconds>YEAR_SECONDS){
            year++;
            seconds -= YEAR_SECONDS;
        }
    }
    tm->tm_year = year%100 + 100;

    /* Month */
    for (i = 0; i < 12; i++)
    {
        if (seconds < days_in_month[i]*DAY_SECONDS){
            tm->tm_mon = i;
            break;
        }

        seconds -= days_in_month[i]*DAY_SECONDS;
    }

    /* Month Day */
    mday = seconds/DAY_SECONDS;
    seconds -= mday*DAY_SECONDS;
    tm->tm_mday = mday + 1; /* 1 ... 31 */

    /* Hour */
    hour = seconds/HOUR_SECONDS;
    seconds -= hour*HOUR_SECONDS;
    tm->tm_hour = hour;

    /* Minute */
    min = seconds/MINUTE_SECONDS;
    seconds -= min*MINUTE_SECONDS;
    tm->tm_min = min;

    /* Second */
    tm->tm_sec = seconds;

    return 7;
}

int rtc_write_datetime(const struct tm *tm)
{
    int i, year;
    unsigned int year_days = 0;
    unsigned int month_days = 0;
    unsigned int seconds = 0;

    year = 2000 + tm->tm_year - 100;

    if(is_leapyear(year)) {
        days_in_month[1] = 29;
    } else {
        days_in_month[1] = 28;
    }
    
    /* Number of days in months gone by this year*/
    for(i = 0; i < tm->tm_mon; i++){
        month_days += days_in_month[i];
    }
    
    /* Number of days in years gone by since 1-Jan-1980 */
    year_days = 365*(tm->tm_year-100+20) + (tm->tm_year-100-1)/4 + 6;

    /* Convert to seconds since 1-Jan-1980 */
    seconds = tm->tm_sec
            + tm->tm_min*MINUTE_SECONDS
            + tm->tm_hour*HOUR_SECONDS
            + (tm->tm_mday-1)*DAY_SECONDS
            + month_days*DAY_SECONDS
            + year_days*DAY_SECONDS;
    seconds += SECS_ADJUST;

    /* Send data to RTC */
    for (i=0; i<4; i++){
        ascodec_write(AS3514_RTC_0 + i, ((seconds >> (8 * i)) & 0xff));
    }
    return 1;
}