clk-boston.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016-2017 Imagination Technologies
  4. * Author: Paul Burton <[email protected]>
  5. */
  6. #define pr_fmt(fmt) "clk-boston: " fmt
  7. #include <linux/clk-provider.h>
  8. #include <linux/kernel.h>
  9. #include <linux/of.h>
  10. #include <linux/regmap.h>
  11. #include <linux/slab.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <dt-bindings/clock/boston-clock.h>
  14. #define BOSTON_PLAT_MMCMDIV 0x30
  15. # define BOSTON_PLAT_MMCMDIV_CLK0DIV (0xff << 0)
  16. # define BOSTON_PLAT_MMCMDIV_INPUT (0xff << 8)
  17. # define BOSTON_PLAT_MMCMDIV_MUL (0xff << 16)
  18. # define BOSTON_PLAT_MMCMDIV_CLK1DIV (0xff << 24)
  19. #define BOSTON_CLK_COUNT 3
  20. static u32 ext_field(u32 val, u32 mask)
  21. {
  22. return (val & mask) >> (ffs(mask) - 1);
  23. }
  24. static void __init clk_boston_setup(struct device_node *np)
  25. {
  26. unsigned long in_freq, cpu_freq, sys_freq;
  27. uint mmcmdiv, mul, cpu_div, sys_div;
  28. struct clk_hw_onecell_data *onecell;
  29. struct regmap *regmap;
  30. struct clk_hw *hw;
  31. int err;
  32. regmap = syscon_node_to_regmap(np->parent);
  33. if (IS_ERR(regmap)) {
  34. pr_err("failed to find regmap\n");
  35. return;
  36. }
  37. err = regmap_read(regmap, BOSTON_PLAT_MMCMDIV, &mmcmdiv);
  38. if (err) {
  39. pr_err("failed to read mmcm_div register: %d\n", err);
  40. return;
  41. }
  42. in_freq = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_INPUT) * 1000000;
  43. mul = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_MUL);
  44. sys_div = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_CLK0DIV);
  45. sys_freq = mult_frac(in_freq, mul, sys_div);
  46. cpu_div = ext_field(mmcmdiv, BOSTON_PLAT_MMCMDIV_CLK1DIV);
  47. cpu_freq = mult_frac(in_freq, mul, cpu_div);
  48. onecell = kzalloc(struct_size(onecell, hws, BOSTON_CLK_COUNT),
  49. GFP_KERNEL);
  50. if (!onecell)
  51. return;
  52. onecell->num = BOSTON_CLK_COUNT;
  53. hw = clk_hw_register_fixed_rate(NULL, "input", NULL, 0, in_freq);
  54. if (IS_ERR(hw)) {
  55. pr_err("failed to register input clock: %ld\n", PTR_ERR(hw));
  56. goto fail_input;
  57. }
  58. onecell->hws[BOSTON_CLK_INPUT] = hw;
  59. hw = clk_hw_register_fixed_rate(NULL, "sys", "input", 0, sys_freq);
  60. if (IS_ERR(hw)) {
  61. pr_err("failed to register sys clock: %ld\n", PTR_ERR(hw));
  62. goto fail_sys;
  63. }
  64. onecell->hws[BOSTON_CLK_SYS] = hw;
  65. hw = clk_hw_register_fixed_rate(NULL, "cpu", "input", 0, cpu_freq);
  66. if (IS_ERR(hw)) {
  67. pr_err("failed to register cpu clock: %ld\n", PTR_ERR(hw));
  68. goto fail_cpu;
  69. }
  70. onecell->hws[BOSTON_CLK_CPU] = hw;
  71. err = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, onecell);
  72. if (err) {
  73. pr_err("failed to add DT provider: %d\n", err);
  74. goto fail_clk_add;
  75. }
  76. return;
  77. fail_clk_add:
  78. clk_hw_unregister_fixed_rate(onecell->hws[BOSTON_CLK_CPU]);
  79. fail_cpu:
  80. clk_hw_unregister_fixed_rate(onecell->hws[BOSTON_CLK_SYS]);
  81. fail_sys:
  82. clk_hw_unregister_fixed_rate(onecell->hws[BOSTON_CLK_INPUT]);
  83. fail_input:
  84. kfree(onecell);
  85. }
  86. /*
  87. * Use CLK_OF_DECLARE so that this driver is probed early enough to provide the
  88. * CPU frequency for use with the GIC or cop0 counters/timers.
  89. */
  90. CLK_OF_DECLARE(clk_boston, "img,boston-clock", clk_boston_setup);