Home | History | Annotate | Line # | Download | only in ep93xx
epclk.c revision 1.12
      1 /*	$NetBSD: epclk.c,v 1.12 2008/04/28 20:23:14 martin 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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * Driver for the ep93xx clock tick.
     31  *
     32  * We use the 64Hz RTC interrupt as its the only thing that allows for timekeeping
     33  * of a second (crystal error only).  There are two general purpose timers
     34  * on the ep93xx, but they run at a frequency that makes a perfect integer
     35  * number of ticks per second impossible.  Note that there was an errata with
     36  * the ep93xx processor and many early boards (including the Cirrus eval board) have
     37  * a broken crystal oscillator input that may make this 64Hz unreliable.  However,
     38  * not all boards are susceptible, the Technologic Systems TS-7200 is a notable
     39  * exception that is immune to this errata.   --joff
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: epclk.c,v 1.12 2008/04/28 20:23:14 martin Exp $");
     44 
     45 #include <sys/types.h>
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/time.h>
     50 #include <sys/timetc.h>
     51 #include <sys/device.h>
     52 
     53 #include <machine/bus.h>
     54 #include <machine/intr.h>
     55 
     56 #include <arm/cpufunc.h>
     57 
     58 #include <arm/ep93xx/epsocvar.h>
     59 #include <arm/ep93xx/epclkreg.h>
     60 #include <arm/ep93xx/ep93xxreg.h>
     61 #include <arm/ep93xx/ep93xxvar.h>
     62 #include <dev/clock_subr.h>
     63 
     64 #include "opt_hz.h"
     65 
     66 #define	TIMER_FREQ	983040
     67 
     68 static int	epclk_match(struct device *, struct cfdata *, void *);
     69 static void	epclk_attach(struct device *, struct device *, void *);
     70 static u_int	epclk_get_timecount(struct timecounter *);
     71 
     72 void		rtcinit(void);
     73 
     74 /* callback functions for intr_functions */
     75 static int      epclk_intr(void* arg);
     76 
     77 struct epclk_softc {
     78 	struct device		sc_dev;
     79 	bus_addr_t		sc_baseaddr;
     80 	bus_space_tag_t		sc_iot;
     81 	bus_space_handle_t	sc_ioh;
     82 #if defined(HZ) && (HZ == 64)
     83 	bus_space_handle_t	sc_teoi_ioh;
     84 #endif
     85 	int			sc_intr;
     86 };
     87 
     88 static struct timecounter epclk_timecounter = {
     89 	epclk_get_timecount,	/* get_timecount */
     90 	0,			/* no poll_pps */
     91 	~0u,			/* counter_mask */
     92 	TIMER_FREQ,		/* frequency */
     93 	"epclk",		/* name */
     94 	100,			/* quality */
     95 	NULL,			/* prev */
     96 	NULL,			/* next */
     97 };
     98 
     99 static struct epclk_softc *epclk_sc = NULL;
    100 
    101 CFATTACH_DECL(epclk, sizeof(struct epclk_softc),
    102     epclk_match, epclk_attach, NULL, NULL);
    103 
    104 #define TIMER4VAL()	(*(volatile u_int32_t *)(EP93XX_APB_VBASE + \
    105 	EP93XX_APB_TIMERS + EP93XX_TIMERS_Timer4ValueLow))
    106 
    107 static int
    108 epclk_match(struct device *parent, struct cfdata *match, void *aux)
    109 {
    110 
    111 	return 2;
    112 }
    113 
    114 static void
    115 epclk_attach(struct device *parent, struct device *self, void *aux)
    116 {
    117 	struct epclk_softc		*sc;
    118 	struct epsoc_attach_args	*sa;
    119 	bool first_run;
    120 
    121 	printf("\n");
    122 
    123 	sc = (struct epclk_softc*) self;
    124 	sa = aux;
    125 	sc->sc_iot = sa->sa_iot;
    126 	sc->sc_baseaddr = sa->sa_addr;
    127 	sc->sc_intr = sa->sa_intr;
    128 
    129 	if (epclk_sc == NULL) {
    130 		first_run = true;
    131 		epclk_sc = sc;
    132 	}
    133 
    134 	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size,
    135 		0, &sc->sc_ioh))
    136 		panic("%s: Cannot map registers", self->dv_xname);
    137 #if defined(HZ) && (HZ == 64)
    138 	if (bus_space_map(sa->sa_iot, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON +
    139 		EP93XX_SYSCON_TEOI, 4, 0, &sc->sc_teoi_ioh))
    140 		panic("%s: Cannot map registers", self->dv_xname);
    141 #endif
    142 
    143 	/* clear and start the debug timer (Timer4) */
    144 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EP93XX_TIMERS_Timer4Enable, 0);
    145 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EP93XX_TIMERS_Timer4Enable, 0x100);
    146 
    147 	if (first_run)
    148 		tc_init(&epclk_timecounter);
    149 }
    150 
    151 /*
    152  * epclk_intr:
    153  *
    154  *	Handle the hardclock interrupt.
    155  */
    156 static int
    157 epclk_intr(void *arg)
    158 {
    159 	struct epclk_softc*	sc;
    160 
    161 	sc = epclk_sc;
    162 
    163 #if defined(HZ) && (HZ == 64)
    164 	bus_space_write_4(sc->sc_iot, sc->sc_teoi_ioh, 0, 1);
    165 #else
    166 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EP93XX_TIMERS_Timer1Clear, 1);
    167 #endif
    168 	hardclock((struct clockframe*) arg);
    169 	return (1);
    170 }
    171 
    172 /*
    173  * setstatclockrate:
    174  *
    175  *	Set the rate of the statistics clock.
    176  *
    177  *	We assume that hz is either stathz or profhz, and that neither
    178  *	will change after being set by cpu_initclocks().  We could
    179  *	recalculate the intervals here, but that would be a pain.
    180  */
    181 void
    182 setstatclockrate(int newhz)
    183 {
    184 
    185 	/* use hardclock */
    186 }
    187 
    188 /*
    189  * cpu_initclocks:
    190  *
    191  *	Initialize the clock and get them going.
    192  */
    193 void
    194 cpu_initclocks(void)
    195 {
    196 	struct epclk_softc*	sc;
    197 
    198 	sc = epclk_sc;
    199 	stathz = profhz = 0;
    200 
    201 #if defined(HZ) && (HZ == 64)
    202 	if (hz != 64) panic("HZ must be 64!");
    203 
    204 	/* clear 64Hz interrupt status */
    205 	bus_space_write_4(sc->sc_iot, sc->sc_teoi_ioh, 0, 1);
    206 #else
    207 #define	CLOCK_SOURCE_RATE	14745600UL
    208 #define	CLOCK_TICK_DIV		29
    209 #define	CLOCK_TICK_RATE \
    210 	(((CLOCK_SOURCE_RATE+(CLOCK_TICK_DIV*hz-1))/(CLOCK_TICK_DIV*hz))*hz)
    211 #define	LATCH	((CLOCK_TICK_RATE + hz/2) / hz)
    212 	/* setup and start the 16bit timer (Timer1) */
    213 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    214 			  EP93XX_TIMERS_Timer1Control,
    215 			  (TimerControl_MODE)|(TimerControl_CLKSEL));
    216 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    217 			  EP93XX_TIMERS_Timer1Load, LATCH-1);
    218 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    219 			  EP93XX_TIMERS_Timer1Control,
    220 			  (TimerControl_ENABLE)|(TimerControl_MODE)|(TimerControl_CLKSEL));
    221 #endif
    222 
    223 	ep93xx_intr_establish(sc->sc_intr, IPL_CLOCK, epclk_intr, NULL);
    224 }
    225 
    226 /*
    227  * delay:
    228  *
    229  *	Delay for at least N microseconds.
    230  */
    231 void
    232 delay(unsigned int n)
    233 {
    234 	unsigned int cur_tick, initial_tick;
    235 	int remaining;
    236 
    237 #ifdef DEBUG
    238 	if (epclk_sc == NULL) {
    239 		printf("delay: called before start epclk\n");
    240 		return;
    241 	}
    242 #endif
    243 
    244 	/*
    245 	 * Read the counter first, so that the rest of the setup overhead is
    246 	 * counted.
    247 	 */
    248 	initial_tick = TIMER4VAL();
    249 
    250 	if (n <= UINT_MAX / TIMER_FREQ) {
    251 		/*
    252 		 * For unsigned arithmetic, division can be replaced with
    253 		 * multiplication with the inverse and a shift.
    254 		 */
    255 		remaining = n * TIMER_FREQ / 1000000;
    256 	} else {
    257 		/* This is a very long delay.
    258 		 * Being slow here doesn't matter.
    259 		 */
    260 		remaining = (unsigned long long) n * TIMER_FREQ / 1000000;
    261 	}
    262 
    263 	while (remaining > 0) {
    264 		cur_tick = TIMER4VAL();
    265 		if (cur_tick > initial_tick)
    266 			remaining -= UINT_MAX - (cur_tick - initial_tick);
    267 		else
    268 			remaining -= initial_tick - cur_tick;
    269 		initial_tick = cur_tick;
    270 	}
    271 }
    272 
    273 static u_int
    274 epclk_get_timecount(struct timecounter *tc)
    275 {
    276 	return TIMER4VAL();
    277 }
    278