Home | History | Annotate | Line # | Download | only in marvell
mvsoctmr.c revision 1.1
      1  1.1  kiyohara /*	$NetBSD: mvsoctmr.c,v 1.1 2010/10/03 05:49:24 kiyohara Exp $	*/
      2  1.1  kiyohara /*
      3  1.1  kiyohara  * Copyright (c) 2007, 2008 KIYOHARA Takashi
      4  1.1  kiyohara  * All rights reserved.
      5  1.1  kiyohara  *
      6  1.1  kiyohara  * Redistribution and use in source and binary forms, with or without
      7  1.1  kiyohara  * modification, are permitted provided that the following conditions
      8  1.1  kiyohara  * are met:
      9  1.1  kiyohara  * 1. Redistributions of source code must retain the above copyright
     10  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer.
     11  1.1  kiyohara  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  kiyohara  *    documentation and/or other materials provided with the distribution.
     14  1.1  kiyohara  *
     15  1.1  kiyohara  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  kiyohara  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.1  kiyohara  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.1  kiyohara  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  1.1  kiyohara  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  1.1  kiyohara  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.1  kiyohara  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  kiyohara  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  1.1  kiyohara  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  1.1  kiyohara  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  1.1  kiyohara  * POSSIBILITY OF SUCH DAMAGE.
     26  1.1  kiyohara  */
     27  1.1  kiyohara #include <sys/cdefs.h>
     28  1.1  kiyohara __KERNEL_RCSID(0, "$NetBSD: mvsoctmr.c,v 1.1 2010/10/03 05:49:24 kiyohara Exp $");
     29  1.1  kiyohara 
     30  1.1  kiyohara #include <sys/param.h>
     31  1.1  kiyohara #include <sys/atomic.h>
     32  1.1  kiyohara #include <sys/bus.h>
     33  1.1  kiyohara #include <sys/device.h>
     34  1.1  kiyohara #include <sys/errno.h>
     35  1.1  kiyohara #include <sys/kernel.h>
     36  1.1  kiyohara #include <sys/time.h>
     37  1.1  kiyohara #include <sys/timetc.h>
     38  1.1  kiyohara #include <sys/systm.h>
     39  1.1  kiyohara 
     40  1.1  kiyohara #include <machine/intr.h>
     41  1.1  kiyohara 
     42  1.1  kiyohara #include <arm/cpufunc.h>
     43  1.1  kiyohara 
     44  1.1  kiyohara #include <arm/marvell/mvsocreg.h>
     45  1.1  kiyohara #include <arm/marvell/mvsocvar.h>
     46  1.1  kiyohara #include <arm/marvell/mvsoctmrreg.h>
     47  1.1  kiyohara 
     48  1.1  kiyohara #include <dev/marvell/marvellvar.h>
     49  1.1  kiyohara 
     50  1.1  kiyohara 
     51  1.1  kiyohara struct mvsoctmr_softc {
     52  1.1  kiyohara 	device_t sc_dev;
     53  1.1  kiyohara 
     54  1.1  kiyohara 	bus_space_tag_t sc_iot;
     55  1.1  kiyohara 	bus_space_handle_t sc_ioh;
     56  1.1  kiyohara };
     57  1.1  kiyohara 
     58  1.1  kiyohara 
     59  1.1  kiyohara static int mvsoctmr_match(device_t, struct cfdata *, void *);
     60  1.1  kiyohara static void mvsoctmr_attach(device_t, device_t, void *);
     61  1.1  kiyohara 
     62  1.1  kiyohara static int clockhandler(void *);
     63  1.1  kiyohara static int statclockhandler(void *);
     64  1.1  kiyohara 
     65  1.1  kiyohara static u_int mvsoctmr_get_timecount(struct timecounter *);
     66  1.1  kiyohara 
     67  1.1  kiyohara static void mvsoctmr_cntl(struct mvsoctmr_softc *, int, u_int, int, int);
     68  1.1  kiyohara 
     69  1.1  kiyohara #ifndef STATHZ
     70  1.1  kiyohara #define STATHZ	64
     71  1.1  kiyohara #endif
     72  1.1  kiyohara 
     73  1.1  kiyohara static struct mvsoctmr_softc *mvsoctmr_sc;
     74  1.1  kiyohara static uint32_t clock_ticks, statclock_ticks;
     75  1.1  kiyohara static struct timecounter mvsoctmr_timecounter = {
     76  1.1  kiyohara 	mvsoctmr_get_timecount,	/* get_timecount */
     77  1.1  kiyohara 	0,			/* no poll_pps */
     78  1.1  kiyohara 	~0u,			/* counter_mask */
     79  1.1  kiyohara 	0,			/* frequency  (set by cpu_initclocks()) */
     80  1.1  kiyohara 	"mvsoctmr",		/* name */
     81  1.1  kiyohara 	100,			/* quality */
     82  1.1  kiyohara 	NULL,			/* prev */
     83  1.1  kiyohara 	NULL,			/* next */
     84  1.1  kiyohara };
     85  1.1  kiyohara static volatile uint32_t mvsoctmr_base;
     86  1.1  kiyohara 
     87  1.1  kiyohara CFATTACH_DECL_NEW(mvsoctmr, sizeof(struct mvsoctmr_softc),
     88  1.1  kiyohara     mvsoctmr_match, mvsoctmr_attach, NULL, NULL);
     89  1.1  kiyohara 
     90  1.1  kiyohara 
     91  1.1  kiyohara /* ARGSUSED */
     92  1.1  kiyohara static int
     93  1.1  kiyohara mvsoctmr_match(device_t parent, struct cfdata *match, void *aux)
     94  1.1  kiyohara {
     95  1.1  kiyohara 	struct marvell_attach_args *mva = aux;
     96  1.1  kiyohara 
     97  1.1  kiyohara 	if (strcmp(mva->mva_name, match->cf_name) != 0)
     98  1.1  kiyohara 		return 0;
     99  1.1  kiyohara 	if (mva->mva_offset == MVA_OFFSET_DEFAULT)
    100  1.1  kiyohara 		return 0;
    101  1.1  kiyohara 
    102  1.1  kiyohara 	mva->mva_size = MVSOCTMR_SIZE;
    103  1.1  kiyohara 	return 1;
    104  1.1  kiyohara }
    105  1.1  kiyohara 
    106  1.1  kiyohara /* ARGSUSED */
    107  1.1  kiyohara static void
    108  1.1  kiyohara mvsoctmr_attach(device_t parent, device_t self, void *aux)
    109  1.1  kiyohara {
    110  1.1  kiyohara         struct mvsoctmr_softc *sc = device_private(self);
    111  1.1  kiyohara 	struct marvell_attach_args *mva = aux;
    112  1.1  kiyohara 
    113  1.1  kiyohara 	aprint_naive("\n");
    114  1.1  kiyohara 	aprint_normal(": Marvell SoC Timer\n");
    115  1.1  kiyohara 
    116  1.1  kiyohara 	if (mvsoctmr_sc == NULL)
    117  1.1  kiyohara 		mvsoctmr_sc = sc;
    118  1.1  kiyohara 
    119  1.1  kiyohara 	sc->sc_dev = self;
    120  1.1  kiyohara 	sc->sc_iot = mva->mva_iot;
    121  1.1  kiyohara 	if (bus_space_subregion(mva->mva_iot, mva->mva_ioh,
    122  1.1  kiyohara 	    mva->mva_offset, mva->mva_size, &sc->sc_ioh))
    123  1.1  kiyohara 		panic("%s: Cannot map registers", device_xname(self));
    124  1.1  kiyohara }
    125  1.1  kiyohara 
    126  1.1  kiyohara /*
    127  1.1  kiyohara  * clockhandler:
    128  1.1  kiyohara  *
    129  1.1  kiyohara  *	Handle the hardclock interrupt.
    130  1.1  kiyohara  */
    131  1.1  kiyohara static int
    132  1.1  kiyohara clockhandler(void *arg)
    133  1.1  kiyohara {
    134  1.1  kiyohara 	struct clockframe *frame = arg;
    135  1.1  kiyohara 
    136  1.1  kiyohara 	atomic_add_32(&mvsoctmr_base, clock_ticks);
    137  1.1  kiyohara 
    138  1.1  kiyohara 	hardclock(frame);
    139  1.1  kiyohara 
    140  1.1  kiyohara 	return 1;
    141  1.1  kiyohara }
    142  1.1  kiyohara 
    143  1.1  kiyohara /*
    144  1.1  kiyohara  * statclockhandler:
    145  1.1  kiyohara  *
    146  1.1  kiyohara  *	Handle the statclock interrupt.
    147  1.1  kiyohara  */
    148  1.1  kiyohara static int
    149  1.1  kiyohara statclockhandler(void *arg)
    150  1.1  kiyohara {
    151  1.1  kiyohara 	struct clockframe *frame = arg;
    152  1.1  kiyohara 
    153  1.1  kiyohara 	statclock(frame);
    154  1.1  kiyohara 
    155  1.1  kiyohara 	return 1;
    156  1.1  kiyohara }
    157  1.1  kiyohara 
    158  1.1  kiyohara 
    159  1.1  kiyohara /*
    160  1.1  kiyohara  * setstatclockrate:
    161  1.1  kiyohara  *
    162  1.1  kiyohara  *	Set the rate of the statistics clock.
    163  1.1  kiyohara  *
    164  1.1  kiyohara  *	We assume that hz is either stathz or profhz, and that neither
    165  1.1  kiyohara  *	will change after being set by cpu_initclocks().  We could
    166  1.1  kiyohara  *	recalculate the intervals here, but that would be a pain.
    167  1.1  kiyohara  */
    168  1.1  kiyohara /* ARGSUSED */
    169  1.1  kiyohara void
    170  1.1  kiyohara setstatclockrate(int newhz)
    171  1.1  kiyohara {
    172  1.1  kiyohara 	struct mvsoctmr_softc *sc = mvsoctmr_sc;
    173  1.1  kiyohara 	const int en = 1, autoen = 1;
    174  1.1  kiyohara 
    175  1.1  kiyohara 	statclock_ticks = mvTclk / newhz;
    176  1.1  kiyohara 
    177  1.1  kiyohara 	mvsoctmr_cntl(sc, MVSOCTMR_TIMER1, statclock_ticks, en, autoen);
    178  1.1  kiyohara }
    179  1.1  kiyohara 
    180  1.1  kiyohara /*
    181  1.1  kiyohara  * cpu_initclocks:
    182  1.1  kiyohara  *
    183  1.1  kiyohara  *	Initialize the clock and get them going.
    184  1.1  kiyohara  */
    185  1.1  kiyohara void
    186  1.1  kiyohara cpu_initclocks()
    187  1.1  kiyohara {
    188  1.1  kiyohara 	struct mvsoctmr_softc *sc;
    189  1.1  kiyohara 	void *clock_ih;
    190  1.1  kiyohara 	const int en = 1, autoen = 1;
    191  1.1  kiyohara 
    192  1.1  kiyohara 	sc = mvsoctmr_sc;
    193  1.1  kiyohara 	if (sc == NULL)
    194  1.1  kiyohara 		panic("cpu_initclocks: mvsoctmr not found");
    195  1.1  kiyohara 
    196  1.1  kiyohara 	stathz = profhz = STATHZ;
    197  1.1  kiyohara 	mvsoctmr_timecounter.tc_frequency = mvTclk;
    198  1.1  kiyohara 	clock_ticks = mvTclk / hz;
    199  1.1  kiyohara 
    200  1.1  kiyohara 	mvsoctmr_cntl(sc, MVSOCTMR_TIMER0, clock_ticks, en, autoen);
    201  1.1  kiyohara 
    202  1.1  kiyohara 	clock_ih = mvsoc_bridge_intr_establish(MVSOC_MLMB_MLMBI_CPUTIMER0INTREQ,
    203  1.1  kiyohara 	    IPL_CLOCK, clockhandler, NULL);
    204  1.1  kiyohara 	if (clock_ih == NULL)
    205  1.1  kiyohara 		panic("cpu_initclocks: unable to register timer interrupt");
    206  1.1  kiyohara 
    207  1.1  kiyohara 	if (stathz) {
    208  1.1  kiyohara 		setstatclockrate(stathz);
    209  1.1  kiyohara 		clock_ih = mvsoc_bridge_intr_establish(
    210  1.1  kiyohara 		    MVSOC_MLMB_MLMBI_CPUTIMER1INTREQ, IPL_HIGH,
    211  1.1  kiyohara 		    statclockhandler, NULL);
    212  1.1  kiyohara 		if (clock_ih == NULL)
    213  1.1  kiyohara 			panic("cpu_initclocks:"
    214  1.1  kiyohara 			    " unable to register statclock timer interrupt");
    215  1.1  kiyohara 	}
    216  1.1  kiyohara 
    217  1.1  kiyohara 	tc_init(&mvsoctmr_timecounter);
    218  1.1  kiyohara }
    219  1.1  kiyohara 
    220  1.1  kiyohara void
    221  1.1  kiyohara delay(unsigned int n)
    222  1.1  kiyohara {
    223  1.1  kiyohara 	struct mvsoctmr_softc *sc;
    224  1.1  kiyohara 	unsigned int cur_tick, initial_tick;
    225  1.1  kiyohara 	int remaining;
    226  1.1  kiyohara 
    227  1.1  kiyohara 	sc = mvsoctmr_sc;
    228  1.1  kiyohara #ifdef DEBUG
    229  1.1  kiyohara 	if (sc == NULL) {
    230  1.1  kiyohara 		printf("%s: called before start mvsoctmr\n", __func__);
    231  1.1  kiyohara 		return;
    232  1.1  kiyohara 	}
    233  1.1  kiyohara #endif
    234  1.1  kiyohara 
    235  1.1  kiyohara 	/*
    236  1.1  kiyohara 	 * Read the counter first, so that the rest of the setup overhead is
    237  1.1  kiyohara 	 * counted.
    238  1.1  kiyohara 	 */
    239  1.1  kiyohara 	initial_tick = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    240  1.1  kiyohara 	    MVSOCTMR_TIMER(MVSOCTMR_TIMER0));
    241  1.1  kiyohara 
    242  1.1  kiyohara 	if (n <= UINT_MAX / mvTclk) {
    243  1.1  kiyohara 		/*
    244  1.1  kiyohara 		 * For unsigned arithmetic, division can be replaced with
    245  1.1  kiyohara 		 * multiplication with the inverse and a shift.
    246  1.1  kiyohara 		 */
    247  1.1  kiyohara 		remaining = n * mvTclk / 1000000;
    248  1.1  kiyohara 	} else {
    249  1.1  kiyohara 		/*
    250  1.1  kiyohara 		 * This is a very long delay.
    251  1.1  kiyohara 		 * Being slow here doesn't matter.
    252  1.1  kiyohara 		 */
    253  1.1  kiyohara 		remaining = (unsigned long long) n * mvTclk / 1000000;
    254  1.1  kiyohara 	}
    255  1.1  kiyohara 
    256  1.1  kiyohara 	while (remaining > 0) {
    257  1.1  kiyohara 		cur_tick = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    258  1.1  kiyohara 		    MVSOCTMR_TIMER(MVSOCTMR_TIMER0));
    259  1.1  kiyohara 		if (cur_tick > initial_tick)
    260  1.1  kiyohara 			remaining -= clock_ticks - cur_tick + initial_tick;
    261  1.1  kiyohara 		else
    262  1.1  kiyohara 			remaining -= (initial_tick - cur_tick);
    263  1.1  kiyohara 		initial_tick = cur_tick;
    264  1.1  kiyohara 	}
    265  1.1  kiyohara }
    266  1.1  kiyohara 
    267  1.1  kiyohara static u_int
    268  1.1  kiyohara mvsoctmr_get_timecount(struct timecounter *tc)
    269  1.1  kiyohara {
    270  1.1  kiyohara 	struct mvsoctmr_softc *sc = mvsoctmr_sc;
    271  1.1  kiyohara 	uint32_t counter, base;
    272  1.1  kiyohara 	u_int intrstat;
    273  1.1  kiyohara 
    274  1.1  kiyohara 	intrstat = disable_interrupts(I32_bit);
    275  1.1  kiyohara 	base = mvsoctmr_base;
    276  1.1  kiyohara 	counter = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    277  1.1  kiyohara 	    MVSOCTMR_TIMER(MVSOCTMR_TIMER0));
    278  1.1  kiyohara 	restore_interrupts(intrstat);
    279  1.1  kiyohara 
    280  1.1  kiyohara 	return base - counter;
    281  1.1  kiyohara }
    282  1.1  kiyohara 
    283  1.1  kiyohara 
    284  1.1  kiyohara static void
    285  1.1  kiyohara mvsoctmr_cntl(struct mvsoctmr_softc *sc, int num, u_int ticks, int en,
    286  1.1  kiyohara 	      int autoen)
    287  1.1  kiyohara {
    288  1.1  kiyohara 	uint32_t ctrl;
    289  1.1  kiyohara 
    290  1.1  kiyohara 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSOCTMR_RELOAD(num),
    291  1.1  kiyohara 	    ticks);
    292  1.1  kiyohara 
    293  1.1  kiyohara 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSOCTMR_TIMER(num), ticks);
    294  1.1  kiyohara 
    295  1.1  kiyohara 	ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSOCTMR_CTCR);
    296  1.1  kiyohara 	if (en)
    297  1.1  kiyohara 		ctrl |= MVSOCTMR_CTCR_CPUTIMEREN(num);
    298  1.1  kiyohara 	else
    299  1.1  kiyohara 		ctrl &= ~MVSOCTMR_CTCR_CPUTIMEREN(num);
    300  1.1  kiyohara 	if (autoen)
    301  1.1  kiyohara 		ctrl |= MVSOCTMR_CTCR_CPUTIMERAUTO(num);
    302  1.1  kiyohara 	else
    303  1.1  kiyohara 		ctrl &= ~MVSOCTMR_CTCR_CPUTIMERAUTO(num);
    304  1.1  kiyohara 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSOCTMR_CTCR, ctrl);
    305  1.1  kiyohara }
    306