xref: /freebsd/sys/dev/ppbus/pps.c (revision a9148abd9da5db2f1c682fb17bed791845fc41c9)
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/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 
39 struct 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 mtx	mtx;
50 	struct resource *intr_resource;	/* interrupt resource */
51 	void *intr_cookie;		/* interrupt registration cookie */
52 };
53 
54 static int	ppsintr(void *arg);
55 static void 	ppshcpoll(void *arg);
56 
57 #define DEVTOSOFTC(dev) \
58 	((struct pps_data *)device_get_softc(dev))
59 
60 static devclass_t pps_devclass;
61 
62 static	d_open_t	ppsopen;
63 static	d_close_t	ppsclose;
64 static	d_ioctl_t	ppsioctl;
65 
66 static struct cdevsw pps_cdevsw = {
67 	.d_version =	D_VERSION,
68 	.d_open =	ppsopen,
69 	.d_close =	ppsclose,
70 	.d_ioctl =	ppsioctl,
71 	.d_name =	PPS_NAME,
72 };
73 
74 static void
75 ppsidentify(driver_t *driver, device_t parent)
76 {
77 
78 	device_t dev;
79 
80 	dev = device_find_child(parent, PPS_NAME, -1);
81 	if (!dev)
82 		BUS_ADD_CHILD(parent, 0, PPS_NAME, -1);
83 }
84 
85 static int
86 ppstry(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 
96 static int
97 ppsprobe(device_t ppsdev)
98 {
99 	device_set_desc(ppsdev, "Pulse per second Timing Interface");
100 
101 	return (0);
102 }
103 
104 static int
105 ppsattach(device_t dev)
106 {
107 	struct pps_data *sc = DEVTOSOFTC(dev);
108 	device_t ppbus = device_get_parent(dev);
109 	struct cdev *d;
110 	int i, unit, rid = 0;
111 
112 	mtx_init(&sc->mtx, device_get_nameunit(dev), "pps", MTX_SPIN);
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 		return (ENXIO);
121 
122 	sc->ppsdev = dev;
123 	sc->ppbus = ppbus;
124 	unit = device_get_unit(ppbus);
125 	d = make_dev(&pps_cdevsw, unit,
126 	    UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%d", unit);
127 	sc->devs[0] = d;
128 	sc->pps[0].ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
129 	d->si_drv1 = sc;
130 	d->si_drv2 = (void*)0;
131 	pps_init(&sc->pps[0]);
132 
133 	if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT))
134 		return (0);
135 
136 	do {
137 		i = ppb_set_mode(sc->ppbus, PPB_EPP);
138 		PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
139 		if (i == -1)
140 			break;
141 		i = 0;
142 		ppb_wctr(ppbus, i);
143 		if (ppstry(ppbus, 0x00, 0x00))
144 			break;
145 		if (ppstry(ppbus, 0x55, 0x55))
146 			break;
147 		if (ppstry(ppbus, 0xaa, 0xaa))
148 			break;
149 		if (ppstry(ppbus, 0xff, 0xff))
150 			break;
151 
152 		i = IRQENABLE | PCD | STROBE | nINIT | SELECTIN;
153 		ppb_wctr(ppbus, i);
154 		PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
155 		if (ppstry(ppbus, 0x00, 0x00))
156 			break;
157 		if (ppstry(ppbus, 0x55, 0x00))
158 			break;
159 		if (ppstry(ppbus, 0xaa, 0x00))
160 			break;
161 		if (ppstry(ppbus, 0xff, 0x00))
162 			break;
163 
164 		i = IRQENABLE | PCD | nINIT | SELECTIN;
165 		ppb_wctr(ppbus, i);
166 		PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
167 		ppstry(ppbus, 0x00, 0xff);
168 		ppstry(ppbus, 0x55, 0xff);
169 		ppstry(ppbus, 0xaa, 0xff);
170 		ppstry(ppbus, 0xff, 0xff);
171 
172 		for (i = 1; i < 9; i++) {
173 			d = make_dev(&pps_cdevsw, unit + 0x10000 * i,
174 			  UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%db%d", unit, i - 1);
175 			sc->devs[i] = d;
176 			sc->pps[i].ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
177 			d->si_drv1 = sc;
178 			d->si_drv2 = (void *)(intptr_t)i;
179 			pps_init(&sc->pps[i]);
180 		}
181 	} while (0);
182 	i = ppb_set_mode(sc->ppbus, PPB_COMPATIBLE);
183 	ppb_release_bus(ppbus, dev);
184 
185 	return (0);
186 }
187 
188 static	int
189 ppsopen(struct cdev *dev, int flags, int fmt, struct thread *td)
190 {
191 	struct pps_data *sc = dev->si_drv1;
192 	int subdev = (intptr_t)dev->si_drv2;
193 	int error, i;
194 
195 	if (!sc->busy) {
196 		device_t ppsdev = sc->ppsdev;
197 		device_t ppbus = sc->ppbus;
198 
199 		if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
200 			return (EINTR);
201 
202 		/* attach the interrupt handler */
203 		if ((error = bus_setup_intr(ppsdev, sc->intr_resource,
204 		    (INTR_TYPE_TTY | INTR_MPSAFE), ppsintr, NULL,
205 		    sc, &sc->intr_cookie))) {
206 			ppb_release_bus(ppbus, ppsdev);
207 			return (error);
208 		}
209 
210 		i = ppb_set_mode(sc->ppbus, PPB_PS2);
211 		PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
212 
213 		i = IRQENABLE | PCD | nINIT | SELECTIN;
214 		ppb_wctr(ppbus, i);
215 	}
216 	if (subdev > 0 && !(sc->busy & ~1)) {
217 		sc->timeout = timeout(ppshcpoll, sc, 1);
218 		sc->lastdata = ppb_rdtr(sc->ppbus);
219 	}
220 	sc->busy |= (1 << subdev);
221 	return(0);
222 }
223 
224 static	int
225 ppsclose(struct cdev *dev, int flags, int fmt, struct thread *td)
226 {
227 	struct pps_data *sc = dev->si_drv1;
228 	int subdev = (intptr_t)dev->si_drv2;
229 
230 	sc->pps[subdev].ppsparam.mode = 0;	/* PHK ??? */
231 	sc->busy &= ~(1 << subdev);
232 	if (subdev > 0 && !(sc->busy & ~1))
233 		untimeout(ppshcpoll, sc, sc->timeout);
234 	if (!sc->busy) {
235 		device_t ppsdev = sc->ppsdev;
236 		device_t ppbus = sc->ppbus;
237 
238 		ppb_wdtr(ppbus, 0);
239 		ppb_wctr(ppbus, 0);
240 
241 		/* Note: the interrupt handler is automatically detached */
242 		ppb_set_mode(ppbus, PPB_COMPATIBLE);
243 		ppb_release_bus(ppbus, ppsdev);
244 	}
245 	return(0);
246 }
247 
248 static void
249 ppshcpoll(void *arg)
250 {
251 	struct pps_data *sc = arg;
252 	int i, j, k, l;
253 
254 	if (!(sc->busy & ~1))
255 		return;
256 	mtx_lock_spin(&sc->mtx);
257 	sc->timeout = timeout(ppshcpoll, sc, 1);
258 	i = ppb_rdtr(sc->ppbus);
259 	if (i == sc->lastdata)
260 		return;
261 	l = sc->lastdata ^ i;
262 	k = 1;
263 	for (j = 1; j < 9; j ++) {
264 		if (l & k) {
265 			pps_capture(&sc->pps[j]);
266 			pps_event(&sc->pps[j],
267 			    i & k ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR);
268 		}
269 		k += k;
270 	}
271 	sc->lastdata = i;
272 	mtx_unlock_spin(&sc->mtx);
273 }
274 
275 static int
276 ppsintr(void *arg)
277 {
278 	struct pps_data *sc = (struct pps_data *)arg;
279 
280 	pps_capture(&sc->pps[0]);
281 	if (!(ppb_rstr(sc->ppbus) & nACK))
282 		return (FILTER_STRAY);
283 	if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
284 		ppb_wctr(sc->ppbus, IRQENABLE | AUTOFEED);
285 	mtx_lock_spin(&sc->mtx);
286 	pps_event(&sc->pps[0], PPS_CAPTUREASSERT);
287 	mtx_unlock_spin(&sc->mtx);
288 	if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
289 		ppb_wctr(sc->ppbus, IRQENABLE);
290 	return (FILTER_HANDLED);
291 }
292 
293 static int
294 ppsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
295 {
296 	struct pps_data *sc = dev->si_drv1;
297 	int subdev = (intptr_t)dev->si_drv2;
298 	int err;
299 
300 	mtx_lock_spin(&sc->mtx);
301 	err = pps_ioctl(cmd, data, &sc->pps[subdev]);
302 	mtx_unlock_spin(&sc->mtx);
303 	return (err);
304 }
305 
306 static device_method_t pps_methods[] = {
307 	/* device interface */
308 	DEVMETHOD(device_identify,	ppsidentify),
309 	DEVMETHOD(device_probe,		ppsprobe),
310 	DEVMETHOD(device_attach,	ppsattach),
311 
312 	{ 0, 0 }
313 };
314 
315 static driver_t pps_driver = {
316 	PPS_NAME,
317 	pps_methods,
318 	sizeof(struct pps_data),
319 };
320 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);
321 MODULE_DEPEND(pps, ppbus, 1, 1, 1);
322