Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2010 |
| 4 | * Heiko Schocher, DENX Software Engineering, hs@denx.de |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 5 | */ |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
| 8 | #include <i2c.h> |
| 9 | #include <rtc.h> |
| 10 | |
Heiko Schocher | 1f1b701 | 2011-03-28 09:24:22 +0200 | [diff] [blame] | 11 | #define RTC_RV3029_CTRL1 0x00 |
| 12 | #define RTC_RV3029_CTRL1_EERE (1 << 3) |
| 13 | |
| 14 | #define RTC_RV3029_CTRL_STATUS 0x03 |
| 15 | #define RTC_RV3029_CTRLS_EEBUSY (1 << 7) |
| 16 | |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 17 | #define RTC_RV3029_CTRL_RESET 0x04 |
| 18 | #define RTC_RV3029_CTRL_SYS_R (1 << 4) |
| 19 | |
| 20 | #define RTC_RV3029_CLOCK_PAGE 0x08 |
| 21 | #define RTC_RV3029_PAGE_LEN 7 |
| 22 | |
| 23 | #define RV3029C2_W_SECONDS 0x00 |
| 24 | #define RV3029C2_W_MINUTES 0x01 |
| 25 | #define RV3029C2_W_HOURS 0x02 |
| 26 | #define RV3029C2_W_DATE 0x03 |
| 27 | #define RV3029C2_W_DAYS 0x04 |
| 28 | #define RV3029C2_W_MONTHS 0x05 |
| 29 | #define RV3029C2_W_YEARS 0x06 |
| 30 | |
| 31 | #define RV3029C2_REG_HR_12_24 (1 << 6) /* 24h/12h mode */ |
| 32 | #define RV3029C2_REG_HR_PM (1 << 5) /* PM/AM bit in 12h mode */ |
| 33 | |
Heiko Schocher | 1f1b701 | 2011-03-28 09:24:22 +0200 | [diff] [blame] | 34 | #define RTC_RV3029_EEPROM_CTRL 0x30 |
| 35 | #define RTC_RV3029_TRICKLE_1K (1 << 4) |
| 36 | #define RTC_RV3029_TRICKLE_5K (1 << 5) |
| 37 | #define RTC_RV3029_TRICKLE_20K (1 << 6) |
| 38 | #define RTC_RV3029_TRICKLE_80K (1 << 7) |
| 39 | |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 40 | int rtc_get( struct rtc_time *tmp ) |
| 41 | { |
| 42 | int ret; |
| 43 | unsigned char buf[RTC_RV3029_PAGE_LEN]; |
| 44 | |
| 45 | ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1, buf, \ |
| 46 | RTC_RV3029_PAGE_LEN); |
| 47 | if (ret) { |
| 48 | printf("%s: error reading RTC: %x\n", __func__, ret); |
| 49 | return -1; |
| 50 | } |
| 51 | tmp->tm_sec = bcd2bin( buf[RV3029C2_W_SECONDS] & 0x7f); |
| 52 | tmp->tm_min = bcd2bin( buf[RV3029C2_W_MINUTES] & 0x7f); |
| 53 | if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_12_24) { |
| 54 | /* 12h format */ |
| 55 | tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x1f); |
| 56 | if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_PM) |
| 57 | /* PM flag set */ |
| 58 | tmp->tm_hour += 12; |
| 59 | } else |
| 60 | tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x3f); |
| 61 | |
| 62 | tmp->tm_mday = bcd2bin( buf[RV3029C2_W_DATE] & 0x3F ); |
| 63 | tmp->tm_mon = bcd2bin( buf[RV3029C2_W_MONTHS] & 0x1F ); |
| 64 | tmp->tm_wday = bcd2bin( buf[RV3029C2_W_DAYS] & 0x07 ); |
| 65 | /* RTC supports only years > 1999 */ |
| 66 | tmp->tm_year = bcd2bin( buf[RV3029C2_W_YEARS]) + 2000; |
| 67 | tmp->tm_yday = 0; |
| 68 | tmp->tm_isdst = 0; |
| 69 | |
Wolfgang Denk | f2974f6 | 2011-11-04 15:55:13 +0000 | [diff] [blame] | 70 | debug( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 71 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 72 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec ); |
| 73 | |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int rtc_set( struct rtc_time *tmp ) |
| 78 | { |
| 79 | int ret; |
| 80 | unsigned char buf[RTC_RV3029_PAGE_LEN]; |
Wolfgang Denk | f2974f6 | 2011-11-04 15:55:13 +0000 | [diff] [blame] | 81 | |
| 82 | debug( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 83 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 84 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 85 | |
| 86 | if (tmp->tm_year < 2000) { |
| 87 | printf("RTC: year %d < 2000 not possible\n", tmp->tm_year); |
| 88 | return -1; |
| 89 | } |
| 90 | buf[RV3029C2_W_SECONDS] = bin2bcd(tmp->tm_sec); |
| 91 | buf[RV3029C2_W_MINUTES] = bin2bcd(tmp->tm_min); |
| 92 | buf[RV3029C2_W_HOURS] = bin2bcd(tmp->tm_hour); |
| 93 | /* set 24h format */ |
| 94 | buf[RV3029C2_W_HOURS] &= ~RV3029C2_REG_HR_12_24; |
| 95 | buf[RV3029C2_W_DATE] = bin2bcd(tmp->tm_mday); |
| 96 | buf[RV3029C2_W_DAYS] = bin2bcd(tmp->tm_wday); |
| 97 | buf[RV3029C2_W_MONTHS] = bin2bcd(tmp->tm_mon); |
| 98 | tmp->tm_year -= 2000; |
| 99 | buf[RV3029C2_W_YEARS] = bin2bcd(tmp->tm_year); |
| 100 | ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1, |
| 101 | buf, RTC_RV3029_PAGE_LEN); |
| 102 | |
| 103 | /* give the RTC some time to update */ |
| 104 | udelay(1000); |
Wolfgang Denk | f2974f6 | 2011-11-04 15:55:13 +0000 | [diff] [blame] | 105 | return ret; |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Heiko Schocher | 1f1b701 | 2011-03-28 09:24:22 +0200 | [diff] [blame] | 108 | /* sets EERE-Bit (automatic EEPROM refresh) */ |
| 109 | static void set_eere_bit(int state) |
| 110 | { |
Heiko Schocher | 1f1b701 | 2011-03-28 09:24:22 +0200 | [diff] [blame] | 111 | unsigned char reg_ctrl1; |
| 112 | |
Wolfgang Denk | f2974f6 | 2011-11-04 15:55:13 +0000 | [diff] [blame] | 113 | (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1, |
Heiko Schocher | 1f1b701 | 2011-03-28 09:24:22 +0200 | [diff] [blame] | 114 | ®_ctrl1, 1); |
| 115 | |
| 116 | if (state) |
| 117 | reg_ctrl1 |= RTC_RV3029_CTRL1_EERE; |
| 118 | else |
| 119 | reg_ctrl1 &= (~RTC_RV3029_CTRL1_EERE); |
| 120 | |
Wolfgang Denk | f2974f6 | 2011-11-04 15:55:13 +0000 | [diff] [blame] | 121 | (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1, |
Heiko Schocher | 1f1b701 | 2011-03-28 09:24:22 +0200 | [diff] [blame] | 122 | ®_ctrl1, 1); |
| 123 | } |
| 124 | |
| 125 | /* waits until EEPROM page is no longer busy (times out after 10ms*loops) */ |
| 126 | static int wait_eebusy(int loops) |
| 127 | { |
Wolfgang Denk | f2974f6 | 2011-11-04 15:55:13 +0000 | [diff] [blame] | 128 | int i; |
Heiko Schocher | 1f1b701 | 2011-03-28 09:24:22 +0200 | [diff] [blame] | 129 | unsigned char ctrl_status; |
| 130 | |
| 131 | for (i = 0; i < loops; i++) { |
Wolfgang Denk | f2974f6 | 2011-11-04 15:55:13 +0000 | [diff] [blame] | 132 | (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_STATUS, |
Heiko Schocher | 1f1b701 | 2011-03-28 09:24:22 +0200 | [diff] [blame] | 133 | 1, &ctrl_status, 1); |
| 134 | |
| 135 | if ((ctrl_status & RTC_RV3029_CTRLS_EEBUSY) == 0) |
| 136 | break; |
| 137 | udelay(10000); |
| 138 | } |
| 139 | return i; |
| 140 | } |
| 141 | |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 142 | void rtc_reset (void) |
| 143 | { |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 144 | unsigned char buf[RTC_RV3029_PAGE_LEN]; |
| 145 | |
| 146 | buf[0] = RTC_RV3029_CTRL_SYS_R; |
Wolfgang Denk | f2974f6 | 2011-11-04 15:55:13 +0000 | [diff] [blame] | 147 | (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_RESET, 1, |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 148 | buf, 1); |
Heiko Schocher | 1f1b701 | 2011-03-28 09:24:22 +0200 | [diff] [blame] | 149 | |
| 150 | #if defined(CONFIG_SYS_RV3029_TCR) |
| 151 | /* |
| 152 | * because EEPROM_CTRL register is in EEPROM page it is necessary to |
| 153 | * disable automatic EEPROM refresh and check if EEPROM is busy |
| 154 | * before EEPORM_CTRL register may be accessed |
| 155 | */ |
| 156 | set_eere_bit(0); |
| 157 | wait_eebusy(100); |
| 158 | /* read current trickle charger setting */ |
Wolfgang Denk | f2974f6 | 2011-11-04 15:55:13 +0000 | [diff] [blame] | 159 | (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_EEPROM_CTRL, |
Heiko Schocher | 1f1b701 | 2011-03-28 09:24:22 +0200 | [diff] [blame] | 160 | 1, buf, 1); |
| 161 | /* enable automatic EEPROM refresh again */ |
| 162 | set_eere_bit(1); |
| 163 | |
| 164 | /* |
| 165 | * to minimize EEPROM access write trickle charger setting only if it |
| 166 | * differs from current value |
| 167 | */ |
| 168 | if ((buf[0] & 0xF0) != CONFIG_SYS_RV3029_TCR) { |
| 169 | buf[0] = (buf[0] & 0x0F) | CONFIG_SYS_RV3029_TCR; |
| 170 | /* |
| 171 | * write trickle charger setting (disable autom. EEPROM |
| 172 | * refresh and wait until EEPROM is idle) |
| 173 | */ |
| 174 | set_eere_bit(0); |
| 175 | wait_eebusy(100); |
Wolfgang Denk | f2974f6 | 2011-11-04 15:55:13 +0000 | [diff] [blame] | 176 | (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR, |
Heiko Schocher | 1f1b701 | 2011-03-28 09:24:22 +0200 | [diff] [blame] | 177 | RTC_RV3029_EEPROM_CTRL, 1, buf, 1); |
| 178 | /* |
| 179 | * it is necessary to wait 10ms before EEBUSY-Bit may be read |
| 180 | * (this is not documented in the data sheet yet, but the |
| 181 | * manufacturer recommends it) |
| 182 | */ |
| 183 | udelay(10000); |
| 184 | /* wait until EEPROM write access is finished */ |
| 185 | wait_eebusy(100); |
| 186 | set_eere_bit(1); |
| 187 | } |
| 188 | #endif |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 189 | } |