Initial import from FreeBSD RELENG_4:
[games.git] / sys / dev / misc / pcfclock / pcfclock.c
1 /*
2  * Copyright (c) 2000 Sascha Schumann. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 
13  * THIS SOFTWARE IS PROVIDED BY SASCHA SCHUMANN ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
16  * EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
19  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
20  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
22  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * $FreeBSD: src/sys/dev/ppbus/pcfclock.c,v 1.3.2.1 2000/05/24 00:20:57 n_hibma Exp $
25  *
26  */
27
28 #include "opt_pcfclock.h"
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/sockio.h>
34 #include <sys/mbuf.h>
35 #include <sys/kernel.h>
36 #include <sys/conf.h>
37 #include <sys/fcntl.h>
38 #include <sys/uio.h>
39
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42 #include <machine/clock.h>      /* for DELAY */
43
44 #include <dev/ppbus/ppbconf.h>
45 #include <dev/ppbus/ppb_msq.h>
46 #include <dev/ppbus/ppbio.h>
47
48 #include "ppbus_if.h"
49
50 #define PCFCLOCK_NAME "pcfclock"
51
52 struct pcfclock_data {
53         int     count;
54         struct  ppb_device pcfclock_dev;
55 };
56
57 #define DEVTOSOFTC(dev) \
58         ((struct pcfclock_data *)device_get_softc(dev))
59 #define UNITOSOFTC(unit) \
60         ((struct pcfclock_data *)devclass_get_softc(pcfclock_devclass, (unit)))
61 #define UNITODEVICE(unit) \
62         (devclass_get_device(pcfclock_devclass, (unit)))
63
64 static devclass_t pcfclock_devclass;
65
66 static  d_open_t                pcfclock_open;
67 static  d_close_t               pcfclock_close;
68 static  d_read_t                pcfclock_read;
69
70 #define CDEV_MAJOR 140
71 static struct cdevsw pcfclock_cdevsw = {
72         /* open */      pcfclock_open,
73         /* close */     pcfclock_close,
74         /* read */      pcfclock_read,
75         /* write */     nowrite,
76         /* ioctl */     noioctl,
77         /* poll */      nopoll,
78         /* mmap */      nommap,
79         /* strategy */  nostrategy,
80         /* name */      PCFCLOCK_NAME,
81         /* maj */       CDEV_MAJOR,
82         /* dump */      nodump,
83         /* psize */     nopsize,
84         /* flags */     0,
85         /* bmaj */      -1
86 };
87
88 #ifndef PCFCLOCK_MAX_RETRIES
89 #define PCFCLOCK_MAX_RETRIES 10
90 #endif
91
92 #define AFC_HI 0
93 #define AFC_LO AUTOFEED
94
95 /* AUTO FEED is used as clock */
96 #define AUTOFEED_CLOCK(val) \
97         ctr = (ctr & ~(AUTOFEED)) ^ (val); ppb_wctr(ppbus, ctr)
98
99 /* SLCT is used as clock */
100 #define CLOCK_OK \
101         ((ppb_rstr(ppbus) & SELECT) == (i & 1 ? SELECT : 0))
102
103 /* PE is used as data */
104 #define BIT_SET (ppb_rstr(ppbus)&PERROR)
105
106 /* the first byte sent as reply must be 00001001b */
107 #define PCFCLOCK_CORRECT_SYNC(buf) (buf[0] == 9)
108
109 #define NR(buf, off) (buf[off+1]*10+buf[off])
110
111 /* check for correct input values */
112 #define PCFCLOCK_CORRECT_FORMAT(buf) (\
113         NR(buf, 14) <= 99 && \
114         NR(buf, 12) <= 12 && \
115         NR(buf, 10) <= 31 && \
116         NR(buf,  6) <= 23 && \
117         NR(buf,  4) <= 59 && \
118         NR(buf,  2) <= 59)
119
120 #define PCFCLOCK_BATTERY_STATUS_LOW(buf) (buf[8] & 4)
121          
122 #define PCFCLOCK_CMD_TIME 0             /* send current time */
123 #define PCFCLOCK_CMD_COPY 7     /* copy received signal to PC */
124
125 static void
126 pcfclock_identify(driver_t *driver, device_t parent)
127 {
128
129         BUS_ADD_CHILD(parent, 0, PCFCLOCK_NAME, 0);
130 }
131
132 static int
133 pcfclock_probe(device_t dev)
134 {
135         struct pcfclock_data *sc;
136
137         device_set_desc(dev, "PCF-1.0");
138
139         sc = DEVTOSOFTC(dev);
140         bzero(sc, sizeof(struct pcfclock_data));
141         
142         return (0);
143 }
144
145 static int
146 pcfclock_attach(device_t dev)
147 {
148         int unit;
149         
150         unit = device_get_unit(dev);
151
152         make_dev(&pcfclock_cdevsw, unit,
153                         UID_ROOT, GID_WHEEL, 0444, PCFCLOCK_NAME "%d", unit);
154
155         return (0);
156 }
157
158 static int 
159 pcfclock_open(dev_t dev, int flag, int fms, struct proc *p)
160 {
161         u_int unit = minor(dev);
162         struct pcfclock_data *sc = UNITOSOFTC(unit);
163         device_t pcfclockdev = UNITODEVICE(unit);
164         device_t ppbus = device_get_parent(pcfclockdev);
165         int res;
166         
167         if (!sc)
168                 return (ENXIO);
169
170         if ((res = ppb_request_bus(ppbus, pcfclockdev,
171                 (flag & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT)))
172                 return (res);
173
174         sc->count++;
175         
176         return (0);
177 }
178
179 static int
180 pcfclock_close(dev_t dev, int flags, int fmt, struct proc *p)
181 {
182         u_int unit = minor(dev);
183         struct pcfclock_data *sc = UNITOSOFTC(unit);
184         device_t pcfclockdev = UNITODEVICE(unit);
185         device_t ppbus = device_get_parent(pcfclockdev);
186
187         sc->count--;
188         if (sc->count == 0) {
189                 ppb_release_bus(ppbus, pcfclockdev);
190         }
191
192         return (0);
193 }
194
195 static void
196 pcfclock_write_cmd(dev_t dev, unsigned char command)
197 {
198         u_int unit = minor(dev);
199         device_t ppidev = UNITODEVICE(unit);
200         device_t ppbus = device_get_parent(ppidev);
201         unsigned char ctr = 14;
202         char i;
203         
204         for (i = 0; i <= 7; i++) {
205                 ppb_wdtr(ppbus, i);
206                 AUTOFEED_CLOCK(i & 1 ? AFC_HI : AFC_LO);
207                 DELAY(3000);
208         }
209         ppb_wdtr(ppbus, command);
210         AUTOFEED_CLOCK(AFC_LO);
211         DELAY(3000);
212         AUTOFEED_CLOCK(AFC_HI);
213 }
214
215 static void
216 pcfclock_display_data(dev_t dev, char buf[18]) 
217 {
218         u_int unit = minor(dev);
219 #ifdef PCFCLOCK_VERBOSE
220         int year;
221
222         year = NR(buf, 14);
223         if (year < 70)
224                 year += 100;
225         printf(PCFCLOCK_NAME "%d: %02d.%02d.%4d %02d:%02d:%02d, "
226                         "battery status: %s\n",
227                         unit,
228                         NR(buf, 10), NR(buf, 12), 1900 + year,
229                         NR(buf, 6), NR(buf, 4), NR(buf, 2),
230                         PCFCLOCK_BATTERY_STATUS_LOW(buf) ? "LOW" : "ok");
231 #else
232         if (PCFCLOCK_BATTERY_STATUS_LOW(buf))
233                 printf(PCFCLOCK_NAME "%d: BATTERY STATUS LOW ON\n",
234                                 unit);
235 #endif
236 }
237
238 static int 
239 pcfclock_read_data(dev_t dev, char *buf, ssize_t bits)
240 {
241         u_int unit = minor(dev);
242         device_t ppidev = UNITODEVICE(unit);
243         device_t ppbus = device_get_parent(ppidev);
244         int i;
245         char waitfor;
246         int offset;
247
248         /* one byte per four bits */
249         bzero(buf, ((bits + 3) >> 2) + 1);
250         
251         waitfor = 100;
252         for (i = 0; i <= bits; i++) {
253                 /* wait for clock, maximum (waitfor*100) usec */
254                 while(!CLOCK_OK && --waitfor > 0)
255                         DELAY(100);
256
257                 /* timed out? */
258                 if (!waitfor) 
259                         return (EIO);
260                 
261                 waitfor = 100; /* reload */
262                 
263                 /* give it some time */
264                 DELAY(500);
265
266                 /* calculate offset into buffer */
267                 offset = i >> 2;
268                 buf[offset] <<= 1;
269
270                 if (BIT_SET)
271                         buf[offset] |= 1;
272         }
273
274         return (0);
275 }
276
277 static int 
278 pcfclock_read_dev(dev_t dev, char *buf, int maxretries) 
279 {
280         u_int unit = minor(dev);
281         device_t ppidev = UNITODEVICE(unit);
282         device_t ppbus = device_get_parent(ppidev);
283         int error = 0;
284
285         ppb_set_mode(ppbus, PPB_COMPATIBLE);
286
287         while (--maxretries > 0) {
288                 pcfclock_write_cmd(dev, PCFCLOCK_CMD_TIME);
289                 if (pcfclock_read_data(dev, buf, 68))
290                         continue;
291                         
292                 if (!PCFCLOCK_CORRECT_SYNC(buf))
293                         continue;
294
295                 if (!PCFCLOCK_CORRECT_FORMAT(buf))
296                         continue;
297
298                 break;
299         }
300
301         if (!maxretries)
302                 error = EIO;
303         
304         return (error);
305 }
306
307 static ssize_t
308 pcfclock_read(dev_t dev, struct uio *uio, int ioflag)
309 {
310         u_int unit = minor(dev);
311         char buf[18];
312         int error = 0;
313
314         if (uio->uio_resid < 18)
315                 return (ERANGE);
316
317         error = pcfclock_read_dev(dev, buf, PCFCLOCK_MAX_RETRIES);
318         
319         if (error) {
320                 printf(PCFCLOCK_NAME "%d: no PCF found\n", unit);
321         } else {
322                 pcfclock_display_data(dev, buf);
323                 
324                 uiomove(buf, 18, uio);
325         }
326         
327         return (error);
328 }
329
330 static device_method_t pcfclock_methods[] = {
331         /* device interface */
332         DEVMETHOD(device_identify,      pcfclock_identify),
333         DEVMETHOD(device_probe,         pcfclock_probe),
334         DEVMETHOD(device_attach,        pcfclock_attach),
335
336         { 0, 0 }
337 };
338
339 static driver_t pcfclock_driver = {
340         PCFCLOCK_NAME,
341         pcfclock_methods,
342         sizeof(struct pcfclock_data),
343 };
344
345 DRIVER_MODULE(pcfclock, ppbus, pcfclock_driver, pcfclock_devclass, 0, 0);