clockdomain.c 28 KB

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