Skip to content

Commit fd09cc8

Browse files
arndbalexandrebelloni
authored andcommitted
rtc: cmos: move mc146818rtc code out of asm-generic/rtc.h
Drivers should not really include stuff from asm-generic directly, and the PC-style cmos rtc driver does this in order to reuse the mc146818 implementation of get_rtc_time/set_rtc_time rather than the architecture specific one for the architecture it gets built for. To make it more obvious what is going on, this moves and renames the two functions into include/linux/mc146818rtc.h, which holds the other mc146818 specific code. Ideally it would be in a .c file, but that would require extra infrastructure as the functions are called by multiple drivers with conflicting dependencies. With this change, the asm-generic/rtc.h header also becomes much more generic, so it can be reused more easily across any architecture that still relies on the genrtc driver. The only caller of the internal __get_rtc_time/__set_rtc_time functions is in arch/alpha/kernel/rtc.c, and we just change those over to the new naming. Signed-off-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
1 parent 2399973 commit fd09cc8

7 files changed

Lines changed: 209 additions & 212 deletions

File tree

‎arch/alpha/kernel/rtc.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#include <linux/rtc.h>
1616
#include <linux/platform_device.h>
1717

18-
#include <asm/rtc.h>
19-
2018
#include "proto.h"
2119

2220

@@ -81,7 +79,7 @@ init_rtc_epoch(void)
8179
static int
8280
alpha_rtc_read_time(struct device *dev, struct rtc_time *tm)
8381
{
84-
__get_rtc_time(tm);
82+
mc146818_get_time(tm);
8583

8684
/* Adjust for non-default epochs. It's easier to depend on the
8785
generic __get_rtc_time and adjust the epoch here than create
@@ -112,7 +110,7 @@ alpha_rtc_set_time(struct device *dev, struct rtc_time *tm)
112110
tm = &xtm;
113111
}
114112

115-
return __set_rtc_time(tm);
113+
return mc146818_set_time(tm);
116114
}
117115

118116
static int

‎arch/x86/include/asm/mc146818rtc.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include <asm/io.h>
88
#include <asm/processor.h>
9-
#include <linux/mc146818rtc.h>
109

1110
#ifndef RTC_PORT
1211
#define RTC_PORT(x) (0x70 + (x))

‎arch/x86/platform/efi/efi_64.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <linux/bootmem.h>
2626
#include <linux/ioport.h>
2727
#include <linux/module.h>
28+
#include <linux/mc146818rtc.h>
2829
#include <linux/efi.h>
2930
#include <linux/uaccess.h>
3031
#include <linux/io.h>

‎arch/x86/platform/intel-mid/intel_mid_vrtc.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/init.h>
2323
#include <linux/sfi.h>
2424
#include <linux/platform_device.h>
25+
#include <linux/mc146818rtc.h>
2526

2627
#include <asm/intel-mid.h>
2728
#include <asm/intel_mid_vrtc.h>

‎drivers/rtc/rtc-cmos.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#include <linux/of_platform.h>
4444

4545
/* this is for "generic access to PC-style RTC" using CMOS_READ/CMOS_WRITE */
46-
#include <asm-generic/rtc.h>
46+
#include <linux/mc146818rtc.h>
4747

4848
struct cmos_rtc {
4949
struct rtc_device *rtc;
@@ -190,10 +190,10 @@ static inline void cmos_write_bank2(unsigned char val, unsigned char addr)
190190
static int cmos_read_time(struct device *dev, struct rtc_time *t)
191191
{
192192
/* REVISIT: if the clock has a "century" register, use
193-
* that instead of the heuristic in get_rtc_time().
193+
* that instead of the heuristic in mc146818_get_time().
194194
* That'll make Y3K compatility (year > 2070) easy!
195195
*/
196-
get_rtc_time(t);
196+
mc146818_get_time(t);
197197
return 0;
198198
}
199199

@@ -205,7 +205,7 @@ static int cmos_set_time(struct device *dev, struct rtc_time *t)
205205
* takes effect exactly 500ms after we write the register.
206206
* (Also queueing and other delays before we get this far.)
207207
*/
208-
return set_rtc_time(t);
208+
return mc146818_set_time(t);
209209
}
210210

211211
static int cmos_read_alarm(struct device *dev, struct rtc_wkalrm *t)
@@ -1142,14 +1142,14 @@ static __init void cmos_of_init(struct platform_device *pdev)
11421142
if (val)
11431143
CMOS_WRITE(be32_to_cpup(val), RTC_FREQ_SELECT);
11441144

1145-
get_rtc_time(&time);
1145+
cmos_read_time(&pdev->dev, &time);
11461146
ret = rtc_valid_tm(&time);
11471147
if (ret) {
11481148
struct rtc_time def_time = {
11491149
.tm_year = 1,
11501150
.tm_mday = 1,
11511151
};
1152-
set_rtc_time(&def_time);
1152+
cmos_set_time(&pdev->dev, &def_time);
11531153
}
11541154
}
11551155
#else

