Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*$NetBSD: at91st.c,v 1.7 2020/07/03 16:23:02 maxv Exp $*/

/*
 * AT91RM9200 clock functions
 * Copyright (c) 2007, Embedtronics Oy
 * All rights reserved.
 *
 * Based on vx115_clk.c,
 * Copyright (c) 2006, Jon Sevy <jsevy@cs.drexel.edu>
 * 
 * Based on epclk.c
 * Copyright (c) 2004 Jesse Off
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Driver for the AT91RM9200 clock tick.
 * We use Timer 1 for the system clock
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: at91st.c,v 1.7 2020/07/03 16:23:02 maxv Exp $");

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/time.h>
#include <sys/device.h>

#include <dev/clock_subr.h>

#include <sys/bus.h>
#include <machine/intr.h>

#include <arm/cpufunc.h>
#include <arm/at91/at91reg.h>
#include <arm/at91/at91var.h>
#include <arm/at91/at91streg.h>

#include <opt_hz.h>     /* for HZ */


//#define DEBUG_CLK
#ifdef DEBUG_CLK
#define DPRINTF(fmt...)  printf(fmt)
#else
#define DPRINTF(fmt...)  
#endif


static int at91st_match(device_t, cfdata_t, void *);
static void at91st_attach(device_t, device_t, void *);

void rtcinit(void);

/* callback functions for intr_functions */
static int at91st_intr(void* arg);

struct at91st_softc {
	bus_space_tag_t	sc_iot;
	bus_space_handle_t sc_ioh;
	int		sc_pid;
	int		sc_initialized;
};

static struct at91st_softc *at91st_sc = NULL;
static struct timeval lasttv;

    
    
/* Match value for clock timer; running at 32.768kHz, want HZ ticks per second  */
/* BTW, we use HZ == 64 or HZ == 128 so have a nice divisor                 */
/* NOTE: don't change there without visiting the functions below which      */
/* convert between timer counts and microseconds                            */
#define AT91ST_DIVIDER	(AT91_SCLK / HZ)
#define USEC_PER_TICK	(1000000 / (AT91_SCLK / AT91ST_DIVIDER))

#if 0
static uint32_t at91st_count_to_usec(uint32_t count)
{
    uint32_t result;
    
    /* convert specified number of ticks to usec, and round up  */
    /* note that with 16 kHz tick rate, maximum count will be   */
    /* 256 (for HZ = 64), so we won't have overflow issues      */ 
    result = (1000000 * count) / AT91_SCLK;

    if ((result * AT91_SCLK) != (count * 1000000))
    {
        /* round up */
        result += 1;
    }
    
    return result;
}

/* This may only be called when overflow is avoided; typically, */
/* it will be used when usec < USEC_PER_TICK              */
static uint32_t usec_to_timer_count(uint32_t usec)
{
    uint32_t result;
    
    /* convert specified number of usec to timer ticks, and round up */
    result = (AT91_SCLK * usec) / 1000000;
    
    if ((result * 1000000) != (usec * AT91_SCLK))
    {
        /* round up */
        result += 1;
    }
    
    return result;
    
}
#endif

/* macros to simplify writing to the timer controller */
#define READ_ST(offset)	STREG(offset)
//bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset)
#define WRITE_ST(offset, value) do {	\
  STREG(offset) = (value);			\
} while (/*CONSTCOND*/0)
//bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, value)



CFATTACH_DECL_NEW(at91st, sizeof(struct at91st_softc), at91st_match, at91st_attach, NULL, NULL);



static int
at91st_match(device_t parent, cfdata_t match, void *aux)
{
    if (strcmp(match->cf_name, "at91st") == 0)
	return 2;
    return 0;
}

static void
at91st_attach(device_t parent, device_t self, void *aux)
{
    struct at91st_softc *sc = device_private(self);
    struct at91bus_attach_args *sa = aux;

    printf("\n");
    
    sc->sc_iot = sa->sa_iot;
    sc->sc_pid = sa->sa_pid;
    
#if 0
    DPRINTF("-> bus_space_map()\n");

    /* map bus space and get handle */
    if (bus_space_map(sc->sc_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh) != 0)
        panic("%s: Cannot map registers", device_xname(self));
#endif

    if (at91st_sc == NULL)
        at91st_sc = sc;

    at91_peripheral_clock(sc->sc_pid, 1);

    WRITE_ST(ST_IDR, -1);	/* make sure interrupts are disabled	*/

    /* set up and enable interval timer 1 as kernel timer, */
    /* using 32kHz clock source */
    WRITE_ST(ST_PIMR, AT91ST_DIVIDER);
    WRITE_ST(ST_RTMR, 1);

    sc->sc_initialized = 1;

    DPRINTF("%s: done\n", __FUNCTION__);

}

/*
 * at91st_intr:
 *
 *Handle the hardclock interrupt.
 */
