Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2007 |
| 4 | * Matthias Fuchs, esd gmbh, matthias.fuchs@esd-electronics.com. |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Epson RX8025 RTC driver. |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <command.h> |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 13 | #include <dm.h> |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 14 | #include <i2c.h> |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 15 | #include <rtc.h> |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 16 | |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 17 | /*---------------------------------------------------------------------*/ |
| 18 | #undef DEBUG_RTC |
| 19 | |
| 20 | #ifdef DEBUG_RTC |
| 21 | #define DEBUGR(fmt,args...) printf(fmt ,##args) |
| 22 | #else |
| 23 | #define DEBUGR(fmt,args...) |
| 24 | #endif |
| 25 | /*---------------------------------------------------------------------*/ |
| 26 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 27 | #ifndef CONFIG_SYS_I2C_RTC_ADDR |
| 28 | # define CONFIG_SYS_I2C_RTC_ADDR 0x32 |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 29 | #endif |
| 30 | |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 31 | #ifdef CONFIG_DM_RTC |
| 32 | #define DEV_TYPE struct udevice |
| 33 | #else |
| 34 | /* Local udevice */ |
| 35 | struct ludevice { |
| 36 | u8 chip; |
| 37 | }; |
| 38 | |
| 39 | #define DEV_TYPE struct ludevice |
| 40 | |
| 41 | #endif |
| 42 | |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 43 | /* |
| 44 | * RTC register addresses |
| 45 | */ |
| 46 | #define RTC_SEC_REG_ADDR 0x00 |
| 47 | #define RTC_MIN_REG_ADDR 0x01 |
| 48 | #define RTC_HR_REG_ADDR 0x02 |
| 49 | #define RTC_DAY_REG_ADDR 0x03 |
| 50 | #define RTC_DATE_REG_ADDR 0x04 |
| 51 | #define RTC_MON_REG_ADDR 0x05 |
| 52 | #define RTC_YR_REG_ADDR 0x06 |
| 53 | |
| 54 | #define RTC_CTL1_REG_ADDR 0x0e |
| 55 | #define RTC_CTL2_REG_ADDR 0x0f |
| 56 | |
| 57 | /* |
| 58 | * Control register 1 bits |
| 59 | */ |
| 60 | #define RTC_CTL1_BIT_2412 0x20 |
| 61 | |
| 62 | /* |
| 63 | * Control register 2 bits |
| 64 | */ |
| 65 | #define RTC_CTL2_BIT_PON 0x10 |
| 66 | #define RTC_CTL2_BIT_VDET 0x40 |
| 67 | #define RTC_CTL2_BIT_XST 0x20 |
| 68 | #define RTC_CTL2_BIT_VDSL 0x80 |
| 69 | |
| 70 | /* |
| 71 | * Note: the RX8025 I2C RTC requires register |
| 72 | * reads and write to consist of a single bus |
| 73 | * cycle. It is not allowed to write the register |
| 74 | * address in a first cycle that is terminated by |
| 75 | * a STOP condition. The chips needs a 'restart' |
| 76 | * sequence (start sequence without a prior stop). |
| 77 | * This driver has been written for a 4xx board. |
| 78 | * U-Boot's 4xx i2c driver is currently not capable |
| 79 | * to generate such cycles to some work arounds |
| 80 | * are used. |
| 81 | */ |
| 82 | |
| 83 | /* static uchar rtc_read (uchar reg); */ |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 84 | #ifdef CONFIG_DM_RTC |
| 85 | /* |
| 86 | * on mpc85xx based board with DM and offset len 1 |
| 87 | * accessing rtc works fine. May we can drop this ? |
| 88 | */ |
| 89 | #define rtc_read(reg) buf[(reg) & 0xf] |
| 90 | #else |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 91 | #define rtc_read(reg) buf[((reg) + 1) & 0xf] |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 92 | #endif |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 93 | |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 94 | static int rtc_write(DEV_TYPE *dev, uchar reg, uchar val); |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * Get the current time from the RTC |
| 98 | */ |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 99 | static int rx8025_rtc_get(DEV_TYPE *dev, struct rtc_time *tmp) |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 100 | { |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 101 | int rel = 0; |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 102 | uchar sec, min, hour, mday, wday, mon, year, ctl2; |
| 103 | uchar buf[16]; |
| 104 | |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 105 | #ifdef CONFIG_DM_RTC |
| 106 | if (dm_i2c_read(dev, 0, buf, sizeof(buf))) { |
| 107 | #else |
| 108 | if (i2c_read(dev->chip, 0, 0, buf, 16)) { |
| 109 | #endif |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 110 | printf("Error reading from RTC\n"); |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 111 | return -EIO; |
| 112 | } |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 113 | |
| 114 | sec = rtc_read(RTC_SEC_REG_ADDR); |
| 115 | min = rtc_read(RTC_MIN_REG_ADDR); |
| 116 | hour = rtc_read(RTC_HR_REG_ADDR); |
| 117 | wday = rtc_read(RTC_DAY_REG_ADDR); |
| 118 | mday = rtc_read(RTC_DATE_REG_ADDR); |
| 119 | mon = rtc_read(RTC_MON_REG_ADDR); |
| 120 | year = rtc_read(RTC_YR_REG_ADDR); |
| 121 | |
Heiko Schocher | 6b659d0 | 2019-07-16 05:31:34 +0200 | [diff] [blame] | 122 | DEBUGR("Get RTC year: %02x mon: %02x mday: %02x wday: %02x " |
| 123 | "hr: %02x min: %02x sec: %02x\n", |
| 124 | year, mon, mday, wday, hour, min, sec); |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 125 | |
| 126 | /* dump status */ |
| 127 | ctl2 = rtc_read(RTC_CTL2_REG_ADDR); |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 128 | if (ctl2 & RTC_CTL2_BIT_PON) { |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 129 | printf("RTC: power-on detected\n"); |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 130 | rel = -1; |
| 131 | } |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 132 | |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 133 | if (ctl2 & RTC_CTL2_BIT_VDET) { |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 134 | printf("RTC: voltage drop detected\n"); |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 135 | rel = -1; |
| 136 | } |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 137 | |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 138 | if (!(ctl2 & RTC_CTL2_BIT_XST)) { |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 139 | printf("RTC: oscillator stop detected\n"); |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 140 | rel = -1; |
| 141 | } |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 142 | |
Heiko Schocher | 6b659d0 | 2019-07-16 05:31:34 +0200 | [diff] [blame] | 143 | tmp->tm_sec = bcd2bin(sec & 0x7F); |
| 144 | tmp->tm_min = bcd2bin(min & 0x7F); |
Yuri Tikhonov | 62a3459 | 2008-08-15 15:42:09 +0200 | [diff] [blame] | 145 | if (rtc_read(RTC_CTL1_REG_ADDR) & RTC_CTL1_BIT_2412) |
Heiko Schocher | 6b659d0 | 2019-07-16 05:31:34 +0200 | [diff] [blame] | 146 | tmp->tm_hour = bcd2bin(hour & 0x3F); |
Yuri Tikhonov | 62a3459 | 2008-08-15 15:42:09 +0200 | [diff] [blame] | 147 | else |
Heiko Schocher | 6b659d0 | 2019-07-16 05:31:34 +0200 | [diff] [blame] | 148 | tmp->tm_hour = bcd2bin(hour & 0x1F) % 12 + |
Yuri Tikhonov | 62a3459 | 2008-08-15 15:42:09 +0200 | [diff] [blame] | 149 | ((hour & 0x20) ? 12 : 0); |
Heiko Schocher | 6b659d0 | 2019-07-16 05:31:34 +0200 | [diff] [blame] | 150 | |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 151 | tmp->tm_mday = bcd2bin (mday & 0x3F); |
| 152 | tmp->tm_mon = bcd2bin (mon & 0x1F); |
| 153 | tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000); |
| 154 | tmp->tm_wday = bcd2bin (wday & 0x07); |
| 155 | tmp->tm_yday = 0; |
| 156 | tmp->tm_isdst= 0; |
| 157 | |
Heiko Schocher | 6b659d0 | 2019-07-16 05:31:34 +0200 | [diff] [blame] | 158 | DEBUGR("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 159 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 160 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
Yuri Tikhonov | 9bacd94 | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 161 | |
| 162 | return rel; |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 165 | /* |
| 166 | * Set the RTC |
| 167 | */ |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 168 | static int rx8025_rtc_set(DEV_TYPE *dev, const struct rtc_time *tmp) |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 169 | { |
Heiko Schocher | 6b659d0 | 2019-07-16 05:31:34 +0200 | [diff] [blame] | 170 | DEBUGR("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 171 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 172 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 173 | |
| 174 | if (tmp->tm_year < 1970 || tmp->tm_year > 2069) |
| 175 | printf("WARNING: year should be between 1970 and 2069!\n"); |
| 176 | |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 177 | if (rtc_write(dev, RTC_YR_REG_ADDR, bin2bcd(tmp->tm_year % 100))) |
| 178 | return -EIO; |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 179 | |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 180 | if (rtc_write(dev, RTC_MON_REG_ADDR, bin2bcd(tmp->tm_mon))) |
| 181 | return -EIO; |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 182 | |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 183 | if (rtc_write(dev, RTC_DAY_REG_ADDR, bin2bcd(tmp->tm_wday))) |
| 184 | return -EIO; |
| 185 | |
| 186 | if (rtc_write(dev, RTC_DATE_REG_ADDR, bin2bcd(tmp->tm_mday))) |
| 187 | return -EIO; |
| 188 | |
| 189 | if (rtc_write(dev, RTC_HR_REG_ADDR, bin2bcd(tmp->tm_hour))) |
| 190 | return -EIO; |
| 191 | |
| 192 | if (rtc_write(dev, RTC_MIN_REG_ADDR, bin2bcd(tmp->tm_min))) |
| 193 | return -EIO; |
| 194 | |
| 195 | if (rtc_write(dev, RTC_SEC_REG_ADDR, bin2bcd(tmp->tm_sec))) |
| 196 | return -EIO; |
| 197 | |
| 198 | return rtc_write(dev, RTC_CTL1_REG_ADDR, RTC_CTL1_BIT_2412); |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 201 | /* |
Chris Packham | 7c105e4 | 2018-03-21 15:40:37 +1300 | [diff] [blame] | 202 | * Reset the RTC |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 203 | */ |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 204 | static int rx8025_rtc_reset(DEV_TYPE *dev) |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 205 | { |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 206 | uchar buf[16]; |
| 207 | uchar ctl2; |
| 208 | |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 209 | #ifdef CONFIG_DM_RTC |
| 210 | if (dm_i2c_read(dev, 0, buf, sizeof(buf))) { |
| 211 | #else |
| 212 | if (i2c_read(dev->chip, 0, 0, buf, 16)) { |
| 213 | #endif |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 214 | printf("Error reading from RTC\n"); |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 215 | return -EIO; |
| 216 | } |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 217 | |
| 218 | ctl2 = rtc_read(RTC_CTL2_REG_ADDR); |
| 219 | ctl2 &= ~(RTC_CTL2_BIT_PON | RTC_CTL2_BIT_VDET); |
| 220 | ctl2 |= RTC_CTL2_BIT_XST | RTC_CTL2_BIT_VDSL; |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 221 | |
| 222 | return rtc_write(dev, RTC_CTL2_REG_ADDR, ctl2); |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 225 | /* |
| 226 | * Helper functions |
| 227 | */ |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 228 | static int rtc_write(DEV_TYPE *dev, uchar reg, uchar val) |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 229 | { |
| 230 | uchar buf[2]; |
| 231 | buf[0] = reg << 4; |
| 232 | buf[1] = val; |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 233 | |
| 234 | #ifdef CONFIG_DM_RTC |
| 235 | if (dm_i2c_write(dev, 0, buf, 2)) { |
| 236 | #else |
| 237 | if (i2c_write(dev->chip, 0, 0, buf, 2) != 0) { |
| 238 | #endif |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 239 | printf("Error writing to RTC\n"); |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 240 | return -EIO; |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | #ifdef CONFIG_DM_RTC |
| 247 | static int rx8025_probe(struct udevice *dev) |
| 248 | { |
| 249 | uchar buf[16]; |
| 250 | int ret = 0; |
| 251 | |
| 252 | if (i2c_get_chip_offset_len(dev) != 1) |
| 253 | ret = i2c_set_chip_offset_len(dev, 1); |
| 254 | |
| 255 | if (ret) |
| 256 | return ret; |
| 257 | |
| 258 | return dm_i2c_read(dev, 0, buf, sizeof(buf)); |
Matthias Fuchs | 6f60bf3 | 2007-12-27 16:55:17 +0100 | [diff] [blame] | 259 | } |
Heiko Schocher | b11a14a | 2019-07-16 05:31:35 +0200 | [diff] [blame] | 260 | |
| 261 | static const struct rtc_ops rx8025_rtc_ops = { |
| 262 | .get = rx8025_rtc_get, |
| 263 | .set = rx8025_rtc_set, |
| 264 | .reset = rx8025_rtc_reset, |
| 265 | }; |
| 266 | |
| 267 | static const struct udevice_id rx8025_rtc_ids[] = { |
| 268 | { .compatible = "epson,rx8025" }, |
| 269 | { } |
| 270 | }; |
| 271 | |
| 272 | U_BOOT_DRIVER(rx8010sj_rtc) = { |
| 273 | .name = "rx8025_rtc", |
| 274 | .id = UCLASS_RTC, |
| 275 | .probe = rx8025_probe, |
| 276 | .of_match = rx8025_rtc_ids, |
| 277 | .ops = &rx8025_rtc_ops, |
| 278 | }; |
| 279 | #else |
| 280 | int rtc_get(struct rtc_time *tm) |
| 281 | { |
| 282 | struct ludevice dev = { |
| 283 | .chip = CONFIG_SYS_I2C_RTC_ADDR, |
| 284 | }; |
| 285 | |
| 286 | return rx8025_rtc_get(&dev, tm); |
| 287 | } |
| 288 | |
| 289 | int rtc_set(struct rtc_time *tm) |
| 290 | { |
| 291 | struct ludevice dev = { |
| 292 | .chip = CONFIG_SYS_I2C_RTC_ADDR, |
| 293 | }; |
| 294 | |
| 295 | return rx8025_rtc_set(&dev, tm); |
| 296 | } |
| 297 | |
| 298 | void rtc_reset(void) |
| 299 | { |
| 300 | struct ludevice dev = { |
| 301 | .chip = CONFIG_SYS_I2C_RTC_ADDR, |
| 302 | }; |
| 303 | |
| 304 | rx8025_rtc_reset(&dev); |
| 305 | } |
| 306 | #endif |