Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / misc / pps / pps.c
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  *
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 <machine/bus.h>
26 #include <machine/resource.h>
27 #include <sys/rman.h>
28
29 #include <dev/ppbus/ppbconf.h>
30 #include "ppbus_if.h"
31 #include <dev/ppbus/ppbio.h>
32 #include "pps.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         /* bmaj */      -1
76 };
77
78 static void
79 ppsidentify(driver_t *driver, device_t parent)
80 {
81
82         BUS_ADD_CHILD(parent, 0, PPS_NAME, 0);
83 }
84
85 static int
86 ppsprobe(device_t ppsdev)
87 {
88         struct pps_data *sc;
89         dev_t dev;
90         int unit;
91
92         sc = DEVTOSOFTC(ppsdev);
93         bzero(sc, sizeof(struct pps_data));
94
95         unit = device_get_unit(ppsdev);
96         dev = make_dev(&pps_cdevsw, unit,
97             UID_ROOT, GID_WHEEL, 0644, PPS_NAME "%d", unit);
98
99         device_set_desc(ppsdev, "Pulse per second Timing Interface");
100
101         sc->pps.ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
102         pps_init(&sc->pps);
103         return (0);
104 }
105
106 static int
107 ppsattach(device_t dev)
108 {
109         struct pps_data *sc = DEVTOSOFTC(dev);
110         device_t ppbus = device_get_parent(dev);
111         int irq, zero = 0;
112
113         /* retrieve the ppbus irq */
114         BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
115
116         if (irq > 0) {
117                 /* declare our interrupt handler */
118                 sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
119                                        &zero, irq, irq, 1, RF_SHAREABLE);
120         }
121         /* interrupts seem mandatory */
122         if (sc->intr_resource == 0)
123                 return (ENXIO);
124
125         return (0);
126 }
127
128 static  int
129 ppsopen(dev_t dev, int flags, int fmt, struct proc *p)
130 {
131         u_int unit = minor(dev);
132         struct pps_data *sc = UNITOSOFTC(unit);
133         device_t ppsdev = UNITODEVICE(unit);
134         device_t ppbus = device_get_parent(ppsdev);
135         int error;
136
137         if (!sc->pps_open) {
138                 if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
139                         return (EINTR);
140
141                 /* attach the interrupt handler */
142                 if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource,
143                                INTR_TYPE_TTY, ppsintr, ppsdev,
144                                &sc->intr_cookie))) {
145                         ppb_release_bus(ppbus, ppsdev);
146                         return (error);
147                 }
148
149                 ppb_wctr(ppbus, 0);
150                 ppb_wctr(ppbus, IRQENABLE);
151                 sc->pps_open = 1;
152         }
153
154         return(0);
155 }
156
157 static  int
158 ppsclose(dev_t dev, int flags, int fmt, struct proc *p)
159 {
160         u_int unit = minor(dev);
161         struct pps_data *sc = UNITOSOFTC(unit);
162         device_t ppsdev = UNITODEVICE(unit);
163         device_t ppbus = device_get_parent(ppsdev);
164
165         sc->pps.ppsparam.mode = 0;      /* PHK ??? */
166
167         ppb_wdtr(ppbus, 0);
168         ppb_wctr(ppbus, 0);
169
170         /* Note: the interrupt handler is automatically detached */
171         ppb_release_bus(ppbus, ppsdev);
172         sc->pps_open = 0;
173         return(0);
174 }
175
176 static void
177 ppsintr(void *arg)
178 {
179         device_t ppsdev = (device_t)arg;
180         device_t ppbus = device_get_parent(ppsdev);
181         struct pps_data *sc = DEVTOSOFTC(ppsdev);
182         struct timecounter *tc;
183         unsigned count;
184
185         tc = timecounter;
186         count = timecounter->tc_get_timecount(tc);
187         if (!(ppb_rstr(ppbus) & nACK))
188                 return;
189         if (sc->pps.ppsparam.mode & PPS_ECHOASSERT) 
190                 ppb_wctr(ppbus, IRQENABLE | AUTOFEED);
191         pps_event(&sc->pps, tc, count, PPS_CAPTUREASSERT);
192         if (sc->pps.ppsparam.mode & PPS_ECHOASSERT) 
193                 ppb_wctr(ppbus, IRQENABLE);
194 }
195
196 static int
197 ppsioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
198 {
199         u_int unit = minor(dev);
200         struct pps_data *sc = UNITOSOFTC(unit);
201
202         return (pps_ioctl(cmd, data, &sc->pps));
203 }
204
205 static device_method_t pps_methods[] = {
206         /* device interface */
207         DEVMETHOD(device_identify,      ppsidentify),
208         DEVMETHOD(device_probe,         ppsprobe),
209         DEVMETHOD(device_attach,        ppsattach),
210
211         { 0, 0 }
212 };
213
214 static driver_t pps_driver = {
215         PPS_NAME,
216         pps_methods,
217         sizeof(struct pps_data),
218 };
219 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);