Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/mc146818rtc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/param.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Yoichi Yuasa | 6457d9f | 2008-04-25 12:11:44 +0900 | [diff] [blame] | 14 | #include <asm/cpu-features.h> |
| 15 | #include <asm/ds1287.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/time.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/dec/interrupts.h> |
| 18 | #include <asm/dec/ioasic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/dec/machtype.h> |
| 20 | |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 21 | unsigned long read_persistent_clock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | { |
| 23 | unsigned int year, mon, day, hour, min, sec, real_year; |
Atsushi Nemoto | 53c2df2 | 2005-11-03 01:01:15 +0900 | [diff] [blame] | 24 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Atsushi Nemoto | 53c2df2 | 2005-11-03 01:01:15 +0900 | [diff] [blame] | 26 | spin_lock_irqsave(&rtc_lock, flags); |
Matt Mackall | ddcabb4 | 2006-03-28 01:56:06 -0800 | [diff] [blame] | 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | 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 Mackall | ddcabb4 | 2006-03-28 01:56:06 -0800 | [diff] [blame] | 35 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } while (sec != CMOS_READ(RTC_SECONDS)); |
Matt Mackall | ddcabb4 | 2006-03-28 01:56:06 -0800 | [diff] [blame] | 42 | |
| 43 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { |
Adrian Bunk | 02112db | 2008-10-18 20:28:44 -0700 | [diff] [blame] | 46 | sec = bcd2bin(sec); |
| 47 | min = bcd2bin(min); |
| 48 | hour = bcd2bin(hour); |
| 49 | day = bcd2bin(day); |
| 50 | mon = bcd2bin(mon); |
| 51 | year = bcd2bin(year); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
Matt Mackall | ddcabb4 | 2006-03-28 01:56:06 -0800 | [diff] [blame] | 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | year += real_year - 72 + 2000; |
| 55 | |
| 56 | return mktime(year, mon, day, hour, min, sec); |
| 57 | } |
| 58 | |
| 59 | /* |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 60 | * In order to set the CMOS clock precisely, rtc_mips_set_mmss has to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | * be called 500 ms after the second nowtime has started, because when |
| 62 | * nowtime is written into the registers of the CMOS clock, it will |
| 63 | * jump to the next second precisely 500 ms later. Check the Dallas |
| 64 | * DS1287 data sheet for details. |
| 65 | */ |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 66 | int rtc_mips_set_mmss(unsigned long nowtime) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
| 68 | int retval = 0; |
| 69 | int real_seconds, real_minutes, cmos_minutes; |
| 70 | unsigned char save_control, save_freq_select; |
| 71 | |
Atsushi Nemoto | 53c2df2 | 2005-11-03 01:01:15 +0900 | [diff] [blame] | 72 | /* irq are locally disabled here */ |
| 73 | spin_lock(&rtc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | /* tell the clock it's being set */ |
| 75 | save_control = CMOS_READ(RTC_CONTROL); |
| 76 | CMOS_WRITE((save_control | RTC_SET), RTC_CONTROL); |
| 77 | |
| 78 | /* stop and reset prescaler */ |
| 79 | save_freq_select = CMOS_READ(RTC_FREQ_SELECT); |
| 80 | CMOS_WRITE((save_freq_select | RTC_DIV_RESET2), RTC_FREQ_SELECT); |
| 81 | |
| 82 | cmos_minutes = CMOS_READ(RTC_MINUTES); |
| 83 | if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) |
Adrian Bunk | 02112db | 2008-10-18 20:28:44 -0700 | [diff] [blame] | 84 | cmos_minutes = bcd2bin(cmos_minutes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * since we're only adjusting minutes and seconds, |
| 88 | * don't interfere with hour overflow. This avoids |
| 89 | * messing with unknown time zones but requires your |
| 90 | * RTC not to be off by more than 15 minutes |
| 91 | */ |
| 92 | real_seconds = nowtime % 60; |
| 93 | real_minutes = nowtime / 60; |
| 94 | if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1) |
| 95 | real_minutes += 30; /* correct for half hour time zone */ |
| 96 | real_minutes %= 60; |
| 97 | |
| 98 | if (abs(real_minutes - cmos_minutes) < 30) { |
| 99 | if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { |
Adrian Bunk | 02112db | 2008-10-18 20:28:44 -0700 | [diff] [blame] | 100 | real_seconds = bin2bcd(real_seconds); |
| 101 | real_minutes = bin2bcd(real_minutes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | CMOS_WRITE(real_seconds, RTC_SECONDS); |
| 104 | CMOS_WRITE(real_minutes, RTC_MINUTES); |
| 105 | } else { |
| 106 | printk(KERN_WARNING |
| 107 | "set_rtc_mmss: can't update from %d to %d\n", |
| 108 | cmos_minutes, real_minutes); |
| 109 | retval = -1; |
| 110 | } |
| 111 | |
| 112 | /* The following flags have to be released exactly in this order, |
| 113 | * otherwise the DS1287 will not reset the oscillator and will not |
| 114 | * update precisely 500 ms later. You won't find this mentioned |
| 115 | * in the Dallas Semiconductor data sheets, but who believes data |
| 116 | * sheets anyway ... -- Markus Kuhn |
| 117 | */ |
| 118 | CMOS_WRITE(save_control, RTC_CONTROL); |
| 119 | CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT); |
Atsushi Nemoto | 53c2df2 | 2005-11-03 01:01:15 +0900 | [diff] [blame] | 120 | spin_unlock(&rtc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | return retval; |
| 123 | } |
| 124 | |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 125 | void __init plat_time_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Yoichi Yuasa | 6457d9f | 2008-04-25 12:11:44 +0900 | [diff] [blame] | 127 | u32 start, end; |
| 128 | int i = HZ / 10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Yoichi Yuasa | 6457d9f | 2008-04-25 12:11:44 +0900 | [diff] [blame] | 130 | /* Set up the rate of periodic DS1287 interrupts. */ |
| 131 | ds1287_set_base_clock(HZ); |
| 132 | |
| 133 | if (cpu_has_counter) { |
| 134 | while (!ds1287_timer_state()) |
| 135 | ; |
| 136 | |
| 137 | start = read_c0_count(); |
| 138 | |
| 139 | while (i--) |
| 140 | while (!ds1287_timer_state()) |
| 141 | ; |
| 142 | |
| 143 | end = read_c0_count(); |
| 144 | |
| 145 | mips_hpt_frequency = (end - start) * 10; |
| 146 | printk(KERN_INFO "MIPS counter frequency %dHz\n", |
| 147 | mips_hpt_frequency); |
| 148 | } else if (IOASIC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | /* For pre-R4k systems we use the I/O ASIC's counter. */ |
Yoichi Yuasa | 4247417 | 2008-04-24 09:48:40 +0900 | [diff] [blame] | 150 | dec_ioasic_clocksource_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Yoichi Yuasa | 6457d9f | 2008-04-25 12:11:44 +0900 | [diff] [blame] | 152 | ds1287_clockevent_init(dec_interrupt[DEC_IRQ_RTC]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |