Switch the rest of sh3 ports to todr(9)

Split code to handle on-chip rtc into a separate driver
Eliminate all todr code from clock.c
Move __HAVE_GENERIC_TODR to sh3/include/types.h
This commit is contained in:
uwe 2006-09-20 00:41:11 +00:00
parent ce2c68fd18
commit 57675076eb
17 changed files with 247 additions and 256 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: clock_machdep.c,v 1.6 2006/09/05 11:09:36 uwe Exp $ */
/* $NetBSD: clock_machdep.c,v 1.7 2006/09/20 00:41:12 uwe Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
@ -34,35 +34,17 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: clock_machdep.c,v 1.6 2006/09/05 11:09:36 uwe Exp $");
__KERNEL_RCSID(0, "$NetBSD: clock_machdep.c,v 1.7 2006/09/20 00:41:12 uwe Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <dev/clock_subr.h>
#include <sh3/clock.h>
#ifdef DEBUG
#define STATIC
#else
#define STATIC static
#endif
STATIC void dreamcast_rtc_init(void *);
STATIC struct rtc_ops dreamcast_rtc_ops = {
.init = dreamcast_rtc_init,
};
void
machine_clock_init(void)
{
sh_clock_init(SH_CLOCK_NORTC, &dreamcast_rtc_ops);
}
STATIC void
dreamcast_rtc_init(void *cookie)
{
sh_clock_init(SH_CLOCK_NORTC);
}

View File

@ -1,9 +1,7 @@
/* $NetBSD: types.h,v 1.3 2006/09/05 11:09:36 uwe Exp $ */
/* $NetBSD: types.h,v 1.4 2006/09/20 00:41:12 uwe Exp $ */
#ifndef _DREAMCAST_TYPES_H_
#define _DREAMCAST_TYPES_H_
#include <sh3/types.h>
#define __HAVE_GENERIC_TODR
#endif /* _DREAMCAST_TYPES_H_ */

View File

@ -1,4 +1,4 @@
# $NetBSD: COMPUTEX7750,v 1.24 2005/12/07 09:10:13 tsutsui Exp $
# $NetBSD: COMPUTEX7750,v 1.25 2006/09/20 00:41:12 uwe Exp $
#
# GENERIC -- everything that's currently supported
#
@ -66,6 +66,7 @@ config netbsd root on ? type ?
#
mainbus0 at root
shb* at mainbus?
rtc0 at shb?
# Serial Devices

View File

@ -1,4 +1,4 @@
# $NetBSD: COMPUTEXEVB,v 1.26 2005/12/07 09:10:13 tsutsui Exp $
# $NetBSD: COMPUTEXEVB,v 1.27 2006/09/20 00:41:12 uwe Exp $
#
# GENERIC -- everything that's currently supported
#
@ -65,6 +65,7 @@ config netbsd root on ? type ?
mainbus0 at root
shb* at mainbus?
rtc0 at shb?
wdog0 at shb?
options SCIFCONSOLE,SCIFCN_SPEED=115200
scif0 at shb?

View File

@ -1,4 +1,4 @@
# $NetBSD: CQREEKSH3,v 1.27 2005/12/07 09:10:13 tsutsui Exp $
# $NetBSD: CQREEKSH3,v 1.28 2006/09/20 00:41:12 uwe Exp $
#
# GENERIC -- everything that's currently supported
#
@ -63,6 +63,7 @@ config netbsd root on ? type ?
#
mainbus0 at root
shb* at mainbus?
rtc0 at shb?
wdog0 at shb?
options SCICONSOLE,SCICN_SPEED=38400
sci0 at shb?

View File

@ -1,4 +1,4 @@
# $NetBSD: KZSH401,v 1.24 2005/12/07 09:10:13 tsutsui Exp $
# $NetBSD: KZSH401,v 1.25 2006/09/20 00:41:12 uwe Exp $
#
# GENERIC -- everything that's currently supported
#
@ -63,6 +63,7 @@ config netbsd root on ? type ?
#
mainbus0 at root
shb* at mainbus?
rtc0 at shb?
options SCIFCN_SPEED=19200
scif0 at shb?
pseudo-device pty # pseudo-terminals

