Zephyr Project API 4.5.0-rc2
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
clock_control_bflb_common.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 MASSDRIVER EI (massdriver.space)
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12
13#ifndef ZEPHYR_INCLUDE_DRIVERS_CLOCK_CONTROL_CLOCK_CONTROL_BFLB_COMMON_H_
14#define ZEPHYR_INCLUDE_DRIVERS_CLOCK_CONTROL_CLOCK_CONTROL_BFLB_COMMON_H_
15
21
25#define BFLB_MAIN_CLOCK_RC32M 0
27#define BFLB_MAIN_CLOCK_XTAL 1
29#define BFLB_MAIN_CLOCK_PLL_RC32M 2
31#define BFLB_MAIN_CLOCK_PLL_XTAL 3
33
35static inline void clock_bflb_settle(void)
36{
37 __asm__ volatile (".rept 20 ; nop ; .endr");
38}
39
46static inline void clock_bflb_set_root_clock(uint32_t clock)
47{
48 uint32_t tmp;
49
50 /* invalid value, fallback to internal 32M */
51 if (clock > BFLB_MAIN_CLOCK_PLL_XTAL) {
53 }
54 tmp = sys_read32(HBN_BASE + HBN_GLB_OFFSET);
55 tmp = (tmp & HBN_ROOT_CLK_SEL_UMSK) | (clock << HBN_ROOT_CLK_SEL_POS);
56 sys_write32(tmp, HBN_BASE + HBN_GLB_OFFSET);
57
59}
60
67{
68 uint32_t tmp;
69
70 tmp = sys_read32(HBN_BASE + HBN_GLB_OFFSET);
71 return ((tmp & HBN_ROOT_CLK_SEL_MSK) >> HBN_ROOT_CLK_SEL_POS);
72}
73
74
76
77/* Avoid overflow when calculating multipliers by reducing precision to 100 KHz
78 * Every _value and _base should be a multiple of this to stay exact
79 */
80#define BFLB_MUL_CLK_UNIT 100000U
81
82/* (_value * _top) / _base without 64-bit division. _top is evaluated twice. */
83#define BFLB_MUL_CLK(_value, _top, _base) \
84 ((((uint32_t)(_top) / ((uint32_t)(_base) / BFLB_MUL_CLK_UNIT)) * \
85 ((uint32_t)(_value) / BFLB_MUL_CLK_UNIT)) + \
86 ((((uint32_t)(_top) % ((uint32_t)(_base) / BFLB_MUL_CLK_UNIT)) * \
87 ((uint32_t)(_value) / BFLB_MUL_CLK_UNIT)) / \
88 ((uint32_t)(_base) / BFLB_MUL_CLK_UNIT)))
89
90/* Runnable from XIP (__udivdi3 call from libgcc) */
91#define BFLB_MUL_CLK_XIP(_value, _top, _base) \
92 (((uint64_t)(_value) * (uint64_t)(_top)) / (uint64_t)(_base))
93
95
97
98#endif /* ZEPHYR_INCLUDE_DRIVERS_CLOCK_CONTROL_CLOCK_CONTROL_BFLB_COMMON_H_ */
static uint32_t clock_bflb_get_root_clock(void)
Get the currently selected root (main) clock source.
Definition clock_control_bflb_common.h:66
static void clock_bflb_settle(void)
Busy-wait for a few CPU cycles to let a clock change settle.
Definition clock_control_bflb_common.h:35
#define BFLB_MAIN_CLOCK_RC32M
XCLK is RC32M, main clock is XCLK.
Definition clock_control_bflb_common.h:25
#define BFLB_MAIN_CLOCK_PLL_XTAL
XCLK is the crystal, main clock is the PLL.
Definition clock_control_bflb_common.h:31
static void clock_bflb_set_root_clock(uint32_t clock)
Select the root (main) clock source.
Definition clock_control_bflb_common.h:46
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
static ALWAYS_INLINE void sys_write32(uint32_t data, mem_addr_t addr)
Definition sys-io-common.h:70
static ALWAYS_INLINE uint32_t sys_read32(mem_addr_t addr)
Definition sys-io-common.h:59