clk.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (c) 2014 MundoReader S.L.
  3. * Author: Heiko Stuebner <heiko@sntech.de>
  4. *
  5. * based on
  6. *
  7. * samsung/clk.h
  8. * Copyright (c) 2013 Samsung Electronics Co., Ltd.
  9. * Copyright (c) 2013 Linaro Ltd.
  10. * Author: Thomas Abraham <thomas.ab@samsung.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #ifndef CLK_ROCKCHIP_CLK_H
  23. #define CLK_ROCKCHIP_CLK_H
  24. #include <linux/io.h>
  25. #include <linux/clk.h>
  26. #include <linux/clk-provider.h>
  27. #define HIWORD_UPDATE(val, mask, shift) \
  28. ((val) << (shift) | (mask) << ((shift) + 16))
  29. /* register positions shared by RK2928, RK3066 and RK3188 */
  30. #define RK2928_PLL_CON(x) (x * 0x4)
  31. #define RK2928_MODE_CON 0x40
  32. #define RK2928_CLKSEL_CON(x) (x * 0x4 + 0x44)
  33. #define RK2928_CLKGATE_CON(x) (x * 0x4 + 0xd0)
  34. #define RK2928_GLB_SRST_FST 0x100
  35. #define RK2928_GLB_SRST_SND 0x104
  36. #define RK2928_SOFTRST_CON(x) (x * 0x4 + 0x110)
  37. #define RK2928_MISC_CON 0x134
  38. #define RK3288_PLL_CON(x) RK2928_PLL_CON(x)
  39. #define RK3288_MODE_CON 0x50
  40. #define RK3288_CLKSEL_CON(x) (x * 0x4 + 0x60)
  41. #define RK3288_CLKGATE_CON(x) (x * 0x4 + 0x160)
  42. #define RK3288_GLB_SRST_FST 0x1b0
  43. #define RK3288_GLB_SRST_SND 0x1b4
  44. #define RK3288_SOFTRST_CON(x) (x * 0x4 + 0x1b8)
  45. #define RK3288_MISC_CON 0x1e8
  46. enum rockchip_pll_type {
  47. pll_rk3066,
  48. };
  49. #define RK3066_PLL_RATE(_rate, _nr, _nf, _no) \
  50. { \
  51. .rate = _rate##U, \
  52. .nr = _nr, \
  53. .nf = _nf, \
  54. .no = _no, \
  55. .bwadj = (_nf >> 1), \
  56. }
  57. struct rockchip_pll_rate_table {
  58. unsigned long rate;
  59. unsigned int nr;
  60. unsigned int nf;
  61. unsigned int no;
  62. unsigned int bwadj;
  63. };
  64. /**
  65. * struct rockchip_pll_clock: information about pll clock
  66. * @id: platform specific id of the clock.
  67. * @name: name of this pll clock.
  68. * @parent_name: name of the parent clock.
  69. * @flags: optional flags for basic clock.
  70. * @con_offset: offset of the register for configuring the PLL.
  71. * @mode_offset: offset of the register for configuring the PLL-mode.
  72. * @mode_shift: offset inside the mode-register for the mode of this pll.
  73. * @lock_shift: offset inside the lock register for the lock status.
  74. * @type: Type of PLL to be registered.
  75. * @rate_table: Table of usable pll rates
  76. */
  77. struct rockchip_pll_clock {
  78. unsigned int id;
  79. const char *name;
  80. const char **parent_names;
  81. u8 num_parents;
  82. unsigned long flags;
  83. int con_offset;
  84. int mode_offset;
  85. int mode_shift;
  86. int lock_shift;
  87. enum rockchip_pll_type type;
  88. struct rockchip_pll_rate_table *rate_table;
  89. };
  90. #define PLL(_type, _id, _name, _pnames, _flags, _con, _mode, _mshift, \
  91. _lshift, _rtable) \
  92. { \
  93. .id = _id, \
  94. .type = _type, \
  95. .name = _name, \
  96. .parent_names = _pnames, \
  97. .num_parents = ARRAY_SIZE(_pnames), \
  98. .flags = CLK_GET_RATE_NOCACHE | _flags, \
  99. .con_offset = _con, \
  100. .mode_offset = _mode, \
  101. .mode_shift = _mshift, \
  102. .lock_shift = _lshift, \
  103. .rate_table = _rtable, \
  104. }
  105. struct clk *rockchip_clk_register_pll(enum rockchip_pll_type pll_type,
  106. const char *name, const char **parent_names, u8 num_parents,
  107. void __iomem *base, int con_offset, int grf_lock_offset,
  108. int lock_shift, int reg_mode, int mode_shift,
  109. struct rockchip_pll_rate_table *rate_table,
  110. spinlock_t *lock);
  111. #define PNAME(x) static const char *x[] __initconst
  112. enum rockchip_clk_branch_type {
  113. branch_composite,
  114. branch_mux,
  115. branch_divider,
  116. branch_fraction_divider,
  117. branch_gate,
  118. };
  119. struct rockchip_clk_branch {
  120. unsigned int id;
  121. enum rockchip_clk_branch_type branch_type;
  122. const char *name;
  123. const char **parent_names;
  124. u8 num_parents;
  125. unsigned long flags;
  126. int muxdiv_offset;
  127. u8 mux_shift;
  128. u8 mux_width;
  129. u8 mux_flags;
  130. u8 div_shift;
  131. u8 div_width;
  132. u8 div_flags;
  133. struct clk_div_table *div_table;
  134. int gate_offset;
  135. u8 gate_shift;
  136. u8 gate_flags;
  137. };
  138. #define COMPOSITE(_id, cname, pnames, f, mo, ms, mw, mf, ds, dw,\
  139. df, go, gs, gf) \
  140. { \
  141. .id = _id, \
  142. .branch_type = branch_composite, \
  143. .name = cname, \
  144. .parent_names = pnames, \
  145. .num_parents = ARRAY_SIZE(pnames), \
  146. .flags = f, \
  147. .muxdiv_offset = mo, \
  148. .mux_shift = ms, \
  149. .mux_width = mw, \
  150. .mux_flags = mf, \
  151. .div_shift = ds, \
  152. .div_width = dw, \
  153. .div_flags = df, \
  154. .gate_offset = go, \
  155. .gate_shift = gs, \
  156. .gate_flags = gf, \
  157. }
  158. #define COMPOSITE_NOMUX(_id, cname, pname, f, mo, ds, dw, df, \
  159. go, gs, gf) \
  160. { \
  161. .id = _id, \
  162. .branch_type = branch_composite, \
  163. .name = cname, \
  164. .parent_names = (const char *[]){ pname }, \
  165. .num_parents = 1, \
  166. .flags = f, \
  167. .muxdiv_offset = mo, \
  168. .div_shift = ds, \
  169. .div_width = dw, \
  170. .div_flags = df, \
  171. .gate_offset = go, \
  172. .gate_shift = gs, \
  173. .gate_flags = gf, \
  174. }
  175. #define COMPOSITE_NOMUX_DIVTBL(_id, cname, pname, f, mo, ds, dw,\
  176. df, dt, go, gs, gf) \
  177. { \
  178. .id = _id, \
  179. .branch_type = branch_composite, \
  180. .name = cname, \
  181. .parent_names = (const char *[]){ pname }, \
  182. .num_parents = 1, \
  183. .flags = f, \
  184. .muxdiv_offset = mo, \
  185. .div_shift = ds, \
  186. .div_width = dw, \
  187. .div_flags = df, \
  188. .div_table = dt, \
  189. .gate_offset = go, \
  190. .gate_shift = gs, \
  191. .gate_flags = gf, \
  192. }
  193. #define COMPOSITE_NODIV(_id, cname, pnames, f, mo, ms, mw, mf, \
  194. go, gs, gf) \
  195. { \
  196. .id = _id, \
  197. .branch_type = branch_composite, \
  198. .name = cname, \
  199. .parent_names = pnames, \
  200. .num_parents = ARRAY_SIZE(pnames), \
  201. .flags = f, \
  202. .muxdiv_offset = mo, \
  203. .mux_shift = ms, \
  204. .mux_width = mw, \
  205. .mux_flags = mf, \
  206. .gate_offset = go, \
  207. .gate_shift = gs, \
  208. .gate_flags = gf, \
  209. }
  210. #define COMPOSITE_NOGATE(_id, cname, pnames, f, mo, ms, mw, mf, \
  211. ds, dw, df) \
  212. { \
  213. .id = _id, \
  214. .branch_type = branch_composite, \
  215. .name = cname, \
  216. .parent_names = pnames, \
  217. .num_parents = ARRAY_SIZE(pnames), \
  218. .flags = f, \
  219. .muxdiv_offset = mo, \
  220. .mux_shift = ms, \
  221. .mux_width = mw, \
  222. .mux_flags = mf, \
  223. .div_shift = ds, \
  224. .div_width = dw, \
  225. .div_flags = df, \
  226. .gate_offset = -1, \
  227. }
  228. #define COMPOSITE_FRAC(_id, cname, pname, f, mo, df, go, gs, gf)\
  229. { \
  230. .id = _id, \
  231. .branch_type = branch_fraction_divider, \
  232. .name = cname, \
  233. .parent_names = (const char *[]){ pname }, \
  234. .num_parents = 1, \
  235. .flags = f, \
  236. .muxdiv_offset = mo, \
  237. .div_shift = 16, \
  238. .div_width = 16, \
  239. .div_flags = df, \
  240. .gate_offset = go, \
  241. .gate_shift = gs, \
  242. .gate_flags = gf, \
  243. }
  244. #define MUX(_id, cname, pnames, f, o, s, w, mf) \
  245. { \
  246. .id = _id, \
  247. .branch_type = branch_mux, \
  248. .name = cname, \
  249. .parent_names = pnames, \
  250. .num_parents = ARRAY_SIZE(pnames), \
  251. .flags = f, \
  252. .muxdiv_offset = o, \
  253. .mux_shift = s, \
  254. .mux_width = w, \
  255. .mux_flags = mf, \
  256. .gate_offset = -1, \
  257. }
  258. #define DIV(_id, cname, pname, f, o, s, w, df) \
  259. { \
  260. .id = _id, \
  261. .branch_type = branch_divider, \
  262. .name = cname, \
  263. .parent_names = (const char *[]){ pname }, \
  264. .num_parents = 1, \
  265. .flags = f, \
  266. .muxdiv_offset = o, \
  267. .div_shift = s, \
  268. .div_width = w, \
  269. .div_flags = df, \
  270. .gate_offset = -1, \
  271. }
  272. #define DIVTBL(_id, cname, pname, f, o, s, w, df, dt) \
  273. { \
  274. .id = _id, \
  275. .branch_type = branch_divider, \
  276. .name = cname, \
  277. .parent_names = (const char *[]){ pname }, \
  278. .num_parents = 1, \
  279. .flags = f, \
  280. .muxdiv_offset = o, \
  281. .div_shift = s, \
  282. .div_width = w, \
  283. .div_flags = df, \
  284. .div_table = dt, \
  285. }
  286. #define GATE(_id, cname, pname, f, o, b, gf) \
  287. { \
  288. .id = _id, \
  289. .branch_type = branch_gate, \
  290. .name = cname, \
  291. .parent_names = (const char *[]){ pname }, \
  292. .num_parents = 1, \
  293. .flags = f, \
  294. .gate_offset = o, \
  295. .gate_shift = b, \
  296. .gate_flags = gf, \
  297. }
  298. void rockchip_clk_init(struct device_node *np, void __iomem *base,
  299. unsigned long nr_clks);
  300. struct regmap *rockchip_clk_get_grf(void);
  301. void rockchip_clk_add_lookup(struct clk *clk, unsigned int id);
  302. void rockchip_clk_register_branches(struct rockchip_clk_branch *clk_list,
  303. unsigned int nr_clk);
  304. void rockchip_clk_register_plls(struct rockchip_pll_clock *pll_list,
  305. unsigned int nr_pll, int grf_lock_offset);
  306. #define ROCKCHIP_SOFTRST_HIWORD_MASK BIT(0)
  307. #ifdef CONFIG_RESET_CONTROLLER
  308. void rockchip_register_softrst(struct device_node *np,
  309. unsigned int num_regs,
  310. void __iomem *base, u8 flags);
  311. #else
  312. static inline void rockchip_register_softrst(struct device_node *np,
  313. unsigned int num_regs,
  314. void __iomem *base, u8 flags)
  315. {
  316. }
  317. #endif
  318. #endif