rtc.c revision 1.1 1 /* $NetBSD: rtc.c,v 1.1 1998/06/09 07:53:06 dbj 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 <stand.h>
36
37 /* ### where shall I put this definition? */
38 #define DELAY(n) { register int N = (n); while (--N > 0); }
39
40 static volatile u_int *scr2 = (u_int *)NEXT_P_SCR2_CON;
41 static u_char new_clock;
42
43 u_char
44 rtc_read(u_char reg)
45 {
46 int i;
47 u_int tmp;
48 u_char val;
49
50 *scr2 = (*scr2 & ~(SCR2_RTDATA | SCR2_RTCLK)) | SCR2_RTCE;
51 DELAY(1);
52
53 val = reg;
54 for (i=0; i<8; i++) {
55 tmp = *scr2 & ~(SCR2_RTDATA | SCR2_RTCLK);
56 if (val & 0x80)
57 tmp |= SCR2_RTDATA;
58
59 *scr2 = tmp;
60 DELAY(1);
61 *scr2 = tmp | SCR2_RTCLK;
62 DELAY(1);
63 *scr2 = tmp;
64 DELAY(1);
65
66 val <<= 1;
67 }
68
69 val = 0; /* should be anyway */
70 for (i=0; i<8; i++) {
71 val <<= 1;
72
73 tmp = *scr2 & ~(SCR2_RTDATA | SCR2_RTCLK);
74
75 *scr2 = tmp | SCR2_RTCLK;
76 DELAY(1);
77 *scr2 = tmp;
78 DELAY(1);
79
80 if (*scr2 & SCR2_RTDATA)
81 val |= 1;
82 }
83
84 *scr2 &= ~(SCR2_RTDATA|SCR2_RTCLK|SCR2_RTCE);
85 DELAY(1);
86
87 return val;
88 }
89
90 void
91 rtc_init(void)
92 {
93 u_char val;
94
95 val = rtc_read(RTC_STATUS);
96 new_clock = (val & RTC_NEW_CLOCK) ? 1 : 0;
97 }
98
99 time_t
100 getsecs(void)
101 {
102 u_int secs;
103
104 if (new_clock) {
105 secs = rtc_read(RTC_CNTR3) << 24 |
106 rtc_read(RTC_CNTR2) << 16 |
107 rtc_read(RTC_CNTR1) << 8 |
108 rtc_read(RTC_CNTR0);
109 } else {
110 u_char d,h,m,s;
111 #define BCD_DECODE(x) (((x) >> 4) * 10 + ((x) & 0xf))
112
113 d = rtc_read(RTC_DAY);
114 h = rtc_read(RTC_HRS);
115 m = rtc_read(RTC_MIN);
116 s = rtc_read(RTC_SEC);
117 secs = BCD_DECODE(d) * (60*60*24) +
118 BCD_DECODE(h) * (60*60) +
119 BCD_DECODE(m) * 60 +
120 BCD_DECODE(s);
121 }
122
123 return secs;
124 }
125