clk-periph-a10.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Altera Corporation. All rights reserved
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/clk-provider.h>
  7. #include <linux/io.h>
  8. #include <linux/of.h>
  9. #include "clk.h"
  10. #define CLK_MGR_FREE_SHIFT 16
  11. #define CLK_MGR_FREE_MASK 0x7
  12. #define SOCFPGA_MPU_FREE_CLK "mpu_free_clk"
  13. #define SOCFPGA_NOC_FREE_CLK "noc_free_clk"
  14. #define SOCFPGA_SDMMC_FREE_CLK "sdmmc_free_clk"
  15. #define to_socfpga_periph_clk(p) container_of(p, struct socfpga_periph_clk, hw.hw)
  16. static unsigned long clk_periclk_recalc_rate(struct clk_hw *hwclk,
  17. unsigned long parent_rate)
  18. {
  19. struct socfpga_periph_clk *socfpgaclk = to_socfpga_periph_clk(hwclk);
  20. u32 div;
  21. if (socfpgaclk->fixed_div) {
  22. div = socfpgaclk->fixed_div;
  23. } else if (socfpgaclk->div_reg) {
  24. div = readl(socfpgaclk->div_reg) >> socfpgaclk->shift;
  25. div &= GENMASK(socfpgaclk->width - 1, 0);
  26. div += 1;
  27. } else {
  28. div = ((readl(socfpgaclk->hw.reg) & 0x7ff) + 1);
  29. }
  30. return parent_rate / div;
  31. }
  32. static u8 clk_periclk_get_parent(struct clk_hw *hwclk)
  33. {
  34. struct socfpga_periph_clk *socfpgaclk = to_socfpga_periph_clk(hwclk);
  35. u32 clk_src;
  36. const char *name = clk_hw_get_name(hwclk);
  37. clk_src = readl(socfpgaclk->hw.reg);
  38. if (streq(name, SOCFPGA_MPU_FREE_CLK) ||
  39. streq(name, SOCFPGA_NOC_FREE_CLK) ||
  40. streq(name, SOCFPGA_SDMMC_FREE_CLK))
  41. return (clk_src >> CLK_MGR_FREE_SHIFT) &
  42. CLK_MGR_FREE_MASK;
  43. else
  44. return 0;
  45. }
  46. static const struct clk_ops periclk_ops = {
  47. .recalc_rate = clk_periclk_recalc_rate,
  48. .get_parent = clk_periclk_get_parent,
  49. };
  50. static __init void __socfpga_periph_init(struct device_node *node,
  51. const struct clk_ops *ops)
  52. {
  53. u32 reg;
  54. struct clk_hw *hw_clk;
  55. struct socfpga_periph_clk *periph_clk;
  56. const char *clk_name = node->name;
  57. const char *parent_name[SOCFPGA_MAX_PARENTS];
  58. struct clk_init_data init;
  59. int rc;
  60. u32 fixed_div;
  61. u32 div_reg[3];
  62. of_property_read_u32(node, "reg", &reg);
  63. periph_clk = kzalloc(sizeof(*periph_clk), GFP_KERNEL);
  64. if (WARN_ON(!periph_clk))
  65. return;
  66. periph_clk->hw.reg = clk_mgr_a10_base_addr + reg;
  67. rc = of_property_read_u32_array(node, "div-reg", div_reg, 3);
  68. if (!rc) {
  69. periph_clk->div_reg = clk_mgr_a10_base_addr + div_reg[0];
  70. periph_clk->shift = div_reg[1];
  71. periph_clk->width = div_reg[2];
  72. } else {
  73. periph_clk->div_reg = NULL;
  74. }
  75. rc = of_property_read_u32(node, "fixed-divider", &fixed_div);
  76. if (rc)
  77. periph_clk->fixed_div = 0;
  78. else
  79. periph_clk->fixed_div = fixed_div;
  80. of_property_read_string(node, "clock-output-names", &clk_name);
  81. init.name = clk_name;
  82. init.ops = ops;
  83. init.flags = 0;
  84. init.num_parents = of_clk_parent_fill(node, parent_name, SOCFPGA_MAX_PARENTS);
  85. init.parent_names = parent_name;
  86. periph_clk->hw.hw.init = &init;
  87. hw_clk = &periph_clk->hw.hw;
  88. if (clk_hw_register(NULL, hw_clk)) {
  89. kfree(periph_clk);
  90. return;
  91. }
  92. rc = of_clk_add_provider(node, of_clk_src_simple_get, hw_clk);
  93. if (rc < 0) {
  94. pr_err("Could not register clock provider for node:%s\n",
  95. clk_name);
  96. goto err_clk;
  97. }
  98. return;
  99. err_clk:
  100. clk_hw_unregister(hw_clk);
  101. }
  102. void __init socfpga_a10_periph_init(struct device_node *node)
  103. {
  104. __socfpga_periph_init(node, &periclk_ops);
  105. }