RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues,...

23
ELCE 2019 RTC subsystem, recent changes and where it is heading Alexandre Belloni [email protected] © Copyright 2004-2019, Bootlin. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! embedded Linux and kernel engineering - Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 1/23

Transcript of RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues,...

Page 1: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

ELCE 2019

RTC subsystem, recentchanges and where it isheadingAlexandre [email protected]

© Copyright 2004-2019, Bootlin.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

embedded Linux and kernel engineering

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 1/23

Page 2: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Alexandre Belloni

▶ Embedded Linux engineer at Bootlin▶ Embedded Linux expertise▶ Development, consulting and training▶ Strong open-source focus

▶ Open-source contributor▶ Maintainer for the Linux kernel RTC subsystem▶ Co-Maintainer of kernel support for Microchip ARM SoCs

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 2/23

Page 3: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

The RTC subsystem

▶ Introduced in 2.6.17 (March 2006) by Alessandro Zummo▶ Taken over by Alexandre Belloni since 4.1 (April 2015)▶ Mailing list was on googlegroups until Google decided that it was sending to much

spam on the 4th of May 2017.▶ [email protected] created on 11th of May 2017▶ Archives are at https://lore.kernel.org/linux-rtc/▶ Patchwork at http://patchwork.ozlabs.org/project/rtc-linux/list/▶ git repository on kernel.org:

git://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 3/23

Page 4: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Mailing list statistics

2015

.03

2015

.05

2015

.07

2015

.09

2015

.11

2016

.01

2016

.03

2016

.05

2016

.07

2016

.09

2016

.11

2017

.01

2017

.03

2017

.05

2017

.07

2017

.09

2017

.11

2018

.01

2018

.03

2018

.05

2018

.07

2018

.09

2018

.11

2019

.01

2019

.03

2019

.05

2019

.07

2019

.09

0

50

100

150

200

250

300

350

400

450

500

RTC mailing list volume

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 4/23

Page 5: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Commits statistics

v2.6.17

v2.6.19

v2.6.21

v2.6.23

v2.6.25

v2.6.27

v2.6.29

v2.6.31

v2.6.33

v2.6.35

v2.6.37

v2.6.39v3.1v3.3v3.5v3.7v3.9v3.11v3.13v3.15v3.17v3.19v4.1v4.3v4.5v4.7v4.9v4.11v4.13v4.15v4.17v4.19v5.0v5.2

0

50

100

150

200

250

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 5/23

Page 6: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Recent changes: new drivers since 4.0

▶ Abracon AB-RTCMC-32.768kHz-EOZ9▶ Alphascale ASM9260▶ Amlogic Meson RTC▶ Amlogic Virtual Wake▶ Android emulator (goldfish) RTC▶ Aspeed BMC SoC RTC▶ Broadcom STB wake-timer▶ Cadence RTC IP▶ Chrome OS EC RTC▶ Cortina Gemini▶ Dialog DA9062▶ Epson RX6110SA▶ Epson RX8010SJ▶ Epson RX8130CE▶ Epson Toyocom rtc-7301sf/dg▶ Freescale FlexTimer Module alarm▶ Freescale i.MX system controller RTC▶ Intersil ISL12026

▶ Maxim IC DS1308▶ Maxim MAX77620▶ Maxim MAX6916▶ Mediatek MT6397▶ Mediatek MT7622 RTC▶ Microchip PIC32▶ Microcrystal RV1805▶ Microcrystal RV3028▶ Microcrystal RV8803▶ Motorola CPCAP PMIC RTC▶ NXP i.MX53 SRTC▶ NXP LPC24xx▶ NXP PCF2127/PCF2129▶ NXP PCF85263/PCF85363▶ Realtek RTD1295▶ Spreadtrum SC27xx PMIC RTC▶ STMicroelectronics STM32▶ Whwave sd3078▶ Xilinx Zynq MP

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 6/23

Page 7: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Recent changes: crystal offset

▶ The RTC has either an in-package or external crystal, usually running 32kHz.▶ There is variance in the exact frequency because of manufacturing, temperature

and aging.▶ Some RTCs can add or subtract correction pulses to make seconds quicker or

longer.▶ New sysfs interface /sys/class/rtc/rtcX/offset▶ The unit is part per billion, positive value adds pulses and negative values

subtracts pulses.▶ The name is coming from the NXP datasheets, it is also called digital trimming.

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 7/23

Page 8: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Recent changes: crystal offset

▶ An other way to accommodate crystal variance is to change the load capacitance.▶ Some RTC oscillators can change the apparent load on X1/X2▶ New device tree property quartz-load-femtofarads▶ It is also called analog trimming.

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 8/23

Page 9: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Recent changes: non volatile memory

▶ Some RTCs have a small amount of RAM.▶ Because the RTC is always on, the RAM is non volatile.▶ This memory is now exposed using the nvmem framework.▶ New registration function: int rtc_nvmem_register(struct rtc_device

*rtc, struct nvmem_config *nvmem_config)▶ Multiple calls are allowed so if EEPROM is available, it can also be exposed

through nvmem.▶ rtc->nvram_old_abi handles the legacy ABI for drivers that were exposing a file

named nvram in sysfs. It issues deprecation warning.

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 9/23

Page 10: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Recent changes: rtc registration

▶ With devm_rtc_device_register, it was difficult to write a probe functionwithout race conditions.