View File

@ -1,4 +1,4 @@
/* $NetBSD: clock_machdep.c,v 1.3 2005/12/11 12:17:13 christos Exp $ */
/* $NetBSD: clock_machdep.c,v 1.4 2006/09/20 00:41:12 uwe Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
@ -34,20 +34,21 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: clock_machdep.c,v 1.3 2005/12/11 12:17:13 christos Exp $");
__KERNEL_RCSID(0, "$NetBSD: clock_machdep.c,v 1.4 2006/09/20 00:41:12 uwe Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sh3/clock.h>
void
machine_clock_init()
machine_clock_init(void)
{
#ifdef INITTODR_ALWAYS_USE_RTC
sh_clock_init(SH_CLOCK_NOINITTODR, 0);
sh_clock_init(SH_CLOCK_NOINITTODR);
#else
sh_clock_init(0, 0);
sh_clock_init(0);
#endif
}

View File

@ -1,4 +1,4 @@
# $NetBSD: GENERIC,v 1.70 2006/08/26 20:26:45 christos Exp $
# $NetBSD: GENERIC,v 1.71 2006/09/20 00:41:13 uwe Exp $
#
# GENERIC machine description file
#
@ -217,6 +217,7 @@ wskbd* at btnmgr0 mux 1
cpu* at mainbus?
shb* at mainbus?
rtc* at shb?
adc* at shb? # analog->digital converter
pfckbd* at mainbus?

View File

@ -1,4 +1,4 @@
# $NetBSD: HPW650PA,v 1.22 2006/02/04 03:04:12 rpaulo Exp $
# $NetBSD: HPW650PA,v 1.23 2006/09/20 00:41:13 uwe Exp $
#
# HITACHI PERSONA HPW650PA
#
@ -73,6 +73,8 @@ hd64465if* at shb?
com* at hd64465if?
hd64465pcmcia* at hd64465if?
rtc* at shb?
# PCMCIA bus support
pcmcia* at hd64465pcmcia? controller ? socket ?

View File

@ -1,4 +1,4 @@
/* $NetBSD: clock_machdep.c,v 1.3 2005/12/11 12:17:36 christos Exp $ */
/* $NetBSD: clock_machdep.c,v 1.4 2006/09/20 00:41:13 uwe Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
@ -34,16 +34,17 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: clock_machdep.c,v 1.3 2005/12/11 12:17:36 christos Exp $");
__KERNEL_RCSID(0, "$NetBSD: clock_machdep.c,v 1.4 2006/09/20 00:41:13 uwe Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sh3/clock.h>
void
machine_clock_init()
machine_clock_init(void)
{
sh_clock_init(0, 0);
sh_clock_init(0);
}

View File

@ -1,10 +1,8 @@
/* $NetBSD: types.h,v 1.2 2006/09/07 01:55:03 uwe Exp $ */
/* $NetBSD: types.h,v 1.3 2006/09/20 00:41:13 uwe Exp $ */
#ifndef _LANDISK_TYPES_H_
#define _LANDISK_TYPES_H_
#include <sh3/types.h>
#define __HAVE_GENERIC_TODR
#endif /* _LANDISK_TYPES_H_ */

View File

