clockdomain.c 29 KB

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