xref: /rk3399_rockchip-uboot/drivers/rtc/isl1208.c (revision b73a19e1609d0f705cbab8014ca17aefe89e4c76)
19536dfccSTor Krill /*
29536dfccSTor Krill  * (C) Copyright 2008
39536dfccSTor Krill  * Tor Krill, Excito Elektronik i Skåne , tor@excito.com
49536dfccSTor Krill  *
59536dfccSTor Krill  * Modelled after the ds1337 driver
69536dfccSTor Krill  *
79536dfccSTor Krill  * This program is free software; you can redistribute it and/or
89536dfccSTor Krill  * modify it under the terms of the GNU General Public License as
99536dfccSTor Krill  * published by the Free Software Foundation; either version 2 of
109536dfccSTor Krill  * the License, or (at your option) any later version.
119536dfccSTor Krill  *
129536dfccSTor Krill  * This program is distributed in the hope that it will be useful,
139536dfccSTor Krill  * but WITHOUT ANY WARRANTY; without even the implied warranty of
149536dfccSTor Krill  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
159536dfccSTor Krill  * GNU General Public License for more details.
169536dfccSTor Krill  *
179536dfccSTor Krill  * You should have received a copy of the GNU General Public License
189536dfccSTor Krill  * along with this program; if not, write to the Free Software
199536dfccSTor Krill  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
209536dfccSTor Krill  * MA 02111-1307 USA
219536dfccSTor Krill  */
229536dfccSTor Krill 
239536dfccSTor Krill /*
249536dfccSTor Krill  * Date & Time support (no alarms) for Intersil
259536dfccSTor Krill  * ISL1208 Real Time Clock (RTC).
269536dfccSTor Krill  */
279536dfccSTor Krill 
289536dfccSTor Krill #include <common.h>
299536dfccSTor Krill #include <command.h>
309536dfccSTor Krill #include <rtc.h>
319536dfccSTor Krill #include <i2c.h>
329536dfccSTor Krill 
339536dfccSTor Krill /*---------------------------------------------------------------------*/
349536dfccSTor Krill #ifdef DEBUG_RTC
359536dfccSTor Krill #define DEBUGR(fmt,args...) printf(fmt ,##args)
369536dfccSTor Krill #else
379536dfccSTor Krill #define DEBUGR(fmt,args...)
389536dfccSTor Krill #endif
399536dfccSTor Krill /*---------------------------------------------------------------------*/
409536dfccSTor Krill 
419536dfccSTor Krill /*
429536dfccSTor Krill  * RTC register addresses
439536dfccSTor Krill  */
449536dfccSTor Krill 
459536dfccSTor Krill #define RTC_SEC_REG_ADDR	0x0
469536dfccSTor Krill #define RTC_MIN_REG_ADDR	0x1
479536dfccSTor Krill #define RTC_HR_REG_ADDR		0x2
489536dfccSTor Krill #define RTC_DATE_REG_ADDR	0x3
499536dfccSTor Krill #define RTC_MON_REG_ADDR	0x4
509536dfccSTor Krill #define RTC_YR_REG_ADDR		0x5
519536dfccSTor Krill #define RTC_DAY_REG_ADDR	0x6
529536dfccSTor Krill #define RTC_STAT_REG_ADDR	0x7
539536dfccSTor Krill /*
549536dfccSTor Krill  * RTC control register bits
559536dfccSTor Krill  */
569536dfccSTor Krill 
579536dfccSTor Krill /*
589536dfccSTor Krill  * RTC status register bits
599536dfccSTor Krill  */
609536dfccSTor Krill #define RTC_STAT_BIT_ARST	0x80	/* AUTO RESET ENABLE BIT */
619536dfccSTor Krill #define RTC_STAT_BIT_XTOSCB	0x40	/* CRYSTAL OSCILLATOR ENABLE BIT */
629536dfccSTor Krill #define RTC_STAT_BIT_WRTC	0x10	/* WRITE RTC ENABLE BIT */
639536dfccSTor Krill #define RTC_STAT_BIT_ALM	0x04	/* ALARM BIT */
649536dfccSTor Krill #define RTC_STAT_BIT_BAT	0x02	/* BATTERY BIT */
659536dfccSTor Krill #define RTC_STAT_BIT_RTCF	0x01	/* REAL TIME CLOCK FAIL BIT */
669536dfccSTor Krill 
679536dfccSTor Krill static uchar rtc_read (uchar reg);
689536dfccSTor Krill static void rtc_write (uchar reg, uchar val);
699536dfccSTor Krill static uchar bin2bcd (unsigned int n);
709536dfccSTor Krill static unsigned bcd2bin (uchar c);
719536dfccSTor Krill 
729536dfccSTor Krill /*
739536dfccSTor Krill  * Get the current time from the RTC
749536dfccSTor Krill  */
759536dfccSTor Krill 
76*b73a19e1SYuri Tikhonov int rtc_get (struct rtc_time *tmp)
779536dfccSTor Krill {
78*b73a19e1SYuri Tikhonov 	int rel = 0;
799536dfccSTor Krill 	uchar sec, min, hour, mday, wday, mon, year, status;
809536dfccSTor Krill 
819536dfccSTor Krill 	status = rtc_read (RTC_STAT_REG_ADDR);
829536dfccSTor Krill 	sec = rtc_read (RTC_SEC_REG_ADDR);
839536dfccSTor Krill 	min = rtc_read (RTC_MIN_REG_ADDR);
849536dfccSTor Krill 	hour = rtc_read (RTC_HR_REG_ADDR);
859536dfccSTor Krill 	wday = rtc_read (RTC_DAY_REG_ADDR);
869536dfccSTor Krill 	mday = rtc_read (RTC_DATE_REG_ADDR);
879536dfccSTor Krill 	mon = rtc_read (RTC_MON_REG_ADDR);
889536dfccSTor Krill 	year = rtc_read (RTC_YR_REG_ADDR);
899536dfccSTor Krill 
909536dfccSTor Krill 	DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
919536dfccSTor Krill 		"hr: %02x min: %02x sec: %02x status: %02x\n",
929536dfccSTor Krill 		year, mon, mday, wday, hour, min, sec, status);
939536dfccSTor Krill 
949536dfccSTor Krill 	if (status & RTC_STAT_BIT_RTCF) {
959536dfccSTor Krill 		printf ("### Warning: RTC oscillator has stopped\n");
969536dfccSTor Krill 		rtc_write(RTC_STAT_REG_ADDR,
979536dfccSTor Krill 			rtc_read(RTC_STAT_REG_ADDR) &~ (RTC_STAT_BIT_BAT|RTC_STAT_BIT_RTCF));
98*b73a19e1SYuri Tikhonov 		rel = -1;
999536dfccSTor Krill 	}
1009536dfccSTor Krill 
1019536dfccSTor Krill 	tmp->tm_sec  = bcd2bin (sec & 0x7F);
1029536dfccSTor Krill 	tmp->tm_min  = bcd2bin (min & 0x7F);
1039536dfccSTor Krill 	tmp->tm_hour = bcd2bin (hour & 0x3F);
1049536dfccSTor Krill 	tmp->tm_mday = bcd2bin (mday & 0x3F);
1059536dfccSTor Krill 	tmp->tm_mon  = bcd2bin (mon & 0x1F);
1069536dfccSTor Krill 	tmp->tm_year = bcd2bin (year)+2000;
1079536dfccSTor Krill 	tmp->tm_wday = bcd2bin (wday & 0x07);
1089536dfccSTor Krill 	tmp->tm_yday = 0;
1099536dfccSTor Krill 	tmp->tm_isdst= 0;
1109536dfccSTor Krill 
1119536dfccSTor Krill 	DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
1129536dfccSTor Krill 		tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
1139536dfccSTor Krill 		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
114*b73a19e1SYuri Tikhonov 
115*b73a19e1SYuri Tikhonov 	return rel;
1169536dfccSTor Krill }
1179536dfccSTor Krill 
1189536dfccSTor Krill /*
1199536dfccSTor Krill  * Set the RTC
1209536dfccSTor Krill  */
1219536dfccSTor Krill void rtc_set (struct rtc_time *tmp)
1229536dfccSTor Krill {
1239536dfccSTor Krill 	DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
1249536dfccSTor Krill 		tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
1259536dfccSTor Krill 		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
1269536dfccSTor Krill 
1279536dfccSTor Krill 	/* enable write */
1289536dfccSTor Krill 	rtc_write(RTC_STAT_REG_ADDR,
1299536dfccSTor Krill 		rtc_read(RTC_STAT_REG_ADDR) | RTC_STAT_BIT_WRTC);
1309536dfccSTor Krill 
1319536dfccSTor Krill 	rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
1329536dfccSTor Krill 	rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon));
1339536dfccSTor Krill 	rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday));
1349536dfccSTor Krill 	rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
1359536dfccSTor Krill 	rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour) | 0x80 ); /* 24h clock */
1369536dfccSTor Krill 	rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
1379536dfccSTor Krill 	rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
1389536dfccSTor Krill 
1399536dfccSTor Krill 	/* disable write */
1409536dfccSTor Krill 	rtc_write(RTC_STAT_REG_ADDR,
1419536dfccSTor Krill 		rtc_read(RTC_STAT_REG_ADDR) & ~RTC_STAT_BIT_WRTC);
1429536dfccSTor Krill }
1439536dfccSTor Krill 
1449536dfccSTor Krill void rtc_reset (void)
1459536dfccSTor Krill {
1469536dfccSTor Krill }
1479536dfccSTor Krill 
1489536dfccSTor Krill /*
1499536dfccSTor Krill  * Helper functions
1509536dfccSTor Krill  */
1519536dfccSTor Krill 
1529536dfccSTor Krill static uchar rtc_read (uchar reg)
1539536dfccSTor Krill {
1549536dfccSTor Krill 	return (i2c_reg_read (CFG_I2C_RTC_ADDR, reg));
1559536dfccSTor Krill }
1569536dfccSTor Krill 
1579536dfccSTor Krill static void rtc_write (uchar reg, uchar val)
1589536dfccSTor Krill {
1599536dfccSTor Krill 	i2c_reg_write (CFG_I2C_RTC_ADDR, reg, val);
1609536dfccSTor Krill }
1619536dfccSTor Krill 
1629536dfccSTor Krill static unsigned bcd2bin (uchar n)
1639536dfccSTor Krill {
1649536dfccSTor Krill 	return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
1659536dfccSTor Krill }
1669536dfccSTor Krill 
1679536dfccSTor Krill static unsigned char bin2bcd (unsigned int n)
1689536dfccSTor Krill {
1699536dfccSTor Krill 	return (((n / 10) << 4) | (n % 10));
1709536dfccSTor Krill }
171