static int
at91st_intr(void *arg)
{
//    struct at91st_softc *sc = at91st_sc;

    /* make sure it's the kernel timer that generated the interrupt  */
    /* need to do this since the interrupt line is shared by the    */
    /* other interval and PWM timers                                */
    if (READ_ST(ST_SR) & ST_SR_PITS)
    {
        /* call the kernel timer handler */
        hardclock((struct clockframe*) arg);
#if 0
        if (getticks() % (HZ * 10) == 0)
            printf("time %i sec\n", getticks()/HZ);
#endif
        return 1;
    }
    else
    {
        /* it's one of the other timers; just pass it on */
        return 0;
    }
   
}

/*
 * setstatclockrate:
 *
 *Set the rate of the statistics clock.
 *
 *We assume that hz is either stathz or profhz, and that neither
 *will change after being set by cpu_initclocks().  We could
 *recalculate the intervals here, but that would be a pain.
 */
void
setstatclockrate(int hzz)
{
        /* use hardclock */
	(void)hzz;
}

/*
 * cpu_initclocks:
 *
 *Initialize the clock and get it going.
 */
static void udelay(unsigned int usec);

void
cpu_initclocks(void)
{
    struct at91st_softc *sc = at91st_sc;

    if (!sc || !sc->sc_initialized)
	panic("%s: driver has not been initialized! (sc=%p)", __FUNCTION__, sc);

    stathz = profhz = 0;

    /* set up and enable interval timer 1 as kernel timer, */
    /* using 32kHz clock source */
    WRITE_ST(ST_PIMR, AT91ST_DIVIDER);

    /* register interrupt handler */
    at91_intr_establish(sc->sc_pid, IPL_CLOCK, INTR_HIGH_LEVEL, at91st_intr, NULL);

    /* enable interrupts from timer */
    WRITE_ST(ST_IER, ST_SR_PITS);
}




/*
 * microtime:
 *
 *Fill in the specified timeval struct with the current time
 *accurate to the microsecond.
 */
void
microtime(register struct timeval *tvp)
{
//    struct at91st_softc *sc = at91st_sc;
    u_int oldirqstate;
    u_int current_count;
    
#ifdef DEBUG
    if (at91st_sc == NULL) {
        printf("microtime: called before initialize at91st\n");
        tvp->tv_sec = 0;
        tvp->tv_usec = 0;
        return;
    }
#endif

    oldirqstate = disable_interrupts(I32_bit);
    
    /* get current timer count */
    current_count = READ_ST(ST_CRTR);

    /* Fill in the timeval struct. */
    *tvp = time;

#if 0
    /* Refine the usec field using current timer count */
    tvp->tv_usec += at91st_count_to_usec(AT91ST_DIVIDER - current_count);
    
    /* Make sure microseconds doesn't overflow. */
    while (__predict_false(tvp->tv_usec >= 1000000)) 
    {
        tvp->tv_usec -= 1000000;
        tvp->tv_sec++;
    }
#endif

    /* Make sure the time has advanced. */
    if (__predict_false(tvp->tv_sec == lasttv.tv_sec && tvp->tv_usec <= lasttv.tv_usec)) 
    {
        tvp->tv_usec = lasttv.tv_usec + 1;
        if (tvp->tv_usec >= 1000000) 
        {
            tvp->tv_usec -= 1000000;
            tvp->tv_sec++;
        }
    }

    lasttv = *tvp;
    
    restore_interrupts(oldirqstate);
}


#if 0
static void tdelay(unsigned int ticks)
{
    uint32_t   start, end, current;
    
    current = getticks();
    start = current;
    end = start + ticks;
    
    /* just loop for the specified number of ticks */
    while (current < end)
        current = getticks();
}
#endif

static void udelay(unsigned int usec)
{
//    struct at91st_softc *sc = at91st_sc;
    uint32_t crtv, t, diff;

    usec = (usec * 1000 + AT91_SCLK - 1) / AT91_SCLK + 1;

    for (crtv = READ_ST(ST_CRTR);;) {
      while (crtv == (t = READ_ST(ST_CRTR))) ;
      diff = (t - crtv) & ST_CRTR_CRTV;
      if (diff >= usec) {
	break;
      }
      crtv = t;
      usec -= diff;
    }
}



/*
 * delay:
 *
 *Delay for at least N microseconds. Note that due to our coarse clock,
 *  our resolution is 61 us. But we round up so we'll wait at least as
 *  long as requested.
 */
void
delay(unsigned int usec)
{

#ifdef DEBUG
    if (at91st_sc == NULL) {
        printf("delay: called before start at91st\n");
        return;
    }
#endif

    if (usec >= USEC_PER_TICK)
    {
        /* have more than 1 tick; just do in ticks */
        unsigned int ticks = usec / USEC_PER_TICK;
        if (ticks*USEC_PER_TICK != usec)
            ticks += 1;
        while (ticks-- > 0) {
	  udelay(USEC_PER_TICK);
	}
    }
    else
    {
        /* less than 1 tick; can do as usec */
        udelay(usec);
    }
    
}