Home | History | Annotate | Line # | Download | only in ep93xx
epclk.c revision 1.6
      1  1.6      he /*	$NetBSD: epclk.c,v 1.6 2005/06/04 22:37:51 he Exp $	*/
      2  1.1    joff 
      3  1.1    joff /*
      4  1.1    joff  * Copyright (c) 2004 Jesse Off
      5  1.1    joff  * All rights reserved.
      6  1.1    joff  *
      7  1.1    joff  * Redistribution and use in source and binary forms, with or without
      8  1.1    joff  * modification, are permitted provided that the following conditions
      9  1.1    joff  * are met:
     10  1.1    joff  * 1. Redistributions of source code must retain the above copyright
     11  1.1    joff  *    notice, this list of conditions and the following disclaimer.
     12  1.1    joff  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1    joff  *    notice, this list of conditions and the following disclaimer in the
     14  1.1    joff  *    documentation and/or other materials provided with the distribution.
     15  1.1    joff  * 3. All advertising materials mentioning features or use of this software
     16  1.1    joff  *    must display the following acknowledgement:
     17  1.1    joff  *	This product includes software developed by the NetBSD
     18  1.1    joff  *	Foundation, Inc. and its contributors.
     19  1.1    joff  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  1.1    joff  *    contributors may be used to endorse or promote products derived
     21  1.1    joff  *    from this software without specific prior written permission.
     22  1.1    joff  *
     23  1.1    joff  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  1.1    joff  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.1    joff  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.1    joff  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  1.1    joff  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.1    joff  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.1    joff  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.1    joff  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.1    joff  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.1    joff  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.1    joff  * POSSIBILITY OF SUCH DAMAGE.
     34  1.1    joff  */
     35  1.1    joff 
     36  1.1    joff /*
     37  1.1    joff  * Driver for the ep93xx clock tick.
     38  1.1    joff  *
     39  1.1    joff  * We use the 64Hz RTC interrupt as its the only thing that allows for timekeeping
     40  1.1    joff  * of a second (crystal error only).  There are two general purpose timers
     41  1.1    joff  * on the ep93xx, but they run at a frequency that makes a perfect integer
     42  1.1    joff  * number of ticks per second impossible.  Note that there was an errata with
     43  1.1    joff  * the ep93xx processor and many early boards (including the Cirrus eval board) have
     44  1.1    joff  * a broken crystal oscillator input that may make this 64Hz unreliable.  However,
     45  1.1    joff  * not all boards are susceptible, the Technologic Systems TS-7200 is a notable
     46  1.1    joff  * exception that is immune to this errata.   --joff
     47  1.1    joff  */
     48  1.1    joff 
     49  1.1    joff #include <sys/cdefs.h>
     50  1.6      he __KERNEL_RCSID(0, "$NetBSD: epclk.c,v 1.6 2005/06/04 22:37:51 he Exp $");
     51  1.1    joff 
     52  1.1    joff #include <sys/types.h>
     53  1.1    joff #include <sys/param.h>
     54  1.1    joff #include <sys/systm.h>
     55  1.1    joff #include <sys/kernel.h>
     56  1.1    joff #include <sys/time.h>
     57  1.1    joff #include <sys/device.h>
     58  1.1    joff 
     59  1.1    joff #include <machine/bus.h>
     60  1.1    joff #include <machine/intr.h>
     61  1.1    joff 
     62  1.1    joff #include <arm/cpufunc.h>
     63  1.1    joff 
     64  1.1    joff #include <arm/ep93xx/epsocvar.h>
     65  1.1    joff #include <arm/ep93xx/epclkreg.h>
     66  1.1    joff #include <arm/ep93xx/ep93xxvar.h>
     67  1.2    joff #include <dev/clock_subr.h>
     68  1.1    joff 
     69  1.1    joff static int	epclk_match(struct device *, struct cfdata *, void *);
     70  1.1    joff static void	epclk_attach(struct device *, struct device *, void *);
     71  1.1    joff 
     72  1.1    joff void		rtcinit(void);
     73  1.1    joff 
     74  1.1    joff /* callback functions for intr_functions */
     75  1.1    joff static int      epclk_intr(void* arg);
     76  1.1    joff 
     77  1.1    joff struct epclk_softc {
     78  1.1    joff 	struct device		sc_dev;
     79  1.1    joff 	bus_addr_t		sc_baseaddr;
     80  1.1    joff 	bus_space_tag_t		sc_iot;
     81  1.1    joff 	bus_space_handle_t	sc_ioh;
     82  1.1    joff 	bus_space_handle_t	sc_teoi_ioh;
     83  1.1    joff 	int			sc_intr;
     84  1.1    joff };
     85  1.1    joff 
     86  1.1    joff static struct epclk_softc *epclk_sc = NULL;
     87  1.1    joff static u_int32_t tmark;
     88  1.1    joff 
     89  1.1    joff 
     90  1.1    joff /* This is a quick ARM way to multiply by 983040/1000000 */
     91  1.1    joff #define US_TO_TIMER4VAL(x) { \
     92  1.1    joff 	u_int32_t hi, lo, scalar = 4222124650UL; \
     93  1.1    joff 	asm volatile ( \
     94  1.1    joff 		"umull %0, %1, %2, %3;" \
     95  1.1    joff 		: "=&r"(lo), "=&r"(hi) \
     96  1.1    joff 		: "r"((x)), "r"(scalar) \
     97  1.1    joff 	); \
     98  1.1    joff 	(x) = hi; \
     99  1.1    joff }
    100  1.1    joff 
    101  1.1    joff /* This is a quick ARM way to multiply by 1000000/983040 */
    102  1.1    joff #define TIMER4VAL_TO_US(x) { \
    103  1.1    joff 	u_int32_t hi, lo, scalar = 2184533333UL; \
    104  1.1    joff 	asm volatile ( \
    105  1.1    joff 		"umull %0, %1, %2, %3;" \
    106  1.1    joff 		"mov %1, %1, lsl #1;" \
    107  1.1    joff 		"mov %0, %0, lsr #31;" \
    108  1.1    joff 		"orr %1, %1, %0;" \
    109  1.1    joff 		: "=&r"(lo), "=&r"(hi) \
    110  1.1    joff 		: "r"((x)), "r"(scalar) \
    111  1.1    joff 	); \
    112  1.1    joff 	(x) = hi; \
    113  1.1    joff }
    114  1.1    joff 
    115  1.1    joff 
    116  1.1    joff CFATTACH_DECL(epclk, sizeof(struct epclk_softc),
    117  1.1    joff     epclk_match, epclk_attach, NULL, NULL);
    118  1.1    joff 
    119  1.1    joff #define TIMER4VAL()	(*(volatile u_int32_t *)(EP93XX_APB_VBASE + \
    120  1.1    joff 	EP93XX_APB_TIMERS + EP93XX_TIMERS_Timer4ValueLow))
    121  1.1    joff 
    122  1.1    joff static int
    123  1.1    joff epclk_match(struct device *parent, struct cfdata *match, void *aux)
    124  1.1    joff {
    125  1.1    joff 
    126  1.1    joff 	return 2;
    127  1.1    joff }
    128  1.1    joff 
    129  1.1    joff static void
    130  1.1    joff epclk_attach(struct device *parent, struct device *self, void *aux)
    131  1.1    joff {
    132  1.1    joff 	struct epclk_softc		*sc;
    133  1.1    joff 	struct epsoc_attach_args	*sa;
    134  1.1    joff 
    135  1.1    joff 	printf("\n");
    136  1.1    joff 
    137  1.1    joff 	sc = (struct epclk_softc*) self;
    138  1.1    joff 	sa = aux;
    139  1.1    joff 	sc->sc_iot = sa->sa_iot;
    140  1.1    joff 	sc->sc_baseaddr = sa->sa_addr;
    141  1.1    joff 	sc->sc_intr = sa->sa_intr;
    142  1.1    joff 
    143  1.1    joff 	if (epclk_sc == NULL)
    144  1.1    joff 		epclk_sc = sc;
    145  1.1    joff 
    146  1.1    joff 	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size,
    147  1.1    joff 		0, &sc->sc_ioh))
    148  1.1    joff 		panic("%s: Cannot map registers", self->dv_xname);
    149  1.1    joff 	if (bus_space_map(sa->sa_iot, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON +
    150  1.1    joff 		EP93XX_SYSCON_TEOI, 4, 0, &sc->sc_teoi_ioh))
    151  1.1    joff 		panic("%s: Cannot map registers", self->dv_xname);
    152  1.1    joff 
    153  1.1    joff 	/* clear and start the debug timer (Timer4) */
    154  1.1    joff 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EP93XX_TIMERS_Timer4Enable, 0);
    155  1.1    joff 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EP93XX_TIMERS_Timer4Enable, 0x100);
    156  1.1    joff }
    157  1.1    joff 
    158  1.1    joff /*
    159  1.1    joff  * epclk_intr:
    160  1.1    joff  *
    161  1.1    joff  *	Handle the hardclock interrupt.
    162  1.1    joff  */
    163  1.1    joff static int
    164  1.1    joff epclk_intr(void *arg)
    165  1.1    joff {
    166  1.1    joff 	struct epclk_softc*	sc;
    167  1.1    joff 
    168  1.1    joff 	tmark = TIMER4VAL();
    169  1.1    joff 	sc = epclk_sc;
    170  1.1    joff 
    171  1.1    joff 	bus_space_write_4(sc->sc_iot, sc->sc_teoi_ioh, 0, 1);
    172  1.1    joff 	hardclock((struct clockframe*) arg);
    173  1.1    joff 	return (1);
    174  1.1    joff }
    175  1.1    joff 
    176  1.1    joff /*
    177  1.1    joff  * setstatclockrate:
    178  1.1    joff  *
    179  1.1    joff  *	Set the rate of the statistics clock.
    180  1.1    joff  *
    181  1.1    joff  *	We assume that hz is either stathz or profhz, and that neither
    182  1.1    joff  *	will change after being set by cpu_initclocks().  We could
    183  1.1    joff  *	recalculate the intervals here, but that would be a pain.
    184  1.1    joff  */
    185  1.1    joff void
    186  1.6      he setstatclockrate(int newhz)
    187  1.1    joff {
    188  1.1    joff 
    189  1.1    joff 	/* use hardclock */
    190  1.1    joff }
    191  1.1    joff 
    192  1.1    joff /*
    193  1.1    joff  * cpu_initclocks:
    194  1.1    joff  *
    195  1.1    joff  *	Initialize the clock and get them going.
    196  1.1    joff  */
    197  1.1    joff void
    198  1.1    joff cpu_initclocks(void)
    199  1.1    joff {
    200  1.1    joff 	struct epclk_softc*	sc;
    201  1.1    joff 
    202  1.1    joff 	sc = epclk_sc;
    203  1.1    joff 	stathz = profhz = 0;
    204  1.1    joff 
    205  1.1    joff 	if (hz != 64) panic("HZ must be 64!");
    206  1.1    joff 
    207  1.1    joff 	/* clear 64Hz interrupt status */
    208  1.1    joff 	bus_space_write_4(sc->sc_iot, sc->sc_teoi_ioh, 0, 1);
    209  1.1    joff 
    210  1.1    joff 	ep93xx_intr_establish(sc->sc_intr, IPL_CLOCK, epclk_intr, NULL);
    211  1.1    joff }
    212  1.1    joff 
    213  1.1    joff /*
    214  1.1    joff  * microtime:
    215  1.1    joff  *
    216  1.1    joff  *	Fill in the specified timeval struct with the current time
    217  1.1    joff  *	accurate to the microsecond.
    218  1.1    joff  */
    219  1.1    joff void
    220  1.1    joff microtime(register struct timeval *tvp)
    221  1.1    joff {
    222  1.1    joff 	u_int			oldirqstate;
    223  1.1    joff 	u_int			tmarknow, delta;
    224  1.1    joff 	static struct timeval	lasttv;
    225  1.1    joff 
    226  1.1    joff #ifdef DEBUG
    227  1.1    joff 	if (epclk_sc == NULL) {
    228  1.1    joff 		printf("microtime: called before initialize epclk\n");
    229  1.1    joff 		tvp->tv_sec = 0;
    230  1.1    joff 		tvp->tv_usec = 0;
    231  1.1    joff 		return;
    232  1.1    joff 	}
    233  1.1    joff #endif
    234  1.1    joff 
    235  1.1    joff 	oldirqstate = disable_interrupts(I32_bit);
    236  1.1    joff 	tmarknow = TIMER4VAL();
    237  1.1    joff 
    238  1.1    joff         /* Fill in the timeval struct. */
    239  1.1    joff 	*tvp = time;
    240  1.1    joff 	if (__predict_false(tmarknow < tmark)) { /* overflow */
    241  1.1    joff 		delta = tmarknow + (UINT_MAX - tmark);
    242  1.1    joff 	} else {
    243  1.1    joff 		delta = tmarknow - tmark;
    244  1.1    joff 	}
    245  1.1    joff 
    246  1.1    joff 	TIMER4VAL_TO_US(delta);
    247  1.1    joff 
    248  1.1    joff 	tvp->tv_usec += delta;
    249  1.1    joff 
    250  1.1    joff         /* Make sure microseconds doesn't overflow. */
    251  1.1    joff 	while (__predict_false(tvp->tv_usec >= 1000000)) {
    252  1.1    joff 		tvp->tv_usec -= 1000000;
    253  1.1    joff 		tvp->tv_sec++;
    254  1.1    joff 	}
    255  1.1    joff 
    256  1.1    joff         /* Make sure the time has advanced. */
    257  1.1    joff 	if (__predict_false(tvp->tv_sec == lasttv.tv_sec &&
    258  1.1    joff 	    tvp->tv_usec <= lasttv.tv_usec)) {
    259  1.1    joff 		tvp->tv_usec = lasttv.tv_usec + 1;
    260  1.1    joff 		if (tvp->tv_usec >= 1000000) {
    261  1.1    joff 			tvp->tv_usec -= 1000000;
    262  1.1    joff 			tvp->tv_sec++;
    263  1.1    joff 		}
    264  1.1    joff 	}
    265  1.1    joff 
    266  1.1    joff 	lasttv = *tvp;
    267  1.1    joff 
    268  1.1    joff 	restore_interrupts(oldirqstate);
    269  1.1    joff }
    270  1.1    joff 
    271  1.1    joff /*
    272  1.1    joff  * delay:
    273  1.1    joff  *
    274  1.1    joff  *	Delay for at least N microseconds.
    275  1.1    joff  */
    276  1.1    joff void
    277  1.1    joff delay(unsigned int len)
    278  1.1    joff {
    279  1.1    joff 	u_int32_t	start, end, ticks;
    280  1.1    joff 
    281  1.1    joff #ifdef DEBUG
    282  1.1    joff 	if (epclk_sc == NULL) {
    283  1.1    joff 		printf("delay: called before start epclk\n");
    284  1.1    joff 		return;
    285  1.1    joff 	}
    286  1.1    joff #endif
    287  1.1    joff 
    288  1.1    joff 	ticks = start = TIMER4VAL();
    289  1.1    joff 	US_TO_TIMER4VAL(len);
    290  1.1    joff 	end = start + len;
    291  1.1    joff 	while (start <= ticks && ticks > end) {
    292  1.1    joff 		/* wait for Timer4ValueLow wraparound */
    293  1.1    joff 		ticks = TIMER4VAL();
    294  1.1    joff 	}
    295  1.1    joff 	while (ticks <= end) {
    296  1.1    joff 		ticks = TIMER4VAL();
    297  1.1    joff 	}
    298  1.1    joff }
    299  1.1    joff 
    300  1.2    joff todr_chip_handle_t todr_handle;
    301  1.2    joff 
    302  1.2    joff /*
    303  1.2    joff  * todr_attach:
    304  1.2    joff  *
    305  1.2    joff  *	Set the specified time-of-day register as the system real-time clock.
    306  1.2    joff  */
    307  1.1    joff void
    308  1.2    joff todr_attach(todr_chip_handle_t todr)
    309  1.1    joff {
    310  1.2    joff 
    311  1.2    joff 	if (todr_handle)
    312  1.2    joff 		panic("todr_attach: rtc already configured");
    313  1.2    joff 	todr_handle = todr;
    314  1.1    joff }
    315  1.1    joff 
    316  1.2    joff /*
    317  1.2    joff  * inittodr:
    318  1.2    joff  *
    319  1.2    joff  *	Initialize time from the time-of-day register.
    320  1.2    joff  */
    321  1.2    joff #define	MINYEAR		2003	/* minimum plausible year */
    322  1.1    joff void
    323  1.1    joff inittodr(time_t base)
    324  1.1    joff {
    325  1.2    joff 	time_t deltat;
    326  1.2    joff 	int badbase;
    327  1.2    joff 
    328  1.2    joff 	if (base < (MINYEAR - 1970) * SECYR) {
    329  1.3    joff 		printf("WARNING: preposterous time in file system\n");
    330  1.2    joff 		/* read the system clock anyway */
    331  1.2    joff 		base = (MINYEAR - 1970) * SECYR;
    332  1.2    joff 		badbase = 1;
    333  1.2    joff 	} else
    334  1.2    joff 		badbase = 0;
    335  1.2    joff 
    336  1.2    joff 	if (todr_handle == NULL ||
    337  1.5      he 	    todr_gettime(todr_handle, &time) != 0 ||
    338  1.2    joff 	    time.tv_sec == 0) {
    339  1.2    joff 		/*
    340  1.2    joff 		 * Believe the time in the file system for lack of
    341  1.2    joff 		 * anything better, resetting the TODR.
    342  1.2    joff 		 */
    343  1.2    joff 		time.tv_sec = base;
    344  1.2    joff 		time.tv_usec = 0;
    345  1.2    joff 		if (todr_handle != NULL && !badbase) {
    346  1.2    joff 			printf("WARNING: preposterous clock chip time\n");
    347  1.2    joff 			resettodr();
    348  1.2    joff 		}
    349  1.2    joff 		goto bad;
    350  1.2    joff 	}
    351  1.2    joff 
    352  1.2    joff 	if (!badbase) {
    353  1.2    joff 		/*
    354  1.4  simonb 		 * See if we gained/lost two or more days; if
    355  1.2    joff 		 * so, assume something is amiss.
    356  1.2    joff 		 */
    357  1.2    joff 		deltat = time.tv_sec - base;
    358  1.2    joff 		if (deltat < 0)
    359  1.2    joff 			deltat = -deltat;
    360  1.2    joff 		if (deltat < 2 * SECDAY)
    361  1.2    joff 			return;		/* all is well */
    362  1.2    joff 		printf("WARNING: clock %s %ld days\n",
    363  1.2    joff 		    time.tv_sec < base ? "lost" : "gained",
    364  1.2    joff 		    (long)deltat / SECDAY);
    365  1.2    joff 	}
    366  1.2    joff  bad:
    367  1.2    joff 	printf("WARNING: CHECK AND RESET THE DATE!\n");
    368  1.2    joff }
    369  1.2    joff 
    370  1.2    joff /*
    371  1.2    joff  * resettodr:
    372  1.2    joff  *
    373  1.2    joff  *	Reset the time-of-day register with the current time.
    374  1.2    joff  */
    375  1.2    joff void
    376  1.2    joff resettodr(void)
    377  1.2    joff {
    378  1.2    joff 
    379  1.2    joff 	if (time.tv_sec == 0)
    380  1.2    joff 		return;
    381  1.1    joff 
    382  1.2    joff 	if (todr_handle != NULL &&
    383  1.5      he 	    todr_settime(todr_handle, &time) != 0)
    384  1.2    joff 		printf("resettodr: failed to set time\n");
    385  1.1    joff }
    386