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