Configure onboard RTC RX8900 (on REV002 and higher)

RTC Kernel module now included in Raspberry Pi OS

Starting from the May 2023 releases of the Raspberry Pi OS the needed RV8803 RTC Kernel module is included in the OS by default. Users no longer need to compile the module themselves, adding one overlay to the config.txt should now be enough to activate the RTC, dtoverlay=i2c-rtc,rv8803.

Procedure for Raspberry Pi OS starting with Kernel 6.1

Add to the /boot/config.txt file the RTC overlay for the module rv8803 and make sure the I2C bus is on as well:

dtparam=i2c_arm=on
dtoverlay=i2c-rtc,rv8803

After that, make sure you have disabled the "fake hwclock" which interferes with the 'real' hwclock:

sudo apt-get -y remove fake-hwclock
sudo update-rc.d -f fake-hwclock remove

Now with the fake-hwclock off, edit the script file /lib/udev/hwclock-set with nano or vim editor

sudo nano /lib/udev/hwclock-set

and comment out these three lines at the top:

#if [ -e /run/systemd/system ] ; then
# exit 0
#fi

Now perform a reboot and login again. Open a terminal and type in this command to check if the RTC module is functional.

dmesg | grep rtc

If you see that rtc0 was successfully registered, this means that your RTC module is working properly and now you can set your RTC to the system time with the following command:

sudo hwclock -w

Display current time and date from the system and the RTC:

date && sudo hwclock -r

Show date and time together with the current time zone using timedatectl:

pi@pitron:~ $ timedatectl
               Local time: Di 2020-11-1 8:26:56 CET
           Universal time: Di 2020-11-1 7:26:56 UTC
                 RTC time: Di 2020-11-1 7:26:56
                Time zone: Europe/Berlin (CET, +0100)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

Procedure for Raspberry Pi OS with Kernel 4.x and 5.x

This section focuses on the building of the RTC kernel module and less on how to build the kernel and find the right kernel version for the system you are using right now. For more detailed information on this topic, take a look at the section Build Linux Kernel which shows how to find the matching kernel sources.
The information on this page is for 32bit systems, if you require a 64bit version please refer to the Raspberry Pi website Kernel building for more information. The guide on this page requires a Linux development system preferably based on a Debian Linux like Ubuntu.

To use the onboard RTC RX8900 in your own application you have to do the following steps:

  1. Compile the right driver
  2. Edit the dtoverlay for the RTC
  3. Setup the RTC configuration

Kernel building changed for newer kernels

For newer kernel versions the Raspberry Pi Foundation no longer uses their own "compile tools" for cross-compiling, but now uses the "crossbuild-essential-armhf" toolchain to do so. Some steps below are splitted into the old and the new procedure. Keep that in mind when following the steps.

1. Compile the right driver

Install the following on your linux-development-system

sudo apt-get install git bc
sudo apt-get install libncurses5-dev
sudo apt-get install make
sudo apt-get install gcc
sudo apt-get install bison flex libssl-dev libc6-dev 

Determine the kernel version of your current Pi-Tron operating system

uname -a

On your linux-development-system cross compile the RPi kernel
Preparations:
Only install one cross-compiler, for newer kernels like 5.x choose step 1.1 and ignore 1.2

1.1 Installing the cross-compile package for newer kernels

sudo apt install crossbuild-essential-armhf

1.2 Installing the Raspberry Pi cross-compile tools for older kernels

git clone https://github.com/raspberrypi/tools ~/tools
echo PATH=\$PATH:~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin >> ~/.bashrc
source ~/.bashrc

If you have a kernel 4.x

git clone --branch rpi-4.14.y https://github.com/raspberrypi/linux

If you have a kernel 5.x

git clone --branch rpi-5.4.y https://github.com/raspberrypi/linux

These are just examples, you must adjust the branch part (rpi-4.14.y / rpi-5.4.y shown above) to an available branch at the Raspberry Pi firmware repository but also match it with the kernel version you are currently using.
Change into the linux folder

cd linux


Here are some commit id's to give you a starting point:

Linux 4.14.79-V7+

git checkout 36612d5d7a88672a3e7dd6cb458dbbbca0d75efe

Linux 4.14.98-v7+

git checkout 5d63a4595d32a8505590d5fea5c4ec1ca79fd49d

Linux 4.19.57-v7+

git checkout 6e787b87ace9f945f17a9022288e2758ed281cf7

Linux 4.19.75-v7+

git checkout 6d8bf28fa4b1ca0a35c0cd1dcb267fb216daf720

Linux 5.4.51-v7+

git checkout 3e92327867b6b40fed95ed36bb20f56d6e0cc879

Linux 5.4.72-v7+

git checkout ff93994fb3f92070d8521d709ad04675ecaa5817


Check the Makefile that it has the correct kernel version

nano Makefile

Depending on your Raspberry Pi, enter the correct kernel parameter at the console and then start make.

