clk-mstp.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * R-Car MSTP clocks
  3. *
  4. * Copyright (C) 2013 Ideas On Board SPRL
  5. *
  6. * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.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 as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/clkdev.h>
  14. #include <linux/io.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/spinlock.h>
  18. /*
  19. * MSTP clocks. We can't use standard gate clocks as we need to poll on the
  20. * status register when enabling the clock.
  21. */
  22. #define MSTP_MAX_CLOCKS 32
  23. /**
  24. * struct mstp_clock_group - MSTP gating clocks group
  25. *
  26. * @data: clocks in this group
  27. * @smstpcr: module stop control register
  28. * @mstpsr: module stop status register (optional)
  29. * @lock: protects writes to SMSTPCR
  30. */
  31. struct mstp_clock_group {
  32. struct clk_onecell_data data;
  33. void __iomem *smstpcr;
  34. void __iomem *mstpsr;
  35. spinlock_t lock;
  36. };
  37. /**
  38. * struct mstp_clock - MSTP gating clock
  39. * @hw: handle between common and hardware-specific interfaces
  40. * @bit_index: control bit index
  41. * @group: MSTP clocks group
  42. */
  43. struct mstp_clock {
  44. struct clk_hw hw;
  45. u32 bit_index;
  46. struct mstp_clock_group *group;
  47. };
  48. #define to_mstp_clock(_hw) container_of(_hw, struct mstp_clock, hw)
  49. static int cpg_mstp_clock_endisable(struct clk_hw *hw, bool enable)
  50. {
  51. struct mstp_clock *clock = to_mstp_clock(hw);
  52. struct mstp_clock_group *group = clock->group;
  53. u32 bitmask = BIT(clock->bit_index);
  54. unsigned long flags;
  55. unsigned int i;
  56. u32 value;
  57. spin_lock_irqsave(&group->lock, flags);
  58. value = clk_readl(group->smstpcr);
  59. if (enable)
  60. value &= ~bitmask;
  61. else
  62. value |= bitmask;
  63. clk_writel(value, group->smstpcr);
  64. spin_unlock_irqrestore(&group->lock, flags);
  65. if (!enable || !group->mstpsr)
  66. return 0;
  67. for (i = 1000; i > 0; --i) {
  68. if (!(clk_readl(group->mstpsr) & bitmask))
  69. break;
  70. cpu_relax();
  71. }
  72. if (!i) {
  73. pr_err("%s: failed to enable %p[%d]\n", __func__,
  74. group->smstpcr, clock->bit_index);
  75. return -ETIMEDOUT;
  76. }
  77. return 0;
  78. }
  79. static int cpg_mstp_clock_enable(struct clk_hw *hw)
  80. {
  81. return cpg_mstp_clock_endisable(hw, true);
  82. }
  83. static void cpg_mstp_clock_disable(struct clk_hw *hw)
  84. {
  85. cpg_mstp_clock_endisable(hw, false);
  86. }
  87. static int cpg_mstp_clock_is_enabled(struct clk_hw *hw)
  88. {
  89. struct mstp_clock *clock = to_mstp_clock(hw);
  90. struct mstp_clock_group *group = clock->group;
  91. u32 value;
  92. if (group->mstpsr)
  93. value = clk_readl(group->mstpsr);
  94. else
  95. value = clk_readl(group->smstpcr);
  96. return !!(value & BIT(clock->bit_index));
  97. }
  98. static const struct clk_ops cpg_mstp_clock_ops = {
  99. .enable = cpg_mstp_clock_enable,
  100. .disable = cpg_mstp_clock_disable,
  101. .is_enabled = cpg_mstp_clock_is_enabled,
  102. };
  103. static struct clk * __init
  104. cpg_mstp_clock_register(const char *name, const char *parent_name,
  105. unsigned int index, struct mstp_clock_group *group)
  106. {
  107. struct clk_init_data init;
  108. struct mstp_clock *clock;
  109. struct clk *clk;
  110. clock = kzalloc(sizeof(*clock), GFP_KERNEL);
  111. if (!clock) {
  112. pr_err("%s: failed to allocate MSTP clock.\n", __func__);
  113. return ERR_PTR(-ENOMEM);
  114. }
  115. init.name = name;
  116. init.ops = &cpg_mstp_clock_ops;
  117. init.flags = CLK_IS_BASIC;
  118. init.parent_names = &parent_name;
  119. init.num_parents = 1;
  120. clock->bit_index = index;
  121. clock->group = group;
  122. clock->hw.init = &init;
  123. clk = clk_register(NULL, &clock->hw);
  124. if (IS_ERR(clk))
  125. kfree(clock);
  126. return clk;
  127. }
  128. static void __init cpg_mstp_clocks_init(struct device_node *np)
  129. {
  130. struct mstp_clock_group *group;
  131. struct clk **clks;
  132. unsigned int i;
  133. group = kzalloc(sizeof(*group), GFP_KERNEL);
  134. clks = kzalloc(MSTP_MAX_CLOCKS * sizeof(*clks), GFP_KERNEL);
  135. if (group == NULL || clks == NULL) {
  136. kfree(group);
  137. kfree(clks);
  138. pr_err("%s: failed to allocate group\n", __func__);
  139. return;
  140. }
  141. spin_lock_init(&group->lock);
  142. group->data.clks = clks;
  143. group->smstpcr = of_iomap(np, 0);
  144. group->mstpsr = of_iomap(np, 1);
  145. if (group->smstpcr == NULL) {
  146. pr_err("%s: failed to remap SMSTPCR\n", __func__);
  147. kfree(group);
  148. kfree(clks);
  149. return;
  150. }
  151. for (i = 0; i < MSTP_MAX_CLOCKS; ++i) {
  152. const char *parent_name;
  153. const char *name;
  154. u32 clkidx;
  155. int ret;
  156. /* Skip clocks with no name. */
  157. ret = of_property_read_string_index(np, "clock-output-names",
  158. i, &name);
  159. if (ret < 0 || strlen(name) == 0)
  160. continue;
  161. parent_name = of_clk_get_parent_name(np, i);
  162. ret = of_property_read_u32_index(np, "renesas,clock-indices", i,
  163. &clkidx);
  164. if (parent_name == NULL || ret < 0)
  165. break;
  166. if (clkidx >= MSTP_MAX_CLOCKS) {
  167. pr_err("%s: invalid clock %s %s index %u)\n",
  168. __func__, np->name, name, clkidx);
  169. continue;
  170. }
  171. clks[clkidx] = cpg_mstp_clock_register(name, parent_name, i,
  172. group);
  173. if (!IS_ERR(clks[clkidx])) {
  174. group->data.clk_num = max(group->data.clk_num, clkidx);
  175. /*
  176. * Register a clkdev to let board code retrieve the
  177. * clock by name and register aliases for non-DT
  178. * devices.
  179. *
  180. * FIXME: Remove this when all devices that require a
  181. * clock will be instantiated from DT.
  182. */
  183. clk_register_clkdev(clks[clkidx], name, NULL);
  184. } else {
  185. pr_err("%s: failed to register %s %s clock (%ld)\n",
  186. __func__, np->name, name, PTR_ERR(clks[clkidx]));
  187. }
  188. }
  189. of_clk_add_provider(np, of_clk_src_onecell_get, &group->data);
  190. }
  191. CLK_OF_DECLARE(cpg_mstp_clks, "renesas,cpg-mstp-clocks", cpg_mstp_clocks_init);