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