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