pps.c revision 130585
1/*
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 * ----------------------------------------------------------------------------
8 *
9 *
10 * This driver implements a draft-mogul-pps-api-02.txt PPS source.
11 *
12 * The input pin is pin#10
13 * The echo output pin is pin#14
14 *
15 */
16
17#include <sys/cdefs.h>
18__FBSDID("$FreeBSD: head/sys/dev/ppbus/pps.c 130585 2004-06-16 09:47:26Z phk $");
19
20#include <sys/param.h>
21#include <sys/kernel.h>
22#include <sys/systm.h>
23#include <sys/module.h>
24#include <sys/bus.h>
25#include <sys/conf.h>
26#include <sys/timepps.h>
27#include <machine/bus.h>
28#include <machine/resource.h>
29#include <sys/rman.h>
30
31#include <dev/ppbus/ppbconf.h>
32#include "ppbus_if.h"
33#include <dev/ppbus/ppbio.h>
34
35#define PPS_NAME	"pps"		/* our official name */
36
37#define PRVERBOSE(fmt, arg...)	if (bootverbose) printf(fmt, ##arg);
38
39struct pps_data {
40	struct	ppb_device pps_dev;
41	struct	pps_state pps[9];
42	struct cdev *devs[9];
43	device_t ppsdev;
44	device_t ppbus;
45	int	busy;
46	struct callout_handle timeout;
47	int	lastdata;
48
49	struct resource *intr_resource;	/* interrupt resource */
50	void *intr_cookie;		/* interrupt registration cookie */
51};
52
53static void	ppsintr(void *arg);
54static void 	ppshcpoll(void *arg);
55
56#define DEVTOSOFTC(dev) \
57	((struct pps_data *)device_get_softc(dev))
58
59static devclass_t pps_devclass;
60
61static	d_open_t	ppsopen;
62static	d_close_t	ppsclose;
63static	d_ioctl_t	ppsioctl;
64
65static struct cdevsw pps_cdevsw = {
66	.d_version =	D_VERSION,
67	.d_flags =	D_NEEDGIANT,
68	.d_open =	ppsopen,
69	.d_close =	ppsclose,
70	.d_ioctl =	ppsioctl,
71	.d_name =	PPS_NAME,
72};
73
74static void
75ppsidentify(driver_t *driver, device_t parent)
76{
77
78	device_t dev;
79
80	dev = device_find_child(parent, PPS_NAME, 0);
81	if (!dev)
82		BUS_ADD_CHILD(parent, 0, PPS_NAME, -1);
83}
84
85static int
86ppstry(device_t ppbus, int send, int expect)
87{
88	int i;
89
90	ppb_wdtr(ppbus, send);
91	i = ppb_rdtr(ppbus);
92	PRVERBOSE("S: %02x E: %02x G: %02x\n", send, expect, i);
93	return (i != expect);
94}
95
96static int
97ppsprobe(device_t ppsdev)
98{
99	device_set_desc(ppsdev, "Pulse per second Timing Interface");
100
101	return (0);
102}
103
104static int
105ppsattach(device_t dev)
106{
107	struct pps_data *sc = DEVTOSOFTC(dev);
108	device_t ppbus = device_get_parent(dev);
109	struct cdev *d;
110	intptr_t irq;
111	int i, unit, zero = 0;
112
113	bzero(sc, sizeof(struct pps_data)); /* XXX doesn't newbus do this? */
114
115	/* retrieve the ppbus irq */
116	BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
117
118	if (irq > 0) {
119		/* declare our interrupt handler */
120		sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
121		    &zero, irq, irq, 1, RF_SHAREABLE);
122	}
123	/* interrupts seem mandatory */
124	if (sc->intr_resource == NULL)
125		return (ENXIO);
126
127	sc->ppsdev = dev;
128	sc->ppbus = ppbus;
129	unit = device_get_unit(ppbus);
130	d = make_dev(&pps_cdevsw, unit,
131	    UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%d", unit);
132	sc->devs[0] = d;
133	sc->pps[0].ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
134	d->si_drv1 = sc;
135	d->si_drv2 = (void*)0;
136	pps_init(&sc->pps[0]);
137
138	if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT))
139		return (0);
140
141	do {
142		i = ppb_set_mode(sc->ppbus, PPB_EPP);
143		PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
144		if (i == -1)
145			break;
146		i = 0;
147		ppb_wctr(ppbus, i);
148		if (ppstry(ppbus, 0x00, 0x00))
149			break;
150		if (ppstry(ppbus, 0x55, 0x55))
151			break;
152		if (ppstry(ppbus, 0xaa, 0xaa))
153			break;
154		if (ppstry(ppbus, 0xff, 0xff))
155			break;
156
157		i = IRQENABLE | PCD | STROBE | nINIT | SELECTIN;
158		ppb_wctr(ppbus, i);
159		PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
160		if (ppstry(ppbus, 0x00, 0x00))
161			break;
162		if (ppstry(ppbus, 0x55, 0x00))
163			break;
164		if (ppstry(ppbus, 0xaa, 0x00))
165			break;
166		if (ppstry(ppbus, 0xff, 0x00))
167			break;
168
169		i = IRQENABLE | PCD | nINIT | SELECTIN;
170		ppb_wctr(ppbus, i);
171		PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
172		ppstry(ppbus, 0x00, 0xff);
173		ppstry(ppbus, 0x55, 0xff);
174		ppstry(ppbus, 0xaa, 0xff);
175		ppstry(ppbus, 0xff, 0xff);
176
177		for (i = 1; i < 9; i++) {
178			d = make_dev(&pps_cdevsw, unit + 0x10000 * i,
179			  UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%db%d", unit, i - 1);
180			sc->devs[i] = d;
181			sc->pps[i].ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
182			d->si_drv1 = sc;
183			d->si_drv2 = (void *)(intptr_t)i;
184			pps_init(&sc->pps[i]);
185		}
186	} while (0);
187	i = ppb_set_mode(sc->ppbus, PPB_COMPATIBLE);
188	ppb_release_bus(ppbus, dev);
189
190	return (0);
191}
192
193static	int
194ppsopen(struct cdev *dev, int flags, int fmt, struct thread *td)
195{
196	struct pps_data *sc = dev->si_drv1;
197	int subdev = (intptr_t)dev->si_drv2;
198	int error, i;
199
200	if (!sc->busy) {
201		device_t ppsdev = sc->ppsdev;
202		device_t ppbus = sc->ppbus;
203
204		if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
205			return (EINTR);
206
207		/* attach the interrupt handler */
208		if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource,
209			       INTR_TYPE_TTY, ppsintr, ppsdev,
210			       &sc->intr_cookie))) {
211			ppb_release_bus(ppbus, ppsdev);
212			return (error);
213		}
214
215		i = ppb_set_mode(sc->ppbus, PPB_PS2);
216		PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
217
218		i = IRQENABLE | PCD | nINIT | SELECTIN;
219		ppb_wctr(ppbus, i);
220	}
221	if (subdev > 0 && !(sc->busy & ~1)) {
222		sc->timeout = timeout(ppshcpoll, sc, 1);
223		sc->lastdata = ppb_rdtr(sc->ppbus);
224	}
225	sc->busy |= (1 << subdev);
226	return(0);
227}
228
229static	int
230ppsclose(struct cdev *dev, int flags, int fmt, struct thread *td)
231{
232	struct pps_data *sc = dev->si_drv1;
233	int subdev = (intptr_t)dev->si_drv2;
234
235	sc->pps[subdev].ppsparam.mode = 0;	/* PHK ??? */
236	sc->busy &= ~(1 << subdev);
237	if (subdev > 0 && !(sc->busy & ~1))
238		untimeout(ppshcpoll, sc, sc->timeout);
239	if (!sc->busy) {
240		device_t ppsdev = sc->ppsdev;
241		device_t ppbus = sc->ppbus;
242
243		ppb_wdtr(ppbus, 0);
244		ppb_wctr(ppbus, 0);
245
246		/* Note: the interrupt handler is automatically detached */
247		ppb_set_mode(ppbus, PPB_COMPATIBLE);
248		ppb_release_bus(ppbus, ppsdev);
249	}
250	return(0);
251}
252
253static void
254ppshcpoll(void *arg)
255{
256	struct pps_data *sc = arg;
257	int i, j, k, l;
258
259	if (!(sc->busy & ~1))
260		return;
261	sc->timeout = timeout(ppshcpoll, sc, 1);
262	i = ppb_rdtr(sc->ppbus);
263	if (i == sc->lastdata)
264		return;
265	l = sc->lastdata ^ i;
266	k = 1;
267	for (j = 1; j < 9; j ++) {
268		if (l & k) {
269			pps_capture(&sc->pps[j]);
270			pps_event(&sc->pps[j],
271			    i & k ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR);
272		}
273		k += k;
274	}
275	sc->lastdata = i;
276}
277
278static void
279ppsintr(void *arg)
280{
281	device_t ppsdev = (device_t)arg;
282	struct pps_data *sc = DEVTOSOFTC(ppsdev);
283	device_t ppbus = sc->ppbus;
284
285	pps_capture(&sc->pps[0]);
286	if (!(ppb_rstr(ppbus) & nACK))
287		return;
288	if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
289		ppb_wctr(ppbus, IRQENABLE | AUTOFEED);
290	pps_event(&sc->pps[0], PPS_CAPTUREASSERT);
291	if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
292		ppb_wctr(ppbus, IRQENABLE);
293}
294
295static int
296ppsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
297{
298	struct pps_data *sc = dev->si_drv1;
299	int subdev = (intptr_t)dev->si_drv2;
300
301	return (pps_ioctl(cmd, data, &sc->pps[subdev]));
302}
303
304static device_method_t pps_methods[] = {
305	/* device interface */
306	DEVMETHOD(device_identify,	ppsidentify),
307	DEVMETHOD(device_probe,		ppsprobe),
308	DEVMETHOD(device_attach,	ppsattach),
309
310	{ 0, 0 }
311};
312
313static driver_t pps_driver = {
314	PPS_NAME,
315	pps_methods,
316	sizeof(struct pps_data),
317};
318DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);
319