]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/rtc/ftrtc010.c
7738a7acaa0c79054d67a835945fdf30a5810cef
[karo-tx-uboot.git] / drivers / rtc / ftrtc010.c
1 /*
2  * Faraday FTRTC010 Real Time Clock
3  *
4  * (C) Copyright 2009 Faraday Technology
5  * Po-Yu Chuang <ratbert@faraday-tech.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <config.h>
23 #include <common.h>
24 #include <rtc.h>
25 #include <asm/io.h>
26
27 struct ftrtc010 {
28         unsigned int sec;               /* 0x00 */
29         unsigned int min;               /* 0x04 */
30         unsigned int hour;              /* 0x08 */
31         unsigned int day;               /* 0x0c */
32         unsigned int alarm_sec;         /* 0x10 */
33         unsigned int alarm_min;         /* 0x14 */
34         unsigned int alarm_hour;        /* 0x18 */
35         unsigned int record;            /* 0x1c */
36         unsigned int cr;                /* 0x20 */
37 };
38
39 /*
40  * RTC Control Register
41  */
42 #define FTRTC010_CR_ENABLE              (1 << 0)
43 #define FTRTC010_CR_INTERRUPT_SEC       (1 << 1)        /* per second irq */
44 #define FTRTC010_CR_INTERRUPT_MIN       (1 << 2)        /* per minute irq */
45 #define FTRTC010_CR_INTERRUPT_HR        (1 << 3)        /* per hour   irq */
46 #define FTRTC010_CR_INTERRUPT_DAY       (1 << 4)        /* per day    irq */
47
48 static struct ftrtc010 *rtc = (struct ftrtc010 *)CONFIG_FTRTC010_BASE;
49
50 static void ftrtc010_enable(void)
51 {
52         writel(FTRTC010_CR_ENABLE, &rtc->cr);
53 }
54
55 /*
56  * return current time in seconds
57  */
58 static unsigned long ftrtc010_time(void)
59 {
60         unsigned long day;
61         unsigned long hour;
62         unsigned long minute;
63         unsigned long second;
64         unsigned long second2;
65
66         do {
67                 second  = readl(&rtc->sec);
68                 day     = readl(&rtc->day);
69                 hour    = readl(&rtc->hour);
70                 minute  = readl(&rtc->min);
71                 second2 = readl(&rtc->sec);
72         } while (second != second2);
73
74         return day * 24 * 60 * 60 + hour * 60 * 60 + minute * 60 + second;
75 }
76
77 /*
78  * Get the current time from the RTC
79  */
80
81 int rtc_get(struct rtc_time *tmp)
82 {
83         unsigned long now;
84
85         debug("%s(): record register: %x\n",
86               __func__, readl(&rtc->record));
87
88         now = ftrtc010_time() + readl(&rtc->record);
89
90         to_tm(now, tmp);
91
92         return 0;
93 }
94
95 /*
96  * Set the RTC
97  */
98 int rtc_set(struct rtc_time *tmp)
99 {
100         unsigned long new;
101         unsigned long now;
102
103         debug("%s(): DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
104               __func__,
105               tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
106               tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
107
108         new = mktime(tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_hour,
109                      tmp->tm_min, tmp->tm_sec);
110
111         now = ftrtc010_time();
112
113         debug("%s(): write %lx to record register\n", __func__, new - now);
114
115         writel(new - now, &rtc->record);
116
117         return 0;
118 }
119
120 void rtc_reset(void)
121 {
122         debug("%s()\n", __func__);
123         ftrtc010_enable();
124 }