1 /* $NetBSD: clock.c,v 1.30 2008/05/11 22:18:08 ad Exp $ */
4 * Copyright (c) 1990 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
8 * William Jolitz and Don Ahn.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)clock.c 7.2 (Berkeley) 5/12/91
37 * Copyright (c) 1993, 1994 Charles M. Hannum.
39 * This code is derived from software contributed to Berkeley by
40 * William Jolitz and Don Ahn.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * @(#)clock.c 7.2 (Berkeley) 5/12/91
73 * Mach Operating System
74 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
75 * All Rights Reserved.
77 * Permission to use, copy, modify and distribute this software and its
78 * documentation is hereby granted, provided that both the copyright
79 * notice and this permission notice appear in all copies of the
80 * software, derivative works or modified versions, and any portions
81 * thereof, and that both notices appear in supporting documentation.
83 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
84 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
85 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
87 * Carnegie Mellon requests users of this software to return to
89 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
90 * School of Computer Science
91 * Carnegie Mellon University
92 * Pittsburgh PA 15213-3890
94 * any improvements or extensions that they make and grant Carnegie Mellon
95 * the rights to redistribute these changes.
98 Copyright 1988, 1989 by Intel Corporation, Santa Clara, California.
102 Permission to use, copy, modify, and distribute this software and
103 its documentation for any purpose and without fee is hereby
104 granted, provided that the above copyright notice appears in all
105 copies and that both the copyright notice and this permission notice
106 appear in supporting documentation, and that the name of Intel
107 not be used in advertising or publicity pertaining to distribution
108 of the software without specific, written prior permission.
110 INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
111 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
112 IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
113 CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
114 LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
115 NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
116 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
120 * Primitive RTC chip routines.
123 #include <sys/cdefs.h>
124 __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.30 2008/05/11 22:18:08 ad Exp $");
126 #include <sys/param.h>
127 #include <sys/systm.h>
128 #include <sys/time.h>
129 #include <sys/timetc.h>
130 #include <sys/kernel.h>
132 #include <dev/isa/isareg.h>
133 #include <dev/isa/isavar.h>
134 #include <i386/isa/nvram.h>
136 #include <machine/pio.h>
138 #include <dev/ic/mc146818reg.h>
145 #include <machine/mca_machdep.h> /* for MCA_system */
148 static void rtcinit(void);
149 static int rtcget(mc_todregs
*);
150 static void rtcput(mc_todregs
*);
151 static int cmoscheck(void);
152 static int clock_expandyear(int);
156 mc146818_read(void *sc
, u_int reg
)
160 return (inb(IO_RTC
+1));
164 mc146818_write(void *sc
, u_int reg
, u_int datum
)
168 outb(IO_RTC
+1, datum
);
174 static int first_rtcopen_ever
= 1;
176 if (!first_rtcopen_ever
)
178 first_rtcopen_ever
= 0;
180 mc146818_write(NULL
, MC_REGA
, /* XXX softc */
181 MC_BASE_32_KHz
| MC_RATE_1024_Hz
);
182 mc146818_write(NULL
, MC_REGB
, MC_REGB_24HR
); /* XXX softc */
186 rtcget(mc_todregs
*regs
)
190 if ((mc146818_read(NULL
, MC_REGD
) & MC_REGD_VRT
) == 0) /* XXX softc */
192 MC146818_GETTOD(NULL
, regs
); /* XXX softc */
197 rtcput(mc_todregs
*regs
)
201 MC146818_PUTTOD(NULL
, regs
); /* XXX softc */
205 * check whether the CMOS layout is "standard"-like (ie, not PS/2-like),
206 * to be called at splclock()
212 unsigned short cksum
= 0;
214 for (i
= 0x10; i
<= 0x2d; i
++)
215 cksum
+= mc146818_read(NULL
, i
); /* XXX softc */
217 return (cksum
== (mc146818_read(NULL
, 0x2e) << 8)
218 + mc146818_read(NULL
, 0x2f));
223 * Check whether the CMOS layout is PS/2 like, to be called at splclock().
225 static int cmoscheckps2(void);
230 /* Disabled until I find out the CRC checksum algorithm IBM uses */
232 unsigned short cksum
= 0;
234 for (i
= 0x10; i
<= 0x31; i
++)
235 cksum
+= mc146818_read(NULL
, i
); /* XXX softc */
237 return (cksum
== (mc146818_read(NULL
, 0x32) << 8)
238 + mc146818_read(NULL
, 0x33));
240 /* Check 'incorrect checksum' bit of IBM PS/2 Diagnostic Status Byte */
241 return ((mc146818_read(NULL
, NVRAM_DIAG
) & (1<<6)) == 0);
244 #endif /* NMCA > 0 */
247 * patchable to control century byte handling:
250 * 0: try to figure out itself
252 int rtc_update_century
= 0;
255 * Expand a two-digit year as read from the clock chip
257 * Being here, deal with the CMOS century byte.
259 static int centb
= NVRAM_CENTURY
;
261 clock_expandyear(int clockyear
)
263 int s
, clockcentury
, cmoscentury
;
265 clockcentury
= (clockyear
< 70) ? 20 : 19;
266 clockyear
+= 100 * clockcentury
;
268 if (rtc_update_century
< 0)
273 cmoscentury
= mc146818_read(NULL
, NVRAM_CENTURY
);
275 else if (MCA_system
&& cmoscheckps2())
276 cmoscentury
= mc146818_read(NULL
, (centb
= 0x37));
283 printf("clock: unknown CMOS layout\n");
287 cmoscentury
= bcdtobin(cmoscentury
);
289 if (cmoscentury
!= clockcentury
) {
290 /* XXX note: saying "century is 20" might confuse the naive. */
291 printf("WARNING: NVRAM century is %d but RTC year is %d\n",
292 cmoscentury
, clockyear
);
294 /* Kludge to roll over century. */
295 if ((rtc_update_century
> 0) ||
296 ((cmoscentury
== 19) && (clockcentury
== 20) &&
297 (clockyear
== 2000))) {
298 printf("WARNING: Setting NVRAM century to %d\n",
301 mc146818_write(NULL
, centb
, bintobcd(clockcentury
));
304 } else if (cmoscentury
== 19 && rtc_update_century
== 0)
305 rtc_update_century
= 1; /* will update later in resettodr() */
311 rtc_get_ymdhms(todr_chip_handle_t tch
, struct clock_ymdhms
*dt
)
317 if (rtcget(&rtclk
)) {
323 dt
->dt_sec
= bcdtobin(rtclk
[MC_SEC
]);
324 dt
->dt_min
= bcdtobin(rtclk
[MC_MIN
]);
325 dt
->dt_hour
= bcdtobin(rtclk
[MC_HOUR
]);
326 dt
->dt_day
= bcdtobin(rtclk
[MC_DOM
]);
327 dt
->dt_mon
= bcdtobin(rtclk
[MC_MONTH
]);
328 dt
->dt_year
= clock_expandyear(bcdtobin(rtclk
[MC_YEAR
]));
334 rtc_set_ymdhms(todr_chip_handle_t tch
, struct clock_ymdhms
*dt
)
342 memset(&rtclk
, 0, sizeof(rtclk
));
345 rtclk
[MC_SEC
] = bintobcd(dt
->dt_sec
);
346 rtclk
[MC_MIN
] = bintobcd(dt
->dt_min
);
347 rtclk
[MC_HOUR
] = bintobcd(dt
->dt_hour
);
348 rtclk
[MC_DOW
] = dt
->dt_wday
+ 1;
349 rtclk
[MC_YEAR
] = bintobcd(dt
->dt_year
% 100);
350 rtclk
[MC_MONTH
] = bintobcd(dt
->dt_mon
);
351 rtclk
[MC_DOM
] = bintobcd(dt
->dt_day
);
354 printf("setclock: %x/%x/%x %x:%x:%x\n", rtclk
[MC_YEAR
], rtclk
[MC_MONTH
],
355 rtclk
[MC_DOM
], rtclk
[MC_HOUR
], rtclk
[MC_MIN
], rtclk
[MC_SEC
]);
359 if (rtc_update_century
> 0) {
360 century
= bintobcd(dt
->dt_year
/ 100);
361 mc146818_write(NULL
, centb
, century
); /* XXX softc */
371 static struct todr_chip_handle tch
;
372 tch
.todr_gettime_ymdhms
= rtc_get_ymdhms
;
373 tch
.todr_settime_ymdhms
= rtc_set_ymdhms
;
374 tch
.todr_setwen
= NULL
;