summaryrefslogtreecommitdiffstats
path: root/sys/dev/ppbus/pps.c
blob: 7a360d12df660f119014cf309f16592f88955c91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*-
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
 * ----------------------------------------------------------------------------
 *
 *
 * This driver implements a draft-mogul-pps-api-02.txt PPS source.
 *
 * The input pin is pin#10
 * The echo output pin is pin#14
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/lock.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/sx.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/timepps.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/rman.h>

#include <dev/ppbus/ppbconf.h>
#include "ppbus_if.h"
#include <dev/ppbus/ppbio.h>

#define PPS_NAME	"pps"		/* our official name */

#define PRVERBOSE(fmt, arg...)	if (bootverbose) printf(fmt, ##arg);

struct pps_data {
	struct	ppb_device pps_dev;
	struct	pps_state pps[9];
	struct cdev *devs[9];
	device_t ppsdev;
	device_t ppbus;
	int	busy;
	struct callout timeout;
	int	lastdata;

	struct sx lock;
	struct resource *intr_resource;	/* interrupt resource */
	void *intr_cookie;		/* interrupt registration cookie */
};

static void	ppsintr(void *arg);
static void 	ppshcpoll(void *arg);

#define DEVTOSOFTC(dev) \
	((struct pps_data *)device_get_softc(dev))

static devclass_t pps_devclass;

static	d_open_t	ppsopen;
static	d_close_t	ppsclose;
static	d_ioctl_t	ppsioctl;

static struct cdevsw pps_cdevsw = {
	.d_version =	D_VERSION,
	.d_open =	ppsopen,
	.d_close =	ppsclose,
	.d_ioctl =	ppsioctl,
	.d_name =	PPS_NAME,
};

static void
ppsidentify(driver_t *driver, device_t parent)
{

	device_t dev;

	dev = device_find_child(parent, PPS_NAME, -1);
	if (!dev)
		BUS_ADD_CHILD(parent, 0, PPS_NAME, -1);
}

static int
ppstry(device_t ppbus, int send, int expect)
{
	int i;

	ppb_wdtr(ppbus, send);
	i = ppb_rdtr(ppbus);
	PRVERBOSE("S: %02x E: %02x G: %02x\n", send, expect, i);
	return (i != expect);
}

static int
ppsprobe(device_t ppsdev)
{
	device_set_desc(ppsdev, "Pulse per second Timing Interface");

	return (0);
}

static int
ppsattach(device_t dev)
{
	struct pps_data *sc = DEVTOSOFTC(dev);
	device_t ppbus = device_get_parent(dev);
	struct cdev *d;
	int error, i, unit, rid = 0;

	/* declare our interrupt handler */
	sc->intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
	    RF_SHAREABLE);

	/* interrupts seem mandatory */
	if (sc->intr_resource == NULL) {
		device_printf(dev, "Unable to allocate interrupt resource\n");
		return (ENXIO);
	}

	error = bus_setup_intr(dev, sc->intr_resource,
	    INTR_TYPE_TTY | INTR_MPSAFE, NULL, ppsintr,
	    sc, &sc->intr_cookie);
	if (error) {
		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->intr_resource);
		device_printf(dev, "Unable to register interrupt handler\n");
		return (error);
	}

	sx_init(&sc->lock, "pps");
	ppb_init_callout(ppbus, &sc->timeout, 0);
	sc->ppsdev = dev;
	sc->ppbus = ppbus;
	unit = device_get_unit(ppbus);
	d = make_dev(&pps_cdevsw, unit,
	    UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%d", unit);
	sc->devs[0] = d;
	sc->pps[0].ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
	d->si_drv1 = sc;
	d->si_drv2 = (void*)0;
	pps_init(&sc->pps[0]);

	ppb_lock(ppbus);
	if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
		ppb_unlock(ppbus);
		return (0);
	}

	do {
		i = ppb_set_mode(sc->ppbus, PPB_EPP);
		PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
		if (i == -1)
			break;
		i = 0;
		ppb_wctr(ppbus, i);
		if (ppstry(ppbus, 0x00, 0x00))
			break;
		if (ppstry(ppbus, 0x55, 0x55))
			break;
		if (ppstry(ppbus, 0xaa, 0xaa))
			break;
		if (ppstry(ppbus, 0xff, 0xff))
			break;

		i = IRQENABLE | PCD | STROBE | nINIT | SELECTIN;
		ppb_wctr(ppbus, i);
		PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
		if (ppstry(ppbus, 0x00, 0x00))
			break;
		if (ppstry(ppbus, 0x55, 0x00))
			break;
		if (ppstry(ppbus, 0xaa, 0x00))
			break;
		if (ppstry(ppbus, 0xff, 0x00))
			break;

		i = IRQENABLE | PCD | nINIT | SELECTIN;
		ppb_wctr(ppbus, i);
		PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
		ppstry(ppbus, 0x00, 0xff);
		ppstry(ppbus, 0x55, 0xff);
		ppstry(ppbus, 0xaa, 0xff);
		ppstry(ppbus, 0xff, 0xff);
		ppb_unlock(ppbus);

		for (i = 1; i < 9; i++) {
			d = make_dev(&pps_cdevsw, unit + 0x10000 * i,
			  UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%db%d", unit, i - 1);
			sc->devs[i] = d;
			sc->pps[i].ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
			d->si_drv1 = sc;
			d->si_drv2 = (void *)(intptr_t)i;
			pps_init(&sc->pps[i]);
		}
		ppb_lock(ppbus);
	} while (0);
	i = ppb_set_mode(sc->ppbus, PPB_COMPATIBLE);
	ppb_release_bus(ppbus, dev);
	ppb_unlock(ppbus);

	return (0);
}

