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