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