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 * ----------------------------------------------------------------------------
9 * $FreeBSD: src/sys/dev/ppbus/pps.c,v 1.24.2.1 2000/05/24 00:20:57 n_hibma Exp $
10 * $DragonFly: src/sys/dev/misc/pps/pps.c,v 1.14 2005/10/28 03:25:50 dillon Exp $
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
21 #include <sys/param.h>
22 #include <sys/kernel.h>
23 #include <sys/systm.h>
24 #include <sys/module.h>
27 #include <sys/timepps.h>
28 #include <machine/bus.h>
29 #include <machine/resource.h>
32 #include <bus/ppbus/ppbconf.h>
34 #include <bus/ppbus/ppbio.h>
36 #define PPS_NAME "pps" /* our official name */
40 struct ppb_device pps_dev;
43 struct resource *intr_resource; /* interrupt resource */
44 void *intr_cookie; /* interrupt registration cookie */
47 static void ppsintr(void *arg);
49 #define DEVTOSOFTC(dev) \
50 ((struct pps_data *)device_get_softc(dev))
51 #define UNITOSOFTC(unit) \
52 ((struct pps_data *)devclass_get_softc(pps_devclass, (unit)))
53 #define UNITODEVICE(unit) \
54 (devclass_get_device(pps_devclass, (unit)))
56 static devclass_t pps_devclass;
58 static d_open_t ppsopen;
59 static d_close_t ppsclose;
60 static d_ioctl_t ppsioctl;
63 static struct cdevsw pps_cdevsw = {
77 /* strategy */ nostrategy,
83 ppsprobe(device_t ppsdev)
87 sc = DEVTOSOFTC(ppsdev);
88 bzero(sc, sizeof(struct pps_data));
90 device_set_desc(ppsdev, "Pulse per second Timing Interface");
92 sc->pps.ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
98 ppsattach(device_t ppsdev)
100 struct pps_data *sc = DEVTOSOFTC(ppsdev);
101 device_t ppbus = device_get_parent(ppsdev);
106 /* retrieve the ppbus irq */
107 BUS_READ_IVAR(ppbus, ppsdev, PPBUS_IVAR_IRQ, &irq);
110 /* declare our interrupt handler */
111 sc->intr_resource = bus_alloc_resource(ppsdev, SYS_RES_IRQ,
112 &zero, irq, irq, 1, RF_SHAREABLE);
114 /* interrupts seem mandatory */
115 if (sc->intr_resource == 0)
118 unit = device_get_unit(ppsdev);
119 cdevsw_add(&pps_cdevsw, -1, unit);
120 make_dev(&pps_cdevsw, unit, UID_ROOT, GID_WHEEL, 0644,
121 PPS_NAME "%d", unit);
126 ppsopen(dev_t dev, int flags, int fmt, struct thread *td)
128 u_int unit = minor(dev);
129 struct pps_data *sc = UNITOSOFTC(unit);
130 device_t ppsdev = UNITODEVICE(unit);
131 device_t ppbus = device_get_parent(ppsdev);
135 if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
138 /* attach the interrupt handler */
139 if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource,
141 &sc->intr_cookie, NULL))) {
142 ppb_release_bus(ppbus, ppsdev);
147 ppb_wctr(ppbus, IRQENABLE);
155 ppsclose(dev_t dev, int flags, int fmt, struct thread *td)
157 u_int unit = minor(dev);
158 struct pps_data *sc = UNITOSOFTC(unit);
159 device_t ppsdev = UNITODEVICE(unit);
160 device_t ppbus = device_get_parent(ppsdev);
162 sc->pps.ppsparam.mode = 0; /* PHK ??? */
167 /* Note: the interrupt handler is automatically detached */
168 ppb_release_bus(ppbus, ppsdev);
176 device_t ppsdev = (device_t)arg;
177 device_t ppbus = device_get_parent(ppsdev);
178 struct pps_data *sc = DEVTOSOFTC(ppsdev);
181 count = sys_cputimer->count();
182 if (!(ppb_rstr(ppbus) & nACK))
184 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
185 ppb_wctr(ppbus, IRQENABLE | AUTOFEED);
186 pps_event(&sc->pps, count, PPS_CAPTUREASSERT);
187 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
188 ppb_wctr(ppbus, IRQENABLE);
192 ppsioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
194 u_int unit = minor(dev);
195 struct pps_data *sc = UNITOSOFTC(unit);
197 return (pps_ioctl(cmd, data, &sc->pps));
201 * Becuase pps is a static device under any attached ppbus, and not scanned
202 * by the ppbus, we need an identify function to create the device.
204 static device_method_t pps_methods[] = {
205 /* device interface */
206 DEVMETHOD(device_identify, bus_generic_identify),
207 DEVMETHOD(device_probe, ppsprobe),
208 DEVMETHOD(device_attach, ppsattach),
213 static driver_t pps_driver = {
216 sizeof(struct pps_data),
218 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);