clk-r8a73a4.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * r8a73a4 Core CPG Clocks
  4. *
  5. * Copyright (C) 2014 Ulrich Hecht
  6. */
  7. #include <linux/clk-provider.h>
  8. #include <linux/clk/renesas.h>
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/spinlock.h>
  16. struct r8a73a4_cpg {
  17. struct clk_onecell_data data;
  18. spinlock_t lock;
  19. };
  20. #define CPG_CKSCR 0xc0
  21. #define CPG_FRQCRA 0x00
  22. #define CPG_FRQCRB 0x04
  23. #define CPG_FRQCRC 0xe0
  24. #define CPG_PLL0CR 0xd8
  25. #define CPG_PLL1CR 0x28
  26. #define CPG_PLL2CR 0x2c
  27. #define CPG_PLL2HCR 0xe4
  28. #define CPG_PLL2SCR 0xf4
  29. #define CLK_ENABLE_ON_INIT BIT(0)
  30. struct div4_clk {
  31. const char *name;
  32. unsigned int reg;
  33. unsigned int shift;
  34. };
  35. static struct div4_clk div4_clks[] = {
  36. { "i", CPG_FRQCRA, 20 },
  37. { "m3", CPG_FRQCRA, 12 },
  38. { "b", CPG_FRQCRA, 8 },
  39. { "m1", CPG_FRQCRA, 4 },
  40. { "m2", CPG_FRQCRA, 0 },
  41. { "zx", CPG_FRQCRB, 12 },
  42. { "zs", CPG_FRQCRB, 8 },
  43. { "hp", CPG_FRQCRB, 4 },
  44. { NULL, 0, 0 },
  45. };
  46. static const struct clk_div_table div4_div_table[] = {
  47. { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 }, { 4, 8 }, { 5, 12 },
  48. { 6, 16 }, { 7, 18 }, { 8, 24 }, { 10, 36 }, { 11, 48 },
  49. { 12, 10 }, { 0, 0 }
  50. };
  51. static struct clk * __init
  52. r8a73a4_cpg_register_clock(struct device_node *np, struct r8a73a4_cpg *cpg,
  53. void __iomem *base, const char *name)
  54. {
  55. const struct clk_div_table *table = NULL;
  56. const char *parent_name;
  57. unsigned int shift, reg;
  58. unsigned int mult = 1;
  59. unsigned int div = 1;
  60. if (!strcmp(name, "main")) {
  61. u32 ckscr = readl(base + CPG_CKSCR);
  62. switch ((ckscr >> 28) & 3) {
  63. case 0: /* extal1 */
  64. parent_name = of_clk_get_parent_name(np, 0);
  65. break;
  66. case 1: /* extal1 / 2 */
  67. parent_name = of_clk_get_parent_name(np, 0);
  68. div = 2;
  69. break;
  70. case 2: /* extal2 */
  71. parent_name = of_clk_get_parent_name(np, 1);
  72. break;
  73. case 3: /* extal2 / 2 */
  74. parent_name = of_clk_get_parent_name(np, 1);
  75. div = 2;
  76. break;
  77. }
  78. } else if (!strcmp(name, "pll0")) {
  79. /* PLL0/1 are configurable multiplier clocks. Register them as
  80. * fixed factor clocks for now as there's no generic multiplier
  81. * clock implementation and we currently have no need to change
  82. * the multiplier value.
  83. */
  84. u32 value = readl(base + CPG_PLL0CR);
  85. parent_name = "main";
  86. mult = ((value >> 24) & 0x7f) + 1;
  87. if (value & BIT(20))
  88. div = 2;
  89. } else if (!strcmp(name, "pll1")) {
  90. u32 value = readl(base + CPG_PLL1CR);
  91. parent_name = "main";
  92. /* XXX: enable bit? */
  93. mult = ((value >> 24) & 0x7f) + 1;
  94. if (value & BIT(7))
  95. div = 2;
  96. } else if (!strncmp(name, "pll2", 4)) {
  97. u32 value, cr;
  98. switch (name[4]) {
  99. case 0:
  100. cr = CPG_PLL2CR;
  101. break;
  102. case 's':
  103. cr = CPG_PLL2SCR;
  104. break;
  105. case 'h':
  106. cr = CPG_PLL2HCR;
  107. break;
  108. default:
  109. return ERR_PTR(-EINVAL);
  110. }
  111. value = readl(base + cr);
  112. switch ((value >> 5) & 7) {
  113. case 0:
  114. parent_name = "main";
  115. div = 2;
  116. break;
  117. case 1:
  118. parent_name = "extal2";
  119. div = 2;
  120. break;
  121. case 3:
  122. parent_name = "extal2";
  123. div = 4;
  124. break;
  125. case 4:
  126. parent_name = "main";
  127. break;
  128. case 5:
  129. parent_name = "extal2";
  130. break;
  131. default:
  132. pr_warn("%s: unexpected parent of %s\n", __func__,
  133. name);
  134. return ERR_PTR(-EINVAL);
  135. }
  136. /* XXX: enable bit? */
  137. mult = ((value >> 24) & 0x7f) + 1;
  138. } else if (!strcmp(name, "z") || !strcmp(name, "z2")) {
  139. u32 shift = 8;
  140. parent_name = "pll0";
  141. if (name[1] == '2') {
  142. div = 2;
  143. shift = 0;
  144. }
  145. div *= 32;
  146. mult = 0x20 - ((readl(base + CPG_FRQCRC) >> shift) & 0x1f);
  147. } else {
  148. struct div4_clk *c;
  149. for (c = div4_clks; c->name; c++) {
  150. if (!strcmp(name, c->name))
  151. break;
  152. }
  153. if (!c->name)
  154. return ERR_PTR(-EINVAL);
  155. parent_name = "pll1";
  156. table = div4_div_table;
  157. reg = c->reg;
  158. shift = c->shift;
  159. }
  160. if (!table) {
  161. return clk_register_fixed_factor(NULL, name, parent_name, 0,
  162. mult, div);
  163. } else {
  164. return clk_register_divider_table(NULL, name, parent_name, 0,
  165. base + reg, shift, 4, 0,
  166. table, &cpg->lock);
  167. }
  168. }
  169. static void __init r8a73a4_cpg_clocks_init(struct device_node *np)
  170. {
  171. struct r8a73a4_cpg *cpg;
  172. void __iomem *base;
  173. struct clk **clks;
  174. unsigned int i;
  175. int num_clks;
  176. num_clks = of_property_count_strings(np, "clock-output-names");
  177. if (num_clks < 0) {
  178. pr_err("%s: failed to count clocks\n", __func__);
  179. return;
  180. }
  181. cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);
  182. clks = kcalloc(num_clks, sizeof(*clks), GFP_KERNEL);
  183. if (cpg == NULL || clks == NULL) {
  184. /* We're leaking memory on purpose, there's no point in cleaning
  185. * up as the system won't boot anyway.
  186. */
  187. return;
  188. }
  189. spin_lock_init(&cpg->lock);
  190. cpg->data.clks = clks;
  191. cpg->data.clk_num = num_clks;
  192. base = of_iomap(np, 0);
  193. if (WARN_ON(base == NULL))
  194. return;
  195. for (i = 0; i < num_clks; ++i) {
  196. const char *name;
  197. struct clk *clk;
  198. of_property_read_string_index(np, "clock-output-names", i,
  199. &name);
  200. clk = r8a73a4_cpg_register_clock(np, cpg, base, name);
  201. if (IS_ERR(clk))
  202. pr_err("%s: failed to register %pOFn %s clock (%ld)\n",
  203. __func__, np, name, PTR_ERR(clk));
  204. else
  205. cpg->data.clks[i] = clk;
  206. }
  207. of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
  208. }
  209. CLK_OF_DECLARE(r8a73a4_cpg_clks, "renesas,r8a73a4-cpg-clocks",
  210. r8a73a4_cpg_clocks_init);