1 /* $OpenBSD: ds1307.c,v 1.2 2020/04/27 12:41:44 kettenis Exp $ */ 2 3 /* 4 * Copyright (c) 2016 Marcus Glocker <mglocker@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/param.h> 20 #include <sys/systm.h> 21 #include <sys/device.h> 22 23 #include <dev/clock_subr.h> 24 #include <dev/i2c/i2cvar.h> 25 26 /* 27 * Defines. 28 */ 29 /* RTC Registers */ 30 #define DS1307_SEC_REG 0x00 31 #define DS1307_SEC_MASK 0x7f 32 #define DS1307_SEC_MASK_CH 0x80 /* Clock Halt bit */ 33 #define DS1307_SEC_BIT_CH 7 /* 0 = osc enabled, 1 = osc disabled */ 34 #define DS1307_MIN_REG 0x01 35 #define DS1307_MIN_MASK 0x7f 36 #define DS1307_HOUR_REG 0x02 37 #define DS1307_HOUR_MASK 0x3f 38 #define DS1307_HOUR_MASK_MODE 0x40 /* Hour Mode bit */ 39 #define DS1307_HOUR_BIT_MODE 6 /* 0 = 24h mode, 1 = 12h mode */ 40 #define DS1307_WDAY_REG 0x03 41 #define DS1307_WDAY_MASK 0x07 42 #define DS1307_DATE_REG 0x04 43 #define DS1307_DATE_MASK 0x3f 44 #define DS1307_MONTH_REG 0x05 45 #define DS1307_MONTH_MASK 0x1f 46 #define DS1307_YEAR_REG 0x06 47 #define DS1307_YEAR_MASK 0xff 48 #define DS1307_CTRL_REG 0x07 49 50 /* RAM Registers */ 51 #define DS1307_RAM_REG 0x08 /* RAM address space 0x08 - 0x3f */ 52 53 /* 54 * Driver structure. 55 */ 56 struct maxrtc_softc { 57 struct device sc_dev; 58 i2c_tag_t sc_tag; 59 int sc_addr; 60 struct todr_chip_handle sc_todr; 61 }; 62 63 /* 64 * Prototypes. 65 */ 66 int maxrtc_match(struct device *, void *, void *); 67 void maxrtc_attach(struct device *, struct device *, void *); 68 int maxrtc_read(struct maxrtc_softc *, uint8_t *, uint8_t, 69 uint8_t *, uint8_t); 70 int maxrtc_write(struct maxrtc_softc *, uint8_t *, uint8_t); 71 int maxrtc_enable_osc(struct maxrtc_softc *); 72 int maxrtc_set_24h_mode(struct maxrtc_softc *); 73 int maxrtc_gettime(struct todr_chip_handle *, struct timeval *); 74 int maxrtc_settime(struct todr_chip_handle *, struct timeval *); 75 int maxrtc_getcal(struct todr_chip_handle *, int *); 76 int maxrtc_setcal(struct todr_chip_handle *, int); 77 78 /* 79 * Driver glue structures. 80 */ 81 struct cfattach maxrtc_ca = { 82 sizeof(struct maxrtc_softc), maxrtc_match, maxrtc_attach 83 }; 84 85 struct cfdriver maxrtc_cd = { 86 NULL, "maxrtc", DV_DULL 87 }; 88 89 extern todr_chip_handle_t todr_handle; 90 91 /* 92 * Functions. 93 */ 94 int 95 maxrtc_match(struct device *parent, void *v, void *arg) 96 { 97 struct i2c_attach_args *ia = arg; 98 99 if (strcmp(ia->ia_name, "dallas,ds1307") == 0 || 100 strcmp(ia->ia_name, "ds1307") == 0) 101 return (1); 102 103 return (0); 104 } 105 106 void 107 maxrtc_attach(struct device *parent, struct device *self, void *arg) 108 { 109 struct maxrtc_softc *sc = (struct maxrtc_softc *)self; 110 struct i2c_attach_args *ia = arg; 111 112 sc->sc_tag = ia->ia_tag; 113 sc->sc_addr = ia->ia_addr; 114 sc->sc_todr.cookie = sc; 115 sc->sc_todr.todr_gettime = maxrtc_gettime; 116 sc->sc_todr.todr_settime = maxrtc_settime; 117 sc->sc_todr.todr_getcal = maxrtc_getcal; 118 sc->sc_todr.todr_setcal = maxrtc_setcal; 119 sc->sc_todr.todr_setwen = NULL; 120 121 if (maxrtc_enable_osc(sc) == -1) 122 return; 123 124 if (maxrtc_set_24h_mode(sc) == -1) 125 return; 126 127 todr_handle = &sc->sc_todr; 128 } 129 130 int 131 maxrtc_read(struct maxrtc_softc *sc, uint8_t *cmd, uint8_t cmd_len, 132 uint8_t *data, uint8_t data_len) 133 { 134 int r; 135 136 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 137 if ((r = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, 138 cmd, cmd_len, data, data_len, I2C_F_POLL))) 139 printf("%s: maxrtc_read failed\n", sc->sc_dev.dv_xname); 140 iic_release_bus(sc->sc_tag, I2C_F_POLL); 141 142 return (r); 143 } 144 145 int 146 maxrtc_write(struct maxrtc_softc *sc, uint8_t *data, uint8_t data_len) 147 { 148 int r; 149 150 /* 151 * On write operation the DS1307 requires the target address to be 152 * stored in the first byte of the data packet. Therefore we don't 153 * fill up the command packet here. 154 */ 155 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 156 if ((r = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, 157 NULL, 0, data, data_len, I2C_F_POLL))) 158 printf("%s: maxrtc_write failed\n", sc->sc_dev.dv_xname); 159 iic_release_bus(sc->sc_tag, I2C_F_POLL); 160 161 return (r); 162 } 163 164 int 165 maxrtc_enable_osc(struct maxrtc_softc *sc) 166 { 167 uint8_t cmd; 168 uint8_t data_r; 169 uint8_t data_w[2]; 170 171 cmd = DS1307_SEC_REG; 172 data_r = 0; 173 if (maxrtc_read(sc, &cmd, sizeof(cmd), &data_r, sizeof(data_r))) { 174 printf("%s: maxrtc_enable_osc failed\n", sc->sc_dev.dv_xname); 175 return (-1); 176 } 177 if ((data_r & DS1307_SEC_MASK_CH) == 0) { 178 /* oscilliator is already enabled */ 179 printf(": rtc is ok\n"); 180 return (0); 181 } 182 printf(": rtc was halted, check battery\n"); 183 184 /* enable the oscillator */ 185 data_r |= 0 << DS1307_SEC_BIT_CH; 186 data_w[0] = DS1307_SEC_REG; 187 data_w[1] = data_r; 188 if (maxrtc_write(sc, data_w, sizeof(data_w))) { 189 printf("%s: maxrtc_enable_osc failed\n", sc->sc_dev.dv_xname); 190 return (-1); 191 } 192 193 return (0); 194 } 195 196 int 197 maxrtc_set_24h_mode(struct maxrtc_softc *sc) 198 { 199 uint8_t cmd; 200 uint8_t data_r; 201 uint8_t data_w[2]; 202 203 cmd = DS1307_HOUR_REG; 204 data_r = 0; 205 if (maxrtc_read(sc, &cmd, sizeof(cmd), &data_r, sizeof(data_r))) { 206 printf("%s: maxrtc_set_24h_mode failed\n", sc->sc_dev.dv_xname); 207 return (-1); 208 } 209 if ((data_r & DS1307_HOUR_MASK_MODE) == 0) { 210 /* 24h mode is already set */ 211 return (0); 212 } 213 214 /* set 24h mode */ 215 data_r |= 0 << DS1307_HOUR_BIT_MODE; 216 data_w[0] = DS1307_HOUR_REG; 217 data_w[1] = data_r; 218 if (maxrtc_write(sc, data_w, sizeof(data_w))) { 219 printf("%s: maxrtc_set_24h_mode failed\n", sc->sc_dev.dv_xname); 220 return (-1); 221 } 222 223 return (0); 224 } 225 226 int 227 maxrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv) 228 { 229 struct maxrtc_softc *sc = ch->cookie; 230 struct clock_ymdhms dt; 231 uint8_t cmd; 232 uint8_t data[7]; 233 234 cmd = DS1307_SEC_REG; 235 memset(data, 0, sizeof(data)); 236 if (maxrtc_read(sc, &cmd, sizeof(cmd), data, sizeof(data))) { 237 printf("%s: maxrtc_gettime failed\n", sc->sc_dev.dv_xname); 238 return (-1); 239 } 240 241 dt.dt_sec = FROMBCD(data[DS1307_SEC_REG] & DS1307_SEC_MASK); 242 dt.dt_min = FROMBCD(data[DS1307_MIN_REG] & DS1307_MIN_MASK); 243 dt.dt_hour = FROMBCD(data[DS1307_HOUR_REG] & DS1307_HOUR_MASK); 244 dt.dt_wday = FROMBCD(data[DS1307_WDAY_REG] & DS1307_WDAY_MASK); 245 dt.dt_day = FROMBCD(data[DS1307_DATE_REG] & DS1307_DATE_MASK); 246 dt.dt_mon = FROMBCD(data[DS1307_MONTH_REG] & DS1307_MONTH_MASK); 247 dt.dt_year = FROMBCD(data[DS1307_YEAR_REG] & DS1307_YEAR_MASK) + 2000; 248 249 tv->tv_sec = clock_ymdhms_to_secs(&dt); 250 tv->tv_usec = 0; 251 252 return (0); 253 } 254 255 int 256 maxrtc_settime(struct todr_chip_handle *ch, struct timeval *tv) 257 { 258 struct maxrtc_softc *sc = ch->cookie; 259 struct clock_ymdhms dt; 260 uint8_t data[8]; 261 262 clock_secs_to_ymdhms(tv->tv_sec, &dt); 263 264 data[0] = DS1307_SEC_REG; 265 data[1] = TOBCD(dt.dt_sec); /* this will also enable the osc */ 266 data[2] = TOBCD(dt.dt_min); 267 data[3] = TOBCD(dt.dt_hour); /* this will also set 24h mode */ 268 data[4] = TOBCD(dt.dt_wday); 269 data[5] = TOBCD(dt.dt_day); 270 data[6] = TOBCD(dt.dt_mon); 271 data[7] = TOBCD(dt.dt_year - 2000); 272 if (maxrtc_write(sc, data, sizeof(data))) { 273 printf("%s: maxrtc_settime failed\n", sc->sc_dev.dv_xname); 274 return (-1); 275 } 276 277 return (0); 278 } 279 280 int 281 maxrtc_getcal(struct todr_chip_handle *ch, int *cal) 282 { 283 return (EOPNOTSUPP); 284 } 285 286 int 287 maxrtc_setcal(struct todr_chip_handle *ch, int cal) 288 { 289 return (EOPNOTSUPP); 290 } 291