clk-gate-a10.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/mfd/syscon.h>
  9. #include <linux/of.h>
  10. #include <linux/regmap.h>
  11. #include "clk.h"
  12. #define streq(a, b) (strcmp((a), (b)) == 0)
  13. #define to_socfpga_gate_clk(p) container_of(p, struct socfpga_gate_clk, hw.hw)
  14. /* SDMMC Group for System Manager defines */
  15. #define SYSMGR_SDMMCGRP_CTRL_OFFSET 0x28
  16. static unsigned long socfpga_gate_clk_recalc_rate(struct clk_hw *hwclk,
  17. unsigned long parent_rate)
  18. {
  19. struct socfpga_gate_clk *socfpgaclk = to_socfpga_gate_clk(hwclk);
  20. u32 div = 1, val;
  21. if (socfpgaclk->fixed_div)
  22. div = socfpgaclk->fixed_div;
  23. else if (socfpgaclk->div_reg) {
  24. val = readl(socfpgaclk->div_reg) >> socfpgaclk->shift;
  25. val &= GENMASK(socfpgaclk->width - 1, 0);
  26. div = (1 << val);
  27. }
  28. return parent_rate / div;
  29. }
  30. static struct clk_ops gateclk_ops = {
  31. .recalc_rate = socfpga_gate_clk_recalc_rate,
  32. };
  33. static void __init __socfpga_gate_init(struct device_node *node,
  34. const struct clk_ops *ops)
  35. {
  36. u32 clk_gate[2];
  37. u32 div_reg[3];
  38. u32 fixed_div;
  39. struct clk_hw *hw_clk;
  40. struct socfpga_gate_clk *socfpga_clk;
  41. const char *clk_name = node->name;
  42. const char *parent_name[SOCFPGA_MAX_PARENTS];
  43. struct clk_init_data init;
  44. int rc;
  45. socfpga_clk = kzalloc(sizeof(*socfpga_clk), GFP_KERNEL);
  46. if (WARN_ON(!socfpga_clk))
  47. return;
  48. rc = of_property_read_u32_array(node, "clk-gate", clk_gate, 2);
  49. if (rc)
  50. clk_gate[0] = 0;
  51. if (clk_gate[0]) {
  52. socfpga_clk->hw.reg = clk_mgr_a10_base_addr + clk_gate[0];
  53. socfpga_clk->hw.bit_idx = clk_gate[1];
  54. gateclk_ops.enable = clk_gate_ops.enable;
  55. gateclk_ops.disable = clk_gate_ops.disable;
  56. }
  57. rc = of_property_read_u32(node, "fixed-divider", &fixed_div);
  58. if (rc)
  59. socfpga_clk->fixed_div = 0;
  60. else
  61. socfpga_clk->fixed_div = fixed_div;
  62. rc = of_property_read_u32_array(node, "div-reg", div_reg, 3);
  63. if (!rc) {
  64. socfpga_clk->div_reg = clk_mgr_a10_base_addr + div_reg[0];
  65. socfpga_clk->shift = div_reg[1];
  66. socfpga_clk->width = div_reg[2];
  67. } else {
  68. socfpga_clk->div_reg = NULL;
  69. }
  70. of_property_read_string(node, "clock-output-names", &clk_name);
  71. init.name = clk_name;
  72. init.ops = ops;
  73. init.flags = 0;
  74. init.num_parents = of_clk_parent_fill(node, parent_name, SOCFPGA_MAX_PARENTS);
  75. init.parent_names = parent_name;
  76. socfpga_clk->hw.hw.init = &init;
  77. hw_clk = &socfpga_clk->hw.hw;
  78. rc = clk_hw_register(NULL, hw_clk);
  79. if (rc) {
  80. pr_err("Could not register clock:%s\n", clk_name);
  81. goto err_clk_hw_register;
  82. }
  83. rc = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw_clk);
  84. if (rc) {
  85. pr_err("Could not register clock provider for node:%s\n",
  86. clk_name);
  87. goto err_of_clk_add_hw_provider;
  88. }
  89. return;
  90. err_of_clk_add_hw_provider:
  91. clk_hw_unregister(hw_clk);
  92. err_clk_hw_register:
  93. kfree(socfpga_clk);
  94. }
  95. void __init socfpga_a10_gate_init(struct device_node *node)
  96. {
  97. __socfpga_gate_init(node, &gateclk_ops);
  98. }