@ -1,4 +1,4 @@
/* $NetBSD: clock_machdep.c,v 1.2 2006/09/07 01:55:03 uwe Exp $ */
/* $NetBSD: clock_machdep.c,v 1.3 2006/09/20 00:41:13 uwe Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
@ -31,28 +31,16 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: clock_machdep.c,v 1.2 2006/09/07 01:55:03 uwe Exp $");
__KERNEL_RCSID(0, "$NetBSD: clock_machdep.c,v 1.3 2006/09/20 00:41:13 uwe Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sh3/clock.h>
static void landisk_rtc_init(void *cookie);
static struct rtc_ops landisk_rtc_ops = {
.init = landisk_rtc_init,
};
void
machine_clock_init(void)
{
sh_clock_init(SH_CLOCK_NORTC, &landisk_rtc_ops);
}
static void
landisk_rtc_init(void *cookie)
{
/* DUMMY */
sh_clock_init(SH_CLOCK_NORTC);
}

View File

@ -1,4 +1,4 @@
# $NetBSD: files.shb,v 1.4 2005/12/11 12:18:58 christos Exp $
# $NetBSD: files.shb,v 1.5 2006/09/20 00:41:11 uwe Exp $
#
# SuperH internal devices.
#
@ -7,6 +7,10 @@ attach shb at mainbus
file arch/sh3/dev/shb.c shb
device rtc
attach rtc at shb
file arch/sh3/dev/rtc.c rtc needs-flag
device wdog
attach wdog at shb
file arch/sh3/dev/wdog.c wdog needs-flag

195
sys/arch/sh3/dev/rtc.c Normal file
View File

