clk.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright 2011-2012 Calxeda, Inc.
  3. * Copyright (C) 2012-2013 Altera Corporation <www.altera.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * Based from clk-highbank.c
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/clkdev.h>
  22. #include <linux/clk-provider.h>
  23. #include <linux/io.h>
  24. #include <linux/of.h>
  25. /* Clock Manager offsets */
  26. #define CLKMGR_CTRL 0x0
  27. #define CLKMGR_BYPASS 0x4
  28. /* Clock bypass bits */
  29. #define MAINPLL_BYPASS (1<<0)
  30. #define SDRAMPLL_BYPASS (1<<1)
  31. #define SDRAMPLL_SRC_BYPASS (1<<2)
  32. #define PERPLL_BYPASS (1<<3)
  33. #define PERPLL_SRC_BYPASS (1<<4)
  34. #define SOCFPGA_PLL_BG_PWRDWN 0
  35. #define SOCFPGA_PLL_EXT_ENA 1
  36. #define SOCFPGA_PLL_PWR_DOWN 2
  37. #define SOCFPGA_PLL_DIVF_MASK 0x0000FFF8
  38. #define SOCFPGA_PLL_DIVF_SHIFT 3
  39. #define SOCFPGA_PLL_DIVQ_MASK 0x003F0000
  40. #define SOCFPGA_PLL_DIVQ_SHIFT 16
  41. extern void __iomem *clk_mgr_base_addr;
  42. struct socfpga_clk {
  43. struct clk_gate hw;
  44. char *parent_name;
  45. char *clk_name;
  46. u32 fixed_div;
  47. };
  48. #define to_socfpga_clk(p) container_of(p, struct socfpga_clk, hw.hw)
  49. static unsigned long clk_pll_recalc_rate(struct clk_hw *hwclk,
  50. unsigned long parent_rate)
  51. {
  52. struct socfpga_clk *socfpgaclk = to_socfpga_clk(hwclk);
  53. unsigned long divf, divq, vco_freq, reg;
  54. unsigned long bypass;
  55. reg = readl(socfpgaclk->hw.reg);
  56. bypass = readl(clk_mgr_base_addr + CLKMGR_BYPASS);
  57. if (bypass & MAINPLL_BYPASS)
  58. return parent_rate;
  59. divf = (reg & SOCFPGA_PLL_DIVF_MASK) >> SOCFPGA_PLL_DIVF_SHIFT;
  60. divq = (reg & SOCFPGA_PLL_DIVQ_MASK) >> SOCFPGA_PLL_DIVQ_SHIFT;
  61. vco_freq = parent_rate * (divf + 1);
  62. return vco_freq / (1 + divq);
  63. }
  64. static struct clk_ops clk_pll_ops = {
  65. .recalc_rate = clk_pll_recalc_rate,
  66. };
  67. static unsigned long clk_periclk_recalc_rate(struct clk_hw *hwclk,
  68. unsigned long parent_rate)
  69. {
  70. struct socfpga_clk *socfpgaclk = to_socfpga_clk(hwclk);
  71. u32 div;
  72. if (socfpgaclk->fixed_div)
  73. div = socfpgaclk->fixed_div;
  74. else
  75. div = ((readl(socfpgaclk->hw.reg) & 0x1ff) + 1);
  76. return parent_rate / div;
  77. }
  78. static const struct clk_ops periclk_ops = {
  79. .recalc_rate = clk_periclk_recalc_rate,
  80. };
  81. static __init struct clk *socfpga_clk_init(struct device_node *node,
  82. const struct clk_ops *ops)
  83. {
  84. u32 reg;
  85. struct clk *clk;
  86. struct socfpga_clk *socfpga_clk;
  87. const char *clk_name = node->name;
  88. const char *parent_name;
  89. struct clk_init_data init;
  90. int rc;
  91. u32 fixed_div;
  92. rc = of_property_read_u32(node, "reg", &reg);
  93. if (WARN_ON(rc))
  94. return NULL;
  95. socfpga_clk = kzalloc(sizeof(*socfpga_clk), GFP_KERNEL);
  96. if (WARN_ON(!socfpga_clk))
  97. return NULL;
  98. socfpga_clk->hw.reg = clk_mgr_base_addr + reg;
  99. rc = of_property_read_u32(node, "fixed-divider", &fixed_div);
  100. if (rc)
  101. socfpga_clk->fixed_div = 0;
  102. else
  103. socfpga_clk->fixed_div = fixed_div;
  104. of_property_read_string(node, "clock-output-names", &clk_name);
  105. init.name = clk_name;
  106. init.ops = ops;
  107. init.flags = 0;
  108. parent_name = of_clk_get_parent_name(node, 0);
  109. init.parent_names = &parent_name;
  110. init.num_parents = 1;
  111. socfpga_clk->hw.hw.init = &init;
  112. if (strcmp(clk_name, "main_pll") || strcmp(clk_name, "periph_pll") ||
  113. strcmp(clk_name, "sdram_pll")) {
  114. socfpga_clk->hw.bit_idx = SOCFPGA_PLL_EXT_ENA;
  115. clk_pll_ops.enable = clk_gate_ops.enable;
  116. clk_pll_ops.disable = clk_gate_ops.disable;
  117. }
  118. clk = clk_register(NULL, &socfpga_clk->hw.hw);
  119. if (WARN_ON(IS_ERR(clk))) {
  120. kfree(socfpga_clk);
  121. return NULL;
  122. }
  123. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  124. return clk;
  125. }
  126. static void __init socfpga_pll_init(struct device_node *node)
  127. {
  128. socfpga_clk_init(node, &clk_pll_ops);
  129. }
  130. CLK_OF_DECLARE(socfpga_pll, "altr,socfpga-pll-clock", socfpga_pll_init);
  131. static void __init socfpga_periph_init(struct device_node *node)
  132. {
  133. socfpga_clk_init(node, &periclk_ops);
  134. }
  135. CLK_OF_DECLARE(socfpga_periph, "altr,socfpga-perip-clk", socfpga_periph_init);
  136. void __init socfpga_init_clocks(void)
  137. {
  138. struct clk *clk;
  139. int ret;
  140. clk = clk_register_fixed_factor(NULL, "smp_twd", "mpuclk", 0, 1, 4);
  141. ret = clk_register_clkdev(clk, NULL, "smp_twd");
  142. if (ret)
  143. pr_err("smp_twd alias not registered\n");
  144. }