gate.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * OMAP gate clock support
  3. *
  4. * Copyright (C) 2013 Texas Instruments, Inc.
  5. *
  6. * Tero Kristo <t-kristo@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/clk-provider.h>
  18. #include <linux/slab.h>
  19. #include <linux/io.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/clk/ti.h>
  23. #include "clock.h"
  24. #undef pr_fmt
  25. #define pr_fmt(fmt) "%s: " fmt, __func__
  26. static int omap36xx_gate_clk_enable_with_hsdiv_restore(struct clk_hw *clk);
  27. static const struct clk_ops omap_gate_clkdm_clk_ops = {
  28. .init = &omap2_init_clk_clkdm,
  29. .enable = &omap2_clkops_enable_clkdm,
  30. .disable = &omap2_clkops_disable_clkdm,
  31. };
  32. static const struct clk_ops omap_gate_clk_ops = {
  33. .init = &omap2_init_clk_clkdm,
  34. .enable = &omap2_dflt_clk_enable,
  35. .disable = &omap2_dflt_clk_disable,
  36. .is_enabled = &omap2_dflt_clk_is_enabled,
  37. };
  38. static const struct clk_ops omap_gate_clk_hsdiv_restore_ops = {
  39. .init = &omap2_init_clk_clkdm,
  40. .enable = &omap36xx_gate_clk_enable_with_hsdiv_restore,
  41. .disable = &omap2_dflt_clk_disable,
  42. .is_enabled = &omap2_dflt_clk_is_enabled,
  43. };
  44. /**
  45. * omap36xx_gate_clk_enable_with_hsdiv_restore - enable clocks suffering
  46. * from HSDivider PWRDN problem Implements Errata ID: i556.
  47. * @clk: DPLL output struct clk
  48. *
  49. * 3630 only: dpll3_m3_ck, dpll4_m2_ck, dpll4_m3_ck, dpll4_m4_ck,
  50. * dpll4_m5_ck & dpll4_m6_ck dividers gets loaded with reset
  51. * valueafter their respective PWRDN bits are set. Any dummy write
  52. * (Any other value different from the Read value) to the
  53. * corresponding CM_CLKSEL register will refresh the dividers.
  54. */
  55. static int omap36xx_gate_clk_enable_with_hsdiv_restore(struct clk_hw *hw)
  56. {
  57. struct clk_divider *parent;
  58. struct clk_hw *parent_hw;
  59. u32 dummy_v, orig_v;
  60. int ret;
  61. /* Clear PWRDN bit of HSDIVIDER */
  62. ret = omap2_dflt_clk_enable(hw);
  63. /* Parent is the x2 node, get parent of parent for the m2 div */
  64. parent_hw = clk_hw_get_parent(clk_hw_get_parent(hw));
  65. parent = to_clk_divider(parent_hw);
  66. /* Restore the dividers */
  67. if (!ret) {
  68. orig_v = ti_clk_ll_ops->clk_readl(parent->reg);
  69. dummy_v = orig_v;
  70. /* Write any other value different from the Read value */
  71. dummy_v ^= (1 << parent->shift);
  72. ti_clk_ll_ops->clk_writel(dummy_v, parent->reg);
  73. /* Write the original divider */
  74. ti_clk_ll_ops->clk_writel(orig_v, parent->reg);
  75. }
  76. return ret;
  77. }
  78. static struct clk *_register_gate(struct device *dev, const char *name,
  79. const char *parent_name, unsigned long flags,
  80. void __iomem *reg, u8 bit_idx,
  81. u8 clk_gate_flags, const struct clk_ops *ops,
  82. const struct clk_hw_omap_ops *hw_ops)
  83. {
  84. struct clk_init_data init = { NULL };
  85. struct clk_hw_omap *clk_hw;
  86. struct clk *clk;
  87. clk_hw = kzalloc(sizeof(*clk_hw), GFP_KERNEL);
  88. if (!clk_hw)
  89. return ERR_PTR(-ENOMEM);
  90. clk_hw->hw.init = &init;
  91. init.name = name;
  92. init.ops = ops;
  93. clk_hw->enable_reg = reg;
  94. clk_hw->enable_bit = bit_idx;
  95. clk_hw->ops = hw_ops;
  96. clk_hw->flags = MEMMAP_ADDRESSING | clk_gate_flags;
  97. init.parent_names = &parent_name;
  98. init.num_parents = 1;
  99. init.flags = flags;
  100. clk = clk_register(NULL, &clk_hw->hw);
  101. if (IS_ERR(clk))
  102. kfree(clk_hw);
  103. return clk;
  104. }
  105. #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_ATAGS)
  106. struct clk *ti_clk_register_gate(struct ti_clk *setup)
  107. {
  108. const struct clk_ops *ops = &omap_gate_clk_ops;
  109. const struct clk_hw_omap_ops *hw_ops = NULL;
  110. u32 reg;
  111. struct clk_omap_reg *reg_setup;
  112. u32 flags = 0;
  113. u8 clk_gate_flags = 0;
  114. struct ti_clk_gate *gate;
  115. gate = setup->data;
  116. if (gate->flags & CLKF_INTERFACE)
  117. return ti_clk_register_interface(setup);
  118. reg_setup = (struct clk_omap_reg *)&reg;
  119. if (gate->flags & CLKF_SET_RATE_PARENT)
  120. flags |= CLK_SET_RATE_PARENT;
  121. if (gate->flags & CLKF_SET_BIT_TO_DISABLE)
  122. clk_gate_flags |= INVERT_ENABLE;
  123. if (gate->flags & CLKF_HSDIV) {
  124. ops = &omap_gate_clk_hsdiv_restore_ops;
  125. hw_ops = &clkhwops_wait;
  126. }
  127. if (gate->flags & CLKF_DSS)
  128. hw_ops = &clkhwops_omap3430es2_dss_usbhost_wait;
  129. if (gate->flags & CLKF_WAIT)
  130. hw_ops = &clkhwops_wait;
  131. if (gate->flags & CLKF_CLKDM)
  132. ops = &omap_gate_clkdm_clk_ops;
  133. if (gate->flags & CLKF_AM35XX)
  134. hw_ops = &clkhwops_am35xx_ipss_module_wait;
  135. reg_setup->index = gate->module;
  136. reg_setup->offset = gate->reg;
  137. return _register_gate(NULL, setup->name, gate->parent, flags,
  138. (void __iomem *)reg, gate->bit_shift,
  139. clk_gate_flags, ops, hw_ops);
  140. }
  141. struct clk_hw *ti_clk_build_component_gate(struct ti_clk_gate *setup)
  142. {
  143. struct clk_hw_omap *gate;
  144. struct clk_omap_reg *reg;
  145. const struct clk_hw_omap_ops *ops = &clkhwops_wait;
  146. if (!setup)
  147. return NULL;
  148. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  149. if (!gate)
  150. return ERR_PTR(-ENOMEM);
  151. reg = (struct clk_omap_reg *)&gate->enable_reg;
  152. reg->index = setup->module;
  153. reg->offset = setup->reg;
  154. gate->enable_bit = setup->bit_shift;
  155. if (setup->flags & CLKF_NO_WAIT)
  156. ops = NULL;
  157. if (setup->flags & CLKF_INTERFACE)
  158. ops = &clkhwops_iclk_wait;
  159. gate->ops = ops;
  160. gate->flags = MEMMAP_ADDRESSING;
  161. return &gate->hw;
  162. }
  163. #endif
  164. static void __init _of_ti_gate_clk_setup(struct device_node *node,
  165. const struct clk_ops *ops,
  166. const struct clk_hw_omap_ops *hw_ops)
  167. {
  168. struct clk *clk;
  169. const char *parent_name;
  170. void __iomem *reg = NULL;
  171. u8 enable_bit = 0;
  172. u32 val;
  173. u32 flags = 0;
  174. u8 clk_gate_flags = 0;
  175. if (ops != &omap_gate_clkdm_clk_ops) {
  176. reg = ti_clk_get_reg_addr(node, 0);
  177. if (IS_ERR(reg))
  178. return;
  179. if (!of_property_read_u32(node, "ti,bit-shift", &val))
  180. enable_bit = val;
  181. }
  182. if (of_clk_get_parent_count(node) != 1) {
  183. pr_err("%s must have 1 parent\n", node->name);
  184. return;
  185. }
  186. parent_name = of_clk_get_parent_name(node, 0);
  187. if (of_property_read_bool(node, "ti,set-rate-parent"))
  188. flags |= CLK_SET_RATE_PARENT;
  189. if (of_property_read_bool(node, "ti,set-bit-to-disable"))
  190. clk_gate_flags |= INVERT_ENABLE;
  191. clk = _register_gate(NULL, node->name, parent_name, flags, reg,
  192. enable_bit, clk_gate_flags, ops, hw_ops);
  193. if (!IS_ERR(clk))
  194. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  195. }
  196. static void __init
  197. _of_ti_composite_gate_clk_setup(struct device_node *node,
  198. const struct clk_hw_omap_ops *hw_ops)
  199. {
  200. struct clk_hw_omap *gate;
  201. u32 val = 0;
  202. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  203. if (!gate)
  204. return;
  205. gate->enable_reg = ti_clk_get_reg_addr(node, 0);
  206. if (IS_ERR(gate->enable_reg))
  207. goto cleanup;
  208. of_property_read_u32(node, "ti,bit-shift", &val);
  209. gate->enable_bit = val;
  210. gate->ops = hw_ops;
  211. gate->flags = MEMMAP_ADDRESSING;
  212. if (!ti_clk_add_component(node, &gate->hw, CLK_COMPONENT_TYPE_GATE))
  213. return;
  214. cleanup:
  215. kfree(gate);
  216. }
  217. static void __init
  218. of_ti_composite_no_wait_gate_clk_setup(struct device_node *node)
  219. {
  220. _of_ti_composite_gate_clk_setup(node, NULL);
  221. }
  222. CLK_OF_DECLARE(ti_composite_no_wait_gate_clk, "ti,composite-no-wait-gate-clock",
  223. of_ti_composite_no_wait_gate_clk_setup);
  224. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  225. static void __init of_ti_composite_interface_clk_setup(struct device_node *node)
  226. {
  227. _of_ti_composite_gate_clk_setup(node, &clkhwops_iclk_wait);
  228. }
  229. CLK_OF_DECLARE(ti_composite_interface_clk, "ti,composite-interface-clock",
  230. of_ti_composite_interface_clk_setup);
  231. #endif
  232. static void __init of_ti_composite_gate_clk_setup(struct device_node *node)
  233. {
  234. _of_ti_composite_gate_clk_setup(node, &clkhwops_wait);
  235. }
  236. CLK_OF_DECLARE(ti_composite_gate_clk, "ti,composite-gate-clock",
  237. of_ti_composite_gate_clk_setup);
  238. static void __init of_ti_clkdm_gate_clk_setup(struct device_node *node)
  239. {
  240. _of_ti_gate_clk_setup(node, &omap_gate_clkdm_clk_ops, NULL);
  241. }
  242. CLK_OF_DECLARE(ti_clkdm_gate_clk, "ti,clkdm-gate-clock",
  243. of_ti_clkdm_gate_clk_setup);
  244. static void __init of_ti_hsdiv_gate_clk_setup(struct device_node *node)
  245. {
  246. _of_ti_gate_clk_setup(node, &omap_gate_clk_hsdiv_restore_ops,
  247. &clkhwops_wait);
  248. }
  249. CLK_OF_DECLARE(ti_hsdiv_gate_clk, "ti,hsdiv-gate-clock",
  250. of_ti_hsdiv_gate_clk_setup);
  251. static void __init of_ti_gate_clk_setup(struct device_node *node)
  252. {
  253. _of_ti_gate_clk_setup(node, &omap_gate_clk_ops, NULL);
  254. }
  255. CLK_OF_DECLARE(ti_gate_clk, "ti,gate-clock", of_ti_gate_clk_setup);
  256. static void __init of_ti_wait_gate_clk_setup(struct device_node *node)
  257. {
  258. _of_ti_gate_clk_setup(node, &omap_gate_clk_ops, &clkhwops_wait);
  259. }
  260. CLK_OF_DECLARE(ti_wait_gate_clk, "ti,wait-gate-clock",
  261. of_ti_wait_gate_clk_setup);
  262. #ifdef CONFIG_ARCH_OMAP3
  263. static void __init of_ti_am35xx_gate_clk_setup(struct device_node *node)
  264. {
  265. _of_ti_gate_clk_setup(node, &omap_gate_clk_ops,
  266. &clkhwops_am35xx_ipss_module_wait);
  267. }
  268. CLK_OF_DECLARE(ti_am35xx_gate_clk, "ti,am35xx-gate-clock",
  269. of_ti_am35xx_gate_clk_setup);
  270. static void __init of_ti_dss_gate_clk_setup(struct device_node *node)
  271. {
  272. _of_ti_gate_clk_setup(node, &omap_gate_clk_ops,
  273. &clkhwops_omap3430es2_dss_usbhost_wait);
  274. }
  275. CLK_OF_DECLARE(ti_dss_gate_clk, "ti,dss-gate-clock",
  276. of_ti_dss_gate_clk_setup);
  277. #endif