clk-gate-a10.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/clk-provider.h>
  17. #include <linux/io.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/of.h>
  20. #include <linux/regmap.h>
  21. #include "clk.h"
  22. #define streq(a, b) (strcmp((a), (b)) == 0)
  23. #define to_socfpga_gate_clk(p) container_of(p, struct socfpga_gate_clk, hw.hw)
  24. /* SDMMC Group for System Manager defines */
  25. #define SYSMGR_SDMMCGRP_CTRL_OFFSET 0x28
  26. static unsigned long socfpga_gate_clk_recalc_rate(struct clk_hw *hwclk,
  27. unsigned long parent_rate)
  28. {
  29. struct socfpga_gate_clk *socfpgaclk = to_socfpga_gate_clk(hwclk);
  30. u32 div = 1, val;
  31. if (socfpgaclk->fixed_div)
  32. div = socfpgaclk->fixed_div;
  33. else if (socfpgaclk->div_reg) {
  34. val = readl(socfpgaclk->div_reg) >> socfpgaclk->shift;
  35. val &= div_mask(socfpgaclk->width);
  36. div = (1 << val);
  37. }
  38. return parent_rate / div;
  39. }
  40. static int socfpga_clk_prepare(struct clk_hw *hwclk)
  41. {
  42. struct socfpga_gate_clk *socfpgaclk = to_socfpga_gate_clk(hwclk);
  43. int i;
  44. u32 hs_timing;
  45. u32 clk_phase[2];
  46. if (socfpgaclk->clk_phase[0] || socfpgaclk->clk_phase[1]) {
  47. for (i = 0; i < ARRAY_SIZE(clk_phase); i++) {
  48. switch (socfpgaclk->clk_phase[i]) {
  49. case 0:
  50. clk_phase[i] = 0;
  51. break;
  52. case 45:
  53. clk_phase[i] = 1;
  54. break;
  55. case 90:
  56. clk_phase[i] = 2;
  57. break;
  58. case 135:
  59. clk_phase[i] = 3;
  60. break;
  61. case 180:
  62. clk_phase[i] = 4;
  63. break;
  64. case 225:
  65. clk_phase[i] = 5;
  66. break;
  67. case 270:
  68. clk_phase[i] = 6;
  69. break;
  70. case 315:
  71. clk_phase[i] = 7;
  72. break;
  73. default:
  74. clk_phase[i] = 0;
  75. break;
  76. }
  77. }
  78. hs_timing = SYSMGR_SDMMC_CTRL_SET(clk_phase[0], clk_phase[1]);
  79. if (!IS_ERR(socfpgaclk->sys_mgr_base_addr))
  80. regmap_write(socfpgaclk->sys_mgr_base_addr,
  81. SYSMGR_SDMMCGRP_CTRL_OFFSET, hs_timing);
  82. else
  83. pr_err("%s: cannot set clk_phase because sys_mgr_base_addr is not available!\n",
  84. __func__);
  85. }
  86. return 0;
  87. }
  88. static struct clk_ops gateclk_ops = {
  89. .prepare = socfpga_clk_prepare,
  90. .recalc_rate = socfpga_gate_clk_recalc_rate,
  91. };
  92. static void __init __socfpga_gate_init(struct device_node *node,
  93. const struct clk_ops *ops)
  94. {
  95. u32 clk_gate[2];
  96. u32 div_reg[3];
  97. u32 clk_phase[2];
  98. u32 fixed_div;
  99. struct clk *clk;
  100. struct socfpga_gate_clk *socfpga_clk;
  101. const char *clk_name = node->name;
  102. const char *parent_name[SOCFPGA_MAX_PARENTS];
  103. struct clk_init_data init;
  104. int rc;
  105. int i = 0;
  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. return;
  141. }
  142. }
  143. of_property_read_string(node, "clock-output-names", &clk_name);
  144. init.name = clk_name;
  145. init.ops = ops;
  146. init.flags = 0;
  147. while (i < SOCFPGA_MAX_PARENTS && (parent_name[i] =
  148. of_clk_get_parent_name(node, i)) != NULL)
  149. i++;
  150. init.parent_names = parent_name;
  151. init.num_parents = i;
  152. socfpga_clk->hw.hw.init = &init;
  153. clk = clk_register(NULL, &socfpga_clk->hw.hw);
  154. if (WARN_ON(IS_ERR(clk))) {
  155. kfree(socfpga_clk);
  156. return;
  157. }
  158. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  159. if (WARN_ON(rc))
  160. return;
  161. }
  162. void __init socfpga_a10_gate_init(struct device_node *node)
  163. {
  164. __socfpga_gate_init(node, &gateclk_ops);
  165. }