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