Home | History | Annotate | Line # | Download | only in dev
clock.c revision 1.33
      1  1.33     lukem /*	$NetBSD: clock.c,v 1.33 2003/07/15 01:19:48 lukem Exp $	*/
      2   1.1       leo 
      3   1.1       leo /*
      4   1.1       leo  * Copyright (c) 1988 University of Utah.
      5   1.1       leo  * Copyright (c) 1982, 1990 The Regents of the University of California.
      6   1.1       leo  * All rights reserved.
      7   1.1       leo  *
      8   1.1       leo  * This code is derived from software contributed to Berkeley by
      9   1.1       leo  * the Systems Programming Group of the University of Utah Computer
     10   1.1       leo  * Science Department.
     11   1.1       leo  *
     12   1.1       leo  * Redistribution and use in source and binary forms, with or without
     13   1.1       leo  * modification, are permitted provided that the following conditions
     14   1.1       leo  * are met:
     15   1.1       leo  * 1. Redistributions of source code must retain the above copyright
     16   1.1       leo  *    notice, this list of conditions and the following disclaimer.
     17   1.1       leo  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.1       leo  *    notice, this list of conditions and the following disclaimer in the
     19   1.1       leo  *    documentation and/or other materials provided with the distribution.
     20   1.1       leo  * 3. All advertising materials mentioning features or use of this software
     21   1.1       leo  *    must display the following acknowledgement:
     22   1.1       leo  *	This product includes software developed by the University of
     23   1.1       leo  *	California, Berkeley and its contributors.
     24   1.1       leo  * 4. Neither the name of the University nor the names of its contributors
     25   1.1       leo  *    may be used to endorse or promote products derived from this software
     26   1.1       leo  *    without specific prior written permission.
     27   1.1       leo  *
     28   1.1       leo  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29   1.1       leo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30   1.1       leo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31   1.1       leo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32   1.1       leo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33   1.1       leo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34   1.1       leo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35   1.1       leo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36   1.1       leo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37   1.1       leo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38   1.1       leo  * SUCH DAMAGE.
     39   1.1       leo  *
     40   1.1       leo  * from: Utah $Hdr: clock.c 1.18 91/01/21$
     41   1.1       leo  *
     42   1.1       leo  *	@(#)clock.c	7.6 (Berkeley) 5/7/91
     43   1.1       leo  */
     44  1.33     lukem 
     45  1.33     lukem #include <sys/cdefs.h>
     46  1.33     lukem __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.33 2003/07/15 01:19:48 lukem Exp $");
     47   1.1       leo 
     48   1.1       leo #include <sys/param.h>
     49   1.1       leo #include <sys/kernel.h>
     50   1.9       leo #include <sys/systm.h>
     51   1.1       leo #include <sys/device.h>
     52  1.14       leo #include <sys/uio.h>
     53  1.14       leo #include <sys/conf.h>
     54  1.32   thorpej #include <sys/proc.h>
     55  1.31  jdolecek #include <sys/event.h>
     56  1.18       leo 
     57  1.18       leo #include <dev/clock_subr.h>
     58  1.18       leo 
     59   1.1       leo #include <machine/psl.h>
     60   1.1       leo #include <machine/cpu.h>
     61   1.1       leo #include <machine/iomap.h>
     62   1.1       leo #include <machine/mfp.h>
     63   1.1       leo #include <atari/dev/clockreg.h>
     64  1.14       leo #include <atari/atari/device.h>
     65   1.1       leo 
     66   1.4       leo #if defined(GPROF) && defined(PROFTIMER)
     67   1.4       leo #include <machine/profile.h>
     68   1.1       leo #endif
     69   1.1       leo 
     70   1.1       leo /*
     71   1.5       leo  * The MFP clock runs at 2457600Hz. We use a {system,stat,prof}clock divider
     72   1.5       leo  * of 200. Therefore the timer runs at an effective rate of:
     73   1.5       leo  * 2457600/200 = 12288Hz.
     74   1.5       leo  */
     75   1.5       leo #define CLOCK_HZ	12288
     76   1.5       leo 
     77   1.5       leo /*
     78   1.1       leo  * Machine-dependent clock routines.
     79   1.1       leo  *
     80   1.1       leo  * Inittodr initializes the time of day hardware which provides
     81   1.1       leo  * date functions.
     82   1.1       leo  *
     83   1.1       leo  * Resettodr restores the time of day hardware after a time change.
     84   1.1       leo  */
     85   1.1       leo 
     86  1.14       leo struct clock_softc {
     87  1.14       leo 	struct device	sc_dev;
     88  1.14       leo 	int		sc_flags;
     89  1.14       leo };
     90  1.14       leo 
     91  1.14       leo /*
     92  1.14       leo  *  'sc_flags' state info. Only used by the rtc-device functions.
     93  1.14       leo  */
     94  1.14       leo #define	RTC_OPEN	1
     95  1.14       leo 
     96  1.14       leo dev_type_open(rtcopen);
     97  1.14       leo dev_type_close(rtcclose);
     98  1.14       leo dev_type_read(rtcread);
     99  1.14       leo dev_type_write(rtcwrite);
    100  1.14       leo 
    101  1.14       leo static void	clockattach __P((struct device *, struct device *, void *));
    102  1.17       leo static int	clockmatch __P((struct device *, struct cfdata *, void *));
    103   1.1       leo 
    104  1.30   thorpej CFATTACH_DECL(clock, sizeof(struct clock_softc),
    105  1.30   thorpej     clockmatch, clockattach, NULL, NULL);
    106  1.10   thorpej 
    107  1.19   thorpej extern struct cfdriver clock_cd;
    108  1.28   gehenna 
    109  1.28   gehenna const struct cdevsw rtc_cdevsw = {
    110  1.28   gehenna 	rtcopen, rtcclose, rtcread, rtcwrite, noioctl,
    111  1.31  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter,
    112  1.28   gehenna };
    113   1.1       leo 
    114  1.16       leo void statintr __P((struct clockframe));
    115   1.9       leo 
    116   1.1       leo static u_long	gettod __P((void));
    117  1.14       leo static int	twodigits __P((char *, int));
    118   1.1       leo 
    119   1.5       leo static int	divisor;	/* Systemclock divisor	*/
    120   1.5       leo 
    121   1.5       leo /*
    122   1.5       leo  * Statistics and profile clock intervals and variances. Variance must
    123   1.5       leo  * be a power of 2. Since this gives us an even number, not an odd number,
    124   1.5       leo  * we discard one case and compensate. That is, a variance of 64 would
    125   1.5       leo  * give us offsets in [0..63]. Instead, we take offsets in [1..63].
    126  1.26       wiz  * This is symmetric around the point 32, or statvar/2, and thus averages
    127   1.5       leo  * to that value (assuming uniform random numbers).
    128   1.5       leo  */
    129   1.5       leo #ifdef STATCLOCK
    130   1.5       leo static int	statvar = 32;	/* {stat,prof}clock variance		*/
    131   1.5       leo static int	statmin;	/* statclock divisor - variance/2	*/
    132   1.5       leo static int	profmin;	/* profclock divisor - variance/2	*/
    133  1.27       wiz static int	clk2min;	/* current, from above choices		*/
    134   1.5       leo #endif
    135   1.1       leo 
    136   1.1       leo int
    137  1.17       leo clockmatch(pdp, cfp, auxp)
    138  1.14       leo struct device	*pdp;
    139  1.17       leo struct cfdata	*cfp;
    140  1.17       leo void		*auxp;
    141   1.1       leo {
    142  1.15       leo 	if (!atari_realconfig) {
    143  1.15       leo 	    /*
    144  1.15       leo 	     * Initialize Timer-B in the ST-MFP. This timer is used by
    145  1.15       leo 	     * the 'delay' function below. This timer is setup to be
    146  1.15       leo 	     * continueously counting from 255 back to zero at a
    147  1.15       leo 	     * frequency of 614400Hz. We do this *early* in the
    148  1.15       leo 	     * initialisation process.
    149  1.15       leo 	     */
    150  1.15       leo 	    MFP->mf_tbcr  = 0;		/* Stop timer			*/
    151  1.15       leo 	    MFP->mf_iera &= ~IA_TIMB;	/* Disable timer interrupts	*/
    152  1.15       leo 	    MFP->mf_tbdr  = 0;
    153  1.15       leo 	    MFP->mf_tbcr  = T_Q004;	/* Start timer			*/
    154  1.15       leo 
    155  1.15       leo 	    /*
    156  1.15       leo 	     * Initialize the time structure
    157  1.15       leo 	     */
    158  1.15       leo 	    time.tv_sec  = 0;
    159  1.15       leo 	    time.tv_usec = 0;
    160  1.15       leo 
    161  1.15       leo 	    return 0;
    162  1.15       leo 	}
    163   1.1       leo 	if(!strcmp("clock", auxp))
    164   1.1       leo 		return(1);
    165   1.1       leo 	return(0);
    166   1.1       leo }
    167   1.1       leo 
    168   1.1       leo /*
    169   1.1       leo  * Start the real-time clock.
    170   1.1       leo  */
    171   1.1       leo void clockattach(pdp, dp, auxp)
    172   1.1       leo struct device	*pdp, *dp;
    173  1.14       leo void		*auxp;
    174   1.1       leo {
    175  1.14       leo 	struct clock_softc *sc = (void *)dp;
    176  1.14       leo 
    177  1.14       leo 	sc->sc_flags = 0;
    178  1.14       leo 
    179   1.1       leo 	/*
    180   1.3       leo 	 * Initialize Timer-A in the ST-MFP. We use a divisor of 200.
    181   1.3       leo 	 * The MFP clock runs at 2457600Hz. Therefore the timer runs
    182   1.3       leo 	 * at an effective rate of: 2457600/200 = 12288Hz. The
    183   1.3       leo 	 * following expression works for 48, 64 or 96 hz.
    184   1.1       leo 	 */
    185   1.5       leo 	divisor       = CLOCK_HZ/hz;
    186   1.2       leo 	MFP->mf_tacr  = 0;		/* Stop timer			*/
    187   1.2       leo 	MFP->mf_iera &= ~IA_TIMA;	/* Disable timer interrupts	*/
    188   1.2       leo 	MFP->mf_tadr  = divisor;	/* Set divisor			*/
    189   1.1       leo 
    190   1.5       leo 	if (hz != 48 && hz != 64 && hz != 96) { /* XXX */
    191  1.13  christos 		printf (": illegal value %d for systemclock, reset to %d\n\t",
    192   1.5       leo 								hz, 64);
    193   1.5       leo 		hz = 64;
    194   1.5       leo 	}
    195  1.13  christos 	printf(": system hz %d timer-A divisor 200/%d\n", hz, divisor);
    196   1.1       leo 
    197   1.5       leo #ifdef STATCLOCK
    198   1.5       leo 	if ((stathz == 0) || (stathz > hz) || (CLOCK_HZ % stathz))
    199   1.5       leo 		stathz = hz;
    200   1.5       leo 	if ((profhz == 0) || (profhz > (hz << 1)) || (CLOCK_HZ % profhz))
    201   1.5       leo 		profhz = hz << 1;
    202   1.5       leo 
    203   1.5       leo 	MFP->mf_tcdcr &= 0x7;			/* Stop timer		*/
    204   1.5       leo 	MFP->mf_ierb  &= ~IB_TIMC;		/* Disable timer inter.	*/
    205   1.5       leo 	MFP->mf_tcdr   = CLOCK_HZ/stathz;	/* Set divisor		*/
    206   1.5       leo 
    207   1.5       leo 	statmin  = (CLOCK_HZ/stathz) - (statvar >> 1);
    208   1.5       leo 	profmin  = (CLOCK_HZ/profhz) - (statvar >> 1);
    209   1.5       leo 	clk2min  = statmin;
    210   1.5       leo #endif /* STATCLOCK */
    211  1.14       leo 
    212   1.1       leo }
    213   1.1       leo 
    214   1.1       leo void cpu_initclocks()
    215   1.1       leo {
    216   1.3       leo 	MFP->mf_tacr  = T_Q200;		/* Start timer			*/
    217  1.20       leo 	MFP->mf_ipra  = (u_int8_t)~IA_TIMA;/* Clear pending interrupts	*/
    218   1.2       leo 	MFP->mf_iera |= IA_TIMA;	/* Enable timer interrupts	*/
    219   1.2       leo 	MFP->mf_imra |= IA_TIMA;	/*    .....			*/
    220   1.5       leo 
    221   1.5       leo #ifdef STATCLOCK
    222   1.5       leo 	MFP->mf_tcdcr = (MFP->mf_tcdcr & 0x7) | (T_Q200<<4); /* Start	*/
    223  1.20       leo 	MFP->mf_iprb  = (u_int8_t)~IB_TIMC;/* Clear pending interrupts	*/
    224   1.5       leo 	MFP->mf_ierb |= IB_TIMC;	/* Enable timer interrupts	*/
    225   1.5       leo 	MFP->mf_imrb |= IB_TIMC;	/*    .....			*/
    226   1.5       leo #endif /* STATCLOCK */
    227   1.1       leo }
    228   1.1       leo 
    229   1.9       leo void
    230   1.5       leo setstatclockrate(newhz)
    231   1.5       leo 	int newhz;
    232   1.1       leo {
    233   1.5       leo #ifdef STATCLOCK
    234   1.5       leo 	if (newhz == stathz)
    235   1.5       leo 		clk2min = statmin;
    236   1.5       leo 	else clk2min = profmin;
    237   1.5       leo #endif /* STATCLOCK */
    238   1.1       leo }
    239   1.1       leo 
    240   1.5       leo #ifdef STATCLOCK
    241   1.5       leo void
    242   1.5       leo statintr(frame)
    243  1.16       leo 	struct clockframe frame;
    244   1.5       leo {
    245   1.5       leo 	register int	var, r;
    246   1.5       leo 
    247   1.5       leo 	var = statvar - 1;
    248   1.5       leo 	do {
    249   1.5       leo 		r = random() & var;
    250   1.5       leo 	} while(r == 0);
    251   1.5       leo 
    252   1.5       leo 	/*
    253   1.5       leo 	 * Note that we are always lagging behind as the new divisor
    254   1.5       leo 	 * value will not be loaded until the next interrupt. This
    255   1.5       leo 	 * shouldn't disturb the median frequency (I think ;-) ) as
    256   1.5       leo 	 * only the value used when switching frequencies is used
    257   1.5       leo 	 * twice. This shouldn't happen very often.
    258   1.5       leo 	 */
    259   1.5       leo 	MFP->mf_tcdr = clk2min + r;
    260   1.5       leo 
    261  1.16       leo 	statclock(&frame);
    262   1.5       leo }
    263   1.5       leo #endif /* STATCLOCK */
    264   1.5       leo 
    265   1.1       leo /*
    266   1.1       leo  * Returns number of usec since last recorded clock "tick"
    267   1.1       leo  * (i.e. clock interrupt).
    268   1.1       leo  */
    269   1.9       leo long
    270   1.1       leo clkread()
    271   1.1       leo {
    272   1.3       leo 	u_int	delta;
    273  1.22       leo 	u_char	ipra, tadr;
    274   1.3       leo 
    275  1.22       leo 	/*
    276  1.22       leo 	 * Note: Order is important!
    277  1.22       leo 	 * By reading 'ipra' before 'tadr' and caching the data, I try to avoid
    278  1.22       leo 	 * the situation that very low value in 'tadr' is read (== a big delta)
    279  1.22       leo 	 * while also acccounting for a full 'tick' because the counter went
    280  1.22       leo 	 * through zero during the calculations.
    281  1.22       leo 	 */
    282  1.22       leo 	ipra = MFP->mf_ipra; tadr = MFP->mf_tadr;
    283  1.22       leo 
    284  1.22       leo 	delta = ((divisor - tadr) * tick) / divisor;
    285   1.1       leo 	/*
    286   1.1       leo 	 * Account for pending clock interrupts
    287   1.1       leo 	 */
    288  1.22       leo 	if(ipra & IA_TIMA)
    289   1.1       leo 		return(delta + tick);
    290   1.1       leo 	return(delta);
    291   1.1       leo }
    292   1.1       leo 
    293   1.2       leo #define TIMB_FREQ	614400
    294   1.2       leo #define TIMB_LIMIT	256
    295   1.1       leo 
    296   1.1       leo /*
    297   1.1       leo  * Wait "n" microseconds.
    298   1.2       leo  * Relies on MFP-Timer B counting down from TIMB_LIMIT at TIMB_FREQ Hz.
    299   1.1       leo  * Note: timer had better have been programmed before this is first used!
    300   1.1       leo  */
    301  1.14       leo void
    302  1.14       leo delay(n)
    303   1.1       leo int	n;
    304   1.1       leo {
    305   1.1       leo 	int	tick, otick;
    306   1.1       leo 
    307   1.1       leo 	/*
    308   1.1       leo 	 * Read the counter first, so that the rest of the setup overhead is
    309   1.1       leo 	 * counted.
    310   1.1       leo 	 */
    311   1.2       leo 	otick = MFP->mf_tbdr;
    312   1.1       leo 
    313   1.1       leo 	/*
    314   1.1       leo 	 * Calculate ((n * TIMER_FREQ) / 1e6) using explicit assembler code so
    315   1.1       leo 	 * we can take advantage of the intermediate 64-bit quantity to prevent
    316   1.1       leo 	 * loss of significance.
    317   1.1       leo 	 */
    318   1.1       leo 	n -= 5;
    319   1.1       leo 	if(n < 0)
    320   1.1       leo 		return;
    321   1.1       leo 	{
    322   1.1       leo 	    u_int	temp;
    323   1.1       leo 
    324   1.1       leo 	    __asm __volatile ("mulul %2,%1:%0" : "=d" (n), "=d" (temp)
    325  1.23       leo 					       : "d" (TIMB_FREQ), "d" (n));
    326   1.1       leo 	    __asm __volatile ("divul %1,%2:%0" : "=d" (n)
    327   1.1       leo 					       : "d"(1000000),"d"(temp),"0"(n));
    328   1.1       leo 	}
    329   1.1       leo 
    330   1.1       leo 	while(n > 0) {
    331   1.2       leo 		tick = MFP->mf_tbdr;
    332   1.1       leo 		if(tick > otick)
    333   1.2       leo 			n -= TIMB_LIMIT - (tick - otick);
    334   1.1       leo 		else n -= otick - tick;
    335   1.1       leo 		otick = tick;
    336   1.1       leo 	}
    337   1.1       leo }
    338   1.1       leo 
    339   1.4       leo #ifdef GPROF
    340   1.1       leo /*
    341   1.1       leo  * profclock() is expanded in line in lev6intr() unless profiling kernel.
    342   1.1       leo  * Assumes it is called with clock interrupts blocked.
    343   1.1       leo  */
    344   1.1       leo profclock(pc, ps)
    345   1.1       leo 	caddr_t pc;
    346   1.1       leo 	int ps;
    347   1.1       leo {
    348   1.1       leo 	/*
    349   1.1       leo 	 * Came from user mode.
    350   1.1       leo 	 * If this process is being profiled record the tick.
    351   1.1       leo 	 */
    352   1.1       leo 	if (USERMODE(ps)) {
    353   1.1       leo 		if (p->p_stats.p_prof.pr_scale)
    354   1.1       leo 			addupc(pc, &curproc->p_stats.p_prof, 1);
    355   1.1       leo 	}
    356   1.1       leo 	/*
    357   1.1       leo 	 * Came from kernel (supervisor) mode.
    358   1.1       leo 	 * If we are profiling the kernel, record the tick.
    359   1.1       leo 	 */
    360   1.1       leo 	else if (profiling < 2) {
    361   1.1       leo 		register int s = pc - s_lowpc;
    362   1.1       leo 
    363   1.1       leo 		if (s < s_textsize)
    364   1.1       leo 			kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
    365   1.1       leo 	}
    366   1.1       leo 	/*
    367   1.1       leo 	 * Kernel profiling was on but has been disabled.
    368   1.1       leo 	 * Mark as no longer profiling kernel and if all profiling done,
    369   1.1       leo 	 * disable the clock.
    370   1.1       leo 	 */
    371   1.1       leo 	if (profiling && (profon & PRF_KERNEL)) {
    372   1.1       leo 		profon &= ~PRF_KERNEL;
    373   1.1       leo 		if (profon == PRF_NONE)
    374   1.1       leo 			stopprofclock();
    375   1.1       leo 	}
    376   1.1       leo }
    377   1.1       leo #endif
    378   1.7       leo 
    379   1.7       leo /***********************************************************************
    380   1.7       leo  *                   Real Time Clock support                           *
    381   1.7       leo  ***********************************************************************/
    382   1.7       leo 
    383   1.7       leo u_int mc146818_read(rtc, regno)
    384   1.7       leo void	*rtc;
    385   1.7       leo u_int	regno;
    386   1.7       leo {
    387   1.7       leo 	((struct rtc *)rtc)->rtc_regno = regno;
    388   1.7       leo 	return(((struct rtc *)rtc)->rtc_data & 0377);
    389   1.7       leo }
    390   1.7       leo 
    391   1.7       leo void mc146818_write(rtc, regno, value)
    392   1.7       leo void	*rtc;
    393   1.7       leo u_int	regno, value;
    394   1.7       leo {
    395   1.7       leo 	((struct rtc *)rtc)->rtc_regno = regno;
    396   1.7       leo 	((struct rtc *)rtc)->rtc_data  = value;
    397   1.7       leo }
    398   1.1       leo 
    399   1.1       leo /*
    400  1.14       leo  * Initialize the time of day register, assuming the RTC runs in UTC.
    401  1.14       leo  * Since we've got the 'rtc' device, this functionality should be removed
    402  1.14       leo  * from the kernel. The only problem to be solved before that can happen
    403  1.14       leo  * is the possibility of init(1) providing a way (rc.boot?) to set
    404  1.14       leo  * the RTC before single-user mode is entered.
    405   1.1       leo  */
    406   1.9       leo void
    407   1.1       leo inittodr(base)
    408   1.1       leo time_t base;
    409   1.1       leo {
    410   1.1       leo 	/* Battery clock does not store usec's, so forget about it. */
    411  1.14       leo 	time.tv_sec  = gettod();
    412  1.11       leo 	time.tv_usec = 0;
    413   1.1       leo }
    414   1.1       leo 
    415  1.14       leo /*
    416  1.14       leo  * Function turned into a No-op. Use /dev/rtc to update the RTC.
    417  1.14       leo  */
    418   1.9       leo void
    419   1.1       leo resettodr()
    420   1.1       leo {
    421  1.14       leo 	return;
    422   1.1       leo }
    423   1.1       leo 
    424   1.1       leo static u_long
    425   1.1       leo gettod()
    426   1.1       leo {
    427  1.18       leo 	int			sps;
    428  1.18       leo 	mc_todregs		clkregs;
    429  1.25       leo 	u_int			regb;
    430  1.18       leo 	struct clock_ymdhms	dt;
    431   1.3       leo 
    432   1.3       leo 	sps = splhigh();
    433  1.25       leo 	regb = mc146818_read(RTC, MC_REGB);
    434   1.3       leo 	MC146818_GETTOD(RTC, &clkregs);
    435   1.3       leo 	splx(sps);
    436   1.1       leo 
    437  1.25       leo 	regb &= MC_REGB_24HR|MC_REGB_BINARY;
    438  1.25       leo 	if (regb != (MC_REGB_24HR|MC_REGB_BINARY)) {
    439  1.25       leo 		printf("Error: Nonstandard RealTimeClock Configuration -"
    440  1.25       leo 			" value ignored\n"
    441  1.25       leo 			"       A write to /dev/rtc will correct this.\n");
    442  1.25       leo 			return(0);
    443  1.25       leo 	}
    444   1.9       leo 	if(clkregs[MC_SEC] > 59)
    445   1.8       leo 		return(0);
    446   1.9       leo 	if(clkregs[MC_MIN] > 59)
    447   1.8       leo 		return(0);
    448   1.9       leo 	if(clkregs[MC_HOUR] > 23)
    449   1.1       leo 		return(0);
    450   1.3       leo 	if(range_test(clkregs[MC_DOM], 1, 31))
    451   1.1       leo 		return(0);
    452   1.3       leo 	if (range_test(clkregs[MC_MONTH], 1, 12))
    453   1.1       leo 		return(0);
    454  1.24       leo 	if(clkregs[MC_YEAR] > 99)
    455   1.1       leo 		return(0);
    456   1.1       leo 
    457  1.18       leo 	dt.dt_year = clkregs[MC_YEAR] + GEMSTARTOFTIME;
    458  1.18       leo 	dt.dt_mon  = clkregs[MC_MONTH];
    459  1.18       leo 	dt.dt_day  = clkregs[MC_DOM];
    460  1.18       leo 	dt.dt_hour = clkregs[MC_HOUR];
    461  1.18       leo 	dt.dt_min  = clkregs[MC_MIN];
    462  1.18       leo 	dt.dt_sec  = clkregs[MC_SEC];
    463   1.1       leo 
    464  1.18       leo 	return(clock_ymdhms_to_secs(&dt));
    465   1.1       leo }
    466  1.14       leo /***********************************************************************
    467  1.14       leo  *                   RTC-device support				       *
    468  1.14       leo  ***********************************************************************/
    469  1.14       leo int
    470  1.14       leo rtcopen(dev, flag, mode, p)
    471  1.14       leo 	dev_t		dev;
    472  1.14       leo 	int		flag, mode;
    473  1.14       leo 	struct proc	*p;
    474  1.14       leo {
    475  1.14       leo 	int			unit = minor(dev);
    476  1.14       leo 	struct clock_softc	*sc;
    477  1.14       leo 
    478  1.14       leo 	if (unit >= clock_cd.cd_ndevs)
    479  1.14       leo 		return ENXIO;
    480  1.14       leo 	sc = clock_cd.cd_devs[unit];
    481  1.14       leo 	if (!sc)
    482  1.14       leo 		return ENXIO;
    483  1.14       leo 	if (sc->sc_flags & RTC_OPEN)
    484  1.14       leo 		return EBUSY;
    485  1.14       leo 
    486  1.14       leo 	sc->sc_flags = RTC_OPEN;
    487  1.14       leo 	return 0;
    488  1.14       leo }
    489   1.1       leo 
    490  1.14       leo int
    491  1.14       leo rtcclose(dev, flag, mode, p)
    492  1.14       leo 	dev_t		dev;
    493  1.14       leo 	int		flag;
    494  1.14       leo 	int		mode;
    495  1.14       leo 	struct proc	*p;
    496   1.1       leo {
    497  1.14       leo 	int			unit = minor(dev);
    498  1.14       leo 	struct clock_softc	*sc = clock_cd.cd_devs[unit];
    499  1.14       leo 
    500  1.14       leo 	sc->sc_flags = 0;
    501  1.14       leo 	return 0;
    502  1.14       leo }
    503  1.14       leo 
    504  1.14       leo int
    505  1.14       leo rtcread(dev, uio, flags)
    506  1.14       leo 	dev_t		dev;
    507  1.14       leo 	struct uio	*uio;
    508  1.14       leo 	int		flags;
    509  1.14       leo {
    510  1.14       leo 	struct clock_softc	*sc;
    511  1.14       leo 	mc_todregs		clkregs;
    512  1.14       leo 	int			s, length;
    513  1.14       leo 	char			buffer[16];
    514  1.14       leo 
    515  1.14       leo 	sc = clock_cd.cd_devs[minor(dev)];
    516  1.14       leo 
    517  1.14       leo 	s = splhigh();
    518  1.14       leo 	MC146818_GETTOD(RTC, &clkregs);
    519  1.14       leo 	splx(s);
    520  1.14       leo 
    521  1.21       leo 	sprintf(buffer, "%4d%02d%02d%02d%02d.%02d\n",
    522  1.21       leo 	    clkregs[MC_YEAR] + GEMSTARTOFTIME,
    523  1.14       leo 	    clkregs[MC_MONTH], clkregs[MC_DOM],
    524  1.14       leo 	    clkregs[MC_HOUR], clkregs[MC_MIN], clkregs[MC_SEC]);
    525  1.14       leo 
    526  1.14       leo 	if (uio->uio_offset > strlen(buffer))
    527  1.14       leo 		return 0;
    528   1.1       leo 
    529  1.14       leo 	length = strlen(buffer) - uio->uio_offset;
    530  1.14       leo 	if (length > uio->uio_resid)
    531  1.14       leo 		length = uio->uio_resid;
    532   1.1       leo 
    533  1.14       leo 	return(uiomove((caddr_t)buffer, length, uio));
    534  1.14       leo }
    535  1.14       leo 
    536  1.14       leo static int
    537  1.14       leo twodigits(buffer, pos)
    538  1.14       leo 	char *buffer;
    539  1.14       leo 	int pos;
    540  1.14       leo {
    541  1.14       leo 	int result = 0;
    542  1.14       leo 
    543  1.14       leo 	if (buffer[pos] >= '0' && buffer[pos] <= '9')
    544  1.14       leo 		result = (buffer[pos] - '0') * 10;
    545  1.14       leo 	if (buffer[pos+1] >= '0' && buffer[pos+1] <= '9')
    546  1.14       leo 		result += (buffer[pos+1] - '0');
    547  1.14       leo 	return(result);
    548  1.14       leo }
    549   1.1       leo 
    550  1.14       leo int
    551  1.14       leo rtcwrite(dev, uio, flags)
    552  1.14       leo 	dev_t		dev;
    553  1.14       leo 	struct uio	*uio;
    554  1.14       leo 	int		flags;
    555  1.14       leo {
    556  1.14       leo 	mc_todregs		clkregs;
    557  1.14       leo 	int			s, length, error;
    558  1.21       leo 	char			buffer[16];
    559  1.14       leo 
    560  1.14       leo 	/*
    561  1.14       leo 	 * We require atomic updates!
    562  1.14       leo 	 */
    563  1.14       leo 	length = uio->uio_resid;
    564  1.14       leo 	if (uio->uio_offset || (length != sizeof(buffer)
    565  1.14       leo 	  && length != sizeof(buffer - 1)))
    566  1.14       leo 		return(EINVAL);
    567  1.14       leo 
    568  1.14       leo 	if ((error = uiomove((caddr_t)buffer, sizeof(buffer), uio)))
    569  1.14       leo 		return(error);
    570   1.1       leo 
    571  1.14       leo 	if (length == sizeof(buffer) && buffer[sizeof(buffer) - 1] != '\n')
    572  1.14       leo 		return(EINVAL);
    573   1.1       leo 
    574  1.14       leo 	s = splclock();
    575  1.25       leo 	mc146818_write(RTC, MC_REGB,
    576  1.25       leo 		mc146818_read(RTC, MC_REGB) | MC_REGB_24HR | MC_REGB_BINARY);
    577   1.3       leo 	MC146818_GETTOD(RTC, &clkregs);
    578  1.14       leo 	splx(s);
    579  1.14       leo 
    580  1.21       leo 	clkregs[MC_SEC]   = twodigits(buffer, 13);
    581  1.21       leo 	clkregs[MC_MIN]   = twodigits(buffer, 10);
    582  1.21       leo 	clkregs[MC_HOUR]  = twodigits(buffer, 8);
    583  1.21       leo 	clkregs[MC_DOM]   = twodigits(buffer, 6);
    584  1.21       leo 	clkregs[MC_MONTH] = twodigits(buffer, 4);
    585  1.21       leo 	s = twodigits(buffer, 0) * 100 + twodigits(buffer, 2);
    586  1.14       leo 	clkregs[MC_YEAR]  = s - GEMSTARTOFTIME;
    587  1.14       leo 
    588  1.14       leo 	s = splclock();
    589   1.3       leo 	MC146818_PUTTOD(RTC, &clkregs);
    590  1.14       leo 	splx(s);
    591   1.1       leo 
    592  1.14       leo 	return(0);
    593   1.1       leo }
    594