clk-rcar-gen2.c 11 KB

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