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