pcfclock.c revision 130585
170658Sobrien/*
270658Sobrien * Copyright (c) 2000 Sascha Schumann. All rights reserved.
385595Sobrien *
485595Sobrien * Redistribution and use in source and binary forms, with or without
570658Sobrien * modification, are permitted provided that the following conditions
670658Sobrien * are met:
7209869Snwhitehorn * 1. Redistributions of source code must retain the above copyright
8209869Snwhitehorn *    notice, this list of conditions and the following disclaimer.
9209869Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
1093399Smarkm *    notice, this list of conditions and the following disclaimer in the
1170658Sobrien *    documentation and/or other materials provided with the distribution.
1296516Sru *
1370658Sobrien * THIS SOFTWARE IS PROVIDED BY SASCHA SCHUMANN ``AS IS'' AND ANY EXPRESS OR
1496530Sru * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15217375Sdim * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
1696530Sru * EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
17217375Sdim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18217375Sdim * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
1970658Sobrien * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
20217375Sdim * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21217375Sdim * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
22217375Sdim * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23217375Sdim *
24217375Sdim *
25217375Sdim */
26217375Sdim
27217375Sdim#include <sys/cdefs.h>
28217375Sdim__FBSDID("$FreeBSD: head/sys/dev/ppbus/pcfclock.c 130585 2004-06-16 09:47:26Z phk $");
29217375Sdim
30217375Sdim#include "opt_pcfclock.h"
31217375Sdim
32217375Sdim#include <sys/param.h>
33217375Sdim#include <sys/systm.h>
3470658Sobrien#include <sys/bus.h>
35100872Sru#include <sys/sockio.h>
3696516Sru#include <sys/mbuf.h>
3770658Sobrien#include <sys/kernel.h>
3870658Sobrien#include <sys/module.h>
39#include <sys/conf.h>
40#include <sys/fcntl.h>
41#include <sys/uio.h>
42
43#include <machine/bus.h>
44#include <machine/resource.h>
45
46#include <dev/ppbus/ppbconf.h>
47#include <dev/ppbus/ppb_msq.h>
48#include <dev/ppbus/ppbio.h>
49
50#include "ppbus_if.h"
51
52#define PCFCLOCK_NAME "pcfclock"
53
54struct pcfclock_data {
55	int	count;
56};
57
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)))
64
65static devclass_t pcfclock_devclass;
66
67static	d_open_t		pcfclock_open;
68static	d_close_t		pcfclock_close;
69static	d_read_t		pcfclock_read;
70
71static 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,
78};
79
80#ifndef PCFCLOCK_MAX_RETRIES
81#define PCFCLOCK_MAX_RETRIES 10
82#endif
83
84#define AFC_HI 0
85#define AFC_LO AUTOFEED
86
87/* AUTO FEED is used as clock */
88#define AUTOFEED_CLOCK(val) \
89	ctr = (ctr & ~(AUTOFEED)) ^ (val); ppb_wctr(ppbus, ctr)
90
91/* SLCT is used as clock */
92#define CLOCK_OK \
93	((ppb_rstr(ppbus) & SELECT) == (i & 1 ? SELECT : 0))
94
95/* PE is used as data */
96#define BIT_SET (ppb_rstr(ppbus)&PERROR)
97
98/* the first byte sent as reply must be 00001001b */
99#define PCFCLOCK_CORRECT_SYNC(buf) (buf[0] == 9)
100
101#define NR(buf, off) (buf[off+1]*10+buf[off])
102
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 && \
110	NR(buf,  2) <= 59)
111
112#define PCFCLOCK_BATTERY_STATUS_LOW(buf) (buf[8] & 4)
113
114#define PCFCLOCK_CMD_TIME 0		/* send current time */
115#define PCFCLOCK_CMD_COPY 7 	/* copy received signal to PC */
116
117static void
118pcfclock_identify(driver_t *driver, device_t parent)
119{
120
121	device_t dev;
122
123	dev = device_find_child(parent, PCFCLOCK_NAME, 0);
124	if (!dev)
125		BUS_ADD_CHILD(parent, 0, PCFCLOCK_NAME, -1);
126}
127
128static int
129pcfclock_probe(device_t dev)
130{
131	struct pcfclock_data *sc;
132
133	device_set_desc(dev, "PCF-1.0");
134
135	sc = DEVTOSOFTC(dev);
136	bzero(sc, sizeof(struct pcfclock_data));
137
138	return (0);
139}
140
141static int
142pcfclock_attach(device_t dev)
143{
144	int unit;
145
146	unit = device_get_unit(dev);
147
148	make_dev(&pcfclock_cdevsw, unit,
149			UID_ROOT, GID_WHEEL, 0400, PCFCLOCK_NAME "%d", unit);
150
151	return (0);
152}
153
154static int
155pcfclock_open(struct cdev *dev, int flag, int fms, struct thread *td)
156{
157	u_int unit = minor(dev);
158	struct pcfclock_data *sc = UNITOSOFTC(unit);
159	device_t pcfclockdev = UNITODEVICE(unit);
160	device_t ppbus = device_get_parent(pcfclockdev);
161	int res;
162
163	if (!sc)
164		return (ENXIO);
165
166	if ((res = ppb_request_bus(ppbus, pcfclockdev,
167		(flag & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT)))
168		return (res);
169
170	sc->count++;
171
172	return (0);
173}
174
175static int
176pcfclock_close(struct cdev *dev, int flags, int fmt, struct thread *td)
177{
178	u_int unit = minor(dev);
179	struct pcfclock_data *sc = UNITOSOFTC(unit);
180	device_t pcfclockdev = UNITODEVICE(unit);
181	device_t ppbus = device_get_parent(pcfclockdev);
182
183	sc->count--;
184	if (sc->count == 0)
185		ppb_release_bus(ppbus, pcfclockdev);
186
187	return (0);
188}
189
190static void
191pcfclock_write_cmd(struct cdev *dev, unsigned char command)
192{
193	u_int unit = minor(dev);
194	device_t ppidev = UNITODEVICE(unit);
195        device_t ppbus = device_get_parent(ppidev);
196	unsigned char ctr = 14;
197	char i;
198
199	for (i = 0; i <= 7; i++) {
200		ppb_wdtr(ppbus, i);
201		AUTOFEED_CLOCK(i & 1 ? AFC_HI : AFC_LO);
202		DELAY(3000);
203	}
204	ppb_wdtr(ppbus, command);
205	AUTOFEED_CLOCK(AFC_LO);
206	DELAY(3000);
207	AUTOFEED_CLOCK(AFC_HI);
208}
209
210static void
211pcfclock_display_data(struct cdev *dev, char buf[18])
212{
213	u_int unit = minor(dev);
214#ifdef PCFCLOCK_VERBOSE
215	int year;
216
217	year = NR(buf, 14);
218	if (year < 70)
219		year += 100;
220
221	printf(PCFCLOCK_NAME "%d: %02d.%02d.%4d %02d:%02d:%02d, "
222			"battery status: %s\n",
223			unit,
224			NR(buf, 10), NR(buf, 12), 1900 + year,
225			NR(buf, 6), NR(buf, 4), NR(buf, 2),
226			PCFCLOCK_BATTERY_STATUS_LOW(buf) ? "LOW" : "ok");
227#else
228	if (PCFCLOCK_BATTERY_STATUS_LOW(buf))
229		printf(PCFCLOCK_NAME "%d: BATTERY STATUS LOW ON\n",
230				unit);
231#endif
232}
233
234static int
235pcfclock_read_data(struct cdev *dev, char *buf, ssize_t bits)
236{
237	u_int unit = minor(dev);
238	device_t ppidev = UNITODEVICE(unit);
239        device_t ppbus = device_get_parent(ppidev);
240	int i;
241	char waitfor;
242	int offset;
243
244	/* one byte per four bits */
245	bzero(buf, ((bits + 3) >> 2) + 1);
246
247	waitfor = 100;
248	for (i = 0; i <= bits; i++) {
249		/* wait for clock, maximum (waitfor*100) usec */
250		while(!CLOCK_OK && --waitfor > 0)
251			DELAY(100);
252
253		/* timed out? */
254		if (!waitfor)
255			return (EIO);
256
257		waitfor = 100; /* reload */
258
259		/* give it some time */
260		DELAY(500);
261
262		/* calculate offset into buffer */
263		offset = i >> 2;
264		buf[offset] <<= 1;
265
266		if (BIT_SET)
267			buf[offset] |= 1;
268	}
269
270	return (0);
271}
272
273static int
274pcfclock_read_dev(struct cdev *dev, char *buf, int maxretries)
275{
276	u_int unit = minor(dev);
277	device_t ppidev = UNITODEVICE(unit);
278        device_t ppbus = device_get_parent(ppidev);
279	int error = 0;
280
281	ppb_set_mode(ppbus, PPB_COMPATIBLE);
282
283	while (--maxretries > 0) {
284		pcfclock_write_cmd(dev, PCFCLOCK_CMD_TIME);
285		if (pcfclock_read_data(dev, buf, 68))
286			continue;
287
288		if (!PCFCLOCK_CORRECT_SYNC(buf))
289			continue;
290
291		if (!PCFCLOCK_CORRECT_FORMAT(buf))
292			continue;
293
294		break;
295	}
296
297	if (!maxretries)
298		error = EIO;
299
300	return (error);
301}
302
303static int
304pcfclock_read(struct cdev *dev, struct uio *uio, int ioflag)
305{
306	u_int unit = minor(dev);
307	char buf[18];
308	int error = 0;
309
310	if (uio->uio_resid < 18)
311		return (ERANGE);
312
313	error = pcfclock_read_dev(dev, buf, PCFCLOCK_MAX_RETRIES);
314
315	if (error) {
316		printf(PCFCLOCK_NAME "%d: no PCF found\n", unit);
317	} else {
318		pcfclock_display_data(dev, buf);
319
320		uiomove(buf, 18, uio);
321	}
322
323	return (error);
324}
325
326static device_method_t pcfclock_methods[] = {
327	/* device interface */
328	DEVMETHOD(device_identify,	pcfclock_identify),
329	DEVMETHOD(device_probe,		pcfclock_probe),
330	DEVMETHOD(device_attach,	pcfclock_attach),
331
332	{ 0, 0 }
333};
334
335static driver_t pcfclock_driver = {
336	PCFCLOCK_NAME,
337	pcfclock_methods,
338	sizeof(struct pcfclock_data),
339};
340
341DRIVER_MODULE(pcfclock, ppbus, pcfclock_driver, pcfclock_devclass, 0, 0);
342