Real Time Clock (RTC) is a small but important piece of hardware in an embedded Linux box. It saves time and keep time valid while box is in shutdown state. When the box boots again next time, it will provide a valid time to Linux system.
Real Time Clock in Linux
Two clocks are important in Linux system: a ‘hardware clock’, also known as RTC, CMOS or BIOS clock. This is the battery backed clock that keeps time even when the system is shut down. The second clock is called the ‘system clock’ or ‘kernel clock’ and is maintained by Linux OS. At boot time, the hardware clock is read and used to set the system clock. From that point onward the system clock is used to track time. Typically during the linux system shutdown process, Linux will flush its time into RTC to save it.
For many SoC, it has RTC embedded. However in my platform an external RTC is connected to SoC through I2C bus with typical address 0x68. This external RTC cnsumes less current than embedded one.
Check the two clocks
The simplest commands to check two clocks are: “date” and “hwclock”
As shown in the above command, there are two different clocks, and they can be synced using hwclock.
What need to be done ?
Linux kernel is able to support mutiple RTC devices in the same time. This can be checked by “ls -l /dev/rtc*” to see how many RTC devices are registered with Linux kernel.
What takes to make RTC devices work properly?
Make sure hardware setup is in place, either connected through I2C, or SPI bus.
Properly configured in device Tree file (DTS) to make it enabled. In my case, I need disable RTC inside SoC, and enable external RTC.
Configure Linux kernel with proper driver, and tell linux kernel the correct RTC instance to use in order to save time.
In the following example, DS1307 driver is enabled in kernel config, which will be the only RTC device rgistered in the system, so "rtc0" is set for system to sync to.
Check the box…
As it is shown from dmesg, external RTC is registered as “rtc0”, SoC RTC is not seen by kernel since it is disabled in DTS. Both clocks are synced.