1 /* $NetBSD: rtc.c,v 1.2 2001/11/17 23:48:15 gmcgarry Exp $ */ 2 3 /* 4 * Copyright (c) 1988 University of Utah. 5 * Copyright (c) 1982, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: Utah $Hdr: clock.c 1.18 91/01/21$ 41 * 42 * @(#)clock.c 8.2 (Berkeley) 1/12/94 43 */ 44 45 /* 46 * attachment for HP300 real-time clock (RTC) 47 */ 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/device.h> 52 #include <sys/malloc.h> 53 54 #include <hp300/dev/intiovar.h> 55 #include <hp300/dev/rtcreg.h> 56 57 #include <dev/clock_subr.h> 58 #include <hp300/hp300/clockvar.h> 59 60 struct rtc_softc { 61 struct device sc_dev; 62 bus_space_tag_t sc_bst; 63 bus_space_handle_t sc_bsh; 64 }; 65 66 static int rtcmatch(struct device *, struct cfdata *, void *); 67 static void rtcattach(struct device *, struct device *, void *aux); 68 69 struct cfattach rtc_ca = { 70 sizeof (struct rtc_softc), rtcmatch, rtcattach, 71 }; 72 73 static int rtc_gettime(todr_chip_handle_t, struct timeval *); 74 static int rtc_settime(todr_chip_handle_t, struct timeval *); 75 static int rtc_getcal(todr_chip_handle_t, int *); 76 static int rtc_setcal(todr_chip_handle_t, int); 77 static u_int8_t rtc_readreg(struct rtc_softc *, int); 78 static u_int8_t rtc_writereg(struct rtc_softc *, int, u_int8_t); 79 80 81 static int 82 rtcmatch(parent, match, aux) 83 struct device *parent; 84 struct cfdata *match; 85 void *aux; 86 { 87 struct intio_attach_args *ia = aux; 88 89 if (strcmp("rtc ", ia->ia_modname) != 0) 90 return (0); 91 92 return (1); 93 } 94 95 static void 96 rtcattach(parent, self, aux) 97 struct device *parent, *self; 98 void *aux; 99 { 100 struct intio_attach_args *ia = aux; 101 struct rtc_softc *sc = (struct rtc_softc *)self; 102 struct todr_chip_handle *todr_handle; 103 104 printf("\n"); 105 106 if (bus_space_map(ia->ia_bst, ia->ia_iobase, INTIO_DEVSIZE, 0, 107 &sc->sc_bsh)) { 108 printf("%s: can't map registers\n", 109 sc->sc_dev.dv_xname); 110 return; 111 } 112 113 todr_handle = malloc(sizeof(struct todr_chip_handle), 114 M_DEVBUF, M_NOWAIT); 115 116 todr_handle->cookie = sc; 117 todr_handle->todr_gettime = rtc_gettime; 118 todr_handle->todr_settime = rtc_settime; 119 todr_handle->todr_getcal = rtc_getcal; 120 todr_handle->todr_setcal = rtc_setcal; 121 todr_handle->todr_setwen = NULL; 122 123 clockattach(todr_handle); 124 } 125 126 static int 127 rtc_gettime(todr_chip_handle_t handle, struct timeval *tv) 128 { 129 struct rtc_softc *sc = (struct rtc_softc *)handle->cookie; 130 int i, read_okay, year; 131 struct clock_ymdhms dt; 132 u_int8_t rtc_registers[NUM_RTC_REGS]; 133 134 /* read rtc registers */ 135 read_okay = 0; 136 while (!read_okay) { 137 read_okay = 1; 138 for (i = 0; i < NUM_RTC_REGS; i++) 139 rtc_registers[i] = rtc_readreg(sc, i); 140 for (i = 0; i < NUM_RTC_REGS; i++) 141 if (rtc_registers[i] != rtc_readreg(sc, i)) 142 read_okay = 0; 143 } 144 145 #define rtc_to_decimal(a,b) (rtc_registers[a] * 10 + rtc_registers[b]) 146 147 dt.dt_sec = rtc_to_decimal(1, 0); 148 dt.dt_min = rtc_to_decimal(3, 2); 149 dt.dt_hour = (rtc_registers[5] & RTC_REG5_HOUR) * 10 + rtc_registers[4]; 150 dt.dt_day = rtc_to_decimal(8, 7); 151 dt.dt_mon = rtc_to_decimal(10, 9); 152 153 year = rtc_to_decimal(12, 11) + RTC_BASE_YEAR; 154 if (year < POSIX_BASE_YEAR) 155 year += 100; 156 dt.dt_year = year; 157 158 #undef rtc_to_decimal 159 160 /* simple sanity checks */ 161 if (dt.dt_mon > 12 || dt.dt_day > 31 || 162 dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60) 163 return (1); 164 165 tv->tv_sec = clock_ymdhms_to_secs(&dt); 166 tv->tv_usec = 0; 167 return (0); 168 } 169 170 static int 171 rtc_settime(todr_chip_handle_t handle, struct timeval *tv) 172 { 173 struct rtc_softc *sc = (struct rtc_softc *)handle->cookie; 174 int i, year; 175 struct clock_ymdhms dt; 176 u_int8_t rtc_registers[NUM_RTC_REGS]; 177 178 /* Note: we ignore `tv_usec' */ 179 clock_secs_to_ymdhms(tv->tv_sec, &dt); 180 181 year = dt.dt_year - RTC_BASE_YEAR; 182 if (year > 99) 183 year -= 100; 184 185 #define decimal_to_rtc(a,b,n) \ 186 rtc_registers[a] = (n) % 10; \ 187 rtc_registers[b] = (n) / 10; 188 189 decimal_to_rtc(0, 1, dt.dt_sec); 190 decimal_to_rtc(2, 3, dt.dt_min); 191 decimal_to_rtc(7, 8, dt.dt_day); 192 decimal_to_rtc(9, 10, dt.dt_mon); 193 decimal_to_rtc(11, 12, year); 194 195 rtc_registers[4] = dt.dt_hour % 10; 196 rtc_registers[5] = ((dt.dt_hour / 10) & RTC_REG5_HOUR) | RTC_REG5_24HR; 197 198 rtc_registers[6] = 0; 199 200 #undef decimal_to_rtc 201 202 /* write rtc registers */ 203 rtc_writereg(sc, 15, 13); /* reset prescalar */ 204 for (i = 0; i < NUM_RTC_REGS; i++) 205 if (rtc_registers[i] != 206 rtc_writereg(sc, i, rtc_registers[i])) 207 return (1); 208 return (0); 209 } 210 211 static int 212 rtc_getcal(todr_chip_handle_t handle, int *vp) 213 { 214 return (EOPNOTSUPP); 215 } 216 217 static int 218 rtc_setcal(todr_chip_handle_t handle, int v) 219 { 220 return (EOPNOTSUPP); 221 } 222 223 static u_int8_t 224 rtc_readreg(struct rtc_softc *sc, int reg) 225 { 226 bus_space_tag_t bst = sc->sc_bst; 227 bus_space_handle_t bsh = sc->sc_bsh; 228 u_int8_t data; 229 int s = splvm(); 230 231 data = reg; 232 intio_device_writecmd(bst, bsh, RTC_SET_REG, &data, 1); 233 intio_device_readcmd(bst, bsh, RTC_READ_REG, &data); 234 235 splx(s); 236 return (data); 237 } 238 239 static u_int8_t 240 rtc_writereg(struct rtc_softc *sc, int reg, u_int8_t data) 241 { 242 bus_space_tag_t bst = sc->sc_bst; 243 bus_space_handle_t bsh = sc->sc_bsh; 244 u_int8_t tmp; 245 int s = splvm(); 246 247 tmp = (data << 4) | reg; 248 intio_device_writecmd(bst, bsh, RTC_SET_REG, &tmp, 1); 249 intio_device_writecmd(bst, bsh, RTC_WRITE_REG, NULL, 0); 250 intio_device_readcmd(bst, bsh, RTC_READ_REG, &tmp); 251 252 splx(s); 253 return (tmp); 254 } 255