xref: /freebsd/sys/dev/ppbus/pps.c (revision f0adf7f5cdd241db2f2c817683191a6ef64a4e95)
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 resource *intr_resource;	/* interrupt resource */
50 	void *intr_cookie;		/* interrupt registration cookie */
51 };
52 
53 static void	ppsintr(void *arg);
54 static void 	ppshcpoll(void *arg);
55 
56 #define DEVTOSOFTC(dev) \
57 	((struct pps_data *)device_get_softc(dev))
58 
59 static devclass_t pps_devclass;
60 
61 static	d_open_t	ppsopen;
62 static	d_close_t	ppsclose;
63 static	d_ioctl_t	ppsioctl;
64 
65 static struct cdevsw pps_cdevsw = {
66 	.d_version =	D_VERSION,
67 	.d_flags =	D_NEEDGIANT,
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, 0);
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 	intptr_t irq;
111 	int i, unit, zero = 0;
112 
113 	/* retrieve the ppbus irq */
114 	BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
115 
116 	if (irq > 0) {
117 		/* declare our interrupt handler */
118 		sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
119 		    &zero, irq, irq, 1, RF_SHAREABLE);
120 	}
121 	/* interrupts seem mandatory */
122 	if (sc->intr_resource == NULL)
123 		return (ENXIO);
124 
125 	sc->ppsdev = dev;
126 	sc->ppbus = ppbus;
127 	unit = device_get_unit(ppbus);
128 	d = make_dev(&pps_cdevsw, unit,
129 	    UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%d", unit);
130 	sc->devs[0] = d;
131 	sc->pps[0].ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
132 	d->si_drv1 = sc;
133 	d->si_drv2 = (void*)0;
134 	pps_init(&sc->pps[0]);
135 
136 	if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT))
137 		return (0);
138 
139 	do {
140 		i = ppb_set_mode(sc->ppbus, PPB_EPP);
141 		PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
142 		if (i == -1)
143 			break;
144 		i = 0;
145 		ppb_wctr(ppbus, i);
146 		if (ppstry(ppbus, 0x00, 0x00))
147 			break;
148 		if (ppstry(ppbus, 0x55, 0x55))
149 			break;
150 		if (ppstry(ppbus, 0xaa, 0xaa))
151 			break;
152 		if (ppstry(ppbus, 0xff, 0xff))
153 			break;
154 
155 		i = IRQENABLE | PCD | STROBE | nINIT | SELECTIN;
156 		ppb_wctr(ppbus, i);
157 		PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
158 		if (ppstry(ppbus, 0x00, 0x00))
159 			break;
160 		if (ppstry(ppbus, 0x55, 0x00))
161 			break;
162 		if (ppstry(ppbus, 0xaa, 0x00))
163 			break;
164 		if (ppstry(ppbus, 0xff, 0x00))
165 			break;
166 
167 		i = IRQENABLE | PCD | nINIT | SELECTIN;
168 		ppb_wctr(ppbus, i);
169 		PRVERBOSE("CTR = %02x (%02x)\n", ppb_rctr(ppbus), i);
170 		ppstry(ppbus, 0x00, 0xff);
171 		ppstry(ppbus, 0x55, 0xff);
172 		ppstry(ppbus, 0xaa, 0xff);
173 		ppstry(ppbus, 0xff, 0xff);
174 
175 		for (i = 1; i < 9; i++) {
176 			d = make_dev(&pps_cdevsw, unit + 0x10000 * i,
177 			  UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%db%d", unit, i - 1);
178 			sc->devs[i] = d;
179 			sc->pps[i].ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
180 			d->si_drv1 = sc;
181 			d->si_drv2 = (void *)(intptr_t)i;
182 			pps_init(&sc->pps[i]);
183 		}
184 	} while (0);
185 	i = ppb_set_mode(sc->ppbus, PPB_COMPATIBLE);
186 	ppb_release_bus(ppbus, dev);
187 
188 	return (0);
189 }
190 
191 static	int
192 ppsopen(struct cdev *dev, int flags, int fmt, struct thread *td)
193 {
194 	struct pps_data *sc = dev->si_drv1;
195 	int subdev = (intptr_t)dev->si_drv2;
196 	int error, i;
197 
198 	if (!sc->busy) {
199 		device_t ppsdev = sc->ppsdev;
200 		device_t ppbus = sc->ppbus;
201 
202 		if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
203 			return (EINTR);
204 
205 		/* attach the interrupt handler */
206 		if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource,
207 			       INTR_TYPE_TTY, ppsintr, ppsdev,
208 			       &sc->intr_cookie))) {
209 			ppb_release_bus(ppbus, ppsdev);
210 			return (error);
211 		}
212 
213 		i = ppb_set_mode(sc->ppbus, PPB_PS2);
214 		PRVERBOSE("EPP: %d %d\n", i, PPB_IN_EPP_MODE(sc->ppbus));
215 
216 		i = IRQENABLE | PCD | nINIT | SELECTIN;
217 		ppb_wctr(ppbus, i);
218 	}
219 	if (subdev > 0 && !(sc->busy & ~1)) {
220 		sc->timeout = timeout(ppshcpoll, sc, 1);
221 		sc->lastdata = ppb_rdtr(sc->ppbus);
222 	}
223 	sc->busy |= (1 << subdev);
224 	return(0);
225 }
226 
227 static	int
228 ppsclose(struct cdev *dev, int flags, int fmt, struct thread *td)
229 {
230 	struct pps_data *sc = dev->si_drv1;
231 	int subdev = (intptr_t)dev->si_drv2;
232 
233 	sc->pps[subdev].ppsparam.mode = 0;	/* PHK ??? */
234 	sc->busy &= ~(1 << subdev);
235 	if (subdev > 0 && !(sc->busy & ~1))
236 		untimeout(ppshcpoll, sc, sc->timeout);
237 	if (!sc->busy) {
238 		device_t ppsdev = sc->ppsdev;
239 		device_t ppbus = sc->ppbus;
240 
241 		ppb_wdtr(ppbus, 0);
242 		ppb_wctr(ppbus, 0);
243 
244 		/* Note: the interrupt handler is automatically detached */
245 		ppb_set_mode(ppbus, PPB_COMPATIBLE);
246 		ppb_release_bus(ppbus, ppsdev);
247 	}
248 	return(0);
249 }
250 
251 static void
252 ppshcpoll(void *arg)
253 {
254 	struct pps_data *sc = arg;
255 	int i, j, k, l;
256 
257 	if (!(sc->busy & ~1))
258 		return;
259 	sc->timeout = timeout(ppshcpoll, sc, 1);
260 	i = ppb_rdtr(sc->ppbus);
261 	if (i == sc->lastdata)
262 		return;
263 	l = sc->lastdata ^ i;
264 	k = 1;
265 	for (j = 1; j < 9; j ++) {
266 		if (l & k) {
267 			pps_capture(&sc->pps[j]);
268 			pps_event(&sc->pps[j],
269 			    i & k ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR);
270 		}
271 		k += k;
272 	}
273 	sc->lastdata = i;
274 }
275 
276 static void
277 ppsintr(void *arg)
278 {
279 	device_t ppsdev = (device_t)arg;
280 	struct pps_data *sc = DEVTOSOFTC(ppsdev);
281 	device_t ppbus = sc->ppbus;
282 
283 	pps_capture(&sc->pps[0]);
284 	if (!(ppb_rstr(ppbus) & nACK))
285 		return;
286 	if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
287 		ppb_wctr(ppbus, IRQENABLE | AUTOFEED);
288 	pps_event(&sc->pps[0], PPS_CAPTUREASSERT);
289 	if (sc->pps[0].ppsparam.mode & PPS_ECHOASSERT)
290 		ppb_wctr(ppbus, IRQENABLE);
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 
299 	return (pps_ioctl(cmd, data, &sc->pps[subdev]));
300 }
301 
302 static device_method_t pps_methods[] = {
303 	/* device interface */
304 	DEVMETHOD(device_identify,	ppsidentify),
305 	DEVMETHOD(device_probe,		ppsprobe),
306 	DEVMETHOD(device_attach,	ppsattach),
307 
308 	{ 0, 0 }
309 };
310 
311 static driver_t pps_driver = {
312 	PPS_NAME,
313 	pps_methods,
314 	sizeof(struct pps_data),
315 };
316 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);
317