rcar-gen2-cpg.c 9.0 KB

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