10c698dcaSJean-Christophe PLAGNIOL-VILLARD /* 20c698dcaSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2001, 2002, 2003 30c698dcaSJean-Christophe PLAGNIOL-VILLARD * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 40c698dcaSJean-Christophe PLAGNIOL-VILLARD * Keith Outwater, keith_outwater@mvis.com` 50c698dcaSJean-Christophe PLAGNIOL-VILLARD * Steven Scholz, steven.scholz@imc-berlin.de 60c698dcaSJean-Christophe PLAGNIOL-VILLARD * 70c698dcaSJean-Christophe PLAGNIOL-VILLARD * See file CREDITS for list of people who contributed to this 80c698dcaSJean-Christophe PLAGNIOL-VILLARD * project. 90c698dcaSJean-Christophe PLAGNIOL-VILLARD * 100c698dcaSJean-Christophe PLAGNIOL-VILLARD * This program is free software; you can redistribute it and/or 110c698dcaSJean-Christophe PLAGNIOL-VILLARD * modify it under the terms of the GNU General Public License as 120c698dcaSJean-Christophe PLAGNIOL-VILLARD * published by the Free Software Foundation; either version 2 of 130c698dcaSJean-Christophe PLAGNIOL-VILLARD * the License, or (at your option) any later version. 140c698dcaSJean-Christophe PLAGNIOL-VILLARD * 150c698dcaSJean-Christophe PLAGNIOL-VILLARD * This program is distributed in the hope that it will be useful, 160c698dcaSJean-Christophe PLAGNIOL-VILLARD * but WITHOUT ANY WARRANTY; without even the implied warranty of 170c698dcaSJean-Christophe PLAGNIOL-VILLARD * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 180c698dcaSJean-Christophe PLAGNIOL-VILLARD * GNU General Public License for more details. 190c698dcaSJean-Christophe PLAGNIOL-VILLARD * 200c698dcaSJean-Christophe PLAGNIOL-VILLARD * You should have received a copy of the GNU General Public License 210c698dcaSJean-Christophe PLAGNIOL-VILLARD * along with this program; if not, write to the Free Software 220c698dcaSJean-Christophe PLAGNIOL-VILLARD * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 230c698dcaSJean-Christophe PLAGNIOL-VILLARD * MA 02111-1307 USA 240c698dcaSJean-Christophe PLAGNIOL-VILLARD */ 250c698dcaSJean-Christophe PLAGNIOL-VILLARD 260c698dcaSJean-Christophe PLAGNIOL-VILLARD /* 270c698dcaSJean-Christophe PLAGNIOL-VILLARD * Date & Time support (no alarms) for Dallas Semiconductor (now Maxim) 280c698dcaSJean-Christophe PLAGNIOL-VILLARD * DS1307 and DS1338 Real Time Clock (RTC). 290c698dcaSJean-Christophe PLAGNIOL-VILLARD * 300c698dcaSJean-Christophe PLAGNIOL-VILLARD * based on ds1337.c 310c698dcaSJean-Christophe PLAGNIOL-VILLARD */ 320c698dcaSJean-Christophe PLAGNIOL-VILLARD 330c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <common.h> 340c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <command.h> 350c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <rtc.h> 360c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <i2c.h> 370c698dcaSJean-Christophe PLAGNIOL-VILLARD 38871c18ddSMichal Simek #if defined(CONFIG_CMD_DATE) 390c698dcaSJean-Christophe PLAGNIOL-VILLARD 400c698dcaSJean-Christophe PLAGNIOL-VILLARD /*---------------------------------------------------------------------*/ 410c698dcaSJean-Christophe PLAGNIOL-VILLARD #undef DEBUG_RTC 420c698dcaSJean-Christophe PLAGNIOL-VILLARD 430c698dcaSJean-Christophe PLAGNIOL-VILLARD #ifdef DEBUG_RTC 440c698dcaSJean-Christophe PLAGNIOL-VILLARD #define DEBUGR(fmt,args...) printf(fmt ,##args) 450c698dcaSJean-Christophe PLAGNIOL-VILLARD #else 460c698dcaSJean-Christophe PLAGNIOL-VILLARD #define DEBUGR(fmt,args...) 470c698dcaSJean-Christophe PLAGNIOL-VILLARD #endif 480c698dcaSJean-Christophe PLAGNIOL-VILLARD /*---------------------------------------------------------------------*/ 490c698dcaSJean-Christophe PLAGNIOL-VILLARD 50*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_SYS_I2C_RTC_ADDR 51*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # define CONFIG_SYS_I2C_RTC_ADDR 0x68 520c698dcaSJean-Christophe PLAGNIOL-VILLARD #endif 530c698dcaSJean-Christophe PLAGNIOL-VILLARD 54*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_RTC_DS1307) && (CONFIG_SYS_I2C_SPEED > 100000) 550c698dcaSJean-Christophe PLAGNIOL-VILLARD # error The DS1307 is specified only up to 100kHz! 560c698dcaSJean-Christophe PLAGNIOL-VILLARD #endif 570c698dcaSJean-Christophe PLAGNIOL-VILLARD 580c698dcaSJean-Christophe PLAGNIOL-VILLARD /* 590c698dcaSJean-Christophe PLAGNIOL-VILLARD * RTC register addresses 600c698dcaSJean-Christophe PLAGNIOL-VILLARD */ 610c698dcaSJean-Christophe PLAGNIOL-VILLARD #define RTC_SEC_REG_ADDR 0x00 620c698dcaSJean-Christophe PLAGNIOL-VILLARD #define RTC_MIN_REG_ADDR 0x01 630c698dcaSJean-Christophe PLAGNIOL-VILLARD #define RTC_HR_REG_ADDR 0x02 640c698dcaSJean-Christophe PLAGNIOL-VILLARD #define RTC_DAY_REG_ADDR 0x03 650c698dcaSJean-Christophe PLAGNIOL-VILLARD #define RTC_DATE_REG_ADDR 0x04 660c698dcaSJean-Christophe PLAGNIOL-VILLARD #define RTC_MON_REG_ADDR 0x05 670c698dcaSJean-Christophe PLAGNIOL-VILLARD #define RTC_YR_REG_ADDR 0x06 680c698dcaSJean-Christophe PLAGNIOL-VILLARD #define RTC_CTL_REG_ADDR 0x07 690c698dcaSJean-Christophe PLAGNIOL-VILLARD 700c698dcaSJean-Christophe PLAGNIOL-VILLARD #define RTC_SEC_BIT_CH 0x80 /* Clock Halt (in Register 0) */ 710c698dcaSJean-Christophe PLAGNIOL-VILLARD 720c698dcaSJean-Christophe PLAGNIOL-VILLARD #define RTC_CTL_BIT_RS0 0x01 /* Rate select 0 */ 730c698dcaSJean-Christophe PLAGNIOL-VILLARD #define RTC_CTL_BIT_RS1 0x02 /* Rate select 1 */ 740c698dcaSJean-Christophe PLAGNIOL-VILLARD #define RTC_CTL_BIT_SQWE 0x10 /* Square Wave Enable */ 750c698dcaSJean-Christophe PLAGNIOL-VILLARD #define RTC_CTL_BIT_OUT 0x80 /* Output Control */ 760c698dcaSJean-Christophe PLAGNIOL-VILLARD 770c698dcaSJean-Christophe PLAGNIOL-VILLARD static uchar rtc_read (uchar reg); 780c698dcaSJean-Christophe PLAGNIOL-VILLARD static void rtc_write (uchar reg, uchar val); 790c698dcaSJean-Christophe PLAGNIOL-VILLARD static uchar bin2bcd (unsigned int n); 800c698dcaSJean-Christophe PLAGNIOL-VILLARD static unsigned bcd2bin (uchar c); 810c698dcaSJean-Christophe PLAGNIOL-VILLARD 820c698dcaSJean-Christophe PLAGNIOL-VILLARD /* 830c698dcaSJean-Christophe PLAGNIOL-VILLARD * Get the current time from the RTC 840c698dcaSJean-Christophe PLAGNIOL-VILLARD */ 85b73a19e1SYuri Tikhonov int rtc_get (struct rtc_time *tmp) 860c698dcaSJean-Christophe PLAGNIOL-VILLARD { 87b73a19e1SYuri Tikhonov int rel = 0; 880c698dcaSJean-Christophe PLAGNIOL-VILLARD uchar sec, min, hour, mday, wday, mon, year; 890c698dcaSJean-Christophe PLAGNIOL-VILLARD 900c698dcaSJean-Christophe PLAGNIOL-VILLARD sec = rtc_read (RTC_SEC_REG_ADDR); 910c698dcaSJean-Christophe PLAGNIOL-VILLARD min = rtc_read (RTC_MIN_REG_ADDR); 920c698dcaSJean-Christophe PLAGNIOL-VILLARD hour = rtc_read (RTC_HR_REG_ADDR); 930c698dcaSJean-Christophe PLAGNIOL-VILLARD wday = rtc_read (RTC_DAY_REG_ADDR); 940c698dcaSJean-Christophe PLAGNIOL-VILLARD mday = rtc_read (RTC_DATE_REG_ADDR); 950c698dcaSJean-Christophe PLAGNIOL-VILLARD mon = rtc_read (RTC_MON_REG_ADDR); 960c698dcaSJean-Christophe PLAGNIOL-VILLARD year = rtc_read (RTC_YR_REG_ADDR); 970c698dcaSJean-Christophe PLAGNIOL-VILLARD 980c698dcaSJean-Christophe PLAGNIOL-VILLARD DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x " 990c698dcaSJean-Christophe PLAGNIOL-VILLARD "hr: %02x min: %02x sec: %02x\n", 1000c698dcaSJean-Christophe PLAGNIOL-VILLARD year, mon, mday, wday, hour, min, sec); 1010c698dcaSJean-Christophe PLAGNIOL-VILLARD 1020c698dcaSJean-Christophe PLAGNIOL-VILLARD if (sec & RTC_SEC_BIT_CH) { 1030c698dcaSJean-Christophe PLAGNIOL-VILLARD printf ("### Warning: RTC oscillator has stopped\n"); 1040c698dcaSJean-Christophe PLAGNIOL-VILLARD /* clear the CH flag */ 1050c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_SEC_REG_ADDR, 1060c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_read (RTC_SEC_REG_ADDR) & ~RTC_SEC_BIT_CH); 107b73a19e1SYuri Tikhonov rel = -1; 1080c698dcaSJean-Christophe PLAGNIOL-VILLARD } 1090c698dcaSJean-Christophe PLAGNIOL-VILLARD 1100c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_sec = bcd2bin (sec & 0x7F); 1110c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_min = bcd2bin (min & 0x7F); 1120c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_hour = bcd2bin (hour & 0x3F); 1130c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_mday = bcd2bin (mday & 0x3F); 1140c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_mon = bcd2bin (mon & 0x1F); 1150c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000); 1160c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_wday = bcd2bin ((wday - 1) & 0x07); 1170c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_yday = 0; 1180c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_isdst= 0; 1190c698dcaSJean-Christophe PLAGNIOL-VILLARD 1200c698dcaSJean-Christophe PLAGNIOL-VILLARD DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", 1210c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, 1220c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_hour, tmp->tm_min, tmp->tm_sec); 123b73a19e1SYuri Tikhonov 124b73a19e1SYuri Tikhonov return rel; 1250c698dcaSJean-Christophe PLAGNIOL-VILLARD } 1260c698dcaSJean-Christophe PLAGNIOL-VILLARD 1270c698dcaSJean-Christophe PLAGNIOL-VILLARD 1280c698dcaSJean-Christophe PLAGNIOL-VILLARD /* 1290c698dcaSJean-Christophe PLAGNIOL-VILLARD * Set the RTC 1300c698dcaSJean-Christophe PLAGNIOL-VILLARD */ 131d1e23194SJean-Christophe PLAGNIOL-VILLARD int rtc_set (struct rtc_time *tmp) 1320c698dcaSJean-Christophe PLAGNIOL-VILLARD { 1330c698dcaSJean-Christophe PLAGNIOL-VILLARD DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", 1340c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, 1350c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_hour, tmp->tm_min, tmp->tm_sec); 1360c698dcaSJean-Christophe PLAGNIOL-VILLARD 1370c698dcaSJean-Christophe PLAGNIOL-VILLARD if (tmp->tm_year < 1970 || tmp->tm_year > 2069) 1380c698dcaSJean-Christophe PLAGNIOL-VILLARD printf("WARNING: year should be between 1970 and 2069!\n"); 1390c698dcaSJean-Christophe PLAGNIOL-VILLARD 1400c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100)); 1410c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon)); 1420c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday + 1)); 1430c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday)); 1440c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour)); 1450c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min)); 1460c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec)); 147d1e23194SJean-Christophe PLAGNIOL-VILLARD 148d1e23194SJean-Christophe PLAGNIOL-VILLARD return 0; 1490c698dcaSJean-Christophe PLAGNIOL-VILLARD } 1500c698dcaSJean-Christophe PLAGNIOL-VILLARD 1510c698dcaSJean-Christophe PLAGNIOL-VILLARD 1520c698dcaSJean-Christophe PLAGNIOL-VILLARD /* 1530c698dcaSJean-Christophe PLAGNIOL-VILLARD * Reset the RTC. We setting the date back to 1970-01-01. 1540c698dcaSJean-Christophe PLAGNIOL-VILLARD * We also enable the oscillator output on the SQW/OUT pin and program 1550c698dcaSJean-Christophe PLAGNIOL-VILLARD * it for 32,768 Hz output. Note that according to the datasheet, turning 1560c698dcaSJean-Christophe PLAGNIOL-VILLARD * on the square wave output increases the current drain on the backup 1570c698dcaSJean-Christophe PLAGNIOL-VILLARD * battery to something between 480nA and 800nA. 1580c698dcaSJean-Christophe PLAGNIOL-VILLARD */ 1590c698dcaSJean-Christophe PLAGNIOL-VILLARD void rtc_reset (void) 1600c698dcaSJean-Christophe PLAGNIOL-VILLARD { 1610c698dcaSJean-Christophe PLAGNIOL-VILLARD struct rtc_time tmp; 1620c698dcaSJean-Christophe PLAGNIOL-VILLARD 1630c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_SEC_REG_ADDR, 0x00); /* clearing Clock Halt */ 1640c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_CTL_REG_ADDR, RTC_CTL_BIT_SQWE | RTC_CTL_BIT_RS1 | RTC_CTL_BIT_RS0); 1650c698dcaSJean-Christophe PLAGNIOL-VILLARD 1660c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp.tm_year = 1970; 1670c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp.tm_mon = 1; 1680c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp.tm_mday= 1; 1690c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp.tm_hour = 0; 1700c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp.tm_min = 0; 1710c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp.tm_sec = 0; 1720c698dcaSJean-Christophe PLAGNIOL-VILLARD 1730c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_set(&tmp); 1740c698dcaSJean-Christophe PLAGNIOL-VILLARD 1750c698dcaSJean-Christophe PLAGNIOL-VILLARD printf ( "RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n", 1760c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp.tm_year, tmp.tm_mon, tmp.tm_mday, 1770c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp.tm_hour, tmp.tm_min, tmp.tm_sec); 1780c698dcaSJean-Christophe PLAGNIOL-VILLARD 1790c698dcaSJean-Christophe PLAGNIOL-VILLARD return; 1800c698dcaSJean-Christophe PLAGNIOL-VILLARD } 1810c698dcaSJean-Christophe PLAGNIOL-VILLARD 1820c698dcaSJean-Christophe PLAGNIOL-VILLARD 1830c698dcaSJean-Christophe PLAGNIOL-VILLARD /* 1840c698dcaSJean-Christophe PLAGNIOL-VILLARD * Helper functions 1850c698dcaSJean-Christophe PLAGNIOL-VILLARD */ 1860c698dcaSJean-Christophe PLAGNIOL-VILLARD 1870c698dcaSJean-Christophe PLAGNIOL-VILLARD static 1880c698dcaSJean-Christophe PLAGNIOL-VILLARD uchar rtc_read (uchar reg) 1890c698dcaSJean-Christophe PLAGNIOL-VILLARD { 190*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg)); 1910c698dcaSJean-Christophe PLAGNIOL-VILLARD } 1920c698dcaSJean-Christophe PLAGNIOL-VILLARD 1930c698dcaSJean-Christophe PLAGNIOL-VILLARD 1940c698dcaSJean-Christophe PLAGNIOL-VILLARD static void rtc_write (uchar reg, uchar val) 1950c698dcaSJean-Christophe PLAGNIOL-VILLARD { 196*6d0f6bcfSJean-Christophe PLAGNIOL-VILLARD i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val); 1970c698dcaSJean-Christophe PLAGNIOL-VILLARD } 1980c698dcaSJean-Christophe PLAGNIOL-VILLARD 1990c698dcaSJean-Christophe PLAGNIOL-VILLARD static unsigned bcd2bin (uchar n) 2000c698dcaSJean-Christophe PLAGNIOL-VILLARD { 2010c698dcaSJean-Christophe PLAGNIOL-VILLARD return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F)); 2020c698dcaSJean-Christophe PLAGNIOL-VILLARD } 2030c698dcaSJean-Christophe PLAGNIOL-VILLARD 2040c698dcaSJean-Christophe PLAGNIOL-VILLARD static unsigned char bin2bcd (unsigned int n) 2050c698dcaSJean-Christophe PLAGNIOL-VILLARD { 2060c698dcaSJean-Christophe PLAGNIOL-VILLARD return (((n / 10) << 4) | (n % 10)); 2070c698dcaSJean-Christophe PLAGNIOL-VILLARD } 2080c698dcaSJean-Christophe PLAGNIOL-VILLARD 2090c698dcaSJean-Christophe PLAGNIOL-VILLARD #endif 210