clk-pll-a10.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/of.h>
  20. #include <linux/of_address.h>
  21. #include "clk.h"
  22. /* Clock Manager offsets */
  23. #define CLK_MGR_PLL_CLK_SRC_SHIFT 8
  24. #define CLK_MGR_PLL_CLK_SRC_MASK 0x3
  25. /* Clock bypass bits */
  26. #define SOCFPGA_PLL_BG_PWRDWN 0
  27. #define SOCFPGA_PLL_PWR_DOWN 1
  28. #define SOCFPGA_PLL_EXT_ENA 2
  29. #define SOCFPGA_PLL_DIVF_MASK 0x00001FFF
  30. #define SOCFPGA_PLL_DIVF_SHIFT 0
  31. #define SOCFPGA_PLL_DIVQ_MASK 0x003F0000
  32. #define SOCFPGA_PLL_DIVQ_SHIFT 16
  33. #define SOCFGPA_MAX_PARENTS 5
  34. #define SOCFPGA_MAIN_PLL_CLK "main_pll"
  35. #define SOCFPGA_PERIP_PLL_CLK "periph_pll"
  36. #define to_socfpga_clk(p) container_of(p, struct socfpga_pll, hw.hw)
  37. void __iomem *clk_mgr_a10_base_addr;
  38. static unsigned long clk_pll_recalc_rate(struct clk_hw *hwclk,
  39. unsigned long parent_rate)
  40. {
  41. struct socfpga_pll *socfpgaclk = to_socfpga_clk(hwclk);
  42. unsigned long divf, divq, reg;
  43. unsigned long long vco_freq;
  44. /* read VCO1 reg for numerator and denominator */
  45. reg = readl(socfpgaclk->hw.reg + 0x4);
  46. divf = (reg & SOCFPGA_PLL_DIVF_MASK) >> SOCFPGA_PLL_DIVF_SHIFT;
  47. divq = (reg & SOCFPGA_PLL_DIVQ_MASK) >> SOCFPGA_PLL_DIVQ_SHIFT;
  48. vco_freq = (unsigned long long)parent_rate * (divf + 1);
  49. do_div(vco_freq, (1 + divq));
  50. return (unsigned long)vco_freq;
  51. }
  52. static u8 clk_pll_get_parent(struct clk_hw *hwclk)
  53. {
  54. struct socfpga_pll *socfpgaclk = to_socfpga_clk(hwclk);
  55. u32 pll_src;
  56. pll_src = readl(socfpgaclk->hw.reg);
  57. return (pll_src >> CLK_MGR_PLL_CLK_SRC_SHIFT) &
  58. CLK_MGR_PLL_CLK_SRC_MASK;
  59. }
  60. static struct clk_ops clk_pll_ops = {
  61. .recalc_rate = clk_pll_recalc_rate,
  62. .get_parent = clk_pll_get_parent,
  63. };
  64. static struct clk * __init __socfpga_pll_init(struct device_node *node,
  65. const struct clk_ops *ops)
  66. {
  67. u32 reg;
  68. struct clk *clk;
  69. struct socfpga_pll *pll_clk;
  70. const char *clk_name = node->name;
  71. const char *parent_name[SOCFGPA_MAX_PARENTS];
  72. struct clk_init_data init;
  73. struct device_node *clkmgr_np;
  74. int rc;
  75. int i = 0;
  76. of_property_read_u32(node, "reg", &reg);
  77. pll_clk = kzalloc(sizeof(*pll_clk), GFP_KERNEL);
  78. if (WARN_ON(!pll_clk))
  79. return NULL;
  80. clkmgr_np = of_find_compatible_node(NULL, NULL, "altr,clk-mgr");
  81. clk_mgr_a10_base_addr = of_iomap(clkmgr_np, 0);
  82. BUG_ON(!clk_mgr_a10_base_addr);
  83. pll_clk->hw.reg = clk_mgr_a10_base_addr + reg;
  84. of_property_read_string(node, "clock-output-names", &clk_name);
  85. init.name = clk_name;
  86. init.ops = ops;
  87. init.flags = 0;
  88. while (i < SOCFGPA_MAX_PARENTS && (parent_name[i] =
  89. of_clk_get_parent_name(node, i)) != NULL)
  90. i++;
  91. init.num_parents = i;
  92. init.parent_names = parent_name;
  93. pll_clk->hw.hw.init = &init;
  94. pll_clk->hw.bit_idx = SOCFPGA_PLL_EXT_ENA;
  95. clk_pll_ops.enable = clk_gate_ops.enable;
  96. clk_pll_ops.disable = clk_gate_ops.disable;
  97. clk = clk_register(NULL, &pll_clk->hw.hw);
  98. if (WARN_ON(IS_ERR(clk))) {
  99. kfree(pll_clk);
  100. return NULL;
  101. }
  102. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  103. return clk;
  104. }
  105. void __init socfpga_a10_pll_init(struct device_node *node)
  106. {
  107. __socfpga_pll_init(node, &clk_pll_ops);
  108. }