For Pi1, PiZero, PiZeroW or CM1

KERNEL=kernel
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcmrpi_defconfig

For Pi2, Pi3, Pi3+, CM3 or CM3+

KERNEL=kernel7
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig

Run menuconfig to activate the RX8900 RTC kernel module, also see section Build Linux Kernel

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig

Navigate to Driver --> Real Time Clock --> RTC-RV8803 / Epson RX8900 and set the kernel module to "M". Then Save all and exit.

2. Edit the dtoverlay for the RTC

Modify dtoverlay :

nano ~/linux/arch/arm/boot/dts/overlays/i2c-rtc-overlay.dts

supplement the following:

        fragment@10 {
        target = <&i2c_arm>;
        __dormant__ {
            #address-cells = <1>;
            #size-cells = <0>;
            status = "okay";

            rx8900: rx8900@32 {
                compatible = "epson,rx8900";
                reg = <0x32>;
                status = "okay";
            };
        };
    };

    __overrides__ {
        abx80x = <0>,"+0";
        ds1307 = <0>,"+1";
        ds1339 = <0>,"+2";
        ds3231 = <0>,"+3";
        mcp7940x = <0>,"+4";
        mcp7941x = <0>,"+5";
        pcf2127 = <0>,"+6";
        pcf8523 = <0>,"+7";
        pcf8563 = <0>,"+8";
        m41t62 = <0>,"+9";
        rx8900 = <0>,"+10";
        trickle-diode-type = <&abx80x>,"abracon,tc-diode";
        trickle-resistor-ohms = <&ds1339>,"trickle-resistor-ohms:0",
                    <&abx80x>,"abracon,tc-resistor";
        wakeup-source = <&ds1339>,"wakeup-source?",
                <&ds3231>,"wakeup-source?",
                <&mcp7940x>,"wakeup-source?",
                <&mcp7941x>,"wakeup-source?",
                <&m41t62>,"wakeup-source?";
    };

Note: The number behind fragment@, in this example 10, needs to be adapted to the current number found already in the file and increased by 1 and then the changes have to be made accordingly. This file is evolving and growing over time which means the maximum number of fragments in the file will be different as well. This is just an example of the needed changes.

Build the kernel:

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs

Note: This will take a while, consider using the -j parameter to make the compile process multi-threaded.

The compiled kernel module can be found in ~/linux/drivers/rtc/ and is named rtc-rv8803.ko. The corresponding new overlay file, i2c-rtc.dtbo, can be found under ~/linux/arch/arm/boot/dts/. Copy the created driver and the overlay file on to your Raspberry Pi, e.g. into the pi user's home folder, and then copy the rtc driver to the rtc driver folder of your current kernel and the overlay file into the overlays folder on the boot partition.

#Copy kernel module into the RTC driver folder
cd ~
sudo cp rtc-rv8803.ko /lib/modules/{KERNELVERSION}/kernel/drivers/rtc

#Example for kernel 5.4.51-v7+
cd ~
sudo cp rtc-rv8803.ko /lib/modules/5.4.51-v7+/kernel/drivers/rtc

#Copy the new overlay file to overlays on the boot partition
cd ~
sudo cp -f i2c-rtc.dtbo /boot/overlays/

3. Setup the RTC configuration

Please ensure that /boot/config.txt file includes those three parameters:

dtparam=i2c_arm=on
dtoverlay=i2c-rtc,rx8900

Start the Module

sudo depmod -ae                 # to re-create the module dependency list
sudo modprobe rtc-rv8803        # (without .ko)
lsmod                           # shows running modules

After that, please make sure you have disabled the "fake hwclock" which interferes with the 'real' hwclock

sudo apt-get -y remove fake-hwclock
sudo update-rc.d -f fake-hwclock remove

Now with the fake-hw clock off, you can start the original 'hardware clock' script. Edit the script file /lib/udev/hwclock-set with nano or vim editor

sudo nano /lib/udev/hwclock-set

and comment out these three lines at the top:

#if [ -e /run/systemd/system ] ; then
# exit 0
#fi

Edit Autorun:

crontab -e

insert:

@reboot sudo  /sbin/hwclock -s

save and reboot your RPi. After reboot and login, open a terminal and type in this command to check if the RTC module is functional.

dmesg |grep rtc

if you see that rtc0 was successfully registered, this means that your RTC module is working properly and now you can set your RTC to the system time with the following command:

sudo hwclock -w

Display current time and date from the system and the RTC:

date && sudo hwclock -r

Show date and time together with the current time zone using timedatectl:

pi@pitron:~ $ timedatectl
               Local time: Di 2020-11-1 8:26:56 CET
           Universal time: Di 2020-11-1 7:26:56 UTC
                 RTC time: Di 2020-11-1 7:26:56
                Time zone: Europe/Berlin (CET, +0100)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no