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