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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*	$NetBSD: clock.c,v 1.18 2018/09/03 16:29:27 riastradh Exp $	*/

/*
 * Copyright 1997
 * Digital Equipment Corporation. All rights reserved.
 *
 * This software is furnished under license and may be used and
 * copied only in accordance with the following terms and conditions.
 * Subject to these conditions, you may download, copy, install,
 * use, modify and distribute this software in source and/or binary
 * form. No title or ownership is transferred hereby.
 *
 * 1) Any source code used, modified or distributed must reproduce
 *    and retain this copyright notice and list of conditions as
 *    they appear in the source file.
 *
 * 2) No right is granted to use any trade name, trademark, or logo of
 *    Digital Equipment Corporation. Neither the "Digital Equipment
 *    Corporation" name nor any trademark or logo of Digital Equipment
 *    Corporation may be used to endorse or promote products derived
 *    from this software without the prior written permission of
 *    Digital Equipment Corporation.
 *
 * 3) This software is provided "AS-IS" and any express or implied
 *    warranties, including but not limited to, any implied warranties
 *    of merchantability, fitness for a particular purpose, or
 *    non-infringement are disclaimed. In no event shall DIGITAL be
 *    liable for any damages whatsoever, and in particular, DIGITAL
 *    shall not be liable for special, indirect, consequential, or
 *    incidental damages or damages for lost profits, loss of
 *    revenue or loss of use, whether such damages arise in contract,
 *    negligence, tort, under statute, in equity, at law or otherwise,
 *    even if advised of the possibility of such damage.
 */

/*-
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * William Jolitz and Don Ahn.
 *
 * 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.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
 *
 *	@(#)clock.c	7.2 (Berkeley) 5/12/91
 */
/*-
 * Copyright (c) 1993, 1994 Charles M. Hannum.
 *
 * This code is derived from software contributed to Berkeley by
 * William Jolitz and Don Ahn.
 *
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
 *
 *	@(#)clock.c	7.2 (Berkeley) 5/12/91
 */
/* 
 * Mach Operating System
 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
 * All Rights Reserved.
 * 
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 * 
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 * 
 * Carnegie Mellon requests users of this software to return to
 * 
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 * 
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */
/*
  Copyright 1988, 1989 by Intel Corporation, Santa Clara, California.

		All Rights Reserved

Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appears in all
copies and that both the copyright notice and this permission notice
appear in supporting documentation, and that the name of Intel
not be used in advertising or publicity pertaining to distribution
of the software without specific, written prior permission.

INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/*
 * Primitive clock interrupt routines.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.18 2018/09/03 16:29:27 riastradh Exp $");

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

#include <machine/cpu.h>
#include <machine/intr.h>
#include <machine/irqhandler.h>
#include <machine/pio.h>
#include <arm/cpufunc.h>

#include <dev/isa/isareg.h>
#include <dev/isa/isavar.h>
#include <dev/ic/mc146818reg.h>
#include <dev/ic/i8253reg.h>
#include <shark/isa/nvram.h>
#include <shark/isa/spkrreg.h>
#include <shark/shark/hat.h>

void	sysbeepstop(void *);
void	sysbeep(int, int);
void	rtcinit(void);
int     timer_hz_to_count(int);

static void findcpuspeed(void);
static void delayloop(int);
static int  clockintr(void *);
static int  gettick(void);
static void tc_init_i8253(void);

void startrtclock(void);

inline unsigned mc146818_read(void *, unsigned);
inline void mc146818_write(void *, unsigned, unsigned);

inline unsigned
mc146818_read(void *sc, unsigned reg)
{

	outb(IO_RTC, reg);
	return (inb(IO_RTC+1));
}

inline void
mc146818_write(void *sc, unsigned reg, unsigned datum)
{

	outb(IO_RTC, reg);
	outb(IO_RTC+1, datum);
}

unsigned int count1024usec; /* calibrated loop variable (1024 microseconds) */

/* number of timer ticks in a Musec = 2^20 usecs */
#define TIMER_MUSECFREQ\
    (((((((TIMER_FREQ) * 1024) + 999) / 1000) * 1024) + 999) / 1000)
#define TIMER_MUSECDIV(x) ((TIMER_MUSECFREQ+(x)/2)/(x))

/*
 * microtime() makes use of the following globals.
 * timer_msb_table[] and timer_lsb_table[] are used to compute the 
 * microsecond increment.
 *
 * time.tv_usec += isa_timer_msb_table[cnt_msb] + isa_timer_lsb_table[cnt_lsb];
 */

u_short	isa_timer_lsb_table[256];	/* timer->usec conversion for LSB */

/* 64 bit counts from timer 0 */
struct count64 {
	unsigned lo;   /* low 32 bits */
	unsigned hi;   /* high 32 bits */
};

#define TIMER0_ROLLOVER  0xFFFF  /* maximum rollover for 8254 counter */

struct count64 timer0count;
struct count64 timer0_at_last_clockintr;
unsigned       timer0last;

