clk.c 15 KB

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