1 /* 2 * Copyright (c) 2021-2022 HPMicro 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 */ 7 #ifndef HPM_CLOCK_DRV_H 8 #define HPM_CLOCK_DRV_H 9 10 #include "hpm_common.h" 11 #include "hpm_sysctl_drv.h" 12 13 14 /** 15 * @brief Error codes for clock driver 16 */ 17 enum { 18 status_clk_div_invalid = MAKE_STATUS(status_group_clk, 0), 19 status_clk_src_invalid = MAKE_STATUS(status_group_clk, 1), 20 status_clk_invalid = MAKE_STATUS(status_group_clk, 2), 21 status_clk_operation_unsupported = MAKE_STATUS(status_group_clk, 3), 22 status_clk_shared_ahb = MAKE_STATUS(status_group_clk, 4), 23 status_clk_shared_axi0 = MAKE_STATUS(status_group_clk, 5), 24 status_clk_shared_axi1 = MAKE_STATUS(status_group_clk, 6), 25 status_clk_shared_axi2 = MAKE_STATUS(status_group_clk, 7), 26 status_clk_shared_cpu0 = MAKE_STATUS(status_group_clk, 8), 27 status_clk_shared_cpu1 = MAKE_STATUS(status_group_clk, 9), 28 status_clk_fixed = MAKE_STATUS(status_group_clk, 10), 29 30 }; 31 32 33 34 /** 35 * @brief Clock source group definitions 36 */ 37 #define CLK_SRC_GROUP_COMMON (0U) 38 #define CLK_SRC_GROUP_ADC (1U) 39 #define CLK_SRC_GROUP_I2S (2U) 40 #define CLK_SRC_GROUP_WDG (3U) 41 #define CLK_SRC_GROUP_PMIC (4U) 42 #define CLK_SRC_GROUP_AHB (5U) 43 #define CLK_SRC_GROUP_AXI (6U) 44 #define CLK_SRC_GROUP_DAC (7U) 45 #define CLK_SRC_GROUP_CPU0 (9U) 46 #define CLK_SRC_GROUP_SRC (10U) 47 #define CLK_SRC_GROUP_INVALID (15U) 48 49 #define MAKE_CLK_SRC(src_grp, index) (((uint8_t)(src_grp)<<4) | (index)) 50 #define GET_CLK_SRC_GROUP(src) (((uint8_t)(src)>>4) & 0x0FU) 51 #define GET_CLK_SRC_INDEX(src) ((uint8_t)(src) & 0x0FU) 52 53 /** 54 * @brief Clock source definitions 55 */ 56 typedef enum _clock_sources { 57 clk_src_osc24m = MAKE_CLK_SRC(CLK_SRC_GROUP_COMMON, 0), 58 clk_src_pll0_clk0 = MAKE_CLK_SRC(CLK_SRC_GROUP_COMMON, 1), 59 clk_src_pll0_clk1 = MAKE_CLK_SRC(CLK_SRC_GROUP_COMMON, 2), 60 clk_src_pll0_clk2 = MAKE_CLK_SRC(CLK_SRC_GROUP_COMMON, 3), 61 clk_src_pll1_clk0 = MAKE_CLK_SRC(CLK_SRC_GROUP_COMMON, 4), 62 clk_src_pll1_clk1 = MAKE_CLK_SRC(CLK_SRC_GROUP_COMMON, 5), 63 clk_src_pll2_clk0 = MAKE_CLK_SRC(CLK_SRC_GROUP_COMMON, 6), 64 clk_src_pll2_clk1 = MAKE_CLK_SRC(CLK_SRC_GROUP_COMMON, 7), 65 clk_src_osc32k = MAKE_CLK_SRC(CLK_SRC_GROUP_COMMON, 8), 66 67 clk_adc_src_ana = MAKE_CLK_SRC(CLK_SRC_GROUP_ADC, 0), 68 clk_adc_src_ahb = MAKE_CLK_SRC(CLK_SRC_GROUP_ADC, 1), 69 70 clk_dac_src_ana = MAKE_CLK_SRC(CLK_SRC_GROUP_DAC, 0), 71 clk_dac_src_ahb = MAKE_CLK_SRC(CLK_SRC_GROUP_DAC, 1), 72 73 clk_i2s_src_aud0 = MAKE_CLK_SRC(CLK_SRC_GROUP_I2S, 0), 74 clk_i2s_src_aud1 = MAKE_CLK_SRC(CLK_SRC_GROUP_I2S, 1), 75 76 clk_wdg_src_ahb0 = MAKE_CLK_SRC(CLK_SRC_GROUP_WDG, 0), 77 clk_wdg_src_osc32k = MAKE_CLK_SRC(CLK_SRC_GROUP_WDG, 1), 78 79 clk_src_invalid = MAKE_CLK_SRC(CLK_SRC_GROUP_INVALID, 15), 80 } clk_src_t; 81 82 83 #define RESOURCE_INVALID (0xFFFFU) 84 #define RESOURCE_SHARED_PTPC (0xFFFEU) 85 #define RESOURCE_SHARED_CPU0 (0xFFFDU) 86 87 #define GET_CLOCK_SOURCE_FROM_CLK_SRC(clk_src) (clock_source_t)((uint32_t)(clk_src) & 0xFU) 88 89 /* Clock NAME related Macros */ 90 #define MAKE_CLOCK_NAME(resource, src_type, node) (((uint32_t)(resource) << 16) | ((uint32_t)(src_type) << 8) | ((uint32_t)node)) 91 #define GET_CLK_SRC_GROUP_FROM_NAME(name) (((uint32_t)(name) >> 8) & 0xFFUL) 92 #define GET_CLK_NODE_FROM_NAME(name) ((uint32_t)(name) & 0xFFUL) 93 #define GET_CLK_RESOURCE_FROM_NAME(name) ((uint32_t)(name) >> 16) 94 95 /** 96 * @brief Peripheral Clock Type Description 97 */ 98 typedef enum _clock_name { 99 clock_cpu0 = MAKE_CLOCK_NAME(sysctl_resource_cpu0, CLK_SRC_GROUP_CPU0, clock_node_cpu0), 100 clock_mchtmr0 = MAKE_CLOCK_NAME(sysctl_resource_mchtmr0, CLK_SRC_GROUP_COMMON, clock_node_mchtmr0), 101 clock_dram = MAKE_CLOCK_NAME(sysctl_resource_dram, CLK_SRC_GROUP_COMMON, clock_node_dram), 102 clock_xpi0 = MAKE_CLOCK_NAME(sysctl_resource_xpi0, CLK_SRC_GROUP_COMMON, clock_node_xpi0), 103 clock_xpi1 = MAKE_CLOCK_NAME(sysctl_resource_xpi1, CLK_SRC_GROUP_COMMON, clock_node_xpi1), 104 clock_gptmr0 = MAKE_CLOCK_NAME(sysctl_resource_gptmr0, CLK_SRC_GROUP_COMMON, clock_node_gptmr0), 105 clock_gptmr1 = MAKE_CLOCK_NAME(sysctl_resource_gptmr1, CLK_SRC_GROUP_COMMON, clock_node_gptmr1), 106 clock_gptmr2 = MAKE_CLOCK_NAME(sysctl_resource_gptmr2, CLK_SRC_GROUP_COMMON, clock_node_gptmr2), 107 clock_gptmr3 = MAKE_CLOCK_NAME(sysctl_resource_gptmr3, CLK_SRC_GROUP_COMMON, clock_node_gptmr3), 108 clock_uart0 = MAKE_CLOCK_NAME(sysctl_resource_uart0, CLK_SRC_GROUP_COMMON, clock_node_uart0), 109 clock_uart1 = MAKE_CLOCK_NAME(sysctl_resource_uart1, CLK_SRC_GROUP_COMMON, clock_node_uart1), 110 clock_uart2 = MAKE_CLOCK_NAME(sysctl_resource_uart2, CLK_SRC_GROUP_COMMON, clock_node_uart2), 111 clock_uart3 = MAKE_CLOCK_NAME(sysctl_resource_uart3, CLK_SRC_GROUP_COMMON, clock_node_uart3), 112 clock_uart4 = MAKE_CLOCK_NAME(sysctl_resource_uart4, CLK_SRC_GROUP_COMMON, clock_node_uart4), 113 clock_uart5 = MAKE_CLOCK_NAME(sysctl_resource_uart5, CLK_SRC_GROUP_COMMON, clock_node_uart5), 114 clock_uart6 = MAKE_CLOCK_NAME(sysctl_resource_uart6, CLK_SRC_GROUP_COMMON, clock_node_uart6), 115 clock_uart7 = MAKE_CLOCK_NAME(sysctl_resource_uart7, CLK_SRC_GROUP_COMMON, clock_node_uart7), 116 clock_i2c0 = MAKE_CLOCK_NAME(sysctl_resource_i2c0, CLK_SRC_GROUP_COMMON, clock_node_i2c0), 117 clock_i2c1 = MAKE_CLOCK_NAME(sysctl_resource_i2c1, CLK_SRC_GROUP_COMMON, clock_node_i2c1), 118 clock_i2c2 = MAKE_CLOCK_NAME(sysctl_resource_i2c2, CLK_SRC_GROUP_COMMON, clock_node_i2c2), 119 clock_i2c3 = MAKE_CLOCK_NAME(sysctl_resource_i2c3, CLK_SRC_GROUP_COMMON, clock_node_i2c3), 120 clock_spi0 = MAKE_CLOCK_NAME(sysctl_resource_spi0, CLK_SRC_GROUP_COMMON, clock_node_spi0), 121 clock_spi1 = MAKE_CLOCK_NAME(sysctl_resource_spi1, CLK_SRC_GROUP_COMMON, clock_node_spi1), 122 clock_spi2 = MAKE_CLOCK_NAME(sysctl_resource_spi2, CLK_SRC_GROUP_COMMON, clock_node_spi2), 123 clock_spi3 = MAKE_CLOCK_NAME(sysctl_resource_spi3, CLK_SRC_GROUP_COMMON, clock_node_spi3), 124 clock_can0 = MAKE_CLOCK_NAME(sysctl_resource_can0, CLK_SRC_GROUP_COMMON, clock_node_can0), 125 clock_can1 = MAKE_CLOCK_NAME(sysctl_resource_can1, CLK_SRC_GROUP_COMMON, clock_node_can1), 126 clock_sdxc0 = MAKE_CLOCK_NAME(sysctl_resource_sdxc0, CLK_SRC_GROUP_COMMON, clock_node_sdxc0), 127 clock_ntmr0 = MAKE_CLOCK_NAME(sysctl_resource_ntmr0, CLK_SRC_GROUP_COMMON, clock_node_ntmr0), 128 clock_ahb = MAKE_CLOCK_NAME(RESOURCE_SHARED_CPU0, CLK_SRC_GROUP_AHB, clock_node_ahb), 129 clock_axi = MAKE_CLOCK_NAME(RESOURCE_SHARED_CPU0, CLK_SRC_GROUP_AXI, clock_node_axi), 130 clock_axic = MAKE_CLOCK_NAME(sysctl_resource_axic, CLK_SRC_GROUP_AXI, clock_node_axi), 131 clock_axis = MAKE_CLOCK_NAME(sysctl_resource_axis, CLK_SRC_GROUP_AXI, clock_node_axi), 132 clock_ahbp = MAKE_CLOCK_NAME(sysctl_resource_ahbp, CLK_SRC_GROUP_AHB, clock_node_ahb), 133 134 clock_ptpc = MAKE_CLOCK_NAME(sysctl_resource_ptpc, CLK_SRC_GROUP_COMMON, clock_node_ptpc), 135 clock_ptp0 = MAKE_CLOCK_NAME(RESOURCE_SHARED_PTPC, CLK_SRC_GROUP_COMMON, clock_node_ptp0), 136 clock_ref0 = MAKE_CLOCK_NAME(sysctl_resource_ref0, CLK_SRC_GROUP_COMMON, clock_node_ref0), 137 clock_ref1 = MAKE_CLOCK_NAME(sysctl_resource_ref1, CLK_SRC_GROUP_COMMON, clock_node_ref0), 138 clock_watchdog0 = MAKE_CLOCK_NAME(sysctl_resource_wdg0, CLK_SRC_GROUP_WDG, 0), 139 clock_watchdog1 = MAKE_CLOCK_NAME(sysctl_resource_wdg1, CLK_SRC_GROUP_WDG, 1), 140 clock_puart = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_PMIC, 0), 141 clock_pwdg = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_PMIC, 1), 142 clock_eth0 = MAKE_CLOCK_NAME(sysctl_resource_eth0, CLK_SRC_GROUP_COMMON, clock_node_eth0), 143 clock_sdp = MAKE_CLOCK_NAME(sysctl_resource_sdp0, CLK_SRC_GROUP_AXI, 0), 144 clock_xdma = MAKE_CLOCK_NAME(sysctl_resource_dma1, CLK_SRC_GROUP_AXI, 1), 145 clock_rom = MAKE_CLOCK_NAME(sysctl_resource_rom0, CLK_SRC_GROUP_AXI, 2), 146 clock_ram0 = MAKE_CLOCK_NAME(sysctl_resource_ram0, CLK_SRC_GROUP_AXI, 3), 147 clock_usb0 = MAKE_CLOCK_NAME(sysctl_resource_usb0, CLK_SRC_GROUP_AXI, 4), 148 clock_kman = MAKE_CLOCK_NAME(sysctl_resource_kman, CLK_SRC_GROUP_AHB, 0), 149 clock_gpio = MAKE_CLOCK_NAME(sysctl_resource_gpio, CLK_SRC_GROUP_AHB, 1), 150 clock_mbx0 = MAKE_CLOCK_NAME(sysctl_resource_mbx0, CLK_SRC_GROUP_AHB, 2), 151 clock_hdma = MAKE_CLOCK_NAME(sysctl_resource_dma0, CLK_SRC_GROUP_AHB, 4), 152 clock_rng = MAKE_CLOCK_NAME(sysctl_resource_rng0, CLK_SRC_GROUP_AHB, 5), 153 clock_mot0 = MAKE_CLOCK_NAME(sysctl_resource_mot0, CLK_SRC_GROUP_AHB, 6), 154 clock_mot1 = MAKE_CLOCK_NAME(sysctl_resource_mot1, CLK_SRC_GROUP_AHB, 7), 155 clock_acmp = MAKE_CLOCK_NAME(sysctl_resource_acmp, CLK_SRC_GROUP_AHB, 10), 156 clock_pdm = MAKE_CLOCK_NAME(sysctl_resource_i2spdm0, CLK_SRC_GROUP_AHB, 11), 157 clock_dao = MAKE_CLOCK_NAME(sysctl_resource_i2sdao, CLK_SRC_GROUP_AHB, 12), 158 clock_msyn = MAKE_CLOCK_NAME(sysctl_resource_msyn, CLK_SRC_GROUP_AHB, 13), 159 clock_ffa0 = MAKE_CLOCK_NAME(sysctl_resource_ffa0, CLK_SRC_GROUP_AHB, 14), 160 clock_lmm0 = MAKE_CLOCK_NAME(sysctl_resource_lmm0, CLK_SRC_GROUP_CPU0, 0), 161 clock_tsns = MAKE_CLOCK_NAME(sysctl_resource_tsns, CLK_SRC_GROUP_CPU0, 0), 162 163 164 /* For ADC, there are 2-stage clock source and divider configurations */ 165 clock_ana0 = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_COMMON, clock_node_ana0), 166 clock_ana1 = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_COMMON, clock_node_ana1), 167 clock_ana2 = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_COMMON, clock_node_ana2), 168 clock_adc0 = MAKE_CLOCK_NAME(sysctl_resource_adc0, CLK_SRC_GROUP_ADC, 0), 169 clock_adc1 = MAKE_CLOCK_NAME(sysctl_resource_adc1, CLK_SRC_GROUP_ADC, 1), 170 clock_adc2 = MAKE_CLOCK_NAME(sysctl_resource_adc2, CLK_SRC_GROUP_ADC, 2), 171 172 /* For DAC, there are 2-stage clock source and divider configurations */ 173 clock_ana3 = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_COMMON, clock_node_ana3), 174 clock_dac0 = MAKE_CLOCK_NAME(sysctl_resource_dac0, CLK_SRC_GROUP_DAC, 0), 175 176 /* For I2S, there are 2-stage clock source and divider configurations */ 177 clock_aud0 = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_COMMON, clock_node_aud0), 178 clock_aud1 = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_COMMON, clock_node_aud1), 179 clock_i2s0 = MAKE_CLOCK_NAME(sysctl_resource_i2s0, CLK_SRC_GROUP_I2S, 0), 180 clock_i2s1 = MAKE_CLOCK_NAME(sysctl_resource_i2s1, CLK_SRC_GROUP_I2S, 1), 181 182 /* Clock sources */ 183 clk_osc0clk0 = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_SRC, 0), 184 clk_pll0clk0 = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_SRC, 1), 185 clk_pll0clk1 = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_SRC, 2), 186 clk_pll0clk2 = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_SRC, 3), 187 clk_pll1clk0 = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_SRC, 4), 188 clk_pll1clk1 = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_SRC, 5), 189 clk_pll2clk0 = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_SRC, 6), 190 clk_pll2clk1 = MAKE_CLOCK_NAME(RESOURCE_INVALID, CLK_SRC_GROUP_SRC, 7), 191 } clock_name_t; 192 193 #ifdef __cplusplus 194 extern "C" { 195 #endif 196 197 /** 198 * @brief Get specified IP frequency 199 * @param[in] clock_name IP clock name 200 * 201 * @return IP clock frequency in Hz 202 */ 203 uint32_t clock_get_frequency(clock_name_t clock_name); 204 205 206 /** 207 * @brief Get Clock frequency for selected clock source 208 * @param [in] source clock source 209 * @return clock frequency for selected clock source 210 */ 211 uint32_t get_frequency_for_source(clock_source_t source); 212 213 /** 214 * @brief Get the IP clock source 215 * Note: This API return the direct clock source 216 * @param [in] clock_name clock name 217 * @return IP clock source 218 */ 219 clk_src_t clock_get_source(clock_name_t clock_name); 220 221 /** 222 * @brief Set ADC clock source 223 * @param[in] clock_name ADC clock name 224 * @param[in] src ADC clock source 225 * 226 * @return #status_success Setting ADC clock source is successful 227 * #status_clk_invalid Invalid ADC clock 228 * #status_clk_src_invalid Invalid ADC clock source 229 */ 230 hpm_stat_t clock_set_adc_source(clock_name_t clock_name, clk_src_t src); 231 232 /** 233 * @brief Set DAC clock source 234 * @param[in] clock_name DAC clock name 235 * @param[in] src DAC clock source 236 * 237 * @return #status_success Setting DAC clock source is successful 238 * #status_clk_invalid Invalid DAC clock 239 * #status_clk_src_invalid Invalid DAC clock source 240 */ 241 hpm_stat_t clock_set_dac_source(clock_name_t clock_name, clk_src_t src); 242 243 /** 244 * @brief Set I2S clock source 245 * @param[in] clock_name I2S clock name 246 * @param[in] src I2S clock source 247 * 248 * @return #status_success Setting I2S clock source is successful 249 * #status_clk_invalid Invalid I2S clock 250 * #status_clk_src_invalid Invalid I2S clock source 251 */ 252 hpm_stat_t clock_set_i2s_source(clock_name_t clock_name, clk_src_t src); 253 254 /** 255 * @brief Set the IP clock source and divider 256 * @param[in] clock_name clock name 257 * @param[in] src clock source 258 * @param[in] div clock divider, valid range (1 - 256) 259 * 260 * @return #status_success Setting Clock source and divider is successful. 261 * #status_clk_src_invalid clock source is invalid. 262 * #status_clk_fixed clock source and divider is a fixed value 263 * #status_clk_shared_ahb Clock is shared with the AHB clock 264 * #status_clk_shared_axi0 Clock is shared with the AXI0 clock 265 * #status_clk_shared_axi1 CLock is shared with the AXI1 clock 266 * #status_clk_shared_axi2 Clock is shared with the AXI2 clock 267 * #status_clk_shared_cpu0 Clock is shared with the CPU0 clock 268 * #status_clk_shared_cpu1 Clock is shared with the CPU1 clock 269 */ 270 hpm_stat_t clock_set_source_divider(clock_name_t clock_name, clk_src_t src, uint32_t div); 271 272 /** 273 * @brief Enable IP clock 274 * @param[in] clock_name IP clock name 275 */ 276 void clock_enable(clock_name_t clock_name); 277 278 /** 279 * @brief Disable IP clock 280 * @param[in] clock_name IP clock name 281 */ 282 void clock_disable(clock_name_t clock_name); 283 284 /** 285 * @brief Add IP to specified group 286 * @param[in] clock_name IP clock name 287 * @param[in] group resource group index, valid value: 0/1/2/3 288 */ 289 void clock_add_to_group(clock_name_t clock_name, uint32_t group); 290 291 /** 292 * @brief Remove IP from specified group 293 * @param[in] clock_name IP clock name 294 * @param[in] group resource group index, valid value: 0/1/2/3 295 */ 296 void clock_remove_from_group(clock_name_t clock_name, uint32_t group); 297 298 /** 299 * @brief Disconnect the clock group from specified CPU 300 * @param[in] group clock group index, value value is 0/1/2/3 301 * @param[in] cpu CPU index, valid value is 0/1 302 */ 303 void clock_connect_group_to_cpu(uint32_t group, uint32_t cpu); 304 305 /** 306 * @brief Disconnect the clock group from specified CPU 307 * @param[in] group clock group index, value value is 0/1/2/3 308 * @param[in] cpu CPU index, valid value is 0/1 309 */ 310 void clock_disconnect_group_from_cpu(uint32_t group, uint32_t cpu); 311 312 /** 313 * @brief Delay specified microseconds 314 * 315 * @param [in] us expected delay interval in microseconds 316 */ 317 void clock_cpu_delay_us(uint32_t us); 318 319 /** 320 * @brief Delay specified milliseconds 321 * 322 * @param [in] ms expected delay interval in milliseconds 323 */ 324 void clock_cpu_delay_ms(uint32_t ms); 325 326 /** 327 * @brief Update the Core clock frequency 328 */ 329 void clock_update_core_clock(void); 330 331 /** 332 * @brief HPM Core clock variable 333 */ 334 extern uint32_t hpm_core_clock; 335 336 #ifdef __cplusplus 337 } 338 #endif 339 340 #endif /* HPM_CLOCK_DRV_H */ 341