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 * ----------------------------------------------------------------------------
10 * This driver implements a draft-mogul-pps-api-02.txt PPS source.
12 * The input pin is pin#10
13 * The echo output pin is pin#14
17 #include <sys/cdefs.h>
18 __FBSDID("$FreeBSD$");
20 #include <sys/param.h>
21 #include <sys/kernel.h>
22 #include <sys/systm.h>
23 #include <sys/module.h>
26 #include <sys/timepps.h>
27 #include <machine/bus.h>
28 #include <machine/resource.h>
31 #include <dev/ppbus/ppbconf.h>
33 #include <dev/ppbus/ppbio.h>
35 #define PPS_NAME "pps" /* our official name */
37 #define PRVERBOSE(fmt, arg...) if (bootverbose) printf(fmt, ##arg);
40 struct ppb_device pps_dev;
41 struct pps_state pps[9];
46 struct callout_handle timeout;
50 struct resource *intr_resource; /* interrupt resource */
51 void *intr_cookie; /* interrupt registration cookie */
54 static void ppsintr(void *arg);
55 static void ppshcpoll(void *arg);
57 #define DEVTOSOFTC(dev) \
58 ((struct pps_data *)device_get_softc(dev))
60 static devclass_t pps_devclass;
62 static d_open_t ppsopen;
63 static d_close_t ppsclose;
64 static d_ioctl_t ppsioctl;
66 static struct cdevsw pps_cdevsw = {
67 .d_version = D_VERSION,
75 ppsidentify(driver_t *driver, device_t parent)
80 dev = device_find_child(parent, PPS_NAME, 0);
82 BUS_ADD_CHILD(parent, 0, PPS_NAME, -1);
86 ppstry(device_t ppbus, int send, int expect)
90 ppb_wdtr(ppbus, send);
92 PRVERBOSE("S: %02x E: %02x G: %02x\n", send, expect, i);
97 ppsprobe(device_t ppsdev)
99 device_set_desc(ppsdev, "Pulse per second Timing Interface");
105 ppsattach(device_t dev)
107 struct pps_data *sc = DEVTOSOFTC(dev);
108 device_t ppbus = device_get_parent(dev);
111 int i, unit, zero = 0;
113 mtx_init(&sc->mtx, device_get_nameunit(dev), "pps", MTX_SPIN);
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 == NULL)
128 unit = device_get_unit(ppbus);
129 d = make_dev(&pps_cdevsw, unit,
130 UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%d", unit);
132 sc->pps[0].ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
134 d->si_drv2 = (void*)0;
135 pps_init(&sc->pps[0]);
137 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT))
141 i = ppb_set_mode(sc->ppbus, PPB_EPP);
142 PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
147 if (ppstry(ppbus, 0x00, 0x00))
149 if (ppstry(ppbus, 0x55, 0x55))
151 if (ppstry(ppbus, 0xaa, 0xaa))
153 if (ppstry(ppbus, 0xff, 0xff))
156 i = IRQENABLE | PCD | STROBE | nINIT | SELECTIN;
158 PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
159 if (ppstry(ppbus, 0x00, 0x00))
161 if (ppstry(ppbus, 0x55, 0x00))
163 if (ppstry(ppbus, 0xaa, 0x00))
165 if (ppstry(ppbus, 0xff, 0x00))
168 i = IRQENABLE | PCD | nINIT | SELECTIN;
170 PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
171 ppstry(ppbus, 0x00, 0xff);
172 ppstry(ppbus, 0x55, 0xff);
173 ppstry(ppbus, 0xaa, 0xff);
174 ppstry(ppbus, 0xff, 0xff);
176 for (i = 1; i < 9; i++) {
177 d = make_dev(&pps_cdevsw, unit + 0x10000 * i,
178 UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%db%d", unit, i - 1);
180 sc->pps[i].ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
182 d->si_drv2 = (void *)(intptr_t)i;
183 pps_init(&sc->pps[i]);
186 i = ppb_set_mode(sc->ppbus, PPB_COMPATIBLE);
187 ppb_release_bus(ppbus, dev);
193 ppsopen(struct cdev *dev, int flags, int fmt, struct thread *td)
195 struct pps_data *sc = dev->si_drv1;
196 int subdev = (intptr_t)dev->si_drv2;
200 device_t ppsdev = sc->ppsdev;
201 device_t ppbus = sc->ppbus;
203 if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
206 /* attach the interrupt handler */
207 if ((error = bus_setup_intr(ppsdev, sc->intr_resource,
208 (INTR_TYPE_TTY | INTR_MPSAFE | INTR_FAST), ppsintr,
209 sc, &sc->intr_cookie))) {
210 ppb_release_bus(ppbus, ppsdev);
214 i = ppb_set_mode(sc->ppbus, PPB_PS2);
215 PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
217 i = IRQENABLE | PCD | nINIT | SELECTIN;
220 if (subdev > 0 && !(sc->busy & ~1)) {
221 sc->timeout = timeout(ppshcpoll, sc, 1);
222 sc->lastdata = ppb_rdtr(sc->ppbus);
224 sc->busy |= (1 << subdev);
229 ppsclose(struct cdev *dev, int flags, int fmt, struct thread *td)
231 struct pps_data *sc = dev->si_drv1;
232 int subdev = (intptr_t)dev->si_drv2;
234 sc->pps[subdev].ppsparam.mode = 0; /* PHK ??? */
235 sc->busy &= ~(1 << subdev);
236 if (subdev > 0 && !(sc->busy & ~1))
237 untimeout(ppshcpoll, sc, sc->timeout);
239 device_t ppsdev = sc->ppsdev;
240 device_t ppbus = sc->ppbus;
245 /* Note: the interrupt handler is automatically detached */
246 ppb_set_mode(ppbus, PPB_COMPATIBLE);
247 ppb_release_bus(ppbus, ppsdev);
255 struct pps_data *sc = arg;
258 if (!(sc->busy & ~1))
260 mtx_lock_spin(&sc->mtx);
261 sc->timeout = timeout(ppshcpoll, sc, 1);
262 i = ppb_rdtr(sc->ppbus);
263 if (i == sc->lastdata)
265 l = sc->lastdata ^ i;
267 for (j = 1; j < 9; j ++) {
269 pps_capture(&sc->pps[j]);
270 pps_event(&sc->pps[j],
271 i & k ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR);
276 mtx_unlock_spin(&sc->mtx);
282 struct pps_data *sc = (struct pps_data *)arg;
284 pps_capture(&sc->pps[0]);
285 if (!(ppb_rstr(sc->ppbus) & nACK))
287 if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
288 ppb_wctr(sc->ppbus, IRQENABLE | AUTOFEED);
289 mtx_lock_spin(&sc->mtx);
290 pps_event(&sc->pps[0], PPS_CAPTUREASSERT);
291 mtx_unlock_spin(&sc->mtx);
292 if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
293 ppb_wctr(sc->ppbus, IRQENABLE);
297 ppsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
299 struct pps_data *sc = dev->si_drv1;
300 int subdev = (intptr_t)dev->si_drv2;
303 mtx_lock_spin(&sc->mtx);
304 err = pps_ioctl(cmd, data, &sc->pps[subdev]);
305 mtx_unlock_spin(&sc->mtx);
309 static device_method_t pps_methods[] = {
310 /* device interface */
311 DEVMETHOD(device_identify, ppsidentify),
312 DEVMETHOD(device_probe, ppsprobe),
313 DEVMETHOD(device_attach, ppsattach),
318 static driver_t pps_driver = {
321 sizeof(struct pps_data),
323 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);