17c36209bSGhennadi Procopciuc /* SPDX-License-Identifier: BSD-3-Clause */ 27c36209bSGhennadi Procopciuc /* 37c36209bSGhennadi Procopciuc * Copyright 2020-2024 NXP 47c36209bSGhennadi Procopciuc */ 57c36209bSGhennadi Procopciuc #ifndef S32CC_CLK_MODULES_H 67c36209bSGhennadi Procopciuc #define S32CC_CLK_MODULES_H 77c36209bSGhennadi Procopciuc 87c36209bSGhennadi Procopciuc #include <inttypes.h> 912e7a2cdSGhennadi Procopciuc #include <stdbool.h> 107c36209bSGhennadi Procopciuc #include <stddef.h> 117c36209bSGhennadi Procopciuc 127c36209bSGhennadi Procopciuc #define MHZ UL(1000000) 137c36209bSGhennadi Procopciuc #define GHZ (UL(1000) * MHZ) 147c36209bSGhennadi Procopciuc 157c36209bSGhennadi Procopciuc enum s32cc_clkm_type { 167c36209bSGhennadi Procopciuc s32cc_osc_t, 177c36209bSGhennadi Procopciuc s32cc_clk_t, 18a8be748aSGhennadi Procopciuc s32cc_pll_t, 19a8be748aSGhennadi Procopciuc s32cc_pll_out_div_t, 2044ae54afSGhennadi Procopciuc s32cc_dfs_t, 2144ae54afSGhennadi Procopciuc s32cc_dfs_div_t, 22a8be748aSGhennadi Procopciuc s32cc_clkmux_t, 233fa91a94SGhennadi Procopciuc s32cc_shared_clkmux_t, 2444e2130aSGhennadi Procopciuc s32cc_fixed_div_t, 257c36209bSGhennadi Procopciuc }; 267c36209bSGhennadi Procopciuc 277c36209bSGhennadi Procopciuc enum s32cc_clk_source { 287c36209bSGhennadi Procopciuc S32CC_FIRC, 297c36209bSGhennadi Procopciuc S32CC_FXOSC, 307c36209bSGhennadi Procopciuc S32CC_SIRC, 31a8be748aSGhennadi Procopciuc S32CC_ARM_PLL, 3244ae54afSGhennadi Procopciuc S32CC_ARM_DFS, 33*8653352aSGhennadi Procopciuc S32CC_PERIPH_PLL, 349dbca85dSGhennadi Procopciuc S32CC_CGM0, 353fa91a94SGhennadi Procopciuc S32CC_CGM1, 367c36209bSGhennadi Procopciuc }; 377c36209bSGhennadi Procopciuc 387c36209bSGhennadi Procopciuc struct s32cc_clk_obj { 397c36209bSGhennadi Procopciuc enum s32cc_clkm_type type; 407c36209bSGhennadi Procopciuc uint32_t refcount; 417c36209bSGhennadi Procopciuc }; 427c36209bSGhennadi Procopciuc 437c36209bSGhennadi Procopciuc struct s32cc_osc { 447c36209bSGhennadi Procopciuc struct s32cc_clk_obj desc; 457c36209bSGhennadi Procopciuc enum s32cc_clk_source source; 467c36209bSGhennadi Procopciuc unsigned long freq; 477c36209bSGhennadi Procopciuc void *base; 487c36209bSGhennadi Procopciuc }; 497c36209bSGhennadi Procopciuc 507c36209bSGhennadi Procopciuc #define S32CC_OSC_INIT(SOURCE) \ 517c36209bSGhennadi Procopciuc { \ 527c36209bSGhennadi Procopciuc .desc = { \ 537c36209bSGhennadi Procopciuc .type = s32cc_osc_t, \ 547c36209bSGhennadi Procopciuc }, \ 557c36209bSGhennadi Procopciuc .source = (SOURCE), \ 567c36209bSGhennadi Procopciuc } 577c36209bSGhennadi Procopciuc 58a8be748aSGhennadi Procopciuc struct s32cc_clkmux { 59a8be748aSGhennadi Procopciuc struct s32cc_clk_obj desc; 60a8be748aSGhennadi Procopciuc enum s32cc_clk_source module; 61a8be748aSGhennadi Procopciuc uint8_t index; /* Mux index in parent module */ 62a8be748aSGhennadi Procopciuc unsigned long source_id; /* Selected source */ 63a8be748aSGhennadi Procopciuc uint8_t nclks; /* Number of input clocks */ 64a8be748aSGhennadi Procopciuc unsigned long clkids[5]; /* IDs of the input clocks */ 65a8be748aSGhennadi Procopciuc }; 66a8be748aSGhennadi Procopciuc 67a8be748aSGhennadi Procopciuc #define S32CC_CLKMUX_TYPE_INIT(TYPE, MODULE, INDEX, NCLKS, ...) \ 68a8be748aSGhennadi Procopciuc { \ 69a8be748aSGhennadi Procopciuc .desc = { \ 70a8be748aSGhennadi Procopciuc .type = (TYPE), \ 71a8be748aSGhennadi Procopciuc }, \ 72a8be748aSGhennadi Procopciuc .module = (MODULE), \ 73a8be748aSGhennadi Procopciuc .index = (INDEX), \ 74a8be748aSGhennadi Procopciuc .nclks = (NCLKS), \ 75a8be748aSGhennadi Procopciuc .clkids = {__VA_ARGS__}, \ 76a8be748aSGhennadi Procopciuc } 77a8be748aSGhennadi Procopciuc 78a8be748aSGhennadi Procopciuc #define S32CC_CLKMUX_INIT(MODULE, INDEX, NCLKS, ...) \ 79a8be748aSGhennadi Procopciuc S32CC_CLKMUX_TYPE_INIT(s32cc_clkmux_t, MODULE, \ 80a8be748aSGhennadi Procopciuc INDEX, NCLKS, __VA_ARGS__) 81a8be748aSGhennadi Procopciuc 823fa91a94SGhennadi Procopciuc #define S32CC_SHARED_CLKMUX_INIT(MODULE, INDEX, NCLKS, ...) \ 833fa91a94SGhennadi Procopciuc S32CC_CLKMUX_TYPE_INIT(s32cc_shared_clkmux_t, MODULE, \ 843fa91a94SGhennadi Procopciuc INDEX, NCLKS, __VA_ARGS__) 853fa91a94SGhennadi Procopciuc 86a8be748aSGhennadi Procopciuc struct s32cc_pll { 87a8be748aSGhennadi Procopciuc struct s32cc_clk_obj desc; 88a8be748aSGhennadi Procopciuc struct s32cc_clk_obj *source; 89a8be748aSGhennadi Procopciuc enum s32cc_clk_source instance; 90a8be748aSGhennadi Procopciuc unsigned long vco_freq; 91a8be748aSGhennadi Procopciuc uint32_t ndividers; 92a8be748aSGhennadi Procopciuc uintptr_t base; 93a8be748aSGhennadi Procopciuc }; 94a8be748aSGhennadi Procopciuc 95a8be748aSGhennadi Procopciuc #define S32CC_PLL_INIT(PLL_MUX_CLK, INSTANCE, NDIVIDERS) \ 96a8be748aSGhennadi Procopciuc { \ 97a8be748aSGhennadi Procopciuc .desc = { \ 98a8be748aSGhennadi Procopciuc .type = s32cc_pll_t, \ 99a8be748aSGhennadi Procopciuc }, \ 100a8be748aSGhennadi Procopciuc .source = &(PLL_MUX_CLK).desc, \ 101a8be748aSGhennadi Procopciuc .instance = (INSTANCE), \ 102a8be748aSGhennadi Procopciuc .ndividers = (NDIVIDERS), \ 103a8be748aSGhennadi Procopciuc } 104a8be748aSGhennadi Procopciuc 105a8be748aSGhennadi Procopciuc struct s32cc_pll_out_div { 106a8be748aSGhennadi Procopciuc struct s32cc_clk_obj desc; 107a8be748aSGhennadi Procopciuc struct s32cc_clk_obj *parent; 108a8be748aSGhennadi Procopciuc uint32_t index; 109a8be748aSGhennadi Procopciuc unsigned long freq; 110a8be748aSGhennadi Procopciuc }; 111a8be748aSGhennadi Procopciuc 112a8be748aSGhennadi Procopciuc #define S32CC_PLL_OUT_DIV_INIT(PARENT, INDEX) \ 113a8be748aSGhennadi Procopciuc { \ 114a8be748aSGhennadi Procopciuc .desc = { \ 115a8be748aSGhennadi Procopciuc .type = s32cc_pll_out_div_t, \ 116a8be748aSGhennadi Procopciuc }, \ 117a8be748aSGhennadi Procopciuc .parent = &(PARENT).desc, \ 118a8be748aSGhennadi Procopciuc .index = (INDEX), \ 119a8be748aSGhennadi Procopciuc } 120a8be748aSGhennadi Procopciuc 12144e2130aSGhennadi Procopciuc #define S32CC_PLL_OUT_DIV_INIT(PARENT, INDEX) \ 12244e2130aSGhennadi Procopciuc { \ 12344e2130aSGhennadi Procopciuc .desc = { \ 12444e2130aSGhennadi Procopciuc .type = s32cc_pll_out_div_t, \ 12544e2130aSGhennadi Procopciuc }, \ 12644e2130aSGhennadi Procopciuc .parent = &(PARENT).desc, \ 12744e2130aSGhennadi Procopciuc .index = (INDEX), \ 12844e2130aSGhennadi Procopciuc } 12944e2130aSGhennadi Procopciuc 13044ae54afSGhennadi Procopciuc struct s32cc_dfs { 13144ae54afSGhennadi Procopciuc struct s32cc_clk_obj desc; 13244ae54afSGhennadi Procopciuc struct s32cc_clk_obj *parent; 13344ae54afSGhennadi Procopciuc enum s32cc_clk_source instance; 13444ae54afSGhennadi Procopciuc uintptr_t base; 13544ae54afSGhennadi Procopciuc }; 13644ae54afSGhennadi Procopciuc 13744ae54afSGhennadi Procopciuc #define S32CC_DFS_INIT(PARENT, INSTANCE) \ 13844ae54afSGhennadi Procopciuc { \ 13944ae54afSGhennadi Procopciuc .desc = { \ 14044ae54afSGhennadi Procopciuc .type = s32cc_dfs_t, \ 14144ae54afSGhennadi Procopciuc }, \ 14244ae54afSGhennadi Procopciuc .parent = &(PARENT).desc, \ 14344ae54afSGhennadi Procopciuc .instance = (INSTANCE), \ 14444ae54afSGhennadi Procopciuc } 14544ae54afSGhennadi Procopciuc 14644ae54afSGhennadi Procopciuc struct s32cc_dfs_div { 14744ae54afSGhennadi Procopciuc struct s32cc_clk_obj desc; 14844ae54afSGhennadi Procopciuc struct s32cc_clk_obj *parent; 14944ae54afSGhennadi Procopciuc uint32_t index; 15044ae54afSGhennadi Procopciuc unsigned long freq; 15144ae54afSGhennadi Procopciuc }; 15244ae54afSGhennadi Procopciuc 15344ae54afSGhennadi Procopciuc #define S32CC_DFS_DIV_INIT(PARENT, INDEX) \ 15444ae54afSGhennadi Procopciuc { \ 15544ae54afSGhennadi Procopciuc .desc = { \ 15644ae54afSGhennadi Procopciuc .type = s32cc_dfs_div_t, \ 15744ae54afSGhennadi Procopciuc }, \ 15844ae54afSGhennadi Procopciuc .parent = &(PARENT).desc, \ 15944ae54afSGhennadi Procopciuc .index = (INDEX), \ 16044ae54afSGhennadi Procopciuc } 16144ae54afSGhennadi Procopciuc 16244e2130aSGhennadi Procopciuc struct s32cc_fixed_div { 16344e2130aSGhennadi Procopciuc struct s32cc_clk_obj desc; 16444e2130aSGhennadi Procopciuc struct s32cc_clk_obj *parent; 16544e2130aSGhennadi Procopciuc uint32_t rate_div; 16644e2130aSGhennadi Procopciuc }; 16744e2130aSGhennadi Procopciuc 16844e2130aSGhennadi Procopciuc #define S32CC_FIXED_DIV_INIT(PARENT, RATE_DIV) \ 16944e2130aSGhennadi Procopciuc { \ 17044e2130aSGhennadi Procopciuc .desc = { \ 17144e2130aSGhennadi Procopciuc .type = s32cc_fixed_div_t, \ 17244e2130aSGhennadi Procopciuc }, \ 17344e2130aSGhennadi Procopciuc .parent = &(PARENT).desc, \ 17444e2130aSGhennadi Procopciuc .rate_div = (RATE_DIV), \ 17544e2130aSGhennadi Procopciuc } 17644e2130aSGhennadi Procopciuc 1777c36209bSGhennadi Procopciuc struct s32cc_clk { 1787c36209bSGhennadi Procopciuc struct s32cc_clk_obj desc; 1797c36209bSGhennadi Procopciuc struct s32cc_clk_obj *module; 1807c36209bSGhennadi Procopciuc struct s32cc_clk *pclock; 1817c36209bSGhennadi Procopciuc unsigned long min_freq; 1827c36209bSGhennadi Procopciuc unsigned long max_freq; 1837c36209bSGhennadi Procopciuc }; 1847c36209bSGhennadi Procopciuc 1857c36209bSGhennadi Procopciuc struct s32cc_clk_array { 1867c36209bSGhennadi Procopciuc unsigned long type_mask; 1877c36209bSGhennadi Procopciuc struct s32cc_clk **clks; 1887c36209bSGhennadi Procopciuc size_t n_clks; 1897c36209bSGhennadi Procopciuc }; 1907c36209bSGhennadi Procopciuc 19144ae54afSGhennadi Procopciuc #define S32CC_FREQ_CLK(PARENT_MODULE, PARENT, MIN_F, MAX_F) \ 1927c36209bSGhennadi Procopciuc { \ 1937c36209bSGhennadi Procopciuc .desc = { \ 1947c36209bSGhennadi Procopciuc .type = s32cc_clk_t, \ 1957c36209bSGhennadi Procopciuc }, \ 19644ae54afSGhennadi Procopciuc .pclock = (PARENT), \ 19744ae54afSGhennadi Procopciuc .module = (PARENT_MODULE), \ 1987c36209bSGhennadi Procopciuc .min_freq = (MIN_F), \ 1997c36209bSGhennadi Procopciuc .max_freq = (MAX_F), \ 2007c36209bSGhennadi Procopciuc } 2017c36209bSGhennadi Procopciuc 2027c36209bSGhennadi Procopciuc #define S32CC_FREQ_MODULE_CLK(PARENT_MODULE, MIN_F, MAX_F) \ 20344ae54afSGhennadi Procopciuc S32CC_FREQ_CLK(&(PARENT_MODULE).desc, NULL, MIN_F, MAX_F) 2047c36209bSGhennadi Procopciuc 2057c36209bSGhennadi Procopciuc #define S32CC_MODULE_CLK(PARENT_MODULE) \ 2067c36209bSGhennadi Procopciuc S32CC_FREQ_MODULE_CLK(PARENT_MODULE, 0, 0) 2077c36209bSGhennadi Procopciuc 20844ae54afSGhennadi Procopciuc #define S32CC_CHILD_CLK(PARENT, MIN_F, MAX_F) \ 20944ae54afSGhennadi Procopciuc S32CC_FREQ_CLK(NULL, &(PARENT), MIN_F, MAX_F) 21044ae54afSGhennadi Procopciuc 211d9373519SGhennadi Procopciuc static inline struct s32cc_osc *s32cc_obj2osc(const struct s32cc_clk_obj *mod) 212d9373519SGhennadi Procopciuc { 213d9373519SGhennadi Procopciuc uintptr_t osc_addr; 214d9373519SGhennadi Procopciuc 215d9373519SGhennadi Procopciuc osc_addr = ((uintptr_t)mod) - offsetof(struct s32cc_osc, desc); 216d9373519SGhennadi Procopciuc return (struct s32cc_osc *)osc_addr; 217d9373519SGhennadi Procopciuc } 218d9373519SGhennadi Procopciuc 219d9373519SGhennadi Procopciuc static inline struct s32cc_clk *s32cc_obj2clk(const struct s32cc_clk_obj *mod) 220d9373519SGhennadi Procopciuc { 221d9373519SGhennadi Procopciuc uintptr_t clk_addr; 222d9373519SGhennadi Procopciuc 223d9373519SGhennadi Procopciuc clk_addr = ((uintptr_t)mod) - offsetof(struct s32cc_clk, desc); 224d9373519SGhennadi Procopciuc return (struct s32cc_clk *)clk_addr; 225d9373519SGhennadi Procopciuc } 226d9373519SGhennadi Procopciuc 22712e7a2cdSGhennadi Procopciuc static inline bool is_s32cc_clk_mux(const struct s32cc_clk *clk) 22812e7a2cdSGhennadi Procopciuc { 22912e7a2cdSGhennadi Procopciuc const struct s32cc_clk_obj *module; 23012e7a2cdSGhennadi Procopciuc 23112e7a2cdSGhennadi Procopciuc module = clk->module; 23212e7a2cdSGhennadi Procopciuc if (module == NULL) { 23312e7a2cdSGhennadi Procopciuc return false; 23412e7a2cdSGhennadi Procopciuc } 23512e7a2cdSGhennadi Procopciuc 2363fa91a94SGhennadi Procopciuc return (module->type == s32cc_clkmux_t) || 2373fa91a94SGhennadi Procopciuc (module->type == s32cc_shared_clkmux_t); 23812e7a2cdSGhennadi Procopciuc } 23912e7a2cdSGhennadi Procopciuc 24012e7a2cdSGhennadi Procopciuc static inline struct s32cc_clkmux *s32cc_obj2clkmux(const struct s32cc_clk_obj *mod) 24112e7a2cdSGhennadi Procopciuc { 24212e7a2cdSGhennadi Procopciuc uintptr_t cmux_addr; 24312e7a2cdSGhennadi Procopciuc 24412e7a2cdSGhennadi Procopciuc cmux_addr = ((uintptr_t)mod) - offsetof(struct s32cc_clkmux, desc); 24512e7a2cdSGhennadi Procopciuc return (struct s32cc_clkmux *)cmux_addr; 24612e7a2cdSGhennadi Procopciuc } 24712e7a2cdSGhennadi Procopciuc 24812e7a2cdSGhennadi Procopciuc static inline struct s32cc_clkmux *s32cc_clk2mux(const struct s32cc_clk *clk) 24912e7a2cdSGhennadi Procopciuc { 25012e7a2cdSGhennadi Procopciuc if (!is_s32cc_clk_mux(clk)) { 25112e7a2cdSGhennadi Procopciuc return NULL; 25212e7a2cdSGhennadi Procopciuc } 25312e7a2cdSGhennadi Procopciuc 25412e7a2cdSGhennadi Procopciuc return s32cc_obj2clkmux(clk->module); 25512e7a2cdSGhennadi Procopciuc } 25612e7a2cdSGhennadi Procopciuc 2577ad4e231SGhennadi Procopciuc static inline struct s32cc_pll *s32cc_obj2pll(const struct s32cc_clk_obj *mod) 2587ad4e231SGhennadi Procopciuc { 2597ad4e231SGhennadi Procopciuc uintptr_t pll_addr; 2607ad4e231SGhennadi Procopciuc 2617ad4e231SGhennadi Procopciuc pll_addr = ((uintptr_t)mod) - offsetof(struct s32cc_pll, desc); 2627ad4e231SGhennadi Procopciuc return (struct s32cc_pll *)pll_addr; 2637ad4e231SGhennadi Procopciuc } 2647ad4e231SGhennadi Procopciuc 265de950ef0SGhennadi Procopciuc static inline struct s32cc_pll_out_div *s32cc_obj2plldiv(const struct s32cc_clk_obj *mod) 266de950ef0SGhennadi Procopciuc { 267de950ef0SGhennadi Procopciuc uintptr_t plldiv_addr; 268de950ef0SGhennadi Procopciuc 269de950ef0SGhennadi Procopciuc plldiv_addr = ((uintptr_t)mod) - offsetof(struct s32cc_pll_out_div, desc); 270de950ef0SGhennadi Procopciuc return (struct s32cc_pll_out_div *)plldiv_addr; 271de950ef0SGhennadi Procopciuc } 272de950ef0SGhennadi Procopciuc 27365739db2SGhennadi Procopciuc static inline struct s32cc_fixed_div *s32cc_obj2fixeddiv(const struct s32cc_clk_obj *mod) 27465739db2SGhennadi Procopciuc { 27565739db2SGhennadi Procopciuc uintptr_t fdiv_addr; 27665739db2SGhennadi Procopciuc 27765739db2SGhennadi Procopciuc fdiv_addr = ((uintptr_t)mod) - offsetof(struct s32cc_fixed_div, desc); 27865739db2SGhennadi Procopciuc return (struct s32cc_fixed_div *)fdiv_addr; 27965739db2SGhennadi Procopciuc } 28065739db2SGhennadi Procopciuc 28144ae54afSGhennadi Procopciuc static inline struct s32cc_dfs *s32cc_obj2dfs(const struct s32cc_clk_obj *mod) 28244ae54afSGhennadi Procopciuc { 28344ae54afSGhennadi Procopciuc uintptr_t dfs_addr; 28444ae54afSGhennadi Procopciuc 28544ae54afSGhennadi Procopciuc dfs_addr = ((uintptr_t)mod) - offsetof(struct s32cc_dfs, desc); 28644ae54afSGhennadi Procopciuc return (struct s32cc_dfs *)dfs_addr; 28744ae54afSGhennadi Procopciuc } 28844ae54afSGhennadi Procopciuc 28944ae54afSGhennadi Procopciuc static inline struct s32cc_dfs_div *s32cc_obj2dfsdiv(const struct s32cc_clk_obj *mod) 29044ae54afSGhennadi Procopciuc { 29144ae54afSGhennadi Procopciuc uintptr_t dfs_div_addr; 29244ae54afSGhennadi Procopciuc 29344ae54afSGhennadi Procopciuc dfs_div_addr = ((uintptr_t)mod) - offsetof(struct s32cc_dfs_div, desc); 29444ae54afSGhennadi Procopciuc return (struct s32cc_dfs_div *)dfs_div_addr; 29544ae54afSGhennadi Procopciuc } 29644ae54afSGhennadi Procopciuc 2977c36209bSGhennadi Procopciuc #endif /* S32CC_CLK_MODULES_H */ 298