‎include/asm-generic/rtc.h‎

Lines changed: 5 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
#ifndef __ASM_RTC_H__
1313
#define __ASM_RTC_H__
1414

15-
#include <linux/mc146818rtc.h>
1615
#include <linux/rtc.h>
17-
#include <linux/bcd.h>
18-
#include <linux/delay.h>
19-
#ifdef CONFIG_ACPI
20-
#include <linux/acpi.h>
16+
17+
#ifndef get_rtc_time
18+
#include <linux/mc146818rtc.h>
19+
#define get_rtc_time mc146818_get_time
20+
#define set_rtc_time mc146818_set_time
2121
#endif
2222

2323
#define RTC_PIE 0x40 /* periodic interrupt enable */
@@ -31,202 +31,6 @@
3131
#define RTC_24H 0x02 /* 24 hour mode - else hours bit 7 means pm */
3232
#define RTC_DST_EN 0x01 /* auto switch DST - works f. USA only */
3333

34-
/*
35-
* Returns true if a clock update is in progress
36-
*/
37-
static inline unsigned char rtc_is_updating(void)
38-
{
39-
unsigned char uip;
40-
unsigned long flags;
41-
42-
spin_lock_irqsave(&rtc_lock, flags);
43-
uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
44-
spin_unlock_irqrestore(&rtc_lock, flags);
45-
return uip;
46-
}
47-
48-
static inline unsigned int __get_rtc_time(struct rtc_time *time)
49-
{
50-
unsigned char ctrl;
51-
unsigned long flags;
52-
unsigned char century = 0;
53-
54-
#ifdef CONFIG_MACH_DECSTATION
55-
unsigned int real_year;
56-
#endif
57-
58-
/*
59-
* read RTC once any update in progress is done. The update
60-
* can take just over 2ms. We wait 20ms. There is no need to
61-
* to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
62-
* If you need to know *exactly* when a second has started, enable
63-
* periodic update complete interrupts, (via ioctl) and then
64-
* immediately read /dev/rtc which will block until you get the IRQ.
65-
* Once the read clears, read the RTC time (again via ioctl). Easy.
66-
*/
67-
if (rtc_is_updating())
68-
mdelay(20);
69-
70-
/*
71-
* Only the values that we read from the RTC are set. We leave
72-
* tm_wday, tm_yday and tm_isdst untouched. Even though the
73-
* RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
74-
* by the RTC when initially set to a non-zero value.
75-
*/
76-
spin_lock_irqsave(&rtc_lock, flags);
77-
time->tm_sec = CMOS_READ(RTC_SECONDS);
78-
time->tm_min = CMOS_READ(RTC_MINUTES);
79-
time->tm_hour = CMOS_READ(RTC_HOURS);
80-
time->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
81-
time->tm_mon = CMOS_READ(RTC_MONTH);
82-
time->tm_year = CMOS_READ(RTC_YEAR);
83-
#ifdef CONFIG_MACH_DECSTATION
84-
real_year = CMOS_READ(RTC_DEC_YEAR);
85-
#endif
86-
#ifdef CONFIG_ACPI
87-
if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
88-
acpi_gbl_FADT.century)
89-
century = CMOS_READ(acpi_gbl_FADT.century);
90-
#endif
91-
ctrl = CMOS_READ(RTC_CONTROL);
92-
spin_unlock_irqrestore(&rtc_lock, flags);
93-
94-
if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
95-
{
96-
time->tm_sec = bcd2bin(time->tm_sec);
97-
time->tm_min = bcd2bin(time->tm_min);
98-
time->tm_hour = bcd2bin(time->tm_hour);
99-
time->tm_mday = bcd2bin(time->tm_mday);
100-
time->tm_mon = bcd2bin(time->tm_mon);
101-
time->tm_year = bcd2bin(time->tm_year);
102-
century = bcd2bin(century);
103-
}
104-
105-
#ifdef CONFIG_MACH_DECSTATION
106-
time->tm_year += real_year - 72;
107-
#endif
108-
109-
if (century)
110-
time->tm_year += (century - 19) * 100;
111-
112-
/*
113-
* Account for differences between how the RTC uses the values
114-
* and how they are defined in a struct rtc_time;
115-
*/
116-
if (time->tm_year <= 69)
117-
time->tm_year += 100;
118-
119-
time->tm_mon--;
120-
121-
return RTC_24H;
122-
}
123-
124-
#ifndef get_rtc_time
125-
#define get_rtc_time __get_rtc_time
126-
#endif
127-
128-
/* Set the current date and time in the real time clock. */
129-
static inline int __set_rtc_time(struct rtc_time *time)
130-
{
131-
unsigned long flags;
132-
unsigned char mon, day, hrs, min, sec;
133-
unsigned char save_control, save_freq_select;
134-
unsigned int yrs;
135-
#ifdef CONFIG_MACH_DECSTATION
136-
unsigned int real_yrs, leap_yr;
137-
#endif
138-
unsigned char century = 0;
139-
140-
yrs = time->tm_year;
141-
mon = time->tm_mon + 1; /* tm_mon starts at zero */
142-
day = time->tm_mday;
143-
hrs = time->tm_hour;
144-
min = time->tm_min;
145-
sec = time->tm_sec;
146-
147-
if (yrs > 255) /* They are unsigned */
148-
return -EINVAL;
149-
150-
spin_lock_irqsave(&rtc_lock, flags);
151-
#ifdef CONFIG_MACH_DECSTATION
152-
real_yrs = yrs;
153-
leap_yr = ((!((yrs + 1900) % 4) && ((yrs + 1900) % 100)) ||
154-
!((yrs + 1900) % 400));
155-
yrs = 72;
156-
157-
/*
158-
* We want to keep the year set to 73 until March
159-
* for non-leap years, so that Feb, 29th is handled
160-
* correctly.
161-
*/
162-
if (!leap_yr && mon < 3) {
163-
real_yrs--;
164-
yrs = 73;
165-
}
166-
#endif
167-
168-
#ifdef CONFIG_ACPI
169-
if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
170-
acpi_gbl_FADT.century) {
171-
century = (yrs + 1900) / 100;
172-
yrs %= 100;
173-
}
174-
#endif
175-
176-
/* These limits and adjustments are independent of
177-
* whether the chip is in binary mode or not.
178-
*/
179-
if (yrs > 169) {
180-
spin_unlock_irqrestore(&rtc_lock, flags);
181-
return -EINVAL;
182-
}
183-
184-
if (yrs >= 100)
185-
yrs -= 100;
186-
187-
if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
188-
|| RTC_ALWAYS_BCD) {
189-
sec = bin2bcd(sec);
190-
min = bin2bcd(min);
191-
hrs = bin2bcd(hrs);
192-
day = bin2bcd(day);
193-
mon = bin2bcd(mon);
194-
yrs = bin2bcd(yrs);
195-
century = bin2bcd(century);
196-
}
197-
198-
save_control = CMOS_READ(RTC_CONTROL);
199-
CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
200-
save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
201-
CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
202-
203-
#ifdef CONFIG_MACH_DECSTATION
204-
CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
205-
#endif
206-
CMOS_WRITE(yrs, RTC_YEAR);
207-
CMOS_WRITE(mon, RTC_MONTH);
208-
CMOS_WRITE(day, RTC_DAY_OF_MONTH);
209-
CMOS_WRITE(hrs, RTC_HOURS);
210-
CMOS_WRITE(min, RTC_MINUTES);
211-
CMOS_WRITE(sec, RTC_SECONDS);
212-
#ifdef CONFIG_ACPI
213-
if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
214-
acpi_gbl_FADT.century)
215-
CMOS_WRITE(century, acpi_gbl_FADT.century);
216-
#endif
217-
218-
CMOS_WRITE(save_control, RTC_CONTROL);
219-
CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
220-
221-
spin_unlock_irqrestore(&rtc_lock, flags);
222-
223-
return 0;
224-
}
225-
226-
#ifndef set_rtc_time
227-
#define set_rtc_time __set_rtc_time
228-
#endif
229-
23034
static inline unsigned int get_rtc_ss(void)
23135
{
23236
struct rtc_time h;

0 commit comments

Comments
 (0)