xref: /openbsd-src/sys/arch/hppa/dev/clock.c (revision 8500990981f885cbe5e6a4958549cacc238b5ae6)
1 /*	$OpenBSD: clock.c,v 1.19 2003/10/15 18:54:55 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 void
56 cpu_initclocks()
57 {
58 	extern volatile u_long cpu_itmr;
59 	extern u_long cpu_hzticks;
60 	u_long __itmr;
61 
62 	mfctl(CR_ITMR, __itmr);
63 	cpu_itmr = __itmr;
64 	__itmr += cpu_hzticks;
65 	mtctl(__itmr, CR_ITMR);
66 }
67 
68 /*
69  * initialize the system time from the time of day clock
70  */
71 void
72 inittodr(t)
73 	time_t t;
74 {
75 	struct pdc_tod tod PDC_ALIGNMENT;
76 	int 	error, tbad = 0;
77 
78 	if (t < 12*SECYR) {
79 		printf ("WARNING: preposterous time in file system");
80 		t = 6*SECYR + 186*SECDAY + SECDAY/2;
81 		tbad = 1;
82 	}
83 
84 	if ((error = pdc_call((iodcio_t)pdc,
85 	    1, PDC_TOD, PDC_TOD_READ, &tod, 0, 0, 0, 0, 0)))
86 		printf("clock: failed to fetch (%d)\n", error);
87 
88 	time.tv_sec = tod.sec;
89 	time.tv_usec = tod.usec;
90 
91 	if (!tbad) {
92 		u_long	dt;
93 
94 		dt = (time.tv_sec < t)?  t - time.tv_sec : time.tv_sec - t;
95 
96 		if (dt < 2 * SECDAY)
97 			return;
98 		printf("WARNING: clock %s %ld days",
99 		    time.tv_sec < t? "lost" : "gained", dt / SECDAY);
100 	}
101 
102 	printf (" -- CHECK AND RESET THE DATE!\n");
103 }
104 
105 /*
106  * reset the time of day clock to the value in time
107  */
108 void
109 resettodr()
110 {
111 	int error;
112 
113 	if ((error = pdc_call((iodcio_t)pdc, 1, PDC_TOD, PDC_TOD_WRITE,
114 	    time.tv_sec, time.tv_usec)))
115 		printf("clock: failed to save (%d)\n");
116 }
117 
118 void
119 setstatclockrate(newhz)
120 	int newhz;
121 {
122 	/* nothing we can do */
123 }
124