How to enable internal RTC in Apalis imx6 with Torizon?

I have Alapis imx6, there is no external RTC in my system. After power on system time restores to power off time.

I try to create /etc/udev/rules.d/99-rtc1.rules with
KERNEL=="rtc1", SUBSYSTEM=="rtc", DRIVER=="", ATTR{name}=="20cc034.snvs-rtc-lp", SYMLINK="rtc", MODE="0666"

But result is the same.

$ timedatectl
               Local time: Thu 2021-11-25 11:12:15 UTC
           Universal time: Thu 2021-11-25 11:12:15 UTC
                 RTC time: n/a
                Time zone: Universal (UTC, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
$ sudo hwclock -r
hwclock: Cannot access the Hardware Clock via any known method.
hwclock: Use the --verbose option to see the details of our search for an access method.

In boot log I see

...
Jan 01 00:00:08 apalis-imx6-10652210 kernel: snvs_rtc 20cc000.snvs:snvs-rtc-lp: registered as rtc1
...

Greetings @Juzujka,

What version of TorizonCore are you running on your device?

When I flash our latest Quarterly Release (TorizonCore Upstream 5.4.0+build.10). The RTC seems to work out of the box:

apalis-imx6-05228985:~$ timedatectl
               Local time: Mon 2021-11-29 21:33:26 UTC
           Universal time: Mon 2021-11-29 21:33:26 UTC
                 RTC time: Mon 2021-11-29 21:33:26
                Time zone: Universal (UTC, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
apalis-imx6-05228985:~$ sudo hwclock -r
2021-11-29 21:33:32.256411+00:00

This was a freshly flashed image with no changes. I didn’t need to create a udev rule or anything like that.

As a side note /dev/rtc0 will be automatically set by the network clock on boot. However this rtc is the one that is on Toradex carrier boards. For SoC internal rtc which is /dev/rtc1 you’ll need to set this rtc as it won’t be set automatically on boot.

Best Regards,
Jeremias

Hello, Jeremias!
Thank you for your reply!
My version is torizon-core-docker-apalis-imx6-Tezi_5.1.0+build.1 + custom splash screen.
My device should work without the network.

In that case then you just need to set the RTC time manually if there’s no network. The process is described in this article here: Real-Time Clock / RTC (Linux) | Toradex Developer Center

Best Regards,
Jeremias

I followed this manual and try to create /etc/udev/rules.d/99-rtc1.rules with
KERNEL=="rtc1", SUBSYSTEM=="rtc", DRIVER=="", ATTR{name}=="20cc034.snvs-rtc-lp", SYMLINK="rtc", MODE="0666"
also with
20cc034.snvs-rtc-lp
20cc000.snvs
m41t00
m41t0
no changes

What are the exact commands you are running and what is there output?

Without network you should be able to set your own time with timedatectl. Then, you can sync the RTC with the system time via hwclock. I was able to do this on my board without issues. The udev rule is only for changing the “default” (rtc1) to the internal SoC RTC. For enabling and setting the the RTC itself you need to run the commands shown in the article.

Best Regards,
Jeremias

Jeremias, yes, I can set time with

timedatectl set-ntp false
timedatectl set-time "2015-01-31 11:13:54"

but I have troubles with hardware RTC

timedatectl
               Local time: Sat 2015-01-31 11:14:58 UTC
           Universal time: Sat 2015-01-31 11:14:58 UTC
                 RTC time: n/a
                Time zone: Universal (UTC, +0000)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

RTC time: n/a

# hwclock -r
hwclock: Cannot access the Hardware Clock via any known method.
hwclock: Use the --verbose option to see the details of our search for an access method.

I used recommendation from the article

$ cat /etc/udev/rules.d/99-rtc1.rules
KERNEL=="rtc1", SUBSYSTEM=="rtc", DRIVER=="", ATTR{name}=="20cc034.snvs-rtc-lp", SYMLINK="rtc", MODE="0666"

And it has no effect to system time and hwclock -r

dmesg says

[    1.869164] snvs_rtc 20cc000.snvs:snvs-rtc-lp: registered as rtc1
[    2.674856] hctosys: unable to open rtc device (rtc0)

But I can read RTC1

# hwclock -f /dev/rtc1 
1970-01-10 17:08:54.480378+00:00

and set RTC1

# hwclock -w -f /dev/rtc1
# hwclock -f /dev/rtc1 
2015-01-31 11:39:43.566688+00:00

But how to synchronize SW clock and RTC1 to automaticaly update SW clock from RTC1 at startup and RTC1 after SW clock is updated from timedatectl or NTP

Okay so all that udev rule does is create a symlink in /dev like so:

apalis-imx6-05228985:~$ ls -l /dev/rtc
lrwxrwxrwx 1 root root 4 Dec  7 00:51 /dev/rtc -> rtc1

By the way you got the ATTR{name} incorrect in that rule, you can check this with:

apalis-imx6-05228985:~$ udevadm info -a -p /sys/class/rtc/rtc1 | grep ATTR{name}
    ATTR{name}=="snvs_rtc 20cc000.snvs:snvs-rtc-lp"

However hwclock sets it’s “default” rtc device in the order of /dev/rtc0 then /dev/rtc. Which is why /dev/rtc0 is still being used as the default despite the udev rule. You’d need to disable or mask this device so that it then uses /dev/rtc which with the udev rule links back to /dev/rtc1.

As for setting the time. You can set the system time with something like date -s "2018-05-05 05:05:05". Then you can set the rtc to the system time with hwclock -w. Finally if you cut power to the system and then boot it again it should use the default rtc to set the time of the system. However the rtc can only retain the time during power off if there is a battery on the carrier board to power it. If done correctly you see something like this in dmesg:

snvs_rtc 30370000.snvs:snvs-rtc-lp: setting system clock to 2018-05-05T05:05:25 UTC (152549)

Showing that the rtc retained the time during power off and set the system clock based on it. I hope this helped make the process more clear.

Best Regards,
Jeremias