Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / next68k / stand / boot / rtc.c
blob9a1de088b17f05560d46c558471dc811f5f4c108
1 /* $NetBSD: rtc.c,v 1.5 2006/03/08 03:29:49 christos Exp $ */
2 /*
3 * Copyright (c) 1997 Rolf Grossmann
4 * All rights reserved.
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
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.
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>
38 u_char rtc_read(u_char);
39 void rtc_init(void);
42 /* ### where shall I put this definition? */
43 #define DELAY(n) { register int N = (n); while (--N > 0); }
45 static volatile u_int *scr2 = (u_int *)NEXT_P_SCR2_CON;
46 static u_char new_clock;
48 u_char
49 rtc_read(u_char reg)
51 int i;
52 u_int tmp;
53 u_char val;
55 *scr2 = (*scr2 & ~(SCR2_RTDATA | SCR2_RTCLK)) | SCR2_RTCE;
56 DELAY(1);
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;
64 *scr2 = tmp;
65 DELAY(1);
66 *scr2 = tmp | SCR2_RTCLK;
67 DELAY(1);
68 *scr2 = tmp;
69 DELAY(1);
71 val <<= 1;
74 val = 0; /* should be anyway */
75 for (i=0; i<8; i++) {
76 val <<= 1;
78 tmp = *scr2 & ~(SCR2_RTDATA | SCR2_RTCLK);
80 *scr2 = tmp | SCR2_RTCLK;
81 DELAY(1);
82 *scr2 = tmp;
83 DELAY(1);
85 if (*scr2 & SCR2_RTDATA)
86 val |= 1;
89 *scr2 &= ~(SCR2_RTDATA|SCR2_RTCLK|SCR2_RTCE);
90 DELAY(1);
92 return val;
95 void
96 rtc_init(void)
98 u_char val;
100 val = rtc_read(RTC_STATUS);
101 new_clock = (val & RTC_NEW_CLOCK) ? 1 : 0;
104 satime_t
105 getsecs(void)
107 u_int secs;
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))
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);
128 return secs;