Home | History | Annotate | Line # | Download | only in ic
mm58167.c revision 1.3
      1 /*	$NetBSD: mm58167.c,v 1.3 2001/11/13 13:14:41 lukem 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/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: mm58167.c,v 1.3 2001/11/13 13:14:41 lukem Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/malloc.h>
     48 #include <sys/systm.h>
     49 #include <sys/errno.h>
     50 #include <sys/device.h>
     51 
     52 #include <machine/bus.h>
     53 #include <dev/clock_subr.h>
     54 #include <dev/ic/mm58167var.h>
     55 
     56 int mm58167_gettime __P((todr_chip_handle_t, struct timeval *));
     57 int mm58167_settime __P((todr_chip_handle_t, struct timeval *));
     58 int mm58167_getcal __P((todr_chip_handle_t, int *));
     59 int mm58167_setcal __P((todr_chip_handle_t, int));
     60 
     61 /*
     62  * To quote SunOS's todreg.h:
     63  * "This brain damaged chip insists on keeping the time in
     64  *  MM/DD HH:MM:SS format, even though it doesn't know about
     65  *  leap years and Feb. 29, thus making it nearly worthless."
     66  */
     67 #define mm58167_read(sc, r) bus_space_read_1(sc->mm58167_regt, sc->mm58167_regh, sc-> r)
     68 #define mm58167_write(sc, r, v) bus_space_write_1(sc->mm58167_regt, sc->mm58167_regh, sc-> r, v)
     69 
     70 todr_chip_handle_t
     71 mm58167_attach(sc)
     72 	struct mm58167_softc *sc;
     73 {
     74 	struct todr_chip_handle *handle;
     75 
     76 	printf(": mm58167");
     77 
     78 	handle = &sc->_mm58167_todr_handle;
     79 	memset(handle, 0, sizeof(handle));
     80 	handle->cookie = sc;
     81 	handle->todr_gettime = mm58167_gettime;
     82 	handle->todr_settime = mm58167_settime;
     83 	handle->todr_getcal = mm58167_getcal;
     84 	handle->todr_setcal = mm58167_setcal;
     85 	return (handle);
     86 }
     87 
     88 /*
     89  * Set up the system's time, given a `reasonable' time value.
     90  */
     91 int
     92 mm58167_gettime(handle, tv)
     93 	todr_chip_handle_t handle;
     94 	struct timeval *tv;
     95 {
     96 	struct mm58167_softc *sc = handle->cookie;
     97 	struct clock_ymdhms dt_hardware;
     98 	struct clock_ymdhms dt_reasonable;
     99 	int s;
    100 	u_int8_t byte_value;
    101 	int leap_year, had_leap_day;
    102 
    103 	/* First, read the date out of the chip. */
    104 
    105 	/* No interrupts while we're in the chip. */
    106 	s = splhigh();
    107 
    108 	/* Reset the status bit: */
    109 	byte_value = mm58167_read(sc, mm58167_status);
    110 
    111 	/*
    112 	 * Read the date values until we get a coherent read (one
    113 	 * where the status stays zero, indicating no increment was
    114 	 * rippling through while we were reading).
    115 	 */
    116 	do {
    117 #define _MM58167_GET(dt_f, mm_f) byte_value = mm58167_read(sc, mm_f); dt_hardware.dt_f = FROMBCD(byte_value)
    118 		_MM58167_GET(dt_mon, mm58167_mon);
    119 		_MM58167_GET(dt_day, mm58167_day);
    120 		_MM58167_GET(dt_hour, mm58167_hour);
    121 		_MM58167_GET(dt_min, mm58167_min);
    122 		_MM58167_GET(dt_sec, mm58167_sec);
    123 #undef _MM58167_GET
    124 	} while ((mm58167_read(sc, mm58167_status) & 1) == 0);
    125 
    126 	splx(s);
    127 
    128 	/* Convert the reasonable time into a date: */
    129 	clock_secs_to_ymdhms(tv->tv_sec, &dt_reasonable);
    130 
    131 	/*
    132 	 * We need to fake a hardware year.  if the hardware MM/DD
    133 	 * HH:MM:SS date is less than the reasonable MM/DD
    134 	 * HH:MM:SS, call it the reasonable year plus one, else call
    135 	 * it the reasonable year.
    136 	 */
    137 	if (dt_hardware.dt_mon < dt_reasonable.dt_mon ||
    138 	    (dt_hardware.dt_mon == dt_reasonable.dt_mon &&
    139 		(dt_hardware.dt_day < dt_reasonable.dt_day ||
    140 		    (dt_hardware.dt_day == dt_reasonable.dt_day &&
    141 			(dt_hardware.dt_hour < dt_reasonable.dt_hour ||
    142 			    (dt_hardware.dt_hour == dt_reasonable.dt_hour &&
    143 				(dt_hardware.dt_min < dt_reasonable.dt_min ||
    144 				    (dt_hardware.dt_min == dt_reasonable.dt_min &&
    145 					(dt_hardware.dt_sec < dt_reasonable.dt_sec))))))))) {
    146 	  dt_hardware.dt_year = dt_reasonable.dt_year + 1;
    147 	} else {
    148 	  dt_hardware.dt_year = dt_reasonable.dt_year;
    149 	}
    150 
    151 	/* convert the hardware date into a time: */
    152 	tv->tv_sec = clock_ymdhms_to_secs(&dt_hardware);
    153 	tv->tv_usec = 0;
    154 
    155 	/*
    156 	 * Make a reasonable effort to see if a leap day has passed
    157 	 * that we need to account for.  This does the right thing
    158 	 * only when the system was shut down before a leap day, and
    159 	 * it is now after that leap day.  It doesn't do the right
    160 	 * thing when a leap day happened while the machine was last
    161 	 * up.  When that happens, the hardware clock becomes
    162 	 * instantly wrong forever, until it gets fixed for some
    163 	 * reason.  Use NTP to deal.
    164 	 */
    165 
    166 	/*
    167 	 * This may have happened if the hardware says we're into
    168 	 * March in the following year.  Check that following year for
    169 	 * a leap day.
    170 	 */
    171 	if (dt_hardware.dt_year > dt_reasonable.dt_year &&
    172 	    dt_hardware.dt_mon >= 3) {
    173 	  leap_year = dt_hardware.dt_year;
    174 	}
    175 
    176 	/*
    177 	 * This may have happened if the hardware says we're in the
    178 	 * following year, and the system was shut down before March
    179 	 * the previous year.  check that previous year for a leap
    180 	 * day.
    181 	 */
    182 	else if (dt_hardware.dt_year > dt_reasonable.dt_year &&
    183 		 dt_reasonable.dt_mon < 3) {
    184 	  leap_year = dt_reasonable.dt_year;
    185 	}
    186 
    187 	/*
    188 	 * This may have happened if the hardware says we're in the
    189 	 * same year, but we weren't to March before, and we're in or
    190 	 * past March now.  Check this year for a leap day.
    191 	 */
    192 	else if (dt_hardware.dt_year == dt_reasonable.dt_year
    193 		 && dt_reasonable.dt_mon < 3
    194 		 && dt_hardware.dt_mon >= 3) {
    195 	  leap_year = dt_reasonable.dt_year;
    196 	}
    197 
    198 	/*
    199 	 * Otherwise, no leap year to check.
    200 	 */
    201 	else {
    202 	  leap_year = 0;
    203 	}
    204 
    205 	/* Do the real leap day check. */
    206 	had_leap_day = 0;
    207 	if (leap_year > 0) {
    208 		if ((leap_year & 3) == 0) {
    209 			had_leap_day = 1;
    210 			if ((leap_year % 100) == 0) {
    211 				had_leap_day = 0;
    212 				if ((leap_year % 400) == 0)
    213 					had_leap_day = 1;
    214 			}
    215 		}
    216 	}
    217 
    218 	/*
    219 	 * If we had a leap day, adjust the value we will return, and
    220 	 * also update the hardware clock.
    221 	 */
    222 	/*
    223 	 * XXX - Since this update just writes back a corrected
    224 	 * version of what we read out above, we lose whatever
    225 	 * amount of time the clock has advanced since that read.
    226 	 * Use NTP to deal.
    227 	 */
    228 	if (had_leap_day) {
    229 	  tv->tv_sec += SECDAY;
    230 	  todr_settime(handle, tv);
    231 	}
    232 
    233 	return (0);
    234 }
    235 
    236 int
    237 mm58167_settime(handle, tv)
    238 	todr_chip_handle_t handle;
    239 	struct timeval *tv;
    240 {
    241 	struct mm58167_softc *sc = handle->cookie;
    242 	struct clock_ymdhms dt_hardware;
    243 	int s;
    244 	u_int8_t byte_value;
    245 
    246 	/* Convert the seconds into ymdhms. */
    247 	clock_secs_to_ymdhms(tv->tv_sec, &dt_hardware);
    248 
    249 	/* No interrupts while we're in the chip. */
    250 	s = splhigh();
    251 
    252 	/*
    253 	 * Issue a GO command to reset everything less significant
    254 	 * than the minutes to zero.
    255 	 */
    256 	mm58167_write(sc, mm58167_go, 0xFF);
    257 
    258 	/* Load everything. */
    259 #define _MM58167_PUT(dt_f, mm_f) byte_value = TOBCD(dt_hardware.dt_f); mm58167_write(sc, mm_f, byte_value)
    260 	_MM58167_PUT(dt_mon, mm58167_mon);
    261 	_MM58167_PUT(dt_day, mm58167_day);
    262 	_MM58167_PUT(dt_hour, mm58167_hour);
    263 	_MM58167_PUT(dt_min, mm58167_min);
    264 	_MM58167_PUT(dt_sec, mm58167_sec);
    265 #undef _MM58167_PUT
    266 
    267 	splx(s);
    268 	return (0);
    269 }
    270 
    271 int
    272 mm58167_getcal(handle, vp)
    273 	todr_chip_handle_t handle;
    274 	int *vp;
    275 {
    276 	return (EOPNOTSUPP);
    277 }
    278 
    279 int
    280 mm58167_setcal(handle, v)
    281 	todr_chip_handle_t handle;
    282 	int v;
    283 {
    284 	return (EOPNOTSUPP);
    285 }
    286