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 {
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 static struct cdevsw pcfclock_cdevsw = {
72 .d_version = D_VERSION,
73 .d_flags = D_NEEDGIANT,
74 .d_open = pcfclock_open,
75 .d_close = pcfclock_close,
76 .d_read = pcfclock_read,
77 .d_name = PCFCLOCK_NAME,
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_identify(driver_t *driver, device_t parent)
123 dev = device_find_child(parent, PCFCLOCK_NAME, 0);
125 BUS_ADD_CHILD(parent, 0, PCFCLOCK_NAME, -1);
129 pcfclock_probe(device_t dev)
131 struct pcfclock_data *sc;
133 device_set_desc(dev, "PCF-1.0");
135 sc = DEVTOSOFTC(dev);
141 pcfclock_attach(device_t dev)
145 unit = device_get_unit(dev);
147 make_dev(&pcfclock_cdevsw, unit,
148 UID_ROOT, GID_WHEEL, 0400, PCFCLOCK_NAME "%d", unit);
154 pcfclock_open(struct cdev *dev, int flag, int fms, struct thread *td)
156 u_int unit = minor(dev);
157 struct pcfclock_data *sc = UNITOSOFTC(unit);
158 device_t pcfclockdev = UNITODEVICE(unit);
159 device_t ppbus = device_get_parent(pcfclockdev);
165 if ((res = ppb_request_bus(ppbus, pcfclockdev,
166 (flag & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT)))
175 pcfclock_close(struct cdev *dev, int flags, int fmt, struct thread *td)
177 u_int unit = minor(dev);
178 struct pcfclock_data *sc = UNITOSOFTC(unit);
179 device_t pcfclockdev = UNITODEVICE(unit);
180 device_t ppbus = device_get_parent(pcfclockdev);
184 ppb_release_bus(ppbus, pcfclockdev);
190 pcfclock_write_cmd(struct cdev *dev, unsigned char command)
192 u_int unit = minor(dev);
193 device_t ppidev = UNITODEVICE(unit);
194 device_t ppbus = device_get_parent(ppidev);
195 unsigned char ctr = 14;
198 for (i = 0; i <= 7; i++) {
200 AUTOFEED_CLOCK(i & 1 ? AFC_HI : AFC_LO);
203 ppb_wdtr(ppbus, command);
204 AUTOFEED_CLOCK(AFC_LO);
206 AUTOFEED_CLOCK(AFC_HI);
210 pcfclock_display_data(struct cdev *dev, char buf[18])
212 u_int unit = minor(dev);
213 #ifdef PCFCLOCK_VERBOSE
220 printf(PCFCLOCK_NAME "%d: %02d.%02d.%4d %02d:%02d:%02d, "
221 "battery status: %s\n",
223 NR(buf, 10), NR(buf, 12), 1900 + year,
224 NR(buf, 6), NR(buf, 4), NR(buf, 2),
225 PCFCLOCK_BATTERY_STATUS_LOW(buf) ? "LOW" : "ok");
227 if (PCFCLOCK_BATTERY_STATUS_LOW(buf))
228 printf(PCFCLOCK_NAME "%d: BATTERY STATUS LOW ON\n",
234 pcfclock_read_data(struct cdev *dev, char *buf, ssize_t bits)
236 u_int unit = minor(dev);
237 device_t ppidev = UNITODEVICE(unit);
238 device_t ppbus = device_get_parent(ppidev);
243 /* one byte per four bits */
244 bzero(buf, ((bits + 3) >> 2) + 1);
247 for (i = 0; i <= bits; i++) {
248 /* wait for clock, maximum (waitfor*100) usec */
249 while(!CLOCK_OK && --waitfor > 0)
256 waitfor = 100; /* reload */
258 /* give it some time */
261 /* calculate offset into buffer */
273 pcfclock_read_dev(struct cdev *dev, char *buf, int maxretries)
275 u_int unit = minor(dev);
276 device_t ppidev = UNITODEVICE(unit);
277 device_t ppbus = device_get_parent(ppidev);
280 ppb_set_mode(ppbus, PPB_COMPATIBLE);
282 while (--maxretries > 0) {
283 pcfclock_write_cmd(dev, PCFCLOCK_CMD_TIME);
284 if (pcfclock_read_data(dev, buf, 68))
287 if (!PCFCLOCK_CORRECT_SYNC(buf))
290 if (!PCFCLOCK_CORRECT_FORMAT(buf))
303 pcfclock_read(struct cdev *dev, struct uio *uio, int ioflag)
305 u_int unit = minor(dev);
309 if (uio->uio_resid < 18)
312 error = pcfclock_read_dev(dev, buf, PCFCLOCK_MAX_RETRIES);
315 printf(PCFCLOCK_NAME "%d: no PCF found\n", unit);
317 pcfclock_display_data(dev, buf);
319 uiomove(buf, 18, uio);
325 static device_method_t pcfclock_methods[] = {
326 /* device interface */
327 DEVMETHOD(device_identify, pcfclock_identify),
328 DEVMETHOD(device_probe, pcfclock_probe),
329 DEVMETHOD(device_attach, pcfclock_attach),
334 static driver_t pcfclock_driver = {
337 sizeof(struct pcfclock_data),
340 DRIVER_MODULE(pcfclock, ppbus, pcfclock_driver, pcfclock_devclass, 0, 0);