xref: /freebsd/sys/dev/ppbus/pps.c (revision 807a5caa14df5ff04b331e24b45893f6a2f6bc1b)
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$
10  *
11  * This driver implements a draft-mogul-pps-api-02.txt PPS source.
12  *
13  * The input pin is pin#10
14  * The echo output pin is pin#14
15  *
16  */
17 
18 #include <sys/param.h>
19 #include <sys/kernel.h>
20 #include <sys/systm.h>
21 #include <sys/module.h>
22 #include <sys/bus.h>
23 #include <sys/conf.h>
24 #include <sys/timetc.h>
25 #include <sys/timepps.h>
26 #include <sys/malloc.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 #include "pps.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 	/* open */	ppsopen,
65 	/* close */	ppsclose,
66 	/* read */	noread,
67 	/* write */	nowrite,
68 	/* ioctl */	ppsioctl,
69 	/* poll */	nopoll,
70 	/* mmap */	nommap,
71 	/* strategy */	nostrategy,
72 	/* name */	PPS_NAME,
73 	/* maj */	CDEV_MAJOR,
74 	/* dump */	nodump,
75 	/* psize */	nopsize,
76 	/* flags */	0,
77 	/* bmaj */	-1
78 };
79 
80 static void
81 ppsidentify(driver_t *driver, device_t parent)
82 {
83 
84 	BUS_ADD_CHILD(parent, 0, PPS_NAME, 0);
85 }
86 
87 static int
88 ppsprobe(device_t ppsdev)
89 {
90 	struct pps_data *sc;
91 	dev_t dev;
92 	int unit;
93 
94 	sc = DEVTOSOFTC(ppsdev);
95 	bzero(sc, sizeof(struct pps_data));
96 
97 	unit = device_get_unit(ppsdev);
98 	dev = make_dev(&pps_cdevsw, unit,
99 	    UID_ROOT, GID_WHEEL, 0644, PPS_NAME "%d", unit);
100 
101 	device_set_desc(ppsdev, "Pulse per second Timing Interface");
102 
103 	sc->pps.ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
104 	pps_init(&sc->pps);
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 	int irq, zero = 0;
114 
115 	/* retrieve the ppbus irq */
116 	BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
117 
118 	if (irq > 0) {
119 		/* declare our interrupt handler */
120 		sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
121 				       &zero, irq, irq, 1, RF_SHAREABLE);
122 	}
123 	/* interrupts seem mandatory */
124 	if (sc->intr_resource == 0)
125 		return (ENXIO);
126 
127 	return (0);
128 }
129 
130 static	int
131 ppsopen(dev_t dev, int flags, int fmt, struct proc *p)
132 {
133 	u_int unit = minor(dev);
134 	struct pps_data *sc = UNITOSOFTC(unit);
135 	device_t ppsdev = UNITODEVICE(unit);
136 	device_t ppbus = device_get_parent(ppsdev);
137 	int error;
138 
139 	if (!sc->pps_open) {
140 		if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
141 			return (EINTR);
142 
143 		/* attach the interrupt handler */
144 		if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource,
145 			       INTR_TYPE_TTY, ppsintr, ppsdev,
146 			       &sc->intr_cookie))) {
147 			ppb_release_bus(ppbus, ppsdev);
148 			return (error);
149 		}
150 
151 		ppb_wctr(ppbus, 0);
152 		ppb_wctr(ppbus, IRQENABLE);
153 		sc->pps_open = 1;
154 	}
155 
156 	return(0);
157 }
158 
159 static	int
160 ppsclose(dev_t dev, int flags, int fmt, struct proc *p)
161 {
162 	u_int unit = minor(dev);
163 	struct pps_data *sc = UNITOSOFTC(unit);
164 	device_t ppsdev = UNITODEVICE(unit);
165 	device_t ppbus = device_get_parent(ppsdev);
166 
167 	sc->pps.ppsparam.mode = 0;	/* PHK ??? */
168 
169 	ppb_wdtr(ppbus, 0);
170 	ppb_wctr(ppbus, 0);
171 
172 	/* Note: the interrupt handler is automatically detached */
173 	ppb_release_bus(ppbus, ppsdev);
174 	sc->pps_open = 0;
175 	return(0);
176 }
177 
178 static void
179 ppsintr(void *arg)
180 {
181 	device_t ppsdev = (device_t)arg;
182 	device_t ppbus = device_get_parent(ppsdev);
183 	struct pps_data *sc = DEVTOSOFTC(ppsdev);
184 	struct timecounter *tc;
185 	unsigned count;
186 
187 	tc = timecounter;
188 	count = timecounter->tc_get_timecount(tc);
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, tc, 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 proc *p)
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