clk-periph-a10.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (C) 2015 Altera Corporation. All rights reserved
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include "clk.h"
  21. #define CLK_MGR_FREE_SHIFT 16
  22. #define CLK_MGR_FREE_MASK 0x7
  23. #define SOCFPGA_MPU_FREE_CLK "mpu_free_clk"
  24. #define SOCFPGA_NOC_FREE_CLK "noc_free_clk"
  25. #define SOCFPGA_SDMMC_FREE_CLK "sdmmc_free_clk"
  26. #define to_socfpga_periph_clk(p) container_of(p, struct socfpga_periph_clk, hw.hw)
  27. static unsigned long clk_periclk_recalc_rate(struct clk_hw *hwclk,
  28. unsigned long parent_rate)
  29. {
  30. struct socfpga_periph_clk *socfpgaclk = to_socfpga_periph_clk(hwclk);
  31. u32 div;
  32. if (socfpgaclk->fixed_div) {
  33. div = socfpgaclk->fixed_div;
  34. } else if (socfpgaclk->div_reg) {
  35. div = readl(socfpgaclk->div_reg) >> socfpgaclk->shift;
  36. div &= GENMASK(socfpgaclk->width - 1, 0);
  37. div += 1;
  38. } else {
  39. div = ((readl(socfpgaclk->hw.reg) & 0x7ff) + 1);
  40. }
  41. return parent_rate / div;
  42. }
  43. static u8 clk_periclk_get_parent(struct clk_hw *hwclk)
  44. {
  45. struct socfpga_periph_clk *socfpgaclk = to_socfpga_periph_clk(hwclk);
  46. u32 clk_src;
  47. clk_src = readl(socfpgaclk->hw.reg);
  48. if (streq(hwclk->init->name, SOCFPGA_MPU_FREE_CLK) ||
  49. streq(hwclk->init->name, SOCFPGA_NOC_FREE_CLK) ||
  50. streq(hwclk->init->name, SOCFPGA_SDMMC_FREE_CLK))
  51. return (clk_src >> CLK_MGR_FREE_SHIFT) &
  52. CLK_MGR_FREE_MASK;
  53. else
  54. return 0;
  55. }
  56. static const struct clk_ops periclk_ops = {
  57. .recalc_rate = clk_periclk_recalc_rate,
  58. .get_parent = clk_periclk_get_parent,
  59. };
  60. static __init void __socfpga_periph_init(struct device_node *node,
  61. const struct clk_ops *ops)
  62. {
  63. u32 reg;
  64. struct clk *clk;
  65. struct socfpga_periph_clk *periph_clk;
  66. const char *clk_name = node->name;
  67. const char *parent_name;
  68. struct clk_init_data init;
  69. int rc;
  70. u32 fixed_div;
  71. u32 div_reg[3];
  72. of_property_read_u32(node, "reg", &reg);
  73. periph_clk = kzalloc(sizeof(*periph_clk), GFP_KERNEL);
  74. if (WARN_ON(!periph_clk))
  75. return;
  76. periph_clk->hw.reg = clk_mgr_a10_base_addr + reg;
  77. rc = of_property_read_u32_array(node, "div-reg", div_reg, 3);
  78. if (!rc) {
  79. periph_clk->div_reg = clk_mgr_a10_base_addr + div_reg[0];
  80. periph_clk->shift = div_reg[1];
  81. periph_clk->width = div_reg[2];
  82. } else {
  83. periph_clk->div_reg = NULL;
  84. }
  85. rc = of_property_read_u32(node, "fixed-divider", &fixed_div);
  86. if (rc)
  87. periph_clk->fixed_div = 0;
  88. else
  89. periph_clk->fixed_div = fixed_div;
  90. of_property_read_string(node, "clock-output-names", &clk_name);
  91. init.name = clk_name;
  92. init.ops = ops;
  93. init.flags = 0;
  94. parent_name = of_clk_get_parent_name(node, 0);
  95. init.num_parents = 1;
  96. init.parent_names = &parent_name;
  97. periph_clk->hw.hw.init = &init;
  98. clk = clk_register(NULL, &periph_clk->hw.hw);
  99. if (WARN_ON(IS_ERR(clk))) {
  100. kfree(periph_clk);
  101. return;
  102. }
  103. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  104. if (rc < 0) {
  105. pr_err("Could not register clock provider for node:%s\n",
  106. clk_name);
  107. goto err_clk;
  108. }
  109. return;
  110. err_clk:
  111. clk_unregister(clk);
  112. }
  113. void __init socfpga_a10_periph_init(struct device_node *node)
  114. {
  115. __socfpga_periph_init(node, &periclk_ops);
  116. }