Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2010 |
| 3 | * Heiko Schocher, DENX Software Engineering, hs@denx.de |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | #include <common.h> |
| 24 | #include <command.h> |
| 25 | #include <i2c.h> |
| 26 | #include <rtc.h> |
| 27 | |
Heiko Schocher | 1f1b701 | 2011-03-28 09:24:22 +0200 | [diff] [blame^] | 28 | #define RTC_RV3029_CTRL1 0x00 |
| 29 | #define RTC_RV3029_CTRL1_EERE (1 << 3) |
| 30 | |
| 31 | #define RTC_RV3029_CTRL_STATUS 0x03 |
| 32 | #define RTC_RV3029_CTRLS_EEBUSY (1 << 7) |
| 33 | |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 34 | #define RTC_RV3029_CTRL_RESET 0x04 |
| 35 | #define RTC_RV3029_CTRL_SYS_R (1 << 4) |
| 36 | |
| 37 | #define RTC_RV3029_CLOCK_PAGE 0x08 |
| 38 | #define RTC_RV3029_PAGE_LEN 7 |
| 39 | |
| 40 | #define RV3029C2_W_SECONDS 0x00 |
| 41 | #define RV3029C2_W_MINUTES 0x01 |
| 42 | #define RV3029C2_W_HOURS 0x02 |
| 43 | #define RV3029C2_W_DATE 0x03 |
| 44 | #define RV3029C2_W_DAYS 0x04 |
| 45 | #define RV3029C2_W_MONTHS 0x05 |
| 46 | #define RV3029C2_W_YEARS 0x06 |
| 47 | |
| 48 | #define RV3029C2_REG_HR_12_24 (1 << 6) /* 24h/12h mode */ |
| 49 | #define RV3029C2_REG_HR_PM (1 << 5) /* PM/AM bit in 12h mode */ |
| 50 | |
Heiko Schocher | 1f1b701 | 2011-03-28 09:24:22 +0200 | [diff] [blame^] | 51 | #define RTC_RV3029_EEPROM_CTRL 0x30 |
| 52 | #define RTC_RV3029_TRICKLE_1K (1 << 4) |
| 53 | #define RTC_RV3029_TRICKLE_5K (1 << 5) |
| 54 | #define RTC_RV3029_TRICKLE_20K (1 << 6) |
| 55 | #define RTC_RV3029_TRICKLE_80K (1 << 7) |
| 56 | |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 57 | int rtc_get( struct rtc_time *tmp ) |
| 58 | { |
| 59 | int ret; |
| 60 | unsigned char buf[RTC_RV3029_PAGE_LEN]; |
| 61 | |
| 62 | ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1, buf, \ |
| 63 | RTC_RV3029_PAGE_LEN); |
| 64 | if (ret) { |
| 65 | printf("%s: error reading RTC: %x\n", __func__, ret); |
| 66 | return -1; |
| 67 | } |
| 68 | tmp->tm_sec = bcd2bin( buf[RV3029C2_W_SECONDS] & 0x7f); |
| 69 | tmp->tm_min = bcd2bin( buf[RV3029C2_W_MINUTES] & 0x7f); |
| 70 | if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_12_24) { |
| 71 | /* 12h format */ |
| 72 | tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x1f); |
| 73 | if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_PM) |
| 74 | /* PM flag set */ |
| 75 | tmp->tm_hour += 12; |
| 76 | } else |
| 77 | tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x3f); |
| 78 | |
| 79 | tmp->tm_mday = bcd2bin( buf[RV3029C2_W_DATE] & 0x3F ); |
| 80 | tmp->tm_mon = bcd2bin( buf[RV3029C2_W_MONTHS] & 0x1F ); |
| 81 | tmp->tm_wday = bcd2bin( buf[RV3029C2_W_DAYS] & 0x07 ); |
| 82 | /* RTC supports only years > 1999 */ |
| 83 | tmp->tm_year = bcd2bin( buf[RV3029C2_W_YEARS]) + 2000; |
| 84 | tmp->tm_yday = 0; |
| 85 | tmp->tm_isdst = 0; |
| 86 | |
| 87 | #ifdef RTC_DEBUG |
| 88 | printf( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 89 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 90 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec ); |
| 91 | |
| 92 | #endif |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | int rtc_set( struct rtc_time *tmp ) |
| 97 | { |
| 98 | int ret; |
| 99 | unsigned char buf[RTC_RV3029_PAGE_LEN]; |
| 100 | #ifdef RTC_DEBUG |
| 101 | printf( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 102 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 103 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 104 | #endif |
| 105 | |
| 106 | if (tmp->tm_year < 2000) { |
| 107 | printf("RTC: year %d < 2000 not possible\n", tmp->tm_year); |
| 108 | return -1; |
| 109 | } |
| 110 | buf[RV3029C2_W_SECONDS] = bin2bcd(tmp->tm_sec); |
| 111 | buf[RV3029C2_W_MINUTES] = bin2bcd(tmp->tm_min); |
| 112 | buf[RV3029C2_W_HOURS] = bin2bcd(tmp->tm_hour); |
| 113 | /* set 24h format */ |
| 114 | buf[RV3029C2_W_HOURS] &= ~RV3029C2_REG_HR_12_24; |
| 115 | buf[RV3029C2_W_DATE] = bin2bcd(tmp->tm_mday); |
| 116 | buf[RV3029C2_W_DAYS] = bin2bcd(tmp->tm_wday); |
| 117 | buf[RV3029C2_W_MONTHS] = bin2bcd(tmp->tm_mon); |
| 118 | tmp->tm_year -= 2000; |
| 119 | buf[RV3029C2_W_YEARS] = bin2bcd(tmp->tm_year); |
| 120 | ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1, |
| 121 | buf, RTC_RV3029_PAGE_LEN); |
| 122 | |
| 123 | /* give the RTC some time to update */ |
| 124 | udelay(1000); |
| 125 | return 0; |
| 126 | } |
| 127 | |
Heiko Schocher | 1f1b701 | 2011-03-28 09:24:22 +0200 | [diff] [blame^] | 128 | /* sets EERE-Bit (automatic EEPROM refresh) */ |
| 129 | static void set_eere_bit(int state) |
| 130 | { |
| 131 | int ret; |
| 132 | unsigned char reg_ctrl1; |
| 133 | |
| 134 | ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1, |
| 135 | ®_ctrl1, 1); |
| 136 | |
| 137 | if (state) |
| 138 | reg_ctrl1 |= RTC_RV3029_CTRL1_EERE; |
| 139 | else |
| 140 | reg_ctrl1 &= (~RTC_RV3029_CTRL1_EERE); |
| 141 | |
| 142 | ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1, |
| 143 | ®_ctrl1, 1); |
| 144 | } |
| 145 | |
| 146 | /* waits until EEPROM page is no longer busy (times out after 10ms*loops) */ |
| 147 | static int wait_eebusy(int loops) |
| 148 | { |
| 149 | int i, ret; |
| 150 | unsigned char ctrl_status; |
| 151 | |
| 152 | for (i = 0; i < loops; i++) { |
| 153 | ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_STATUS, |
| 154 | 1, &ctrl_status, 1); |
| 155 | |
| 156 | if ((ctrl_status & RTC_RV3029_CTRLS_EEBUSY) == 0) |
| 157 | break; |
| 158 | udelay(10000); |
| 159 | } |
| 160 | return i; |
| 161 | } |
| 162 | |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 163 | void rtc_reset (void) |
| 164 | { |
| 165 | int ret; |
| 166 | unsigned char buf[RTC_RV3029_PAGE_LEN]; |
| 167 | |
| 168 | buf[0] = RTC_RV3029_CTRL_SYS_R; |
| 169 | ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_RESET, 1, |
| 170 | buf, 1); |
Heiko Schocher | 1f1b701 | 2011-03-28 09:24:22 +0200 | [diff] [blame^] | 171 | |
| 172 | #if defined(CONFIG_SYS_RV3029_TCR) |
| 173 | /* |
| 174 | * because EEPROM_CTRL register is in EEPROM page it is necessary to |
| 175 | * disable automatic EEPROM refresh and check if EEPROM is busy |
| 176 | * before EEPORM_CTRL register may be accessed |
| 177 | */ |
| 178 | set_eere_bit(0); |
| 179 | wait_eebusy(100); |
| 180 | /* read current trickle charger setting */ |
| 181 | ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_EEPROM_CTRL, |
| 182 | 1, buf, 1); |
| 183 | /* enable automatic EEPROM refresh again */ |
| 184 | set_eere_bit(1); |
| 185 | |
| 186 | /* |
| 187 | * to minimize EEPROM access write trickle charger setting only if it |
| 188 | * differs from current value |
| 189 | */ |
| 190 | if ((buf[0] & 0xF0) != CONFIG_SYS_RV3029_TCR) { |
| 191 | buf[0] = (buf[0] & 0x0F) | CONFIG_SYS_RV3029_TCR; |
| 192 | /* |
| 193 | * write trickle charger setting (disable autom. EEPROM |
| 194 | * refresh and wait until EEPROM is idle) |
| 195 | */ |
| 196 | set_eere_bit(0); |
| 197 | wait_eebusy(100); |
| 198 | ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, |
| 199 | RTC_RV3029_EEPROM_CTRL, 1, buf, 1); |
| 200 | /* |
| 201 | * it is necessary to wait 10ms before EEBUSY-Bit may be read |
| 202 | * (this is not documented in the data sheet yet, but the |
| 203 | * manufacturer recommends it) |
| 204 | */ |
| 205 | udelay(10000); |
| 206 | /* wait until EEPROM write access is finished */ |
| 207 | wait_eebusy(100); |
| 208 | set_eere_bit(1); |
| 209 | } |
| 210 | #endif |
Heiko Schocher | 641f0ab | 2011-01-12 08:20:05 +0100 | [diff] [blame] | 211 | } |