gate.c 9.2 KB

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