blob: 1914e56f0d963c149e9ac66c8852986f7fca84bd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
3 * Copyright (C) 2000, 2003 Maciej W. Rozycki
4 *
5 * This file contains the time handling details for PC-style clocks as
6 * found in some MIPS systems.
7 *
8 */
9#include <linux/bcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mc146818rtc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/param.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Yoichi Yuasa6457d9f2008-04-25 12:11:44 +090014#include <asm/cpu-features.h>
15#include <asm/ds1287.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/dec/interrupts.h>
18#include <asm/dec/ioasic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/dec/machtype.h>
20
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +020021void read_persistent_clock(struct timespec *ts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
23 unsigned int year, mon, day, hour, min, sec, real_year;
Atsushi Nemoto53c2df22005-11-03 01:01:15 +090024 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Atsushi Nemoto53c2df22005-11-03 01:01:15 +090026 spin_lock_irqsave(&rtc_lock, flags);
Matt Mackallddcabb42006-03-28 01:56:06 -080027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 do {
29 sec = CMOS_READ(RTC_SECONDS);
30 min = CMOS_READ(RTC_MINUTES);
31 hour = CMOS_READ(RTC_HOURS);
32 day = CMOS_READ(RTC_DAY_OF_MONTH);
33 mon = CMOS_READ(RTC_MONTH);
34 year = CMOS_READ(RTC_YEAR);
Matt Mackallddcabb42006-03-28 01:56:06 -080035 /*
36 * The PROM will reset the year to either '72 or '73.
37 * Therefore we store the real year separately, in one
38 * of unused BBU RAM locations.
39 */
40 real_year = CMOS_READ(RTC_DEC_YEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 } while (sec != CMOS_READ(RTC_SECONDS));
Matt Mackallddcabb42006-03-28 01:56:06 -080042
43 spin_unlock_irqrestore(&rtc_lock, flags);
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
Adrian Bunk02112db2008-10-18 20:28:44 -070046 sec = bcd2bin(sec);
47 min = bcd2bin(min);
48 hour = bcd2bin(hour);
49 day = bcd2bin(day);
50 mon = bcd2bin(mon);
51 year = bcd2bin(year);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 }
Matt Mackallddcabb42006-03-28 01:56:06 -080053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 year += real_year - 72 + 2000;
55
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +020056 ts->tv_sec = mktime(year, mon, day, hour, min, sec);
57 ts->tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
60/*
Ralf Baechle4b550482007-10-11 23:46:08 +010061 * In order to set the CMOS clock precisely, rtc_mips_set_mmss has to
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * be called 500 ms after the second nowtime has started, because when
63 * nowtime is written into the registers of the CMOS clock, it will
64 * jump to the next second precisely 500 ms later. Check the Dallas
65 * DS1287 data sheet for details.
66 */
Ralf Baechle4b550482007-10-11 23:46:08 +010067int rtc_mips_set_mmss(unsigned long nowtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 int retval = 0;
70 int real_seconds, real_minutes, cmos_minutes;
71 unsigned char save_control, save_freq_select;
72
Atsushi Nemoto53c2df22005-11-03 01:01:15 +090073 /* irq are locally disabled here */
74 spin_lock(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 /* tell the clock it's being set */
76 save_control = CMOS_READ(RTC_CONTROL);
77 CMOS_WRITE((save_control | RTC_SET), RTC_CONTROL);
78
79 /* stop and reset prescaler */
80 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
81 CMOS_WRITE((save_freq_select | RTC_DIV_RESET2), RTC_FREQ_SELECT);
82
83 cmos_minutes = CMOS_READ(RTC_MINUTES);
84 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
Adrian Bunk02112db2008-10-18 20:28:44 -070085 cmos_minutes = bcd2bin(cmos_minutes);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 /*
88 * since we're only adjusting minutes and seconds,
89 * don't interfere with hour overflow. This avoids
90 * messing with unknown time zones but requires your
91 * RTC not to be off by more than 15 minutes
92 */
93 real_seconds = nowtime % 60;
94 real_minutes = nowtime / 60;
95 if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
96 real_minutes += 30; /* correct for half hour time zone */
97 real_minutes %= 60;
98
99 if (abs(real_minutes - cmos_minutes) < 30) {
100 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
Adrian Bunk02112db2008-10-18 20:28:44 -0700101 real_seconds = bin2bcd(real_seconds);
102 real_minutes = bin2bcd(real_minutes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104 CMOS_WRITE(real_seconds, RTC_SECONDS);
105 CMOS_WRITE(real_minutes, RTC_MINUTES);
106 } else {
Stephen Hemminger3e5c1242011-01-12 16:59:31 -0800107 printk_once(KERN_NOTICE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 "set_rtc_mmss: can't update from %d to %d\n",
109 cmos_minutes, real_minutes);
110 retval = -1;
111 }
112
113 /* The following flags have to be released exactly in this order,
114 * otherwise the DS1287 will not reset the oscillator and will not
115 * update precisely 500 ms later. You won't find this mentioned
116 * in the Dallas Semiconductor data sheets, but who believes data
117 * sheets anyway ... -- Markus Kuhn
118 */
119 CMOS_WRITE(save_control, RTC_CONTROL);
120 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Atsushi Nemoto53c2df22005-11-03 01:01:15 +0900121 spin_unlock(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 return retval;
124}
125
Ralf Baechle4b550482007-10-11 23:46:08 +0100126void __init plat_time_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Maciej W. Rozyckidaed1282013-09-12 12:01:53 +0100128 int ioasic_clock = 0;
Yoichi Yuasa6457d9f2008-04-25 12:11:44 +0900129 u32 start, end;
Maciej W. Rozycki85339662013-09-04 23:47:45 +0100130 int i = HZ / 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Yoichi Yuasa6457d9f2008-04-25 12:11:44 +0900132 /* Set up the rate of periodic DS1287 interrupts. */
133 ds1287_set_base_clock(HZ);
134
Maciej W. Rozyckidaed1282013-09-12 12:01:53 +0100135 /* On some I/O ASIC systems we have the I/O ASIC's counter. */
136 if (IOASIC)
137 ioasic_clock = dec_ioasic_clocksource_init() == 0;
Yoichi Yuasa6457d9f2008-04-25 12:11:44 +0900138 if (cpu_has_counter) {
Maciej W. Rozycki85339662013-09-04 23:47:45 +0100139 ds1287_timer_state();
Yoichi Yuasa6457d9f2008-04-25 12:11:44 +0900140 while (!ds1287_timer_state())
141 ;
142
143 start = read_c0_count();
144
145 while (i--)
146 while (!ds1287_timer_state())
147 ;
148
149 end = read_c0_count();
150
Maciej W. Rozycki85339662013-09-04 23:47:45 +0100151 mips_hpt_frequency = (end - start) * 8;
Yoichi Yuasa6457d9f2008-04-25 12:11:44 +0900152 printk(KERN_INFO "MIPS counter frequency %dHz\n",
153 mips_hpt_frequency);
Maciej W. Rozyckidaed1282013-09-12 12:01:53 +0100154
155 /*
156 * All R4k DECstations suffer from the CP0 Count erratum,
157 * so we can't use the timer as a clock source, and a clock
158 * event both at a time. An accurate wall clock is more
159 * important than a high-precision interval timer so only
160 * use the timer as a clock source, and not a clock event
161 * if there's no I/O ASIC counter available to serve as a
162 * clock source.
163 */
164 if (!ioasic_clock) {
165 init_r4k_clocksource();
166 mips_hpt_frequency = 0;
167 }
168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Yoichi Yuasa6457d9f2008-04-25 12:11:44 +0900170 ds1287_clockevent_init(dec_interrupt[DEC_IRQ_RTC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}