xref: /openbmc/linux/arch/mips/dec/time.c (revision f06e7aa4)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992, 1995  Linus Torvalds
41da177e4SLinus Torvalds  *  Copyright (C) 2000, 2003  Maciej W. Rozycki
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This file contains the time handling details for PC-style clocks as
71da177e4SLinus Torvalds  * found in some MIPS systems.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds #include <linux/bcd.h>
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds #include <linux/mc146818rtc.h>
131da177e4SLinus Torvalds #include <linux/param.h>
141da177e4SLinus Torvalds 
156457d9fcSYoichi Yuasa #include <asm/cpu-features.h>
166457d9fcSYoichi Yuasa #include <asm/ds1287.h>
171da177e4SLinus Torvalds #include <asm/time.h>
181da177e4SLinus Torvalds #include <asm/dec/interrupts.h>
191da177e4SLinus Torvalds #include <asm/dec/ioasic.h>
201da177e4SLinus Torvalds #include <asm/dec/machtype.h>
211da177e4SLinus Torvalds 
read_persistent_clock64(struct timespec64 * ts)2209adad17SBaolin Wang void read_persistent_clock64(struct timespec64 *ts)
231da177e4SLinus Torvalds {
241da177e4SLinus Torvalds 	unsigned int year, mon, day, hour, min, sec, real_year;
2553c2df2fSAtsushi Nemoto 	unsigned long flags;
261da177e4SLinus Torvalds 
2753c2df2fSAtsushi Nemoto 	spin_lock_irqsave(&rtc_lock, flags);
28ddcabb4fSMatt Mackall 
291da177e4SLinus Torvalds 	do {
301da177e4SLinus Torvalds 		sec = CMOS_READ(RTC_SECONDS);
311da177e4SLinus Torvalds 		min = CMOS_READ(RTC_MINUTES);
321da177e4SLinus Torvalds 		hour = CMOS_READ(RTC_HOURS);
331da177e4SLinus Torvalds 		day = CMOS_READ(RTC_DAY_OF_MONTH);
341da177e4SLinus Torvalds 		mon = CMOS_READ(RTC_MONTH);
351da177e4SLinus Torvalds 		year = CMOS_READ(RTC_YEAR);
36ddcabb4fSMatt Mackall 		/*
37ddcabb4fSMatt Mackall 		 * The PROM will reset the year to either '72 or '73.
38ddcabb4fSMatt Mackall 		 * Therefore we store the real year separately, in one
39ddcabb4fSMatt Mackall 		 * of unused BBU RAM locations.
40ddcabb4fSMatt Mackall 		 */
41ddcabb4fSMatt Mackall 		real_year = CMOS_READ(RTC_DEC_YEAR);
421da177e4SLinus Torvalds 	} while (sec != CMOS_READ(RTC_SECONDS));
43ddcabb4fSMatt Mackall 
44ddcabb4fSMatt Mackall 	spin_unlock_irqrestore(&rtc_lock, flags);
45ddcabb4fSMatt Mackall 
461da177e4SLinus Torvalds 	if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
4702112dbcSAdrian Bunk 		sec = bcd2bin(sec);
4802112dbcSAdrian Bunk 		min = bcd2bin(min);
4902112dbcSAdrian Bunk 		hour = bcd2bin(hour);
5002112dbcSAdrian Bunk 		day = bcd2bin(day);
5102112dbcSAdrian Bunk 		mon = bcd2bin(mon);
5202112dbcSAdrian Bunk 		year = bcd2bin(year);
531da177e4SLinus Torvalds 	}
54ddcabb4fSMatt Mackall 
551da177e4SLinus Torvalds 	year += real_year - 72 + 2000;
561da177e4SLinus Torvalds 
5709adad17SBaolin Wang 	ts->tv_sec = mktime64(year, mon, day, hour, min, sec);
58d4f587c6SMartin Schwidefsky 	ts->tv_nsec = 0;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds /*
62f06e7aa4SBaolin Wang  * In order to set the CMOS clock precisely, update_persistent_clock64 has to
631da177e4SLinus Torvalds  * be called 500 ms after the second nowtime has started, because when
641da177e4SLinus Torvalds  * nowtime is written into the registers of the CMOS clock, it will
651da177e4SLinus Torvalds  * jump to the next second precisely 500 ms later.  Check the Dallas
661da177e4SLinus Torvalds  * DS1287 data sheet for details.
671da177e4SLinus Torvalds  */
update_persistent_clock64(struct timespec64 now)68f06e7aa4SBaolin Wang int update_persistent_clock64(struct timespec64 now)
691da177e4SLinus Torvalds {
70f06e7aa4SBaolin Wang 	time64_t nowtime = now.tv_sec;
711da177e4SLinus Torvalds 	int retval = 0;
721da177e4SLinus Torvalds 	int real_seconds, real_minutes, cmos_minutes;
731da177e4SLinus Torvalds 	unsigned char save_control, save_freq_select;
741da177e4SLinus Torvalds 
7553c2df2fSAtsushi Nemoto 	/* irq are locally disabled here */
7653c2df2fSAtsushi Nemoto 	spin_lock(&rtc_lock);
771da177e4SLinus Torvalds 	/* tell the clock it's being set */
781da177e4SLinus Torvalds 	save_control = CMOS_READ(RTC_CONTROL);
791da177e4SLinus Torvalds 	CMOS_WRITE((save_control | RTC_SET), RTC_CONTROL);
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	/* stop and reset prescaler */
821da177e4SLinus Torvalds 	save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
831da177e4SLinus Torvalds 	CMOS_WRITE((save_freq_select | RTC_DIV_RESET2), RTC_FREQ_SELECT);
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	cmos_minutes = CMOS_READ(RTC_MINUTES);
861da177e4SLinus Torvalds 	if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
8702112dbcSAdrian Bunk 		cmos_minutes = bcd2bin(cmos_minutes);
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	/*
901da177e4SLinus Torvalds 	 * since we're only adjusting minutes and seconds,
911da177e4SLinus Torvalds 	 * don't interfere with hour overflow. This avoids
921da177e4SLinus Torvalds 	 * messing with unknown time zones but requires your
931da177e4SLinus Torvalds 	 * RTC not to be off by more than 15 minutes
941da177e4SLinus Torvalds 	 */
95f06e7aa4SBaolin Wang 	real_minutes = div_s64_rem(nowtime, 60, &real_seconds);
961da177e4SLinus Torvalds 	if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
971da177e4SLinus Torvalds 		real_minutes += 30;	/* correct for half hour time zone */
981da177e4SLinus Torvalds 	real_minutes %= 60;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	if (abs(real_minutes - cmos_minutes) < 30) {
1011da177e4SLinus Torvalds 		if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
10202112dbcSAdrian Bunk 			real_seconds = bin2bcd(real_seconds);
10302112dbcSAdrian Bunk 			real_minutes = bin2bcd(real_minutes);
1041da177e4SLinus Torvalds 		}
1051da177e4SLinus Torvalds 		CMOS_WRITE(real_seconds, RTC_SECONDS);
1061da177e4SLinus Torvalds 		CMOS_WRITE(real_minutes, RTC_MINUTES);
1071da177e4SLinus Torvalds 	} else {
1083e5c1240SStephen Hemminger 		printk_once(KERN_NOTICE
1091da177e4SLinus Torvalds 		       "set_rtc_mmss: can't update from %d to %d\n",
1101da177e4SLinus Torvalds 		       cmos_minutes, real_minutes);
1111da177e4SLinus Torvalds 		retval = -1;
1121da177e4SLinus Torvalds 	}
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	/* The following flags have to be released exactly in this order,
1151da177e4SLinus Torvalds 	 * otherwise the DS1287 will not reset the oscillator and will not
1161da177e4SLinus Torvalds 	 * update precisely 500 ms later.  You won't find this mentioned
1171da177e4SLinus Torvalds 	 * in the Dallas Semiconductor data sheets, but who believes data
1181da177e4SLinus Torvalds 	 * sheets anyway ...                           -- Markus Kuhn
1191da177e4SLinus Torvalds 	 */
1201da177e4SLinus Torvalds 	CMOS_WRITE(save_control, RTC_CONTROL);
1211da177e4SLinus Torvalds 	CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
12253c2df2fSAtsushi Nemoto 	spin_unlock(&rtc_lock);
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	return retval;
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
plat_time_init(void)1274b550488SRalf Baechle void __init plat_time_init(void)
1281da177e4SLinus Torvalds {
129daed1285SMaciej W. Rozycki 	int ioasic_clock = 0;
1306457d9fcSYoichi Yuasa 	u32 start, end;
1318533966aSMaciej W. Rozycki 	int i = HZ / 8;
1321da177e4SLinus Torvalds 
1336457d9fcSYoichi Yuasa 	/* Set up the rate of periodic DS1287 interrupts. */
1346457d9fcSYoichi Yuasa 	ds1287_set_base_clock(HZ);
1356457d9fcSYoichi Yuasa 
136daed1285SMaciej W. Rozycki 	/* On some I/O ASIC systems we have the I/O ASIC's counter.  */
137daed1285SMaciej W. Rozycki 	if (IOASIC)
138daed1285SMaciej W. Rozycki 		ioasic_clock = dec_ioasic_clocksource_init() == 0;
1396457d9fcSYoichi Yuasa 	if (cpu_has_counter) {
1408533966aSMaciej W. Rozycki 		ds1287_timer_state();
1416457d9fcSYoichi Yuasa 		while (!ds1287_timer_state())
1426457d9fcSYoichi Yuasa 			;
1436457d9fcSYoichi Yuasa 
1446457d9fcSYoichi Yuasa 		start = read_c0_count();
1456457d9fcSYoichi Yuasa 
1466457d9fcSYoichi Yuasa 		while (i--)
1476457d9fcSYoichi Yuasa 			while (!ds1287_timer_state())
1486457d9fcSYoichi Yuasa 				;
1496457d9fcSYoichi Yuasa 
1506457d9fcSYoichi Yuasa 		end = read_c0_count();
1516457d9fcSYoichi Yuasa 
1528533966aSMaciej W. Rozycki 		mips_hpt_frequency = (end - start) * 8;
1536457d9fcSYoichi Yuasa 		printk(KERN_INFO "MIPS counter frequency %dHz\n",
1546457d9fcSYoichi Yuasa 			mips_hpt_frequency);
155daed1285SMaciej W. Rozycki 
156daed1285SMaciej W. Rozycki 		/*
157daed1285SMaciej W. Rozycki 		 * All R4k DECstations suffer from the CP0 Count erratum,
158daed1285SMaciej W. Rozycki 		 * so we can't use the timer as a clock source, and a clock
159daed1285SMaciej W. Rozycki 		 * event both at a time.  An accurate wall clock is more
160daed1285SMaciej W. Rozycki 		 * important than a high-precision interval timer so only
161daed1285SMaciej W. Rozycki 		 * use the timer as a clock source, and not a clock event
162daed1285SMaciej W. Rozycki 		 * if there's no I/O ASIC counter available to serve as a
163daed1285SMaciej W. Rozycki 		 * clock source.
164daed1285SMaciej W. Rozycki 		 */
165daed1285SMaciej W. Rozycki 		if (!ioasic_clock) {
166daed1285SMaciej W. Rozycki 			init_r4k_clocksource();
167daed1285SMaciej W. Rozycki 			mips_hpt_frequency = 0;
168daed1285SMaciej W. Rozycki 		}
169daed1285SMaciej W. Rozycki 	}
1701da177e4SLinus Torvalds 
1716457d9fcSYoichi Yuasa 	ds1287_clockevent_init(dec_interrupt[DEC_IRQ_RTC]);
1721da177e4SLinus Torvalds }
173