/*#define TESTHAT*/
#ifdef TESTHAT
#define HATSTACKSIZE 1024
#define HATHZ  50000
#define HATHZ2 10000
unsigned char hatStack[HATSTACKSIZE];

unsigned testHatOn = 0;
unsigned nHats = 0;
unsigned nHatWedges = 0;
unsigned fiqReason = 0;
unsigned hatCount = 0;
unsigned hatCount2 = 0;

void hatTest(int testReason)
{

	fiqReason |= testReason;
	nHats++;
}

void hatWedge(int nFIQs)
{

	printf("Unwedging the HAT.  fiqs_happened = %d\n", nFIQs);
	nHatWedges++;
}
#endif

void
startrtclock(void)
{

	findcpuspeed();		/* use the clock (while it's free) to
				   find the CPU speed */

	timer0count.lo = 0;
	timer0count.hi = 0;
	timer0_at_last_clockintr.lo = 0;
	timer0_at_last_clockintr.hi = 0;
	timer0last     = 0;

	/* initialize 8253 clock */
	outb(IO_TIMER1 + TIMER_MODE, TIMER_SEL0|TIMER_RATEGEN|TIMER_16BIT);
	outb(IO_TIMER1 + TIMER_CNTR0, TIMER0_ROLLOVER % 256);
	outb(IO_TIMER1 + TIMER_CNTR0, TIMER0_ROLLOVER / 256);

#ifdef TESTHAT
	hatCount = timer_hz_to_count(HATHZ);
	hatCount2 = timer_hz_to_count(HATHZ2);
	printf("HAT test on @ %d Hz = %d ticks\n", HATHZ, hatCount);
#endif
}

int
timer_hz_to_count(int timer_hz)
{
	u_long tval;

	tval = (TIMER_FREQ * 2) / (u_long) timer_hz;
	tval = (tval / 2) + (tval & 0x1);

	return (int)tval;
}

void gettimer0count(struct count64 *);

/* must be called at SPL_CLOCK or higher */
void gettimer0count(struct count64 *pcount)
{
	unsigned current, ticks, oldlo;

	/*
	 * Latch the current value of the timer and then read it.
	 * This guarantees an atomic reading of the time.
	 */

	current = gettick();

	if (timer0last >= current)
		ticks = timer0last - current;
	else
		ticks = timer0last + (TIMER0_ROLLOVER - current);

	timer0last = current;

	oldlo = timer0count.lo;

	if (oldlo > (timer0count.lo = oldlo + ticks)) /* carry? */
		timer0count.hi++;

	*pcount = timer0count;
}

static int
clockintr(void *arg)
{
	struct clockframe *frame = arg;		/* not strictly necessary */
	extern void isa_specific_eoi(int irq);
#ifdef TESTHAT
	static int ticks = 0;
#endif
	static int hatUnwedgeCtr = 0;

	gettimer0count(&timer0_at_last_clockintr);

	mc146818_read(NULL, MC_REGC); /* clear the clock interrupt */

	/* check to see if the high-availability timer needs to be unwedged */
	if (++hatUnwedgeCtr >= (hz / HAT_MIN_FREQ)) {
		hatUnwedgeCtr = 0;
		hatUnwedge();
	}

#ifdef TESTHAT
	++ticks;

	if (testHatOn && ((ticks & 0x3f) == 0)) {
		if (testHatOn == 1) {
			hatClkAdjust(hatCount2);
			testHatOn = 2;
		} else {
			testHatOn = 0;
			hatClkOff();
			printf("hat off status: %d %d %x\n", nHats,
			    nHatWedges, fiqReason);
		}
	} else if (!testHatOn && (ticks & 0x1ff) == 0) {
		printf("hat on status: %d %d %x\n",
		    nHats, nHatWedges, fiqReason);
		testHatOn = 1;
		nHats = 0;
		fiqReason = 0;
		hatClkOn(hatCount, hatTest, 0xfeedface,
		    hatStack + HATSTACKSIZE - sizeof(unsigned),
		    hatWedge);
	}
#endif
	hardclock(frame);
	return(1);
}

