wdenk | 4c0d4c3 | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 4c0d4c3 | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Date & Time support for MAXIM MAX6900 RTC |
| 10 | */ |
| 11 | |
| 12 | /* #define DEBUG */ |
| 13 | |
| 14 | #include <common.h> |
| 15 | #include <command.h> |
| 16 | #include <rtc.h> |
| 17 | #include <i2c.h> |
| 18 | |
Michal Simek | 871c18d | 2008-07-14 19:45:37 +0200 | [diff] [blame] | 19 | #if defined(CONFIG_CMD_DATE) |
wdenk | 4c0d4c3 | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 20 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 21 | #ifndef CONFIG_SYS_I2C_RTC_ADDR |
| 22 | #define CONFIG_SYS_I2C_RTC_ADDR 0x50 |
wdenk | 4c0d4c3 | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | /* ------------------------------------------------------------------------- */ |
| 26 | |
| 27 | static uchar rtc_read (uchar reg) |
| 28 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 29 | return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg)); |
wdenk | 4c0d4c3 | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static void rtc_write (uchar reg, uchar val) |
| 33 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 34 | i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val); |
wdenk | 4c0d4c3 | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 35 | udelay(2500); |
| 36 | } |
| 37 | |
wdenk | 4c0d4c3 | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 38 | /* ------------------------------------------------------------------------- */ |
| 39 | |
Yuri Tikhonov | b73a19e | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 40 | int rtc_get (struct rtc_time *tmp) |
wdenk | 4c0d4c3 | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 41 | { |
| 42 | uchar sec, min, hour, mday, wday, mon, cent, year; |
| 43 | int retry = 1; |
| 44 | |
| 45 | do { |
| 46 | sec = rtc_read (0x80); |
| 47 | min = rtc_read (0x82); |
| 48 | hour = rtc_read (0x84); |
| 49 | mday = rtc_read (0x86); |
| 50 | mon = rtc_read (0x88); |
| 51 | wday = rtc_read (0x8a); |
| 52 | year = rtc_read (0x8c); |
| 53 | cent = rtc_read (0x92); |
| 54 | /* |
| 55 | * Check for seconds rollover |
| 56 | */ |
| 57 | if ((sec != 59) || (rtc_read(0x80) == sec)){ |
| 58 | retry = 0; |
| 59 | } |
| 60 | } while (retry); |
| 61 | |
| 62 | debug ( "Get RTC year: %02x mon: %02x cent: %02x mday: %02x wday: %02x " |
| 63 | "hr: %02x min: %02x sec: %02x\n", |
| 64 | year, mon, cent, mday, wday, |
| 65 | hour, min, sec ); |
| 66 | |
| 67 | tmp->tm_sec = bcd2bin (sec & 0x7F); |
| 68 | tmp->tm_min = bcd2bin (min & 0x7F); |
| 69 | tmp->tm_hour = bcd2bin (hour & 0x3F); |
| 70 | tmp->tm_mday = bcd2bin (mday & 0x3F); |
| 71 | tmp->tm_mon = bcd2bin (mon & 0x1F); |
| 72 | tmp->tm_year = bcd2bin (year) + bcd2bin(cent) * 100; |
| 73 | tmp->tm_wday = bcd2bin (wday & 0x07); |
| 74 | tmp->tm_yday = 0; |
| 75 | tmp->tm_isdst= 0; |
| 76 | |
| 77 | debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 78 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 79 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
Yuri Tikhonov | b73a19e | 2008-03-20 17:56:04 +0300 | [diff] [blame] | 80 | |
| 81 | return 0; |
wdenk | 4c0d4c3 | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 84 | int rtc_set (struct rtc_time *tmp) |
wdenk | 4c0d4c3 | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 85 | { |
| 86 | |
| 87 | debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 88 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
| 89 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 90 | |
| 91 | rtc_write (0x9E, 0x00); |
| 92 | rtc_write (0x80, 0); /* Clear seconds to ensure no rollover */ |
| 93 | rtc_write (0x92, bin2bcd(tmp->tm_year / 100)); |
| 94 | rtc_write (0x8c, bin2bcd(tmp->tm_year % 100)); |
| 95 | rtc_write (0x8a, bin2bcd(tmp->tm_wday)); |
| 96 | rtc_write (0x88, bin2bcd(tmp->tm_mon)); |
| 97 | rtc_write (0x86, bin2bcd(tmp->tm_mday)); |
| 98 | rtc_write (0x84, bin2bcd(tmp->tm_hour)); |
| 99 | rtc_write (0x82, bin2bcd(tmp->tm_min )); |
| 100 | rtc_write (0x80, bin2bcd(tmp->tm_sec )); |
Jean-Christophe PLAGNIOL-VILLARD | d1e2319 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 101 | |
| 102 | return 0; |
wdenk | 4c0d4c3 | 2004-06-09 17:34:58 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void rtc_reset (void) |
| 106 | { |
| 107 | } |
| 108 | |
Jon Loeliger | 068b60a | 2007-07-10 10:27:39 -0500 | [diff] [blame] | 109 | #endif |