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