rcar-gen2-cpg.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * R-Car Gen2 Clock Pulse Generator
  3. *
  4. * Copyright (C) 2016 Cogent Embedded Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. #include <linux/bug.h>
  11. #include <linux/clk.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/slab.h>
  18. #include "renesas-cpg-mssr.h"
  19. #include "rcar-gen2-cpg.h"
  20. #define CPG_FRQCRB 0x0004
  21. #define CPG_FRQCRB_KICK BIT(31)
  22. #define CPG_SDCKCR 0x0074
  23. #define CPG_PLL0CR 0x00d8
  24. #define CPG_PLL0CR_STC_SHIFT 24
  25. #define CPG_PLL0CR_STC_MASK (0x7f << CPG_PLL0CR_STC_SHIFT)
  26. #define CPG_FRQCRC 0x00e0
  27. #define CPG_FRQCRC_ZFC_SHIFT 8
  28. #define CPG_FRQCRC_ZFC_MASK (0x1f << CPG_FRQCRC_ZFC_SHIFT)
  29. #define CPG_ADSPCKCR 0x025c
  30. #define CPG_RCANCKCR 0x0270
  31. static spinlock_t cpg_lock;
  32. /*
  33. * Z Clock
  34. *
  35. * Traits of this clock:
  36. * prepare - clk_prepare only ensures that parents are prepared
  37. * enable - clk_enable only ensures that parents are enabled
  38. * rate - rate is adjustable. clk->rate = parent->rate * mult / 32
  39. * parent - fixed parent. No clk_set_parent support
  40. */
  41. struct cpg_z_clk {
  42. struct clk_hw hw;
  43. void __iomem *reg;
  44. void __iomem *kick_reg;
  45. };
  46. #define to_z_clk(_hw) container_of(_hw, struct cpg_z_clk, hw)
  47. static unsigned long cpg_z_clk_recalc_rate(struct clk_hw *hw,
  48. unsigned long parent_rate)
  49. {
  50. struct cpg_z_clk *zclk = to_z_clk(hw);
  51. unsigned int mult;
  52. unsigned int val;
  53. val = (readl(zclk->reg) & CPG_FRQCRC_ZFC_MASK) >> CPG_FRQCRC_ZFC_SHIFT;
  54. mult = 32 - val;
  55. return div_u64((u64)parent_rate * mult, 32);
  56. }
  57. static long cpg_z_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  58. unsigned long *parent_rate)
  59. {
  60. unsigned long prate = *parent_rate;
  61. unsigned int mult;
  62. if (!prate)
  63. prate = 1;
  64. mult = div_u64((u64)rate * 32, prate);
  65. mult = clamp(mult, 1U, 32U);
  66. return *parent_rate / 32 * mult;
  67. }
  68. static int cpg_z_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  69. unsigned long parent_rate)
  70. {
  71. struct cpg_z_clk *zclk = to_z_clk(hw);
  72. unsigned int mult;
  73. u32 val, kick;
  74. unsigned int i;
  75. mult = div_u64((u64)rate * 32, parent_rate);
  76. mult = clamp(mult, 1U, 32U);
  77. if (readl(zclk->kick_reg) & CPG_FRQCRB_KICK)
  78. return -EBUSY;
  79. val = readl(zclk->reg);
  80. val &= ~CPG_FRQCRC_ZFC_MASK;
  81. val |= (32 - mult) << CPG_FRQCRC_ZFC_SHIFT;
  82. writel(val, zclk->reg);
  83. /*
  84. * Set KICK bit in FRQCRB to update hardware setting and wait for
  85. * clock change completion.
  86. */
  87. kick = readl(zclk->kick_reg);
  88. kick |= CPG_FRQCRB_KICK;
  89. writel(kick, zclk->kick_reg);
  90. /*
  91. * Note: There is no HW information about the worst case latency.
  92. *
  93. * Using experimental measurements, it seems that no more than
  94. * ~10 iterations are needed, independently of the CPU rate.
  95. * Since this value might be dependent on external xtal rate, pll1
  96. * rate or even the other emulation clocks rate, use 1000 as a
  97. * "super" safe value.
  98. */
  99. for (i = 1000; i; i--) {
  100. if (!(readl(zclk->kick_reg) & CPG_FRQCRB_KICK))
  101. return 0;
  102. cpu_relax();
  103. }
  104. return -ETIMEDOUT;
  105. }
  106. static const struct clk_ops cpg_z_clk_ops = {
  107. .recalc_rate = cpg_z_clk_recalc_rate,
  108. .round_rate = cpg_z_clk_round_rate,
  109. .set_rate = cpg_z_clk_set_rate,
  110. };
  111. static struct clk * __init cpg_z_clk_register(const char *name,
  112. const char *parent_name,
  113. void __iomem *base)
  114. {
  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 = name;
  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 = base + CPG_FRQCRC;
  127. zclk->kick_reg = base + 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(const char *name,
  135. const char *parent_name,
  136. void __iomem *base)
  137. {
  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 = base + 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, name, &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(const char *name,
  171. const char *parent_name,
  172. void __iomem *base)
  173. {
  174. struct clk_divider *div;
  175. struct clk_gate *gate;
  176. struct clk *clk;
  177. div = kzalloc(sizeof(*div), GFP_KERNEL);
  178. if (!div)
  179. return ERR_PTR(-ENOMEM);
  180. div->reg = base + CPG_ADSPCKCR;
  181. div->width = 4;
  182. div->table = cpg_adsp_div_table;
  183. div->lock = &cpg_lock;
  184. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  185. if (!gate) {
  186. kfree(div);
  187. return ERR_PTR(-ENOMEM);
  188. }
  189. gate->reg = base + CPG_ADSPCKCR;
  190. gate->bit_idx = 8;
  191. gate->flags = CLK_GATE_SET_TO_DISABLE;
  192. gate->lock = &cpg_lock;
  193. clk = clk_register_composite(NULL, name, &parent_name, 1, NULL, NULL,
  194. &div->hw, &clk_divider_ops,
  195. &gate->hw, &clk_gate_ops, 0);
  196. if (IS_ERR(clk)) {
  197. kfree(gate);
  198. kfree(div);
  199. }
  200. return clk;
  201. }
  202. /* SDHI divisors */
  203. static const struct clk_div_table cpg_sdh_div_table[] = {
  204. { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 },
  205. { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 },
  206. { 8, 24 }, { 10, 36 }, { 11, 48 }, { 0, 0 },
  207. };
  208. static const struct clk_div_table cpg_sd01_div_table[] = {
  209. { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 },
  210. { 8, 24 }, { 10, 36 }, { 11, 48 }, { 12, 10 },
  211. { 0, 0 },
  212. };
  213. static const struct rcar_gen2_cpg_pll_config *cpg_pll_config __initdata;
  214. static unsigned int cpg_pll0_div __initdata;
  215. static u32 cpg_mode __initdata;
  216. struct clk * __init rcar_gen2_cpg_clk_register(struct device *dev,
  217. const struct cpg_core_clk *core,
  218. const struct cpg_mssr_info *info,
  219. struct clk **clks,
  220. void __iomem *base)
  221. {
  222. const struct clk_div_table *table = NULL;
  223. const struct clk *parent;
  224. const char *parent_name;
  225. unsigned int mult = 1;
  226. unsigned int div = 1;
  227. unsigned int shift;
  228. parent = clks[core->parent];
  229. if (IS_ERR(parent))
  230. return ERR_CAST(parent);
  231. parent_name = __clk_get_name(parent);
  232. switch (core->type) {
  233. /* R-Car Gen2 */
  234. case CLK_TYPE_GEN2_MAIN:
  235. div = cpg_pll_config->extal_div;
  236. break;
  237. case CLK_TYPE_GEN2_PLL0:
  238. /*
  239. * PLL0 is a configurable multiplier clock except on R-Car
  240. * V2H/E2. Register the PLL0 clock as a fixed factor clock for
  241. * now as there's no generic multiplier clock implementation and
  242. * we currently have no need to change the multiplier value.
  243. */
  244. mult = cpg_pll_config->pll0_mult;
  245. div = cpg_pll0_div;
  246. if (!mult) {
  247. u32 pll0cr = readl(base + CPG_PLL0CR);
  248. mult = (((pll0cr & CPG_PLL0CR_STC_MASK) >>
  249. CPG_PLL0CR_STC_SHIFT) + 1) * 2;
  250. }
  251. break;
  252. case CLK_TYPE_GEN2_PLL1:
  253. mult = cpg_pll_config->pll1_mult / 2;
  254. break;
  255. case CLK_TYPE_GEN2_PLL3:
  256. mult = cpg_pll_config->pll3_mult;
  257. break;
  258. case CLK_TYPE_GEN2_Z:
  259. return cpg_z_clk_register(core->name, parent_name, base);
  260. case CLK_TYPE_GEN2_LB:
  261. div = cpg_mode & BIT(18) ? 36 : 24;
  262. break;
  263. case CLK_TYPE_GEN2_ADSP:
  264. return cpg_adsp_clk_register(core->name, parent_name, base);
  265. case CLK_TYPE_GEN2_SDH:
  266. table = cpg_sdh_div_table;
  267. shift = 8;
  268. break;
  269. case CLK_TYPE_GEN2_SD0:
  270. table = cpg_sd01_div_table;
  271. shift = 4;
  272. break;
  273. case CLK_TYPE_GEN2_SD1:
  274. table = cpg_sd01_div_table;
  275. shift = 0;
  276. break;
  277. case CLK_TYPE_GEN2_QSPI:
  278. div = (cpg_mode & (BIT(3) | BIT(2) | BIT(1))) == BIT(2) ?
  279. 8 : 10;
  280. break;
  281. case CLK_TYPE_GEN2_RCAN:
  282. return cpg_rcan_clk_register(core->name, parent_name, base);
  283. default:
  284. return ERR_PTR(-EINVAL);
  285. }
  286. if (!table)
  287. return clk_register_fixed_factor(NULL, core->name, parent_name,
  288. 0, mult, div);
  289. else
  290. return clk_register_divider_table(NULL, core->name,
  291. parent_name, 0,
  292. base + CPG_SDCKCR, shift, 4,
  293. 0, table, &cpg_lock);
  294. }
  295. int __init rcar_gen2_cpg_init(const struct rcar_gen2_cpg_pll_config *config,
  296. unsigned int pll0_div, u32 mode)
  297. {
  298. cpg_pll_config = config;
  299. cpg_pll0_div = pll0_div;
  300. cpg_mode = mode;
  301. spin_lock_init(&cpg_lock);
  302. return 0;
  303. }