static int
gettick(void)
{
	u_char lo, hi;
	u_int savedints;

	/* Don't want someone screwing with the counter while we're here. */
	savedints = disable_interrupts(I32_bit);
	/* Select counter 0 and latch it. */
	outb(IO_TIMER1 + TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
	lo = inb(IO_TIMER1 + TIMER_CNTR0);
	hi = inb(IO_TIMER1 + TIMER_CNTR0);
	restore_interrupts(savedints);
	return ((hi << 8) | lo);
}

/* modifications from i386 to shark isa version:
   - removed hardcoded "n -=" values that approximated the time to
     calculate delay ticks
   - made the time to calculate delay ticks almost negligable. 4 multiplies
     = maximum of 12 cycles = 75ns on a slow SA-110, plus a bunch of shifts;
     as opposed to 4 multiplies plus a bunch of divides.
   - removed i386 assembly language hack
   - put code in findcpuspeed that works even if FIRST_GUESS is orders
     of magnitude low
   - put code in delay() to use delayloop() for short delays
   - microtime no longer in assembly language
*/

/*
 * Wait "n" microseconds.
 * Relies on timer 1 counting down from (TIMER_FREQ / hz) at TIMER_FREQ Hz.
 * Note: timer had better have been programmed before this is first used!
 * (Note that we use `rate generator' mode, which counts at 1:1; `square
 * wave' mode counts at 2:1).
 */
void
delay(unsigned n)
{
	int ticks, otick;
	int nticks;

	if (n < 100) {
		/* it can take a long time (1 usec or longer) just for
		   1 ISA read, so it's best not to use the timer for
		   short delays */
		delayloop((n * count1024usec) >> 10);
		return;
	}

	/*
	 * Read the counter first, so that the rest of the setup overhead is
	 * counted.
	 */
	otick = gettick();

	/*
	 * Calculate ((n * TIMER_FREQ) / 1e6) without using floating point and
	 * without any avoidable overflows.
	 */
	{
	        /* a Musec = 2^20 usec */
		int Musec = n >> 20,
		    usec = n & ((1 << 20) - 1);
		nticks 
		  = (Musec * TIMER_MUSECFREQ) +
		    (usec * (TIMER_MUSECFREQ >> 20)) +
		    ((usec * ((TIMER_MUSECFREQ & ((1 <<20) - 1)) >>10)) >>10) +
		    ((usec * (TIMER_MUSECFREQ & ((1 << 10) - 1))) >> 20);
	}

	while (nticks > 0) {
		ticks = gettick();
		if (ticks > otick)
			nticks -= TIMER0_ROLLOVER - (ticks - otick);
		else
			nticks -= otick - ticks;
		otick = ticks;
	}

}

void
sysbeepstop(void *arg)
{
}

void
sysbeep(int pitch, int period)
{
}

#define FIRST_GUESS   0x2000

static void
findcpuspeed(void)
{
	int ticks;
	unsigned int guess = FIRST_GUESS;

	while (1) { /* loop until accurate enough */
		/* Put counter in count down mode */
		outb(IO_TIMER1 + TIMER_MODE,
		    TIMER_SEL0 | TIMER_16BIT | TIMER_RATEGEN);
		outb(IO_TIMER1 + TIMER_CNTR0, 0xff);
		outb(IO_TIMER1 + TIMER_CNTR0, 0xff);
		delayloop(guess);

		/* Read the value left in the counter */
		/*
		 * Formula for delaycount is:
		 *  (loopcount * timer clock speed) / (counter ticks * 1000)
		 */
		ticks = 0xFFFF - gettick();
		if (ticks == 0) ticks = 1; /* just in case */
		if (ticks < (TIMER_MUSECDIV(1024))) { /* not accurate enough */
			guess *= uimax(2, (TIMER_MUSECDIV(1024) / ticks));
			continue;
		}
		count1024usec = (guess * (TIMER_MUSECDIV(1024))) / ticks;
		return;
	}
}

static void
delayloop(int counts)
{
	while (counts--)
		__insn_barrier();
}

void
cpu_initclocks(void)
{
        unsigned hzval;

	printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);

	/* install RTC interrupt handler */
	(void)isa_intr_establish(NULL, IRQ_RTC, IST_LEVEL, IPL_CLOCK, 
				 clockintr, 0);

	/* set  up periodic interrupt @ hz
	   this is the subset of hz values in kern_clock.c that are
	   supported by the ISA RTC */
	switch (hz) {
	case 64:
		hzval = MC_RATE_64_Hz;
		break;
	case 128:
		hzval = MC_RATE_128_Hz;
		break;
	case 256:
		hzval = MC_RATE_256_Hz;
		break;
	case 1024:
		hzval = MC_RATE_1024_Hz;
		break;
	default:
		panic("cannot configure hz = %d", hz);
        }
	
	rtcinit(); /* make sure basics are done by now */

	/* blast values to set up clock interrupt */
	mc146818_write(NULL, MC_REGA, MC_BASE_32_KHz | hzval);
	/* enable periodic interrupt */
	mc146818_write(NULL, MC_REGB,
		       mc146818_read(NULL, MC_REGB) | MC_REGB_PIE);

	tc_init_i8253();
}

void
rtcinit(void)
{
	static int first_rtcopen_ever = 1;

	if (!first_rtcopen_ever)
		return;
	first_rtcopen_ever = 0;

	mc146818_write(NULL, MC_REGA,			/* XXX softc */
	    MC_BASE_32_KHz | MC_RATE_1024_Hz);
	mc146818_write(NULL, MC_REGB, MC_REGB_24HR);	/* XXX softc */
}

void
setstatclockrate(int arg)
{
}

static uint32_t
i8253_get_timecount(struct timecounter *tc)
{
	return (TIMER0_ROLLOVER - gettick());
}

void
tc_init_i8253(void)
{
	static struct timecounter i8253_tc = {
		.tc_get_timecount = i8253_get_timecount,
		.tc_counter_mask = TIMER0_ROLLOVER,
		.tc_frequency = TIMER_FREQ,
		.tc_name = "i8253",
		.tc_quality = 100
	};

	tc_init(&i8253_tc);
}

/* End of clock.c */