Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/mc146818rtc.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/param.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/time.h> |
| 23 | #include <linux/types.h> |
| 24 | |
| 25 | #include <asm/bootinfo.h> |
| 26 | #include <asm/cpu.h> |
| 27 | #include <asm/div64.h> |
| 28 | #include <asm/io.h> |
| 29 | #include <asm/irq.h> |
| 30 | #include <asm/mipsregs.h> |
| 31 | #include <asm/sections.h> |
| 32 | #include <asm/time.h> |
| 33 | |
| 34 | #include <asm/dec/interrupts.h> |
| 35 | #include <asm/dec/ioasic.h> |
| 36 | #include <asm/dec/ioasic_addrs.h> |
| 37 | #include <asm/dec/machtype.h> |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | static unsigned long dec_rtc_get_time(void) |
| 40 | { |
| 41 | unsigned int year, mon, day, hour, min, sec, real_year; |
Atsushi Nemoto | 53c2df2 | 2005-11-03 01:01:15 +0900 | [diff] [blame] | 42 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Atsushi Nemoto | 53c2df2 | 2005-11-03 01:01:15 +0900 | [diff] [blame] | 44 | spin_lock_irqsave(&rtc_lock, flags); |
Matt Mackall | ddcabb4 | 2006-03-28 01:56:06 -0800 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | do { |
| 47 | sec = CMOS_READ(RTC_SECONDS); |
| 48 | min = CMOS_READ(RTC_MINUTES); |
| 49 | hour = CMOS_READ(RTC_HOURS); |
| 50 | day = CMOS_READ(RTC_DAY_OF_MONTH); |
| 51 | mon = CMOS_READ(RTC_MONTH); |
| 52 | year = CMOS_READ(RTC_YEAR); |
Matt Mackall | ddcabb4 | 2006-03-28 01:56:06 -0800 | [diff] [blame] | 53 | /* |
| 54 | * The PROM will reset the year to either '72 or '73. |
| 55 | * Therefore we store the real year separately, in one |
| 56 | * of unused BBU RAM locations. |
| 57 | */ |
| 58 | real_year = CMOS_READ(RTC_DEC_YEAR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } while (sec != CMOS_READ(RTC_SECONDS)); |
Matt Mackall | ddcabb4 | 2006-03-28 01:56:06 -0800 | [diff] [blame] | 60 | |
| 61 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { |
| 64 | sec = BCD2BIN(sec); |
| 65 | min = BCD2BIN(min); |
| 66 | hour = BCD2BIN(hour); |
| 67 | day = BCD2BIN(day); |
| 68 | mon = BCD2BIN(mon); |
| 69 | year = BCD2BIN(year); |
| 70 | } |
Matt Mackall | ddcabb4 | 2006-03-28 01:56:06 -0800 | [diff] [blame] | 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | year += real_year - 72 + 2000; |
| 73 | |
| 74 | return mktime(year, mon, day, hour, min, sec); |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * In order to set the CMOS clock precisely, dec_rtc_set_mmss has to |
| 79 | * be called 500 ms after the second nowtime has started, because when |
| 80 | * nowtime is written into the registers of the CMOS clock, it will |
| 81 | * jump to the next second precisely 500 ms later. Check the Dallas |
| 82 | * DS1287 data sheet for details. |
| 83 | */ |
| 84 | static int dec_rtc_set_mmss(unsigned long nowtime) |
| 85 | { |
| 86 | int retval = 0; |
| 87 | int real_seconds, real_minutes, cmos_minutes; |
| 88 | unsigned char save_control, save_freq_select; |
| 89 | |
Atsushi Nemoto | 53c2df2 | 2005-11-03 01:01:15 +0900 | [diff] [blame] | 90 | /* irq are locally disabled here */ |
| 91 | spin_lock(&rtc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | /* tell the clock it's being set */ |
| 93 | save_control = CMOS_READ(RTC_CONTROL); |
| 94 | CMOS_WRITE((save_control | RTC_SET), RTC_CONTROL); |
| 95 | |
| 96 | /* stop and reset prescaler */ |
| 97 | save_freq_select = CMOS_READ(RTC_FREQ_SELECT); |
| 98 | CMOS_WRITE((save_freq_select | RTC_DIV_RESET2), RTC_FREQ_SELECT); |
| 99 | |
| 100 | cmos_minutes = CMOS_READ(RTC_MINUTES); |
| 101 | if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) |
| 102 | cmos_minutes = BCD2BIN(cmos_minutes); |
| 103 | |
| 104 | /* |
| 105 | * since we're only adjusting minutes and seconds, |
| 106 | * don't interfere with hour overflow. This avoids |
| 107 | * messing with unknown time zones but requires your |
| 108 | * RTC not to be off by more than 15 minutes |
| 109 | */ |
| 110 | real_seconds = nowtime % 60; |
| 111 | real_minutes = nowtime / 60; |
| 112 | if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1) |
| 113 | real_minutes += 30; /* correct for half hour time zone */ |
| 114 | real_minutes %= 60; |
| 115 | |
| 116 | if (abs(real_minutes - cmos_minutes) < 30) { |
| 117 | if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { |
| 118 | real_seconds = BIN2BCD(real_seconds); |
| 119 | real_minutes = BIN2BCD(real_minutes); |
| 120 | } |
| 121 | CMOS_WRITE(real_seconds, RTC_SECONDS); |
| 122 | CMOS_WRITE(real_minutes, RTC_MINUTES); |
| 123 | } else { |
| 124 | printk(KERN_WARNING |
| 125 | "set_rtc_mmss: can't update from %d to %d\n", |
| 126 | cmos_minutes, real_minutes); |
| 127 | retval = -1; |
| 128 | } |
| 129 | |
| 130 | /* The following flags have to be released exactly in this order, |
| 131 | * otherwise the DS1287 will not reset the oscillator and will not |
| 132 | * update precisely 500 ms later. You won't find this mentioned |
| 133 | * in the Dallas Semiconductor data sheets, but who believes data |
| 134 | * sheets anyway ... -- Markus Kuhn |
| 135 | */ |
| 136 | CMOS_WRITE(save_control, RTC_CONTROL); |
| 137 | CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT); |
Atsushi Nemoto | 53c2df2 | 2005-11-03 01:01:15 +0900 | [diff] [blame] | 138 | spin_unlock(&rtc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | return retval; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | static int dec_timer_state(void) |
| 145 | { |
| 146 | return (CMOS_READ(RTC_REG_C) & RTC_PF) != 0; |
| 147 | } |
| 148 | |
| 149 | static void dec_timer_ack(void) |
| 150 | { |
| 151 | CMOS_READ(RTC_REG_C); /* Ack the RTC interrupt. */ |
| 152 | } |
| 153 | |
| 154 | static unsigned int dec_ioasic_hpt_read(void) |
| 155 | { |
| 156 | /* |
| 157 | * The free-running counter is 32-bit which is good for about |
| 158 | * 2 minutes, 50 seconds at possible count rates of up to 25MHz. |
| 159 | */ |
| 160 | return ioasic_read(IO_REG_FCTR); |
| 161 | } |
| 162 | |
| 163 | static void dec_ioasic_hpt_init(unsigned int count) |
| 164 | { |
| 165 | ioasic_write(IO_REG_FCTR, ioasic_read(IO_REG_FCTR) - count); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | void __init dec_time_init(void) |
| 170 | { |
Yoichi Yuasa | d23ee8f | 2006-03-27 01:16:33 -0800 | [diff] [blame] | 171 | rtc_mips_get_time = dec_rtc_get_time; |
| 172 | rtc_mips_set_mmss = dec_rtc_set_mmss; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | mips_timer_state = dec_timer_state; |
| 175 | mips_timer_ack = dec_timer_ack; |
| 176 | |
| 177 | if (!cpu_has_counter && IOASIC) { |
| 178 | /* For pre-R4k systems we use the I/O ASIC's counter. */ |
| 179 | mips_hpt_read = dec_ioasic_hpt_read; |
| 180 | mips_hpt_init = dec_ioasic_hpt_init; |
| 181 | } |
| 182 | |
| 183 | /* Set up the rate of periodic DS1287 interrupts. */ |
Atsushi Nemoto | 1723b4a | 2006-06-20 00:19:13 +0900 | [diff] [blame] | 184 | CMOS_WRITE(RTC_REF_CLCK_32KHZ | (16 - __ffs(HZ)), RTC_REG_A); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | EXPORT_SYMBOL(do_settimeofday); |
| 188 | |
Ralf Baechle | 54d0a21 | 2006-07-09 21:38:56 +0100 | [diff] [blame^] | 189 | void __init plat_timer_setup(struct irqaction *irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
| 191 | setup_irq(dec_interrupt[DEC_IRQ_RTC], irq); |
| 192 | |
| 193 | /* Enable periodic DS1287 interrupts. */ |
| 194 | CMOS_WRITE(CMOS_READ(RTC_REG_B) | RTC_PIE, RTC_REG_B); |
| 195 | } |