xref: /openbmc/u-boot/drivers/rtc/ds174x.c (revision 5c8fd32b)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2001
4  * ARIO Data Networks, Inc. dchiu@ariodata.com
5  *
6  * Based on MontaVista DS1743 code and U-Boot mc146818 code
7  */
8 
9 /*
10  * Date & Time support for the DS174x RTC
11  */
12 
13 /*#define	DEBUG*/
14 
15 #include <common.h>
16 #include <command.h>
17 #include <rtc.h>
18 
19 static uchar rtc_read( unsigned int addr );
20 static void  rtc_write( unsigned int addr, uchar val);
21 
22 #define RTC_BASE		( CONFIG_SYS_NVRAM_SIZE + CONFIG_SYS_NVRAM_BASE_ADDR )
23 
24 #define RTC_YEAR		( RTC_BASE + 7 )
25 #define RTC_MONTH		( RTC_BASE + 6 )
26 #define RTC_DAY_OF_MONTH	( RTC_BASE + 5 )
27 #define RTC_DAY_OF_WEEK		( RTC_BASE + 4 )
28 #define RTC_HOURS		( RTC_BASE + 3 )
29 #define RTC_MINUTES		( RTC_BASE + 2 )
30 #define RTC_SECONDS		( RTC_BASE + 1 )
31 #define RTC_CENTURY		( RTC_BASE + 0 )
32 
33 #define RTC_CONTROLA		RTC_CENTURY
34 #define RTC_CONTROLB		RTC_SECONDS
35 #define RTC_CONTROLC		RTC_DAY_OF_WEEK
36 
37 #define RTC_CA_WRITE		0x80
38 #define RTC_CA_READ		0x40
39 
40 #define RTC_CB_OSC_DISABLE	0x80
41 
42 #define RTC_CC_BATTERY_FLAG	0x80
43 #define RTC_CC_FREQ_TEST	0x40
44 
45 /* ------------------------------------------------------------------------- */
46 
47 int rtc_get( struct rtc_time *tmp )
48 {
49 	uchar sec, min, hour;
50 	uchar mday, wday, mon, year;
51 
52 	int century;
53 
54 	uchar reg_a;
55 
56 	reg_a = rtc_read( RTC_CONTROLA );
57 	/* lock clock registers for read */
58 	rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_READ ));
59 
60 	sec     = rtc_read( RTC_SECONDS );
61 	min     = rtc_read( RTC_MINUTES );
62 	hour    = rtc_read( RTC_HOURS );
63 	mday    = rtc_read( RTC_DAY_OF_MONTH );
64 	wday    = rtc_read( RTC_DAY_OF_WEEK );
65 	mon     = rtc_read( RTC_MONTH );
66 	year    = rtc_read( RTC_YEAR );
67 	century = rtc_read( RTC_CENTURY );
68 
69 	/* unlock clock registers after read */
70 	rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_READ ));
71 
72 #ifdef RTC_DEBUG
73 	printf( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
74 		"hr: %02x min: %02x sec: %02x\n",
75 		year, mon_cent, mday, wday,
76 		hour, min, sec );
77 #endif
78 	tmp->tm_sec  = bcd2bin( sec  & 0x7F );
79 	tmp->tm_min  = bcd2bin( min  & 0x7F );
80 	tmp->tm_hour = bcd2bin( hour & 0x3F );
81 	tmp->tm_mday = bcd2bin( mday & 0x3F );
82 	tmp->tm_mon  = bcd2bin( mon & 0x1F );
83 	tmp->tm_wday = bcd2bin( wday & 0x07 );
84 
85 	/* glue year from century and year in century */
86 	tmp->tm_year = bcd2bin( year ) +
87 		( bcd2bin( century & 0x3F ) * 100 );
88 
89 	tmp->tm_yday = 0;
90 	tmp->tm_isdst= 0;
91 #ifdef RTC_DEBUG
92 	printf( "Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
93 		tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
94 		tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
95 #endif
96 	return 0;
97 }
98 
99 int rtc_set( struct rtc_time *tmp )
100 {
101 	uchar reg_a;
102 #ifdef RTC_DEBUG
103 	printf( "Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
104 		tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
105 		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
106 #endif
107 	/* lock clock registers for write */
108 	reg_a = rtc_read( RTC_CONTROLA );
109 	rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_WRITE ));
110 
111 	rtc_write( RTC_MONTH, bin2bcd( tmp->tm_mon ));
112 
113 	rtc_write( RTC_DAY_OF_WEEK, bin2bcd( tmp->tm_wday ));
114 	rtc_write( RTC_DAY_OF_MONTH, bin2bcd( tmp->tm_mday ));
115 	rtc_write( RTC_HOURS, bin2bcd( tmp->tm_hour ));
116 	rtc_write( RTC_MINUTES, bin2bcd( tmp->tm_min ));
117 	rtc_write( RTC_SECONDS, bin2bcd( tmp->tm_sec ));
118 
119 	/* break year up into century and year in century */
120 	rtc_write( RTC_YEAR, bin2bcd( tmp->tm_year % 100 ));
121 	rtc_write( RTC_CENTURY, bin2bcd( tmp->tm_year / 100 ));
122 
123 	/* unlock clock registers after read */
124 	rtc_write( RTC_CONTROLA, ( reg_a  & ~RTC_CA_WRITE ));
125 
126 	return 0;
127 }
128 
129 void rtc_reset (void)
130 {
131 	uchar reg_a, reg_b, reg_c;
132 
133 	reg_a = rtc_read( RTC_CONTROLA );
134 	reg_b = rtc_read( RTC_CONTROLB );
135 
136 	if ( reg_b & RTC_CB_OSC_DISABLE )
137 	{
138 		printf( "real-time-clock was stopped. Now starting...\n" );
139 		reg_a |= RTC_CA_WRITE;
140 		reg_b &= ~RTC_CB_OSC_DISABLE;
141 
142 		rtc_write( RTC_CONTROLA, reg_a );
143 		rtc_write( RTC_CONTROLB, reg_b );
144 	}
145 
146 	/* make sure read/write clock register bits are cleared */
147 	reg_a &= ~( RTC_CA_WRITE | RTC_CA_READ );
148 	rtc_write( RTC_CONTROLA, reg_a );
149 
150 	reg_c = rtc_read( RTC_CONTROLC );
151 	if (( reg_c & RTC_CC_BATTERY_FLAG ) == 0 )
152 		printf( "RTC battery low. Clock setting may not be reliable.\n" );
153 }
154 
155 /* ------------------------------------------------------------------------- */
156 
157 static uchar rtc_read( unsigned int addr )
158 {
159 	uchar val = in8( addr );
160 #ifdef RTC_DEBUG
161 	printf( "rtc_read: %x:%x\n", addr, val );
162 #endif
163 	return( val );
164 }
165 
166 static void rtc_write( unsigned int addr, uchar val )
167 {
168 #ifdef RTC_DEBUG
169 	printf( "rtc_write: %x:%x\n", addr, val );
170 #endif
171 	out8( addr, val );
172 }
173