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.12 2005/06/01 17:43:44 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 ppsidentify(driver_t *driver, device_t parent)
86 BUS_ADD_CHILD(parent, 0, PPS_NAME, 0);
90 ppsprobe(device_t ppsdev)
94 sc = DEVTOSOFTC(ppsdev);
95 bzero(sc, sizeof(struct pps_data));
97 device_set_desc(ppsdev, "Pulse per second Timing Interface");
99 sc->pps.ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
105 ppsattach(device_t ppsdev)
107 struct pps_data *sc = DEVTOSOFTC(ppsdev);
108 device_t ppbus = device_get_parent(ppsdev);
113 /* retrieve the ppbus irq */
114 BUS_READ_IVAR(ppbus, ppsdev, PPBUS_IVAR_IRQ, &irq);
117 /* declare our interrupt handler */
118 sc->intr_resource = bus_alloc_resource(ppsdev, SYS_RES_IRQ,
119 &zero, irq, irq, 1, RF_SHAREABLE);
121 /* interrupts seem mandatory */
122 if (sc->intr_resource == 0)
125 unit = device_get_unit(ppsdev);
126 cdevsw_add(&pps_cdevsw, -1, unit);
127 make_dev(&pps_cdevsw, unit, UID_ROOT, GID_WHEEL, 0644,
128 PPS_NAME "%d", unit);
133 ppsopen(dev_t dev, int flags, int fmt, struct thread *td)
135 u_int unit = minor(dev);
136 struct pps_data *sc = UNITOSOFTC(unit);
137 device_t ppsdev = UNITODEVICE(unit);
138 device_t ppbus = device_get_parent(ppsdev);
142 if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
145 /* attach the interrupt handler */
146 if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource,
147 INTR_TYPE_TTY, ppsintr, ppsdev,
148 &sc->intr_cookie, NULL))) {
149 ppb_release_bus(ppbus, ppsdev);
154 ppb_wctr(ppbus, IRQENABLE);
162 ppsclose(dev_t dev, int flags, int fmt, struct thread *td)
164 u_int unit = minor(dev);
165 struct pps_data *sc = UNITOSOFTC(unit);
166 device_t ppsdev = UNITODEVICE(unit);
167 device_t ppbus = device_get_parent(ppsdev);
169 sc->pps.ppsparam.mode = 0; /* PHK ??? */
174 /* Note: the interrupt handler is automatically detached */
175 ppb_release_bus(ppbus, ppsdev);
183 device_t ppsdev = (device_t)arg;
184 device_t ppbus = device_get_parent(ppsdev);
185 struct pps_data *sc = DEVTOSOFTC(ppsdev);
188 count = sys_cputimer->count();
189 if (!(ppb_rstr(ppbus) & nACK))
191 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
192 ppb_wctr(ppbus, IRQENABLE | AUTOFEED);
193 pps_event(&sc->pps, count, PPS_CAPTUREASSERT);
194 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
195 ppb_wctr(ppbus, IRQENABLE);
199 ppsioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
201 u_int unit = minor(dev);
202 struct pps_data *sc = UNITOSOFTC(unit);
204 return (pps_ioctl(cmd, data, &sc->pps));
207 static device_method_t pps_methods[] = {
208 /* device interface */
209 DEVMETHOD(device_identify, ppsidentify),
210 DEVMETHOD(device_probe, ppsprobe),
211 DEVMETHOD(device_attach, ppsattach),
216 static driver_t pps_driver = {
219 sizeof(struct pps_data),
221 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);