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