Home | History | Annotate | Line # | Download | only in ic
mk48txx.c revision 1.26
      1 /*	$NetBSD: mk48txx.c,v 1.26 2011/01/04 01:28:15 matt 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  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * Mostek MK48T02, MK48T08, MK48T59 time-of-day chip subroutines.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: mk48txx.c,v 1.26 2011/01/04 01:28:15 matt Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/device.h>
     41 #include <sys/errno.h>
     42 
     43 #include <sys/bus.h>
     44 #include <dev/clock_subr.h>
     45 #include <dev/ic/mk48txxreg.h>
     46 #include <dev/ic/mk48txxvar.h>
     47 
     48 int mk48txx_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
     49 int mk48txx_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
     50 uint8_t mk48txx_def_nvrd(struct mk48txx_softc *, int);
     51 void mk48txx_def_nvwr(struct mk48txx_softc *, int, uint8_t);
     52 
     53 const struct {
     54 	const char *name;
     55 	bus_size_t nvramsz;
     56 	bus_size_t clkoff;
     57 	int flags;
     58 #define MK48TXX_EXT_REGISTERS	1	/* Has extended register set */
     59 } mk48txx_models[] = {
     60 	{ "mk48t02", MK48T02_CLKSZ, MK48T02_CLKOFF, 0 },
     61 	{ "mk48t08", MK48T08_CLKSZ, MK48T08_CLKOFF, 0 },
     62 	{ "mk48t18", MK48T18_CLKSZ, MK48T18_CLKOFF, 0 },
     63 	{ "mk48t59", MK48T59_CLKSZ, MK48T59_CLKOFF, MK48TXX_EXT_REGISTERS },
     64 	{ "ds1553", DS1553_CLKSZ, DS1553_CLKOFF, MK48TXX_EXT_REGISTERS },
     65 };
     66 
     67 void
     68 mk48txx_attach(struct mk48txx_softc *sc)
     69 {
     70 	todr_chip_handle_t handle;
     71 	int i;
     72 
     73 	aprint_normal(": %s", sc->sc_model);
     74 
     75 	i = __arraycount(mk48txx_models);
     76 	while (--i >= 0) {
     77 		if (strcmp(sc->sc_model, mk48txx_models[i].name) == 0)
     78 			break;
     79 	}
     80 	if (i < 0)
     81 		panic("%s: unsupported model", __func__);
     82 
     83 	sc->sc_nvramsz = mk48txx_models[i].nvramsz;
     84 	sc->sc_clkoffset = mk48txx_models[i].clkoff;
     85 
     86 	handle = &sc->sc_handle;
     87 	handle->cookie = sc;
     88 	handle->todr_gettime = NULL;
     89 	handle->todr_settime = NULL;
     90 	handle->todr_gettime_ymdhms = mk48txx_gettime_ymdhms;
     91 	handle->todr_settime_ymdhms = mk48txx_settime_ymdhms;
     92 
     93 	if (sc->sc_nvrd == NULL)
     94 		sc->sc_nvrd = mk48txx_def_nvrd;
     95 	if (sc->sc_nvwr == NULL)
     96 		sc->sc_nvwr = mk48txx_def_nvwr;
     97 
     98 	todr_attach(handle);
     99 }
    100 
    101 /*
    102  * Get time-of-day and convert to a `struct timeval'
    103  * Return 0 on success; an error number otherwise.
    104  */
    105 int
    106 mk48txx_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
    107 {
    108 	struct mk48txx_softc *sc;
    109 	bus_size_t clkoff;
    110 	int year;
    111 	uint8_t csr;
    112 
    113 	sc = handle->cookie;
    114 	clkoff = sc->sc_clkoffset;
    115 
    116 	todr_wenable(handle, 1);
    117 
    118 	/* enable read (stop time) */
    119 	csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
    120 	csr |= MK48TXX_CSR_READ;
    121 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
    122 
    123 	dt->dt_sec = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_ISEC));
    124 	dt->dt_min = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IMIN));
    125 	dt->dt_hour = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IHOUR));
    126 	dt->dt_day = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IDAY));
    127 	dt->dt_wday = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IWDAY));
    128 	dt->dt_mon = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IMON));
    129 	year = FROMBCD((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IYEAR));
    130 
    131 	if (sc->sc_flag & MK48TXX_HAVE_CENT_REG) {
    132 		year += 100*FROMBCD(csr & MK48TXX_CSR_CENT_MASK);
    133 	} else {
    134 		year += sc->sc_year0;
    135 		if (year < POSIX_BASE_YEAR &&
    136 		    (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) == 0)
    137 			year += 100;
    138 	}
    139 
    140 	dt->dt_year = year;
    141 
    142 	/* time wears on */
    143 	csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
    144 	csr &= ~MK48TXX_CSR_READ;
    145 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
    146 	todr_wenable(handle, 0);
    147 
    148 	return 0;
    149 }
    150 
    151 /*
    152  * Set the time-of-day clock based on the value of the `struct timeval' arg.
    153  * Return 0 on success; an error number otherwise.
    154  */
    155 int
    156 mk48txx_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
    157 {
    158 	struct mk48txx_softc *sc;
    159 	bus_size_t clkoff;
    160 	uint8_t csr;
    161 	int year;
    162 	int cent;
    163 
    164 	sc = handle->cookie;
    165 	clkoff = sc->sc_clkoffset;
    166 
    167 	if ((sc->sc_flag & MK48TXX_HAVE_CENT_REG) == 0) {
    168 		cent = 0;
    169 		year = dt->dt_year - sc->sc_year0;
    170 		if (year > 99 &&
    171 		    (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) == 0)
    172 			year -= 100;
    173 	} else {
    174 		cent = dt->dt_year / 100;
    175 		year = dt->dt_year % 100;
    176 	}
    177 
    178 	todr_wenable(handle, 1);
    179 	/* enable write */
    180 	csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
    181 	csr |= MK48TXX_CSR_WRITE;
    182 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
    183 
    184 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_ISEC, TOBCD(dt->dt_sec));
    185 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_IMIN, TOBCD(dt->dt_min));
    186 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_IHOUR, TOBCD(dt->dt_hour));
    187 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_IWDAY, TOBCD(dt->dt_wday));
    188 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_IDAY, TOBCD(dt->dt_day));
    189 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_IMON, TOBCD(dt->dt_mon));
    190 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_IYEAR, TOBCD(year));
    191 
    192 	/*
    193 	 * If we have a century register and the century has changed
    194 	 * update it.
    195 	 */
    196 	if ((sc->sc_flag & MK48TXX_HAVE_CENT_REG)
    197 	    && (csr & MK48TXX_CSR_CENT_MASK) != TOBCD(cent)) {
    198 		csr &= ~MK48TXX_CSR_CENT_MASK;
    199 		csr |= MK48TXX_CSR_CENT_MASK & TOBCD(cent);
    200 		(*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
    201 	}
    202 
    203 	/* load them up */
    204 	csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR);
    205 	csr &= ~MK48TXX_CSR_WRITE;
    206 	(*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr);
    207 	todr_wenable(handle, 0);
    208 	return 0;
    209 }
    210 
    211 int
    212 mk48txx_get_nvram_size(todr_chip_handle_t handle, bus_size_t *vp)
    213 {
    214 	struct mk48txx_softc *sc;
    215 
    216 	sc = handle->cookie;
    217 	*vp = sc->sc_nvramsz;
    218 	return 0;
    219 }
    220 
    221 uint8_t
    222 mk48txx_def_nvrd(struct mk48txx_softc *sc, int off)
    223 {
    224 
    225 	return bus_space_read_1(sc->sc_bst, sc->sc_bsh, off);
    226 }
    227 
    228 void
    229 mk48txx_def_nvwr(struct mk48txx_softc *sc, int off, uint8_t v)
    230 {
    231 
    232 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, v);
    233 }
    234