▶ The registration is now split in two parts: devm_rtc_allocate_device andrtc_register_device.

▶ This allows to handle the struct rtc_device before registering it.▶ Non devm managed registration has been removed from the driver API.

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 10/23

Page 11: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Recent changes: sysfs attributes registration

▶ sysfs attributes were created after devm_rtc_device_register. This opened upa possible race condition because the rtc folder appeared before the extraattributes.

▶ New functions: int rtc_add_group(struct rtc_device *rtc, conststruct attribute_group *grp) and int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps).

▶ Used between devm_rtc_allocate_device and rtc_register_device, theyensure all the RTC attributes are available at the same time.

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 11/23

Page 12: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Recent changes: RTC range

▶ New members of struct rtc_device: range_min and range_max.▶ They allow the driver to declare the date and time range supported by the RTC

hardware.▶ No discontinuity is allowed in this range.▶ The core will check struct rtc_tm is in the range before passing it to the driver.

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 12/23

Page 13: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Recent changes: RTC offset

▶ To solve RTC end of time issues, the core is now able to offset and window thehardware range.

▶ The offset is added or subtracted before calling the driver callbacks.▶ Note that because the RTC time is always positive, there are almost no RTCs

failing in 2038 (currently only 3). The most common failing date is 2100, followedby 2106.

▶ This is still useful to handle dates before 2000 as it seem to be mandatory forAndroid.

▶ New device tree property: start-year

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 13/23

Page 14: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Recent changes: other improvements

▶ New %ptR printk format▶ Tracepoints▶ time_t overflow is now properly handled in rtc_hctosys to avoid breaking 32bit

platforms.▶ rtc-range tool to test the continuous range of an rtc git:

//git.kernel.org/pub/scm/linux/kernel/git/abelloni/rtc-tools.git▶ Timestamping▶ Trickle charging

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 14/23

Page 15: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Recent changes: cleanups

▶ Unnecessary rtc_valid_tm calls have been removed.▶ open, release, set_mmss and set_mmss64, read_callback members of

struct rtc_class_ops have been removed.▶ struct rtc_class_ops are now const were possible.▶ rtc_control API has been removed. It has been replaced by hrtimer.▶ rtc_irq_register has been removed.▶ Core files have been renamed for consistency.

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 15/23

Page 16: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Future changes: voltage detection

▶ Current voltage drop detection ABI is not working well for more advanced RTCs.▶ RTC_VL_READ returns a currently undocumented value.▶ There are different types of monitored voltages: main supply, battery or auxiliary

supply.▶ They can have multiple states: OK, low, low and functionalities are disabled, data

lost.▶ A new ioctl has to be developed.

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 16/23

Page 17: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Future changes: timestamping

▶ Some RTCs can store one or multiple timestamp when an event happens.▶ Currently exposed through /sys/class/rtc/rtcX/timestampX▶ This is open coded in each driver. There is room for factorization in the core.▶ Timestamp events will probably need to be configured in the future (timestamp

first event, last event, input pin).▶ Not sure whether this will be using a new ioctl or more sysfs files.

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 17/23

Page 18: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Future changes: backup switch mode

▶ It is possible to select the backup switch policy for some RTCs.▶ The common policies are:

▶ disabled▶ direct (VDD < VAUX)▶ standby▶ level (VDD < threshold)

▶ This will be implemented through a device tree property because it directlydepends on the type of auxiliary voltage provided by the board.

▶ It is necessary to avoid hardcoding in the driver because it may have been setproperly by the bootloader.

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 18/23

Page 19: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Future changes: alarm handling

▶ Alarm support detection. Many drivers modify the struct rtc_ops when alarmsare not present. This prevents constification.

▶ Alarms with minute granularity. Support is currently open coded in each driver.▶ Wakeup support: many parameters have to be taken into account to know

whether the RTC can wakeup the system, there is potential for factorization inthe core.

▶ Alarm routing:▶ Some RTCs have multiple interrupt pins and can configure which interrupt goes to

which pin.▶ This is useful to route alarms to a CPU (wakeup) or a PMIC (powerup)▶ Device tree properties will be used.

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 19/23

Page 20: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Future changes: BCD RTCs

▶ Many RTCs store the date and time in BCD in a somewhat common format.▶ There is potential for code factorization, especially when using regmap to access

the RTC registers.

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 20/23

Page 21: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Future changes: timers

▶ Some timers are now able to wakeup the platform.▶ They don’t actually handle the system time, either because the counter is too

small or it is counting downward or it is impossible to read it.▶ The drivers currently open code the dummy read_time and set_time to keep

the RTC core happy.

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 21/23

Page 22: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Future changes: other topics

▶ Revisit the RTC_EPOCH_READ and RTC_EPOCH_SET to change the RTC offset.This will allow changing the RTC offset at runtime.

▶ Write documentation on how to write an RTC driver and avoid the commonpitfalls.

▶ Split up the ds1307 frankendriver.

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 22/23

Page 23: RTC subsystem, recent · 2019-10-30 · Recent changes: RTC offset To solve RTC end of time issues, the core is now able to offset and window the hardware range. The offset is added

Questions? Suggestions? Comments?

Alexandre [email protected]

Slides under CC-BY-SA 3.0https://bootlin.com/pub/conferences/2019/elce/belloni-rtc

- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 23/23