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