Skip to content

Add MFD Support for Motorola mc146818 RTC, Counter, and BBRAM #91751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

akanisetti
Copy link
Contributor

@akanisetti akanisetti commented Jun 17, 2025

This PR introduces support for the Motorola mc146818 RTC and its associated functionalities through a Multi-Function Device (MFD) framework. The following key changes are included:

MFD Driver: Added and enabled the motorola, mc146818 MFD driver to manage RTC read/write operations and ensure data integrity through synchronized access.
RTC Driver: Enabled MFD support in the existing RTC driver for mc146818.
Counter Driver: Integrated MFD support into the CMOS counter driver.
BBRAM Driver: Enabled access to RTC RAM via a new BBRAM driver under the MFD hierarchy.
Device Tree Updates: Modified relevant .dtsi files to support the mc146818 MFD, including RTC, Counter, and BBRAM components.

These changes collectively enable full support for the mc146818 device and its subcomponents in a modular and maintainable way.

Signed-off-by: Anisetti Avinash Krishna anisetti.avinash.krishna@intel.com

@akanisetti akanisetti requested a review from Copilot June 18, 2025 05:54
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enables the Motorola MC146818 RTC’s 241 byte battery-backed RAM (BBRAM) as a child device of the MC146818 RTC on Raptor Lake-S platforms.

  • Extracts and centralizes the RTC driver’s data structure into a shared header.
  • Adds device-tree nodes and bindings for the MC146818 BBRAM.
  • Introduces and integrates a new bbram_mc146818 driver, plus corresponding Kconfig and build files.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
include/zephyr/drivers/rtc/mc146818.h Define shared rtc_mc146818_data struct
dts/x86/intel/raptor_lake_s.dtsi Add bbram@72 node under RTC for battery-backed RAM
dts/bindings/memory-controllers/motorola,mc146818-bbram.yaml Create binding for motorola,mc146818-bbram
drivers/rtc/rtc_mc146818.c Remove duplicate struct, include new header
drivers/bbram/bbram_mc146818.c New driver implementation for MC146818 BBRAM
drivers/bbram/Kconfig.mc146818 Add Kconfig option for MC146818 BBRAM
drivers/bbram/Kconfig Include the new Kconfig file
drivers/bbram/CMakeLists.txt Add bbram_mc146818.c to library sources
boards/intel/rpl/intel_rpl_s_crb.dts Alias bbram to the new BBRAM node

Copy link
Contributor

