Home | History | Annotate | Line # | Download | only in ic
mk48txx.c revision 1.19
      1 /*	$NetBSD: mk48txx.c,v 1.19 2006/09/04 23:45:30 gdamore Exp $ */
      2 /*-
      3  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Paul Kranenburg.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *        This product includes software developed by the NetBSD
     20  *        Foundation, Inc. and its contributors.
     21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Mostek MK48T02, MK48T08, MK48T59 time-of-day chip subroutines.
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: mk48txx.c,v 1.19 2006/09/04 23:45:30 gdamore Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/device.h>
     48 #include <sys/errno.h>
     49 
     50 #include <machine/bus.h>
     51 #include <dev/clock_subr.h>
     52 #include <dev/ic/mk48txxreg.h>
     53 #include <dev/ic/mk48txxvar.h>
     54 
     55 int mk48txx_gettime(todr_chip_handle_t, volatile struct timeval *);
     56 int mk48txx_settime(todr_chip_handle_t, volatile struct timeval *);
     57 u_int8_t mk48txx_def_nvrd(struct mk48txx_softc *, int);
     58 void mk48txx_def_nvwr(struct mk48txx_softc *, int, u_int8_t);
     59 
     60 struct {
     61 	const char *name;
     62 	bus_size_t nvramsz;
     63 	bus_size_t clkoff;
     64 	int flags;
     65 #define MK48TXX_EXT_REGISTERS	1	/* Has extended register set */
     66 } mk48txx_models[] = {
     67 	{ "mk48t02", MK48T02_CLKSZ, MK48T02_CLKOFF, 0 },
     68 	{ "mk48t08", MK48T08_CLKSZ, MK48T08_CLKOFF, 0 },
     69 	{ "mk48t18", MK48T18_CLKSZ, MK48T18_CLKOFF, 0 },
     70 	{ "mk48t59", MK48T59_CLKSZ, MK48T59_CLKOFF, MK48TXX_EXT_REGISTERS },
     71 };
     72 
     73 void
     74 mk48txx_attach(sc)
     75 	struct mk48txx_softc *sc;
     76 {
     77 	todr_chip_handle_t handle;
     78 	int i;
     79 
     80 	printf(": %s", sc->sc_model);
     81 
     82 	i = sizeof(mk48txx_models) / sizeof(mk48txx_models[0]);
     83 	while (--i >= 0) {
     84 		if (strcmp(sc->sc_model, mk48txx_models[i].name) == 0)
     85 			break;
     86 	}
     87 	if (i < 0)
     88 		panic("mk48txx_attach: unsupported model");
     89 
     90 	sc->sc_nvramsz = mk48txx_models[i].nvramsz;
     91 	sc->sc_clkoffset = mk48txx_models[i].clkoff;
     92 
     93 	handle = &sc->sc_handle;
     94 	handle->cookie = sc;
     95 	handle->todr_gettime = mk48txx_gettime;
     96 	handle->todr_settime = mk48txx_settime;
     97 	handle->todr_setwen = NULL;
     98 
     99 	if (sc->sc_nvrd == NULL)
    100 		sc->sc_nvrd = mk48txx_def_nvrd;
    101 	if (sc->sc_nvwr == NULL)
    102 		sc->sc_nvwr = mk48txx_def_nvwr;
    103 }
    104 
    105 /*
    106  * Get time-of-day and convert to a `struct timeval'
    107  * Return 0 on success; an error number otherwise.
    108  */
    109 int
    110 mk48txx_gettime(handle, tv)
    111 	todr_chip_handle_t handle;
    112 	volatile struct timeval *tv;
    113 {
    114 	struct mk48txx_softc *sc;
    115 	bus_size_t clkoff;
    116 	struct clock_ymdhms dt;
    117 	int year;
    118 	u_int8_t csr;
    119 
    120 	sc = handle->cookie;
    121 	clkoff = sc->sc_clkoffset;
    122 
    123 	todr_wenable(handle, 1);
    124 
    125 	/* enable read (stop time) */
    126 	csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
    127 	csr |= MK48TXX_CSR_READ;
    128 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
    129 
    130 	dt.dt_sec = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_ISEC));
    131 	dt.dt_min = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IMIN));
    132 	dt.dt_hour = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IHOUR));
    133 	dt.dt_day = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IDAY));
    134 	dt.dt_wday = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IWDAY));
    135 	dt.dt_mon = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IMON));
    136 	year = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IYEAR));
    137 
    138 	year += sc->sc_year0;
    139 	if (year < POSIX_BASE_YEAR &&
    140 	    (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) == 0)
    141 		year += 100;
    142 
    143 	dt.dt_year = year;
    144 
    145 	/* time wears on */
    146 	csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
    147 	csr &= ~MK48TXX_CSR_READ;
    148 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
    149 	todr_wenable(handle, 0);
    150 
    151 	/* simple sanity checks */
    152 	if (dt.dt_mon > 12 || dt.dt_day > 31 ||
    153 	    dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
    154 		return EIO;
    155 
    156 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
    157 	if (tv->tv_sec == -1) return ERANGE;
    158 	tv->tv_usec = 0;
    159 	return 0;
    160 }
    161 
    162 /*
    163  * Set the time-of-day clock based on the value of the `struct timeval' arg.
    164  * Return 0 on success; an error number otherwise.
    165  */
    166 int
    167 mk48txx_settime(handle, tv)
    168 	todr_chip_handle_t handle;
    169 	volatile struct timeval *tv;
    170 {
    171 	struct mk48txx_softc *sc;
    172 	bus_size_t clkoff;
    173 	struct clock_ymdhms dt;
    174 	u_int8_t csr;
    175 	int year;
    176 	long sec;
    177 
    178 	sc = handle->cookie;
    179 	clkoff = sc->sc_clkoffset;
    180 
    181 	/* Note: we ignore `tv_usec' */
    182 	sec = tv->tv_sec + ((tv->tv_usec < 500000) ? 0 : 1);
    183 	clock_secs_to_ymdhms(sec, &dt);
    184 
    185 	year = dt.dt_year - sc->sc_year0;
    186 	if (year > 99 &&
    187 	    (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) == 0)
    188 		year -= 100;
    189 
    190 	todr_wenable(handle, 1);
    191 	/* enable write */
    192 	csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
    193 	csr |= MK48TXX_CSR_WRITE;
    194 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
    195 
    196 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_ISEC, TOBCD(dt.dt_sec));
    197 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_IMIN, TOBCD(dt.dt_min));
    198 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_IHOUR, TOBCD(dt.dt_hour));
    199 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_IWDAY, TOBCD(dt.dt_wday));
    200 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_IDAY, TOBCD(dt.dt_day));
    201 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_IMON, TOBCD(dt.dt_mon));
    202 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_IYEAR, TOBCD(year));
    203 
    204 	/* load them up */
    205 	csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
    206 	csr &= ~MK48TXX_CSR_WRITE;
    207 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
    208 	todr_wenable(handle, 0);
    209 	return 0;
    210 }
    211 
    212 int
    213 mk48txx_get_nvram_size(handle, vp)
    214 	todr_chip_handle_t handle;
    215 	bus_size_t *vp;
    216 {
    217 	struct mk48txx_softc *sc;
    218 
    219 	sc = handle->cookie;
    220 	*vp = sc->sc_nvramsz;
    221 	return 0;
    222 }
    223 
    224 u_int8_t
    225 mk48txx_def_nvrd(struct mk48txx_softc *sc, int off)
    226 {
    227 
    228 	return bus_space_read_1(sc->sc_bst, sc->sc_bsh, off);
    229 }
    230 
    231 void
    232 mk48txx_def_nvwr(struct mk48txx_softc *sc, int off, u_int8_t v)
    233 {
    234 
    235 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, v);
    236 }
    237