clk-gating-ctrl.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Marvell MVEBU clock gating control.
  3. *
  4. * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
  5. * Andrew Lunn <andrew@lunn.ch>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/bitops.h>
  13. #include <linux/io.h>
  14. #include <linux/clk.h>
  15. #include <linux/clkdev.h>
  16. #include <linux/clk-provider.h>
  17. #include <linux/clk/mvebu.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. struct mvebu_gating_ctrl {
  21. spinlock_t lock;
  22. struct clk **gates;
  23. int num_gates;
  24. };
  25. struct mvebu_soc_descr {
  26. const char *name;
  27. const char *parent;
  28. int bit_idx;
  29. };
  30. #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
  31. static struct clk __init *mvebu_clk_gating_get_src(
  32. struct of_phandle_args *clkspec, void *data)
  33. {
  34. struct mvebu_gating_ctrl *ctrl = (struct mvebu_gating_ctrl *)data;
  35. int n;
  36. if (clkspec->args_count < 1)
  37. return ERR_PTR(-EINVAL);
  38. for (n = 0; n < ctrl->num_gates; n++) {
  39. struct clk_gate *gate =
  40. to_clk_gate(__clk_get_hw(ctrl->gates[n]));
  41. if (clkspec->args[0] == gate->bit_idx)
  42. return ctrl->gates[n];
  43. }
  44. return ERR_PTR(-ENODEV);
  45. }
  46. static void __init mvebu_clk_gating_setup(
  47. struct device_node *np, const struct mvebu_soc_descr *descr)
  48. {
  49. struct mvebu_gating_ctrl *ctrl;
  50. struct clk *clk;
  51. void __iomem *base;
  52. const char *default_parent = NULL;
  53. int n;
  54. base = of_iomap(np, 0);
  55. clk = of_clk_get(np, 0);
  56. if (!IS_ERR(clk)) {
  57. default_parent = __clk_get_name(clk);
  58. clk_put(clk);
  59. }
  60. ctrl = kzalloc(sizeof(struct mvebu_gating_ctrl), GFP_KERNEL);
  61. if (WARN_ON(!ctrl))
  62. return;
  63. spin_lock_init(&ctrl->lock);
  64. /*
  65. * Count, allocate, and register clock gates
  66. */
  67. for (n = 0; descr[n].name;)
  68. n++;
  69. ctrl->num_gates = n;
  70. ctrl->gates = kzalloc(ctrl->num_gates * sizeof(struct clk *),
  71. GFP_KERNEL);
  72. if (WARN_ON(!ctrl->gates)) {
  73. kfree(ctrl);
  74. return;
  75. }
  76. for (n = 0; n < ctrl->num_gates; n++) {
  77. const char *parent =
  78. (descr[n].parent) ? descr[n].parent : default_parent;
  79. ctrl->gates[n] = clk_register_gate(NULL, descr[n].name, parent,
  80. 0, base, descr[n].bit_idx, 0, &ctrl->lock);
  81. WARN_ON(IS_ERR(ctrl->gates[n]));
  82. }
  83. of_clk_add_provider(np, mvebu_clk_gating_get_src, ctrl);
  84. }
  85. /*
  86. * SoC specific clock gating control
  87. */
  88. #ifdef CONFIG_ARCH_DOVE
  89. static const struct mvebu_soc_descr __initconst dove_gating_descr[] = {
  90. { "usb0", NULL, 0 },
  91. { "usb1", NULL, 1 },
  92. { "ge", "gephy", 2 },
  93. { "sata", NULL, 3 },
  94. { "pex0", NULL, 4 },
  95. { "pex1", NULL, 5 },
  96. { "sdio0", NULL, 8 },
  97. { "sdio1", NULL, 9 },
  98. { "nand", NULL, 10 },
  99. { "camera", NULL, 11 },
  100. { "i2s0", NULL, 12 },
  101. { "i2s1", NULL, 13 },
  102. { "crypto", NULL, 15 },
  103. { "ac97", NULL, 21 },
  104. { "pdma", NULL, 22 },
  105. { "xor0", NULL, 23 },
  106. { "xor1", NULL, 24 },
  107. { "gephy", NULL, 30 },
  108. { }
  109. };
  110. #endif
  111. #ifdef CONFIG_ARCH_KIRKWOOD
  112. static const struct mvebu_soc_descr __initconst kirkwood_gating_descr[] = {
  113. { "ge0", NULL, 0 },
  114. { "pex0", NULL, 2 },
  115. { "usb0", NULL, 3 },
  116. { "sdio", NULL, 4 },
  117. { "tsu", NULL, 5 },
  118. { "runit", NULL, 7 },
  119. { "xor0", NULL, 8 },
  120. { "audio", NULL, 9 },
  121. { "sata0", NULL, 14 },
  122. { "sata1", NULL, 15 },
  123. { "xor1", NULL, 16 },
  124. { "crypto", NULL, 17 },
  125. { "pex1", NULL, 18 },
  126. { "ge1", NULL, 19 },
  127. { "tdm", NULL, 20 },
  128. { }
  129. };
  130. #endif
  131. static const __initdata struct of_device_id clk_gating_match[] = {
  132. #ifdef CONFIG_ARCH_DOVE
  133. {
  134. .compatible = "marvell,dove-gating-clock",
  135. .data = dove_gating_descr,
  136. },
  137. #endif
  138. #ifdef CONFIG_ARCH_KIRKWOOD
  139. {
  140. .compatible = "marvell,kirkwood-gating-clock",
  141. .data = kirkwood_gating_descr,
  142. },
  143. #endif
  144. { }
  145. };
  146. void __init mvebu_gating_clk_init(void)
  147. {
  148. struct device_node *np;
  149. for_each_matching_node(np, clk_gating_match) {
  150. const struct of_device_id *match =
  151. of_match_node(clk_gating_match, np);
  152. mvebu_clk_gating_setup(np,
  153. (const struct mvebu_soc_descr *)match->data);
  154. }
  155. }