xref: /openbsd/sys/arch/hppa/dev/clock.c (revision 610f49f8)
1 /*	$OpenBSD: clock.c,v 1.12 2002/02/01 19:03:18 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 <uvm/uvm_extern.h>
50 #include <machine/db_machdep.h>
51 #include <ddb/db_sym.h>
52 #include <ddb/db_extern.h>
53 #endif
54 
55 struct timeval time;
56 
57 void startrtclock __P((void));
58 
59 void
60 cpu_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 
70 int
71 clock_intr (v)
72 	void *v;
73 {
74 	struct trapframe *frame = v;
75 
76 	/* printf ("clock int 0x%x @ 0x%x for %p\n", t,
77 	   frame->tf_iioq_head, curproc); */
78 
79 	cpu_initclocks();
80 	hardclock(frame);
81 
82 #if 0
83 	ddb_regs = *frame;
84 	db_show_regs(NULL, 0, 0, NULL);
85 #endif
86 
87 	/* printf ("clock out 0x%x\n", t); */
88 
89 	return 1;
90 }
91 
92 
93 /*
94  * initialize the system time from the time of day clock
95  */
96 void
97 inittodr(t)
98 	time_t t;
99 {
100 	struct pdc_tod tod PDC_ALIGNMENT;
101 	int 	tbad = 0;
102 
103 	if (t < 5*SECYR) {
104 		printf ("WARNING: preposterous time in file system");
105 		t = 6*SECYR + 186*SECDAY + SECDAY/2;
106 		tbad = 1;
107 	}
108 
109 	pdc_call((iodcio_t)PAGE0->mem_pdc, 1, PDC_TOD, PDC_TOD_READ,
110 		&tod, 0, 0, 0, 0, 0);
111 
112 	time.tv_sec = tod.sec;
113 	time.tv_usec = tod.usec;
114 
115 	if (!tbad) {
116 		u_long	dt;
117 
118 		dt = (time.tv_sec < t)?  t - time.tv_sec : time.tv_sec - t;
119 
120 		if (dt < 2 * SECDAY)
121 			return;
122 		printf("WARNING: clock %s %ld days",
123 		    time.tv_sec < t? "lost" : "gained", dt / SECDAY);
124 	}
125 
126 	printf (" -- CHECK AND RESET THE DATE!\n");
127 }
128 
129 /*
130  * reset the time of day clock to the value in time
131  */
132 void
133 resettodr()
134 {
135 	struct pdc_tod tod PDC_ALIGNMENT;
136 
137 	tod.sec = time.tv_sec;
138 	tod.usec = time.tv_usec;
139 
140 	pdc_call((iodcio_t)PAGE0->mem_pdc, 1, PDC_TOD, PDC_TOD_WRITE, &tod);
141 }
142 
143 void
144 setstatclockrate(newhz)
145 	int newhz;
146 {
147 	/* nothing we can do */
148 }
149 
150