xref: /openbmc/u-boot/drivers/rtc/mc146818.c (revision e2a53458)
1 /*
2  * (C) Copyright 2001
3  * Denis Peter MPL AG Switzerland. d.peter@mpl.ch
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 MC146818 (PIXX4) RTC
26  */
27 
28 /*#define	DEBUG*/
29 
30 #include <common.h>
31 #include <command.h>
32 #include <rtc.h>
33 
34 #ifdef __I386__
35 #include <asm/io.h>
36 #define in8(p) inb(p)
37 #define out8(p, v) outb(v, p)
38 #endif
39 
40 #if defined(CONFIG_CMD_DATE)
41 
42 static uchar rtc_read  (uchar reg);
43 static void  rtc_write (uchar reg, uchar val);
44 
45 #define RTC_PORT_MC146818	CONFIG_SYS_ISA_IO_BASE_ADDRESS +  0x70
46 #define RTC_SECONDS		0x00
47 #define RTC_SECONDS_ALARM	0x01
48 #define RTC_MINUTES		0x02
49 #define RTC_MINUTES_ALARM	0x03
50 #define RTC_HOURS		0x04
51 #define RTC_HOURS_ALARM		0x05
52 #define RTC_DAY_OF_WEEK		0x06
53 #define RTC_DATE_OF_MONTH	0x07
54 #define RTC_MONTH		0x08
55 #define RTC_YEAR		0x09
56 #define RTC_CONFIG_A		0x0A
57 #define RTC_CONFIG_B		0x0B
58 #define RTC_CONFIG_C		0x0C
59 #define RTC_CONFIG_D		0x0D
60 
61 
62 /* ------------------------------------------------------------------------- */
63 
64 int rtc_get (struct rtc_time *tmp)
65 {
66 	uchar sec, min, hour, mday, wday, mon, year;
67   /* here check if rtc can be accessed */
68 	while((rtc_read(RTC_CONFIG_A)&0x80)==0x80);
69 	sec	= rtc_read (RTC_SECONDS);
70 	min	= rtc_read (RTC_MINUTES);
71 	hour	= rtc_read (RTC_HOURS);
72 	mday	= rtc_read (RTC_DATE_OF_MONTH);
73 	wday	= rtc_read (RTC_DAY_OF_WEEK);
74 	mon	= rtc_read (RTC_MONTH);
75 	year	= rtc_read (RTC_YEAR);
76 #ifdef RTC_DEBUG
77 	printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
78 		"hr: %02x min: %02x sec: %02x\n",
79 		year, mon, mday, wday,
80 		hour, min, sec );
81 	printf ( "Alarms: month: %02x hour: %02x min: %02x sec: %02x\n",
82 		rtc_read (RTC_CONFIG_D) & 0x3F,
83 		rtc_read (RTC_HOURS_ALARM),
84 		rtc_read (RTC_MINUTES_ALARM),
85 		rtc_read (RTC_SECONDS_ALARM) );
86 #endif
87 	tmp->tm_sec  = bcd2bin (sec  & 0x7F);
88 	tmp->tm_min  = bcd2bin (min  & 0x7F);
89 	tmp->tm_hour = bcd2bin (hour & 0x3F);
90 	tmp->tm_mday = bcd2bin (mday & 0x3F);
91 	tmp->tm_mon  = bcd2bin (mon & 0x1F);
92 	tmp->tm_year = bcd2bin (year);
93 	tmp->tm_wday = bcd2bin (wday & 0x07);
94 	if(tmp->tm_year<70)
95 		tmp->tm_year+=2000;
96 	else
97 		tmp->tm_year+=1900;
98 	tmp->tm_yday = 0;
99 	tmp->tm_isdst= 0;
100 #ifdef RTC_DEBUG
101 	printf ( "Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
102 		tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
103 		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
104 #endif
105 
106 	return 0;
107 }
108 
109 int rtc_set (struct rtc_time *tmp)
110 {
111 #ifdef RTC_DEBUG
112 	printf ( "Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
113 		tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
114 		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
115 #endif
116 	rtc_write(RTC_CONFIG_B,0x82); /* disables the RTC to update the regs */
117 
118 	rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
119 	rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
120 	rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
121 	rtc_write (RTC_DATE_OF_MONTH, bin2bcd(tmp->tm_mday));
122 	rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
123 	rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
124 	rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
125 	rtc_write(RTC_CONFIG_B,0x02); /* enables the RTC to update the regs */
126 
127 	return 0;
128 }
129 
130 void rtc_reset (void)
131 {
132 	rtc_write(RTC_CONFIG_B,0x82); /* disables the RTC to update the regs */
133 	rtc_write(RTC_CONFIG_A,0x20); /* Normal OP */
134 	rtc_write(RTC_CONFIG_B,0x00);
135 	rtc_write(RTC_CONFIG_B,0x00);
136 	rtc_write(RTC_CONFIG_B,0x02); /* enables the RTC to update the regs */
137 }
138 
139 /* ------------------------------------------------------------------------- */
140 
141 #ifdef CONFIG_SYS_RTC_REG_BASE_ADDR
142 /*
143  * use direct memory access
144  */
145 static uchar rtc_read (uchar reg)
146 {
147 	return(in8(CONFIG_SYS_RTC_REG_BASE_ADDR+reg));
148 }
149 
150 static void rtc_write (uchar reg, uchar val)
151 {
152 	out8(CONFIG_SYS_RTC_REG_BASE_ADDR+reg, val);
153 }
154 #else
155 static uchar rtc_read (uchar reg)
156 {
157 	out8(RTC_PORT_MC146818,reg);
158 	return(in8(RTC_PORT_MC146818+1));
159 }
160 
161 static void rtc_write (uchar reg, uchar val)
162 {
163 	out8(RTC_PORT_MC146818,reg);
164 	out8(RTC_PORT_MC146818+1,val);
165 }
166 #endif
167 
168 #endif
169