clockdomain.c 29 KB

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