1 /* $NetBSD: mm58167.c,v 1.2 2001/07/07 16:13:49 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Matthew Fredette. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * National Semiconductor MM58167 time-of-day chip subroutines. 41 */ 42 43 #include <sys/param.h> 44 #include <sys/malloc.h> 45 #include <sys/systm.h> 46 #include <sys/errno.h> 47 #include <sys/device.h> 48 49 #include <machine/bus.h> 50 #include <dev/clock_subr.h> 51 #include <dev/ic/mm58167var.h> 52 53 int mm58167_gettime __P((todr_chip_handle_t, struct timeval *)); 54 int mm58167_settime __P((todr_chip_handle_t, struct timeval *)); 55 int mm58167_getcal __P((todr_chip_handle_t, int *)); 56 int mm58167_setcal __P((todr_chip_handle_t, int)); 57 58 /* 59 * To quote SunOS's todreg.h: 60 * "This brain damaged chip insists on keeping the time in 61 * MM/DD HH:MM:SS format, even though it doesn't know about 62 * leap years and Feb. 29, thus making it nearly worthless." 63 */ 64 #define mm58167_read(sc, r) bus_space_read_1(sc->mm58167_regt, sc->mm58167_regh, sc-> r) 65 #define mm58167_write(sc, r, v) bus_space_write_1(sc->mm58167_regt, sc->mm58167_regh, sc-> r, v) 66 67 todr_chip_handle_t 68 mm58167_attach(sc) 69 struct mm58167_softc *sc; 70 { 71 struct todr_chip_handle *handle; 72 73 printf(": mm58167"); 74 75 handle = &sc->_mm58167_todr_handle; 76 memset(handle, 0, sizeof(handle)); 77 handle->cookie = sc; 78 handle->todr_gettime = mm58167_gettime; 79 handle->todr_settime = mm58167_settime; 80 handle->todr_getcal = mm58167_getcal; 81 handle->todr_setcal = mm58167_setcal; 82 return (handle); 83 } 84 85 /* 86 * Set up the system's time, given a `reasonable' time value. 87 */ 88 int 89 mm58167_gettime(handle, tv) 90 todr_chip_handle_t handle; 91 struct timeval *tv; 92 { 93 struct mm58167_softc *sc = handle->cookie; 94 struct clock_ymdhms dt_hardware; 95 struct clock_ymdhms dt_reasonable; 96 int s; 97 u_int8_t byte_value; 98 int leap_year, had_leap_day; 99 100 /* First, read the date out of the chip. */ 101 102 /* No interrupts while we're in the chip. */ 103 s = splhigh(); 104 105 /* Reset the status bit: */ 106 byte_value = mm58167_read(sc, mm58167_status); 107 108 /* 109 * Read the date values until we get a coherent read (one 110 * where the status stays zero, indicating no increment was 111 * rippling through while we were reading). 112 */ 113 do { 114 #define _MM58167_GET(dt_f, mm_f) byte_value = mm58167_read(sc, mm_f); dt_hardware.dt_f = FROMBCD(byte_value) 115 _MM58167_GET(dt_mon, mm58167_mon); 116 _MM58167_GET(dt_day, mm58167_day); 117 _MM58167_GET(dt_hour, mm58167_hour); 118 _MM58167_GET(dt_min, mm58167_min); 119 _MM58167_GET(dt_sec, mm58167_sec); 120 #undef _MM58167_GET 121 } while ((mm58167_read(sc, mm58167_status) & 1) == 0); 122 123 splx(s); 124 125 /* Convert the reasonable time into a date: */ 126 clock_secs_to_ymdhms(tv->tv_sec, &dt_reasonable); 127 128 /* 129 * We need to fake a hardware year. if the hardware MM/DD 130 * HH:MM:SS date is less than the reasonable MM/DD 131 * HH:MM:SS, call it the reasonable year plus one, else call 132 * it the reasonable year. 133 */ 134 if (dt_hardware.dt_mon < dt_reasonable.dt_mon || 135 (dt_hardware.dt_mon == dt_reasonable.dt_mon && 136 (dt_hardware.dt_day < dt_reasonable.dt_day || 137 (dt_hardware.dt_day == dt_reasonable.dt_day && 138 (dt_hardware.dt_hour < dt_reasonable.dt_hour || 139 (dt_hardware.dt_hour == dt_reasonable.dt_hour && 140 (dt_hardware.dt_min < dt_reasonable.dt_min || 141 (dt_hardware.dt_min == dt_reasonable.dt_min && 142 (dt_hardware.dt_sec < dt_reasonable.dt_sec))))))))) { 143 dt_hardware.dt_year = dt_reasonable.dt_year + 1; 144 } else { 145 dt_hardware.dt_year = dt_reasonable.dt_year; 146 } 147 148 /* convert the hardware date into a time: */ 149 tv->tv_sec = clock_ymdhms_to_secs(&dt_hardware); 150 tv->tv_usec = 0; 151 152 /* 153 * Make a reasonable effort to see if a leap day has passed 154 * that we need to account for. This does the right thing 155 * only when the system was shut down before a leap day, and 156 * it is now after that leap day. It doesn't do the right 157 * thing when a leap day happened while the machine was last 158 * up. When that happens, the hardware clock becomes 159 * instantly wrong forever, until it gets fixed for some 160 * reason. Use NTP to deal. 161 */ 162 163 /* 164 * This may have happened if the hardware says we're into 165 * March in the following year. Check that following year for 166 * a leap day. 167 */ 168 if (dt_hardware.dt_year > dt_reasonable.dt_year && 169 dt_hardware.dt_mon >= 3) { 170 leap_year = dt_hardware.dt_year; 171 } 172 173 /* 174 * This may have happened if the hardware says we're in the 175 * following year, and the system was shut down before March 176 * the previous year. check that previous year for a leap 177 * day. 178 */ 179 else if (dt_hardware.dt_year > dt_reasonable.dt_year && 180 dt_reasonable.dt_mon < 3) { 181 leap_year = dt_reasonable.dt_year; 182 } 183 184 /* 185 * This may have happened if the hardware says we're in the 186 * same year, but we weren't to March before, and we're in or 187 * past March now. Check this year for a leap day. 188 */ 189 else if (dt_hardware.dt_year == dt_reasonable.dt_year 190 && dt_reasonable.dt_mon < 3 191 && dt_hardware.dt_mon >= 3) { 192 leap_year = dt_reasonable.dt_year; 193 } 194 195 /* 196 * Otherwise, no leap year to check. 197 */ 198 else { 199 leap_year = 0; 200 } 201 202 /* Do the real leap day check. */ 203 had_leap_day = 0; 204 if (leap_year > 0) { 205 if ((leap_year & 3) == 0) { 206 had_leap_day = 1; 207 if ((leap_year % 100) == 0) { 208 had_leap_day = 0; 209 if ((leap_year % 400) == 0) 210 had_leap_day = 1; 211 } 212 } 213 } 214 215 /* 216 * If we had a leap day, adjust the value we will return, and 217 * also update the hardware clock. 218 */ 219 /* 220 * XXX - Since this update just writes back a corrected 221 * version of what we read out above, we lose whatever 222 * amount of time the clock has advanced since that read. 223 * Use NTP to deal. 224 */ 225 if (had_leap_day) { 226 tv->tv_sec += SECDAY; 227 todr_settime(handle, tv); 228 } 229 230 return (0); 231 } 232 233 int 234 mm58167_settime(handle, tv) 235 todr_chip_handle_t handle; 236 struct timeval *tv; 237 { 238 struct mm58167_softc *sc = handle->cookie; 239 struct clock_ymdhms dt_hardware; 240 int s; 241 u_int8_t byte_value; 242 243 /* Convert the seconds into ymdhms. */ 244 clock_secs_to_ymdhms(tv->tv_sec, &dt_hardware); 245 246 /* No interrupts while we're in the chip. */ 247 s = splhigh(); 248 249 /* 250 * Issue a GO command to reset everything less significant 251 * than the minutes to zero. 252 */ 253 mm58167_write(sc, mm58167_go, 0xFF); 254 255 /* Load everything. */ 256 #define _MM58167_PUT(dt_f, mm_f) byte_value = TOBCD(dt_hardware.dt_f); mm58167_write(sc, mm_f, byte_value) 257 _MM58167_PUT(dt_mon, mm58167_mon); 258 _MM58167_PUT(dt_day, mm58167_day); 259 _MM58167_PUT(dt_hour, mm58167_hour); 260 _MM58167_PUT(dt_min, mm58167_min); 261 _MM58167_PUT(dt_sec, mm58167_sec); 262 #undef _MM58167_PUT 263 264 splx(s); 265 return (0); 266 } 267 268 int 269 mm58167_getcal(handle, vp) 270 todr_chip_handle_t handle; 271 int *vp; 272 { 273 return (EOPNOTSUPP); 274 } 275 276 int 277 mm58167_setcal(handle, v) 278 todr_chip_handle_t handle; 279 int v; 280 { 281 return (EOPNOTSUPP); 282 } 283