1 /* $NetBSD: clock.c,v 1.12 2010/04/24 19:58:13 dbj Exp $ */ 2 /* 3 * Copyright (c) 1998 Darrin B. Jewell 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.12 2010/04/24 19:58:13 dbj Exp $"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/kernel.h> 33 #include <sys/tty.h> 34 #include <sys/device.h> 35 36 #include <machine/psl.h> 37 #include <machine/bus.h> 38 #include <machine/cpu.h> 39 40 #include <next68k/dev/clockreg.h> 41 #include <next68k/dev/intiovar.h> 42 43 #include <next68k/next68k/rtc.h> 44 #include <next68k/next68k/isr.h> 45 46 /* @@@ This is pretty bogus and will need fixing once 47 * things are working better. 48 * -- jewell@mit.edu 49 */ 50 51 /* 52 * Note that the value of delay_divisor is roughly 53 * 2048 / cpuspeed (where cpuspeed is in MHz) on 68020 54 * and 68030 systems. See clock.c for the delay 55 * calibration algorithm. 56 */ 57 int cpuspeed; /* relative cpu speed; XXX skewed on 68040 */ 58 int delay_divisor = 2048/25; /* delay constant */ 59 60 /* 61 * Calibrate the delay constant. 62 */ 63 void 64 next68k_calibrate_delay(void) 65 { 66 extern int delay_divisor; 67 68 /* @@@ write this once we know how to read 69 * a real time clock 70 */ 71 72 /* 73 * Sanity check the delay_divisor value. If we totally lost, 74 * assume a 25MHz CPU; 75 */ 76 if (delay_divisor == 0) 77 delay_divisor = 2048 / 25; 78 79 /* Calculate CPU speed. */ 80 cpuspeed = 2048 / delay_divisor; 81 } 82 83 int clock_intr(void *); 84 85 int 86 clock_intr(void *arg) 87 { 88 volatile struct timer_reg *timer; 89 int whilecount = 0; 90 91 if (!INTR_OCCURRED(NEXT_I_TIMER)) { 92 return(0); 93 } 94 95 do { 96 static int in_hardclock = 0; 97 int s; 98 99 timer = (volatile struct timer_reg *)IIOV(NEXT_P_TIMER); 100 timer->csr |= TIMER_REG_UPDATE; 101 102 if (! in_hardclock) { 103 in_hardclock = 1; 104 s = splclock (); 105 hardclock(arg); 106 splx(s); 107 in_hardclock = 0; 108 } 109 if (whilecount++ > 10) 110 panic ("whilecount"); 111 } while (INTR_OCCURRED(NEXT_I_TIMER)); 112 return(1); 113 } 114 115 /* 116 * Set up the real-time and statistics clocks. Leave stathz 0 only 117 * if no alternative timer is available. 118 * 119 * The frequencies of these clocks must be an even number of microseconds. 120 */ 121 void 122 cpu_initclocks(void) 123 { 124 int s, cnt; 125 volatile struct timer_reg *timer; 126 127 rtc_init(); 128 hz = 100; 129 s = splclock(); 130 timer = (volatile struct timer_reg *)IIOV(NEXT_P_TIMER); 131 cnt = 1000000/hz; /* usec timer */ 132 timer->csr = 0; 133 timer->msb = (cnt >> 8); 134 timer->lsb = cnt; 135 timer->csr = TIMER_REG_ENABLE|TIMER_REG_UPDATE; 136 isrlink_autovec(clock_intr, NULL, NEXT_I_IPL(NEXT_I_TIMER), 0, NULL); 137 INTR_ENABLE(NEXT_I_TIMER); 138 splx(s); 139 } 140 141 142 void 143 setstatclockrate(int newhz) 144 { 145 146 /* XXX should we do something here? XXX */ 147 } 148 149 /* @@@ update this to use the usec timer 150 * Darrin B Jewell <jewell@mit.edu> Sun Feb 8 05:01:02 1998 151 * XXX: Well, there is no more microtime. But if there is a hardware 152 * timer of any sort, it could be added. ENODOCS. gdamore. 153 */ 154 155