xref: /netbsd/sys/arch/next68k/stand/boot/rtc.c (revision 6550d01e)
1 /*      $NetBSD: rtc.c,v 1.6 2009/01/12 11:32:44 tsutsui Exp $        */
2 /*
3  * Copyright (c) 1997 Rolf Grossmann
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Rolf Grossmann.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <next68k/dev/clockreg.h>
34 #include <machine/cpu.h>
35 #include <lib/libsa/stand.h>
36 #include <lib/libsa/net.h>
37 
38 u_char rtc_read(u_char);
39 void rtc_init(void);
40 
41 
42 /* ### where shall I put this definition? */
43 #define	DELAY(n)	{ register int N = (n); while (--N > 0); }
44 
45 static volatile u_int *scr2 = (u_int *)NEXT_P_SCR2_CON;
46 static u_char new_clock;
47 
48 u_char
49 rtc_read(u_char reg)
50 {
51 	int i;
52 	u_int tmp;
53 	u_char val;
54 
55 	*scr2 = (*scr2 & ~(SCR2_RTDATA | SCR2_RTCLK)) | SCR2_RTCE;
56 	DELAY(1);
57 
58 	val = reg;
59 	for (i=0; i<8; i++) {
60 		tmp = *scr2 & ~(SCR2_RTDATA | SCR2_RTCLK);
61 		if (val & 0x80)
62 			tmp |= SCR2_RTDATA;
63 
64 		*scr2 = tmp;
65 		DELAY(1);
66 		*scr2 = tmp | SCR2_RTCLK;
67 		DELAY(1);
68 		*scr2 = tmp;
69 		DELAY(1);
70 
71 		val <<= 1;
72 	}
73 
74 	val = 0;			/* should be anyway */
75 	for (i=0; i<8; i++) {
76 		val <<= 1;
77 
78 		tmp = *scr2 & ~(SCR2_RTDATA | SCR2_RTCLK);
79 
80 		*scr2 = tmp | SCR2_RTCLK;
81 		DELAY(1);
82 		*scr2 = tmp;
83 		DELAY(1);
84 
85 		if (*scr2 & SCR2_RTDATA)
86 			val |= 1;
87 	}
88 
89 	*scr2 &= ~(SCR2_RTDATA|SCR2_RTCLK|SCR2_RTCE);
90 	DELAY(1);
91 
92 	return val;
93 }
94 
95 void
96 rtc_init(void)
97 {
98 	u_char val;
99 
100 	val = rtc_read(RTC_STATUS);
101 	new_clock = (val & RTC_NEW_CLOCK) ? 1 : 0;
102 }
103 
104 satime_t
105 getsecs(void)
106 {
107 	u_int secs;
108 
109 	if (new_clock) {
110 		secs = rtc_read(RTC_CNTR0) << 24 |
111 		       rtc_read(RTC_CNTR1) << 16 |
112 		       rtc_read(RTC_CNTR2) << 8  |
113 		       rtc_read(RTC_CNTR3);
114 	} else {
115 		u_char d,h,m,s;
116 #define BCD_DECODE(x) (((x) >> 4) * 10 + ((x) & 0xf))
117 
118 		d = rtc_read(RTC_DAY);
119 		h = rtc_read(RTC_HRS);
120 		m = rtc_read(RTC_MIN);
121 		s = rtc_read(RTC_SEC);
122 		secs = BCD_DECODE(d) * (60*60*24) +
123 		       BCD_DECODE(h) * (60*60) +
124 		       BCD_DECODE(m) * 60 +
125 		       BCD_DECODE(s);
126 	}
127 
128 	return secs;
129 }
130