blob: 960348bb3eaa9471fdc30f6525ec71c9a8183c22 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00002/*
3 * (C) Copyright 2002
4 * Andrew May, Viasat Inc, amay@viasat.com
wdenkaffae2b2002-08-17 09:36:01 +00005 */
6
7/*
8 * M41T11 Serial Access Timekeeper(R) SRAM
9 * can you believe a trademark on that?
10 */
11
wdenke6325152005-03-17 16:43:10 +000012/* #define DEBUG 1 */
13
wdenkaffae2b2002-08-17 09:36:01 +000014#include <common.h>
15#include <command.h>
16#include <rtc.h>
17#include <i2c.h>
18
19/*
20 I Don't have an example config file but this
21 is what should be done.
22
23#define CONFIG_RTC_M41T11 1
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020024#define CONFIG_SYS_I2C_RTC_ADDR 0x68
wdenkaffae2b2002-08-17 09:36:01 +000025#if 0
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020026#define CONFIG_SYS_M41T11_EXT_CENTURY_DATA
wdenkaffae2b2002-08-17 09:36:01 +000027#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020028#define CONFIG_SYS_M41T11_BASE_YEAR 2000
wdenkaffae2b2002-08-17 09:36:01 +000029#endif
30*/
31
wdenkaffae2b2002-08-17 09:36:01 +000032/* ------------------------------------------------------------------------- */
33/*
34 these are simple defines for the chip local to here so they aren't too
35 verbose
36 DAY/DATE aren't nice but that is how they are on the data sheet
37*/
38#define RTC_SEC_ADDR 0x0
39#define RTC_MIN_ADDR 0x1
40#define RTC_HOUR_ADDR 0x2
41#define RTC_DAY_ADDR 0x3
42#define RTC_DATE_ADDR 0x4
43#define RTC_MONTH_ADDR 0x5
44#define RTC_YEARS_ADDR 0x6
45
46#define RTC_REG_CNT 7
47
48#define RTC_CONTROL_ADDR 0x7
49
50
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020051#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
wdenkaffae2b2002-08-17 09:36:01 +000052
53#define REG_CNT (RTC_REG_CNT+1)
54
55/*
56 you only get 00-99 for the year we will asume you
57 want from the year 2000 if you don't set the config
58*/
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020059#ifndef CONFIG_SYS_M41T11_BASE_YEAR
60#define CONFIG_SYS_M41T11_BASE_YEAR 2000
wdenkaffae2b2002-08-17 09:36:01 +000061#endif
62
63#else
64/* we will store extra year info in byte 9*/
65#define M41T11_YEAR_DATA 0x8
66#define M41T11_YEAR_SIZE 1
67#define REG_CNT (RTC_REG_CNT+1+M41T11_YEAR_SIZE)
68#endif
69
70#define M41T11_STORAGE_SZ (64-REG_CNT)
71
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030072int rtc_get (struct rtc_time *tmp)
wdenkaffae2b2002-08-17 09:36:01 +000073{
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030074 int rel = 0;
wdenk8bde7f72003-06-27 21:31:46 +000075 uchar data[RTC_REG_CNT];
wdenkaffae2b2002-08-17 09:36:01 +000076
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020077 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
wdenkaffae2b2002-08-17 09:36:01 +000078
wdenk8bde7f72003-06-27 21:31:46 +000079 if( data[RTC_SEC_ADDR] & 0x80 ){
80 printf( "m41t11 RTC Clock stopped!!!\n" );
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030081 rel = -1;
wdenk8bde7f72003-06-27 21:31:46 +000082 }
wdenkaffae2b2002-08-17 09:36:01 +000083 tmp->tm_sec = bcd2bin (data[RTC_SEC_ADDR] & 0x7F);
84 tmp->tm_min = bcd2bin (data[RTC_MIN_ADDR] & 0x7F);
85 tmp->tm_hour = bcd2bin (data[RTC_HOUR_ADDR] & 0x3F);
86 tmp->tm_mday = bcd2bin (data[RTC_DATE_ADDR] & 0x3F);
87 tmp->tm_mon = bcd2bin (data[RTC_MONTH_ADDR]& 0x1F);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020088#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
89 tmp->tm_year = CONFIG_SYS_M41T11_BASE_YEAR
wdenk8bde7f72003-06-27 21:31:46 +000090 + bcd2bin(data[RTC_YEARS_ADDR])
91 + ((data[RTC_HOUR_ADDR]&0x40) ? 100 : 0);
wdenkaffae2b2002-08-17 09:36:01 +000092#else
wdenk8bde7f72003-06-27 21:31:46 +000093 {
94 unsigned char cent;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020095 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
wdenk8bde7f72003-06-27 21:31:46 +000096 if( !(data[RTC_HOUR_ADDR] & 0x80) ){
97 printf( "m41t11 RTC: cann't keep track of years without CEB set\n" );
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030098 rel = -1;
wdenk8bde7f72003-06-27 21:31:46 +000099 }
100 if( (cent & 0x1) != ((data[RTC_HOUR_ADDR]&0x40)>>7) ){
101 /*century flip store off new year*/
102 cent += 1;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200103 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
wdenk8bde7f72003-06-27 21:31:46 +0000104 }
105 tmp->tm_year =((int)cent*100)+bcd2bin(data[RTC_YEARS_ADDR]);
106 }
wdenkaffae2b2002-08-17 09:36:01 +0000107#endif
108 tmp->tm_wday = bcd2bin (data[RTC_DAY_ADDR] & 0x07);
109 tmp->tm_yday = 0;
110 tmp->tm_isdst= 0;
111
112 debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
113 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
114 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300115
116 return rel;
wdenkaffae2b2002-08-17 09:36:01 +0000117}
118
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200119int rtc_set (struct rtc_time *tmp)
wdenkaffae2b2002-08-17 09:36:01 +0000120{
wdenk8bde7f72003-06-27 21:31:46 +0000121 uchar data[RTC_REG_CNT];
wdenkaffae2b2002-08-17 09:36:01 +0000122
123 debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
124 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
125 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
126
127 data[RTC_SEC_ADDR] = bin2bcd(tmp->tm_sec) & 0x7F;/*just in case*/
128 data[RTC_MIN_ADDR] = bin2bcd(tmp->tm_min);
129 data[RTC_HOUR_ADDR] = bin2bcd(tmp->tm_hour) & 0x3F;/*handle cent stuff later*/
130 data[RTC_DATE_ADDR] = bin2bcd(tmp->tm_mday) & 0x3F;
131 data[RTC_MONTH_ADDR] = bin2bcd(tmp->tm_mon);
132 data[RTC_DAY_ADDR] = bin2bcd(tmp->tm_wday) & 0x07;
133
wdenk8bde7f72003-06-27 21:31:46 +0000134 data[RTC_HOUR_ADDR] |= 0x80;/*we will always use CEB*/
wdenkaffae2b2002-08-17 09:36:01 +0000135
wdenk8bde7f72003-06-27 21:31:46 +0000136 data[RTC_YEARS_ADDR] = bin2bcd(tmp->tm_year%100);/*same thing either way*/
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200137#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
138 if( ((tmp->tm_year - CONFIG_SYS_M41T11_BASE_YEAR) > 200) ||
139 (tmp->tm_year < CONFIG_SYS_M41T11_BASE_YEAR) ){
wdenk8bde7f72003-06-27 21:31:46 +0000140 printf( "m41t11 RTC setting year out of range!!need recompile\n" );
141 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200142 data[RTC_HOUR_ADDR] |= (tmp->tm_year - CONFIG_SYS_M41T11_BASE_YEAR) > 100 ? 0x40 : 0;
wdenkaffae2b2002-08-17 09:36:01 +0000143#else
wdenk8bde7f72003-06-27 21:31:46 +0000144 {
145 unsigned char cent;
146 cent = tmp->tm_year ? tmp->tm_year / 100 : 0;
147 data[RTC_HOUR_ADDR] |= (cent & 0x1) ? 0x40 : 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200148 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
wdenk8bde7f72003-06-27 21:31:46 +0000149 }
wdenkaffae2b2002-08-17 09:36:01 +0000150#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200151 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200152
153 return 0;
wdenkaffae2b2002-08-17 09:36:01 +0000154}
155
156void rtc_reset (void)
157{
wdenk8bde7f72003-06-27 21:31:46 +0000158 unsigned char val;
wdenkaffae2b2002-08-17 09:36:01 +0000159 /* clear all control & status registers */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200160 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, 1);
wdenk8bde7f72003-06-27 21:31:46 +0000161 val = val & 0x7F;/*make sure we are running*/
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200162 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, RTC_REG_CNT);
wdenkaffae2b2002-08-17 09:36:01 +0000163
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200164 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
wdenk8bde7f72003-06-27 21:31:46 +0000165 val = val & 0x3F;/*turn off freq test keep calibration*/
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200166 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
wdenkaffae2b2002-08-17 09:36:01 +0000167}