Home | History | Annotate | Line # | Download | only in dev
rtc.c revision 1.2.34.1
      1  1.2.34.1  mjf /*	$NetBSD: rtc.c,v 1.2.34.1 2008/04/03 12:42:24 mjf Exp $ */
      2  1.2.34.1  mjf 
      3       1.1  uwe /*-
      4       1.1  uwe  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5       1.1  uwe  * All rights reserved.
      6       1.1  uwe  *
      7       1.1  uwe  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  uwe  * by UCHIYAMA Yasushi.
      9       1.1  uwe  *
     10       1.1  uwe  * Redistribution and use in source and binary forms, with or without
     11       1.1  uwe  * modification, are permitted provided that the following conditions
     12       1.1  uwe  * are met:
     13       1.1  uwe  * 1. Redistributions of source code must retain the above copyright
     14       1.1  uwe  *    notice, this list of conditions and the following disclaimer.
     15       1.1  uwe  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  uwe  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  uwe  *    documentation and/or other materials provided with the distribution.
     18       1.1  uwe  * 3. All advertising materials mentioning features or use of this software
     19       1.1  uwe  *    must display the following acknowledgement:
     20       1.1  uwe  *        This product includes software developed by the NetBSD
     21       1.1  uwe  *        Foundation, Inc. and its contributors.
     22       1.1  uwe  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1  uwe  *    contributors may be used to endorse or promote products derived
     24       1.1  uwe  *    from this software without specific prior written permission.
     25       1.1  uwe  *
     26       1.1  uwe  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1  uwe  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1  uwe  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1  uwe  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1  uwe  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1  uwe  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1  uwe  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1  uwe  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1  uwe  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1  uwe  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1  uwe  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1  uwe  */
     38       1.1  uwe 
     39       1.1  uwe #include <sys/cdefs.h>
     40  1.2.34.1  mjf __KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.2.34.1 2008/04/03 12:42:24 mjf Exp $");
     41       1.1  uwe 
     42       1.1  uwe #include <sys/param.h>
     43       1.1  uwe #include <sys/kernel.h>
     44       1.1  uwe #include <sys/device.h>
     45       1.1  uwe #include <sys/malloc.h>
     46       1.1  uwe #include <sys/systm.h>
     47       1.1  uwe #ifdef GPROF
     48       1.1  uwe #include <sys/gmon.h>
     49       1.1  uwe #endif
     50       1.1  uwe 
     51       1.1  uwe #include <dev/clock_subr.h>
     52       1.1  uwe 
     53       1.1  uwe #include <sh3/rtcreg.h>
     54       1.1  uwe 
     55       1.2  uwe #if defined(DEBUG) && !defined(RTC_DEBUG)
     56       1.2  uwe #define RTC_DEBUG
     57       1.2  uwe #endif
     58       1.2  uwe 
     59       1.1  uwe 
     60       1.1  uwe struct rtc_softc {
     61  1.2.34.1  mjf 	device_t sc_dev;
     62       1.1  uwe 
     63       1.2  uwe 	int sc_valid;
     64       1.1  uwe 	struct todr_chip_handle sc_todr;
     65       1.1  uwe };
     66       1.1  uwe 
     67  1.2.34.1  mjf static int	rtc_match(device_t, cfdata_t, void *);
     68  1.2.34.1  mjf static void	rtc_attach(device_t, device_t, void *);
     69       1.1  uwe 
     70  1.2.34.1  mjf CFATTACH_DECL_NEW(rtc, sizeof(struct rtc_softc),
     71       1.1  uwe     rtc_match, rtc_attach, NULL, NULL);
     72       1.1  uwe 
     73       1.1  uwe 
     74       1.1  uwe /* todr(9) methods */
     75       1.1  uwe static int rtc_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
     76       1.1  uwe static int rtc_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
     77       1.1  uwe 
     78       1.1  uwe 
     79       1.1  uwe 
     80       1.1  uwe static int
     81  1.2.34.1  mjf rtc_match(device_t parent, cfdata_t cfp, void *aux)
     82       1.1  uwe {
     83       1.1  uwe 
     84       1.1  uwe 	return 1;
     85       1.1  uwe }
     86       1.1  uwe 
     87       1.1  uwe 
     88       1.1  uwe static void
     89  1.2.34.1  mjf rtc_attach(device_t parent, device_t self, void *aux)
     90       1.1  uwe {
     91  1.2.34.1  mjf 	struct rtc_softc *sc;
     92       1.2  uwe 	uint8_t r;
     93       1.2  uwe #ifdef RTC_DEBUG
     94       1.2  uwe 	char bits[128];
     95       1.2  uwe #endif
     96       1.1  uwe 
     97  1.2.34.1  mjf 	aprint_naive("\n");
     98  1.2.34.1  mjf 	aprint_normal("\n");
     99  1.2.34.1  mjf 
    100  1.2.34.1  mjf 	sc = device_private(self);
    101  1.2.34.1  mjf 	sc->sc_dev = self;
    102       1.1  uwe 
    103       1.2  uwe 	r = _reg_read_1(SH_(RCR2));
    104       1.1  uwe 
    105       1.1  uwe #ifdef RTC_DEBUG
    106  1.2.34.1  mjf 	aprint_debug_dev(sc->sc_dev, "RCR2=%s\n",
    107  1.2.34.1  mjf 		bitmask_snprintf(r, SH_RCR2_BITS, bits, sizeof(bits)));
    108       1.2  uwe #endif
    109       1.1  uwe 
    110       1.2  uwe 	/* Was the clock running? */
    111       1.2  uwe 	if ((r & (SH_RCR2_ENABLE | SH_RCR2_START)) == (SH_RCR2_ENABLE
    112       1.2  uwe 						       | SH_RCR2_START))
    113       1.2  uwe 		sc->sc_valid = 1;
    114       1.2  uwe 	else {
    115       1.2  uwe 		sc->sc_valid = 0;
    116  1.2.34.1  mjf 		aprint_error_dev(sc->sc_dev, "WARNING: clock was stopped\n");
    117       1.1  uwe 	}
    118       1.2  uwe 
    119       1.2  uwe 	/* Disable carry and alarm interrupts */
    120       1.2  uwe 	_reg_write_1(SH_(RCR1), 0);
    121       1.2  uwe 
    122       1.2  uwe 	/* Clock runs, no periodic interrupts, no 30-sec adjustment */
    123       1.2  uwe 	_reg_write_1(SH_(RCR2), SH_RCR2_ENABLE | SH_RCR2_START);
    124       1.2  uwe 
    125       1.1  uwe 	sc->sc_todr.cookie = sc;
    126       1.1  uwe 	sc->sc_todr.todr_gettime_ymdhms = rtc_gettime_ymdhms;
    127       1.1  uwe 	sc->sc_todr.todr_settime_ymdhms = rtc_settime_ymdhms;
    128       1.1  uwe 
    129       1.1  uwe 	todr_attach(&sc->sc_todr);
    130       1.2  uwe 
    131       1.2  uwe #ifdef RTC_DEBUG
    132       1.2  uwe 	{
    133       1.2  uwe 		struct clock_ymdhms dt;
    134       1.2  uwe 		rtc_gettime_ymdhms(&sc->sc_todr, &dt);
    135       1.2  uwe 	}
    136       1.2  uwe #endif
    137       1.1  uwe }
    138       1.1  uwe 
    139       1.1  uwe 
    140       1.1  uwe static int
    141       1.1  uwe rtc_gettime_ymdhms(todr_chip_handle_t h, struct clock_ymdhms *dt)
    142       1.1  uwe {
    143       1.2  uwe 	struct rtc_softc *sc = h->cookie;
    144       1.1  uwe 	unsigned int year;
    145       1.1  uwe 	int retry = 8;
    146       1.1  uwe 
    147       1.2  uwe 	if (!sc->sc_valid) {
    148       1.2  uwe #ifdef RTC_DEBUG
    149  1.2.34.1  mjf 		aprint_debug_dev(sc->sc_dev, "gettime: not valid\n");
    150       1.2  uwe 		/* but proceed and read/print it anyway */
    151       1.2  uwe #else
    152       1.2  uwe 		return EIO;
    153       1.2  uwe #endif
    154       1.2  uwe 	}
    155       1.2  uwe 
    156       1.1  uwe 	/* disable carry interrupt */
    157       1.1  uwe 	_reg_bclr_1(SH_(RCR1), SH_RCR1_CIE);
    158       1.1  uwe 
    159       1.1  uwe 	do {
    160       1.1  uwe 		uint8_t r = _reg_read_1(SH_(RCR1));
    161       1.1  uwe 		r &= ~SH_RCR1_CF;
    162       1.1  uwe 		r |= SH_RCR1_AF; /* don't clear alarm flag */
    163       1.1  uwe 		_reg_write_1(SH_(RCR1), r);
    164       1.1  uwe 
    165       1.1  uwe 		if (CPU_IS_SH3)
    166       1.1  uwe 			year = _reg_read_1(SH3_RYRCNT);
    167       1.1  uwe 		else
    168       1.1  uwe 			year = _reg_read_2(SH4_RYRCNT) & 0x00ff;
    169       1.1  uwe 		dt->dt_year = FROMBCD(year);
    170       1.1  uwe 
    171       1.1  uwe 		/* read counter */
    172       1.1  uwe #define	RTCGET(x, y) \
    173       1.1  uwe 		dt->dt_ ## x = FROMBCD(_reg_read_1(SH_(R ## y ## CNT)))
    174       1.1  uwe 
    175       1.1  uwe 		RTCGET(mon, MON);
    176       1.1  uwe 		RTCGET(wday, WK);
    177       1.1  uwe 		RTCGET(day, DAY);
    178       1.1  uwe 		RTCGET(hour, HR);
    179       1.1  uwe 		RTCGET(min, MIN);
    180       1.1  uwe 		RTCGET(sec, SEC);
    181       1.1  uwe #undef RTCGET
    182       1.1  uwe 	} while ((_reg_read_1(SH_(RCR1)) & SH_RCR1_CF) && --retry > 0);
    183       1.1  uwe 
    184       1.2  uwe 	if (retry == 0) {
    185       1.2  uwe #ifdef RTC_DEBUG
    186  1.2.34.1  mjf 		aprint_debug_dev(sc->sc_dev, "gettime: retry failed\n");
    187       1.2  uwe #endif
    188       1.1  uwe 		return EIO;
    189       1.2  uwe 	}
    190       1.1  uwe 
    191       1.1  uwe 	dt->dt_year += 1900;
    192       1.1  uwe 	if (dt->dt_year < POSIX_BASE_YEAR)
    193       1.1  uwe 		dt->dt_year += 100;
    194       1.1  uwe 
    195       1.2  uwe #ifdef RTC_DEBUG
    196  1.2.34.1  mjf 	aprint_debug_dev(sc->sc_dev,
    197  1.2.34.1  mjf 			 "gettime: %04d-%02d-%02d %02d:%02d:%02d\n",
    198  1.2.34.1  mjf 			 dt->dt_year, dt->dt_mon, dt->dt_day,
    199  1.2.34.1  mjf 			 dt->dt_hour, dt->dt_min, dt->dt_sec);
    200       1.2  uwe 
    201       1.2  uwe 	if (!sc->sc_valid)
    202       1.2  uwe 		return EIO;
    203       1.2  uwe #endif
    204       1.2  uwe 
    205       1.1  uwe 	return 0;
    206       1.1  uwe }
    207       1.1  uwe 
    208       1.1  uwe 
    209       1.1  uwe static int
    210       1.1  uwe rtc_settime_ymdhms(todr_chip_handle_t h, struct clock_ymdhms *dt)
    211       1.1  uwe {
    212       1.2  uwe 	struct rtc_softc *sc = h->cookie;
    213       1.1  uwe 	unsigned int year;
    214       1.1  uwe 	uint8_t r;
    215       1.1  uwe 
    216       1.1  uwe 	year = TOBCD(dt->dt_year % 100);
    217       1.1  uwe 
    218       1.2  uwe 	r = _reg_read_1(SH_(RCR2));
    219       1.2  uwe 
    220       1.1  uwe 	/* stop clock */
    221       1.2  uwe 	_reg_write_1(SH_(RCR2), (r & ~SH_RCR2_START) | SH_RCR2_RESET);
    222       1.1  uwe 
    223       1.1  uwe 	/* set time */
    224       1.1  uwe 	if (CPU_IS_SH3)
    225       1.1  uwe 		_reg_write_1(SH3_RYRCNT, year);
    226       1.1  uwe 	else
    227       1.1  uwe 		_reg_write_2(SH4_RYRCNT, year);
    228       1.1  uwe 
    229       1.1  uwe #define	RTCSET(x, y) \
    230       1.1  uwe 	_reg_write_1(SH_(R ## x ## CNT), TOBCD(dt->dt_ ## y))
    231       1.1  uwe 
    232       1.1  uwe 	RTCSET(MON, mon);
    233       1.1  uwe 	RTCSET(WK, wday);
    234       1.1  uwe 	RTCSET(DAY, day);
    235       1.1  uwe 	RTCSET(HR, hour);
    236       1.1  uwe 	RTCSET(MIN, min);
    237       1.1  uwe 	RTCSET(SEC, sec);
    238       1.1  uwe 
    239       1.1  uwe #undef RTCSET
    240       1.1  uwe 
    241       1.1  uwe 	/* start clock */
    242       1.2  uwe 	_reg_write_1(SH_(RCR2), r);
    243       1.2  uwe 	sc->sc_valid = 1;
    244       1.2  uwe 
    245       1.2  uwe #ifdef RTC_DEBUG
    246  1.2.34.1  mjf 	aprint_debug_dev(sc->sc_dev,
    247  1.2.34.1  mjf 			 "settime: %04d-%02d-%02d %02d:%02d:%02d\n",
    248  1.2.34.1  mjf 			 dt->dt_year, dt->dt_mon, dt->dt_day,
    249  1.2.34.1  mjf 			 dt->dt_hour, dt->dt_min, dt->dt_sec);
    250       1.2  uwe #endif
    251       1.1  uwe 
    252       1.1  uwe 	return 0;
    253       1.1  uwe }
    254