clk-r8a7740.c 5.0 KB

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