1 /* $OpenBSD: clock.c,v 1.22 2007/07/22 19:24:45 kettenis Exp $ */ 2 3 /* 4 * Copyright (c) 1998-2003 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 26 * THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/kernel.h> 32 #include <sys/timetc.h> 33 34 #include <dev/clock_subr.h> 35 36 #include <machine/pdc.h> 37 #include <machine/iomod.h> 38 #include <machine/psl.h> 39 #include <machine/intr.h> 40 #include <machine/reg.h> 41 #include <machine/cpufunc.h> 42 #include <machine/autoconf.h> 43 44 #if defined(DDB) 45 #include <uvm/uvm_extern.h> 46 #include <machine/db_machdep.h> 47 #include <ddb/db_sym.h> 48 #include <ddb/db_extern.h> 49 #endif 50 51 u_int itmr_get_timecount(struct timecounter *); 52 53 struct timecounter itmr_timecounter = { 54 itmr_get_timecount, NULL, 0xffffffff, 0, "itmr", 0, NULL 55 }; 56 57 void 58 cpu_initclocks() 59 { 60 extern volatile u_long cpu_itmr; 61 extern u_long cpu_hzticks; 62 u_long __itmr; 63 64 itmr_timecounter.tc_frequency = PAGE0->mem_10msec * 100; 65 tc_init(&itmr_timecounter); 66 67 mfctl(CR_ITMR, __itmr); 68 cpu_itmr = __itmr; 69 __itmr += cpu_hzticks; 70 mtctl(__itmr, CR_ITMR); 71 } 72 73 /* 74 * initialize the system time from the time of day clock 75 */ 76 void 77 inittodr(t) 78 time_t t; 79 { 80 struct pdc_tod tod PDC_ALIGNMENT; 81 int error, tbad = 0; 82 struct timespec ts; 83 84 if (t < 12*SECYR) { 85 printf ("WARNING: preposterous time in file system"); 86 t = 6*SECYR + 186*SECDAY + SECDAY/2; 87 tbad = 1; 88 } 89 90 if ((error = pdc_call((iodcio_t)pdc, 91 1, PDC_TOD, PDC_TOD_READ, &tod, 0, 0, 0, 0, 0))) 92 printf("clock: failed to fetch (%d)\n", error); 93 94 ts.tv_sec = tod.sec; 95 ts.tv_nsec = tod.usec * 1000; 96 tc_setclock(&ts); 97 98 if (!tbad) { 99 u_long dt; 100 101 dt = (tod.sec < t)? t - tod.sec : tod.sec - t; 102 103 if (dt < 2 * SECDAY) 104 return; 105 printf("WARNING: clock %s %ld days", 106 tod.sec < t? "lost" : "gained", dt / SECDAY); 107 } 108 109 printf (" -- CHECK AND RESET THE DATE!\n"); 110 } 111 112 /* 113 * reset the time of day clock to the value in time 114 */ 115 void 116 resettodr() 117 { 118 struct timeval tv; 119 int error; 120 121 microtime(&tv); 122 123 if ((error = pdc_call((iodcio_t)pdc, 1, PDC_TOD, PDC_TOD_WRITE, 124 tv.tv_sec, tv.tv_usec))) 125 printf("clock: failed to save (%d)\n", error); 126 } 127 128 void 129 setstatclockrate(newhz) 130 int newhz; 131 { 132 /* nothing we can do */ 133 } 134 135 u_int 136 itmr_get_timecount(struct timecounter *tc) 137 { 138 u_long __itmr; 139 140 mfctl(CR_ITMR, __itmr); 141 return (__itmr); 142 } 143