clk-r8a7740.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * r8a7740 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 r8a7740_cpg {
  17. struct clk_onecell_data data;
  18. spinlock_t lock;
  19. };
  20. #define CPG_FRQCRA 0x00
  21. #define CPG_FRQCRB 0x04
  22. #define CPG_PLLC2CR 0x2c
  23. #define CPG_USBCKCR 0x8c
  24. #define CPG_FRQCRC 0xe0
  25. #define CLK_ENABLE_ON_INIT BIT(0)
  26. struct div4_clk {
  27. const char *name;
  28. unsigned int reg;
  29. unsigned int shift;
  30. int flags;
  31. };
  32. static struct div4_clk div4_clks[] = {
  33. { "i", CPG_FRQCRA, 20, CLK_ENABLE_ON_INIT },
  34. { "zg", CPG_FRQCRA, 16, CLK_ENABLE_ON_INIT },
  35. { "b", CPG_FRQCRA, 8, CLK_ENABLE_ON_INIT },
  36. { "m1", CPG_FRQCRA, 4, CLK_ENABLE_ON_INIT },
  37. { "hp", CPG_FRQCRB, 4, 0 },
  38. { "hpp", CPG_FRQCRC, 20, 0 },
  39. { "usbp", CPG_FRQCRC, 16, 0 },
  40. { "s", CPG_FRQCRC, 12, 0 },
  41. { "zb", CPG_FRQCRC, 8, 0 },
  42. { "m3", CPG_FRQCRC, 4, 0 },
  43. { "cp", CPG_FRQCRC, 0, 0 },
  44. { NULL, 0, 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 }, { 9, 32 }, { 10, 36 }, { 11, 48 },
  49. { 13, 72 }, { 14, 96 }, { 0, 0 }
  50. };
  51. static u32 cpg_mode __initdata;
  52. static struct clk * __init
  53. r8a7740_cpg_register_clock(struct device_node *np, struct r8a7740_cpg *cpg,
  54. void __iomem *base, const char *name)
  55. {
  56. const struct clk_div_table *table = NULL;
  57. const char *parent_name;
  58. unsigned int shift, reg;
  59. unsigned int mult = 1;
  60. unsigned int div = 1;
  61. if (!strcmp(name, "r")) {
  62. switch (cpg_mode & (BIT(2) | BIT(1))) {
  63. case BIT(1) | BIT(2):
  64. /* extal1 */
  65. parent_name = of_clk_get_parent_name(np, 0);
  66. div = 2048;
  67. break;
  68. case BIT(2):
  69. /* extal1 */
  70. parent_name = of_clk_get_parent_name(np, 0);
  71. div = 1024;
  72. break;
  73. default:
  74. /* extalr */
  75. parent_name = of_clk_get_parent_name(np, 2);
  76. break;
  77. }
  78. } else if (!strcmp(name, "system")) {
  79. parent_name = of_clk_get_parent_name(np, 0);
  80. if (cpg_mode & BIT(1))
  81. div = 2;
  82. } else if (!strcmp(name, "pllc0")) {
  83. /* PLLC0/1 are configurable multiplier clocks. Register them as
  84. * fixed factor clocks for now as there's no generic multiplier
  85. * clock implementation and we currently have no need to change
  86. * the multiplier value.
  87. */
  88. u32 value = readl(base + CPG_FRQCRC);
  89. parent_name = "system";
  90. mult = ((value >> 24) & 0x7f) + 1;
  91. } else if (!strcmp(name, "pllc1")) {
  92. u32 value = readl(base + CPG_FRQCRA);
  93. parent_name = "system";
  94. mult = ((value >> 24) & 0x7f) + 1;
  95. div = 2;
  96. } else if (!strcmp(name, "pllc2")) {
  97. u32 value = readl(base + CPG_PLLC2CR);
  98. parent_name = "system";
  99. mult = ((value >> 24) & 0x3f) + 1;
  100. } else if (!strcmp(name, "usb24s")) {
  101. u32 value = readl(base + CPG_USBCKCR);
  102. if (value & BIT(7))
  103. /* extal2 */
  104. parent_name = of_clk_get_parent_name(np, 1);
  105. else
  106. parent_name = "system";
  107. if (!(value & BIT(6)))
  108. div = 2;
  109. } else {
  110. struct div4_clk *c;
  111. for (c = div4_clks; c->name; c++) {
  112. if (!strcmp(name, c->name)) {
  113. parent_name = "pllc1";
  114. table = div4_div_table;
  115. reg = c->reg;
  116. shift = c->shift;
  117. break;
  118. }
  119. }
  120. if (!c->name)
  121. return ERR_PTR(-EINVAL);
  122. }
  123. if (!table) {
  124. return clk_register_fixed_factor(NULL, name, parent_name, 0,
  125. mult, div);
  126. } else {
  127. return clk_register_divider_table(NULL, name, parent_name, 0,
  128. base + reg, shift, 4, 0,
  129. table, &cpg->lock);
  130. }
  131. }
  132. static void __init r8a7740_cpg_clocks_init(struct device_node *np)
  133. {
  134. struct r8a7740_cpg *cpg;
  135. void __iomem *base;
  136. struct clk **clks;
  137. unsigned int i;
  138. int num_clks;
  139. if (of_property_read_u32(np, "renesas,mode", &cpg_mode))
  140. pr_warn("%s: missing renesas,mode property\n", __func__);
  141. num_clks = of_property_count_strings(np, "clock-output-names");
  142. if (num_clks < 0) {
  143. pr_err("%s: failed to count clocks\n", __func__);
  144. return;
  145. }
  146. cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);
  147. clks = kcalloc(num_clks, sizeof(*clks), GFP_KERNEL);
  148. if (cpg == NULL || clks == NULL) {
  149. /* We're leaking memory on purpose, there's no point in cleaning
  150. * up as the system won't boot anyway.
  151. */
  152. return;
  153. }
  154. spin_lock_init(&cpg->lock);
  155. cpg->data.clks = clks;
  156. cpg->data.clk_num = num_clks;
  157. base = of_iomap(np, 0);
  158. if (WARN_ON(base == NULL))
  159. return;
  160. for (i = 0; i < num_clks; ++i) {
  161. const char *name;
  162. struct clk *clk;
  163. of_property_read_string_index(np, "clock-output-names", i,
  164. &name);
  165. clk = r8a7740_cpg_register_clock(np, cpg, base, name);
  166. if (IS_ERR(clk))
  167. pr_err("%s: failed to register %pOFn %s clock (%ld)\n",
  168. __func__, np, name, PTR_ERR(clk));
  169. else
  170. cpg->data.clks[i] = clk;
  171. }
  172. of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
  173. }
  174. CLK_OF_DECLARE(r8a7740_cpg_clks, "renesas,r8a7740-cpg-clocks",
  175. r8a7740_cpg_clocks_init);