clk.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 __CLK_H__
  32. #define __CLK_H__
  33. #include "common_ccmu.h"
  34. //#include "platform_clk.h"
  35. #include <hal_clk.h>
  36. #define clk_driver_version "v_1_0_2"
  37. typedef struct clk_base
  38. {
  39. hal_clk_id_t clk;
  40. hal_clk_id_t parent;
  41. u32 clk_rate;
  42. } clk_base_t, *clk_base_pt;
  43. typedef struct clk_core
  44. {
  45. hal_clk_id_t clk;
  46. hal_clk_type_t clk_type;
  47. hal_clk_id_t current_parent;
  48. hal_clk_type_t current_parent_type;
  49. u32 clk_rate;
  50. u32 parent_rate;
  51. hal_clk_status_t clk_enbale;
  52. } clk_core_t, *clk_core_pt;
  53. typedef struct clk_fix_factor
  54. {
  55. clk_core_t clk_core;
  56. u32 clk_mult;
  57. u32 clk_div;
  58. } clk_fixed_factor_t, *clk_fixed_factor_pt ;
  59. typedef struct clk_factor
  60. {
  61. clk_core_t clk_core;
  62. struct factor_init_data *factor_data ;
  63. } clk_factor_t, *clk_factor_pt ;
  64. typedef struct clk_periph
  65. {
  66. clk_core_t clk_core;
  67. hal_clk_id_t *parent_arry;
  68. u32 parent_arry_size;
  69. struct sunxi_clk_periph *config;
  70. } clk_periph_t, *clk_periph_pt ;
  71. #define SETMASK(width, shift) ((width?((-1U) >> (32-width)):0) << (shift))
  72. #define CLRMASK(width, shift) (~(SETMASK(width, shift)))
  73. #define SET_BITS(shift, width, reg, val) \
  74. (((reg) & CLRMASK(width, shift)) | (val << (shift)))
  75. #define GET_BITS(shift, width, reg) \
  76. (((reg) & SETMASK(width, shift)) >> (shift))
  77. #define CLK_LOCKBIT(x) x
  78. #define do_div(n,base) ({ \
  79. u32 __base = (base); \
  80. u32 __rem; \
  81. __rem = ((u64)(n)) % __base; \
  82. (n) = ((u64)(n)) / __base; \
  83. if (__rem > __base / 2) \
  84. ++(n); \
  85. __rem; \
  86. })
  87. /************************************************************************************************
  88. * Macro definition SUNXI_CLK_FIXED_SRC
  89. * @Description: This definition used to defining a Soc fixed-src-clk type clock structure variable and statically initialized
  90. *************************************************************************************************/
  91. #define SUNXI_CLK_FIXED_SRC(_name, _clk, _current_parent, _current_parent_type, _clk_rate, _parent_rate) \
  92. clk_core_t sunxi_clk_fixed_src_##_name = { \
  93. .clk = _clk, \
  94. .clk_type = HAL_CLK_FIXED_SRC, \
  95. .current_parent = _current_parent, \
  96. .current_parent_type = _current_parent_type, \
  97. .clk_rate = _clk_rate, \
  98. .parent_rate = _parent_rate, \
  99. .clk_enbale = HAL_CLK_STATUS_ENABLED, \
  100. }
  101. /************************************************************************************************
  102. * Macro definition SUNXI_CLK_FIXED_FACTOR
  103. * @Description: This definition used to defining a Soc fixed-factor-clk type clock structure variable and statically initialized
  104. *************************************************************************************************/
  105. #define SUNXI_CLK_FIXED_FACTOR(_name, _clk, _current_parent, _current_parent_type, _mult, _div) \
  106. clk_fixed_factor_t sunxi_clk_fixed_factor_##_name = { \
  107. .clk_core = { \
  108. .clk = _clk, \
  109. .clk_type = HAL_CLK_FIXED_FACTOR, \
  110. .current_parent = _current_parent, \
  111. .current_parent_type = _current_parent_type, \
  112. .clk_rate = 0, \
  113. .parent_rate = 0, \
  114. .clk_enbale = HAL_CLK_STATUS_DISABLED, \
  115. }, \
  116. .clk_mult = _mult, \
  117. .clk_div = _div, \
  118. }
  119. #define SUNXI_CLK_FACTOR_PERI1_MAX_FREQ (636000000U)
  120. /************************************************************************************************
  121. * @Function: clk_udelay
  122. * @Description: implement for seting delay time
  123. * @Parameters:
  124. * # us: delay time of us unit
  125. * @Return values:
  126. * # void: No parameters returned
  127. * @Attention: .etc
  128. *************************************************************************************************/
  129. void clk_udelay(u32 us);
  130. /************************************************************************************************
  131. * @Function: clk_get_core
  132. * @Description: implement to find clock structure variable pointer of clock-id
  133. * @Parameters:
  134. * # clk: clock-id of soc specific clock
  135. * @Return values:
  136. * # NULL: input parameter of clock-id defined in hal but not defined by soc clock driver
  137. * # others : clk_core_pt structure variable pointer of clock-id
  138. * @Attention: .etc
  139. *************************************************************************************************/
  140. clk_core_pt clk_get_core(hal_clk_id_t clk);
  141. /************************************************************************************************
  142. * @Function: clk_init
  143. * @Description: implement for initialize soc clocks during the system power-on startup phase
  144. * @Parameters:
  145. * # void: No parameters required
  146. * @Return values:
  147. * # HAL_CLK_STATUS_OK: soc clocks initialize successed
  148. * # others : soc clocks initialization may have some abnormal problems
  149. * @Attention: clock initialize timing depands on specific soc platform clock design
  150. *************************************************************************************************/
  151. hal_clk_status_t clk_init(void);
  152. hal_clk_id_t clk_get(hal_clk_type_t type, hal_clk_id_t id);
  153. hal_clk_status_t clk_put(hal_clk_id_t id);
  154. /************************************************************************************************
  155. * @Function: clk_get_rate
  156. * @Description: implement for factor-clk, bus-clk and periph-clk get current rate cached witch may not current Runtime rate
  157. * @Parameters:
  158. * # clk: clock-id of soc specific clock
  159. * @Return values:
  160. * # HAL_CLK_STATUS_INVALID_PARAMETER: input parameter of clock-id undefined in hal ot rate value is invalid
  161. * # 0 : input parameter of clock-id defined in hal but not defined by soc clock driver or clock disbaled
  162. * # others: return rate cached successed
  163. * @Attention: .etc
  164. *************************************************************************************************/
  165. hal_clk_status_t clk_get_rate(hal_clk_id_t clk, u32 *rate);
  166. /************************************************************************************************
  167. * @Function: clk_set_rate
  168. * @Description: implement for bus-clk and periph-clk to set new rate
  169. * @Parameters:
  170. * # clk: clock-id of soc specific clock
  171. * # rate: the new rate value
  172. * @Return values:
  173. * # HAL_CLK_STATUS_INVALID_PARAMETER: input parameter of clock-id undefined in hal ot rate value is invalid
  174. * # HAL_CLK_STATUS_ERROR_CLK_SET_RATE_REFUSED: fixed-clk and factor clk not allowed User to change rate because of stability
  175. * # HAL_CLK_STATUS_ERROT_CLK_UNDEFINED: input parameter of clock-id defined in hal but not defined by soc clock driver
  176. * # HAL_CLK_STATUS_ERROR_CLK_NOT_FOUND: input parameter of clock-id defined in hal but not defined by soc clock driver
  177. * # HAL_CLK_STATUS_OK: set new rate successed
  178. * @Attention: .etc
  179. *************************************************************************************************/
  180. hal_clk_status_t clk_set_rate(hal_clk_id_t clk, u32 rate);
  181. /************************************************************************************************
  182. * @Function: clk_recalc_rate
  183. * @Description: implement for factor-clk, bus-clk and periph-clk to recalculate current Runtime rate
  184. * @Parameters:
  185. * # clk: clock-id of soc specific clock
  186. * @Return values:
  187. * # HAL_CLK_STATUS_INVALID_PARAMETER: input parameter of clock-id undefined in hal
  188. * # 0 : input parameter of clock-id defined in hal but not defined by soc clock driver or clock disbaled
  189. * # others: return current clock rate successed
  190. * @Attention: .etc
  191. *************************************************************************************************/
  192. hal_clk_status_t clk_recalc_rate(hal_clk_id_t clk, u32 *prate);
  193. /************************************************************************************************
  194. * @Function: clk_round_rate
  195. * @Description: implement for for factor-clk, bus-clk and periph-clk round target rate to the most suitable rate
  196. * @Parameters:
  197. * # clk: clock-id of soc specific clock
  198. * # rate: the target rate form API-User
  199. * @Return values:
  200. * # HAL_CLK_STATUS_INVALID_PARAMETER: input parameter of clock-id undefined in hal ot rate value is invalid
  201. * # 0 : input parameter of clock-id defined in hal but not defined by soc clock driver or clock disbaled
  202. * # others: return round rate successed
  203. * @Attention: .etc
  204. *************************************************************************************************/
  205. hal_clk_status_t clk_round_rate(hal_clk_id_t clk, u32 rate, u32 *prate);
  206. /************************************************************************************************
  207. * @Function: clk_is_enabled
  208. * @Description: implement for bus-clk and periph-clk to get clock enabled statue
  209. * @Parameters:
  210. * # clk: clock-id of soc specific clock
  211. * @Return values:
  212. * # HAL_CLK_STATUS_INVALID_PARAMETER: input parameter of clock-id undefined in hal ot rate value is invalid
  213. * # HAL_CLK_STATUS_ERROR_CLK_SET_RATE_REFUSED: fixed-clk and factor clk not allowed User to change rate because of stability
  214. * # HAL_CLK_STATUS_ERROT_CLK_UNDEFINED: input parameter of clock-id defined in hal but not defined by soc clock driver
  215. * # HAL_CLK_STATUS_ERROR_CLK_NOT_FOUND: input parameter of clock-id defined in hal but not defined by soc clock driver
  216. * # HAL_CLK_STATUS_ENABLED: clock current status is enabled
  217. * # HAL_CLK_STATUS_DISABLED: clock current status is disabled
  218. * @Attention: .etc
  219. *************************************************************************************************/
  220. hal_clk_status_t clk_is_enabled(hal_clk_id_t clk);
  221. /************************************************************************************************
  222. * @Function: clk_prepare_enable
  223. * @Description: implement for bus-clk and periph-clk to enable clock
  224. * @Parameters:
  225. * # clk: clock-id of soc specific clock
  226. * @Return values:
  227. * # HAL_CLK_STATUS_INVALID_PARAMETER: input parameter of clock-id undefined in hal ot rate value is invalid
  228. * # HAL_CLK_STATUS_ERROR_CLK_SET_RATE_REFUSED: fixed-clk and factor clk not allowed User to change rate because of stability
  229. * # HAL_CLK_STATUS_ERROT_CLK_UNDEFINED: input parameter of clock-id defined in hal but not defined by soc clock driver
  230. * # HAL_CLK_STATUS_ERROR_CLK_NOT_FOUND: input parameter of clock-id defined in hal but not defined by soc clock driver
  231. * # HAL_CLK_STATUS_ENABLED: clock current status is enabled
  232. * # HAL_CLK_STATUS_DISABLED: clock current status is disabled
  233. * @Attention: .etc
  234. *************************************************************************************************/
  235. hal_clk_status_t clk_prepare_enable(hal_clk_id_t clk);
  236. /************************************************************************************************
  237. * @Function: clk_disable_unprepare
  238. * @Description: implement for bus-clk and periph-clk to disable clock
  239. * @Parameters:
  240. * # clk: clock-id of soc specific clock
  241. * @Return values:
  242. * # HAL_CLK_STATUS_INVALID_PARAMETER: input parameter of clock-id undefined in hal ot rate value is invalid
  243. * # HAL_CLK_STATUS_ERROR_CLK_SET_RATE_REFUSED: fixed-clk and factor clk not allowed User to change rate because of stability
  244. * # HAL_CLK_STATUS_ERROT_CLK_UNDEFINED: input parameter of clock-id defined in hal but not defined by soc clock driver
  245. * # HAL_CLK_STATUS_ERROR_CLK_NOT_FOUND: input parameter of clock-id defined in hal but not defined by soc clock driver
  246. * # HAL_CLK_STATUS_OK: clock current status disabled successed
  247. * @Attention: .etc
  248. *************************************************************************************************/
  249. hal_clk_status_t clk_disable_unprepare(hal_clk_id_t clk);
  250. /************************************************************************************************
  251. * @Function: clk_get_parent
  252. * @Description: implement for factor-clk, bus-clk and periph-clk to select parent clock
  253. * @Parameters:
  254. * # clk: clock-id of soc specific clock witch nedds to adjust parent clock
  255. * # parent: clock-id of soc specific clock's parent clock
  256. * @Return values:
  257. * # HAL_CLK_STATUS_OK: soc specific clock select and siwtch parent clock successed
  258. * # others : soc specific clock select and siwtch parent clock may have some abnormal problems
  259. * @Attention: soc specific clock and parent clock must be according to the SOC_User_Manual definition
  260. *************************************************************************************************/
  261. hal_clk_id_t clk_get_parent(hal_clk_id_t clk);
  262. /************************************************************************************************
  263. * @Function: clk_set_parent
  264. * @Description: implement for factor-clk, bus-clk and periph-clk to select parent clock
  265. * @Parameters:
  266. * # clk: clock-id of soc specific clock witch nedds to adjust parent clock
  267. * # parent: clock-id of soc specific clock's parent clock
  268. * @Return values:
  269. * # HAL_CLK_STATUS_OK: soc specific clock select and siwtch parent clock successed
  270. * # others : soc specific clock select and siwtch parent clock may have some abnormal problems
  271. * @Attention: soc specific clock and parent clock must be according to the SOC_User_Manual definition
  272. *************************************************************************************************/
  273. hal_clk_status_t clk_set_parent(hal_clk_id_t clk, hal_clk_id_t parent_clk);
  274. #endif /* __HAL_CLOCK_H__ */