static	int
ppsopen(struct cdev *dev, int flags, int fmt, struct thread *td)
{
	struct pps_data *sc = dev->si_drv1;
	device_t ppbus = sc->ppbus;
	int subdev = (intptr_t)dev->si_drv2;
	int i;

	/*
	 * The sx lock is here solely to serialize open()'s to close
	 * the race of concurrent open()'s when pps(4) doesn't own the
	 * ppbus.
	 */
	sx_xlock(&sc->lock);
	ppb_lock(ppbus);
	if (!sc->busy) {
		device_t ppsdev = sc->ppsdev;

		if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR)) {
			ppb_unlock(ppbus);
			sx_xunlock(&sc->lock);
			return (EINTR);
		}

		i = ppb_set_mode(sc->ppbus, PPB_PS2);
		PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));

		i = IRQENABLE | PCD | nINIT | SELECTIN;
		ppb_wctr(ppbus, i);
	}
	if (subdev > 0 && !(sc->busy & ~1)) {
		/* XXX: Timeout of 1?  hz/100 instead perhaps? */
		callout_reset(&sc->timeout, 1, ppshcpoll, sc);
		sc->lastdata = ppb_rdtr(sc->ppbus);
	}
	sc->busy |= (1 << subdev);
	ppb_unlock(ppbus);
	sx_xunlock(&sc->lock);
	return(0);
}

static	int
ppsclose(struct cdev *dev, int flags, int fmt, struct thread *td)
{
	struct pps_data *sc = dev->si_drv1;
	int subdev = (intptr_t)dev->si_drv2;

	sx_xlock(&sc->lock);
	sc->pps[subdev].ppsparam.mode = 0;	/* PHK ??? */
	ppb_lock(sc->ppbus);
	sc->busy &= ~(1 << subdev);
	if (subdev > 0 && !(sc->busy & ~1))
		callout_stop(&sc->timeout);
	if (!sc->busy) {
		device_t ppsdev = sc->ppsdev;
		device_t ppbus = sc->ppbus;

		ppb_wdtr(ppbus, 0);
		ppb_wctr(ppbus, 0);

		ppb_set_mode(ppbus, PPB_COMPATIBLE);
		ppb_release_bus(ppbus, ppsdev);
	}
	ppb_unlock(sc->ppbus);
	sx_xunlock(&sc->lock);
	return(0);
}

static void
ppshcpoll(void *arg)
{
	struct pps_data *sc = arg;
	int i, j, k, l;

	KASSERT(sc->busy & ~1, ("pps polling w/o opened devices"));
	i = ppb_rdtr(sc->ppbus);
	if (i == sc->lastdata)
		return;
	l = sc->lastdata ^ i;
	k = 1;
	for (j = 1; j < 9; j ++) {
		if (l & k) {
			pps_capture(&sc->pps[j]);
			pps_event(&sc->pps[j],
			    i & k ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR);
		}
		k += k;
	}
	sc->lastdata = i;
	callout_reset(&sc->timeout, 1, ppshcpoll, sc);
}

static void
ppsintr(void *arg)
{
	struct pps_data *sc = (struct pps_data *)arg;

	ppb_assert_locked(sc->ppbus);
	pps_capture(&sc->pps[0]);
	if (!(ppb_rstr(sc->ppbus) & nACK))
		return;

	if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
		ppb_wctr(sc->ppbus, IRQENABLE | AUTOFEED);
	pps_event(&sc->pps[0], PPS_CAPTUREASSERT);
	if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
		ppb_wctr(sc->ppbus, IRQENABLE);
}

static int
ppsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
{
	struct pps_data *sc = dev->si_drv1;
	int subdev = (intptr_t)dev->si_drv2;
	int err;

	ppb_lock(sc->ppbus);
	err = pps_ioctl(cmd, data, &sc->pps[subdev]);
	ppb_unlock(sc->ppbus);
	return (err);
}

static device_method_t pps_methods[] = {
	/* device interface */
	DEVMETHOD(device_identify,	ppsidentify),
	DEVMETHOD(device_probe,		ppsprobe),
	DEVMETHOD(device_attach,	ppsattach),

	{ 0, 0 }
};

static driver_t pps_driver = {
	PPS_NAME,
	pps_methods,
	sizeof(struct pps_data),
};
DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);
MODULE_DEPEND(pps, ppbus, 1, 1, 1);
OpenPOWER on IntegriCloud