pps.c revision 126076
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 126076 2004-02-21 19:42:58Z 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	dev_t	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_open =	ppsopen,
67	.d_close =	ppsclose,
68	.d_ioctl =	ppsioctl,
69	.d_name =	PPS_NAME,
70};
71
72static void
73ppsidentify(driver_t *driver, device_t parent)
74{
75
76	BUS_ADD_CHILD(parent, 0, PPS_NAME, -1);
77}
78
79static int
80ppstry(device_t ppbus, int send, int expect)
81{
82	int i;
83
84	ppb_wdtr(ppbus, send);
85	i = ppb_rdtr(ppbus);
86	PRVERBOSE("S: %02x E: %02x G: %02x\n", send, expect, i);
87	return (i != expect);
88}
89
90static int
91ppsprobe(device_t ppsdev)
92{
93	device_set_desc(ppsdev, "Pulse per second Timing Interface");
94
95	return (0);
96}
97
98static int
99ppsattach(device_t dev)
100{
101	struct pps_data *sc = DEVTOSOFTC(dev);
102	device_t ppbus = device_get_parent(dev);
103	dev_t d;
104	intptr_t irq;
105	int i, unit, zero = 0;
106
107	bzero(sc, sizeof(struct pps_data)); /* XXX doesn't newbus do this? */
108
109	/* retrieve the ppbus irq */
110	BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
111
112	if (irq > 0) {
113		/* declare our interrupt handler */
114		sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
115		    &zero, irq, irq, 1, RF_SHAREABLE);
116	}
117	/* interrupts seem mandatory */
118	if (sc->intr_resource == NULL)
119		return (ENXIO);
120
121	sc->ppsdev = dev;
122	sc->ppbus = ppbus;
123	unit = device_get_unit(ppbus);
124	d = make_dev(&pps_cdevsw, unit,
125	    UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%d", unit);
126	sc->devs[0] = d;
127	sc->pps[0].ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
128	d->si_drv1 = sc;
129	d->si_drv2 = (void*)0;
130	pps_init(&sc->pps[0]);
131
132	if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT))
133		return (0);
134
135	do {
136		i = ppb_set_mode(sc->ppbus, PPB_EPP);
137		PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
138		if (i == -1)
139			break;
140		i = 0;
141		ppb_wctr(ppbus, i);
142		if (ppstry(ppbus, 0x00, 0x00))
143			break;
144		if (ppstry(ppbus, 0x55, 0x55))
145			break;
146		if (ppstry(ppbus, 0xaa, 0xaa))
147			break;
148		if (ppstry(ppbus, 0xff, 0xff))
149			break;
150
151		i = IRQENABLE | PCD | STROBE | nINIT | SELECTIN;
152		ppb_wctr(ppbus, i);
153		PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
154		if (ppstry(ppbus, 0x00, 0x00))
155			break;
156		if (ppstry(ppbus, 0x55, 0x00))
157			break;
158		if (ppstry(ppbus, 0xaa, 0x00))
159			break;
160		if (ppstry(ppbus, 0xff, 0x00))
161			break;
162
163		i = IRQENABLE | PCD | nINIT | SELECTIN;
164		ppb_wctr(ppbus, i);
165		PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
166		ppstry(ppbus, 0x00, 0xff);
167		ppstry(ppbus, 0x55, 0xff);
168		ppstry(ppbus, 0xaa, 0xff);
169		ppstry(ppbus, 0xff, 0xff);
170
171		for (i = 1; i < 9; i++) {
172			d = make_dev(&pps_cdevsw, unit + 0x10000 * i,
173			  UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%db%d", unit, i - 1);
174			sc->devs[i] = d;
175			sc->pps[i].ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
176			d->si_drv1 = sc;
177			d->si_drv2 = (void *)(intptr_t)i;
178			pps_init(&sc->pps[i]);
179		}
180	} while (0);
181	i = ppb_set_mode(sc->ppbus, PPB_COMPATIBLE);
182	ppb_release_bus(ppbus, dev);
183
184	return (0);
185}
186
187static	int
188ppsopen(dev_t dev, int flags, int fmt, struct thread *td)
189{
190	struct pps_data *sc = dev->si_drv1;
191	int subdev = (intptr_t)dev->si_drv2;
192	int error, i;
193
194	if (!sc->busy) {
195		device_t ppsdev = sc->ppsdev;
196		device_t ppbus = sc->ppbus;
197
198		if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
199			return (EINTR);
200
201		/* attach the interrupt handler */
202		if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource,
203			       INTR_TYPE_TTY, ppsintr, ppsdev,
204			       &sc->intr_cookie))) {
205			ppb_release_bus(ppbus, ppsdev);
206			return (error);
207		}
208
209		i = ppb_set_mode(sc->ppbus, PPB_PS2);
210		PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
211
212		i = IRQENABLE | PCD | nINIT | SELECTIN;
213		ppb_wctr(ppbus, i);
214	}
215	if (subdev > 0 && !(sc->busy & ~1)) {
216		sc->timeout = timeout(ppshcpoll, sc, 1);
217		sc->lastdata = ppb_rdtr(sc->ppbus);
218	}
219	sc->busy |= (1 << subdev);
220	return(0);
221}
222
223static	int
224ppsclose(dev_t dev, int flags, int fmt, struct thread *td)
225{
226	struct pps_data *sc = dev->si_drv1;
227	int subdev = (intptr_t)dev->si_drv2;
228
229	sc->pps[subdev].ppsparam.mode = 0;	/* PHK ??? */
230	sc->busy &= ~(1 << subdev);
231	if (subdev > 0 && !(sc->busy & ~1))
232		untimeout(ppshcpoll, sc, sc->timeout);
233	if (!sc->busy) {
234		device_t ppsdev = sc->ppsdev;
235		device_t ppbus = sc->ppbus;
236
237		ppb_wdtr(ppbus, 0);
238		ppb_wctr(ppbus, 0);
239
240		/* Note: the interrupt handler is automatically detached */
241		ppb_set_mode(ppbus, PPB_COMPATIBLE);
242		ppb_release_bus(ppbus, ppsdev);
243	}
244	return(0);
245}
246
247static void
248ppshcpoll(void *arg)
249{
250	struct pps_data *sc = arg;
251	int i, j, k, l;
252
253	if (!(sc->busy & ~1))
254		return;
255	sc->timeout = timeout(ppshcpoll, sc, 1);
256	i = ppb_rdtr(sc->ppbus);
257	if (i == sc->lastdata)
258		return;
259	l = sc->lastdata ^ i;
260	k = 1;
261	for (j = 1; j < 9; j ++) {
262		if (l & k) {
263			pps_capture(&sc->pps[j]);
264			pps_event(&sc->pps[j],
265			    i & k ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR);
266		}
267		k += k;
268	}
269	sc->lastdata = i;
270}
271
272static void
273ppsintr(void *arg)
274{
275	device_t ppsdev = (device_t)arg;
276	struct pps_data *sc = DEVTOSOFTC(ppsdev);
277	device_t ppbus = sc->ppbus;
278
279	pps_capture(&sc->pps[0]);
280	if (!(ppb_rstr(ppbus) & nACK))
281		return;
282	if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
283		ppb_wctr(ppbus, IRQENABLE | AUTOFEED);
284	pps_event(&sc->pps[0], PPS_CAPTUREASSERT);
285	if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
286		ppb_wctr(ppbus, IRQENABLE);
287}
288
289static int
290ppsioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
291{
292	struct pps_data *sc = dev->si_drv1;
293	int subdev = (intptr_t)dev->si_drv2;
294
295	return (pps_ioctl(cmd, data, &sc->pps[subdev]));
296}
297
298static device_method_t pps_methods[] = {
299	/* device interface */
300	DEVMETHOD(device_identify,	ppsidentify),
301	DEVMETHOD(device_probe,		ppsprobe),
302	DEVMETHOD(device_attach,	ppsattach),
303
304	{ 0, 0 }
305};
306
307static driver_t pps_driver = {
308	PPS_NAME,
309	pps_methods,
310	sizeof(struct pps_data),
311};
312DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);
313