2 * Copyright (c) 2000 Sascha Schumann. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
24 * $FreeBSD: src/sys/dev/ppbus/pcfclock.c,v 1.3.2.1 2000/05/24 00:20:57 n_hibma Exp $
25 * $DragonFly: src/sys/dev/misc/pcfclock/pcfclock.c,v 1.12 2006/12/22 23:26:18 swildner Exp $
29 #include "opt_pcfclock.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/device.h>
36 #include <sys/sockio.h>
38 #include <sys/kernel.h>
39 #include <sys/fcntl.h>
42 #include <machine/clock.h> /* for DELAY */
44 #include <bus/ppbus/ppbconf.h>
45 #include <bus/ppbus/ppb_msq.h>
46 #include <bus/ppbus/ppbio.h>
50 #define PCFCLOCK_NAME "pcfclock"
52 struct pcfclock_data {
54 struct ppb_device pcfclock_dev;
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)))
64 static devclass_t pcfclock_devclass;
66 static d_open_t pcfclock_open;
67 static d_close_t pcfclock_close;
68 static d_read_t pcfclock_read;
70 #define CDEV_MAJOR 140
71 static struct dev_ops pcfclock_ops = {
72 { PCFCLOCK_NAME, CDEV_MAJOR, 0 },
73 .d_open = pcfclock_open,
74 .d_close = pcfclock_close,
75 .d_read = pcfclock_read,
78 #ifndef PCFCLOCK_MAX_RETRIES
79 #define PCFCLOCK_MAX_RETRIES 10
83 #define AFC_LO AUTOFEED
85 /* AUTO FEED is used as clock */
86 #define AUTOFEED_CLOCK(val) \
87 ctr = (ctr & ~(AUTOFEED)) ^ (val); ppb_wctr(ppbus, ctr)
89 /* SLCT is used as clock */
91 ((ppb_rstr(ppbus) & SELECT) == (i & 1 ? SELECT : 0))
93 /* PE is used as data */
94 #define BIT_SET (ppb_rstr(ppbus)&PERROR)
96 /* the first byte sent as reply must be 00001001b */
97 #define PCFCLOCK_CORRECT_SYNC(buf) (buf[0] == 9)
99 #define NR(buf, off) (buf[off+1]*10+buf[off])
101 /* check for correct input values */
102 #define PCFCLOCK_CORRECT_FORMAT(buf) (\
103 NR(buf, 14) <= 99 && \
104 NR(buf, 12) <= 12 && \
105 NR(buf, 10) <= 31 && \
106 NR(buf, 6) <= 23 && \
107 NR(buf, 4) <= 59 && \
110 #define PCFCLOCK_BATTERY_STATUS_LOW(buf) (buf[8] & 4)
112 #define PCFCLOCK_CMD_TIME 0 /* send current time */
113 #define PCFCLOCK_CMD_COPY 7 /* copy received signal to PC */
116 pcfclock_probe(device_t dev)
118 struct pcfclock_data *sc;
120 device_set_desc(dev, "PCF-1.0");
122 sc = DEVTOSOFTC(dev);
123 bzero(sc, sizeof(struct pcfclock_data));
129 pcfclock_attach(device_t dev)
133 unit = device_get_unit(dev);
135 dev_ops_add(&pcfclock_ops, -1, unit);
136 make_dev(&pcfclock_ops, unit,
137 UID_ROOT, GID_WHEEL, 0444, PCFCLOCK_NAME "%d", unit);
143 pcfclock_open(struct dev_open_args *ap)
145 cdev_t dev = ap->a_head.a_dev;
146 u_int unit = minor(dev);
147 struct pcfclock_data *sc = UNITOSOFTC(unit);
148 device_t pcfclockdev = UNITODEVICE(unit);
149 device_t ppbus = device_get_parent(pcfclockdev);
155 if ((res = ppb_request_bus(ppbus, pcfclockdev,
156 (ap->a_oflags & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT)))
165 pcfclock_close(struct dev_close_args *ap)
167 cdev_t dev = ap->a_head.a_dev;
168 u_int unit = minor(dev);
169 struct pcfclock_data *sc = UNITOSOFTC(unit);
170 device_t pcfclockdev = UNITODEVICE(unit);
171 device_t ppbus = device_get_parent(pcfclockdev);
174 if (sc->count == 0) {
175 ppb_release_bus(ppbus, pcfclockdev);
182 pcfclock_write_cmd(cdev_t dev, unsigned char command)
184 u_int unit = minor(dev);
185 device_t ppidev = UNITODEVICE(unit);
186 device_t ppbus = device_get_parent(ppidev);
187 unsigned char ctr = 14;
190 for (i = 0; i <= 7; i++) {
192 AUTOFEED_CLOCK(i & 1 ? AFC_HI : AFC_LO);
195 ppb_wdtr(ppbus, command);
196 AUTOFEED_CLOCK(AFC_LO);
198 AUTOFEED_CLOCK(AFC_HI);
202 pcfclock_display_data(cdev_t dev, char buf[18])
204 u_int unit = minor(dev);
205 #ifdef PCFCLOCK_VERBOSE
211 kprintf(PCFCLOCK_NAME "%d: %02d.%02d.%4d %02d:%02d:%02d, "
212 "battery status: %s\n",
214 NR(buf, 10), NR(buf, 12), 1900 + year,
215 NR(buf, 6), NR(buf, 4), NR(buf, 2),
216 PCFCLOCK_BATTERY_STATUS_LOW(buf) ? "LOW" : "ok");
218 if (PCFCLOCK_BATTERY_STATUS_LOW(buf))
219 kprintf(PCFCLOCK_NAME "%d: BATTERY STATUS LOW ON\n",
225 pcfclock_read_data(cdev_t dev, char *buf, ssize_t bits)
227 u_int unit = minor(dev);
228 device_t ppidev = UNITODEVICE(unit);
229 device_t ppbus = device_get_parent(ppidev);
234 /* one byte per four bits */
235 bzero(buf, ((bits + 3) >> 2) + 1);
238 for (i = 0; i <= bits; i++) {
239 /* wait for clock, maximum (waitfor*100) usec */
240 while(!CLOCK_OK && --waitfor > 0)
247 waitfor = 100; /* reload */
249 /* give it some time */
252 /* calculate offset into buffer */
264 pcfclock_read_dev(cdev_t dev, char *buf, int maxretries)
266 u_int unit = minor(dev);
267 device_t ppidev = UNITODEVICE(unit);
268 device_t ppbus = device_get_parent(ppidev);
271 ppb_set_mode(ppbus, PPB_COMPATIBLE);
273 while (--maxretries > 0) {
274 pcfclock_write_cmd(dev, PCFCLOCK_CMD_TIME);
275 if (pcfclock_read_data(dev, buf, 68))
278 if (!PCFCLOCK_CORRECT_SYNC(buf))
281 if (!PCFCLOCK_CORRECT_FORMAT(buf))
294 pcfclock_read(struct dev_read_args *ap)
296 cdev_t dev = ap->a_head.a_dev;
297 u_int unit = minor(dev);
301 if (ap->a_uio->uio_resid < 18)
304 error = pcfclock_read_dev(dev, buf, PCFCLOCK_MAX_RETRIES);
307 kprintf(PCFCLOCK_NAME "%d: no PCF found\n", unit);
309 pcfclock_display_data(dev, buf);
311 uiomove(buf, 18, ap->a_uio);
318 * Because pcfclock is a static device that always exists under any
319 * attached ppbus, and not scanned by the ppbus, we need an identify function
320 * to create the device.
322 static device_method_t pcfclock_methods[] = {
323 /* device interface */
324 DEVMETHOD(device_identify, bus_generic_identify),
325 DEVMETHOD(device_probe, pcfclock_probe),
326 DEVMETHOD(device_attach, pcfclock_attach),
331 static driver_t pcfclock_driver = {
334 sizeof(struct pcfclock_data),
337 DRIVER_MODULE(pcfclock, ppbus, pcfclock_driver, pcfclock_devclass, 0, 0);