wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * rs5c372.c |
| 3 | * |
| 4 | * Device driver for Ricoh's Real Time Controller RS5C372A. |
| 5 | * |
| 6 | * Copyright (C) 2004 Gary Jennejohn garyj@denx.de |
| 7 | * |
| 8 | * Based in part in ds1307.c - |
| 9 | * (C) Copyright 2001, 2002, 2003 |
| 10 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 11 | * Keith Outwater, keith_outwater@mvis.com` |
| 12 | * Steven Scholz, steven.scholz@imc-berlin.de |
| 13 | * |
| 14 | * See file CREDITS for list of people who contributed to this |
| 15 | * project. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or modify |
| 18 | * it under the terms of the GNU General Public License version 2 as |
Wolfgang Denk | 3765b3e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 19 | * published by the Free Software Foundation. |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <common.h> |
| 23 | #include <command.h> |
| 24 | #include <rtc.h> |
| 25 | #include <i2c.h> |
| 26 | |
Michal Simek | 871c18d | 2008-07-14 19:45:37 +0200 | [diff] [blame] | 27 | #if defined(CONFIG_CMD_DATE) |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 28 | /* |
| 29 | * Reads are always done starting with register 15, which requires some |
| 30 | * jumping-through-hoops to access the data correctly. |
| 31 | * |
| 32 | * Writes are always done starting with register 0. |
| 33 | */ |
| 34 | |
| 35 | #define DEBUG 0 |
| 36 | |
| 37 | #if DEBUG |
| 38 | static unsigned int rtc_debug = DEBUG; |
| 39 | #else |
| 40 | #define rtc_debug 0 /* gcc will remove all the debug code for us */ |
| 41 | #endif |
| 42 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 43 | #ifndef CONFIG_SYS_I2C_RTC_ADDR |
| 44 | #define CONFIG_SYS_I2C_RTC_ADDR 0x32 |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
| 47 | #define RS5C372_RAM_SIZE 0x10 |
| 48 | #define RATE_32000HZ 0x80 /* Rate Select 32.000KHz */ |
| 49 | #define RATE_32768HZ 0x00 /* Rate Select 32.768KHz */ |
| 50 | |
| 51 | #define STATUS_XPT 0x10 /* data invalid because voltage was 0 */ |
| 52 | |
| 53 | #define USE_24HOUR_MODE 0x20 |
| 54 | #define TWELVE_HOUR_MODE(n) ((((n) >> 5) & 1) == 0) |
| 55 | #define HOURS_AP(n) (((n) >> 5) & 1) |
| 56 | #define HOURS_12(n) bcd2bin((n) & 0x1F) |
| 57 | #define HOURS_24(n) bcd2bin((n) & 0x3F) |
| 58 | |
| 59 | |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 60 | static int setup_done = 0; |
| 61 | |
| 62 | static int |
Wolfgang Denk | d52fb7e | 2006-03-11 22:53:33 +0100 | [diff] [blame] | 63 | rs5c372_readram(unsigned char *buf, int len) |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 64 | { |
| 65 | int ret; |
| 66 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 67 | ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, len); |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 68 | if (ret != 0) { |
| 69 | printf("%s: failed to read\n", __FUNCTION__); |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | if (buf[0] & STATUS_XPT) |
| 74 | printf("### Warning: RTC lost power\n"); |
| 75 | |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | static void |
| 80 | rs5c372_enable(void) |
| 81 | { |
| 82 | unsigned char buf[RS5C372_RAM_SIZE + 1]; |
| 83 | int ret; |
| 84 | |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 85 | /* note that this returns reg. 15 in buf[1] */ |
| 86 | ret = rs5c372_readram(&buf[1], RS5C372_RAM_SIZE); |
| 87 | if (ret != 0) { |
| 88 | printf("%s: failed\n", __FUNCTION__); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | buf[0] = 0; |
| 93 | /* we want to start writing at register 0 so we have to copy the */ |
| 94 | /* register contents up one slot */ |
| 95 | for (ret = 2; ret < 9; ret++) |
| 96 | buf[ret - 1] = buf[ret]; |
| 97 | /* registers 0 to 6 (time values) are not touched */ |
| 98 | buf[8] = RATE_32768HZ; /* reg. 7 */ |
| 99 | buf[9] = 0; /* reg. 8 */ |
| 100 | buf[10] = 0; /* reg. 9 */ |
| 101 | buf[11] = 0; /* reg. 10 */ |
| 102 | buf[12] = 0; /* reg. 11 */ |
| 103 | buf[13] = 0; /* reg. 12 */ |
| 104 | buf[14] = 0; /* reg. 13 */ |
| 105 | buf[15] = 0; /* reg. 14 */ |
| 106 | buf[16] = USE_24HOUR_MODE; /* reg. 15 */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 107 | ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, RS5C372_RAM_SIZE+1); |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 108 | if (ret != 0) { |
| 109 | printf("%s: failed\n", __FUNCTION__); |
| 110 | return; |
| 111 | } |
| 112 | setup_done = 1; |
| 113 | |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | static void |
Wolfgang Denk | d52fb7e | 2006-03-11 22:53:33 +0100 | [diff] [blame] | 118 | rs5c372_convert_to_time(struct rtc_time *dt, unsigned char *buf) |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 119 | { |
| 120 | /* buf[0] is register 15 */ |
| 121 | dt->tm_sec = bcd2bin(buf[1]); |
| 122 | dt->tm_min = bcd2bin(buf[2]); |
| 123 | |
| 124 | if (TWELVE_HOUR_MODE(buf[0])) { |
| 125 | dt->tm_hour = HOURS_12(buf[3]); |
| 126 | if (HOURS_AP(buf[3])) /* PM */ |
| 127 | dt->tm_hour += 12; |
| 128 | } else /* 24-hour-mode */ |
| 129 | dt->tm_hour = HOURS_24(buf[3]); |
| 130 | |
| 131 | dt->tm_mday = bcd2bin(buf[5]); |
| 132 | dt->tm_mon = bcd2bin(buf[6]); |
| 133 | dt->tm_year = bcd2bin(buf[7]); |
| 134 | if (dt->tm_year >= 70) |
| 135 | dt->tm_year += 1900; |
| 136 | else |
| 137 | dt->tm_year += 2000; |
| 138 | /* 0 is Sunday */ |
| 139 | dt->tm_wday = bcd2bin(buf[4] & 0x07); |
| 140 | dt->tm_yday = 0; |
| 141 | dt->tm_isdst= 0; |
| 142 | |
| 143 | if(rtc_debug > 2) { |
| 144 | printf("rs5c372_convert_to_time: year = %d\n", dt->tm_year); |
| 145 | printf("rs5c372_convert_to_time: mon = %d\n", dt->tm_mon); |
| 146 | printf("rs5c372_convert_to_time: mday = %d\n", dt->tm_mday); |
| 147 | printf("rs5c372_convert_to_time: hour = %d\n", dt->tm_hour); |
| 148 | printf("rs5c372_convert_to_time: min = %d\n", dt->tm_min); |
| 149 | printf("rs5c372_convert_to_time: sec = %d\n", dt->tm_sec); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Get the current time from the RTC |
| 155 | */ |
Yuri Tikhonov | b73a19e | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 156 | int |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 157 | rtc_get (struct rtc_time *tmp) |
| 158 | { |
| 159 | unsigned char buf[RS5C372_RAM_SIZE]; |
| 160 | int ret; |
| 161 | |
| 162 | if (!setup_done) |
| 163 | rs5c372_enable(); |
| 164 | |
| 165 | if (!setup_done) |
Yuri Tikhonov | b73a19e | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 166 | return -1; |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 167 | |
| 168 | memset(buf, 0, sizeof(buf)); |
| 169 | |
| 170 | /* note that this returns reg. 15 in buf[0] */ |
| 171 | ret = rs5c372_readram(buf, RS5C372_RAM_SIZE); |
| 172 | if (ret != 0) { |
| 173 | printf("%s: failed\n", __FUNCTION__); |
Yuri Tikhonov | b73a19e | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 174 | return -1; |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | rs5c372_convert_to_time(tmp, buf); |
| 178 | |
Yuri Tikhonov | b73a19e | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 179 | return 0; |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Set the RTC |
| 184 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 13b4db0 | 2008-09-09 00:59:39 +0200 | [diff] [blame] | 185 | int rtc_set (struct rtc_time *tmp) |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 186 | { |
| 187 | unsigned char buf[8], reg15; |
| 188 | int ret; |
| 189 | |
| 190 | if (!setup_done) |
| 191 | rs5c372_enable(); |
| 192 | |
| 193 | if (!setup_done) |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 194 | return -1; |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 195 | |
| 196 | if(rtc_debug > 2) { |
| 197 | printf("rtc_set: tm_year = %d\n", tmp->tm_year); |
| 198 | printf("rtc_set: tm_mon = %d\n", tmp->tm_mon); |
| 199 | printf("rtc_set: tm_mday = %d\n", tmp->tm_mday); |
| 200 | printf("rtc_set: tm_hour = %d\n", tmp->tm_hour); |
| 201 | printf("rtc_set: tm_min = %d\n", tmp->tm_min); |
| 202 | printf("rtc_set: tm_sec = %d\n", tmp->tm_sec); |
| 203 | } |
| 204 | |
| 205 | memset(buf, 0, sizeof(buf)); |
| 206 | |
| 207 | /* only read register 15 */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 208 | ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 1); |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 209 | |
| 210 | if (ret == 0) { |
| 211 | /* need to save register 15 */ |
| 212 | reg15 = buf[0]; |
| 213 | buf[0] = 0; /* register address on RS5C372 */ |
| 214 | buf[1] = bin2bcd(tmp->tm_sec); |
| 215 | buf[2] = bin2bcd(tmp->tm_min); |
| 216 | /* need to handle 12 hour mode */ |
| 217 | if (TWELVE_HOUR_MODE(reg15)) { |
| 218 | if (tmp->tm_hour >= 12) { /* PM */ |
| 219 | /* 12 PM is a special case */ |
| 220 | if (tmp->tm_hour == 12) |
| 221 | buf[3] = bin2bcd(tmp->tm_hour); |
| 222 | else |
| 223 | buf[3] = bin2bcd(tmp->tm_hour - 12); |
| 224 | buf[3] |= 0x20; |
| 225 | } |
| 226 | } else { |
| 227 | buf[3] = bin2bcd(tmp->tm_hour); |
| 228 | } |
| 229 | |
| 230 | buf[4] = bin2bcd(tmp->tm_wday); |
| 231 | buf[5] = bin2bcd(tmp->tm_mday); |
| 232 | buf[6] = bin2bcd(tmp->tm_mon); |
| 233 | if (tmp->tm_year < 1970 || tmp->tm_year > 2069) |
| 234 | printf("WARNING: year should be between 1970 and 2069!\n"); |
| 235 | buf[7] = bin2bcd(tmp->tm_year % 100); |
| 236 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 237 | ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 8); |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 238 | if (ret != 0) { |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 239 | printf("rs5c372_set_datetime(), i2c_master_send() returned %d\n",ret); |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 240 | return -1; |
| 241 | } |
| 242 | } else { |
| 243 | return -1; |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 246 | return 0; |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* |
Chris Packham | 05d63d8 | 2018-03-21 15:40:36 +1300 | [diff] [blame] | 250 | * Reset the RTC. |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 251 | */ |
| 252 | void |
| 253 | rtc_reset (void) |
| 254 | { |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 255 | if (!setup_done) |
| 256 | rs5c372_enable(); |
wdenk | b650851 | 2004-10-10 18:03:33 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Jon Loeliger | a593814 | 2007-07-09 18:10:50 -0500 | [diff] [blame] | 259 | #endif |