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.2 2003/06/17 04:28:29 dillon Exp $
29 #include "opt_pcfclock.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/sockio.h>
36 #include <sys/kernel.h>
38 #include <sys/fcntl.h>
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 #include <machine/clock.h> /* for DELAY */
45 #include <dev/ppbus/ppbconf.h>
46 #include <dev/ppbus/ppb_msq.h>
47 #include <dev/ppbus/ppbio.h>
51 #define PCFCLOCK_NAME "pcfclock"
53 struct pcfclock_data {
55 struct ppb_device pcfclock_dev;
58 #define DEVTOSOFTC(dev) \
59 ((struct pcfclock_data *)device_get_softc(dev))
60 #define UNITOSOFTC(unit) \
61 ((struct pcfclock_data *)devclass_get_softc(pcfclock_devclass, (unit)))
62 #define UNITODEVICE(unit) \
63 (devclass_get_device(pcfclock_devclass, (unit)))
65 static devclass_t pcfclock_devclass;
67 static d_open_t pcfclock_open;
68 static d_close_t pcfclock_close;
69 static d_read_t pcfclock_read;
71 #define CDEV_MAJOR 140
72 static struct cdevsw pcfclock_cdevsw = {
73 /* open */ pcfclock_open,
74 /* close */ pcfclock_close,
75 /* read */ pcfclock_read,
80 /* strategy */ nostrategy,
81 /* name */ PCFCLOCK_NAME,
89 #ifndef PCFCLOCK_MAX_RETRIES
90 #define PCFCLOCK_MAX_RETRIES 10
94 #define AFC_LO AUTOFEED
96 /* AUTO FEED is used as clock */
97 #define AUTOFEED_CLOCK(val) \
98 ctr = (ctr & ~(AUTOFEED)) ^ (val); ppb_wctr(ppbus, ctr)
100 /* SLCT is used as clock */
102 ((ppb_rstr(ppbus) & SELECT) == (i & 1 ? SELECT : 0))
104 /* PE is used as data */
105 #define BIT_SET (ppb_rstr(ppbus)&PERROR)
107 /* the first byte sent as reply must be 00001001b */
108 #define PCFCLOCK_CORRECT_SYNC(buf) (buf[0] == 9)
110 #define NR(buf, off) (buf[off+1]*10+buf[off])
112 /* check for correct input values */
113 #define PCFCLOCK_CORRECT_FORMAT(buf) (\
114 NR(buf, 14) <= 99 && \
115 NR(buf, 12) <= 12 && \
116 NR(buf, 10) <= 31 && \
117 NR(buf, 6) <= 23 && \
118 NR(buf, 4) <= 59 && \
121 #define PCFCLOCK_BATTERY_STATUS_LOW(buf) (buf[8] & 4)
123 #define PCFCLOCK_CMD_TIME 0 /* send current time */
124 #define PCFCLOCK_CMD_COPY 7 /* copy received signal to PC */
127 pcfclock_identify(driver_t *driver, device_t parent)
130 BUS_ADD_CHILD(parent, 0, PCFCLOCK_NAME, 0);
134 pcfclock_probe(device_t dev)
136 struct pcfclock_data *sc;
138 device_set_desc(dev, "PCF-1.0");
140 sc = DEVTOSOFTC(dev);
141 bzero(sc, sizeof(struct pcfclock_data));
147 pcfclock_attach(device_t dev)
151 unit = device_get_unit(dev);
153 make_dev(&pcfclock_cdevsw, unit,
154 UID_ROOT, GID_WHEEL, 0444, PCFCLOCK_NAME "%d", unit);
160 pcfclock_open(dev_t dev, int flag, int fms, struct proc *p)
162 u_int unit = minor(dev);
163 struct pcfclock_data *sc = UNITOSOFTC(unit);
164 device_t pcfclockdev = UNITODEVICE(unit);
165 device_t ppbus = device_get_parent(pcfclockdev);
171 if ((res = ppb_request_bus(ppbus, pcfclockdev,
172 (flag & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT)))
181 pcfclock_close(dev_t dev, int flags, int fmt, struct proc *p)
183 u_int unit = minor(dev);
184 struct pcfclock_data *sc = UNITOSOFTC(unit);
185 device_t pcfclockdev = UNITODEVICE(unit);
186 device_t ppbus = device_get_parent(pcfclockdev);
189 if (sc->count == 0) {
190 ppb_release_bus(ppbus, pcfclockdev);
197 pcfclock_write_cmd(dev_t dev, unsigned char command)
199 u_int unit = minor(dev);
200 device_t ppidev = UNITODEVICE(unit);
201 device_t ppbus = device_get_parent(ppidev);
202 unsigned char ctr = 14;
205 for (i = 0; i <= 7; i++) {
207 AUTOFEED_CLOCK(i & 1 ? AFC_HI : AFC_LO);
210 ppb_wdtr(ppbus, command);
211 AUTOFEED_CLOCK(AFC_LO);
213 AUTOFEED_CLOCK(AFC_HI);
217 pcfclock_display_data(dev_t dev, char buf[18])
219 u_int unit = minor(dev);
220 #ifdef PCFCLOCK_VERBOSE
226 printf(PCFCLOCK_NAME "%d: %02d.%02d.%4d %02d:%02d:%02d, "
227 "battery status: %s\n",
229 NR(buf, 10), NR(buf, 12), 1900 + year,
230 NR(buf, 6), NR(buf, 4), NR(buf, 2),
231 PCFCLOCK_BATTERY_STATUS_LOW(buf) ? "LOW" : "ok");
233 if (PCFCLOCK_BATTERY_STATUS_LOW(buf))
234 printf(PCFCLOCK_NAME "%d: BATTERY STATUS LOW ON\n",
240 pcfclock_read_data(dev_t dev, char *buf, ssize_t bits)
242 u_int unit = minor(dev);
243 device_t ppidev = UNITODEVICE(unit);
244 device_t ppbus = device_get_parent(ppidev);
249 /* one byte per four bits */
250 bzero(buf, ((bits + 3) >> 2) + 1);
253 for (i = 0; i <= bits; i++) {
254 /* wait for clock, maximum (waitfor*100) usec */
255 while(!CLOCK_OK && --waitfor > 0)
262 waitfor = 100; /* reload */
264 /* give it some time */
267 /* calculate offset into buffer */
279 pcfclock_read_dev(dev_t dev, char *buf, int maxretries)
281 u_int unit = minor(dev);
282 device_t ppidev = UNITODEVICE(unit);
283 device_t ppbus = device_get_parent(ppidev);
286 ppb_set_mode(ppbus, PPB_COMPATIBLE);
288 while (--maxretries > 0) {
289 pcfclock_write_cmd(dev, PCFCLOCK_CMD_TIME);
290 if (pcfclock_read_data(dev, buf, 68))
293 if (!PCFCLOCK_CORRECT_SYNC(buf))
296 if (!PCFCLOCK_CORRECT_FORMAT(buf))
309 pcfclock_read(dev_t dev, struct uio *uio, int ioflag)
311 u_int unit = minor(dev);
315 if (uio->uio_resid < 18)
318 error = pcfclock_read_dev(dev, buf, PCFCLOCK_MAX_RETRIES);
321 printf(PCFCLOCK_NAME "%d: no PCF found\n", unit);
323 pcfclock_display_data(dev, buf);
325 uiomove(buf, 18, uio);
331 static device_method_t pcfclock_methods[] = {
332 /* device interface */
333 DEVMETHOD(device_identify, pcfclock_identify),
334 DEVMETHOD(device_probe, pcfclock_probe),
335 DEVMETHOD(device_attach, pcfclock_attach),
340 static driver_t pcfclock_driver = {
343 sizeof(struct pcfclock_data),
346 DRIVER_MODULE(pcfclock, ppbus, pcfclock_driver, pcfclock_devclass, 0, 0);