1 // SPDX-License-Identifier: GPL-2.0+
4 * Matthias Fuchs, esd gmbh, matthias.fuchs@esd-electronics.com.
8 * Epson RX8025 RTC driver.
16 #if defined(CONFIG_CMD_DATE)
18 /*---------------------------------------------------------------------*/
22 #define DEBUGR(fmt,args...) printf(fmt ,##args)
24 #define DEBUGR(fmt,args...)
26 /*---------------------------------------------------------------------*/
28 #ifndef CONFIG_SYS_I2C_RTC_ADDR
29 # define CONFIG_SYS_I2C_RTC_ADDR 0x32
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
43 #define RTC_CTL1_REG_ADDR 0x0e
44 #define RTC_CTL2_REG_ADDR 0x0f
47 * Control register 1 bits
49 #define RTC_CTL1_BIT_2412 0x20
52 * Control register 2 bits
54 #define RTC_CTL2_BIT_PON 0x10
55 #define RTC_CTL2_BIT_VDET 0x40
56 #define RTC_CTL2_BIT_XST 0x20
57 #define RTC_CTL2_BIT_VDSL 0x80
60 * Note: the RX8025 I2C RTC requires register
61 * reads and write to consist of a single bus
62 * cycle. It is not allowed to write the register
63 * address in a first cycle that is terminated by
64 * a STOP condition. The chips needs a 'restart'
65 * sequence (start sequence without a prior stop).
66 * This driver has been written for a 4xx board.
67 * U-Boot's 4xx i2c driver is currently not capable
68 * to generate such cycles to some work arounds
72 /* static uchar rtc_read (uchar reg); */
73 #define rtc_read(reg) buf[((reg) + 1) & 0xf]
75 static void rtc_write (uchar reg, uchar val);
78 * Get the current time from the RTC
80 int rtc_get (struct rtc_time *tmp)
83 uchar sec, min, hour, mday, wday, mon, year, ctl2;
86 if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 16))
87 printf("Error reading from RTC\n");
89 sec = rtc_read(RTC_SEC_REG_ADDR);
90 min = rtc_read(RTC_MIN_REG_ADDR);
91 hour = rtc_read(RTC_HR_REG_ADDR);
92 wday = rtc_read(RTC_DAY_REG_ADDR);
93 mday = rtc_read(RTC_DATE_REG_ADDR);
94 mon = rtc_read(RTC_MON_REG_ADDR);
95 year = rtc_read(RTC_YR_REG_ADDR);
97 DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
98 "hr: %02x min: %02x sec: %02x\n",
99 year, mon, mday, wday, hour, min, sec);
102 ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
103 if (ctl2 & RTC_CTL2_BIT_PON) {
104 printf("RTC: power-on detected\n");
108 if (ctl2 & RTC_CTL2_BIT_VDET) {
109 printf("RTC: voltage drop detected\n");
113 if (!(ctl2 & RTC_CTL2_BIT_XST)) {
114 printf("RTC: oscillator stop detected\n");
118 tmp->tm_sec = bcd2bin (sec & 0x7F);
119 tmp->tm_min = bcd2bin (min & 0x7F);
120 if (rtc_read(RTC_CTL1_REG_ADDR) & RTC_CTL1_BIT_2412)
121 tmp->tm_hour = bcd2bin (hour & 0x3F);
123 tmp->tm_hour = bcd2bin (hour & 0x1F) % 12 +
124 ((hour & 0x20) ? 12 : 0);
125 tmp->tm_mday = bcd2bin (mday & 0x3F);
126 tmp->tm_mon = bcd2bin (mon & 0x1F);
127 tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
128 tmp->tm_wday = bcd2bin (wday & 0x07);
132 DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
133 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
134 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
142 int rtc_set (struct rtc_time *tmp)
144 DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
145 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
146 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
148 if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
149 printf("WARNING: year should be between 1970 and 2069!\n");
151 rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
152 rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon));
153 rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday));
154 rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
155 rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour));
156 rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
157 rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
159 rtc_write (RTC_CTL1_REG_ADDR, RTC_CTL1_BIT_2412);
167 void rtc_reset (void)
172 if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 16))
173 printf("Error reading from RTC\n");
175 ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
176 ctl2 &= ~(RTC_CTL2_BIT_PON | RTC_CTL2_BIT_VDET);
177 ctl2 |= RTC_CTL2_BIT_XST | RTC_CTL2_BIT_VDSL;
178 rtc_write (RTC_CTL2_REG_ADDR, ctl2);
184 static void rtc_write (uchar reg, uchar val)
189 if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 2) != 0)
190 printf("Error writing to RTC\n");
194 #endif /* CONFIG_RTC_RX8025 && CONFIG_CMD_DATE */