1 /* $OpenBSD: rs5c372.c,v 1.6 2021/04/24 10:15:15 mpi 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 111 int 112 ricohrtc_match(struct device *parent, void *v, void *arg) 113 { 114 struct i2c_attach_args *ia = arg; 115 #ifdef PARANOID_CHECKS 116 u_int8_t data, cmd; 117 #endif 118 119 if (ia->ia_addr != RICOHRTC_ADDR) 120 return (0); 121 122 #ifdef PARANOID_CHECKS 123 /* Verify that the 'reserved bits in a few registers read 0 */ 124 if (iic_acquire_bus(ia->ia_tag, I2C_F_POLL)) { 125 printf("ricohrtc acquire fail\n"); 126 return (0); 127 } 128 129 cmd = RICOHRTC_SECONDS; 130 if (iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr, 131 &cmd, sizeof cmd, &data, sizeof data, I2C_F_POLL)) { 132 iic_release_bus(ia->ia_tag, I2C_F_POLL); 133 printf("ricohrtc read %d fail\n", cmd); 134 return (0); 135 } 136 if ((data & ~RICOHRTC_SECONDS_MASK) != 0) { 137 iic_release_bus(ia->ia_tag, I2C_F_POLL); 138 printf("ricohrtc second %d\n",data); 139 return (0); 140 } 141 142 cmd = RICOHRTC_MINUTES; 143 if (iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr, 144 &cmd, sizeof cmd, &data, sizeof data, I2C_F_POLL)) { 145 iic_release_bus(ia->ia_tag, I2C_F_POLL); 146 printf("ricohrtc read %d fail\n", cmd); 147 return (0); 148 } 149 150 if ((data & ~RICOHRTC_MINUTES_MASK) != 0) { 151 iic_release_bus(ia->ia_tag, I2C_F_POLL); 152 printf("ricohrtc minute %d\n",data); 153 return (0); 154 } 155 156 cmd = RICOHRTC_HOURS; 157 if (iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr, 158 &cmd, sizeof cmd, &data, sizeof data, I2C_F_POLL)) { 159 iic_release_bus(ia->ia_tag, I2C_F_POLL); 160 printf("ricohrtc read %d fail\n", cmd); 161 return (0); 162 } 163 if ((data & ~RICOHRTC_HOURS_24MASK) != 0) { 164 iic_release_bus(ia->ia_tag, I2C_F_POLL); 165 printf("ricohrtc hour %d\n",data); 166 return (0); 167 } 168 iic_release_bus(ia->ia_tag, I2C_F_POLL); 169 170 #endif 171 return (1); 172 } 173 174 void 175 ricohrtc_attach(struct device *parent, struct device *self, void *arg) 176 { 177 struct ricohrtc_softc *sc = (struct ricohrtc_softc *)self; 178 struct i2c_attach_args *ia = arg; 179 180 printf(": RICOH RS5C372[AB] Real-time Clock\n"); 181 182 sc->sc_tag = ia->ia_tag; 183 sc->sc_address = ia->ia_addr; 184 sc->sc_todr.cookie = sc; 185 sc->sc_todr.todr_gettime = ricohrtc_gettime; 186 sc->sc_todr.todr_settime = ricohrtc_settime; 187 sc->sc_todr.todr_setwen = NULL; 188 189 #if 0 190 todr_attach(&sc->sc_todr); 191 #else 192 /* XXX */ 193 { 194 extern todr_chip_handle_t todr_handle; 195 todr_handle = &sc->sc_todr; 196 } 197 #endif 198 199 /* Initialize RTC */ 200 ricohrtc_reg_write(sc, RICOHRTC_CONTROL2, RICOHRTC_CONTROL2_24HRS); 201 ricohrtc_reg_write(sc, RICOHRTC_CONTROL1, 0); 202 } 203 204 int 205 ricohrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv) 206 { 207 struct ricohrtc_softc *sc = ch->cookie; 208 struct clock_ymdhms dt; 209 210 memset(&dt, 0, sizeof(dt)); 211 if (ricohrtc_clock_read(sc, &dt) == 0) 212 return (-1); 213 214 tv->tv_sec = clock_ymdhms_to_secs(&dt); 215 tv->tv_usec = 0; 216 return (0); 217 } 218 219 int 220 ricohrtc_settime(struct todr_chip_handle *ch, struct timeval *tv) 221 { 222 struct ricohrtc_softc *sc = ch->cookie; 223 struct clock_ymdhms dt; 224 225 clock_secs_to_ymdhms(tv->tv_sec, &dt); 226 227 if (ricohrtc_clock_write(sc, &dt) == 0) 228 return (-1); 229 return (0); 230 } 231 232 void 233 ricohrtc_reg_write(struct ricohrtc_softc *sc, int reg, uint8_t val) 234 { 235 uint8_t cmd; 236 237 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 238 reg &= 0xf; 239 cmd = (reg << 4); 240 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address, 241 &cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL)) { 242 iic_release_bus(sc->sc_tag, I2C_F_POLL); 243 printf("%s: ricohrtc_reg_write: failed to write reg%d\n", 244 sc->sc_dev.dv_xname, reg); 245 return; 246 } 247 iic_release_bus(sc->sc_tag, I2C_F_POLL); 248 } 249 250 int 251 ricohrtc_clock_read(struct ricohrtc_softc *sc, struct clock_ymdhms *dt) 252 { 253 uint8_t bcd[RICOHRTC_NRTC_REGS]; 254 uint8_t cmd; 255 256 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 257 cmd = (RICOHRTC_SECONDS << 4); 258 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address, 259 &cmd, sizeof cmd, bcd, RICOHRTC_NRTC_REGS, I2C_F_POLL)) { 260 iic_release_bus(sc->sc_tag, I2C_F_POLL); 261 printf("%s: ricohrtc_clock_read: failed to read rtc\n", 262 sc->sc_dev.dv_xname); 263 return (0); 264 } 265 iic_release_bus(sc->sc_tag, I2C_F_POLL); 266 267 /* 268 * Convert the RICOHRTC's register values into something useable 269 */ 270 dt->dt_sec = FROMBCD(bcd[RICOHRTC_SECONDS] & RICOHRTC_SECONDS_MASK); 271 dt->dt_min = FROMBCD(bcd[RICOHRTC_MINUTES] & RICOHRTC_MINUTES_MASK); 272 dt->dt_hour = FROMBCD(bcd[RICOHRTC_HOURS] & RICOHRTC_HOURS_24MASK); 273 dt->dt_day = FROMBCD(bcd[RICOHRTC_DATE] & RICOHRTC_DATE_MASK); 274 dt->dt_mon = FROMBCD(bcd[RICOHRTC_MONTH] & RICOHRTC_MONTH_MASK); 275 dt->dt_year = FROMBCD(bcd[RICOHRTC_YEAR]) + POSIX_BASE_YEAR; 276 return (1); 277 } 278 279 int 280 ricohrtc_clock_write(struct ricohrtc_softc *sc, struct clock_ymdhms *dt) 281 { 282 uint8_t bcd[RICOHRTC_NRTC_REGS]; 283 uint8_t cmd; 284 285 /* 286 * Convert our time representation into something the RICOHRTC 287 * can understand. 288 */ 289 bcd[RICOHRTC_SECONDS] = TOBCD(dt->dt_sec); 290 bcd[RICOHRTC_MINUTES] = TOBCD(dt->dt_min); 291 bcd[RICOHRTC_HOURS] = TOBCD(dt->dt_hour); 292 bcd[RICOHRTC_DATE] = TOBCD(dt->dt_day); 293 bcd[RICOHRTC_DAY] = TOBCD(dt->dt_wday); 294 bcd[RICOHRTC_MONTH] = TOBCD(dt->dt_mon); 295 bcd[RICOHRTC_YEAR] = TOBCD(dt->dt_year - POSIX_BASE_YEAR); 296 297 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 298 cmd = (RICOHRTC_SECONDS << 4); 299 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address, 300 &cmd, sizeof cmd, bcd, RICOHRTC_NRTC_REGS, I2C_F_POLL)) { 301 iic_release_bus(sc->sc_tag, I2C_F_POLL); 302 printf("%s: ricohrtc_clock_write: failed to write rtc\n", 303 sc->sc_dev.dv_xname); 304 return (0); 305 } 306 iic_release_bus(sc->sc_tag, I2C_F_POLL); 307 return (1); 308 } 309