clockdomain.c 28 KB

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