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