162306a36Sopenharmony_ci=======================================
262306a36Sopenharmony_ciReal Time Clock (RTC) Drivers for Linux
362306a36Sopenharmony_ci=======================================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ciWhen Linux developers talk about a "Real Time Clock", they usually mean
662306a36Sopenharmony_cisomething that tracks wall clock time and is battery backed so that it
762306a36Sopenharmony_ciworks even with system power off.  Such clocks will normally not track
862306a36Sopenharmony_cithe local time zone or daylight savings time -- unless they dual boot
962306a36Sopenharmony_ciwith MS-Windows -- but will instead be set to Coordinated Universal Time
1062306a36Sopenharmony_ci(UTC, formerly "Greenwich Mean Time").
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ciThe newest non-PC hardware tends to just count seconds, like the time(2)
1362306a36Sopenharmony_cisystem call reports, but RTCs also very commonly represent time using
1462306a36Sopenharmony_cithe Gregorian calendar and 24 hour time, as reported by gmtime(3).
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ciLinux has two largely-compatible userspace RTC API families you may
1762306a36Sopenharmony_cineed to know about:
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci    *	/dev/rtc ... is the RTC provided by PC compatible systems,
2062306a36Sopenharmony_ci	so it's not very portable to non-x86 systems.
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci    *	/dev/rtc0, /dev/rtc1 ... are part of a framework that's
2362306a36Sopenharmony_ci	supported by a wide variety of RTC chips on all systems.
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciProgrammers need to understand that the PC/AT functionality is not
2662306a36Sopenharmony_cialways available, and some systems can do much more.  That is, the
2762306a36Sopenharmony_ciRTCs use the same API to make requests in both RTC frameworks (using
2862306a36Sopenharmony_cidifferent filenames of course), but the hardware may not offer the
2962306a36Sopenharmony_cisame functionality.  For example, not every RTC is hooked up to an
3062306a36Sopenharmony_ciIRQ, so they can't all issue alarms; and where standard PC RTCs can
3162306a36Sopenharmony_cionly issue an alarm up to 24 hours in the future, other hardware may
3262306a36Sopenharmony_cibe able to schedule one any time in the upcoming century.
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ciOld PC/AT-Compatible driver:  /dev/rtc
3662306a36Sopenharmony_ci--------------------------------------
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ciAll PCs (even Alpha machines) have a Real Time Clock built into them.
3962306a36Sopenharmony_ciUsually they are built into the chipset of the computer, but some may
4062306a36Sopenharmony_ciactually have a Motorola MC146818 (or clone) on the board. This is the
4162306a36Sopenharmony_ciclock that keeps the date and time while your computer is turned off.
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ciACPI has standardized that MC146818 functionality, and extended it in
4462306a36Sopenharmony_cia few ways (enabling longer alarm periods, and wake-from-hibernate).
4562306a36Sopenharmony_ciThat functionality is NOT exposed in the old driver.
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciHowever it can also be used to generate signals from a slow 2Hz to a
4862306a36Sopenharmony_cirelatively fast 8192Hz, in increments of powers of two. These signals
4962306a36Sopenharmony_ciare reported by interrupt number 8. (Oh! So *that* is what IRQ 8 is
5062306a36Sopenharmony_cifor...) It can also function as a 24hr alarm, raising IRQ 8 when the
5162306a36Sopenharmony_cialarm goes off. The alarm can also be programmed to only check any
5262306a36Sopenharmony_cisubset of the three programmable values, meaning that it could be set to
5362306a36Sopenharmony_ciring on the 30th second of the 30th minute of every hour, for example.
5462306a36Sopenharmony_ciThe clock can also be set to generate an interrupt upon every clock
5562306a36Sopenharmony_ciupdate, thus generating a 1Hz signal.
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ciThe interrupts are reported via /dev/rtc (major 10, minor 135, read only
5862306a36Sopenharmony_cicharacter device) in the form of an unsigned long. The low byte contains
5962306a36Sopenharmony_cithe type of interrupt (update-done, alarm-rang, or periodic) that was
6062306a36Sopenharmony_ciraised, and the remaining bytes contain the number of interrupts since
6162306a36Sopenharmony_cithe last read.  Status information is reported through the pseudo-file
6262306a36Sopenharmony_ci/proc/driver/rtc if the /proc filesystem was enabled.  The driver has
6362306a36Sopenharmony_cibuilt in locking so that only one process is allowed to have the /dev/rtc
6462306a36Sopenharmony_ciinterface open at a time.
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ciA user process can monitor these interrupts by doing a read(2) or a
6762306a36Sopenharmony_ciselect(2) on /dev/rtc -- either will block/stop the user process until
6862306a36Sopenharmony_cithe next interrupt is received. This is useful for things like
6962306a36Sopenharmony_cireasonably high frequency data acquisition where one doesn't want to
7062306a36Sopenharmony_ciburn up 100% CPU by polling gettimeofday etc. etc.
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ciAt high frequencies, or under high loads, the user process should check
7362306a36Sopenharmony_cithe number of interrupts received since the last read to determine if
7462306a36Sopenharmony_cithere has been any interrupt "pileup" so to speak. Just for reference, a
7562306a36Sopenharmony_citypical 486-33 running a tight read loop on /dev/rtc will start to suffer
7662306a36Sopenharmony_cioccasional interrupt pileup (i.e. > 1 IRQ event since last read) for
7762306a36Sopenharmony_cifrequencies above 1024Hz. So you really should check the high bytes
7862306a36Sopenharmony_ciof the value you read, especially at frequencies above that of the
7962306a36Sopenharmony_cinormal timer interrupt, which is 100Hz.
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ciProgramming and/or enabling interrupt frequencies greater than 64Hz is
8262306a36Sopenharmony_cionly allowed by root. This is perhaps a bit conservative, but we don't want
8362306a36Sopenharmony_cian evil user generating lots of IRQs on a slow 386sx-16, where it might have
8462306a36Sopenharmony_cia negative impact on performance. This 64Hz limit can be changed by writing
8562306a36Sopenharmony_cia different value to /proc/sys/dev/rtc/max-user-freq. Note that the
8662306a36Sopenharmony_ciinterrupt handler is only a few lines of code to minimize any possibility
8762306a36Sopenharmony_ciof this effect.
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ciAlso, if the kernel time is synchronized with an external source, the 
9062306a36Sopenharmony_cikernel will write the time back to the CMOS clock every 11 minutes. In 
9162306a36Sopenharmony_cithe process of doing this, the kernel briefly turns off RTC periodic 
9262306a36Sopenharmony_ciinterrupts, so be aware of this if you are doing serious work. If you
9362306a36Sopenharmony_cidon't synchronize the kernel time with an external source (via ntp or
9462306a36Sopenharmony_ciwhatever) then the kernel will keep its hands off the RTC, allowing you
9562306a36Sopenharmony_ciexclusive access to the device for your applications.
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ciThe alarm and/or interrupt frequency are programmed into the RTC via
9862306a36Sopenharmony_civarious ioctl(2) calls as listed in ./include/linux/rtc.h
9962306a36Sopenharmony_ciRather than write 50 pages describing the ioctl() and so on, it is
10062306a36Sopenharmony_ciperhaps more useful to include a small test program that demonstrates
10162306a36Sopenharmony_cihow to use them, and demonstrates the features of the driver. This is
10262306a36Sopenharmony_ciprobably a lot more useful to people interested in writing applications
10362306a36Sopenharmony_cithat will be using this driver.  See the code at the end of this document.
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci(The original /dev/rtc driver was written by Paul Gortmaker.)
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ciNew portable "RTC Class" drivers:  /dev/rtcN
10962306a36Sopenharmony_ci--------------------------------------------
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ciBecause Linux supports many non-ACPI and non-PC platforms, some of which
11262306a36Sopenharmony_cihave more than one RTC style clock, it needed a more portable solution
11362306a36Sopenharmony_cithan expecting a single battery-backed MC146818 clone on every system.
11462306a36Sopenharmony_ciAccordingly, a new "RTC Class" framework has been defined.  It offers
11562306a36Sopenharmony_cithree different userspace interfaces:
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci    *	/dev/rtcN ... much the same as the older /dev/rtc interface
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci    *	/sys/class/rtc/rtcN ... sysfs attributes support readonly
12062306a36Sopenharmony_ci	access to some RTC attributes.
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci    *	/proc/driver/rtc ... the system clock RTC may expose itself
12362306a36Sopenharmony_ci	using a procfs interface. If there is no RTC for the system clock,
12462306a36Sopenharmony_ci	rtc0 is used by default. More information is (currently) shown
12562306a36Sopenharmony_ci	here than through sysfs.
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ciThe RTC Class framework supports a wide variety of RTCs, ranging from those
12862306a36Sopenharmony_ciintegrated into embeddable system-on-chip (SOC) processors to discrete chips
12962306a36Sopenharmony_ciusing I2C, SPI, or some other bus to communicate with the host CPU.  There's
13062306a36Sopenharmony_cieven support for PC-style RTCs ... including the features exposed on newer PCs
13162306a36Sopenharmony_cithrough ACPI.
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ciThe new framework also removes the "one RTC per system" restriction.  For
13462306a36Sopenharmony_ciexample, maybe the low-power battery-backed RTC is a discrete I2C chip, but
13562306a36Sopenharmony_cia high functionality RTC is integrated into the SOC.  That system might read
13662306a36Sopenharmony_cithe system clock from the discrete RTC, but use the integrated one for all
13762306a36Sopenharmony_ciother tasks, because of its greater functionality.
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ciCheck out tools/testing/selftests/rtc/rtctest.c for an example usage of the
14062306a36Sopenharmony_ciioctl interface.
141