This commit represents a major revamping of the clock interrupt and timebase
[dragonfly.git] / sys / dev / misc / pps / pps.c
CommitLineData
984263bc
MD
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 $
88c4d2f6 10 * $DragonFly: src/sys/dev/misc/pps/pps.c,v 1.6 2004/01/30 05:42:15 dillon Exp $
984263bc
MD
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
1f2de5d4
MD
19#include "use_pps.h"
20
984263bc
MD
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
1f2de5d4 32#include <bus/ppbus/ppbconf.h>
984263bc 33#include "ppbus_if.h"
1f2de5d4 34#include <bus/ppbus/ppbio.h>
984263bc
MD
35
36#define PPS_NAME "pps" /* our official name */
37
38struct 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
47static 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
56static devclass_t pps_devclass;
57
58static d_open_t ppsopen;
59static d_close_t ppsclose;
60static d_ioctl_t ppsioctl;
61
62#define CDEV_MAJOR 89
63static struct cdevsw pps_cdevsw = {
fabb8ceb
MD
64 /* name */ PPS_NAME,
65 /* maj */ CDEV_MAJOR,
66 /* flags */ 0,
67 /* port */ NULL,
68 /* autoq */ 0,
69
984263bc
MD
70 /* open */ ppsopen,
71 /* close */ ppsclose,
72 /* read */ noread,
73 /* write */ nowrite,
74 /* ioctl */ ppsioctl,
75 /* poll */ nopoll,
76 /* mmap */ nommap,
77 /* strategy */ nostrategy,
984263bc 78 /* dump */ nodump,
fabb8ceb 79 /* psize */ nopsize
984263bc
MD
80};
81
82static void
83ppsidentify(driver_t *driver, device_t parent)
84{
85
86 BUS_ADD_CHILD(parent, 0, PPS_NAME, 0);
87}
88
89static int
90ppsprobe(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
110static int
111ppsattach(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
132static int
7b95be2a 133ppsopen(dev_t dev, int flags, int fmt, struct thread *td)
984263bc
MD
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
161static int
7b95be2a 162ppsclose(dev_t dev, int flags, int fmt, struct thread *td)
984263bc
MD
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
180static void
181ppsintr(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);
88c4d2f6 186 sysclock_t count;
984263bc 187
88c4d2f6 188 count = cputimer_get_count();
984263bc
MD
189 if (!(ppb_rstr(ppbus) & nACK))
190 return;
191 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
192 ppb_wctr(ppbus, IRQENABLE | AUTOFEED);
88c4d2f6 193 pps_event(&sc->pps, count, PPS_CAPTUREASSERT);
984263bc
MD
194 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
195 ppb_wctr(ppbus, IRQENABLE);
196}
197
198static int
7b95be2a 199ppsioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
984263bc
MD
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
207static 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
216static driver_t pps_driver = {
217 PPS_NAME,
218 pps_methods,
219 sizeof(struct pps_data),
220};
221DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);