blob: 1359c03ded5111adb0892029b59ebffbd26eee7f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/mips/dec/time.c
3 *
4 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
5 * Copyright (C) 2000, 2003 Maciej W. Rozycki
6 *
7 * This file contains the time handling details for PC-style clocks as
8 * found in some MIPS systems.
9 *
10 */
11#include <linux/bcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/mc146818rtc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/param.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Yoichi Yuasa6457d9f2008-04-25 12:11:44 +090016#include <asm/cpu-features.h>
17#include <asm/ds1287.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/dec/interrupts.h>
20#include <asm/dec/ioasic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/dec/machtype.h>
22
Ralf Baechle4b550482007-10-11 23:46:08 +010023unsigned long read_persistent_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
25 unsigned int year, mon, day, hour, min, sec, real_year;
Atsushi Nemoto53c2df22005-11-03 01:01:15 +090026 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Atsushi Nemoto53c2df22005-11-03 01:01:15 +090028 spin_lock_irqsave(&rtc_lock, flags);
Matt Mackallddcabb42006-03-28 01:56:06 -080029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 do {
31 sec = CMOS_READ(RTC_SECONDS);
32 min = CMOS_READ(RTC_MINUTES);
33 hour = CMOS_READ(RTC_HOURS);
34 day = CMOS_READ(RTC_DAY_OF_MONTH);
35 mon = CMOS_READ(RTC_MONTH);
36 year = CMOS_READ(RTC_YEAR);
Matt Mackallddcabb42006-03-28 01:56:06 -080037 /*
38 * The PROM will reset the year to either '72 or '73.
39 * Therefore we store the real year separately, in one
40 * of unused BBU RAM locations.
41 */
42 real_year = CMOS_READ(RTC_DEC_YEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 } while (sec != CMOS_READ(RTC_SECONDS));
Matt Mackallddcabb42006-03-28 01:56:06 -080044
45 spin_unlock_irqrestore(&rtc_lock, flags);
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
Adrian Bunk02112db2008-10-18 20:28:44 -070048 sec = bcd2bin(sec);
49 min = bcd2bin(min);
50 hour = bcd2bin(hour);
51 day = bcd2bin(day);
52 mon = bcd2bin(mon);
53 year = bcd2bin(year);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 }
Matt Mackallddcabb42006-03-28 01:56:06 -080055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 year += real_year - 72 + 2000;
57
58 return mktime(year, mon, day, hour, min, sec);
59}
60
61/*
Ralf Baechle4b550482007-10-11 23:46:08 +010062 * In order to set the CMOS clock precisely, rtc_mips_set_mmss has to
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * be called 500 ms after the second nowtime has started, because when
64 * nowtime is written into the registers of the CMOS clock, it will
65 * jump to the next second precisely 500 ms later. Check the Dallas
66 * DS1287 data sheet for details.
67 */
Ralf Baechle4b550482007-10-11 23:46:08 +010068int rtc_mips_set_mmss(unsigned long nowtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 int retval = 0;
71 int real_seconds, real_minutes, cmos_minutes;
72 unsigned char save_control, save_freq_select;
73
Atsushi Nemoto53c2df22005-11-03 01:01:15 +090074 /* irq are locally disabled here */
75 spin_lock(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 /* tell the clock it's being set */
77 save_control = CMOS_READ(RTC_CONTROL);
78 CMOS_WRITE((save_control | RTC_SET), RTC_CONTROL);
79
80 /* stop and reset prescaler */
81 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
82 CMOS_WRITE((save_freq_select | RTC_DIV_RESET2), RTC_FREQ_SELECT);
83
84 cmos_minutes = CMOS_READ(RTC_MINUTES);
85 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
Adrian Bunk02112db2008-10-18 20:28:44 -070086 cmos_minutes = bcd2bin(cmos_minutes);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 /*
89 * since we're only adjusting minutes and seconds,
90 * don't interfere with hour overflow. This avoids
91 * messing with unknown time zones but requires your
92 * RTC not to be off by more than 15 minutes
93 */
94 real_seconds = nowtime % 60;
95 real_minutes = nowtime / 60;
96 if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
97 real_minutes += 30; /* correct for half hour time zone */
98 real_minutes %= 60;
99
100 if (abs(real_minutes - cmos_minutes) < 30) {
101 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
Adrian Bunk02112db2008-10-18 20:28:44 -0700102 real_seconds = bin2bcd(real_seconds);
103 real_minutes = bin2bcd(real_minutes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105 CMOS_WRITE(real_seconds, RTC_SECONDS);
106 CMOS_WRITE(real_minutes, RTC_MINUTES);
107 } else {
108 printk(KERN_WARNING
109 "set_rtc_mmss: can't update from %d to %d\n",
110 cmos_minutes, real_minutes);
111 retval = -1;
112 }
113
114 /* The following flags have to be released exactly in this order,
115 * otherwise the DS1287 will not reset the oscillator and will not
116 * update precisely 500 ms later. You won't find this mentioned
117 * in the Dallas Semiconductor data sheets, but who believes data
118 * sheets anyway ... -- Markus Kuhn
119 */
120 CMOS_WRITE(save_control, RTC_CONTROL);
121 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Atsushi Nemoto53c2df22005-11-03 01:01:15 +0900122 spin_unlock(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 return retval;
125}
126
Ralf Baechle4b550482007-10-11 23:46:08 +0100127void __init plat_time_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Yoichi Yuasa6457d9f2008-04-25 12:11:44 +0900129 u32 start, end;
130 int i = HZ / 10;
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
135 if (cpu_has_counter) {
136 while (!ds1287_timer_state())
137 ;
138
139 start = read_c0_count();
140
141 while (i--)
142 while (!ds1287_timer_state())
143 ;
144
145 end = read_c0_count();
146
147 mips_hpt_frequency = (end - start) * 10;
148 printk(KERN_INFO "MIPS counter frequency %dHz\n",
149 mips_hpt_frequency);
150 } else if (IOASIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* For pre-R4k systems we use the I/O ASIC's counter. */
Yoichi Yuasa42474172008-04-24 09:48:40 +0900152 dec_ioasic_clocksource_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Yoichi Yuasa6457d9f2008-04-25 12:11:44 +0900154 ds1287_clockevent_init(dec_interrupt[DEC_IRQ_RTC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}