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