clockdomain.c 28 KB

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