clk-gate-a10.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 2015 Altera Corporation. All rights reserved
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/io.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/of.h>
  21. #include <linux/regmap.h>
  22. #include "clk.h"
  23. #define streq(a, b) (strcmp((a), (b)) == 0)
  24. #define to_socfpga_gate_clk(p) container_of(p, struct socfpga_gate_clk, hw.hw)
  25. /* SDMMC Group for System Manager defines */
  26. #define SYSMGR_SDMMCGRP_CTRL_OFFSET 0x28
  27. static unsigned long socfpga_gate_clk_recalc_rate(struct clk_hw *hwclk,
  28. unsigned long parent_rate)
  29. {
  30. struct socfpga_gate_clk *socfpgaclk = to_socfpga_gate_clk(hwclk);
  31. u32 div = 1, val;
  32. if (socfpgaclk->fixed_div)
  33. div = socfpgaclk->fixed_div;
  34. else if (socfpgaclk->div_reg) {
  35. val = readl(socfpgaclk->div_reg) >> socfpgaclk->shift;
  36. val &= GENMASK(socfpgaclk->width - 1, 0);
  37. div = (1 << val);
  38. }
  39. return parent_rate / div;
  40. }
  41. static int socfpga_clk_prepare(struct clk_hw *hwclk)
  42. {
  43. struct socfpga_gate_clk *socfpgaclk = to_socfpga_gate_clk(hwclk);
  44. int i;
  45. u32 hs_timing;
  46. u32 clk_phase[2];
  47. if (socfpgaclk->clk_phase[0] || socfpgaclk->clk_phase[1]) {
  48. for (i = 0; i < ARRAY_SIZE(clk_phase); i++) {
  49. switch (socfpgaclk->clk_phase[i]) {
  50. case 0:
  51. clk_phase[i] = 0;
  52. break;
  53. case 45:
  54. clk_phase[i] = 1;
  55. break;
  56. case 90:
  57. clk_phase[i] = 2;
  58. break;
  59. case 135:
  60. clk_phase[i] = 3;
  61. break;
  62. case 180:
  63. clk_phase[i] = 4;
  64. break;
  65. case 225:
  66. clk_phase[i] = 5;
  67. break;
  68. case 270:
  69. clk_phase[i] = 6;
  70. break;
  71. case 315:
  72. clk_phase[i] = 7;
  73. break;
  74. default:
  75. clk_phase[i] = 0;
  76. break;
  77. }
  78. }
  79. hs_timing = SYSMGR_SDMMC_CTRL_SET_AS10(clk_phase[0], clk_phase[1]);
  80. if (!IS_ERR(socfpgaclk->sys_mgr_base_addr))
  81. regmap_write(socfpgaclk->sys_mgr_base_addr,
  82. SYSMGR_SDMMCGRP_CTRL_OFFSET, hs_timing);
  83. else
  84. pr_err("%s: cannot set clk_phase because sys_mgr_base_addr is not available!\n",
  85. __func__);
  86. }
  87. return 0;
  88. }
  89. static struct clk_ops gateclk_ops = {
  90. .prepare = socfpga_clk_prepare,
  91. .recalc_rate = socfpga_gate_clk_recalc_rate,
  92. };
  93. static void __init __socfpga_gate_init(struct device_node *node,
  94. const struct clk_ops *ops)
  95. {
  96. u32 clk_gate[2];
  97. u32 div_reg[3];
  98. u32 clk_phase[2];
  99. u32 fixed_div;
  100. struct clk *clk;
  101. struct socfpga_gate_clk *socfpga_clk;
  102. const char *clk_name = node->name;
  103. const char *parent_name[SOCFPGA_MAX_PARENTS];
  104. struct clk_init_data init;
  105. int rc;
  106. socfpga_clk = kzalloc(sizeof(*socfpga_clk), GFP_KERNEL);
  107. if (WARN_ON(!socfpga_clk))
  108. return;
  109. rc = of_property_read_u32_array(node, "clk-gate", clk_gate, 2);
  110. if (rc)
  111. clk_gate[0] = 0;
  112. if (clk_gate[0]) {
  113. socfpga_clk->hw.reg = clk_mgr_a10_base_addr + clk_gate[0];
  114. socfpga_clk->hw.bit_idx = clk_gate[1];
  115. gateclk_ops.enable = clk_gate_ops.enable;
  116. gateclk_ops.disable = clk_gate_ops.disable;
  117. }
  118. rc = of_property_read_u32(node, "fixed-divider", &fixed_div);
  119. if (rc)
  120. socfpga_clk->fixed_div = 0;
  121. else
  122. socfpga_clk->fixed_div = fixed_div;
  123. rc = of_property_read_u32_array(node, "div-reg", div_reg, 3);
  124. if (!rc) {
  125. socfpga_clk->div_reg = clk_mgr_a10_base_addr + div_reg[0];
  126. socfpga_clk->shift = div_reg[1];
  127. socfpga_clk->width = div_reg[2];
  128. } else {
  129. socfpga_clk->div_reg = NULL;
  130. }
  131. rc = of_property_read_u32_array(node, "clk-phase", clk_phase, 2);
  132. if (!rc) {
  133. socfpga_clk->clk_phase[0] = clk_phase[0];
  134. socfpga_clk->clk_phase[1] = clk_phase[1];
  135. socfpga_clk->sys_mgr_base_addr =
  136. syscon_regmap_lookup_by_compatible("altr,sys-mgr");
  137. if (IS_ERR(socfpga_clk->sys_mgr_base_addr)) {
  138. pr_err("%s: failed to find altr,sys-mgr regmap!\n",
  139. __func__);
  140. kfree(socfpga_clk);
  141. return;
  142. }
  143. }
  144. of_property_read_string(node, "clock-output-names", &clk_name);
  145. init.name = clk_name;
  146. init.ops = ops;
  147. init.flags = 0;
  148. init.num_parents = of_clk_parent_fill(node, parent_name, SOCFPGA_MAX_PARENTS);
  149. init.parent_names = parent_name;
  150. socfpga_clk->hw.hw.init = &init;
  151. clk = clk_register(NULL, &socfpga_clk->hw.hw);
  152. if (WARN_ON(IS_ERR(clk))) {
  153. kfree(socfpga_clk);
  154. return;
  155. }
  156. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  157. if (WARN_ON(rc))
  158. return;
  159. }
  160. void __init socfpga_a10_gate_init(struct device_node *node)
  161. {
  162. __socfpga_gate_init(node, &gateclk_ops);
  163. }