pcfclock.c revision 127189
1/*
2 * Copyright (c) 2000 Sascha Schumann. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
23 *
24 *
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/ppbus/pcfclock.c 127189 2004-03-18 21:10:11Z guido $");
29
30#include "opt_pcfclock.h"
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/bus.h>
35#include <sys/sockio.h>
36#include <sys/mbuf.h>
37#include <sys/kernel.h>
38#include <sys/conf.h>
39#include <sys/fcntl.h>
40#include <sys/uio.h>
41
42#include <machine/bus.h>
43#include <machine/resource.h>
44
45#include <dev/ppbus/ppbconf.h>
46#include <dev/ppbus/ppb_msq.h>
47#include <dev/ppbus/ppbio.h>
48
49#include "ppbus_if.h"
50
51#define PCFCLOCK_NAME "pcfclock"
52
53struct pcfclock_data {
54	int	count;
55};
56
57#define DEVTOSOFTC(dev) \
58	((struct pcfclock_data *)device_get_softc(dev))
59#define UNITOSOFTC(unit) \
60	((struct pcfclock_data *)devclass_get_softc(pcfclock_devclass, (unit)))
61#define UNITODEVICE(unit) \
62	(devclass_get_device(pcfclock_devclass, (unit)))
63
64static devclass_t pcfclock_devclass;
65
66static	d_open_t		pcfclock_open;
67static	d_close_t		pcfclock_close;
68static	d_read_t		pcfclock_read;
69
70static struct cdevsw pcfclock_cdevsw = {
71	.d_version =	D_VERSION,
72	.d_flags =	D_NEEDGIANT,
73	.d_open =	pcfclock_open,
74	.d_close =	pcfclock_close,
75	.d_read =	pcfclock_read,
76	.d_name =	PCFCLOCK_NAME,
77};
78
79#ifndef PCFCLOCK_MAX_RETRIES
80#define PCFCLOCK_MAX_RETRIES 10
81#endif
82
83#define AFC_HI 0
84#define AFC_LO AUTOFEED
85
86/* AUTO FEED is used as clock */
87#define AUTOFEED_CLOCK(val) \
88	ctr = (ctr & ~(AUTOFEED)) ^ (val); ppb_wctr(ppbus, ctr)
89
90/* SLCT is used as clock */
91#define CLOCK_OK \
92	((ppb_rstr(ppbus) & SELECT) == (i & 1 ? SELECT : 0))
93
94/* PE is used as data */
95#define BIT_SET (ppb_rstr(ppbus)&PERROR)
96
97/* the first byte sent as reply must be 00001001b */
98#define PCFCLOCK_CORRECT_SYNC(buf) (buf[0] == 9)
99
100#define NR(buf, off) (buf[off+1]*10+buf[off])
101
102/* check for correct input values */
103#define PCFCLOCK_CORRECT_FORMAT(buf) (\
104	NR(buf, 14) <= 99 && \
105	NR(buf, 12) <= 12 && \
106	NR(buf, 10) <= 31 && \
107	NR(buf,  6) <= 23 && \
108	NR(buf,  4) <= 59 && \
109	NR(buf,  2) <= 59)
110
111#define PCFCLOCK_BATTERY_STATUS_LOW(buf) (buf[8] & 4)
112
113#define PCFCLOCK_CMD_TIME 0		/* send current time */
114#define PCFCLOCK_CMD_COPY 7 	/* copy received signal to PC */
115
116static void
117pcfclock_identify(driver_t *driver, device_t parent)
118{
119
120	device_t dev;
121
122	dev = device_find_child(parent, PCFCLOCK_NAME, 0);
123	if (!dev)
124		BUS_ADD_CHILD(parent, 0, PCFCLOCK_NAME, -1);
125}
126
127static int
128pcfclock_probe(device_t dev)
129{
130	struct pcfclock_data *sc;
131
132	device_set_desc(dev, "PCF-1.0");
133
134	sc = DEVTOSOFTC(dev);
135	bzero(sc, sizeof(struct pcfclock_data));
136
137	return (0);
138}
139
140static int
141pcfclock_attach(device_t dev)
142{
143	int unit;
144
145	unit = device_get_unit(dev);
146
147	make_dev(&pcfclock_cdevsw, unit,
148			UID_ROOT, GID_WHEEL, 0400, PCFCLOCK_NAME "%d", unit);
149
150	return (0);
151}
152
153static int
154pcfclock_open(dev_t dev, int flag, int fms, struct thread *td)
155{
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);
160	int res;
161
162	if (!sc)
163		return (ENXIO);
164
165	if ((res = ppb_request_bus(ppbus, pcfclockdev,
166		(flag & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT)))
167		return (res);
168
169	sc->count++;
170
171	return (0);
172}
173
174static int
175pcfclock_close(dev_t dev, int flags, int fmt, struct thread *td)
176{
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);
181
182	sc->count--;
183	if (sc->count == 0)
184		ppb_release_bus(ppbus, pcfclockdev);
185
186	return (0);
187}
188
189static void
190pcfclock_write_cmd(dev_t dev, unsigned char command)
191{
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;
196	char i;
197
198	for (i = 0; i <= 7; i++) {
199		ppb_wdtr(ppbus, i);
200		AUTOFEED_CLOCK(i & 1 ? AFC_HI : AFC_LO);
201		DELAY(3000);
202	}
203	ppb_wdtr(ppbus, command);
204	AUTOFEED_CLOCK(AFC_LO);
205	DELAY(3000);
206	AUTOFEED_CLOCK(AFC_HI);
207}
208
209static void
210pcfclock_display_data(dev_t dev, char buf[18])
211{
212	u_int unit = minor(dev);
213#ifdef PCFCLOCK_VERBOSE
214	int year;
215
216	year = NR(buf, 14);
217	if (year < 70)
218		year += 100;
219
220	printf(PCFCLOCK_NAME "%d: %02d.%02d.%4d %02d:%02d:%02d, "
221			"battery status: %s\n",
222			unit,
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");
226#else
227	if (PCFCLOCK_BATTERY_STATUS_LOW(buf))
228		printf(PCFCLOCK_NAME "%d: BATTERY STATUS LOW ON\n",
229				unit);
230#endif
231}
232
233static int
234pcfclock_read_data(dev_t dev, char *buf, ssize_t bits)
235{
236	u_int unit = minor(dev);
237	device_t ppidev = UNITODEVICE(unit);
238        device_t ppbus = device_get_parent(ppidev);
239	int i;
240	char waitfor;
241	int offset;
242
243	/* one byte per four bits */
244	bzero(buf, ((bits + 3) >> 2) + 1);
245
246	waitfor = 100;
247	for (i = 0; i <= bits; i++) {
248		/* wait for clock, maximum (waitfor*100) usec */
249		while(!CLOCK_OK && --waitfor > 0)
250			DELAY(100);
251
252		/* timed out? */
253		if (!waitfor)
254			return (EIO);
255
256		waitfor = 100; /* reload */
257
258		/* give it some time */
259		DELAY(500);
260
261		/* calculate offset into buffer */
262		offset = i >> 2;
263		buf[offset] <<= 1;
264
265		if (BIT_SET)
266			buf[offset] |= 1;
267	}
268
269	return (0);
270}
271
272static int
273pcfclock_read_dev(dev_t dev, char *buf, int maxretries)
274{
275	u_int unit = minor(dev);
276	device_t ppidev = UNITODEVICE(unit);
277        device_t ppbus = device_get_parent(ppidev);
278	int error = 0;
279
280	ppb_set_mode(ppbus, PPB_COMPATIBLE);
281
282	while (--maxretries > 0) {
283		pcfclock_write_cmd(dev, PCFCLOCK_CMD_TIME);
284		if (pcfclock_read_data(dev, buf, 68))
285			continue;
286
287		if (!PCFCLOCK_CORRECT_SYNC(buf))
288			continue;
289
290		if (!PCFCLOCK_CORRECT_FORMAT(buf))
291			continue;
292
293		break;
294	}
295
296	if (!maxretries)
297		error = EIO;
298
299	return (error);
300}
301
302static int
303pcfclock_read(dev_t dev, struct uio *uio, int ioflag)
304{
305	u_int unit = minor(dev);
306	char buf[18];
307	int error = 0;
308
309	if (uio->uio_resid < 18)
310		return (ERANGE);
311
312	error = pcfclock_read_dev(dev, buf, PCFCLOCK_MAX_RETRIES);
313
314	if (error) {
315		printf(PCFCLOCK_NAME "%d: no PCF found\n", unit);
316	} else {
317		pcfclock_display_data(dev, buf);
318
319		uiomove(buf, 18, uio);
320	}
321
322	return (error);
323}
324
325static 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),
330
331	{ 0, 0 }
332};
333
334static driver_t pcfclock_driver = {
335	PCFCLOCK_NAME,
336	pcfclock_methods,
337	sizeof(struct pcfclock_data),
338};
339
340DRIVER_MODULE(pcfclock, ppbus, pcfclock_driver, pcfclock_devclass, 0, 0);
341