Skip to content

arch/risc-v/k210: Add sysctl driver for clock and reset control.#18540

Open
no1wudi wants to merge 1 commit intoapache:masterfrom
no1wudi:k210
Open

arch/risc-v/k210: Add sysctl driver for clock and reset control.#18540
no1wudi wants to merge 1 commit intoapache:masterfrom
no1wudi:k210

Conversation

@no1wudi
Copy link
Contributor

@no1wudi no1wudi commented Mar 14, 2026

Summary

This change adds a comprehensive sysctl driver for the Kendryte K210 SoC, providing essential clock and reset control functionality. The driver enables proper clock management, peripheral reset control, and configurable CPU frequency.

  • Why: The K210 SoC requires proper clock and reset management for peripherals and CPU frequency configuration. Previously, the CPU frequency was hardcoded to 400MHz with limited clock control capabilities. This driver provides:

    • Complete clock enable/disable with proper two-stage APB/peripheral enable pattern
    • Peripheral reset control with proper timing
    • Clock frequency query for PLL, CPU, APB buses, and all peripherals
    • PLL frequency calculation and runtime CPU frequency configuration
    • Configurable CPU frequency via Kconfig (40-600 MHz range)
  • What:

    • New driver: k210_sysctl.c - Full sysctl implementation with clock/reset control
    • New header: k210_sysctl.h - Public API for clock and reset operations
    • Extended hardware definitions: hardware/k210_sysctl.h - Complete register definitions
    • Kconfig option: K210_CPU_FREQ - Target CPU frequency in Hz (default 400MHz, range 40-600MHz)
    • Updated build files: CMakeLists.txt and Make.defs to include new driver
    • Updated clockconfig: Modified to use new sysctl driver for CPU frequency setup
  • How:

    • The sysctl driver is built unconditionally for K210 boards as it provides essential functionality
    • CPU frequency can be configured via make menuconfig → "K210 CPU target frequency (Hz)"
    • Runtime API available for peripheral clock/reset management
    • PLL frequency calculation ensures accurate clock generation

Impact

  • Is new feature added? YES - Complete sysctl driver with clock/reset control and configurable CPU frequency
  • Is existing feature changed? YES - CPU frequency configuration moved from hardcoded macro to Kconfig option
  • Impact on user? NO - Default behavior unchanged (400MHz), but users now have full clock control capabilities
  • Impact on build? NO - New source files added to build system
  • Impact on hardware? YES - K210 architecture only, enables proper peripheral clock/reset management
  • Impact on documentation? YES - Documentation update recommended to describe new Kconfig option and sysctl API
  • Impact on security? NO
  • Impact on compatibility? NO - Fully backward compatible, existing configurations continue to work

Testing

I confirm that changes are verified on local setup and works as intended:

  • Build Host(s): Linux x86_64, GCC 15.2.0, riscv-none-elf-gcc
  • Target(s): RISC-V K210, maix-bit:nsh

Build Verification

cmake -S nuttx -B build -DBOARD_CONFIG=boards/risc-v/k210/maix-bit/configs/nsh
cmake --build build -j8
# Build successful, nuttx.bin generated

CPU Frequency Testing

Tested CPU frequencies from 40MHz to 590MHz in 50MHz increments using getprime benchmark to verify:

  1. Kconfig configuration works correctly
  2. CPU frequency changes take effect at runtime
  3. Performance scales linearly with frequency

Testing results:

Frequency    getprime time    Performance
(MHz)        (msec)           (relative)
---------    -------------    ----------
40           9897             1.0x
90           4416             2.2x
140          2829             3.5x
190          2073             4.8x
240          1644             6.0x
290          1351             7.3x
340          1169             8.5x
390          1013             9.8x
440          894              11.1x
490          800              12.4x
540          724              13.7x
590          675              14.7x

Results demonstrate:

  • CPU frequency configuration via Kconfig works correctly across full range
  • Performance scales linearly with frequency (40MHz to 590MHz = ~14.7x speedup)
  • Full range of 40-600 MHz is functional and stable

Runtime Verification

Verified on real K210 hardware (Maix Bit board):

  • NSH shell boots successfully at all tested frequencies
  • UART communication stable at 115200 baud
  • getprime benchmark runs correctly
  • No crashes or instability observed

Files Changed

  • arch/risc-v/src/k210/CMakeLists.txt - Add k210_sysctl.c to build
  • arch/risc-v/src/k210/Make.defs - Add k210_sysctl.c to build
  • arch/risc-v/src/k210/Kconfig - Add K210_CPU_FREQ configuration option
  • arch/risc-v/src/k210/hardware/k210_sysctl.h - Extended register definitions
  • arch/risc-v/src/k210/k210_clockconfig.c - Use new sysctl driver
  • arch/risc-v/src/k210/k210_sysctl.c - New sysctl driver implementation
  • arch/risc-v/src/k210/k210_sysctl.h - New public API header

@github-actions github-actions bot added Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. labels Mar 14, 2026
Add sysctl driver for K210 SoC providing clock and reset control:
- Clock enable/disable with two-stage APB/peripheral enable pattern
- Peripheral reset control with proper timing
- Clock frequency query for PLL, CPU, APB buses, and peripherals
- PLL frequency calculation and CPU frequency configuration

The sysctl driver is built unconditionally for K210 boards as it
provides essential clock and reset control functionality.

Add Kconfig option:
- K210_CPU_FREQ: Target CPU frequency in Hz (default 400MHz)

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
Copy link
Contributor

@acassis acassis left a comment

Choose a reason for hiding this comment

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

@no1wudi very nice! Please update the K210 Documentation to include this info about this sysctl?

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

Labels

Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants