Detlev Zundel | 58d0bff | 2009-03-30 00:31:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2008, 2009 |
| 3 | * Andreas Pfefferle, DENX Software Engineering, ap@denx.de. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Detlev Zundel | 58d0bff | 2009-03-30 00:31:33 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <asm/io.h> |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
| 11 | #include <config.h> |
Detlev Zundel | 58d0bff | 2009-03-30 00:31:33 +0200 | [diff] [blame] | 12 | #include <rtc.h> |
| 13 | #include <tws.h> |
| 14 | |
| 15 | #if defined(CONFIG_CMD_DATE) |
| 16 | |
| 17 | /* |
| 18 | * Note: The acrobatics below is due to the hideously ingenius idea of |
| 19 | * the chip designers. As the chip does not allow register |
| 20 | * addressing, all values need to be read and written in one go. Sure |
| 21 | * enough, the 'wday' field (0-6) is transferred using the economic |
| 22 | * number of 4 bits right in the middle of the packet..... |
| 23 | */ |
| 24 | |
| 25 | int rtc_get(struct rtc_time *tm) |
| 26 | { |
| 27 | int rel = 0; |
| 28 | uchar buffer[7]; |
| 29 | |
| 30 | memset(buffer, 0, 7); |
| 31 | |
| 32 | /* Read 52 bits into our buffer */ |
| 33 | tws_read(buffer, 52); |
| 34 | |
Albin Tonnerre | 8aca720 | 2009-08-13 15:31:11 +0200 | [diff] [blame] | 35 | tm->tm_sec = bcd2bin( buffer[0] & 0x7F); |
| 36 | tm->tm_min = bcd2bin( buffer[1] & 0x7F); |
| 37 | tm->tm_hour = bcd2bin( buffer[2] & 0x3F); |
| 38 | tm->tm_wday = bcd2bin( buffer[3] & 0x07); |
| 39 | tm->tm_mday = bcd2bin((buffer[3] & 0xF0) >> 4 | (buffer[4] & 0x0F) << 4); |
| 40 | tm->tm_mon = bcd2bin((buffer[4] & 0x30) >> 4 | (buffer[5] & 0x0F) << 4); |
| 41 | tm->tm_year = bcd2bin((buffer[5] & 0xF0) >> 4 | (buffer[6] & 0x0F) << 4) + 2000; |
Detlev Zundel | 58d0bff | 2009-03-30 00:31:33 +0200 | [diff] [blame] | 42 | tm->tm_yday = 0; |
| 43 | tm->tm_isdst = 0; |
| 44 | |
| 45 | if (tm->tm_sec & 0x80) { |
| 46 | puts("### Warning: RTC Low Voltage - date/time not reliable\n"); |
| 47 | rel = -1; |
| 48 | } |
| 49 | |
| 50 | debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 51 | tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday, |
| 52 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 53 | |
| 54 | return rel; |
| 55 | } |
| 56 | |
| 57 | int rtc_set(struct rtc_time *tm) |
| 58 | { |
| 59 | uchar buffer[7]; |
| 60 | uchar tmp; |
| 61 | |
| 62 | debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 63 | tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday, |
| 64 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 65 | |
| 66 | memset(buffer, 0, 7); |
Albin Tonnerre | 8aca720 | 2009-08-13 15:31:11 +0200 | [diff] [blame] | 67 | buffer[0] = bin2bcd(tm->tm_sec); |
| 68 | buffer[1] = bin2bcd(tm->tm_min); |
| 69 | buffer[2] = bin2bcd(tm->tm_hour); |
| 70 | buffer[3] = bin2bcd(tm->tm_wday); |
| 71 | tmp = bin2bcd(tm->tm_mday); |
Detlev Zundel | 58d0bff | 2009-03-30 00:31:33 +0200 | [diff] [blame] | 72 | buffer[3] |= (tmp & 0x0F) << 4; |
| 73 | buffer[4] = (tmp & 0xF0) >> 4; |
Albin Tonnerre | 8aca720 | 2009-08-13 15:31:11 +0200 | [diff] [blame] | 74 | tmp = bin2bcd(tm->tm_mon); |
Detlev Zundel | 58d0bff | 2009-03-30 00:31:33 +0200 | [diff] [blame] | 75 | buffer[4] |= (tmp & 0x0F) << 4; |
| 76 | buffer[5] = (tmp & 0xF0) >> 4; |
Albin Tonnerre | 8aca720 | 2009-08-13 15:31:11 +0200 | [diff] [blame] | 77 | tmp = bin2bcd(tm->tm_year % 100); |
Detlev Zundel | 58d0bff | 2009-03-30 00:31:33 +0200 | [diff] [blame] | 78 | buffer[5] |= (tmp & 0x0F) << 4; |
| 79 | buffer[6] = (tmp & 0xF0) >> 4; |
| 80 | |
| 81 | /* Write the resulting 52 bits to device */ |
| 82 | tws_write(buffer, 52); |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | void rtc_reset(void) |
| 88 | { |
| 89 | struct rtc_time tmp; |
| 90 | |
| 91 | tmp.tm_sec = 0; |
| 92 | tmp.tm_min = 0; |
| 93 | tmp.tm_hour = 0; |
| 94 | tmp.tm_wday = 4; |
| 95 | tmp.tm_mday = 1; |
| 96 | tmp.tm_mon = 1; |
| 97 | tmp.tm_year = 2000; |
| 98 | rtc_set(&tmp); |
| 99 | } |
| 100 | |
| 101 | #endif |