| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
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: src/sys/dev/ppbus/pcfclock.c,v 1.3.2.1 2000/05/24 00:20:57 n_hibma Exp $ | |
| e3869ec7 | 25 | * $DragonFly: src/sys/dev/misc/pcfclock/pcfclock.c,v 1.12 2006/12/22 23:26:18 swildner Exp $ |
| 984263bc MD |
26 | * |
| 27 | */ | |
| 28 | ||
| 29 | #include "opt_pcfclock.h" | |
| 30 | ||
| 31 | #include <sys/param.h> | |
| 32 | #include <sys/systm.h> | |
| 33 | #include <sys/bus.h> | |
| fef8985e MD |
34 | #include <sys/conf.h> |
| 35 | #include <sys/device.h> | |
| 984263bc MD |
36 | #include <sys/sockio.h> |
| 37 | #include <sys/mbuf.h> | |
| 38 | #include <sys/kernel.h> | |
| 984263bc MD |
39 | #include <sys/fcntl.h> |
| 40 | #include <sys/uio.h> | |
| 41 | ||
| 984263bc MD |
42 | #include <machine/clock.h> /* for DELAY */ |
| 43 | ||
| 1f2de5d4 MD |
44 | #include <bus/ppbus/ppbconf.h> |
| 45 | #include <bus/ppbus/ppb_msq.h> | |
| 46 | #include <bus/ppbus/ppbio.h> | |
| 984263bc MD |
47 | |
| 48 | #include "ppbus_if.h" | |
| 49 | ||
| 50 | #define PCFCLOCK_NAME "pcfclock" | |
| 51 | ||
| 52 | struct pcfclock_data { | |
| 53 | int count; | |
| 54 | struct ppb_device pcfclock_dev; | |
| 55 | }; | |
| 56 | ||
| 57 | #define DEVTOSOFTC(dev) \ | |
| 58 | ((struct pcfclock_data *)device_get_softc(dev)) | |
| 59 | #define UNITOSOFTC(unit) \ | |
| 60 | ((struct pcfclock_data *)devclass_get_softc(pcfclock_devclass, (unit))) | |
| 61 | #define UNITODEVICE(unit) \ | |
| 62 | (devclass_get_device(pcfclock_devclass, (unit))) | |
| 63 | ||
| 64 | static devclass_t pcfclock_devclass; | |
| 65 | ||
| 66 | static d_open_t pcfclock_open; | |
| 67 | static d_close_t pcfclock_close; | |
| 68 | static d_read_t pcfclock_read; | |
| 69 | ||
| 70 | #define CDEV_MAJOR 140 | |
| fef8985e MD |
71 | static struct dev_ops pcfclock_ops = { |
| 72 | { PCFCLOCK_NAME, CDEV_MAJOR, 0 }, | |
| 73 | .d_open = pcfclock_open, | |
| 74 | .d_close = pcfclock_close, | |
| 75 | .d_read = pcfclock_read, | |
| 984263bc MD |
76 | }; |
| 77 | ||
| 78 | #ifndef PCFCLOCK_MAX_RETRIES | |
| 79 | #define PCFCLOCK_MAX_RETRIES 10 | |
| 80 | #endif | |
| 81 | ||
| 82 | #define AFC_HI 0 | |
| 83 | #define AFC_LO AUTOFEED | |
| 84 | ||
| 85 | /* AUTO FEED is used as clock */ | |
| 86 | #define AUTOFEED_CLOCK(val) \ | |
| 87 | ctr = (ctr & ~(AUTOFEED)) ^ (val); ppb_wctr(ppbus, ctr) | |
| 88 | ||
| 89 | /* SLCT is used as clock */ | |
| 90 | #define CLOCK_OK \ | |
| 91 | ((ppb_rstr(ppbus) & SELECT) == (i & 1 ? SELECT : 0)) | |
| 92 | ||
| 93 | /* PE is used as data */ | |
| 94 | #define BIT_SET (ppb_rstr(ppbus)&PERROR) | |
| 95 | ||
| 96 | /* the first byte sent as reply must be 00001001b */ | |
| 97 | #define PCFCLOCK_CORRECT_SYNC(buf) (buf[0] == 9) | |
| 98 | ||
| 99 | #define NR(buf, off) (buf[off+1]*10+buf[off]) | |
| 100 | ||
| 101 | /* check for correct input values */ | |
| 102 | #define PCFCLOCK_CORRECT_FORMAT(buf) (\ | |
| 103 | NR(buf, 14) <= 99 && \ | |
| 104 | NR(buf, 12) <= 12 && \ | |
| 105 | NR(buf, 10) <= 31 && \ | |
| 106 | NR(buf, 6) <= 23 && \ | |
| 107 | NR(buf, 4) <= 59 && \ | |
| 108 | NR(buf, 2) <= 59) | |
| 109 | ||
| 110 | #define PCFCLOCK_BATTERY_STATUS_LOW(buf) (buf[8] & 4) | |
| 111 | ||
| 112 | #define PCFCLOCK_CMD_TIME 0 /* send current time */ | |
| 113 | #define PCFCLOCK_CMD_COPY 7 /* copy received signal to PC */ | |
| 114 | ||
| 984263bc MD |
115 | static int |
| 116 | pcfclock_probe(device_t dev) | |
| 117 | { | |
| 118 | struct pcfclock_data *sc; | |
| 119 | ||
| 120 | device_set_desc(dev, "PCF-1.0"); | |
| 121 | ||
| 122 | sc = DEVTOSOFTC(dev); | |
| 123 | bzero(sc, sizeof(struct pcfclock_data)); | |
| 124 | ||
| 125 | return (0); | |
| 126 | } | |
| 127 | ||
| 128 | static int | |
| 129 | pcfclock_attach(device_t dev) | |
| 130 | { | |
| 131 | int unit; | |
| 132 | ||
| 133 | unit = device_get_unit(dev); | |
| 134 | ||
| fef8985e | 135 | make_dev(&pcfclock_ops, unit, |
| 3e82b46c | 136 | UID_ROOT, GID_WHEEL, 0444, PCFCLOCK_NAME "%d", unit); |
| 984263bc MD |
137 | |
| 138 | return (0); | |
| 139 | } | |
| 140 | ||
| 141 | static int | |
| fef8985e | 142 | pcfclock_open(struct dev_open_args *ap) |
| 984263bc | 143 | { |
| b13267a5 | 144 | cdev_t dev = ap->a_head.a_dev; |
| 984263bc MD |
145 | u_int unit = minor(dev); |
| 146 | struct pcfclock_data *sc = UNITOSOFTC(unit); | |
| 147 | device_t pcfclockdev = UNITODEVICE(unit); | |
| 148 | device_t ppbus = device_get_parent(pcfclockdev); | |
| 149 | int res; | |
| 150 | ||
| 151 | if (!sc) | |
| 152 | return (ENXIO); | |
| 153 | ||
| 154 | if ((res = ppb_request_bus(ppbus, pcfclockdev, | |
| fef8985e | 155 | (ap->a_oflags & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT))) |
| 984263bc MD |
156 | return (res); |
| 157 | ||
| 158 | sc->count++; | |
| 159 | ||
| 160 | return (0); | |
| 161 | } | |
| 162 | ||
| 163 | static int | |
| fef8985e | 164 | pcfclock_close(struct dev_close_args *ap) |
| 984263bc | 165 | { |
| b13267a5 | 166 | cdev_t dev = ap->a_head.a_dev; |
| 984263bc MD |
167 | u_int unit = minor(dev); |
| 168 | struct pcfclock_data *sc = UNITOSOFTC(unit); | |
| 169 | device_t pcfclockdev = UNITODEVICE(unit); | |
| 170 | device_t ppbus = device_get_parent(pcfclockdev); | |
| 171 | ||
| 172 | sc->count--; | |
| 173 | if (sc->count == 0) { | |
| 174 | ppb_release_bus(ppbus, pcfclockdev); | |
| 175 | } | |
| 176 | ||
| 177 | return (0); | |
| 178 | } | |
| 179 | ||
| 180 | static void | |
| b13267a5 | 181 | pcfclock_write_cmd(cdev_t dev, unsigned char command) |
| 984263bc MD |
182 | { |
| 183 | u_int unit = minor(dev); | |
| 184 | device_t ppidev = UNITODEVICE(unit); | |
| 185 | device_t ppbus = device_get_parent(ppidev); | |
| 186 | unsigned char ctr = 14; | |
| 187 | char i; | |
| 188 | ||
| 189 | for (i = 0; i <= 7; i++) { | |
| 190 | ppb_wdtr(ppbus, i); | |
| 191 | AUTOFEED_CLOCK(i & 1 ? AFC_HI : AFC_LO); | |
| 192 | DELAY(3000); | |
| 193 | } | |
| 194 | ppb_wdtr(ppbus, command); | |
| 195 | AUTOFEED_CLOCK(AFC_LO); | |
| 196 | DELAY(3000); | |
| 197 | AUTOFEED_CLOCK(AFC_HI); | |
| 198 | } | |
| 199 | ||
| 200 | static void | |
| b13267a5 | 201 | pcfclock_display_data(cdev_t dev, char buf[18]) |
| 984263bc MD |
202 | { |
| 203 | u_int unit = minor(dev); | |
| 204 | #ifdef PCFCLOCK_VERBOSE | |
| 205 | int year; | |
| 206 | ||
| 207 | year = NR(buf, 14); | |
| 208 | if (year < 70) | |
| 209 | year += 100; | |
| e3869ec7 | 210 | kprintf(PCFCLOCK_NAME "%d: %02d.%02d.%4d %02d:%02d:%02d, " |
| 984263bc MD |
211 | "battery status: %s\n", |
| 212 | unit, | |
| 213 | NR(buf, 10), NR(buf, 12), 1900 + year, | |
| 214 | NR(buf, 6), NR(buf, 4), NR(buf, 2), | |
| 215 | PCFCLOCK_BATTERY_STATUS_LOW(buf) ? "LOW" : "ok"); | |
| 216 | #else | |
| 217 | if (PCFCLOCK_BATTERY_STATUS_LOW(buf)) | |
| e3869ec7 | 218 | kprintf(PCFCLOCK_NAME "%d: BATTERY STATUS LOW ON\n", |
| 984263bc MD |
219 | unit); |
| 220 | #endif | |
| 221 | } | |
| 222 | ||
| 223 | static int | |
| b13267a5 | 224 | pcfclock_read_data(cdev_t dev, char *buf, ssize_t bits) |
| 984263bc MD |
225 | { |
| 226 | u_int unit = minor(dev); | |
| 227 | device_t ppidev = UNITODEVICE(unit); | |
| 228 | device_t ppbus = device_get_parent(ppidev); | |
| 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 | |
| b13267a5 | 263 | pcfclock_read_dev(cdev_t dev, char *buf, int maxretries) |
| 984263bc MD |
264 | { |
| 265 | u_int unit = minor(dev); | |
| 266 | device_t ppidev = UNITODEVICE(unit); | |
| 267 | device_t ppbus = device_get_parent(ppidev); | |
| 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 | ||
| fef8985e MD |
292 | static int |
| 293 | pcfclock_read(struct dev_read_args *ap) | |
| 984263bc | 294 | { |
| b13267a5 | 295 | cdev_t dev = ap->a_head.a_dev; |
| 984263bc MD |
296 | u_int unit = minor(dev); |
| 297 | char buf[18]; | |
| 298 | int error = 0; | |
| 299 | ||
| fef8985e | 300 | if (ap->a_uio->uio_resid < 18) |
| 984263bc MD |
301 | return (ERANGE); |
| 302 | ||
| 303 | error = pcfclock_read_dev(dev, buf, PCFCLOCK_MAX_RETRIES); | |
| 304 | ||
| 305 | if (error) { | |
| e3869ec7 | 306 | kprintf(PCFCLOCK_NAME "%d: no PCF found\n", unit); |
| 984263bc MD |
307 | } else { |
| 308 | pcfclock_display_data(dev, buf); | |
| 309 | ||
| fef8985e | 310 | uiomove(buf, 18, ap->a_uio); |
| 984263bc MD |
311 | } |
| 312 | ||
| 313 | return (error); | |
| 314 | } | |
| 315 | ||
| 39b5d600 MD |
316 | /* |
| 317 | * Because pcfclock is a static device that always exists under any | |
| 318 | * attached ppbus, and not scanned by the ppbus, we need an identify function | |
| 319 | * to create the device. | |
| 320 | */ | |
| 984263bc MD |
321 | static device_method_t pcfclock_methods[] = { |
| 322 | /* device interface */ | |
| 39b5d600 | 323 | DEVMETHOD(device_identify, bus_generic_identify), |
| 984263bc MD |
324 | DEVMETHOD(device_probe, pcfclock_probe), |
| 325 | DEVMETHOD(device_attach, pcfclock_attach), | |
| 326 | ||
| 327 | { 0, 0 } | |
| 328 | }; | |
| 329 | ||
| 330 | static driver_t pcfclock_driver = { | |
| 331 | PCFCLOCK_NAME, | |
| 332 | pcfclock_methods, | |
| 333 | sizeof(struct pcfclock_data), | |
| 334 | }; | |
| 335 | ||
| 336 | DRIVER_MODULE(pcfclock, ppbus, pcfclock_driver, pcfclock_devclass, 0, 0); | |
| 39b5d600 | 337 |