clk-sh73a0.c 5.4 KB

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