clock.c revision 1.9 1 /* $NetBSD: clock.c,v 1.9 2011/08/23 14:37:50 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) 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 <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.9 2011/08/23 14:37:50 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/proc.h>
34 #include <sys/systm.h>
35 #include <sys/device.h>
36 #include <sys/timetc.h>
37 #include <sys/time.h>
38
39 #include <machine/mainbus.h>
40 #include <machine/thunk.h>
41
42 #include <dev/clock_subr.h>
43
44 static int clock_match(device_t, cfdata_t, void *);
45 static void clock_attach(device_t, device_t, void *);
46
47 static void clock_intr(int);
48 static u_int clock_getcounter(struct timecounter *);
49
50 static int clock_todr_gettime(struct todr_chip_handle *, struct timeval *);
51
52 typedef struct clock_softc {
53 device_t sc_dev;
54 struct todr_chip_handle sc_todr;
55 } clock_softc_t;
56
57 static struct timecounter clock_timecounter = {
58 clock_getcounter, /* get_timecount */
59 0, /* no poll_pps */
60 ~0u, /* counter_mask */
61 0, /* frequency */
62 "CLOCK_MONOTONIC", /* name */
63 -100, /* quality */
64 NULL, /* prev */
65 NULL, /* next */
66 };
67
68 CFATTACH_DECL_NEW(clock, sizeof(clock_softc_t),
69 clock_match, clock_attach, NULL, NULL);
70
71 static int
72 clock_match(device_t parent, cfdata_t match, void *opaque)
73 {
74 struct thunkbus_attach_args *taa = opaque;
75
76 if (taa->taa_type != THUNKBUS_TYPE_CLOCK)
77 return 0;
78
79 return 1;
80 }
81
82 static void
83 clock_attach(device_t parent, device_t self, void *opaque)
84 {
85 clock_softc_t *sc = device_private(self);
86 struct itimerval itimer;
87 long tcres;
88
89 aprint_naive("\n");
90 aprint_normal("\n");
91
92 sc->sc_dev = self;
93
94 sc->sc_todr.todr_gettime = clock_todr_gettime;
95 todr_attach(&sc->sc_todr);
96
97 (void)signal(SIGALRM, clock_intr);
98
99 itimer.it_interval.tv_sec = 0;
100 itimer.it_interval.tv_usec = 10000;
101 itimer.it_value = itimer.it_interval;
102 thunk_setitimer(ITIMER_REAL, &itimer, NULL);
103
104 tcres = thunk_clock_getres_monotonic();
105 if (tcres > 0) {
106 clock_timecounter.tc_quality = 1000;
107 clock_timecounter.tc_frequency = 1000000000 / tcres;
108 }
109 tc_init(&clock_timecounter);
110 }
111
112 static void
113 clock_intr(int notused)
114 {
115 extern int usermode_x;
116 struct clockframe cf;
117
118 curcpu()->ci_idepth++;
119
120 hardclock(&cf);
121
122 curcpu()->ci_idepth--;
123 }
124
125 static u_int
126 clock_getcounter(struct timecounter *tc)
127 {
128 struct timespec ts;
129
130 thunk_clock_gettime(CLOCK_MONOTONIC, &ts);
131 return ts.tv_nsec;
132 }
133
134 static int
135 clock_todr_gettime(struct todr_chip_handle *tch, struct timeval *tv)
136 {
137 return thunk_gettimeofday(tv, NULL);
138 }
139