2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Sascha Schumann. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY SASCHA SCHUMANN ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 * EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
21 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_pcfclock.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/sockio.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/fcntl.h>
45 #include <machine/bus.h>
46 #include <machine/resource.h>
48 #include <dev/ppbus/ppbconf.h>
49 #include <dev/ppbus/ppb_msq.h>
50 #include <dev/ppbus/ppbio.h>
54 #define PCFCLOCK_NAME "pcfclock"
56 struct pcfclock_data {
61 static devclass_t pcfclock_devclass;
63 static d_open_t pcfclock_open;
64 static d_close_t pcfclock_close;
65 static d_read_t pcfclock_read;
67 static struct cdevsw pcfclock_cdevsw = {
68 .d_version = D_VERSION,
69 .d_open = pcfclock_open,
70 .d_close = pcfclock_close,
71 .d_read = pcfclock_read,
72 .d_name = PCFCLOCK_NAME,
75 #ifndef PCFCLOCK_MAX_RETRIES
76 #define PCFCLOCK_MAX_RETRIES 10
80 #define AFC_LO AUTOFEED
82 /* AUTO FEED is used as clock */
83 #define AUTOFEED_CLOCK(val) \
84 ctr = (ctr & ~(AUTOFEED)) ^ (val); ppb_wctr(ppbus, ctr)
86 /* SLCT is used as clock */
88 ((ppb_rstr(ppbus) & SELECT) == (i & 1 ? SELECT : 0))
90 /* PE is used as data */
91 #define BIT_SET (ppb_rstr(ppbus)&PERROR)
93 /* the first byte sent as reply must be 00001001b */
94 #define PCFCLOCK_CORRECT_SYNC(buf) (buf[0] == 9)
96 #define NR(buf, off) (buf[off+1]*10+buf[off])
98 /* check for correct input values */
99 #define PCFCLOCK_CORRECT_FORMAT(buf) (\
100 NR(buf, 14) <= 99 && \
101 NR(buf, 12) <= 12 && \
102 NR(buf, 10) <= 31 && \
103 NR(buf, 6) <= 23 && \
104 NR(buf, 4) <= 59 && \
107 #define PCFCLOCK_BATTERY_STATUS_LOW(buf) (buf[8] & 4)
109 #define PCFCLOCK_CMD_TIME 0 /* send current time */
110 #define PCFCLOCK_CMD_COPY 7 /* copy received signal to PC */
113 pcfclock_identify(driver_t *driver, device_t parent)
118 dev = device_find_child(parent, PCFCLOCK_NAME, -1);
120 BUS_ADD_CHILD(parent, 0, PCFCLOCK_NAME, -1);
124 pcfclock_probe(device_t dev)
127 device_set_desc(dev, "PCF-1.0");
132 pcfclock_attach(device_t dev)
134 struct pcfclock_data *sc = device_get_softc(dev);
137 unit = device_get_unit(dev);
140 sc->cdev = make_dev(&pcfclock_cdevsw, unit,
141 UID_ROOT, GID_WHEEL, 0400, PCFCLOCK_NAME "%d", unit);
142 if (sc->cdev == NULL) {
143 device_printf(dev, "Failed to create character device\n");
146 sc->cdev->si_drv1 = sc;
152 pcfclock_open(struct cdev *dev, int flag, int fms, struct thread *td)
154 struct pcfclock_data *sc = dev->si_drv1;
155 device_t pcfclockdev;
161 pcfclockdev = sc->dev;
162 ppbus = device_get_parent(pcfclockdev);
165 res = ppb_request_bus(ppbus, pcfclockdev,
166 (flag & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT);
172 pcfclock_close(struct cdev *dev, int flags, int fmt, struct thread *td)
174 struct pcfclock_data *sc = dev->si_drv1;
175 device_t pcfclockdev = sc->dev;
176 device_t ppbus = device_get_parent(pcfclockdev);
179 ppb_release_bus(ppbus, pcfclockdev);
186 pcfclock_write_cmd(struct cdev *dev, unsigned char command)
188 struct pcfclock_data *sc = dev->si_drv1;
189 device_t pcfclockdev = sc->dev;
190 device_t ppbus = device_get_parent(pcfclockdev);
191 unsigned char ctr = 14;
194 for (i = 0; i <= 7; i++) {
196 AUTOFEED_CLOCK(i & 1 ? AFC_HI : AFC_LO);
199 ppb_wdtr(ppbus, command);
200 AUTOFEED_CLOCK(AFC_LO);
202 AUTOFEED_CLOCK(AFC_HI);
206 pcfclock_display_data(struct cdev *dev, char buf[18])
208 struct pcfclock_data *sc = dev->si_drv1;
209 #ifdef PCFCLOCK_VERBOSE
216 device_printf(sc->dev, "%02d.%02d.%4d %02d:%02d:%02d, "
217 "battery status: %s\n",
218 NR(buf, 10), NR(buf, 12), 1900 + year,
219 NR(buf, 6), NR(buf, 4), NR(buf, 2),
220 PCFCLOCK_BATTERY_STATUS_LOW(buf) ? "LOW" : "ok");
222 if (PCFCLOCK_BATTERY_STATUS_LOW(buf))
223 device_printf(sc->dev, "BATTERY STATUS LOW ON\n");
228 pcfclock_read_data(struct cdev *dev, char *buf, ssize_t bits)
230 struct pcfclock_data *sc = dev->si_drv1;
231 device_t pcfclockdev = sc->dev;
232 device_t ppbus = device_get_parent(pcfclockdev);
237 /* one byte per four bits */
238 bzero(buf, ((bits + 3) >> 2) + 1);
241 for (i = 0; i <= bits; i++) {
242 /* wait for clock, maximum (waitfor*100) usec */
243 while (!CLOCK_OK && --waitfor > 0)
250 waitfor = 100; /* reload */
252 /* give it some time */
255 /* calculate offset into buffer */
267 pcfclock_read_dev(struct cdev *dev, char *buf, int maxretries)
269 struct pcfclock_data *sc = dev->si_drv1;
270 device_t pcfclockdev = sc->dev;
271 device_t ppbus = device_get_parent(pcfclockdev);
274 ppb_set_mode(ppbus, PPB_COMPATIBLE);
276 while (--maxretries > 0) {
277 pcfclock_write_cmd(dev, PCFCLOCK_CMD_TIME);
278 if (pcfclock_read_data(dev, buf, 68))
281 if (!PCFCLOCK_CORRECT_SYNC(buf))
284 if (!PCFCLOCK_CORRECT_FORMAT(buf))
297 pcfclock_read(struct cdev *dev, struct uio *uio, int ioflag)
299 struct pcfclock_data *sc = dev->si_drv1;
304 if (uio->uio_resid < 18)
307 ppbus = device_get_parent(sc->dev);
309 error = pcfclock_read_dev(dev, buf, PCFCLOCK_MAX_RETRIES);
313 device_printf(sc->dev, "no PCF found\n");
315 pcfclock_display_data(dev, buf);
317 uiomove(buf, 18, uio);
323 static device_method_t pcfclock_methods[] = {
324 /* device interface */
325 DEVMETHOD(device_identify, pcfclock_identify),
326 DEVMETHOD(device_probe, pcfclock_probe),
327 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);