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.2 2003/06/17 04:28:29 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
19 #include <sys/param.h>
20 #include <sys/kernel.h>
21 #include <sys/systm.h>
22 #include <sys/module.h>
25 #include <sys/timepps.h>
26 #include <machine/bus.h>
27 #include <machine/resource.h>
30 #include <dev/ppbus/ppbconf.h>
32 #include <dev/ppbus/ppbio.h>
35 #define PPS_NAME "pps" /* our official name */
39 struct ppb_device pps_dev;
42 struct resource *intr_resource; /* interrupt resource */
43 void *intr_cookie; /* interrupt registration cookie */
46 static void ppsintr(void *arg);
48 #define DEVTOSOFTC(dev) \
49 ((struct pps_data *)device_get_softc(dev))
50 #define UNITOSOFTC(unit) \
51 ((struct pps_data *)devclass_get_softc(pps_devclass, (unit)))
52 #define UNITODEVICE(unit) \
53 (devclass_get_device(pps_devclass, (unit)))
55 static devclass_t pps_devclass;
57 static d_open_t ppsopen;
58 static d_close_t ppsclose;
59 static d_ioctl_t ppsioctl;
62 static struct cdevsw pps_cdevsw = {
70 /* strategy */ nostrategy,
80 ppsidentify(driver_t *driver, device_t parent)
83 BUS_ADD_CHILD(parent, 0, PPS_NAME, 0);
87 ppsprobe(device_t ppsdev)
93 sc = DEVTOSOFTC(ppsdev);
94 bzero(sc, sizeof(struct pps_data));
96 unit = device_get_unit(ppsdev);
97 dev = make_dev(&pps_cdevsw, unit,
98 UID_ROOT, GID_WHEEL, 0644, PPS_NAME "%d", unit);
100 device_set_desc(ppsdev, "Pulse per second Timing Interface");
102 sc->pps.ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
108 ppsattach(device_t dev)
110 struct pps_data *sc = DEVTOSOFTC(dev);
111 device_t ppbus = device_get_parent(dev);
114 /* retrieve the ppbus irq */
115 BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
118 /* declare our interrupt handler */
119 sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
120 &zero, irq, irq, 1, RF_SHAREABLE);
122 /* interrupts seem mandatory */
123 if (sc->intr_resource == 0)
130 ppsopen(dev_t dev, int flags, int fmt, struct proc *p)
132 u_int unit = minor(dev);
133 struct pps_data *sc = UNITOSOFTC(unit);
134 device_t ppsdev = UNITODEVICE(unit);
135 device_t ppbus = device_get_parent(ppsdev);
139 if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
142 /* attach the interrupt handler */
143 if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource,
144 INTR_TYPE_TTY, ppsintr, ppsdev,
145 &sc->intr_cookie))) {
146 ppb_release_bus(ppbus, ppsdev);
151 ppb_wctr(ppbus, IRQENABLE);
159 ppsclose(dev_t dev, int flags, int fmt, struct proc *p)
161 u_int unit = minor(dev);
162 struct pps_data *sc = UNITOSOFTC(unit);
163 device_t ppsdev = UNITODEVICE(unit);
164 device_t ppbus = device_get_parent(ppsdev);
166 sc->pps.ppsparam.mode = 0; /* PHK ??? */
171 /* Note: the interrupt handler is automatically detached */
172 ppb_release_bus(ppbus, ppsdev);
180 device_t ppsdev = (device_t)arg;
181 device_t ppbus = device_get_parent(ppsdev);
182 struct pps_data *sc = DEVTOSOFTC(ppsdev);
183 struct timecounter *tc;
187 count = timecounter->tc_get_timecount(tc);
188 if (!(ppb_rstr(ppbus) & nACK))
190 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
191 ppb_wctr(ppbus, IRQENABLE | AUTOFEED);
192 pps_event(&sc->pps, tc, count, PPS_CAPTUREASSERT);
193 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
194 ppb_wctr(ppbus, IRQENABLE);
198 ppsioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
200 u_int unit = minor(dev);
201 struct pps_data *sc = UNITOSOFTC(unit);
203 return (pps_ioctl(cmd, data, &sc->pps));
206 static device_method_t pps_methods[] = {
207 /* device interface */
208 DEVMETHOD(device_identify, ppsidentify),
209 DEVMETHOD(device_probe, ppsprobe),
210 DEVMETHOD(device_attach, ppsattach),
215 static driver_t pps_driver = {
218 sizeof(struct pps_data),
220 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);