1 /* $OpenBSD: rs5c372.c,v 1.8 2022/10/12 13:39:50 kettenis 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 const 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 185 sc->sc_todr.cookie = sc; 186 sc->sc_todr.todr_gettime = ricohrtc_gettime; 187 sc->sc_todr.todr_settime = ricohrtc_settime; 188 sc->sc_todr.todr_setwen = NULL; 189 sc->sc_todr.todr_quality = 1000; 190 todr_attach(&sc->sc_todr); 191 192 /* Initialize RTC */ 193 ricohrtc_reg_write(sc, RICOHRTC_CONTROL2, RICOHRTC_CONTROL2_24HRS); 194 ricohrtc_reg_write(sc, RICOHRTC_CONTROL1, 0); 195 } 196 197 int 198 ricohrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv) 199 { 200 struct ricohrtc_softc *sc = ch->cookie; 201 struct clock_ymdhms dt; 202 203 memset(&dt, 0, sizeof(dt)); 204 if (ricohrtc_clock_read(sc, &dt) == 0) 205 return (-1); 206 207 tv->tv_sec = clock_ymdhms_to_secs(&dt); 208 tv->tv_usec = 0; 209 return (0); 210 } 211 212 int 213 ricohrtc_settime(struct todr_chip_handle *ch, struct timeval *tv) 214 { 215 struct ricohrtc_softc *sc = ch->cookie; 216 struct clock_ymdhms dt; 217 218 clock_secs_to_ymdhms(tv->tv_sec, &dt); 219 220 if (ricohrtc_clock_write(sc, &dt) == 0) 221 return (-1); 222 return (0); 223 } 224 225 void 226 ricohrtc_reg_write(struct ricohrtc_softc *sc, int reg, uint8_t val) 227 { 228 uint8_t cmd; 229 230 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 231 reg &= 0xf; 232 cmd = (reg << 4); 233 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address, 234 &cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL)) { 235 iic_release_bus(sc->sc_tag, I2C_F_POLL); 236 printf("%s: ricohrtc_reg_write: failed to write reg%d\n", 237 sc->sc_dev.dv_xname, reg); 238 return; 239 } 240 iic_release_bus(sc->sc_tag, I2C_F_POLL); 241 } 242 243 int 244 ricohrtc_clock_read(struct ricohrtc_softc *sc, struct clock_ymdhms *dt) 245 { 246 uint8_t bcd[RICOHRTC_NRTC_REGS]; 247 uint8_t cmd; 248 249 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 250 cmd = (RICOHRTC_SECONDS << 4); 251 if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address, 252 &cmd, sizeof cmd, bcd, RICOHRTC_NRTC_REGS, I2C_F_POLL)) { 253 iic_release_bus(sc->sc_tag, I2C_F_POLL); 254 printf("%s: ricohrtc_clock_read: failed to read rtc\n", 255 sc->sc_dev.dv_xname); 256 return (0); 257 } 258 iic_release_bus(sc->sc_tag, I2C_F_POLL); 259 260 /* 261 * Convert the RICOHRTC's register values into something useable 262 */ 263 dt->dt_sec = FROMBCD(bcd[RICOHRTC_SECONDS] & RICOHRTC_SECONDS_MASK); 264 dt->dt_min = FROMBCD(bcd[RICOHRTC_MINUTES] & RICOHRTC_MINUTES_MASK); 265 dt->dt_hour = FROMBCD(bcd[RICOHRTC_HOURS] & RICOHRTC_HOURS_24MASK); 266 dt->dt_day = FROMBCD(bcd[RICOHRTC_DATE] & RICOHRTC_DATE_MASK); 267 dt->dt_mon = FROMBCD(bcd[RICOHRTC_MONTH] & RICOHRTC_MONTH_MASK); 268 dt->dt_year = FROMBCD(bcd[RICOHRTC_YEAR]) + POSIX_BASE_YEAR; 269 return (1); 270 } 271 272 int 273 ricohrtc_clock_write(struct ricohrtc_softc *sc, struct clock_ymdhms *dt) 274 { 275 uint8_t bcd[RICOHRTC_NRTC_REGS]; 276 uint8_t cmd; 277 278 /* 279 * Convert our time representation into something the RICOHRTC 280 * can understand. 281 */ 282 bcd[RICOHRTC_SECONDS] = TOBCD(dt->dt_sec); 283 bcd[RICOHRTC_MINUTES] = TOBCD(dt->dt_min); 284 bcd[RICOHRTC_HOURS] = TOBCD(dt->dt_hour); 285 bcd[RICOHRTC_DATE] = TOBCD(dt->dt_day); 286 bcd[RICOHRTC_DAY] = TOBCD(dt->dt_wday); 287 bcd[RICOHRTC_MONTH] = TOBCD(dt->dt_mon); 288 bcd[RICOHRTC_YEAR] = TOBCD(dt->dt_year - POSIX_BASE_YEAR); 289 290 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 291 cmd = (RICOHRTC_SECONDS << 4); 292 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address, 293 &cmd, sizeof cmd, bcd, RICOHRTC_NRTC_REGS, I2C_F_POLL)) { 294 iic_release_bus(sc->sc_tag, I2C_F_POLL); 295 printf("%s: ricohrtc_clock_write: failed to write rtc\n", 296 sc->sc_dev.dv_xname); 297 return (0); 298 } 299 iic_release_bus(sc->sc_tag, I2C_F_POLL); 300 return (1); 301 } 302