clk-rcar-gen2.c 12 KB

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