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.3 2003/07/21 05:50:34 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 = {
76 /* strategy */ nostrategy,
82 ppsidentify(driver_t *driver, device_t parent)
85 BUS_ADD_CHILD(parent, 0, PPS_NAME, 0);
89 ppsprobe(device_t ppsdev)
95 sc = DEVTOSOFTC(ppsdev);
96 bzero(sc, sizeof(struct pps_data));
98 unit = device_get_unit(ppsdev);
99 dev = make_dev(&pps_cdevsw, unit,
100 UID_ROOT, GID_WHEEL, 0644, PPS_NAME "%d", unit);
102 device_set_desc(ppsdev, "Pulse per second Timing Interface");
104 sc->pps.ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
110 ppsattach(device_t dev)
112 struct pps_data *sc = DEVTOSOFTC(dev);
113 device_t ppbus = device_get_parent(dev);
116 /* retrieve the ppbus irq */
117 BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
120 /* declare our interrupt handler */
121 sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
122 &zero, irq, irq, 1, RF_SHAREABLE);
124 /* interrupts seem mandatory */
125 if (sc->intr_resource == 0)
132 ppsopen(dev_t dev, int flags, int fmt, struct proc *p)
134 u_int unit = minor(dev);
135 struct pps_data *sc = UNITOSOFTC(unit);
136 device_t ppsdev = UNITODEVICE(unit);
137 device_t ppbus = device_get_parent(ppsdev);
141 if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
144 /* attach the interrupt handler */
145 if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource,
146 INTR_TYPE_TTY, ppsintr, ppsdev,
147 &sc->intr_cookie))) {
148 ppb_release_bus(ppbus, ppsdev);
153 ppb_wctr(ppbus, IRQENABLE);
161 ppsclose(dev_t dev, int flags, int fmt, struct proc *p)
163 u_int unit = minor(dev);
164 struct pps_data *sc = UNITOSOFTC(unit);
165 device_t ppsdev = UNITODEVICE(unit);
166 device_t ppbus = device_get_parent(ppsdev);
168 sc->pps.ppsparam.mode = 0; /* PHK ??? */
173 /* Note: the interrupt handler is automatically detached */
174 ppb_release_bus(ppbus, ppsdev);
182 device_t ppsdev = (device_t)arg;
183 device_t ppbus = device_get_parent(ppsdev);
184 struct pps_data *sc = DEVTOSOFTC(ppsdev);
185 struct timecounter *tc;
189 count = timecounter->tc_get_timecount(tc);
190 if (!(ppb_rstr(ppbus) & nACK))
192 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
193 ppb_wctr(ppbus, IRQENABLE | AUTOFEED);
194 pps_event(&sc->pps, tc, count, PPS_CAPTUREASSERT);
195 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
196 ppb_wctr(ppbus, IRQENABLE);
200 ppsioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
202 u_int unit = minor(dev);
203 struct pps_data *sc = UNITOSOFTC(unit);
205 return (pps_ioctl(cmd, data, &sc->pps));
208 static device_method_t pps_methods[] = {
209 /* device interface */
210 DEVMETHOD(device_identify, ppsidentify),
211 DEVMETHOD(device_probe, ppsprobe),
212 DEVMETHOD(device_attach, ppsattach),
217 static driver_t pps_driver = {
220 sizeof(struct pps_data),
222 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);