Home | History | Annotate | Line # | Download | only in vr
rtc.c revision 1.14
      1  1.14  takemura /*	$NetBSD: rtc.c,v 1.14 2002/02/10 14:15:32 takemura Exp $	*/
      2   1.1  takemura 
      3   1.1  takemura /*-
      4   1.1  takemura  * Copyright (c) 1999 Shin Takemura. All rights reserved.
      5   1.1  takemura  * Copyright (c) 1999 SATO Kazumi. All rights reserved.
      6   1.1  takemura  * Copyright (c) 1999 PocketBSD Project. All rights reserved.
      7   1.1  takemura  *
      8   1.1  takemura  * Redistribution and use in source and binary forms, with or without
      9   1.1  takemura  * modification, are permitted provided that the following conditions
     10   1.1  takemura  * are met:
     11   1.1  takemura  * 1. Redistributions of source code must retain the above copyright
     12   1.1  takemura  *    notice, this list of conditions and the following disclaimer.
     13   1.1  takemura  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1  takemura  *    notice, this list of conditions and the following disclaimer in the
     15   1.1  takemura  *    documentation and/or other materials provided with the distribution.
     16   1.1  takemura  * 3. All advertising materials mentioning features or use of this software
     17   1.1  takemura  *    must display the following acknowledgement:
     18   1.1  takemura  *	This product includes software developed by the PocketBSD project
     19   1.1  takemura  *	and its contributors.
     20   1.1  takemura  * 4. Neither the name of the project nor the names of its contributors
     21   1.1  takemura  *    may be used to endorse or promote products derived from this software
     22   1.1  takemura  *    without specific prior written permission.
     23   1.1  takemura  *
     24   1.1  takemura  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.1  takemura  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.1  takemura  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1  takemura  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.1  takemura  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1  takemura  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.1  takemura  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1  takemura  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1  takemura  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1  takemura  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1  takemura  * SUCH DAMAGE.
     35   1.1  takemura  *
     36   1.1  takemura  */
     37   1.1  takemura 
     38   1.5     enami #include "opt_vr41xx.h"
     39   1.5     enami 
     40   1.1  takemura #include <sys/param.h>
     41   1.1  takemura #include <sys/systm.h>
     42   1.1  takemura 
     43   1.8       uch #include <machine/sysconf.h>
     44   1.1  takemura #include <machine/bus.h>
     45   1.8       uch 
     46   1.8       uch #include <dev/clock_subr.h>
     47   1.1  takemura 
     48   1.1  takemura #include <hpcmips/vr/vr.h>
     49   1.5     enami #include <hpcmips/vr/vrcpudef.h>
     50  1.12  takemura #include <hpcmips/vr/vripif.h>
     51  1.14  takemura #include <hpcmips/vr/vripreg.h>
     52   1.1  takemura #include <hpcmips/vr/rtcreg.h>
     53   1.1  takemura 
     54   1.3      sato /*
     55   1.3      sato  * for debugging definitions
     56   1.6    toshii  * 	VRRTCDEBUG	print rtc debugging information
     57   1.4      sato  *	VRRTC_HEARTBEAT	print HEARTBEAT (too many print...)
     58   1.3      sato  */
     59   1.4      sato #ifdef VRRTCDEBUG
     60   1.4      sato #ifndef VRRTCDEBUG_CONF
     61   1.4      sato #define VRRTCDEBUG_CONF 0
     62   1.4      sato #endif
     63   1.4      sato int vrrtc_debug = VRRTCDEBUG_CONF;
     64   1.4      sato #define DPRINTF(arg) if (vrrtc_debug) printf arg;
     65   1.4      sato #define DDUMP_REGS(arg) if (vrrtc_debug) vrrtc_dump_regs(arg);
     66   1.4      sato #else /* VRRTCDEBUG */
     67   1.4      sato #define DPRINTF(arg)
     68   1.4      sato #define DDUMP_REGS(arg)
     69   1.4      sato #endif /* VRRTCDEBUG */
     70   1.1  takemura 
     71   1.1  takemura struct vrrtc_softc {
     72   1.1  takemura 	struct device sc_dev;
     73   1.1  takemura 	bus_space_tag_t sc_iot;
     74   1.1  takemura 	bus_space_handle_t sc_ioh;
     75   1.1  takemura 	void *sc_ih;
     76  1.14  takemura #ifndef SINGLE_VRIP_BASE
     77  1.14  takemura 	int sc_rtcint_reg;
     78  1.14  takemura 	int sc_tclk_h_reg, sc_tclk_l_reg;
     79  1.14  takemura 	int sc_tclk_cnt_h_reg, sc_tclk_cnt_l_reg;
     80  1.14  takemura #endif /* SINGLE_VRIP_BASE */
     81   1.1  takemura };
     82   1.1  takemura 
     83   1.7       uch void	clock_init(struct device *);
     84   1.8       uch void	clock_get(struct device *, time_t, struct clock_ymdhms *);
     85   1.8       uch void	clock_set(struct device *, struct clock_ymdhms *);
     86   1.1  takemura 
     87   1.8       uch struct platform_clock vr_clock = {
     88   1.8       uch #define CLOCK_RATE	128
     89   1.8       uch 	CLOCK_RATE, clock_init, clock_get, clock_set,
     90   1.1  takemura };
     91   1.1  takemura 
     92   1.7       uch int	vrrtc_match(struct device *, struct cfdata *, void *);
     93   1.7       uch void	vrrtc_attach(struct device *, struct device *, void *);
     94   1.7       uch int	vrrtc_intr(void*, u_int32_t, u_int32_t);
     95   1.7       uch void	vrrtc_dump_regs(struct vrrtc_softc *);
     96   1.1  takemura 
     97   1.1  takemura struct cfattach vrrtc_ca = {
     98   1.1  takemura 	sizeof(struct vrrtc_softc), vrrtc_match, vrrtc_attach
     99   1.1  takemura };
    100   1.1  takemura 
    101   1.9       uch static __inline__ void vrrtc_write(struct vrrtc_softc *, int, u_int16_t);
    102   1.9       uch static __inline__ u_int16_t vrrtc_read(struct vrrtc_softc *, int);
    103   1.8       uch void	cvt_timehl_ymdhms(u_int32_t, u_int32_t, struct clock_ymdhms *);
    104   1.1  takemura 
    105   1.1  takemura extern int rtc_offset;
    106   1.8       uch static int m2d[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    107   1.1  takemura 
    108   1.8       uch static __inline__ void
    109   1.8       uch vrrtc_write(struct vrrtc_softc *sc, int port, u_int16_t val)
    110   1.1  takemura {
    111   1.7       uch 
    112   1.8       uch 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
    113   1.1  takemura }
    114   1.1  takemura 
    115   1.8       uch static __inline__ u_int16_t
    116   1.8       uch vrrtc_read(struct vrrtc_softc *sc, int port)
    117   1.1  takemura {
    118   1.7       uch 
    119   1.8       uch 	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, port));
    120   1.1  takemura }
    121   1.1  takemura 
    122   1.8       uch int
    123   1.8       uch vrrtc_match(struct device *parent, struct cfdata *cf, void *aux)
    124   1.1  takemura {
    125   1.7       uch 
    126   1.8       uch 	return (1);
    127   1.1  takemura }
    128   1.1  takemura 
    129  1.14  takemura #ifndef SINGLE_VRIP_BASE
    130  1.14  takemura #define RTCINT_REG_W		(sc->sc_rtcint_reg)
    131  1.14  takemura #define TCLK_H_REG_W		(sc->sc_tclk_h_reg)
    132  1.14  takemura #define TCLK_L_REG_W		(sc->sc_tclk_l_reg)
    133  1.14  takemura #define TCLK_CNT_H_REG_W	(sc->sc_tclk_cnt_h_reg)
    134  1.14  takemura #define TCLK_CNT_L_REG_W	(sc->sc_tclk_cnt_l_reg)
    135  1.14  takemura #endif /* SINGLE_VRIP_BASE */
    136  1.14  takemura 
    137   1.1  takemura void
    138   1.7       uch vrrtc_attach(struct device *parent, struct device *self, void *aux)
    139   1.1  takemura {
    140   1.1  takemura 	struct vrip_attach_args *va = aux;
    141   1.8       uch 	struct vrrtc_softc *sc = (void *)self;
    142  1.14  takemura 
    143  1.14  takemura #ifndef SINGLE_VRIP_BASE
    144  1.14  takemura 	if (va->va_addr == VR4102_RTC_ADDR) {
    145  1.14  takemura 		sc->sc_rtcint_reg = VR4102_RTCINT_REG_W;
    146  1.14  takemura 		sc->sc_tclk_h_reg = VR4102_TCLK_H_REG_W;
    147  1.14  takemura 		sc->sc_tclk_l_reg = VR4102_TCLK_L_REG_W;
    148  1.14  takemura 		sc->sc_tclk_cnt_h_reg = VR4102_TCLK_CNT_H_REG_W;
    149  1.14  takemura 		sc->sc_tclk_cnt_l_reg = VR4102_TCLK_CNT_L_REG_W;
    150  1.14  takemura 	} else
    151  1.14  takemura 	if (va->va_addr == VR4122_RTC_ADDR) {
    152  1.14  takemura 		sc->sc_rtcint_reg = VR4122_RTCINT_REG_W;
    153  1.14  takemura 		sc->sc_tclk_h_reg = VR4122_TCLK_H_REG_W;
    154  1.14  takemura 		sc->sc_tclk_l_reg = VR4122_TCLK_L_REG_W;
    155  1.14  takemura 		sc->sc_tclk_cnt_h_reg = VR4122_TCLK_CNT_H_REG_W;
    156  1.14  takemura 		sc->sc_tclk_cnt_l_reg = VR4122_TCLK_CNT_L_REG_W;
    157  1.14  takemura 	} else
    158  1.14  takemura 	if (va->va_addr == VR4181_RTC_ADDR) {
    159  1.14  takemura 		sc->sc_rtcint_reg = VR4181_RTCINT_REG_W;
    160  1.14  takemura 		sc->sc_tclk_h_reg = RTC_NO_REG_W;
    161  1.14  takemura 		sc->sc_tclk_l_reg = RTC_NO_REG_W;
    162  1.14  takemura 		sc->sc_tclk_cnt_h_reg = RTC_NO_REG_W;
    163  1.14  takemura 		sc->sc_tclk_cnt_l_reg = RTC_NO_REG_W;
    164  1.14  takemura 	} else {
    165  1.14  takemura 		panic("%s: unknown base address 0x%lx\n",
    166  1.14  takemura 		    sc->sc_dev.dv_xname, va->va_addr);
    167  1.14  takemura 	}
    168  1.14  takemura #endif /* SINGLE_VRIP_BASE */
    169  1.14  takemura 
    170   1.1  takemura 	sc->sc_iot = va->va_iot;
    171   1.1  takemura 	if (bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
    172   1.7       uch 	    0 /* no flags */, &sc->sc_ioh)) {
    173   1.1  takemura 		printf("vrrtc_attach: can't map i/o space\n");
    174   1.1  takemura 		return;
    175   1.1  takemura 	}
    176   1.1  takemura 	/* RTC interrupt handler is directly dispatched from CPU intr */
    177   1.1  takemura 	vr_intr_establish(VR_INTR1, vrrtc_intr, sc);
    178   1.1  takemura 	/* But need to set level 1 interupt mask register,
    179   1.1  takemura 	 * so regsiter fake interrurpt handler
    180   1.1  takemura 	 */
    181  1.12  takemura 	if (!(sc->sc_ih = vrip_intr_establish(va->va_vc, va->va_unit, 0,
    182   1.7       uch 	    IPL_CLOCK, 0, 0))) {
    183   1.1  takemura 		printf (":can't map interrupt.\n");
    184   1.1  takemura 		return;
    185   1.1  takemura 	}
    186   1.1  takemura 	/*
    187   1.1  takemura 	 *  Rtc is attached to call this routine
    188   1.1  takemura 	 *  before cpu_initclock() calls clock_init().
    189   1.1  takemura 	 *  So we must disable all interrupt for now.
    190   1.1  takemura 	 */
    191   1.1  takemura 	/*
    192   1.1  takemura 	 * Disable all rtc interrupts
    193   1.1  takemura 	 */
    194   1.1  takemura 	/* Disable Elapse compare intr */
    195   1.1  takemura 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECMP_H_REG_W, 0);
    196   1.1  takemura 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECMP_M_REG_W, 0);
    197   1.1  takemura 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECMP_L_REG_W, 0);
    198   1.1  takemura 	/* Disable RTC Long1 intr */
    199   1.1  takemura 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL1_H_REG_W, 0);
    200   1.1  takemura 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL1_L_REG_W, 0);
    201   1.1  takemura 	/* Disable RTC Long2 intr */
    202   1.1  takemura 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL2_H_REG_W, 0);
    203   1.1  takemura 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL2_L_REG_W, 0);
    204   1.1  takemura 	/* Disable RTC TCLK intr */
    205  1.13      sato 	if (TCLK_H_REG_W != RTC_NO_REG_W) {
    206  1.10      sato 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, TCLK_H_REG_W, 0);
    207  1.10      sato 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, TCLK_L_REG_W, 0);
    208  1.10      sato 	}
    209   1.1  takemura 	/*
    210   1.1  takemura 	 * Clear all rtc intrrupts.
    211   1.1  takemura 	 */
    212   1.1  takemura 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCINT_REG_W, RTCINT_ALL);
    213   1.1  takemura 
    214   1.8       uch 	platform_clock_attach(sc, &vr_clock);
    215   1.1  takemura }
    216   1.1  takemura 
    217   1.1  takemura int
    218   1.7       uch vrrtc_intr(void *arg, u_int32_t pc, u_int32_t statusReg)
    219   1.1  takemura {
    220   1.1  takemura 	struct vrrtc_softc *sc = arg;
    221   1.1  takemura 	struct clockframe cf;
    222  1.14  takemura 
    223   1.1  takemura 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCINT_REG_W, RTCINT_ALL);
    224   1.1  takemura 	cf.pc = pc;
    225   1.1  takemura 	cf.sr = statusReg;
    226   1.1  takemura 	hardclock(&cf);
    227   1.1  takemura 	intrcnt[HARDCLOCK]++;
    228   1.1  takemura 
    229   1.4      sato #ifdef VRRTC_HEARTBEAT
    230   1.1  takemura 	if ((intrcnt[HARDCLOCK] % (CLOCK_RATE * 5)) == 0) {
    231  1.14  takemura 		struct clock_ymdhms dt;
    232  1.14  takemura 		clock_get((struct device *)sc, NULL, &dt);
    233   1.1  takemura 		printf("%s(%d): rtc_intr: %2d.%2d.%2d %02d:%02d:%02d\n",
    234   1.7       uch 		    __FILE__, __LINE__,
    235  1.14  takemura 		    dt.dt_year, dt.dt_mon, dt.dt_day,
    236  1.14  takemura 		    dt.dt_hour, dt.dt_min, dt.dt_sec);
    237   1.1  takemura 	}
    238   1.1  takemura #endif
    239   1.1  takemura 	return 0;
    240   1.1  takemura }
    241   1.1  takemura 
    242   1.1  takemura void
    243   1.8       uch clock_init(struct device *dev)
    244   1.1  takemura {
    245   1.8       uch 	struct vrrtc_softc *sc = (struct vrrtc_softc *)dev;
    246   1.1  takemura 
    247   1.8       uch 	DDUMP_REGS(sc);
    248   1.8       uch 	/*
    249   1.8       uch 	 * Set tick (CLOCK_RATE)
    250   1.8       uch 	 */
    251   1.8       uch 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL1_H_REG_W, 0);
    252   1.8       uch 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL1_L_REG_W,
    253   1.8       uch 	    RTCL1_L_HZ/CLOCK_RATE);
    254   1.8       uch }
    255   1.1  takemura 
    256   1.8       uch void
    257   1.8       uch clock_get(struct device *dev, time_t base, struct clock_ymdhms *dt)
    258   1.8       uch {
    259   1.1  takemura 
    260   1.8       uch 	struct vrrtc_softc *sc = (struct vrrtc_softc *)dev;
    261   1.8       uch 	bus_space_tag_t iot = sc->sc_iot;
    262   1.8       uch 	bus_space_handle_t ioh = sc->sc_ioh;
    263   1.8       uch 	u_int32_t timeh;	/* elapse time (2*timeh sec) */
    264   1.8       uch 	u_int32_t timel;	/* timel/32768 sec */
    265   1.8       uch 
    266   1.8       uch 	timeh = bus_space_read_2(iot, ioh, ETIME_H_REG_W);
    267   1.8       uch 	timeh = (timeh << 16) | bus_space_read_2(iot, ioh, ETIME_M_REG_W);
    268   1.8       uch 	timel = bus_space_read_2(iot, ioh, ETIME_L_REG_W);
    269   1.1  takemura 
    270  1.11      shin 	DPRINTF(("clock_get: timeh %08x timel %08x\n", timeh, timel));
    271   1.1  takemura 
    272   1.8       uch 	cvt_timehl_ymdhms(timeh, timel, dt);
    273   1.1  takemura 
    274   1.8       uch 	DPRINTF(("clock_get: %d/%d/%d/%d/%d/%d\n", dt->dt_year, dt->dt_mon,
    275   1.8       uch 	    dt->dt_day, dt->dt_hour, dt->dt_min, dt->dt_sec));
    276   1.4      sato }
    277   1.4      sato 
    278   1.4      sato void
    279   1.8       uch clock_set(struct device *dev, struct clock_ymdhms *dt)
    280   1.4      sato {
    281   1.4      sato 	struct vrrtc_softc *sc = (struct vrrtc_softc *)dev;
    282   1.8       uch 	bus_space_tag_t iot = sc->sc_iot;
    283   1.8       uch 	bus_space_handle_t ioh = sc->sc_ioh;
    284   1.8       uch 	u_int32_t timeh;	/* elapse time (2*timeh sec) */
    285   1.8       uch 	u_int32_t timel;	/* timel/32768 sec */
    286   1.8       uch 	int year, month, sec2;
    287   1.8       uch 
    288   1.8       uch 	timeh = 0;
    289   1.8       uch 	timel = 0;
    290   1.8       uch 
    291   1.8       uch 	DPRINTF(("clock_set: %d/%d/%d/%d/%d/%d\n", dt->dt_year, dt->dt_mon,
    292   1.8       uch 	    dt->dt_day, dt->dt_hour, dt->dt_min, dt->dt_sec));
    293   1.8       uch 
    294   1.8       uch 	dt->dt_year += YBASE;
    295   1.8       uch 
    296   1.8       uch 	DPRINTF(("clock_set: %d/%d/%d/%d/%d/%d\n", dt->dt_year, dt->dt_mon,
    297   1.8       uch 	    dt->dt_day, dt->dt_hour, dt->dt_min, dt->dt_sec));
    298   1.8       uch 
    299   1.8       uch 	year = EPOCHYEAR;
    300   1.8       uch 	sec2 = LEAPYEAR4(year)?SEC2YR+SEC2DAY:SEC2YR;
    301   1.8       uch 	while (year < dt->dt_year) {
    302   1.8       uch 		year++;
    303   1.8       uch 		timeh += sec2;
    304   1.8       uch 		sec2 = LEAPYEAR4(year)?SEC2YR+SEC2DAY:SEC2YR;
    305   1.8       uch 	}
    306   1.8       uch 	month = 1; /* now month is 1..12 */
    307   1.8       uch 	sec2 = SEC2DAY * m2d[month-1];
    308   1.8       uch 	while (month < dt->dt_mon) {
    309   1.8       uch 		month++;
    310   1.8       uch 		timeh += sec2;
    311   1.8       uch 		sec2 = SEC2DAY * m2d[month-1];
    312   1.8       uch 		if (month == 2 && LEAPYEAR4(year)) /* feb. and leapyear */
    313   1.8       uch 			sec2 += SEC2DAY;
    314   1.8       uch 	}
    315   1.8       uch 
    316   1.8       uch 	timeh += (dt->dt_day - 1)*SEC2DAY;
    317   1.8       uch 
    318   1.8       uch 	timeh += dt->dt_hour*SEC2HOUR;
    319   1.8       uch 
    320   1.8       uch 	timeh += dt->dt_min*SEC2MIN;
    321   1.4      sato 
    322   1.8       uch 	timeh += dt->dt_sec/2;
    323   1.8       uch 	timel += (dt->dt_sec%2)*ETIME_L_HZ;
    324   1.8       uch 
    325   1.8       uch 	timeh += EPOCHOFF;
    326   1.8       uch 	timeh -= (rtc_offset*SEC2MIN);
    327   1.8       uch 
    328   1.8       uch #ifdef VRRTCDEBUG
    329   1.8       uch 	cvt_timehl_ymdhms(timeh, timel, NULL);
    330   1.8       uch #endif /* RTCDEBUG */
    331   1.8       uch 
    332   1.8       uch 	bus_space_write_2(iot, ioh, ETIME_H_REG_W, (timeh >> 16) & 0xffff);
    333   1.8       uch 	bus_space_write_2(iot, ioh, ETIME_M_REG_W, timeh & 0xffff);
    334   1.8       uch 	bus_space_write_2(iot, ioh, ETIME_L_REG_W, timel);
    335   1.1  takemura }
    336   1.1  takemura 
    337   1.1  takemura void
    338   1.8       uch cvt_timehl_ymdhms(
    339   1.8       uch 	u_int32_t timeh, /* 2 sec */
    340   1.8       uch 	u_int32_t timel, /* 1/32768 sec */
    341   1.8       uch 	struct clock_ymdhms *dt)
    342   1.1  takemura {
    343   1.8       uch 	u_int32_t year, month, date, hour, min, sec, sec2;
    344   1.1  takemura 
    345   1.1  takemura 	timeh -= EPOCHOFF;
    346   1.1  takemura 
    347   1.2      sato 	timeh += (rtc_offset*SEC2MIN);
    348   1.1  takemura 
    349   1.1  takemura 	year = EPOCHYEAR;
    350   1.2      sato 	sec2 = LEAPYEAR4(year)?SEC2YR+SEC2DAY:SEC2YR;
    351   1.1  takemura 	while (timeh > sec2) {
    352   1.1  takemura 		year++;
    353   1.1  takemura 		timeh -= sec2;
    354   1.2      sato 		sec2 = LEAPYEAR4(year)?SEC2YR+SEC2DAY:SEC2YR;
    355   1.1  takemura 	}
    356   1.1  takemura 
    357  1.11      shin 	DPRINTF(("cvt_timehl_ymdhms: timeh %08x year %d yrref %d\n",
    358   1.7       uch 	    timeh, year, sec2));
    359   1.1  takemura 
    360   1.1  takemura 	month = 0; /* now month is 0..11 */
    361   1.2      sato 	sec2 = SEC2DAY * m2d[month];
    362   1.1  takemura 	while (timeh > sec2) {
    363   1.1  takemura 		timeh -= sec2;
    364   1.1  takemura 		month++;
    365   1.2      sato 		sec2 = SEC2DAY * m2d[month];
    366   1.1  takemura 		if (month == 1 && LEAPYEAR4(year)) /* feb. and leapyear */
    367   1.2      sato 			sec2 += SEC2DAY;
    368   1.1  takemura 	}
    369   1.1  takemura 	month +=1; /* now month is 1..12 */
    370   1.1  takemura 
    371  1.11      shin 	DPRINTF(("cvt_timehl_ymdhms: timeh %08x month %d mref %d\n",
    372   1.7       uch 	    timeh, month, sec2));
    373   1.1  takemura 
    374   1.2      sato 	sec2 = SEC2DAY;
    375   1.1  takemura 	date = timeh/sec2+1; /* date is 1..31 */
    376   1.1  takemura 	timeh -= (date-1)*sec2;
    377   1.1  takemura 
    378  1.11      shin 	DPRINTF(("cvt_timehl_ymdhms: timeh %08x date %d dref %d\n",
    379   1.7       uch 	    timeh, date, sec2));
    380   1.1  takemura 
    381   1.2      sato 	sec2 = SEC2HOUR;
    382   1.1  takemura 	hour = timeh/sec2;
    383   1.1  takemura 	timeh -= hour*sec2;
    384   1.1  takemura 
    385   1.2      sato 	sec2 = SEC2MIN;
    386   1.1  takemura 	min = timeh/sec2;
    387   1.1  takemura 	timeh -= min*sec2;
    388   1.1  takemura 
    389   1.1  takemura 	sec = timeh*2 + timel/ETIME_L_HZ;
    390   1.1  takemura 
    391  1.11      shin 	DPRINTF(("cvt_timehl_ymdhms: hour %d min %d sec %d\n", hour, min, sec));
    392   1.1  takemura 
    393   1.8       uch 	if (dt) {
    394   1.8       uch 		dt->dt_year	= year - YBASE; /* base 1900 */
    395   1.8       uch 		dt->dt_mon	= month;
    396   1.8       uch 		dt->dt_day	= date;
    397   1.8       uch 		dt->dt_hour	= hour;
    398   1.8       uch 		dt->dt_min	= min;
    399   1.8       uch 		dt->dt_sec	= sec;
    400   1.1  takemura 	}
    401   1.1  takemura }
    402   1.1  takemura 
    403   1.1  takemura void
    404   1.8       uch vrrtc_dump_regs(struct vrrtc_softc *sc)
    405   1.1  takemura {
    406   1.8       uch 	int timeh;
    407   1.8       uch 	int timel;
    408   1.1  takemura 
    409   1.1  takemura 	timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_H_REG_W);
    410   1.8       uch 	timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_M_REG_W);
    411   1.8       uch 	timel = (timel << 16)
    412   1.8       uch 	    | bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_L_REG_W);
    413   1.8       uch 	printf("clock_init()  Elapse Time %04x%04x\n", timeh, timel);
    414   1.1  takemura 
    415   1.8       uch 	timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ECMP_H_REG_W);
    416   1.8       uch 	timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ECMP_M_REG_W);
    417   1.8       uch 	timel = (timel << 16)
    418   1.8       uch 	    | bus_space_read_2(sc->sc_iot, sc->sc_ioh, ECMP_L_REG_W);
    419   1.8       uch 	printf("clock_init()  Elapse Compare %04x%04x\n", timeh, timel);
    420   1.1  takemura 
    421   1.8       uch 	timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL1_H_REG_W);
    422   1.8       uch 	timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL1_L_REG_W);
    423   1.8       uch 	printf("clock_init()  LONG1 %04x%04x\n", timeh, timel);
    424   1.1  takemura 
    425   1.8       uch 	timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL1_CNT_H_REG_W);
    426   1.8       uch 	timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL1_CNT_L_REG_W);
    427   1.8       uch 	printf("clock_init()  LONG1 CNTL %04x%04x\n", timeh, timel);
    428   1.1  takemura 
    429   1.8       uch 	timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL2_H_REG_W);
    430   1.8       uch 	timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL2_L_REG_W);
    431   1.8       uch 	printf("clock_init()  LONG2 %04x%04x\n", timeh, timel);
    432   1.1  takemura 
    433   1.8       uch 	timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL2_CNT_H_REG_W);
    434   1.8       uch 	timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL2_CNT_L_REG_W);
    435   1.8       uch 	printf("clock_init()  LONG2 CNTL %04x%04x\n", timeh, timel);
    436   1.1  takemura 
    437  1.13      sato 	if (TCLK_H_REG_W != RTC_NO_REG_W) {
    438  1.10      sato 		timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, TCLK_H_REG_W);
    439  1.10      sato 		timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, TCLK_L_REG_W);
    440  1.10      sato 		printf("clock_init()  TCLK %04x%04x\n", timeh, timel);
    441  1.10      sato 
    442  1.10      sato 		timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, TCLK_CNT_H_REG_W);
    443  1.10      sato 		timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, TCLK_CNT_L_REG_W);
    444  1.10      sato 		printf("clock_init()  TCLK CNTL %04x%04x\n", timeh, timel);
    445  1.10      sato 	}
    446   1.1  takemura }
    447