@bjarki-andreasen bjarki-andreasen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check out how the ds3231 is implemented, moving common functions of the "parent" device to include/zephyr/drivers/mfd and implementation of common code to zephyr/drivers/mfd, while keeping the bbram and rtc partial drivers isolated (not sharing data structures between one or the other.

@akanisetti
Copy link
Contributor Author

akanisetti commented Jun 27, 2025

Please check out how the ds3231 is implemented, moving common functions of the "parent" device to include/zephyr/drivers/mfd and implementation of common code to zephyr/drivers/mfd, while keeping the bbram and rtc partial drivers isolated (not sharing data structures between one or the other.

Sure, I will add the modifications suggested. With this approach the intension is to avoid data structures between BBRAM and RTC, but they will be two independent drivers under one MFD driver as its child nodes in DTS. Please confirm if my understanding is correct.

@bjarki-andreasen
Copy link
Contributor

Please check out how the ds3231 is implemented, moving common functions of the "parent" device to include/zephyr/drivers/mfd and implementation of common code to zephyr/drivers/mfd, while keeping the bbram and rtc partial drivers isolated (not sharing data structures between one or the other.

Sure, I will the modifications suggested. With this approach the intension is to avoid data structures between BBRAM and RTC, but they will be two independent drivers under one MFD driver as its child nodes in DTS. Please confirm if my understanding is correct.

That is correct :) It should be possible to enable only BBRAM, and only RTC parts of the device. That means, using kconfig to describe it, you should have

config MFD_MC146818
        bool
        help
          common code required by bbram and rtc, which will likely be a locking mechanism and a bus transfer,
          so both RTC and BBRAM can set/get regs on the "bus/device" safely and independently. The MC146818
          may not actually need this if the registers can be and will be interacted with independently, this would be
          for a i2c or spi attached device for example

config RTC_MC146818
        bool "RTC driver part"
        select MFD_MC146818
        depends on RTC

config BBRAM_MC146818
        bool "BBRAM driver part"
        select MFD_MC146818
        depends on BBRAM

@akanisetti
Copy link
Contributor Author

Please check out how the ds3231 is implemented, moving common functions of the "parent" device to include/zephyr/drivers/mfd and implementation of common code to zephyr/drivers/mfd, while keeping the bbram and rtc partial drivers isolated (not sharing data structures between one or the other.

Sure, I will the modifications suggested. With this approach the intension is to avoid data structures between BBRAM and RTC, but they will be two independent drivers under one MFD driver as its child nodes in DTS. Please confirm if my understanding is correct.

That is correct :) It should be possible to enable only BBRAM, and only RTC parts of the device. That means, using kconfig to describe it, you should have

config MFD_MC146818
        bool
        help
          common code required by bbram and rtc, which will likely be a locking mechanism and a bus transfer,
          so both RTC and BBRAM can set/get regs on the "bus/device" safely and independently. The MC146818
          may not actually need this if the registers can be and will be interacted with independently, this would be
          for a i2c or spi attached device for example

config RTC_MC146818
        bool "RTC driver part"
        select MFD_MC146818
        depends on RTC

config BBRAM_MC146818
        bool "BBRAM driver part"
        select MFD_MC146818
        depends on BBRAM

A small query regarding counter, using this method COUNTER_CMOS will also have dependency on MFD_MC146818.

@bjarki-andreasen
Copy link
Contributor

Please check out how the ds3231 is implemented, moving common functions of the "parent" device to include/zephyr/drivers/mfd and implementation of common code to zephyr/drivers/mfd, while keeping the bbram and rtc partial drivers isolated (not sharing data structures between one or the other.

Sure, I will the modifications suggested. With this approach the intension is to avoid data structures between BBRAM and RTC, but they will be two independent drivers under one MFD driver as its child nodes in DTS. Please confirm if my understanding is correct.

That is correct :) It should be possible to enable only BBRAM, and only RTC parts of the device. That means, using kconfig to describe it, you should have

config MFD_MC146818
        bool
        help
          common code required by bbram and rtc, which will likely be a locking mechanism and a bus transfer,
          so both RTC and BBRAM can set/get regs on the "bus/device" safely and independently. The MC146818
          may not actually need this if the registers can be and will be interacted with independently, this would be
          for a i2c or spi attached device for example

config RTC_MC146818
        bool "RTC driver part"
        select MFD_MC146818
        depends on RTC

config BBRAM_MC146818
        bool "BBRAM driver part"
        select MFD_MC146818
        depends on BBRAM

A small query regarding counter, using this method COUNTER_CMOS will also have dependency on MFD_MC146818.

Sort if, COUNTER_CMOS and RTC_MC146818 are mutually exclusive, COUNTER_CMOS is just there for legacy reasons, you could refactor it to work with MFD_MC146818 yes, but unless you have a usecase specifically for COUNTER_CMOS I would leave it alone :)

@akanisetti
Copy link
Contributor Author

Please check out how the ds3231 is implemented, moving common functions of the "parent" device to include/zephyr/drivers/mfd and implementation of common code to zephyr/drivers/mfd, while keeping the bbram and rtc partial drivers isolated (not sharing data structures between one or the other.

Sure, I will the modifications suggested. With this approach the intension is to avoid data structures between BBRAM and RTC, but they will be two independent drivers under one MFD driver as its child nodes in DTS. Please confirm if my understanding is correct.

That is correct :) It should be possible to enable only BBRAM, and only RTC parts of the device. That means, using kconfig to describe it, you should have

config MFD_MC146818
        bool
        help
          common code required by bbram and rtc, which will likely be a locking mechanism and a bus transfer,
          so both RTC and BBRAM can set/get regs on the "bus/device" safely and independently. The MC146818
          may not actually need this if the registers can be and will be interacted with independently, this would be
          for a i2c or spi attached device for example

config RTC_MC146818
        bool "RTC driver part"
        select MFD_MC146818
        depends on RTC

config BBRAM_MC146818
        bool "BBRAM driver part"
        select MFD_MC146818
        depends on BBRAM

A small query regarding counter, using this method COUNTER_CMOS will also have dependency on MFD_MC146818.

Sort if, COUNTER_CMOS and RTC_MC146818 are mutually exclusive, COUNTER_CMOS is just there for legacy reasons, you could refactor it to work with MFD_MC146818 yes, but unless you have a usecase specifically for COUNTER_CMOS I would leave it alone :)

