1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2001, 2002, 2003
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Keith Outwater, keith_outwater@mvis.com`
6 * Steven Scholz, steven.scholz@imc-berlin.de
10 * Date & Time support (no alarms) for Dallas Semiconductor (now Maxim)
11 * DS1307 and DS1338/9 Real Time Clock (RTC).
33 * RTC register addresses
35 #define RTC_SEC_REG_ADDR 0x00
36 #define RTC_MIN_REG_ADDR 0x01
37 #define RTC_HR_REG_ADDR 0x02
38 #define RTC_DAY_REG_ADDR 0x03
39 #define RTC_DATE_REG_ADDR 0x04
40 #define RTC_MON_REG_ADDR 0x05
41 #define RTC_YR_REG_ADDR 0x06
42 #define RTC_CTL_REG_ADDR 0x07
44 #define RTC_SEC_BIT_CH 0x80 /* Clock Halt (in Register 0) */
46 /* DS1307-specific bits */
47 #define RTC_CTL_BIT_RS0 0x01 /* Rate select 0 */
48 #define RTC_CTL_BIT_RS1 0x02 /* Rate select 1 */
49 #define RTC_CTL_BIT_SQWE 0x10 /* Square Wave Enable */
50 #define RTC_CTL_BIT_OUT 0x80 /* Output Control */
52 /* DS1337-specific bits */
53 #define DS1337_CTL_BIT_RS1 0x08 /* Rate select 1 */
54 #define DS1337_CTL_BIT_RS2 0x10 /* Rate select 2 */
55 #define DS1337_CTL_BIT_EOSC 0x80 /* Enable Oscillator */
57 /* DS1340-specific bits */
58 #define DS1340_SEC_BIT_EOSC 0x80 /* Enable Oscillator */
59 #define DS1340_CTL_BIT_OUT 0x80 /* Output Control */
61 /* MCP7941X-specific bits */
62 #define MCP7941X_BIT_ST 0x80
63 #define MCP7941X_BIT_VBATEN 0x08
67 /*---------------------------------------------------------------------*/
71 #define DEBUGR(fmt, args...) printf(fmt, ##args)
73 #define DEBUGR(fmt, args...)
75 /*---------------------------------------------------------------------*/
77 #ifndef CONFIG_SYS_I2C_RTC_ADDR
78 # define CONFIG_SYS_I2C_RTC_ADDR 0x68
81 #if defined(CONFIG_RTC_DS1307) && (CONFIG_SYS_I2C_SPEED > 100000)
82 # error The DS1307 is specified only up to 100kHz!
85 static uchar rtc_read (uchar reg);
86 static void rtc_write (uchar reg, uchar val);
89 * Get the current time from the RTC
91 int rtc_get (struct rtc_time *tmp)
94 uchar sec, min, hour, mday, wday, mon, year;
96 #ifdef CONFIG_RTC_MCP79411
99 sec = rtc_read (RTC_SEC_REG_ADDR);
100 min = rtc_read (RTC_MIN_REG_ADDR);
101 hour = rtc_read (RTC_HR_REG_ADDR);
102 wday = rtc_read (RTC_DAY_REG_ADDR);
103 mday = rtc_read (RTC_DATE_REG_ADDR);
104 mon = rtc_read (RTC_MON_REG_ADDR);
105 year = rtc_read (RTC_YR_REG_ADDR);
107 DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
108 "hr: %02x min: %02x sec: %02x\n",
109 year, mon, mday, wday, hour, min, sec);
111 #ifdef CONFIG_RTC_DS1307
112 if (sec & RTC_SEC_BIT_CH) {
113 printf ("### Warning: RTC oscillator has stopped\n");
114 /* clear the CH flag */
115 rtc_write (RTC_SEC_REG_ADDR,
116 rtc_read (RTC_SEC_REG_ADDR) & ~RTC_SEC_BIT_CH);
121 #ifdef CONFIG_RTC_MCP79411
122 /* make sure that the backup battery is enabled */
123 if (!(wday & MCP7941X_BIT_VBATEN)) {
124 rtc_write(RTC_DAY_REG_ADDR,
125 wday | MCP7941X_BIT_VBATEN);
128 /* clock halted? turn it on, so clock can tick. */
129 if (!(sec & MCP7941X_BIT_ST)) {
130 rtc_write(RTC_SEC_REG_ADDR, MCP7941X_BIT_ST);
131 printf("Started RTC\n");
137 tmp->tm_sec = bcd2bin (sec & 0x7F);
138 tmp->tm_min = bcd2bin (min & 0x7F);
139 tmp->tm_hour = bcd2bin (hour & 0x3F);
140 tmp->tm_mday = bcd2bin (mday & 0x3F);
141 tmp->tm_mon = bcd2bin (mon & 0x1F);
142 tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
143 tmp->tm_wday = bcd2bin ((wday - 1) & 0x07);
147 DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
148 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
149 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
158 int rtc_set (struct rtc_time *tmp)
160 DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
161 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
162 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
164 if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
165 printf("WARNING: year should be between 1970 and 2069!\n");
167 rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
168 rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon));
169 #ifdef CONFIG_RTC_MCP79411
170 rtc_write (RTC_DAY_REG_ADDR,
171 bin2bcd (tmp->tm_wday + 1) | MCP7941X_BIT_VBATEN);
173 rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday + 1));
175 rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
176 rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour));
177 rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
178 #ifdef CONFIG_RTC_MCP79411
179 rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec) | MCP7941X_BIT_ST);
181 rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
189 * Reset the RTC. We setting the date back to 1970-01-01.
190 * We also enable the oscillator output on the SQW/OUT pin and program
191 * it for 32,768 Hz output. Note that according to the datasheet, turning
192 * on the square wave output increases the current drain on the backup
193 * battery to something between 480nA and 800nA.
195 void rtc_reset (void)
197 rtc_write (RTC_SEC_REG_ADDR, 0x00); /* clearing Clock Halt */
198 rtc_write (RTC_CTL_REG_ADDR, RTC_CTL_BIT_SQWE | RTC_CTL_BIT_RS1 | RTC_CTL_BIT_RS0);
207 uchar rtc_read (uchar reg)
209 return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg));
213 static void rtc_write (uchar reg, uchar val)
215 i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
218 #endif /* !CONFIG_DM_RTC */
221 static int ds1307_rtc_set(struct udevice *dev, const struct rtc_time *tm)
225 enum ds_type type = dev_get_driver_data(dev);
227 debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
228 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
229 tm->tm_hour, tm->tm_min, tm->tm_sec);
231 if (tm->tm_year < 1970 || tm->tm_year > 2069)
232 printf("WARNING: year should be between 1970 and 2069!\n");
234 buf[RTC_YR_REG_ADDR] = bin2bcd(tm->tm_year % 100);
235 buf[RTC_MON_REG_ADDR] = bin2bcd(tm->tm_mon);
236 buf[RTC_DAY_REG_ADDR] = bin2bcd(tm->tm_wday + 1);
237 buf[RTC_DATE_REG_ADDR] = bin2bcd(tm->tm_mday);
238 buf[RTC_HR_REG_ADDR] = bin2bcd(tm->tm_hour);
239 buf[RTC_MIN_REG_ADDR] = bin2bcd(tm->tm_min);
240 buf[RTC_SEC_REG_ADDR] = bin2bcd(tm->tm_sec);
242 if (type == mcp794xx) {
243 buf[RTC_DAY_REG_ADDR] |= MCP7941X_BIT_VBATEN;
244 buf[RTC_SEC_REG_ADDR] |= MCP7941X_BIT_ST;
247 ret = dm_i2c_write(dev, 0, buf, sizeof(buf));
254 static int ds1307_rtc_get(struct udevice *dev, struct rtc_time *tm)
258 enum ds_type type = dev_get_driver_data(dev);
261 ret = dm_i2c_read(dev, 0, buf, sizeof(buf));
265 if (type == ds_1307) {
266 if (buf[RTC_SEC_REG_ADDR] & RTC_SEC_BIT_CH) {
267 printf("### Warning: RTC oscillator has stopped\n");
268 /* clear the CH flag */
269 buf[RTC_SEC_REG_ADDR] &= ~RTC_SEC_BIT_CH;
270 dm_i2c_reg_write(dev, RTC_SEC_REG_ADDR,
271 buf[RTC_SEC_REG_ADDR]);
274 } else if (type == ds_1337) {
275 if (buf[RTC_CTL_REG_ADDR] & DS1337_CTL_BIT_EOSC) {
276 printf("### Warning: RTC oscillator has stopped\n");
277 /* clear the not oscillator enable (~EOSC) flag */
278 buf[RTC_CTL_REG_ADDR] &= ~DS1337_CTL_BIT_EOSC;
279 dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR,
280 buf[RTC_CTL_REG_ADDR]);
283 } else if (type == ds_1340) {
284 if (buf[RTC_SEC_REG_ADDR] & DS1340_SEC_BIT_EOSC) {
285 printf("### Warning: RTC oscillator has stopped\n");
286 /* clear the not oscillator enable (~EOSC) flag */
287 buf[RTC_SEC_REG_ADDR] &= ~DS1340_SEC_BIT_EOSC;
288 dm_i2c_reg_write(dev, RTC_SEC_REG_ADDR,
289 buf[RTC_SEC_REG_ADDR]);
292 } else if (type == m41t11) {
293 /* clock halted? turn it on, so clock can tick. */
294 if (buf[RTC_SEC_REG_ADDR] & RTC_SEC_BIT_CH) {
295 buf[RTC_SEC_REG_ADDR] &= ~RTC_SEC_BIT_CH;
296 dm_i2c_reg_write(dev, RTC_SEC_REG_ADDR,
298 dm_i2c_reg_write(dev, RTC_SEC_REG_ADDR,
299 buf[RTC_SEC_REG_ADDR]);
302 } else if (type == mcp794xx) {
303 /* make sure that the backup battery is enabled */
304 if (!(buf[RTC_DAY_REG_ADDR] & MCP7941X_BIT_VBATEN)) {
305 dm_i2c_reg_write(dev, RTC_DAY_REG_ADDR,
306 buf[RTC_DAY_REG_ADDR] |
307 MCP7941X_BIT_VBATEN);
310 /* clock halted? turn it on, so clock can tick. */
311 if (!(buf[RTC_SEC_REG_ADDR] & MCP7941X_BIT_ST)) {
312 dm_i2c_reg_write(dev, RTC_SEC_REG_ADDR,
314 printf("Started RTC\n");
319 tm->tm_sec = bcd2bin(buf[RTC_SEC_REG_ADDR] & 0x7F);
320 tm->tm_min = bcd2bin(buf[RTC_MIN_REG_ADDR] & 0x7F);
321 tm->tm_hour = bcd2bin(buf[RTC_HR_REG_ADDR] & 0x3F);
322 tm->tm_mday = bcd2bin(buf[RTC_DATE_REG_ADDR] & 0x3F);
323 tm->tm_mon = bcd2bin(buf[RTC_MON_REG_ADDR] & 0x1F);
324 tm->tm_year = bcd2bin(buf[RTC_YR_REG_ADDR]) +
325 (bcd2bin(buf[RTC_YR_REG_ADDR]) >= 70 ?
327 tm->tm_wday = bcd2bin((buf[RTC_DAY_REG_ADDR] - 1) & 0x07);
331 debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
332 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
333 tm->tm_hour, tm->tm_min, tm->tm_sec);
338 static int ds1307_rtc_reset(struct udevice *dev)
341 enum ds_type type = dev_get_driver_data(dev);
344 * reset clock/oscillator in the seconds register:
345 * on DS1307 bit 7 enables Clock Halt (CH),
346 * on DS1340 bit 7 disables the oscillator (not EOSC)
347 * on MCP794xx bit 7 enables Start Oscillator (ST)
349 ret = dm_i2c_reg_write(dev, RTC_SEC_REG_ADDR, 0x00);
353 if (type == ds_1307) {
354 /* Write control register in order to enable square-wave
355 * output (SQWE) and set a default rate of 32.768kHz (RS1|RS0).
357 ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR,
358 RTC_CTL_BIT_SQWE | RTC_CTL_BIT_RS1 |
360 } else if (type == ds_1337) {
361 /* Write control register in order to enable oscillator output
362 * (not EOSC) and set a default rate of 32.768kHz (RS2|RS1).
364 ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR,
365 DS1337_CTL_BIT_RS2 | DS1337_CTL_BIT_RS1);
366 } else if (type == ds_1340 || type == mcp794xx || type == m41t11) {
367 /* Reset clock calibration, frequency test and output level. */
368 ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR, 0x00);
374 static int ds1307_probe(struct udevice *dev)
376 i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS |
377 DM_I2C_CHIP_WR_ADDRESS);
382 static const struct rtc_ops ds1307_rtc_ops = {
383 .get = ds1307_rtc_get,
384 .set = ds1307_rtc_set,
385 .reset = ds1307_rtc_reset,
388 static const struct udevice_id ds1307_rtc_ids[] = {
389 { .compatible = "dallas,ds1307", .data = ds_1307 },
390 { .compatible = "dallas,ds1337", .data = ds_1337 },
391 { .compatible = "dallas,ds1339", .data = ds_1339 },
392 { .compatible = "dallas,ds1340", .data = ds_1340 },
393 { .compatible = "microchip,mcp7941x", .data = mcp794xx },
394 { .compatible = "st,m41t11", .data = m41t11 },
398 U_BOOT_DRIVER(rtc_ds1307) = {
399 .name = "rtc-ds1307",
401 .probe = ds1307_probe,
402 .of_match = ds1307_rtc_ids,
403 .ops = &ds1307_rtc_ops,
405 #endif /* CONFIG_DM_RTC */