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