| 1 | [[PageOutline]] |
| 2 | |
| 3 | [=#rtc] |
| 4 | = Real Time Clock (RTC) = |
| 5 | A Real Time Clock (RTC), also known as a 'Hardware Clock' is a time keeping device on a computer that is able to keep the time even when the device is powered off. This is typically done via a dedicated chip that is powered off a coin-cell battery. |
| 6 | |
| 7 | |
| 8 | [=#rtc-class] |
| 9 | == Linux RTC class (sysfs) == |
| 10 | The Linux kernel has a RTC class for RTC drivers which creates entries in {{{/sys/class/rtc}}}. Each RTC device detected and registered via supported drivers has a directory along with its cooresponding {{{/dev/rtc<n>}}} device node that can be used to interact with the RTC. |
| 11 | |
| 12 | Here are some examples of what you can do via this {{{sysfs}}} interface: |
| 13 | * Show the name of the rtc driver: |
| 14 | {{{#!bash |
| 15 | root@xenial-ventana:~# cat /sys/class/rtc/rtc0/name |
| 16 | rtc-ds1672 |
| 17 | }}} |
| 18 | * Show number of seconds since the [https://en.wikipedia.org/wiki/Unix_time Unix epoch]: |
| 19 | {{{#!bash |
| 20 | root@xenial-ventana:~# cat /sys/class/rtc/rtc0/since_epoch |
| 21 | 143 |
| 22 | }}} |
| 23 | * Show Date and Time from the RTC's perspective: |
| 24 | {{{#!bash |
| 25 | root@xenial-ventana:~# cat /sys/class/rtc/rtc0/date |
| 26 | 1970-01-01 |
| 27 | root@xenial-ventana:~# cat /sys/class/rtc/rtc0/time |
| 28 | 00:02:29 |
| 29 | }}} |
| 30 | |
| 31 | |
| 32 | [=#hwclock] |
| 33 | == hwclock Application == |
| 34 | The {{{hwclock}}} application is a userspace app that uses the Linux kernel RTC API to interace with available RTC's on the system in order to: |
| 35 | - get the time from the RTC |
| 36 | - set the time from the RTC to the Linux system time |
| 37 | - set the Linux system time to the RTC |
| 38 | |
| 39 | Examples: |
| 40 | * Update the Linux System Time via NTP, then store the Linux System Time in the RTC: |
| 41 | {{{#!bash |
| 42 | ntpdate -s time.nist.gov |
| 43 | hwclock --systohc |
| 44 | }}} |
| 45 | * Set the system time from the RTC: |
| 46 | {{{#!bash |
| 47 | hwclock --hctosys # set the system time from the RTC |
| 48 | }}} |
| 49 | - In Ubuntu for example, this is done for you via {{{/etc/init.d/hwclock.sh}}} from the {{{util-linux}}} package. |
| 50 | |
| 51 | Note that if you happen to have more than one RTC in the system you can add the {{{-f <device>}}} parameter to refer to the one you want to work with. If you do have more than one RTC and one of them is not useful (such as the snvs RTC from an iMX6 processor on a Ventana board) you can disable or [#modules blacklist the kernel module] to keep it from loading. |
| 52 | |
| 53 | |
| 54 | [=#hctosys] |
| 55 | == hctosys Kernel option (RTC_HCTOSYS / RTC_HTOSYS_DEVICE) == |
| 56 | A kernel option is available at compile time that can set the Linux system time from the value stored in an RTC provided the RTC driver is built static in the kernel. Note that this is not always the case and many RTC drivers these days are built as kernel modules. To enable this you need to build your kernel with the following: |
| 57 | - RTC_HCTOSYS=y |
| 58 | - RTC_HCTOSYS_DEVICE=rtc0 (or whichever static RTC you want to use if you have multiple) |
| 59 | - RTC_DRV_DS1672=y (for the GSC RTC for example) |
| 60 | |
| 61 | Note that Gateworks boards have an RTC implemented in the [wiki:gsc Gateworks System Controller (GSC)] that emulates a DS1672 RTC. |
| 62 | |
| 63 | If you are using for example an Ubuntu kernel this option is enabled but won't do you any good because the RTC on Gateworks boards are built as a kernel module. In order to set the system time from an RTC built as a module see [#hwclock hwclock above] |
| 64 | |
| 65 | |
| 66 | [=#gsc-rtc] |
| 67 | == Gateworks Boards and GSC RTC == |
| 68 | Gateworks boards have a [wiki:gsc Gateworks System Controller (GSC)] which emulates a DS1672 RTC. |
| 69 | |
| 70 | Note that Ventana boards based on the i.MX6 SoC may also have an additional RTC that is detected but not used. On these boards you should disable or blacklist the snvs RTC driver as it has no value because the i.MX6 RTC does not have a battery source. |
| 71 | |
| 72 | |
| 73 | [=#modules] |
| 74 | == working with RTC kernel modules |
| 75 | === blacklisting un-usable RTCs |
| 76 | Some SoC's have RTC's built in that are un-usable because they don't have a battery powering them. An example of this is the i.MX6 RTC (rtc_snvs kernel driver) on Ventana boards. These boards use the DS1672 RTC emulated by the Gateworks GSC RTC instead. |
| 77 | |
| 78 | To get rid of a kernel driver like the i.MX6 RTC driver (rtc_snvs) you can: |
| 79 | - build your own kernel with it disabled |
| 80 | - remove the kernel module which depends on the OS your using: |
| 81 | * for Ubuntu it would be in /lib/modules/.../rtc_snvs.ko |
| 82 | - blacklist the kernel module from the init scripts that load modules which depends on the OS your using |
| 83 | |
| 84 | Module blacklist examples: |
| 85 | * for Ubuntu you can add it to a file in /etc/modprobe.d or create your own such as: |
| 86 | {{{#!bash |
| 87 | echo "blacklist rtc_snvs" > /etc/modprobe.d/blacklist-rtc.conf |
| 88 | }}} |
| 89 | |
| 90 | === syncing the system time with the RTC === |
| 91 | If using an RTC that is built in the kernel as a static driver you can use the [#hctosys hctosys] driver option. However if your using a kernel module you need to sync the time manually after the kernel boots. You can do this using the [#hwclock hwclock] application. |
| 92 | |
| 93 | |
| 94 | [=#ntp] |
| 95 | == Network Time Protocol (NTP) == |
| 96 | The Network Time Protocol can be used to update the Linux system clock. While this is completely independent from an RTC it is often useful to update the Linux system time using NTP prior to setting the RTC: |
| 97 | {{{#!bash |
| 98 | ntpdate -s time.nist.gov |
| 99 | hwclock --systohc |
| 100 | }}} |