Home | History | Annotate | Line # | Download | only in iomd
iomd_clock.c revision 1.17.8.2
      1  1.17.8.2     yamt /*	$NetBSD: iomd_clock.c,v 1.17.8.2 2006/08/11 15:41:11 yamt Exp $	*/
      2       1.1  reinoud 
      3       1.1  reinoud /*
      4       1.1  reinoud  * Copyright (c) 1994-1997 Mark Brinicombe.
      5       1.1  reinoud  * Copyright (c) 1994 Brini.
      6       1.1  reinoud  * All rights reserved.
      7       1.1  reinoud  *
      8       1.1  reinoud  * This code is derived from software written for Brini by Mark Brinicombe
      9       1.1  reinoud  *
     10       1.1  reinoud  * Redistribution and use in source and binary forms, with or without
     11       1.1  reinoud  * modification, are permitted provided that the following conditions
     12       1.1  reinoud  * are met:
     13       1.1  reinoud  * 1. Redistributions of source code must retain the above copyright
     14       1.1  reinoud  *    notice, this list of conditions and the following disclaimer.
     15       1.1  reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  reinoud  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  reinoud  *    documentation and/or other materials provided with the distribution.
     18       1.1  reinoud  * 3. All advertising materials mentioning features or use of this software
     19       1.1  reinoud  *    must display the following acknowledgement:
     20       1.1  reinoud  *	This product includes software developed by Mark Brinicombe.
     21       1.1  reinoud  * 4. The name of the company nor the name of the author may be used to
     22       1.1  reinoud  *    endorse or promote products derived from this software without specific
     23       1.1  reinoud  *    prior written permission.
     24       1.1  reinoud  *
     25       1.1  reinoud  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     26       1.1  reinoud  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     27       1.1  reinoud  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     28       1.1  reinoud  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     29       1.1  reinoud  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     30       1.1  reinoud  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     31       1.1  reinoud  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32       1.1  reinoud  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33       1.1  reinoud  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34       1.1  reinoud  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35       1.1  reinoud  * SUCH DAMAGE.
     36       1.1  reinoud  *
     37       1.1  reinoud  * RiscBSD kernel project
     38       1.1  reinoud  *
     39       1.1  reinoud  * clock.c
     40       1.1  reinoud  *
     41       1.1  reinoud  * Timer related machine specific code
     42       1.1  reinoud  *
     43       1.1  reinoud  * Created      : 29/09/94
     44       1.1  reinoud  */
     45       1.1  reinoud 
     46       1.1  reinoud /* Include header files */
     47       1.1  reinoud 
     48       1.1  reinoud #include <sys/param.h>
     49       1.5    bjh21 
     50  1.17.8.2     yamt __KERNEL_RCSID(0, "$NetBSD: iomd_clock.c,v 1.17.8.2 2006/08/11 15:41:11 yamt Exp $");
     51       1.5    bjh21 
     52       1.1  reinoud #include <sys/systm.h>
     53       1.1  reinoud #include <sys/kernel.h>
     54       1.1  reinoud #include <sys/time.h>
     55  1.17.8.2     yamt #include <sys/timetc.h>
     56       1.1  reinoud #include <sys/device.h>
     57  1.17.8.2     yamt #include <sys/lock.h>
     58       1.1  reinoud 
     59      1.12  thorpej #include <dev/clock_subr.h>
     60      1.12  thorpej 
     61       1.4  thorpej #include <machine/intr.h>
     62       1.3  thorpej 
     63       1.3  thorpej #include <arm/cpufunc.h>
     64       1.3  thorpej 
     65       1.1  reinoud #include <arm/iomd/iomdvar.h>
     66       1.1  reinoud #include <arm/iomd/iomdreg.h>
     67       1.1  reinoud 
     68       1.1  reinoud struct clock_softc {
     69       1.1  reinoud 	struct device 		sc_dev;
     70       1.1  reinoud 	bus_space_tag_t		sc_iot;
     71       1.1  reinoud 	bus_space_handle_t	sc_ioh;
     72       1.1  reinoud };
     73       1.1  reinoud 
     74       1.1  reinoud #define TIMER_FREQUENCY 2000000		/* 2MHz clock */
     75       1.1  reinoud #define TICKS_PER_MICROSECOND (TIMER_FREQUENCY / 1000000)
     76       1.1  reinoud 
     77       1.1  reinoud static void *clockirq;
     78       1.1  reinoud static void *statclockirq;
     79       1.1  reinoud static struct clock_softc *clock_sc;
     80       1.1  reinoud static int timer0_count;
     81  1.17.8.2     yamt static int timeset;
     82       1.1  reinoud 
     83  1.17.8.2     yamt static int clockmatch(struct device *parent, struct cfdata *cf, void *aux);
     84  1.17.8.2     yamt static void clockattach(struct device *parent, struct device *self, void *aux);
     85       1.1  reinoud #ifdef DIAGNOSTIC
     86  1.17.8.2     yamt static void checkdelay(void);
     87       1.1  reinoud #endif
     88       1.1  reinoud 
     89  1.17.8.2     yamt static u_int iomd_timecounter0_get(struct timecounter *tc);
     90  1.17.8.2     yamt 
     91  1.17.8.2     yamt 
     92  1.17.8.2     yamt static volatile uint32_t timer0_lastcount;
     93  1.17.8.2     yamt static volatile uint32_t timer0_offset;
     94  1.17.8.2     yamt static volatile int timer0_ticked;
     95  1.17.8.2     yamt /* TODO: Get IRQ status */
     96  1.17.8.2     yamt 
     97  1.17.8.2     yamt static struct simplelock tmr_lock = SIMPLELOCK_INITIALIZER;  /* protect TC timer variables */
     98  1.17.8.2     yamt 
     99  1.17.8.2     yamt 
    100  1.17.8.2     yamt static struct timecounter iomd_timecounter = {
    101  1.17.8.2     yamt 	iomd_timecounter0_get,
    102  1.17.8.2     yamt 	0, /* No poll_pps */
    103  1.17.8.2     yamt 	~0, /* 32bit accuracy */
    104  1.17.8.2     yamt 	TIMER_FREQUENCY,
    105  1.17.8.2     yamt 	"iomd_timer0",
    106  1.17.8.2     yamt 	100
    107  1.17.8.2     yamt };
    108  1.17.8.2     yamt 
    109  1.17.8.2     yamt int clockhandler(void *);
    110  1.17.8.2     yamt int statclockhandler(void *);
    111       1.5    bjh21 
    112       1.9  thorpej CFATTACH_DECL(clock, sizeof(struct clock_softc),
    113      1.10  thorpej     clockmatch, clockattach, NULL, NULL);
    114       1.1  reinoud 
    115       1.1  reinoud /*
    116       1.1  reinoud  * int clockmatch(struct device *parent, void *match, void *aux)
    117       1.1  reinoud  *
    118       1.1  reinoud  * Just return ok for this if it is device 0
    119       1.1  reinoud  */
    120       1.1  reinoud 
    121       1.1  reinoud static int
    122  1.17.8.2     yamt clockmatch(struct device *parent, struct cfdata *cf, void *aux)
    123       1.1  reinoud {
    124       1.1  reinoud 	struct clk_attach_args *ca = aux;
    125       1.1  reinoud 
    126       1.1  reinoud 	if (strcmp(ca->ca_name, "clk") == 0)
    127       1.1  reinoud 		return(1);
    128       1.1  reinoud 	return(0);
    129       1.1  reinoud }
    130       1.1  reinoud 
    131       1.1  reinoud 
    132       1.1  reinoud /*
    133       1.1  reinoud  * void clockattach(struct device *parent, struct device *dev, void *aux)
    134       1.1  reinoud  *
    135       1.1  reinoud  * Map the IOMD and identify it.
    136       1.1  reinoud  * Then configure the child devices based on the IOMD ID.
    137       1.1  reinoud  */
    138       1.1  reinoud 
    139       1.1  reinoud static void
    140  1.17.8.2     yamt clockattach(struct device *parent, struct device *self,	void *aux)
    141       1.1  reinoud {
    142       1.1  reinoud 	struct clock_softc *sc = (struct clock_softc *)self;
    143       1.1  reinoud 	struct clk_attach_args *ca = aux;
    144       1.1  reinoud 
    145       1.1  reinoud 	sc->sc_iot = ca->ca_iot;
    146       1.1  reinoud 	sc->sc_ioh = ca->ca_ioh; /* This is a handle for the whole IOMD */
    147       1.1  reinoud 
    148       1.1  reinoud 	clock_sc = sc;
    149       1.1  reinoud 
    150       1.1  reinoud 	/* Cannot do anything until cpu_initclocks() has been called */
    151       1.1  reinoud 
    152       1.1  reinoud 	printf("\n");
    153       1.1  reinoud }
    154       1.1  reinoud 
    155       1.1  reinoud 
    156  1.17.8.2     yamt static void
    157  1.17.8.2     yamt tickle_tc(void)
    158  1.17.8.2     yamt {
    159  1.17.8.2     yamt 	if (timer0_count &&
    160  1.17.8.2     yamt 	    timecounter->tc_get_timecount == iomd_timecounter0_get) {
    161  1.17.8.2     yamt 		simple_lock(&tmr_lock);
    162  1.17.8.2     yamt 		if (timer0_ticked)
    163  1.17.8.2     yamt 			timer0_ticked    = 0;
    164  1.17.8.2     yamt 		else {
    165  1.17.8.2     yamt 			timer0_offset   += timer0_count;
    166  1.17.8.2     yamt 			timer0_lastcount = 0;
    167  1.17.8.2     yamt 		}
    168  1.17.8.2     yamt 		simple_unlock(&tmr_lock);
    169  1.17.8.2     yamt 	}
    170  1.17.8.2     yamt 
    171  1.17.8.2     yamt }
    172  1.17.8.2     yamt 
    173  1.17.8.2     yamt 
    174       1.1  reinoud /*
    175       1.1  reinoud  * int clockhandler(struct clockframe *frame)
    176       1.1  reinoud  *
    177       1.1  reinoud  * Function called by timer 0 interrupts. This just calls
    178       1.1  reinoud  * hardclock(). Eventually the irqhandler can call hardclock() directly
    179       1.1  reinoud  * but for now we use this function so that we can debug IRQ's
    180       1.1  reinoud  */
    181       1.1  reinoud 
    182       1.1  reinoud int
    183  1.17.8.2     yamt clockhandler(void *cookie)
    184       1.1  reinoud {
    185       1.5    bjh21 	struct clockframe *frame = cookie;
    186  1.17.8.2     yamt 	tickle_tc();
    187       1.5    bjh21 
    188       1.1  reinoud 	hardclock(frame);
    189  1.17.8.2     yamt 	return 0;	/* Pass the interrupt on down the chain */
    190       1.1  reinoud }
    191       1.1  reinoud 
    192       1.1  reinoud 
    193       1.1  reinoud /*
    194       1.1  reinoud  * int statclockhandler(struct clockframe *frame)
    195       1.1  reinoud  *
    196       1.1  reinoud  * Function called by timer 1 interrupts. This just calls
    197       1.1  reinoud  * statclock(). Eventually the irqhandler can call statclock() directly
    198       1.1  reinoud  * but for now we use this function so that we can debug IRQ's
    199       1.1  reinoud  */
    200       1.1  reinoud 
    201       1.1  reinoud int
    202  1.17.8.2     yamt statclockhandler(void *cookie)
    203       1.1  reinoud {
    204       1.5    bjh21 	struct clockframe *frame = cookie;
    205       1.5    bjh21 
    206       1.1  reinoud 	statclock(frame);
    207  1.17.8.2     yamt 	return 0;	/* Pass the interrupt on down the chain */
    208       1.1  reinoud }
    209       1.1  reinoud 
    210       1.1  reinoud 
    211       1.1  reinoud /*
    212      1.15       he  * void setstatclockrate(int newhz)
    213       1.1  reinoud  *
    214       1.1  reinoud  * Set the stat clock rate. The stat clock uses timer1
    215       1.1  reinoud  */
    216       1.1  reinoud 
    217       1.1  reinoud void
    218      1.14    chris setstatclockrate(int newhz)
    219       1.1  reinoud {
    220       1.1  reinoud 	int count;
    221       1.1  reinoud 
    222      1.14    chris 	count = TIMER_FREQUENCY / newhz;
    223       1.1  reinoud 
    224      1.14    chris 	printf("Setting statclock to %dHz (%d ticks)\n", newhz, count);
    225       1.1  reinoud 
    226       1.1  reinoud 	bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    227       1.1  reinoud 	    IOMD_T1LOW, (count >> 0) & 0xff);
    228       1.1  reinoud 	bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    229       1.1  reinoud 	    IOMD_T1HIGH, (count >> 8) & 0xff);
    230       1.1  reinoud 
    231       1.1  reinoud 	/* reload the counter */
    232       1.1  reinoud 
    233       1.1  reinoud 	bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    234       1.1  reinoud 	    IOMD_T1GO, 0);
    235       1.1  reinoud }
    236       1.1  reinoud 
    237       1.1  reinoud 
    238       1.1  reinoud #ifdef DIAGNOSTIC
    239       1.1  reinoud static void
    240  1.17.8.2     yamt checkdelay(void)
    241       1.1  reinoud {
    242       1.1  reinoud 	struct timeval start, end, diff;
    243       1.1  reinoud 
    244       1.1  reinoud 	microtime(&start);
    245       1.1  reinoud 	delay(10000);
    246       1.1  reinoud 	microtime(&end);
    247       1.1  reinoud 	timersub(&end, &start, &diff);
    248       1.1  reinoud 	if (diff.tv_sec > 0)
    249       1.1  reinoud 		return;
    250       1.1  reinoud 	if (diff.tv_usec > 10000)
    251       1.1  reinoud 		return;
    252       1.1  reinoud 	printf("WARNING: delay(10000) took %ld us\n", diff.tv_usec);
    253       1.1  reinoud }
    254       1.1  reinoud #endif
    255       1.1  reinoud 
    256       1.1  reinoud /*
    257       1.1  reinoud  * void cpu_initclocks(void)
    258       1.1  reinoud  *
    259       1.1  reinoud  * Initialise the clocks.
    260       1.1  reinoud  * This sets up the two timers in the IOMD and installs the IRQ handlers
    261       1.1  reinoud  *
    262       1.1  reinoud  * NOTE: Currently only timer 0 is setup and the IRQ handler is not installed
    263       1.1  reinoud  */
    264       1.1  reinoud 
    265       1.1  reinoud void
    266  1.17.8.2     yamt cpu_initclocks(void)
    267       1.1  reinoud {
    268       1.1  reinoud 	/*
    269       1.1  reinoud 	 * Load timer 0 with count down value
    270       1.1  reinoud 	 * This timer generates 100Hz interrupts for the system clock
    271       1.1  reinoud 	 */
    272       1.1  reinoud 
    273       1.1  reinoud 	printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
    274       1.1  reinoud 
    275       1.1  reinoud 	timer0_count = TIMER_FREQUENCY / hz;
    276       1.1  reinoud 
    277       1.1  reinoud 	bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    278       1.1  reinoud 	    IOMD_T0LOW, (timer0_count >> 0) & 0xff);
    279       1.1  reinoud 	bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    280       1.1  reinoud 	    IOMD_T0HIGH, (timer0_count >> 8) & 0xff);
    281       1.1  reinoud 
    282       1.1  reinoud 	/* reload the counter */
    283       1.1  reinoud 
    284       1.1  reinoud 	bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    285       1.1  reinoud 	    IOMD_T0GO, 0);
    286       1.1  reinoud 
    287       1.1  reinoud 	clockirq = intr_claim(IRQ_TIMER0, IPL_CLOCK, "tmr0 hard clk",
    288       1.1  reinoud 	    clockhandler, 0);
    289       1.1  reinoud 
    290       1.1  reinoud 	if (clockirq == NULL)
    291       1.7   provos 		panic("%s: Cannot installer timer 0 IRQ handler",
    292       1.1  reinoud 		    clock_sc->sc_dev.dv_xname);
    293       1.1  reinoud 
    294       1.1  reinoud 	if (stathz) {
    295       1.1  reinoud 		setstatclockrate(stathz);
    296       1.1  reinoud        		statclockirq = intr_claim(IRQ_TIMER1, IPL_CLOCK,
    297       1.1  reinoud        		    "tmr1 stat clk", statclockhandler, 0);
    298       1.1  reinoud 		if (statclockirq == NULL)
    299       1.7   provos 			panic("%s: Cannot installer timer 1 IRQ handler",
    300       1.1  reinoud 			    clock_sc->sc_dev.dv_xname);
    301       1.1  reinoud 	}
    302       1.1  reinoud #ifdef DIAGNOSTIC
    303       1.1  reinoud 	checkdelay();
    304       1.1  reinoud #endif
    305  1.17.8.2     yamt 	tc_init(&iomd_timecounter);
    306       1.1  reinoud }
    307       1.1  reinoud 
    308       1.1  reinoud 
    309       1.1  reinoud 
    310  1.17.8.2     yamt static u_int iomd_timecounter0_get(struct timecounter *tc)
    311       1.1  reinoud {
    312       1.1  reinoud 	int s;
    313  1.17.8.2     yamt 	u_int tm;
    314       1.1  reinoud 
    315       1.1  reinoud 	/*
    316       1.1  reinoud 	 * Latch the current value of the timer and then read it.
    317       1.1  reinoud 	 * This garentees an atmoic reading of the time.
    318       1.1  reinoud 	 */
    319  1.17.8.2     yamt 	s = splhigh();
    320       1.1  reinoud 	bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    321       1.1  reinoud 	    IOMD_T0LATCH, 0);
    322       1.1  reinoud 
    323       1.1  reinoud 	tm = bus_space_read_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    324       1.1  reinoud 	    IOMD_T0LOW);
    325       1.1  reinoud 	tm += (bus_space_read_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    326       1.1  reinoud 	    IOMD_T0HIGH) << 8);
    327  1.17.8.2     yamt 	splx(s);
    328  1.17.8.2     yamt 	simple_lock(&tmr_lock);
    329       1.1  reinoud 
    330  1.17.8.2     yamt 	tm = timer0_count - tm;
    331  1.17.8.2     yamt 
    332       1.1  reinoud 
    333  1.17.8.2     yamt 	if (timer0_count &&
    334  1.17.8.2     yamt 	    (tm < timer0_lastcount || (!timer0_ticked && FALSE/* XXX: clkintr_pending */))) {
    335  1.17.8.2     yamt 		timer0_ticked = 1;
    336  1.17.8.2     yamt 		timer0_offset += timer0_count;
    337       1.1  reinoud 	}
    338  1.17.8.2     yamt 
    339  1.17.8.2     yamt 	timer0_lastcount = tm;
    340  1.17.8.2     yamt 	tm += timer0_offset;
    341  1.17.8.2     yamt 
    342  1.17.8.2     yamt 	simple_unlock(&tmr_lock);
    343  1.17.8.2     yamt 	return tm;
    344       1.1  reinoud }
    345       1.1  reinoud 
    346  1.17.8.2     yamt 
    347  1.17.8.2     yamt 
    348       1.1  reinoud /*
    349       1.1  reinoud  * Estimated loop for n microseconds
    350       1.1  reinoud  */
    351       1.1  reinoud 
    352       1.1  reinoud /* Need to re-write this to use the timers */
    353       1.1  reinoud 
    354       1.1  reinoud /* One day soon I will actually do this */
    355       1.1  reinoud 
    356       1.1  reinoud int delaycount = 100;
    357       1.1  reinoud 
    358       1.1  reinoud void
    359  1.17.8.2     yamt delay(u_int n)
    360       1.1  reinoud {
    361  1.17.8.1     yamt 	volatile u_int n2;
    362  1.17.8.1     yamt 	volatile u_int i;
    363       1.1  reinoud 
    364       1.1  reinoud 	if (n == 0) return;
    365  1.17.8.1     yamt 	n2 = n;
    366  1.17.8.1     yamt 	while (n2-- > 0) {
    367       1.1  reinoud 		if (cputype == CPU_ID_SA110)	/* XXX - Seriously gross hack */
    368       1.1  reinoud 			for (i = delaycount; --i;);
    369       1.1  reinoud 		else
    370       1.1  reinoud 			for (i = 8; --i;);
    371       1.1  reinoud 	}
    372      1.12  thorpej }
    373      1.12  thorpej 
    374      1.12  thorpej todr_chip_handle_t todr_handle;
    375      1.12  thorpej 
    376      1.12  thorpej /*
    377      1.12  thorpej  * todr_attach:
    378      1.12  thorpej  *
    379      1.12  thorpej  *	Set the specified time-of-day register as the system real-time clock.
    380      1.12  thorpej  */
    381      1.12  thorpej void
    382      1.12  thorpej todr_attach(todr_chip_handle_t todr)
    383      1.12  thorpej {
    384      1.12  thorpej 
    385      1.12  thorpej 	if (todr_handle)
    386      1.12  thorpej 		panic("todr_attach: rtc already configured");
    387      1.12  thorpej 	todr_handle = todr;
    388      1.12  thorpej }
    389      1.12  thorpej 
    390      1.12  thorpej /*
    391      1.12  thorpej  * inittodr:
    392      1.12  thorpej  *
    393      1.12  thorpej  *	Initialize time from the time-of-day register.
    394      1.12  thorpej  */
    395      1.12  thorpej #define	MINYEAR		2003	/* minimum plausible year */
    396      1.12  thorpej void
    397      1.12  thorpej inittodr(time_t base)
    398      1.12  thorpej {
    399      1.12  thorpej 	time_t deltat;
    400      1.12  thorpej 	int badbase;
    401  1.17.8.2     yamt 	int badtime;
    402  1.17.8.2     yamt 	struct timeval time;
    403  1.17.8.2     yamt 	struct timespec tc_time; /* for timecounters */
    404  1.17.8.2     yamt 
    405  1.17.8.2     yamt 	/* Default is to assume that time from somewhere will be okay.
    406  1.17.8.2     yamt 	 * If not badtime will be set to 1 */
    407  1.17.8.2     yamt 	badtime = 0;
    408      1.12  thorpej 	if (base < (MINYEAR - 1970) * SECYR) {
    409      1.12  thorpej 		printf("WARNING: preposterous time in file system");
    410      1.12  thorpej 		/* read the system clock anyway */
    411      1.12  thorpej 		base = (MINYEAR - 1970) * SECYR;
    412      1.12  thorpej 		badbase = 1;
    413      1.12  thorpej 	} else
    414      1.12  thorpej 		badbase = 0;
    415      1.12  thorpej 
    416      1.12  thorpej 	if (todr_handle == NULL ||
    417      1.15       he 	    todr_gettime(todr_handle, &time) != 0 ||
    418      1.15       he 	    time.tv_sec == 0) {
    419      1.12  thorpej 		/*
    420      1.12  thorpej 		 * Believe the time in the file system for lack of
    421      1.12  thorpej 		 * anything better, resetting the TODR.
    422      1.12  thorpej 		 */
    423      1.12  thorpej 		time.tv_sec = base;
    424      1.12  thorpej 		time.tv_usec = 0;
    425      1.12  thorpej 		if (todr_handle != NULL && !badbase) {
    426      1.12  thorpej 			printf("WARNING: preposterous clock chip time\n");
    427      1.12  thorpej 			resettodr();
    428      1.12  thorpej 		}
    429  1.17.8.2     yamt 		badtime = 1;
    430      1.12  thorpej 	}
    431      1.12  thorpej 
    432      1.12  thorpej 	if (!badbase) {
    433      1.12  thorpej 		/*
    434      1.13   simonb 		 * See if we gained/lost two or more days; if
    435      1.12  thorpej 		 * so, assume something is amiss.
    436      1.12  thorpej 		 */
    437      1.15       he 		deltat = time.tv_sec - base;
    438      1.12  thorpej 		if (deltat < 0)
    439      1.12  thorpej 			deltat = -deltat;
    440  1.17.8.2     yamt 		if (deltat >= 2 * SECDAY)
    441      1.12  thorpej 		printf("WARNING: clock %s %ld days\n",
    442  1.17.8.2     yamt 		       time.tv_sec < base ? "lost" : "gained",
    443  1.17.8.2     yamt 		       (long)deltat / SECDAY);
    444      1.12  thorpej 	}
    445  1.17.8.2     yamt 	/* At this point time is a time value we can initialise the system
    446  1.17.8.2     yamt 	 * clock with */
    447  1.17.8.2     yamt 	tc_time.tv_sec = time.tv_sec;
    448  1.17.8.2     yamt 	tc_time.tv_nsec = time.tv_usec * 1000;
    449  1.17.8.2     yamt 	tc_setclock(&tc_time);
    450  1.17.8.2     yamt 	timeset = 1;
    451  1.17.8.2     yamt 
    452  1.17.8.2     yamt 	if (badtime)
    453  1.17.8.2     yamt 		printf("WARNING: CHECK AND RESET THE DATE!\n");
    454      1.12  thorpej }
    455      1.12  thorpej 
    456      1.12  thorpej /*
    457      1.12  thorpej  * resettodr:
    458      1.12  thorpej  *
    459      1.12  thorpej  *	Reset the time-of-day register with the current time.
    460      1.12  thorpej  */
    461      1.12  thorpej void
    462      1.12  thorpej resettodr(void)
    463      1.12  thorpej {
    464  1.17.8.2     yamt 	struct timeval time;
    465  1.17.8.2     yamt 	if (!timeset)
    466      1.12  thorpej 		return;
    467      1.12  thorpej 
    468  1.17.8.2     yamt 	getmicrotime(&time);
    469      1.12  thorpej 	if (todr_handle != NULL &&
    470      1.15       he 	    todr_settime(todr_handle, &time) != 0)
    471      1.12  thorpej 		printf("resettodr: failed to set time\n");
    472       1.1  reinoud }
    473       1.1  reinoud 
    474       1.1  reinoud /* End of iomd_clock.c */
    475