clk.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. struct clk;
  26. #define HIWORD_UPDATE(val, mask, shift) \
  27. ((val) << (shift) | (mask) << ((shift) + 16))
  28. /* register positions shared by RK2928, RK3066 and RK3188 */
  29. #define RK2928_PLL_CON(x) ((x) * 0x4)
  30. #define RK2928_MODE_CON 0x40
  31. #define RK2928_CLKSEL_CON(x) ((x) * 0x4 + 0x44)
  32. #define RK2928_CLKGATE_CON(x) ((x) * 0x4 + 0xd0)
  33. #define RK2928_GLB_SRST_FST 0x100
  34. #define RK2928_GLB_SRST_SND 0x104
  35. #define RK2928_SOFTRST_CON(x) ((x) * 0x4 + 0x110)
  36. #define RK2928_MISC_CON 0x134
  37. #define RK3288_PLL_CON(x) RK2928_PLL_CON(x)
  38. #define RK3288_MODE_CON 0x50
  39. #define RK3288_CLKSEL_CON(x) ((x) * 0x4 + 0x60)
  40. #define RK3288_CLKGATE_CON(x) ((x) * 0x4 + 0x160)
  41. #define RK3288_GLB_SRST_FST 0x1b0
  42. #define RK3288_GLB_SRST_SND 0x1b4
  43. #define RK3288_SOFTRST_CON(x) ((x) * 0x4 + 0x1b8)
  44. #define RK3288_MISC_CON 0x1e8
  45. #define RK3288_SDMMC_CON0 0x200
  46. #define RK3288_SDMMC_CON1 0x204
  47. #define RK3288_SDIO0_CON0 0x208
  48. #define RK3288_SDIO0_CON1 0x20c
  49. #define RK3288_SDIO1_CON0 0x210
  50. #define RK3288_SDIO1_CON1 0x214
  51. #define RK3288_EMMC_CON0 0x218
  52. #define RK3288_EMMC_CON1 0x21c
  53. #define RK3368_PLL_CON(x) RK2928_PLL_CON(x)
  54. #define RK3368_CLKSEL_CON(x) ((x) * 0x4 + 0x100)
  55. #define RK3368_CLKGATE_CON(x) ((x) * 0x4 + 0x200)
  56. #define RK3368_GLB_SRST_FST 0x280
  57. #define RK3368_GLB_SRST_SND 0x284
  58. #define RK3368_SOFTRST_CON(x) ((x) * 0x4 + 0x300)
  59. #define RK3368_MISC_CON 0x380
  60. #define RK3368_SDMMC_CON0 0x400
  61. #define RK3368_SDMMC_CON1 0x404
  62. #define RK3368_SDIO0_CON0 0x408
  63. #define RK3368_SDIO0_CON1 0x40c
  64. #define RK3368_SDIO1_CON0 0x410
  65. #define RK3368_SDIO1_CON1 0x414
  66. #define RK3368_EMMC_CON0 0x418
  67. #define RK3368_EMMC_CON1 0x41c
  68. enum rockchip_pll_type {
  69. pll_rk3066,
  70. };
  71. #define RK3066_PLL_RATE(_rate, _nr, _nf, _no) \
  72. { \
  73. .rate = _rate##U, \
  74. .nr = _nr, \
  75. .nf = _nf, \
  76. .no = _no, \
  77. .nb = ((_nf) < 2) ? 1 : (_nf) >> 1, \
  78. }
  79. #define RK3066_PLL_RATE_NB(_rate, _nr, _nf, _no, _nb) \
  80. { \
  81. .rate = _rate##U, \
  82. .nr = _nr, \
  83. .nf = _nf, \
  84. .no = _no, \
  85. .nb = _nb, \
  86. }
  87. struct rockchip_pll_rate_table {
  88. unsigned long rate;
  89. unsigned int nr;
  90. unsigned int nf;
  91. unsigned int no;
  92. unsigned int nb;
  93. };
  94. /**
  95. * struct rockchip_pll_clock: information about pll clock
  96. * @id: platform specific id of the clock.
  97. * @name: name of this pll clock.
  98. * @parent_name: name of the parent clock.
  99. * @flags: optional flags for basic clock.
  100. * @con_offset: offset of the register for configuring the PLL.
  101. * @mode_offset: offset of the register for configuring the PLL-mode.
  102. * @mode_shift: offset inside the mode-register for the mode of this pll.
  103. * @lock_shift: offset inside the lock register for the lock status.
  104. * @type: Type of PLL to be registered.
  105. * @pll_flags: hardware-specific flags
  106. * @rate_table: Table of usable pll rates
  107. *
  108. * Flags:
  109. * ROCKCHIP_PLL_SYNC_RATE - check rate parameters to match against the
  110. * rate_table parameters and ajust them if necessary.
  111. */
  112. struct rockchip_pll_clock {
  113. unsigned int id;
  114. const char *name;
  115. const char *const *parent_names;
  116. u8 num_parents;
  117. unsigned long flags;
  118. int con_offset;
  119. int mode_offset;
  120. int mode_shift;
  121. int lock_shift;
  122. enum rockchip_pll_type type;
  123. u8 pll_flags;
  124. struct rockchip_pll_rate_table *rate_table;
  125. };
  126. #define ROCKCHIP_PLL_SYNC_RATE BIT(0)
  127. #define PLL(_type, _id, _name, _pnames, _flags, _con, _mode, _mshift, \
  128. _lshift, _pflags, _rtable) \
  129. { \
  130. .id = _id, \
  131. .type = _type, \
  132. .name = _name, \
  133. .parent_names = _pnames, \
  134. .num_parents = ARRAY_SIZE(_pnames), \
  135. .flags = CLK_GET_RATE_NOCACHE | _flags, \
  136. .con_offset = _con, \
  137. .mode_offset = _mode, \
  138. .mode_shift = _mshift, \
  139. .lock_shift = _lshift, \
  140. .pll_flags = _pflags, \
  141. .rate_table = _rtable, \
  142. }
  143. struct clk *rockchip_clk_register_pll(enum rockchip_pll_type pll_type,
  144. const char *name, const char *const *parent_names,
  145. u8 num_parents, void __iomem *base, int con_offset,
  146. int grf_lock_offset, int lock_shift, int reg_mode,
  147. int mode_shift, struct rockchip_pll_rate_table *rate_table,
  148. u8 clk_pll_flags, spinlock_t *lock);
  149. struct rockchip_cpuclk_clksel {
  150. int reg;
  151. u32 val;
  152. };
  153. #define ROCKCHIP_CPUCLK_NUM_DIVIDERS 2
  154. struct rockchip_cpuclk_rate_table {
  155. unsigned long prate;
  156. struct rockchip_cpuclk_clksel divs[ROCKCHIP_CPUCLK_NUM_DIVIDERS];
  157. };
  158. /**
  159. * struct rockchip_cpuclk_reg_data: describes register offsets and masks of the cpuclock
  160. * @core_reg: register offset of the core settings register
  161. * @div_core_shift: core divider offset used to divide the pll value
  162. * @div_core_mask: core divider mask
  163. * @mux_core_shift: offset of the core multiplexer
  164. */
  165. struct rockchip_cpuclk_reg_data {
  166. int core_reg;
  167. u8 div_core_shift;
  168. u32 div_core_mask;
  169. int mux_core_reg;
  170. u8 mux_core_shift;
  171. };
  172. struct clk *rockchip_clk_register_cpuclk(const char *name,
  173. const char *const *parent_names, u8 num_parents,
  174. const struct rockchip_cpuclk_reg_data *reg_data,
  175. const struct rockchip_cpuclk_rate_table *rates,
  176. int nrates, void __iomem *reg_base, spinlock_t *lock);
  177. struct clk *rockchip_clk_register_mmc(const char *name,
  178. const char *const *parent_names, u8 num_parents,
  179. void __iomem *reg, int shift);
  180. #define ROCKCHIP_INVERTER_HIWORD_MASK BIT(0)
  181. struct clk *rockchip_clk_register_inverter(const char *name,
  182. const char *const *parent_names, u8 num_parents,
  183. void __iomem *reg, int shift, int flags,
  184. spinlock_t *lock);
  185. #define PNAME(x) static const char *const x[] __initconst
  186. enum rockchip_clk_branch_type {
  187. branch_composite,
  188. branch_mux,
  189. branch_divider,
  190. branch_fraction_divider,
  191. branch_gate,
  192. branch_mmc,
  193. branch_inverter,
  194. };
  195. struct rockchip_clk_branch {
  196. unsigned int id;
  197. enum rockchip_clk_branch_type branch_type;
  198. const char *name;
  199. const char *const *parent_names;
  200. u8 num_parents;
  201. unsigned long flags;
  202. int muxdiv_offset;
  203. u8 mux_shift;
  204. u8 mux_width;
  205. u8 mux_flags;
  206. u8 div_shift;
  207. u8 div_width;
  208. u8 div_flags;
  209. struct clk_div_table *div_table;
  210. int gate_offset;
  211. u8 gate_shift;
  212. u8 gate_flags;
  213. };
  214. #define COMPOSITE(_id, cname, pnames, f, mo, ms, mw, mf, ds, dw,\
  215. df, go, gs, gf) \
  216. { \
  217. .id = _id, \
  218. .branch_type = branch_composite, \
  219. .name = cname, \
  220. .parent_names = pnames, \
  221. .num_parents = ARRAY_SIZE(pnames), \
  222. .flags = f, \
  223. .muxdiv_offset = mo, \
  224. .mux_shift = ms, \
  225. .mux_width = mw, \
  226. .mux_flags = mf, \
  227. .div_shift = ds, \
  228. .div_width = dw, \
  229. .div_flags = df, \
  230. .gate_offset = go, \
  231. .gate_shift = gs, \
  232. .gate_flags = gf, \
  233. }
  234. #define COMPOSITE_NOMUX(_id, cname, pname, f, mo, ds, dw, df, \
  235. go, gs, gf) \
  236. { \
  237. .id = _id, \
  238. .branch_type = branch_composite, \
  239. .name = cname, \
  240. .parent_names = (const char *[]){ pname }, \
  241. .num_parents = 1, \
  242. .flags = f, \
  243. .muxdiv_offset = mo, \
  244. .div_shift = ds, \
  245. .div_width = dw, \
  246. .div_flags = df, \
  247. .gate_offset = go, \
  248. .gate_shift = gs, \
  249. .gate_flags = gf, \
  250. }
  251. #define COMPOSITE_NOMUX_DIVTBL(_id, cname, pname, f, mo, ds, dw,\
  252. df, dt, go, gs, gf) \
  253. { \
  254. .id = _id, \
  255. .branch_type = branch_composite, \
  256. .name = cname, \
  257. .parent_names = (const char *[]){ pname }, \
  258. .num_parents = 1, \
  259. .flags = f, \
  260. .muxdiv_offset = mo, \
  261. .div_shift = ds, \
  262. .div_width = dw, \
  263. .div_flags = df, \
  264. .div_table = dt, \
  265. .gate_offset = go, \
  266. .gate_shift = gs, \
  267. .gate_flags = gf, \
  268. }
  269. #define COMPOSITE_NODIV(_id, cname, pnames, f, mo, ms, mw, mf, \
  270. go, gs, gf) \
  271. { \
  272. .id = _id, \
  273. .branch_type = branch_composite, \
  274. .name = cname, \
  275. .parent_names = pnames, \
  276. .num_parents = ARRAY_SIZE(pnames), \
  277. .flags = f, \
  278. .muxdiv_offset = mo, \
  279. .mux_shift = ms, \
  280. .mux_width = mw, \
  281. .mux_flags = mf, \
  282. .gate_offset = go, \
  283. .gate_shift = gs, \
  284. .gate_flags = gf, \
  285. }
  286. #define COMPOSITE_NOGATE(_id, cname, pnames, f, mo, ms, mw, mf, \
  287. ds, dw, df) \
  288. { \
  289. .id = _id, \
  290. .branch_type = branch_composite, \
  291. .name = cname, \
  292. .parent_names = pnames, \
  293. .num_parents = ARRAY_SIZE(pnames), \
  294. .flags = f, \
  295. .muxdiv_offset = mo, \
  296. .mux_shift = ms, \
  297. .mux_width = mw, \
  298. .mux_flags = mf, \
  299. .div_shift = ds, \
  300. .div_width = dw, \
  301. .div_flags = df, \
  302. .gate_offset = -1, \
  303. }
  304. #define COMPOSITE_NOGATE_DIVTBL(_id, cname, pnames, f, mo, ms, \
  305. mw, mf, ds, dw, df, dt) \
  306. { \
  307. .id = _id, \
  308. .branch_type = branch_composite, \
  309. .name = cname, \
  310. .parent_names = pnames, \
  311. .num_parents = ARRAY_SIZE(pnames), \
  312. .flags = f, \
  313. .muxdiv_offset = mo, \
  314. .mux_shift = ms, \
  315. .mux_width = mw, \
  316. .mux_flags = mf, \
  317. .div_shift = ds, \
  318. .div_width = dw, \
  319. .div_flags = df, \
  320. .div_table = dt, \
  321. .gate_offset = -1, \
  322. }
  323. #define COMPOSITE_FRAC(_id, cname, pname, f, mo, df, go, gs, gf)\
  324. { \
  325. .id = _id, \
  326. .branch_type = branch_fraction_divider, \
  327. .name = cname, \
  328. .parent_names = (const char *[]){ pname }, \
  329. .num_parents = 1, \
  330. .flags = f, \
  331. .muxdiv_offset = mo, \
  332. .div_shift = 16, \
  333. .div_width = 16, \
  334. .div_flags = df, \
  335. .gate_offset = go, \
  336. .gate_shift = gs, \
  337. .gate_flags = gf, \
  338. }
  339. #define MUX(_id, cname, pnames, f, o, s, w, mf) \
  340. { \
  341. .id = _id, \
  342. .branch_type = branch_mux, \
  343. .name = cname, \
  344. .parent_names = pnames, \
  345. .num_parents = ARRAY_SIZE(pnames), \
  346. .flags = f, \
  347. .muxdiv_offset = o, \
  348. .mux_shift = s, \
  349. .mux_width = w, \
  350. .mux_flags = mf, \
  351. .gate_offset = -1, \
  352. }
  353. #define DIV(_id, cname, pname, f, o, s, w, df) \
  354. { \
  355. .id = _id, \
  356. .branch_type = branch_divider, \
  357. .name = cname, \
  358. .parent_names = (const char *[]){ pname }, \
  359. .num_parents = 1, \
  360. .flags = f, \
  361. .muxdiv_offset = o, \
  362. .div_shift = s, \
  363. .div_width = w, \
  364. .div_flags = df, \
  365. .gate_offset = -1, \
  366. }
  367. #define DIVTBL(_id, cname, pname, f, o, s, w, df, dt) \
  368. { \
  369. .id = _id, \
  370. .branch_type = branch_divider, \
  371. .name = cname, \
  372. .parent_names = (const char *[]){ pname }, \
  373. .num_parents = 1, \
  374. .flags = f, \
  375. .muxdiv_offset = o, \
  376. .div_shift = s, \
  377. .div_width = w, \
  378. .div_flags = df, \
  379. .div_table = dt, \
  380. }
  381. #define GATE(_id, cname, pname, f, o, b, gf) \
  382. { \
  383. .id = _id, \
  384. .branch_type = branch_gate, \
  385. .name = cname, \
  386. .parent_names = (const char *[]){ pname }, \
  387. .num_parents = 1, \
  388. .flags = f, \
  389. .gate_offset = o, \
  390. .gate_shift = b, \
  391. .gate_flags = gf, \
  392. }
  393. #define MMC(_id, cname, pname, offset, shift) \
  394. { \
  395. .id = _id, \
  396. .branch_type = branch_mmc, \
  397. .name = cname, \
  398. .parent_names = (const char *[]){ pname }, \
  399. .num_parents = 1, \
  400. .muxdiv_offset = offset, \
  401. .div_shift = shift, \
  402. }
  403. #define INVERTER(_id, cname, pname, io, is, if) \
  404. { \
  405. .id = _id, \
  406. .branch_type = branch_inverter, \
  407. .name = cname, \
  408. .parent_names = (const char *[]){ pname }, \
  409. .num_parents = 1, \
  410. .muxdiv_offset = io, \
  411. .div_shift = is, \
  412. .div_flags = if, \
  413. }
  414. void rockchip_clk_init(struct device_node *np, void __iomem *base,
  415. unsigned long nr_clks);
  416. struct regmap *rockchip_clk_get_grf(void);
  417. void rockchip_clk_add_lookup(struct clk *clk, unsigned int id);
  418. void rockchip_clk_register_branches(struct rockchip_clk_branch *clk_list,
  419. unsigned int nr_clk);
  420. void rockchip_clk_register_plls(struct rockchip_pll_clock *pll_list,
  421. unsigned int nr_pll, int grf_lock_offset);
  422. void rockchip_clk_register_armclk(unsigned int lookup_id, const char *name,
  423. const char *const *parent_names, u8 num_parents,
  424. const struct rockchip_cpuclk_reg_data *reg_data,
  425. const struct rockchip_cpuclk_rate_table *rates,
  426. int nrates);
  427. void rockchip_clk_protect_critical(const char *const clocks[], int nclocks);
  428. void rockchip_register_restart_notifier(unsigned int reg);
  429. #define ROCKCHIP_SOFTRST_HIWORD_MASK BIT(0)
  430. #ifdef CONFIG_RESET_CONTROLLER
  431. void rockchip_register_softrst(struct device_node *np,
  432. unsigned int num_regs,
  433. void __iomem *base, u8 flags);
  434. #else
  435. static inline void rockchip_register_softrst(struct device_node *np,
  436. unsigned int num_regs,
  437. void __iomem *base, u8 flags)
  438. {
  439. }
  440. #endif
  441. #endif