2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
10 * This driver implements a draft-mogul-pps-api-02.txt PPS source.
12 * The input pin is pin#10
13 * The echo output pin is pin#14
17 #include <sys/cdefs.h>
18 __FBSDID("$FreeBSD$");
20 #include <sys/param.h>
22 #include <sys/kernel.h>
23 #include <sys/systm.h>
24 #include <sys/module.h>
28 #include <sys/timepps.h>
29 #include <machine/bus.h>
30 #include <machine/resource.h>
33 #include <dev/ppbus/ppbconf.h>
35 #include <dev/ppbus/ppbio.h>
37 #define PPS_NAME "pps" /* our official name */
39 #define PRVERBOSE(fmt, arg...) if (bootverbose) printf(fmt, ##arg);
42 struct ppb_device pps_dev;
43 struct pps_state pps[9];
48 struct callout timeout;
52 struct resource *intr_resource; /* interrupt resource */
53 void *intr_cookie; /* interrupt registration cookie */
56 static void ppsintr(void *arg);
57 static void ppshcpoll(void *arg);
59 #define DEVTOSOFTC(dev) \
60 ((struct pps_data *)device_get_softc(dev))
62 static devclass_t pps_devclass;
64 static d_open_t ppsopen;
65 static d_close_t ppsclose;
66 static d_ioctl_t ppsioctl;
68 static struct cdevsw pps_cdevsw = {
69 .d_version = D_VERSION,
77 ppsidentify(driver_t *driver, device_t parent)
82 dev = device_find_child(parent, PPS_NAME, -1);
84 BUS_ADD_CHILD(parent, 0, PPS_NAME, -1);
88 ppstry(device_t ppbus, int send, int expect)
92 ppb_wdtr(ppbus, send);
94 PRVERBOSE("S: %02x E: %02x G: %02x\n", send, expect, i);
99 ppsprobe(device_t ppsdev)
101 device_set_desc(ppsdev, "Pulse per second Timing Interface");
107 ppsattach(device_t dev)
109 struct pps_data *sc = DEVTOSOFTC(dev);
110 device_t ppbus = device_get_parent(dev);
112 int error, i, unit, rid = 0;
114 /* declare our interrupt handler */
115 sc->intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
118 /* interrupts seem mandatory */
119 if (sc->intr_resource == NULL) {
120 device_printf(dev, "Unable to allocate interrupt resource\n");
124 error = bus_setup_intr(dev, sc->intr_resource,
125 INTR_TYPE_TTY | INTR_MPSAFE, NULL, ppsintr,
126 sc, &sc->intr_cookie);
128 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->intr_resource);
129 device_printf(dev, "Unable to register interrupt handler\n");
133 sx_init(&sc->lock, "pps");
134 ppb_init_callout(ppbus, &sc->timeout, 0);
137 unit = device_get_unit(ppbus);
138 d = make_dev(&pps_cdevsw, unit,
139 UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%d", unit);
141 sc->pps[0].ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
143 d->si_drv2 = (void*)0;
144 pps_init(&sc->pps[0]);
147 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
153 i = ppb_set_mode(sc->ppbus, PPB_EPP);
154 PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
159 if (ppstry(ppbus, 0x00, 0x00))
161 if (ppstry(ppbus, 0x55, 0x55))
163 if (ppstry(ppbus, 0xaa, 0xaa))
165 if (ppstry(ppbus, 0xff, 0xff))
168 i = IRQENABLE | PCD | STROBE | nINIT | SELECTIN;
170 PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
171 if (ppstry(ppbus, 0x00, 0x00))
173 if (ppstry(ppbus, 0x55, 0x00))
175 if (ppstry(ppbus, 0xaa, 0x00))
177 if (ppstry(ppbus, 0xff, 0x00))
180 i = IRQENABLE | PCD | nINIT | SELECTIN;
182 PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
183 ppstry(ppbus, 0x00, 0xff);
184 ppstry(ppbus, 0x55, 0xff);
185 ppstry(ppbus, 0xaa, 0xff);
186 ppstry(ppbus, 0xff, 0xff);
189 for (i = 1; i < 9; i++) {
190 d = make_dev(&pps_cdevsw, unit + 0x10000 * i,
191 UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%db%d", unit, i - 1);
193 sc->pps[i].ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
195 d->si_drv2 = (void *)(intptr_t)i;
196 pps_init(&sc->pps[i]);
200 i = ppb_set_mode(sc->ppbus, PPB_COMPATIBLE);
201 ppb_release_bus(ppbus, dev);
208 ppsopen(struct cdev *dev, int flags, int fmt, struct thread *td)
210 struct pps_data *sc = dev->si_drv1;
211 device_t ppbus = sc->ppbus;
212 int subdev = (intptr_t)dev->si_drv2;
216 * The sx lock is here solely to serialize open()'s to close
217 * the race of concurrent open()'s when pps(4) doesn't own the
223 device_t ppsdev = sc->ppsdev;
225 if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR)) {
227 sx_xunlock(&sc->lock);
231 i = ppb_set_mode(sc->ppbus, PPB_PS2);
232 PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
234 i = IRQENABLE | PCD | nINIT | SELECTIN;
237 if (subdev > 0 && !(sc->busy & ~1)) {
238 /* XXX: Timeout of 1? hz/100 instead perhaps? */
239 callout_reset(&sc->timeout, 1, ppshcpoll, sc);
240 sc->lastdata = ppb_rdtr(sc->ppbus);
242 sc->busy |= (1 << subdev);
244 sx_xunlock(&sc->lock);
249 ppsclose(struct cdev *dev, int flags, int fmt, struct thread *td)
251 struct pps_data *sc = dev->si_drv1;
252 int subdev = (intptr_t)dev->si_drv2;
255 sc->pps[subdev].ppsparam.mode = 0; /* PHK ??? */
257 sc->busy &= ~(1 << subdev);
258 if (subdev > 0 && !(sc->busy & ~1))
259 callout_stop(&sc->timeout);
261 device_t ppsdev = sc->ppsdev;
262 device_t ppbus = sc->ppbus;
267 ppb_set_mode(ppbus, PPB_COMPATIBLE);
268 ppb_release_bus(ppbus, ppsdev);
270 ppb_unlock(sc->ppbus);
271 sx_xunlock(&sc->lock);
278 struct pps_data *sc = arg;
281 KASSERT(sc->busy & ~1, ("pps polling w/o opened devices"));
282 i = ppb_rdtr(sc->ppbus);
283 if (i == sc->lastdata)
285 l = sc->lastdata ^ i;
287 for (j = 1; j < 9; j ++) {
289 pps_capture(&sc->pps[j]);
290 pps_event(&sc->pps[j],
291 i & k ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR);
296 callout_reset(&sc->timeout, 1, ppshcpoll, sc);
302 struct pps_data *sc = (struct pps_data *)arg;
304 ppb_assert_locked(sc->ppbus);
305 pps_capture(&sc->pps[0]);
306 if (!(ppb_rstr(sc->ppbus) & nACK))
309 if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
310 ppb_wctr(sc->ppbus, IRQENABLE | AUTOFEED);
311 pps_event(&sc->pps[0], PPS_CAPTUREASSERT);
312 if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
313 ppb_wctr(sc->ppbus, IRQENABLE);
317 ppsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
319 struct pps_data *sc = dev->si_drv1;
320 int subdev = (intptr_t)dev->si_drv2;
324 err = pps_ioctl(cmd, data, &sc->pps[subdev]);
325 ppb_unlock(sc->ppbus);
329 static device_method_t pps_methods[] = {
330 /* device interface */
331 DEVMETHOD(device_identify, ppsidentify),
332 DEVMETHOD(device_probe, ppsprobe),
333 DEVMETHOD(device_attach, ppsattach),
338 static driver_t pps_driver = {
341 sizeof(struct pps_data),
343 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);
344 MODULE_DEPEND(pps, ppbus, 1, 1, 1);