xref: /openbmc/linux/arch/sh/boards/mach-dreamcast/rtc.c (revision 9d56dd3b083a3bec56e9da35ce07baca81030b03)
1da2014a2SPaul Mundt /*
2da2014a2SPaul Mundt  * arch/sh/boards/dreamcast/rtc.c
3da2014a2SPaul Mundt  *
4da2014a2SPaul Mundt  * Dreamcast AICA RTC routines.
5da2014a2SPaul Mundt  *
6da2014a2SPaul Mundt  * Copyright (c) 2001, 2002 M. R. Brown <mrbrown@0xd6.org>
7da2014a2SPaul Mundt  * Copyright (c) 2002 Paul Mundt <lethal@chaoticdreams.org>
8da2014a2SPaul Mundt  *
9da2014a2SPaul Mundt  * Released under the terms of the GNU GPL v2.0.
10da2014a2SPaul Mundt  *
11da2014a2SPaul Mundt  */
12da2014a2SPaul Mundt 
13da2014a2SPaul Mundt #include <linux/time.h>
14da2014a2SPaul Mundt #include <asm/rtc.h>
15da2014a2SPaul Mundt #include <asm/io.h>
16da2014a2SPaul Mundt 
17da2014a2SPaul Mundt /* The AICA RTC has an Epoch of 1/1/1950, so we must subtract 20 years (in
18da2014a2SPaul Mundt    seconds) to get the standard Unix Epoch when getting the time, and add
19da2014a2SPaul Mundt    20 years when setting the time. */
20da2014a2SPaul Mundt #define TWENTY_YEARS ((20 * 365LU + 5) * 86400)
21da2014a2SPaul Mundt 
22da2014a2SPaul Mundt /* The AICA RTC is represented by a 32-bit seconds counter stored in 2 16-bit
23da2014a2SPaul Mundt    registers.*/
24da2014a2SPaul Mundt #define AICA_RTC_SECS_H		0xa0710000
25da2014a2SPaul Mundt #define AICA_RTC_SECS_L		0xa0710004
26da2014a2SPaul Mundt 
27da2014a2SPaul Mundt /**
28da2014a2SPaul Mundt  * aica_rtc_gettimeofday - Get the time from the AICA RTC
29da2014a2SPaul Mundt  * @ts: pointer to resulting timespec
30da2014a2SPaul Mundt  *
31da2014a2SPaul Mundt  * Grabs the current RTC seconds counter and adjusts it to the Unix Epoch.
32da2014a2SPaul Mundt  */
33da2014a2SPaul Mundt static void aica_rtc_gettimeofday(struct timespec *ts)
34da2014a2SPaul Mundt {
35da2014a2SPaul Mundt 	unsigned long val1, val2;
36da2014a2SPaul Mundt 
37da2014a2SPaul Mundt 	do {
38*9d56dd3bSPaul Mundt 		val1 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
39*9d56dd3bSPaul Mundt 			(__raw_readl(AICA_RTC_SECS_L) & 0xffff);
40da2014a2SPaul Mundt 
41*9d56dd3bSPaul Mundt 		val2 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
42*9d56dd3bSPaul Mundt 			(__raw_readl(AICA_RTC_SECS_L) & 0xffff);
43da2014a2SPaul Mundt 	} while (val1 != val2);
44da2014a2SPaul Mundt 
45da2014a2SPaul Mundt 	ts->tv_sec = val1 - TWENTY_YEARS;
46da2014a2SPaul Mundt 
47da2014a2SPaul Mundt 	/* Can't get nanoseconds with just a seconds counter. */
48da2014a2SPaul Mundt 	ts->tv_nsec = 0;
49da2014a2SPaul Mundt }
50da2014a2SPaul Mundt 
51da2014a2SPaul Mundt /**
52da2014a2SPaul Mundt  * aica_rtc_settimeofday - Set the AICA RTC to the current time
53da2014a2SPaul Mundt  * @secs: contains the time_t to set
54da2014a2SPaul Mundt  *
55da2014a2SPaul Mundt  * Adjusts the given @tv to the AICA Epoch and sets the RTC seconds counter.
56da2014a2SPaul Mundt  */
57da2014a2SPaul Mundt static int aica_rtc_settimeofday(const time_t secs)
58da2014a2SPaul Mundt {
59da2014a2SPaul Mundt 	unsigned long val1, val2;
60da2014a2SPaul Mundt 	unsigned long adj = secs + TWENTY_YEARS;
61da2014a2SPaul Mundt 
62da2014a2SPaul Mundt 	do {
63*9d56dd3bSPaul Mundt 		__raw_writel((adj & 0xffff0000) >> 16, AICA_RTC_SECS_H);
64*9d56dd3bSPaul Mundt 		__raw_writel((adj & 0xffff), AICA_RTC_SECS_L);
65da2014a2SPaul Mundt 
66*9d56dd3bSPaul Mundt 		val1 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
67*9d56dd3bSPaul Mundt 			(__raw_readl(AICA_RTC_SECS_L) & 0xffff);
68da2014a2SPaul Mundt 
69*9d56dd3bSPaul Mundt 		val2 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
70*9d56dd3bSPaul Mundt 			(__raw_readl(AICA_RTC_SECS_L) & 0xffff);
71da2014a2SPaul Mundt 	} while (val1 != val2);
72da2014a2SPaul Mundt 
73da2014a2SPaul Mundt 	return 0;
74da2014a2SPaul Mundt }
75da2014a2SPaul Mundt 
76da2014a2SPaul Mundt void aica_time_init(void)
77da2014a2SPaul Mundt {
78da2014a2SPaul Mundt 	rtc_sh_get_time = aica_rtc_gettimeofday;
79da2014a2SPaul Mundt 	rtc_sh_set_time = aica_rtc_settimeofday;
80da2014a2SPaul Mundt }
81da2014a2SPaul Mundt 
82