common.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. return;
  43. /* Register TCLK */
  44. of_property_read_string_index(np, "clock-output-names", 0,
  45. &tclk_name);
  46. rate = desc->get_tclk_freq(base);
  47. clk_data.clks[0] = clk_register_fixed_rate(NULL, tclk_name, NULL,
  48. CLK_IS_ROOT, rate);
  49. WARN_ON(IS_ERR(clk_data.clks[0]));
  50. /* Register CPU clock */
  51. of_property_read_string_index(np, "clock-output-names", 1,
  52. &cpuclk_name);
  53. rate = desc->get_cpu_freq(base);
  54. clk_data.clks[1] = clk_register_fixed_rate(NULL, cpuclk_name, NULL,
  55. CLK_IS_ROOT, rate);
  56. WARN_ON(IS_ERR(clk_data.clks[1]));
  57. /* Register fixed-factor clocks derived from CPU clock */
  58. for (n = 0; n < desc->num_ratios; n++) {
  59. const char *rclk_name = desc->ratios[n].name;
  60. int mult, div;
  61. of_property_read_string_index(np, "clock-output-names",
  62. 2+n, &rclk_name);
  63. desc->get_clk_ratio(base, desc->ratios[n].id, &mult, &div);
  64. clk_data.clks[2+n] = clk_register_fixed_factor(NULL, rclk_name,
  65. cpuclk_name, 0, mult, div);
  66. WARN_ON(IS_ERR(clk_data.clks[2+n]));
  67. };
  68. /* SAR register isn't needed anymore */
  69. iounmap(base);
  70. of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
  71. }
  72. /*
  73. * Clock Gating Control
  74. */
  75. struct clk_gating_ctrl {
  76. spinlock_t lock;
  77. struct clk **gates;
  78. int num_gates;
  79. };
  80. #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
  81. static struct clk *clk_gating_get_src(
  82. struct of_phandle_args *clkspec, void *data)
  83. {
  84. struct clk_gating_ctrl *ctrl = (struct clk_gating_ctrl *)data;
  85. int n;
  86. if (clkspec->args_count < 1)
  87. return ERR_PTR(-EINVAL);
  88. for (n = 0; n < ctrl->num_gates; n++) {
  89. struct clk_gate *gate =
  90. to_clk_gate(__clk_get_hw(ctrl->gates[n]));
  91. if (clkspec->args[0] == gate->bit_idx)
  92. return ctrl->gates[n];
  93. }
  94. return ERR_PTR(-ENODEV);
  95. }
  96. void __init mvebu_clk_gating_setup(struct device_node *np,
  97. const struct clk_gating_soc_desc *desc)
  98. {
  99. struct clk_gating_ctrl *ctrl;
  100. struct clk *clk;
  101. void __iomem *base;
  102. const char *default_parent = NULL;
  103. int n;
  104. base = of_iomap(np, 0);
  105. if (WARN_ON(!base))
  106. return;
  107. clk = of_clk_get(np, 0);
  108. if (!IS_ERR(clk)) {
  109. default_parent = __clk_get_name(clk);
  110. clk_put(clk);
  111. }
  112. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  113. if (WARN_ON(!ctrl))
  114. return;
  115. spin_lock_init(&ctrl->lock);
  116. /* Count, allocate, and register clock gates */
  117. for (n = 0; desc[n].name;)
  118. n++;
  119. ctrl->num_gates = n;
  120. ctrl->gates = kzalloc(ctrl->num_gates * sizeof(struct clk *),
  121. GFP_KERNEL);
  122. if (WARN_ON(!ctrl->gates)) {
  123. kfree(ctrl);
  124. return;
  125. }
  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. }