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