clockdomain.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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-34xx.h"
  29. #include "cminst44xx.h"
  30. #include "prcm44xx.h"
  31. #include <plat/clock.h>
  32. #include <plat/powerdomain.h>
  33. #include <plat/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. u32 bits, v;
  203. if (cpu_is_omap24xx())
  204. bits = OMAP24XX_CLKSTCTRL_ENABLE_AUTO;
  205. else if (cpu_is_omap34xx())
  206. bits = OMAP34XX_CLKSTCTRL_ENABLE_AUTO;
  207. else if (cpu_is_omap44xx())
  208. return omap4_cminst_clkdm_enable_hwsup(clkdm->prcm_partition,
  209. clkdm->cm_inst,
  210. clkdm->clkdm_offs);
  211. else
  212. BUG();
  213. bits = bits << __ffs(clkdm->clktrctrl_mask);
  214. /*
  215. * XXX clkstctrl_reg is known on OMAP2 - this clkdm
  216. * field is not needed
  217. */
  218. v = __raw_readl(clkdm->clkstctrl_reg);
  219. v &= ~(clkdm->clktrctrl_mask);
  220. v |= bits;
  221. __raw_writel(v, clkdm->clkstctrl_reg);
  222. }
  223. /**
  224. * _disable_hwsup - place a clockdomain into software-supervised idle
  225. * @clkdm: struct clockdomain *
  226. *
  227. * Place the clockdomain @clkdm into software-supervised idle mode.
  228. * No return value.
  229. *
  230. * XXX Should this return an error if the clockdomain does not support
  231. * software-supervised idle mode?
  232. */
  233. static void _disable_hwsup(struct clockdomain *clkdm)
  234. {
  235. u32 bits, v;
  236. if (cpu_is_omap24xx())
  237. bits = OMAP24XX_CLKSTCTRL_DISABLE_AUTO;
  238. else if (cpu_is_omap34xx())
  239. bits = OMAP34XX_CLKSTCTRL_DISABLE_AUTO;
  240. else if (cpu_is_omap44xx())
  241. return omap4_cminst_clkdm_disable_hwsup(clkdm->prcm_partition,
  242. clkdm->cm_inst,
  243. clkdm->clkdm_offs);
  244. else
  245. BUG();
  246. bits = bits << __ffs(clkdm->clktrctrl_mask);
  247. /*
  248. * XXX clkstctrl_reg is known on OMAP2 - this clkdm
  249. * field is not needed
  250. */
  251. v = __raw_readl(clkdm->clkstctrl_reg);
  252. v &= ~(clkdm->clktrctrl_mask);
  253. v |= bits;
  254. __raw_writel(v, clkdm->clkstctrl_reg);
  255. }
  256. /* Public functions */
  257. /**
  258. * clkdm_init - set up the clockdomain layer
  259. * @clkdms: optional pointer to an array of clockdomains to register
  260. * @init_autodeps: optional pointer to an array of autodeps to register
  261. *
  262. * Set up internal state. If a pointer to an array of clockdomains
  263. * @clkdms was supplied, loop through the list of clockdomains,
  264. * register all that are available on the current platform. Similarly,
  265. * if a pointer to an array of clockdomain autodependencies
  266. * @init_autodeps was provided, register those. No return value.
  267. */
  268. void clkdm_init(struct clockdomain **clkdms,
  269. struct clkdm_autodep *init_autodeps)
  270. {
  271. struct clockdomain **c = NULL;
  272. struct clockdomain *clkdm;
  273. struct clkdm_autodep *autodep = NULL;
  274. if (clkdms)
  275. for (c = clkdms; *c; c++)
  276. _clkdm_register(*c);
  277. autodeps = init_autodeps;
  278. if (autodeps)
  279. for (autodep = autodeps; autodep->clkdm.ptr; autodep++)
  280. _autodep_lookup(autodep);
  281. /*
  282. * Put all clockdomains into software-supervised mode; PM code
  283. * should later enable hardware-supervised mode as appropriate
  284. */
  285. list_for_each_entry(clkdm, &clkdm_list, node) {
  286. if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
  287. omap2_clkdm_wakeup(clkdm);
  288. else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
  289. omap2_clkdm_deny_idle(clkdm);
  290. clkdm_clear_all_wkdeps(clkdm);
  291. clkdm_clear_all_sleepdeps(clkdm);
  292. }
  293. }
  294. /**
  295. * clkdm_lookup - look up a clockdomain by name, return a pointer
  296. * @name: name of clockdomain
  297. *
  298. * Find a registered clockdomain by its name @name. Returns a pointer
  299. * to the struct clockdomain if found, or NULL otherwise.
  300. */
  301. struct clockdomain *clkdm_lookup(const char *name)
  302. {
  303. struct clockdomain *clkdm, *temp_clkdm;
  304. if (!name)
  305. return NULL;
  306. clkdm = NULL;
  307. list_for_each_entry(temp_clkdm, &clkdm_list, node) {
  308. if (!strcmp(name, temp_clkdm->name)) {
  309. clkdm = temp_clkdm;
  310. break;
  311. }
  312. }
  313. return clkdm;
  314. }
  315. /**
  316. * clkdm_for_each - call function on each registered clockdomain
  317. * @fn: callback function *
  318. *
  319. * Call the supplied function @fn for each registered clockdomain.
  320. * The callback function @fn can return anything but 0 to bail
  321. * out early from the iterator. The callback function is called with
  322. * the clkdm_mutex held, so no clockdomain structure manipulation
  323. * functions should be called from the callback, although hardware
  324. * clockdomain control functions are fine. Returns the last return
  325. * value of the callback function, which should be 0 for success or
  326. * anything else to indicate failure; or -EINVAL if the function pointer
  327. * is null.
  328. */
  329. int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
  330. void *user)
  331. {
  332. struct clockdomain *clkdm;
  333. int ret = 0;
  334. if (!fn)
  335. return -EINVAL;
  336. list_for_each_entry(clkdm, &clkdm_list, node) {
  337. ret = (*fn)(clkdm, user);
  338. if (ret)
  339. break;
  340. }
  341. return ret;
  342. }
  343. /**
  344. * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
  345. * @clkdm: struct clockdomain *
  346. *
  347. * Return a pointer to the struct powerdomain that the specified clockdomain
  348. * @clkdm exists in, or returns NULL if @clkdm is NULL.
  349. */
  350. struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
  351. {
  352. if (!clkdm)
  353. return NULL;
  354. return clkdm->pwrdm.ptr;
  355. }
  356. /* Hardware clockdomain control */
  357. /**
  358. * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
  359. * @clkdm1: wake this struct clockdomain * up (dependent)
  360. * @clkdm2: when this struct clockdomain * wakes up (source)
  361. *
  362. * When the clockdomain represented by @clkdm2 wakes up, wake up
  363. * @clkdm1. Implemented in hardware on the OMAP, this feature is
  364. * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
  365. * Returns -EINVAL if presented with invalid clockdomain pointers,
  366. * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
  367. * success.
  368. */
  369. int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  370. {
  371. struct clkdm_dep *cd;
  372. if (!clkdm1 || !clkdm2)
  373. return -EINVAL;
  374. cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
  375. if (IS_ERR(cd)) {
  376. pr_debug("clockdomain: hardware cannot set/clear wake up of "
  377. "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
  378. return PTR_ERR(cd);
  379. }
  380. if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
  381. pr_debug("clockdomain: hardware will wake up %s when %s wakes "
  382. "up\n", clkdm1->name, clkdm2->name);
  383. omap2_prm_set_mod_reg_bits((1 << clkdm2->dep_bit),
  384. clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP);
  385. }
  386. return 0;
  387. }
  388. /**
  389. * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
  390. * @clkdm1: wake this struct clockdomain * up (dependent)
  391. * @clkdm2: when this struct clockdomain * wakes up (source)
  392. *
  393. * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
  394. * wakes up. Returns -EINVAL if presented with invalid clockdomain
  395. * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
  396. * 0 upon success.
  397. */
  398. int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  399. {
  400. struct clkdm_dep *cd;
  401. if (!clkdm1 || !clkdm2)
  402. return -EINVAL;
  403. cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
  404. if (IS_ERR(cd)) {
  405. pr_debug("clockdomain: hardware cannot set/clear wake up of "
  406. "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
  407. return PTR_ERR(cd);
  408. }
  409. if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
  410. pr_debug("clockdomain: hardware will no longer wake up %s "
  411. "after %s wakes up\n", clkdm1->name, clkdm2->name);
  412. omap2_prm_clear_mod_reg_bits((1 << clkdm2->dep_bit),
  413. clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP);
  414. }
  415. return 0;
  416. }
  417. /**
  418. * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
  419. * @clkdm1: wake this struct clockdomain * up (dependent)
  420. * @clkdm2: when this struct clockdomain * wakes up (source)
  421. *
  422. * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
  423. * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
  424. * if either clockdomain pointer is invalid; or -ENOENT if the hardware
  425. * is incapable.
  426. *
  427. * REVISIT: Currently this function only represents software-controllable
  428. * wakeup dependencies. Wakeup dependencies fixed in hardware are not
  429. * yet handled here.
  430. */
  431. int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  432. {
  433. struct clkdm_dep *cd;
  434. if (!clkdm1 || !clkdm2)
  435. return -EINVAL;
  436. cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
  437. if (IS_ERR(cd)) {
  438. pr_debug("clockdomain: hardware cannot set/clear wake up of "
  439. "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
  440. return PTR_ERR(cd);
  441. }
  442. /* XXX It's faster to return the atomic wkdep_usecount */
  443. return omap2_prm_read_mod_bits_shift(clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP,
  444. (1 << clkdm2->dep_bit));
  445. }
  446. /**
  447. * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
  448. * @clkdm: struct clockdomain * to remove all wakeup dependencies from
  449. *
  450. * Remove all inter-clockdomain wakeup dependencies that could cause
  451. * @clkdm to wake. Intended to be used during boot to initialize the
  452. * PRCM to a known state, after all clockdomains are put into swsup idle
  453. * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
  454. * 0 upon success.
  455. */
  456. int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
  457. {
  458. struct clkdm_dep *cd;
  459. u32 mask = 0;
  460. if (!clkdm)
  461. return -EINVAL;
  462. for (cd = clkdm->wkdep_srcs; cd && cd->clkdm_name; cd++) {
  463. if (!omap_chip_is(cd->omap_chip))
  464. continue;
  465. if (!cd->clkdm && cd->clkdm_name)
  466. cd->clkdm = _clkdm_lookup(cd->clkdm_name);
  467. /* PRM accesses are slow, so minimize them */
  468. mask |= 1 << cd->clkdm->dep_bit;
  469. atomic_set(&cd->wkdep_usecount, 0);
  470. }
  471. omap2_prm_clear_mod_reg_bits(mask, clkdm->pwrdm.ptr->prcm_offs, PM_WKDEP);
  472. return 0;
  473. }
  474. /**
  475. * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
  476. * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
  477. * @clkdm2: when this struct clockdomain * is active (source)
  478. *
  479. * Prevent @clkdm1 from automatically going inactive (and then to
  480. * retention or off) if @clkdm2 is active. Returns -EINVAL if
  481. * presented with invalid clockdomain pointers or called on a machine
  482. * that does not support software-configurable hardware sleep
  483. * dependencies, -ENOENT if the specified dependency cannot be set in
  484. * hardware, or 0 upon success.
  485. */
  486. int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  487. {
  488. struct clkdm_dep *cd;
  489. if (!cpu_is_omap34xx())
  490. return -EINVAL;
  491. if (!clkdm1 || !clkdm2)
  492. return -EINVAL;
  493. cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
  494. if (IS_ERR(cd)) {
  495. pr_debug("clockdomain: hardware cannot set/clear sleep "
  496. "dependency affecting %s from %s\n", clkdm1->name,
  497. clkdm2->name);
  498. return PTR_ERR(cd);
  499. }
  500. if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
  501. pr_debug("clockdomain: will prevent %s from sleeping if %s "
  502. "is active\n", clkdm1->name, clkdm2->name);
  503. omap2_cm_set_mod_reg_bits((1 << clkdm2->dep_bit),
  504. clkdm1->pwrdm.ptr->prcm_offs,
  505. OMAP3430_CM_SLEEPDEP);
  506. }
  507. return 0;
  508. }
  509. /**
  510. * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
  511. * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
  512. * @clkdm2: when this struct clockdomain * is active (source)
  513. *
  514. * Allow @clkdm1 to automatically go inactive (and then to retention or
  515. * off), independent of the activity state of @clkdm2. Returns -EINVAL
  516. * if presented with invalid clockdomain pointers or called on a machine
  517. * that does not support software-configurable hardware sleep dependencies,
  518. * -ENOENT if the specified dependency cannot be cleared in hardware, or
  519. * 0 upon success.
  520. */
  521. int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  522. {
  523. struct clkdm_dep *cd;
  524. if (!cpu_is_omap34xx())
  525. return -EINVAL;
  526. if (!clkdm1 || !clkdm2)
  527. return -EINVAL;
  528. cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
  529. if (IS_ERR(cd)) {
  530. pr_debug("clockdomain: hardware cannot set/clear sleep "
  531. "dependency affecting %s from %s\n", clkdm1->name,
  532. clkdm2->name);
  533. return PTR_ERR(cd);
  534. }
  535. if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
  536. pr_debug("clockdomain: will no longer prevent %s from "
  537. "sleeping if %s is active\n", clkdm1->name,
  538. clkdm2->name);
  539. omap2_cm_clear_mod_reg_bits((1 << clkdm2->dep_bit),
  540. clkdm1->pwrdm.ptr->prcm_offs,
  541. OMAP3430_CM_SLEEPDEP);
  542. }
  543. return 0;
  544. }
  545. /**
  546. * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
  547. * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
  548. * @clkdm2: when this struct clockdomain * is active (source)
  549. *
  550. * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
  551. * not be allowed to automatically go inactive if @clkdm2 is active;
  552. * 0 if @clkdm1's automatic power state inactivity transition is independent
  553. * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
  554. * on a machine that does not support software-configurable hardware sleep
  555. * dependencies; or -ENOENT if the hardware is incapable.
  556. *
  557. * REVISIT: Currently this function only represents software-controllable
  558. * sleep dependencies. Sleep dependencies fixed in hardware are not
  559. * yet handled here.
  560. */
  561. int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  562. {
  563. struct clkdm_dep *cd;
  564. if (!cpu_is_omap34xx())
  565. return -EINVAL;
  566. if (!clkdm1 || !clkdm2)
  567. return -EINVAL;
  568. cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
  569. if (IS_ERR(cd)) {
  570. pr_debug("clockdomain: hardware cannot set/clear sleep "
  571. "dependency affecting %s from %s\n", clkdm1->name,
  572. clkdm2->name);
  573. return PTR_ERR(cd);
  574. }
  575. /* XXX It's faster to return the atomic sleepdep_usecount */
  576. return omap2_prm_read_mod_bits_shift(clkdm1->pwrdm.ptr->prcm_offs,
  577. OMAP3430_CM_SLEEPDEP,
  578. (1 << clkdm2->dep_bit));
  579. }
  580. /**
  581. * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
  582. * @clkdm: struct clockdomain * to remove all sleep dependencies from
  583. *
  584. * Remove all inter-clockdomain sleep dependencies that could prevent
  585. * @clkdm from idling. Intended to be used during boot to initialize the
  586. * PRCM to a known state, after all clockdomains are put into swsup idle
  587. * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
  588. * 0 upon success.
  589. */
  590. int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
  591. {
  592. struct clkdm_dep *cd;
  593. u32 mask = 0;
  594. if (!cpu_is_omap34xx())
  595. return -EINVAL;
  596. if (!clkdm)
  597. return -EINVAL;
  598. for (cd = clkdm->sleepdep_srcs; cd && cd->clkdm_name; cd++) {
  599. if (!omap_chip_is(cd->omap_chip))
  600. continue;
  601. if (!cd->clkdm && cd->clkdm_name)
  602. cd->clkdm = _clkdm_lookup(cd->clkdm_name);
  603. /* PRM accesses are slow, so minimize them */
  604. mask |= 1 << cd->clkdm->dep_bit;
  605. atomic_set(&cd->sleepdep_usecount, 0);
  606. }
  607. omap2_prm_clear_mod_reg_bits(mask, clkdm->pwrdm.ptr->prcm_offs,
  608. OMAP3430_CM_SLEEPDEP);
  609. return 0;
  610. }
  611. /**
  612. * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
  613. * @clkdm: struct clkdm * of a clockdomain
  614. *
  615. * Return the clockdomain @clkdm current state transition mode from the
  616. * corresponding domain CM_CLKSTCTRL register. Returns -EINVAL if @clkdm
  617. * is NULL or the current mode upon success.
  618. */
  619. static int omap2_clkdm_clktrctrl_read(struct clockdomain *clkdm)
  620. {
  621. u32 v = 0;
  622. if (!clkdm)
  623. return -EINVAL;
  624. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  625. v = __raw_readl(clkdm->clkstctrl_reg);
  626. v &= clkdm->clktrctrl_mask;
  627. v >>= __ffs(clkdm->clktrctrl_mask);
  628. } else if (cpu_is_omap44xx()) {
  629. pr_warn("OMAP4 clockdomain: missing wakeup/sleep deps\n");
  630. } else {
  631. BUG();
  632. }
  633. return v;
  634. }
  635. /**
  636. * omap2_clkdm_sleep - force clockdomain sleep transition
  637. * @clkdm: struct clockdomain *
  638. *
  639. * Instruct the CM to force a sleep transition on the specified
  640. * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
  641. * clockdomain does not support software-initiated sleep; 0 upon
  642. * success.
  643. */
  644. int omap2_clkdm_sleep(struct clockdomain *clkdm)
  645. {
  646. u32 bits, v;
  647. if (!clkdm)
  648. return -EINVAL;
  649. if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
  650. pr_debug("clockdomain: %s does not support forcing "
  651. "sleep via software\n", clkdm->name);
  652. return -EINVAL;
  653. }
  654. pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
  655. if (cpu_is_omap24xx()) {
  656. omap2_cm_set_mod_reg_bits(OMAP24XX_FORCESTATE_MASK,
  657. clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
  658. } else if (cpu_is_omap34xx()) {
  659. bits = (OMAP34XX_CLKSTCTRL_FORCE_SLEEP <<
  660. __ffs(clkdm->clktrctrl_mask));
  661. v = __raw_readl(clkdm->clkstctrl_reg);
  662. v &= ~(clkdm->clktrctrl_mask);
  663. v |= bits;
  664. __raw_writel(v, clkdm->clkstctrl_reg);
  665. } else if (cpu_is_omap44xx()) {
  666. omap4_cminst_clkdm_force_sleep(clkdm->prcm_partition,
  667. clkdm->cm_inst,
  668. clkdm->clkdm_offs);
  669. } else {
  670. BUG();
  671. };
  672. return 0;
  673. }
  674. /**
  675. * omap2_clkdm_wakeup - force clockdomain wakeup transition
  676. * @clkdm: struct clockdomain *
  677. *
  678. * Instruct the CM to force a wakeup transition on the specified
  679. * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
  680. * clockdomain does not support software-controlled wakeup; 0 upon
  681. * success.
  682. */
  683. int omap2_clkdm_wakeup(struct clockdomain *clkdm)
  684. {
  685. u32 bits, v;
  686. if (!clkdm)
  687. return -EINVAL;
  688. if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
  689. pr_debug("clockdomain: %s does not support forcing "
  690. "wakeup via software\n", clkdm->name);
  691. return -EINVAL;
  692. }
  693. pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
  694. if (cpu_is_omap24xx()) {
  695. omap2_cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE_MASK,
  696. clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
  697. } else if (cpu_is_omap34xx()) {
  698. bits = (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP <<
  699. __ffs(clkdm->clktrctrl_mask));
  700. v = __raw_readl(clkdm->clkstctrl_reg);
  701. v &= ~(clkdm->clktrctrl_mask);
  702. v |= bits;
  703. __raw_writel(v, clkdm->clkstctrl_reg);
  704. } else if (cpu_is_omap44xx()) {
  705. omap4_cminst_clkdm_force_wakeup(clkdm->prcm_partition,
  706. clkdm->cm_inst,
  707. clkdm->clkdm_offs);
  708. } else {
  709. BUG();
  710. };
  711. return 0;
  712. }
  713. /**
  714. * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
  715. * @clkdm: struct clockdomain *
  716. *
  717. * Allow the hardware to automatically switch the clockdomain @clkdm into
  718. * active or idle states, as needed by downstream clocks. If the
  719. * clockdomain has any downstream clocks enabled in the clock
  720. * framework, wkdep/sleepdep autodependencies are added; this is so
  721. * device drivers can read and write to the device. No return value.
  722. */
  723. void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
  724. {
  725. if (!clkdm)
  726. return;
  727. if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
  728. pr_debug("clock: automatic idle transitions cannot be enabled "
  729. "on clockdomain %s\n", clkdm->name);
  730. return;
  731. }
  732. pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
  733. clkdm->name);
  734. /*
  735. * XXX This should be removed once TI adds wakeup/sleep
  736. * dependency code and data for OMAP4.
  737. */
  738. if (cpu_is_omap44xx()) {
  739. WARN_ONCE(1, "clockdomain: OMAP4 wakeup/sleep dependency "
  740. "support is not yet implemented\n");
  741. } else {
  742. if (atomic_read(&clkdm->usecount) > 0)
  743. _clkdm_add_autodeps(clkdm);
  744. }
  745. _enable_hwsup(clkdm);
  746. pwrdm_clkdm_state_switch(clkdm);
  747. }
  748. /**
  749. * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
  750. * @clkdm: struct clockdomain *
  751. *
  752. * Prevent the hardware from automatically switching the clockdomain
  753. * @clkdm into inactive or idle states. If the clockdomain has
  754. * downstream clocks enabled in the clock framework, wkdep/sleepdep
  755. * autodependencies are removed. No return value.
  756. */
  757. void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
  758. {
  759. if (!clkdm)
  760. return;
  761. if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
  762. pr_debug("clockdomain: automatic idle transitions cannot be "
  763. "disabled on %s\n", clkdm->name);
  764. return;
  765. }
  766. pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
  767. clkdm->name);
  768. _disable_hwsup(clkdm);
  769. /*
  770. * XXX This should be removed once TI adds wakeup/sleep
  771. * dependency code and data for OMAP4.
  772. */
  773. if (cpu_is_omap44xx()) {
  774. WARN_ONCE(1, "clockdomain: OMAP4 wakeup/sleep dependency "
  775. "support is not yet implemented\n");
  776. } else {
  777. if (atomic_read(&clkdm->usecount) > 0)
  778. _clkdm_del_autodeps(clkdm);
  779. }
  780. }
  781. /* Clockdomain-to-clock framework interface code */
  782. /**
  783. * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
  784. * @clkdm: struct clockdomain *
  785. * @clk: struct clk * of the enabled downstream clock
  786. *
  787. * Increment the usecount of the clockdomain @clkdm and ensure that it
  788. * is awake before @clk is enabled. Intended to be called by
  789. * clk_enable() code. If the clockdomain is in software-supervised
  790. * idle mode, force the clockdomain to wake. If the clockdomain is in
  791. * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
  792. * ensure that devices in the clockdomain can be read from/written to
  793. * by on-chip processors. Returns -EINVAL if passed null pointers;
  794. * returns 0 upon success or if the clockdomain is in hwsup idle mode.
  795. */
  796. int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
  797. {
  798. int v;
  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. if (atomic_inc_return(&clkdm->usecount) > 1)
  806. return 0;
  807. /* Clockdomain now has one enabled downstream clock */
  808. pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
  809. clk->name);
  810. if (!clkdm->clkstctrl_reg)
  811. return 0;
  812. v = omap2_clkdm_clktrctrl_read(clkdm);
  813. if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
  814. (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
  815. /* Disable HW transitions when we are changing deps */
  816. _disable_hwsup(clkdm);
  817. _clkdm_add_autodeps(clkdm);
  818. _enable_hwsup(clkdm);
  819. } else {
  820. omap2_clkdm_wakeup(clkdm);
  821. }
  822. pwrdm_wait_transition(clkdm->pwrdm.ptr);
  823. pwrdm_clkdm_state_switch(clkdm);
  824. return 0;
  825. }
  826. /**
  827. * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
  828. * @clkdm: struct clockdomain *
  829. * @clk: struct clk * of the disabled downstream clock
  830. *
  831. * Decrement the usecount of this clockdomain @clkdm when @clk is
  832. * disabled. Intended to be called by clk_disable() code. If the
  833. * clockdomain usecount goes to 0, put the clockdomain to sleep
  834. * (software-supervised mode) or remove the clkdm autodependencies
  835. * (hardware-supervised mode). Returns -EINVAL if passed null
  836. * pointers; -ERANGE if the @clkdm usecount underflows and debugging
  837. * is enabled; or returns 0 upon success or if the clockdomain is in
  838. * hwsup idle mode.
  839. */
  840. int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
  841. {
  842. int v;
  843. /*
  844. * XXX Rewrite this code to maintain a list of enabled
  845. * downstream clocks for debugging purposes?
  846. */
  847. if (!clkdm || !clk)
  848. return -EINVAL;
  849. #ifdef DEBUG
  850. if (atomic_read(&clkdm->usecount) == 0) {
  851. WARN_ON(1); /* underflow */
  852. return -ERANGE;
  853. }
  854. #endif
  855. if (atomic_dec_return(&clkdm->usecount) > 0)
  856. return 0;
  857. /* All downstream clocks of this clockdomain are now disabled */
  858. pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
  859. clk->name);
  860. if (!clkdm->clkstctrl_reg)
  861. return 0;
  862. v = omap2_clkdm_clktrctrl_read(clkdm);
  863. if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
  864. (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
  865. /* Disable HW transitions when we are changing deps */
  866. _disable_hwsup(clkdm);
  867. _clkdm_del_autodeps(clkdm);
  868. _enable_hwsup(clkdm);
  869. } else {
  870. omap2_clkdm_sleep(clkdm);
  871. }
  872. pwrdm_clkdm_state_switch(clkdm);
  873. return 0;
  874. }