Home | History | Annotate | Line # | Download | only in dev
timekeeper.c revision 1.2
      1  1.2   thorpej /* $NetBSD: timekeeper.c,v 1.2 2002/10/02 05:31:47 thorpej 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.2   thorpej __KERNEL_RCSID(0, "$NetBSD: timekeeper.c,v 1.2 2002/10/02 05:31:47 thorpej 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 /*
     65  1.1  nisimura  * BCD to decimal and decimal to BCD.
     66  1.1  nisimura  */
     67  1.1  nisimura #define FROMBCD(x)      (((x) >> 4) * 10 + ((x) & 0xf))
     68  1.1  nisimura #define TOBCD(x)        (((x) / 10 * 16) + ((x) % 10))
     69  1.1  nisimura 
     70  1.1  nisimura static int  clock_match __P((struct device *, struct cfdata *, void *));
     71  1.1  nisimura static void clock_attach __P((struct device *, struct device *, void *));
     72  1.1  nisimura 
     73  1.2   thorpej CFATTACH_DECL(clock, sizeof (struct timekeeper_softc),
     74  1.2   thorpej     clock_match, clock_attach, NULL, NULL);
     75  1.1  nisimura extern struct cfdriver clock_cd;
     76  1.1  nisimura 
     77  1.1  nisimura static void mkclock_get __P((struct device *, time_t, struct clock_ymdhms *));
     78  1.1  nisimura static void mkclock_set __P((struct device *, struct clock_ymdhms *));
     79  1.1  nisimura static void dsclock_get __P((struct device *, time_t, struct clock_ymdhms *));
     80  1.1  nisimura static void dsclock_set __P((struct device *, struct clock_ymdhms *));
     81  1.1  nisimura 
     82  1.1  nisimura static struct clockfns mkclock_clockfns = {
     83  1.1  nisimura 	NULL /* never used */, mkclock_get, mkclock_set,
     84  1.1  nisimura };
     85  1.1  nisimura 
     86  1.1  nisimura static struct clockfns dsclock_clockfns = {
     87  1.1  nisimura 	NULL /* never used */, dsclock_get, dsclock_set,
     88  1.1  nisimura };
     89  1.1  nisimura 
     90  1.1  nisimura static int
     91  1.1  nisimura clock_match(parent, match, aux)
     92  1.1  nisimura         struct device *parent;
     93  1.1  nisimura         struct cfdata *match;
     94  1.1  nisimura         void *aux;
     95  1.1  nisimura {
     96  1.1  nisimura 	struct mainbus_attach_args *ma = aux;
     97  1.1  nisimura 
     98  1.1  nisimura 	if (strcmp(ma->ma_name, clock_cd.cd_name))
     99  1.1  nisimura 		return 0;
    100  1.1  nisimura 	return 1;
    101  1.1  nisimura }
    102  1.1  nisimura 
    103  1.1  nisimura static void
    104  1.1  nisimura clock_attach(parent, self, aux)
    105  1.1  nisimura         struct device *parent, *self;
    106  1.1  nisimura         void *aux;
    107  1.1  nisimura {
    108  1.1  nisimura 	struct timekeeper_softc *sc = (void *)self;
    109  1.1  nisimura 	struct mainbus_attach_args *ma = aux;
    110  1.1  nisimura 	struct clockfns *clockwork;
    111  1.1  nisimura 
    112  1.1  nisimura 	switch (machtype) {
    113  1.1  nisimura 	default:
    114  1.1  nisimura 	case LUNA_I:	/* Mostek MK48T02 */
    115  1.1  nisimura 		sc->sc_clock = (void *)(ma->ma_addr + 2040);
    116  1.1  nisimura 		sc->sc_nvram = (void *)ma->ma_addr;
    117  1.1  nisimura 		sc->sc_nvramsize = 2040;
    118  1.1  nisimura 		clockwork = &mkclock_clockfns;
    119  1.1  nisimura 		printf(": mk48t02\n");
    120  1.1  nisimura 		break;
    121  1.1  nisimura 	case LUNA_II: /* Dallas DS1287A */
    122  1.1  nisimura 		sc->sc_clock = (void *)ma->ma_addr;
    123  1.1  nisimura 		sc->sc_nvram = (void *)(ma->ma_addr + 50);
    124  1.1  nisimura 		sc->sc_nvramsize = 50;
    125  1.1  nisimura 		clockwork = &dsclock_clockfns;
    126  1.1  nisimura 		printf(": ds1287a\n");
    127  1.1  nisimura 		break;
    128  1.1  nisimura 	}
    129  1.1  nisimura 	clockattach(&sc->sc_dev, clockwork);
    130  1.1  nisimura 	memcpy(sc->sc_image, sc->sc_nvram, sc->sc_nvramsize);
    131  1.1  nisimura }
    132  1.1  nisimura 
    133  1.1  nisimura /*
    134  1.1  nisimura  * Get the time of day, based on the clock's value and/or the base value.
    135  1.1  nisimura  */
    136  1.1  nisimura static void
    137  1.1  nisimura mkclock_get(dev, base, dt)
    138  1.1  nisimura 	struct device *dev;
    139  1.1  nisimura 	time_t base;
    140  1.1  nisimura 	struct clock_ymdhms *dt;
    141  1.1  nisimura {
    142  1.1  nisimura 	struct timekeeper_softc *sc = (void *)dev;
    143  1.1  nisimura 	volatile u_int8_t *chiptime = (void *)sc->sc_clock;
    144  1.1  nisimura 	int s;
    145  1.1  nisimura 
    146  1.1  nisimura 	s = splclock();
    147  1.1  nisimura 	chiptime[MK_CSR] |= MK_CSR_READ;	/* enable read (stop time) */
    148  1.1  nisimura 	dt->dt_sec = FROMBCD(chiptime[MK_SEC]);
    149  1.1  nisimura 	dt->dt_min = FROMBCD(chiptime[MK_MIN]);
    150  1.1  nisimura 	dt->dt_hour = FROMBCD(chiptime[MK_HOUR]);
    151  1.1  nisimura 	dt->dt_wday = FROMBCD(chiptime[MK_DOW]);
    152  1.1  nisimura 	dt->dt_day = FROMBCD(chiptime[MK_DOM]);
    153  1.1  nisimura 	dt->dt_mon = FROMBCD(chiptime[MK_MONTH]);
    154  1.1  nisimura 	dt->dt_year = FROMBCD(chiptime[MK_YEAR]) + YEAR0;
    155  1.1  nisimura 	chiptime[MK_CSR] &= ~MK_CSR_READ;	/* time wears on */
    156  1.1  nisimura 	splx(s);
    157  1.1  nisimura #ifdef TIMEKEEPER_DEBUG
    158  1.1  nisimura 	printf("get %d/%d/%d %d:%d:%d\n",
    159  1.1  nisimura 	dt->dt_year, dt->dt_mon, dt->dt_day,
    160  1.1  nisimura 	dt->dt_hour, dt->dt_min, dt->dt_sec);
    161  1.1  nisimura #endif
    162  1.1  nisimura }
    163  1.1  nisimura 
    164  1.1  nisimura /*
    165  1.1  nisimura  * Reset the TODR based on the time value.
    166  1.1  nisimura  */
    167  1.1  nisimura static void
    168  1.1  nisimura mkclock_set(dev, dt)
    169  1.1  nisimura 	struct device *dev;
    170  1.1  nisimura 	struct clock_ymdhms *dt;
    171  1.1  nisimura {
    172  1.1  nisimura 	struct timekeeper_softc *sc = (void *)dev;
    173  1.1  nisimura 	volatile u_int8_t *chiptime = (void *)sc->sc_clock;
    174  1.1  nisimura 	volatile u_int8_t *stamp = (u_int8_t *)sc->sc_nvram + 0x10;
    175  1.1  nisimura 	int s;
    176  1.1  nisimura 
    177  1.1  nisimura 	s = splclock();
    178  1.1  nisimura 	chiptime[MK_CSR] |= MK_CSR_WRITE;	/* enable write */
    179  1.1  nisimura 	chiptime[MK_SEC] = TOBCD(dt->dt_sec);
    180  1.1  nisimura 	chiptime[MK_MIN] = TOBCD(dt->dt_min);
    181  1.1  nisimura 	chiptime[MK_HOUR] = TOBCD(dt->dt_hour);
    182  1.1  nisimura 	chiptime[MK_DOW] = TOBCD(dt->dt_wday);
    183  1.1  nisimura 	chiptime[MK_DOM] = TOBCD(dt->dt_day);
    184  1.1  nisimura 	chiptime[MK_MONTH] = TOBCD(dt->dt_mon);
    185  1.1  nisimura 	chiptime[MK_YEAR] = TOBCD(dt->dt_year - YEAR0);
    186  1.1  nisimura 	chiptime[MK_CSR] &= ~MK_CSR_WRITE;	/* load them up */
    187  1.1  nisimura 	splx(s);
    188  1.1  nisimura #ifdef TIMEKEEPER_DEBUG
    189  1.1  nisimura 	printf("set %d/%d/%d %d:%d:%d\n",
    190  1.1  nisimura 	dt->dt_year, dt->dt_mon, dt->dt_day,
    191  1.1  nisimura 	dt->dt_hour, dt->dt_min, dt->dt_sec);
    192  1.1  nisimura #endif
    193  1.1  nisimura 
    194  1.1  nisimura 	stamp[0] = 'R'; stamp[1] = 'T'; stamp[2] = 'C'; stamp[3] = '\0';
    195  1.1  nisimura }
    196  1.1  nisimura 
    197  1.1  nisimura /*
    198  1.1  nisimura  * Get the time of day, based on the clock's value and/or the base value.
    199  1.1  nisimura  */
    200  1.1  nisimura static void
    201  1.1  nisimura dsclock_get(dev, base, dt)
    202  1.1  nisimura 	struct device *dev;
    203  1.1  nisimura 	time_t base;
    204  1.1  nisimura 	struct clock_ymdhms *dt;
    205  1.1  nisimura {
    206  1.1  nisimura 	struct timekeeper_softc *sc = (void *)dev;
    207  1.1  nisimura 	volatile u_int8_t *chiptime = (void *)sc->sc_clock;
    208  1.1  nisimura 	int s;
    209  1.1  nisimura 
    210  1.1  nisimura 	s = splclock();
    211  1.1  nisimura 	/* update in progress; spin loop */
    212  1.1  nisimura 	while (chiptime[MC_REGA] & MC_REGA_UIP)
    213  1.1  nisimura 		;
    214  1.1  nisimura 	dt->dt_sec = chiptime[MC_SEC];
    215  1.1  nisimura 	dt->dt_min = chiptime[MC_MIN];
    216  1.1  nisimura 	dt->dt_hour = chiptime[MC_HOUR];
    217  1.1  nisimura 	dt->dt_wday = chiptime[MC_DOW];
    218  1.1  nisimura 	dt->dt_day = chiptime[MC_DOM];
    219  1.1  nisimura 	dt->dt_mon = chiptime[MC_MONTH];
    220  1.1  nisimura 	dt->dt_year = chiptime[MC_YEAR] + YEAR0;
    221  1.1  nisimura 	splx(s);
    222  1.1  nisimura }
    223  1.1  nisimura 
    224  1.1  nisimura /*
    225  1.1  nisimura  * Reset the TODR based on the time value.
    226  1.1  nisimura  */
    227  1.1  nisimura static void
    228  1.1  nisimura dsclock_set(dev, dt)
    229  1.1  nisimura 	struct device *dev;
    230  1.1  nisimura 	struct clock_ymdhms *dt;
    231  1.1  nisimura {
    232  1.1  nisimura 	struct timekeeper_softc *sc = (void *)dev;
    233  1.1  nisimura 	volatile u_int8_t *chiptime = (void *)sc->sc_clock;
    234  1.1  nisimura 	int s;
    235  1.1  nisimura 
    236  1.1  nisimura 	s = splclock();
    237  1.1  nisimura 	chiptime[MC_REGB] |= MC_REGB_SET;	/* enable write */
    238  1.1  nisimura 	chiptime[MC_SEC] = dt->dt_sec;
    239  1.1  nisimura 	chiptime[MC_MIN] = dt->dt_min;
    240  1.1  nisimura 	chiptime[MC_HOUR] = dt->dt_hour;
    241  1.1  nisimura 	chiptime[MC_DOW] = dt->dt_wday;
    242  1.1  nisimura 	chiptime[MC_DOM] = dt->dt_day;
    243  1.1  nisimura 	chiptime[MC_MONTH] = dt->dt_mon;
    244  1.1  nisimura 	chiptime[MC_YEAR] = dt->dt_year - YEAR0;
    245  1.1  nisimura 	chiptime[MC_REGB] &= ~MC_REGB_SET;	/* load them up */
    246  1.1  nisimura 	splx(s);
    247  1.1  nisimura }
    248