blob: fa1178a36e9bf0ac5b3691e002d4cf90e2bfd683 [file] [log] [blame]
Tor Krill9536dfc2008-03-15 15:40:26 +01001/*
2 * (C) Copyright 2008
3 * Tor Krill, Excito Elektronik i Skåne , tor@excito.com
4 *
5 * Modelled after the ds1337 driver
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Tor Krill9536dfc2008-03-15 15:40:26 +01008 */
9
10/*
11 * Date & Time support (no alarms) for Intersil
12 * ISL1208 Real Time Clock (RTC).
13 */
14
15#include <common.h>
16#include <command.h>
Klaus Goger52280312018-03-19 20:32:05 +010017#include <dm.h>
Tor Krill9536dfc2008-03-15 15:40:26 +010018#include <rtc.h>
19#include <i2c.h>
20
21/*---------------------------------------------------------------------*/
22#ifdef DEBUG_RTC
23#define DEBUGR(fmt,args...) printf(fmt ,##args)
24#else
25#define DEBUGR(fmt,args...)
26#endif
27/*---------------------------------------------------------------------*/
28
29/*
30 * RTC register addresses
31 */
32
33#define RTC_SEC_REG_ADDR 0x0
34#define RTC_MIN_REG_ADDR 0x1
35#define RTC_HR_REG_ADDR 0x2
36#define RTC_DATE_REG_ADDR 0x3
37#define RTC_MON_REG_ADDR 0x4
38#define RTC_YR_REG_ADDR 0x5
39#define RTC_DAY_REG_ADDR 0x6
40#define RTC_STAT_REG_ADDR 0x7
41/*
42 * RTC control register bits
43 */
44
45/*
46 * RTC status register bits
47 */
48#define RTC_STAT_BIT_ARST 0x80 /* AUTO RESET ENABLE BIT */
49#define RTC_STAT_BIT_XTOSCB 0x40 /* CRYSTAL OSCILLATOR ENABLE BIT */
50#define RTC_STAT_BIT_WRTC 0x10 /* WRITE RTC ENABLE BIT */
51#define RTC_STAT_BIT_ALM 0x04 /* ALARM BIT */
52#define RTC_STAT_BIT_BAT 0x02 /* BATTERY BIT */
53#define RTC_STAT_BIT_RTCF 0x01 /* REAL TIME CLOCK FAIL BIT */
54
Tor Krill9536dfc2008-03-15 15:40:26 +010055/*
56 * Get the current time from the RTC
57 */
58
Klaus Goger52280312018-03-19 20:32:05 +010059static int isl1208_rtc_get(struct udevice *dev, struct rtc_time *tmp)
Tor Krill9536dfc2008-03-15 15:40:26 +010060{
Klaus Goger52280312018-03-19 20:32:05 +010061 int ret;
62 uchar buf[8], val;
Tor Krill9536dfc2008-03-15 15:40:26 +010063
Klaus Goger52280312018-03-19 20:32:05 +010064 ret = dm_i2c_read(dev, 0, buf, sizeof(buf));
65 if (ret < 0)
66 return ret;
Tor Krill9536dfc2008-03-15 15:40:26 +010067
Klaus Goger52280312018-03-19 20:32:05 +010068 if (buf[RTC_STAT_REG_ADDR] & RTC_STAT_BIT_RTCF) {
Tor Krill9536dfc2008-03-15 15:40:26 +010069 printf ("### Warning: RTC oscillator has stopped\n");
Klaus Goger52280312018-03-19 20:32:05 +010070 ret = dm_i2c_read(dev, RTC_STAT_REG_ADDR, &val, sizeof(val));
71 if (ret < 0)
72 return ret;
73
74 val = val & ~(RTC_STAT_BIT_BAT | RTC_STAT_BIT_RTCF);
75 ret = dm_i2c_write(dev, RTC_STAT_REG_ADDR, &val, sizeof(val));
76 if (ret < 0)
77 return ret;
Tor Krill9536dfc2008-03-15 15:40:26 +010078 }
79
Klaus Goger52280312018-03-19 20:32:05 +010080 tmp->tm_sec = bcd2bin(buf[RTC_SEC_REG_ADDR] & 0x7F);
81 tmp->tm_min = bcd2bin(buf[RTC_MIN_REG_ADDR] & 0x7F);
82 tmp->tm_hour = bcd2bin(buf[RTC_HR_REG_ADDR] & 0x3F);
83 tmp->tm_mday = bcd2bin(buf[RTC_DATE_REG_ADDR] & 0x3F);
84 tmp->tm_mon = bcd2bin(buf[RTC_MON_REG_ADDR] & 0x1F);
85 tmp->tm_year = bcd2bin(buf[RTC_YR_REG_ADDR]) + 2000;
86 tmp->tm_wday = bcd2bin(buf[RTC_DAY_REG_ADDR] & 0x07);
Tor Krill9536dfc2008-03-15 15:40:26 +010087 tmp->tm_yday = 0;
88 tmp->tm_isdst= 0;
89
90 DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
91 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
92 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030093
Klaus Goger52280312018-03-19 20:32:05 +010094 return 0;
Tor Krill9536dfc2008-03-15 15:40:26 +010095}
96
97/*
98 * Set the RTC
99 */
Klaus Goger52280312018-03-19 20:32:05 +0100100static int isl1208_rtc_set(struct udevice *dev, const struct rtc_time *tmp)
Tor Krill9536dfc2008-03-15 15:40:26 +0100101{
Klaus Goger52280312018-03-19 20:32:05 +0100102 int ret;
103 uchar val, buf[7];
104
Tor Krill9536dfc2008-03-15 15:40:26 +0100105 DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
106 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
107 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
108
Klaus Goger52280312018-03-19 20:32:05 +0100109 if (tmp->tm_year < 2000 || tmp->tm_year > 2099)
110 printf("WARNING: year should be between 2000 and 2099!\n");
Tor Krill9536dfc2008-03-15 15:40:26 +0100111
Klaus Goger52280312018-03-19 20:32:05 +0100112 /* enable write */
113 ret = dm_i2c_read(dev, RTC_STAT_REG_ADDR, &val, sizeof(val));
114 if (ret < 0)
115 return ret;
116
117 val = val | RTC_STAT_BIT_WRTC;
118
119 ret = dm_i2c_write(dev, RTC_STAT_REG_ADDR, &val, sizeof(val));
120 if (ret < 0)
121 return ret;
122
123 buf[RTC_YR_REG_ADDR] = bin2bcd(tmp->tm_year % 100);
124 buf[RTC_MON_REG_ADDR] = bin2bcd(tmp->tm_mon);
125 buf[RTC_DAY_REG_ADDR] = bin2bcd(tmp->tm_wday);
126 buf[RTC_DATE_REG_ADDR] = bin2bcd(tmp->tm_mday);
127 buf[RTC_HR_REG_ADDR] = bin2bcd(tmp->tm_hour) | 0x80; /* 24h clock */
128 buf[RTC_MIN_REG_ADDR] = bin2bcd(tmp->tm_min);
129 buf[RTC_SEC_REG_ADDR] = bin2bcd(tmp->tm_sec);
130
131 ret = dm_i2c_write(dev, 0, buf, sizeof(buf));
132 if (ret < 0)
133 return ret;
Tor Krill9536dfc2008-03-15 15:40:26 +0100134
135 /* disable write */
Klaus Goger52280312018-03-19 20:32:05 +0100136 ret = dm_i2c_read(dev, RTC_STAT_REG_ADDR, &val, sizeof(val));
137 if (ret < 0)
138 return ret;
139
140 val = val & ~RTC_STAT_BIT_WRTC;
141 ret = dm_i2c_write(dev, RTC_STAT_REG_ADDR, &val, sizeof(val));
142 if (ret < 0)
143 return ret;
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200144
145 return 0;
Tor Krill9536dfc2008-03-15 15:40:26 +0100146}
147
Klaus Goger52280312018-03-19 20:32:05 +0100148static int isl1208_rtc_reset(struct udevice *dev)
Tor Krill9536dfc2008-03-15 15:40:26 +0100149{
Klaus Goger52280312018-03-19 20:32:05 +0100150 return 0;
Tor Krill9536dfc2008-03-15 15:40:26 +0100151}
152
Klaus Goger52280312018-03-19 20:32:05 +0100153static int isl1208_probe(struct udevice *dev)
Tor Krill9536dfc2008-03-15 15:40:26 +0100154{
Klaus Goger52280312018-03-19 20:32:05 +0100155 i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS |
156 DM_I2C_CHIP_WR_ADDRESS);
157
158 return 0;
Tor Krill9536dfc2008-03-15 15:40:26 +0100159}
160
Klaus Goger52280312018-03-19 20:32:05 +0100161static const struct rtc_ops isl1208_rtc_ops = {
162 .get = isl1208_rtc_get,
163 .set = isl1208_rtc_set,
164 .reset = isl1208_rtc_reset,
165};
166
167static const struct udevice_id isl1208_rtc_ids[] = {
168 { .compatible = "isil,isl1208" },
169 { }
170};
171
172U_BOOT_DRIVER(rtc_isl1208) = {
173 .name = "rtc-isl1208",
174 .id = UCLASS_RTC,
175 .probe = isl1208_probe,
176 .of_match = isl1208_rtc_ids,
177 .ops = &isl1208_rtc_ops,
178};