unsigned int mc146818_get_time()

in rtc-mc146818-lib.c [11:120]


unsigned int mc146818_get_time(struct rtc_time *time)
{
	unsigned char ctrl;
	unsigned long flags;
	unsigned char century = 0;
	bool retry;

#ifdef CONFIG_MACH_DECSTATION
	unsigned int real_year;
#endif

again:
	spin_lock_irqsave(&rtc_lock, flags);
	/* Ensure that the RTC is accessible. Bit 6 must be 0! */
	if (WARN_ON_ONCE((CMOS_READ(RTC_VALID) & 0x40) != 0)) {
		spin_unlock_irqrestore(&rtc_lock, flags);
		memset(time, 0xff, sizeof(*time));
		return 0;
	}

	/*
	 * Check whether there is an update in progress during which the
	 * readout is unspecified. The maximum update time is ~2ms. Poll
	 * every msec for completion.
	 *
	 * Store the second value before checking UIP so a long lasting NMI
	 * which happens to hit after the UIP check cannot make an update
	 * cycle invisible.
	 */
	time->tm_sec = CMOS_READ(RTC_SECONDS);

	if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) {
		spin_unlock_irqrestore(&rtc_lock, flags);
		mdelay(1);
		goto again;
	}

	/* Revalidate the above readout */
	if (time->tm_sec != CMOS_READ(RTC_SECONDS)) {
		spin_unlock_irqrestore(&rtc_lock, flags);
		goto again;
	}

	/*
	 * Only the values that we read from the RTC are set. We leave
	 * tm_wday, tm_yday and tm_isdst untouched. Even though the
	 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
	 * by the RTC when initially set to a non-zero value.
	 */
	time->tm_min = CMOS_READ(RTC_MINUTES);
	time->tm_hour = CMOS_READ(RTC_HOURS);
	time->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
	time->tm_mon = CMOS_READ(RTC_MONTH);
	time->tm_year = CMOS_READ(RTC_YEAR);
#ifdef CONFIG_MACH_DECSTATION
	real_year = CMOS_READ(RTC_DEC_YEAR);
#endif
#ifdef CONFIG_ACPI
	if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
	    acpi_gbl_FADT.century)
		century = CMOS_READ(acpi_gbl_FADT.century);
#endif
	ctrl = CMOS_READ(RTC_CONTROL);
	/*
	 * Check for the UIP bit again. If it is set now then
	 * the above values may contain garbage.
	 */
	retry = CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP;
	/*
	 * A NMI might have interrupted the above sequence so check whether
	 * the seconds value has changed which indicates that the NMI took
	 * longer than the UIP bit was set. Unlikely, but possible and
	 * there is also virt...
	 */
	retry |= time->tm_sec != CMOS_READ(RTC_SECONDS);

	spin_unlock_irqrestore(&rtc_lock, flags);

	if (retry)
		goto again;

	if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
	{
		time->tm_sec = bcd2bin(time->tm_sec);
		time->tm_min = bcd2bin(time->tm_min);
		time->tm_hour = bcd2bin(time->tm_hour);
		time->tm_mday = bcd2bin(time->tm_mday);
		time->tm_mon = bcd2bin(time->tm_mon);
		time->tm_year = bcd2bin(time->tm_year);
		century = bcd2bin(century);
	}

#ifdef CONFIG_MACH_DECSTATION
	time->tm_year += real_year - 72;
#endif

	if (century > 20)
		time->tm_year += (century - 19) * 100;

	/*
	 * Account for differences between how the RTC uses the values
	 * and how they are defined in a struct rtc_time;
	 */
	if (time->tm_year <= 69)
		time->tm_year += 100;

	time->tm_mon--;

	return RTC_24H;
}