Home | History | Annotate | Line # | Download | only in at91
at91st.c revision 1.1.20.1
      1 /*$NetBSD: at91st.c,v 1.1.20.1 2009/05/04 08:10:39 yamt Exp $*/
      2 
      3 /*
      4  * AT91RM9200 clock functions
      5  * Copyright (c) 2007, Embedtronics Oy
      6  * All rights reserved.
      7  *
      8  * Based on vx115_clk.c,
      9  * Copyright (c) 2006, Jon Sevy <jsevy (at) cs.drexel.edu>
     10  *
     11  * Based on epclk.c
     12  * Copyright (c) 2004 Jesse Off
     13  * All rights reserved.
     14  *
     15  * Redistribution and use in source and binary forms, with or without
     16  * modification, are permitted provided that the following conditions
     17  * are met:
     18  * 1. Redistributions of source code must retain the above copyright
     19  *    notice, this list of conditions and the following disclaimer.
     20  * 2. Redistributions in binary form must reproduce the above copyright
     21  *    notice, this list of conditions and the following disclaimer in the
     22  *    documentation and/or other materials provided with the distribution.
     23  * 3. All advertising materials mentioning features or use of this software
     24  *    must display the following acknowledgement:
     25  *This product includes software developed by the NetBSD
     26  *Foundation, Inc. and its contributors.
     27  * 4. Neither the name of The NetBSD Foundation nor the names of its
     28  *    contributors may be used to endorse or promote products derived
     29  *    from this software without specific prior written permission.
     30  *
     31  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     32  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     33  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     34  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     35  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     36  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     37  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     39  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  * POSSIBILITY OF SUCH DAMAGE.
     42  */
     43 
     44 /*
     45  * Driver for the AT91RM9200 clock tick.
     46  * We use Timer 1 for the system clock
     47  */
     48 
     49 #include <sys/cdefs.h>
     50 __KERNEL_RCSID(0, "$NetBSD: at91st.c,v 1.1.20.1 2009/05/04 08:10:39 yamt Exp $");
     51 
     52 #include <sys/types.h>
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/kernel.h>
     56 #include <sys/time.h>
     57 #include <sys/device.h>
     58 
     59 #include <dev/clock_subr.h>
     60 
     61 #include <machine/bus.h>
     62 #include <machine/intr.h>
     63 
     64 #include <arm/cpufunc.h>
     65 #include <arm/at91/at91reg.h>
     66 #include <arm/at91/at91var.h>
     67 #include <arm/at91/at91streg.h>
     68 
     69 #include <opt_hz.h>     /* for HZ */
     70 
     71 
     72 //#define DEBUG_CLK
     73 #ifdef DEBUG_CLK
     74 #define DPRINTF(fmt...)  printf(fmt)
     75 #else
     76 #define DPRINTF(fmt...)
     77 #endif
     78 
     79 
     80 static int at91st_match(device_t, cfdata_t, void *);
     81 static void at91st_attach(device_t, device_t, void *);
     82 
     83 void rtcinit(void);
     84 
     85 /* callback functions for intr_functions */
     86 static int at91st_intr(void* arg);
     87 
     88 struct at91st_softc {
     89 	struct device	sc_dev;
     90 	bus_space_tag_t	sc_iot;
     91 	bus_space_handle_t sc_ioh;
     92 	int		sc_pid;
     93 	int		sc_initialized;
     94 };
     95 
     96 static struct at91st_softc *at91st_sc = NULL;
     97 static struct timeval lasttv;
     98 
     99 
    100 
    101 /* Match value for clock timer; running at 32.768kHz, want HZ ticks per second  */
    102 /* BTW, we use HZ == 64 or HZ == 128 so have a nice divisor                 */
    103 /* NOTE: don't change there without visiting the functions below which      */
    104 /* convert between timer counts and microseconds                            */
    105 #define AT91ST_DIVIDER	(AT91_SCLK / HZ)
    106 #define USEC_PER_TICK	(1000000 / (AT91_SCLK / AT91ST_DIVIDER))
    107 
    108 #if 0
    109 static uint32_t at91st_count_to_usec(uint32_t count)
    110 {
    111     uint32_t result;
    112 
    113     /* convert specified number of ticks to usec, and round up  */
    114     /* note that with 16 kHz tick rate, maximum count will be   */
    115     /* 256 (for HZ = 64), so we won't have overflow issues      */
    116     result = (1000000 * count) / AT91_SCLK;
    117 
    118     if ((result * AT91_SCLK) != (count * 1000000))
    119     {
    120         /* round up */
    121         result += 1;
    122     }
    123 
    124     return result;
    125 }
    126 
    127 /* This may only be called when overflow is avoided; typically, */
    128 /* it will be used when usec < USEC_PER_TICK              */
    129 static uint32_t usec_to_timer_count(uint32_t usec)
    130 {
    131     uint32_t result;
    132 
    133     /* convert specified number of usec to timer ticks, and round up */
    134     result = (AT91_SCLK * usec) / 1000000;
    135 
    136     if ((result * 1000000) != (usec * AT91_SCLK))
    137     {
    138         /* round up */
    139         result += 1;
    140     }
    141 
    142     return result;
    143 
    144 }
    145 #endif
    146 
    147 /* macros to simplify writing to the timer controller */
    148 #define READ_ST(offset)	STREG(offset)
    149 //bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset)
    150 #define WRITE_ST(offset, value) do {	\
    151   STREG(offset) = (value);			\
    152 } while (/*CONSTCOND*/0)
    153 //bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, value)
    154 
    155 
    156 
    157 CFATTACH_DECL(at91st, sizeof(struct at91st_softc), at91st_match, at91st_attach, NULL, NULL);
    158 
    159 
    160 
    161 static int
    162 at91st_match(device_t parent, cfdata_t match, void *aux)
    163 {
    164     if (strcmp(match->cf_name, "at91st") == 0)
    165 	return 2;
    166     return 0;
    167 }
    168 
    169 static void
    170 at91st_attach(device_t parent, device_t self, void *aux)
    171 {
    172     struct at91st_softc *sc = (struct at91st_softc*) self;
    173     struct at91bus_attach_args *sa = (struct at91bus_attach_args*) aux;
    174 
    175     printf("\n");
    176 
    177     sc->sc_iot = sa->sa_iot;
    178     sc->sc_pid = sa->sa_pid;
    179 
    180 #if 0
    181     DPRINTF("-> bus_space_map()\n");
    182 
    183     /* map bus space and get handle */
    184     if (bus_space_map(sc->sc_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh) != 0)
    185         panic("%s: Cannot map registers", self->dv_xname);
    186 #endif
    187 
    188     if (at91st_sc == NULL)
    189         at91st_sc = sc;
    190 
    191     at91_peripheral_clock(sc->sc_pid, 1);
    192 
    193     WRITE_ST(ST_IDR, -1);	/* make sure interrupts are disabled	*/
    194 
    195     /* set up and enable interval timer 1 as kernel timer, */
    196     /* using 32kHz clock source */
    197     WRITE_ST(ST_PIMR, AT91ST_DIVIDER);
    198     WRITE_ST(ST_RTMR, 1);
    199 
    200     sc->sc_initialized = 1;
    201 
    202     DPRINTF("%s: done\n", __FUNCTION__);
    203 
    204 }
    205 
    206 /*
    207  * at91st_intr:
    208  *
    209  *Handle the hardclock interrupt.
    210  */
    211 static int
    212 at91st_intr(void *arg)
    213 {
    214 //    struct at91st_softc *sc = at91st_sc;
    215 
    216     /* make sure it's the kernel timer that generated the interrupt  */
    217     /* need to do this since the interrupt line is shared by the    */
    218     /* other interval and PWM timers                                */
    219     if (READ_ST(ST_SR) & ST_SR_PITS)
    220     {
    221         /* call the kernel timer handler */
    222         hardclock((struct clockframe*) arg);
    223 #if 0
    224         if (hardclock_ticks % (HZ * 10) == 0)
    225             printf("time %i sec\n", hardclock_ticks/HZ);
    226 #endif
    227         return 1;
    228     }
    229     else
    230     {
    231         /* it's one of the other timers; just pass it on */
    232         return 0;
    233     }
    234 
    235 }
    236 
    237 /*
    238  * setstatclockrate:
    239  *
    240  *Set the rate of the statistics clock.
    241  *
    242  *We assume that hz is either stathz or profhz, and that neither
    243  *will change after being set by cpu_initclocks().  We could
    244  *recalculate the intervals here, but that would be a pain.
    245  */
    246 void
    247 setstatclockrate(int hzz)
    248 {
    249         /* use hardclock */
    250 	(void)hzz;
    251 }
    252 
    253 /*
    254  * cpu_initclocks:
    255  *
    256  *Initialize the clock and get it going.
    257  */
    258 static void udelay(unsigned int usec);
    259 
    260 void
    261 cpu_initclocks(void)
    262 {
    263     struct at91st_softc *sc = at91st_sc;
    264 
    265     if (!sc || !sc->sc_initialized)
    266 	panic("%s: driver has not been initialized! (sc=%p)", __FUNCTION__, sc);
    267 
    268     stathz = profhz = 0;
    269 
    270     /* set up and enable interval timer 1 as kernel timer, */
    271     /* using 32kHz clock source */
    272     WRITE_ST(ST_PIMR, AT91ST_DIVIDER);
    273 
    274     /* register interrupt handler */
    275     at91_intr_establish(sc->sc_pid, IPL_CLOCK, INTR_HIGH_LEVEL, at91st_intr, NULL);
    276 
    277     /* enable interrupts from timer */
    278     WRITE_ST(ST_IER, ST_SR_PITS);
    279 }
    280 
    281 
    282 
    283 
    284 /*
    285  * microtime:
    286  *
    287  *Fill in the specified timeval struct with the current time
    288  *accurate to the microsecond.
    289  */
    290 void
    291 microtime(register struct timeval *tvp)
    292 {
    293 //    struct at91st_softc *sc = at91st_sc;
    294     u_int oldirqstate;
    295     u_int current_count;
    296 
    297 #ifdef DEBUG
    298     if (at91st_sc == NULL) {
    299         printf("microtime: called before initialize at91st\n");
    300         tvp->tv_sec = 0;
    301         tvp->tv_usec = 0;
    302         return;
    303     }
    304 #endif
    305 
    306     oldirqstate = disable_interrupts(I32_bit);
    307 
    308     /* get current timer count */
    309     current_count = READ_ST(ST_CRTR);
    310 
    311     /* Fill in the timeval struct. */
    312     *tvp = time;
    313 
    314 #if 0
    315     /* Refine the usec field using current timer count */
    316     tvp->tv_usec += at91st_count_to_usec(AT91ST_DIVIDER - current_count);
    317 
    318     /* Make sure microseconds doesn't overflow. */
    319     while (__predict_false(tvp->tv_usec >= 1000000))
    320     {
    321         tvp->tv_usec -= 1000000;
    322         tvp->tv_sec++;
    323     }
    324 #endif
    325 
    326     /* Make sure the time has advanced. */
    327     if (__predict_false(tvp->tv_sec == lasttv.tv_sec && tvp->tv_usec <= lasttv.tv_usec))
    328     {
    329         tvp->tv_usec = lasttv.tv_usec + 1;
    330         if (tvp->tv_usec >= 1000000)
    331         {
    332             tvp->tv_usec -= 1000000;
    333             tvp->tv_sec++;
    334         }
    335     }
    336 
    337     lasttv = *tvp;
    338 
    339     restore_interrupts(oldirqstate);
    340 }
    341 
    342 
    343 #if 0
    344 extern int hardclock_ticks;
    345 static void tdelay(unsigned int ticks)
    346 {
    347     u_int32_t   start, end, current;
    348 
    349     current = hardclock_ticks;
    350     start = current;
    351     end = start + ticks;
    352 
    353     /* just loop for the specified number of ticks */
    354     while (current < end)
    355         current = hardclock_ticks;
    356 }
    357 #endif
    358 
    359 static void udelay(unsigned int usec)
    360 {
    361 //    struct at91st_softc *sc = at91st_sc;
    362     u_int32_t crtv, t, diff;
    363 
    364     usec = (usec * 1000 + AT91_SCLK - 1) / AT91_SCLK + 1;
    365 
    366     for (crtv = READ_ST(ST_CRTR);;) {
    367       while (crtv == (t = READ_ST(ST_CRTR))) ;
    368       diff = (t - crtv) & ST_CRTR_CRTV;
    369       if (diff >= usec) {
    370 	break;
    371       }
    372       crtv = t;
    373       usec -= diff;
    374     }
    375 }
    376 
    377 
    378 
    379 /*
    380  * delay:
    381  *
    382  *Delay for at least N microseconds. Note that due to our coarse clock,
    383  *  our resolution is 61 us. But we round up so we'll wait at least as
    384  *  long as requested.
    385  */
    386 void
    387 delay(unsigned int usec)
    388 {
    389 
    390 #ifdef DEBUG
    391     if (at91st_sc == NULL) {
    392         printf("delay: called before start at91st\n");
    393         return;
    394     }
    395 #endif
    396 
    397     if (usec >= USEC_PER_TICK)
    398     {
    399         /* have more than 1 tick; just do in ticks */
    400         unsigned int ticks = usec / USEC_PER_TICK;
    401         if (ticks*USEC_PER_TICK != usec)
    402             ticks += 1;
    403         while (ticks-- > 0) {
    404 	  udelay(USEC_PER_TICK);
    405 	}
    406     }
    407     else
    408     {
    409         /* less than 1 tick; can do as usec */
    410         udelay(usec);
    411     }
    412 
    413 }
    414 
    415