blob: d8a519b8fe4660625418a880b3d117e218907348 [file] [log] [blame]
wdenk15ef8a52003-06-18 20:22:24 +00001/*
2 * (C) Copyright 2003
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
wdenk15ef8a52003-06-18 20:22:24 +00005 */
6
7/*
8 * Date & Time support for the DS12887 RTC
9 */
10
11#undef RTC_DEBUG
12
13#include <common.h>
14#include <command.h>
15#include <config.h>
16#include <rtc.h>
17
Michal Simek871c18d2008-07-14 19:45:37 +020018#if defined(CONFIG_CMD_DATE)
wdenk15ef8a52003-06-18 20:22:24 +000019
20#define RTC_SECONDS 0x00
21#define RTC_SECONDS_ALARM 0x01
22#define RTC_MINUTES 0x02
23#define RTC_MINUTES_ALARM 0x03
24#define RTC_HOURS 0x04
Wolfgang Denk53677ef2008-05-20 16:00:29 +020025#define RTC_HOURS_ALARM 0x05
26#define RTC_DAY_OF_WEEK 0x06
wdenk15ef8a52003-06-18 20:22:24 +000027#define RTC_DATE_OF_MONTH 0x07
28#define RTC_MONTH 0x08
29#define RTC_YEAR 0x09
Wolfgang Denk53677ef2008-05-20 16:00:29 +020030#define RTC_CONTROL_A 0x0A
31#define RTC_CONTROL_B 0x0B
32#define RTC_CONTROL_C 0x0C
wdenk15ef8a52003-06-18 20:22:24 +000033#define RTC_CONTROL_D 0x0D
34
wdenk993cad92003-06-26 22:04:09 +000035#define RTC_CA_UIP 0x80
wdenk15ef8a52003-06-18 20:22:24 +000036#define RTC_CB_DM 0x04
37#define RTC_CB_24_12 0x02
38#define RTC_CB_SET 0x80
39
40#if defined(CONFIG_ATC)
41
42static uchar rtc_read (uchar reg)
43{
44 uchar val;
wdenk993cad92003-06-26 22:04:09 +000045
wdenk8bde7f72003-06-27 21:31:46 +000046 *(volatile unsigned char*)(RTC_PORT_ADDR) = reg;
wdenk15ef8a52003-06-18 20:22:24 +000047 __asm__ __volatile__ ("sync");
wdenk993cad92003-06-26 22:04:09 +000048
wdenk15ef8a52003-06-18 20:22:24 +000049 val = *(volatile unsigned char*)(RTC_PORT_DATA);
50 return (val);
51}
52
53static void rtc_write (uchar reg, uchar val)
54{
wdenk8bde7f72003-06-27 21:31:46 +000055 *(volatile unsigned char*)(RTC_PORT_ADDR) = reg;
wdenk15ef8a52003-06-18 20:22:24 +000056 __asm__ __volatile__ ("sync");
57
58 *(volatile unsigned char*)(RTC_PORT_DATA) = val;
59 __asm__ __volatile__ ("sync");
60}
61
62#else
63# error Board specific rtc access functions should be supplied
64#endif
65
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030066int rtc_get (struct rtc_time *tmp)
wdenk15ef8a52003-06-18 20:22:24 +000067{
68 uchar sec, min, hour, mday, wday, mon, year;
69
70 /* check if rtc is available for access */
71 while( rtc_read(RTC_CONTROL_A) & RTC_CA_UIP)
72 ;
wdenk993cad92003-06-26 22:04:09 +000073
wdenk15ef8a52003-06-18 20:22:24 +000074 sec = rtc_read(RTC_SECONDS);
75 min = rtc_read(RTC_MINUTES);
76 hour = rtc_read(RTC_HOURS);
77 mday = rtc_read(RTC_DATE_OF_MONTH);
78 wday = rtc_read(RTC_DAY_OF_WEEK);
79 mon = rtc_read(RTC_MONTH);
80 year = rtc_read(RTC_YEAR);
81
82#ifdef RTC_DEBUG
83 printf( "Get RTC year: %d; mon: %d; mday: %d; wday: %d; "
84 "hr: %d; min: %d; sec: %d\n",
85 year, mon, mday, wday, hour, min, sec );
86
87 printf ( "Alarms: hour: %02x min: %02x sec: %02x\n",
88 rtc_read (RTC_HOURS_ALARM),
89 rtc_read (RTC_MINUTES_ALARM),
90 rtc_read (RTC_SECONDS_ALARM) );
91#endif
92
93 if( !(rtc_read(RTC_CONTROL_B) & RTC_CB_DM))
94 { /* Information is in BCD format */
95printf(" Get: Convert BSD to BIN\n");
96 tmp->tm_sec = bcd2bin (sec & 0x7F);
97 tmp->tm_min = bcd2bin (min & 0x7F);
98 tmp->tm_hour = bcd2bin (hour & 0x3F);
99 tmp->tm_mday = bcd2bin (mday & 0x3F);
100 tmp->tm_mon = bcd2bin (mon & 0x1F);
101 tmp->tm_year = bcd2bin (year);
102 tmp->tm_wday = bcd2bin (wday & 0x07);
103 }
104else
105 {
106 tmp->tm_sec = sec & 0x7F;
107 tmp->tm_min = min & 0x7F;
108 tmp->tm_hour = hour & 0x3F;
109 tmp->tm_mday = mday & 0x3F;
110 tmp->tm_mon = mon & 0x1F;
111 tmp->tm_year = year;
112 tmp->tm_wday = wday & 0x07;
113 }
114
115
116 if(tmp->tm_year<70)
117 tmp->tm_year+=2000;
118 else
119 tmp->tm_year+=1900;
120
121 tmp->tm_yday = 0;
122 tmp->tm_isdst= 0;
123#ifdef RTC_DEBUG
124 printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
125 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
126 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
127#endif
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300128
129 return 0;
wdenk15ef8a52003-06-18 20:22:24 +0000130}
131
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200132int rtc_set (struct rtc_time *tmp)
wdenk15ef8a52003-06-18 20:22:24 +0000133{
134 uchar save_ctrl_b;
135 uchar sec, min, hour, mday, wday, mon, year;
136
137#ifdef RTC_DEBUG
138 printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
139 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
140 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
141#endif
142
143 if( !(rtc_read(RTC_CONTROL_B) & RTC_CB_DM))
144 { /* Information is in BCD format */
145 year = bin2bcd(tmp->tm_year % 100);
146 mon = bin2bcd(tmp->tm_mon);
147 wday = bin2bcd(tmp->tm_wday);
148 mday = bin2bcd(tmp->tm_mday);
149 hour = bin2bcd(tmp->tm_hour);
150 min = bin2bcd(tmp->tm_min);
151 sec = bin2bcd(tmp->tm_sec);
152 }
153 else
154 {
155 year = tmp->tm_year % 100;
156 mon = tmp->tm_mon;
157 wday = tmp->tm_wday;
158 mday = tmp->tm_mday;
159 hour = tmp->tm_hour;
160 min = tmp->tm_min;
161 sec = tmp->tm_sec;
162 }
wdenk993cad92003-06-26 22:04:09 +0000163
wdenk15ef8a52003-06-18 20:22:24 +0000164 /* disables the RTC to update the regs */
165 save_ctrl_b = rtc_read(RTC_CONTROL_B);
166 save_ctrl_b |= RTC_CB_SET;
wdenk993cad92003-06-26 22:04:09 +0000167 rtc_write(RTC_CONTROL_B, save_ctrl_b);
wdenk15ef8a52003-06-18 20:22:24 +0000168
169 rtc_write (RTC_YEAR, year);
170 rtc_write (RTC_MONTH, mon);
171 rtc_write (RTC_DAY_OF_WEEK, wday);
172 rtc_write (RTC_DATE_OF_MONTH, mday);
173 rtc_write (RTC_HOURS, hour);
174 rtc_write (RTC_MINUTES, min);
175 rtc_write (RTC_SECONDS, sec);
176
177 /* enables the RTC to update the regs */
178 save_ctrl_b &= ~RTC_CB_SET;
wdenk993cad92003-06-26 22:04:09 +0000179 rtc_write(RTC_CONTROL_B, save_ctrl_b);
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200180
181 return 0;
wdenk15ef8a52003-06-18 20:22:24 +0000182}
183
184void rtc_reset (void)
185{
186 struct rtc_time tmp;
187 uchar ctrl_rg;
wdenk993cad92003-06-26 22:04:09 +0000188
wdenk15ef8a52003-06-18 20:22:24 +0000189 ctrl_rg = RTC_CB_SET;
wdenk993cad92003-06-26 22:04:09 +0000190 rtc_write(RTC_CONTROL_B,ctrl_rg);
191
wdenk15ef8a52003-06-18 20:22:24 +0000192 tmp.tm_year = 1970 % 100;
193 tmp.tm_mon = 1;
194 tmp.tm_mday= 1;
195 tmp.tm_hour = 0;
196 tmp.tm_min = 0;
197 tmp.tm_sec = 0;
wdenk993cad92003-06-26 22:04:09 +0000198
wdenk15ef8a52003-06-18 20:22:24 +0000199#ifdef RTC_DEBUG
wdenk8bde7f72003-06-27 21:31:46 +0000200 printf ( "RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
201 tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
wdenk15ef8a52003-06-18 20:22:24 +0000202 tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
203#endif
204
205 ctrl_rg = RTC_CB_SET | RTC_CB_24_12 | RTC_CB_DM;
wdenk993cad92003-06-26 22:04:09 +0000206 rtc_write(RTC_CONTROL_B,ctrl_rg);
wdenk8bde7f72003-06-27 21:31:46 +0000207 rtc_set(&tmp);
wdenk15ef8a52003-06-18 20:22:24 +0000208
209 rtc_write(RTC_HOURS_ALARM, 0),
wdenk993cad92003-06-26 22:04:09 +0000210 rtc_write(RTC_MINUTES_ALARM, 0),
wdenk15ef8a52003-06-18 20:22:24 +0000211 rtc_write(RTC_SECONDS_ALARM, 0);
wdenk993cad92003-06-26 22:04:09 +0000212
wdenk15ef8a52003-06-18 20:22:24 +0000213 ctrl_rg = RTC_CB_24_12 | RTC_CB_DM;
wdenk993cad92003-06-26 22:04:09 +0000214 rtc_write(RTC_CONTROL_B,ctrl_rg);
wdenk15ef8a52003-06-18 20:22:24 +0000215}
216
Jon Loeligera5938142007-07-09 18:10:50 -0500217#endif