blob: b20f193b418c05a8e4e82da1540c415aed583676 [file] [log] [blame]
wdenk1cb8e982003-03-06 21:55:29 +00001/*
2 * (C) Copyright 2001, 2002, 2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 * Keith Outwater, keith_outwater@mvis.com`
5 * Steven Scholz, steven.scholz@imc-berlin.de
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26/*
27 * Date & Time support (no alarms) for Dallas Semiconductor (now Maxim)
wdenk7f70e852003-05-20 14:25:27 +000028 * DS1307 and DS1338 Real Time Clock (RTC).
wdenk1cb8e982003-03-06 21:55:29 +000029 *
30 * based on ds1337.c
31 */
32
33#include <common.h>
34#include <command.h>
35#include <rtc.h>
36#include <i2c.h>
37
wdenk7f70e852003-05-20 14:25:27 +000038#if (defined(CONFIG_RTC_DS1307) || defined(CONFIG_RTC_DS1338) ) && \
Jon Loeligera5938142007-07-09 18:10:50 -050039 defined(CONFIG_CMD_DATE)
wdenk1cb8e982003-03-06 21:55:29 +000040
41/*---------------------------------------------------------------------*/
42#undef DEBUG_RTC
43
44#ifdef DEBUG_RTC
45#define DEBUGR(fmt,args...) printf(fmt ,##args)
46#else
47#define DEBUGR(fmt,args...)
48#endif
49/*---------------------------------------------------------------------*/
50
51#ifndef CFG_I2C_RTC_ADDR
52# define CFG_I2C_RTC_ADDR 0x68
53#endif
54
wdenk3d3befa2004-03-14 15:06:13 +000055#if defined(CONFIG_RTC_DS1307) && (CFG_I2C_SPEED > 100000)
wdenk1cb8e982003-03-06 21:55:29 +000056# error The DS1307 is specified only up to 100kHz!
57#endif
58
59/*
60 * RTC register addresses
61 */
62#define RTC_SEC_REG_ADDR 0x00
63#define RTC_MIN_REG_ADDR 0x01
64#define RTC_HR_REG_ADDR 0x02
65#define RTC_DAY_REG_ADDR 0x03
66#define RTC_DATE_REG_ADDR 0x04
67#define RTC_MON_REG_ADDR 0x05
68#define RTC_YR_REG_ADDR 0x06
69#define RTC_CTL_REG_ADDR 0x07
70
71#define RTC_SEC_BIT_CH 0x80 /* Clock Halt (in Register 0) */
72
73#define RTC_CTL_BIT_RS0 0x01 /* Rate select 0 */
74#define RTC_CTL_BIT_RS1 0x02 /* Rate select 1 */
75#define RTC_CTL_BIT_SQWE 0x10 /* Square Wave Enable */
76#define RTC_CTL_BIT_OUT 0x80 /* Output Control */
77
78static uchar rtc_read (uchar reg);
79static void rtc_write (uchar reg, uchar val);
80static uchar bin2bcd (unsigned int n);
81static unsigned bcd2bin (uchar c);
82
83/*
84 * Get the current time from the RTC
85 */
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030086int rtc_get (struct rtc_time *tmp)
wdenk1cb8e982003-03-06 21:55:29 +000087{
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030088 int rel = 0;
wdenk1cb8e982003-03-06 21:55:29 +000089 uchar sec, min, hour, mday, wday, mon, year;
90
91 sec = rtc_read (RTC_SEC_REG_ADDR);
92 min = rtc_read (RTC_MIN_REG_ADDR);
93 hour = rtc_read (RTC_HR_REG_ADDR);
94 wday = rtc_read (RTC_DAY_REG_ADDR);
95 mday = rtc_read (RTC_DATE_REG_ADDR);
96 mon = rtc_read (RTC_MON_REG_ADDR);
97 year = rtc_read (RTC_YR_REG_ADDR);
98
99 DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
100 "hr: %02x min: %02x sec: %02x\n",
101 year, mon, mday, wday, hour, min, sec);
102
103 if (sec & RTC_SEC_BIT_CH) {
104 printf ("### Warning: RTC oscillator has stopped\n");
105 /* clear the CH flag */
106 rtc_write (RTC_SEC_REG_ADDR,
107 rtc_read (RTC_SEC_REG_ADDR) & ~RTC_SEC_BIT_CH);
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300108 rel = -1;
wdenk1cb8e982003-03-06 21:55:29 +0000109 }
wdenk8bde7f72003-06-27 21:31:46 +0000110
wdenk1cb8e982003-03-06 21:55:29 +0000111 tmp->tm_sec = bcd2bin (sec & 0x7F);
112 tmp->tm_min = bcd2bin (min & 0x7F);
113 tmp->tm_hour = bcd2bin (hour & 0x3F);
114 tmp->tm_mday = bcd2bin (mday & 0x3F);
115 tmp->tm_mon = bcd2bin (mon & 0x1F);
116 tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
117 tmp->tm_wday = bcd2bin ((wday - 1) & 0x07);
118 tmp->tm_yday = 0;
119 tmp->tm_isdst= 0;
120
121 DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
122 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
123 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300124
125 return rel;
wdenk1cb8e982003-03-06 21:55:29 +0000126}
127
128
129/*
130 * Set the RTC
131 */
132void rtc_set (struct rtc_time *tmp)
133{
134 DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
135 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
136 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
137
138 if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
139 printf("WARNING: year should be between 1970 and 2069!\n");
wdenk8bde7f72003-06-27 21:31:46 +0000140
wdenk1cb8e982003-03-06 21:55:29 +0000141 rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
142 rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon));
143 rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday + 1));
144 rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
145 rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour));
146 rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
147 rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
148}
149
150
151/*
wdenk8bde7f72003-06-27 21:31:46 +0000152 * Reset the RTC. We setting the date back to 1970-01-01.
153 * We also enable the oscillator output on the SQW/OUT pin and program
wdenk1cb8e982003-03-06 21:55:29 +0000154 * it for 32,768 Hz output. Note that according to the datasheet, turning
155 * on the square wave output increases the current drain on the backup
156 * battery to something between 480nA and 800nA.
157 */
158void rtc_reset (void)
159{
160 struct rtc_time tmp;
161
162 rtc_write (RTC_SEC_REG_ADDR, 0x00); /* clearing Clock Halt */
163 rtc_write (RTC_CTL_REG_ADDR, RTC_CTL_BIT_SQWE | RTC_CTL_BIT_RS1 | RTC_CTL_BIT_RS0);
164
165 tmp.tm_year = 1970;
166 tmp.tm_mon = 1;
167 tmp.tm_mday= 1;
168 tmp.tm_hour = 0;
169 tmp.tm_min = 0;
170 tmp.tm_sec = 0;
171
172 rtc_set(&tmp);
173
174 printf ( "RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
175 tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
176 tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
177
178 return;
179}
180
181
182/*
183 * Helper functions
184 */
185
186static
187uchar rtc_read (uchar reg)
188{
189 return (i2c_reg_read (CFG_I2C_RTC_ADDR, reg));
190}
191
192
193static void rtc_write (uchar reg, uchar val)
194{
195 i2c_reg_write (CFG_I2C_RTC_ADDR, reg, val);
196}
197
198static unsigned bcd2bin (uchar n)
199{
200 return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
201}
202
203static unsigned char bin2bcd (unsigned int n)
204{
205 return (((n / 10) << 4) | (n % 10));
206}
207
Jon Loeliger068b60a2007-07-10 10:27:39 -0500208#endif