2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2002-08-17 09:36:01 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2002
|
|
|
|
* Andrew May, Viasat Inc, amay@viasat.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* M41T11 Serial Access Timekeeper(R) SRAM
|
|
|
|
* can you believe a trademark on that?
|
|
|
|
*/
|
|
|
|
|
2005-03-17 16:43:10 +00:00
|
|
|
/* #define DEBUG 1 */
|
|
|
|
|
2002-08-17 09:36:01 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <rtc.h>
|
|
|
|
#include <i2c.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
I Don't have an example config file but this
|
|
|
|
is what should be done.
|
|
|
|
|
|
|
|
#define CONFIG_RTC_M41T11 1
|
2008-10-16 13:01:15 +00:00
|
|
|
#define CONFIG_SYS_I2C_RTC_ADDR 0x68
|
2002-08-17 09:36:01 +00:00
|
|
|
#if 0
|
2008-10-16 13:01:15 +00:00
|
|
|
#define CONFIG_SYS_M41T11_EXT_CENTURY_DATA
|
2002-08-17 09:36:01 +00:00
|
|
|
#else
|
2008-10-16 13:01:15 +00:00
|
|
|
#define CONFIG_SYS_M41T11_BASE_YEAR 2000
|
2002-08-17 09:36:01 +00:00
|
|
|
#endif
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
/*
|
|
|
|
these are simple defines for the chip local to here so they aren't too
|
|
|
|
verbose
|
|
|
|
DAY/DATE aren't nice but that is how they are on the data sheet
|
|
|
|
*/
|
|
|
|
#define RTC_SEC_ADDR 0x0
|
|
|
|
#define RTC_MIN_ADDR 0x1
|
|
|
|
#define RTC_HOUR_ADDR 0x2
|
|
|
|
#define RTC_DAY_ADDR 0x3
|
|
|
|
#define RTC_DATE_ADDR 0x4
|
|
|
|
#define RTC_MONTH_ADDR 0x5
|
|
|
|
#define RTC_YEARS_ADDR 0x6
|
|
|
|
|
|
|
|
#define RTC_REG_CNT 7
|
|
|
|
|
|
|
|
#define RTC_CONTROL_ADDR 0x7
|
|
|
|
|
|
|
|
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
|
2002-08-17 09:36:01 +00:00
|
|
|
|
|
|
|
#define REG_CNT (RTC_REG_CNT+1)
|
|
|
|
|
|
|
|
/*
|
|
|
|
you only get 00-99 for the year we will asume you
|
|
|
|
want from the year 2000 if you don't set the config
|
|
|
|
*/
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifndef CONFIG_SYS_M41T11_BASE_YEAR
|
|
|
|
#define CONFIG_SYS_M41T11_BASE_YEAR 2000
|
2002-08-17 09:36:01 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* we will store extra year info in byte 9*/
|
|
|
|
#define M41T11_YEAR_DATA 0x8
|
|
|
|
#define M41T11_YEAR_SIZE 1
|
|
|
|
#define REG_CNT (RTC_REG_CNT+1+M41T11_YEAR_SIZE)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define M41T11_STORAGE_SZ (64-REG_CNT)
|
|
|
|
|
2008-03-20 14:56:04 +00:00
|
|
|
int rtc_get (struct rtc_time *tmp)
|
2002-08-17 09:36:01 +00:00
|
|
|
{
|
2008-03-20 14:56:04 +00:00
|
|
|
int rel = 0;
|
2003-06-27 21:31:46 +00:00
|
|
|
uchar data[RTC_REG_CNT];
|
2002-08-17 09:36:01 +00:00
|
|
|
|
2008-10-16 13:01:15 +00:00
|
|
|
i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
|
2002-08-17 09:36:01 +00:00
|
|
|
|
2003-06-27 21:31:46 +00:00
|
|
|
if( data[RTC_SEC_ADDR] & 0x80 ){
|
|
|
|
printf( "m41t11 RTC Clock stopped!!!\n" );
|
2008-03-20 14:56:04 +00:00
|
|
|
rel = -1;
|
2003-06-27 21:31:46 +00:00
|
|
|
}
|
2002-08-17 09:36:01 +00:00
|
|
|
tmp->tm_sec = bcd2bin (data[RTC_SEC_ADDR] & 0x7F);
|
|
|
|
tmp->tm_min = bcd2bin (data[RTC_MIN_ADDR] & 0x7F);
|
|
|
|
tmp->tm_hour = bcd2bin (data[RTC_HOUR_ADDR] & 0x3F);
|
|
|
|
tmp->tm_mday = bcd2bin (data[RTC_DATE_ADDR] & 0x3F);
|
|
|
|
tmp->tm_mon = bcd2bin (data[RTC_MONTH_ADDR]& 0x1F);
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
|
|
|
|
tmp->tm_year = CONFIG_SYS_M41T11_BASE_YEAR
|
2003-06-27 21:31:46 +00:00
|
|
|
+ bcd2bin(data[RTC_YEARS_ADDR])
|
|
|
|
+ ((data[RTC_HOUR_ADDR]&0x40) ? 100 : 0);
|
2002-08-17 09:36:01 +00:00
|
|
|
#else
|
2003-06-27 21:31:46 +00:00
|
|
|
{
|
|
|
|
unsigned char cent;
|
2008-10-16 13:01:15 +00:00
|
|
|
i2c_read(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, ¢, M41T11_YEAR_SIZE);
|
2003-06-27 21:31:46 +00:00
|
|
|
if( !(data[RTC_HOUR_ADDR] & 0x80) ){
|
|
|
|
printf( "m41t11 RTC: cann't keep track of years without CEB set\n" );
|
2008-03-20 14:56:04 +00:00
|
|
|
rel = -1;
|
2003-06-27 21:31:46 +00:00
|
|
|
}
|
|
|
|
if( (cent & 0x1) != ((data[RTC_HOUR_ADDR]&0x40)>>7) ){
|
|
|
|
/*century flip store off new year*/
|
|
|
|
cent += 1;
|
2008-10-16 13:01:15 +00:00
|
|
|
i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, ¢, M41T11_YEAR_SIZE);
|
2003-06-27 21:31:46 +00:00
|
|
|
}
|
|
|
|
tmp->tm_year =((int)cent*100)+bcd2bin(data[RTC_YEARS_ADDR]);
|
|
|
|
}
|
2002-08-17 09:36:01 +00:00
|
|
|
#endif
|
|
|
|
tmp->tm_wday = bcd2bin (data[RTC_DAY_ADDR] & 0x07);
|
|
|
|
tmp->tm_yday = 0;
|
|
|
|
tmp->tm_isdst= 0;
|
|
|
|
|
|
|
|
debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
|
|
|
|
tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
|
|
|
|
tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
|
2008-03-20 14:56:04 +00:00
|
|
|
|
|
|
|
return rel;
|
2002-08-17 09:36:01 +00:00
|
|
|
}
|
|
|
|
|
2008-09-01 21:06:23 +00:00
|
|
|
int rtc_set (struct rtc_time *tmp)
|
2002-08-17 09:36:01 +00:00
|
|
|
{
|
2003-06-27 21:31:46 +00:00
|
|
|
uchar data[RTC_REG_CNT];
|
2002-08-17 09:36:01 +00:00
|
|
|
|
|
|
|
debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
|
|
|
|
tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
|
|
|
|
tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
|
|
|
|
|
|
|
|
data[RTC_SEC_ADDR] = bin2bcd(tmp->tm_sec) & 0x7F;/*just in case*/
|
|
|
|
data[RTC_MIN_ADDR] = bin2bcd(tmp->tm_min);
|
|
|
|
data[RTC_HOUR_ADDR] = bin2bcd(tmp->tm_hour) & 0x3F;/*handle cent stuff later*/
|
|
|
|
data[RTC_DATE_ADDR] = bin2bcd(tmp->tm_mday) & 0x3F;
|
|
|
|
data[RTC_MONTH_ADDR] = bin2bcd(tmp->tm_mon);
|
|
|
|
data[RTC_DAY_ADDR] = bin2bcd(tmp->tm_wday) & 0x07;
|
|
|
|
|
2003-06-27 21:31:46 +00:00
|
|
|
data[RTC_HOUR_ADDR] |= 0x80;/*we will always use CEB*/
|
2002-08-17 09:36:01 +00:00
|
|
|
|
2003-06-27 21:31:46 +00:00
|
|
|
data[RTC_YEARS_ADDR] = bin2bcd(tmp->tm_year%100);/*same thing either way*/
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
|
|
|
|
if( ((tmp->tm_year - CONFIG_SYS_M41T11_BASE_YEAR) > 200) ||
|
|
|
|
(tmp->tm_year < CONFIG_SYS_M41T11_BASE_YEAR) ){
|
2003-06-27 21:31:46 +00:00
|
|
|
printf( "m41t11 RTC setting year out of range!!need recompile\n" );
|
|
|
|
}
|
2008-10-16 13:01:15 +00:00
|
|
|
data[RTC_HOUR_ADDR] |= (tmp->tm_year - CONFIG_SYS_M41T11_BASE_YEAR) > 100 ? 0x40 : 0;
|
2002-08-17 09:36:01 +00:00
|
|
|
#else
|
2003-06-27 21:31:46 +00:00
|
|
|
{
|
|
|
|
unsigned char cent;
|
|
|
|
cent = tmp->tm_year ? tmp->tm_year / 100 : 0;
|
|
|
|
data[RTC_HOUR_ADDR] |= (cent & 0x1) ? 0x40 : 0;
|
2008-10-16 13:01:15 +00:00
|
|
|
i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, ¢, M41T11_YEAR_SIZE);
|
2003-06-27 21:31:46 +00:00
|
|
|
}
|
2002-08-17 09:36:01 +00:00
|
|
|
#endif
|
2008-10-16 13:01:15 +00:00
|
|
|
i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
|
2008-09-01 21:06:23 +00:00
|
|
|
|
|
|
|
return 0;
|
2002-08-17 09:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_reset (void)
|
|
|
|
{
|
2003-06-27 21:31:46 +00:00
|
|
|
unsigned char val;
|
2002-08-17 09:36:01 +00:00
|
|
|
/* clear all control & status registers */
|
2008-10-16 13:01:15 +00:00
|
|
|
i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, 1);
|
2003-06-27 21:31:46 +00:00
|
|
|
val = val & 0x7F;/*make sure we are running*/
|
2008-10-16 13:01:15 +00:00
|
|
|
i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, RTC_REG_CNT);
|
2002-08-17 09:36:01 +00:00
|
|
|
|
2008-10-16 13:01:15 +00:00
|
|
|
i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
|
2003-06-27 21:31:46 +00:00
|
|
|
val = val & 0x3F;/*turn off freq test keep calibration*/
|
2008-10-16 13:01:15 +00:00
|
|
|
i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
|
2002-08-17 09:36:01 +00:00
|
|
|
}
|