clk-mstp.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 | CLK_SET_RATE_PARENT;
  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. const char *idxname;
  132. struct clk **clks;
  133. unsigned int i;
  134. group = kzalloc(sizeof(*group), GFP_KERNEL);
  135. clks = kmalloc(MSTP_MAX_CLOCKS * sizeof(*clks), GFP_KERNEL);
  136. if (group == NULL || clks == NULL) {
  137. kfree(group);
  138. kfree(clks);
  139. pr_err("%s: failed to allocate group\n", __func__);
  140. return;
  141. }
  142. spin_lock_init(&group->lock);
  143. group->data.clks = clks;
  144. group->smstpcr = of_iomap(np, 0);
  145. group->mstpsr = of_iomap(np, 1);
  146. if (group->smstpcr == NULL) {
  147. pr_err("%s: failed to remap SMSTPCR\n", __func__);
  148. kfree(group);
  149. kfree(clks);
  150. return;
  151. }
  152. for (i = 0; i < MSTP_MAX_CLOCKS; ++i)
  153. clks[i] = ERR_PTR(-ENOENT);
  154. if (of_find_property(np, "clock-indices", &i))
  155. idxname = "clock-indices";
  156. else
  157. idxname = "renesas,clock-indices";
  158. for (i = 0; i < MSTP_MAX_CLOCKS; ++i) {
  159. const char *parent_name;
  160. const char *name;
  161. u32 clkidx;
  162. int ret;
  163. /* Skip clocks with no name. */
  164. ret = of_property_read_string_index(np, "clock-output-names",
  165. i, &name);
  166. if (ret < 0 || strlen(name) == 0)
  167. continue;
  168. parent_name = of_clk_get_parent_name(np, i);
  169. ret = of_property_read_u32_index(np, idxname, i, &clkidx);
  170. if (parent_name == NULL || ret < 0)
  171. break;
  172. if (clkidx >= MSTP_MAX_CLOCKS) {
  173. pr_err("%s: invalid clock %s %s index %u)\n",
  174. __func__, np->name, name, clkidx);
  175. continue;
  176. }
  177. clks[clkidx] = cpg_mstp_clock_register(name, parent_name,
  178. clkidx, group);
  179. if (!IS_ERR(clks[clkidx])) {
  180. group->data.clk_num = max(group->data.clk_num,
  181. clkidx + 1);
  182. /*
  183. * Register a clkdev to let board code retrieve the
  184. * clock by name and register aliases for non-DT
  185. * devices.
  186. *
  187. * FIXME: Remove this when all devices that require a
  188. * clock will be instantiated from DT.
  189. */
  190. clk_register_clkdev(clks[clkidx], name, NULL);
  191. } else {
  192. pr_err("%s: failed to register %s %s clock (%ld)\n",
  193. __func__, np->name, name, PTR_ERR(clks[clkidx]));
  194. }
  195. }
  196. of_clk_add_provider(np, of_clk_src_onecell_get, &group->data);
  197. }
  198. CLK_OF_DECLARE(cpg_mstp_clks, "renesas,cpg-mstp-clocks", cpg_mstp_clocks_init);