Home | History | Annotate | Line # | Download | only in isa
clock.c revision 1.4.6.2
      1  1.4.6.2  yamt /*	$NetBSD: clock.c,v 1.4.6.2 2006/09/14 12:31:22 yamt Exp $	*/
      2  1.4.6.2  yamt 
      3  1.4.6.2  yamt /*-
      4  1.4.6.2  yamt  * Copyright (c) 1990 The Regents of the University of California.
      5  1.4.6.2  yamt  * All rights reserved.
      6  1.4.6.2  yamt  *
      7  1.4.6.2  yamt  * This code is derived from software contributed to Berkeley by
      8  1.4.6.2  yamt  * William Jolitz and Don Ahn.
      9  1.4.6.2  yamt  *
     10  1.4.6.2  yamt  * Redistribution and use in source and binary forms, with or without
     11  1.4.6.2  yamt  * modification, are permitted provided that the following conditions
     12  1.4.6.2  yamt  * are met:
     13  1.4.6.2  yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.4.6.2  yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.4.6.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.4.6.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.4.6.2  yamt  *    documentation and/or other materials provided with the distribution.
     18  1.4.6.2  yamt  * 3. Neither the name of the University nor the names of its contributors
     19  1.4.6.2  yamt  *    may be used to endorse or promote products derived from this software
     20  1.4.6.2  yamt  *    without specific prior written permission.
     21  1.4.6.2  yamt  *
     22  1.4.6.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.4.6.2  yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.4.6.2  yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.4.6.2  yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.4.6.2  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.4.6.2  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.4.6.2  yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.4.6.2  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.4.6.2  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.4.6.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.4.6.2  yamt  * SUCH DAMAGE.
     33  1.4.6.2  yamt  *
     34  1.4.6.2  yamt  *	@(#)clock.c	7.2 (Berkeley) 5/12/91
     35  1.4.6.2  yamt  */
     36  1.4.6.2  yamt /*-
     37  1.4.6.2  yamt  * Copyright (c) 1993, 1994 Charles M. Hannum.
     38  1.4.6.2  yamt  *
     39  1.4.6.2  yamt  * This code is derived from software contributed to Berkeley by
     40  1.4.6.2  yamt  * William Jolitz and Don Ahn.
     41  1.4.6.2  yamt  *
     42  1.4.6.2  yamt  * Redistribution and use in source and binary forms, with or without
     43  1.4.6.2  yamt  * modification, are permitted provided that the following conditions
     44  1.4.6.2  yamt  * are met:
     45  1.4.6.2  yamt  * 1. Redistributions of source code must retain the above copyright
     46  1.4.6.2  yamt  *    notice, this list of conditions and the following disclaimer.
     47  1.4.6.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     48  1.4.6.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     49  1.4.6.2  yamt  *    documentation and/or other materials provided with the distribution.
     50  1.4.6.2  yamt  * 3. All advertising materials mentioning features or use of this software
     51  1.4.6.2  yamt  *    must display the following acknowledgement:
     52  1.4.6.2  yamt  *	This product includes software developed by the University of
     53  1.4.6.2  yamt  *	California, Berkeley and its contributors.
     54  1.4.6.2  yamt  * 4. Neither the name of the University nor the names of its contributors
     55  1.4.6.2  yamt  *    may be used to endorse or promote products derived from this software
     56  1.4.6.2  yamt  *    without specific prior written permission.
     57  1.4.6.2  yamt  *
     58  1.4.6.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  1.4.6.2  yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  1.4.6.2  yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  1.4.6.2  yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  1.4.6.2  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  1.4.6.2  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  1.4.6.2  yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  1.4.6.2  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  1.4.6.2  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  1.4.6.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  1.4.6.2  yamt  * SUCH DAMAGE.
     69  1.4.6.2  yamt  *
     70  1.4.6.2  yamt  *	@(#)clock.c	7.2 (Berkeley) 5/12/91
     71  1.4.6.2  yamt  */
     72  1.4.6.2  yamt /*
     73  1.4.6.2  yamt  * Mach Operating System
     74  1.4.6.2  yamt  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
     75  1.4.6.2  yamt  * All Rights Reserved.
     76  1.4.6.2  yamt  *
     77  1.4.6.2  yamt  * Permission to use, copy, modify and distribute this software and its
     78  1.4.6.2  yamt  * documentation is hereby granted, provided that both the copyright
     79  1.4.6.2  yamt  * notice and this permission notice appear in all copies of the
     80  1.4.6.2  yamt  * software, derivative works or modified versions, and any portions
     81  1.4.6.2  yamt  * thereof, and that both notices appear in supporting documentation.
     82  1.4.6.2  yamt  *
     83  1.4.6.2  yamt  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     84  1.4.6.2  yamt  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     85  1.4.6.2  yamt  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     86  1.4.6.2  yamt  *
     87  1.4.6.2  yamt  * Carnegie Mellon requests users of this software to return to
     88  1.4.6.2  yamt  *
     89  1.4.6.2  yamt  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     90  1.4.6.2  yamt  *  School of Computer Science
     91  1.4.6.2  yamt  *  Carnegie Mellon University
     92  1.4.6.2  yamt  *  Pittsburgh PA 15213-3890
     93  1.4.6.2  yamt  *
     94  1.4.6.2  yamt  * any improvements or extensions that they make and grant Carnegie Mellon
     95  1.4.6.2  yamt  * the rights to redistribute these changes.
     96  1.4.6.2  yamt  */
     97  1.4.6.2  yamt /*
     98  1.4.6.2  yamt   Copyright 1988, 1989 by Intel Corporation, Santa Clara, California.
     99  1.4.6.2  yamt 
    100  1.4.6.2  yamt 		All Rights Reserved
    101  1.4.6.2  yamt 
    102  1.4.6.2  yamt Permission to use, copy, modify, and distribute this software and
    103  1.4.6.2  yamt its documentation for any purpose and without fee is hereby
    104  1.4.6.2  yamt granted, provided that the above copyright notice appears in all
    105  1.4.6.2  yamt copies and that both the copyright notice and this permission notice
    106  1.4.6.2  yamt appear in supporting documentation, and that the name of Intel
    107  1.4.6.2  yamt not be used in advertising or publicity pertaining to distribution
    108  1.4.6.2  yamt of the software without specific, written prior permission.
    109  1.4.6.2  yamt 
    110  1.4.6.2  yamt INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
    111  1.4.6.2  yamt INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
    112  1.4.6.2  yamt IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
    113  1.4.6.2  yamt CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
    114  1.4.6.2  yamt LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
    115  1.4.6.2  yamt NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
    116  1.4.6.2  yamt WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    117  1.4.6.2  yamt */
    118  1.4.6.2  yamt 
    119  1.4.6.2  yamt /*
    120  1.4.6.2  yamt  * Primitive clock interrupt routines.
    121  1.4.6.2  yamt  */
    122  1.4.6.2  yamt 
    123  1.4.6.2  yamt #include <sys/cdefs.h>
    124  1.4.6.2  yamt __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.4.6.2 2006/09/14 12:31:22 yamt Exp $");
    125  1.4.6.2  yamt 
    126  1.4.6.2  yamt /* #define CLOCKDEBUG */
    127  1.4.6.2  yamt /* #define CLOCK_PARANOIA */
    128  1.4.6.2  yamt 
    129  1.4.6.2  yamt #include "opt_multiprocessor.h"
    130  1.4.6.2  yamt #include "opt_ntp.h"
    131  1.4.6.2  yamt 
    132  1.4.6.2  yamt #include <sys/param.h>
    133  1.4.6.2  yamt #include <sys/systm.h>
    134  1.4.6.2  yamt #include <sys/time.h>
    135  1.4.6.2  yamt #include <sys/timetc.h>
    136  1.4.6.2  yamt #include <sys/kernel.h>
    137  1.4.6.2  yamt #include <sys/device.h>
    138  1.4.6.2  yamt 
    139  1.4.6.2  yamt #include <machine/cpu.h>
    140  1.4.6.2  yamt #include <machine/intr.h>
    141  1.4.6.2  yamt #include <machine/pio.h>
    142  1.4.6.2  yamt #include <machine/cpufunc.h>
    143  1.4.6.2  yamt 
    144  1.4.6.2  yamt #include <dev/isa/isareg.h>
    145  1.4.6.2  yamt #include <dev/isa/isavar.h>
    146  1.4.6.2  yamt #include <dev/ic/mc146818reg.h>
    147  1.4.6.2  yamt #include <dev/ic/i8253reg.h>
    148  1.4.6.2  yamt #include <i386/isa/nvram.h>
    149  1.4.6.2  yamt #include <x86/x86/tsc.h>
    150  1.4.6.2  yamt #include <dev/clock_subr.h>
    151  1.4.6.2  yamt #include <machine/specialreg.h>
    152  1.4.6.2  yamt 
    153  1.4.6.2  yamt #include "config_time.h"		/* for CONFIG_TIME */
    154  1.4.6.2  yamt 
    155  1.4.6.2  yamt #ifndef __x86_64__
    156  1.4.6.2  yamt #include "mca.h"
    157  1.4.6.2  yamt #endif
    158  1.4.6.2  yamt #if NMCA > 0
    159  1.4.6.2  yamt #include <machine/mca_machdep.h>	/* for MCA_system */
    160  1.4.6.2  yamt #endif
    161  1.4.6.2  yamt 
    162  1.4.6.2  yamt #include "pcppi.h"
    163  1.4.6.2  yamt #if (NPCPPI > 0)
    164  1.4.6.2  yamt #include <dev/isa/pcppivar.h>
    165  1.4.6.2  yamt 
    166  1.4.6.2  yamt int sysbeepmatch(struct device *, struct cfdata *, void *);
    167  1.4.6.2  yamt void sysbeepattach(struct device *, struct device *, void *);
    168  1.4.6.2  yamt 
    169  1.4.6.2  yamt CFATTACH_DECL(sysbeep, sizeof(struct device),
    170  1.4.6.2  yamt     sysbeepmatch, sysbeepattach, NULL, NULL);
    171  1.4.6.2  yamt 
    172  1.4.6.2  yamt static int ppi_attached;
    173  1.4.6.2  yamt static pcppi_tag_t ppicookie;
    174  1.4.6.2  yamt #endif /* PCPPI */
    175  1.4.6.2  yamt 
    176  1.4.6.2  yamt #ifdef __x86_64__
    177  1.4.6.2  yamt #define READ_FLAGS()	read_rflags()
    178  1.4.6.2  yamt #define WRITE_FLAGS(x)	write_rflags(x)
    179  1.4.6.2  yamt #else /* i386 architecture processor */
    180  1.4.6.2  yamt #define READ_FLAGS()	read_eflags()
    181  1.4.6.2  yamt #define WRITE_FLAGS(x)	write_eflags(x)
    182  1.4.6.2  yamt #endif
    183  1.4.6.2  yamt 
    184  1.4.6.2  yamt #ifdef CLOCKDEBUG
    185  1.4.6.2  yamt int clock_debug = 0;
    186  1.4.6.2  yamt #define DPRINTF(arg) if (clock_debug) printf arg
    187  1.4.6.2  yamt #else
    188  1.4.6.2  yamt #define DPRINTF(arg)
    189  1.4.6.2  yamt #endif
    190  1.4.6.2  yamt 
    191  1.4.6.2  yamt int		gettick(void);
    192  1.4.6.2  yamt void		sysbeep(int, int);
    193  1.4.6.2  yamt static void     tickle_tc(void);
    194  1.4.6.2  yamt 
    195  1.4.6.2  yamt static int	clockintr(void *, struct intrframe);
    196  1.4.6.2  yamt static void	rtcinit(void);
    197  1.4.6.2  yamt static int	rtcget(mc_todregs *);
    198  1.4.6.2  yamt static void	rtcput(mc_todregs *);
    199  1.4.6.2  yamt 
    200  1.4.6.2  yamt static int	cmoscheck(void);
    201  1.4.6.2  yamt 
    202  1.4.6.2  yamt static int	clock_expandyear(int);
    203  1.4.6.2  yamt 
    204  1.4.6.2  yamt static inline int gettick_broken_latch(void);
    205  1.4.6.2  yamt 
    206  1.4.6.2  yamt static volatile uint32_t i8254_lastcount;
    207  1.4.6.2  yamt static volatile uint32_t i8254_offset;
    208  1.4.6.2  yamt static volatile int i8254_ticked;
    209  1.4.6.2  yamt 
    210  1.4.6.2  yamt static struct simplelock tmr_lock = SIMPLELOCK_INITIALIZER;  /* protect TC timer variables */
    211  1.4.6.2  yamt 
    212  1.4.6.2  yamt inline u_int mc146818_read(void *, u_int);
    213  1.4.6.2  yamt inline void mc146818_write(void *, u_int, u_int);
    214  1.4.6.2  yamt 
    215  1.4.6.2  yamt u_int i8254_get_timecount(struct timecounter *);
    216  1.4.6.2  yamt static void rtc_register(void);
    217  1.4.6.2  yamt 
    218  1.4.6.2  yamt static struct timecounter i8254_timecounter = {
    219  1.4.6.2  yamt 	i8254_get_timecount,	/* get_timecount */
    220  1.4.6.2  yamt 	0,			/* no poll_pps */
    221  1.4.6.2  yamt 	~0u,			/* counter_mask */
    222  1.4.6.2  yamt 	TIMER_FREQ,		/* frequency */
    223  1.4.6.2  yamt 	"i8254",		/* name */
    224  1.4.6.2  yamt 	100,			/* quality */
    225  1.4.6.2  yamt 	NULL,			/* prev */
    226  1.4.6.2  yamt 	NULL,			/* next */
    227  1.4.6.2  yamt };
    228  1.4.6.2  yamt 
    229  1.4.6.2  yamt /* XXX use sc? */
    230  1.4.6.2  yamt inline u_int
    231  1.4.6.2  yamt mc146818_read(void *sc, u_int reg)
    232  1.4.6.2  yamt {
    233  1.4.6.2  yamt 
    234  1.4.6.2  yamt 	outb(IO_RTC, reg);
    235  1.4.6.2  yamt 	return (inb(IO_RTC+1));
    236  1.4.6.2  yamt }
    237  1.4.6.2  yamt 
    238  1.4.6.2  yamt /* XXX use sc? */
    239  1.4.6.2  yamt inline void
    240  1.4.6.2  yamt mc146818_write(void *sc, u_int reg, u_int datum)
    241  1.4.6.2  yamt {
    242  1.4.6.2  yamt 
    243  1.4.6.2  yamt 	outb(IO_RTC, reg);
    244  1.4.6.2  yamt 	outb(IO_RTC+1, datum);
    245  1.4.6.2  yamt }
    246  1.4.6.2  yamt 
    247  1.4.6.2  yamt u_long rtclock_tval;		/* i8254 reload value for countdown */
    248  1.4.6.2  yamt int    rtclock_init = 0;
    249  1.4.6.2  yamt 
    250  1.4.6.2  yamt int clock_broken_latch = 0;
    251  1.4.6.2  yamt 
    252  1.4.6.2  yamt #ifdef CLOCK_PARANOIA
    253  1.4.6.2  yamt static int ticks[6];
    254  1.4.6.2  yamt #endif
    255  1.4.6.2  yamt /*
    256  1.4.6.2  yamt  * i8254 latch check routine:
    257  1.4.6.2  yamt  *     National Geode (formerly Cyrix MediaGX) has a serious bug in
    258  1.4.6.2  yamt  *     its built-in i8254-compatible clock module.
    259  1.4.6.2  yamt  *     machdep sets the variable 'clock_broken_latch' to indicate it.
    260  1.4.6.2  yamt  */
    261  1.4.6.2  yamt 
    262  1.4.6.2  yamt int
    263  1.4.6.2  yamt gettick_broken_latch(void)
    264  1.4.6.2  yamt {
    265  1.4.6.2  yamt 	u_long flags;
    266  1.4.6.2  yamt 	int v1, v2, v3;
    267  1.4.6.2  yamt 	int w1, w2, w3;
    268  1.4.6.2  yamt 
    269  1.4.6.2  yamt 	/* Don't want someone screwing with the counter while we're here. */
    270  1.4.6.2  yamt 	flags = READ_FLAGS();
    271  1.4.6.2  yamt 	disable_intr();
    272  1.4.6.2  yamt 
    273  1.4.6.2  yamt 	v1 = inb(IO_TIMER1+TIMER_CNTR0);
    274  1.4.6.2  yamt 	v1 |= inb(IO_TIMER1+TIMER_CNTR0) << 8;
    275  1.4.6.2  yamt 	v2 = inb(IO_TIMER1+TIMER_CNTR0);
    276  1.4.6.2  yamt 	v2 |= inb(IO_TIMER1+TIMER_CNTR0) << 8;
    277  1.4.6.2  yamt 	v3 = inb(IO_TIMER1+TIMER_CNTR0);
    278  1.4.6.2  yamt 	v3 |= inb(IO_TIMER1+TIMER_CNTR0) << 8;
    279  1.4.6.2  yamt 
    280  1.4.6.2  yamt 	WRITE_FLAGS(flags);
    281  1.4.6.2  yamt 
    282  1.4.6.2  yamt #ifdef CLOCK_PARANOIA
    283  1.4.6.2  yamt 	if (clock_debug) {
    284  1.4.6.2  yamt 		ticks[0] = ticks[3];
    285  1.4.6.2  yamt 		ticks[1] = ticks[4];
    286  1.4.6.2  yamt 		ticks[2] = ticks[5];
    287  1.4.6.2  yamt 		ticks[3] = v1;
    288  1.4.6.2  yamt 		ticks[4] = v2;
    289  1.4.6.2  yamt 		ticks[5] = v3;
    290  1.4.6.2  yamt 	}
    291  1.4.6.2  yamt #endif
    292  1.4.6.2  yamt 
    293  1.4.6.2  yamt 	if (v1 >= v2 && v2 >= v3 && v1 - v3 < 0x200)
    294  1.4.6.2  yamt 		return (v2);
    295  1.4.6.2  yamt 
    296  1.4.6.2  yamt #define _swap_val(a, b) do { \
    297  1.4.6.2  yamt 	int c = a; \
    298  1.4.6.2  yamt 	a = b; \
    299  1.4.6.2  yamt 	b = c; \
    300  1.4.6.2  yamt } while (0)
    301  1.4.6.2  yamt 
    302  1.4.6.2  yamt 	/*
    303  1.4.6.2  yamt 	 * sort v1 v2 v3
    304  1.4.6.2  yamt 	 */
    305  1.4.6.2  yamt 	if (v1 < v2)
    306  1.4.6.2  yamt 		_swap_val(v1, v2);
    307  1.4.6.2  yamt 	if (v2 < v3)
    308  1.4.6.2  yamt 		_swap_val(v2, v3);
    309  1.4.6.2  yamt 	if (v1 < v2)
    310  1.4.6.2  yamt 		_swap_val(v1, v2);
    311  1.4.6.2  yamt 
    312  1.4.6.2  yamt 	/*
    313  1.4.6.2  yamt 	 * compute the middle value
    314  1.4.6.2  yamt 	 */
    315  1.4.6.2  yamt 
    316  1.4.6.2  yamt 	if (v1 - v3 < 0x200)
    317  1.4.6.2  yamt 		return (v2);
    318  1.4.6.2  yamt 
    319  1.4.6.2  yamt 	w1 = v2 - v3;
    320  1.4.6.2  yamt 	w2 = v3 - v1 + rtclock_tval;
    321  1.4.6.2  yamt 	w3 = v1 - v2;
    322  1.4.6.2  yamt 	if (w1 >= w2) {
    323  1.4.6.2  yamt 		if (w1 >= w3)
    324  1.4.6.2  yamt 		        return (v1);
    325  1.4.6.2  yamt 	} else {
    326  1.4.6.2  yamt 		if (w2 >= w3)
    327  1.4.6.2  yamt 			return (v2);
    328  1.4.6.2  yamt 	}
    329  1.4.6.2  yamt 	return (v3);
    330  1.4.6.2  yamt }
    331  1.4.6.2  yamt 
    332  1.4.6.2  yamt /* minimal initialization, enough for delay() */
    333  1.4.6.2  yamt void
    334  1.4.6.2  yamt initrtclock(u_long freq)
    335  1.4.6.2  yamt {
    336  1.4.6.2  yamt 	u_long tval;
    337  1.4.6.2  yamt 	/*
    338  1.4.6.2  yamt 	 * Compute timer_count, the count-down count the timer will be
    339  1.4.6.2  yamt 	 * set to.  Also, correctly round
    340  1.4.6.2  yamt 	 * this by carrying an extra bit through the division.
    341  1.4.6.2  yamt 	 */
    342  1.4.6.2  yamt 	tval = (freq * 2) / (u_long) hz;
    343  1.4.6.2  yamt 	tval = (tval / 2) + (tval & 0x1);
    344  1.4.6.2  yamt 
    345  1.4.6.2  yamt 	/* initialize 8254 clock */
    346  1.4.6.2  yamt 	outb(IO_TIMER1+TIMER_MODE, TIMER_SEL0|TIMER_RATEGEN|TIMER_16BIT);
    347  1.4.6.2  yamt 
    348  1.4.6.2  yamt 	/* Correct rounding will buy us a better precision in timekeeping */
    349  1.4.6.2  yamt 	outb(IO_TIMER1+TIMER_CNTR0, tval % 256);
    350  1.4.6.2  yamt 	outb(IO_TIMER1+TIMER_CNTR0, tval / 256);
    351  1.4.6.2  yamt 
    352  1.4.6.2  yamt 	rtclock_tval = tval ? tval : 0xFFFF;
    353  1.4.6.2  yamt 	rtclock_init = 1;
    354  1.4.6.2  yamt }
    355  1.4.6.2  yamt 
    356  1.4.6.2  yamt void
    357  1.4.6.2  yamt startrtclock(void)
    358  1.4.6.2  yamt {
    359  1.4.6.2  yamt 	int s;
    360  1.4.6.2  yamt 
    361  1.4.6.2  yamt 	if (!rtclock_init)
    362  1.4.6.2  yamt 		initrtclock(TIMER_FREQ);
    363  1.4.6.2  yamt 
    364  1.4.6.2  yamt 	/* Check diagnostic status */
    365  1.4.6.2  yamt 	if ((s = mc146818_read(NULL, NVRAM_DIAG)) != 0) { /* XXX softc */
    366  1.4.6.2  yamt 		char bits[128];
    367  1.4.6.2  yamt 		printf("RTC BIOS diagnostic error %s\n",
    368  1.4.6.2  yamt 		    bitmask_snprintf(s, NVRAM_DIAG_BITS, bits, sizeof(bits)));
    369  1.4.6.2  yamt 	}
    370  1.4.6.2  yamt 
    371  1.4.6.2  yamt 	tc_init(&i8254_timecounter);
    372  1.4.6.2  yamt 
    373  1.4.6.2  yamt #if defined(I586_CPU) || defined(I686_CPU) || defined(__x86_64__)
    374  1.4.6.2  yamt 	init_TSC();
    375  1.4.6.2  yamt #endif
    376  1.4.6.2  yamt 
    377  1.4.6.2  yamt 	rtc_register();
    378  1.4.6.2  yamt }
    379  1.4.6.2  yamt 
    380  1.4.6.2  yamt 
    381  1.4.6.2  yamt static void
    382  1.4.6.2  yamt tickle_tc(void)
    383  1.4.6.2  yamt {
    384  1.4.6.2  yamt #if defined(MULTIPROCESSOR)
    385  1.4.6.2  yamt 	struct cpu_info *ci = curcpu();
    386  1.4.6.2  yamt 	/*
    387  1.4.6.2  yamt 	 * If we are not the primary CPU, we're not allowed to do
    388  1.4.6.2  yamt 	 * any more work.
    389  1.4.6.2  yamt 	 */
    390  1.4.6.2  yamt 	if (CPU_IS_PRIMARY(ci) == 0)
    391  1.4.6.2  yamt 		return;
    392  1.4.6.2  yamt #endif
    393  1.4.6.2  yamt 	if (rtclock_tval && timecounter->tc_get_timecount == i8254_get_timecount) {
    394  1.4.6.2  yamt 		simple_lock(&tmr_lock);
    395  1.4.6.2  yamt 		if (i8254_ticked)
    396  1.4.6.2  yamt 			i8254_ticked    = 0;
    397  1.4.6.2  yamt 		else {
    398  1.4.6.2  yamt 			i8254_offset   += rtclock_tval;
    399  1.4.6.2  yamt 			i8254_lastcount = 0;
    400  1.4.6.2  yamt 		}
    401  1.4.6.2  yamt 		simple_unlock(&tmr_lock);
    402  1.4.6.2  yamt 	}
    403  1.4.6.2  yamt 
    404  1.4.6.2  yamt }
    405  1.4.6.2  yamt 
    406  1.4.6.2  yamt static int
    407  1.4.6.2  yamt clockintr(void *arg, struct intrframe frame)
    408  1.4.6.2  yamt {
    409  1.4.6.2  yamt 	tickle_tc();
    410  1.4.6.2  yamt 
    411  1.4.6.2  yamt 	hardclock((struct clockframe *)&frame);
    412  1.4.6.2  yamt 
    413  1.4.6.2  yamt #if NMCA > 0
    414  1.4.6.2  yamt 	if (MCA_system) {
    415  1.4.6.2  yamt 		/* Reset PS/2 clock interrupt by asserting bit 7 of port 0x61 */
    416  1.4.6.2  yamt 		outb(0x61, inb(0x61) | 0x80);
    417  1.4.6.2  yamt 	}
    418  1.4.6.2  yamt #endif
    419  1.4.6.2  yamt 	return -1;
    420  1.4.6.2  yamt }
    421  1.4.6.2  yamt 
    422  1.4.6.2  yamt u_int
    423  1.4.6.2  yamt i8254_get_timecount(struct timecounter *tc)
    424  1.4.6.2  yamt {
    425  1.4.6.2  yamt 	u_int count;
    426  1.4.6.2  yamt 	u_char high, low;
    427  1.4.6.2  yamt 	u_long flags;
    428  1.4.6.2  yamt 
    429  1.4.6.2  yamt 	/* Don't want someone screwing with the counter while we're here. */
    430  1.4.6.2  yamt 	flags = READ_FLAGS();
    431  1.4.6.2  yamt 	disable_intr();
    432  1.4.6.2  yamt 
    433  1.4.6.2  yamt 	simple_lock(&tmr_lock);
    434  1.4.6.2  yamt 
    435  1.4.6.2  yamt 	/* Select timer0 and latch counter value. */
    436  1.4.6.2  yamt 	outb(IO_TIMER1 + TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
    437  1.4.6.2  yamt 
    438  1.4.6.2  yamt 	low = inb(IO_TIMER1 + TIMER_CNTR0);
    439  1.4.6.2  yamt 	high = inb(IO_TIMER1 + TIMER_CNTR0);
    440  1.4.6.2  yamt 	count = rtclock_tval - ((high << 8) | low);
    441  1.4.6.2  yamt 
    442  1.4.6.2  yamt 	if (rtclock_tval && (count < i8254_lastcount || !i8254_ticked)) {
    443  1.4.6.2  yamt 		i8254_ticked = 1;
    444  1.4.6.2  yamt 		i8254_offset += rtclock_tval;
    445  1.4.6.2  yamt 	}
    446  1.4.6.2  yamt 
    447  1.4.6.2  yamt 	i8254_lastcount = count;
    448  1.4.6.2  yamt 	count += i8254_offset;
    449  1.4.6.2  yamt 
    450  1.4.6.2  yamt 	simple_unlock(&tmr_lock);
    451  1.4.6.2  yamt 
    452  1.4.6.2  yamt 	WRITE_FLAGS(flags);
    453  1.4.6.2  yamt 	return (count);
    454  1.4.6.2  yamt }
    455  1.4.6.2  yamt 
    456  1.4.6.2  yamt int
    457  1.4.6.2  yamt gettick(void)
    458  1.4.6.2  yamt {
    459  1.4.6.2  yamt 	u_long flags;
    460  1.4.6.2  yamt 	u_char lo, hi;
    461  1.4.6.2  yamt 
    462  1.4.6.2  yamt 	if (clock_broken_latch)
    463  1.4.6.2  yamt 		return (gettick_broken_latch());
    464  1.4.6.2  yamt 
    465  1.4.6.2  yamt 	/* Don't want someone screwing with the counter while we're here. */
    466  1.4.6.2  yamt 	flags = READ_FLAGS();
    467  1.4.6.2  yamt 	disable_intr();
    468  1.4.6.2  yamt 	/* Select counter 0 and latch it. */
    469  1.4.6.2  yamt 	outb(IO_TIMER1+TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
    470  1.4.6.2  yamt 	lo = inb(IO_TIMER1+TIMER_CNTR0);
    471  1.4.6.2  yamt 	hi = inb(IO_TIMER1+TIMER_CNTR0);
    472  1.4.6.2  yamt 	WRITE_FLAGS(flags);
    473  1.4.6.2  yamt 	return ((hi << 8) | lo);
    474  1.4.6.2  yamt }
    475  1.4.6.2  yamt 
    476  1.4.6.2  yamt /*
    477  1.4.6.2  yamt  * Wait approximately `n' microseconds.
    478  1.4.6.2  yamt  * Relies on timer 1 counting down from (TIMER_FREQ / hz) at TIMER_FREQ Hz.
    479  1.4.6.2  yamt  * Note: timer had better have been programmed before this is first used!
    480  1.4.6.2  yamt  * (Note that we use `rate generator' mode, which counts at 1:1; `square
    481  1.4.6.2  yamt  * wave' mode counts at 2:1).
    482  1.4.6.2  yamt  * Don't rely on this being particularly accurate.
    483  1.4.6.2  yamt  */
    484  1.4.6.2  yamt void
    485  1.4.6.2  yamt i8254_delay(int n)
    486  1.4.6.2  yamt {
    487  1.4.6.2  yamt 	int delay_tick, odelay_tick;
    488  1.4.6.2  yamt 	static const int delaytab[26] = {
    489  1.4.6.2  yamt 		 0,  2,  3,  4,  5,  6,  7,  9, 10, 11,
    490  1.4.6.2  yamt 		12, 13, 15, 16, 17, 18, 19, 21, 22, 23,
    491  1.4.6.2  yamt 		24, 25, 27, 28, 29, 30,
    492  1.4.6.2  yamt 	};
    493  1.4.6.2  yamt 
    494  1.4.6.2  yamt 	/* allow DELAY() to be used before startrtclock() */
    495  1.4.6.2  yamt 	if (!rtclock_init)
    496  1.4.6.2  yamt 		initrtclock(TIMER_FREQ);
    497  1.4.6.2  yamt 
    498  1.4.6.2  yamt 	/*
    499  1.4.6.2  yamt 	 * Read the counter first, so that the rest of the setup overhead is
    500  1.4.6.2  yamt 	 * counted.
    501  1.4.6.2  yamt 	 */
    502  1.4.6.2  yamt 	odelay_tick = gettick();
    503  1.4.6.2  yamt 
    504  1.4.6.2  yamt 	if (n <= 25)
    505  1.4.6.2  yamt 		n = delaytab[n];
    506  1.4.6.2  yamt 	else {
    507  1.4.6.2  yamt #ifdef __GNUC__
    508  1.4.6.2  yamt 		/*
    509  1.4.6.2  yamt 		 * Calculate ((n * TIMER_FREQ) / 1e6) using explicit assembler
    510  1.4.6.2  yamt 		 * code so we can take advantage of the intermediate 64-bit
    511  1.4.6.2  yamt 		 * quantity to prevent loss of significance.
    512  1.4.6.2  yamt 		 */
    513  1.4.6.2  yamt 		int m;
    514  1.4.6.2  yamt 		__asm volatile("mul %3"
    515  1.4.6.2  yamt 				 : "=a" (n), "=d" (m)
    516  1.4.6.2  yamt 				 : "0" (n), "r" (TIMER_FREQ));
    517  1.4.6.2  yamt 		__asm volatile("div %4"
    518  1.4.6.2  yamt 				 : "=a" (n), "=d" (m)
    519  1.4.6.2  yamt 				 : "0" (n), "1" (m), "r" (1000000));
    520  1.4.6.2  yamt #else
    521  1.4.6.2  yamt 		/*
    522  1.4.6.2  yamt 		 * Calculate ((n * TIMER_FREQ) / 1e6) without using floating
    523  1.4.6.2  yamt 		 * point and without any avoidable overflows.
    524  1.4.6.2  yamt 		 */
    525  1.4.6.2  yamt 		int sec = n / 1000000,
    526  1.4.6.2  yamt 		    usec = n % 1000000;
    527  1.4.6.2  yamt 		n = sec * TIMER_FREQ +
    528  1.4.6.2  yamt 		    usec * (TIMER_FREQ / 1000000) +
    529  1.4.6.2  yamt 		    usec * ((TIMER_FREQ % 1000000) / 1000) / 1000 +
    530  1.4.6.2  yamt 		    usec * (TIMER_FREQ % 1000) / 1000000;
    531  1.4.6.2  yamt #endif
    532  1.4.6.2  yamt 	}
    533  1.4.6.2  yamt 
    534  1.4.6.2  yamt 	while (n > 0) {
    535  1.4.6.2  yamt #ifdef CLOCK_PARANOIA
    536  1.4.6.2  yamt 		int delta;
    537  1.4.6.2  yamt 		delay_tick = gettick();
    538  1.4.6.2  yamt 		if (delay_tick > odelay_tick)
    539  1.4.6.2  yamt 			delta = rtclock_tval - (delay_tick - odelay_tick);
    540  1.4.6.2  yamt 		else
    541  1.4.6.2  yamt 			delta = odelay_tick - delay_tick;
    542  1.4.6.2  yamt 		if (delta < 0 || delta >= rtclock_tval / 2) {
    543  1.4.6.2  yamt 			DPRINTF(("delay: ignore ticks %.4x-%.4x",
    544  1.4.6.2  yamt 				 odelay_tick, delay_tick));
    545  1.4.6.2  yamt 			if (clock_broken_latch) {
    546  1.4.6.2  yamt 				DPRINTF(("  (%.4x %.4x %.4x %.4x %.4x %.4x)\n",
    547  1.4.6.2  yamt 				         ticks[0], ticks[1], ticks[2],
    548  1.4.6.2  yamt 				         ticks[3], ticks[4], ticks[5]));
    549  1.4.6.2  yamt 			} else {
    550  1.4.6.2  yamt 				DPRINTF(("\n"));
    551  1.4.6.2  yamt 			}
    552  1.4.6.2  yamt 		} else
    553  1.4.6.2  yamt 			n -= delta;
    554  1.4.6.2  yamt #else
    555  1.4.6.2  yamt 		delay_tick = gettick();
    556  1.4.6.2  yamt 		if (delay_tick > odelay_tick)
    557  1.4.6.2  yamt 			n -= rtclock_tval - (delay_tick - odelay_tick);
    558  1.4.6.2  yamt 		else
    559  1.4.6.2  yamt 			n -= odelay_tick - delay_tick;
    560  1.4.6.2  yamt #endif
    561  1.4.6.2  yamt 		odelay_tick = delay_tick;
    562  1.4.6.2  yamt 	}
    563  1.4.6.2  yamt }
    564  1.4.6.2  yamt 
    565  1.4.6.2  yamt #if (NPCPPI > 0)
    566  1.4.6.2  yamt int
    567  1.4.6.2  yamt sysbeepmatch(struct device *parent, struct cfdata *match, void *aux)
    568  1.4.6.2  yamt {
    569  1.4.6.2  yamt 	return (!ppi_attached);
    570  1.4.6.2  yamt }
    571  1.4.6.2  yamt 
    572  1.4.6.2  yamt void
    573  1.4.6.2  yamt sysbeepattach(struct device *parent, struct device *self, void *aux)
    574  1.4.6.2  yamt {
    575  1.4.6.2  yamt 	aprint_naive("\n");
    576  1.4.6.2  yamt 	aprint_normal("\n");
    577  1.4.6.2  yamt 
    578  1.4.6.2  yamt 	ppicookie = ((struct pcppi_attach_args *)aux)->pa_cookie;
    579  1.4.6.2  yamt 	ppi_attached = 1;
    580  1.4.6.2  yamt }
    581  1.4.6.2  yamt #endif
    582  1.4.6.2  yamt 
    583  1.4.6.2  yamt void
    584  1.4.6.2  yamt sysbeep(int pitch, int period)
    585  1.4.6.2  yamt {
    586  1.4.6.2  yamt #if (NPCPPI > 0)
    587  1.4.6.2  yamt 	if (ppi_attached)
    588  1.4.6.2  yamt 		pcppi_bell(ppicookie, pitch, period, 0);
    589  1.4.6.2  yamt #endif
    590  1.4.6.2  yamt }
    591  1.4.6.2  yamt 
    592  1.4.6.2  yamt void
    593  1.4.6.2  yamt i8254_initclocks(void)
    594  1.4.6.2  yamt {
    595  1.4.6.2  yamt 
    596  1.4.6.2  yamt 	/*
    597  1.4.6.2  yamt 	 * XXX If you're doing strange things with multiple clocks, you might
    598  1.4.6.2  yamt 	 * want to keep track of clock handlers.
    599  1.4.6.2  yamt 	 */
    600  1.4.6.2  yamt 	(void)isa_intr_establish(NULL, 0, IST_PULSE, IPL_CLOCK,
    601  1.4.6.2  yamt 	    (int (*)(void *))clockintr, 0);
    602  1.4.6.2  yamt }
    603  1.4.6.2  yamt 
    604  1.4.6.2  yamt static void
    605  1.4.6.2  yamt rtcinit(void)
    606  1.4.6.2  yamt {
    607  1.4.6.2  yamt 	static int first_rtcopen_ever = 1;
    608  1.4.6.2  yamt 
    609  1.4.6.2  yamt 	if (!first_rtcopen_ever)
    610  1.4.6.2  yamt 		return;
    611  1.4.6.2  yamt 	first_rtcopen_ever = 0;
    612  1.4.6.2  yamt 
    613  1.4.6.2  yamt 	mc146818_write(NULL, MC_REGA,			/* XXX softc */
    614  1.4.6.2  yamt 	    MC_BASE_32_KHz | MC_RATE_1024_Hz);
    615  1.4.6.2  yamt 	mc146818_write(NULL, MC_REGB, MC_REGB_24HR);	/* XXX softc */
    616  1.4.6.2  yamt }
    617  1.4.6.2  yamt 
    618  1.4.6.2  yamt static int
    619  1.4.6.2  yamt rtcget(mc_todregs *regs)
    620  1.4.6.2  yamt {
    621  1.4.6.2  yamt 
    622  1.4.6.2  yamt 	rtcinit();
    623  1.4.6.2  yamt 	if ((mc146818_read(NULL, MC_REGD) & MC_REGD_VRT) == 0) /* XXX softc */
    624  1.4.6.2  yamt 		return (-1);
    625  1.4.6.2  yamt 	MC146818_GETTOD(NULL, regs);			/* XXX softc */
    626  1.4.6.2  yamt 	return (0);
    627  1.4.6.2  yamt }
    628  1.4.6.2  yamt 
    629  1.4.6.2  yamt static void
    630  1.4.6.2  yamt rtcput(mc_todregs *regs)
    631  1.4.6.2  yamt {
    632  1.4.6.2  yamt 
    633  1.4.6.2  yamt 	rtcinit();
    634  1.4.6.2  yamt 	MC146818_PUTTOD(NULL, regs);			/* XXX softc */
    635  1.4.6.2  yamt }
    636  1.4.6.2  yamt 
    637  1.4.6.2  yamt /*
    638  1.4.6.2  yamt  * check whether the CMOS layout is "standard"-like (ie, not PS/2-like),
    639  1.4.6.2  yamt  * to be called at splclock()
    640  1.4.6.2  yamt  */
    641  1.4.6.2  yamt static int
    642  1.4.6.2  yamt cmoscheck(void)
    643  1.4.6.2  yamt {
    644  1.4.6.2  yamt 	int i;
    645  1.4.6.2  yamt 	unsigned short cksum = 0;
    646  1.4.6.2  yamt 
    647  1.4.6.2  yamt 	for (i = 0x10; i <= 0x2d; i++)
    648  1.4.6.2  yamt 		cksum += mc146818_read(NULL, i); /* XXX softc */
    649  1.4.6.2  yamt 
    650  1.4.6.2  yamt 	return (cksum == (mc146818_read(NULL, 0x2e) << 8)
    651  1.4.6.2  yamt 			  + mc146818_read(NULL, 0x2f));
    652  1.4.6.2  yamt }
    653  1.4.6.2  yamt 
    654  1.4.6.2  yamt #if NMCA > 0
    655  1.4.6.2  yamt /*
    656  1.4.6.2  yamt  * Check whether the CMOS layout is PS/2 like, to be called at splclock().
    657  1.4.6.2  yamt  */
    658  1.4.6.2  yamt static int cmoscheckps2(void);
    659  1.4.6.2  yamt static int
    660  1.4.6.2  yamt cmoscheckps2(void)
    661  1.4.6.2  yamt {
    662  1.4.6.2  yamt #if 0
    663  1.4.6.2  yamt 	/* Disabled until I find out the CRC checksum algorithm IBM uses */
    664  1.4.6.2  yamt 	int i;
    665  1.4.6.2  yamt 	unsigned short cksum = 0;
    666  1.4.6.2  yamt 
    667  1.4.6.2  yamt 	for (i = 0x10; i <= 0x31; i++)
    668  1.4.6.2  yamt 		cksum += mc146818_read(NULL, i); /* XXX softc */
    669  1.4.6.2  yamt 
    670  1.4.6.2  yamt 	return (cksum == (mc146818_read(NULL, 0x32) << 8)
    671  1.4.6.2  yamt 			  + mc146818_read(NULL, 0x33));
    672  1.4.6.2  yamt #else
    673  1.4.6.2  yamt 	/* Check 'incorrect checksum' bit of IBM PS/2 Diagnostic Status Byte */
    674  1.4.6.2  yamt 	return ((mc146818_read(NULL, NVRAM_DIAG) & (1<<6)) == 0);
    675  1.4.6.2  yamt #endif
    676  1.4.6.2  yamt }
    677  1.4.6.2  yamt #endif /* NMCA > 0 */
    678  1.4.6.2  yamt 
    679  1.4.6.2  yamt /*
    680  1.4.6.2  yamt  * patchable to control century byte handling:
    681  1.4.6.2  yamt  * 1: always update
    682  1.4.6.2  yamt  * -1: never touch
    683  1.4.6.2  yamt  * 0: try to figure out itself
    684  1.4.6.2  yamt  */
    685  1.4.6.2  yamt int rtc_update_century = 0;
    686  1.4.6.2  yamt 
    687  1.4.6.2  yamt /*
    688  1.4.6.2  yamt  * Expand a two-digit year as read from the clock chip
    689  1.4.6.2  yamt  * into full width.
    690  1.4.6.2  yamt  * Being here, deal with the CMOS century byte.
    691  1.4.6.2  yamt  */
    692  1.4.6.2  yamt static int centb = NVRAM_CENTURY;
    693  1.4.6.2  yamt static int
    694  1.4.6.2  yamt clock_expandyear(int clockyear)
    695  1.4.6.2  yamt {
    696  1.4.6.2  yamt 	int s, clockcentury, cmoscentury;
    697  1.4.6.2  yamt 
    698  1.4.6.2  yamt 	clockcentury = (clockyear < 70) ? 20 : 19;
    699  1.4.6.2  yamt 	clockyear += 100 * clockcentury;
    700  1.4.6.2  yamt 
    701  1.4.6.2  yamt 	if (rtc_update_century < 0)
    702  1.4.6.2  yamt 		return (clockyear);
    703  1.4.6.2  yamt 
    704  1.4.6.2  yamt 	s = splclock();
    705  1.4.6.2  yamt 	if (cmoscheck())
    706  1.4.6.2  yamt 		cmoscentury = mc146818_read(NULL, NVRAM_CENTURY);
    707  1.4.6.2  yamt #if NMCA > 0
    708  1.4.6.2  yamt 	else if (MCA_system && cmoscheckps2())
    709  1.4.6.2  yamt 		cmoscentury = mc146818_read(NULL, (centb = 0x37));
    710  1.4.6.2  yamt #endif
    711  1.4.6.2  yamt 	else
    712  1.4.6.2  yamt 		cmoscentury = 0;
    713  1.4.6.2  yamt 	splx(s);
    714  1.4.6.2  yamt 	if (!cmoscentury) {
    715  1.4.6.2  yamt #ifdef DIAGNOSTIC
    716  1.4.6.2  yamt 		printf("clock: unknown CMOS layout\n");
    717  1.4.6.2  yamt #endif
    718  1.4.6.2  yamt 		return (clockyear);
    719  1.4.6.2  yamt 	}
    720  1.4.6.2  yamt 	cmoscentury = bcdtobin(cmoscentury);
    721  1.4.6.2  yamt 
    722  1.4.6.2  yamt 	if (cmoscentury != clockcentury) {
    723  1.4.6.2  yamt 		/* XXX note: saying "century is 20" might confuse the naive. */
    724  1.4.6.2  yamt 		printf("WARNING: NVRAM century is %d but RTC year is %d\n",
    725  1.4.6.2  yamt 		       cmoscentury, clockyear);
    726  1.4.6.2  yamt 
    727  1.4.6.2  yamt 		/* Kludge to roll over century. */
    728  1.4.6.2  yamt 		if ((rtc_update_century > 0) ||
    729  1.4.6.2  yamt 		    ((cmoscentury == 19) && (clockcentury == 20) &&
    730  1.4.6.2  yamt 		     (clockyear == 2000))) {
    731  1.4.6.2  yamt 			printf("WARNING: Setting NVRAM century to %d\n",
    732  1.4.6.2  yamt 			       clockcentury);
    733  1.4.6.2  yamt 			s = splclock();
    734  1.4.6.2  yamt 			mc146818_write(NULL, centb, bintobcd(clockcentury));
    735  1.4.6.2  yamt 			splx(s);
    736  1.4.6.2  yamt 		}
    737  1.4.6.2  yamt 	} else if (cmoscentury == 19 && rtc_update_century == 0)
    738  1.4.6.2  yamt 		rtc_update_century = 1; /* will update later in resettodr() */
    739  1.4.6.2  yamt 
    740  1.4.6.2  yamt 	return (clockyear);
    741  1.4.6.2  yamt }
    742  1.4.6.2  yamt 
    743  1.4.6.2  yamt static int
    744  1.4.6.2  yamt rtc_get_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    745  1.4.6.2  yamt {
    746  1.4.6.2  yamt 	int s;
    747  1.4.6.2  yamt 	mc_todregs rtclk;
    748  1.4.6.2  yamt 
    749  1.4.6.2  yamt 	s = splclock();
    750  1.4.6.2  yamt 	if (rtcget(&rtclk)) {
    751  1.4.6.2  yamt 		splx(s);
    752  1.4.6.2  yamt 		return -1;
    753  1.4.6.2  yamt 	}
    754  1.4.6.2  yamt 	splx(s);
    755  1.4.6.2  yamt 
    756  1.4.6.2  yamt 	dt->dt_sec = bcdtobin(rtclk[MC_SEC]);
    757  1.4.6.2  yamt 	dt->dt_min = bcdtobin(rtclk[MC_MIN]);
    758  1.4.6.2  yamt 	dt->dt_hour = bcdtobin(rtclk[MC_HOUR]);
    759  1.4.6.2  yamt 	dt->dt_day = bcdtobin(rtclk[MC_DOM]);
    760  1.4.6.2  yamt 	dt->dt_mon = bcdtobin(rtclk[MC_MONTH]);
    761  1.4.6.2  yamt 	dt->dt_year = clock_expandyear(bcdtobin(rtclk[MC_YEAR]));
    762  1.4.6.2  yamt 
    763  1.4.6.2  yamt 	return 0;
    764  1.4.6.2  yamt }
    765  1.4.6.2  yamt 
    766  1.4.6.2  yamt static int
    767  1.4.6.2  yamt rtc_set_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    768  1.4.6.2  yamt {
    769  1.4.6.2  yamt 	mc_todregs rtclk;
    770  1.4.6.2  yamt 	int century;
    771  1.4.6.2  yamt 	int s;
    772  1.4.6.2  yamt 
    773  1.4.6.2  yamt 	s = splclock();
    774  1.4.6.2  yamt 	if (rtcget(&rtclk))
    775  1.4.6.2  yamt 		memset(&rtclk, 0, sizeof(rtclk));
    776  1.4.6.2  yamt 	splx(s);
    777  1.4.6.2  yamt 
    778  1.4.6.2  yamt 	rtclk[MC_SEC] = bintobcd(dt->dt_sec);
    779  1.4.6.2  yamt 	rtclk[MC_MIN] = bintobcd(dt->dt_min);
    780  1.4.6.2  yamt 	rtclk[MC_HOUR] = bintobcd(dt->dt_hour);
    781  1.4.6.2  yamt 	rtclk[MC_DOW] = dt->dt_wday + 1;
    782  1.4.6.2  yamt 	rtclk[MC_YEAR] = bintobcd(dt->dt_year % 100);
    783  1.4.6.2  yamt 	rtclk[MC_MONTH] = bintobcd(dt->dt_mon);
    784  1.4.6.2  yamt 	rtclk[MC_DOM] = bintobcd(dt->dt_day);
    785  1.4.6.2  yamt 
    786  1.4.6.2  yamt #ifdef DEBUG_CLOCK
    787  1.4.6.2  yamt 	printf("setclock: %x/%x/%x %x:%x:%x\n", rtclk[MC_YEAR], rtclk[MC_MONTH],
    788  1.4.6.2  yamt 	   rtclk[MC_DOM], rtclk[MC_HOUR], rtclk[MC_MIN], rtclk[MC_SEC]);
    789  1.4.6.2  yamt #endif
    790  1.4.6.2  yamt 	s = splclock();
    791  1.4.6.2  yamt 	rtcput(&rtclk);
    792  1.4.6.2  yamt 	if (rtc_update_century > 0) {
    793  1.4.6.2  yamt 		century = bintobcd(dt->dt_year / 100);
    794  1.4.6.2  yamt 		mc146818_write(NULL, centb, century); /* XXX softc */
    795  1.4.6.2  yamt 	}
    796  1.4.6.2  yamt 	splx(s);
    797  1.4.6.2  yamt 	return 0;
    798  1.4.6.2  yamt 
    799  1.4.6.2  yamt }
    800  1.4.6.2  yamt 
    801  1.4.6.2  yamt static void
    802  1.4.6.2  yamt rtc_register(void)
    803  1.4.6.2  yamt {
    804  1.4.6.2  yamt 	static struct todr_chip_handle	tch;
    805  1.4.6.2  yamt 	tch.todr_gettime_ymdhms = rtc_get_ymdhms;
    806  1.4.6.2  yamt 	tch.todr_settime_ymdhms = rtc_set_ymdhms;
    807  1.4.6.2  yamt 	tch.todr_setwen = NULL;
    808  1.4.6.2  yamt 
    809  1.4.6.2  yamt 	todr_attach(&tch);
    810  1.4.6.2  yamt }
    811  1.4.6.2  yamt 
    812  1.4.6.2  yamt void
    813  1.4.6.2  yamt setstatclockrate(int arg)
    814  1.4.6.2  yamt {
    815  1.4.6.2  yamt }
    816