hal_clk.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Copyright (c) 2019-2025 Allwinner Technology Co., Ltd. ALL rights reserved.
  2. *
  3. * Allwinner is a trademark of Allwinner Technology Co.,Ltd., registered in
  4. *the the People's Republic of China and other countries.
  5. * All Allwinner Technology Co.,Ltd. trademarks are used with permission.
  6. *
  7. * DISCLAIMER
  8. * THIRD PARTY LICENCES MAY BE REQUIRED TO IMPLEMENT THE SOLUTION/PRODUCT.
  9. * IF YOU NEED TO INTEGRATE THIRD PARTY’S TECHNOLOGY (SONY, DTS, DOLBY, AVS OR MPEGLA, ETC.)
  10. * IN ALLWINNERS’SDK OR PRODUCTS, YOU SHALL BE SOLELY RESPONSIBLE TO OBTAIN
  11. * ALL APPROPRIATELY REQUIRED THIRD PARTY LICENCES.
  12. * ALLWINNER SHALL HAVE NO WARRANTY, INDEMNITY OR OTHER OBLIGATIONS WITH RESPECT TO MATTERS
  13. * COVERED UNDER ANY REQUIRED THIRD PARTY LICENSE.
  14. * YOU ARE SOLELY RESPONSIBLE FOR YOUR USAGE OF THIRD PARTY’S TECHNOLOGY.
  15. *
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY ALLWINNER"AS IS" AND TO THE MAXIMUM EXTENT
  18. * PERMITTED BY LAW, ALLWINNER EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND,
  19. * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION REGARDING
  20. * THE TITLE, NON-INFRINGEMENT, ACCURACY, CONDITION, COMPLETENESS, PERFORMANCE
  21. * OR MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  22. * IN NO EVENT SHALL ALLWINNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  27. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  29. * OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifndef __SUNXI_HAL_CLK_H__
  32. #define __SUNXI_HAL_CLK_H__
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <sunxi_hal_common.h>
  36. #include <common_ccmu.h>
  37. #include <platform_ccmu.h>
  38. #define hal_clk_api_version "hal_clk_api_version_1_1_0"
  39. /************************************************************************************************
  40. * Macro definition readl 、writel hal_read_xxx and hal_write_xxx
  41. * @Description: These definitions used to CCMU Drivers to read and write Physical I/O register
  42. *************************************************************************************************/
  43. #define hal_write_reg8(addr ,data) ((*(volatile u8 *)(addr)) = (u8)(data))
  44. #define hal_write_reg16(addr ,data) ((*(volatile u16 *)(addr)) = (u16)(data))
  45. #define hal_write_reg32(addr ,data) ((*(volatile u32 *)(addr)) = (u32)(data))
  46. #define hal_read_reg8(x) (*(volatile u8 *)(x))
  47. #define hal_read_reg16(x) (*(volatile u16 *)(x))
  48. #define hal_read_reg32(x) (*(volatile u32 *)(x))
  49. #undef readl
  50. #undef writel
  51. #define readl(x) hal_read_reg32(x)
  52. #define writel(val, reg) hal_write_reg32(reg, val)
  53. /************************************************************************************************
  54. * Macro definition CCMU_XXX
  55. * @Description: These definitions used to CCMU HAL-API and Drivers source code debug
  56. *************************************************************************************************/
  57. //#define CCMU_DBG_EN 1
  58. #if defined(CCMU_DBG_LEAVE_TINY) || defined(CCMU_DBG_LEAVE_HIGH)
  59. #define CCMU_DBG(fmt,args...) printf("[CCMU:dbg..] %-*s:%d "fmt ,30, __func__, __LINE__, ##args)
  60. #define CCMU_ERR(fmt,args...) printf("[CCMU:err**] %-*s:%d "fmt ,30, __func__, __LINE__, ##args)
  61. #else
  62. #define CCMU_DBG(fmt,args...) do{} while(0)
  63. #define CCMU_ERR(fmt,args...) do{} while(0)
  64. #endif
  65. #if defined(CCMU_DBG_LEAVE_HIGH)
  66. #define CCMU_TRACE() printf("[CCMU:trace] %-*s:%d \n",30, __func__, __LINE__)
  67. #define CCMU_TRACE_CLK(tpye, clk) printf("CCMU:trace %s:%d CLK "#tpye" id %d\n",__func__, __LINE__, clk)
  68. #else
  69. #define CCMU_TRACE() do{} while(0)
  70. #define CCMU_TRACE_CLK(clk, rate) do{} while(0)
  71. #endif
  72. /************************************************************************************************
  73. * @Function: hal_clock_init
  74. * @Description: provide HAL API for initialize soc clocks during the system power-on startup phase
  75. * @Parameters:
  76. * # void: No parameters required
  77. * @Return values:
  78. * # HAL_CLK_STATUS_OK: soc clocks initialize successed
  79. * # others : soc clocks initialization may have some abnormal problems
  80. * @Attention: clock initialize timing depands on specific soc platform clock design
  81. *************************************************************************************************/
  82. void hal_clock_init(void);
  83. /************************************************************************************************
  84. * @Function: hal_clock_init
  85. * @Description: provide HAL API for initialize soc clocks during the system power-on startup phase
  86. * @Parameters:
  87. * # void: No parameters required
  88. * @Return values:
  89. * # HAL_CLK_STATUS_OK: soc clocks initialize successed
  90. * # others : soc clocks initialization may have some abnormal problems
  91. * @Attention: clock initialize timing depands on specific soc platform clock design
  92. *************************************************************************************************/
  93. hal_clk_t hal_clock_get(hal_clk_type_t type, hal_clk_id_t id);
  94. hal_clk_status_t hal_clock_put(hal_clk_t clk);
  95. /************************************************************************************************
  96. * @Function: hal_clock_is_enabled
  97. * @Description: provide HAL API for bus-clk and periph-clk to get clock enabled statue
  98. * @Parameters:
  99. * # clk: clock-id of soc specific clock
  100. * @Return values:
  101. * # HAL_CLK_STATUS_INVALID_PARAMETER: input parameter of clock-id undefined in hal ot rate value is invalid
  102. * # HAL_CLK_STATUS_ERROR_CLK_SET_RATE_REFUSED: fixed-clk and factor clk not allowed User to change rate because of stability
  103. * # HAL_CLK_STATUS_ERROT_CLK_UNDEFINED: input parameter of clock-id defined in hal but not defined by soc clock driver
  104. * # HAL_CLK_STATUS_ERROR_CLK_NOT_FOUND: input parameter of clock-id defined in hal but not defined by soc clock driver
  105. * # HAL_CLK_STATUS_ENABLED: clock current status is enabled
  106. * # HAL_CLK_STATUS_DISABLED: clock current status is disabled
  107. * @Attention: .etc
  108. *************************************************************************************************/
  109. hal_clk_status_t hal_clock_is_enabled(hal_clk_t clk);
  110. /************************************************************************************************
  111. * @Function: hal_clock_enable
  112. * @Description: provide HAL API for bus-clk and periph-clk to enable clock
  113. * @Parameters:
  114. * # clk: clock-id of soc specific clock
  115. * @Return values:
  116. * # HAL_CLK_STATUS_INVALID_PARAMETER: input parameter of clock-id undefined in hal ot rate value is invalid
  117. * # HAL_CLK_STATUS_ERROR_CLK_SET_RATE_REFUSED: fixed-clk and factor clk not allowed User to change rate because of stability
  118. * # HAL_CLK_STATUS_ERROT_CLK_UNDEFINED: input parameter of clock-id defined in hal but not defined by soc clock driver
  119. * # HAL_CLK_STATUS_ERROR_CLK_NOT_FOUND: input parameter of clock-id defined in hal but not defined by soc clock driver
  120. * # HAL_CLK_STATUS_ENABLED: clock current status is enabled
  121. * # HAL_CLK_STATUS_DISABLED: clock current status is disabled
  122. * @Attention: .etc
  123. *************************************************************************************************/
  124. hal_clk_status_t hal_clock_enable(hal_clk_t clk);
  125. /************************************************************************************************
  126. * @Function: hal_clock_disable
  127. * @Description: provide HAL API for bus-clk and periph-clk to disable clock
  128. * @Parameters:
  129. * # clk: clock-id of soc specific clock
  130. * @Return values:
  131. * # HAL_CLK_STATUS_INVALID_PARAMETER: input parameter of clock-id undefined in hal ot rate value is invalid
  132. * # HAL_CLK_STATUS_ERROR_CLK_SET_RATE_REFUSED: fixed-clk and factor clk not allowed User to change rate because of stability
  133. * # HAL_CLK_STATUS_ERROT_CLK_UNDEFINED: input parameter of clock-id defined in hal but not defined by soc clock driver
  134. * # HAL_CLK_STATUS_ERROR_CLK_NOT_FOUND: input parameter of clock-id defined in hal but not defined by soc clock driver
  135. * # HAL_CLK_STATUS_OK: clock current status disabled successed
  136. * @Attention: .etc
  137. *************************************************************************************************/
  138. hal_clk_status_t hal_clock_disable(hal_clk_t clk);
  139. /************************************************************************************************
  140. * @Function: hal_clk_recalc_rate
  141. * @Description: provide HAL API for factor-clk, bus-clk and periph-clk to recalculate current Runtime rate
  142. * @Parameters:
  143. * # clk: clock-id of soc specific clock
  144. * @Return values:
  145. * # HAL_CLK_STATUS_INVALID_PARAMETER: input parameter of clock-id undefined in hal
  146. * # HAL_CLK_RATE_UNINITIALIZED : input parameter of clock-id defined in hal but not defined by soc clock driver or clock disbaled
  147. * # others: return current clock rate successed
  148. * @Attention: .etc
  149. *************************************************************************************************/
  150. u32 hal_clk_recalc_rate(hal_clk_t clk);
  151. /************************************************************************************************
  152. * @Function: hal_clk_round_rate
  153. * @Description: provide HAL API for factor-clk, bus-clk and periph-clk round target rate to the most suitable rate
  154. * @Parameters:
  155. * # clk: clock-id of soc specific clock
  156. * # rate: the target rate form API-User
  157. * @Return values:
  158. * # HAL_CLK_STATUS_INVALID_PARAMETER: input parameter of clock-id undefined in hal ot rate value is invalid
  159. * # HAL_CLK_RATE_UNINITIALIZED : input parameter of clock-id defined in hal but not defined by soc clock driver or clock disbaled
  160. * # others: return round rate successed
  161. * @Attention: .etc
  162. *************************************************************************************************/
  163. u32 hal_clk_round_rate(hal_clk_t clk, u32 rate);
  164. /************************************************************************************************
  165. * @Function: hal_clk_get_rate
  166. * @Description: provide HAL API for factor-clk, bus-clk and periph-clk get current rate cached witch may not current Runtime rate
  167. * @Parameters:
  168. * # clk: clock-id of soc specific clock
  169. * @Return values:
  170. * # HAL_CLK_STATUS_INVALID_PARAMETER: input parameter of clock-id undefined in hal ot rate value is invalid
  171. * # HAL_CLK_RATE_UNINITIALIZED : input parameter of clock-id defined in hal but not defined by soc clock driver or clock disbaled
  172. * # others: return rate cached successed
  173. * @Attention: .etc
  174. *************************************************************************************************/
  175. u32 hal_clk_get_rate(hal_clk_t clk);
  176. /************************************************************************************************
  177. * @Function: hal_clk_set_rate
  178. * @Description: provide HAL API for bus-clk and periph-clk to set new rate
  179. * @Parameters:
  180. * # clk: clock-id of soc specific clock
  181. * # rate: the new rate value
  182. * @Return values:
  183. * # HAL_CLK_STATUS_INVALID_PARAMETER: input parameter of clock-id undefined in hal ot rate value is invalid
  184. * # HAL_CLK_STATUS_ERROR_CLK_SET_RATE_REFUSED: fixed-clk and factor clk not allowed User to change rate because of stability
  185. * # HAL_CLK_STATUS_ERROT_CLK_UNDEFINED: input parameter of clock-id defined in hal but not defined by soc clock driver
  186. * # HAL_CLK_STATUS_ERROR_CLK_NOT_FOUND: input parameter of clock-id defined in hal but not defined by soc clock driver
  187. * # HAL_CLK_STATUS_OK: set new rate successed
  188. * @Attention: .etc
  189. *************************************************************************************************/
  190. hal_clk_status_t hal_clk_set_rate(hal_clk_t clk, u32 rate);
  191. /************************************************************************************************
  192. * @Function: hal_clk_set_parent
  193. * @Description: provide HAL API for factor-clk, bus-clk and periph-clk to select parent clock
  194. * @Parameters:
  195. * # clk: clock-id of soc specific clock witch nedds to adjust parent clock
  196. * # parent: clock-id of soc specific clock's parent clock
  197. * @Return values:
  198. * # HAL_CLK_STATUS_OK: soc specific clock select and siwtch parent clock successed
  199. * # others : soc specific clock select and siwtch parent clock may have some abnormal problems
  200. * @Attention: soc specific clock and parent clock must be according to the SOC_User_Manual definition
  201. *************************************************************************************************/
  202. hal_clk_status_t hal_clk_set_parent(hal_clk_t clk, hal_clk_t parent);
  203. /************************************************************************************************
  204. * @Function: hal_clk_get_parent
  205. * @Description: provide HAL API for factor-clk, bus-clk and periph-clk to get current parent clock
  206. * @Parameters:
  207. * # clk: clock-id of soc specific clock
  208. * @Return values:
  209. * # HAL_CLK_STATUS_INVALID_PARAMETER: input parameter of clock-id undefined in hal
  210. * # HAL_CLK_UNINITIALIZED : input parameter of clock-id defined in hal but not defined by soc clock driver
  211. * # others: return current parent clock-id successed
  212. * @Attention: soc specific clock and parent clock must be according to the SOC_User_Manual definition
  213. *************************************************************************************************/
  214. hal_clk_t hal_clk_get_parent(hal_clk_t clk);
  215. #endif /* __HAL_CLOCK_H__ */