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 $ 25 * $DragonFly: src/sys/dev/misc/pcfclock/pcfclock.c,v 1.9 2006/07/28 02:17:36 dillon Exp $ 26 * 27 */ 28 29 #include "opt_pcfclock.h" 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/bus.h> 34 #include <sys/conf.h> 35 #include <sys/device.h> 36 #include <sys/sockio.h> 37 #include <sys/mbuf.h> 38 #include <sys/kernel.h> 39 #include <sys/fcntl.h> 40 #include <sys/uio.h> 41 42 #include <machine/bus.h> 43 #include <machine/resource.h> 44 #include <machine/clock.h> /* for DELAY */ 45 46 #include <bus/ppbus/ppbconf.h> 47 #include <bus/ppbus/ppb_msq.h> 48 #include <bus/ppbus/ppbio.h> 49 50 #include "ppbus_if.h" 51 52 #define PCFCLOCK_NAME "pcfclock" 53 54 struct pcfclock_data { 55 int count; 56 struct ppb_device pcfclock_dev; 57 }; 58 59 #define DEVTOSOFTC(dev) \ 60 ((struct pcfclock_data *)device_get_softc(dev)) 61 #define UNITOSOFTC(unit) \ 62 ((struct pcfclock_data *)devclass_get_softc(pcfclock_devclass, (unit))) 63 #define UNITODEVICE(unit) \ 64 (devclass_get_device(pcfclock_devclass, (unit))) 65 66 static devclass_t pcfclock_devclass; 67 68 static d_open_t pcfclock_open; 69 static d_close_t pcfclock_close; 70 static d_read_t pcfclock_read; 71 72 #define CDEV_MAJOR 140 73 static struct dev_ops pcfclock_ops = { 74 { PCFCLOCK_NAME, CDEV_MAJOR, 0 }, 75 .d_open = pcfclock_open, 76 .d_close = pcfclock_close, 77 .d_read = pcfclock_read, 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 117 static int 118 pcfclock_probe(device_t dev) 119 { 120 struct pcfclock_data *sc; 121 122 device_set_desc(dev, "PCF-1.0"); 123 124 sc = DEVTOSOFTC(dev); 125 bzero(sc, sizeof(struct pcfclock_data)); 126 127 return (0); 128 } 129 130 static int 131 pcfclock_attach(device_t dev) 132 { 133 int unit; 134 135 unit = device_get_unit(dev); 136 137 dev_ops_add(&pcfclock_ops, -1, unit); 138 make_dev(&pcfclock_ops, unit, 139 UID_ROOT, GID_WHEEL, 0444, PCFCLOCK_NAME "%d", unit); 140 141 return (0); 142 } 143 144 static int 145 pcfclock_open(struct dev_open_args *ap) 146 { 147 dev_t dev = ap->a_head.a_dev; 148 u_int unit = minor(dev); 149 struct pcfclock_data *sc = UNITOSOFTC(unit); 150 device_t pcfclockdev = UNITODEVICE(unit); 151 device_t ppbus = device_get_parent(pcfclockdev); 152 int res; 153 154 if (!sc) 155 return (ENXIO); 156 157 if ((res = ppb_request_bus(ppbus, pcfclockdev, 158 (ap->a_oflags & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT))) 159 return (res); 160 161 sc->count++; 162 163 return (0); 164 } 165 166 static int 167 pcfclock_close(struct dev_close_args *ap) 168 { 169 dev_t dev = ap->a_head.a_dev; 170 u_int unit = minor(dev); 171 struct pcfclock_data *sc = UNITOSOFTC(unit); 172 device_t pcfclockdev = UNITODEVICE(unit); 173 device_t ppbus = device_get_parent(pcfclockdev); 174 175 sc->count--; 176 if (sc->count == 0) { 177 ppb_release_bus(ppbus, pcfclockdev); 178 } 179 180 return (0); 181 } 182 183 static void 184 pcfclock_write_cmd(dev_t dev, unsigned char command) 185 { 186 u_int unit = minor(dev); 187 device_t ppidev = UNITODEVICE(unit); 188 device_t ppbus = device_get_parent(ppidev); 189 unsigned char ctr = 14; 190 char i; 191 192 for (i = 0; i <= 7; i++) { 193 ppb_wdtr(ppbus, i); 194 AUTOFEED_CLOCK(i & 1 ? AFC_HI : AFC_LO); 195 DELAY(3000); 196 } 197 ppb_wdtr(ppbus, command); 198 AUTOFEED_CLOCK(AFC_LO); 199 DELAY(3000); 200 AUTOFEED_CLOCK(AFC_HI); 201 } 202 203 static void 204 pcfclock_display_data(dev_t dev, char buf[18]) 205 { 206 u_int unit = minor(dev); 207 #ifdef PCFCLOCK_VERBOSE 208 int year; 209 210 year = NR(buf, 14); 211 if (year < 70) 212 year += 100; 213 printf(PCFCLOCK_NAME "%d: %02d.%02d.%4d %02d:%02d:%02d, " 214 "battery status: %s\n", 215 unit, 216 NR(buf, 10), NR(buf, 12), 1900 + year, 217 NR(buf, 6), NR(buf, 4), NR(buf, 2), 218 PCFCLOCK_BATTERY_STATUS_LOW(buf) ? "LOW" : "ok"); 219 #else 220 if (PCFCLOCK_BATTERY_STATUS_LOW(buf)) 221 printf(PCFCLOCK_NAME "%d: BATTERY STATUS LOW ON\n", 222 unit); 223 #endif 224 } 225 226 static int 227 pcfclock_read_data(dev_t dev, char *buf, ssize_t bits) 228 { 229 u_int unit = minor(dev); 230 device_t ppidev = UNITODEVICE(unit); 231 device_t ppbus = device_get_parent(ppidev); 232 int i; 233 char waitfor; 234 int offset; 235 236 /* one byte per four bits */ 237 bzero(buf, ((bits + 3) >> 2) + 1); 238 239 waitfor = 100; 240 for (i = 0; i <= bits; i++) { 241 /* wait for clock, maximum (waitfor*100) usec */ 242 while(!CLOCK_OK && --waitfor > 0) 243 DELAY(100); 244 245 /* timed out? */ 246 if (!waitfor) 247 return (EIO); 248 249 waitfor = 100; /* reload */ 250 251 /* give it some time */ 252 DELAY(500); 253 254 /* calculate offset into buffer */ 255 offset = i >> 2; 256 buf[offset] <<= 1; 257 258 if (BIT_SET) 259 buf[offset] |= 1; 260 } 261 262 return (0); 263 } 264 265 static int 266 pcfclock_read_dev(dev_t dev, char *buf, int maxretries) 267 { 268 u_int unit = minor(dev); 269 device_t ppidev = UNITODEVICE(unit); 270 device_t ppbus = device_get_parent(ppidev); 271 int error = 0; 272 273 ppb_set_mode(ppbus, PPB_COMPATIBLE); 274 275 while (--maxretries > 0) { 276 pcfclock_write_cmd(dev, PCFCLOCK_CMD_TIME); 277 if (pcfclock_read_data(dev, buf, 68)) 278 continue; 279 280 if (!PCFCLOCK_CORRECT_SYNC(buf)) 281 continue; 282 283 if (!PCFCLOCK_CORRECT_FORMAT(buf)) 284 continue; 285 286 break; 287 } 288 289 if (!maxretries) 290 error = EIO; 291 292 return (error); 293 } 294 295 static int 296 pcfclock_read(struct dev_read_args *ap) 297 { 298 dev_t dev = ap->a_head.a_dev; 299 u_int unit = minor(dev); 300 char buf[18]; 301 int error = 0; 302 303 if (ap->a_uio->uio_resid < 18) 304 return (ERANGE); 305 306 error = pcfclock_read_dev(dev, buf, PCFCLOCK_MAX_RETRIES); 307 308 if (error) { 309 printf(PCFCLOCK_NAME "%d: no PCF found\n", unit); 310 } else { 311 pcfclock_display_data(dev, buf); 312 313 uiomove(buf, 18, ap->a_uio); 314 } 315 316 return (error); 317 } 318 319 /* 320 * Because pcfclock is a static device that always exists under any 321 * attached ppbus, and not scanned by the ppbus, we need an identify function 322 * to create the device. 323 */ 324 static device_method_t pcfclock_methods[] = { 325 /* device interface */ 326 DEVMETHOD(device_identify, bus_generic_identify), 327 DEVMETHOD(device_probe, pcfclock_probe), 328 DEVMETHOD(device_attach, pcfclock_attach), 329 330 { 0, 0 } 331 }; 332 333 static driver_t pcfclock_driver = { 334 PCFCLOCK_NAME, 335 pcfclock_methods, 336 sizeof(struct pcfclock_data), 337 }; 338 339 DRIVER_MODULE(pcfclock, ppbus, pcfclock_driver, pcfclock_devclass, 0, 0); 340 341