clock.c revision 1.8
1/*	$OpenBSD: clock.c,v 1.8 2000/02/09 05:51:21 mickey Exp $	*/
2
3/*
4 * Copyright (c) 1998,1999 Michael Shalayeff
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Michael Shalayeff.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF MIND,
27 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/time.h>
37
38#include <dev/clock_subr.h>
39
40#include <machine/pdc.h>
41#include <machine/iomod.h>
42#include <machine/psl.h>
43#include <machine/intr.h>
44#include <machine/reg.h>
45#include <machine/cpufunc.h>
46#include <machine/autoconf.h>
47
48#if defined(DDB)
49#include <vm/vm.h>
50#include <machine/db_machdep.h>
51#include <ddb/db_sym.h>
52#include <ddb/db_extern.h>
53#endif
54
55struct timeval time;
56
57void startrtclock __P((void));
58
59void
60cpu_initclocks()
61{
62	extern u_int cpu_hzticks;
63	u_int time_inval;
64
65	/* Start the interval timer. */
66	mfctl(CR_ITMR, time_inval);
67	mtctl(time_inval + cpu_hzticks, CR_ITMR);
68}
69
70int
71clock_intr (v)
72	void *v;
73{
74	struct trapframe *frame = v;
75#ifdef USELEDS
76	static u_int hbcnt = 0;
77
78	if (!(hbcnt % (hz / 8)) && ((hbcnt / (hz / 8)) & 7) < 4)
79		ledctl(0, 0, PALED_HEARTBEAT);
80	hbcnt++;
81#endif
82
83	/* printf ("clock int 0x%x @ 0x%x for %p\n", t,
84	   frame->tf_iioq_head, curproc); */
85
86	cpu_initclocks();
87	hardclock(frame);
88
89#if 0
90	ddb_regs = *frame;
91	db_show_regs(NULL, 0, 0, NULL);
92#endif
93
94	/* printf ("clock out 0x%x\n", t); */
95
96	return 1;
97}
98
99
100/*
101 * initialize the system time from the time of day clock
102 */
103void
104inittodr(t)
105	time_t t;
106{
107	struct pdc_tod tod PDC_ALIGNMENT;
108	int 	tbad = 0;
109
110	if (t < 5*SECYR) {
111		printf ("WARNING: preposterous time in file system");
112		t = 6*SECYR + 186*SECDAY + SECDAY/2;
113		tbad = 1;
114	}
115
116	pdc_call((iodcio_t)PAGE0->mem_pdc, 1, PDC_TOD, PDC_TOD_READ,
117		&tod, 0, 0, 0, 0, 0);
118
119	time.tv_sec = tod.sec;
120	time.tv_usec = tod.usec;
121
122	if (!tbad) {
123		u_long	dt;
124
125		dt = (time.tv_sec < t)?  t - time.tv_sec : time.tv_sec - t;
126
127		if (dt < 2 * SECDAY)
128			return;
129		printf ("WARNING: clock %s %d days",
130			time.tv_sec < t? "lost" : "gained", dt / SECDAY);
131	}
132
133	printf (" -- CHECK AND RESET THE DATE!\n");
134}
135
136/*
137 * reset the time of day clock to the value in time
138 */
139void
140resettodr()
141{
142	static struct pdc_tod tod;
143
144	tod.sec = time.tv_sec;
145	tod.usec = time.tv_usec;
146
147	pdc_call((iodcio_t)PAGE0->mem_pdc, 1, PDC_TOD, PDC_TOD_WRITE, &tod);
148}
149
150void
151setstatclockrate(newhz)
152	int newhz;
153{
154	/* nothing we can do */
155}
156
157