Okay, I will update the PR accordingly

@zephyrbot zephyrbot added the platform: Intel Intel Corporation label Jul 18, 2025
@akanisetti akanisetti changed the title drivers: bbram: Enable bbram driver for MC146818 Add MFD Support for Motorola mc146818 RTC, Counter, and BBRAM Jul 18, 2025
@akanisetti akanisetti force-pushed the bbram_mc141618 branch 2 times, most recently from fa69ee0 to 9ec3de2 Compare July 18, 2025 10:12
Copy link
Contributor

@bjarki-andreasen bjarki-andreasen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic work! I have a few improvements but overall the addition is great!

RTC_MC146818_INIT_FN_DEFINE(inst) \
DEVICE_DT_INST_DEFINE(inst, &rtc_mc146818_init##inst, NULL, \
&rtc_mc146818_data##inst, &rtc_mc146818_config##inst,\
POST_KERNEL, CONFIG_RTC_INIT_PRIORITY, \
Copy link
Contributor

@bjarki-andreasen bjarki-andreasen Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, update the CONFIG_RTC_INIT_PRIORITY to UTIL_INC(CONFIG_MFD_MOTOROLA_MC146818_INIT_PRIORITY). This will ensure the RTC is always initialized right after the parent :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do the same for the counter based driver :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

DEVICE_DT_INST_DEFINE(n, &bbram_mc146818_init, NULL, \
&bbram_data_##n, &bbram_config_##n, \
POST_KERNEL, \
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UTIL_INC(CONFIG_MFD_MOTOROLA_MC146818_INIT_PRIORITY)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Comment on lines +407 to +424
mfd: mfd@70 {
compatible = "motorola,mc146818-mfd";
reg = <0x70 0x01 0x71 0x01 0x72 0x01 0x73 0x01>;

rtc: counter: rtc {
compatible = "motorola,mc146818";
interrupts = <8 IRQ_TYPE_LOWEST_EDGE_RISING 3>;
interrupt-parent = <&intc>;
alarms-count = <1>;

status = "okay";
};

bbram: bbram {
compatible = "motorola,mc146818-bbram";
size = <241>;
status = "okay";
};
Copy link
Contributor

@bjarki-andreasen bjarki-andreasen Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add this as an example snippet to the description of the binding for "motorola,mc146818-mfd". That way it is clear to users how the MFD is supposed to be added to an soc :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated


compatible: "motorola,mc146818-mfd"

include: base.yaml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the following entry here

bus: mc146818-mfd

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then extend the bindings for the child devices with on-bus: mc146818-mfd to help users know the child bindings belong to this parent mfd :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Enabled Motorola, mc146818 MFD, which implements RTC
read/write operations and prevents data corruption
by synchronizing these operations.

Signed-off-by: Anisetti Avinash Krishna <anisetti.avinash.krishna@intel.com>
Enabled support for motorola mc146818 RTC driver.

Signed-off-by: Anisetti Avinash Krishna <anisetti.avinash.krishna@intel.com>
Enabled support for MFD in CMOS counter.

Signed-off-by: Anisetti Avinash Krishna <anisetti.avinash.krishna@intel.com>
Enables bbram driver for motorola, mc146818 under its MFD
to access the RAM of the RTC.

Signed-off-by: Anisetti Avinash Krishna <anisetti.avinash.krishna@intel.com>
Added required modifications to dtsi files to support Motorola, mc146818
MFD, Counter, RTC, and BBRAM (for supported devices).

Signed-off-by: Anisetti Avinash Krishna <anisetti.avinash.krishna@intel.com>
Copy link
Contributor

@bjarki-andreasen bjarki-andreasen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link

@akanisetti
Copy link
Contributor Author

Hi @yperess, could you please review the PR

@yperess
Copy link
Contributor

yperess commented Jul 18, 2025

Hi @yperess, could you please review the PR

Please fix the checks, whatever review I give will be dismissed automatically even if it's just a lint check that's failing.

@akanisetti
Copy link
Contributor Author

Hi @yperess, could you please review the PR

Please fix the checks, whatever review I give will be dismissed automatically even if it's just a lint check that's failing.

The check failure is not related to this PR and has to be fixed by another PR. So please continue with the review as I am working on fixing it through another PR.

@akanisetti
Copy link
Contributor Author

PR has been created to resolve the twister issue #93403

Copy link
Member

@seov-nordic seov-nordic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants