xref: /openbmc/linux/arch/x86/kernel/rtc.c (revision d0b73b48)
1 /*
2  * RTC related functions
3  */
4 #include <linux/platform_device.h>
5 #include <linux/mc146818rtc.h>
6 #include <linux/acpi.h>
7 #include <linux/bcd.h>
8 #include <linux/export.h>
9 #include <linux/pnp.h>
10 #include <linux/of.h>
11 
12 #include <asm/vsyscall.h>
13 #include <asm/x86_init.h>
14 #include <asm/time.h>
15 #include <asm/mrst.h>
16 
17 #ifdef CONFIG_X86_32
18 /*
19  * This is a special lock that is owned by the CPU and holds the index
20  * register we are working with.  It is required for NMI access to the
21  * CMOS/RTC registers.  See include/asm-i386/mc146818rtc.h for details.
22  */
23 volatile unsigned long cmos_lock;
24 EXPORT_SYMBOL(cmos_lock);
25 #endif /* CONFIG_X86_32 */
26 
27 /* For two digit years assume time is always after that */
28 #define CMOS_YEARS_OFFS 2000
29 
30 DEFINE_SPINLOCK(rtc_lock);
31 EXPORT_SYMBOL(rtc_lock);
32 
33 /*
34  * In order to set the CMOS clock precisely, set_rtc_mmss has to be
35  * called 500 ms after the second nowtime has started, because when
36  * nowtime is written into the registers of the CMOS clock, it will
37  * jump to the next second precisely 500 ms later. Check the Motorola
38  * MC146818A or Dallas DS12887 data sheet for details.
39  *
40  * BUG: This routine does not handle hour overflow properly; it just
41  *      sets the minutes. Usually you'll only notice that after reboot!
42  */
43 int mach_set_rtc_mmss(unsigned long nowtime)
44 {
45 	int real_seconds, real_minutes, cmos_minutes;
46 	unsigned char save_control, save_freq_select;
47 	unsigned long flags;
48 	int retval = 0;
49 
50 	spin_lock_irqsave(&rtc_lock, flags);
51 
52 	 /* tell the clock it's being set */
53 	save_control = CMOS_READ(RTC_CONTROL);
54 	CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
55 
56 	/* stop and reset prescaler */
57 	save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
58 	CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
59 
60 	cmos_minutes = CMOS_READ(RTC_MINUTES);
61 	if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
62 		cmos_minutes = bcd2bin(cmos_minutes);
63 
64 	/*
65 	 * since we're only adjusting minutes and seconds,
66 	 * don't interfere with hour overflow. This avoids
67 	 * messing with unknown time zones but requires your
68 	 * RTC not to be off by more than 15 minutes
69 	 */
70 	real_seconds = nowtime % 60;
71 	real_minutes = nowtime / 60;
72 	/* correct for half hour time zone */
73 	if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
74 		real_minutes += 30;
75 	real_minutes %= 60;
76 
77 	if (abs(real_minutes - cmos_minutes) < 30) {
78 		if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
79 			real_seconds = bin2bcd(real_seconds);
80 			real_minutes = bin2bcd(real_minutes);
81 		}
82 		CMOS_WRITE(real_seconds, RTC_SECONDS);
83 		CMOS_WRITE(real_minutes, RTC_MINUTES);
84 	} else {
85 		printk_once(KERN_NOTICE
86 		       "set_rtc_mmss: can't update from %d to %d\n",
87 		       cmos_minutes, real_minutes);
88 		retval = -1;
89 	}
90 
91 	/* The following flags have to be released exactly in this order,
92 	 * otherwise the DS12887 (popular MC146818A clone with integrated
93 	 * battery and quartz) will not reset the oscillator and will not
94 	 * update precisely 500 ms later. You won't find this mentioned in
95 	 * the Dallas Semiconductor data sheets, but who believes data
96 	 * sheets anyway ...                           -- Markus Kuhn
97 	 */
98 	CMOS_WRITE(save_control, RTC_CONTROL);
99 	CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
100 
101 	spin_unlock_irqrestore(&rtc_lock, flags);
102 
103 	return retval;
104 }
105 
106 unsigned long mach_get_cmos_time(void)
107 {
108 	unsigned int status, year, mon, day, hour, min, sec, century = 0;
109 	unsigned long flags;
110 
111 	spin_lock_irqsave(&rtc_lock, flags);
112 
113 	/*
114 	 * If UIP is clear, then we have >= 244 microseconds before
115 	 * RTC registers will be updated.  Spec sheet says that this
116 	 * is the reliable way to read RTC - registers. If UIP is set
117 	 * then the register access might be invalid.
118 	 */
119 	while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
120 		cpu_relax();
121 
122 	sec = CMOS_READ(RTC_SECONDS);
123 	min = CMOS_READ(RTC_MINUTES);
124 	hour = CMOS_READ(RTC_HOURS);
125 	day = CMOS_READ(RTC_DAY_OF_MONTH);
126 	mon = CMOS_READ(RTC_MONTH);
127 	year = CMOS_READ(RTC_YEAR);
128 
129 #ifdef CONFIG_ACPI
130 	if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
131 	    acpi_gbl_FADT.century)
132 		century = CMOS_READ(acpi_gbl_FADT.century);
133 #endif
134 
135 	status = CMOS_READ(RTC_CONTROL);
136 	WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY));
137 
138 	spin_unlock_irqrestore(&rtc_lock, flags);
139 
140 	if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) {
141 		sec = bcd2bin(sec);
142 		min = bcd2bin(min);
143 		hour = bcd2bin(hour);
144 		day = bcd2bin(day);
145 		mon = bcd2bin(mon);
146 		year = bcd2bin(year);
147 	}
148 
149 	if (century) {
150 		century = bcd2bin(century);
151 		year += century * 100;
152 		printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
153 	} else
154 		year += CMOS_YEARS_OFFS;
155 
156 	return mktime(year, mon, day, hour, min, sec);
157 }
158 
159 /* Routines for accessing the CMOS RAM/RTC. */
160 unsigned char rtc_cmos_read(unsigned char addr)
161 {
162 	unsigned char val;
163 
164 	lock_cmos_prefix(addr);
165 	outb(addr, RTC_PORT(0));
166 	val = inb(RTC_PORT(1));
167 	lock_cmos_suffix(addr);
168 
169 	return val;
170 }
171 EXPORT_SYMBOL(rtc_cmos_read);
172 
173 void rtc_cmos_write(unsigned char val, unsigned char addr)
174 {
175 	lock_cmos_prefix(addr);
176 	outb(addr, RTC_PORT(0));
177 	outb(val, RTC_PORT(1));
178 	lock_cmos_suffix(addr);
179 }
180 EXPORT_SYMBOL(rtc_cmos_write);
181 
182 int update_persistent_clock(struct timespec now)
183 {
184 	return x86_platform.set_wallclock(now.tv_sec);
185 }
186 
187 /* not static: needed by APM */
188 void read_persistent_clock(struct timespec *ts)
189 {
190 	unsigned long retval;
191 
192 	retval = x86_platform.get_wallclock();
193 
194 	ts->tv_sec = retval;
195 	ts->tv_nsec = 0;
196 }
197 
198 
199 static struct resource rtc_resources[] = {
200 	[0] = {
201 		.start	= RTC_PORT(0),
202 		.end	= RTC_PORT(1),
203 		.flags	= IORESOURCE_IO,
204 	},
205 	[1] = {
206 		.start	= RTC_IRQ,
207 		.end	= RTC_IRQ,
208 		.flags	= IORESOURCE_IRQ,
209 	}
210 };
211 
212 static struct platform_device rtc_device = {
213 	.name		= "rtc_cmos",
214 	.id		= -1,
215 	.resource	= rtc_resources,
216 	.num_resources	= ARRAY_SIZE(rtc_resources),
217 };
218 
219 static __init int add_rtc_cmos(void)
220 {
221 #ifdef CONFIG_PNP
222 	static const char * const  const ids[] __initconst =
223 	    { "PNP0b00", "PNP0b01", "PNP0b02", };
224 	struct pnp_dev *dev;
225 	struct pnp_id *id;
226 	int i;
227 
228 	pnp_for_each_dev(dev) {
229 		for (id = dev->id; id; id = id->next) {
230 			for (i = 0; i < ARRAY_SIZE(ids); i++) {
231 				if (compare_pnp_id(id, ids[i]) != 0)
232 					return 0;
233 			}
234 		}
235 	}
236 #endif
237 	if (of_have_populated_dt())
238 		return 0;
239 
240 	/* Intel MID platforms don't have ioport rtc */
241 	if (mrst_identify_cpu())
242 		return -ENODEV;
243 
244 	platform_device_register(&rtc_device);
245 	dev_info(&rtc_device.dev,
246 		 "registered platform RTC device (no PNP device found)\n");
247 
248 	return 0;
249 }
250 device_initcall(add_rtc_cmos);
251