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