11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/arch/mips/dec/time.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1995 Linus Torvalds 51da177e4SLinus Torvalds * Copyright (C) 2000, 2003 Maciej W. Rozycki 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This file contains the time handling details for PC-style clocks as 81da177e4SLinus Torvalds * found in some MIPS systems. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds #include <linux/bcd.h> 121da177e4SLinus Torvalds #include <linux/errno.h> 131da177e4SLinus Torvalds #include <linux/init.h> 141da177e4SLinus Torvalds #include <linux/interrupt.h> 151da177e4SLinus Torvalds #include <linux/kernel.h> 161da177e4SLinus Torvalds #include <linux/mc146818rtc.h> 171da177e4SLinus Torvalds #include <linux/mm.h> 181da177e4SLinus Torvalds #include <linux/module.h> 191da177e4SLinus Torvalds #include <linux/param.h> 201da177e4SLinus Torvalds #include <linux/sched.h> 211da177e4SLinus Torvalds #include <linux/string.h> 221da177e4SLinus Torvalds #include <linux/time.h> 231da177e4SLinus Torvalds #include <linux/types.h> 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #include <asm/bootinfo.h> 261da177e4SLinus Torvalds #include <asm/cpu.h> 271da177e4SLinus Torvalds #include <asm/div64.h> 281da177e4SLinus Torvalds #include <asm/io.h> 291da177e4SLinus Torvalds #include <asm/irq.h> 301da177e4SLinus Torvalds #include <asm/mipsregs.h> 311da177e4SLinus Torvalds #include <asm/sections.h> 321da177e4SLinus Torvalds #include <asm/time.h> 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #include <asm/dec/interrupts.h> 351da177e4SLinus Torvalds #include <asm/dec/ioasic.h> 361da177e4SLinus Torvalds #include <asm/dec/ioasic_addrs.h> 371da177e4SLinus Torvalds #include <asm/dec/machtype.h> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds static unsigned long dec_rtc_get_time(void) 401da177e4SLinus Torvalds { 411da177e4SLinus Torvalds unsigned int year, mon, day, hour, min, sec, real_year; 4253c2df2fSAtsushi Nemoto unsigned long flags; 431da177e4SLinus Torvalds 4453c2df2fSAtsushi Nemoto spin_lock_irqsave(&rtc_lock, flags); 45ddcabb4fSMatt Mackall 461da177e4SLinus Torvalds do { 471da177e4SLinus Torvalds sec = CMOS_READ(RTC_SECONDS); 481da177e4SLinus Torvalds min = CMOS_READ(RTC_MINUTES); 491da177e4SLinus Torvalds hour = CMOS_READ(RTC_HOURS); 501da177e4SLinus Torvalds day = CMOS_READ(RTC_DAY_OF_MONTH); 511da177e4SLinus Torvalds mon = CMOS_READ(RTC_MONTH); 521da177e4SLinus Torvalds year = CMOS_READ(RTC_YEAR); 53ddcabb4fSMatt Mackall /* 54ddcabb4fSMatt Mackall * The PROM will reset the year to either '72 or '73. 55ddcabb4fSMatt Mackall * Therefore we store the real year separately, in one 56ddcabb4fSMatt Mackall * of unused BBU RAM locations. 57ddcabb4fSMatt Mackall */ 58ddcabb4fSMatt Mackall real_year = CMOS_READ(RTC_DEC_YEAR); 591da177e4SLinus Torvalds } while (sec != CMOS_READ(RTC_SECONDS)); 60ddcabb4fSMatt Mackall 61ddcabb4fSMatt Mackall spin_unlock_irqrestore(&rtc_lock, flags); 62ddcabb4fSMatt Mackall 631da177e4SLinus Torvalds if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { 641da177e4SLinus Torvalds sec = BCD2BIN(sec); 651da177e4SLinus Torvalds min = BCD2BIN(min); 661da177e4SLinus Torvalds hour = BCD2BIN(hour); 671da177e4SLinus Torvalds day = BCD2BIN(day); 681da177e4SLinus Torvalds mon = BCD2BIN(mon); 691da177e4SLinus Torvalds year = BCD2BIN(year); 701da177e4SLinus Torvalds } 71ddcabb4fSMatt Mackall 721da177e4SLinus Torvalds year += real_year - 72 + 2000; 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds return mktime(year, mon, day, hour, min, sec); 751da177e4SLinus Torvalds } 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds /* 781da177e4SLinus Torvalds * In order to set the CMOS clock precisely, dec_rtc_set_mmss has to 791da177e4SLinus Torvalds * be called 500 ms after the second nowtime has started, because when 801da177e4SLinus Torvalds * nowtime is written into the registers of the CMOS clock, it will 811da177e4SLinus Torvalds * jump to the next second precisely 500 ms later. Check the Dallas 821da177e4SLinus Torvalds * DS1287 data sheet for details. 831da177e4SLinus Torvalds */ 841da177e4SLinus Torvalds static int dec_rtc_set_mmss(unsigned long nowtime) 851da177e4SLinus Torvalds { 861da177e4SLinus Torvalds int retval = 0; 871da177e4SLinus Torvalds int real_seconds, real_minutes, cmos_minutes; 881da177e4SLinus Torvalds unsigned char save_control, save_freq_select; 891da177e4SLinus Torvalds 9053c2df2fSAtsushi Nemoto /* irq are locally disabled here */ 9153c2df2fSAtsushi Nemoto spin_lock(&rtc_lock); 921da177e4SLinus Torvalds /* tell the clock it's being set */ 931da177e4SLinus Torvalds save_control = CMOS_READ(RTC_CONTROL); 941da177e4SLinus Torvalds CMOS_WRITE((save_control | RTC_SET), RTC_CONTROL); 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /* stop and reset prescaler */ 971da177e4SLinus Torvalds save_freq_select = CMOS_READ(RTC_FREQ_SELECT); 981da177e4SLinus Torvalds CMOS_WRITE((save_freq_select | RTC_DIV_RESET2), RTC_FREQ_SELECT); 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds cmos_minutes = CMOS_READ(RTC_MINUTES); 1011da177e4SLinus Torvalds if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) 1021da177e4SLinus Torvalds cmos_minutes = BCD2BIN(cmos_minutes); 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* 1051da177e4SLinus Torvalds * since we're only adjusting minutes and seconds, 1061da177e4SLinus Torvalds * don't interfere with hour overflow. This avoids 1071da177e4SLinus Torvalds * messing with unknown time zones but requires your 1081da177e4SLinus Torvalds * RTC not to be off by more than 15 minutes 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds real_seconds = nowtime % 60; 1111da177e4SLinus Torvalds real_minutes = nowtime / 60; 1121da177e4SLinus Torvalds if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1) 1131da177e4SLinus Torvalds real_minutes += 30; /* correct for half hour time zone */ 1141da177e4SLinus Torvalds real_minutes %= 60; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds if (abs(real_minutes - cmos_minutes) < 30) { 1171da177e4SLinus Torvalds if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { 1181da177e4SLinus Torvalds real_seconds = BIN2BCD(real_seconds); 1191da177e4SLinus Torvalds real_minutes = BIN2BCD(real_minutes); 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds CMOS_WRITE(real_seconds, RTC_SECONDS); 1221da177e4SLinus Torvalds CMOS_WRITE(real_minutes, RTC_MINUTES); 1231da177e4SLinus Torvalds } else { 1241da177e4SLinus Torvalds printk(KERN_WARNING 1251da177e4SLinus Torvalds "set_rtc_mmss: can't update from %d to %d\n", 1261da177e4SLinus Torvalds cmos_minutes, real_minutes); 1271da177e4SLinus Torvalds retval = -1; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds /* The following flags have to be released exactly in this order, 1311da177e4SLinus Torvalds * otherwise the DS1287 will not reset the oscillator and will not 1321da177e4SLinus Torvalds * update precisely 500 ms later. You won't find this mentioned 1331da177e4SLinus Torvalds * in the Dallas Semiconductor data sheets, but who believes data 1341da177e4SLinus Torvalds * sheets anyway ... -- Markus Kuhn 1351da177e4SLinus Torvalds */ 1361da177e4SLinus Torvalds CMOS_WRITE(save_control, RTC_CONTROL); 1371da177e4SLinus Torvalds CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT); 13853c2df2fSAtsushi Nemoto spin_unlock(&rtc_lock); 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds return retval; 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds static int dec_timer_state(void) 1451da177e4SLinus Torvalds { 1461da177e4SLinus Torvalds return (CMOS_READ(RTC_REG_C) & RTC_PF) != 0; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds static void dec_timer_ack(void) 1501da177e4SLinus Torvalds { 1511da177e4SLinus Torvalds CMOS_READ(RTC_REG_C); /* Ack the RTC interrupt. */ 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds static unsigned int dec_ioasic_hpt_read(void) 1551da177e4SLinus Torvalds { 1561da177e4SLinus Torvalds /* 1571da177e4SLinus Torvalds * The free-running counter is 32-bit which is good for about 1581da177e4SLinus Torvalds * 2 minutes, 50 seconds at possible count rates of up to 25MHz. 1591da177e4SLinus Torvalds */ 1601da177e4SLinus Torvalds return ioasic_read(IO_REG_FCTR); 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds static void dec_ioasic_hpt_init(unsigned int count) 1641da177e4SLinus Torvalds { 1651da177e4SLinus Torvalds ioasic_write(IO_REG_FCTR, ioasic_read(IO_REG_FCTR) - count); 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds void __init dec_time_init(void) 1701da177e4SLinus Torvalds { 171d23ee8feSYoichi Yuasa rtc_mips_get_time = dec_rtc_get_time; 172d23ee8feSYoichi Yuasa rtc_mips_set_mmss = dec_rtc_set_mmss; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds mips_timer_state = dec_timer_state; 1751da177e4SLinus Torvalds mips_timer_ack = dec_timer_ack; 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds if (!cpu_has_counter && IOASIC) { 1781da177e4SLinus Torvalds /* For pre-R4k systems we use the I/O ASIC's counter. */ 1791da177e4SLinus Torvalds mips_hpt_read = dec_ioasic_hpt_read; 1801da177e4SLinus Torvalds mips_hpt_init = dec_ioasic_hpt_init; 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds /* Set up the rate of periodic DS1287 interrupts. */ 1841723b4a3SAtsushi Nemoto CMOS_WRITE(RTC_REF_CLCK_32KHZ | (16 - __ffs(HZ)), RTC_REG_A); 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds EXPORT_SYMBOL(do_settimeofday); 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds void __init dec_timer_setup(struct irqaction *irq) 1901da177e4SLinus Torvalds { 1911da177e4SLinus Torvalds setup_irq(dec_interrupt[DEC_IRQ_RTC], irq); 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds /* Enable periodic DS1287 interrupts. */ 1941da177e4SLinus Torvalds CMOS_WRITE(CMOS_READ(RTC_REG_B) | RTC_PIE, RTC_REG_B); 1951da177e4SLinus Torvalds } 196