rtc.c 2.0 KB
Newer Older
P
Paul Mundt 已提交
1 2
/*
 * arch/sh/boards/dreamcast/rtc.c
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13
 *
 * Dreamcast AICA RTC routines.
 *
 * Copyright (c) 2001, 2002 M. R. Brown <mrbrown@0xd6.org>
 * Copyright (c) 2002 Paul Mundt <lethal@chaoticdreams.org>
 *
 * Released under the terms of the GNU GPL v2.0.
 *
 */

#include <linux/time.h>
P
Paul Mundt 已提交
14
#include <asm/rtc.h>
L
Linus Torvalds 已提交
15 16 17
#include <asm/io.h>

/* The AICA RTC has an Epoch of 1/1/1950, so we must subtract 20 years (in
P
Paul Mundt 已提交
18 19
   seconds) to get the standard Unix Epoch when getting the time, and add
   20 years when setting the time. */
L
Linus Torvalds 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32
#define TWENTY_YEARS ((20 * 365LU + 5) * 86400)

/* The AICA RTC is represented by a 32-bit seconds counter stored in 2 16-bit
   registers.*/
#define AICA_RTC_SECS_H		0xa0710000
#define AICA_RTC_SECS_L		0xa0710004

/**
 * aica_rtc_gettimeofday - Get the time from the AICA RTC
 * @ts: pointer to resulting timespec
 *
 * Grabs the current RTC seconds counter and adjusts it to the Unix Epoch.
 */
P
Paul Mundt 已提交
33 34
void aica_rtc_gettimeofday(struct timespec *ts)
{
L
Linus Torvalds 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
	unsigned long val1, val2;

	do {
		val1 = ((ctrl_inl(AICA_RTC_SECS_H) & 0xffff) << 16) |
			(ctrl_inl(AICA_RTC_SECS_L) & 0xffff);

		val2 = ((ctrl_inl(AICA_RTC_SECS_H) & 0xffff) << 16) |
			(ctrl_inl(AICA_RTC_SECS_L) & 0xffff);
	} while (val1 != val2);

	ts->tv_sec = val1 - TWENTY_YEARS;

	/* Can't get nanoseconds with just a seconds counter. */
	ts->tv_nsec = 0;
}

/**
 * aica_rtc_settimeofday - Set the AICA RTC to the current time
 * @secs: contains the time_t to set
 *
 * Adjusts the given @tv to the AICA Epoch and sets the RTC seconds counter.
 */
P
Paul Mundt 已提交
57 58
int aica_rtc_settimeofday(const time_t secs)
{
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	unsigned long val1, val2;
	unsigned long adj = secs + TWENTY_YEARS;

	do {
		ctrl_outl((adj & 0xffff0000) >> 16, AICA_RTC_SECS_H);
		ctrl_outl((adj & 0xffff), AICA_RTC_SECS_L);

		val1 = ((ctrl_inl(AICA_RTC_SECS_H) & 0xffff) << 16) |
			(ctrl_inl(AICA_RTC_SECS_L) & 0xffff);

		val2 = ((ctrl_inl(AICA_RTC_SECS_H) & 0xffff) << 16) |
			(ctrl_inl(AICA_RTC_SECS_L) & 0xffff);
	} while (val1 != val2);

	return 0;
}

void aica_time_init(void)
{
78 79
	rtc_sh_get_time = aica_rtc_gettimeofday;
	rtc_sh_set_time = aica_rtc_settimeofday;
L
Linus Torvalds 已提交
80 81
}