rcar-gen3-cpg.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * R-Car Gen3 Clock Pulse Generator
  3. *
  4. * Copyright (C) 2015-2016 Glider bvba
  5. *
  6. * Based on clk-rcar-gen3.c
  7. *
  8. * Copyright (C) 2015 Renesas Electronics Corp.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. */
  14. #include <linux/bug.h>
  15. #include <linux/clk.h>
  16. #include <linux/clk-provider.h>
  17. #include <linux/device.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/io.h>
  21. #include <linux/slab.h>
  22. #include <linux/sys_soc.h>
  23. #include "renesas-cpg-mssr.h"
  24. #include "rcar-gen3-cpg.h"
  25. #define CPG_PLL0CR 0x00d8
  26. #define CPG_PLL2CR 0x002c
  27. #define CPG_PLL4CR 0x01f4
  28. /*
  29. * SDn Clock
  30. */
  31. #define CPG_SD_STP_HCK BIT(9)
  32. #define CPG_SD_STP_CK BIT(8)
  33. #define CPG_SD_STP_MASK (CPG_SD_STP_HCK | CPG_SD_STP_CK)
  34. #define CPG_SD_FC_MASK (0x7 << 2 | 0x3 << 0)
  35. #define CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) \
  36. { \
  37. .val = ((stp_hck) ? CPG_SD_STP_HCK : 0) | \
  38. ((stp_ck) ? CPG_SD_STP_CK : 0) | \
  39. ((sd_srcfc) << 2) | \
  40. ((sd_fc) << 0), \
  41. .div = (sd_div), \
  42. }
  43. struct sd_div_table {
  44. u32 val;
  45. unsigned int div;
  46. };
  47. struct sd_clock {
  48. struct clk_hw hw;
  49. void __iomem *reg;
  50. const struct sd_div_table *div_table;
  51. unsigned int div_num;
  52. unsigned int div_min;
  53. unsigned int div_max;
  54. };
  55. /* SDn divider
  56. * sd_srcfc sd_fc div
  57. * stp_hck stp_ck (div) (div) = sd_srcfc x sd_fc
  58. *-------------------------------------------------------------------
  59. * 0 0 0 (1) 1 (4) 4
  60. * 0 0 1 (2) 1 (4) 8
  61. * 1 0 2 (4) 1 (4) 16
  62. * 1 0 3 (8) 1 (4) 32
  63. * 1 0 4 (16) 1 (4) 64
  64. * 0 0 0 (1) 0 (2) 2
  65. * 0 0 1 (2) 0 (2) 4
  66. * 1 0 2 (4) 0 (2) 8
  67. * 1 0 3 (8) 0 (2) 16
  68. * 1 0 4 (16) 0 (2) 32
  69. */
  70. static const struct sd_div_table cpg_sd_div_table[] = {
  71. /* CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) */
  72. CPG_SD_DIV_TABLE_DATA(0, 0, 0, 1, 4),
  73. CPG_SD_DIV_TABLE_DATA(0, 0, 1, 1, 8),
  74. CPG_SD_DIV_TABLE_DATA(1, 0, 2, 1, 16),
  75. CPG_SD_DIV_TABLE_DATA(1, 0, 3, 1, 32),
  76. CPG_SD_DIV_TABLE_DATA(1, 0, 4, 1, 64),
  77. CPG_SD_DIV_TABLE_DATA(0, 0, 0, 0, 2),
  78. CPG_SD_DIV_TABLE_DATA(0, 0, 1, 0, 4),
  79. CPG_SD_DIV_TABLE_DATA(1, 0, 2, 0, 8),
  80. CPG_SD_DIV_TABLE_DATA(1, 0, 3, 0, 16),
  81. CPG_SD_DIV_TABLE_DATA(1, 0, 4, 0, 32),
  82. };
  83. #define to_sd_clock(_hw) container_of(_hw, struct sd_clock, hw)
  84. static int cpg_sd_clock_enable(struct clk_hw *hw)
  85. {
  86. struct sd_clock *clock = to_sd_clock(hw);
  87. u32 val, sd_fc;
  88. unsigned int i;
  89. val = readl(clock->reg);
  90. sd_fc = val & CPG_SD_FC_MASK;
  91. for (i = 0; i < clock->div_num; i++)
  92. if (sd_fc == (clock->div_table[i].val & CPG_SD_FC_MASK))
  93. break;
  94. if (i >= clock->div_num)
  95. return -EINVAL;
  96. val &= ~(CPG_SD_STP_MASK);
  97. val |= clock->div_table[i].val & CPG_SD_STP_MASK;
  98. writel(val, clock->reg);
  99. return 0;
  100. }
  101. static void cpg_sd_clock_disable(struct clk_hw *hw)
  102. {
  103. struct sd_clock *clock = to_sd_clock(hw);
  104. writel(readl(clock->reg) | CPG_SD_STP_MASK, clock->reg);
  105. }
  106. static int cpg_sd_clock_is_enabled(struct clk_hw *hw)
  107. {
  108. struct sd_clock *clock = to_sd_clock(hw);
  109. return !(readl(clock->reg) & CPG_SD_STP_MASK);
  110. }
  111. static unsigned long cpg_sd_clock_recalc_rate(struct clk_hw *hw,
  112. unsigned long parent_rate)
  113. {
  114. struct sd_clock *clock = to_sd_clock(hw);
  115. unsigned long rate = parent_rate;
  116. u32 val, sd_fc;
  117. unsigned int i;
  118. val = readl(clock->reg);
  119. sd_fc = val & CPG_SD_FC_MASK;
  120. for (i = 0; i < clock->div_num; i++)
  121. if (sd_fc == (clock->div_table[i].val & CPG_SD_FC_MASK))
  122. break;
  123. if (i >= clock->div_num)
  124. return -EINVAL;
  125. return DIV_ROUND_CLOSEST(rate, clock->div_table[i].div);
  126. }
  127. static unsigned int cpg_sd_clock_calc_div(struct sd_clock *clock,
  128. unsigned long rate,
  129. unsigned long parent_rate)
  130. {
  131. unsigned int div;
  132. if (!rate)
  133. rate = 1;
  134. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  135. return clamp_t(unsigned int, div, clock->div_min, clock->div_max);
  136. }
  137. static long cpg_sd_clock_round_rate(struct clk_hw *hw, unsigned long rate,
  138. unsigned long *parent_rate)
  139. {
  140. struct sd_clock *clock = to_sd_clock(hw);
  141. unsigned int div = cpg_sd_clock_calc_div(clock, rate, *parent_rate);
  142. return DIV_ROUND_CLOSEST(*parent_rate, div);
  143. }
  144. static int cpg_sd_clock_set_rate(struct clk_hw *hw, unsigned long rate,
  145. unsigned long parent_rate)
  146. {
  147. struct sd_clock *clock = to_sd_clock(hw);
  148. unsigned int div = cpg_sd_clock_calc_div(clock, rate, parent_rate);
  149. u32 val;
  150. unsigned int i;
  151. for (i = 0; i < clock->div_num; i++)
  152. if (div == clock->div_table[i].div)
  153. break;
  154. if (i >= clock->div_num)
  155. return -EINVAL;
  156. val = readl(clock->reg);
  157. val &= ~(CPG_SD_STP_MASK | CPG_SD_FC_MASK);
  158. val |= clock->div_table[i].val & (CPG_SD_STP_MASK | CPG_SD_FC_MASK);
  159. writel(val, clock->reg);
  160. return 0;
  161. }
  162. static const struct clk_ops cpg_sd_clock_ops = {
  163. .enable = cpg_sd_clock_enable,
  164. .disable = cpg_sd_clock_disable,
  165. .is_enabled = cpg_sd_clock_is_enabled,
  166. .recalc_rate = cpg_sd_clock_recalc_rate,
  167. .round_rate = cpg_sd_clock_round_rate,
  168. .set_rate = cpg_sd_clock_set_rate,
  169. };
  170. static struct clk * __init cpg_sd_clk_register(const struct cpg_core_clk *core,
  171. void __iomem *base,
  172. const char *parent_name)
  173. {
  174. struct clk_init_data init;
  175. struct sd_clock *clock;
  176. struct clk *clk;
  177. unsigned int i;
  178. clock = kzalloc(sizeof(*clock), GFP_KERNEL);
  179. if (!clock)
  180. return ERR_PTR(-ENOMEM);
  181. init.name = core->name;
  182. init.ops = &cpg_sd_clock_ops;
  183. init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
  184. init.parent_names = &parent_name;
  185. init.num_parents = 1;
  186. clock->reg = base + core->offset;
  187. clock->hw.init = &init;
  188. clock->div_table = cpg_sd_div_table;
  189. clock->div_num = ARRAY_SIZE(cpg_sd_div_table);
  190. clock->div_max = clock->div_table[0].div;
  191. clock->div_min = clock->div_max;
  192. for (i = 1; i < clock->div_num; i++) {
  193. clock->div_max = max(clock->div_max, clock->div_table[i].div);
  194. clock->div_min = min(clock->div_min, clock->div_table[i].div);
  195. }
  196. clk = clk_register(NULL, &clock->hw);
  197. if (IS_ERR(clk))
  198. kfree(clock);
  199. return clk;
  200. }
  201. static const struct rcar_gen3_cpg_pll_config *cpg_pll_config __initdata;
  202. static unsigned int cpg_clk_extalr __initdata;
  203. static u32 cpg_mode __initdata;
  204. static u32 cpg_quirks __initdata;
  205. #define PLL_ERRATA BIT(0) /* Missing PLL0/2/4 post-divider */
  206. #define RCKCR_CKSEL BIT(1) /* Manual RCLK parent selection */
  207. static const struct soc_device_attribute cpg_quirks_match[] __initconst = {
  208. {
  209. .soc_id = "r8a7795", .revision = "ES1.0",
  210. .data = (void *)(PLL_ERRATA | RCKCR_CKSEL),
  211. },
  212. {
  213. .soc_id = "r8a7795", .revision = "ES1.*",
  214. .data = (void *)RCKCR_CKSEL,
  215. },
  216. {
  217. .soc_id = "r8a7796", .revision = "ES1.0",
  218. .data = (void *)RCKCR_CKSEL,
  219. },
  220. { /* sentinel */ }
  221. };
  222. struct clk * __init rcar_gen3_cpg_clk_register(struct device *dev,
  223. const struct cpg_core_clk *core, const struct cpg_mssr_info *info,
  224. struct clk **clks, void __iomem *base)
  225. {
  226. const struct clk *parent;
  227. unsigned int mult = 1;
  228. unsigned int div = 1;
  229. u32 value;
  230. parent = clks[core->parent];
  231. if (IS_ERR(parent))
  232. return ERR_CAST(parent);
  233. switch (core->type) {
  234. case CLK_TYPE_GEN3_MAIN:
  235. div = cpg_pll_config->extal_div;
  236. break;
  237. case CLK_TYPE_GEN3_PLL0:
  238. /*
  239. * PLL0 is a configurable multiplier clock. Register it as a
  240. * fixed factor clock for now as there's no generic multiplier
  241. * clock implementation and we currently have no need to change
  242. * the multiplier value.
  243. */
  244. value = readl(base + CPG_PLL0CR);
  245. mult = (((value >> 24) & 0x7f) + 1) * 2;
  246. if (cpg_quirks & PLL_ERRATA)
  247. mult *= 2;
  248. break;
  249. case CLK_TYPE_GEN3_PLL1:
  250. mult = cpg_pll_config->pll1_mult;
  251. break;
  252. case CLK_TYPE_GEN3_PLL2:
  253. /*
  254. * PLL2 is a configurable multiplier clock. Register it as a
  255. * fixed factor clock for now as there's no generic multiplier
  256. * clock implementation and we currently have no need to change
  257. * the multiplier value.
  258. */
  259. value = readl(base + CPG_PLL2CR);
  260. mult = (((value >> 24) & 0x7f) + 1) * 2;
  261. if (cpg_quirks & PLL_ERRATA)
  262. mult *= 2;
  263. break;
  264. case CLK_TYPE_GEN3_PLL3:
  265. mult = cpg_pll_config->pll3_mult;
  266. break;
  267. case CLK_TYPE_GEN3_PLL4:
  268. /*
  269. * PLL4 is a configurable multiplier clock. Register it as a
  270. * fixed factor clock for now as there's no generic multiplier
  271. * clock implementation and we currently have no need to change
  272. * the multiplier value.
  273. */
  274. value = readl(base + CPG_PLL4CR);
  275. mult = (((value >> 24) & 0x7f) + 1) * 2;
  276. if (cpg_quirks & PLL_ERRATA)
  277. mult *= 2;
  278. break;
  279. case CLK_TYPE_GEN3_SD:
  280. return cpg_sd_clk_register(core, base, __clk_get_name(parent));
  281. case CLK_TYPE_GEN3_R:
  282. if (cpg_quirks & RCKCR_CKSEL) {
  283. /*
  284. * RINT is default.
  285. * Only if EXTALR is populated, we switch to it.
  286. */
  287. value = readl(base + CPG_RCKCR) & 0x3f;
  288. if (clk_get_rate(clks[cpg_clk_extalr])) {
  289. parent = clks[cpg_clk_extalr];
  290. value |= BIT(15);
  291. }
  292. writel(value, base + CPG_RCKCR);
  293. break;
  294. }
  295. /* Select parent clock of RCLK by MD28 */
  296. if (cpg_mode & BIT(28))
  297. parent = clks[cpg_clk_extalr];
  298. break;
  299. default:
  300. return ERR_PTR(-EINVAL);
  301. }
  302. return clk_register_fixed_factor(NULL, core->name,
  303. __clk_get_name(parent), 0, mult, div);
  304. }
  305. int __init rcar_gen3_cpg_init(const struct rcar_gen3_cpg_pll_config *config,
  306. unsigned int clk_extalr, u32 mode)
  307. {
  308. const struct soc_device_attribute *attr;
  309. cpg_pll_config = config;
  310. cpg_clk_extalr = clk_extalr;
  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. return 0;
  317. }