pps_ppbus.c revision 1.1.4.2 1 1.1.4.2 skrll /* $NetBSD: pps_ppbus.c,v 1.1.4.2 2004/08/03 10:50:27 skrll Exp $ */
2 1.1.4.2 skrll
3 1.1.4.2 skrll /*
4 1.1.4.2 skrll * Copyright (c) 2004
5 1.1.4.2 skrll * Matthias Drochner. All rights reserved.
6 1.1.4.2 skrll *
7 1.1.4.2 skrll * Redistribution and use in source and binary forms, with or without
8 1.1.4.2 skrll * modification, are permitted provided that the following conditions
9 1.1.4.2 skrll * are met:
10 1.1.4.2 skrll * 1. Redistributions of source code must retain the above copyright
11 1.1.4.2 skrll * notice, this list of conditions, and the following disclaimer.
12 1.1.4.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.4.2 skrll * notice, this list of conditions and the following disclaimer in the
14 1.1.4.2 skrll * documentation and/or other materials provided with the distribution.
15 1.1.4.2 skrll *
16 1.1.4.2 skrll * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1.4.2 skrll * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1.4.2 skrll * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1.4.2 skrll * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1.4.2 skrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1.4.2 skrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1.4.2 skrll * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1.4.2 skrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1.4.2 skrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.4.2 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.4.2 skrll * SUCH DAMAGE.
27 1.1.4.2 skrll */
28 1.1.4.2 skrll
29 1.1.4.2 skrll #include <sys/cdefs.h>
30 1.1.4.2 skrll __KERNEL_RCSID(0, "$NetBSD: pps_ppbus.c,v 1.1.4.2 2004/08/03 10:50:27 skrll Exp $");
31 1.1.4.2 skrll
32 1.1.4.2 skrll #include "opt_ntp.h"
33 1.1.4.2 skrll
34 1.1.4.2 skrll #include <sys/param.h>
35 1.1.4.2 skrll #include <sys/systm.h>
36 1.1.4.2 skrll #include <sys/conf.h>
37 1.1.4.2 skrll #include <sys/proc.h>
38 1.1.4.2 skrll #include <sys/ioctl.h>
39 1.1.4.2 skrll #include <sys/timepps.h>
40 1.1.4.2 skrll
41 1.1.4.2 skrll #include <dev/ppbus/ppbus_base.h>
42 1.1.4.2 skrll #include <dev/ppbus/ppbus_device.h>
43 1.1.4.2 skrll #include <dev/ppbus/ppbus_io.h>
44 1.1.4.2 skrll #include <dev/ppbus/ppbus_var.h>
45 1.1.4.2 skrll
46 1.1.4.2 skrll struct pps_softc {
47 1.1.4.2 skrll struct ppbus_device_softc pps_dev;
48 1.1.4.2 skrll struct device *ppbus;
49 1.1.4.2 skrll int busy;
50 1.1.4.2 skrll pps_info_t ppsinfo;
51 1.1.4.2 skrll pps_params_t ppsparam;
52 1.1.4.2 skrll #ifdef PPS_SYNC
53 1.1.4.2 skrll int hardpps;
54 1.1.4.2 skrll #endif
55 1.1.4.2 skrll };
56 1.1.4.2 skrll
57 1.1.4.2 skrll static int pps_probe(struct device *, struct cfdata *, void *);
58 1.1.4.2 skrll static void pps_attach(struct device *, struct device *, void *);
59 1.1.4.2 skrll CFATTACH_DECL(pps, sizeof(struct pps_softc), pps_probe, pps_attach,
60 1.1.4.2 skrll NULL, NULL);
61 1.1.4.2 skrll extern struct cfdriver pps_cd;
62 1.1.4.2 skrll
63 1.1.4.2 skrll static dev_type_open(ppsopen);
64 1.1.4.2 skrll static dev_type_close(ppsclose);
65 1.1.4.2 skrll static dev_type_ioctl(ppsioctl);
66 1.1.4.2 skrll const struct cdevsw pps_cdevsw = {
67 1.1.4.2 skrll ppsopen, ppsclose, noread, nowrite, ppsioctl,
68 1.1.4.2 skrll nostop, notty, nopoll, nommap, nokqfilter
69 1.1.4.2 skrll };
70 1.1.4.2 skrll
71 1.1.4.2 skrll static void ppsintr(void *arg);
72 1.1.4.2 skrll
73 1.1.4.2 skrll static int ppscap = PPS_TSFMT_TSPEC | PPS_CAPTUREASSERT | PPS_OFFSETASSERT;
74 1.1.4.2 skrll
75 1.1.4.2 skrll static int
76 1.1.4.2 skrll pps_probe(struct device *parent, struct cfdata *match, void *aux)
77 1.1.4.2 skrll {
78 1.1.4.2 skrll struct ppbus_attach_args *args = aux;
79 1.1.4.2 skrll
80 1.1.4.2 skrll /* we need an interrupt */
81 1.1.4.2 skrll if (!(args->capabilities & PPBUS_HAS_INTR))
82 1.1.4.2 skrll return 0;
83 1.1.4.2 skrll
84 1.1.4.2 skrll return 1;
85 1.1.4.2 skrll }
86 1.1.4.2 skrll
87 1.1.4.2 skrll static void
88 1.1.4.2 skrll pps_attach(struct device *parent, struct device *self, void *aux)
89 1.1.4.2 skrll {
90 1.1.4.2 skrll struct pps_softc *sc = (struct pps_softc *)self;
91 1.1.4.2 skrll
92 1.1.4.2 skrll sc->ppbus = parent;
93 1.1.4.2 skrll
94 1.1.4.2 skrll printf("\n");
95 1.1.4.2 skrll }
96 1.1.4.2 skrll
97 1.1.4.2 skrll static int
98 1.1.4.2 skrll ppsopen(dev_t dev, int flags, int fmt, struct proc *p)
99 1.1.4.2 skrll {
100 1.1.4.2 skrll struct pps_softc *sc;
101 1.1.4.2 skrll int res;
102 1.1.4.2 skrll
103 1.1.4.2 skrll sc = device_lookup(&pps_cd, minor(dev));
104 1.1.4.2 skrll if (!sc)
105 1.1.4.2 skrll return (ENXIO);
106 1.1.4.2 skrll
107 1.1.4.2 skrll if (sc->busy)
108 1.1.4.2 skrll return (0);
109 1.1.4.2 skrll
110 1.1.4.2 skrll if (ppbus_request_bus(sc->ppbus, &sc->pps_dev.sc_dev,
111 1.1.4.2 skrll PPBUS_WAIT|PPBUS_INTR, 0))
112 1.1.4.2 skrll return (EINTR);
113 1.1.4.2 skrll
114 1.1.4.2 skrll /* attach the interrupt handler */
115 1.1.4.2 skrll /* XXX priority should be set here */
116 1.1.4.2 skrll res = ppbus_add_handler(sc->ppbus, ppsintr, sc);
117 1.1.4.2 skrll if (res) {
118 1.1.4.2 skrll ppbus_release_bus(sc->ppbus, &sc->pps_dev.sc_dev,
119 1.1.4.2 skrll PPBUS_WAIT, 0);
120 1.1.4.2 skrll return (res);
121 1.1.4.2 skrll }
122 1.1.4.2 skrll
123 1.1.4.2 skrll ppbus_set_mode(sc->ppbus, PPBUS_PS2, 0);
124 1.1.4.2 skrll ppbus_wctr(sc->ppbus, IRQENABLE | PCD | nINIT | SELECTIN);
125 1.1.4.2 skrll
126 1.1.4.2 skrll sc->busy = 1;
127 1.1.4.2 skrll return (0);
128 1.1.4.2 skrll }
129 1.1.4.2 skrll
130 1.1.4.2 skrll static int
131 1.1.4.2 skrll ppsclose(dev_t dev, int flags, int fmt, struct proc *p)
132 1.1.4.2 skrll {
133 1.1.4.2 skrll struct pps_softc *sc = device_lookup(&pps_cd, minor(dev));
134 1.1.4.2 skrll struct device *ppbus = sc->ppbus;
135 1.1.4.2 skrll
136 1.1.4.2 skrll sc->ppsparam.mode = 0;
137 1.1.4.2 skrll sc->busy = 0;
138 1.1.4.2 skrll #ifdef PPS_SYNC
139 1.1.4.2 skrll sc->hardpps = 0;
140 1.1.4.2 skrll #endif
141 1.1.4.2 skrll ppbus_wdtr(ppbus, 0);
142 1.1.4.2 skrll ppbus_wctr(ppbus, 0);
143 1.1.4.2 skrll
144 1.1.4.2 skrll ppbus_remove_handler(ppbus, ppsintr);
145 1.1.4.2 skrll ppbus_set_mode(ppbus, PPBUS_COMPATIBLE, 0);
146 1.1.4.2 skrll ppbus_release_bus(ppbus, &sc->pps_dev.sc_dev, PPBUS_WAIT, 0);
147 1.1.4.2 skrll return (0);
148 1.1.4.2 skrll }
149 1.1.4.2 skrll
150 1.1.4.2 skrll static void
151 1.1.4.2 skrll ppsintr(void *arg)
152 1.1.4.2 skrll {
153 1.1.4.2 skrll struct pps_softc *sc = arg;
154 1.1.4.2 skrll struct device *ppbus = sc->ppbus;
155 1.1.4.2 skrll struct timeval tv;
156 1.1.4.2 skrll
157 1.1.4.2 skrll if (!(ppbus_rstr(ppbus) & nACK))
158 1.1.4.2 skrll return;
159 1.1.4.2 skrll
160 1.1.4.2 skrll microtime(&tv);
161 1.1.4.2 skrll TIMEVAL_TO_TIMESPEC(&tv, &sc->ppsinfo.assert_timestamp);
162 1.1.4.2 skrll if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
163 1.1.4.2 skrll timespecadd(&sc->ppsinfo.assert_timestamp,
164 1.1.4.2 skrll &sc->ppsparam.assert_offset,
165 1.1.4.2 skrll &sc->ppsinfo.assert_timestamp);
166 1.1.4.2 skrll }
167 1.1.4.2 skrll #ifdef PPS_SYNC
168 1.1.4.2 skrll if (sc->hardpps)
169 1.1.4.2 skrll hardpps(&tv, tv.tv_usec);
170 1.1.4.2 skrll #endif
171 1.1.4.2 skrll sc->ppsinfo.assert_sequence++;
172 1.1.4.2 skrll sc->ppsinfo.current_mode = sc->ppsparam.mode;
173 1.1.4.2 skrll }
174 1.1.4.2 skrll
175 1.1.4.2 skrll static int
176 1.1.4.2 skrll ppsioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
177 1.1.4.2 skrll {
178 1.1.4.2 skrll struct pps_softc *sc = device_lookup(&pps_cd, minor(dev));
179 1.1.4.2 skrll int error = 0;
180 1.1.4.2 skrll
181 1.1.4.2 skrll switch (cmd) {
182 1.1.4.2 skrll case PPS_IOC_CREATE:
183 1.1.4.2 skrll break;
184 1.1.4.2 skrll
185 1.1.4.2 skrll case PPS_IOC_DESTROY:
186 1.1.4.2 skrll break;
187 1.1.4.2 skrll
188 1.1.4.2 skrll case PPS_IOC_GETPARAMS: {
189 1.1.4.2 skrll pps_params_t *pp;
190 1.1.4.2 skrll pp = (pps_params_t *)data;
191 1.1.4.2 skrll *pp = sc->ppsparam;
192 1.1.4.2 skrll break;
193 1.1.4.2 skrll }
194 1.1.4.2 skrll
195 1.1.4.2 skrll case PPS_IOC_SETPARAMS: {
196 1.1.4.2 skrll pps_params_t *pp;
197 1.1.4.2 skrll pp = (pps_params_t *)data;
198 1.1.4.2 skrll if (pp->mode & ~(ppscap)) {
199 1.1.4.2 skrll error = EINVAL;
200 1.1.4.2 skrll break;
201 1.1.4.2 skrll }
202 1.1.4.2 skrll sc->ppsparam = *pp;
203 1.1.4.2 skrll break;
204 1.1.4.2 skrll }
205 1.1.4.2 skrll
206 1.1.4.2 skrll case PPS_IOC_GETCAP:
207 1.1.4.2 skrll *(int*)data = ppscap;
208 1.1.4.2 skrll break;
209 1.1.4.2 skrll
210 1.1.4.2 skrll case PPS_IOC_FETCH: {
211 1.1.4.2 skrll pps_info_t *pi;
212 1.1.4.2 skrll pi = (pps_info_t *)data;
213 1.1.4.2 skrll *pi = sc->ppsinfo;
214 1.1.4.2 skrll break;
215 1.1.4.2 skrll }
216 1.1.4.2 skrll
217 1.1.4.2 skrll #ifdef PPS_SYNC
218 1.1.4.2 skrll case PPS_IOC_KCBIND:
219 1.1.4.2 skrll if (*(int *)data & PPS_CAPTUREASSERT)
220 1.1.4.2 skrll sc->hardpps = 1;
221 1.1.4.2 skrll else
222 1.1.4.2 skrll sc->hardpps = 0;
223 1.1.4.2 skrll break;
224 1.1.4.2 skrll #endif
225 1.1.4.2 skrll
226 1.1.4.2 skrll default:
227 1.1.4.2 skrll error = EPASSTHROUGH;
228 1.1.4.2 skrll break;
229 1.1.4.2 skrll }
230 1.1.4.2 skrll return (error);
231 1.1.4.2 skrll }
232