clk-stm32-core.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (C) 2022-2024, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
  5. */
  6. #ifndef CLK_STM32_CORE_H
  7. #define CLK_STM32_CORE_H
  8. struct mux_cfg {
  9. uint16_t offset;
  10. uint8_t shift;
  11. uint8_t width;
  12. uint8_t bitrdy;
  13. };
  14. struct gate_cfg {
  15. uint16_t offset;
  16. uint8_t bit_idx;
  17. uint8_t set_clr;
  18. };
  19. struct clk_div_table {
  20. uint16_t val;
  21. uint16_t div;
  22. };
  23. struct div_cfg {
  24. const struct clk_div_table *table;
  25. uint16_t offset;
  26. uint8_t shift;
  27. uint8_t width;
  28. uint8_t flags;
  29. uint8_t bitrdy;
  30. };
  31. struct parent_cfg {
  32. const uint16_t *id_parents;
  33. struct mux_cfg *mux;
  34. uint8_t num_parents;
  35. };
  36. struct stm32_clk_priv;
  37. struct stm32_clk_ops {
  38. unsigned long (*recalc_rate)(struct stm32_clk_priv *priv, int id, unsigned long rate);
  39. int (*get_parent)(struct stm32_clk_priv *priv, int id);
  40. int (*set_rate)(struct stm32_clk_priv *priv, int id, unsigned long rate,
  41. unsigned long prate);
  42. int (*enable)(struct stm32_clk_priv *priv, int id);
  43. void (*disable)(struct stm32_clk_priv *priv, int id);
  44. bool (*is_enabled)(struct stm32_clk_priv *priv, int id);
  45. void (*init)(struct stm32_clk_priv *priv, int id);
  46. };
  47. struct clk_stm32 {
  48. uint16_t binding;
  49. uint16_t parent;
  50. uint8_t ops;
  51. uint8_t flags;
  52. void *clock_cfg;
  53. };
  54. struct stm32_clk_priv {
  55. uintptr_t base;
  56. const uint32_t num;
  57. const struct clk_stm32 *clks;
  58. const struct parent_cfg *parents;
  59. const uint32_t nb_parents;
  60. const struct gate_cfg *gates;
  61. const uint32_t nb_gates;
  62. const struct div_cfg *div;
  63. const uint32_t nb_div;
  64. struct clk_oscillator_data *osci_data;
  65. const uint32_t nb_osci_data;
  66. uint8_t *gate_refcounts;
  67. void *pdata;
  68. const struct stm32_clk_ops **ops_array;
  69. };
  70. struct stm32_clk_bypass {
  71. uint16_t offset;
  72. uint8_t bit_byp;
  73. uint8_t bit_digbyp;
  74. };
  75. struct stm32_clk_css {
  76. uint16_t offset;
  77. uint8_t bit_css;
  78. };
  79. struct stm32_clk_drive {
  80. uint16_t offset;
  81. uint8_t drv_shift;
  82. uint8_t drv_width;
  83. uint8_t drv_default;
  84. };
  85. struct clk_oscillator_data {
  86. const char *name;
  87. struct stm32_clk_bypass *bypass;
  88. struct stm32_clk_css *css;
  89. struct stm32_clk_drive *drive;
  90. unsigned long frequency;
  91. uint16_t id_clk;
  92. uint16_t gate_id;
  93. uint16_t gate_rdy_id;
  94. };
  95. struct clk_gate_cfg {
  96. uint32_t offset;
  97. uint8_t bit_idx;
  98. };
  99. /* CLOCK FLAGS */
  100. #define CLK_IS_CRITICAL BIT(0)
  101. #define CLK_IGNORE_UNUSED BIT(1)
  102. #define CLK_SET_RATE_PARENT BIT(2)
  103. #define CLK_DIVIDER_ONE_BASED BIT(0)
  104. #define CLK_DIVIDER_POWER_OF_TWO BIT(1)
  105. #define CLK_DIVIDER_ALLOW_ZERO BIT(2)
  106. #define CLK_DIVIDER_HIWORD_MASK BIT(3)
  107. #define CLK_DIVIDER_ROUND_CLOSEST BIT(4)
  108. #define CLK_DIVIDER_READ_ONLY BIT(5)
  109. #define CLK_DIVIDER_MAX_AT_ZERO BIT(6)
  110. #define CLK_DIVIDER_BIG_ENDIAN BIT(7)
  111. #define MUX_MAX_PARENTS U(0x8000)
  112. #define MUX_PARENT_MASK GENMASK(14, 0)
  113. #define MUX_FLAG U(0x8000)
  114. #define MUX(mux) ((mux) | MUX_FLAG)
  115. #define NO_GATE 0
  116. #define _NO_ID UINT16_MAX
  117. #define CLK_IS_ROOT UINT16_MAX
  118. #define MUX_NO_BIT_RDY UINT8_MAX
  119. #define DIV_NO_BIT_RDY UINT8_MAX
  120. #define MASK_WIDTH_SHIFT(_width, _shift) \
  121. GENMASK(((_width) + (_shift) - 1U), (_shift))
  122. void clk_stm32_rcc_regs_lock(void);
  123. void clk_stm32_rcc_regs_unlock(void);
  124. int clk_stm32_init(struct stm32_clk_priv *priv, uintptr_t base);
  125. void clk_stm32_enable_critical_clocks(void);
  126. struct stm32_clk_priv *clk_stm32_get_priv(void);
  127. int clk_get_index(struct stm32_clk_priv *priv, unsigned long binding_id);
  128. const struct clk_stm32 *_clk_get(struct stm32_clk_priv *priv, int id);
  129. void clk_oscillator_set_bypass(struct stm32_clk_priv *priv, int id, bool digbyp, bool bypass);
  130. void clk_oscillator_set_drive(struct stm32_clk_priv *priv, int id, uint8_t lsedrv);
  131. void clk_oscillator_set_css(struct stm32_clk_priv *priv, int id, bool css);
  132. int _clk_stm32_gate_wait_ready(struct stm32_clk_priv *priv, uint16_t gate_id, bool ready_on);
  133. int clk_oscillator_wait_ready(struct stm32_clk_priv *priv, int id, bool ready_on);
  134. int clk_oscillator_wait_ready_on(struct stm32_clk_priv *priv, int id);
  135. int clk_oscillator_wait_ready_off(struct stm32_clk_priv *priv, int id);
  136. int clk_stm32_get_counter(unsigned long binding_id);
  137. void _clk_stm32_gate_disable(struct stm32_clk_priv *priv, uint16_t gate_id);
  138. int _clk_stm32_gate_enable(struct stm32_clk_priv *priv, uint16_t gate_id);
  139. int _clk_stm32_set_parent(struct stm32_clk_priv *priv, int id, int src_id);
  140. int _clk_stm32_set_parent_by_index(struct stm32_clk_priv *priv, int clk, int sel);
  141. int _clk_stm32_get_parent(struct stm32_clk_priv *priv, int id);
  142. int _clk_stm32_get_parent_by_index(struct stm32_clk_priv *priv, int clk_id, int idx);
  143. int _clk_stm32_get_parent_index(struct stm32_clk_priv *priv, int clk_id);
  144. unsigned long _clk_stm32_get_rate(struct stm32_clk_priv *priv, int id);
  145. unsigned long _clk_stm32_get_parent_rate(struct stm32_clk_priv *priv, int id);
  146. bool _stm32_clk_is_flags(struct stm32_clk_priv *priv, int id, uint8_t flag);
  147. int _clk_stm32_enable(struct stm32_clk_priv *priv, int id);
  148. void _clk_stm32_disable(struct stm32_clk_priv *priv, int id);
  149. int clk_stm32_enable_call_ops(struct stm32_clk_priv *priv, uint16_t id);
  150. void clk_stm32_disable_call_ops(struct stm32_clk_priv *priv, uint16_t id);
  151. bool _clk_stm32_is_enabled(struct stm32_clk_priv *priv, int id);
  152. int _clk_stm32_divider_set_rate(struct stm32_clk_priv *priv, int div_id,
  153. unsigned long rate, unsigned long parent_rate);
  154. int clk_stm32_divider_set_rate(struct stm32_clk_priv *priv, int id, unsigned long rate,
  155. unsigned long prate);
  156. unsigned long _clk_stm32_divider_recalc(struct stm32_clk_priv *priv,
  157. int div_id,
  158. unsigned long prate);
  159. unsigned long clk_stm32_divider_recalc(struct stm32_clk_priv *priv, int idx,
  160. unsigned long prate);
  161. int clk_stm32_gate_enable(struct stm32_clk_priv *priv, int idx);
  162. void clk_stm32_gate_disable(struct stm32_clk_priv *priv, int idx);
  163. bool _clk_stm32_gate_is_enabled(struct stm32_clk_priv *priv, int gate_id);
  164. bool clk_stm32_gate_is_enabled(struct stm32_clk_priv *priv, int idx);
  165. uint32_t clk_stm32_div_get_value(struct stm32_clk_priv *priv, int div_id);
  166. int clk_stm32_set_div(struct stm32_clk_priv *priv, uint32_t div_id, uint32_t value);
  167. int clk_mux_set_parent(struct stm32_clk_priv *priv, uint16_t pid, uint8_t sel);
  168. int clk_mux_get_parent(struct stm32_clk_priv *priv, uint32_t mux_id);
  169. int stm32_clk_parse_fdt_by_name(void *fdt, int node, const char *name, uint32_t *tab, uint32_t *nb);
  170. #ifdef CFG_STM32_CLK_DEBUG
  171. void clk_stm32_display_clock_info(void);
  172. #endif
  173. struct clk_stm32_div_cfg {
  174. uint8_t id;
  175. };
  176. #define STM32_DIV(idx, _binding, _parent, _flags, _div_id) \
  177. [(idx)] = (struct clk_stm32){ \
  178. .binding = (_binding),\
  179. .parent = (_parent),\
  180. .flags = (_flags),\
  181. .clock_cfg = &(struct clk_stm32_div_cfg){\
  182. .id = (_div_id),\
  183. },\
  184. .ops = STM32_DIVIDER_OPS,\
  185. }
  186. struct clk_stm32_gate_cfg {
  187. uint8_t id;
  188. };
  189. #define STM32_GATE(idx, _binding, _parent, _flags, _gate_id) \
  190. [(idx)] = (struct clk_stm32){ \
  191. .binding = (_binding),\
  192. .parent = (_parent),\
  193. .flags = (_flags),\
  194. .clock_cfg = &(struct clk_stm32_gate_cfg){\
  195. .id = (_gate_id),\
  196. },\
  197. .ops = STM32_GATE_OPS,\
  198. }
  199. struct fixed_factor_cfg {
  200. uint8_t mult;
  201. uint8_t div;
  202. };
  203. unsigned long fixed_factor_recalc_rate(struct stm32_clk_priv *priv,
  204. int _idx, unsigned long prate);
  205. #define FIXED_FACTOR(idx, _idx, _parent, _mult, _div) \
  206. [(idx)] = (struct clk_stm32){ \
  207. .binding = (_idx),\
  208. .parent = (_parent),\
  209. .clock_cfg = &(struct fixed_factor_cfg){\
  210. .mult = (_mult),\
  211. .div = (_div),\
  212. },\
  213. .ops = FIXED_FACTOR_OPS,\
  214. }
  215. #define GATE(idx, _binding, _parent, _flags, _offset, _bit_idx) \
  216. [(idx)] = (struct clk_stm32){ \
  217. .binding = (_binding),\
  218. .parent = (_parent),\
  219. .flags = (_flags),\
  220. .clock_cfg = &(struct clk_gate_cfg){\
  221. .offset = (_offset),\
  222. .bit_idx = (_bit_idx),\
  223. },\
  224. .ops = GATE_OPS,\
  225. }
  226. #define STM32_MUX(idx, _binding, _mux_id, _flags) \
  227. [(idx)] = (struct clk_stm32){ \
  228. .binding = (_binding),\
  229. .parent = (MUX(_mux_id)),\
  230. .flags = (_flags),\
  231. .clock_cfg = NULL,\
  232. .ops = STM32_MUX_OPS\
  233. }
  234. struct clk_timer_cfg {
  235. uint32_t apbdiv;
  236. uint32_t timpre;
  237. };
  238. #define CK_TIMER(idx, _idx, _parent, _flags, _apbdiv, _timpre) \
  239. [(idx)] = (struct clk_stm32){ \
  240. .binding = (_idx),\
  241. .parent = (_parent),\
  242. .flags = (CLK_SET_RATE_PARENT | (_flags)),\
  243. .clock_cfg = &(struct clk_timer_cfg){\
  244. .apbdiv = (_apbdiv),\
  245. .timpre = (_timpre),\
  246. },\
  247. .ops = STM32_TIMER_OPS,\
  248. }
  249. struct clk_stm32_fixed_rate_cfg {
  250. unsigned long rate;
  251. };
  252. #define CLK_FIXED_RATE(idx, _binding, _rate) \
  253. [(idx)] = (struct clk_stm32){ \
  254. .binding = (_binding),\
  255. .parent = (CLK_IS_ROOT),\
  256. .clock_cfg = &(struct clk_stm32_fixed_rate_cfg){\
  257. .rate = (_rate),\
  258. },\
  259. .ops = STM32_FIXED_RATE_OPS,\
  260. }
  261. #define BYPASS(_offset, _bit_byp, _bit_digbyp) &(struct stm32_clk_bypass){\
  262. .offset = (_offset),\
  263. .bit_byp = (_bit_byp),\
  264. .bit_digbyp = (_bit_digbyp),\
  265. }
  266. #define CSS(_offset, _bit_css) &(struct stm32_clk_css){\
  267. .offset = (_offset),\
  268. .bit_css = (_bit_css),\
  269. }
  270. #define DRIVE(_offset, _shift, _width, _default) &(struct stm32_clk_drive){\
  271. .offset = (_offset),\
  272. .drv_shift = (_shift),\
  273. .drv_width = (_width),\
  274. .drv_default = (_default),\
  275. }
  276. #define OSCILLATOR(idx_osc, _id, _name, _gate_id, _gate_rdy_id, _bypass, _css, _drive) \
  277. [(idx_osc)] = (struct clk_oscillator_data){\
  278. .name = (_name),\
  279. .id_clk = (_id),\
  280. .gate_id = (_gate_id),\
  281. .gate_rdy_id = (_gate_rdy_id),\
  282. .bypass = (_bypass),\
  283. .css = (_css),\
  284. .drive = (_drive),\
  285. }
  286. struct clk_oscillator_data *clk_oscillator_get_data(struct stm32_clk_priv *priv, int id);
  287. void clk_stm32_osc_init(struct stm32_clk_priv *priv, int id);
  288. bool clk_stm32_osc_gate_is_enabled(struct stm32_clk_priv *priv, int id);
  289. int clk_stm32_osc_gate_enable(struct stm32_clk_priv *priv, int id);
  290. void clk_stm32_osc_gate_disable(struct stm32_clk_priv *priv, int id);
  291. struct stm32_osc_cfg {
  292. uint8_t osc_id;
  293. };
  294. #define CLK_OSC(idx, _idx, _parent, _osc_id) \
  295. [(idx)] = (struct clk_stm32){ \
  296. .binding = (_idx),\
  297. .parent = (_parent),\
  298. .flags = CLK_IS_CRITICAL,\
  299. .clock_cfg = &(struct stm32_osc_cfg){\
  300. .osc_id = (_osc_id),\
  301. },\
  302. .ops = STM32_OSC_OPS,\
  303. }
  304. #define CLK_OSC_FIXED(idx, _idx, _parent, _osc_id) \
  305. [(idx)] = (struct clk_stm32){ \
  306. .binding = (_idx),\
  307. .parent = (_parent),\
  308. .flags = CLK_IS_CRITICAL,\
  309. .clock_cfg = &(struct stm32_osc_cfg){\
  310. .osc_id = (_osc_id),\
  311. },\
  312. .ops = STM32_OSC_NOGATE_OPS,\
  313. }
  314. extern const struct stm32_clk_ops clk_mux_ops;
  315. extern const struct stm32_clk_ops clk_stm32_divider_ops;
  316. extern const struct stm32_clk_ops clk_stm32_gate_ops;
  317. extern const struct stm32_clk_ops clk_fixed_factor_ops;
  318. extern const struct stm32_clk_ops clk_gate_ops;
  319. extern const struct stm32_clk_ops clk_timer_ops;
  320. extern const struct stm32_clk_ops clk_stm32_fixed_rate_ops;
  321. extern const struct stm32_clk_ops clk_stm32_osc_ops;
  322. extern const struct stm32_clk_ops clk_stm32_osc_nogate_ops;
  323. enum {
  324. NO_OPS,
  325. FIXED_FACTOR_OPS,
  326. GATE_OPS,
  327. STM32_MUX_OPS,
  328. STM32_DIVIDER_OPS,
  329. STM32_GATE_OPS,
  330. STM32_TIMER_OPS,
  331. STM32_FIXED_RATE_OPS,
  332. STM32_OSC_OPS,
  333. STM32_OSC_NOGATE_OPS,
  334. STM32_LAST_OPS
  335. };
  336. #endif /* CLK_STM32_CORE_H */