clk-pll.c 3.6 KB

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