clk-rcar-gen2.c 11 KB

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