xref: /openbmc/linux/arch/mips/dec/time.c (revision d23ee8fe)
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 
4053c2df2fSAtsushi Nemoto /*
4153c2df2fSAtsushi Nemoto  * Returns true if a clock update is in progress
4253c2df2fSAtsushi Nemoto  */
4353c2df2fSAtsushi Nemoto static inline unsigned char dec_rtc_is_updating(void)
4453c2df2fSAtsushi Nemoto {
4553c2df2fSAtsushi Nemoto 	unsigned char uip;
4653c2df2fSAtsushi Nemoto 	unsigned long flags;
4753c2df2fSAtsushi Nemoto 
4853c2df2fSAtsushi Nemoto 	spin_lock_irqsave(&rtc_lock, flags);
4953c2df2fSAtsushi Nemoto 	uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
5053c2df2fSAtsushi Nemoto 	spin_unlock_irqrestore(&rtc_lock, flags);
5153c2df2fSAtsushi Nemoto 	return uip;
5253c2df2fSAtsushi Nemoto }
5353c2df2fSAtsushi Nemoto 
541da177e4SLinus Torvalds static unsigned long dec_rtc_get_time(void)
551da177e4SLinus Torvalds {
561da177e4SLinus Torvalds 	unsigned int year, mon, day, hour, min, sec, real_year;
571da177e4SLinus Torvalds 	int i;
5853c2df2fSAtsushi Nemoto 	unsigned long flags;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	/* The Linux interpretation of the DS1287 clock register contents:
611da177e4SLinus Torvalds 	 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
621da177e4SLinus Torvalds 	 * RTC registers show the second which has precisely just started.
631da177e4SLinus Torvalds 	 * Let's hope other operating systems interpret the RTC the same way.
641da177e4SLinus Torvalds 	 */
651da177e4SLinus Torvalds 	/* read RTC exactly on falling edge of update flag */
661da177e4SLinus Torvalds 	for (i = 0; i < 1000000; i++)	/* may take up to 1 second... */
6753c2df2fSAtsushi Nemoto 		if (dec_rtc_is_updating())
681da177e4SLinus Torvalds 			break;
691da177e4SLinus Torvalds 	for (i = 0; i < 1000000; i++)	/* must try at least 2.228 ms */
7053c2df2fSAtsushi Nemoto 		if (!dec_rtc_is_updating())
711da177e4SLinus Torvalds 			break;
7253c2df2fSAtsushi Nemoto 	spin_lock_irqsave(&rtc_lock, flags);
731da177e4SLinus Torvalds 	/* Isn't this overkill?  UIP above should guarantee consistency */
741da177e4SLinus Torvalds 	do {
751da177e4SLinus Torvalds 		sec = CMOS_READ(RTC_SECONDS);
761da177e4SLinus Torvalds 		min = CMOS_READ(RTC_MINUTES);
771da177e4SLinus Torvalds 		hour = CMOS_READ(RTC_HOURS);
781da177e4SLinus Torvalds 		day = CMOS_READ(RTC_DAY_OF_MONTH);
791da177e4SLinus Torvalds 		mon = CMOS_READ(RTC_MONTH);
801da177e4SLinus Torvalds 		year = CMOS_READ(RTC_YEAR);
811da177e4SLinus Torvalds 	} while (sec != CMOS_READ(RTC_SECONDS));
821da177e4SLinus Torvalds 	if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
831da177e4SLinus Torvalds 		sec = BCD2BIN(sec);
841da177e4SLinus Torvalds 		min = BCD2BIN(min);
851da177e4SLinus Torvalds 		hour = BCD2BIN(hour);
861da177e4SLinus Torvalds 		day = BCD2BIN(day);
871da177e4SLinus Torvalds 		mon = BCD2BIN(mon);
881da177e4SLinus Torvalds 		year = BCD2BIN(year);
891da177e4SLinus Torvalds 	}
901da177e4SLinus Torvalds 	/*
911da177e4SLinus Torvalds 	 * The PROM will reset the year to either '72 or '73.
921da177e4SLinus Torvalds 	 * Therefore we store the real year separately, in one
931da177e4SLinus Torvalds 	 * of unused BBU RAM locations.
941da177e4SLinus Torvalds 	 */
951da177e4SLinus Torvalds 	real_year = CMOS_READ(RTC_DEC_YEAR);
9653c2df2fSAtsushi Nemoto 	spin_unlock_irqrestore(&rtc_lock, flags);
971da177e4SLinus Torvalds 	year += real_year - 72 + 2000;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	return mktime(year, mon, day, hour, min, sec);
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds /*
1031da177e4SLinus Torvalds  * In order to set the CMOS clock precisely, dec_rtc_set_mmss has to
1041da177e4SLinus Torvalds  * be called 500 ms after the second nowtime has started, because when
1051da177e4SLinus Torvalds  * nowtime is written into the registers of the CMOS clock, it will
1061da177e4SLinus Torvalds  * jump to the next second precisely 500 ms later.  Check the Dallas
1071da177e4SLinus Torvalds  * DS1287 data sheet for details.
1081da177e4SLinus Torvalds  */
1091da177e4SLinus Torvalds static int dec_rtc_set_mmss(unsigned long nowtime)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	int retval = 0;
1121da177e4SLinus Torvalds 	int real_seconds, real_minutes, cmos_minutes;
1131da177e4SLinus Torvalds 	unsigned char save_control, save_freq_select;
1141da177e4SLinus Torvalds 
11553c2df2fSAtsushi Nemoto 	/* irq are locally disabled here */
11653c2df2fSAtsushi Nemoto 	spin_lock(&rtc_lock);
1171da177e4SLinus Torvalds 	/* tell the clock it's being set */
1181da177e4SLinus Torvalds 	save_control = CMOS_READ(RTC_CONTROL);
1191da177e4SLinus Torvalds 	CMOS_WRITE((save_control | RTC_SET), RTC_CONTROL);
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	/* stop and reset prescaler */
1221da177e4SLinus Torvalds 	save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1231da177e4SLinus Torvalds 	CMOS_WRITE((save_freq_select | RTC_DIV_RESET2), RTC_FREQ_SELECT);
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	cmos_minutes = CMOS_READ(RTC_MINUTES);
1261da177e4SLinus Torvalds 	if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1271da177e4SLinus Torvalds 		cmos_minutes = BCD2BIN(cmos_minutes);
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	/*
1301da177e4SLinus Torvalds 	 * since we're only adjusting minutes and seconds,
1311da177e4SLinus Torvalds 	 * don't interfere with hour overflow. This avoids
1321da177e4SLinus Torvalds 	 * messing with unknown time zones but requires your
1331da177e4SLinus Torvalds 	 * RTC not to be off by more than 15 minutes
1341da177e4SLinus Torvalds 	 */
1351da177e4SLinus Torvalds 	real_seconds = nowtime % 60;
1361da177e4SLinus Torvalds 	real_minutes = nowtime / 60;
1371da177e4SLinus Torvalds 	if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
1381da177e4SLinus Torvalds 		real_minutes += 30;	/* correct for half hour time zone */
1391da177e4SLinus Torvalds 	real_minutes %= 60;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	if (abs(real_minutes - cmos_minutes) < 30) {
1421da177e4SLinus Torvalds 		if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1431da177e4SLinus Torvalds 			real_seconds = BIN2BCD(real_seconds);
1441da177e4SLinus Torvalds 			real_minutes = BIN2BCD(real_minutes);
1451da177e4SLinus Torvalds 		}
1461da177e4SLinus Torvalds 		CMOS_WRITE(real_seconds, RTC_SECONDS);
1471da177e4SLinus Torvalds 		CMOS_WRITE(real_minutes, RTC_MINUTES);
1481da177e4SLinus Torvalds 	} else {
1491da177e4SLinus Torvalds 		printk(KERN_WARNING
1501da177e4SLinus Torvalds 		       "set_rtc_mmss: can't update from %d to %d\n",
1511da177e4SLinus Torvalds 		       cmos_minutes, real_minutes);
1521da177e4SLinus Torvalds 		retval = -1;
1531da177e4SLinus Torvalds 	}
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	/* The following flags have to be released exactly in this order,
1561da177e4SLinus Torvalds 	 * otherwise the DS1287 will not reset the oscillator and will not
1571da177e4SLinus Torvalds 	 * update precisely 500 ms later.  You won't find this mentioned
1581da177e4SLinus Torvalds 	 * in the Dallas Semiconductor data sheets, but who believes data
1591da177e4SLinus Torvalds 	 * sheets anyway ...                           -- Markus Kuhn
1601da177e4SLinus Torvalds 	 */
1611da177e4SLinus Torvalds 	CMOS_WRITE(save_control, RTC_CONTROL);
1621da177e4SLinus Torvalds 	CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
16353c2df2fSAtsushi Nemoto 	spin_unlock(&rtc_lock);
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	return retval;
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds static int dec_timer_state(void)
1701da177e4SLinus Torvalds {
1711da177e4SLinus Torvalds 	return (CMOS_READ(RTC_REG_C) & RTC_PF) != 0;
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds static void dec_timer_ack(void)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	CMOS_READ(RTC_REG_C);			/* Ack the RTC interrupt.  */
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds static unsigned int dec_ioasic_hpt_read(void)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	/*
1821da177e4SLinus Torvalds 	 * The free-running counter is 32-bit which is good for about
1831da177e4SLinus Torvalds 	 * 2 minutes, 50 seconds at possible count rates of up to 25MHz.
1841da177e4SLinus Torvalds 	 */
1851da177e4SLinus Torvalds 	return ioasic_read(IO_REG_FCTR);
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds static void dec_ioasic_hpt_init(unsigned int count)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	ioasic_write(IO_REG_FCTR, ioasic_read(IO_REG_FCTR) - count);
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds void __init dec_time_init(void)
1951da177e4SLinus Torvalds {
196d23ee8feSYoichi Yuasa 	rtc_mips_get_time = dec_rtc_get_time;
197d23ee8feSYoichi Yuasa 	rtc_mips_set_mmss = dec_rtc_set_mmss;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	mips_timer_state = dec_timer_state;
2001da177e4SLinus Torvalds 	mips_timer_ack = dec_timer_ack;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	if (!cpu_has_counter && IOASIC) {
2031da177e4SLinus Torvalds 		/* For pre-R4k systems we use the I/O ASIC's counter.  */
2041da177e4SLinus Torvalds 		mips_hpt_read = dec_ioasic_hpt_read;
2051da177e4SLinus Torvalds 		mips_hpt_init = dec_ioasic_hpt_init;
2061da177e4SLinus Torvalds 	}
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	/* Set up the rate of periodic DS1287 interrupts.  */
2091da177e4SLinus Torvalds 	CMOS_WRITE(RTC_REF_CLCK_32KHZ | (16 - LOG_2_HZ), RTC_REG_A);
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds EXPORT_SYMBOL(do_settimeofday);
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds void __init dec_timer_setup(struct irqaction *irq)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	setup_irq(dec_interrupt[DEC_IRQ_RTC], irq);
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	/* Enable periodic DS1287 interrupts.  */
2191da177e4SLinus Torvalds 	CMOS_WRITE(CMOS_READ(RTC_REG_B) | RTC_PIE, RTC_REG_B);
2201da177e4SLinus Torvalds }
221