clk-rcar-gen2.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * rcar_gen2 Core CPG Clocks
  3. *
  4. * Copyright (C) 2013 Ideas On Board SPRL
  5. *
  6. * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/clkdev.h>
  14. #include <linux/clk/shmobile.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/math64.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/spinlock.h>
  21. struct rcar_gen2_cpg {
  22. struct clk_onecell_data data;
  23. spinlock_t lock;
  24. void __iomem *reg;
  25. };
  26. #define CPG_SDCKCR 0x00000074
  27. #define CPG_PLL0CR 0x000000d8
  28. #define CPG_FRQCRC 0x000000e0
  29. #define CPG_FRQCRC_ZFC_MASK (0x1f << 8)
  30. #define CPG_FRQCRC_ZFC_SHIFT 8
  31. /* -----------------------------------------------------------------------------
  32. * Z Clock
  33. *
  34. * Traits of this clock:
  35. * prepare - clk_prepare only ensures that parents are prepared
  36. * enable - clk_enable only ensures that parents are enabled
  37. * rate - rate is adjustable. clk->rate = parent->rate * mult / 32
  38. * parent - fixed parent. No clk_set_parent support
  39. */
  40. struct cpg_z_clk {
  41. struct clk_hw hw;
  42. void __iomem *reg;
  43. };
  44. #define to_z_clk(_hw) container_of(_hw, struct cpg_z_clk, hw)
  45. static unsigned long cpg_z_clk_recalc_rate(struct clk_hw *hw,
  46. unsigned long parent_rate)
  47. {
  48. struct cpg_z_clk *zclk = to_z_clk(hw);
  49. unsigned int mult;
  50. unsigned int val;
  51. val = (clk_readl(zclk->reg) & CPG_FRQCRC_ZFC_MASK)
  52. >> CPG_FRQCRC_ZFC_SHIFT;
  53. mult = 32 - val;
  54. return div_u64((u64)parent_rate * mult, 32);
  55. }
  56. static long cpg_z_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  57. unsigned long *parent_rate)
  58. {
  59. unsigned long prate = *parent_rate;
  60. unsigned int mult;
  61. if (!prate)
  62. prate = 1;
  63. mult = div_u64((u64)rate * 32, prate);
  64. mult = clamp(mult, 1U, 32U);
  65. return *parent_rate / 32 * mult;
  66. }
  67. static int cpg_z_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  68. unsigned long parent_rate)
  69. {
  70. struct cpg_z_clk *zclk = to_z_clk(hw);
  71. unsigned int mult;
  72. u32 val;
  73. mult = div_u64((u64)rate * 32, parent_rate);
  74. mult = clamp(mult, 1U, 32U);
  75. val = clk_readl(zclk->reg);
  76. val &= ~CPG_FRQCRC_ZFC_MASK;
  77. val |= (32 - mult) << CPG_FRQCRC_ZFC_SHIFT;
  78. clk_writel(val, zclk->reg);
  79. return 0;
  80. }
  81. static const struct clk_ops cpg_z_clk_ops = {
  82. .recalc_rate = cpg_z_clk_recalc_rate,
  83. .round_rate = cpg_z_clk_round_rate,
  84. .set_rate = cpg_z_clk_set_rate,
  85. };
  86. static struct clk * __init cpg_z_clk_register(struct rcar_gen2_cpg *cpg)
  87. {
  88. static const char *parent_name = "pll0";
  89. struct clk_init_data init;
  90. struct cpg_z_clk *zclk;
  91. struct clk *clk;
  92. zclk = kzalloc(sizeof(*zclk), GFP_KERNEL);
  93. if (!zclk)
  94. return ERR_PTR(-ENOMEM);
  95. init.name = "z";
  96. init.ops = &cpg_z_clk_ops;
  97. init.flags = 0;
  98. init.parent_names = &parent_name;
  99. init.num_parents = 1;
  100. zclk->reg = cpg->reg + CPG_FRQCRC;
  101. zclk->hw.init = &init;
  102. clk = clk_register(NULL, &zclk->hw);
  103. if (IS_ERR(clk))
  104. kfree(zclk);
  105. return clk;
  106. }
  107. /* -----------------------------------------------------------------------------
  108. * CPG Clock Data
  109. */
  110. /*
  111. * MD EXTAL PLL0 PLL1 PLL3
  112. * 14 13 19 (MHz) *1 *1
  113. *---------------------------------------------------
  114. * 0 0 0 15 x 1 x172/2 x208/2 x106
  115. * 0 0 1 15 x 1 x172/2 x208/2 x88
  116. * 0 1 0 20 x 1 x130/2 x156/2 x80
  117. * 0 1 1 20 x 1 x130/2 x156/2 x66
  118. * 1 0 0 26 / 2 x200/2 x240/2 x122
  119. * 1 0 1 26 / 2 x200/2 x240/2 x102
  120. * 1 1 0 30 / 2 x172/2 x208/2 x106
  121. * 1 1 1 30 / 2 x172/2 x208/2 x88
  122. *
  123. * *1 : Table 7.6 indicates VCO ouput (PLLx = VCO/2)
  124. */
  125. #define CPG_PLL_CONFIG_INDEX(md) ((((md) & BIT(14)) >> 12) | \
  126. (((md) & BIT(13)) >> 12) | \
  127. (((md) & BIT(19)) >> 19))
  128. struct cpg_pll_config {
  129. unsigned int extal_div;
  130. unsigned int pll1_mult;
  131. unsigned int pll3_mult;
  132. };
  133. static const struct cpg_pll_config cpg_pll_configs[8] __initconst = {
  134. { 1, 208, 106 }, { 1, 208, 88 }, { 1, 156, 80 }, { 1, 156, 66 },
  135. { 2, 240, 122 }, { 2, 240, 102 }, { 2, 208, 106 }, { 2, 208, 88 },
  136. };
  137. /* SDHI divisors */
  138. static const struct clk_div_table cpg_sdh_div_table[] = {
  139. { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 },
  140. { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 },
  141. { 8, 24 }, { 10, 36 }, { 11, 48 }, { 0, 0 },
  142. };
  143. static const struct clk_div_table cpg_sd01_div_table[] = {
  144. { 5, 12 }, { 6, 16 }, { 7, 18 }, { 8, 24 },
  145. { 10, 36 }, { 11, 48 }, { 12, 10 }, { 0, 0 },
  146. };
  147. /* -----------------------------------------------------------------------------
  148. * Initialization
  149. */
  150. static u32 cpg_mode __initdata;
  151. static struct clk * __init
  152. rcar_gen2_cpg_register_clock(struct device_node *np, struct rcar_gen2_cpg *cpg,
  153. const struct cpg_pll_config *config,
  154. const char *name)
  155. {
  156. const struct clk_div_table *table = NULL;
  157. const char *parent_name = "main";
  158. unsigned int shift;
  159. unsigned int mult = 1;
  160. unsigned int div = 1;
  161. if (!strcmp(name, "main")) {
  162. parent_name = of_clk_get_parent_name(np, 0);
  163. div = config->extal_div;
  164. } else if (!strcmp(name, "pll0")) {
  165. /* PLL0 is a configurable multiplier clock. Register it as a
  166. * fixed factor clock for now as there's no generic multiplier
  167. * clock implementation and we currently have no need to change
  168. * the multiplier value.
  169. */
  170. u32 value = clk_readl(cpg->reg + CPG_PLL0CR);
  171. mult = ((value >> 24) & ((1 << 7) - 1)) + 1;
  172. } else if (!strcmp(name, "pll1")) {
  173. mult = config->pll1_mult / 2;
  174. } else if (!strcmp(name, "pll3")) {
  175. mult = config->pll3_mult;
  176. } else if (!strcmp(name, "lb")) {
  177. div = cpg_mode & BIT(18) ? 36 : 24;
  178. } else if (!strcmp(name, "qspi")) {
  179. div = (cpg_mode & (BIT(3) | BIT(2) | BIT(1))) == BIT(2)
  180. ? 16 : 20;
  181. } else if (!strcmp(name, "sdh")) {
  182. table = cpg_sdh_div_table;
  183. shift = 8;
  184. } else if (!strcmp(name, "sd0")) {
  185. table = cpg_sd01_div_table;
  186. shift = 4;
  187. } else if (!strcmp(name, "sd1")) {
  188. table = cpg_sd01_div_table;
  189. shift = 0;
  190. } else if (!strcmp(name, "z")) {
  191. return cpg_z_clk_register(cpg);
  192. } else {
  193. return ERR_PTR(-EINVAL);
  194. }
  195. if (!table)
  196. return clk_register_fixed_factor(NULL, name, parent_name, 0,
  197. mult, div);
  198. else
  199. return clk_register_divider_table(NULL, name, parent_name, 0,
  200. cpg->reg + CPG_SDCKCR, shift,
  201. 4, 0, table, &cpg->lock);
  202. }
  203. static void __init rcar_gen2_cpg_clocks_init(struct device_node *np)
  204. {
  205. const struct cpg_pll_config *config;
  206. struct rcar_gen2_cpg *cpg;
  207. struct clk **clks;
  208. unsigned int i;
  209. int num_clks;
  210. num_clks = of_property_count_strings(np, "clock-output-names");
  211. if (num_clks < 0) {
  212. pr_err("%s: failed to count clocks\n", __func__);
  213. return;
  214. }
  215. cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);
  216. clks = kzalloc(num_clks * sizeof(*clks), GFP_KERNEL);
  217. if (cpg == NULL || clks == NULL) {
  218. /* We're leaking memory on purpose, there's no point in cleaning
  219. * up as the system won't boot anyway.
  220. */
  221. pr_err("%s: failed to allocate cpg\n", __func__);
  222. return;
  223. }
  224. spin_lock_init(&cpg->lock);
  225. cpg->data.clks = clks;
  226. cpg->data.clk_num = num_clks;
  227. cpg->reg = of_iomap(np, 0);
  228. if (WARN_ON(cpg->reg == NULL))
  229. return;
  230. config = &cpg_pll_configs[CPG_PLL_CONFIG_INDEX(cpg_mode)];
  231. for (i = 0; i < num_clks; ++i) {
  232. const char *name;
  233. struct clk *clk;
  234. of_property_read_string_index(np, "clock-output-names", i,
  235. &name);
  236. clk = rcar_gen2_cpg_register_clock(np, cpg, config, name);
  237. if (IS_ERR(clk))
  238. pr_err("%s: failed to register %s %s clock (%ld)\n",
  239. __func__, np->name, name, PTR_ERR(clk));
  240. else
  241. cpg->data.clks[i] = clk;
  242. }
  243. of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
  244. }
  245. CLK_OF_DECLARE(rcar_gen2_cpg_clks, "renesas,rcar-gen2-cpg-clocks",
  246. rcar_gen2_cpg_clocks_init);
  247. void __init rcar_gen2_clocks_init(u32 mode)
  248. {
  249. cpg_mode = mode;
  250. of_clk_init(NULL);
  251. }