blob: f5c651bad0d3bef2bca6018eebc7b34ccac2e998 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk4c0d4c32004-06-09 17:34:58 +00002/*
3 * (C) Copyright 2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk4c0d4c32004-06-09 17:34:58 +00005 */
6
7/*
8 * Date & Time support for MAXIM MAX6900 RTC
9 */
10
11/* #define DEBUG */
12
13#include <common.h>
14#include <command.h>
15#include <rtc.h>
16#include <i2c.h>
17
Michal Simek871c18d2008-07-14 19:45:37 +020018#if defined(CONFIG_CMD_DATE)
wdenk4c0d4c32004-06-09 17:34:58 +000019
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020020#ifndef CONFIG_SYS_I2C_RTC_ADDR
21#define CONFIG_SYS_I2C_RTC_ADDR 0x50
wdenk4c0d4c32004-06-09 17:34:58 +000022#endif
23
24/* ------------------------------------------------------------------------- */
25
26static uchar rtc_read (uchar reg)
27{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020028 return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg));
wdenk4c0d4c32004-06-09 17:34:58 +000029}
30
31static void rtc_write (uchar reg, uchar val)
32{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020033 i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
wdenk4c0d4c32004-06-09 17:34:58 +000034 udelay(2500);
35}
36
wdenk4c0d4c32004-06-09 17:34:58 +000037/* ------------------------------------------------------------------------- */
38
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030039int rtc_get (struct rtc_time *tmp)
wdenk4c0d4c32004-06-09 17:34:58 +000040{
41 uchar sec, min, hour, mday, wday, mon, cent, year;
42 int retry = 1;
43
44 do {
45 sec = rtc_read (0x80);
46 min = rtc_read (0x82);
47 hour = rtc_read (0x84);
48 mday = rtc_read (0x86);
49 mon = rtc_read (0x88);
50 wday = rtc_read (0x8a);
51 year = rtc_read (0x8c);
52 cent = rtc_read (0x92);
53 /*
54 * Check for seconds rollover
55 */
56 if ((sec != 59) || (rtc_read(0x80) == sec)){
57 retry = 0;
58 }
59 } while (retry);
60
61 debug ( "Get RTC year: %02x mon: %02x cent: %02x mday: %02x wday: %02x "
62 "hr: %02x min: %02x sec: %02x\n",
63 year, mon, cent, mday, wday,
64 hour, min, sec );
65
66 tmp->tm_sec = bcd2bin (sec & 0x7F);
67 tmp->tm_min = bcd2bin (min & 0x7F);
68 tmp->tm_hour = bcd2bin (hour & 0x3F);
69 tmp->tm_mday = bcd2bin (mday & 0x3F);
70 tmp->tm_mon = bcd2bin (mon & 0x1F);
71 tmp->tm_year = bcd2bin (year) + bcd2bin(cent) * 100;
72 tmp->tm_wday = bcd2bin (wday & 0x07);
73 tmp->tm_yday = 0;
74 tmp->tm_isdst= 0;
75
76 debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
77 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
78 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030079
80 return 0;
wdenk4c0d4c32004-06-09 17:34:58 +000081}
82
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +020083int rtc_set (struct rtc_time *tmp)
wdenk4c0d4c32004-06-09 17:34:58 +000084{
85
86 debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
87 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
88 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
89
90 rtc_write (0x9E, 0x00);
91 rtc_write (0x80, 0); /* Clear seconds to ensure no rollover */
92 rtc_write (0x92, bin2bcd(tmp->tm_year / 100));
93 rtc_write (0x8c, bin2bcd(tmp->tm_year % 100));
94 rtc_write (0x8a, bin2bcd(tmp->tm_wday));
95 rtc_write (0x88, bin2bcd(tmp->tm_mon));
96 rtc_write (0x86, bin2bcd(tmp->tm_mday));
97 rtc_write (0x84, bin2bcd(tmp->tm_hour));
98 rtc_write (0x82, bin2bcd(tmp->tm_min ));
99 rtc_write (0x80, bin2bcd(tmp->tm_sec ));
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200100
101 return 0;
wdenk4c0d4c32004-06-09 17:34:58 +0000102}
103
104void rtc_reset (void)
105{
106}
107
Jon Loeliger068b60a2007-07-10 10:27:39 -0500108#endif