Home | History | Annotate | Line # | Download | only in dev
timekeeper.c revision 1.3
      1  1.3        he /* $NetBSD: timekeeper.c,v 1.3 2006/03/12 22:55:19 he Exp $ */
      2  1.1  nisimura 
      3  1.1  nisimura /*-
      4  1.1  nisimura  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.1  nisimura  * All rights reserved.
      6  1.1  nisimura  *
      7  1.1  nisimura  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  nisimura  * by Tohru Nishimura.
      9  1.1  nisimura  *
     10  1.1  nisimura  * Redistribution and use in source and binary forms, with or without
     11  1.1  nisimura  * modification, are permitted provided that the following conditions
     12  1.1  nisimura  * are met:
     13  1.1  nisimura  * 1. Redistributions of source code must retain the above copyright
     14  1.1  nisimura  *    notice, this list of conditions and the following disclaimer.
     15  1.1  nisimura  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  nisimura  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  nisimura  *    documentation and/or other materials provided with the distribution.
     18  1.1  nisimura  * 3. All advertising materials mentioning features or use of this software
     19  1.1  nisimura  *    must display the following acknowledgement:
     20  1.1  nisimura  *	This product includes software developed by the NetBSD
     21  1.1  nisimura  *	Foundation, Inc. and its contributors.
     22  1.1  nisimura  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  nisimura  *    contributors may be used to endorse or promote products derived
     24  1.1  nisimura  *    from this software without specific prior written permission.
     25  1.1  nisimura  *
     26  1.1  nisimura  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  nisimura  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  nisimura  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  nisimura  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  nisimura  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  nisimura  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  nisimura  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  nisimura  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  nisimura  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  nisimura  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  nisimura  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  nisimura  */
     38  1.1  nisimura 
     39  1.1  nisimura #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     40  1.1  nisimura 
     41  1.3        he __KERNEL_RCSID(0, "$NetBSD: timekeeper.c,v 1.3 2006/03/12 22:55:19 he Exp $");
     42  1.1  nisimura 
     43  1.1  nisimura #include <sys/param.h>
     44  1.1  nisimura #include <sys/systm.h>
     45  1.1  nisimura #include <sys/device.h>
     46  1.1  nisimura #include <sys/kernel.h>
     47  1.1  nisimura 
     48  1.1  nisimura #include <machine/cpu.h>
     49  1.1  nisimura 
     50  1.1  nisimura #include <dev/clock_subr.h>
     51  1.1  nisimura #include <luna68k/luna68k/clockvar.h>
     52  1.1  nisimura #include <luna68k/dev/timekeeper.h>
     53  1.1  nisimura #include <machine/autoconf.h>
     54  1.1  nisimura 
     55  1.1  nisimura #define	YEAR0	1970	/* year offset */
     56  1.1  nisimura 
     57  1.1  nisimura struct timekeeper_softc {
     58  1.1  nisimura 	struct device sc_dev;
     59  1.1  nisimura 	void *sc_clock, *sc_nvram;
     60  1.1  nisimura 	int sc_nvramsize;
     61  1.1  nisimura 	u_int8_t sc_image[2040];
     62  1.1  nisimura };
     63  1.1  nisimura 
     64  1.1  nisimura static int  clock_match __P((struct device *, struct cfdata *, void *));
     65  1.1  nisimura static void clock_attach __P((struct device *, struct device *, void *));
     66  1.1  nisimura 
     67  1.2   thorpej CFATTACH_DECL(clock, sizeof (struct timekeeper_softc),
     68  1.2   thorpej     clock_match, clock_attach, NULL, NULL);
     69  1.1  nisimura extern struct cfdriver clock_cd;
     70  1.1  nisimura 
     71  1.1  nisimura static void mkclock_get __P((struct device *, time_t, struct clock_ymdhms *));
     72  1.1  nisimura static void mkclock_set __P((struct device *, struct clock_ymdhms *));
     73  1.1  nisimura static void dsclock_get __P((struct device *, time_t, struct clock_ymdhms *));
     74  1.1  nisimura static void dsclock_set __P((struct device *, struct clock_ymdhms *));
     75  1.1  nisimura 
     76  1.1  nisimura static struct clockfns mkclock_clockfns = {
     77  1.1  nisimura 	NULL /* never used */, mkclock_get, mkclock_set,
     78  1.1  nisimura };
     79  1.1  nisimura 
     80  1.1  nisimura static struct clockfns dsclock_clockfns = {
     81  1.1  nisimura 	NULL /* never used */, dsclock_get, dsclock_set,
     82  1.1  nisimura };
     83  1.1  nisimura 
     84  1.1  nisimura static int
     85  1.1  nisimura clock_match(parent, match, aux)
     86  1.1  nisimura         struct device *parent;
     87  1.1  nisimura         struct cfdata *match;
     88  1.1  nisimura         void *aux;
     89  1.1  nisimura {
     90  1.1  nisimura 	struct mainbus_attach_args *ma = aux;
     91  1.1  nisimura 
     92  1.1  nisimura 	if (strcmp(ma->ma_name, clock_cd.cd_name))
     93  1.1  nisimura 		return 0;
     94  1.1  nisimura 	return 1;
     95  1.1  nisimura }
     96  1.1  nisimura 
     97  1.1  nisimura static void
     98  1.1  nisimura clock_attach(parent, self, aux)
     99  1.1  nisimura         struct device *parent, *self;
    100  1.1  nisimura         void *aux;
    101  1.1  nisimura {
    102  1.1  nisimura 	struct timekeeper_softc *sc = (void *)self;
    103  1.1  nisimura 	struct mainbus_attach_args *ma = aux;
    104  1.1  nisimura 	struct clockfns *clockwork;
    105  1.1  nisimura 
    106  1.1  nisimura 	switch (machtype) {
    107  1.1  nisimura 	default:
    108  1.1  nisimura 	case LUNA_I:	/* Mostek MK48T02 */
    109  1.1  nisimura 		sc->sc_clock = (void *)(ma->ma_addr + 2040);
    110  1.1  nisimura 		sc->sc_nvram = (void *)ma->ma_addr;
    111  1.1  nisimura 		sc->sc_nvramsize = 2040;
    112  1.1  nisimura 		clockwork = &mkclock_clockfns;
    113  1.1  nisimura 		printf(": mk48t02\n");
    114  1.1  nisimura 		break;
    115  1.1  nisimura 	case LUNA_II: /* Dallas DS1287A */
    116  1.1  nisimura 		sc->sc_clock = (void *)ma->ma_addr;
    117  1.1  nisimura 		sc->sc_nvram = (void *)(ma->ma_addr + 50);
    118  1.1  nisimura 		sc->sc_nvramsize = 50;
    119  1.1  nisimura 		clockwork = &dsclock_clockfns;
    120  1.1  nisimura 		printf(": ds1287a\n");
    121  1.1  nisimura 		break;
    122  1.1  nisimura 	}
    123  1.1  nisimura 	clockattach(&sc->sc_dev, clockwork);
    124  1.1  nisimura 	memcpy(sc->sc_image, sc->sc_nvram, sc->sc_nvramsize);
    125  1.1  nisimura }
    126  1.1  nisimura 
    127  1.1  nisimura /*
    128  1.1  nisimura  * Get the time of day, based on the clock's value and/or the base value.
    129  1.1  nisimura  */
    130  1.1  nisimura static void
    131  1.1  nisimura mkclock_get(dev, base, dt)
    132  1.1  nisimura 	struct device *dev;
    133  1.1  nisimura 	time_t base;
    134  1.1  nisimura 	struct clock_ymdhms *dt;
    135  1.1  nisimura {
    136  1.1  nisimura 	struct timekeeper_softc *sc = (void *)dev;
    137  1.1  nisimura 	volatile u_int8_t *chiptime = (void *)sc->sc_clock;
    138  1.1  nisimura 	int s;
    139  1.1  nisimura 
    140  1.1  nisimura 	s = splclock();
    141  1.1  nisimura 	chiptime[MK_CSR] |= MK_CSR_READ;	/* enable read (stop time) */
    142  1.1  nisimura 	dt->dt_sec = FROMBCD(chiptime[MK_SEC]);
    143  1.1  nisimura 	dt->dt_min = FROMBCD(chiptime[MK_MIN]);
    144  1.1  nisimura 	dt->dt_hour = FROMBCD(chiptime[MK_HOUR]);
    145  1.1  nisimura 	dt->dt_wday = FROMBCD(chiptime[MK_DOW]);
    146  1.1  nisimura 	dt->dt_day = FROMBCD(chiptime[MK_DOM]);
    147  1.1  nisimura 	dt->dt_mon = FROMBCD(chiptime[MK_MONTH]);
    148  1.1  nisimura 	dt->dt_year = FROMBCD(chiptime[MK_YEAR]) + YEAR0;
    149  1.1  nisimura 	chiptime[MK_CSR] &= ~MK_CSR_READ;	/* time wears on */
    150  1.1  nisimura 	splx(s);
    151  1.1  nisimura #ifdef TIMEKEEPER_DEBUG
    152  1.1  nisimura 	printf("get %d/%d/%d %d:%d:%d\n",
    153  1.1  nisimura 	dt->dt_year, dt->dt_mon, dt->dt_day,
    154  1.1  nisimura 	dt->dt_hour, dt->dt_min, dt->dt_sec);
    155  1.1  nisimura #endif
    156  1.1  nisimura }
    157  1.1  nisimura 
    158  1.1  nisimura /*
    159  1.1  nisimura  * Reset the TODR based on the time value.
    160  1.1  nisimura  */
    161  1.1  nisimura static void
    162  1.1  nisimura mkclock_set(dev, dt)
    163  1.1  nisimura 	struct device *dev;
    164  1.1  nisimura 	struct clock_ymdhms *dt;
    165  1.1  nisimura {
    166  1.1  nisimura 	struct timekeeper_softc *sc = (void *)dev;
    167  1.1  nisimura 	volatile u_int8_t *chiptime = (void *)sc->sc_clock;
    168  1.1  nisimura 	volatile u_int8_t *stamp = (u_int8_t *)sc->sc_nvram + 0x10;
    169  1.1  nisimura 	int s;
    170  1.1  nisimura 
    171  1.1  nisimura 	s = splclock();
    172  1.1  nisimura 	chiptime[MK_CSR] |= MK_CSR_WRITE;	/* enable write */
    173  1.1  nisimura 	chiptime[MK_SEC] = TOBCD(dt->dt_sec);
    174  1.1  nisimura 	chiptime[MK_MIN] = TOBCD(dt->dt_min);
    175  1.1  nisimura 	chiptime[MK_HOUR] = TOBCD(dt->dt_hour);
    176  1.1  nisimura 	chiptime[MK_DOW] = TOBCD(dt->dt_wday);
    177  1.1  nisimura 	chiptime[MK_DOM] = TOBCD(dt->dt_day);
    178  1.1  nisimura 	chiptime[MK_MONTH] = TOBCD(dt->dt_mon);
    179  1.1  nisimura 	chiptime[MK_YEAR] = TOBCD(dt->dt_year - YEAR0);
    180  1.1  nisimura 	chiptime[MK_CSR] &= ~MK_CSR_WRITE;	/* load them up */
    181  1.1  nisimura 	splx(s);
    182  1.1  nisimura #ifdef TIMEKEEPER_DEBUG
    183  1.1  nisimura 	printf("set %d/%d/%d %d:%d:%d\n",
    184  1.1  nisimura 	dt->dt_year, dt->dt_mon, dt->dt_day,
    185  1.1  nisimura 	dt->dt_hour, dt->dt_min, dt->dt_sec);
    186  1.1  nisimura #endif
    187  1.1  nisimura 
    188  1.1  nisimura 	stamp[0] = 'R'; stamp[1] = 'T'; stamp[2] = 'C'; stamp[3] = '\0';
    189  1.1  nisimura }
    190  1.1  nisimura 
    191  1.1  nisimura /*
    192  1.1  nisimura  * Get the time of day, based on the clock's value and/or the base value.
    193  1.1  nisimura  */
    194  1.1  nisimura static void
    195  1.1  nisimura dsclock_get(dev, base, dt)
    196  1.1  nisimura 	struct device *dev;
    197  1.1  nisimura 	time_t base;
    198  1.1  nisimura 	struct clock_ymdhms *dt;
    199  1.1  nisimura {
    200  1.1  nisimura 	struct timekeeper_softc *sc = (void *)dev;
    201  1.1  nisimura 	volatile u_int8_t *chiptime = (void *)sc->sc_clock;
    202  1.1  nisimura 	int s;
    203  1.1  nisimura 
    204  1.1  nisimura 	s = splclock();
    205  1.1  nisimura 	/* update in progress; spin loop */
    206  1.1  nisimura 	while (chiptime[MC_REGA] & MC_REGA_UIP)
    207  1.1  nisimura 		;
    208  1.1  nisimura 	dt->dt_sec = chiptime[MC_SEC];
    209  1.1  nisimura 	dt->dt_min = chiptime[MC_MIN];
    210  1.1  nisimura 	dt->dt_hour = chiptime[MC_HOUR];
    211  1.1  nisimura 	dt->dt_wday = chiptime[MC_DOW];
    212  1.1  nisimura 	dt->dt_day = chiptime[MC_DOM];
    213  1.1  nisimura 	dt->dt_mon = chiptime[MC_MONTH];
    214  1.1  nisimura 	dt->dt_year = chiptime[MC_YEAR] + YEAR0;
    215  1.1  nisimura 	splx(s);
    216  1.1  nisimura }
    217  1.1  nisimura 
    218  1.1  nisimura /*
    219  1.1  nisimura  * Reset the TODR based on the time value.
    220  1.1  nisimura  */
    221  1.1  nisimura static void
    222  1.1  nisimura dsclock_set(dev, dt)
    223  1.1  nisimura 	struct device *dev;
    224  1.1  nisimura 	struct clock_ymdhms *dt;
    225  1.1  nisimura {
    226  1.1  nisimura 	struct timekeeper_softc *sc = (void *)dev;
    227  1.1  nisimura 	volatile u_int8_t *chiptime = (void *)sc->sc_clock;
    228  1.1  nisimura 	int s;
    229  1.1  nisimura 
    230  1.1  nisimura 	s = splclock();
    231  1.1  nisimura 	chiptime[MC_REGB] |= MC_REGB_SET;	/* enable write */
    232  1.1  nisimura 	chiptime[MC_SEC] = dt->dt_sec;
    233  1.1  nisimura 	chiptime[MC_MIN] = dt->dt_min;
    234  1.1  nisimura 	chiptime[MC_HOUR] = dt->dt_hour;
    235  1.1  nisimura 	chiptime[MC_DOW] = dt->dt_wday;
    236  1.1  nisimura 	chiptime[MC_DOM] = dt->dt_day;
    237  1.1  nisimura 	chiptime[MC_MONTH] = dt->dt_mon;
    238  1.1  nisimura 	chiptime[MC_YEAR] = dt->dt_year - YEAR0;
    239  1.1  nisimura 	chiptime[MC_REGB] &= ~MC_REGB_SET;	/* load them up */
    240  1.1  nisimura 	splx(s);
    241  1.1  nisimura }
    242