xref: /openbmc/u-boot/drivers/rtc/mk48t59.c (revision 63495ad7)
1 /*
2  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
3  * Andreas Heppel <aheppel@sysgo.de>
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 
24 /*
25  * Date & Time support for the MK48T59 RTC
26  */
27 
28 #undef	RTC_DEBUG
29 
30 #include <common.h>
31 #include <command.h>
32 #include <config.h>
33 #include <rtc.h>
34 #include <mk48t59.h>
35 
36 #if defined(CONFIG_BAB7xx)
37 
38 static uchar rtc_read (short reg)
39 {
40 	out8(RTC_PORT_ADDR0, reg & 0xFF);
41 	out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
42 	return in8(RTC_PORT_DATA);
43 }
44 
45 static void rtc_write (short reg, uchar val)
46 {
47 	out8(RTC_PORT_ADDR0, reg & 0xFF);
48 	out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
49 	out8(RTC_PORT_DATA, val);
50 }
51 
52 #elif defined(CONFIG_EVAL5200)
53 
54 static uchar rtc_read (short reg)
55 {
56 	return in8(RTC(reg));
57 }
58 
59 static void rtc_write (short reg, uchar val)
60 {
61 	out8(RTC(reg),val);
62 }
63 
64 #else
65 # error Board specific rtc access functions should be supplied
66 #endif
67 
68 /* ------------------------------------------------------------------------- */
69 
70 void *nvram_read(void *dest, const short src, size_t count)
71 {
72 	uchar *d = (uchar *) dest;
73 	short s = src;
74 
75 	while (count--)
76 		*d++ = rtc_read(s++);
77 
78 	return dest;
79 }
80 
81 void nvram_write(short dest, const void *src, size_t count)
82 {
83 	short d = dest;
84 	uchar *s = (uchar *) src;
85 
86 	while (count--)
87 		rtc_write(d++, *s++);
88 }
89 
90 #if defined(CONFIG_CMD_DATE)
91 
92 /* ------------------------------------------------------------------------- */
93 
94 int rtc_get (struct rtc_time *tmp)
95 {
96 	uchar save_ctrl_a;
97 	uchar sec, min, hour, mday, wday, mon, year;
98 
99 	/* Simple: freeze the clock, read it and allow updates again */
100 	save_ctrl_a = rtc_read(RTC_CONTROLA);
101 
102 	/* Set the register to read the value. */
103 	save_ctrl_a |= RTC_CA_READ;
104 	rtc_write(RTC_CONTROLA, save_ctrl_a);
105 
106 	sec		= rtc_read (RTC_SECONDS);
107 	min		= rtc_read (RTC_MINUTES);
108 	hour	= rtc_read (RTC_HOURS);
109 	mday	= rtc_read (RTC_DAY_OF_MONTH);
110 	wday	= rtc_read (RTC_DAY_OF_WEEK);
111 	mon		= rtc_read (RTC_MONTH);
112 	year	= rtc_read (RTC_YEAR);
113 
114 	/* re-enable update */
115 	save_ctrl_a &= ~RTC_CA_READ;
116 	rtc_write(RTC_CONTROLA, save_ctrl_a);
117 
118 #ifdef RTC_DEBUG
119 	printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
120 		"hr: %02x min: %02x sec: %02x\n",
121 		year, mon, mday, wday,
122 		hour, min, sec );
123 #endif
124 	tmp->tm_sec  = bcd2bin (sec  & 0x7F);
125 	tmp->tm_min  = bcd2bin (min  & 0x7F);
126 	tmp->tm_hour = bcd2bin (hour & 0x3F);
127 	tmp->tm_mday = bcd2bin (mday & 0x3F);
128 	tmp->tm_mon  = bcd2bin (mon & 0x1F);
129 	tmp->tm_year = bcd2bin (year);
130 	tmp->tm_wday = bcd2bin (wday & 0x07);
131 	if(tmp->tm_year<70)
132 		tmp->tm_year+=2000;
133 	else
134 		tmp->tm_year+=1900;
135 	tmp->tm_yday = 0;
136 	tmp->tm_isdst= 0;
137 #ifdef RTC_DEBUG
138 	printf ( "Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
139 		tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
140 		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
141 #endif
142 
143 	return 0;
144 }
145 
146 int rtc_set (struct rtc_time *tmp)
147 {
148 	uchar save_ctrl_a;
149 
150 #ifdef RTC_DEBUG
151 	printf ( "Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
152 		tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
153 		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
154 #endif
155 	save_ctrl_a = rtc_read(RTC_CONTROLA);
156 
157 	save_ctrl_a |= RTC_CA_WRITE;
158 	rtc_write(RTC_CONTROLA, save_ctrl_a); /* disables the RTC to update the regs */
159 
160 	rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
161 	rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
162 
163 	rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
164 	rtc_write (RTC_DAY_OF_MONTH, bin2bcd(tmp->tm_mday));
165 	rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
166 	rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
167 	rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
168 
169 	save_ctrl_a &= ~RTC_CA_WRITE;
170 	rtc_write(RTC_CONTROLA, save_ctrl_a); /* enables the RTC to update the regs */
171 
172 	return 0;
173 }
174 
175 void rtc_reset (void)
176 {
177 	uchar control_b;
178 
179 	/*
180 	 * Start oscillator here.
181 	 */
182 	control_b = rtc_read(RTC_CONTROLB);
183 
184 	control_b &= ~RTC_CB_STOP;
185 	rtc_write(RTC_CONTROLB, control_b);
186 }
187 
188 void rtc_set_watchdog(short multi, short res)
189 {
190 	uchar wd_value;
191 
192 	wd_value = RTC_WDS | ((multi & 0x1F) << 2) | (res & 0x3);
193 	rtc_write(RTC_WATCHDOG, wd_value);
194 }
195 
196 #endif
197