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