clockdomain.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. /*
  2. * OMAP2/3/4 clockdomain framework functions
  3. *
  4. * Copyright (C) 2008-2010 Texas Instruments, Inc.
  5. * Copyright (C) 2008-2010 Nokia Corporation
  6. *
  7. * Written by Paul Walmsley and Jouni Högander
  8. * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #undef DEBUG
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/device.h>
  18. #include <linux/list.h>
  19. #include <linux/errno.h>
  20. #include <linux/delay.h>
  21. #include <linux/clk.h>
  22. #include <linux/limits.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/bitops.h>
  26. #include "prm.h"
  27. #include "prm-regbits-24xx.h"
  28. #include "cm.h"
  29. #include <plat/clock.h>
  30. #include <plat/powerdomain.h>
  31. #include <plat/clockdomain.h>
  32. #include <plat/prcm.h>
  33. /* clkdm_list contains all registered struct clockdomains */
  34. static LIST_HEAD(clkdm_list);
  35. /* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
  36. static struct clkdm_autodep *autodeps;
  37. /* Private functions */
  38. static struct clockdomain *_clkdm_lookup(const char *name)
  39. {
  40. struct clockdomain *clkdm, *temp_clkdm;
  41. if (!name)
  42. return NULL;
  43. clkdm = NULL;
  44. list_for_each_entry(temp_clkdm, &clkdm_list, node) {
  45. if (!strcmp(name, temp_clkdm->name)) {
  46. clkdm = temp_clkdm;
  47. break;
  48. }
  49. }
  50. return clkdm;
  51. }
  52. /**
  53. * _clkdm_register - register a clockdomain
  54. * @clkdm: struct clockdomain * to register
  55. *
  56. * Adds a clockdomain to the internal clockdomain list.
  57. * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
  58. * already registered by the provided name, or 0 upon success.
  59. */
  60. static int _clkdm_register(struct clockdomain *clkdm)
  61. {
  62. struct powerdomain *pwrdm;
  63. if (!clkdm || !clkdm->name)
  64. return -EINVAL;
  65. if (!omap_chip_is(clkdm->omap_chip))
  66. return -EINVAL;
  67. pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
  68. if (!pwrdm) {
  69. pr_err("clockdomain: %s: powerdomain %s does not exist\n",
  70. clkdm->name, clkdm->pwrdm.name);
  71. return -EINVAL;
  72. }
  73. clkdm->pwrdm.ptr = pwrdm;
  74. /* Verify that the clockdomain is not already registered */
  75. if (_clkdm_lookup(clkdm->name))
  76. return -EEXIST;
  77. list_add(&clkdm->node, &clkdm_list);
  78. pwrdm_add_clkdm(pwrdm, clkdm);
  79. pr_debug("clockdomain: registered %s\n", clkdm->name);
  80. return 0;
  81. }
  82. /* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
  83. static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
  84. struct clkdm_dep *deps)
  85. {
  86. struct clkdm_dep *cd;
  87. if (!clkdm || !deps || !omap_chip_is(clkdm->omap_chip))
  88. return ERR_PTR(-EINVAL);
  89. for (cd = deps; cd->clkdm_name; cd++) {
  90. if (!omap_chip_is(cd->omap_chip))
  91. continue;
  92. if (!cd->clkdm && cd->clkdm_name)
  93. cd->clkdm = _clkdm_lookup(cd->clkdm_name);
  94. if (cd->clkdm == clkdm)
  95. break;
  96. }
  97. if (!cd->clkdm_name)
  98. return ERR_PTR(-ENOENT);
  99. return cd;
  100. }
  101. /*
  102. * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
  103. * @autodep: struct clkdm_autodep * to resolve
  104. *
  105. * Resolve autodep clockdomain names to clockdomain pointers via
  106. * clkdm_lookup() and store the pointers in the autodep structure. An
  107. * "autodep" is a clockdomain sleep/wakeup dependency that is
  108. * automatically added and removed whenever clocks in the associated
  109. * clockdomain are enabled or disabled (respectively) when the
  110. * clockdomain is in hardware-supervised mode. Meant to be called
  111. * once at clockdomain layer initialization, since these should remain
  112. * fixed for a particular architecture. No return value.
  113. */
  114. static void _autodep_lookup(struct clkdm_autodep *autodep)
  115. {
  116. struct clockdomain *clkdm;
  117. if (!autodep)
  118. return;
  119. if (!omap_chip_is(autodep->omap_chip))
  120. return;
  121. clkdm = clkdm_lookup(autodep->clkdm.name);
  122. if (!clkdm) {
  123. pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
  124. autodep->clkdm.name);
  125. clkdm = ERR_PTR(-ENOENT);
  126. }
  127. autodep->clkdm.ptr = clkdm;
  128. }
  129. /*
  130. * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
  131. * @clkdm: struct clockdomain *
  132. *
  133. * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
  134. * in hardware-supervised mode. Meant to be called from clock framework
  135. * when a clock inside clockdomain 'clkdm' is enabled. No return value.
  136. */
  137. static void _clkdm_add_autodeps(struct clockdomain *clkdm)
  138. {
  139. struct clkdm_autodep *autodep;
  140. if (!autodeps)
  141. return;
  142. for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
  143. if (IS_ERR(autodep->clkdm.ptr))
  144. continue;
  145. if (!omap_chip_is(autodep->omap_chip))
  146. continue;
  147. pr_debug("clockdomain: adding %s sleepdep/wkdep for "
  148. "clkdm %s\n", autodep->clkdm.ptr->name,
  149. clkdm->name);
  150. clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
  151. clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
  152. }
  153. }
  154. /*
  155. * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
  156. * @clkdm: struct clockdomain *
  157. *
  158. * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
  159. * in hardware-supervised mode. Meant to be called from clock framework
  160. * when a clock inside clockdomain 'clkdm' is disabled. No return value.
  161. */
  162. static void _clkdm_del_autodeps(struct clockdomain *clkdm)
  163. {
  164. struct clkdm_autodep *autodep;
  165. if (!autodeps)
  166. return;
  167. for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
  168. if (IS_ERR(autodep->clkdm.ptr))
  169. continue;
  170. if (!omap_chip_is(autodep->omap_chip))
  171. continue;
  172. pr_debug("clockdomain: removing %s sleepdep/wkdep for "
  173. "clkdm %s\n", autodep->clkdm.ptr->name,
  174. clkdm->name);
  175. clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
  176. clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
  177. }
  178. }
  179. /*
  180. * _omap2_clkdm_set_hwsup - set the hwsup idle transition bit
  181. * @clkdm: struct clockdomain *
  182. * @enable: int 0 to disable, 1 to enable
  183. *
  184. * Internal helper for actually switching the bit that controls hwsup
  185. * idle transitions for clkdm.
  186. */
  187. static void _omap2_clkdm_set_hwsup(struct clockdomain *clkdm, int enable)
  188. {
  189. u32 bits, v;
  190. if (cpu_is_omap24xx()) {
  191. if (enable)
  192. bits = OMAP24XX_CLKSTCTRL_ENABLE_AUTO;
  193. else
  194. bits = OMAP24XX_CLKSTCTRL_DISABLE_AUTO;
  195. } else if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
  196. if (enable)
  197. bits = OMAP34XX_CLKSTCTRL_ENABLE_AUTO;
  198. else
  199. bits = OMAP34XX_CLKSTCTRL_DISABLE_AUTO;
  200. } else {
  201. BUG();
  202. }
  203. bits = bits << __ffs(clkdm->clktrctrl_mask);
  204. v = __raw_readl(clkdm->clkstctrl_reg);
  205. v &= ~(clkdm->clktrctrl_mask);
  206. v |= bits;
  207. __raw_writel(v, clkdm->clkstctrl_reg);
  208. }
  209. /* Public functions */
  210. /**
  211. * clkdm_init - set up the clockdomain layer
  212. * @clkdms: optional pointer to an array of clockdomains to register
  213. * @init_autodeps: optional pointer to an array of autodeps to register
  214. *
  215. * Set up internal state. If a pointer to an array of clockdomains
  216. * @clkdms was supplied, loop through the list of clockdomains,
  217. * register all that are available on the current platform. Similarly,
  218. * if a pointer to an array of clockdomain autodependencies
  219. * @init_autodeps was provided, register those. No return value.
  220. */
  221. void clkdm_init(struct clockdomain **clkdms,
  222. struct clkdm_autodep *init_autodeps)
  223. {
  224. struct clockdomain **c = NULL;
  225. struct clockdomain *clkdm;
  226. struct clkdm_autodep *autodep = NULL;
  227. if (clkdms)
  228. for (c = clkdms; *c; c++)
  229. _clkdm_register(*c);
  230. autodeps = init_autodeps;
  231. if (autodeps)
  232. for (autodep = autodeps; autodep->clkdm.ptr; autodep++)
  233. _autodep_lookup(autodep);
  234. /*
  235. * Put all clockdomains into software-supervised mode; PM code
  236. * should later enable hardware-supervised mode as appropriate
  237. */
  238. list_for_each_entry(clkdm, &clkdm_list, node) {
  239. if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
  240. omap2_clkdm_wakeup(clkdm);
  241. else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
  242. omap2_clkdm_deny_idle(clkdm);
  243. clkdm_clear_all_wkdeps(clkdm);
  244. clkdm_clear_all_sleepdeps(clkdm);
  245. }
  246. }
  247. /**
  248. * clkdm_lookup - look up a clockdomain by name, return a pointer
  249. * @name: name of clockdomain
  250. *
  251. * Find a registered clockdomain by its name @name. Returns a pointer
  252. * to the struct clockdomain if found, or NULL otherwise.
  253. */
  254. struct clockdomain *clkdm_lookup(const char *name)
  255. {
  256. struct clockdomain *clkdm, *temp_clkdm;
  257. if (!name)
  258. return NULL;
  259. clkdm = NULL;
  260. list_for_each_entry(temp_clkdm, &clkdm_list, node) {
  261. if (!strcmp(name, temp_clkdm->name)) {
  262. clkdm = temp_clkdm;
  263. break;
  264. }
  265. }
  266. return clkdm;
  267. }
  268. /**
  269. * clkdm_for_each - call function on each registered clockdomain
  270. * @fn: callback function *
  271. *
  272. * Call the supplied function @fn for each registered clockdomain.
  273. * The callback function @fn can return anything but 0 to bail
  274. * out early from the iterator. The callback function is called with
  275. * the clkdm_mutex held, so no clockdomain structure manipulation
  276. * functions should be called from the callback, although hardware
  277. * clockdomain control functions are fine. Returns the last return
  278. * value of the callback function, which should be 0 for success or
  279. * anything else to indicate failure; or -EINVAL if the function pointer
  280. * is null.
  281. */
  282. int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
  283. void *user)
  284. {
  285. struct clockdomain *clkdm;
  286. int ret = 0;
  287. if (!fn)
  288. return -EINVAL;
  289. list_for_each_entry(clkdm, &clkdm_list, node) {
  290. ret = (*fn)(clkdm, user);
  291. if (ret)
  292. break;
  293. }
  294. return ret;
  295. }
  296. /**
  297. * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
  298. * @clkdm: struct clockdomain *
  299. *
  300. * Return a pointer to the struct powerdomain that the specified clockdomain
  301. * @clkdm exists in, or returns NULL if @clkdm is NULL.
  302. */
  303. struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
  304. {
  305. if (!clkdm)
  306. return NULL;
  307. return clkdm->pwrdm.ptr;
  308. }
  309. /* Hardware clockdomain control */
  310. /**
  311. * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
  312. * @clkdm1: wake this struct clockdomain * up (dependent)
  313. * @clkdm2: when this struct clockdomain * wakes up (source)
  314. *
  315. * When the clockdomain represented by @clkdm2 wakes up, wake up
  316. * @clkdm1. Implemented in hardware on the OMAP, this feature is
  317. * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
  318. * Returns -EINVAL if presented with invalid clockdomain pointers,
  319. * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
  320. * success.
  321. */
  322. int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  323. {
  324. struct clkdm_dep *cd;
  325. if (!clkdm1 || !clkdm2)
  326. return -EINVAL;
  327. cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
  328. if (IS_ERR(cd)) {
  329. pr_debug("clockdomain: hardware cannot set/clear wake up of "
  330. "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
  331. return PTR_ERR(cd);
  332. }
  333. if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
  334. pr_debug("clockdomain: hardware will wake up %s when %s wakes "
  335. "up\n", clkdm1->name, clkdm2->name);
  336. prm_set_mod_reg_bits((1 << clkdm2->dep_bit),
  337. clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP);
  338. }
  339. return 0;
  340. }
  341. /**
  342. * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
  343. * @clkdm1: wake this struct clockdomain * up (dependent)
  344. * @clkdm2: when this struct clockdomain * wakes up (source)
  345. *
  346. * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
  347. * wakes up. Returns -EINVAL if presented with invalid clockdomain
  348. * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
  349. * 0 upon success.
  350. */
  351. int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  352. {
  353. struct clkdm_dep *cd;
  354. if (!clkdm1 || !clkdm2)
  355. return -EINVAL;
  356. cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
  357. if (IS_ERR(cd)) {
  358. pr_debug("clockdomain: hardware cannot set/clear wake up of "
  359. "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
  360. return PTR_ERR(cd);
  361. }
  362. if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
  363. pr_debug("clockdomain: hardware will no longer wake up %s "
  364. "after %s wakes up\n", clkdm1->name, clkdm2->name);
  365. prm_clear_mod_reg_bits((1 << clkdm2->dep_bit),
  366. clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP);
  367. }
  368. return 0;
  369. }
  370. /**
  371. * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
  372. * @clkdm1: wake this struct clockdomain * up (dependent)
  373. * @clkdm2: when this struct clockdomain * wakes up (source)
  374. *
  375. * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
  376. * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
  377. * if either clockdomain pointer is invalid; or -ENOENT if the hardware
  378. * is incapable.
  379. *
  380. * REVISIT: Currently this function only represents software-controllable
  381. * wakeup dependencies. Wakeup dependencies fixed in hardware are not
  382. * yet handled here.
  383. */
  384. int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  385. {
  386. struct clkdm_dep *cd;
  387. if (!clkdm1 || !clkdm2)
  388. return -EINVAL;
  389. cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
  390. if (IS_ERR(cd)) {
  391. pr_debug("clockdomain: hardware cannot set/clear wake up of "
  392. "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
  393. return PTR_ERR(cd);
  394. }
  395. /* XXX It's faster to return the atomic wkdep_usecount */
  396. return prm_read_mod_bits_shift(clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP,
  397. (1 << clkdm2->dep_bit));
  398. }
  399. /**
  400. * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
  401. * @clkdm: struct clockdomain * to remove all wakeup dependencies from
  402. *
  403. * Remove all inter-clockdomain wakeup dependencies that could cause
  404. * @clkdm to wake. Intended to be used during boot to initialize the
  405. * PRCM to a known state, after all clockdomains are put into swsup idle
  406. * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
  407. * 0 upon success.
  408. */
  409. int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
  410. {
  411. struct clkdm_dep *cd;
  412. u32 mask = 0;
  413. if (!clkdm)
  414. return -EINVAL;
  415. for (cd = clkdm->wkdep_srcs; cd && cd->clkdm_name; cd++) {
  416. if (!omap_chip_is(cd->omap_chip))
  417. continue;
  418. if (!cd->clkdm && cd->clkdm_name)
  419. cd->clkdm = _clkdm_lookup(cd->clkdm_name);
  420. /* PRM accesses are slow, so minimize them */
  421. mask |= 1 << cd->clkdm->dep_bit;
  422. atomic_set(&cd->wkdep_usecount, 0);
  423. }
  424. prm_clear_mod_reg_bits(mask, clkdm->pwrdm.ptr->prcm_offs, PM_WKDEP);
  425. return 0;
  426. }
  427. /**
  428. * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
  429. * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
  430. * @clkdm2: when this struct clockdomain * is active (source)
  431. *
  432. * Prevent @clkdm1 from automatically going inactive (and then to
  433. * retention or off) if @clkdm2 is active. Returns -EINVAL if
  434. * presented with invalid clockdomain pointers or called on a machine
  435. * that does not support software-configurable hardware sleep
  436. * dependencies, -ENOENT if the specified dependency cannot be set in
  437. * hardware, or 0 upon success.
  438. */
  439. int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  440. {
  441. struct clkdm_dep *cd;
  442. if (!cpu_is_omap34xx())
  443. return -EINVAL;
  444. if (!clkdm1 || !clkdm2)
  445. return -EINVAL;
  446. cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
  447. if (IS_ERR(cd)) {
  448. pr_debug("clockdomain: hardware cannot set/clear sleep "
  449. "dependency affecting %s from %s\n", clkdm1->name,
  450. clkdm2->name);
  451. return PTR_ERR(cd);
  452. }
  453. if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
  454. pr_debug("clockdomain: will prevent %s from sleeping if %s "
  455. "is active\n", clkdm1->name, clkdm2->name);
  456. cm_set_mod_reg_bits((1 << clkdm2->dep_bit),
  457. clkdm1->pwrdm.ptr->prcm_offs,
  458. OMAP3430_CM_SLEEPDEP);
  459. }
  460. return 0;
  461. }
  462. /**
  463. * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
  464. * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
  465. * @clkdm2: when this struct clockdomain * is active (source)
  466. *
  467. * Allow @clkdm1 to automatically go inactive (and then to retention or
  468. * off), independent of the activity state of @clkdm2. Returns -EINVAL
  469. * if presented with invalid clockdomain pointers or called on a machine
  470. * that does not support software-configurable hardware sleep dependencies,
  471. * -ENOENT if the specified dependency cannot be cleared in hardware, or
  472. * 0 upon success.
  473. */
  474. int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  475. {
  476. struct clkdm_dep *cd;
  477. if (!cpu_is_omap34xx())
  478. return -EINVAL;
  479. if (!clkdm1 || !clkdm2)
  480. return -EINVAL;
  481. cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
  482. if (IS_ERR(cd)) {
  483. pr_debug("clockdomain: hardware cannot set/clear sleep "
  484. "dependency affecting %s from %s\n", clkdm1->name,
  485. clkdm2->name);
  486. return PTR_ERR(cd);
  487. }
  488. if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
  489. pr_debug("clockdomain: will no longer prevent %s from "
  490. "sleeping if %s is active\n", clkdm1->name,
  491. clkdm2->name);
  492. cm_clear_mod_reg_bits((1 << clkdm2->dep_bit),
  493. clkdm1->pwrdm.ptr->prcm_offs,
  494. OMAP3430_CM_SLEEPDEP);
  495. }
  496. return 0;
  497. }
  498. /**
  499. * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
  500. * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
  501. * @clkdm2: when this struct clockdomain * is active (source)
  502. *
  503. * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
  504. * not be allowed to automatically go inactive if @clkdm2 is active;
  505. * 0 if @clkdm1's automatic power state inactivity transition is independent
  506. * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
  507. * on a machine that does not support software-configurable hardware sleep
  508. * dependencies; or -ENOENT if the hardware is incapable.
  509. *
  510. * REVISIT: Currently this function only represents software-controllable
  511. * sleep dependencies. Sleep dependencies fixed in hardware are not
  512. * yet handled here.
  513. */
  514. int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  515. {
  516. struct clkdm_dep *cd;
  517. if (!cpu_is_omap34xx())
  518. return -EINVAL;
  519. if (!clkdm1 || !clkdm2)
  520. return -EINVAL;
  521. cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
  522. if (IS_ERR(cd)) {
  523. pr_debug("clockdomain: hardware cannot set/clear sleep "
  524. "dependency affecting %s from %s\n", clkdm1->name,
  525. clkdm2->name);
  526. return PTR_ERR(cd);
  527. }
  528. /* XXX It's faster to return the atomic sleepdep_usecount */
  529. return prm_read_mod_bits_shift(clkdm1->pwrdm.ptr->prcm_offs,
  530. OMAP3430_CM_SLEEPDEP,
  531. (1 << clkdm2->dep_bit));
  532. }
  533. /**
  534. * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
  535. * @clkdm: struct clockdomain * to remove all sleep dependencies from
  536. *
  537. * Remove all inter-clockdomain sleep dependencies that could prevent
  538. * @clkdm from idling. Intended to be used during boot to initialize the
  539. * PRCM to a known state, after all clockdomains are put into swsup idle
  540. * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
  541. * 0 upon success.
  542. */
  543. int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
  544. {
  545. struct clkdm_dep *cd;
  546. u32 mask = 0;
  547. if (!cpu_is_omap34xx())
  548. return -EINVAL;
  549. if (!clkdm)
  550. return -EINVAL;
  551. for (cd = clkdm->sleepdep_srcs; cd && cd->clkdm_name; cd++) {
  552. if (!omap_chip_is(cd->omap_chip))
  553. continue;
  554. if (!cd->clkdm && cd->clkdm_name)
  555. cd->clkdm = _clkdm_lookup(cd->clkdm_name);
  556. /* PRM accesses are slow, so minimize them */
  557. mask |= 1 << cd->clkdm->dep_bit;
  558. atomic_set(&cd->sleepdep_usecount, 0);
  559. }
  560. prm_clear_mod_reg_bits(mask, clkdm->pwrdm.ptr->prcm_offs,
  561. OMAP3430_CM_SLEEPDEP);
  562. return 0;
  563. }
  564. /**
  565. * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
  566. * @clkdm: struct clkdm * of a clockdomain
  567. *
  568. * Return the clockdomain @clkdm current state transition mode from the
  569. * corresponding domain CM_CLKSTCTRL register. Returns -EINVAL if @clkdm
  570. * is NULL or the current mode upon success.
  571. */
  572. static int omap2_clkdm_clktrctrl_read(struct clockdomain *clkdm)
  573. {
  574. u32 v;
  575. if (!clkdm)
  576. return -EINVAL;
  577. v = __raw_readl(clkdm->clkstctrl_reg);
  578. v &= clkdm->clktrctrl_mask;
  579. v >>= __ffs(clkdm->clktrctrl_mask);
  580. return v;
  581. }
  582. /**
  583. * omap2_clkdm_sleep - force clockdomain sleep transition
  584. * @clkdm: struct clockdomain *
  585. *
  586. * Instruct the CM to force a sleep transition on the specified
  587. * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
  588. * clockdomain does not support software-initiated sleep; 0 upon
  589. * success.
  590. */
  591. int omap2_clkdm_sleep(struct clockdomain *clkdm)
  592. {
  593. if (!clkdm)
  594. return -EINVAL;
  595. if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
  596. pr_debug("clockdomain: %s does not support forcing "
  597. "sleep via software\n", clkdm->name);
  598. return -EINVAL;
  599. }
  600. pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
  601. if (cpu_is_omap24xx()) {
  602. cm_set_mod_reg_bits(OMAP24XX_FORCESTATE_MASK,
  603. clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
  604. } else if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
  605. u32 bits = (OMAP34XX_CLKSTCTRL_FORCE_SLEEP <<
  606. __ffs(clkdm->clktrctrl_mask));
  607. u32 v = __raw_readl(clkdm->clkstctrl_reg);
  608. v &= ~(clkdm->clktrctrl_mask);
  609. v |= bits;
  610. __raw_writel(v, clkdm->clkstctrl_reg);
  611. } else {
  612. BUG();
  613. };
  614. return 0;
  615. }
  616. /**
  617. * omap2_clkdm_wakeup - force clockdomain wakeup transition
  618. * @clkdm: struct clockdomain *
  619. *
  620. * Instruct the CM to force a wakeup transition on the specified
  621. * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
  622. * clockdomain does not support software-controlled wakeup; 0 upon
  623. * success.
  624. */
  625. int omap2_clkdm_wakeup(struct clockdomain *clkdm)
  626. {
  627. if (!clkdm)
  628. return -EINVAL;
  629. if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
  630. pr_debug("clockdomain: %s does not support forcing "
  631. "wakeup via software\n", clkdm->name);
  632. return -EINVAL;
  633. }
  634. pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
  635. if (cpu_is_omap24xx()) {
  636. cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE_MASK,
  637. clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
  638. } else if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
  639. u32 bits = (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP <<
  640. __ffs(clkdm->clktrctrl_mask));
  641. u32 v = __raw_readl(clkdm->clkstctrl_reg);
  642. v &= ~(clkdm->clktrctrl_mask);
  643. v |= bits;
  644. __raw_writel(v, clkdm->clkstctrl_reg);
  645. } else {
  646. BUG();
  647. };
  648. return 0;
  649. }
  650. /**
  651. * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
  652. * @clkdm: struct clockdomain *
  653. *
  654. * Allow the hardware to automatically switch the clockdomain @clkdm into
  655. * active or idle states, as needed by downstream clocks. If the
  656. * clockdomain has any downstream clocks enabled in the clock
  657. * framework, wkdep/sleepdep autodependencies are added; this is so
  658. * device drivers can read and write to the device. No return value.
  659. */
  660. void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
  661. {
  662. if (!clkdm)
  663. return;
  664. if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
  665. pr_debug("clock: automatic idle transitions cannot be enabled "
  666. "on clockdomain %s\n", clkdm->name);
  667. return;
  668. }
  669. pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
  670. clkdm->name);
  671. /*
  672. * XXX This should be removed once TI adds wakeup/sleep
  673. * dependency code and data for OMAP4.
  674. */
  675. if (cpu_is_omap44xx()) {
  676. WARN_ONCE(1, "clockdomain: OMAP4 wakeup/sleep dependency "
  677. "support is not yet implemented\n");
  678. } else {
  679. if (atomic_read(&clkdm->usecount) > 0)
  680. _clkdm_add_autodeps(clkdm);
  681. }
  682. _omap2_clkdm_set_hwsup(clkdm, 1);
  683. pwrdm_clkdm_state_switch(clkdm);
  684. }
  685. /**
  686. * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
  687. * @clkdm: struct clockdomain *
  688. *
  689. * Prevent the hardware from automatically switching the clockdomain
  690. * @clkdm into inactive or idle states. If the clockdomain has
  691. * downstream clocks enabled in the clock framework, wkdep/sleepdep
  692. * autodependencies are removed. No return value.
  693. */
  694. void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
  695. {
  696. if (!clkdm)
  697. return;
  698. if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
  699. pr_debug("clockdomain: automatic idle transitions cannot be "
  700. "disabled on %s\n", clkdm->name);
  701. return;
  702. }
  703. pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
  704. clkdm->name);
  705. _omap2_clkdm_set_hwsup(clkdm, 0);
  706. /*
  707. * XXX This should be removed once TI adds wakeup/sleep
  708. * dependency code and data for OMAP4.
  709. */
  710. if (cpu_is_omap44xx()) {
  711. WARN_ONCE(1, "clockdomain: OMAP4 wakeup/sleep dependency "
  712. "support is not yet implemented\n");
  713. } else {
  714. if (atomic_read(&clkdm->usecount) > 0)
  715. _clkdm_del_autodeps(clkdm);
  716. }
  717. }
  718. /* Clockdomain-to-clock framework interface code */
  719. /**
  720. * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
  721. * @clkdm: struct clockdomain *
  722. * @clk: struct clk * of the enabled downstream clock
  723. *
  724. * Increment the usecount of the clockdomain @clkdm and ensure that it
  725. * is awake before @clk is enabled. Intended to be called by
  726. * clk_enable() code. If the clockdomain is in software-supervised
  727. * idle mode, force the clockdomain to wake. If the clockdomain is in
  728. * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
  729. * ensure that devices in the clockdomain can be read from/written to
  730. * by on-chip processors. Returns -EINVAL if passed null pointers;
  731. * returns 0 upon success or if the clockdomain is in hwsup idle mode.
  732. */
  733. int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
  734. {
  735. int v;
  736. /*
  737. * XXX Rewrite this code to maintain a list of enabled
  738. * downstream clocks for debugging purposes?
  739. */
  740. if (!clkdm || !clk)
  741. return -EINVAL;
  742. if (atomic_inc_return(&clkdm->usecount) > 1)
  743. return 0;
  744. /* Clockdomain now has one enabled downstream clock */
  745. pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
  746. clk->name);
  747. if (!clkdm->clkstctrl_reg)
  748. return 0;
  749. v = omap2_clkdm_clktrctrl_read(clkdm);
  750. if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
  751. (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
  752. /* Disable HW transitions when we are changing deps */
  753. _omap2_clkdm_set_hwsup(clkdm, 0);
  754. _clkdm_add_autodeps(clkdm);
  755. _omap2_clkdm_set_hwsup(clkdm, 1);
  756. } else {
  757. omap2_clkdm_wakeup(clkdm);
  758. }
  759. pwrdm_wait_transition(clkdm->pwrdm.ptr);
  760. pwrdm_clkdm_state_switch(clkdm);
  761. return 0;
  762. }
  763. /**
  764. * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
  765. * @clkdm: struct clockdomain *
  766. * @clk: struct clk * of the disabled downstream clock
  767. *
  768. * Decrement the usecount of this clockdomain @clkdm when @clk is
  769. * disabled. Intended to be called by clk_disable() code. If the
  770. * clockdomain usecount goes to 0, put the clockdomain to sleep
  771. * (software-supervised mode) or remove the clkdm autodependencies
  772. * (hardware-supervised mode). Returns -EINVAL if passed null
  773. * pointers; -ERANGE if the @clkdm usecount underflows and debugging
  774. * is enabled; or returns 0 upon success or if the clockdomain is in
  775. * hwsup idle mode.
  776. */
  777. int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
  778. {
  779. int v;
  780. /*
  781. * XXX Rewrite this code to maintain a list of enabled
  782. * downstream clocks for debugging purposes?
  783. */
  784. if (!clkdm || !clk)
  785. return -EINVAL;
  786. #ifdef DEBUG
  787. if (atomic_read(&clkdm->usecount) == 0) {
  788. WARN_ON(1); /* underflow */
  789. return -ERANGE;
  790. }
  791. #endif
  792. if (atomic_dec_return(&clkdm->usecount) > 0)
  793. return 0;
  794. /* All downstream clocks of this clockdomain are now disabled */
  795. pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
  796. clk->name);
  797. if (!clkdm->clkstctrl_reg)
  798. return 0;
  799. v = omap2_clkdm_clktrctrl_read(clkdm);
  800. if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
  801. (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
  802. /* Disable HW transitions when we are changing deps */
  803. _omap2_clkdm_set_hwsup(clkdm, 0);
  804. _clkdm_del_autodeps(clkdm);
  805. _omap2_clkdm_set_hwsup(clkdm, 1);
  806. } else {
  807. omap2_clkdm_sleep(clkdm);
  808. }
  809. pwrdm_clkdm_state_switch(clkdm);
  810. return 0;
  811. }