1 /* $NetBSD: x1226.c,v 1.10 2007/01/13 18:42:45 cube Exp $ */ 2 3 /* 4 * Copyright (c) 2003 Shigeyuki Fukushima. 5 * All rights reserved. 6 * 7 * Written by Shigeyuki Fukushima for the NetBSD Project. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Shigeyuki Fukushima. 21 * 4. The name of Shigeyuki Fukushima may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY SHIGEYUKI FUKUSHIMA ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SHIGEYUKI FUKUSHIMA 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: x1226.c,v 1.10 2007/01/13 18:42:45 cube Exp $"); 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/device.h> 44 #include <sys/kernel.h> 45 #include <sys/fcntl.h> 46 #include <sys/uio.h> 47 #include <sys/conf.h> 48 #include <sys/event.h> 49 50 #include <dev/clock_subr.h> 51 52 #include <dev/i2c/i2cvar.h> 53 #include <dev/i2c/x1226reg.h> 54 55 struct xrtc_softc { 56 struct device sc_dev; 57 i2c_tag_t sc_tag; 58 int sc_address; 59 int sc_open; 60 struct todr_chip_handle sc_todr; 61 }; 62 63 static void xrtc_attach(struct device *, struct device *, void *); 64 static int xrtc_match(struct device *, struct cfdata *, void *); 65 66 CFATTACH_DECL(xrtc, sizeof(struct xrtc_softc), 67 xrtc_match, xrtc_attach, NULL, NULL); 68 extern struct cfdriver xrtc_cd; 69 70 dev_type_open(xrtc_open); 71 dev_type_close(xrtc_close); 72 dev_type_read(xrtc_read); 73 dev_type_write(xrtc_write); 74 75 const struct cdevsw xrtc_cdevsw = { 76 xrtc_open, xrtc_close, xrtc_read, xrtc_write, 77 noioctl, nostop, notty, nopoll, nommap, nokqfilter, D_OTHER 78 }; 79 80 static int xrtc_clock_read(struct xrtc_softc *, struct clock_ymdhms *); 81 static int xrtc_clock_write(struct xrtc_softc *, struct clock_ymdhms *); 82 static int xrtc_gettime(struct todr_chip_handle *, volatile struct timeval *); 83 static int xrtc_settime(struct todr_chip_handle *, volatile struct timeval *); 84 85 /* 86 * xrtc_match() 87 */ 88 static int 89 xrtc_match(struct device *parent, struct cfdata *cf, void *arg) 90 { 91 struct i2c_attach_args *ia = arg; 92 93 /* match only this RTC devices */ 94 if (ia->ia_addr == X1226_ADDR) 95 return (1); 96 97 return (0); 98 } 99 100 /* 101 * xrtc_attach() 102 */ 103 static void 104 xrtc_attach(struct device *parent, struct device *self, void *arg) 105 { 106 struct xrtc_softc *sc = device_private(self); 107 struct i2c_attach_args *ia = arg; 108 109 aprint_naive(": Real-time Clock/NVRAM\n"); 110 aprint_normal(": Xicor X1226 Real-time Clock/NVRAM\n"); 111 112 sc->sc_tag = ia->ia_tag; 113 sc->sc_address = ia->ia_addr; 114 sc->sc_open = 0; 115 sc->sc_todr.cookie = sc; 116 sc->sc_todr.todr_gettime = xrtc_gettime; 117 sc->sc_todr.todr_settime = xrtc_settime; 118 sc->sc_todr.todr_setwen = NULL; 119 120 todr_attach(&sc->sc_todr); 121 } 122 123 124 /*ARGSUSED*/ 125 int 126 xrtc_open(dev_t dev, int flag, int fmt, struct lwp *l) 127 { 128 struct xrtc_softc *sc; 129 130 if ((sc = device_lookup(&xrtc_cd, minor(dev))) == NULL) 131 return (ENXIO); 132 133 /* XXX: Locking */ 134 135 if (sc->sc_open) 136 return (EBUSY); 137 138 sc->sc_open = 1; 139 return (0); 140 } 141 142 /*ARGSUSED*/ 143 int 144 xrtc_close(dev_t dev, int flag, int fmt, struct lwp *l) 145 { 146 struct xrtc_softc *sc; 147 148 if ((sc = device_lookup(&xrtc_cd, minor(dev))) == NULL) 149 return (ENXIO); 150 151 sc->sc_open = 0; 152 return (0); 153 } 154 155 /*ARGSUSED*/ 156 int 157 xrtc_read(dev_t dev, struct uio *uio, int flags) 158 { 159 struct xrtc_softc *sc; 160 u_int8_t ch, cmdbuf[2]; 161 int addr, error; 162 163 if ((sc = device_lookup(&xrtc_cd, minor(dev))) == NULL) 164 return (ENXIO); 165 166 if (uio->uio_offset >= X1226_NVRAM_SIZE) 167 return (EINVAL); 168 169 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) 170 return (error); 171 172 while (uio->uio_resid && uio->uio_offset < X1226_NVRAM_SIZE) { 173 addr = (int)uio->uio_offset + X1226_NVRAM_START; 174 cmdbuf[0] = (addr >> 8) && 0xff; 175 cmdbuf[1] = addr && 0xff; 176 if ((error = iic_exec(sc->sc_tag, 177 I2C_OP_READ_WITH_STOP, 178 sc->sc_address, cmdbuf, 2, &ch, 1, 0)) != 0) { 179 iic_release_bus(sc->sc_tag, 0); 180 printf("%s: xrtc_read: read failed at 0x%x\n", 181 sc->sc_dev.dv_xname, (int)uio->uio_offset); 182 return (error); 183 } 184 if ((error = uiomove(&ch, 1, uio)) != 0) { 185 iic_release_bus(sc->sc_tag, 0); 186 return (error); 187 } 188 } 189 190 iic_release_bus(sc->sc_tag, 0); 191 192 return (0); 193 } 194 195 /*ARGSUSED*/ 196 int 197 xrtc_write(dev_t dev, struct uio *uio, int flags) 198 { 199 struct xrtc_softc *sc; 200 u_int8_t cmdbuf[3]; 201 int addr, error; 202 203 if ((sc = device_lookup(&xrtc_cd, minor(dev))) == NULL) 204 return (ENXIO); 205 206 if (uio->uio_offset >= X1226_NVRAM_SIZE) 207 return (EINVAL); 208 209 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) 210 return (error); 211 212 while (uio->uio_resid && uio->uio_offset < X1226_NVRAM_SIZE) { 213 addr = (int)uio->uio_offset + X1226_NVRAM_START; 214 cmdbuf[0] = (addr >> 8) && 0xff; 215 cmdbuf[1] = addr && 0xff; 216 if ((error = uiomove(&cmdbuf[2], 1, uio)) != 0) { 217 break; 218 } 219 if ((error = iic_exec(sc->sc_tag, 220 uio->uio_resid ? I2C_OP_WRITE : I2C_OP_WRITE_WITH_STOP, 221 sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) { 222 iic_release_bus(sc->sc_tag, 0); 223 printf("%s: xrtc_write: write failed at 0x%x\n", 224 sc->sc_dev.dv_xname, (int)uio->uio_offset); 225 return (error); 226 } 227 } 228 229 iic_release_bus(sc->sc_tag, 0); 230 231 return (0); 232 } 233 234 235 static int 236 xrtc_gettime(struct todr_chip_handle *ch, volatile struct timeval *tv) 237 { 238 struct xrtc_softc *sc = ch->cookie; 239 struct clock_ymdhms dt, check; 240 int retries; 241 242 memset(&dt, 0, sizeof(dt)); 243 memset(&check, 0, sizeof(check)); 244 245 retries = 5; 246 do { 247 xrtc_clock_read(sc, &dt); 248 xrtc_clock_read(sc, &check); 249 } while (memcmp(&dt, &check, sizeof(check)) != 0 && --retries); 250 251 tv->tv_sec = clock_ymdhms_to_secs(&dt); 252 tv->tv_usec = 0; 253 254 return (0); 255 } 256 257 static int 258 xrtc_settime(struct todr_chip_handle *ch, volatile struct timeval *tv) 259 { 260 struct xrtc_softc *sc = ch->cookie; 261 struct clock_ymdhms dt; 262 263 clock_secs_to_ymdhms(tv->tv_sec, &dt); 264 265 if (xrtc_clock_write(sc, &dt) == 0) 266 return (-1); 267 268 return (0); 269 } 270 271 static int 272 xrtc_clock_read(struct xrtc_softc *sc, struct clock_ymdhms *dt) 273 { 274 int i = 0; 275 u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[2]; 276 277 if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) { 278 printf("%s: xrtc_clock_read: failed to acquire I2C bus\n", 279 sc->sc_dev.dv_xname); 280 return (0); 281 } 282 283 /* Read each RTC register in order */ 284 for (i = 0 ; i < X1226_REG_RTC_SIZE ; i++) { 285 int addr = i + X1226_REG_RTC_BASE; 286 cmdbuf[0] = (addr >> 8) & 0xff; 287 cmdbuf[1] = addr & 0xff; 288 289 if (iic_exec(sc->sc_tag, 290 I2C_OP_READ_WITH_STOP, 291 sc->sc_address, cmdbuf, 2, 292 &bcd[i], 1, I2C_F_POLL)) { 293 iic_release_bus(sc->sc_tag, I2C_F_POLL); 294 printf("%s: xrtc_clock_read: failed to read rtc " 295 "at 0x%x\n", sc->sc_dev.dv_xname, i); 296 return (0); 297 } 298 } 299 300 /* Done with I2C */ 301 iic_release_bus(sc->sc_tag, I2C_F_POLL); 302 303 /* 304 * Convert the X1226's register bcd values 305 */ 306 dt->dt_sec = FROMBCD(bcd[X1226_REG_SC - X1226_REG_RTC_BASE] 307 & X1226_REG_SC_MASK); 308 dt->dt_min = FROMBCD(bcd[X1226_REG_MN - X1226_REG_RTC_BASE] 309 & X1226_REG_MN_MASK); 310 if (!(bcd[X1226_REG_HR - X1226_REG_RTC_BASE] & X1226_FLAG_HR_24H)) { 311 dt->dt_hour = FROMBCD(bcd[X1226_REG_HR - X1226_REG_RTC_BASE] 312 & X1226_REG_HR12_MASK); 313 if (bcd[X1226_REG_HR - X1226_REG_RTC_BASE] & X1226_FLAG_HR_12HPM) { 314 dt->dt_hour += 12; 315 } 316 } else { 317 dt->dt_hour = FROMBCD(bcd[X1226_REG_HR - X1226_REG_RTC_BASE] 318 & X1226_REG_HR24_MASK); 319 } 320 dt->dt_wday = FROMBCD(bcd[X1226_REG_DW - X1226_REG_RTC_BASE] 321 & X1226_REG_DT_MASK); 322 dt->dt_day = FROMBCD(bcd[X1226_REG_DT - X1226_REG_RTC_BASE] 323 & X1226_REG_DT_MASK); 324 dt->dt_mon = FROMBCD(bcd[X1226_REG_MO - X1226_REG_RTC_BASE] 325 & X1226_REG_MO_MASK); 326 dt->dt_year = FROMBCD(bcd[X1226_REG_YR - X1226_REG_RTC_BASE] 327 & X1226_REG_YR_MASK); 328 dt->dt_year += FROMBCD(bcd[X1226_REG_Y2K - X1226_REG_RTC_BASE] 329 & X1226_REG_Y2K_MASK) * 100; 330 331 return (1); 332 } 333 334 static int 335 xrtc_clock_write(struct xrtc_softc *sc, struct clock_ymdhms *dt) 336 { 337 int i = 0, addr; 338 u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[3]; 339 340 /* 341 * Convert our time to bcd values 342 */ 343 bcd[X1226_REG_SC - X1226_REG_RTC_BASE] = TOBCD(dt->dt_sec); 344 bcd[X1226_REG_MN - X1226_REG_RTC_BASE] = TOBCD(dt->dt_min); 345 bcd[X1226_REG_HR - X1226_REG_RTC_BASE] = TOBCD(dt->dt_hour) 346 | X1226_FLAG_HR_24H; 347 bcd[X1226_REG_DW - X1226_REG_RTC_BASE] = TOBCD(dt->dt_wday); 348 bcd[X1226_REG_DT - X1226_REG_RTC_BASE] = TOBCD(dt->dt_day); 349 bcd[X1226_REG_MO - X1226_REG_RTC_BASE] = TOBCD(dt->dt_mon); 350 bcd[X1226_REG_YR - X1226_REG_RTC_BASE] = TOBCD(dt->dt_year % 100); 351 bcd[X1226_REG_Y2K - X1226_REG_RTC_BASE] = TOBCD(dt->dt_year / 100); 352 353 if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) { 354 printf("%s: xrtc_clock_write: failed to acquire I2C bus\n", 355 sc->sc_dev.dv_xname); 356 return (0); 357 } 358 359 /* Unlock register: Write Enable Latch */ 360 addr = X1226_REG_SR; 361 cmdbuf[0] = ((addr >> 8) & 0xff); 362 cmdbuf[1] = (addr & 0xff); 363 cmdbuf[2] = X1226_FLAG_SR_WEL; 364 if (iic_exec(sc->sc_tag, 365 I2C_OP_WRITE_WITH_STOP, 366 sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0) != 0) { 367 iic_release_bus(sc->sc_tag, I2C_F_POLL); 368 printf("%s: xrtc_clock_write: " 369 "failed to write-unlock status register(WEL=1)\n", 370 sc->sc_dev.dv_xname); 371 return (0); 372 } 373 374 /* Unlock register: Register Write Enable Latch */ 375 addr = X1226_REG_SR; 376 cmdbuf[0] = ((addr >> 8) & 0xff); 377 cmdbuf[1] = (addr & 0xff); 378 cmdbuf[2] = X1226_FLAG_SR_WEL | X1226_FLAG_SR_RWEL; 379 if (iic_exec(sc->sc_tag, 380 I2C_OP_WRITE_WITH_STOP, 381 sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0) != 0) { 382 iic_release_bus(sc->sc_tag, I2C_F_POLL); 383 printf("%s: xrtc_clock_write: " 384 "failed to write-unlock status register(RWEL=1)\n", 385 sc->sc_dev.dv_xname); 386 return (0); 387 } 388 389 /* Write each RTC register in reverse order */ 390 for (i = (X1226_REG_RTC_SIZE - 1) ; i >= 0; i--) { 391 addr = i + X1226_REG_RTC_BASE; 392 cmdbuf[0] = ((addr >> 8) & 0xff); 393 cmdbuf[1] = (addr & 0xff); 394 if (iic_exec(sc->sc_tag, 395 I2C_OP_WRITE_WITH_STOP, 396 sc->sc_address, cmdbuf, 2, 397 &bcd[i], 1, I2C_F_POLL)) { 398 399 /* Lock register: WEL/RWEL off */ 400 addr = X1226_REG_SR; 401 cmdbuf[0] = ((addr >> 8) & 0xff); 402 cmdbuf[1] = (addr & 0xff); 403 cmdbuf[2] = 0; 404 iic_exec(sc->sc_tag, 405 I2C_OP_WRITE_WITH_STOP, 406 sc->sc_address, cmdbuf, 2, 407 &cmdbuf[2], 1, 0); 408 409 iic_release_bus(sc->sc_tag, I2C_F_POLL); 410 printf("%s: xrtc_clock_write: failed to write rtc " 411 "at 0x%x\n", sc->sc_dev.dv_xname, i); 412 return (0); 413 } 414 } 415 416 /* Lock register: WEL/RWEL off */ 417 addr = X1226_REG_SR; 418 cmdbuf[0] = ((addr >> 8) & 0xff); 419 cmdbuf[1] = (addr & 0xff); 420 cmdbuf[2] = 0; 421 if (iic_exec(sc->sc_tag, 422 I2C_OP_WRITE_WITH_STOP, 423 sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0) != 0) { 424 iic_release_bus(sc->sc_tag, I2C_F_POLL); 425 printf("%s: xrtc_clock_write: " 426 "failed to write-lock status register\n", 427 sc->sc_dev.dv_xname); 428 return (0); 429 } 430 431 iic_release_bus(sc->sc_tag, I2C_F_POLL); 432 return (1); 433 } 434