clk-gating-ctrl.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 *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. u8 flags = 0;
  78. const char *parent =
  79. (descr[n].parent) ? descr[n].parent : default_parent;
  80. /*
  81. * On Armada 370, the DDR clock is a special case: it
  82. * isn't taken by any driver, but should anyway be
  83. * kept enabled, so we mark it as IGNORE_UNUSED for
  84. * now.
  85. */
  86. if (!strcmp(descr[n].name, "ddr"))
  87. flags |= CLK_IGNORE_UNUSED;
  88. ctrl->gates[n] = clk_register_gate(NULL, descr[n].name, parent,
  89. flags, base, descr[n].bit_idx, 0, &ctrl->lock);
  90. WARN_ON(IS_ERR(ctrl->gates[n]));
  91. }
  92. of_clk_add_provider(np, mvebu_clk_gating_get_src, ctrl);
  93. }
  94. /*
  95. * SoC specific clock gating control
  96. */
  97. #ifdef CONFIG_MACH_ARMADA_370
  98. static const struct mvebu_soc_descr __initconst armada_370_gating_descr[] = {
  99. { "audio", NULL, 0 },
  100. { "pex0_en", NULL, 1 },
  101. { "pex1_en", NULL, 2 },
  102. { "ge1", NULL, 3 },
  103. { "ge0", NULL, 4 },
  104. { "pex0", NULL, 5 },
  105. { "pex1", NULL, 9 },
  106. { "sata0", NULL, 15 },
  107. { "sdio", NULL, 17 },
  108. { "tdm", NULL, 25 },
  109. { "ddr", NULL, 28 },
  110. { "sata1", NULL, 30 },
  111. { }
  112. };
  113. #endif
  114. #ifdef CONFIG_MACH_ARMADA_XP
  115. static const struct mvebu_soc_descr __initconst armada_xp_gating_descr[] = {
  116. { "audio", NULL, 0 },
  117. { "ge3", NULL, 1 },
  118. { "ge2", NULL, 2 },
  119. { "ge1", NULL, 3 },
  120. { "ge0", NULL, 4 },
  121. { "pex0", NULL, 5 },
  122. { "pex1", NULL, 6 },
  123. { "pex2", NULL, 7 },
  124. { "pex3", NULL, 8 },
  125. { "bp", NULL, 13 },
  126. { "sata0lnk", NULL, 14 },
  127. { "sata0", "sata0lnk", 15 },
  128. { "lcd", NULL, 16 },
  129. { "sdio", NULL, 17 },
  130. { "usb0", NULL, 18 },
  131. { "usb1", NULL, 19 },
  132. { "usb2", NULL, 20 },
  133. { "xor0", NULL, 22 },
  134. { "crypto", NULL, 23 },
  135. { "tdm", NULL, 25 },
  136. { "xor1", NULL, 28 },
  137. { "sata1lnk", NULL, 29 },
  138. { "sata1", "sata1lnk", 30 },
  139. { }
  140. };
  141. #endif
  142. #ifdef CONFIG_ARCH_DOVE
  143. static const struct mvebu_soc_descr __initconst dove_gating_descr[] = {
  144. { "usb0", NULL, 0 },
  145. { "usb1", NULL, 1 },
  146. { "ge", "gephy", 2 },
  147. { "sata", NULL, 3 },
  148. { "pex0", NULL, 4 },
  149. { "pex1", NULL, 5 },
  150. { "sdio0", NULL, 8 },
  151. { "sdio1", NULL, 9 },
  152. { "nand", NULL, 10 },
  153. { "camera", NULL, 11 },
  154. { "i2s0", NULL, 12 },
  155. { "i2s1", NULL, 13 },
  156. { "crypto", NULL, 15 },
  157. { "ac97", NULL, 21 },
  158. { "pdma", NULL, 22 },
  159. { "xor0", NULL, 23 },
  160. { "xor1", NULL, 24 },
  161. { "gephy", NULL, 30 },
  162. { }
  163. };
  164. #endif
  165. #ifdef CONFIG_ARCH_KIRKWOOD
  166. static const struct mvebu_soc_descr __initconst kirkwood_gating_descr[] = {
  167. { "ge0", NULL, 0 },
  168. { "pex0", NULL, 2 },
  169. { "usb0", NULL, 3 },
  170. { "sdio", NULL, 4 },
  171. { "tsu", NULL, 5 },
  172. { "runit", NULL, 7 },
  173. { "xor0", NULL, 8 },
  174. { "audio", NULL, 9 },
  175. { "sata0", NULL, 14 },
  176. { "sata1", NULL, 15 },
  177. { "xor1", NULL, 16 },
  178. { "crypto", NULL, 17 },
  179. { "pex1", NULL, 18 },
  180. { "ge1", NULL, 19 },
  181. { "tdm", NULL, 20 },
  182. { }
  183. };
  184. #endif
  185. static const __initdata struct of_device_id clk_gating_match[] = {
  186. #ifdef CONFIG_MACH_ARMADA_370
  187. {
  188. .compatible = "marvell,armada-370-gating-clock",
  189. .data = armada_370_gating_descr,
  190. },
  191. #endif
  192. #ifdef CONFIG_MACH_ARMADA_XP
  193. {
  194. .compatible = "marvell,armada-xp-gating-clock",
  195. .data = armada_xp_gating_descr,
  196. },
  197. #endif
  198. #ifdef CONFIG_ARCH_DOVE
  199. {
  200. .compatible = "marvell,dove-gating-clock",
  201. .data = dove_gating_descr,
  202. },
  203. #endif
  204. #ifdef CONFIG_ARCH_KIRKWOOD
  205. {
  206. .compatible = "marvell,kirkwood-gating-clock",
  207. .data = kirkwood_gating_descr,
  208. },
  209. #endif
  210. { }
  211. };
  212. void __init mvebu_gating_clk_init(void)
  213. {
  214. struct device_node *np;
  215. for_each_matching_node(np, clk_gating_match) {
  216. const struct of_device_id *match =
  217. of_match_node(clk_gating_match, np);
  218. mvebu_clk_gating_setup(np,
  219. (const struct mvebu_soc_descr *)match->data);
  220. }
  221. }