1 /* 2 * Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @addtogroup Driver 18 * 19 * @file cru.h 20 */ 21 #ifndef DRIVER_CRU_H 22 #define DRIVER_CRU_H 23 24 #include "sys/queue.h" 25 26 typedef struct _CLK_GATE { 27 SLIST_ENTRY(_CLK_GATE) entries; 28 unsigned int gateID; 29 int enableCount; 30 int refCount; 31 } CLK_GATE; 32 33 typedef struct _CLK_INIT { 34 const char *name; 35 unsigned int clkID; 36 unsigned int initRate; 37 } CLK_INIT; 38 39 typedef struct _CLK_UNUSED { 40 unsigned int isPmucru : 1; 41 unsigned int gateCon : 31; 42 unsigned int gateVal; 43 } CLK_UNUSED; 44 45 /** 46 * @brief Clock enable by gate ID. 47 * 48 * @param gateID Indicates the clock gate ID. 49 * @return Returns {@link HAL_SUCCESS} if the clock is enabled; 50 * returns {@link HAL_FAILURE} otherwise. For details about other return values, see the chip description. 51 */ 52 unsigned int ClkEnableByID(int gateID); 53 54 /** 55 * @brief Clock disable by gate ID. 56 * 57 * @param gateID Indicates the clock gate ID. 58 * @return Returns {@link HAL_SUCCESS} if the clock is disabled; 59 * returns {@link HAL_FAILURE} otherwise. For details about other return values, see the chip description. 60 */ 61 unsigned int ClkDisableByID(int gateID); 62 63 /** 64 * @brief Clock enable. 65 * 66 * @param gate Indicates the clock gate. 67 * @return Returns {@link HAL_SUCCESS} if the clock is enabled; 68 * returns {@link HAL_FAILURE} otherwise. For details about other return values, see the chip description. 69 */ 70 unsigned int ClkEnable(CLK_GATE *gate); 71 72 /** 73 * @brief Clock disable. 74 * 75 * @param gate Indicates the clock gate. 76 * @return Returns {@link HAL_SUCCESS} if the clock is enabled; 77 * returns {@link HAL_FAILURE} otherwise. For details about other return values, see the chip description. 78 */ 79 unsigned int ClkDisable(CLK_GATE *gate); 80 81 /** 82 * @brief Whether the clock is enabled. 83 * 84 * @param gate Indicates the clock gate. 85 * @return Returns 1 if the clock is enabled; otherwise return 0. 86 */ 87 unsigned int ClkIsEnabled(CLK_GATE *gate); 88 89 /** 90 * @brief Get clock gate struct. 91 * 92 * @param gateID Indicates the clock gate ID. 93 * @return Returns the pointer of clock gate; otherwise return NULL. 94 */ 95 CLK_GATE *GetClkGate(int gateID); 96 97 /** 98 * @brief Put clock gate. 99 * 100 * @param gate Indicates the clock gate. 101 */ 102 void PutClkGate(CLK_GATE *gate); 103 104 /** 105 * @brief Get clock rate. 106 * 107 * @param clkID Indicates the clock ID. 108 * @return Returns the clock rate. 109 */ 110 unsigned int ClkGetRate(eCLOCK_Name clkID); 111 112 /** 113 * @brief Set clock rate. 114 * 115 * @param clkID Indicates the clock ID. 116 * @param rate Indicates the clock rate. 117 * @return Returns {@link HAL_SUCCESS} if the clock rate is set; 118 * returns {@link HAL_FAILURE} otherwise. For details about other return values, see the chip description. 119 */ 120 unsigned int ClkSetRate(eCLOCK_Name clkID, unsigned int rate); 121 122 /** 123 * @brief Get HCLK system core frequency. 124 * 125 * @return Returns the frequency. 126 */ 127 unsigned int ClkHclkSysCoreFreq(void); 128 129 /** 130 * @brief Initializes clock device. 131 * 132 * @return Returns {@link HAL_SUCCESS} if the clock rate is initialized; 133 * returns {@link HAL_FAILURE} otherwise. For details about other return values, see the chip description. 134 */ 135 unsigned int ClkDevInit(void); 136 137 /** 138 * @brief Deinitializes clock device. 139 * 140 * @return Returns {@link HAL_SUCCESS} if the clock rate is initialized; 141 * returns {@link HAL_FAILURE} otherwise. For details about other return values, see the chip description. 142 */ 143 unsigned int ClkDeDeinit(void); 144 145 /** 146 * @brief Set rate and enable clock list. 147 * 148 * @param clkInits Indicates the clock list to be set and enable. 149 * @param clkCount Indicates the count of clock list. 150 * @param clkDump Indicates whether dump clock init results. 151 */ 152 void ClkInit(const CLK_INIT *clkInits, unsigned int clkCount, bool clkDump); 153 154 /** 155 * @brief Disable unused clock list. 156 * 157 * @param clkUnuseds Indicates the clock list to be disabled. 158 * @param clkCount Indicates the count of clock list. 159 */ 160 void clkDisableUnused(const CLK_UNUSED *clkUnuseds, unsigned int clkCount); 161 162 #endif 163