18c2ecf20Sopenharmony_ci=======================================
28c2ecf20Sopenharmony_ciReal Time Clock (RTC) Drivers for Linux
38c2ecf20Sopenharmony_ci=======================================
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciWhen Linux developers talk about a "Real Time Clock", they usually mean
68c2ecf20Sopenharmony_cisomething that tracks wall clock time and is battery backed so that it
78c2ecf20Sopenharmony_ciworks even with system power off.  Such clocks will normally not track
88c2ecf20Sopenharmony_cithe local time zone or daylight savings time -- unless they dual boot
98c2ecf20Sopenharmony_ciwith MS-Windows -- but will instead be set to Coordinated Universal Time
108c2ecf20Sopenharmony_ci(UTC, formerly "Greenwich Mean Time").
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ciThe newest non-PC hardware tends to just count seconds, like the time(2)
138c2ecf20Sopenharmony_cisystem call reports, but RTCs also very commonly represent time using
148c2ecf20Sopenharmony_cithe Gregorian calendar and 24 hour time, as reported by gmtime(3).
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciLinux has two largely-compatible userspace RTC API families you may
178c2ecf20Sopenharmony_cineed to know about:
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci    *	/dev/rtc ... is the RTC provided by PC compatible systems,
208c2ecf20Sopenharmony_ci	so it's not very portable to non-x86 systems.
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci    *	/dev/rtc0, /dev/rtc1 ... are part of a framework that's
238c2ecf20Sopenharmony_ci	supported by a wide variety of RTC chips on all systems.
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciProgrammers need to understand that the PC/AT functionality is not
268c2ecf20Sopenharmony_cialways available, and some systems can do much more.  That is, the
278c2ecf20Sopenharmony_ciRTCs use the same API to make requests in both RTC frameworks (using
288c2ecf20Sopenharmony_cidifferent filenames of course), but the hardware may not offer the
298c2ecf20Sopenharmony_cisame functionality.  For example, not every RTC is hooked up to an
308c2ecf20Sopenharmony_ciIRQ, so they can't all issue alarms; and where standard PC RTCs can
318c2ecf20Sopenharmony_cionly issue an alarm up to 24 hours in the future, other hardware may
328c2ecf20Sopenharmony_cibe able to schedule one any time in the upcoming century.
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ciOld PC/AT-Compatible driver:  /dev/rtc
368c2ecf20Sopenharmony_ci--------------------------------------
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciAll PCs (even Alpha machines) have a Real Time Clock built into them.
398c2ecf20Sopenharmony_ciUsually they are built into the chipset of the computer, but some may
408c2ecf20Sopenharmony_ciactually have a Motorola MC146818 (or clone) on the board. This is the
418c2ecf20Sopenharmony_ciclock that keeps the date and time while your computer is turned off.
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ciACPI has standardized that MC146818 functionality, and extended it in
448c2ecf20Sopenharmony_cia few ways (enabling longer alarm periods, and wake-from-hibernate).
458c2ecf20Sopenharmony_ciThat functionality is NOT exposed in the old driver.
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciHowever it can also be used to generate signals from a slow 2Hz to a
488c2ecf20Sopenharmony_cirelatively fast 8192Hz, in increments of powers of two. These signals
498c2ecf20Sopenharmony_ciare reported by interrupt number 8. (Oh! So *that* is what IRQ 8 is
508c2ecf20Sopenharmony_cifor...) It can also function as a 24hr alarm, raising IRQ 8 when the
518c2ecf20Sopenharmony_cialarm goes off. The alarm can also be programmed to only check any
528c2ecf20Sopenharmony_cisubset of the three programmable values, meaning that it could be set to
538c2ecf20Sopenharmony_ciring on the 30th second of the 30th minute of every hour, for example.
548c2ecf20Sopenharmony_ciThe clock can also be set to generate an interrupt upon every clock
558c2ecf20Sopenharmony_ciupdate, thus generating a 1Hz signal.
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ciThe interrupts are reported via /dev/rtc (major 10, minor 135, read only
588c2ecf20Sopenharmony_cicharacter device) in the form of an unsigned long. The low byte contains
598c2ecf20Sopenharmony_cithe type of interrupt (update-done, alarm-rang, or periodic) that was
608c2ecf20Sopenharmony_ciraised, and the remaining bytes contain the number of interrupts since
618c2ecf20Sopenharmony_cithe last read.  Status information is reported through the pseudo-file
628c2ecf20Sopenharmony_ci/proc/driver/rtc if the /proc filesystem was enabled.  The driver has
638c2ecf20Sopenharmony_cibuilt in locking so that only one process is allowed to have the /dev/rtc
648c2ecf20Sopenharmony_ciinterface open at a time.
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ciA user process can monitor these interrupts by doing a read(2) or a
678c2ecf20Sopenharmony_ciselect(2) on /dev/rtc -- either will block/stop the user process until
688c2ecf20Sopenharmony_cithe next interrupt is received. This is useful for things like
698c2ecf20Sopenharmony_cireasonably high frequency data acquisition where one doesn't want to
708c2ecf20Sopenharmony_ciburn up 100% CPU by polling gettimeofday etc. etc.
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ciAt high frequencies, or under high loads, the user process should check
738c2ecf20Sopenharmony_cithe number of interrupts received since the last read to determine if
748c2ecf20Sopenharmony_cithere has been any interrupt "pileup" so to speak. Just for reference, a
758c2ecf20Sopenharmony_citypical 486-33 running a tight read loop on /dev/rtc will start to suffer
768c2ecf20Sopenharmony_cioccasional interrupt pileup (i.e. > 1 IRQ event since last read) for
778c2ecf20Sopenharmony_cifrequencies above 1024Hz. So you really should check the high bytes
788c2ecf20Sopenharmony_ciof the value you read, especially at frequencies above that of the
798c2ecf20Sopenharmony_cinormal timer interrupt, which is 100Hz.
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ciProgramming and/or enabling interrupt frequencies greater than 64Hz is
828c2ecf20Sopenharmony_cionly allowed by root. This is perhaps a bit conservative, but we don't want
838c2ecf20Sopenharmony_cian evil user generating lots of IRQs on a slow 386sx-16, where it might have
848c2ecf20Sopenharmony_cia negative impact on performance. This 64Hz limit can be changed by writing
858c2ecf20Sopenharmony_cia different value to /proc/sys/dev/rtc/max-user-freq. Note that the
868c2ecf20Sopenharmony_ciinterrupt handler is only a few lines of code to minimize any possibility
878c2ecf20Sopenharmony_ciof this effect.
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ciAlso, if the kernel time is synchronized with an external source, the 
908c2ecf20Sopenharmony_cikernel will write the time back to the CMOS clock every 11 minutes. In 
918c2ecf20Sopenharmony_cithe process of doing this, the kernel briefly turns off RTC periodic 
928c2ecf20Sopenharmony_ciinterrupts, so be aware of this if you are doing serious work. If you
938c2ecf20Sopenharmony_cidon't synchronize the kernel time with an external source (via ntp or
948c2ecf20Sopenharmony_ciwhatever) then the kernel will keep its hands off the RTC, allowing you
958c2ecf20Sopenharmony_ciexclusive access to the device for your applications.
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ciThe alarm and/or interrupt frequency are programmed into the RTC via
988c2ecf20Sopenharmony_civarious ioctl(2) calls as listed in ./include/linux/rtc.h
998c2ecf20Sopenharmony_ciRather than write 50 pages describing the ioctl() and so on, it is
1008c2ecf20Sopenharmony_ciperhaps more useful to include a small test program that demonstrates
1018c2ecf20Sopenharmony_cihow to use them, and demonstrates the features of the driver. This is
1028c2ecf20Sopenharmony_ciprobably a lot more useful to people interested in writing applications
1038c2ecf20Sopenharmony_cithat will be using this driver.  See the code at the end of this document.
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci(The original /dev/rtc driver was written by Paul Gortmaker.)
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ciNew portable "RTC Class" drivers:  /dev/rtcN
1098c2ecf20Sopenharmony_ci--------------------------------------------
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ciBecause Linux supports many non-ACPI and non-PC platforms, some of which
1128c2ecf20Sopenharmony_cihave more than one RTC style clock, it needed a more portable solution
1138c2ecf20Sopenharmony_cithan expecting a single battery-backed MC146818 clone on every system.
1148c2ecf20Sopenharmony_ciAccordingly, a new "RTC Class" framework has been defined.  It offers
1158c2ecf20Sopenharmony_cithree different userspace interfaces:
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci    *	/dev/rtcN ... much the same as the older /dev/rtc interface
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci    *	/sys/class/rtc/rtcN ... sysfs attributes support readonly
1208c2ecf20Sopenharmony_ci	access to some RTC attributes.
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci    *	/proc/driver/rtc ... the system clock RTC may expose itself
1238c2ecf20Sopenharmony_ci	using a procfs interface. If there is no RTC for the system clock,
1248c2ecf20Sopenharmony_ci	rtc0 is used by default. More information is (currently) shown
1258c2ecf20Sopenharmony_ci	here than through sysfs.
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ciThe RTC Class framework supports a wide variety of RTCs, ranging from those
1288c2ecf20Sopenharmony_ciintegrated into embeddable system-on-chip (SOC) processors to discrete chips
1298c2ecf20Sopenharmony_ciusing I2C, SPI, or some other bus to communicate with the host CPU.  There's
1308c2ecf20Sopenharmony_cieven support for PC-style RTCs ... including the features exposed on newer PCs
1318c2ecf20Sopenharmony_cithrough ACPI.
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ciThe new framework also removes the "one RTC per system" restriction.  For
1348c2ecf20Sopenharmony_ciexample, maybe the low-power battery-backed RTC is a discrete I2C chip, but
1358c2ecf20Sopenharmony_cia high functionality RTC is integrated into the SOC.  That system might read
1368c2ecf20Sopenharmony_cithe system clock from the discrete RTC, but use the integrated one for all
1378c2ecf20Sopenharmony_ciother tasks, because of its greater functionality.
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ciCheck out tools/testing/selftests/rtc/rtctest.c for an example usage of the
1408c2ecf20Sopenharmony_ciioctl interface.
141