clock.c revision 1.19 1 /* $NetBSD: clock.c,v 1.19 2011/09/16 16:24:01 reinoud 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.19 2011/09/16 16:24:01 reinoud 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_signal(int sig);
48 static unsigned 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 static struct clock_softc *clock_sc;
69
70 CFATTACH_DECL_NEW(clock, sizeof(clock_softc_t),
71 clock_match, clock_attach, NULL, NULL);
72
73 static int
74 clock_match(device_t parent, cfdata_t match, void *opaque)
75 {
76 struct thunkbus_attach_args *taa = opaque;
77
78 if (taa->taa_type != THUNKBUS_TYPE_CLOCK)
79 return 0;
80
81 return 1;
82 }
83
84 static void
85 clock_attach(device_t parent, device_t self, void *opaque)
86 {
87 static struct sigaction sa;
88 clock_softc_t *sc = device_private(self);
89 long tcres;
90
91 aprint_naive("\n");
92 aprint_normal("\n");
93
94 KASSERT(clock_sc == NULL);
95 clock_sc = sc;
96
97 sc->sc_dev = self;
98
99 sc->sc_todr.todr_gettime = clock_todr_gettime;
100 todr_attach(&sc->sc_todr);
101
102 memset(&sa, 0, sizeof(sa));
103 thunk_sigemptyset(&sa.sa_mask);
104 sa.sa_handler = clock_signal;
105 sa.sa_flags = SA_RESTART | SA_SIGINFO;
106 if (thunk_sigaction(SIGALRM, &sa, NULL) == -1)
107 panic("couldn't register SIGALRM handler : %d",
108 thunk_geterrno());
109
110 tcres = thunk_clock_getres_monotonic();
111 if (tcres > 0) {
112 clock_timecounter.tc_quality = 1000;
113 clock_timecounter.tc_frequency = 1000000000 / tcres;
114 }
115 tc_init(&clock_timecounter);
116 }
117
118 static void
119 clock_signal(int sig)
120 {
121 struct clockframe cf;
122
123 curcpu()->ci_idepth++;
124
125 spl_intr(IPL_SOFTCLOCK, (void (*)(void *)) hardclock, &cf);
126 // hardclock(&cf);
127
128 curcpu()->ci_idepth--;
129 }
130
131 static unsigned int
132 clock_getcounter(struct timecounter *tc)
133 {
134 return thunk_getcounter();
135 }
136
137 static int
138 clock_todr_gettime(struct todr_chip_handle *tch, struct timeval *tv)
139 {
140 struct thunk_timeval ttv;
141 int error;
142
143 error = thunk_gettimeofday(&ttv, NULL);
144 if (error)
145 return error;
146
147 tv->tv_sec = ttv.tv_sec;
148 tv->tv_usec = ttv.tv_usec;
149
150 return 0;
151 }
152