1 /* $NetBSD: clock.c,v 1.26 2012/01/21 22:09:56 reinoud Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca> 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "opt_hz.h" 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.26 2012/01/21 22:09:56 reinoud Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/proc.h> 36 #include <sys/systm.h> 37 #include <sys/device.h> 38 #include <sys/lwp.h> 39 #include <sys/cpu.h> 40 #include <sys/malloc.h> 41 #include <sys/timetc.h> 42 #include <sys/time.h> 43 44 #include <machine/pcb.h> 45 #include <machine/mainbus.h> 46 #include <machine/thunk.h> 47 48 #include <dev/clock_subr.h> 49 50 static int clock_match(device_t, cfdata_t, void *); 51 static void clock_attach(device_t, device_t, void *); 52 53 static unsigned int clock_getcounter(struct timecounter *); 54 55 static int clock_todr_gettime(struct todr_chip_handle *, struct timeval *); 56 57 extern void setup_clock_intr(void); 58 void clock_intr(void *priv); 59 60 61 struct clock_softc { 62 device_t sc_dev; 63 struct todr_chip_handle sc_todr; 64 }; 65 66 static struct timecounter clock_timecounter = { 67 clock_getcounter, /* get_timecount */ 68 0, /* no poll_pps */ 69 ~0u, /* counter_mask */ 70 1000000000ULL, /* frequency */ 71 "CLOCK_MONOTONIC", /* name */ 72 -100, /* quality */ 73 NULL, /* prev */ 74 NULL, /* next */ 75 }; 76 77 timer_t clock_timerid; 78 int clock_running = 0; 79 80 CFATTACH_DECL_NEW(clock, sizeof(struct clock_softc), 81 clock_match, clock_attach, NULL, NULL); 82 83 static int 84 clock_match(device_t parent, cfdata_t match, void *opaque) 85 { 86 struct thunkbus_attach_args *taa = opaque; 87 88 if (taa->taa_type != THUNKBUS_TYPE_CLOCK) 89 return 0; 90 91 return 1; 92 } 93 94 static void 95 clock_attach(device_t parent, device_t self, void *opaque) 96 { 97 struct clock_softc *sc = device_private(self); 98 99 aprint_naive("\n"); 100 aprint_normal("\n"); 101 102 sc->sc_dev = self; 103 104 sc->sc_todr.todr_gettime = clock_todr_gettime; 105 todr_attach(&sc->sc_todr); 106 107 clock_timerid = thunk_timer_attach(); 108 clock_timecounter.tc_quality = 1000; 109 tc_init(&clock_timecounter); 110 111 setup_clock_intr(); 112 clock_running = 1; 113 } 114 115 void 116 clock_intr(void *priv) 117 { 118 struct clockframe cf; 119 int nticks = thunk_timer_getoverrun(clock_timerid) + 1; 120 121 while (nticks-- > 0) { 122 hardclock(&cf); 123 } 124 } 125 126 127 static unsigned int 128 clock_getcounter(struct timecounter *tc) 129 { 130 return thunk_getcounter(); 131 } 132 133 static int 134 clock_todr_gettime(struct todr_chip_handle *tch, struct timeval *tv) 135 { 136 struct thunk_timeval ttv; 137 int error; 138 139 error = thunk_gettimeofday(&ttv, NULL); 140 if (error) 141 return error; 142 143 tv->tv_sec = ttv.tv_sec; 144 tv->tv_usec = ttv.tv_usec; 145 146 return 0; 147 } 148