1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2000 Sascha Schumann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY SASCHA SCHUMANN ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 * EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
21 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 *
27 */
28
29 #include <sys/cdefs.h>
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/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
54 struct pcfclock_data {
55 device_t dev;
56 struct cdev *cdev;
57 };
58
59 static d_open_t pcfclock_open;
60 static d_close_t pcfclock_close;
61 static d_read_t pcfclock_read;
62
63 static struct cdevsw pcfclock_cdevsw = {
64 .d_version = D_VERSION,
65 .d_open = pcfclock_open,
66 .d_close = pcfclock_close,
67 .d_read = pcfclock_read,
68 .d_name = PCFCLOCK_NAME,
69 };
70
71 #ifndef PCFCLOCK_MAX_RETRIES
72 #define PCFCLOCK_MAX_RETRIES 10
73 #endif
74
75 #define AFC_HI 0
76 #define AFC_LO AUTOFEED
77
78 /* AUTO FEED is used as clock */
79 #define AUTOFEED_CLOCK(val) \
80 ctr = (ctr & ~(AUTOFEED)) ^ (val); ppb_wctr(ppbus, ctr)
81
82 /* SLCT is used as clock */
83 #define CLOCK_OK \
84 ((ppb_rstr(ppbus) & SELECT) == (i & 1 ? SELECT : 0))
85
86 /* PE is used as data */
87 #define BIT_SET (ppb_rstr(ppbus)&PERROR)
88
89 /* the first byte sent as reply must be 00001001b */
90 #define PCFCLOCK_CORRECT_SYNC(buf) (buf[0] == 9)
91
92 #define NR(buf, off) (buf[off+1]*10+buf[off])
93
94 /* check for correct input values */
95 #define PCFCLOCK_CORRECT_FORMAT(buf) (\
96 NR(buf, 14) <= 99 && \
97 NR(buf, 12) <= 12 && \
98 NR(buf, 10) <= 31 && \
99 NR(buf, 6) <= 23 && \
100 NR(buf, 4) <= 59 && \
101 NR(buf, 2) <= 59)
102
103 #define PCFCLOCK_BATTERY_STATUS_LOW(buf) (buf[8] & 4)
104
105 #define PCFCLOCK_CMD_TIME 0 /* send current time */
106 #define PCFCLOCK_CMD_COPY 7 /* copy received signal to PC */
107
108 static void
pcfclock_identify(driver_t * driver,device_t parent)109 pcfclock_identify(driver_t *driver, device_t parent)
110 {
111
112 device_t dev;
113
114 dev = device_find_child(parent, PCFCLOCK_NAME, DEVICE_UNIT_ANY);
115 if (!dev)
116 BUS_ADD_CHILD(parent, 0, PCFCLOCK_NAME, DEVICE_UNIT_ANY);
117 }
118
119 static int
pcfclock_probe(device_t dev)120 pcfclock_probe(device_t dev)
121 {
122
123 device_set_desc(dev, "PCF-1.0");
124 return (0);
125 }
126
127 static int
pcfclock_attach(device_t dev)128 pcfclock_attach(device_t dev)
129 {
130 struct pcfclock_data *sc = device_get_softc(dev);
131 int unit;
132
133 unit = device_get_unit(dev);
134
135 sc->dev = dev;
136 sc->cdev = make_dev(&pcfclock_cdevsw, unit,
137 UID_ROOT, GID_WHEEL, 0400, PCFCLOCK_NAME "%d", unit);
138 if (sc->cdev == NULL) {
139 device_printf(dev, "Failed to create character device\n");
140 return (ENXIO);
141 }
142 sc->cdev->si_drv1 = sc;
143
144 return (0);
145 }
146
147 static int
pcfclock_open(struct cdev * dev,int flag,int fms,struct thread * td)148 pcfclock_open(struct cdev *dev, int flag, int fms, struct thread *td)
149 {
150 struct pcfclock_data *sc = dev->si_drv1;
151 device_t pcfclockdev;
152 device_t ppbus;
153 int res;
154
155 if (!sc)
156 return (ENXIO);
157 pcfclockdev = sc->dev;
158 ppbus = device_get_parent(pcfclockdev);
159
160 ppb_lock(ppbus);
161 res = ppb_request_bus(ppbus, pcfclockdev,
162 (flag & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT);
163 ppb_unlock(ppbus);
164 return (res);
165 }
166
167 static int
pcfclock_close(struct cdev * dev,int flags,int fmt,struct thread * td)168 pcfclock_close(struct cdev *dev, int flags, int fmt, struct thread *td)
169 {
170 struct pcfclock_data *sc = dev->si_drv1;
171 device_t pcfclockdev = sc->dev;
172 device_t ppbus = device_get_parent(pcfclockdev);
173
174 ppb_lock(ppbus);
175 ppb_release_bus(ppbus, pcfclockdev);
176 ppb_unlock(ppbus);
177
178 return (0);
179 }
180
181 static void
pcfclock_write_cmd(struct cdev * dev,unsigned char command)182 pcfclock_write_cmd(struct cdev *dev, unsigned char command)
183 {
184 struct pcfclock_data *sc = dev->si_drv1;
185 device_t pcfclockdev = sc->dev;
186 device_t ppbus = device_get_parent(pcfclockdev);
187 unsigned char ctr = 14;
188 char i;
189
190 for (i = 0; i <= 7; i++) {
191 ppb_wdtr(ppbus, i);
192 AUTOFEED_CLOCK(i & 1 ? AFC_HI : AFC_LO);
193 DELAY(3000);
194 }
195 ppb_wdtr(ppbus, command);
196 AUTOFEED_CLOCK(AFC_LO);
197 DELAY(3000);
198 AUTOFEED_CLOCK(AFC_HI);
199 }
200
201 static void
pcfclock_display_data(struct cdev * dev,char buf[18])202 pcfclock_display_data(struct cdev *dev, char buf[18])
203 {
204 struct pcfclock_data *sc = dev->si_drv1;
205 #ifdef PCFCLOCK_VERBOSE
206 int year;
207
208 year = NR(buf, 14);
209 if (year < 70)
210 year += 100;
211
212 device_printf(sc->dev, "%02d.%02d.%4d %02d:%02d:%02d, "
213 "battery status: %s\n",
214 NR(buf, 10), NR(buf, 12), 1900 + year,
215 NR(buf, 6), NR(buf, 4), NR(buf, 2),
216 PCFCLOCK_BATTERY_STATUS_LOW(buf) ? "LOW" : "ok");
217 #else
218 if (PCFCLOCK_BATTERY_STATUS_LOW(buf))
219 device_printf(sc->dev, "BATTERY STATUS LOW ON\n");
220 #endif
221 }
222
223 static int
pcfclock_read_data(struct cdev * dev,char * buf,ssize_t bits)224 pcfclock_read_data(struct cdev *dev, char *buf, ssize_t bits)
225 {
226 struct pcfclock_data *sc = dev->si_drv1;
227 device_t pcfclockdev = sc->dev;
228 device_t ppbus = device_get_parent(pcfclockdev);
229 int i;
230 char waitfor;
231 int offset;
232
233 /* one byte per four bits */
234 bzero(buf, ((bits + 3) >> 2) + 1);
235
236 waitfor = 100;
237 for (i = 0; i <= bits; i++) {
238 /* wait for clock, maximum (waitfor*100) usec */
239 while (!CLOCK_OK && --waitfor > 0)
240 DELAY(100);
241
242 /* timed out? */
243 if (!waitfor)
244 return (EIO);
245
246 waitfor = 100; /* reload */
247
248 /* give it some time */
249 DELAY(500);
250
251 /* calculate offset into buffer */
252 offset = i >> 2;
253 buf[offset] <<= 1;
254
255 if (BIT_SET)
256 buf[offset] |= 1;
257 }
258
259 return (0);
260 }
261
262 static int
pcfclock_read_dev(struct cdev * dev,char * buf,int maxretries)263 pcfclock_read_dev(struct cdev *dev, char *buf, int maxretries)
264 {
265 struct pcfclock_data *sc = dev->si_drv1;
266 device_t pcfclockdev = sc->dev;
267 device_t ppbus = device_get_parent(pcfclockdev);
268 int error = 0;
269
270 ppb_set_mode(ppbus, PPB_COMPATIBLE);
271
272 while (--maxretries > 0) {
273 pcfclock_write_cmd(dev, PCFCLOCK_CMD_TIME);
274 if (pcfclock_read_data(dev, buf, 68))
275 continue;
276
277 if (!PCFCLOCK_CORRECT_SYNC(buf))
278 continue;
279
280 if (!PCFCLOCK_CORRECT_FORMAT(buf))
281 continue;
282
283 break;
284 }
285
286 if (!maxretries)
287 error = EIO;
288
289 return (error);
290 }
291
292 static int
pcfclock_read(struct cdev * dev,struct uio * uio,int ioflag)293 pcfclock_read(struct cdev *dev, struct uio *uio, int ioflag)
294 {
295 struct pcfclock_data *sc = dev->si_drv1;
296 device_t ppbus;
297 char buf[18];
298 int error = 0;
299
300 if (uio->uio_resid < 18)
301 return (ERANGE);
302
303 ppbus = device_get_parent(sc->dev);
304 ppb_lock(ppbus);
305 error = pcfclock_read_dev(dev, buf, PCFCLOCK_MAX_RETRIES);
306 ppb_unlock(ppbus);
307
308 if (error) {
309 device_printf(sc->dev, "no PCF found\n");
310 } else {
311 pcfclock_display_data(dev, buf);
312
313 uiomove(buf, 18, uio);
314 }
315
316 return (error);
317 }
318
319 static device_method_t pcfclock_methods[] = {
320 /* device interface */
321 DEVMETHOD(device_identify, pcfclock_identify),
322 DEVMETHOD(device_probe, pcfclock_probe),
323 DEVMETHOD(device_attach, pcfclock_attach),
324 { 0, 0 }
325 };
326
327 static driver_t pcfclock_driver = {
328 PCFCLOCK_NAME,
329 pcfclock_methods,
330 sizeof(struct pcfclock_data),
331 };
332
333 DRIVER_MODULE(pcfclock, ppbus, pcfclock_driver, 0, 0);
334