blob: 486105f5edb9eb6ef535ec1f1912f47159f10636 [file] [log] [blame]
wdenk15ef8a52003-06-18 20:22:24 +00001/*
2 * (C) Copyright 2003
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17 * MA 02111-1307 USA
18 */
19
20/*
21 * Date & Time support for the DS12887 RTC
22 */
23
24#undef RTC_DEBUG
25
26#include <common.h>
27#include <command.h>
28#include <config.h>
29#include <rtc.h>
30
Michal Simek871c18d2008-07-14 19:45:37 +020031#if defined(CONFIG_CMD_DATE)
wdenk15ef8a52003-06-18 20:22:24 +000032
33#define RTC_SECONDS 0x00
34#define RTC_SECONDS_ALARM 0x01
35#define RTC_MINUTES 0x02
36#define RTC_MINUTES_ALARM 0x03
37#define RTC_HOURS 0x04
Wolfgang Denk53677ef2008-05-20 16:00:29 +020038#define RTC_HOURS_ALARM 0x05
39#define RTC_DAY_OF_WEEK 0x06
wdenk15ef8a52003-06-18 20:22:24 +000040#define RTC_DATE_OF_MONTH 0x07
41#define RTC_MONTH 0x08
42#define RTC_YEAR 0x09
Wolfgang Denk53677ef2008-05-20 16:00:29 +020043#define RTC_CONTROL_A 0x0A
44#define RTC_CONTROL_B 0x0B
45#define RTC_CONTROL_C 0x0C
wdenk15ef8a52003-06-18 20:22:24 +000046#define RTC_CONTROL_D 0x0D
47
wdenk993cad92003-06-26 22:04:09 +000048#define RTC_CA_UIP 0x80
wdenk15ef8a52003-06-18 20:22:24 +000049#define RTC_CB_DM 0x04
50#define RTC_CB_24_12 0x02
51#define RTC_CB_SET 0x80
52
53#if defined(CONFIG_ATC)
54
55static uchar rtc_read (uchar reg)
56{
57 uchar val;
wdenk993cad92003-06-26 22:04:09 +000058
wdenk8bde7f72003-06-27 21:31:46 +000059 *(volatile unsigned char*)(RTC_PORT_ADDR) = reg;
wdenk15ef8a52003-06-18 20:22:24 +000060 __asm__ __volatile__ ("sync");
wdenk993cad92003-06-26 22:04:09 +000061
wdenk15ef8a52003-06-18 20:22:24 +000062 val = *(volatile unsigned char*)(RTC_PORT_DATA);
63 return (val);
64}
65
66static void rtc_write (uchar reg, uchar val)
67{
wdenk8bde7f72003-06-27 21:31:46 +000068 *(volatile unsigned char*)(RTC_PORT_ADDR) = reg;
wdenk15ef8a52003-06-18 20:22:24 +000069 __asm__ __volatile__ ("sync");
70
71 *(volatile unsigned char*)(RTC_PORT_DATA) = val;
72 __asm__ __volatile__ ("sync");
73}
74
75#else
76# error Board specific rtc access functions should be supplied
77#endif
78
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030079int rtc_get (struct rtc_time *tmp)
wdenk15ef8a52003-06-18 20:22:24 +000080{
81 uchar sec, min, hour, mday, wday, mon, year;
82
83 /* check if rtc is available for access */
84 while( rtc_read(RTC_CONTROL_A) & RTC_CA_UIP)
85 ;
wdenk993cad92003-06-26 22:04:09 +000086
wdenk15ef8a52003-06-18 20:22:24 +000087 sec = rtc_read(RTC_SECONDS);
88 min = rtc_read(RTC_MINUTES);
89 hour = rtc_read(RTC_HOURS);
90 mday = rtc_read(RTC_DATE_OF_MONTH);
91 wday = rtc_read(RTC_DAY_OF_WEEK);
92 mon = rtc_read(RTC_MONTH);
93 year = rtc_read(RTC_YEAR);
94
95#ifdef RTC_DEBUG
96 printf( "Get RTC year: %d; mon: %d; mday: %d; wday: %d; "
97 "hr: %d; min: %d; sec: %d\n",
98 year, mon, mday, wday, hour, min, sec );
99
100 printf ( "Alarms: hour: %02x min: %02x sec: %02x\n",
101 rtc_read (RTC_HOURS_ALARM),
102 rtc_read (RTC_MINUTES_ALARM),
103 rtc_read (RTC_SECONDS_ALARM) );
104#endif
105
106 if( !(rtc_read(RTC_CONTROL_B) & RTC_CB_DM))
107 { /* Information is in BCD format */
108printf(" Get: Convert BSD to BIN\n");
109 tmp->tm_sec = bcd2bin (sec & 0x7F);
110 tmp->tm_min = bcd2bin (min & 0x7F);
111 tmp->tm_hour = bcd2bin (hour & 0x3F);
112 tmp->tm_mday = bcd2bin (mday & 0x3F);
113 tmp->tm_mon = bcd2bin (mon & 0x1F);
114 tmp->tm_year = bcd2bin (year);
115 tmp->tm_wday = bcd2bin (wday & 0x07);
116 }
117else
118 {
119 tmp->tm_sec = sec & 0x7F;
120 tmp->tm_min = min & 0x7F;
121 tmp->tm_hour = hour & 0x3F;
122 tmp->tm_mday = mday & 0x3F;
123 tmp->tm_mon = mon & 0x1F;
124 tmp->tm_year = year;
125 tmp->tm_wday = wday & 0x07;
126 }
127
128
129 if(tmp->tm_year<70)
130 tmp->tm_year+=2000;
131 else
132 tmp->tm_year+=1900;
133
134 tmp->tm_yday = 0;
135 tmp->tm_isdst= 0;
136#ifdef RTC_DEBUG
137 printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
138 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
139 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
140#endif
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300141
142 return 0;
wdenk15ef8a52003-06-18 20:22:24 +0000143}
144
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200145int rtc_set (struct rtc_time *tmp)
wdenk15ef8a52003-06-18 20:22:24 +0000146{
147 uchar save_ctrl_b;
148 uchar sec, min, hour, mday, wday, mon, year;
149
150#ifdef RTC_DEBUG
151 printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
152 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
153 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
154#endif
155
156 if( !(rtc_read(RTC_CONTROL_B) & RTC_CB_DM))
157 { /* Information is in BCD format */
158 year = bin2bcd(tmp->tm_year % 100);
159 mon = bin2bcd(tmp->tm_mon);
160 wday = bin2bcd(tmp->tm_wday);
161 mday = bin2bcd(tmp->tm_mday);
162 hour = bin2bcd(tmp->tm_hour);
163 min = bin2bcd(tmp->tm_min);
164 sec = bin2bcd(tmp->tm_sec);
165 }
166 else
167 {
168 year = tmp->tm_year % 100;
169 mon = tmp->tm_mon;
170 wday = tmp->tm_wday;
171 mday = tmp->tm_mday;
172 hour = tmp->tm_hour;
173 min = tmp->tm_min;
174 sec = tmp->tm_sec;
175 }
wdenk993cad92003-06-26 22:04:09 +0000176
wdenk15ef8a52003-06-18 20:22:24 +0000177 /* disables the RTC to update the regs */
178 save_ctrl_b = rtc_read(RTC_CONTROL_B);
179 save_ctrl_b |= RTC_CB_SET;
wdenk993cad92003-06-26 22:04:09 +0000180 rtc_write(RTC_CONTROL_B, save_ctrl_b);
wdenk15ef8a52003-06-18 20:22:24 +0000181
182 rtc_write (RTC_YEAR, year);
183 rtc_write (RTC_MONTH, mon);
184 rtc_write (RTC_DAY_OF_WEEK, wday);
185 rtc_write (RTC_DATE_OF_MONTH, mday);
186 rtc_write (RTC_HOURS, hour);
187 rtc_write (RTC_MINUTES, min);
188 rtc_write (RTC_SECONDS, sec);
189
190 /* enables the RTC to update the regs */
191 save_ctrl_b &= ~RTC_CB_SET;
wdenk993cad92003-06-26 22:04:09 +0000192 rtc_write(RTC_CONTROL_B, save_ctrl_b);
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200193
194 return 0;
wdenk15ef8a52003-06-18 20:22:24 +0000195}
196
197void rtc_reset (void)
198{
199 struct rtc_time tmp;
200 uchar ctrl_rg;
wdenk993cad92003-06-26 22:04:09 +0000201
wdenk15ef8a52003-06-18 20:22:24 +0000202 ctrl_rg = RTC_CB_SET;
wdenk993cad92003-06-26 22:04:09 +0000203 rtc_write(RTC_CONTROL_B,ctrl_rg);
204
wdenk15ef8a52003-06-18 20:22:24 +0000205 tmp.tm_year = 1970 % 100;
206 tmp.tm_mon = 1;
207 tmp.tm_mday= 1;
208 tmp.tm_hour = 0;
209 tmp.tm_min = 0;
210 tmp.tm_sec = 0;
wdenk993cad92003-06-26 22:04:09 +0000211
wdenk15ef8a52003-06-18 20:22:24 +0000212#ifdef RTC_DEBUG
wdenk8bde7f72003-06-27 21:31:46 +0000213 printf ( "RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
214 tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
wdenk15ef8a52003-06-18 20:22:24 +0000215 tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
216#endif
217
218 ctrl_rg = RTC_CB_SET | RTC_CB_24_12 | RTC_CB_DM;
wdenk993cad92003-06-26 22:04:09 +0000219 rtc_write(RTC_CONTROL_B,ctrl_rg);
wdenk8bde7f72003-06-27 21:31:46 +0000220 rtc_set(&tmp);
wdenk15ef8a52003-06-18 20:22:24 +0000221
222 rtc_write(RTC_HOURS_ALARM, 0),
wdenk993cad92003-06-26 22:04:09 +0000223 rtc_write(RTC_MINUTES_ALARM, 0),
wdenk15ef8a52003-06-18 20:22:24 +0000224 rtc_write(RTC_SECONDS_ALARM, 0);
wdenk993cad92003-06-26 22:04:09 +0000225
wdenk15ef8a52003-06-18 20:22:24 +0000226 ctrl_rg = RTC_CB_24_12 | RTC_CB_DM;
wdenk993cad92003-06-26 22:04:09 +0000227 rtc_write(RTC_CONTROL_B,ctrl_rg);
wdenk15ef8a52003-06-18 20:22:24 +0000228}
229
Jon Loeligera5938142007-07-09 18:10:50 -0500230#endif