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