clk-rcar-gen2.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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_FRQCRB 0x00000004
  27. #define CPG_FRQCRB_KICK BIT(31)
  28. #define CPG_SDCKCR 0x00000074
  29. #define CPG_PLL0CR 0x000000d8
  30. #define CPG_FRQCRC 0x000000e0
  31. #define CPG_FRQCRC_ZFC_MASK (0x1f << 8)
  32. #define CPG_FRQCRC_ZFC_SHIFT 8
  33. /* -----------------------------------------------------------------------------
  34. * Z Clock
  35. *
  36. * Traits of this clock:
  37. * prepare - clk_prepare only ensures that parents are prepared
  38. * enable - clk_enable only ensures that parents are enabled
  39. * rate - rate is adjustable. clk->rate = parent->rate * mult / 32
  40. * parent - fixed parent. No clk_set_parent support
  41. */
  42. struct cpg_z_clk {
  43. struct clk_hw hw;
  44. void __iomem *reg;
  45. void __iomem *kick_reg;
  46. };
  47. #define to_z_clk(_hw) container_of(_hw, struct cpg_z_clk, hw)
  48. static unsigned long cpg_z_clk_recalc_rate(struct clk_hw *hw,
  49. unsigned long parent_rate)
  50. {
  51. struct cpg_z_clk *zclk = to_z_clk(hw);
  52. unsigned int mult;
  53. unsigned int val;
  54. val = (clk_readl(zclk->reg) & CPG_FRQCRC_ZFC_MASK)
  55. >> CPG_FRQCRC_ZFC_SHIFT;
  56. mult = 32 - val;
  57. return div_u64((u64)parent_rate * mult, 32);
  58. }
  59. static long cpg_z_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  60. unsigned long *parent_rate)
  61. {
  62. unsigned long prate = *parent_rate;
  63. unsigned int mult;
  64. if (!prate)
  65. prate = 1;
  66. mult = div_u64((u64)rate * 32, prate);
  67. mult = clamp(mult, 1U, 32U);
  68. return *parent_rate / 32 * mult;
  69. }
  70. static int cpg_z_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  71. unsigned long parent_rate)
  72. {
  73. struct cpg_z_clk *zclk = to_z_clk(hw);
  74. unsigned int mult;
  75. u32 val, kick;
  76. unsigned int i;
  77. mult = div_u64((u64)rate * 32, parent_rate);
  78. mult = clamp(mult, 1U, 32U);
  79. if (clk_readl(zclk->kick_reg) & CPG_FRQCRB_KICK)
  80. return -EBUSY;
  81. val = clk_readl(zclk->reg);
  82. val &= ~CPG_FRQCRC_ZFC_MASK;
  83. val |= (32 - mult) << CPG_FRQCRC_ZFC_SHIFT;
  84. clk_writel(val, zclk->reg);
  85. /*
  86. * Set KICK bit in FRQCRB to update hardware setting and wait for
  87. * clock change completion.
  88. */
  89. kick = clk_readl(zclk->kick_reg);
  90. kick |= CPG_FRQCRB_KICK;
  91. clk_writel(kick, zclk->kick_reg);
  92. /*
  93. * Note: There is no HW information about the worst case latency.
  94. *
  95. * Using experimental measurements, it seems that no more than
  96. * ~10 iterations are needed, independently of the CPU rate.
  97. * Since this value might be dependant of external xtal rate, pll1
  98. * rate or even the other emulation clocks rate, use 1000 as a
  99. * "super" safe value.
  100. */
  101. for (i = 1000; i; i--) {
  102. if (!(clk_readl(zclk->kick_reg) & CPG_FRQCRB_KICK))
  103. return 0;
  104. cpu_relax();
  105. }
  106. return -ETIMEDOUT;
  107. }
  108. static const struct clk_ops cpg_z_clk_ops = {
  109. .recalc_rate = cpg_z_clk_recalc_rate,
  110. .round_rate = cpg_z_clk_round_rate,
  111. .set_rate = cpg_z_clk_set_rate,
  112. };
  113. static struct clk * __init cpg_z_clk_register(struct rcar_gen2_cpg *cpg)
  114. {
  115. static const char *parent_name = "pll0";
  116. struct clk_init_data init;
  117. struct cpg_z_clk *zclk;
  118. struct clk *clk;
  119. zclk = kzalloc(sizeof(*zclk), GFP_KERNEL);
  120. if (!zclk)
  121. return ERR_PTR(-ENOMEM);
  122. init.name = "z";
  123. init.ops = &cpg_z_clk_ops;
  124. init.flags = 0;
  125. init.parent_names = &parent_name;
  126. init.num_parents = 1;
  127. zclk->reg = cpg->reg + CPG_FRQCRC;
  128. zclk->kick_reg = cpg->reg + CPG_FRQCRB;
  129. zclk->hw.init = &init;
  130. clk = clk_register(NULL, &zclk->hw);
  131. if (IS_ERR(clk))
  132. kfree(zclk);
  133. return clk;
  134. }
  135. /* -----------------------------------------------------------------------------
  136. * CPG Clock Data
  137. */
  138. /*
  139. * MD EXTAL PLL0 PLL1 PLL3
  140. * 14 13 19 (MHz) *1 *1
  141. *---------------------------------------------------
  142. * 0 0 0 15 x 1 x172/2 x208/2 x106
  143. * 0 0 1 15 x 1 x172/2 x208/2 x88
  144. * 0 1 0 20 x 1 x130/2 x156/2 x80
  145. * 0 1 1 20 x 1 x130/2 x156/2 x66
  146. * 1 0 0 26 / 2 x200/2 x240/2 x122
  147. * 1 0 1 26 / 2 x200/2 x240/2 x102
  148. * 1 1 0 30 / 2 x172/2 x208/2 x106
  149. * 1 1 1 30 / 2 x172/2 x208/2 x88
  150. *
  151. * *1 : Table 7.6 indicates VCO ouput (PLLx = VCO/2)
  152. */
  153. #define CPG_PLL_CONFIG_INDEX(md) ((((md) & BIT(14)) >> 12) | \
  154. (((md) & BIT(13)) >> 12) | \
  155. (((md) & BIT(19)) >> 19))
  156. struct cpg_pll_config {
  157. unsigned int extal_div;
  158. unsigned int pll1_mult;
  159. unsigned int pll3_mult;
  160. };
  161. static const struct cpg_pll_config cpg_pll_configs[8] __initconst = {
  162. { 1, 208, 106 }, { 1, 208, 88 }, { 1, 156, 80 }, { 1, 156, 66 },
  163. { 2, 240, 122 }, { 2, 240, 102 }, { 2, 208, 106 }, { 2, 208, 88 },
  164. };
  165. /* SDHI divisors */
  166. static const struct clk_div_table cpg_sdh_div_table[] = {
  167. { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 },
  168. { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 },
  169. { 8, 24 }, { 10, 36 }, { 11, 48 }, { 0, 0 },
  170. };
  171. static const struct clk_div_table cpg_sd01_div_table[] = {
  172. { 4, 8 },
  173. { 5, 12 }, { 6, 16 }, { 7, 18 }, { 8, 24 },
  174. { 10, 36 }, { 11, 48 }, { 12, 10 }, { 0, 0 },
  175. };
  176. /* -----------------------------------------------------------------------------
  177. * Initialization
  178. */
  179. static u32 cpg_mode __initdata;
  180. static struct clk * __init
  181. rcar_gen2_cpg_register_clock(struct device_node *np, struct rcar_gen2_cpg *cpg,
  182. const struct cpg_pll_config *config,
  183. const char *name)
  184. {
  185. const struct clk_div_table *table = NULL;
  186. const char *parent_name;
  187. unsigned int shift;
  188. unsigned int mult = 1;
  189. unsigned int div = 1;
  190. if (!strcmp(name, "main")) {
  191. parent_name = of_clk_get_parent_name(np, 0);
  192. div = config->extal_div;
  193. } else if (!strcmp(name, "pll0")) {
  194. /* PLL0 is a configurable multiplier clock. Register it as a
  195. * fixed factor clock for now as there's no generic multiplier
  196. * clock implementation and we currently have no need to change
  197. * the multiplier value.
  198. */
  199. u32 value = clk_readl(cpg->reg + CPG_PLL0CR);
  200. parent_name = "main";
  201. mult = ((value >> 24) & ((1 << 7) - 1)) + 1;
  202. } else if (!strcmp(name, "pll1")) {
  203. parent_name = "main";
  204. mult = config->pll1_mult / 2;
  205. } else if (!strcmp(name, "pll3")) {
  206. parent_name = "main";
  207. mult = config->pll3_mult;
  208. } else if (!strcmp(name, "lb")) {
  209. parent_name = "pll1";
  210. div = cpg_mode & BIT(18) ? 36 : 24;
  211. } else if (!strcmp(name, "qspi")) {
  212. parent_name = "pll1_div2";
  213. div = (cpg_mode & (BIT(3) | BIT(2) | BIT(1))) == BIT(2)
  214. ? 8 : 10;
  215. } else if (!strcmp(name, "sdh")) {
  216. parent_name = "pll1";
  217. table = cpg_sdh_div_table;
  218. shift = 8;
  219. } else if (!strcmp(name, "sd0")) {
  220. parent_name = "pll1";
  221. table = cpg_sd01_div_table;
  222. shift = 4;
  223. } else if (!strcmp(name, "sd1")) {
  224. parent_name = "pll1";
  225. table = cpg_sd01_div_table;
  226. shift = 0;
  227. } else if (!strcmp(name, "z")) {
  228. return cpg_z_clk_register(cpg);
  229. } else {
  230. return ERR_PTR(-EINVAL);
  231. }
  232. if (!table)
  233. return clk_register_fixed_factor(NULL, name, parent_name, 0,
  234. mult, div);
  235. else
  236. return clk_register_divider_table(NULL, name, parent_name, 0,
  237. cpg->reg + CPG_SDCKCR, shift,
  238. 4, 0, table, &cpg->lock);
  239. }
  240. static void __init rcar_gen2_cpg_clocks_init(struct device_node *np)
  241. {
  242. const struct cpg_pll_config *config;
  243. struct rcar_gen2_cpg *cpg;
  244. struct clk **clks;
  245. unsigned int i;
  246. int num_clks;
  247. num_clks = of_property_count_strings(np, "clock-output-names");
  248. if (num_clks < 0) {
  249. pr_err("%s: failed to count clocks\n", __func__);
  250. return;
  251. }
  252. cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);
  253. clks = kzalloc(num_clks * sizeof(*clks), GFP_KERNEL);
  254. if (cpg == NULL || clks == NULL) {
  255. /* We're leaking memory on purpose, there's no point in cleaning
  256. * up as the system won't boot anyway.
  257. */
  258. pr_err("%s: failed to allocate cpg\n", __func__);
  259. return;
  260. }
  261. spin_lock_init(&cpg->lock);
  262. cpg->data.clks = clks;
  263. cpg->data.clk_num = num_clks;
  264. cpg->reg = of_iomap(np, 0);
  265. if (WARN_ON(cpg->reg == NULL))
  266. return;
  267. config = &cpg_pll_configs[CPG_PLL_CONFIG_INDEX(cpg_mode)];
  268. for (i = 0; i < num_clks; ++i) {
  269. const char *name;
  270. struct clk *clk;
  271. of_property_read_string_index(np, "clock-output-names", i,
  272. &name);
  273. clk = rcar_gen2_cpg_register_clock(np, cpg, config, name);
  274. if (IS_ERR(clk))
  275. pr_err("%s: failed to register %s %s clock (%ld)\n",
  276. __func__, np->name, name, PTR_ERR(clk));
  277. else
  278. cpg->data.clks[i] = clk;
  279. }
  280. of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
  281. }
  282. CLK_OF_DECLARE(rcar_gen2_cpg_clks, "renesas,rcar-gen2-cpg-clocks",
  283. rcar_gen2_cpg_clocks_init);
  284. void __init rcar_gen2_clocks_init(u32 mode)
  285. {
  286. cpg_mode = mode;
  287. of_clk_init(NULL);
  288. }