clk-sh73a0.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * sh73a0 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/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. struct sh73a0_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_SD0CKCR 0x74
  23. #define CPG_SD1CKCR 0x78
  24. #define CPG_SD2CKCR 0x7c
  25. #define CPG_PLLECR 0xd0
  26. #define CPG_PLL0CR 0xd8
  27. #define CPG_PLL1CR 0x28
  28. #define CPG_PLL2CR 0x2c
  29. #define CPG_PLL3CR 0xdc
  30. #define CPG_CKSCR 0xc0
  31. #define CPG_DSI0PHYCR 0x6c
  32. #define CPG_DSI1PHYCR 0x70
  33. #define CLK_ENABLE_ON_INIT BIT(0)
  34. struct div4_clk {
  35. const char *name;
  36. const char *parent;
  37. unsigned int reg;
  38. unsigned int shift;
  39. };
  40. static const struct div4_clk div4_clks[] = {
  41. { "zg", "pll0", CPG_FRQCRA, 16 },
  42. { "m3", "pll1", CPG_FRQCRA, 12 },
  43. { "b", "pll1", CPG_FRQCRA, 8 },
  44. { "m1", "pll1", CPG_FRQCRA, 4 },
  45. { "m2", "pll1", CPG_FRQCRA, 0 },
  46. { "zx", "pll1", CPG_FRQCRB, 12 },
  47. { "hp", "pll1", CPG_FRQCRB, 4 },
  48. { NULL, NULL, 0, 0 },
  49. };
  50. static const struct clk_div_table div4_div_table[] = {
  51. { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 }, { 4, 8 }, { 5, 12 },
  52. { 6, 16 }, { 7, 18 }, { 8, 24 }, { 10, 36 }, { 11, 48 },
  53. { 12, 7 }, { 0, 0 }
  54. };
  55. static const struct clk_div_table z_div_table[] = {
  56. /* ZSEL == 0 */
  57. { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
  58. { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 10, 1 }, { 11, 1 },
  59. { 12, 1 }, { 13, 1 }, { 14, 1 }, { 15, 1 },
  60. /* ZSEL == 1 */
  61. { 16, 2 }, { 17, 3 }, { 18, 4 }, { 19, 6 }, { 20, 8 }, { 21, 12 },
  62. { 22, 16 }, { 24, 24 }, { 27, 48 }, { 0, 0 }
  63. };
  64. static struct clk * __init
  65. sh73a0_cpg_register_clock(struct device_node *np, struct sh73a0_cpg *cpg,
  66. void __iomem *base, const char *name)
  67. {
  68. const struct clk_div_table *table = NULL;
  69. unsigned int shift, reg, width;
  70. const char *parent_name = NULL;
  71. unsigned int mult = 1;
  72. unsigned int div = 1;
  73. if (!strcmp(name, "main")) {
  74. /* extal1, extal1_div2, extal2, extal2_div2 */
  75. u32 parent_idx = (readl(base + CPG_CKSCR) >> 28) & 3;
  76. parent_name = of_clk_get_parent_name(np, parent_idx >> 1);
  77. div = (parent_idx & 1) + 1;
  78. } else if (!strncmp(name, "pll", 3)) {
  79. void __iomem *enable_reg = base;
  80. u32 enable_bit = name[3] - '0';
  81. parent_name = "main";
  82. switch (enable_bit) {
  83. case 0:
  84. enable_reg += CPG_PLL0CR;
  85. break;
  86. case 1:
  87. enable_reg += CPG_PLL1CR;
  88. break;
  89. case 2:
  90. enable_reg += CPG_PLL2CR;
  91. break;
  92. case 3:
  93. enable_reg += CPG_PLL3CR;
  94. break;
  95. default:
  96. return ERR_PTR(-EINVAL);
  97. }
  98. if (readl(base + CPG_PLLECR) & BIT(enable_bit)) {
  99. mult = ((readl(enable_reg) >> 24) & 0x3f) + 1;
  100. /* handle CFG bit for PLL1 and PLL2 */
  101. if (enable_bit == 1 || enable_bit == 2)
  102. if (readl(enable_reg) & BIT(20))
  103. mult *= 2;
  104. }
  105. } else if (!strcmp(name, "dsi0phy") || !strcmp(name, "dsi1phy")) {
  106. u32 phy_no = name[3] - '0';
  107. void __iomem *dsi_reg = base +
  108. (phy_no ? CPG_DSI1PHYCR : CPG_DSI0PHYCR);
  109. parent_name = phy_no ? "dsi1pck" : "dsi0pck";
  110. mult = readl(dsi_reg);
  111. if (!(mult & 0x8000))
  112. mult = 1;
  113. else
  114. mult = (mult & 0x3f) + 1;
  115. } else if (!strcmp(name, "z")) {
  116. parent_name = "pll0";
  117. table = z_div_table;
  118. reg = CPG_FRQCRB;
  119. shift = 24;
  120. width = 5;
  121. } else {
  122. const struct div4_clk *c;
  123. for (c = div4_clks; c->name; c++) {
  124. if (!strcmp(name, c->name)) {
  125. parent_name = c->parent;
  126. table = div4_div_table;
  127. reg = c->reg;
  128. shift = c->shift;
  129. width = 4;
  130. break;
  131. }
  132. }
  133. if (!c->name)
  134. return ERR_PTR(-EINVAL);
  135. }
  136. if (!table) {
  137. return clk_register_fixed_factor(NULL, name, parent_name, 0,
  138. mult, div);
  139. } else {
  140. return clk_register_divider_table(NULL, name, parent_name, 0,
  141. base + reg, shift, width, 0,
  142. table, &cpg->lock);
  143. }
  144. }
  145. static void __init sh73a0_cpg_clocks_init(struct device_node *np)
  146. {
  147. struct sh73a0_cpg *cpg;
  148. void __iomem *base;
  149. struct clk **clks;
  150. unsigned int i;
  151. int num_clks;
  152. num_clks = of_property_count_strings(np, "clock-output-names");
  153. if (num_clks < 0) {
  154. pr_err("%s: failed to count clocks\n", __func__);
  155. return;
  156. }
  157. cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);
  158. clks = kcalloc(num_clks, sizeof(*clks), GFP_KERNEL);
  159. if (cpg == NULL || clks == NULL) {
  160. /* We're leaking memory on purpose, there's no point in cleaning
  161. * up as the system won't boot anyway.
  162. */
  163. return;
  164. }
  165. spin_lock_init(&cpg->lock);
  166. cpg->data.clks = clks;
  167. cpg->data.clk_num = num_clks;
  168. base = of_iomap(np, 0);
  169. if (WARN_ON(base == NULL))
  170. return;
  171. /* Set SDHI clocks to a known state */
  172. writel(0x108, base + CPG_SD0CKCR);
  173. writel(0x108, base + CPG_SD1CKCR);
  174. writel(0x108, base + CPG_SD2CKCR);
  175. for (i = 0; i < num_clks; ++i) {
  176. const char *name;
  177. struct clk *clk;
  178. of_property_read_string_index(np, "clock-output-names", i,
  179. &name);
  180. clk = sh73a0_cpg_register_clock(np, cpg, base, name);
  181. if (IS_ERR(clk))
  182. pr_err("%s: failed to register %pOFn %s clock (%ld)\n",
  183. __func__, np, name, PTR_ERR(clk));
  184. else
  185. cpg->data.clks[i] = clk;
  186. }
  187. of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
  188. }
  189. CLK_OF_DECLARE(sh73a0_cpg_clks, "renesas,sh73a0-cpg-clocks",
  190. sh73a0_cpg_clocks_init);