@ -0,0 +1,195 @@
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by UCHIYAMA Yasushi.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.1 2006/09/20 00:41:11 uwe Exp $");
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#ifdef GPROF
#include <sys/gmon.h>
#endif
#include <dev/clock_subr.h>
#include <sh3/rtcreg.h>
struct rtc_softc {
struct device sc_dev;
struct todr_chip_handle sc_todr;
};
static int rtc_match(struct device *, struct cfdata *, void *);
static void rtc_attach(struct device *, struct device *, void *);
CFATTACH_DECL(rtc, sizeof(struct rtc_softc),
rtc_match, rtc_attach, NULL, NULL);
/* todr(9) methods */
static int rtc_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
static int rtc_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
static int
rtc_match(struct device *parent, struct cfdata *cfp, void *aux)
{
return 1;
}
static void
rtc_attach(struct device *parent, struct device *self, void *aux)
{
struct rtc_softc *sc = (void *)self;
printf("\n");
/* Make sure RTC is started */
_reg_write_1(SH_(RCR2), SH_RCR2_ENABLE | SH_RCR2_START);
#ifdef RTC_DEBUG
{
struct clock_ymdhms dt;
int error;
error = rtc_gettime_ymdhms(NULL, &dt);
if (error)
printf("%s: error %d\n", sc->sc_dev.dv_xname, error);
else
printf("%s: %04d-%02d-%02d %02d:%02d:%02d\n",
sc->sc_dev.dv_xname,
dt.dt_year, dt.dt_mon, dt.dt_day,
dt.dt_hour, dt.dt_min, dt.dt_sec);
}
#endif
sc->sc_todr.cookie = sc;
sc->sc_todr.todr_gettime_ymdhms = rtc_gettime_ymdhms;
sc->sc_todr.todr_settime_ymdhms = rtc_settime_ymdhms;
todr_attach(&sc->sc_todr);
}
static int
rtc_gettime_ymdhms(todr_chip_handle_t h, struct clock_ymdhms *dt)
{
unsigned int year;
int retry = 8;
/* disable carry interrupt */
_reg_bclr_1(SH_(RCR1), SH_RCR1_CIE);
do {
uint8_t r = _reg_read_1(SH_(RCR1));
r &= ~SH_RCR1_CF;
r |= SH_RCR1_AF; /* don't clear alarm flag */
_reg_write_1(SH_(RCR1), r);
if (CPU_IS_SH3)
year = _reg_read_1(SH3_RYRCNT);
else
year = _reg_read_2(SH4_RYRCNT) & 0x00ff;
dt->dt_year = FROMBCD(year);
/* read counter */
#define RTCGET(x, y) \
dt->dt_ ## x = FROMBCD(_reg_read_1(SH_(R ## y ## CNT)))
RTCGET(mon, MON);
RTCGET(wday, WK);
RTCGET(day, DAY);
RTCGET(hour, HR);
RTCGET(min, MIN);
RTCGET(sec, SEC);
#undef RTCGET
} while ((_reg_read_1(SH_(RCR1)) & SH_RCR1_CF) && --retry > 0);
if (retry == 0)
return EIO;
dt->dt_year += 1900;
if (dt->dt_year < POSIX_BASE_YEAR)
dt->dt_year += 100;
return 0;
}
static int
rtc_settime_ymdhms(todr_chip_handle_t h, struct clock_ymdhms *dt)
{
unsigned int year;
uint8_t r;
year = TOBCD(dt->dt_year % 100);
/* stop clock */
r = _reg_read_1(SH_(RCR2));
r |= SH_RCR2_RESET;
r &= ~SH_RCR2_START;
_reg_write_1(SH_(RCR2), r);
/* set time */
if (CPU_IS_SH3)
_reg_write_1(SH3_RYRCNT, year);
else
_reg_write_2(SH4_RYRCNT, year);
#define RTCSET(x, y) \
_reg_write_1(SH_(R ## x ## CNT), TOBCD(dt->dt_ ## y))
RTCSET(MON, mon);
RTCSET(WK, wday);
RTCSET(DAY, day);
RTCSET(HR, hour);
RTCSET(MIN, min);
RTCSET(SEC, sec);
#undef RTCSET
/* start clock */
_reg_write_1(SH_(RCR2), r | SH_RCR2_START);
return 0;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: clock.h,v 1.2 2002/04/28 17:10:33 uch Exp $ */
/* $NetBSD: clock.h,v 1.3 2006/09/20 00:41:12 uwe Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
@ -58,32 +58,16 @@
* file, this value is estimated by sh_clock_init().
*
*/
struct rtc_ops;
struct clock_ymdhms;
#ifndef _SH3_CLOCK_H_
#define _SH3_CLOCK_H_
void sh_clock_init(int, struct rtc_ops *);
void machine_clock_init(void);
void sh_clock_init(int);
#define SH_CLOCK_NORTC 0x00000001
#define SH_CLOCK_NOINITTODR 0x00000002
void machine_clock_init(void);
int sh_clock_get_cpuclock(void);
int sh_clock_get_pclock(void);
/*
* SH RTC module interface.
*/
void sh_rtc_init(void *);
void sh_rtc_get(void *, time_t, struct clock_ymdhms *);
void sh_rtc_set(void *, struct clock_ymdhms *);
/*
* machine specific RTC ops
*/
struct clock_ymdhms;
struct rtc_ops {
void *_cookie;
void (*init)(void *);
void (*get)(void *, time_t, struct clock_ymdhms *);
void (*set)(void *, struct clock_ymdhms *);
};
#endif /* _SH3_CLOCK_H_ */

View File

@ -1,4 +1,4 @@
/* $NetBSD: types.h,v 1.22 2005/12/11 12:18:58 christos Exp $ */
/* $NetBSD: types.h,v 1.23 2006/09/20 00:41:12 uwe Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
@ -66,6 +66,7 @@ typedef volatile unsigned char __cpu_simple_lock_t;
#define __HAVE_AST_PERPROC
#define __HAVE_GENERIC_SOFT_INTERRUPTS
#define __HAVE_GENERIC_TODR
#define __HAVE_SYSCALL_INTERN
#if defined(_KERNEL)

View File

@ -1,4 +1,4 @@
/* $NetBSD: clock.c,v 1.32 2006/09/05 11:09:36 uwe Exp $ */
/* $NetBSD: clock.c,v 1.33 2006/09/20 00:41:12 uwe Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
@ -37,7 +37,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.32 2006/09/05 11:09:36 uwe Exp $");
__KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.33 2006/09/20 00:41:12 uwe Exp $");
#include "opt_pclock.h"
#include "opt_hz.h"
@ -62,7 +62,6 @@ __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.32 2006/09/05 11:09:36 uwe Exp $");
#ifndef HZ
#define HZ 64
#endif
#define MINYEAR 2002 /* "today" */
#define SH_RTC_CLOCK 16384 /* Hz */
/*
@ -79,10 +78,6 @@ struct {
uint32_t cpucycle_1us; /* calibrated loop variable (1 us) */
uint32_t tmuclk; /* source clock of TMU0 (Hz) */
/* RTC ops holder. default SH RTC module */
struct rtc_ops rtc;
int rtc_initialized;
uint32_t pclock; /* PCLOCK */
uint32_t cpuclock; /* CPU clock */
int flags;
@ -90,12 +85,6 @@ struct {
#ifdef PCLOCK
.pclock = PCLOCK,
#endif
.rtc = {
/* SH RTC module to default RTC */
.init = sh_rtc_init,
.get = sh_rtc_get,
.set = sh_rtc_set
}
};
uint32_t maxwdog;
@ -117,13 +106,11 @@ do { \
#define TMU_ELAPSED(x) \
(0xffffffff - _reg_read_4(SH_(TCNT ## x)))
void
sh_clock_init(int flags, struct rtc_ops *rtc)
sh_clock_init(int flags)
{
uint32_t s, t0, t1 __attribute__((__unused__));
sh_clock.flags = flags;
if (rtc != NULL)
sh_clock.rtc = *rtc; /* structure copy */
/* Initialize TMU */
_reg_write_2(SH_(TCR0), 0);
@ -148,6 +135,9 @@ sh_clock_init(int flags, struct rtc_ops *rtc)
_reg_write_2(SH_(TCR0),
CPU_IS_SH3 ? SH3_TCR_TPSC_RTC : SH4_TCR_TPSC_RTC);
sh_clock.tmuclk = SH_RTC_CLOCK;
/* Make sure RTC oscillator is enabled */
_reg_write_1(SH_(RCR2), SH_RCR2_ENABLE);
}
s = _cpu_exception_suspend();
@ -292,86 +282,9 @@ cpu_initclocks()
_reg_write_4(SH_(TCOR1), 0xffffffff);
_reg_write_2(SH_(TCR2), TCR_UNIE | TCR_TPSC_P4);
_reg_write_4(SH_(TCOR2), 0xffffffff);
/* Make sure to start RTC */
sh_clock.rtc.init(sh_clock.rtc._cookie);
}
#if !defined(__HAVE_GENERIC_TODR)
void
inittodr(time_t base)
{
struct clock_ymdhms dt;
time_t rtc;
int s;
if (!sh_clock.rtc_initialized)
sh_clock.rtc_initialized = 1;
sh_clock.rtc.get(sh_clock.rtc._cookie, base, &dt);
rtc = clock_ymdhms_to_secs(&dt);
#ifdef DEBUG
printf("inittodr: %d/%d/%d/%d/%d/%d(%d)\n", dt.dt_year,
dt.dt_mon, dt.dt_day, dt.dt_hour, dt.dt_min, dt.dt_sec,
dt.dt_wday);
#endif
if (!(sh_clock.flags & SH_CLOCK_NOINITTODR) &&
(rtc < base ||
dt.dt_year < MINYEAR || dt.dt_year > 2037 ||
dt.dt_mon < 1 || dt.dt_mon > 12 ||
dt.dt_wday > 6 ||
dt.dt_day < 1 || dt.dt_day > 31 ||
dt.dt_hour > 23 || dt.dt_min > 59 || dt.dt_sec > 59)) {
/*
* Believe the time in the file system for lack of
* anything better, resetting the RTC.
*/
s = splclock();
time.tv_sec = base;
time.tv_usec = 0;
splx(s);
printf("WARNING: preposterous clock chip time\n");
resettodr();
printf(" -- CHECK AND RESET THE DATE!\n");
return;
}
s = splclock();
time.tv_sec = rtc + rtc_offset * 60;
time.tv_usec = 0;
splx(s);
return;
}
void
resettodr()
{
struct clock_ymdhms dt;
int s;
if (!sh_clock.rtc_initialized)
return;
s = splclock();
clock_secs_to_ymdhms(time.tv_sec - rtc_offset * 60, &dt);
splx(s);
sh_clock.rtc.set(sh_clock.rtc._cookie, &dt);
#ifdef DEBUG
printf("%s: %d/%d/%d/%d/%d/%d(%d) rtc_offset %d\n", __FUNCTION__,
dt.dt_year, dt.dt_mon, dt.dt_day, dt.dt_hour, dt.dt_min, dt.dt_sec,
dt.dt_wday, rtc_offset);
#endif
}
#endif /* __HAVE_GENERIC_TODR */
#ifdef SH3
int
sh3_clock_intr(void *arg) /* trap frame */
@ -412,84 +325,3 @@ sh4_clock_intr(void *arg) /* trap frame */
return (1);
}
#endif /* SH4 */
/*
* SH3 RTC module ops.
*/
void
sh_rtc_init(void *cookie)
{
/* Make sure to start RTC */
_reg_write_1(SH_(RCR2), SH_RCR2_ENABLE | SH_RCR2_START);
}
void
sh_rtc_get(void *cookie, time_t base, struct clock_ymdhms *dt)
{
int retry = 8;
/* disable carry interrupt */
_reg_bclr_1(SH_(RCR1), SH_RCR1_CIE);
do {
uint8_t r = _reg_read_1(SH_(RCR1));
r &= ~SH_RCR1_CF;
r |= SH_RCR1_AF; /* don't clear alarm flag */
_reg_write_1(SH_(RCR1), r);
if (CPU_IS_SH3)
dt->dt_year = FROMBCD(_reg_read_1(SH3_RYRCNT));
else
dt->dt_year = FROMBCD(_reg_read_2(SH4_RYRCNT) & 0x00ff);
/* read counter */
#define RTCGET(x, y) dt->dt_ ## x = FROMBCD(_reg_read_1(SH_(R ## y ## CNT)))
RTCGET(mon, MON);
RTCGET(wday, WK);
RTCGET(day, DAY);
RTCGET(hour, HR);
RTCGET(min, MIN);
RTCGET(sec, SEC);
#undef RTCGET
} while ((_reg_read_1(SH_(RCR1)) & SH_RCR1_CF) && --retry > 0);
if (retry == 0) {
printf("rtc_gettime: couldn't read RTC register.\n");
memset(dt, 0, sizeof(*dt));
return;
}
dt->dt_year = (dt->dt_year % 100) + 1900;
if (dt->dt_year < 1970)
dt->dt_year += 100;
}
void
sh_rtc_set(void *cookie, struct clock_ymdhms *dt)
{
uint8_t r;
/* stop clock */
r = _reg_read_1(SH_(RCR2));
r |= SH_RCR2_RESET;
r &= ~SH_RCR2_START;
_reg_write_1(SH_(RCR2), r);
/* set time */
if (CPU_IS_SH3)
_reg_write_1(SH3_RYRCNT, TOBCD(dt->dt_year % 100));
else
_reg_write_2(SH4_RYRCNT, TOBCD(dt->dt_year % 100));
#define RTCSET(x, y) _reg_write_1(SH_(R ## x ## CNT), TOBCD(dt->dt_ ## y))
RTCSET(MON, mon);
RTCSET(WK, wday);
RTCSET(DAY, day);
RTCSET(HR, hour);
RTCSET(MIN, min);
RTCSET(SEC, sec);
#undef RTCSET
/* start clock */
_reg_write_1(SH_(RCR2), r | SH_RCR2_START);
}