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.9 2006/07/28 02:17:36 dillon 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/bus.h>
43 #include <machine/resource.h>
44 #include <machine/clock.h> /* for DELAY */
46 #include <bus/ppbus/ppbconf.h>
47 #include <bus/ppbus/ppb_msq.h>
48 #include <bus/ppbus/ppbio.h>
52 #define PCFCLOCK_NAME "pcfclock"
54 struct pcfclock_data {
56 struct ppb_device pcfclock_dev;
59 #define DEVTOSOFTC(dev) \
60 ((struct pcfclock_data *)device_get_softc(dev))
61 #define UNITOSOFTC(unit) \
62 ((struct pcfclock_data *)devclass_get_softc(pcfclock_devclass, (unit)))
63 #define UNITODEVICE(unit) \
64 (devclass_get_device(pcfclock_devclass, (unit)))
66 static devclass_t pcfclock_devclass;
68 static d_open_t pcfclock_open;
69 static d_close_t pcfclock_close;
70 static d_read_t pcfclock_read;
72 #define CDEV_MAJOR 140
73 static struct dev_ops pcfclock_ops = {
74 { PCFCLOCK_NAME, CDEV_MAJOR, 0 },
75 .d_open = pcfclock_open,
76 .d_close = pcfclock_close,
77 .d_read = pcfclock_read,
80 #ifndef PCFCLOCK_MAX_RETRIES
81 #define PCFCLOCK_MAX_RETRIES 10
85 #define AFC_LO AUTOFEED
87 /* AUTO FEED is used as clock */
88 #define AUTOFEED_CLOCK(val) \
89 ctr = (ctr & ~(AUTOFEED)) ^ (val); ppb_wctr(ppbus, ctr)
91 /* SLCT is used as clock */
93 ((ppb_rstr(ppbus) & SELECT) == (i & 1 ? SELECT : 0))
95 /* PE is used as data */
96 #define BIT_SET (ppb_rstr(ppbus)&PERROR)
98 /* the first byte sent as reply must be 00001001b */
99 #define PCFCLOCK_CORRECT_SYNC(buf) (buf[0] == 9)
101 #define NR(buf, off) (buf[off+1]*10+buf[off])
103 /* check for correct input values */
104 #define PCFCLOCK_CORRECT_FORMAT(buf) (\
105 NR(buf, 14) <= 99 && \
106 NR(buf, 12) <= 12 && \
107 NR(buf, 10) <= 31 && \
108 NR(buf, 6) <= 23 && \
109 NR(buf, 4) <= 59 && \
112 #define PCFCLOCK_BATTERY_STATUS_LOW(buf) (buf[8] & 4)
114 #define PCFCLOCK_CMD_TIME 0 /* send current time */
115 #define PCFCLOCK_CMD_COPY 7 /* copy received signal to PC */
118 pcfclock_probe(device_t dev)
120 struct pcfclock_data *sc;
122 device_set_desc(dev, "PCF-1.0");
124 sc = DEVTOSOFTC(dev);
125 bzero(sc, sizeof(struct pcfclock_data));
131 pcfclock_attach(device_t dev)
135 unit = device_get_unit(dev);
137 dev_ops_add(&pcfclock_ops, -1, unit);
138 make_dev(&pcfclock_ops, unit,
139 UID_ROOT, GID_WHEEL, 0444, PCFCLOCK_NAME "%d", unit);
145 pcfclock_open(struct dev_open_args *ap)
147 dev_t dev = ap->a_head.a_dev;
148 u_int unit = minor(dev);
149 struct pcfclock_data *sc = UNITOSOFTC(unit);
150 device_t pcfclockdev = UNITODEVICE(unit);
151 device_t ppbus = device_get_parent(pcfclockdev);
157 if ((res = ppb_request_bus(ppbus, pcfclockdev,
158 (ap->a_oflags & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT)))
167 pcfclock_close(struct dev_close_args *ap)
169 dev_t dev = ap->a_head.a_dev;
170 u_int unit = minor(dev);
171 struct pcfclock_data *sc = UNITOSOFTC(unit);
172 device_t pcfclockdev = UNITODEVICE(unit);
173 device_t ppbus = device_get_parent(pcfclockdev);
176 if (sc->count == 0) {
177 ppb_release_bus(ppbus, pcfclockdev);
184 pcfclock_write_cmd(dev_t dev, unsigned char command)
186 u_int unit = minor(dev);
187 device_t ppidev = UNITODEVICE(unit);
188 device_t ppbus = device_get_parent(ppidev);
189 unsigned char ctr = 14;
192 for (i = 0; i <= 7; i++) {
194 AUTOFEED_CLOCK(i & 1 ? AFC_HI : AFC_LO);
197 ppb_wdtr(ppbus, command);
198 AUTOFEED_CLOCK(AFC_LO);
200 AUTOFEED_CLOCK(AFC_HI);
204 pcfclock_display_data(dev_t dev, char buf[18])
206 u_int unit = minor(dev);
207 #ifdef PCFCLOCK_VERBOSE
213 printf(PCFCLOCK_NAME "%d: %02d.%02d.%4d %02d:%02d:%02d, "
214 "battery status: %s\n",
216 NR(buf, 10), NR(buf, 12), 1900 + year,
217 NR(buf, 6), NR(buf, 4), NR(buf, 2),
218 PCFCLOCK_BATTERY_STATUS_LOW(buf) ? "LOW" : "ok");
220 if (PCFCLOCK_BATTERY_STATUS_LOW(buf))
221 printf(PCFCLOCK_NAME "%d: BATTERY STATUS LOW ON\n",
227 pcfclock_read_data(dev_t dev, char *buf, ssize_t bits)
229 u_int unit = minor(dev);
230 device_t ppidev = UNITODEVICE(unit);
231 device_t ppbus = device_get_parent(ppidev);
236 /* one byte per four bits */
237 bzero(buf, ((bits + 3) >> 2) + 1);
240 for (i = 0; i <= bits; i++) {
241 /* wait for clock, maximum (waitfor*100) usec */
242 while(!CLOCK_OK && --waitfor > 0)
249 waitfor = 100; /* reload */
251 /* give it some time */
254 /* calculate offset into buffer */
266 pcfclock_read_dev(dev_t dev, char *buf, int maxretries)
268 u_int unit = minor(dev);
269 device_t ppidev = UNITODEVICE(unit);
270 device_t ppbus = device_get_parent(ppidev);
273 ppb_set_mode(ppbus, PPB_COMPATIBLE);
275 while (--maxretries > 0) {
276 pcfclock_write_cmd(dev, PCFCLOCK_CMD_TIME);
277 if (pcfclock_read_data(dev, buf, 68))
280 if (!PCFCLOCK_CORRECT_SYNC(buf))
283 if (!PCFCLOCK_CORRECT_FORMAT(buf))
296 pcfclock_read(struct dev_read_args *ap)
298 dev_t dev = ap->a_head.a_dev;
299 u_int unit = minor(dev);
303 if (ap->a_uio->uio_resid < 18)
306 error = pcfclock_read_dev(dev, buf, PCFCLOCK_MAX_RETRIES);
309 printf(PCFCLOCK_NAME "%d: no PCF found\n", unit);
311 pcfclock_display_data(dev, buf);
313 uiomove(buf, 18, ap->a_uio);
320 * Because pcfclock is a static device that always exists under any
321 * attached ppbus, and not scanned by the ppbus, we need an identify function
322 * to create the device.
324 static device_method_t pcfclock_methods[] = {
325 /* device interface */
326 DEVMETHOD(device_identify, bus_generic_identify),
327 DEVMETHOD(device_probe, pcfclock_probe),
328 DEVMETHOD(device_attach, pcfclock_attach),
333 static driver_t pcfclock_driver = {
336 sizeof(struct pcfclock_data),
339 DRIVER_MODULE(pcfclock, ppbus, pcfclock_driver, pcfclock_devclass, 0, 0);