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