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 AUTHOR 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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_pcfclock.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/sockio.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/fcntl.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
46 #include <dev/ppbus/ppbconf.h>
47 #include <dev/ppbus/ppb_msq.h>
48 #include <dev/ppbus/ppbio.h>
52 #define PCFCLOCK_NAME "pcfclock"
54 struct pcfclock_data {
59 static devclass_t pcfclock_devclass;
61 static d_open_t pcfclock_open;
62 static d_close_t pcfclock_close;
63 static d_read_t pcfclock_read;
65 static struct cdevsw pcfclock_cdevsw = {
66 .d_version = D_VERSION,
67 .d_open = pcfclock_open,
68 .d_close = pcfclock_close,
69 .d_read = pcfclock_read,
70 .d_name = PCFCLOCK_NAME,
73 #ifndef PCFCLOCK_MAX_RETRIES
74 #define PCFCLOCK_MAX_RETRIES 10
78 #define AFC_LO AUTOFEED
80 /* AUTO FEED is used as clock */
81 #define AUTOFEED_CLOCK(val) \
82 ctr = (ctr & ~(AUTOFEED)) ^ (val); ppb_wctr(ppbus, ctr)
84 /* SLCT is used as clock */
86 ((ppb_rstr(ppbus) & SELECT) == (i & 1 ? SELECT : 0))
88 /* PE is used as data */
89 #define BIT_SET (ppb_rstr(ppbus)&PERROR)
91 /* the first byte sent as reply must be 00001001b */
92 #define PCFCLOCK_CORRECT_SYNC(buf) (buf[0] == 9)
94 #define NR(buf, off) (buf[off+1]*10+buf[off])
96 /* check for correct input values */
97 #define PCFCLOCK_CORRECT_FORMAT(buf) (\
98 NR(buf, 14) <= 99 && \
99 NR(buf, 12) <= 12 && \
100 NR(buf, 10) <= 31 && \
101 NR(buf, 6) <= 23 && \
102 NR(buf, 4) <= 59 && \
105 #define PCFCLOCK_BATTERY_STATUS_LOW(buf) (buf[8] & 4)
107 #define PCFCLOCK_CMD_TIME 0 /* send current time */
108 #define PCFCLOCK_CMD_COPY 7 /* copy received signal to PC */
111 pcfclock_identify(driver_t *driver, device_t parent)
116 dev = device_find_child(parent, PCFCLOCK_NAME, -1);
118 BUS_ADD_CHILD(parent, 0, PCFCLOCK_NAME, -1);
122 pcfclock_probe(device_t dev)
125 device_set_desc(dev, "PCF-1.0");
130 pcfclock_attach(device_t dev)
132 struct pcfclock_data *sc = device_get_softc(dev);
135 unit = device_get_unit(dev);
138 sc->cdev = make_dev(&pcfclock_cdevsw, unit,
139 UID_ROOT, GID_WHEEL, 0400, PCFCLOCK_NAME "%d", unit);
140 if (sc->cdev == NULL) {
141 device_printf(dev, "Failed to create character device\n");
144 sc->cdev->si_drv1 = sc;
150 pcfclock_open(struct cdev *dev, int flag, int fms, struct thread *td)
152 struct pcfclock_data *sc = dev->si_drv1;
153 device_t pcfclockdev;
159 pcfclockdev = sc->dev;
160 ppbus = device_get_parent(pcfclockdev);
163 res = ppb_request_bus(ppbus, pcfclockdev,
164 (flag & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT);
170 pcfclock_close(struct cdev *dev, int flags, int fmt, struct thread *td)
172 struct pcfclock_data *sc = dev->si_drv1;
173 device_t pcfclockdev = sc->dev;
174 device_t ppbus = device_get_parent(pcfclockdev);
177 ppb_release_bus(ppbus, pcfclockdev);
184 pcfclock_write_cmd(struct cdev *dev, unsigned char command)
186 struct pcfclock_data *sc = dev->si_drv1;
187 device_t pcfclockdev = sc->dev;
188 device_t ppbus = device_get_parent(pcfclockdev);
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(struct cdev *dev, char buf[18])
206 struct pcfclock_data *sc = dev->si_drv1;
207 #ifdef PCFCLOCK_VERBOSE
214 device_printf(sc->dev, "%02d.%02d.%4d %02d:%02d:%02d, "
215 "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 device_printf(sc->dev, "BATTERY STATUS LOW ON\n");
226 pcfclock_read_data(struct cdev *dev, char *buf, ssize_t bits)
228 struct pcfclock_data *sc = dev->si_drv1;
229 device_t pcfclockdev = sc->dev;
230 device_t ppbus = device_get_parent(pcfclockdev);
235 /* one byte per four bits */
236 bzero(buf, ((bits + 3) >> 2) + 1);
239 for (i = 0; i <= bits; i++) {
240 /* wait for clock, maximum (waitfor*100) usec */
241 while (!CLOCK_OK && --waitfor > 0)
248 waitfor = 100; /* reload */
250 /* give it some time */
253 /* calculate offset into buffer */
265 pcfclock_read_dev(struct cdev *dev, char *buf, int maxretries)
267 struct pcfclock_data *sc = dev->si_drv1;
268 device_t pcfclockdev = sc->dev;
269 device_t ppbus = device_get_parent(pcfclockdev);
272 ppb_set_mode(ppbus, PPB_COMPATIBLE);
274 while (--maxretries > 0) {
275 pcfclock_write_cmd(dev, PCFCLOCK_CMD_TIME);
276 if (pcfclock_read_data(dev, buf, 68))
279 if (!PCFCLOCK_CORRECT_SYNC(buf))
282 if (!PCFCLOCK_CORRECT_FORMAT(buf))
295 pcfclock_read(struct cdev *dev, struct uio *uio, int ioflag)
297 struct pcfclock_data *sc = dev->si_drv1;
302 if (uio->uio_resid < 18)
305 ppbus = device_get_parent(sc->dev);
307 error = pcfclock_read_dev(dev, buf, PCFCLOCK_MAX_RETRIES);
311 device_printf(sc->dev, "no PCF found\n");
313 pcfclock_display_data(dev, buf);
315 uiomove(buf, 18, uio);
321 static device_method_t pcfclock_methods[] = {
322 /* device interface */
323 DEVMETHOD(device_identify, pcfclock_identify),
324 DEVMETHOD(device_probe, pcfclock_probe),
325 DEVMETHOD(device_attach, pcfclock_attach),
330 static driver_t pcfclock_driver = {
333 sizeof(struct pcfclock_data),
336 DRIVER_MODULE(pcfclock, ppbus, pcfclock_driver, pcfclock_devclass, 0, 0);