Home | History | Annotate | Line # | Download | only in dev
timekeeper.c revision 1.10.12.3
      1  1.10.12.2       tls /* $NetBSD: timekeeper.c,v 1.10.12.3 2017/12/03 11:36:23 jdolecek 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  *
     19        1.1  nisimura  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1  nisimura  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1  nisimura  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1  nisimura  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1  nisimura  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1  nisimura  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1  nisimura  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1  nisimura  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1  nisimura  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1  nisimura  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1  nisimura  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1  nisimura  */
     31        1.1  nisimura 
     32        1.1  nisimura #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     33        1.1  nisimura 
     34  1.10.12.2       tls __KERNEL_RCSID(0, "$NetBSD: timekeeper.c,v 1.10.12.3 2017/12/03 11:36:23 jdolecek Exp $");
     35        1.1  nisimura 
     36        1.1  nisimura #include <sys/param.h>
     37        1.1  nisimura #include <sys/systm.h>
     38        1.1  nisimura #include <sys/device.h>
     39        1.1  nisimura #include <sys/kernel.h>
     40        1.1  nisimura 
     41        1.1  nisimura #include <machine/cpu.h>
     42        1.1  nisimura 
     43        1.1  nisimura #include <dev/clock_subr.h>
     44        1.1  nisimura #include <luna68k/dev/timekeeper.h>
     45        1.1  nisimura #include <machine/autoconf.h>
     46        1.1  nisimura 
     47        1.9   tsutsui #include "ioconf.h"
     48        1.9   tsutsui 
     49        1.1  nisimura #define	YEAR0	1970	/* year offset */
     50        1.1  nisimura 
     51        1.1  nisimura struct timekeeper_softc {
     52        1.9   tsutsui 	device_t sc_dev;
     53        1.1  nisimura 	void *sc_clock, *sc_nvram;
     54        1.1  nisimura 	int sc_nvramsize;
     55       1.10   tsutsui 	uint8_t sc_image[2040];
     56        1.4   gdamore 	struct todr_chip_handle sc_todr;
     57        1.1  nisimura };
     58        1.1  nisimura 
     59        1.9   tsutsui static int  clock_match(device_t, cfdata_t, void *);
     60        1.9   tsutsui static void clock_attach(device_t, device_t, void *);
     61  1.10.12.2       tls static void dsclock_init(struct timekeeper_softc *);
     62        1.1  nisimura 
     63        1.9   tsutsui CFATTACH_DECL_NEW(clock, sizeof (struct timekeeper_softc),
     64        1.2   thorpej     clock_match, clock_attach, NULL, NULL);
     65        1.1  nisimura 
     66        1.6       dsl static int mkclock_get(todr_chip_handle_t, struct clock_ymdhms *);
     67        1.6       dsl static int mkclock_set(todr_chip_handle_t, struct clock_ymdhms *);
     68        1.6       dsl static int dsclock_get(todr_chip_handle_t, struct clock_ymdhms *);
     69        1.6       dsl static int dsclock_set(todr_chip_handle_t, struct clock_ymdhms *);
     70        1.1  nisimura 
     71        1.1  nisimura static int
     72        1.9   tsutsui clock_match(device_t parent, cfdata_t cf, void *aux)
     73        1.1  nisimura {
     74        1.1  nisimura 	struct mainbus_attach_args *ma = aux;
     75        1.1  nisimura 
     76        1.1  nisimura 	if (strcmp(ma->ma_name, clock_cd.cd_name))
     77        1.1  nisimura 		return 0;
     78        1.1  nisimura 	return 1;
     79        1.1  nisimura }
     80        1.1  nisimura 
     81        1.1  nisimura static void
     82        1.9   tsutsui clock_attach(device_t parent, device_t self, void *aux)
     83        1.1  nisimura {
     84        1.9   tsutsui 	struct timekeeper_softc *sc = device_private(self);
     85        1.1  nisimura 	struct mainbus_attach_args *ma = aux;
     86        1.1  nisimura 
     87        1.9   tsutsui 	sc->sc_dev = self;
     88        1.9   tsutsui 
     89        1.1  nisimura 	switch (machtype) {
     90        1.1  nisimura 	default:
     91        1.1  nisimura 	case LUNA_I:	/* Mostek MK48T02 */
     92        1.1  nisimura 		sc->sc_clock = (void *)(ma->ma_addr + 2040);
     93        1.1  nisimura 		sc->sc_nvram = (void *)ma->ma_addr;
     94        1.1  nisimura 		sc->sc_nvramsize = 2040;
     95        1.4   gdamore 		sc->sc_todr.todr_gettime_ymdhms = mkclock_get;
     96        1.4   gdamore 		sc->sc_todr.todr_settime_ymdhms = mkclock_set;
     97  1.10.12.1       tls 		sc->sc_todr.cookie = sc;
     98        1.9   tsutsui 		aprint_normal(": mk48t02\n");
     99        1.1  nisimura 		break;
    100        1.1  nisimura 	case LUNA_II: /* Dallas DS1287A */
    101        1.1  nisimura 		sc->sc_clock = (void *)ma->ma_addr;
    102  1.10.12.1       tls 		sc->sc_nvram = (void *)(ma->ma_addr + MC_NREGS);
    103        1.1  nisimura 		sc->sc_nvramsize = 50;
    104        1.4   gdamore 		sc->sc_todr.todr_gettime_ymdhms = dsclock_get;
    105        1.4   gdamore 		sc->sc_todr.todr_settime_ymdhms = dsclock_set;
    106  1.10.12.1       tls 		sc->sc_todr.cookie = sc;
    107  1.10.12.2       tls 		dsclock_init(sc);
    108        1.9   tsutsui 		aprint_normal(": ds1287a\n");
    109        1.1  nisimura 		break;
    110        1.1  nisimura 	}
    111        1.4   gdamore 	todr_attach(&sc->sc_todr);
    112        1.1  nisimura 	memcpy(sc->sc_image, sc->sc_nvram, sc->sc_nvramsize);
    113        1.1  nisimura }
    114        1.1  nisimura 
    115        1.1  nisimura /*
    116        1.1  nisimura  * Get the time of day, based on the clock's value and/or the base value.
    117        1.1  nisimura  */
    118        1.4   gdamore static int
    119        1.4   gdamore mkclock_get(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    120        1.1  nisimura {
    121        1.4   gdamore 	struct timekeeper_softc *sc = (void *)tch->cookie;
    122       1.10   tsutsui 	volatile uint8_t *chiptime = (void *)sc->sc_clock;
    123        1.1  nisimura 	int s;
    124        1.1  nisimura 
    125        1.1  nisimura 	s = splclock();
    126        1.1  nisimura 	chiptime[MK_CSR] |= MK_CSR_READ;	/* enable read (stop time) */
    127  1.10.12.3  jdolecek 	dt->dt_sec = bcdtobin(chiptime[MK_SEC]);
    128  1.10.12.3  jdolecek 	dt->dt_min = bcdtobin(chiptime[MK_MIN]);
    129  1.10.12.3  jdolecek 	dt->dt_hour = bcdtobin(chiptime[MK_HOUR]);
    130  1.10.12.3  jdolecek 	dt->dt_wday = bcdtobin(chiptime[MK_DOW]);
    131  1.10.12.3  jdolecek 	dt->dt_day = bcdtobin(chiptime[MK_DOM]);
    132  1.10.12.3  jdolecek 	dt->dt_mon = bcdtobin(chiptime[MK_MONTH]);
    133  1.10.12.3  jdolecek 	dt->dt_year = bcdtobin(chiptime[MK_YEAR]) + YEAR0;
    134        1.1  nisimura 	chiptime[MK_CSR] &= ~MK_CSR_READ;	/* time wears on */
    135        1.1  nisimura 	splx(s);
    136        1.4   gdamore 	return 0;
    137        1.1  nisimura }
    138        1.1  nisimura 
    139        1.1  nisimura /*
    140        1.1  nisimura  * Reset the TODR based on the time value.
    141        1.1  nisimura  */
    142        1.4   gdamore static int
    143        1.4   gdamore mkclock_set(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    144        1.1  nisimura {
    145        1.4   gdamore 	struct timekeeper_softc *sc = (void *)tch->cookie;
    146       1.10   tsutsui 	volatile uint8_t *chiptime = (void *)sc->sc_clock;
    147       1.10   tsutsui 	volatile uint8_t *stamp = (uint8_t *)sc->sc_nvram + 0x10;
    148        1.1  nisimura 	int s;
    149        1.1  nisimura 
    150        1.1  nisimura 	s = splclock();
    151        1.1  nisimura 	chiptime[MK_CSR] |= MK_CSR_WRITE;	/* enable write */
    152  1.10.12.3  jdolecek 	chiptime[MK_SEC] = bintobcd(dt->dt_sec);
    153  1.10.12.3  jdolecek 	chiptime[MK_MIN] = bintobcd(dt->dt_min);
    154  1.10.12.3  jdolecek 	chiptime[MK_HOUR] = bintobcd(dt->dt_hour);
    155  1.10.12.3  jdolecek 	chiptime[MK_DOW] = bintobcd(dt->dt_wday);
    156  1.10.12.3  jdolecek 	chiptime[MK_DOM] = bintobcd(dt->dt_day);
    157  1.10.12.3  jdolecek 	chiptime[MK_MONTH] = bintobcd(dt->dt_mon);
    158  1.10.12.3  jdolecek 	chiptime[MK_YEAR] = bintobcd(dt->dt_year - YEAR0);
    159        1.1  nisimura 	chiptime[MK_CSR] &= ~MK_CSR_WRITE;	/* load them up */
    160        1.1  nisimura 	splx(s);
    161        1.1  nisimura 
    162        1.1  nisimura 	stamp[0] = 'R'; stamp[1] = 'T'; stamp[2] = 'C'; stamp[3] = '\0';
    163        1.4   gdamore 	return 0;
    164        1.1  nisimura }
    165        1.1  nisimura 
    166  1.10.12.2       tls static void
    167  1.10.12.2       tls dsclock_init(struct timekeeper_softc *sc)
    168  1.10.12.2       tls {
    169  1.10.12.2       tls 	volatile uint8_t *chiptime = (void *)sc->sc_clock;
    170  1.10.12.2       tls 
    171  1.10.12.2       tls 	/*
    172  1.10.12.2       tls 	 * It looks the firmware ROM doesn't initialize DS1287 at all
    173  1.10.12.2       tls 	 * even after the chip is replaced, so explicitly initialize
    174  1.10.12.2       tls 	 * control registers here.
    175  1.10.12.2       tls 	 */
    176  1.10.12.2       tls 	chiptime = (void *)sc->sc_clock;
    177  1.10.12.2       tls 
    178  1.10.12.2       tls 	/* No DSE, 24HR, BINARY */
    179  1.10.12.2       tls 	chiptime[MC_REGB] =
    180  1.10.12.2       tls 	    (chiptime[MC_REGB] & ~MC_REGB_DSE) |
    181  1.10.12.2       tls 	    (MC_REGB_24HR | MC_REGB_BINARY);
    182  1.10.12.2       tls 
    183  1.10.12.2       tls 	/* make sure to start integrated clock OSC */
    184  1.10.12.2       tls 	chiptime[MC_REGA] =
    185  1.10.12.2       tls 	    (chiptime[MC_REGA] & ~MC_REGA_DVMASK) | MC_BASE_32_KHz;
    186  1.10.12.2       tls }
    187  1.10.12.2       tls 
    188        1.1  nisimura /*
    189        1.1  nisimura  * Get the time of day, based on the clock's value and/or the base value.
    190        1.1  nisimura  */
    191        1.4   gdamore static int
    192        1.4   gdamore dsclock_get(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    193        1.1  nisimura {
    194        1.4   gdamore 	struct timekeeper_softc *sc = (void *)tch->cookie;
    195       1.10   tsutsui 	volatile uint8_t *chiptime = (void *)sc->sc_clock;
    196        1.1  nisimura 	int s;
    197        1.1  nisimura 
    198        1.1  nisimura 	s = splclock();
    199        1.1  nisimura 	/* update in progress; spin loop */
    200        1.1  nisimura 	while (chiptime[MC_REGA] & MC_REGA_UIP)
    201       1.10   tsutsui 		continue;
    202        1.1  nisimura 	dt->dt_sec = chiptime[MC_SEC];
    203        1.1  nisimura 	dt->dt_min = chiptime[MC_MIN];
    204        1.1  nisimura 	dt->dt_hour = chiptime[MC_HOUR];
    205        1.1  nisimura 	dt->dt_wday = chiptime[MC_DOW];
    206        1.1  nisimura 	dt->dt_day = chiptime[MC_DOM];
    207        1.1  nisimura 	dt->dt_mon = chiptime[MC_MONTH];
    208        1.1  nisimura 	dt->dt_year = chiptime[MC_YEAR] + YEAR0;
    209        1.1  nisimura 	splx(s);
    210        1.4   gdamore 	return 0;
    211        1.1  nisimura }
    212        1.1  nisimura 
    213        1.1  nisimura /*
    214        1.1  nisimura  * Reset the TODR based on the time value.
    215        1.1  nisimura  */
    216        1.4   gdamore static int
    217        1.4   gdamore dsclock_set(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    218        1.1  nisimura {
    219        1.4   gdamore 	struct timekeeper_softc *sc = (void *)tch->cookie;
    220       1.10   tsutsui 	volatile uint8_t *chiptime = (void *)sc->sc_clock;
    221        1.1  nisimura 	int s;
    222        1.1  nisimura 
    223        1.1  nisimura 	s = splclock();
    224        1.1  nisimura 	chiptime[MC_REGB] |= MC_REGB_SET;	/* enable write */
    225        1.1  nisimura 	chiptime[MC_SEC] = dt->dt_sec;
    226        1.1  nisimura 	chiptime[MC_MIN] = dt->dt_min;
    227        1.1  nisimura 	chiptime[MC_HOUR] = dt->dt_hour;
    228        1.1  nisimura 	chiptime[MC_DOW] = dt->dt_wday;
    229        1.1  nisimura 	chiptime[MC_DOM] = dt->dt_day;
    230        1.1  nisimura 	chiptime[MC_MONTH] = dt->dt_mon;
    231        1.1  nisimura 	chiptime[MC_YEAR] = dt->dt_year - YEAR0;
    232        1.1  nisimura 	chiptime[MC_REGB] &= ~MC_REGB_SET;	/* load them up */
    233        1.1  nisimura 	splx(s);
    234        1.4   gdamore 	return 0;
    235        1.1  nisimura }
    236