1 /* $OpenBSD: rs5c372.c,v 1.5 2009/10/05 18:59:36 deraadt Exp $ */ 2 /* $NetBSD: rs5c372.c,v 1.5 2006/03/29 06:41:24 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 2005 Kimihiro Nonaka 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/device.h> 33 #include <sys/kernel.h> 34 #include <sys/fcntl.h> 35 #include <sys/uio.h> 36 #include <sys/conf.h> 37 #include <sys/event.h> 38 39 #include <dev/clock_subr.h> 40 41 #include <dev/i2c/i2cvar.h> 42 43 /* 44 * RS5C372[AB] Real-Time Clock 45 */ 46 47 #define RICOHRTC_ADDR 0x32 /* Fixed I2C Slave Address */ 48 49 #define RICOHRTC_SECONDS 0 50 #define RICOHRTC_MINUTES 1 51 #define RICOHRTC_HOURS 2 52 #define RICOHRTC_DAY 3 53 #define RICOHRTC_DATE 4 54 #define RICOHRTC_MONTH 5 55 #define RICOHRTC_YEAR 6 56 #define RICOHRTC_CLOCK_CORRECT 7 57 #define RICOHRTC_ALARMA_MIN 8 58 #define RICOHRTC_ALARMA_HOUR 9 59 #define RICOHRTC_ALARMA_DATE 10 60 #define RICOHRTC_ALARMB_MIN 11 61 #define RICOHRTC_ALARMB_HOUR 12 62 #define RICOHRTC_ALARMB_DATE 13 63 #define RICOHRTC_CONTROL1 14 64 #define RICOHRTC_CONTROL2 15 65 #define RICOHRTC_NREGS 16 66 #define RICOHRTC_NRTC_REGS 7 67 68 /* 69 * Bit definitions. 70 */ 71 #define RICOHRTC_SECONDS_MASK 0x7f 72 #define RICOHRTC_MINUTES_MASK 0x7f 73 #define RICOHRTC_HOURS_12HRS_PM (1u << 5) /* If 12 hr mode, set = PM */ 74 #define RICOHRTC_HOURS_12MASK 0x1f 75 #define RICOHRTC_HOURS_24MASK 0x3f 76 #define RICOHRTC_DAY_MASK 0x07 77 #define RICOHRTC_DATE_MASK 0x3f 78 #define RICOHRTC_MONTH_MASK 0x1f 79 #define RICOHRTC_CONTROL2_24HRS (1u << 5) 80 #define RICOHRTC_CONTROL2_XSTP (1u << 4) /* read */ 81 #define RICOHRTC_CONTROL2_ADJ (1u << 4) /* write */ 82 #define RICOHRTC_CONTROL2_NCLEN (1u << 3) 83 #define RICOHRTC_CONTROL2_CTFG (1u << 2) 84 #define RICOHRTC_CONTROL2_AAFG (1u << 1) 85 #define RICOHRTC_CONTROL2_BAFG (1u << 0) 86 87 struct ricohrtc_softc { 88 struct device sc_dev; 89 i2c_tag_t sc_tag; 90 int sc_address; 91 struct todr_chip_handle sc_todr; 92 }; 93 94 int ricohrtc_match(struct device *, void *, void *); 95 void ricohrtc_attach(struct device *, struct device *, void *); 96 97 struct cfattach ricohrtc_ca = { 98 sizeof(struct ricohrtc_softc), ricohrtc_match, ricohrtc_attach 99 }; 100 101 struct cfdriver ricohrtc_cd = { 102 NULL, "ricohrtc", DV_DULL 103 }; 104 105 void ricohrtc_reg_write(struct ricohrtc_softc *, int, uint8_t); 106 int ricohrtc_clock_read(struct ricohrtc_softc *, struct clock_ymdhms *); 107 int ricohrtc_clock_write(struct ricohrtc_softc *, struct clock_ymdhms *); 108 int ricohrtc_gettime(struct todr_chip_handle *, struct timeval *); 109 int ricohrtc_settime(struct todr_chip_handle *, struct timeval *); 110 int ricohrtc_getcal(struct todr_chip_handle *, int *); 111 int ricohrtc_setcal(struct todr_chip_handle *, int); 112 113 int 114 ricohrtc_match(struct device *parent, void *v, void *arg) 115 { 116 struct i2c_attach_args *ia = arg; 117 #ifdef PARANOID_CHECKS 118 u_int8_t data, cmd; 119 #endif 120 121 if (ia->ia_addr != RICOHRTC_ADDR) 122 return (0); 123 124 #ifdef PARANOID_CHECKS 125 /* Verify that the 'reserved bits in a few registers read 0 */ 126 if (iic_acquire_bus(ia->ia_tag, I2C_F_POLL)) { 127 printf("ricohrtc acquire fail\n"); 128 return (0); 129 } 130 131 cmd = RICOHRTC_SECONDS; 132 if (iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr, 133 &cmd, sizeof cmd, &data, sizeof data, I2C_F_POLL)) { 134 iic_release_bus(ia->ia_tag, I2C_F_POLL); 135 printf("ricohrtc read %d fail\n", cmd); 136 return (0); 137 } 138 if ((data & ~RICOHRTC_SECONDS_MASK) != 0) { 139 iic_release_bus(ia->ia_tag, I2C_F_POLL); 140 printf("ricohrtc second %d\n",data); 141 return (0); 142 } 143 144 cmd = RICOHRTC_MINUTES; 145 if (iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr, 146 &cmd, sizeof cmd, &data, sizeof data, I2C_F_POLL)) { 147 iic_release_bus(ia->ia_tag, I2C_F_POLL); 148 printf("ricohrtc read %d fail\n", cmd); 149 return (0); 150 } 151 152 if ((data & ~RICOHRTC_MINUTES_MASK) != 0) { 153 iic_release_bus(ia->ia_tag, I2C_F_POLL); 154 printf("ricohrtc minute %d\n",data); 155 return (0); 156 } 157 158 cmd = RICOHRTC_HOURS; 159 if (iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr, 160 &cmd, sizeof cmd, &data, sizeof data, I2C_F_POLL)) { 161 iic_release_bus(ia->ia_tag, I2C_F_POLL); 162 printf("ricohrtc read %d fail\n", cmd); 163 return (0); 164 } 165 if ((data & ~RICOHRTC_HOURS_24MASK) != 0) { 166 iic_release_bus(ia->ia_tag, I2C_F_POLL); 167 printf("ricohrtc hour %d\n",data); 168 return (0); 169 } 170 iic_release_bus(ia->ia_tag, I2C_F_POLL); 171 172 #endif 173 return (1); 174 } 175 176 void 177 ricohrtc_attach(struct device *parent, struct device *self, void *arg) 178 { 179 struct ricohrtc_softc *sc = (struct ricohrtc_softc *)self; 180 struct i2c_attach_args *ia = arg; 181 182 printf(": RICOH RS5C372[AB] Real-time Clock\n"); 183 184 sc->sc_tag = ia->ia_tag; 185 sc->sc_address = ia->ia_addr; 186 sc->sc_todr.cookie = sc; 187 sc->sc_todr.todr_gettime = ricohrtc_gettime; 188 sc->sc_todr.todr_settime = ricohrtc_settime; 189 sc->sc_todr.todr_getcal = ricohrtc_getcal; 190 sc->sc_todr.todr_setcal = ricohrtc_setcal; 191 sc->sc_todr.todr_setwen = NULL; 192 193 #if 0 194 todr_attach(&sc->sc_todr); 195 #else 196 /* XXX */ 197 { 198 extern todr_chip_handle_t todr_handle; 199 todr_handle = &sc->sc_todr; 200 } 201 #endif 202 203 /* Initialize RTC */ 204 ricohrtc_reg_write(sc, RICOHRTC_CONTROL2, RICOHRTC_CONTROL2_24HRS); 205 ricohrtc_reg_write(sc, RICOHRTC_CONTROL1, 0); 206 } 207 208 int 209 ricohrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv) 210 { 211 struct ricohrtc_softc *sc = ch->cookie; 212 struct clock_ymdhms dt; 213 214 memset(&dt, 0, sizeof(dt)); 215 if (ricohrtc_clock_read(sc, &dt) == 0) 216 return (-1); 217 218 tv->tv_sec = clock_ymdhms_to_secs(&dt); 219 tv->tv_usec = 0; 220 return (0); 221 } 222 223 int 224 ricohrtc_settime(struct todr_chip_handle *ch, struct timeval *tv) 225 { 226 struct ricohrtc_softc *sc = ch->cookie; 227 struct clock_ymdhms dt; 228 229 clock_secs_to_ymdhms(tv->tv_sec, &dt); 230 231 if (ricohrtc_clock_write(sc, &dt) == 0) 232 return (-1); 233 return (0); 234 } 235 236 int 237 ricohrtc_setcal(struct todr_chip_handle *ch, int cal) 238 { 239 240 return (EOPNOTSUPP); 241 } 242 243 int 244 ricohrtc_getcal(struct todr_chip_handle *ch, int *cal) 245 { 246 247 return (EOPNOTSUPP); 248 } 249 250 void 251 ricohrtc_reg_write(struct ricohrtc_softc *sc, int reg, uint8_t val) 252 { 253 uint8_t cmd; 254 255 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 256 reg &= 0xf; 257 cmd = (reg << 4); 258 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address, 259 &cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL)) { 260 iic_release_bus(sc->sc_tag, I2C_F_POLL); 261 printf("%s: ricohrtc_reg_write: failed to write reg%d\n", 262 sc->sc_dev.dv_xname, reg); 263 return; 264 } 265 iic_release_bus(sc->sc_tag, I2C_F_POLL); 266 } 267 268 int 269 ricohrtc_clock_read(struct ricohrtc_softc *sc, struct clock_ymdhms *dt) 270 { 271 uint8_t bcd[RICOHRTC_NRTC_REGS]; 272 uint8_t cmd; 273 274 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 275 cmd = (RICOHRTC_SECONDS << 4); 276 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address, 277 &cmd, sizeof cmd, bcd, RICOHRTC_NRTC_REGS, I2C_F_POLL)) { 278 iic_release_bus(sc->sc_tag, I2C_F_POLL); 279 printf("%s: ricohrtc_clock_read: failed to read rtc\n", 280 sc->sc_dev.dv_xname); 281 return (0); 282 } 283 iic_release_bus(sc->sc_tag, I2C_F_POLL); 284 285 /* 286 * Convert the RICOHRTC's register values into something useable 287 */ 288 dt->dt_sec = FROMBCD(bcd[RICOHRTC_SECONDS] & RICOHRTC_SECONDS_MASK); 289 dt->dt_min = FROMBCD(bcd[RICOHRTC_MINUTES] & RICOHRTC_MINUTES_MASK); 290 dt->dt_hour = FROMBCD(bcd[RICOHRTC_HOURS] & RICOHRTC_HOURS_24MASK); 291 dt->dt_day = FROMBCD(bcd[RICOHRTC_DATE] & RICOHRTC_DATE_MASK); 292 dt->dt_mon = FROMBCD(bcd[RICOHRTC_MONTH] & RICOHRTC_MONTH_MASK); 293 dt->dt_year = FROMBCD(bcd[RICOHRTC_YEAR]) + POSIX_BASE_YEAR; 294 return (1); 295 } 296 297 int 298 ricohrtc_clock_write(struct ricohrtc_softc *sc, struct clock_ymdhms *dt) 299 { 300 uint8_t bcd[RICOHRTC_NRTC_REGS]; 301 uint8_t cmd; 302 303 /* 304 * Convert our time representation into something the RICOHRTC 305 * can understand. 306 */ 307 bcd[RICOHRTC_SECONDS] = TOBCD(dt->dt_sec); 308 bcd[RICOHRTC_MINUTES] = TOBCD(dt->dt_min); 309 bcd[RICOHRTC_HOURS] = TOBCD(dt->dt_hour); 310 bcd[RICOHRTC_DATE] = TOBCD(dt->dt_day); 311 bcd[RICOHRTC_DAY] = TOBCD(dt->dt_wday); 312 bcd[RICOHRTC_MONTH] = TOBCD(dt->dt_mon); 313 bcd[RICOHRTC_YEAR] = TOBCD(dt->dt_year - POSIX_BASE_YEAR); 314 315 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 316 cmd = (RICOHRTC_SECONDS << 4); 317 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address, 318 &cmd, sizeof cmd, bcd, RICOHRTC_NRTC_REGS, I2C_F_POLL)) { 319 iic_release_bus(sc->sc_tag, I2C_F_POLL); 320 printf("%s: ricohrtc_clock_write: failed to write rtc\n", 321 sc->sc_dev.dv_xname); 322 return (0); 323 } 324 iic_release_bus(sc->sc_tag, I2C_F_POLL); 325 return (1); 326 } 327