drivers/rtc/rtc-mc146818-lib.c

Source file repositories/reference/linux-study-clean/drivers/rtc/rtc-mc146818-lib.c

File Facts

System
Linux kernel
Corpus path
drivers/rtc/rtc-mc146818-lib.c
Extension
.c
Size
8050 bytes
Lines
310
Domain
Driver Families
Bucket
drivers/rtc
Inferred role
Driver Families: exported/initcall integration point
Status
integration implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

struct mc146818_get_time_callback_param {
	struct rtc_time *time;
	unsigned char ctrl;
#ifdef CONFIG_ACPI
	unsigned char century;
#endif
#ifdef CONFIG_MACH_DECSTATION
	unsigned int real_year;
#endif
};

static void mc146818_get_time_callback(unsigned char seconds, void *param_in)
{
	struct mc146818_get_time_callback_param *p = param_in;

	/*
	 * 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.
	 */
	p->time->tm_sec = seconds;
	p->time->tm_min = CMOS_READ(RTC_MINUTES);
	p->time->tm_hour = CMOS_READ(RTC_HOURS);
	p->time->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
	p->time->tm_mon = CMOS_READ(RTC_MONTH);
	p->time->tm_year = CMOS_READ(RTC_YEAR);
#ifdef CONFIG_MACH_DECSTATION
	p->real_year = CMOS_READ(RTC_DEC_YEAR);
#endif
#ifdef CONFIG_ACPI
	if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
	    acpi_gbl_FADT.century) {
		p->century = CMOS_READ(acpi_gbl_FADT.century);
	} else {
		p->century = 0;
	}
#endif

	p->ctrl = CMOS_READ(RTC_CONTROL);
}

/**
 * mc146818_get_time - Get the current time from the RTC
 * @time: pointer to struct rtc_time to store the current time
 * @timeout: timeout value in ms
 *
 * This function reads the current time from the RTC and stores it in the
 * provided struct rtc_time. The timeout parameter specifies the maximum
 * time to wait for the RTC to become ready.
 *
 * Return: 0 on success, -ETIMEDOUT if the RTC did not become ready within
 * the specified timeout, or another error code if an error occurred.
 */
int mc146818_get_time(struct rtc_time *time, int timeout)
{
	struct mc146818_get_time_callback_param p = {
		.time = time
	};

	if (!mc146818_avoid_UIP(mc146818_get_time_callback, timeout, &p)) {
		memset(time, 0, sizeof(*time));
		return -ETIMEDOUT;
	}

	if (!(p.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);
#ifdef CONFIG_ACPI
		p.century = bcd2bin(p.century);
#endif
	}

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

#ifdef CONFIG_ACPI
	if (p.century > 19)
		time->tm_year += (p.century - 19) * 100;
#endif

	/*
	 * Account for differences between how the RTC uses the values
	 * and how they are defined in a struct rtc_time;

Annotation

Implementation Notes