rcar-gen3-cpg.c 9.1 KB

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