gdsc.c 9.6 KB

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