clk-rcar-gen2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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/renesas.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 dependent on 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 output (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. unsigned int pll0_mult; /* For R-Car V2H and E2 only */
  230. };
  231. static const struct cpg_pll_config cpg_pll_configs[8] __initconst = {
  232. { 1, 208, 106, 200 }, { 1, 208, 88, 200 },
  233. { 1, 156, 80, 150 }, { 1, 156, 66, 150 },
  234. { 2, 240, 122, 230 }, { 2, 240, 102, 230 },
  235. { 2, 208, 106, 200 }, { 2, 208, 88, 200 },
  236. };
  237. /* SDHI divisors */
  238. static const struct clk_div_table cpg_sdh_div_table[] = {
  239. { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 },
  240. { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 },
  241. { 8, 24 }, { 10, 36 }, { 11, 48 }, { 0, 0 },
  242. };
  243. static const struct clk_div_table cpg_sd01_div_table[] = {
  244. { 4, 8 },
  245. { 5, 12 }, { 6, 16 }, { 7, 18 }, { 8, 24 },
  246. { 10, 36 }, { 11, 48 }, { 12, 10 }, { 0, 0 },
  247. };
  248. /* -----------------------------------------------------------------------------
  249. * Initialization
  250. */
  251. static u32 cpg_mode __initdata;
  252. static const char * const pll0_mult_match[] = {
  253. "renesas,r8a7792-cpg-clocks",
  254. "renesas,r8a7794-cpg-clocks",
  255. NULL
  256. };
  257. static struct clk * __init
  258. rcar_gen2_cpg_register_clock(struct device_node *np, struct rcar_gen2_cpg *cpg,
  259. const struct cpg_pll_config *config,
  260. const char *name)
  261. {
  262. const struct clk_div_table *table = NULL;
  263. const char *parent_name;
  264. unsigned int shift;
  265. unsigned int mult = 1;
  266. unsigned int div = 1;
  267. if (!strcmp(name, "main")) {
  268. parent_name = of_clk_get_parent_name(np, 0);
  269. div = config->extal_div;
  270. } else if (!strcmp(name, "pll0")) {
  271. /* PLL0 is a configurable multiplier clock. Register it as a
  272. * fixed factor clock for now as there's no generic multiplier
  273. * clock implementation and we currently have no need to change
  274. * the multiplier value.
  275. */
  276. if (of_device_compatible_match(np, pll0_mult_match)) {
  277. /* R-Car V2H and E2 do not have PLL0CR */
  278. mult = config->pll0_mult;
  279. div = 3;
  280. } else {
  281. u32 value = clk_readl(cpg->reg + CPG_PLL0CR);
  282. mult = ((value >> 24) & ((1 << 7) - 1)) + 1;
  283. }
  284. parent_name = "main";
  285. } else if (!strcmp(name, "pll1")) {
  286. parent_name = "main";
  287. mult = config->pll1_mult / 2;
  288. } else if (!strcmp(name, "pll3")) {
  289. parent_name = "main";
  290. mult = config->pll3_mult;
  291. } else if (!strcmp(name, "lb")) {
  292. parent_name = "pll1";
  293. div = cpg_mode & BIT(18) ? 36 : 24;
  294. } else if (!strcmp(name, "qspi")) {
  295. parent_name = "pll1_div2";
  296. div = (cpg_mode & (BIT(3) | BIT(2) | BIT(1))) == BIT(2)
  297. ? 8 : 10;
  298. } else if (!strcmp(name, "sdh")) {
  299. parent_name = "pll1";
  300. table = cpg_sdh_div_table;
  301. shift = 8;
  302. } else if (!strcmp(name, "sd0")) {
  303. parent_name = "pll1";
  304. table = cpg_sd01_div_table;
  305. shift = 4;
  306. } else if (!strcmp(name, "sd1")) {
  307. parent_name = "pll1";
  308. table = cpg_sd01_div_table;
  309. shift = 0;
  310. } else if (!strcmp(name, "z")) {
  311. return cpg_z_clk_register(cpg);
  312. } else if (!strcmp(name, "rcan")) {
  313. return cpg_rcan_clk_register(cpg, np);
  314. } else if (!strcmp(name, "adsp")) {
  315. return cpg_adsp_clk_register(cpg);
  316. } else {
  317. return ERR_PTR(-EINVAL);
  318. }
  319. if (!table)
  320. return clk_register_fixed_factor(NULL, name, parent_name, 0,
  321. mult, div);
  322. else
  323. return clk_register_divider_table(NULL, name, parent_name, 0,
  324. cpg->reg + CPG_SDCKCR, shift,
  325. 4, 0, table, &cpg->lock);
  326. }
  327. static void __init rcar_gen2_cpg_clocks_init(struct device_node *np)
  328. {
  329. const struct cpg_pll_config *config;
  330. struct rcar_gen2_cpg *cpg;
  331. struct clk **clks;
  332. unsigned int i;
  333. int num_clks;
  334. num_clks = of_property_count_strings(np, "clock-output-names");
  335. if (num_clks < 0) {
  336. pr_err("%s: failed to count clocks\n", __func__);
  337. return;
  338. }
  339. cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);
  340. clks = kzalloc(num_clks * sizeof(*clks), GFP_KERNEL);
  341. if (cpg == NULL || clks == NULL) {
  342. /* We're leaking memory on purpose, there's no point in cleaning
  343. * up as the system won't boot anyway.
  344. */
  345. pr_err("%s: failed to allocate cpg\n", __func__);
  346. return;
  347. }
  348. spin_lock_init(&cpg->lock);
  349. cpg->data.clks = clks;
  350. cpg->data.clk_num = num_clks;
  351. cpg->reg = of_iomap(np, 0);
  352. if (WARN_ON(cpg->reg == NULL))
  353. return;
  354. config = &cpg_pll_configs[CPG_PLL_CONFIG_INDEX(cpg_mode)];
  355. for (i = 0; i < num_clks; ++i) {
  356. const char *name;
  357. struct clk *clk;
  358. of_property_read_string_index(np, "clock-output-names", i,
  359. &name);
  360. clk = rcar_gen2_cpg_register_clock(np, cpg, config, name);
  361. if (IS_ERR(clk))
  362. pr_err("%s: failed to register %s %s clock (%ld)\n",
  363. __func__, np->name, name, PTR_ERR(clk));
  364. else
  365. cpg->data.clks[i] = clk;
  366. }
  367. of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
  368. cpg_mstp_add_clk_domain(np);
  369. }
  370. CLK_OF_DECLARE(rcar_gen2_cpg_clks, "renesas,rcar-gen2-cpg-clocks",
  371. rcar_gen2_cpg_clocks_init);
  372. void __init rcar_gen2_clocks_init(u32 mode)
  373. {
  374. cpg_mode = mode;
  375. of_clk_init(NULL);
  376. }