xref: /dragonfly/sys/dev/misc/pps/pps.c (revision 743230ab)
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  * $FreeBSD: src/sys/dev/ppbus/pps.c,v 1.24.2.1 2000/05/24 00:20:57 n_hibma Exp $
10  * $DragonFly: src/sys/dev/misc/pps/pps.c,v 1.6 2004/01/30 05:42:15 dillon Exp $
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 "use_pps.h"
20 
21 #include <sys/param.h>
22 #include <sys/kernel.h>
23 #include <sys/systm.h>
24 #include <sys/module.h>
25 #include <sys/bus.h>
26 #include <sys/conf.h>
27 #include <sys/timepps.h>
28 #include <machine/bus.h>
29 #include <machine/resource.h>
30 #include <sys/rman.h>
31 
32 #include <bus/ppbus/ppbconf.h>
33 #include "ppbus_if.h"
34 #include <bus/ppbus/ppbio.h>
35 
36 #define PPS_NAME	"pps"		/* our official name */
37 
38 struct pps_data {
39 	int	pps_open;
40 	struct	ppb_device pps_dev;
41 	struct	pps_state pps;
42 
43 	struct resource *intr_resource;	/* interrupt resource */
44 	void *intr_cookie;		/* interrupt registration cookie */
45 };
46 
47 static void	ppsintr(void *arg);
48 
49 #define DEVTOSOFTC(dev) \
50 	((struct pps_data *)device_get_softc(dev))
51 #define UNITOSOFTC(unit) \
52 	((struct pps_data *)devclass_get_softc(pps_devclass, (unit)))
53 #define UNITODEVICE(unit) \
54 	(devclass_get_device(pps_devclass, (unit)))
55 
56 static devclass_t pps_devclass;
57 
58 static	d_open_t	ppsopen;
59 static	d_close_t	ppsclose;
60 static	d_ioctl_t	ppsioctl;
61 
62 #define CDEV_MAJOR 89
63 static struct cdevsw pps_cdevsw = {
64 	/* name */	PPS_NAME,
65 	/* maj */	CDEV_MAJOR,
66 	/* flags */	0,
67 	/* port */	NULL,
68 	/* autoq */	0,
69 
70 	/* open */	ppsopen,
71 	/* close */	ppsclose,
72 	/* read */	noread,
73 	/* write */	nowrite,
74 	/* ioctl */	ppsioctl,
75 	/* poll */	nopoll,
76 	/* mmap */	nommap,
77 	/* strategy */	nostrategy,
78 	/* dump */	nodump,
79 	/* psize */	nopsize
80 };
81 
82 static void
83 ppsidentify(driver_t *driver, device_t parent)
84 {
85 
86 	BUS_ADD_CHILD(parent, 0, PPS_NAME, 0);
87 }
88 
89 static int
90 ppsprobe(device_t ppsdev)
91 {
92 	struct pps_data *sc;
93 	dev_t dev;
94 	int unit;
95 
96 	sc = DEVTOSOFTC(ppsdev);
97 	bzero(sc, sizeof(struct pps_data));
98 
99 	unit = device_get_unit(ppsdev);
100 	dev = make_dev(&pps_cdevsw, unit,
101 	    UID_ROOT, GID_WHEEL, 0644, PPS_NAME "%d", unit);
102 
103 	device_set_desc(ppsdev, "Pulse per second Timing Interface");
104 
105 	sc->pps.ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
106 	pps_init(&sc->pps);
107 	return (0);
108 }
109 
110 static int
111 ppsattach(device_t dev)
112 {
113 	struct pps_data *sc = DEVTOSOFTC(dev);
114 	device_t ppbus = device_get_parent(dev);
115 	int irq, zero = 0;
116 
117 	/* retrieve the ppbus irq */
118 	BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
119 
120 	if (irq > 0) {
121 		/* declare our interrupt handler */
122 		sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
123 				       &zero, irq, irq, 1, RF_SHAREABLE);
124 	}
125 	/* interrupts seem mandatory */
126 	if (sc->intr_resource == 0)
127 		return (ENXIO);
128 
129 	return (0);
130 }
131 
132 static	int
133 ppsopen(dev_t dev, int flags, int fmt, struct thread *td)
134 {
135 	u_int unit = minor(dev);
136 	struct pps_data *sc = UNITOSOFTC(unit);
137 	device_t ppsdev = UNITODEVICE(unit);
138 	device_t ppbus = device_get_parent(ppsdev);
139 	int error;
140 
141 	if (!sc->pps_open) {
142 		if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
143 			return (EINTR);
144 
145 		/* attach the interrupt handler */
146 		if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource,
147 			       INTR_TYPE_TTY, ppsintr, ppsdev,
148 			       &sc->intr_cookie))) {
149 			ppb_release_bus(ppbus, ppsdev);
150 			return (error);
151 		}
152 
153 		ppb_wctr(ppbus, 0);
154 		ppb_wctr(ppbus, IRQENABLE);
155 		sc->pps_open = 1;
156 	}
157 
158 	return(0);
159 }
160 
161 static	int
162 ppsclose(dev_t dev, int flags, int fmt, struct thread *td)
163 {
164 	u_int unit = minor(dev);
165 	struct pps_data *sc = UNITOSOFTC(unit);
166 	device_t ppsdev = UNITODEVICE(unit);
167 	device_t ppbus = device_get_parent(ppsdev);
168 
169 	sc->pps.ppsparam.mode = 0;	/* PHK ??? */
170 
171 	ppb_wdtr(ppbus, 0);
172 	ppb_wctr(ppbus, 0);
173 
174 	/* Note: the interrupt handler is automatically detached */
175 	ppb_release_bus(ppbus, ppsdev);
176 	sc->pps_open = 0;
177 	return(0);
178 }
179 
180 static void
181 ppsintr(void *arg)
182 {
183 	device_t ppsdev = (device_t)arg;
184 	device_t ppbus = device_get_parent(ppsdev);
185 	struct pps_data *sc = DEVTOSOFTC(ppsdev);
186 	sysclock_t count;
187 
188 	count = cputimer_get_count();
189 	if (!(ppb_rstr(ppbus) & nACK))
190 		return;
191 	if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
192 		ppb_wctr(ppbus, IRQENABLE | AUTOFEED);
193 	pps_event(&sc->pps, count, PPS_CAPTUREASSERT);
194 	if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
195 		ppb_wctr(ppbus, IRQENABLE);
196 }
197 
198 static int
199 ppsioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
200 {
201 	u_int unit = minor(dev);
202 	struct pps_data *sc = UNITOSOFTC(unit);
203 
204 	return (pps_ioctl(cmd, data, &sc->pps));
205 }
206 
207 static device_method_t pps_methods[] = {
208 	/* device interface */
209 	DEVMETHOD(device_identify,	ppsidentify),
210 	DEVMETHOD(device_probe,		ppsprobe),
211 	DEVMETHOD(device_attach,	ppsattach),
212 
213 	{ 0, 0 }
214 };
215 
216 static driver_t pps_driver = {
217 	PPS_NAME,
218 	pps_methods,
219 	sizeof(struct pps_data),
220 };
221 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);
222