rcar-gen3-cpg.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. unsigned int cur_div_idx;
  55. };
  56. /* SDn divider
  57. * sd_srcfc sd_fc div
  58. * stp_hck stp_ck (div) (div) = sd_srcfc x sd_fc
  59. *-------------------------------------------------------------------
  60. * 0 0 0 (1) 1 (4) 4
  61. * 0 0 1 (2) 1 (4) 8
  62. * 1 0 2 (4) 1 (4) 16
  63. * 1 0 3 (8) 1 (4) 32
  64. * 1 0 4 (16) 1 (4) 64
  65. * 0 0 0 (1) 0 (2) 2
  66. * 0 0 1 (2) 0 (2) 4
  67. * 1 0 2 (4) 0 (2) 8
  68. * 1 0 3 (8) 0 (2) 16
  69. * 1 0 4 (16) 0 (2) 32
  70. */
  71. static const struct sd_div_table cpg_sd_div_table[] = {
  72. /* CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) */
  73. CPG_SD_DIV_TABLE_DATA(0, 0, 0, 1, 4),
  74. CPG_SD_DIV_TABLE_DATA(0, 0, 1, 1, 8),
  75. CPG_SD_DIV_TABLE_DATA(1, 0, 2, 1, 16),
  76. CPG_SD_DIV_TABLE_DATA(1, 0, 3, 1, 32),
  77. CPG_SD_DIV_TABLE_DATA(1, 0, 4, 1, 64),
  78. CPG_SD_DIV_TABLE_DATA(0, 0, 0, 0, 2),
  79. CPG_SD_DIV_TABLE_DATA(0, 0, 1, 0, 4),
  80. CPG_SD_DIV_TABLE_DATA(1, 0, 2, 0, 8),
  81. CPG_SD_DIV_TABLE_DATA(1, 0, 3, 0, 16),
  82. CPG_SD_DIV_TABLE_DATA(1, 0, 4, 0, 32),
  83. };
  84. #define to_sd_clock(_hw) container_of(_hw, struct sd_clock, hw)
  85. static int cpg_sd_clock_enable(struct clk_hw *hw)
  86. {
  87. struct sd_clock *clock = to_sd_clock(hw);
  88. u32 val = readl(clock->reg);
  89. val &= ~(CPG_SD_STP_MASK);
  90. val |= clock->div_table[clock->cur_div_idx].val & CPG_SD_STP_MASK;
  91. writel(val, clock->reg);
  92. return 0;
  93. }
  94. static void cpg_sd_clock_disable(struct clk_hw *hw)
  95. {
  96. struct sd_clock *clock = to_sd_clock(hw);
  97. writel(readl(clock->reg) | CPG_SD_STP_MASK, clock->reg);
  98. }
  99. static int cpg_sd_clock_is_enabled(struct clk_hw *hw)
  100. {
  101. struct sd_clock *clock = to_sd_clock(hw);
  102. return !(readl(clock->reg) & CPG_SD_STP_MASK);
  103. }
  104. static unsigned long cpg_sd_clock_recalc_rate(struct clk_hw *hw,
  105. unsigned long parent_rate)
  106. {
  107. struct sd_clock *clock = to_sd_clock(hw);
  108. return DIV_ROUND_CLOSEST(parent_rate,
  109. clock->div_table[clock->cur_div_idx].div);
  110. }
  111. static unsigned int cpg_sd_clock_calc_div(struct sd_clock *clock,
  112. unsigned long rate,
  113. unsigned long parent_rate)
  114. {
  115. unsigned int div;
  116. if (!rate)
  117. rate = 1;
  118. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  119. return clamp_t(unsigned int, div, clock->div_min, clock->div_max);
  120. }
  121. static long cpg_sd_clock_round_rate(struct clk_hw *hw, unsigned long rate,
  122. unsigned long *parent_rate)
  123. {
  124. struct sd_clock *clock = to_sd_clock(hw);
  125. unsigned int div = cpg_sd_clock_calc_div(clock, rate, *parent_rate);
  126. return DIV_ROUND_CLOSEST(*parent_rate, div);
  127. }
  128. static int cpg_sd_clock_set_rate(struct clk_hw *hw, unsigned long rate,
  129. unsigned long parent_rate)
  130. {
  131. struct sd_clock *clock = to_sd_clock(hw);
  132. unsigned int div = cpg_sd_clock_calc_div(clock, rate, parent_rate);
  133. u32 val;
  134. unsigned int i;
  135. for (i = 0; i < clock->div_num; i++)
  136. if (div == clock->div_table[i].div)
  137. break;
  138. if (i >= clock->div_num)
  139. return -EINVAL;
  140. clock->cur_div_idx = i;
  141. val = readl(clock->reg);
  142. val &= ~(CPG_SD_STP_MASK | CPG_SD_FC_MASK);
  143. val |= clock->div_table[i].val & (CPG_SD_STP_MASK | CPG_SD_FC_MASK);
  144. writel(val, clock->reg);
  145. return 0;
  146. }
  147. static const struct clk_ops cpg_sd_clock_ops = {
  148. .enable = cpg_sd_clock_enable,
  149. .disable = cpg_sd_clock_disable,
  150. .is_enabled = cpg_sd_clock_is_enabled,
  151. .recalc_rate = cpg_sd_clock_recalc_rate,
  152. .round_rate = cpg_sd_clock_round_rate,
  153. .set_rate = cpg_sd_clock_set_rate,
  154. };
  155. static struct clk * __init cpg_sd_clk_register(const struct cpg_core_clk *core,
  156. void __iomem *base,
  157. const char *parent_name)
  158. {
  159. struct clk_init_data init;
  160. struct sd_clock *clock;
  161. struct clk *clk;
  162. unsigned int i;
  163. u32 sd_fc;
  164. clock = kzalloc(sizeof(*clock), GFP_KERNEL);
  165. if (!clock)
  166. return ERR_PTR(-ENOMEM);
  167. init.name = core->name;
  168. init.ops = &cpg_sd_clock_ops;
  169. init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
  170. init.parent_names = &parent_name;
  171. init.num_parents = 1;
  172. clock->reg = base + core->offset;
  173. clock->hw.init = &init;
  174. clock->div_table = cpg_sd_div_table;
  175. clock->div_num = ARRAY_SIZE(cpg_sd_div_table);
  176. sd_fc = readl(clock->reg) & CPG_SD_FC_MASK;
  177. for (i = 0; i < clock->div_num; i++)
  178. if (sd_fc == (clock->div_table[i].val & CPG_SD_FC_MASK))
  179. break;
  180. if (WARN_ON(i >= clock->div_num)) {
  181. kfree(clock);
  182. return ERR_PTR(-EINVAL);
  183. }
  184. clock->cur_div_idx = i;
  185. clock->div_max = clock->div_table[0].div;
  186. clock->div_min = clock->div_max;
  187. for (i = 1; i < clock->div_num; i++) {
  188. clock->div_max = max(clock->div_max, clock->div_table[i].div);
  189. clock->div_min = min(clock->div_min, clock->div_table[i].div);
  190. }
  191. clk = clk_register(NULL, &clock->hw);
  192. if (IS_ERR(clk))
  193. kfree(clock);
  194. return clk;
  195. }
  196. static const struct rcar_gen3_cpg_pll_config *cpg_pll_config __initdata;
  197. static unsigned int cpg_clk_extalr __initdata;
  198. static u32 cpg_mode __initdata;
  199. static u32 cpg_quirks __initdata;
  200. #define PLL_ERRATA BIT(0) /* Missing PLL0/2/4 post-divider */
  201. #define RCKCR_CKSEL BIT(1) /* Manual RCLK parent selection */
  202. static const struct soc_device_attribute cpg_quirks_match[] __initconst = {
  203. {
  204. .soc_id = "r8a7795", .revision = "ES1.0",
  205. .data = (void *)(PLL_ERRATA | RCKCR_CKSEL),
  206. },
  207. {
  208. .soc_id = "r8a7795", .revision = "ES1.*",
  209. .data = (void *)RCKCR_CKSEL,
  210. },
  211. {
  212. .soc_id = "r8a7796", .revision = "ES1.0",
  213. .data = (void *)RCKCR_CKSEL,
  214. },
  215. { /* sentinel */ }
  216. };
  217. struct clk * __init rcar_gen3_cpg_clk_register(struct device *dev,
  218. const struct cpg_core_clk *core, const struct cpg_mssr_info *info,
  219. struct clk **clks, void __iomem *base)
  220. {
  221. const struct clk *parent;
  222. unsigned int mult = 1;
  223. unsigned int div = 1;
  224. u32 value;
  225. parent = clks[core->parent & 0xffff]; /* CLK_TYPE_PE uses high bits */
  226. if (IS_ERR(parent))
  227. return ERR_CAST(parent);
  228. switch (core->type) {
  229. case CLK_TYPE_GEN3_MAIN:
  230. div = cpg_pll_config->extal_div;
  231. break;
  232. case CLK_TYPE_GEN3_PLL0:
  233. /*
  234. * PLL0 is a configurable multiplier clock. Register it as a
  235. * fixed factor clock for now as there's no generic multiplier
  236. * clock implementation and we currently have no need to change
  237. * the multiplier value.
  238. */
  239. value = readl(base + CPG_PLL0CR);
  240. mult = (((value >> 24) & 0x7f) + 1) * 2;
  241. if (cpg_quirks & PLL_ERRATA)
  242. mult *= 2;
  243. break;
  244. case CLK_TYPE_GEN3_PLL1:
  245. mult = cpg_pll_config->pll1_mult;
  246. div = cpg_pll_config->pll1_div;
  247. break;
  248. case CLK_TYPE_GEN3_PLL2:
  249. /*
  250. * PLL2 is a configurable multiplier clock. Register it as a
  251. * fixed factor clock for now as there's no generic multiplier
  252. * clock implementation and we currently have no need to change
  253. * the multiplier value.
  254. */
  255. value = readl(base + CPG_PLL2CR);
  256. mult = (((value >> 24) & 0x7f) + 1) * 2;
  257. if (cpg_quirks & PLL_ERRATA)
  258. mult *= 2;
  259. break;
  260. case CLK_TYPE_GEN3_PLL3:
  261. mult = cpg_pll_config->pll3_mult;
  262. div = cpg_pll_config->pll3_div;
  263. break;
  264. case CLK_TYPE_GEN3_PLL4:
  265. /*
  266. * PLL4 is a configurable multiplier clock. Register it as a
  267. * fixed factor clock for now as there's no generic multiplier
  268. * clock implementation and we currently have no need to change
  269. * the multiplier value.
  270. */
  271. value = readl(base + CPG_PLL4CR);
  272. mult = (((value >> 24) & 0x7f) + 1) * 2;
  273. if (cpg_quirks & PLL_ERRATA)
  274. mult *= 2;
  275. break;
  276. case CLK_TYPE_GEN3_SD:
  277. return cpg_sd_clk_register(core, base, __clk_get_name(parent));
  278. case CLK_TYPE_GEN3_R:
  279. if (cpg_quirks & RCKCR_CKSEL) {
  280. /*
  281. * RINT is default.
  282. * Only if EXTALR is populated, we switch to it.
  283. */
  284. value = readl(base + CPG_RCKCR) & 0x3f;
  285. if (clk_get_rate(clks[cpg_clk_extalr])) {
  286. parent = clks[cpg_clk_extalr];
  287. value |= BIT(15);
  288. }
  289. writel(value, base + CPG_RCKCR);
  290. break;
  291. }
  292. /* Select parent clock of RCLK by MD28 */
  293. if (cpg_mode & BIT(28))
  294. parent = clks[cpg_clk_extalr];
  295. break;
  296. case CLK_TYPE_GEN3_PE:
  297. /*
  298. * Peripheral clock with a fixed divider, selectable between
  299. * clean and spread spectrum parents using MD12
  300. */
  301. if (cpg_mode & BIT(12)) {
  302. /* Clean */
  303. div = core->div & 0xffff;
  304. } else {
  305. /* SCCG */
  306. parent = clks[core->parent >> 16];
  307. if (IS_ERR(parent))
  308. return ERR_CAST(parent);
  309. div = core->div >> 16;
  310. }
  311. mult = 1;
  312. break;
  313. default:
  314. return ERR_PTR(-EINVAL);
  315. }
  316. return clk_register_fixed_factor(NULL, core->name,
  317. __clk_get_name(parent), 0, mult, div);
  318. }
  319. int __init rcar_gen3_cpg_init(const struct rcar_gen3_cpg_pll_config *config,
  320. unsigned int clk_extalr, u32 mode)
  321. {
  322. const struct soc_device_attribute *attr;
  323. cpg_pll_config = config;
  324. cpg_clk_extalr = clk_extalr;
  325. cpg_mode = mode;
  326. attr = soc_device_match(cpg_quirks_match);
  327. if (attr)
  328. cpg_quirks = (uintptr_t)attr->data;
  329. pr_debug("%s: mode = 0x%x quirks = 0x%x\n", __func__, mode, cpg_quirks);
  330. return 0;
  331. }