berlin2-pll.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2014 Marvell Technology Group Ltd.
  4. *
  5. * Alexandre Belloni <[email protected]>
  6. * Sebastian Hesselbarth <[email protected]>
  7. */
  8. #include <linux/clk-provider.h>
  9. #include <linux/io.h>
  10. #include <linux/kernel.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/slab.h>
  14. #include <asm/div64.h>
  15. #include "berlin2-div.h"
  16. #include "berlin2-pll.h"
  17. struct berlin2_pll {
  18. struct clk_hw hw;
  19. void __iomem *base;
  20. struct berlin2_pll_map map;
  21. };
  22. #define to_berlin2_pll(hw) container_of(hw, struct berlin2_pll, hw)
  23. #define SPLL_CTRL0 0x00
  24. #define SPLL_CTRL1 0x04
  25. #define SPLL_CTRL2 0x08
  26. #define SPLL_CTRL3 0x0c
  27. #define SPLL_CTRL4 0x10
  28. #define FBDIV_MASK 0x1ff
  29. #define RFDIV_MASK 0x1f
  30. #define DIVSEL_MASK 0xf
  31. /*
  32. * The output frequency formula for the pll is:
  33. * clkout = fbdiv / refdiv * parent / vcodiv
  34. */
  35. static unsigned long
  36. berlin2_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  37. {
  38. struct berlin2_pll *pll = to_berlin2_pll(hw);
  39. struct berlin2_pll_map *map = &pll->map;
  40. u32 val, fbdiv, rfdiv, vcodivsel, vcodiv;
  41. u64 rate = parent_rate;
  42. val = readl_relaxed(pll->base + SPLL_CTRL0);
  43. fbdiv = (val >> map->fbdiv_shift) & FBDIV_MASK;
  44. rfdiv = (val >> map->rfdiv_shift) & RFDIV_MASK;
  45. if (rfdiv == 0) {
  46. pr_warn("%s has zero rfdiv\n", clk_hw_get_name(hw));
  47. rfdiv = 1;
  48. }
  49. val = readl_relaxed(pll->base + SPLL_CTRL1);
  50. vcodivsel = (val >> map->divsel_shift) & DIVSEL_MASK;
  51. vcodiv = map->vcodiv[vcodivsel];
  52. if (vcodiv == 0) {
  53. pr_warn("%s has zero vcodiv (index %d)\n",
  54. clk_hw_get_name(hw), vcodivsel);
  55. vcodiv = 1;
  56. }
  57. rate *= fbdiv * map->mult;
  58. do_div(rate, rfdiv * vcodiv);
  59. return (unsigned long)rate;
  60. }
  61. static const struct clk_ops berlin2_pll_ops = {
  62. .recalc_rate = berlin2_pll_recalc_rate,
  63. };
  64. int __init
  65. berlin2_pll_register(const struct berlin2_pll_map *map,
  66. void __iomem *base, const char *name,
  67. const char *parent_name, unsigned long flags)
  68. {
  69. struct clk_init_data init;
  70. struct berlin2_pll *pll;
  71. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  72. if (!pll)
  73. return -ENOMEM;
  74. /* copy pll_map to allow __initconst */
  75. memcpy(&pll->map, map, sizeof(*map));
  76. pll->base = base;
  77. pll->hw.init = &init;
  78. init.name = name;
  79. init.ops = &berlin2_pll_ops;
  80. init.parent_names = &parent_name;
  81. init.num_parents = 1;
  82. init.flags = flags;
  83. return clk_hw_register(NULL, &pll->hw);
  84. }