Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2007 |
| 4 | * Larry Johnson, lrj@acm.org |
| 5 | * |
| 6 | * based on rtc/m41t11.c which is ... |
| 7 | * |
| 8 | * (C) Copyright 2002 |
| 9 | * Andrew May, Viasat Inc, amay@viasat.com |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * STMicroelectronics M41T60 serial access real-time clock |
| 14 | */ |
| 15 | |
| 16 | /* #define DEBUG 1 */ |
| 17 | |
| 18 | #include <common.h> |
| 19 | #include <command.h> |
| 20 | #include <rtc.h> |
| 21 | #include <i2c.h> |
| 22 | |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 23 | /* |
| 24 | * Convert between century and "century bits" (CB1 and CB0). These routines |
| 25 | * assume years are in the range 1900 - 2299. |
| 26 | */ |
| 27 | |
| 28 | static unsigned char year2cb(unsigned const year) |
| 29 | { |
| 30 | if (year < 1900 || year >= 2300) |
| 31 | printf("M41T60 RTC: year %d out of range\n", year); |
| 32 | |
| 33 | return (year / 100) & 0x3; |
| 34 | } |
| 35 | |
| 36 | static unsigned cb2year(unsigned const cb) |
| 37 | { |
| 38 | return 1900 + 100 * ((cb + 1) & 0x3); |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | * These are simple defines for the chip local to here so they aren't too |
| 43 | * verbose. DAY/DATE aren't nice but that is how they are on the data sheet. |
| 44 | */ |
| 45 | #define RTC_SEC 0x0 |
| 46 | #define RTC_MIN 0x1 |
| 47 | #define RTC_HOUR 0x2 |
| 48 | #define RTC_DAY 0x3 |
| 49 | #define RTC_DATE 0x4 |
| 50 | #define RTC_MONTH 0x5 |
| 51 | #define RTC_YEAR 0x6 |
| 52 | |
| 53 | #define RTC_REG_CNT 7 |
| 54 | |
| 55 | #define RTC_CTRL 0x7 |
| 56 | |
| 57 | #if defined(DEBUG) |
| 58 | static void rtc_dump(char const *const label) |
| 59 | { |
| 60 | uchar data[8]; |
| 61 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 62 | if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) { |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 63 | printf("I2C read failed in rtc_dump()\n"); |
| 64 | return; |
| 65 | } |
| 66 | printf("RTC dump %s: %02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n", |
| 67 | label, data[0], data[1], data[2], data[3], |
| 68 | data[4], data[5], data[6], data[7]); |
| 69 | } |
| 70 | #else |
| 71 | #define rtc_dump(label) |
| 72 | #endif |
| 73 | |
| 74 | static uchar *rtc_validate(void) |
| 75 | { |
| 76 | /* |
| 77 | * This routine uses the OUT bit and the validity of the time values to |
| 78 | * determine whether there has been an initial power-up since the last |
| 79 | * time the routine was run. It assumes that the OUT bit is not being |
| 80 | * used for any other purpose. |
| 81 | */ |
| 82 | static const uchar daysInMonth[0x13] = { |
| 83 | 0x00, 0x31, 0x29, 0x31, 0x30, 0x31, 0x30, 0x31, |
| 84 | 0x31, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 85 | 0x31, 0x30, 0x31 |
| 86 | }; |
| 87 | static uchar data[8]; |
| 88 | uchar min, date, month, years; |
| 89 | |
| 90 | rtc_dump("begin validate"); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 91 | if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) { |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 92 | printf("I2C read failed in rtc_validate()\n"); |
| 93 | return 0; |
| 94 | } |
| 95 | /* |
| 96 | * If the OUT bit is "1", there has been a loss of power, so stop the |
| 97 | * oscillator so it can be "kick-started" as per data sheet. |
| 98 | */ |
| 99 | if (0x00 != (data[RTC_CTRL] & 0x80)) { |
| 100 | printf("M41T60 RTC clock lost power.\n"); |
| 101 | data[RTC_SEC] = 0x80; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 102 | if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC, 1, data, 1)) { |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 103 | printf("I2C write failed in rtc_validate()\n"); |
| 104 | return 0; |
| 105 | } |
| 106 | } |
| 107 | /* |
| 108 | * If the oscillator is stopped or the date is invalid, then reset the |
| 109 | * OUT bit to "0", reset the date registers, and start the oscillator. |
| 110 | */ |
| 111 | min = data[RTC_MIN] & 0x7F; |
| 112 | date = data[RTC_DATE]; |
| 113 | month = data[RTC_MONTH] & 0x3F; |
| 114 | years = data[RTC_YEAR]; |
| 115 | if (0x59 < data[RTC_SEC] || 0x09 < (data[RTC_SEC] & 0x0F) || |
| 116 | 0x59 < min || 0x09 < (min & 0x0F) || |
| 117 | 0x23 < data[RTC_HOUR] || 0x09 < (data[RTC_HOUR] & 0x0F) || |
| 118 | 0x07 < data[RTC_DAY] || 0x00 == data[RTC_DAY] || |
| 119 | 0x12 < month || |
| 120 | 0x99 < years || 0x09 < (years & 0x0F) || |
| 121 | daysInMonth[month] < date || 0x09 < (date & 0x0F) || 0x00 == date || |
| 122 | (0x29 == date && 0x02 == month && |
| 123 | ((0x00 != (years & 0x03)) || |
| 124 | (0x00 == years && 0x00 != (data[RTC_MONTH] & 0xC0))))) { |
| 125 | printf("Resetting M41T60 RTC clock.\n"); |
| 126 | /* |
| 127 | * Set to 00:00:00 1900-01-01 (Monday) |
| 128 | */ |
| 129 | data[RTC_SEC] = 0x00; |
| 130 | data[RTC_MIN] &= 0x80; /* preserve OFIE bit */ |
| 131 | data[RTC_HOUR] = 0x00; |
| 132 | data[RTC_DAY] = 0x02; |
| 133 | data[RTC_DATE] = 0x01; |
| 134 | data[RTC_MONTH] = 0xC1; |
| 135 | data[RTC_YEAR] = 0x00; |
| 136 | data[RTC_CTRL] &= 0x7F; /* reset OUT bit */ |
| 137 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 138 | if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) { |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 139 | printf("I2C write failed in rtc_validate()\n"); |
| 140 | return 0; |
| 141 | } |
| 142 | } |
| 143 | return data; |
| 144 | } |
| 145 | |
Yuri Tikhonov | b73a19e | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 146 | int rtc_get(struct rtc_time *tmp) |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 147 | { |
| 148 | uchar const *const data = rtc_validate(); |
| 149 | |
| 150 | if (!data) |
Yuri Tikhonov | b73a19e | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 151 | return -1; |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 152 | |
| 153 | tmp->tm_sec = bcd2bin(data[RTC_SEC] & 0x7F); |
| 154 | tmp->tm_min = bcd2bin(data[RTC_MIN] & 0x7F); |
| 155 | tmp->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3F); |
| 156 | tmp->tm_mday = bcd2bin(data[RTC_DATE] & 0x3F); |
| 157 | tmp->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1F); |
| 158 | tmp->tm_year = cb2year(data[RTC_MONTH] >> 6) + bcd2bin(data[RTC_YEAR]); |
| 159 | tmp->tm_wday = bcd2bin(data[RTC_DAY] & 0x07) - 1; |
| 160 | tmp->tm_yday = 0; |
| 161 | tmp->tm_isdst = 0; |
| 162 | |
| 163 | debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 164 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 165 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
Yuri Tikhonov | b73a19e | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 166 | |
| 167 | return 0; |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 168 | } |
| 169 | |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 170 | int rtc_set(struct rtc_time *tmp) |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 171 | { |
| 172 | uchar *const data = rtc_validate(); |
| 173 | |
| 174 | if (!data) |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 175 | return -1; |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 176 | |
| 177 | debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 178 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 179 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 180 | |
| 181 | data[RTC_SEC] = (data[RTC_SEC] & 0x80) | (bin2bcd(tmp->tm_sec) & 0x7F); |
| 182 | data[RTC_MIN] = (data[RTC_MIN] & 0X80) | (bin2bcd(tmp->tm_min) & 0X7F); |
| 183 | data[RTC_HOUR] = bin2bcd(tmp->tm_hour) & 0x3F; |
| 184 | data[RTC_DATE] = bin2bcd(tmp->tm_mday) & 0x3F; |
| 185 | data[RTC_MONTH] = bin2bcd(tmp->tm_mon) & 0x1F; |
| 186 | data[RTC_YEAR] = bin2bcd(tmp->tm_year % 100); |
| 187 | data[RTC_MONTH] |= year2cb(tmp->tm_year) << 6; |
| 188 | data[RTC_DAY] = bin2bcd(tmp->tm_wday + 1) & 0x07; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 189 | if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, RTC_REG_CNT)) { |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 190 | printf("I2C write failed in rtc_set()\n"); |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 191 | return -1; |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 192 | } |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 193 | |
| 194 | return 0; |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void rtc_reset(void) |
| 198 | { |
| 199 | uchar *const data = rtc_validate(); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 200 | char const *const s = env_get("rtccal"); |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 201 | |
| 202 | if (!data) |
| 203 | return; |
| 204 | |
| 205 | rtc_dump("begin reset"); |
| 206 | /* |
| 207 | * If environmental variable "rtccal" is present, it must be a hex value |
| 208 | * between 0x00 and 0x3F, inclusive. The five least-significan bits |
| 209 | * represent the calibration magnitude, and the sixth bit the sign bit. |
| 210 | * If these do not match the contents of the hardware register, that |
| 211 | * register is updated. The value 0x00 imples no correction. Consult |
| 212 | * the M41T60 documentation for further details. |
| 213 | */ |
| 214 | if (s) { |
| 215 | unsigned long const l = simple_strtoul(s, 0, 16); |
| 216 | |
| 217 | if (l <= 0x3F) { |
| 218 | if ((data[RTC_CTRL] & 0x3F) != l) { |
Stefan Roese | 10943c9 | 2008-07-10 10:00:45 +0200 | [diff] [blame] | 219 | printf("Setting RTC calibration to 0x%02lX\n", |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 220 | l); |
| 221 | data[RTC_CTRL] &= 0xC0; |
| 222 | data[RTC_CTRL] |= (uchar) l; |
| 223 | } |
| 224 | } else |
| 225 | printf("environment parameter \"rtccal\" not valid: " |
| 226 | "ignoring\n"); |
| 227 | } |
| 228 | /* |
| 229 | * Turn off frequency test. |
| 230 | */ |
| 231 | data[RTC_CTRL] &= 0xBF; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 232 | if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CTRL, 1, data + RTC_CTRL, 1)) { |
Larry Johnson | 1261827 | 2007-12-22 15:14:00 -0500 | [diff] [blame] | 233 | printf("I2C write failed in rtc_reset()\n"); |
| 234 | return; |
| 235 | } |
| 236 | rtc_dump("end reset"); |
| 237 | } |