xref: /OK3568_Linux_fs/kernel/arch/sh/boards/mach-dreamcast/rtc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * arch/sh/boards/dreamcast/rtc.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Dreamcast AICA RTC routines.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (c) 2001, 2002 M. R. Brown <mrbrown@0xd6.org>
8*4882a593Smuzhiyun  * Copyright (c) 2002 Paul Mundt <lethal@chaoticdreams.org>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/time.h>
12*4882a593Smuzhiyun #include <linux/rtc.h>
13*4882a593Smuzhiyun #include <linux/io.h>
14*4882a593Smuzhiyun #include <linux/platform_device.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun /* The AICA RTC has an Epoch of 1/1/1950, so we must subtract 20 years (in
17*4882a593Smuzhiyun    seconds) to get the standard Unix Epoch when getting the time, and add
18*4882a593Smuzhiyun    20 years when setting the time. */
19*4882a593Smuzhiyun #define TWENTY_YEARS ((20 * 365LU + 5) * 86400)
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /* The AICA RTC is represented by a 32-bit seconds counter stored in 2 16-bit
22*4882a593Smuzhiyun    registers.*/
23*4882a593Smuzhiyun #define AICA_RTC_SECS_H		0xa0710000
24*4882a593Smuzhiyun #define AICA_RTC_SECS_L		0xa0710004
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /**
27*4882a593Smuzhiyun  * aica_rtc_gettimeofday - Get the time from the AICA RTC
28*4882a593Smuzhiyun  * @dev: the RTC device (ignored)
29*4882a593Smuzhiyun  * @tm: pointer to resulting RTC time structure
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * Grabs the current RTC seconds counter and adjusts it to the Unix Epoch.
32*4882a593Smuzhiyun  */
aica_rtc_gettimeofday(struct device * dev,struct rtc_time * tm)33*4882a593Smuzhiyun static int aica_rtc_gettimeofday(struct device *dev, struct rtc_time *tm)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun 	unsigned long val1, val2;
36*4882a593Smuzhiyun 	time64_t t;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	do {
39*4882a593Smuzhiyun 		val1 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
40*4882a593Smuzhiyun 			(__raw_readl(AICA_RTC_SECS_L) & 0xffff);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 		val2 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
43*4882a593Smuzhiyun 			(__raw_readl(AICA_RTC_SECS_L) & 0xffff);
44*4882a593Smuzhiyun 	} while (val1 != val2);
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	/* normalize to 1970..2106 time range */
47*4882a593Smuzhiyun 	t = (u32)(val1 - TWENTY_YEARS);
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	rtc_time64_to_tm(t, tm);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	return 0;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /**
55*4882a593Smuzhiyun  * aica_rtc_settimeofday - Set the AICA RTC to the current time
56*4882a593Smuzhiyun  * @dev: the RTC device (ignored)
57*4882a593Smuzhiyun  * @tm: pointer to new RTC time structure
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * Adjusts the given @tv to the AICA Epoch and sets the RTC seconds counter.
60*4882a593Smuzhiyun  */
aica_rtc_settimeofday(struct device * dev,struct rtc_time * tm)61*4882a593Smuzhiyun static int aica_rtc_settimeofday(struct device *dev, struct rtc_time *tm)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	unsigned long val1, val2;
64*4882a593Smuzhiyun 	time64_t secs = rtc_tm_to_time64(tm);
65*4882a593Smuzhiyun 	u32 adj = secs + TWENTY_YEARS;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	do {
68*4882a593Smuzhiyun 		__raw_writel((adj & 0xffff0000) >> 16, AICA_RTC_SECS_H);
69*4882a593Smuzhiyun 		__raw_writel((adj & 0xffff), AICA_RTC_SECS_L);
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 		val1 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
72*4882a593Smuzhiyun 			(__raw_readl(AICA_RTC_SECS_L) & 0xffff);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 		val2 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
75*4882a593Smuzhiyun 			(__raw_readl(AICA_RTC_SECS_L) & 0xffff);
76*4882a593Smuzhiyun 	} while (val1 != val2);
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	return 0;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun static const struct rtc_class_ops rtc_generic_ops = {
82*4882a593Smuzhiyun 	.read_time = aica_rtc_gettimeofday,
83*4882a593Smuzhiyun 	.set_time = aica_rtc_settimeofday,
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun 
aica_time_init(void)86*4882a593Smuzhiyun static int __init aica_time_init(void)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	struct platform_device *pdev;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	pdev = platform_device_register_data(NULL, "rtc-generic", -1,
91*4882a593Smuzhiyun 					     &rtc_generic_ops,
92*4882a593Smuzhiyun 					     sizeof(rtc_generic_ops));
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	return PTR_ERR_OR_ZERO(pdev);
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun arch_initcall(aica_time_init);
97