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