clk.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. * Copyright (c) 2014 MundoReader S.L.
  3. * Author: Heiko Stuebner <heiko@sntech.de>
  4. *
  5. * Copyright (c) 2016 Rockchip Electronics Co. Ltd.
  6. * Author: Xing Zheng <zhengxing@rock-chips.com>
  7. *
  8. * based on
  9. *
  10. * samsung/clk.c
  11. * Copyright (c) 2013 Samsung Electronics Co., Ltd.
  12. * Copyright (c) 2013 Linaro Ltd.
  13. * Author: Thomas Abraham <thomas.ab@samsung.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. */
  25. #include <linux/slab.h>
  26. #include <linux/clk.h>
  27. #include <linux/clk-provider.h>
  28. #include <linux/mfd/syscon.h>
  29. #include <linux/regmap.h>
  30. #include <linux/reboot.h>
  31. #include <linux/rational.h>
  32. #include "clk.h"
  33. /**
  34. * Register a clock branch.
  35. * Most clock branches have a form like
  36. *
  37. * src1 --|--\
  38. * |M |--[GATE]-[DIV]-
  39. * src2 --|--/
  40. *
  41. * sometimes without one of those components.
  42. */
  43. static struct clk *rockchip_clk_register_branch(const char *name,
  44. const char *const *parent_names, u8 num_parents,
  45. void __iomem *base,
  46. int muxdiv_offset, u8 mux_shift, u8 mux_width, u8 mux_flags,
  47. u8 div_shift, u8 div_width, u8 div_flags,
  48. struct clk_div_table *div_table, int gate_offset,
  49. u8 gate_shift, u8 gate_flags, unsigned long flags,
  50. spinlock_t *lock)
  51. {
  52. struct clk *clk;
  53. struct clk_mux *mux = NULL;
  54. struct clk_gate *gate = NULL;
  55. struct clk_divider *div = NULL;
  56. const struct clk_ops *mux_ops = NULL, *div_ops = NULL,
  57. *gate_ops = NULL;
  58. if (num_parents > 1) {
  59. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  60. if (!mux)
  61. return ERR_PTR(-ENOMEM);
  62. mux->reg = base + muxdiv_offset;
  63. mux->shift = mux_shift;
  64. mux->mask = BIT(mux_width) - 1;
  65. mux->flags = mux_flags;
  66. mux->lock = lock;
  67. mux_ops = (mux_flags & CLK_MUX_READ_ONLY) ? &clk_mux_ro_ops
  68. : &clk_mux_ops;
  69. }
  70. if (gate_offset >= 0) {
  71. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  72. if (!gate)
  73. goto err_gate;
  74. gate->flags = gate_flags;
  75. gate->reg = base + gate_offset;
  76. gate->bit_idx = gate_shift;
  77. gate->lock = lock;
  78. gate_ops = &clk_gate_ops;
  79. }
  80. if (div_width > 0) {
  81. div = kzalloc(sizeof(*div), GFP_KERNEL);
  82. if (!div)
  83. goto err_div;
  84. div->flags = div_flags;
  85. div->reg = base + muxdiv_offset;
  86. div->shift = div_shift;
  87. div->width = div_width;
  88. div->lock = lock;
  89. div->table = div_table;
  90. div_ops = (div_flags & CLK_DIVIDER_READ_ONLY)
  91. ? &clk_divider_ro_ops
  92. : &clk_divider_ops;
  93. }
  94. clk = clk_register_composite(NULL, name, parent_names, num_parents,
  95. mux ? &mux->hw : NULL, mux_ops,
  96. div ? &div->hw : NULL, div_ops,
  97. gate ? &gate->hw : NULL, gate_ops,
  98. flags);
  99. return clk;
  100. err_div:
  101. kfree(gate);
  102. err_gate:
  103. kfree(mux);
  104. return ERR_PTR(-ENOMEM);
  105. }
  106. struct rockchip_clk_frac {
  107. struct notifier_block clk_nb;
  108. struct clk_fractional_divider div;
  109. struct clk_gate gate;
  110. struct clk_mux mux;
  111. const struct clk_ops *mux_ops;
  112. int mux_frac_idx;
  113. bool rate_change_remuxed;
  114. int rate_change_idx;
  115. };
  116. #define to_rockchip_clk_frac_nb(nb) \
  117. container_of(nb, struct rockchip_clk_frac, clk_nb)
  118. static int rockchip_clk_frac_notifier_cb(struct notifier_block *nb,
  119. unsigned long event, void *data)
  120. {
  121. struct clk_notifier_data *ndata = data;
  122. struct rockchip_clk_frac *frac = to_rockchip_clk_frac_nb(nb);
  123. struct clk_mux *frac_mux = &frac->mux;
  124. int ret = 0;
  125. pr_debug("%s: event %lu, old_rate %lu, new_rate: %lu\n",
  126. __func__, event, ndata->old_rate, ndata->new_rate);
  127. if (event == PRE_RATE_CHANGE) {
  128. frac->rate_change_idx =
  129. frac->mux_ops->get_parent(&frac_mux->hw);
  130. if (frac->rate_change_idx != frac->mux_frac_idx) {
  131. frac->mux_ops->set_parent(&frac_mux->hw,
  132. frac->mux_frac_idx);
  133. frac->rate_change_remuxed = 1;
  134. }
  135. } else if (event == POST_RATE_CHANGE) {
  136. /*
  137. * The POST_RATE_CHANGE notifier runs directly after the
  138. * divider clock is set in clk_change_rate, so we'll have
  139. * remuxed back to the original parent before clk_change_rate
  140. * reaches the mux itself.
  141. */
  142. if (frac->rate_change_remuxed) {
  143. frac->mux_ops->set_parent(&frac_mux->hw,
  144. frac->rate_change_idx);
  145. frac->rate_change_remuxed = 0;
  146. }
  147. }
  148. return notifier_from_errno(ret);
  149. }
  150. /**
  151. * fractional divider must set that denominator is 20 times larger than
  152. * numerator to generate precise clock frequency.
  153. */
  154. static void rockchip_fractional_approximation(struct clk_hw *hw,
  155. unsigned long rate, unsigned long *parent_rate,
  156. unsigned long *m, unsigned long *n)
  157. {
  158. struct clk_fractional_divider *fd = to_clk_fd(hw);
  159. unsigned long p_rate, p_parent_rate;
  160. struct clk_hw *p_parent;
  161. unsigned long scale;
  162. p_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
  163. if ((rate * 20 > p_rate) && (p_rate % rate != 0)) {
  164. p_parent = clk_hw_get_parent(clk_hw_get_parent(hw));
  165. p_parent_rate = clk_hw_get_rate(p_parent);
  166. *parent_rate = p_parent_rate;
  167. }
  168. /*
  169. * Get rate closer to *parent_rate to guarantee there is no overflow
  170. * for m and n. In the result it will be the nearest rate left shifted
  171. * by (scale - fd->nwidth) bits.
  172. */
  173. scale = fls_long(*parent_rate / rate - 1);
  174. if (scale > fd->nwidth)
  175. rate <<= scale - fd->nwidth;
  176. rational_best_approximation(rate, *parent_rate,
  177. GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
  178. m, n);
  179. }
  180. static struct clk *rockchip_clk_register_frac_branch(
  181. struct rockchip_clk_provider *ctx, const char *name,
  182. const char *const *parent_names, u8 num_parents,
  183. void __iomem *base, int muxdiv_offset, u8 div_flags,
  184. int gate_offset, u8 gate_shift, u8 gate_flags,
  185. unsigned long flags, struct rockchip_clk_branch *child,
  186. spinlock_t *lock)
  187. {
  188. struct rockchip_clk_frac *frac;
  189. struct clk *clk;
  190. struct clk_gate *gate = NULL;
  191. struct clk_fractional_divider *div = NULL;
  192. const struct clk_ops *div_ops = NULL, *gate_ops = NULL;
  193. if (muxdiv_offset < 0)
  194. return ERR_PTR(-EINVAL);
  195. if (child && child->branch_type != branch_mux) {
  196. pr_err("%s: fractional child clock for %s can only be a mux\n",
  197. __func__, name);
  198. return ERR_PTR(-EINVAL);
  199. }
  200. frac = kzalloc(sizeof(*frac), GFP_KERNEL);
  201. if (!frac)
  202. return ERR_PTR(-ENOMEM);
  203. if (gate_offset >= 0) {
  204. gate = &frac->gate;
  205. gate->flags = gate_flags;
  206. gate->reg = base + gate_offset;
  207. gate->bit_idx = gate_shift;
  208. gate->lock = lock;
  209. gate_ops = &clk_gate_ops;
  210. }
  211. div = &frac->div;
  212. div->flags = div_flags;
  213. div->reg = base + muxdiv_offset;
  214. div->mshift = 16;
  215. div->mwidth = 16;
  216. div->mmask = GENMASK(div->mwidth - 1, 0) << div->mshift;
  217. div->nshift = 0;
  218. div->nwidth = 16;
  219. div->nmask = GENMASK(div->nwidth - 1, 0) << div->nshift;
  220. div->lock = lock;
  221. div->approximation = rockchip_fractional_approximation;
  222. div_ops = &clk_fractional_divider_ops;
  223. clk = clk_register_composite(NULL, name, parent_names, num_parents,
  224. NULL, NULL,
  225. &div->hw, div_ops,
  226. gate ? &gate->hw : NULL, gate_ops,
  227. flags | CLK_SET_RATE_UNGATE);
  228. if (IS_ERR(clk)) {
  229. kfree(frac);
  230. return clk;
  231. }
  232. if (child) {
  233. struct clk_mux *frac_mux = &frac->mux;
  234. struct clk_init_data init;
  235. struct clk *mux_clk;
  236. int i, ret;
  237. frac->mux_frac_idx = -1;
  238. for (i = 0; i < child->num_parents; i++) {
  239. if (!strcmp(name, child->parent_names[i])) {
  240. pr_debug("%s: found fractional parent in mux at pos %d\n",
  241. __func__, i);
  242. frac->mux_frac_idx = i;
  243. break;
  244. }
  245. }
  246. frac->mux_ops = &clk_mux_ops;
  247. frac->clk_nb.notifier_call = rockchip_clk_frac_notifier_cb;
  248. frac_mux->reg = base + child->muxdiv_offset;
  249. frac_mux->shift = child->mux_shift;
  250. frac_mux->mask = BIT(child->mux_width) - 1;
  251. frac_mux->flags = child->mux_flags;
  252. frac_mux->lock = lock;
  253. frac_mux->hw.init = &init;
  254. init.name = child->name;
  255. init.flags = child->flags | CLK_SET_RATE_PARENT;
  256. init.ops = frac->mux_ops;
  257. init.parent_names = child->parent_names;
  258. init.num_parents = child->num_parents;
  259. mux_clk = clk_register(NULL, &frac_mux->hw);
  260. if (IS_ERR(mux_clk))
  261. return clk;
  262. rockchip_clk_add_lookup(ctx, mux_clk, child->id);
  263. /* notifier on the fraction divider to catch rate changes */
  264. if (frac->mux_frac_idx >= 0) {
  265. ret = clk_notifier_register(clk, &frac->clk_nb);
  266. if (ret)
  267. pr_err("%s: failed to register clock notifier for %s\n",
  268. __func__, name);
  269. } else {
  270. pr_warn("%s: could not find %s as parent of %s, rate changes may not work\n",
  271. __func__, name, child->name);
  272. }
  273. }
  274. return clk;
  275. }
  276. static struct clk *rockchip_clk_register_factor_branch(const char *name,
  277. const char *const *parent_names, u8 num_parents,
  278. void __iomem *base, unsigned int mult, unsigned int div,
  279. int gate_offset, u8 gate_shift, u8 gate_flags,
  280. unsigned long flags, spinlock_t *lock)
  281. {
  282. struct clk *clk;
  283. struct clk_gate *gate = NULL;
  284. struct clk_fixed_factor *fix = NULL;
  285. /* without gate, register a simple factor clock */
  286. if (gate_offset == 0) {
  287. return clk_register_fixed_factor(NULL, name,
  288. parent_names[0], flags, mult,
  289. div);
  290. }
  291. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  292. if (!gate)
  293. return ERR_PTR(-ENOMEM);
  294. gate->flags = gate_flags;
  295. gate->reg = base + gate_offset;
  296. gate->bit_idx = gate_shift;
  297. gate->lock = lock;
  298. fix = kzalloc(sizeof(*fix), GFP_KERNEL);
  299. if (!fix) {
  300. kfree(gate);
  301. return ERR_PTR(-ENOMEM);
  302. }
  303. fix->mult = mult;
  304. fix->div = div;
  305. clk = clk_register_composite(NULL, name, parent_names, num_parents,
  306. NULL, NULL,
  307. &fix->hw, &clk_fixed_factor_ops,
  308. &gate->hw, &clk_gate_ops, flags);
  309. if (IS_ERR(clk)) {
  310. kfree(fix);
  311. kfree(gate);
  312. }
  313. return clk;
  314. }
  315. struct rockchip_clk_provider * __init rockchip_clk_init(struct device_node *np,
  316. void __iomem *base, unsigned long nr_clks)
  317. {
  318. struct rockchip_clk_provider *ctx;
  319. struct clk **clk_table;
  320. int i;
  321. ctx = kzalloc(sizeof(struct rockchip_clk_provider), GFP_KERNEL);
  322. if (!ctx)
  323. return ERR_PTR(-ENOMEM);
  324. clk_table = kcalloc(nr_clks, sizeof(struct clk *), GFP_KERNEL);
  325. if (!clk_table)
  326. goto err_free;
  327. for (i = 0; i < nr_clks; ++i)
  328. clk_table[i] = ERR_PTR(-ENOENT);
  329. ctx->reg_base = base;
  330. ctx->clk_data.clks = clk_table;
  331. ctx->clk_data.clk_num = nr_clks;
  332. ctx->cru_node = np;
  333. spin_lock_init(&ctx->lock);
  334. ctx->grf = syscon_regmap_lookup_by_phandle(ctx->cru_node,
  335. "rockchip,grf");
  336. return ctx;
  337. err_free:
  338. kfree(ctx);
  339. return ERR_PTR(-ENOMEM);
  340. }
  341. void __init rockchip_clk_of_add_provider(struct device_node *np,
  342. struct rockchip_clk_provider *ctx)
  343. {
  344. if (of_clk_add_provider(np, of_clk_src_onecell_get,
  345. &ctx->clk_data))
  346. pr_err("%s: could not register clk provider\n", __func__);
  347. }
  348. void rockchip_clk_add_lookup(struct rockchip_clk_provider *ctx,
  349. struct clk *clk, unsigned int id)
  350. {
  351. if (ctx->clk_data.clks && id)
  352. ctx->clk_data.clks[id] = clk;
  353. }
  354. void __init rockchip_clk_register_plls(struct rockchip_clk_provider *ctx,
  355. struct rockchip_pll_clock *list,
  356. unsigned int nr_pll, int grf_lock_offset)
  357. {
  358. struct clk *clk;
  359. int idx;
  360. for (idx = 0; idx < nr_pll; idx++, list++) {
  361. clk = rockchip_clk_register_pll(ctx, list->type, list->name,
  362. list->parent_names, list->num_parents,
  363. list->con_offset, grf_lock_offset,
  364. list->lock_shift, list->mode_offset,
  365. list->mode_shift, list->rate_table,
  366. list->flags, list->pll_flags);
  367. if (IS_ERR(clk)) {
  368. pr_err("%s: failed to register clock %s\n", __func__,
  369. list->name);
  370. continue;
  371. }
  372. rockchip_clk_add_lookup(ctx, clk, list->id);
  373. }
  374. }
  375. void __init rockchip_clk_register_branches(
  376. struct rockchip_clk_provider *ctx,
  377. struct rockchip_clk_branch *list,
  378. unsigned int nr_clk)
  379. {
  380. struct clk *clk = NULL;
  381. unsigned int idx;
  382. unsigned long flags;
  383. for (idx = 0; idx < nr_clk; idx++, list++) {
  384. flags = list->flags;
  385. /* catch simple muxes */
  386. switch (list->branch_type) {
  387. case branch_mux:
  388. clk = clk_register_mux(NULL, list->name,
  389. list->parent_names, list->num_parents,
  390. flags, ctx->reg_base + list->muxdiv_offset,
  391. list->mux_shift, list->mux_width,
  392. list->mux_flags, &ctx->lock);
  393. break;
  394. case branch_muxgrf:
  395. clk = rockchip_clk_register_muxgrf(list->name,
  396. list->parent_names, list->num_parents,
  397. flags, ctx->grf, list->muxdiv_offset,
  398. list->mux_shift, list->mux_width,
  399. list->mux_flags);
  400. break;
  401. case branch_divider:
  402. if (list->div_table)
  403. clk = clk_register_divider_table(NULL,
  404. list->name, list->parent_names[0],
  405. flags,
  406. ctx->reg_base + list->muxdiv_offset,
  407. list->div_shift, list->div_width,
  408. list->div_flags, list->div_table,
  409. &ctx->lock);
  410. else
  411. clk = clk_register_divider(NULL, list->name,
  412. list->parent_names[0], flags,
  413. ctx->reg_base + list->muxdiv_offset,
  414. list->div_shift, list->div_width,
  415. list->div_flags, &ctx->lock);
  416. break;
  417. case branch_fraction_divider:
  418. clk = rockchip_clk_register_frac_branch(ctx, list->name,
  419. list->parent_names, list->num_parents,
  420. ctx->reg_base, list->muxdiv_offset,
  421. list->div_flags,
  422. list->gate_offset, list->gate_shift,
  423. list->gate_flags, flags, list->child,
  424. &ctx->lock);
  425. break;
  426. case branch_gate:
  427. flags |= CLK_SET_RATE_PARENT;
  428. clk = clk_register_gate(NULL, list->name,
  429. list->parent_names[0], flags,
  430. ctx->reg_base + list->gate_offset,
  431. list->gate_shift, list->gate_flags, &ctx->lock);
  432. break;
  433. case branch_composite:
  434. clk = rockchip_clk_register_branch(list->name,
  435. list->parent_names, list->num_parents,
  436. ctx->reg_base, list->muxdiv_offset,
  437. list->mux_shift,
  438. list->mux_width, list->mux_flags,
  439. list->div_shift, list->div_width,
  440. list->div_flags, list->div_table,
  441. list->gate_offset, list->gate_shift,
  442. list->gate_flags, flags, &ctx->lock);
  443. break;
  444. case branch_mmc:
  445. clk = rockchip_clk_register_mmc(
  446. list->name,
  447. list->parent_names, list->num_parents,
  448. ctx->reg_base + list->muxdiv_offset,
  449. list->div_shift
  450. );
  451. break;
  452. case branch_inverter:
  453. clk = rockchip_clk_register_inverter(
  454. list->name, list->parent_names,
  455. list->num_parents,
  456. ctx->reg_base + list->muxdiv_offset,
  457. list->div_shift, list->div_flags, &ctx->lock);
  458. break;
  459. case branch_factor:
  460. clk = rockchip_clk_register_factor_branch(
  461. list->name, list->parent_names,
  462. list->num_parents, ctx->reg_base,
  463. list->div_shift, list->div_width,
  464. list->gate_offset, list->gate_shift,
  465. list->gate_flags, flags, &ctx->lock);
  466. break;
  467. case branch_ddrclk:
  468. clk = rockchip_clk_register_ddrclk(
  469. list->name, list->flags,
  470. list->parent_names, list->num_parents,
  471. list->muxdiv_offset, list->mux_shift,
  472. list->mux_width, list->div_shift,
  473. list->div_width, list->div_flags,
  474. ctx->reg_base, &ctx->lock);
  475. break;
  476. }
  477. /* none of the cases above matched */
  478. if (!clk) {
  479. pr_err("%s: unknown clock type %d\n",
  480. __func__, list->branch_type);
  481. continue;
  482. }
  483. if (IS_ERR(clk)) {
  484. pr_err("%s: failed to register clock %s: %ld\n",
  485. __func__, list->name, PTR_ERR(clk));
  486. continue;
  487. }
  488. rockchip_clk_add_lookup(ctx, clk, list->id);
  489. }
  490. }
  491. void __init rockchip_clk_register_armclk(struct rockchip_clk_provider *ctx,
  492. unsigned int lookup_id,
  493. const char *name, const char *const *parent_names,
  494. u8 num_parents,
  495. const struct rockchip_cpuclk_reg_data *reg_data,
  496. const struct rockchip_cpuclk_rate_table *rates,
  497. int nrates)
  498. {
  499. struct clk *clk;
  500. clk = rockchip_clk_register_cpuclk(name, parent_names, num_parents,
  501. reg_data, rates, nrates,
  502. ctx->reg_base, &ctx->lock);
  503. if (IS_ERR(clk)) {
  504. pr_err("%s: failed to register clock %s: %ld\n",
  505. __func__, name, PTR_ERR(clk));
  506. return;
  507. }
  508. rockchip_clk_add_lookup(ctx, clk, lookup_id);
  509. }
  510. void __init rockchip_clk_protect_critical(const char *const clocks[],
  511. int nclocks)
  512. {
  513. int i;
  514. /* Protect the clocks that needs to stay on */
  515. for (i = 0; i < nclocks; i++) {
  516. struct clk *clk = __clk_lookup(clocks[i]);
  517. if (clk)
  518. clk_prepare_enable(clk);
  519. }
  520. }
  521. static void __iomem *rst_base;
  522. static unsigned int reg_restart;
  523. static void (*cb_restart)(void);
  524. static int rockchip_restart_notify(struct notifier_block *this,
  525. unsigned long mode, void *cmd)
  526. {
  527. if (cb_restart)
  528. cb_restart();
  529. writel(0xfdb9, rst_base + reg_restart);
  530. return NOTIFY_DONE;
  531. }
  532. static struct notifier_block rockchip_restart_handler = {
  533. .notifier_call = rockchip_restart_notify,
  534. .priority = 128,
  535. };
  536. void __init
  537. rockchip_register_restart_notifier(struct rockchip_clk_provider *ctx,
  538. unsigned int reg,
  539. void (*cb)(void))
  540. {
  541. int ret;
  542. rst_base = ctx->reg_base;
  543. reg_restart = reg;
  544. cb_restart = cb;
  545. ret = register_restart_handler(&rockchip_restart_handler);
  546. if (ret)
  547. pr_err("%s: cannot register restart handler, %d\n",
  548. __func__, ret);
  549. }