Real Time Clock (RTC) Drivers for Linux

https://www.kernel.org/doc/html/latest/admin-guide/rtc.html

Real Time Clock (RTC) Drivers for Linux

When Linux developers talk about a “Real Time Clock”, they usually mean something that tracks wall clock time and is battery backed so that it works even with system power off. Such clocks will normally not track the local time zone or daylight savings time – unless they dual boot with MS-Windows – but will instead be set to Coordinated Universal Time (UTC, formerly “Greenwich Mean Time”).

When a Linux developer says "Real Time Clock", it usually means that it follows the wall clock time and is backed up by a battery to keep it running even when system power is off. Such clocks usually do not follow the local timezone or daylight savings time (unless you have a dual boot with MS-Windows). Instead, it is set to Coordinated Universal Time (UTC, traditional "Greenwich Mean Time).

The newest non-PC hardware tends to just count seconds, like the time(2) system call reports, but RTCs also very commonly represent time using the Gregorian calendar and 24 hour time, as reported by gmtime(3).

Modern non-PC hardware tends to count in seconds, as reported by time (2) system call. However, RTCs are also very common to use the Gregorian calendar and 24-hour time to represent time, as reported in gmtime (3).

Linux has two largely-compatible userspace RTC API families you may need to know about:

Linux has two major compatible user-space RTC API familiy that you should be aware of.

・ / Dev / rtc… is the RTC provided by PC compatible systems, so it ’s not very portable to non-x86 systems. ・ / Dev / rtc0, / dev / rtc1… are part of a framework that ’s supported by a wide variety of RTC chips on all systems.

-/ Dev / rtc ... RTC provided by a PC compatible system. Not portable on non-x86 systems. -/ Dev / rtc0, / dev / rtc1 ... A part of the framework supported by a wide range of RTC chips on all systems.

Programmers need to understand that the PC/AT functionality is not always available, and some systems can do much more. That is, the RTCs use the same API to make requests in both RTC frameworks (using different filenames of course), but the hardware may not offer the same functionality. For example, not every RTC is hooked up to an IRQ, so they can’t all issue alarms; and where standard PC RTCs can only issue an alarm up to 24 hours in the future, other hardware may be able to schedule one any time in the upcoming century.

Programmers must understand that PC / AT functionality is not always enabled. And depending on the system, you can do more than that. This means that the RTC uses the same API for requests from two RTC frameworks. However, the hardware does not provide the same functionality. For example, not all RTCs can be hooked with IRQs and cannot handle alarm issues. Also, the RTC on a standard PC can set alarms for the next 24 hours, but other hardware can schedule more for the future.

Old PC/AT-Compatible driver: /dev/rtc

All PCs (even Alpha machines) have a Real Time Clock built into them. Usually they are built into the chipset of the computer, but some may actually have a Motorola MC146818 (or clone) on the board. This is the clock that keeps the date and time while your computer is turned off.

All PCs (including Alpha machines) have a built-in Real Time Clock. Usually these will either be built into your computer's chipset or have a Motorola MC146818 (or a clone of it) on the board. This is a clock that holds the date and time while the computer is turned off.

ACPI has standardized that MC146818 functionality, and extended it in a few ways (enabling longer alarm periods, and wake-from-hibernate). That functionality is NOT exposed in the old driver.

ACPI has standardized the functionality of MC146818 and also made some enhancements (extension of alarm period and wake-from-hibernate). This functionality is not provided by older drivers.

However it can also be used to generate signals from a slow 2Hz to a relatively fast 8192Hz, in increments of powers of two. These signals are reported by interrupt number 8. (Oh! So that is what IRQ 8 is for…) It can also function as a 24hr alarm, raising IRQ 8 when the alarm goes off. The alarm can also be programmed to only check any subset of the three programmable values, meaning that it could be set to ring on the 30th second of the 30th minute of every hour, for example. The clock can also be set to generate an interrupt upon every clock update, thus generating a 1Hz signal.

However, this can be used to generate a signal, in the slowest case 2Hz, relatively fast up to 8192Hz, in increments of factorial 2. This signal is signaled with interrupt number 8 (Oh! That's the role of IRQ 8). It can function as a 24-hour alarm by dropping the IRQ 8 signal when the alarm arrives. The Alarm can also check only a subset of the three mutable variables, i.e. it can be set to sound at 30/30 seconds per hour, for example. The clock can also generate an interrupt each time the clock is updated, in which case it will generate a 1Hz signal.

The interrupts are reported via /dev/rtc (major 10, minor 135, read only character device) in the form of an unsigned long. The low byte contains the type of interrupt (update-done, alarm-rang, or periodic) that was raised, and the remaining bytes contain the number of interrupts since the last read. Status information is reported through the pseudo-file /proc/driver/rtc if the /proc filesystem was enabled. The driver has built in locking so that only one process is allowed to have the /dev/rtc interface open at a time.

Interrupts are signaled in the form of unsigned long via / dev / rtc (major 10, minor 135, read only character device). The low byte contains the occurrence of the interrupt type (update-done, alarm-rang, or periodic), and the remaining bytes contain the number of interrupts since the last read. If / proc filesystem is enabled, Status information will be communicated via the pseudofile / proc / driver / rtc. The driver has a built-in lock, so only one process at a time can open / dev / rtc.

A user process can monitor these interrupts by doing a read(2) or a select(2) on /dev/rtc – either will block/stop the user process until the next interrupt is received. This is useful for things like reasonably high frequency data acquisition where one doesn’t want to burn up 100% CPU by polling gettimeofday etc. etc.

The user process can monitor these interrupts with read (2) or select (2) for / dev / rtc. These will block / stop the user process until the next interrupt. This is useful for frequently acquiring data when you do not want to keep turning at 100% CPU usage by polling gettimeofday etc.

At high frequencies, or under high loads, the user process should check the number of interrupts received since the last read to determine if there has been any interrupt “pileup” so to speak. Just for reference, a typical 486-33 running a tight read loop on /dev/rtc will start to suffer occasional interrupt pileup (i.e. > 1 IRQ event since last read) for frequencies above 1024Hz. So you really should check the high bytes of the value you read, especially at frequencies above that of the normal timer interrupt, which is 100Hz.

At high frequency or high append, the user process must check the number of interrupts received since the last read to determine if there was a so-called "mountain" of interrupts. For reference, a typical 486-33, which reads / dev / rtc rigorously, occasionally causes interrupt pileups at frequencies above 1024Hz (that is, it seems to be 33MHz on the i486). 1 IRQ event is greater than the last read). Therefore, it is necessary to check the high-order byte of the read value, especially if the normal timer interrupt has a frequency exceeding 100Hz.

Programming and/or enabling interrupt frequencies greater than 64Hz is only allowed by root. This is perhaps a bit conservative, but we don’t want an evil user generating lots of IRQs on a slow 386sx-16, where it might have a negative impact on performance. This 64Hz limit can be changed by writing a different value to /proc/sys/dev/rtc/max-user-freq. Note that the interrupt handler is only a few lines of code to minimize any possibility of this effect.

Only root is allowed to enable programming and / or interrupts above 64Hz. This may be a bit conservative. However, on slow 386sx-16, a malicious user's high volume of IRQs can have a negative impact on performance. This 64Hz limit can be changed by rewriting / proc / sys / dev / rtc / max-user-freq. Note that the interrupt handler is just a few lines of code to minimize this effect.

Also, if the kernel time is synchronized with an external source, the kernel will write the time back to the CMOS clock every 11 minutes. In the process of doing this, the kernel briefly turns off RTC periodic interrupts, so be aware of this if you are doing serious work. If you don’t synchronize the kernel time with an external source (via ntp or whatever) then the kernel will keep its hands off the RTC, allowing you exclusive access to the device for your applications.

In addition, if the kernel time is synchronized by an external source, the kernel will write back the COMS clock every 11 minutes. During this process, the kernel disables RTC periodic interrupts. So be careful if you are doing serious work. If you do not need to correct the kernel time on an external source (eg ntp or otherwise), the kernel will take your hand off the RTC and allow exclusive access to your application.

The alarm and/or interrupt frequency are programmed into the RTC via various ioctl(2) calls as listed in ./include/linux/rtc.h . Rather than write 50 pages describing the ioctl() and so on, it is perhaps more useful to include a small test program that demonstrates how to use them, and demonstrates the features of the driver. This is probably a lot more useful to people interested in writing applications that will be using this driver. See the code at the end of this document.

The alarm and interrupt frequency for the RTC can be programmed by the various ioctl (2) calls listed in include / linux / rtc.h. Rather than writing a 50-page description of ioctl (), it's probably more useful to demonstrate a small test program and driver features that explain how to use it through demos. This is probably useful for many people writing applications that utilize this driver. See the code at the end of this document.

(The original /dev/rtc driver was written by Paul Gortmaker.)

New portable “RTC Class” drivers: /dev/rtcN

Because Linux supports many non-ACPI and non-PC platforms, some of which have more than one RTC style clock, it needed a more portable solution than expecting a single battery-backed MC146818 clone on every system. Accordingly, a new “RTC Class” framework has been defined. It offers three different userspace interfaces:

Linux supports non-PC platforms with many non-ACPIs, and they have one or more RTC style clocks, so on all systems you would expect one battery backup MC146818 clone. , I needed a portable solution. Therefore, a new "RTC Class" framework has been defined. It provides three different user interfaces.

・ / Dev / rtcN… much the same as the older / dev / rtc interface · / Sys / class / rtc / rtcN… sysfs attributes support readonly access to some RTC attributes. ・ / Proc / driver / rtc… the system clock RTC may expose itself using a procfs interface. If there is no RTC for the system clock, rtc0 is used by default. More information is (currently) shown here than through sysfs.

・ / Dev / rtcN… Similar to / dev / rtc interface · / Sys / class / rtc / rtcN… sysfs attributes that allow readonly access to RTC attributes · / Proc / driver / rtc… The system clock RTC may need to expose itself via the procfs interface. If there is no RTC for system clock, rtc0 is used by default. More information is presented here than via sysfs (currently).

The RTC Class framework supports a wide variety of RTCs, ranging from those integrated into embeddable system-on-chip (SOC) processors to discrete chips using I2C, SPI, or some other bus to communicate with the host CPU. There’s even support for PC-style RTCs … including the features exposed on newer PCs through ACPI.

The RTC Class framework supports a very wide range of RTCs. From those integrated into embedded system-on-chip (SOC) processors to those connected to buses that can communicate with I2C, SPI or other host CPUs. There is even support for PC-style RTC, including features exposed on new PCs via ACPI.

The new framework also removes the “one RTC per system” restriction. For example, maybe the low-power battery-backed RTC is a discrete I2C chip, but a high functionality RTC is integrated into the SOC. That system might read the system clock from the discrete RTC, but use the integrated one for all other tasks, because of its greater functionality.

The new framework also removes the "one RTC per system" constraint. For example, the power-saving backup battery RTC is a separate I2C chip, and the sophisticated RTC is integrated into the SOC. In this system, the system clock reads from a separate RTC, but due to its high functionality, it will use an integrated RTC for other processing.

Check out tools/testing/selftests/rtc/rtctest.c for an example usage of the ioctl interface.

Check tools /testing/selftests/rtc/rtctest.c for an example of how to use the ioctl interface.


Originally, it is a part of the Linux Kernel source code, so it will be treated as GPLv2 (recognition that it should be).

https://www.kernel.org/doc/html/latest/index.html

Licensing documentation

The following describes the license of the Linux kernel source code (GPLv2), how to properly mark the license of individual files in the source tree, as well as links to the full license text.

https://www.kernel.org/doc/html/latest/process/license-rules.html#kernel-licensing

Recommended Posts

Real Time Clock (RTC) Drivers for Linux
Let's try Linux for the first time
pyenv for linux
For the first time, I learned about Unix (Linux).
I will install Arch Linux for the time being.
Library for measuring execution time in Linux C applications
Linux kernel build time
What is Linux for?
Linux command for self-collection
If you're learning Linux for the first time, do this!