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