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