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> 97c36209bSGhennadi Procopciuc #include <stddef.h> 107c36209bSGhennadi Procopciuc 117c36209bSGhennadi Procopciuc #define MHZ UL(1000000) 127c36209bSGhennadi Procopciuc #define GHZ (UL(1000) * MHZ) 137c36209bSGhennadi Procopciuc 147c36209bSGhennadi Procopciuc enum s32cc_clkm_type { 157c36209bSGhennadi Procopciuc s32cc_osc_t, 167c36209bSGhennadi Procopciuc s32cc_clk_t, 177c36209bSGhennadi Procopciuc }; 187c36209bSGhennadi Procopciuc 197c36209bSGhennadi Procopciuc enum s32cc_clk_source { 207c36209bSGhennadi Procopciuc S32CC_FIRC, 217c36209bSGhennadi Procopciuc S32CC_FXOSC, 227c36209bSGhennadi Procopciuc S32CC_SIRC, 237c36209bSGhennadi Procopciuc }; 247c36209bSGhennadi Procopciuc 257c36209bSGhennadi Procopciuc struct s32cc_clk_obj { 267c36209bSGhennadi Procopciuc enum s32cc_clkm_type type; 277c36209bSGhennadi Procopciuc uint32_t refcount; 287c36209bSGhennadi Procopciuc }; 297c36209bSGhennadi Procopciuc 307c36209bSGhennadi Procopciuc struct s32cc_osc { 317c36209bSGhennadi Procopciuc struct s32cc_clk_obj desc; 327c36209bSGhennadi Procopciuc enum s32cc_clk_source source; 337c36209bSGhennadi Procopciuc unsigned long freq; 347c36209bSGhennadi Procopciuc void *base; 357c36209bSGhennadi Procopciuc }; 367c36209bSGhennadi Procopciuc 377c36209bSGhennadi Procopciuc #define S32CC_OSC_INIT(SOURCE) \ 387c36209bSGhennadi Procopciuc { \ 397c36209bSGhennadi Procopciuc .desc = { \ 407c36209bSGhennadi Procopciuc .type = s32cc_osc_t, \ 417c36209bSGhennadi Procopciuc }, \ 427c36209bSGhennadi Procopciuc .source = (SOURCE), \ 437c36209bSGhennadi Procopciuc } 447c36209bSGhennadi Procopciuc 457c36209bSGhennadi Procopciuc struct s32cc_clk { 467c36209bSGhennadi Procopciuc struct s32cc_clk_obj desc; 477c36209bSGhennadi Procopciuc struct s32cc_clk_obj *module; 487c36209bSGhennadi Procopciuc struct s32cc_clk *pclock; 497c36209bSGhennadi Procopciuc unsigned long min_freq; 507c36209bSGhennadi Procopciuc unsigned long max_freq; 517c36209bSGhennadi Procopciuc }; 527c36209bSGhennadi Procopciuc 537c36209bSGhennadi Procopciuc struct s32cc_clk_array { 547c36209bSGhennadi Procopciuc unsigned long type_mask; 557c36209bSGhennadi Procopciuc struct s32cc_clk **clks; 567c36209bSGhennadi Procopciuc size_t n_clks; 577c36209bSGhennadi Procopciuc }; 587c36209bSGhennadi Procopciuc 597c36209bSGhennadi Procopciuc #define S32CC_FREQ_MODULE(PARENT_MODULE, MIN_F, MAX_F) \ 607c36209bSGhennadi Procopciuc { \ 617c36209bSGhennadi Procopciuc .desc = { \ 627c36209bSGhennadi Procopciuc .type = s32cc_clk_t, \ 637c36209bSGhennadi Procopciuc }, \ 647c36209bSGhennadi Procopciuc .module = &(PARENT_MODULE).desc, \ 657c36209bSGhennadi Procopciuc .min_freq = (MIN_F), \ 667c36209bSGhennadi Procopciuc .max_freq = (MAX_F), \ 677c36209bSGhennadi Procopciuc } 687c36209bSGhennadi Procopciuc 697c36209bSGhennadi Procopciuc #define S32CC_FREQ_MODULE_CLK(PARENT_MODULE, MIN_F, MAX_F) \ 707c36209bSGhennadi Procopciuc S32CC_FREQ_MODULE(PARENT_MODULE, MIN_F, MAX_F) 717c36209bSGhennadi Procopciuc 727c36209bSGhennadi Procopciuc #define S32CC_MODULE_CLK(PARENT_MODULE) \ 737c36209bSGhennadi Procopciuc S32CC_FREQ_MODULE_CLK(PARENT_MODULE, 0, 0) 747c36209bSGhennadi Procopciuc 75*d9373519SGhennadi Procopciuc static inline struct s32cc_osc *s32cc_obj2osc(const struct s32cc_clk_obj *mod) 76*d9373519SGhennadi Procopciuc { 77*d9373519SGhennadi Procopciuc uintptr_t osc_addr; 78*d9373519SGhennadi Procopciuc 79*d9373519SGhennadi Procopciuc osc_addr = ((uintptr_t)mod) - offsetof(struct s32cc_osc, desc); 80*d9373519SGhennadi Procopciuc return (struct s32cc_osc *)osc_addr; 81*d9373519SGhennadi Procopciuc } 82*d9373519SGhennadi Procopciuc 83*d9373519SGhennadi Procopciuc static inline struct s32cc_clk *s32cc_obj2clk(const struct s32cc_clk_obj *mod) 84*d9373519SGhennadi Procopciuc { 85*d9373519SGhennadi Procopciuc uintptr_t clk_addr; 86*d9373519SGhennadi Procopciuc 87*d9373519SGhennadi Procopciuc clk_addr = ((uintptr_t)mod) - offsetof(struct s32cc_clk, desc); 88*d9373519SGhennadi Procopciuc return (struct s32cc_clk *)clk_addr; 89*d9373519SGhennadi Procopciuc } 90*d9373519SGhennadi Procopciuc 917c36209bSGhennadi Procopciuc #endif /* S32CC_CLK_MODULES_H */ 92