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