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 $
11 * This driver implements a draft-mogul-pps-api-02.txt PPS source.
13 * The input pin is pin#10
14 * The echo output pin is pin#14
18 #include <sys/param.h>
19 #include <sys/kernel.h>
20 #include <sys/systm.h>
22 #include <sys/device.h>
23 #include <sys/module.h>
25 #include <sys/timepps.h>
28 #include <bus/ppbus/ppbconf.h>
29 #include <bus/ppbus/ppbio.h>
33 #define PPS_NAME "pps" /* our official name */
37 struct ppb_device pps_dev;
40 struct resource *intr_resource; /* interrupt resource */
41 void *intr_cookie; /* interrupt registration cookie */
44 static void ppsintr(void *arg);
46 #define DEVTOSOFTC(dev) \
47 ((struct pps_data *)device_get_softc(dev))
48 #define UNITOSOFTC(unit) \
49 ((struct pps_data *)devclass_get_softc(pps_devclass, (unit)))
50 #define UNITODEVICE(unit) \
51 (devclass_get_device(pps_devclass, (unit)))
53 static devclass_t pps_devclass;
55 static d_open_t ppsopen;
56 static d_close_t ppsclose;
57 static d_ioctl_t ppsioctl;
59 static struct dev_ops pps_ops = {
67 ppsprobe(device_t ppsdev)
71 sc = DEVTOSOFTC(ppsdev);
72 bzero(sc, sizeof(struct pps_data));
74 device_set_desc(ppsdev, "Pulse per second Timing Interface");
76 sc->pps.ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
82 ppsattach(device_t ppsdev)
84 struct pps_data *sc = DEVTOSOFTC(ppsdev);
85 device_t ppbus = device_get_parent(ppsdev);
90 /* retrieve the ppbus irq */
91 BUS_READ_IVAR(ppbus, ppsdev, PPBUS_IVAR_IRQ, &irq);
94 /* declare our interrupt handler */
95 sc->intr_resource = bus_alloc_resource(ppsdev, SYS_RES_IRQ,
96 &zero, irq, irq, 1, RF_SHAREABLE);
98 /* interrupts seem mandatory */
99 if (sc->intr_resource == 0)
102 unit = device_get_unit(ppsdev);
103 make_dev(&pps_ops, unit, UID_ROOT, GID_WHEEL,
104 0644, PPS_NAME "%d", unit);
109 ppsopen(struct dev_open_args *ap)
111 cdev_t dev = ap->a_head.a_dev;
112 u_int unit = minor(dev);
113 struct pps_data *sc = UNITOSOFTC(unit);
114 device_t ppsdev = UNITODEVICE(unit);
115 device_t ppbus = device_get_parent(ppsdev);
119 if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
122 /* attach the interrupt handler */
123 if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource,
125 &sc->intr_cookie, NULL))) {
126 ppb_release_bus(ppbus, ppsdev);
131 ppb_wctr(ppbus, IRQENABLE);
139 ppsclose(struct dev_close_args *ap)
141 cdev_t dev = ap->a_head.a_dev;
142 u_int unit = minor(dev);
143 struct pps_data *sc = UNITOSOFTC(unit);
144 device_t ppsdev = UNITODEVICE(unit);
145 device_t ppbus = device_get_parent(ppsdev);
147 sc->pps.ppsparam.mode = 0; /* PHK ??? */
152 /* Note: the interrupt handler is automatically detached */
153 ppb_release_bus(ppbus, ppsdev);
161 device_t ppsdev = (device_t)arg;
162 device_t ppbus = device_get_parent(ppsdev);
163 struct pps_data *sc = DEVTOSOFTC(ppsdev);
166 count = sys_cputimer->count();
167 if (!(ppb_rstr(ppbus) & nACK))
169 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
170 ppb_wctr(ppbus, IRQENABLE | AUTOFEED);
171 pps_event(&sc->pps, count, PPS_CAPTUREASSERT);
172 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
173 ppb_wctr(ppbus, IRQENABLE);
177 ppsioctl(struct dev_ioctl_args *ap)
179 cdev_t dev = ap->a_head.a_dev;
180 u_int unit = minor(dev);
181 struct pps_data *sc = UNITOSOFTC(unit);
183 return (pps_ioctl(ap->a_cmd, ap->a_data, &sc->pps));
187 * Becuase pps is a static device under any attached ppbus, and not scanned
188 * by the ppbus, we need an identify function to create the device.
190 static device_method_t pps_methods[] = {
191 /* device interface */
192 DEVMETHOD(device_identify, bus_generic_identify),
193 DEVMETHOD(device_probe, ppsprobe),
194 DEVMETHOD(device_attach, ppsattach),
199 static driver_t pps_driver = {
202 sizeof(struct pps_data),
204 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, NULL, NULL);