Skip to content

Commit f8475a6

Browse files
committed
Merge tag 'x86_timers_for_v6.1_rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 RTC cleanups from Borislav Petkov: - Cleanup x86/rtc.c and delete duplicated functionality in favor of using the respective functionality from the RTC library * tag 'x86_timers_for_v6.1_rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/rtc: Rename mach_set_rtc_mmss() to mach_set_cmos_time() x86/rtc: Rewrite & simplify mach_get_cmos_time() by deleting duplicated functionality
2 parents 3339914 + e1a6bc7 commit f8475a6

File tree

3 files changed

+11
-56
lines changed

3 files changed

+11
-56
lines changed

arch/x86/include/asm/mc146818rtc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static inline unsigned char current_lock_cmos_reg(void)
9595
unsigned char rtc_cmos_read(unsigned char addr);
9696
void rtc_cmos_write(unsigned char val, unsigned char addr);
9797

98-
extern int mach_set_rtc_mmss(const struct timespec64 *now);
98+
extern int mach_set_cmos_time(const struct timespec64 *now);
9999
extern void mach_get_cmos_time(struct timespec64 *now);
100100

101101
#define RTC_IRQ 8

arch/x86/kernel/rtc.c

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
*/
55
#include <linux/platform_device.h>
66
#include <linux/mc146818rtc.h>
7-
#include <linux/acpi.h>
8-
#include <linux/bcd.h>
97
#include <linux/export.h>
108
#include <linux/pnp.h>
11-
#include <linux/of.h>
129

1310
#include <asm/vsyscall.h>
1411
#include <asm/x86_init.h>
@@ -20,26 +17,23 @@
2017
/*
2118
* This is a special lock that is owned by the CPU and holds the index
2219
* register we are working with. It is required for NMI access to the
23-
* CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details.
20+
* CMOS/RTC registers. See arch/x86/include/asm/mc146818rtc.h for details.
2421
*/
2522
volatile unsigned long cmos_lock;
2623
EXPORT_SYMBOL(cmos_lock);
2724
#endif /* CONFIG_X86_32 */
2825

29-
/* For two digit years assume time is always after that */
30-
#define CMOS_YEARS_OFFS 2000
31-
3226
DEFINE_SPINLOCK(rtc_lock);
3327
EXPORT_SYMBOL(rtc_lock);
3428

3529
/*
36-
* In order to set the CMOS clock precisely, set_rtc_mmss has to be
30+
* In order to set the CMOS clock precisely, mach_set_cmos_time has to be
3731
* called 500 ms after the second nowtime has started, because when
3832
* nowtime is written into the registers of the CMOS clock, it will
3933
* jump to the next second precisely 500 ms later. Check the Motorola
4034
* MC146818A or Dallas DS12887 data sheet for details.
4135
*/
42-
int mach_set_rtc_mmss(const struct timespec64 *now)
36+
int mach_set_cmos_time(const struct timespec64 *now)
4337
{
4438
unsigned long long nowtime = now->tv_sec;
4539
struct rtc_time tm;
@@ -62,8 +56,7 @@ int mach_set_rtc_mmss(const struct timespec64 *now)
6256

6357
void mach_get_cmos_time(struct timespec64 *now)
6458
{
65-
unsigned int status, year, mon, day, hour, min, sec, century = 0;
66-
unsigned long flags;
59+
struct rtc_time tm;
6760

6861
/*
6962
* If pm_trace abused the RTC as storage, set the timespec to 0,
@@ -74,51 +67,13 @@ void mach_get_cmos_time(struct timespec64 *now)
7467
return;
7568
}
7669

77-
spin_lock_irqsave(&rtc_lock, flags);
78-
79-
/*
80-
* If UIP is clear, then we have >= 244 microseconds before
81-
* RTC registers will be updated. Spec sheet says that this
82-
* is the reliable way to read RTC - registers. If UIP is set
83-
* then the register access might be invalid.
84-
*/
85-
while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
86-
cpu_relax();
87-
88-
sec = CMOS_READ(RTC_SECONDS);
89-
min = CMOS_READ(RTC_MINUTES);
90-
hour = CMOS_READ(RTC_HOURS);
91-
day = CMOS_READ(RTC_DAY_OF_MONTH);
92-
mon = CMOS_READ(RTC_MONTH);
93-
year = CMOS_READ(RTC_YEAR);
94-
95-
#ifdef CONFIG_ACPI
96-
if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
97-
acpi_gbl_FADT.century)
98-
century = CMOS_READ(acpi_gbl_FADT.century);
99-
#endif
100-
101-
status = CMOS_READ(RTC_CONTROL);
102-
WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY));
103-
104-
spin_unlock_irqrestore(&rtc_lock, flags);
105-
106-
if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) {
107-
sec = bcd2bin(sec);
108-
min = bcd2bin(min);
109-
hour = bcd2bin(hour);
110-
day = bcd2bin(day);
111-
mon = bcd2bin(mon);
112-
year = bcd2bin(year);
70+
if (mc146818_get_time(&tm)) {
71+
pr_err("Unable to read current time from RTC\n");
72+
now->tv_sec = now->tv_nsec = 0;
73+
return;
11374
}
11475

115-
if (century) {
116-
century = bcd2bin(century);
117-
year += century * 100;
118-
} else
119-
year += CMOS_YEARS_OFFS;
120-
121-
now->tv_sec = mktime64(year, mon, day, hour, min, sec);
76+
now->tv_sec = rtc_tm_to_time64(&tm);
12277
now->tv_nsec = 0;
12378
}
12479

arch/x86/kernel/x86_init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ struct x86_platform_ops x86_platform __ro_after_init = {
138138
.calibrate_cpu = native_calibrate_cpu_early,
139139
.calibrate_tsc = native_calibrate_tsc,
140140
.get_wallclock = mach_get_cmos_time,
141-
.set_wallclock = mach_set_rtc_mmss,
141+
.set_wallclock = mach_set_cmos_time,
142142
.iommu_shutdown = iommu_shutdown_noop,
143143
.is_untracked_pat_range = is_ISA_range,
144144
.nmi_init = default_nmi_init,

0 commit comments

Comments
 (0)