gdsc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/bitops.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/kernel.h>
  18. #include <linux/ktime.h>
  19. #include <linux/pm_domain.h>
  20. #include <linux/regmap.h>
  21. #include <linux/reset-controller.h>
  22. #include <linux/slab.h>
  23. #include "gdsc.h"
  24. #define PWR_ON_MASK BIT(31)
  25. #define EN_REST_WAIT_MASK GENMASK_ULL(23, 20)
  26. #define EN_FEW_WAIT_MASK GENMASK_ULL(19, 16)
  27. #define CLK_DIS_WAIT_MASK GENMASK_ULL(15, 12)
  28. #define SW_OVERRIDE_MASK BIT(2)
  29. #define HW_CONTROL_MASK BIT(1)
  30. #define SW_COLLAPSE_MASK BIT(0)
  31. /* Wait 2^n CXO cycles between all states. Here, n=2 (4 cycles). */
  32. #define EN_REST_WAIT_VAL (0x2 << 20)
  33. #define EN_FEW_WAIT_VAL (0x8 << 16)
  34. #define CLK_DIS_WAIT_VAL (0x2 << 12)
  35. #define RETAIN_MEM BIT(14)
  36. #define RETAIN_PERIPH BIT(13)
  37. #define TIMEOUT_US 100
  38. #define domain_to_gdsc(domain) container_of(domain, struct gdsc, pd)
  39. static int gdsc_is_enabled(struct gdsc *sc, unsigned int reg)
  40. {
  41. u32 val;
  42. int ret;
  43. ret = regmap_read(sc->regmap, reg, &val);
  44. if (ret)
  45. return ret;
  46. return !!(val & PWR_ON_MASK);
  47. }
  48. static int gdsc_toggle_logic(struct gdsc *sc, bool en)
  49. {
  50. int ret;
  51. u32 val = en ? 0 : SW_COLLAPSE_MASK;
  52. ktime_t start;
  53. unsigned int status_reg = sc->gdscr;
  54. ret = regmap_update_bits(sc->regmap, sc->gdscr, SW_COLLAPSE_MASK, val);
  55. if (ret)
  56. return ret;
  57. /* If disabling votable gdscs, don't poll on status */
  58. if ((sc->flags & VOTABLE) && !en) {
  59. /*
  60. * Add a short delay here to ensure that an enable
  61. * right after it was disabled does not put it in an
  62. * unknown state
  63. */
  64. udelay(TIMEOUT_US);
  65. return 0;
  66. }
  67. if (sc->gds_hw_ctrl) {
  68. status_reg = sc->gds_hw_ctrl;
  69. /*
  70. * The gds hw controller asserts/de-asserts the status bit soon
  71. * after it receives a power on/off request from a master.
  72. * The controller then takes around 8 xo cycles to start its
  73. * internal state machine and update the status bit. During
  74. * this time, the status bit does not reflect the true status
  75. * of the core.
  76. * Add a delay of 1 us between writing to the SW_COLLAPSE bit
  77. * and polling the status bit.
  78. */
  79. udelay(1);
  80. }
  81. start = ktime_get();
  82. do {
  83. if (gdsc_is_enabled(sc, status_reg) == en)
  84. return 0;
  85. } while (ktime_us_delta(ktime_get(), start) < TIMEOUT_US);
  86. if (gdsc_is_enabled(sc, status_reg) == en)
  87. return 0;
  88. return -ETIMEDOUT;
  89. }
  90. static inline int gdsc_deassert_reset(struct gdsc *sc)
  91. {
  92. int i;
  93. for (i = 0; i < sc->reset_count; i++)
  94. sc->rcdev->ops->deassert(sc->rcdev, sc->resets[i]);
  95. return 0;
  96. }
  97. static inline int gdsc_assert_reset(struct gdsc *sc)
  98. {
  99. int i;
  100. for (i = 0; i < sc->reset_count; i++)
  101. sc->rcdev->ops->assert(sc->rcdev, sc->resets[i]);
  102. return 0;
  103. }
  104. static inline void gdsc_force_mem_on(struct gdsc *sc)
  105. {
  106. int i;
  107. u32 mask = RETAIN_MEM | RETAIN_PERIPH;
  108. for (i = 0; i < sc->cxc_count; i++)
  109. regmap_update_bits(sc->regmap, sc->cxcs[i], mask, mask);
  110. }
  111. static inline void gdsc_clear_mem_on(struct gdsc *sc)
  112. {
  113. int i;
  114. u32 mask = RETAIN_MEM | RETAIN_PERIPH;
  115. for (i = 0; i < sc->cxc_count; i++)
  116. regmap_update_bits(sc->regmap, sc->cxcs[i], mask, 0);
  117. }
  118. static int gdsc_enable(struct generic_pm_domain *domain)
  119. {
  120. struct gdsc *sc = domain_to_gdsc(domain);
  121. int ret;
  122. if (sc->pwrsts == PWRSTS_ON)
  123. return gdsc_deassert_reset(sc);
  124. ret = gdsc_toggle_logic(sc, true);
  125. if (ret)
  126. return ret;
  127. if (sc->pwrsts & PWRSTS_OFF)
  128. gdsc_force_mem_on(sc);
  129. /*
  130. * If clocks to this power domain were already on, they will take an
  131. * additional 4 clock cycles to re-enable after the power domain is
  132. * enabled. Delay to account for this. A delay is also needed to ensure
  133. * clocks are not enabled within 400ns of enabling power to the
  134. * memories.
  135. */
  136. udelay(1);
  137. return 0;
  138. }
  139. static int gdsc_disable(struct generic_pm_domain *domain)
  140. {
  141. struct gdsc *sc = domain_to_gdsc(domain);
  142. if (sc->pwrsts == PWRSTS_ON)
  143. return gdsc_assert_reset(sc);
  144. if (sc->pwrsts & PWRSTS_OFF)
  145. gdsc_clear_mem_on(sc);
  146. return gdsc_toggle_logic(sc, false);
  147. }
  148. static int gdsc_init(struct gdsc *sc)
  149. {
  150. u32 mask, val;
  151. int on, ret;
  152. unsigned int reg;
  153. /*
  154. * Disable HW trigger: collapse/restore occur based on registers writes.
  155. * Disable SW override: Use hardware state-machine for sequencing.
  156. * Configure wait time between states.
  157. */
  158. mask = HW_CONTROL_MASK | SW_OVERRIDE_MASK |
  159. EN_REST_WAIT_MASK | EN_FEW_WAIT_MASK | CLK_DIS_WAIT_MASK;
  160. val = EN_REST_WAIT_VAL | EN_FEW_WAIT_VAL | CLK_DIS_WAIT_VAL;
  161. ret = regmap_update_bits(sc->regmap, sc->gdscr, mask, val);
  162. if (ret)
  163. return ret;
  164. /* Force gdsc ON if only ON state is supported */
  165. if (sc->pwrsts == PWRSTS_ON) {
  166. ret = gdsc_toggle_logic(sc, true);
  167. if (ret)
  168. return ret;
  169. }
  170. reg = sc->gds_hw_ctrl ? sc->gds_hw_ctrl : sc->gdscr;
  171. on = gdsc_is_enabled(sc, reg);
  172. if (on < 0)
  173. return on;
  174. /*
  175. * Votable GDSCs can be ON due to Vote from other masters.
  176. * If a Votable GDSC is ON, make sure we have a Vote.
  177. */
  178. if ((sc->flags & VOTABLE) && on)
  179. gdsc_enable(&sc->pd);
  180. if (on || (sc->pwrsts & PWRSTS_RET))
  181. gdsc_force_mem_on(sc);
  182. else
  183. gdsc_clear_mem_on(sc);
  184. sc->pd.power_off = gdsc_disable;
  185. sc->pd.power_on = gdsc_enable;
  186. pm_genpd_init(&sc->pd, NULL, !on);
  187. return 0;
  188. }
  189. int gdsc_register(struct gdsc_desc *desc,
  190. struct reset_controller_dev *rcdev, struct regmap *regmap)
  191. {
  192. int i, ret;
  193. struct genpd_onecell_data *data;
  194. struct device *dev = desc->dev;
  195. struct gdsc **scs = desc->scs;
  196. size_t num = desc->num;
  197. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  198. if (!data)
  199. return -ENOMEM;
  200. data->domains = devm_kcalloc(dev, num, sizeof(*data->domains),
  201. GFP_KERNEL);
  202. if (!data->domains)
  203. return -ENOMEM;
  204. data->num_domains = num;
  205. for (i = 0; i < num; i++) {
  206. if (!scs[i])
  207. continue;
  208. scs[i]->regmap = regmap;
  209. scs[i]->rcdev = rcdev;
  210. ret = gdsc_init(scs[i]);
  211. if (ret)
  212. return ret;
  213. data->domains[i] = &scs[i]->pd;
  214. }
  215. /* Add subdomains */
  216. for (i = 0; i < num; i++) {
  217. if (!scs[i])
  218. continue;
  219. if (scs[i]->parent)
  220. pm_genpd_add_subdomain(scs[i]->parent, &scs[i]->pd);
  221. }
  222. return of_genpd_add_provider_onecell(dev->of_node, data);
  223. }
  224. void gdsc_unregister(struct gdsc_desc *desc)
  225. {
  226. int i;
  227. struct device *dev = desc->dev;
  228. struct gdsc **scs = desc->scs;
  229. size_t num = desc->num;
  230. /* Remove subdomains */
  231. for (i = 0; i < num; i++) {
  232. if (!scs[i])
  233. continue;
  234. if (scs[i]->parent)
  235. pm_genpd_remove_subdomain(scs[i]->parent, &scs[i]->pd);
  236. }
  237. of_genpd_del_provider(dev->of_node);
  238. }