gdsc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #define GMEM_CLAMP_IO_MASK BIT(0)
  32. /* Wait 2^n CXO cycles between all states. Here, n=2 (4 cycles). */
  33. #define EN_REST_WAIT_VAL (0x2 << 20)
  34. #define EN_FEW_WAIT_VAL (0x8 << 16)
  35. #define CLK_DIS_WAIT_VAL (0x2 << 12)
  36. #define RETAIN_MEM BIT(14)
  37. #define RETAIN_PERIPH BIT(13)
  38. #define TIMEOUT_US 100
  39. #define domain_to_gdsc(domain) container_of(domain, struct gdsc, pd)
  40. static int gdsc_is_enabled(struct gdsc *sc, unsigned int reg)
  41. {
  42. u32 val;
  43. int ret;
  44. ret = regmap_read(sc->regmap, reg, &val);
  45. if (ret)
  46. return ret;
  47. return !!(val & PWR_ON_MASK);
  48. }
  49. static int gdsc_hwctrl(struct gdsc *sc, bool en)
  50. {
  51. u32 val = en ? HW_CONTROL_MASK : 0;
  52. return regmap_update_bits(sc->regmap, sc->gdscr, HW_CONTROL_MASK, val);
  53. }
  54. static int gdsc_toggle_logic(struct gdsc *sc, bool en)
  55. {
  56. int ret;
  57. u32 val = en ? 0 : SW_COLLAPSE_MASK;
  58. ktime_t start;
  59. unsigned int status_reg = sc->gdscr;
  60. ret = regmap_update_bits(sc->regmap, sc->gdscr, SW_COLLAPSE_MASK, val);
  61. if (ret)
  62. return ret;
  63. /* If disabling votable gdscs, don't poll on status */
  64. if ((sc->flags & VOTABLE) && !en) {
  65. /*
  66. * Add a short delay here to ensure that an enable
  67. * right after it was disabled does not put it in an
  68. * unknown state
  69. */
  70. udelay(TIMEOUT_US);
  71. return 0;
  72. }
  73. if (sc->gds_hw_ctrl) {
  74. status_reg = sc->gds_hw_ctrl;
  75. /*
  76. * The gds hw controller asserts/de-asserts the status bit soon
  77. * after it receives a power on/off request from a master.
  78. * The controller then takes around 8 xo cycles to start its
  79. * internal state machine and update the status bit. During
  80. * this time, the status bit does not reflect the true status
  81. * of the core.
  82. * Add a delay of 1 us between writing to the SW_COLLAPSE bit
  83. * and polling the status bit.
  84. */
  85. udelay(1);
  86. }
  87. start = ktime_get();
  88. do {
  89. if (gdsc_is_enabled(sc, status_reg) == en)
  90. return 0;
  91. } while (ktime_us_delta(ktime_get(), start) < TIMEOUT_US);
  92. if (gdsc_is_enabled(sc, status_reg) == en)
  93. return 0;
  94. return -ETIMEDOUT;
  95. }
  96. static inline int gdsc_deassert_reset(struct gdsc *sc)
  97. {
  98. int i;
  99. for (i = 0; i < sc->reset_count; i++)
  100. sc->rcdev->ops->deassert(sc->rcdev, sc->resets[i]);
  101. return 0;
  102. }
  103. static inline int gdsc_assert_reset(struct gdsc *sc)
  104. {
  105. int i;
  106. for (i = 0; i < sc->reset_count; i++)
  107. sc->rcdev->ops->assert(sc->rcdev, sc->resets[i]);
  108. return 0;
  109. }
  110. static inline void gdsc_force_mem_on(struct gdsc *sc)
  111. {
  112. int i;
  113. u32 mask = RETAIN_MEM | RETAIN_PERIPH;
  114. for (i = 0; i < sc->cxc_count; i++)
  115. regmap_update_bits(sc->regmap, sc->cxcs[i], mask, mask);
  116. }
  117. static inline void gdsc_clear_mem_on(struct gdsc *sc)
  118. {
  119. int i;
  120. u32 mask = RETAIN_MEM | RETAIN_PERIPH;
  121. for (i = 0; i < sc->cxc_count; i++)
  122. regmap_update_bits(sc->regmap, sc->cxcs[i], mask, 0);
  123. }
  124. static inline void gdsc_deassert_clamp_io(struct gdsc *sc)
  125. {
  126. regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
  127. GMEM_CLAMP_IO_MASK, 0);
  128. }
  129. static inline void gdsc_assert_clamp_io(struct gdsc *sc)
  130. {
  131. regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
  132. GMEM_CLAMP_IO_MASK, 1);
  133. }
  134. static int gdsc_enable(struct generic_pm_domain *domain)
  135. {
  136. struct gdsc *sc = domain_to_gdsc(domain);
  137. int ret;
  138. if (sc->pwrsts == PWRSTS_ON)
  139. return gdsc_deassert_reset(sc);
  140. if (sc->flags & CLAMP_IO)
  141. gdsc_deassert_clamp_io(sc);
  142. ret = gdsc_toggle_logic(sc, true);
  143. if (ret)
  144. return ret;
  145. if (sc->pwrsts & PWRSTS_OFF)
  146. gdsc_force_mem_on(sc);
  147. /*
  148. * If clocks to this power domain were already on, they will take an
  149. * additional 4 clock cycles to re-enable after the power domain is
  150. * enabled. Delay to account for this. A delay is also needed to ensure
  151. * clocks are not enabled within 400ns of enabling power to the
  152. * memories.
  153. */
  154. udelay(1);
  155. /* Turn on HW trigger mode if supported */
  156. if (sc->flags & HW_CTRL)
  157. return gdsc_hwctrl(sc, true);
  158. return 0;
  159. }
  160. static int gdsc_disable(struct generic_pm_domain *domain)
  161. {
  162. struct gdsc *sc = domain_to_gdsc(domain);
  163. int ret;
  164. if (sc->pwrsts == PWRSTS_ON)
  165. return gdsc_assert_reset(sc);
  166. /* Turn off HW trigger mode if supported */
  167. if (sc->flags & HW_CTRL) {
  168. ret = gdsc_hwctrl(sc, false);
  169. if (ret < 0)
  170. return ret;
  171. }
  172. if (sc->pwrsts & PWRSTS_OFF)
  173. gdsc_clear_mem_on(sc);
  174. ret = gdsc_toggle_logic(sc, false);
  175. if (ret)
  176. return ret;
  177. if (sc->flags & CLAMP_IO)
  178. gdsc_assert_clamp_io(sc);
  179. return 0;
  180. }
  181. static int gdsc_init(struct gdsc *sc)
  182. {
  183. u32 mask, val;
  184. int on, ret;
  185. unsigned int reg;
  186. /*
  187. * Disable HW trigger: collapse/restore occur based on registers writes.
  188. * Disable SW override: Use hardware state-machine for sequencing.
  189. * Configure wait time between states.
  190. */
  191. mask = HW_CONTROL_MASK | SW_OVERRIDE_MASK |
  192. EN_REST_WAIT_MASK | EN_FEW_WAIT_MASK | CLK_DIS_WAIT_MASK;
  193. val = EN_REST_WAIT_VAL | EN_FEW_WAIT_VAL | CLK_DIS_WAIT_VAL;
  194. ret = regmap_update_bits(sc->regmap, sc->gdscr, mask, val);
  195. if (ret)
  196. return ret;
  197. /* Force gdsc ON if only ON state is supported */
  198. if (sc->pwrsts == PWRSTS_ON) {
  199. ret = gdsc_toggle_logic(sc, true);
  200. if (ret)
  201. return ret;
  202. }
  203. reg = sc->gds_hw_ctrl ? sc->gds_hw_ctrl : sc->gdscr;
  204. on = gdsc_is_enabled(sc, reg);
  205. if (on < 0)
  206. return on;
  207. /*
  208. * Votable GDSCs can be ON due to Vote from other masters.
  209. * If a Votable GDSC is ON, make sure we have a Vote.
  210. */
  211. if ((sc->flags & VOTABLE) && on)
  212. gdsc_enable(&sc->pd);
  213. if (on || (sc->pwrsts & PWRSTS_RET))
  214. gdsc_force_mem_on(sc);
  215. else
  216. gdsc_clear_mem_on(sc);
  217. sc->pd.power_off = gdsc_disable;
  218. sc->pd.power_on = gdsc_enable;
  219. pm_genpd_init(&sc->pd, NULL, !on);
  220. return 0;
  221. }
  222. int gdsc_register(struct gdsc_desc *desc,
  223. struct reset_controller_dev *rcdev, struct regmap *regmap)
  224. {
  225. int i, ret;
  226. struct genpd_onecell_data *data;
  227. struct device *dev = desc->dev;
  228. struct gdsc **scs = desc->scs;
  229. size_t num = desc->num;
  230. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  231. if (!data)
  232. return -ENOMEM;
  233. data->domains = devm_kcalloc(dev, num, sizeof(*data->domains),
  234. GFP_KERNEL);
  235. if (!data->domains)
  236. return -ENOMEM;
  237. data->num_domains = num;
  238. for (i = 0; i < num; i++) {
  239. if (!scs[i])
  240. continue;
  241. scs[i]->regmap = regmap;
  242. scs[i]->rcdev = rcdev;
  243. ret = gdsc_init(scs[i]);
  244. if (ret)
  245. return ret;
  246. data->domains[i] = &scs[i]->pd;
  247. }
  248. /* Add subdomains */
  249. for (i = 0; i < num; i++) {
  250. if (!scs[i])
  251. continue;
  252. if (scs[i]->parent)
  253. pm_genpd_add_subdomain(scs[i]->parent, &scs[i]->pd);
  254. }
  255. return of_genpd_add_provider_onecell(dev->of_node, data);
  256. }
  257. void gdsc_unregister(struct gdsc_desc *desc)
  258. {
  259. int i;
  260. struct device *dev = desc->dev;
  261. struct gdsc **scs = desc->scs;
  262. size_t num = desc->num;
  263. /* Remove subdomains */
  264. for (i = 0; i < num; i++) {
  265. if (!scs[i])
  266. continue;
  267. if (scs[i]->parent)
  268. pm_genpd_remove_subdomain(scs[i]->parent, &scs[i]->pd);
  269. }
  270. of_genpd_del_provider(dev->of_node);
  271. }