clk-r8a73a4.c 5.4 KB

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