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