1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/arch/alpha/kernel/rtc.c 4 * 5 * Copyright (C) 1991, 1992, 1995, 1999, 2000 Linus Torvalds 6 * 7 * This file contains date handling. 8 */ 9 #include <linux/errno.h> 10 #include <linux/init.h> 11 #include <linux/kernel.h> 12 #include <linux/param.h> 13 #include <linux/string.h> 14 #include <linux/mc146818rtc.h> 15 #include <linux/bcd.h> 16 #include <linux/rtc.h> 17 #include <linux/platform_device.h> 18 #include <linux/workqueue.h> 19 20 #include "proto.h" 21 22 23 /* 24 * Support for the RTC device. 25 * 26 * We don't want to use the rtc-cmos driver, because we don't want to support 27 * alarms, as that would be indistinguishable from timer interrupts. 28 * 29 * Further, generic code is really, really tied to a 1900 epoch. This is 30 * true in __get_rtc_time as well as the users of struct rtc_time e.g. 31 * rtc_tm_to_time. Thankfully all of the other epochs in use are later 32 * than 1900, and so it's easy to adjust. 33 */ 34 35 static unsigned long rtc_epoch; 36 37 static int __init 38 specifiy_epoch(char *str) 39 { 40 unsigned long epoch = simple_strtoul(str, NULL, 0); 41 if (epoch < 1900) 42 printk("Ignoring invalid user specified epoch %lu\n", epoch); 43 else 44 rtc_epoch = epoch; 45 return 1; 46 } 47 __setup("epoch=", specifiy_epoch); 48 49 static void __init 50 init_rtc_epoch(void) 51 { 52 int epoch, year, ctrl; 53 54 if (rtc_epoch != 0) { 55 /* The epoch was specified on the command-line. */ 56 return; 57 } 58 59 /* Detect the epoch in use on this computer. */ 60 ctrl = CMOS_READ(RTC_CONTROL); 61 year = CMOS_READ(RTC_YEAR); 62 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) 63 year = bcd2bin(year); 64 65 /* PC-like is standard; used for year >= 70 */ 66 epoch = 1900; 67 if (year < 20) { 68 epoch = 2000; 69 } else if (year >= 20 && year < 48) { 70 /* NT epoch */ 71 epoch = 1980; 72 } else if (year >= 48 && year < 70) { 73 /* Digital UNIX epoch */ 74 epoch = 1952; 75 } 76 rtc_epoch = epoch; 77 78 printk(KERN_INFO "Using epoch %d for rtc year %d\n", epoch, year); 79 } 80 81 static int 82 alpha_rtc_read_time(struct device *dev, struct rtc_time *tm) 83 { 84 int ret = mc146818_get_time(tm, 10); 85 86 if (ret < 0) { 87 dev_err_ratelimited(dev, "unable to read current time\n"); 88 return ret; 89 } 90 91 /* Adjust for non-default epochs. It's easier to depend on the 92 generic __get_rtc_time and adjust the epoch here than create 93 a copy of __get_rtc_time with the edits we need. */ 94 if (rtc_epoch != 1900) { 95 int year = tm->tm_year; 96 /* Undo the century adjustment made in __get_rtc_time. */ 97 if (year >= 100) 98 year -= 100; 99 year += rtc_epoch - 1900; 100 /* Redo the century adjustment with the epoch in place. */ 101 if (year <= 69) 102 year += 100; 103 tm->tm_year = year; 104 } 105 106 return 0; 107 } 108 109 static int 110 alpha_rtc_set_time(struct device *dev, struct rtc_time *tm) 111 { 112 struct rtc_time xtm; 113 114 if (rtc_epoch != 1900) { 115 xtm = *tm; 116 xtm.tm_year -= rtc_epoch - 1900; 117 tm = &xtm; 118 } 119 120 return mc146818_set_time(tm); 121 } 122 123 static int 124 alpha_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 125 { 126 switch (cmd) { 127 case RTC_EPOCH_READ: 128 return put_user(rtc_epoch, (unsigned long __user *)arg); 129 case RTC_EPOCH_SET: 130 if (arg < 1900) 131 return -EINVAL; 132 rtc_epoch = arg; 133 return 0; 134 default: 135 return -ENOIOCTLCMD; 136 } 137 } 138 139 static const struct rtc_class_ops alpha_rtc_ops = { 140 .read_time = alpha_rtc_read_time, 141 .set_time = alpha_rtc_set_time, 142 .ioctl = alpha_rtc_ioctl, 143 }; 144 145 /* 146 * Similarly, except do the actual CMOS access on the boot cpu only. The 147 * access polls for the RTC update cycle and takes rtc_lock, so run it in a 148 * worker on that cpu rather than from an interprocessor interrupt. 149 */ 150 151 #if defined(CONFIG_SMP) && \ 152 (defined(CONFIG_ALPHA_GENERIC) || defined(CONFIG_ALPHA_MARVEL)) 153 # define HAVE_REMOTE_RTC 1 154 155 static long 156 do_remote_read(void *data) 157 { 158 return alpha_rtc_read_time(NULL, data); 159 } 160 161 static int 162 remote_read_time(struct device *dev, struct rtc_time *tm) 163 { 164 if (smp_processor_id() != boot_cpuid) 165 return work_on_cpu(boot_cpuid, do_remote_read, tm); 166 return alpha_rtc_read_time(NULL, tm); 167 } 168 169 static long 170 do_remote_set(void *data) 171 { 172 return alpha_rtc_set_time(NULL, data); 173 } 174 175 static int 176 remote_set_time(struct device *dev, struct rtc_time *tm) 177 { 178 if (smp_processor_id() != boot_cpuid) 179 return work_on_cpu(boot_cpuid, do_remote_set, tm); 180 return alpha_rtc_set_time(NULL, tm); 181 } 182 183 static const struct rtc_class_ops remote_rtc_ops = { 184 .read_time = remote_read_time, 185 .set_time = remote_set_time, 186 .ioctl = alpha_rtc_ioctl, 187 }; 188 #endif 189 190 static int __init 191 alpha_rtc_init(void) 192 { 193 struct platform_device *pdev; 194 struct rtc_device *rtc; 195 196 init_rtc_epoch(); 197 198 pdev = platform_device_register_simple("rtc-alpha", -1, NULL, 0); 199 rtc = devm_rtc_allocate_device(&pdev->dev); 200 if (IS_ERR(rtc)) 201 return PTR_ERR(rtc); 202 203 platform_set_drvdata(pdev, rtc); 204 rtc->ops = &alpha_rtc_ops; 205 206 #ifdef HAVE_REMOTE_RTC 207 if (alpha_mv.rtc_boot_cpu_only) 208 rtc->ops = &remote_rtc_ops; 209 #endif 210 211 return devm_rtc_register_device(rtc); 212 } 213 device_initcall(alpha_rtc_init); 214