mirror of
https://git.proxmox.com/git/mirror_ubuntu-kernels.git
synced 2026-01-04 16:21:11 +00:00
There are functions in drivers/rtc/rtc-mc146818-lib.c that handle
reading from / writing to the CMOS RTC clock. mach_get_cmos_time() in
arch/x86/kernel/rtc.c did not use them and was mostly a duplicate of
mc146818_get_time(). Modify mach_get_cmos_time() to use
mc146818_get_time() and remove the duplicated functionality.
mach_get_cmos_time() used a different algorithm than
mc146818_get_time(), but these functions are equivalent. The major
differences are:
- mc146818_get_time() is better refined and handles various edge
conditions,
- when the UIP ("Update in progress") bit of the RTC is set,
mach_get_cmos_time() was busy waiting with cpu_relax() while
mc146818_get_time() is using mdelay(1) in every loop iteration.
(However, there is my commit merged for Linux 5.20 / 6.0 to decrease
this period to 100us:
commit d2a632a8a1 ("rtc: mc146818-lib: reduce RTC_UIP polling period")
),
- mach_get_cmos_time() assumed that the RTC year is >= 2000, which
may not be true on some old boxes with a dead battery,
- mach_get_cmos_time() was holding the rtc_lock for a long time
and could hang if the RTC is broken or not present.
The RTC writing counterpart, mach_set_rtc_mmss() is already using
mc146818_get_time() from drivers/rtc. This was done in
commit 3195ef59cb ("x86: Do full rtc synchronization with ntp")
It appears that mach_get_cmos_time() was simply forgotten.
mach_get_cmos_time() is really used only in read_persistent_clock64(),
which is called only in a few places in kernel/time/timekeeping.c .
[ mingo: These changes are not supposed to change behavior, but they are
not identity transformations either, as mc146818_get_time() is a
better but different implementation of the same logic - so
regressions are possible in principle. ]
Signed-off-by: Mateusz Jończyk <mat.jonczyk@o2.pl>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Link: https://lore.kernel.org/r/20220813131034.768527-1-mat.jonczyk@o2.pl
163 lines
3.6 KiB
C
163 lines
3.6 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* RTC related functions
|
|
*/
|
|
#include <linux/platform_device.h>
|
|
#include <linux/mc146818rtc.h>
|
|
#include <linux/export.h>
|
|
#include <linux/pnp.h>
|
|
|
|
#include <asm/vsyscall.h>
|
|
#include <asm/x86_init.h>
|
|
#include <asm/time.h>
|
|
#include <asm/intel-mid.h>
|
|
#include <asm/setup.h>
|
|
|
|
#ifdef CONFIG_X86_32
|
|
/*
|
|
* This is a special lock that is owned by the CPU and holds the index
|
|
* register we are working with. It is required for NMI access to the
|
|
* CMOS/RTC registers. See arch/x86/include/asm/mc146818rtc.h for details.
|
|
*/
|
|
volatile unsigned long cmos_lock;
|
|
EXPORT_SYMBOL(cmos_lock);
|
|
#endif /* CONFIG_X86_32 */
|
|
|
|
DEFINE_SPINLOCK(rtc_lock);
|
|
EXPORT_SYMBOL(rtc_lock);
|
|
|
|
/*
|
|
* In order to set the CMOS clock precisely, set_rtc_mmss has to be
|
|
* called 500 ms after the second nowtime has started, because when
|
|
* nowtime is written into the registers of the CMOS clock, it will
|
|
* jump to the next second precisely 500 ms later. Check the Motorola
|
|
* MC146818A or Dallas DS12887 data sheet for details.
|
|
*/
|
|
int mach_set_rtc_mmss(const struct timespec64 *now)
|
|
{
|
|
unsigned long long nowtime = now->tv_sec;
|
|
struct rtc_time tm;
|
|
int retval = 0;
|
|
|
|
rtc_time64_to_tm(nowtime, &tm);
|
|
if (!rtc_valid_tm(&tm)) {
|
|
retval = mc146818_set_time(&tm);
|
|
if (retval)
|
|
printk(KERN_ERR "%s: RTC write failed with error %d\n",
|
|
__func__, retval);
|
|
} else {
|
|
printk(KERN_ERR
|
|
"%s: Invalid RTC value: write of %llx to RTC failed\n",
|
|
__func__, nowtime);
|
|
retval = -EINVAL;
|
|
}
|
|
return retval;
|
|
}
|
|
|
|
void mach_get_cmos_time(struct timespec64 *now)
|
|
{
|
|
struct rtc_time tm;
|
|
|
|
/*
|
|
* If pm_trace abused the RTC as storage, set the timespec to 0,
|
|
* which tells the caller that this RTC value is unusable.
|
|
*/
|
|
if (!pm_trace_rtc_valid()) {
|
|
now->tv_sec = now->tv_nsec = 0;
|
|
return;
|
|
}
|
|
|
|
if (mc146818_get_time(&tm)) {
|
|
pr_err("Unable to read current time from RTC\n");
|
|
now->tv_sec = now->tv_nsec = 0;
|
|
return;
|
|
}
|
|
|
|
now->tv_sec = rtc_tm_to_time64(&tm);
|
|
now->tv_nsec = 0;
|
|
}
|
|
|
|
/* Routines for accessing the CMOS RAM/RTC. */
|
|
unsigned char rtc_cmos_read(unsigned char addr)
|
|
{
|
|
unsigned char val;
|
|
|
|
lock_cmos_prefix(addr);
|
|
outb(addr, RTC_PORT(0));
|
|
val = inb(RTC_PORT(1));
|
|
lock_cmos_suffix(addr);
|
|
|
|
return val;
|
|
}
|
|
EXPORT_SYMBOL(rtc_cmos_read);
|
|
|
|
void rtc_cmos_write(unsigned char val, unsigned char addr)
|
|
{
|
|
lock_cmos_prefix(addr);
|
|
outb(addr, RTC_PORT(0));
|
|
outb(val, RTC_PORT(1));
|
|
lock_cmos_suffix(addr);
|
|
}
|
|
EXPORT_SYMBOL(rtc_cmos_write);
|
|
|
|
int update_persistent_clock64(struct timespec64 now)
|
|
{
|
|
return x86_platform.set_wallclock(&now);
|
|
}
|
|
|
|
/* not static: needed by APM */
|
|
void read_persistent_clock64(struct timespec64 *ts)
|
|
{
|
|
x86_platform.get_wallclock(ts);
|
|
}
|
|
|
|
|
|
static struct resource rtc_resources[] = {
|
|
[0] = {
|
|
.start = RTC_PORT(0),
|
|
.end = RTC_PORT(1),
|
|
.flags = IORESOURCE_IO,
|
|
},
|
|
[1] = {
|
|
.start = RTC_IRQ,
|
|
.end = RTC_IRQ,
|
|
.flags = IORESOURCE_IRQ,
|
|
}
|
|
};
|
|
|
|
static struct platform_device rtc_device = {
|
|
.name = "rtc_cmos",
|
|
.id = -1,
|
|
.resource = rtc_resources,
|
|
.num_resources = ARRAY_SIZE(rtc_resources),
|
|
};
|
|
|
|
static __init int add_rtc_cmos(void)
|
|
{
|
|
#ifdef CONFIG_PNP
|
|
static const char * const ids[] __initconst =
|
|
{ "PNP0b00", "PNP0b01", "PNP0b02", };
|
|
struct pnp_dev *dev;
|
|
struct pnp_id *id;
|
|
int i;
|
|
|
|
pnp_for_each_dev(dev) {
|
|
for (id = dev->id; id; id = id->next) {
|
|
for (i = 0; i < ARRAY_SIZE(ids); i++) {
|
|
if (compare_pnp_id(id, ids[i]) != 0)
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
if (!x86_platform.legacy.rtc)
|
|
return -ENODEV;
|
|
|
|
platform_device_register(&rtc_device);
|
|
dev_info(&rtc_device.dev,
|
|
"registered platform RTC device (no PNP device found)\n");
|
|
|
|
return 0;
|
|
}
|
|
device_initcall(add_rtc_cmos);
|