10c698dcaSJean-Christophe PLAGNIOL-VILLARD /* 20c698dcaSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2004 30c698dcaSJean-Christophe PLAGNIOL-VILLARD * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 40c698dcaSJean-Christophe PLAGNIOL-VILLARD * 50c698dcaSJean-Christophe PLAGNIOL-VILLARD * See file CREDITS for list of people who contributed to this 60c698dcaSJean-Christophe PLAGNIOL-VILLARD * project. 70c698dcaSJean-Christophe PLAGNIOL-VILLARD * 80c698dcaSJean-Christophe PLAGNIOL-VILLARD * This program is free software; you can redistribute it and/or 90c698dcaSJean-Christophe PLAGNIOL-VILLARD * modify it under the terms of the GNU General Public License as 100c698dcaSJean-Christophe PLAGNIOL-VILLARD * published by the Free Software Foundation; either version 2 of 110c698dcaSJean-Christophe PLAGNIOL-VILLARD * the License, or (at your option) any later version. 120c698dcaSJean-Christophe PLAGNIOL-VILLARD * 130c698dcaSJean-Christophe PLAGNIOL-VILLARD * This program is distributed in the hope that it will be useful, 140c698dcaSJean-Christophe PLAGNIOL-VILLARD * but WITHOUT ANY WARRANTY; without even the implied warranty of 150c698dcaSJean-Christophe PLAGNIOL-VILLARD * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 160c698dcaSJean-Christophe PLAGNIOL-VILLARD * GNU General Public License for more details. 170c698dcaSJean-Christophe PLAGNIOL-VILLARD * 180c698dcaSJean-Christophe PLAGNIOL-VILLARD * You should have received a copy of the GNU General Public License 190c698dcaSJean-Christophe PLAGNIOL-VILLARD * along with this program; if not, write to the Free Software 200c698dcaSJean-Christophe PLAGNIOL-VILLARD * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 210c698dcaSJean-Christophe PLAGNIOL-VILLARD * MA 02111-1307 USA 220c698dcaSJean-Christophe PLAGNIOL-VILLARD */ 230c698dcaSJean-Christophe PLAGNIOL-VILLARD 240c698dcaSJean-Christophe PLAGNIOL-VILLARD /* 250c698dcaSJean-Christophe PLAGNIOL-VILLARD * Date & Time support for MAXIM MAX6900 RTC 260c698dcaSJean-Christophe PLAGNIOL-VILLARD */ 270c698dcaSJean-Christophe PLAGNIOL-VILLARD 280c698dcaSJean-Christophe PLAGNIOL-VILLARD /* #define DEBUG */ 290c698dcaSJean-Christophe PLAGNIOL-VILLARD 300c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <common.h> 310c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <command.h> 320c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <rtc.h> 330c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <i2c.h> 340c698dcaSJean-Christophe PLAGNIOL-VILLARD 35871c18ddSMichal Simek #if defined(CONFIG_CMD_DATE) 360c698dcaSJean-Christophe PLAGNIOL-VILLARD 37*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_SYS_I2C_RTC_ADDR 38*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_I2C_RTC_ADDR 0x50 390c698dcaSJean-Christophe PLAGNIOL-VILLARD #endif 400c698dcaSJean-Christophe PLAGNIOL-VILLARD 410c698dcaSJean-Christophe PLAGNIOL-VILLARD /* ------------------------------------------------------------------------- */ 420c698dcaSJean-Christophe PLAGNIOL-VILLARD 430c698dcaSJean-Christophe PLAGNIOL-VILLARD static uchar rtc_read (uchar reg) 440c698dcaSJean-Christophe PLAGNIOL-VILLARD { 45*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg)); 460c698dcaSJean-Christophe PLAGNIOL-VILLARD } 470c698dcaSJean-Christophe PLAGNIOL-VILLARD 480c698dcaSJean-Christophe PLAGNIOL-VILLARD static void rtc_write (uchar reg, uchar val) 490c698dcaSJean-Christophe PLAGNIOL-VILLARD { 50*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val); 510c698dcaSJean-Christophe PLAGNIOL-VILLARD udelay(2500); 520c698dcaSJean-Christophe PLAGNIOL-VILLARD } 530c698dcaSJean-Christophe PLAGNIOL-VILLARD 540c698dcaSJean-Christophe PLAGNIOL-VILLARD static unsigned bcd2bin (uchar n) 550c698dcaSJean-Christophe PLAGNIOL-VILLARD { 560c698dcaSJean-Christophe PLAGNIOL-VILLARD return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F)); 570c698dcaSJean-Christophe PLAGNIOL-VILLARD } 580c698dcaSJean-Christophe PLAGNIOL-VILLARD 590c698dcaSJean-Christophe PLAGNIOL-VILLARD static unsigned char bin2bcd (unsigned int n) 600c698dcaSJean-Christophe PLAGNIOL-VILLARD { 610c698dcaSJean-Christophe PLAGNIOL-VILLARD return (((n / 10) << 4) | (n % 10)); 620c698dcaSJean-Christophe PLAGNIOL-VILLARD } 630c698dcaSJean-Christophe PLAGNIOL-VILLARD 640c698dcaSJean-Christophe PLAGNIOL-VILLARD /* ------------------------------------------------------------------------- */ 650c698dcaSJean-Christophe PLAGNIOL-VILLARD 66b73a19e1SYuri Tikhonov int rtc_get (struct rtc_time *tmp) 670c698dcaSJean-Christophe PLAGNIOL-VILLARD { 680c698dcaSJean-Christophe PLAGNIOL-VILLARD uchar sec, min, hour, mday, wday, mon, cent, year; 690c698dcaSJean-Christophe PLAGNIOL-VILLARD int retry = 1; 700c698dcaSJean-Christophe PLAGNIOL-VILLARD 710c698dcaSJean-Christophe PLAGNIOL-VILLARD do { 720c698dcaSJean-Christophe PLAGNIOL-VILLARD sec = rtc_read (0x80); 730c698dcaSJean-Christophe PLAGNIOL-VILLARD min = rtc_read (0x82); 740c698dcaSJean-Christophe PLAGNIOL-VILLARD hour = rtc_read (0x84); 750c698dcaSJean-Christophe PLAGNIOL-VILLARD mday = rtc_read (0x86); 760c698dcaSJean-Christophe PLAGNIOL-VILLARD mon = rtc_read (0x88); 770c698dcaSJean-Christophe PLAGNIOL-VILLARD wday = rtc_read (0x8a); 780c698dcaSJean-Christophe PLAGNIOL-VILLARD year = rtc_read (0x8c); 790c698dcaSJean-Christophe PLAGNIOL-VILLARD cent = rtc_read (0x92); 800c698dcaSJean-Christophe PLAGNIOL-VILLARD /* 810c698dcaSJean-Christophe PLAGNIOL-VILLARD * Check for seconds rollover 820c698dcaSJean-Christophe PLAGNIOL-VILLARD */ 830c698dcaSJean-Christophe PLAGNIOL-VILLARD if ((sec != 59) || (rtc_read(0x80) == sec)){ 840c698dcaSJean-Christophe PLAGNIOL-VILLARD retry = 0; 850c698dcaSJean-Christophe PLAGNIOL-VILLARD } 860c698dcaSJean-Christophe PLAGNIOL-VILLARD } while (retry); 870c698dcaSJean-Christophe PLAGNIOL-VILLARD 880c698dcaSJean-Christophe PLAGNIOL-VILLARD debug ( "Get RTC year: %02x mon: %02x cent: %02x mday: %02x wday: %02x " 890c698dcaSJean-Christophe PLAGNIOL-VILLARD "hr: %02x min: %02x sec: %02x\n", 900c698dcaSJean-Christophe PLAGNIOL-VILLARD year, mon, cent, mday, wday, 910c698dcaSJean-Christophe PLAGNIOL-VILLARD hour, min, sec ); 920c698dcaSJean-Christophe PLAGNIOL-VILLARD 930c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_sec = bcd2bin (sec & 0x7F); 940c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_min = bcd2bin (min & 0x7F); 950c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_hour = bcd2bin (hour & 0x3F); 960c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_mday = bcd2bin (mday & 0x3F); 970c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_mon = bcd2bin (mon & 0x1F); 980c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_year = bcd2bin (year) + bcd2bin(cent) * 100; 990c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_wday = bcd2bin (wday & 0x07); 1000c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_yday = 0; 1010c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_isdst= 0; 1020c698dcaSJean-Christophe PLAGNIOL-VILLARD 1030c698dcaSJean-Christophe PLAGNIOL-VILLARD debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", 1040c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, 1050c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_hour, tmp->tm_min, tmp->tm_sec); 106b73a19e1SYuri Tikhonov 107b73a19e1SYuri Tikhonov return 0; 1080c698dcaSJean-Christophe PLAGNIOL-VILLARD } 1090c698dcaSJean-Christophe PLAGNIOL-VILLARD 110d1e23194SJean-Christophe PLAGNIOL-VILLARD int rtc_set (struct rtc_time *tmp) 1110c698dcaSJean-Christophe PLAGNIOL-VILLARD { 1120c698dcaSJean-Christophe PLAGNIOL-VILLARD 1130c698dcaSJean-Christophe PLAGNIOL-VILLARD debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", 1140c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, 1150c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_hour, tmp->tm_min, tmp->tm_sec); 1160c698dcaSJean-Christophe PLAGNIOL-VILLARD 1170c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x9E, 0x00); 1180c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x80, 0); /* Clear seconds to ensure no rollover */ 1190c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x92, bin2bcd(tmp->tm_year / 100)); 1200c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x8c, bin2bcd(tmp->tm_year % 100)); 1210c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x8a, bin2bcd(tmp->tm_wday)); 1220c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x88, bin2bcd(tmp->tm_mon)); 1230c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x86, bin2bcd(tmp->tm_mday)); 1240c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x84, bin2bcd(tmp->tm_hour)); 1250c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x82, bin2bcd(tmp->tm_min )); 1260c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x80, bin2bcd(tmp->tm_sec )); 127d1e23194SJean-Christophe PLAGNIOL-VILLARD 128d1e23194SJean-Christophe PLAGNIOL-VILLARD return 0; 1290c698dcaSJean-Christophe PLAGNIOL-VILLARD } 1300c698dcaSJean-Christophe PLAGNIOL-VILLARD 1310c698dcaSJean-Christophe PLAGNIOL-VILLARD void rtc_reset (void) 1320c698dcaSJean-Christophe PLAGNIOL-VILLARD { 1330c698dcaSJean-Christophe PLAGNIOL-VILLARD } 1340c698dcaSJean-Christophe PLAGNIOL-VILLARD 1350c698dcaSJean-Christophe PLAGNIOL-VILLARD #endif 136