clk.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. struct rockchip_cpuclk_clksel {
  112. int reg;
  113. u32 val;
  114. };
  115. #define ROCKCHIP_CPUCLK_NUM_DIVIDERS 2
  116. struct rockchip_cpuclk_rate_table {
  117. unsigned long prate;
  118. struct rockchip_cpuclk_clksel divs[ROCKCHIP_CPUCLK_NUM_DIVIDERS];
  119. };
  120. /**
  121. * struct rockchip_cpuclk_reg_data: describes register offsets and masks of the cpuclock
  122. * @core_reg: register offset of the core settings register
  123. * @div_core_shift: core divider offset used to divide the pll value
  124. * @div_core_mask: core divider mask
  125. * @mux_core_shift: offset of the core multiplexer
  126. */
  127. struct rockchip_cpuclk_reg_data {
  128. int core_reg;
  129. u8 div_core_shift;
  130. u32 div_core_mask;
  131. int mux_core_reg;
  132. u8 mux_core_shift;
  133. };
  134. struct clk *rockchip_clk_register_cpuclk(const char *name,
  135. const char **parent_names, u8 num_parents,
  136. const struct rockchip_cpuclk_reg_data *reg_data,
  137. const struct rockchip_cpuclk_rate_table *rates,
  138. int nrates, void __iomem *reg_base, spinlock_t *lock);
  139. #define PNAME(x) static const char *x[] __initconst
  140. enum rockchip_clk_branch_type {
  141. branch_composite,
  142. branch_mux,
  143. branch_divider,
  144. branch_fraction_divider,
  145. branch_gate,
  146. };
  147. struct rockchip_clk_branch {
  148. unsigned int id;
  149. enum rockchip_clk_branch_type branch_type;
  150. const char *name;
  151. const char **parent_names;
  152. u8 num_parents;
  153. unsigned long flags;
  154. int muxdiv_offset;
  155. u8 mux_shift;
  156. u8 mux_width;
  157. u8 mux_flags;
  158. u8 div_shift;
  159. u8 div_width;
  160. u8 div_flags;
  161. struct clk_div_table *div_table;
  162. int gate_offset;
  163. u8 gate_shift;
  164. u8 gate_flags;
  165. };
  166. #define COMPOSITE(_id, cname, pnames, f, mo, ms, mw, mf, ds, dw,\
  167. df, go, gs, gf) \
  168. { \
  169. .id = _id, \
  170. .branch_type = branch_composite, \
  171. .name = cname, \
  172. .parent_names = pnames, \
  173. .num_parents = ARRAY_SIZE(pnames), \
  174. .flags = f, \
  175. .muxdiv_offset = mo, \
  176. .mux_shift = ms, \
  177. .mux_width = mw, \
  178. .mux_flags = mf, \
  179. .div_shift = ds, \
  180. .div_width = dw, \
  181. .div_flags = df, \
  182. .gate_offset = go, \
  183. .gate_shift = gs, \
  184. .gate_flags = gf, \
  185. }
  186. #define COMPOSITE_NOMUX(_id, cname, pname, f, mo, ds, dw, df, \
  187. go, gs, gf) \
  188. { \
  189. .id = _id, \
  190. .branch_type = branch_composite, \
  191. .name = cname, \
  192. .parent_names = (const char *[]){ pname }, \
  193. .num_parents = 1, \
  194. .flags = f, \
  195. .muxdiv_offset = mo, \
  196. .div_shift = ds, \
  197. .div_width = dw, \
  198. .div_flags = df, \
  199. .gate_offset = go, \
  200. .gate_shift = gs, \
  201. .gate_flags = gf, \
  202. }
  203. #define COMPOSITE_NOMUX_DIVTBL(_id, cname, pname, f, mo, ds, dw,\
  204. df, dt, go, gs, gf) \
  205. { \
  206. .id = _id, \
  207. .branch_type = branch_composite, \
  208. .name = cname, \
  209. .parent_names = (const char *[]){ pname }, \
  210. .num_parents = 1, \
  211. .flags = f, \
  212. .muxdiv_offset = mo, \
  213. .div_shift = ds, \
  214. .div_width = dw, \
  215. .div_flags = df, \
  216. .div_table = dt, \
  217. .gate_offset = go, \
  218. .gate_shift = gs, \
  219. .gate_flags = gf, \
  220. }
  221. #define COMPOSITE_NODIV(_id, cname, pnames, f, mo, ms, mw, mf, \
  222. go, gs, gf) \
  223. { \
  224. .id = _id, \
  225. .branch_type = branch_composite, \
  226. .name = cname, \
  227. .parent_names = pnames, \
  228. .num_parents = ARRAY_SIZE(pnames), \
  229. .flags = f, \
  230. .muxdiv_offset = mo, \
  231. .mux_shift = ms, \
  232. .mux_width = mw, \
  233. .mux_flags = mf, \
  234. .gate_offset = go, \
  235. .gate_shift = gs, \
  236. .gate_flags = gf, \
  237. }
  238. #define COMPOSITE_NOGATE(_id, cname, pnames, f, mo, ms, mw, mf, \
  239. ds, dw, df) \
  240. { \
  241. .id = _id, \
  242. .branch_type = branch_composite, \
  243. .name = cname, \
  244. .parent_names = pnames, \
  245. .num_parents = ARRAY_SIZE(pnames), \
  246. .flags = f, \
  247. .muxdiv_offset = mo, \
  248. .mux_shift = ms, \
  249. .mux_width = mw, \
  250. .mux_flags = mf, \
  251. .div_shift = ds, \
  252. .div_width = dw, \
  253. .div_flags = df, \
  254. .gate_offset = -1, \
  255. }
  256. #define COMPOSITE_FRAC(_id, cname, pname, f, mo, df, go, gs, gf)\
  257. { \
  258. .id = _id, \
  259. .branch_type = branch_fraction_divider, \
  260. .name = cname, \
  261. .parent_names = (const char *[]){ pname }, \
  262. .num_parents = 1, \
  263. .flags = f, \
  264. .muxdiv_offset = mo, \
  265. .div_shift = 16, \
  266. .div_width = 16, \
  267. .div_flags = df, \
  268. .gate_offset = go, \
  269. .gate_shift = gs, \
  270. .gate_flags = gf, \
  271. }
  272. #define MUX(_id, cname, pnames, f, o, s, w, mf) \
  273. { \
  274. .id = _id, \
  275. .branch_type = branch_mux, \
  276. .name = cname, \
  277. .parent_names = pnames, \
  278. .num_parents = ARRAY_SIZE(pnames), \
  279. .flags = f, \
  280. .muxdiv_offset = o, \
  281. .mux_shift = s, \
  282. .mux_width = w, \
  283. .mux_flags = mf, \
  284. .gate_offset = -1, \
  285. }
  286. #define DIV(_id, cname, pname, f, o, s, w, df) \
  287. { \
  288. .id = _id, \
  289. .branch_type = branch_divider, \
  290. .name = cname, \
  291. .parent_names = (const char *[]){ pname }, \
  292. .num_parents = 1, \
  293. .flags = f, \
  294. .muxdiv_offset = o, \
  295. .div_shift = s, \
  296. .div_width = w, \
  297. .div_flags = df, \
  298. .gate_offset = -1, \
  299. }
  300. #define DIVTBL(_id, cname, pname, f, o, s, w, df, dt) \
  301. { \
  302. .id = _id, \
  303. .branch_type = branch_divider, \
  304. .name = cname, \
  305. .parent_names = (const char *[]){ pname }, \
  306. .num_parents = 1, \
  307. .flags = f, \
  308. .muxdiv_offset = o, \
  309. .div_shift = s, \
  310. .div_width = w, \
  311. .div_flags = df, \
  312. .div_table = dt, \
  313. }
  314. #define GATE(_id, cname, pname, f, o, b, gf) \
  315. { \
  316. .id = _id, \
  317. .branch_type = branch_gate, \
  318. .name = cname, \
  319. .parent_names = (const char *[]){ pname }, \
  320. .num_parents = 1, \
  321. .flags = f, \
  322. .gate_offset = o, \
  323. .gate_shift = b, \
  324. .gate_flags = gf, \
  325. }
  326. void rockchip_clk_init(struct device_node *np, void __iomem *base,
  327. unsigned long nr_clks);
  328. struct regmap *rockchip_clk_get_grf(void);
  329. void rockchip_clk_add_lookup(struct clk *clk, unsigned int id);
  330. void rockchip_clk_register_branches(struct rockchip_clk_branch *clk_list,
  331. unsigned int nr_clk);
  332. void rockchip_clk_register_plls(struct rockchip_pll_clock *pll_list,
  333. unsigned int nr_pll, int grf_lock_offset);
  334. void rockchip_clk_register_armclk(unsigned int lookup_id, const char *name,
  335. const char **parent_names, u8 num_parents,
  336. const struct rockchip_cpuclk_reg_data *reg_data,
  337. const struct rockchip_cpuclk_rate_table *rates,
  338. int nrates);
  339. void rockchip_clk_protect_critical(const char *clocks[], int nclocks);
  340. void rockchip_register_restart_notifier(unsigned int reg);
  341. #define ROCKCHIP_SOFTRST_HIWORD_MASK BIT(0)
  342. #ifdef CONFIG_RESET_CONTROLLER
  343. void rockchip_register_softrst(struct device_node *np,
  344. unsigned int num_regs,
  345. void __iomem *base, u8 flags);
  346. #else
  347. static inline void rockchip_register_softrst(struct device_node *np,
  348. unsigned int num_regs,
  349. void __iomem *base, u8 flags)
  350. {
  351. }
  352. #endif
  353. #endif