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>
21 #include <sys/module.h>
24 #include <sys/timepps.h>
25 #include <machine/bus.h>
26 #include <machine/resource.h>
29 #include <dev/ppbus/ppbconf.h>
31 #include <dev/ppbus/ppbio.h>
34 #define PPS_NAME "pps" /* our official name */
38 struct ppb_device pps_dev;
41 struct resource *intr_resource; /* interrupt resource */
42 void *intr_cookie; /* interrupt registration cookie */
45 static void ppsintr(void *arg);
47 #define DEVTOSOFTC(dev) \
48 ((struct pps_data *)device_get_softc(dev))
49 #define UNITOSOFTC(unit) \
50 ((struct pps_data *)devclass_get_softc(pps_devclass, (unit)))
51 #define UNITODEVICE(unit) \
52 (devclass_get_device(pps_devclass, (unit)))
54 static devclass_t pps_devclass;
56 static d_open_t ppsopen;
57 static d_close_t ppsclose;
58 static d_ioctl_t ppsioctl;
61 static struct cdevsw pps_cdevsw = {
69 /* strategy */ nostrategy,
79 ppsidentify(driver_t *driver, device_t parent)
82 BUS_ADD_CHILD(parent, 0, PPS_NAME, 0);
86 ppsprobe(device_t ppsdev)
92 sc = DEVTOSOFTC(ppsdev);
93 bzero(sc, sizeof(struct pps_data));
95 unit = device_get_unit(ppsdev);
96 dev = make_dev(&pps_cdevsw, unit,
97 UID_ROOT, GID_WHEEL, 0644, PPS_NAME "%d", unit);
99 device_set_desc(ppsdev, "Pulse per second Timing Interface");
101 sc->pps.ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
107 ppsattach(device_t dev)
109 struct pps_data *sc = DEVTOSOFTC(dev);
110 device_t ppbus = device_get_parent(dev);
113 /* retrieve the ppbus irq */
114 BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
117 /* declare our interrupt handler */
118 sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
119 &zero, irq, irq, 1, RF_SHAREABLE);
121 /* interrupts seem mandatory */
122 if (sc->intr_resource == 0)
129 ppsopen(dev_t dev, int flags, int fmt, struct proc *p)
131 u_int unit = minor(dev);
132 struct pps_data *sc = UNITOSOFTC(unit);
133 device_t ppsdev = UNITODEVICE(unit);
134 device_t ppbus = device_get_parent(ppsdev);
138 if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
141 /* attach the interrupt handler */
142 if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource,
143 INTR_TYPE_TTY, ppsintr, ppsdev,
144 &sc->intr_cookie))) {
145 ppb_release_bus(ppbus, ppsdev);
150 ppb_wctr(ppbus, IRQENABLE);
158 ppsclose(dev_t dev, int flags, int fmt, struct proc *p)
160 u_int unit = minor(dev);
161 struct pps_data *sc = UNITOSOFTC(unit);
162 device_t ppsdev = UNITODEVICE(unit);
163 device_t ppbus = device_get_parent(ppsdev);
165 sc->pps.ppsparam.mode = 0; /* PHK ??? */
170 /* Note: the interrupt handler is automatically detached */
171 ppb_release_bus(ppbus, ppsdev);
179 device_t ppsdev = (device_t)arg;
180 device_t ppbus = device_get_parent(ppsdev);
181 struct pps_data *sc = DEVTOSOFTC(ppsdev);
182 struct timecounter *tc;
186 count = timecounter->tc_get_timecount(tc);
187 if (!(ppb_rstr(ppbus) & nACK))
189 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
190 ppb_wctr(ppbus, IRQENABLE | AUTOFEED);
191 pps_event(&sc->pps, tc, count, PPS_CAPTUREASSERT);
192 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
193 ppb_wctr(ppbus, IRQENABLE);
197 ppsioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
199 u_int unit = minor(dev);
200 struct pps_data *sc = UNITOSOFTC(unit);
202 return (pps_ioctl(cmd, data, &sc->pps));
205 static device_method_t pps_methods[] = {
206 /* device interface */
207 DEVMETHOD(device_identify, ppsidentify),
208 DEVMETHOD(device_probe, ppsprobe),
209 DEVMETHOD(device_attach, ppsattach),
214 static driver_t pps_driver = {
217 sizeof(struct pps_data),
219 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);