clockdomain.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. * @clkdms was supplied, loop through the list of clockdomains,
  287. * register all that are available on the current platform. Similarly,
  288. * if a pointer to an array of clockdomain autodependencies
  289. * @init_autodeps was provided, register 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 @name. Returns a pointer
  318. * to the 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 @fn for each registered clockdomain.
  339. * The callback function @fn 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 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. * @clkdm: struct clkdm * of a clockdomain
  629. *
  630. * Return the clockdomain @clkdm current state transition mode from the
  631. * corresponding domain CM_CLKSTCTRL register. Returns -EINVAL if @clkdm
  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 @clkdm 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 @clkdm 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. * @clkdm into inactive or idle states. If the clockdomain has
  744. * downstream 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 the clockdomain @clkdm and ensure that it
  769. * is awake before @clk is enabled. Intended to be called by
  770. * clk_enable() code. If the clockdomain is in software-supervised
  771. * idle mode, force the clockdomain to wake. If the clockdomain is in
  772. * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
  773. * ensure that devices in the clockdomain can be read from/written to
  774. * by on-chip processors. Returns -EINVAL if passed null pointers;
  775. * returns 0 upon success or 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 when @clk is
  811. * disabled. Intended to be called by clk_disable() code. If the
  812. * clockdomain usecount goes to 0, put the clockdomain to sleep
  813. * (software-supervised mode) or remove the clkdm autodependencies
  814. * (hardware-supervised mode). Returns -EINVAL if passed null
  815. * pointers; -ERANGE if the @clkdm usecount underflows and debugging
  816. * is enabled; or returns 0 upon success or if the clockdomain is in
  817. * hwsup idle mode.
  818. */
  819. int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
  820. {
  821. int v;
  822. /*
  823. * XXX Rewrite this code to maintain a list of enabled
  824. * downstream clocks for debugging purposes?
  825. */
  826. if (!clkdm || !clk || !clkdm->clkstctrl_reg)
  827. return -EINVAL;
  828. #ifdef DEBUG
  829. if (atomic_read(&clkdm->usecount) == 0) {
  830. WARN_ON(1); /* underflow */
  831. return -ERANGE;
  832. }
  833. #endif
  834. if (atomic_dec_return(&clkdm->usecount) > 0)
  835. return 0;
  836. /* All downstream clocks of this clockdomain are now disabled */
  837. pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
  838. clk->name);
  839. v = omap2_clkdm_clktrctrl_read(clkdm);
  840. if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
  841. (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
  842. /* Disable HW transitions when we are changing deps */
  843. _omap2_clkdm_set_hwsup(clkdm, 0);
  844. _clkdm_del_autodeps(clkdm);
  845. _omap2_clkdm_set_hwsup(clkdm, 1);
  846. } else {
  847. omap2_clkdm_sleep(clkdm);
  848. }
  849. pwrdm_clkdm_state_switch(clkdm);
  850. return 0;
  851. }