1 1.10 cegger /* $NetBSD: ofwgencfg_clock.c,v 1.10 2009/03/18 10:22:24 cegger Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /* 4 1.1 thorpej * Copyright 1997 5 1.1 thorpej * Digital Equipment Corporation. All rights reserved. 6 1.1 thorpej * 7 1.1 thorpej * This software is furnished under license and may be used and 8 1.1 thorpej * copied only in accordance with the following terms and conditions. 9 1.1 thorpej * Subject to these conditions, you may download, copy, install, 10 1.1 thorpej * use, modify and distribute this software in source and/or binary 11 1.1 thorpej * form. No title or ownership is transferred hereby. 12 1.1 thorpej * 13 1.1 thorpej * 1) Any source code used, modified or distributed must reproduce 14 1.1 thorpej * and retain this copyright notice and list of conditions as 15 1.1 thorpej * they appear in the source file. 16 1.1 thorpej * 17 1.1 thorpej * 2) No right is granted to use any trade name, trademark, or logo of 18 1.1 thorpej * Digital Equipment Corporation. Neither the "Digital Equipment 19 1.1 thorpej * Corporation" name nor any trademark or logo of Digital Equipment 20 1.1 thorpej * Corporation may be used to endorse or promote products derived 21 1.1 thorpej * from this software without the prior written permission of 22 1.1 thorpej * Digital Equipment Corporation. 23 1.1 thorpej * 24 1.1 thorpej * 3) This software is provided "AS-IS" and any express or implied 25 1.1 thorpej * warranties, including but not limited to, any implied warranties 26 1.1 thorpej * of merchantability, fitness for a particular purpose, or 27 1.1 thorpej * non-infringement are disclaimed. In no event shall DIGITAL be 28 1.1 thorpej * liable for any damages whatsoever, and in particular, DIGITAL 29 1.1 thorpej * shall not be liable for special, indirect, consequential, or 30 1.1 thorpej * incidental damages or damages for lost profits, loss of 31 1.1 thorpej * revenue or loss of use, whether such damages arise in contract, 32 1.1 thorpej * negligence, tort, under statute, in equity, at law or otherwise, 33 1.1 thorpej * even if advised of the possibility of such damage. 34 1.1 thorpej */ 35 1.1 thorpej 36 1.1 thorpej /* Include header files */ 37 1.4 lukem 38 1.4 lukem #include <sys/cdefs.h> 39 1.10 cegger __KERNEL_RCSID(0, "$NetBSD: ofwgencfg_clock.c,v 1.10 2009/03/18 10:22:24 cegger Exp $"); 40 1.1 thorpej 41 1.1 thorpej #include <sys/types.h> 42 1.1 thorpej #include <sys/param.h> 43 1.1 thorpej #include <sys/systm.h> 44 1.1 thorpej #include <sys/kernel.h> 45 1.1 thorpej #include <sys/time.h> 46 1.1 thorpej 47 1.1 thorpej #include <machine/intr.h> 48 1.7 matt #include <machine/irqhandler.h> 49 1.1 thorpej #include <arm/cpufunc.h> 50 1.1 thorpej #include <machine/cpu.h> 51 1.1 thorpej #include <machine/ofw.h> 52 1.1 thorpej 53 1.1 thorpej static void *clockirq; 54 1.1 thorpej 55 1.1 thorpej 56 1.1 thorpej /* 57 1.1 thorpej * int clockhandler(struct clockframe *frame) 58 1.1 thorpej * 59 1.1 thorpej * Function called by timer 0 interrupts. This just calls 60 1.1 thorpej * hardclock(). Eventually the irqhandler can call hardclock() directly 61 1.1 thorpej * but for now we use this function so that we can debug IRQ's 62 1.1 thorpej */ 63 1.5 thorpej 64 1.5 thorpej static int 65 1.5 thorpej clockhandler(struct clockframe *frame) 66 1.1 thorpej { 67 1.1 thorpej 68 1.1 thorpej hardclock(frame); 69 1.1 thorpej return(0); /* Pass the interrupt on down the chain */ 70 1.1 thorpej } 71 1.1 thorpej 72 1.5 thorpej #if 0 73 1.1 thorpej /* 74 1.1 thorpej * int statclockhandler(struct clockframe *frame) 75 1.1 thorpej * 76 1.1 thorpej * Function called by timer 1 interrupts. This just calls 77 1.1 thorpej * statclock(). Eventually the irqhandler can call statclock() directly 78 1.1 thorpej * but for now we use this function so that we can debug IRQ's 79 1.1 thorpej */ 80 1.5 thorpej 81 1.1 thorpej int 82 1.9 dsl statclockhandler(struct clockframe *frame) 83 1.1 thorpej { 84 1.1 thorpej 85 1.1 thorpej statclock(frame); 86 1.1 thorpej return(0); /* Pass the interrupt on down the chain */ 87 1.1 thorpej } 88 1.5 thorpej #endif 89 1.1 thorpej 90 1.1 thorpej /* 91 1.1 thorpej * void setstatclockrate(int hz) 92 1.1 thorpej * 93 1.1 thorpej * Set the stat clock rate. The stat clock uses timer1 94 1.1 thorpej */ 95 1.1 thorpej 96 1.1 thorpej void 97 1.7 matt setstatclockrate(int arg) 98 1.1 thorpej { 99 1.1 thorpej #ifdef OFWGENCFG 100 1.1 thorpej printf("Not setting statclock: OFW generic has only one clock.\n"); 101 1.1 thorpej #endif 102 1.1 thorpej } 103 1.1 thorpej 104 1.1 thorpej 105 1.1 thorpej /* 106 1.1 thorpej * void cpu_initclocks(void) 107 1.1 thorpej * 108 1.1 thorpej * Initialise the clocks. 109 1.1 thorpej * This sets up the two timers in the IOMD and installs the IRQ handlers 110 1.1 thorpej * 111 1.1 thorpej * NOTE: Currently only timer 0 is setup and the IRQ handler is not installed 112 1.1 thorpej */ 113 1.1 thorpej 114 1.1 thorpej void 115 1.10 cegger cpu_initclocks(void) 116 1.1 thorpej { 117 1.1 thorpej /* 118 1.1 thorpej * Load timer 0 with count down value 119 1.1 thorpej * This timer generates 100Hz interrupts for the system clock 120 1.1 thorpej */ 121 1.1 thorpej 122 1.1 thorpej printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz); 123 1.1 thorpej 124 1.7 matt clockirq = intr_claim(IRQ_TIMER0, IPL_CLOCK, 125 1.7 matt (int (*)(void *))clockhandler, 0, "clock", "hard intr"); 126 1.1 thorpej if (clockirq == NULL) 127 1.3 provos panic("Cannot installer timer 0 IRQ handler"); 128 1.1 thorpej 129 1.1 thorpej /* Notify callback handler that it can start processing ticks. */ 130 1.1 thorpej ofw_handleticks = 1; 131 1.1 thorpej 132 1.1 thorpej if (stathz) { 133 1.1 thorpej printf("Not installing statclock: OFW generic has only one clock.\n"); 134 1.1 thorpej } 135 1.1 thorpej } 136 1.1 thorpej 137 1.1 thorpej /* 138 1.1 thorpej * Estimated loop for n microseconds 139 1.1 thorpej */ 140 1.1 thorpej 141 1.1 thorpej /* Need to re-write this to use the timers */ 142 1.1 thorpej 143 1.1 thorpej /* One day soon I will actually do this */ 144 1.1 thorpej 145 1.1 thorpej int delaycount = 50; 146 1.1 thorpej 147 1.1 thorpej void 148 1.9 dsl delay(u_int n) 149 1.1 thorpej { 150 1.1 thorpej u_int i; 151 1.1 thorpej 152 1.1 thorpej if (n == 0) return; 153 1.2 mycroft while (n-- > 0) { 154 1.1 thorpej if (cputype == CPU_ID_SA110) /* XXX - Seriously gross hack */ 155 1.1 thorpej for (i = delaycount; --i;); 156 1.1 thorpej else 157 1.1 thorpej for (i = 8; --i;); 158 1.1 thorpej } 159 1.1 thorpej } 160