2 * SPDX-License-Identifier: Beerware
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 * ----------------------------------------------------------------------------
12 * This driver implements a draft-mogul-pps-api-02.txt PPS source.
14 * The input pin is pin#10
15 * The echo output pin is pin#14
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
22 #include <sys/param.h>
24 #include <sys/kernel.h>
25 #include <sys/systm.h>
26 #include <sys/module.h>
30 #include <sys/timepps.h>
31 #include <machine/bus.h>
32 #include <machine/resource.h>
35 #include <dev/ppbus/ppbconf.h>
37 #include <dev/ppbus/ppbio.h>
39 #define PPS_NAME "pps" /* our official name */
41 #define PRVERBOSE(fmt, arg...) if (bootverbose) printf(fmt, ##arg);
44 struct ppb_device pps_dev;
45 struct pps_state pps[9];
50 struct callout timeout;
54 struct resource *intr_resource; /* interrupt resource */
55 void *intr_cookie; /* interrupt registration cookie */
58 static void ppsintr(void *arg);
59 static void ppshcpoll(void *arg);
61 #define DEVTOSOFTC(dev) \
62 ((struct pps_data *)device_get_softc(dev))
64 static devclass_t pps_devclass;
66 static d_open_t ppsopen;
67 static d_close_t ppsclose;
68 static d_ioctl_t ppsioctl;
70 static struct cdevsw pps_cdevsw = {
71 .d_version = D_VERSION,
79 ppsidentify(driver_t *driver, device_t parent)
84 dev = device_find_child(parent, PPS_NAME, -1);
86 BUS_ADD_CHILD(parent, 0, PPS_NAME, -1);
90 ppstry(device_t ppbus, int send, int expect)
94 ppb_wdtr(ppbus, send);
96 PRVERBOSE("S: %02x E: %02x G: %02x\n", send, expect, i);
101 ppsprobe(device_t ppsdev)
103 device_set_desc(ppsdev, "Pulse per second Timing Interface");
109 ppsattach(device_t dev)
111 struct pps_data *sc = DEVTOSOFTC(dev);
112 device_t ppbus = device_get_parent(dev);
114 int error, i, unit, rid = 0;
116 /* declare our interrupt handler */
117 sc->intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
120 /* interrupts seem mandatory */
121 if (sc->intr_resource == NULL) {
122 device_printf(dev, "Unable to allocate interrupt resource\n");
126 error = bus_setup_intr(dev, sc->intr_resource,
127 INTR_TYPE_TTY | INTR_MPSAFE, NULL, ppsintr,
128 sc, &sc->intr_cookie);
130 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->intr_resource);
131 device_printf(dev, "Unable to register interrupt handler\n");
135 sx_init(&sc->lock, "pps");
136 ppb_init_callout(ppbus, &sc->timeout, 0);
139 unit = device_get_unit(ppbus);
140 d = make_dev(&pps_cdevsw, unit,
141 UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%d", unit);
143 sc->pps[0].ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
145 d->si_drv2 = (void*)0;
146 pps_init(&sc->pps[0]);
149 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
155 i = ppb_set_mode(sc->ppbus, PPB_EPP);
156 PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
161 if (ppstry(ppbus, 0x00, 0x00))
163 if (ppstry(ppbus, 0x55, 0x55))
165 if (ppstry(ppbus, 0xaa, 0xaa))
167 if (ppstry(ppbus, 0xff, 0xff))
170 i = IRQENABLE | PCD | STROBE | nINIT | SELECTIN;
172 PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
173 if (ppstry(ppbus, 0x00, 0x00))
175 if (ppstry(ppbus, 0x55, 0x00))
177 if (ppstry(ppbus, 0xaa, 0x00))
179 if (ppstry(ppbus, 0xff, 0x00))
182 i = IRQENABLE | PCD | nINIT | SELECTIN;
184 PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
185 ppstry(ppbus, 0x00, 0xff);
186 ppstry(ppbus, 0x55, 0xff);
187 ppstry(ppbus, 0xaa, 0xff);
188 ppstry(ppbus, 0xff, 0xff);
191 for (i = 1; i < 9; i++) {
192 d = make_dev(&pps_cdevsw, unit + 0x10000 * i,
193 UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%db%d", unit, i - 1);
195 sc->pps[i].ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
197 d->si_drv2 = (void *)(intptr_t)i;
198 pps_init(&sc->pps[i]);
202 i = ppb_set_mode(sc->ppbus, PPB_COMPATIBLE);
203 ppb_release_bus(ppbus, dev);
210 ppsopen(struct cdev *dev, int flags, int fmt, struct thread *td)
212 struct pps_data *sc = dev->si_drv1;
213 device_t ppbus = sc->ppbus;
214 int subdev = (intptr_t)dev->si_drv2;
218 * The sx lock is here solely to serialize open()'s to close
219 * the race of concurrent open()'s when pps(4) doesn't own the
225 device_t ppsdev = sc->ppsdev;
227 if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR)) {
229 sx_xunlock(&sc->lock);
233 i = ppb_set_mode(sc->ppbus, PPB_PS2);
234 PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
236 i = IRQENABLE | PCD | nINIT | SELECTIN;
239 if (subdev > 0 && !(sc->busy & ~1)) {
240 /* XXX: Timeout of 1? hz/100 instead perhaps? */
241 callout_reset(&sc->timeout, 1, ppshcpoll, sc);
242 sc->lastdata = ppb_rdtr(sc->ppbus);
244 sc->busy |= (1 << subdev);
246 sx_xunlock(&sc->lock);
251 ppsclose(struct cdev *dev, int flags, int fmt, struct thread *td)
253 struct pps_data *sc = dev->si_drv1;
254 int subdev = (intptr_t)dev->si_drv2;
257 sc->pps[subdev].ppsparam.mode = 0; /* PHK ??? */
259 sc->busy &= ~(1 << subdev);
260 if (subdev > 0 && !(sc->busy & ~1))
261 callout_stop(&sc->timeout);
263 device_t ppsdev = sc->ppsdev;
264 device_t ppbus = sc->ppbus;
269 ppb_set_mode(ppbus, PPB_COMPATIBLE);
270 ppb_release_bus(ppbus, ppsdev);
272 ppb_unlock(sc->ppbus);
273 sx_xunlock(&sc->lock);
280 struct pps_data *sc = arg;
283 KASSERT(sc->busy & ~1, ("pps polling w/o opened devices"));
284 i = ppb_rdtr(sc->ppbus);
285 if (i == sc->lastdata)
287 l = sc->lastdata ^ i;
289 for (j = 1; j < 9; j ++) {
291 pps_capture(&sc->pps[j]);
292 pps_event(&sc->pps[j],
293 i & k ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR);
298 callout_reset(&sc->timeout, 1, ppshcpoll, sc);
304 struct pps_data *sc = (struct pps_data *)arg;
306 ppb_assert_locked(sc->ppbus);
307 pps_capture(&sc->pps[0]);
308 if (!(ppb_rstr(sc->ppbus) & nACK))
311 if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
312 ppb_wctr(sc->ppbus, IRQENABLE | AUTOFEED);
313 pps_event(&sc->pps[0], PPS_CAPTUREASSERT);
314 if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
315 ppb_wctr(sc->ppbus, IRQENABLE);
319 ppsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
321 struct pps_data *sc = dev->si_drv1;
322 int subdev = (intptr_t)dev->si_drv2;
326 err = pps_ioctl(cmd, data, &sc->pps[subdev]);
327 ppb_unlock(sc->ppbus);
331 static device_method_t pps_methods[] = {
332 /* device interface */
333 DEVMETHOD(device_identify, ppsidentify),
334 DEVMETHOD(device_probe, ppsprobe),
335 DEVMETHOD(device_attach, ppsattach),
340 static driver_t pps_driver = {
343 sizeof(struct pps_data),
345 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);
346 MODULE_DEPEND(pps, ppbus, 1, 1, 1);