clk.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. #define RK3288_SDMMC_CON0 0x200
  47. #define RK3288_SDMMC_CON1 0x204
  48. #define RK3288_SDIO0_CON0 0x208
  49. #define RK3288_SDIO0_CON1 0x20c
  50. #define RK3288_SDIO1_CON0 0x210
  51. #define RK3288_SDIO1_CON1 0x214
  52. #define RK3288_EMMC_CON0 0x218
  53. #define RK3288_EMMC_CON1 0x21c
  54. enum rockchip_pll_type {
  55. pll_rk3066,
  56. };
  57. #define RK3066_PLL_RATE(_rate, _nr, _nf, _no) \
  58. { \
  59. .rate = _rate##U, \
  60. .nr = _nr, \
  61. .nf = _nf, \
  62. .no = _no, \
  63. .bwadj = (_nf >> 1), \
  64. }
  65. #define RK3066_PLL_RATE_BWADJ(_rate, _nr, _nf, _no, _bw) \
  66. { \
  67. .rate = _rate##U, \
  68. .nr = _nr, \
  69. .nf = _nf, \
  70. .no = _no, \
  71. .bwadj = _bw, \
  72. }
  73. struct rockchip_pll_rate_table {
  74. unsigned long rate;
  75. unsigned int nr;
  76. unsigned int nf;
  77. unsigned int no;
  78. unsigned int bwadj;
  79. };
  80. /**
  81. * struct rockchip_pll_clock: information about pll clock
  82. * @id: platform specific id of the clock.
  83. * @name: name of this pll clock.
  84. * @parent_name: name of the parent clock.
  85. * @flags: optional flags for basic clock.
  86. * @con_offset: offset of the register for configuring the PLL.
  87. * @mode_offset: offset of the register for configuring the PLL-mode.
  88. * @mode_shift: offset inside the mode-register for the mode of this pll.
  89. * @lock_shift: offset inside the lock register for the lock status.
  90. * @type: Type of PLL to be registered.
  91. * @pll_flags: hardware-specific flags
  92. * @rate_table: Table of usable pll rates
  93. *
  94. * Flags:
  95. * ROCKCHIP_PLL_SYNC_RATE - check rate parameters to match against the
  96. * rate_table parameters and ajust them if necessary.
  97. */
  98. struct rockchip_pll_clock {
  99. unsigned int id;
  100. const char *name;
  101. const char **parent_names;
  102. u8 num_parents;
  103. unsigned long flags;
  104. int con_offset;
  105. int mode_offset;
  106. int mode_shift;
  107. int lock_shift;
  108. enum rockchip_pll_type type;
  109. u8 pll_flags;
  110. struct rockchip_pll_rate_table *rate_table;
  111. };
  112. #define ROCKCHIP_PLL_SYNC_RATE BIT(0)
  113. #define PLL(_type, _id, _name, _pnames, _flags, _con, _mode, _mshift, \
  114. _lshift, _pflags, _rtable) \
  115. { \
  116. .id = _id, \
  117. .type = _type, \
  118. .name = _name, \
  119. .parent_names = _pnames, \
  120. .num_parents = ARRAY_SIZE(_pnames), \
  121. .flags = CLK_GET_RATE_NOCACHE | _flags, \
  122. .con_offset = _con, \
  123. .mode_offset = _mode, \
  124. .mode_shift = _mshift, \
  125. .lock_shift = _lshift, \
  126. .pll_flags = _pflags, \
  127. .rate_table = _rtable, \
  128. }
  129. struct clk *rockchip_clk_register_pll(enum rockchip_pll_type pll_type,
  130. const char *name, const char **parent_names, u8 num_parents,
  131. void __iomem *base, int con_offset, int grf_lock_offset,
  132. int lock_shift, int reg_mode, int mode_shift,
  133. struct rockchip_pll_rate_table *rate_table,
  134. u8 clk_pll_flags, spinlock_t *lock);
  135. struct rockchip_cpuclk_clksel {
  136. int reg;
  137. u32 val;
  138. };
  139. #define ROCKCHIP_CPUCLK_NUM_DIVIDERS 2
  140. struct rockchip_cpuclk_rate_table {
  141. unsigned long prate;
  142. struct rockchip_cpuclk_clksel divs[ROCKCHIP_CPUCLK_NUM_DIVIDERS];
  143. };
  144. /**
  145. * struct rockchip_cpuclk_reg_data: describes register offsets and masks of the cpuclock
  146. * @core_reg: register offset of the core settings register
  147. * @div_core_shift: core divider offset used to divide the pll value
  148. * @div_core_mask: core divider mask
  149. * @mux_core_shift: offset of the core multiplexer
  150. */
  151. struct rockchip_cpuclk_reg_data {
  152. int core_reg;
  153. u8 div_core_shift;
  154. u32 div_core_mask;
  155. int mux_core_reg;
  156. u8 mux_core_shift;
  157. };
  158. struct clk *rockchip_clk_register_cpuclk(const char *name,
  159. const char **parent_names, u8 num_parents,
  160. const struct rockchip_cpuclk_reg_data *reg_data,
  161. const struct rockchip_cpuclk_rate_table *rates,
  162. int nrates, void __iomem *reg_base, spinlock_t *lock);
  163. struct clk *rockchip_clk_register_mmc(const char *name,
  164. const char **parent_names, u8 num_parents,
  165. void __iomem *reg, int shift);
  166. #define PNAME(x) static const char *x[] __initconst
  167. enum rockchip_clk_branch_type {
  168. branch_composite,
  169. branch_mux,
  170. branch_divider,
  171. branch_fraction_divider,
  172. branch_gate,
  173. branch_mmc,
  174. };
  175. struct rockchip_clk_branch {
  176. unsigned int id;
  177. enum rockchip_clk_branch_type branch_type;
  178. const char *name;
  179. const char **parent_names;
  180. u8 num_parents;
  181. unsigned long flags;
  182. int muxdiv_offset;
  183. u8 mux_shift;
  184. u8 mux_width;
  185. u8 mux_flags;
  186. u8 div_shift;
  187. u8 div_width;
  188. u8 div_flags;
  189. struct clk_div_table *div_table;
  190. int gate_offset;
  191. u8 gate_shift;
  192. u8 gate_flags;
  193. };
  194. #define COMPOSITE(_id, cname, pnames, f, mo, ms, mw, mf, ds, dw,\
  195. df, go, gs, gf) \
  196. { \
  197. .id = _id, \
  198. .branch_type = branch_composite, \
  199. .name = cname, \
  200. .parent_names = pnames, \
  201. .num_parents = ARRAY_SIZE(pnames), \
  202. .flags = f, \
  203. .muxdiv_offset = mo, \
  204. .mux_shift = ms, \
  205. .mux_width = mw, \
  206. .mux_flags = mf, \
  207. .div_shift = ds, \
  208. .div_width = dw, \
  209. .div_flags = df, \
  210. .gate_offset = go, \
  211. .gate_shift = gs, \
  212. .gate_flags = gf, \
  213. }
  214. #define COMPOSITE_NOMUX(_id, cname, pname, f, mo, ds, dw, df, \
  215. go, gs, gf) \
  216. { \
  217. .id = _id, \
  218. .branch_type = branch_composite, \
  219. .name = cname, \
  220. .parent_names = (const char *[]){ pname }, \
  221. .num_parents = 1, \
  222. .flags = f, \
  223. .muxdiv_offset = mo, \
  224. .div_shift = ds, \
  225. .div_width = dw, \
  226. .div_flags = df, \
  227. .gate_offset = go, \
  228. .gate_shift = gs, \
  229. .gate_flags = gf, \
  230. }
  231. #define COMPOSITE_NOMUX_DIVTBL(_id, cname, pname, f, mo, ds, dw,\
  232. df, dt, go, gs, gf) \
  233. { \
  234. .id = _id, \
  235. .branch_type = branch_composite, \
  236. .name = cname, \
  237. .parent_names = (const char *[]){ pname }, \
  238. .num_parents = 1, \
  239. .flags = f, \
  240. .muxdiv_offset = mo, \
  241. .div_shift = ds, \
  242. .div_width = dw, \
  243. .div_flags = df, \
  244. .div_table = dt, \
  245. .gate_offset = go, \
  246. .gate_shift = gs, \
  247. .gate_flags = gf, \
  248. }
  249. #define COMPOSITE_NODIV(_id, cname, pnames, f, mo, ms, mw, mf, \
  250. go, gs, gf) \
  251. { \
  252. .id = _id, \
  253. .branch_type = branch_composite, \
  254. .name = cname, \
  255. .parent_names = pnames, \
  256. .num_parents = ARRAY_SIZE(pnames), \
  257. .flags = f, \
  258. .muxdiv_offset = mo, \
  259. .mux_shift = ms, \
  260. .mux_width = mw, \
  261. .mux_flags = mf, \
  262. .gate_offset = go, \
  263. .gate_shift = gs, \
  264. .gate_flags = gf, \
  265. }
  266. #define COMPOSITE_NOGATE(_id, cname, pnames, f, mo, ms, mw, mf, \
  267. ds, dw, df) \
  268. { \
  269. .id = _id, \
  270. .branch_type = branch_composite, \
  271. .name = cname, \
  272. .parent_names = pnames, \
  273. .num_parents = ARRAY_SIZE(pnames), \
  274. .flags = f, \
  275. .muxdiv_offset = mo, \
  276. .mux_shift = ms, \
  277. .mux_width = mw, \
  278. .mux_flags = mf, \
  279. .div_shift = ds, \
  280. .div_width = dw, \
  281. .div_flags = df, \
  282. .gate_offset = -1, \
  283. }
  284. #define COMPOSITE_FRAC(_id, cname, pname, f, mo, df, go, gs, gf)\
  285. { \
  286. .id = _id, \
  287. .branch_type = branch_fraction_divider, \
  288. .name = cname, \
  289. .parent_names = (const char *[]){ pname }, \
  290. .num_parents = 1, \
  291. .flags = f, \
  292. .muxdiv_offset = mo, \
  293. .div_shift = 16, \
  294. .div_width = 16, \
  295. .div_flags = df, \
  296. .gate_offset = go, \
  297. .gate_shift = gs, \
  298. .gate_flags = gf, \
  299. }
  300. #define MUX(_id, cname, pnames, f, o, s, w, mf) \
  301. { \
  302. .id = _id, \
  303. .branch_type = branch_mux, \
  304. .name = cname, \
  305. .parent_names = pnames, \
  306. .num_parents = ARRAY_SIZE(pnames), \
  307. .flags = f, \
  308. .muxdiv_offset = o, \
  309. .mux_shift = s, \
  310. .mux_width = w, \
  311. .mux_flags = mf, \
  312. .gate_offset = -1, \
  313. }
  314. #define DIV(_id, cname, pname, f, o, s, w, df) \
  315. { \
  316. .id = _id, \
  317. .branch_type = branch_divider, \
  318. .name = cname, \
  319. .parent_names = (const char *[]){ pname }, \
  320. .num_parents = 1, \
  321. .flags = f, \
  322. .muxdiv_offset = o, \
  323. .div_shift = s, \
  324. .div_width = w, \
  325. .div_flags = df, \
  326. .gate_offset = -1, \
  327. }
  328. #define DIVTBL(_id, cname, pname, f, o, s, w, df, dt) \
  329. { \
  330. .id = _id, \
  331. .branch_type = branch_divider, \
  332. .name = cname, \
  333. .parent_names = (const char *[]){ pname }, \
  334. .num_parents = 1, \
  335. .flags = f, \
  336. .muxdiv_offset = o, \
  337. .div_shift = s, \
  338. .div_width = w, \
  339. .div_flags = df, \
  340. .div_table = dt, \
  341. }
  342. #define GATE(_id, cname, pname, f, o, b, gf) \
  343. { \
  344. .id = _id, \
  345. .branch_type = branch_gate, \
  346. .name = cname, \
  347. .parent_names = (const char *[]){ pname }, \
  348. .num_parents = 1, \
  349. .flags = f, \
  350. .gate_offset = o, \
  351. .gate_shift = b, \
  352. .gate_flags = gf, \
  353. }
  354. #define MMC(_id, cname, pname, offset, shift) \
  355. { \
  356. .id = _id, \
  357. .branch_type = branch_mmc, \
  358. .name = cname, \
  359. .parent_names = (const char *[]){ pname }, \
  360. .num_parents = 1, \
  361. .muxdiv_offset = offset, \
  362. .div_shift = shift, \
  363. }
  364. void rockchip_clk_init(struct device_node *np, void __iomem *base,
  365. unsigned long nr_clks);
  366. struct regmap *rockchip_clk_get_grf(void);
  367. void rockchip_clk_add_lookup(struct clk *clk, unsigned int id);
  368. void rockchip_clk_register_branches(struct rockchip_clk_branch *clk_list,
  369. unsigned int nr_clk);
  370. void rockchip_clk_register_plls(struct rockchip_pll_clock *pll_list,
  371. unsigned int nr_pll, int grf_lock_offset);
  372. void rockchip_clk_register_armclk(unsigned int lookup_id, const char *name,
  373. const char **parent_names, u8 num_parents,
  374. const struct rockchip_cpuclk_reg_data *reg_data,
  375. const struct rockchip_cpuclk_rate_table *rates,
  376. int nrates);
  377. void rockchip_clk_protect_critical(const char *clocks[], int nclocks);
  378. void rockchip_register_restart_notifier(unsigned int reg);
  379. #define ROCKCHIP_SOFTRST_HIWORD_MASK BIT(0)
  380. #ifdef CONFIG_RESET_CONTROLLER
  381. void rockchip_register_softrst(struct device_node *np,
  382. unsigned int num_regs,
  383. void __iomem *base, u8 flags);
  384. #else
  385. static inline void rockchip_register_softrst(struct device_node *np,
  386. unsigned int num_regs,
  387. void __iomem *base, u8 flags)
  388. {
  389. }
  390. #endif
  391. #endif