common.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Marvell EBU SoC common clock handling
  3. *
  4. * Copyright (C) 2012 Marvell
  5. *
  6. * Gregory CLEMENT <gregory.clement@free-electrons.com>
  7. * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
  8. * Andrew Lunn <andrew@lunn.ch>
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/clk.h>
  16. #include <linux/clkdev.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include "common.h"
  22. /*
  23. * Core Clocks
  24. */
  25. static struct clk_onecell_data clk_data;
  26. void __init mvebu_coreclk_setup(struct device_node *np,
  27. const struct coreclk_soc_desc *desc)
  28. {
  29. const char *tclk_name = "tclk";
  30. const char *cpuclk_name = "cpuclk";
  31. void __iomem *base;
  32. unsigned long rate;
  33. int n;
  34. base = of_iomap(np, 0);
  35. if (WARN_ON(!base))
  36. return;
  37. /* Allocate struct for TCLK, cpu clk, and core ratio clocks */
  38. clk_data.clk_num = 2 + desc->num_ratios;
  39. clk_data.clks = kzalloc(clk_data.clk_num * sizeof(struct clk *),
  40. GFP_KERNEL);
  41. if (WARN_ON(!clk_data.clks)) {
  42. iounmap(base);
  43. return;
  44. }
  45. /* Register TCLK */
  46. of_property_read_string_index(np, "clock-output-names", 0,
  47. &tclk_name);
  48. rate = desc->get_tclk_freq(base);
  49. clk_data.clks[0] = clk_register_fixed_rate(NULL, tclk_name, NULL,
  50. CLK_IS_ROOT, rate);
  51. WARN_ON(IS_ERR(clk_data.clks[0]));
  52. /* Register CPU clock */
  53. of_property_read_string_index(np, "clock-output-names", 1,
  54. &cpuclk_name);
  55. rate = desc->get_cpu_freq(base);
  56. clk_data.clks[1] = clk_register_fixed_rate(NULL, cpuclk_name, NULL,
  57. CLK_IS_ROOT, rate);
  58. WARN_ON(IS_ERR(clk_data.clks[1]));
  59. /* Register fixed-factor clocks derived from CPU clock */
  60. for (n = 0; n < desc->num_ratios; n++) {
  61. const char *rclk_name = desc->ratios[n].name;
  62. int mult, div;
  63. of_property_read_string_index(np, "clock-output-names",
  64. 2+n, &rclk_name);
  65. desc->get_clk_ratio(base, desc->ratios[n].id, &mult, &div);
  66. clk_data.clks[2+n] = clk_register_fixed_factor(NULL, rclk_name,
  67. cpuclk_name, 0, mult, div);
  68. WARN_ON(IS_ERR(clk_data.clks[2+n]));
  69. };
  70. /* SAR register isn't needed anymore */
  71. iounmap(base);
  72. of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
  73. }
  74. /*
  75. * Clock Gating Control
  76. */
  77. struct clk_gating_ctrl {
  78. spinlock_t lock;
  79. struct clk **gates;
  80. int num_gates;
  81. };
  82. #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
  83. static struct clk *clk_gating_get_src(
  84. struct of_phandle_args *clkspec, void *data)
  85. {
  86. struct clk_gating_ctrl *ctrl = (struct clk_gating_ctrl *)data;
  87. int n;
  88. if (clkspec->args_count < 1)
  89. return ERR_PTR(-EINVAL);
  90. for (n = 0; n < ctrl->num_gates; n++) {
  91. struct clk_gate *gate =
  92. to_clk_gate(__clk_get_hw(ctrl->gates[n]));
  93. if (clkspec->args[0] == gate->bit_idx)
  94. return ctrl->gates[n];
  95. }
  96. return ERR_PTR(-ENODEV);
  97. }
  98. void __init mvebu_clk_gating_setup(struct device_node *np,
  99. const struct clk_gating_soc_desc *desc)
  100. {
  101. struct clk_gating_ctrl *ctrl;
  102. struct clk *clk;
  103. void __iomem *base;
  104. const char *default_parent = NULL;
  105. int n;
  106. base = of_iomap(np, 0);
  107. if (WARN_ON(!base))
  108. return;
  109. clk = of_clk_get(np, 0);
  110. if (!IS_ERR(clk)) {
  111. default_parent = __clk_get_name(clk);
  112. clk_put(clk);
  113. }
  114. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  115. if (WARN_ON(!ctrl))
  116. goto ctrl_out;
  117. spin_lock_init(&ctrl->lock);
  118. /* Count, allocate, and register clock gates */
  119. for (n = 0; desc[n].name;)
  120. n++;
  121. ctrl->num_gates = n;
  122. ctrl->gates = kzalloc(ctrl->num_gates * sizeof(struct clk *),
  123. GFP_KERNEL);
  124. if (WARN_ON(!ctrl->gates))
  125. goto gates_out;
  126. for (n = 0; n < ctrl->num_gates; n++) {
  127. const char *parent =
  128. (desc[n].parent) ? desc[n].parent : default_parent;
  129. ctrl->gates[n] = clk_register_gate(NULL, desc[n].name, parent,
  130. desc[n].flags, base, desc[n].bit_idx,
  131. 0, &ctrl->lock);
  132. WARN_ON(IS_ERR(ctrl->gates[n]));
  133. }
  134. of_clk_add_provider(np, clk_gating_get_src, ctrl);
  135. return;
  136. gates_out:
  137. kfree(ctrl);
  138. ctrl_out:
  139. iounmap(base);
  140. }