Home | History | Annotate | Line # | Download | only in at91
at91tctmr.c revision 1.7
      1  1.7    skrll /*$NetBSD: at91tctmr.c,v 1.7 2012/11/12 18:00:36 skrll Exp $*/
      2  1.2     matt 
      3  1.2     matt /*
      4  1.2     matt  * AT91 Timer Counter (TC) based clock functions
      5  1.2     matt  * Copyright (c) 2007, Embedtronics Oy
      6  1.2     matt  * All rights reserved.
      7  1.2     matt  *
      8  1.2     matt  * Based on vx115_clk.c,
      9  1.2     matt  * Copyright (c) 2006, Jon Sevy <jsevy (at) cs.drexel.edu>
     10  1.2     matt  *
     11  1.2     matt  * Based on epclk.c
     12  1.2     matt  * Copyright (c) 2004 Jesse Off
     13  1.2     matt  * All rights reserved.
     14  1.2     matt  *
     15  1.2     matt  * Redistribution and use in source and binary forms, with or without
     16  1.2     matt  * modification, are permitted provided that the following conditions
     17  1.2     matt  * are met:
     18  1.2     matt  * 1. Redistributions of source code must retain the above copyright
     19  1.2     matt  *    notice, this list of conditions and the following disclaimer.
     20  1.2     matt  * 2. Redistributions in binary form must reproduce the above copyright
     21  1.2     matt  *    notice, this list of conditions and the following disclaimer in the
     22  1.2     matt  *    documentation and/or other materials provided with the distribution.
     23  1.2     matt  *
     24  1.2     matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  1.2     matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  1.2     matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  1.2     matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  1.2     matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  1.2     matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  1.2     matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  1.2     matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  1.2     matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  1.2     matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  1.2     matt  * POSSIBILITY OF SUCH DAMAGE.
     35  1.2     matt  */
     36  1.2     matt 
     37  1.2     matt /*
     38  1.2     matt  * Driver for the AT91RM9200 clock tick.
     39  1.2     matt  * We use Timer 1 for the system clock
     40  1.2     matt  */
     41  1.2     matt 
     42  1.2     matt #include <sys/cdefs.h>
     43  1.7    skrll __KERNEL_RCSID(0, "$NetBSD: at91tctmr.c,v 1.7 2012/11/12 18:00:36 skrll Exp $");
     44  1.2     matt 
     45  1.2     matt #include <sys/types.h>
     46  1.2     matt #include <sys/param.h>
     47  1.2     matt #include <sys/systm.h>
     48  1.2     matt #include <sys/kernel.h>
     49  1.2     matt #include <sys/time.h>
     50  1.2     matt #include <sys/timetc.h>
     51  1.2     matt #include <sys/device.h>
     52  1.2     matt 
     53  1.2     matt #include <dev/clock_subr.h>
     54  1.2     matt 
     55  1.5   dyoung #include <sys/bus.h>
     56  1.2     matt #include <machine/intr.h>
     57  1.2     matt 
     58  1.2     matt #include <arm/cpufunc.h>
     59  1.2     matt #include <arm/at91/at91reg.h>
     60  1.2     matt #include <arm/at91/at91var.h>
     61  1.2     matt #include <arm/at91/at91tcreg.h>
     62  1.2     matt 
     63  1.2     matt #include <opt_hz.h>     /* for HZ */
     64  1.2     matt 
     65  1.2     matt 
     66  1.2     matt #define DEBUG_CLK
     67  1.2     matt #ifdef DEBUG_CLK
     68  1.2     matt #define DPRINTF(fmt...)  printf(fmt)
     69  1.2     matt #else
     70  1.2     matt #define DPRINTF(fmt...)
     71  1.2     matt #endif
     72  1.2     matt 
     73  1.2     matt 
     74  1.2     matt static int at91tctmr_match(device_t, cfdata_t, void *);
     75  1.2     matt static void at91tctmr_attach(device_t, device_t, void *);
     76  1.2     matt 
     77  1.2     matt void rtcinit(void);
     78  1.2     matt 
     79  1.2     matt /* callback functions for intr_functions */
     80  1.2     matt static int at91tctmr_intr(void* arg);
     81  1.2     matt 
     82  1.2     matt struct at91tctmr_softc {
     83  1.2     matt 	device_t	sc_dev;
     84  1.2     matt 	u_char		*sc_addr;
     85  1.2     matt 	int		sc_pid;
     86  1.2     matt 	int		sc_initialized;
     87  1.2     matt 	uint32_t	sc_timerclock;
     88  1.2     matt 	uint32_t	sc_divider;
     89  1.2     matt 	uint32_t	sc_usec_per_tick;
     90  1.2     matt };
     91  1.2     matt 
     92  1.2     matt static struct at91tctmr_softc *at91tctmr_sc = NULL;
     93  1.6  aymeric #if 0
     94  1.2     matt static struct timeval lasttv;
     95  1.6  aymeric #endif
     96  1.2     matt 
     97  1.2     matt 
     98  1.2     matt 
     99  1.2     matt /* Match value for clock timer; running at master clock, want HZ ticks per second  */
    100  1.2     matt /* NOTE: don't change there without visiting the functions below which      */
    101  1.2     matt /* convert between timer counts and microseconds                            */
    102  1.2     matt 
    103  1.2     matt static inline uint32_t
    104  1.2     matt at91tctmr_count_to_usec(struct at91tctmr_softc *sc, uint32_t count)
    105  1.2     matt {
    106  1.2     matt     uint64_t tmp;
    107  1.2     matt 
    108  1.2     matt     tmp = count;
    109  1.2     matt     tmp *= 1000000U;
    110  1.2     matt 
    111  1.2     matt     return (tmp / sc->sc_timerclock);
    112  1.2     matt }
    113  1.2     matt 
    114  1.2     matt #if 0
    115  1.2     matt /* This may only be called when overflow is avoided; typically, */
    116  1.2     matt /* it will be used when usec < USEC_PER_TICK              */
    117  1.2     matt static uint32_t
    118  1.2     matt usec_to_timer_count(uint32_t usec)
    119  1.2     matt {
    120  1.2     matt     uint32_t result;
    121  1.2     matt 
    122  1.2     matt     /* convert specified number of usec to timer ticks, and round up */
    123  1.2     matt     result = (AT91_SCLK * usec) / 1000000;
    124  1.2     matt 
    125  1.2     matt     if ((result * 1000000) != (usec * AT91_SCLK))
    126  1.2     matt     {
    127  1.2     matt         /* round up */
    128  1.2     matt         result += 1;
    129  1.2     matt     }
    130  1.2     matt 
    131  1.2     matt     return result;
    132  1.2     matt 
    133  1.2     matt }
    134  1.2     matt #endif
    135  1.2     matt 
    136  1.2     matt /* macros to simplify writing to the timer controller */
    137  1.7    skrll static inline uint32_t
    138  1.2     matt READ_TC(struct at91tctmr_softc *sc, uint offset)
    139  1.2     matt {
    140  1.7    skrll 	volatile uint32_t *addr = (void*)(sc->sc_addr + offset);
    141  1.2     matt 	return *addr;
    142  1.2     matt }
    143  1.2     matt 
    144  1.2     matt //bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset)
    145  1.2     matt static inline void
    146  1.7    skrll WRITE_TC(struct at91tctmr_softc *sc, uint offset, uint32_t value)
    147  1.2     matt {
    148  1.7    skrll 	volatile uint32_t *addr = (void*)(sc->sc_addr + offset);
    149  1.2     matt 	*addr = value;
    150  1.2     matt }
    151  1.2     matt 
    152  1.2     matt 
    153  1.2     matt CFATTACH_DECL_NEW(at91tctmr, sizeof(struct at91tctmr_softc),
    154  1.2     matt     at91tctmr_match, at91tctmr_attach, NULL, NULL);
    155  1.2     matt 
    156  1.6  aymeric #if 0
    157  1.2     matt static u_int at91tctmr_get_timecount(struct timecounter *);
    158  1.2     matt 
    159  1.2     matt static struct timecounter at91tctmr_timecounter = {
    160  1.2     matt 	at91tctmr_get_timecount,/* get_timecount */
    161  1.2     matt 	0,                      /* no poll_pps */
    162  1.2     matt 	0xffffffff,		/* counter_mask */
    163  1.2     matt 	COUNTS_PER_SEC,		/* frequency */
    164  1.2     matt 	"at91tctmr",		/* name */
    165  1.2     matt 	100,			/* quality */
    166  1.2     matt 	NULL,			/* prev */
    167  1.2     matt 	NULL,			/* next */
    168  1.2     matt };
    169  1.6  aymeric #endif
    170  1.2     matt 
    171  1.2     matt static int
    172  1.2     matt at91tctmr_match(device_t parent, cfdata_t match, void *aux)
    173  1.2     matt {
    174  1.2     matt 	if (strcmp(match->cf_name, "at91tctmr") == 0)
    175  1.2     matt 		return 2;
    176  1.2     matt 	return 0;
    177  1.2     matt }
    178  1.2     matt 
    179  1.2     matt static void
    180  1.2     matt at91tctmr_attach(device_t parent, device_t self, void *aux)
    181  1.2     matt {
    182  1.2     matt     struct at91tctmr_softc *sc = device_private(self);
    183  1.2     matt     struct at91bus_attach_args *sa = aux;
    184  1.2     matt 
    185  1.2     matt     aprint_normal("\n");
    186  1.2     matt 
    187  1.2     matt     sc->sc_dev = self;
    188  1.2     matt     sc->sc_addr = (void*)sa->sa_addr;
    189  1.2     matt     sc->sc_pid = sa->sa_pid;
    190  1.2     matt 
    191  1.2     matt     if (at91tctmr_sc == NULL)
    192  1.2     matt         at91tctmr_sc = sc;
    193  1.2     matt 
    194  1.2     matt     at91_peripheral_clock(sc->sc_pid, 1);
    195  1.2     matt 
    196  1.2     matt     WRITE_TC(sc, TC_CCR, TC_CCR_CLKDIS);
    197  1.2     matt     WRITE_TC(sc, TC_IDR, -1);	/* make sure interrupts are disabled	*/
    198  1.2     matt 
    199  1.2     matt     /* find divider */
    200  1.7    skrll     uint32_t cmr = 0;
    201  1.2     matt     if (AT91_MSTCLK / 2U / HZ <= 65536) {
    202  1.2     matt       sc->sc_timerclock = AT91_MSTCLK / 2U;
    203  1.2     matt       cmr = TC_CMR_TCCLKS_MCK_DIV_2;
    204  1.2     matt     } else if (AT91_MSTCLK / 8U / HZ <= 65536) {
    205  1.2     matt       sc->sc_timerclock = AT91_MSTCLK / 8U;
    206  1.2     matt       cmr = TC_CMR_TCCLKS_MCK_DIV_8;
    207  1.2     matt     } else if (AT91_MSTCLK / 32U / HZ <= 65536) {
    208  1.2     matt       sc->sc_timerclock = AT91_MSTCLK / 32U;
    209  1.2     matt       cmr = TC_CMR_TCCLKS_MCK_DIV_32;
    210  1.2     matt     } else if (AT91_MSTCLK / 128U / HZ <= 65536) {
    211  1.2     matt       sc->sc_timerclock = AT91_MSTCLK / 128U;
    212  1.2     matt       cmr = TC_CMR_TCCLKS_MCK_DIV_128;
    213  1.2     matt     } else
    214  1.4     matt       panic("%s: cannot setup timer to reach HZ", device_xname(sc->sc_dev));
    215  1.2     matt 
    216  1.2     matt     sc->sc_divider = (sc->sc_timerclock + HZ - 1) / HZ; /* round up */
    217  1.2     matt     sc->sc_usec_per_tick = 1000000UL / (sc->sc_timerclock / sc->sc_divider);
    218  1.2     matt 
    219  1.2     matt     WRITE_TC(sc, TC_CMR, TC_CMR_WAVE | cmr | TC_CMR_WAVSEL_UP_RC);
    220  1.2     matt     WRITE_TC(sc, TC_CCR, TC_CCR_CLKEN);
    221  1.2     matt     WRITE_TC(sc, TC_RC,  sc->sc_divider - 1);
    222  1.2     matt     WRITE_TC(sc, TC_CCR, TC_CCR_SWTRG);
    223  1.2     matt 
    224  1.2     matt     sc->sc_initialized = 1;
    225  1.2     matt 
    226  1.2     matt     DPRINTF("%s: done, tclock=%"PRIu32" div=%"PRIu32" uspertick=%"PRIu32"\n", __FUNCTION__, sc->sc_timerclock, sc->sc_divider, sc->sc_usec_per_tick);
    227  1.2     matt 
    228  1.2     matt }
    229  1.2     matt 
    230  1.2     matt /*
    231  1.2     matt  * at91tctmr_intr:
    232  1.2     matt  *
    233  1.2     matt  *Handle the hardclock interrupt.
    234  1.2     matt  */
    235  1.2     matt static int
    236  1.2     matt at91tctmr_intr(void *arg)
    237  1.2     matt {
    238  1.2     matt     struct at91tctmr_softc *sc = arg;
    239  1.2     matt 
    240  1.2     matt     /* make sure it's the kernel timer that generated the interrupt  */
    241  1.2     matt     /* need to do this since the interrupt line is shared by the    */
    242  1.2     matt     /* other interval and PWM timers                                */
    243  1.2     matt     if (READ_TC(sc, TC_SR) & TC_SR_CPCS) {
    244  1.2     matt         /* call the kernel timer handler */
    245  1.2     matt         hardclock((struct clockframe*) arg);
    246  1.2     matt         return 1;
    247  1.2     matt     } else {
    248  1.2     matt         /* it's one of the other timers; just pass it on */
    249  1.2     matt         return 0;
    250  1.2     matt     }
    251  1.2     matt }
    252  1.2     matt 
    253  1.2     matt /*
    254  1.2     matt  * setstatclockrate:
    255  1.2     matt  *
    256  1.2     matt  *Set the rate of the statistics clock.
    257  1.2     matt  *
    258  1.2     matt  *We assume that hz is either stathz or profhz, and that neither
    259  1.2     matt  *will change after being set by cpu_initclocks().  We could
    260  1.2     matt  *recalculate the intervals here, but that would be a pain.
    261  1.2     matt  */
    262  1.2     matt void
    263  1.2     matt setstatclockrate(int hzz)
    264  1.2     matt {
    265  1.2     matt         /* use hardclock */
    266  1.2     matt 	(void)hzz;
    267  1.2     matt }
    268  1.2     matt 
    269  1.2     matt /*
    270  1.2     matt  * cpu_initclocks:
    271  1.2     matt  *
    272  1.2     matt  *Initialize the clock and get it going.
    273  1.2     matt  */
    274  1.2     matt static void udelay(unsigned int usec);
    275  1.2     matt 
    276  1.2     matt void
    277  1.2     matt cpu_initclocks(void)
    278  1.2     matt {
    279  1.2     matt     struct at91tctmr_softc *sc = at91tctmr_sc;
    280  1.2     matt 
    281  1.2     matt     if (!sc || !sc->sc_initialized)
    282  1.2     matt 	panic("%s: driver has not been initialized! (sc=%p)", __FUNCTION__, sc);
    283  1.2     matt 
    284  1.2     matt     hz = sc->sc_timerclock / sc->sc_divider;
    285  1.2     matt     stathz = profhz = 0;
    286  1.2     matt 
    287  1.2     matt     /* set up and enable interval timer 1 as kernel timer, */
    288  1.2     matt     /* using 32kHz clock source */
    289  1.2     matt 
    290  1.2     matt     /* register interrupt handler */
    291  1.2     matt     at91_intr_establish(sc->sc_pid, IPL_CLOCK, INTR_HIGH_LEVEL, at91tctmr_intr, sc);
    292  1.2     matt 
    293  1.2     matt     /* enable interrupts from timer */
    294  1.2     matt     WRITE_TC(sc, TC_IER, TC_SR_CPCS);
    295  1.2     matt }
    296  1.2     matt 
    297  1.2     matt 
    298  1.2     matt 
    299  1.2     matt 
    300  1.2     matt static void udelay(unsigned int usec)
    301  1.2     matt {
    302  1.2     matt     struct at91tctmr_softc *sc = at91tctmr_sc;
    303  1.7    skrll     uint32_t prev_cvr, cvr, divi = READ_TC(sc, TC_RC), diff;
    304  1.2     matt     int prev_ticks, ticks, ticks2;
    305  1.2     matt     unsigned footick = (sc->sc_timerclock * 64ULL / 1000000UL);
    306  1.2     matt 
    307  1.2     matt     if (usec > 0) {
    308  1.2     matt       prev_ticks = hardclock_ticks;
    309  1.2     matt       __insn_barrier();
    310  1.2     matt       prev_cvr = READ_TC(sc, TC_CV);
    311  1.2     matt       ticks = hardclock_ticks;
    312  1.2     matt       __insn_barrier();
    313  1.2     matt       if (ticks != prev_ticks) {
    314  1.2     matt 	prev_cvr = READ_TC(sc, TC_CV);
    315  1.2     matt 	prev_ticks = ticks;
    316  1.2     matt       }
    317  1.2     matt       for (;;) {
    318  1.2     matt 	ticks = hardclock_ticks;
    319  1.2     matt 	__insn_barrier();
    320  1.2     matt 	cvr = READ_TC(sc, TC_CV);
    321  1.2     matt 	ticks2 = hardclock_ticks;
    322  1.2     matt 	__insn_barrier();
    323  1.2     matt 	if (ticks2 != ticks) {
    324  1.2     matt 	  cvr = READ_TC(sc, TC_CV);
    325  1.2     matt 	}
    326  1.2     matt 	diff = (ticks2 - prev_ticks) * divi;
    327  1.2     matt 	if (cvr < prev_cvr) {
    328  1.2     matt 	  if (!diff)
    329  1.2     matt 	    diff = divi;
    330  1.2     matt 	  diff -= prev_cvr - cvr;
    331  1.2     matt 	} else
    332  1.2     matt 	  diff += cvr - prev_cvr;
    333  1.2     matt 	diff = diff * 64 / footick;
    334  1.2     matt 	if (diff) {
    335  1.2     matt 	  if (usec <= diff)
    336  1.2     matt 	    break;
    337  1.2     matt 	  prev_ticks = ticks2;
    338  1.2     matt 	  prev_cvr = (prev_cvr + footick * diff / 64) % divi;
    339  1.2     matt 	  usec -= diff;
    340  1.2     matt 	}
    341  1.2     matt       }
    342  1.2     matt     }
    343  1.2     matt }
    344  1.2     matt 
    345  1.2     matt 
    346  1.2     matt 
    347  1.2     matt /*
    348  1.2     matt  * delay:
    349  1.2     matt  *
    350  1.2     matt  *Delay for at least N microseconds. Note that due to our coarse clock,
    351  1.2     matt  *  our resolution is 61 us. But we round up so we'll wait at least as
    352  1.2     matt  *  long as requested.
    353  1.2     matt  */
    354  1.2     matt void
    355  1.2     matt delay(unsigned int usec)
    356  1.2     matt {
    357  1.2     matt     struct at91tctmr_softc *sc = at91tctmr_sc;
    358  1.2     matt 
    359  1.2     matt #ifdef DEBUG
    360  1.2     matt     if (sc == NULL) {
    361  1.2     matt         printf("delay: called before start at91tc\n");
    362  1.2     matt         return;
    363  1.2     matt     }
    364  1.2     matt #endif
    365  1.2     matt 
    366  1.2     matt     if (usec >= sc->sc_usec_per_tick) {
    367  1.2     matt         /* have more than 1 tick; just do in ticks */
    368  1.2     matt         unsigned int ticks = (usec + sc->sc_usec_per_tick - 1) / sc->sc_usec_per_tick;
    369  1.2     matt         while (ticks-- > 0) {
    370  1.2     matt 	  udelay(sc->sc_usec_per_tick);
    371  1.2     matt 	}
    372  1.2     matt     } else {
    373  1.2     matt         /* less than 1 tick; can do as usec */
    374  1.2     matt         udelay(usec);
    375  1.2     matt     }
    376  1.2     matt 
    377  1.2     matt }
    378  1.2     matt 
    379