gdsc.c 8.5 KB

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