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