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