blob: fe0b5fb605b5d773916617f19844915dc2f22631 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2002
3 * Andrew May, Viasat Inc, amay@viasat.com
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00006 */
7
8/*
9 * M41T11 Serial Access Timekeeper(R) SRAM
10 * can you believe a trademark on that?
11 */
12
wdenke6325152005-03-17 16:43:10 +000013/* #define DEBUG 1 */
14
wdenkaffae2b2002-08-17 09:36:01 +000015#include <common.h>
16#include <command.h>
17#include <rtc.h>
18#include <i2c.h>
19
20/*
21 I Don't have an example config file but this
22 is what should be done.
23
24#define CONFIG_RTC_M41T11 1
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020025#define CONFIG_SYS_I2C_RTC_ADDR 0x68
wdenkaffae2b2002-08-17 09:36:01 +000026#if 0
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020027#define CONFIG_SYS_M41T11_EXT_CENTURY_DATA
wdenkaffae2b2002-08-17 09:36:01 +000028#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020029#define CONFIG_SYS_M41T11_BASE_YEAR 2000
wdenkaffae2b2002-08-17 09:36:01 +000030#endif
31*/
32
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020033#if defined(CONFIG_SYS_I2C_RTC_ADDR) && defined(CONFIG_CMD_DATE)
wdenkaffae2b2002-08-17 09:36:01 +000034
wdenkaffae2b2002-08-17 09:36:01 +000035/* ------------------------------------------------------------------------- */
36/*
37 these are simple defines for the chip local to here so they aren't too
38 verbose
39 DAY/DATE aren't nice but that is how they are on the data sheet
40*/
41#define RTC_SEC_ADDR 0x0
42#define RTC_MIN_ADDR 0x1
43#define RTC_HOUR_ADDR 0x2
44#define RTC_DAY_ADDR 0x3
45#define RTC_DATE_ADDR 0x4
46#define RTC_MONTH_ADDR 0x5
47#define RTC_YEARS_ADDR 0x6
48
49#define RTC_REG_CNT 7
50
51#define RTC_CONTROL_ADDR 0x7
52
53
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020054#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
wdenkaffae2b2002-08-17 09:36:01 +000055
56#define REG_CNT (RTC_REG_CNT+1)
57
58/*
59 you only get 00-99 for the year we will asume you
60 want from the year 2000 if you don't set the config
61*/
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020062#ifndef CONFIG_SYS_M41T11_BASE_YEAR
63#define CONFIG_SYS_M41T11_BASE_YEAR 2000
wdenkaffae2b2002-08-17 09:36:01 +000064#endif
65
66#else
67/* we will store extra year info in byte 9*/
68#define M41T11_YEAR_DATA 0x8
69#define M41T11_YEAR_SIZE 1
70#define REG_CNT (RTC_REG_CNT+1+M41T11_YEAR_SIZE)
71#endif
72
73#define M41T11_STORAGE_SZ (64-REG_CNT)
74
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030075int rtc_get (struct rtc_time *tmp)
wdenkaffae2b2002-08-17 09:36:01 +000076{
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030077 int rel = 0;
wdenk8bde7f72003-06-27 21:31:46 +000078 uchar data[RTC_REG_CNT];
wdenkaffae2b2002-08-17 09:36:01 +000079
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020080 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
wdenkaffae2b2002-08-17 09:36:01 +000081
wdenk8bde7f72003-06-27 21:31:46 +000082 if( data[RTC_SEC_ADDR] & 0x80 ){
83 printf( "m41t11 RTC Clock stopped!!!\n" );
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030084 rel = -1;
wdenk8bde7f72003-06-27 21:31:46 +000085 }
wdenkaffae2b2002-08-17 09:36:01 +000086 tmp->tm_sec = bcd2bin (data[RTC_SEC_ADDR] & 0x7F);
87 tmp->tm_min = bcd2bin (data[RTC_MIN_ADDR] & 0x7F);
88 tmp->tm_hour = bcd2bin (data[RTC_HOUR_ADDR] & 0x3F);
89 tmp->tm_mday = bcd2bin (data[RTC_DATE_ADDR] & 0x3F);
90 tmp->tm_mon = bcd2bin (data[RTC_MONTH_ADDR]& 0x1F);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020091#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
92 tmp->tm_year = CONFIG_SYS_M41T11_BASE_YEAR
wdenk8bde7f72003-06-27 21:31:46 +000093 + bcd2bin(data[RTC_YEARS_ADDR])
94 + ((data[RTC_HOUR_ADDR]&0x40) ? 100 : 0);
wdenkaffae2b2002-08-17 09:36:01 +000095#else
wdenk8bde7f72003-06-27 21:31:46 +000096 {
97 unsigned char cent;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020098 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
wdenk8bde7f72003-06-27 21:31:46 +000099 if( !(data[RTC_HOUR_ADDR] & 0x80) ){
100 printf( "m41t11 RTC: cann't keep track of years without CEB set\n" );
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300101 rel = -1;
wdenk8bde7f72003-06-27 21:31:46 +0000102 }
103 if( (cent & 0x1) != ((data[RTC_HOUR_ADDR]&0x40)>>7) ){
104 /*century flip store off new year*/
105 cent += 1;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200106 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
wdenk8bde7f72003-06-27 21:31:46 +0000107 }
108 tmp->tm_year =((int)cent*100)+bcd2bin(data[RTC_YEARS_ADDR]);
109 }
wdenkaffae2b2002-08-17 09:36:01 +0000110#endif
111 tmp->tm_wday = bcd2bin (data[RTC_DAY_ADDR] & 0x07);
112 tmp->tm_yday = 0;
113 tmp->tm_isdst= 0;
114
115 debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
116 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
117 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300118
119 return rel;
wdenkaffae2b2002-08-17 09:36:01 +0000120}
121
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200122int rtc_set (struct rtc_time *tmp)
wdenkaffae2b2002-08-17 09:36:01 +0000123{
wdenk8bde7f72003-06-27 21:31:46 +0000124 uchar data[RTC_REG_CNT];
wdenkaffae2b2002-08-17 09:36:01 +0000125
126 debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
127 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
128 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
129
130 data[RTC_SEC_ADDR] = bin2bcd(tmp->tm_sec) & 0x7F;/*just in case*/
131 data[RTC_MIN_ADDR] = bin2bcd(tmp->tm_min);
132 data[RTC_HOUR_ADDR] = bin2bcd(tmp->tm_hour) & 0x3F;/*handle cent stuff later*/
133 data[RTC_DATE_ADDR] = bin2bcd(tmp->tm_mday) & 0x3F;
134 data[RTC_MONTH_ADDR] = bin2bcd(tmp->tm_mon);
135 data[RTC_DAY_ADDR] = bin2bcd(tmp->tm_wday) & 0x07;
136
wdenk8bde7f72003-06-27 21:31:46 +0000137 data[RTC_HOUR_ADDR] |= 0x80;/*we will always use CEB*/
wdenkaffae2b2002-08-17 09:36:01 +0000138
wdenk8bde7f72003-06-27 21:31:46 +0000139 data[RTC_YEARS_ADDR] = bin2bcd(tmp->tm_year%100);/*same thing either way*/
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200140#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
141 if( ((tmp->tm_year - CONFIG_SYS_M41T11_BASE_YEAR) > 200) ||
142 (tmp->tm_year < CONFIG_SYS_M41T11_BASE_YEAR) ){
wdenk8bde7f72003-06-27 21:31:46 +0000143 printf( "m41t11 RTC setting year out of range!!need recompile\n" );
144 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200145 data[RTC_HOUR_ADDR] |= (tmp->tm_year - CONFIG_SYS_M41T11_BASE_YEAR) > 100 ? 0x40 : 0;
wdenkaffae2b2002-08-17 09:36:01 +0000146#else
wdenk8bde7f72003-06-27 21:31:46 +0000147 {
148 unsigned char cent;
149 cent = tmp->tm_year ? tmp->tm_year / 100 : 0;
150 data[RTC_HOUR_ADDR] |= (cent & 0x1) ? 0x40 : 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200151 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
wdenk8bde7f72003-06-27 21:31:46 +0000152 }
wdenkaffae2b2002-08-17 09:36:01 +0000153#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200154 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200155
156 return 0;
wdenkaffae2b2002-08-17 09:36:01 +0000157}
158
159void rtc_reset (void)
160{
wdenk8bde7f72003-06-27 21:31:46 +0000161 unsigned char val;
wdenkaffae2b2002-08-17 09:36:01 +0000162 /* clear all control & status registers */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200163 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, 1);
wdenk8bde7f72003-06-27 21:31:46 +0000164 val = val & 0x7F;/*make sure we are running*/
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200165 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, RTC_REG_CNT);
wdenkaffae2b2002-08-17 09:36:01 +0000166
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200167 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
wdenk8bde7f72003-06-27 21:31:46 +0000168 val = val & 0x3F;/*turn off freq test keep calibration*/
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200169 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
wdenkaffae2b2002-08-17 09:36:01 +0000170}
Jon Loeliger068b60a2007-07-10 10:27:39 -0500171#endif