2008-07-15 17:17:01 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by Maurus Cuelenaere
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Jz OnChip Real Time Clock interface for Linux
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "jz4740.h"
|
|
|
|
#include "rtc.h"
|
|
|
|
#include "timefuncs.h"
|
2009-02-13 00:45:49 +00:00
|
|
|
#include "logf.h"
|
2008-07-15 17:17:01 +00:00
|
|
|
|
2010-01-08 14:52:15 +00:00
|
|
|
/* Stolen from dietlibc-0.29/libugly/gmtime_r.c (GPLv2) */
|
|
|
|
#define SPD (24*60*60)
|
|
|
|
#define ISLEAP(year) (!(year%4) && ((year%100) || !(year%400)))
|
|
|
|
static void _localtime(const time_t t, struct tm *r)
|
2008-07-15 17:17:01 +00:00
|
|
|
{
|
2010-01-08 14:52:15 +00:00
|
|
|
time_t i;
|
|
|
|
register time_t work = t % SPD;
|
|
|
|
static int m_to_d[12] = /* This could be shared with mktime() */
|
|
|
|
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
|
|
|
|
|
|
|
|
r->tm_sec = work % 60;
|
|
|
|
work /= 60;
|
|
|
|
r->tm_min = work % 60;
|
|
|
|
r->tm_hour = work / 60;
|
|
|
|
work = t / SPD;
|
|
|
|
r->tm_wday = (4 + work) % 7;
|
|
|
|
|
|
|
|
for (i=1970; ; ++i)
|
|
|
|
{
|
|
|
|
register time_t k = ISLEAP(i) ? 366 : 365;
|
2008-07-15 17:17:01 +00:00
|
|
|
|
2010-01-08 14:52:15 +00:00
|
|
|
if (work >= k)
|
|
|
|
work -= k;
|
2008-07-15 17:17:01 +00:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2010-01-08 14:52:15 +00:00
|
|
|
|
|
|
|
r->tm_year = i - 1900;
|
|
|
|
r->tm_yday = work;
|
|
|
|
|
|
|
|
r->tm_mday = 1;
|
|
|
|
if (ISLEAP(i) && (work>58))
|
2008-07-15 17:17:01 +00:00
|
|
|
{
|
2010-01-08 14:52:15 +00:00
|
|
|
if (work==59)
|
|
|
|
r->tm_mday=2; /* 29.2. */
|
|
|
|
|
|
|
|
work-=1;
|
2008-07-15 17:17:01 +00:00
|
|
|
}
|
2010-01-08 14:52:15 +00:00
|
|
|
|
|
|
|
for (i=11; i && (m_to_d[i] > work); --i);
|
|
|
|
r->tm_mon = i;
|
|
|
|
r->tm_mday += work - m_to_d[i];
|
2008-07-15 17:17:01 +00:00
|
|
|
}
|
|
|
|
|
2009-09-26 14:58:32 +00:00
|
|
|
int rtc_read_datetime(struct tm *tm)
|
2008-07-15 17:17:01 +00:00
|
|
|
{
|
2010-01-08 14:52:15 +00:00
|
|
|
_localtime(REG_RTC_RSR, tm);
|
2009-09-26 14:58:32 +00:00
|
|
|
|
2008-07-15 17:17:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-09-26 14:58:32 +00:00
|
|
|
int rtc_write_datetime(const struct tm *tm)
|
2009-02-09 10:02:38 +00:00
|
|
|
{
|
2010-01-08 14:52:15 +00:00
|
|
|
time_t val = mktime((struct tm*)tm);
|
2009-05-06 19:45:33 +00:00
|
|
|
|
2009-02-26 22:58:10 +00:00
|
|
|
__cpm_start_rtc();
|
2009-05-06 19:45:33 +00:00
|
|
|
udelay(100);
|
2010-01-08 14:52:15 +00:00
|
|
|
REG_RTC_RSR = val;
|
2009-02-26 22:58:10 +00:00
|
|
|
__cpm_stop_rtc();
|
2009-02-09 10:02:38 +00:00
|
|
|
|
2010-01-08 14:52:15 +00:00
|
|
|
return 0;
|
2008-07-15 17:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_init(void)
|
|
|
|
{
|
2008-12-20 13:10:04 +00:00
|
|
|
__cpm_start_rtc();
|
2008-07-17 10:13:56 +00:00
|
|
|
REG_RTC_RCR = RTC_RCR_RTCE;
|
2008-07-15 17:17:01 +00:00
|
|
|
udelay(70);
|
2008-07-17 10:13:56 +00:00
|
|
|
while( !(REG_RTC_RCR & RTC_RCR_WRDY) );
|
|
|
|
REG_RTC_RGR = (0x7fff | RTC_RGR_LOCK);
|
2008-07-15 17:17:01 +00:00
|
|
|
udelay(70);
|
2008-12-20 13:10:04 +00:00
|
|
|
__cpm_stop_rtc();
|
2008-07-15 17:17:01 +00:00
|
|
|
}
|