xref: /openbmc/u-boot/drivers/rtc/mk48t59.c (revision ae485b54)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4  * Andreas Heppel <aheppel@sysgo.de>
5  */
6 
7 /*
8  * Date & Time support for the MK48T59 RTC
9  */
10 
11 #undef	RTC_DEBUG
12 
13 #include <common.h>
14 #include <command.h>
15 #include <config.h>
16 #include <rtc.h>
17 #include <mk48t59.h>
18 
19 #if defined(CONFIG_BAB7xx)
20 
21 static uchar rtc_read (short reg)
22 {
23 	out8(RTC_PORT_ADDR0, reg & 0xFF);
24 	out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
25 	return in8(RTC_PORT_DATA);
26 }
27 
28 static void rtc_write (short reg, uchar val)
29 {
30 	out8(RTC_PORT_ADDR0, reg & 0xFF);
31 	out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
32 	out8(RTC_PORT_DATA, val);
33 }
34 
35 #elif defined(CONFIG_EVAL5200)
36 
37 static uchar rtc_read (short reg)
38 {
39 	return in8(RTC(reg));
40 }
41 
42 static void rtc_write (short reg, uchar val)
43 {
44 	out8(RTC(reg),val);
45 }
46 
47 #else
48 # error Board specific rtc access functions should be supplied
49 #endif
50 
51 /* ------------------------------------------------------------------------- */
52 
53 void *nvram_read(void *dest, const short src, size_t count)
54 {
55 	uchar *d = (uchar *) dest;
56 	short s = src;
57 
58 	while (count--)
59 		*d++ = rtc_read(s++);
60 
61 	return dest;
62 }
63 
64 void nvram_write(short dest, const void *src, size_t count)
65 {
66 	short d = dest;
67 	uchar *s = (uchar *) src;
68 
69 	while (count--)
70 		rtc_write(d++, *s++);
71 }
72 
73 #if defined(CONFIG_CMD_DATE)
74 
75 /* ------------------------------------------------------------------------- */
76 
77 int rtc_get (struct rtc_time *tmp)
78 {
79 	uchar save_ctrl_a;
80 	uchar sec, min, hour, mday, wday, mon, year;
81 
82 	/* Simple: freeze the clock, read it and allow updates again */
83 	save_ctrl_a = rtc_read(RTC_CONTROLA);
84 
85 	/* Set the register to read the value. */
86 	save_ctrl_a |= RTC_CA_READ;
87 	rtc_write(RTC_CONTROLA, save_ctrl_a);
88 
89 	sec		= rtc_read (RTC_SECONDS);
90 	min		= rtc_read (RTC_MINUTES);
91 	hour	= rtc_read (RTC_HOURS);
92 	mday	= rtc_read (RTC_DAY_OF_MONTH);
93 	wday	= rtc_read (RTC_DAY_OF_WEEK);
94 	mon		= rtc_read (RTC_MONTH);
95 	year	= rtc_read (RTC_YEAR);
96 
97 	/* re-enable update */
98 	save_ctrl_a &= ~RTC_CA_READ;
99 	rtc_write(RTC_CONTROLA, save_ctrl_a);
100 
101 #ifdef RTC_DEBUG
102 	printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
103 		"hr: %02x min: %02x sec: %02x\n",
104 		year, mon, mday, wday,
105 		hour, min, sec );
106 #endif
107 	tmp->tm_sec  = bcd2bin (sec  & 0x7F);
108 	tmp->tm_min  = bcd2bin (min  & 0x7F);
109 	tmp->tm_hour = bcd2bin (hour & 0x3F);
110 	tmp->tm_mday = bcd2bin (mday & 0x3F);
111 	tmp->tm_mon  = bcd2bin (mon & 0x1F);
112 	tmp->tm_year = bcd2bin (year);
113 	tmp->tm_wday = bcd2bin (wday & 0x07);
114 	if(tmp->tm_year<70)
115 		tmp->tm_year+=2000;
116 	else
117 		tmp->tm_year+=1900;
118 	tmp->tm_yday = 0;
119 	tmp->tm_isdst= 0;
120 #ifdef RTC_DEBUG
121 	printf ( "Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
122 		tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
123 		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
124 #endif
125 
126 	return 0;
127 }
128 
129 int rtc_set (struct rtc_time *tmp)
130 {
131 	uchar save_ctrl_a;
132 
133 #ifdef RTC_DEBUG
134 	printf ( "Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
135 		tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
136 		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
137 #endif
138 	save_ctrl_a = rtc_read(RTC_CONTROLA);
139 
140 	save_ctrl_a |= RTC_CA_WRITE;
141 	rtc_write(RTC_CONTROLA, save_ctrl_a); /* disables the RTC to update the regs */
142 
143 	rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
144 	rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
145 
146 	rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
147 	rtc_write (RTC_DAY_OF_MONTH, bin2bcd(tmp->tm_mday));
148 	rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
149 	rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
150 	rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
151 
152 	save_ctrl_a &= ~RTC_CA_WRITE;
153 	rtc_write(RTC_CONTROLA, save_ctrl_a); /* enables the RTC to update the regs */
154 
155 	return 0;
156 }
157 
158 void rtc_reset (void)
159 {
160 	uchar control_b;
161 
162 	/*
163 	 * Start oscillator here.
164 	 */
165 	control_b = rtc_read(RTC_CONTROLB);
166 
167 	control_b &= ~RTC_CB_STOP;
168 	rtc_write(RTC_CONTROLB, control_b);
169 }
170 
171 void rtc_set_watchdog(short multi, short res)
172 {
173 	uchar wd_value;
174 
175 	wd_value = RTC_WDS | ((multi & 0x1F) << 2) | (res & 0x3);
176 	rtc_write(RTC_WATCHDOG, wd_value);
177 }
178 
179 #endif
180