clockdomain.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. * OMAP2/3 clockdomain framework functions
  3. *
  4. * Copyright (C) 2008 Texas Instruments, Inc.
  5. * Copyright (C) 2008 Nokia Corporation
  6. *
  7. * Written by Paul Walmsley and Jouni Högander
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #ifdef CONFIG_OMAP_DEBUG_CLOCKDOMAIN
  14. # define DEBUG
  15. #endif
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #include <linux/list.h>
  20. #include <linux/errno.h>
  21. #include <linux/delay.h>
  22. #include <linux/clk.h>
  23. #include <linux/limits.h>
  24. #include <linux/io.h>
  25. #include <linux/bitops.h>
  26. #include <mach/clock.h>
  27. #include "prm.h"
  28. #include "prm-regbits-24xx.h"
  29. #include "cm.h"
  30. #include <mach/powerdomain.h>
  31. #include <mach/clockdomain.h>
  32. /* clkdm_list contains all registered struct clockdomains */
  33. static LIST_HEAD(clkdm_list);
  34. /* clkdm_mutex protects clkdm_list add and del ops */
  35. static DEFINE_MUTEX(clkdm_mutex);
  36. /* array of powerdomain deps to be added/removed when clkdm in hwsup mode */
  37. static struct clkdm_pwrdm_autodep *autodeps;
  38. /* Private functions */
  39. /*
  40. * _autodep_lookup - resolve autodep pwrdm names to pwrdm pointers; store
  41. * @autodep: struct clkdm_pwrdm_autodep * to resolve
  42. *
  43. * Resolve autodep powerdomain names to powerdomain pointers via
  44. * pwrdm_lookup() and store the pointers in the autodep structure. An
  45. * "autodep" is a powerdomain sleep/wakeup dependency that is
  46. * automatically added and removed whenever clocks in the associated
  47. * clockdomain are enabled or disabled (respectively) when the
  48. * clockdomain is in hardware-supervised mode. Meant to be called
  49. * once at clockdomain layer initialization, since these should remain
  50. * fixed for a particular architecture. No return value.
  51. */
  52. static void _autodep_lookup(struct clkdm_pwrdm_autodep *autodep)
  53. {
  54. struct powerdomain *pwrdm;
  55. if (!autodep)
  56. return;
  57. if (!omap_chip_is(autodep->omap_chip))
  58. return;
  59. pwrdm = pwrdm_lookup(autodep->pwrdm_name);
  60. if (!pwrdm) {
  61. pr_debug("clockdomain: _autodep_lookup: powerdomain %s "
  62. "does not exist\n", autodep->pwrdm_name);
  63. WARN_ON(1);
  64. return;
  65. }
  66. autodep->pwrdm = pwrdm;
  67. return;
  68. }
  69. /*
  70. * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
  71. * @clkdm: struct clockdomain *
  72. *
  73. * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
  74. * in hardware-supervised mode. Meant to be called from clock framework
  75. * when a clock inside clockdomain 'clkdm' is enabled. No return value.
  76. */
  77. static void _clkdm_add_autodeps(struct clockdomain *clkdm)
  78. {
  79. struct clkdm_pwrdm_autodep *autodep;
  80. for (autodep = autodeps; autodep->pwrdm_name; autodep++) {
  81. if (!autodep->pwrdm)
  82. continue;
  83. pr_debug("clockdomain: adding %s sleepdep/wkdep for "
  84. "pwrdm %s\n", autodep->pwrdm_name,
  85. clkdm->pwrdm->name);
  86. pwrdm_add_sleepdep(clkdm->pwrdm, autodep->pwrdm);
  87. pwrdm_add_wkdep(clkdm->pwrdm, autodep->pwrdm);
  88. }
  89. }
  90. /*
  91. * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
  92. * @clkdm: struct clockdomain *
  93. *
  94. * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
  95. * in hardware-supervised mode. Meant to be called from clock framework
  96. * when a clock inside clockdomain 'clkdm' is disabled. No return value.
  97. */
  98. static void _clkdm_del_autodeps(struct clockdomain *clkdm)
  99. {
  100. struct clkdm_pwrdm_autodep *autodep;
  101. for (autodep = autodeps; autodep->pwrdm_name; autodep++) {
  102. if (!autodep->pwrdm)
  103. continue;
  104. pr_debug("clockdomain: removing %s sleepdep/wkdep for "
  105. "pwrdm %s\n", autodep->pwrdm_name,
  106. clkdm->pwrdm->name);
  107. pwrdm_del_sleepdep(clkdm->pwrdm, autodep->pwrdm);
  108. pwrdm_del_wkdep(clkdm->pwrdm, autodep->pwrdm);
  109. }
  110. }
  111. static struct clockdomain *_clkdm_lookup(const char *name)
  112. {
  113. struct clockdomain *clkdm, *temp_clkdm;
  114. if (!name)
  115. return NULL;
  116. clkdm = NULL;
  117. list_for_each_entry(temp_clkdm, &clkdm_list, node) {
  118. if (!strcmp(name, temp_clkdm->name)) {
  119. clkdm = temp_clkdm;
  120. break;
  121. }
  122. }
  123. return clkdm;
  124. }
  125. /* Public functions */
  126. /**
  127. * clkdm_init - set up the clockdomain layer
  128. * @clkdms: optional pointer to an array of clockdomains to register
  129. * @init_autodeps: optional pointer to an array of autodeps to register
  130. *
  131. * Set up internal state. If a pointer to an array of clockdomains
  132. * was supplied, loop through the list of clockdomains, register all
  133. * that are available on the current platform. Similarly, if a
  134. * pointer to an array of clockdomain-powerdomain autodependencies was
  135. * provided, register those. No return value.
  136. */
  137. void clkdm_init(struct clockdomain **clkdms,
  138. struct clkdm_pwrdm_autodep *init_autodeps)
  139. {
  140. struct clockdomain **c = NULL;
  141. struct clkdm_pwrdm_autodep *autodep = NULL;
  142. if (clkdms)
  143. for (c = clkdms; *c; c++)
  144. clkdm_register(*c);
  145. autodeps = init_autodeps;
  146. if (autodeps)
  147. for (autodep = autodeps; autodep->pwrdm_name; autodep++)
  148. _autodep_lookup(autodep);
  149. }
  150. /**
  151. * clkdm_register - register a clockdomain
  152. * @clkdm: struct clockdomain * to register
  153. *
  154. * Adds a clockdomain to the internal clockdomain list.
  155. * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
  156. * already registered by the provided name, or 0 upon success.
  157. */
  158. int clkdm_register(struct clockdomain *clkdm)
  159. {
  160. int ret = -EINVAL;
  161. struct powerdomain *pwrdm;
  162. if (!clkdm || !clkdm->name)
  163. return -EINVAL;
  164. if (!omap_chip_is(clkdm->omap_chip))
  165. return -EINVAL;
  166. pwrdm = pwrdm_lookup(clkdm->pwrdm_name);
  167. if (!pwrdm) {
  168. pr_debug("clockdomain: clkdm_register %s: powerdomain %s "
  169. "does not exist\n", clkdm->name, clkdm->pwrdm_name);
  170. return -EINVAL;
  171. }
  172. clkdm->pwrdm = pwrdm;
  173. mutex_lock(&clkdm_mutex);
  174. /* Verify that the clockdomain is not already registered */
  175. if (_clkdm_lookup(clkdm->name)) {
  176. ret = -EEXIST;
  177. goto cr_unlock;
  178. };
  179. list_add(&clkdm->node, &clkdm_list);
  180. pwrdm_add_clkdm(pwrdm, clkdm);
  181. pr_debug("clockdomain: registered %s\n", clkdm->name);
  182. ret = 0;
  183. cr_unlock:
  184. mutex_unlock(&clkdm_mutex);
  185. return ret;
  186. }
  187. /**
  188. * clkdm_unregister - unregister a clockdomain
  189. * @clkdm: struct clockdomain * to unregister
  190. *
  191. * Removes a clockdomain from the internal clockdomain list. Returns
  192. * -EINVAL if clkdm argument is NULL.
  193. */
  194. int clkdm_unregister(struct clockdomain *clkdm)
  195. {
  196. if (!clkdm)
  197. return -EINVAL;
  198. pwrdm_del_clkdm(clkdm->pwrdm, clkdm);
  199. mutex_lock(&clkdm_mutex);
  200. list_del(&clkdm->node);
  201. mutex_unlock(&clkdm_mutex);
  202. pr_debug("clockdomain: unregistered %s\n", clkdm->name);
  203. return 0;
  204. }
  205. /**
  206. * clkdm_lookup - look up a clockdomain by name, return a pointer
  207. * @name: name of clockdomain
  208. *
  209. * Find a registered clockdomain by its name. Returns a pointer to the
  210. * struct clockdomain if found, or NULL otherwise.
  211. */
  212. struct clockdomain *clkdm_lookup(const char *name)
  213. {
  214. struct clockdomain *clkdm, *temp_clkdm;
  215. if (!name)
  216. return NULL;
  217. clkdm = NULL;
  218. mutex_lock(&clkdm_mutex);
  219. list_for_each_entry(temp_clkdm, &clkdm_list, node) {
  220. if (!strcmp(name, temp_clkdm->name)) {
  221. clkdm = temp_clkdm;
  222. break;
  223. }
  224. }
  225. mutex_unlock(&clkdm_mutex);
  226. return clkdm;
  227. }
  228. /**
  229. * clkdm_for_each - call function on each registered clockdomain
  230. * @fn: callback function *
  231. *
  232. * Call the supplied function for each registered clockdomain.
  233. * The callback function can return anything but 0 to bail
  234. * out early from the iterator. The callback function is called with
  235. * the clkdm_mutex held, so no clockdomain structure manipulation
  236. * functions should be called from the callback, although hardware
  237. * clockdomain control functions are fine. Returns the last return
  238. * value of the callback function, which should be 0 for success or
  239. * anything else to indicate failure; or -EINVAL if the function pointer
  240. * is null.
  241. */
  242. int clkdm_for_each(int (*fn)(struct clockdomain *clkdm))
  243. {
  244. struct clockdomain *clkdm;
  245. int ret = 0;
  246. if (!fn)
  247. return -EINVAL;
  248. mutex_lock(&clkdm_mutex);
  249. list_for_each_entry(clkdm, &clkdm_list, node) {
  250. ret = (*fn)(clkdm);
  251. if (ret)
  252. break;
  253. }
  254. mutex_unlock(&clkdm_mutex);
  255. return ret;
  256. }
  257. /**
  258. * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
  259. * @clkdm: struct clockdomain *
  260. *
  261. * Return a pointer to the struct powerdomain that the specified clockdomain
  262. * 'clkdm' exists in, or returns NULL if clkdm argument is NULL.
  263. */
  264. struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
  265. {
  266. if (!clkdm)
  267. return NULL;
  268. return clkdm->pwrdm;
  269. }
  270. /* Hardware clockdomain control */
  271. /**
  272. * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
  273. * @clk: struct clk * of a clockdomain
  274. *
  275. * Return the clockdomain's current state transition mode from the
  276. * corresponding domain CM_CLKSTCTRL register. Returns -EINVAL if clk
  277. * is NULL or the current mode upon success.
  278. */
  279. static int omap2_clkdm_clktrctrl_read(struct clockdomain *clkdm)
  280. {
  281. u32 v;
  282. if (!clkdm)
  283. return -EINVAL;
  284. v = cm_read_mod_reg(clkdm->pwrdm->prcm_offs, CM_CLKSTCTRL);
  285. v &= clkdm->clktrctrl_mask;
  286. v >>= __ffs(clkdm->clktrctrl_mask);
  287. return v;
  288. }
  289. /**
  290. * omap2_clkdm_sleep - force clockdomain sleep transition
  291. * @clkdm: struct clockdomain *
  292. *
  293. * Instruct the CM to force a sleep transition on the specified
  294. * clockdomain 'clkdm'. Returns -EINVAL if clk is NULL or if
  295. * clockdomain does not support software-initiated sleep; 0 upon
  296. * success.
  297. */
  298. int omap2_clkdm_sleep(struct clockdomain *clkdm)
  299. {
  300. if (!clkdm)
  301. return -EINVAL;
  302. if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
  303. pr_debug("clockdomain: %s does not support forcing "
  304. "sleep via software\n", clkdm->name);
  305. return -EINVAL;
  306. }
  307. pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
  308. if (cpu_is_omap24xx()) {
  309. cm_set_mod_reg_bits(OMAP24XX_FORCESTATE,
  310. clkdm->pwrdm->prcm_offs, PM_PWSTCTRL);
  311. } else if (cpu_is_omap34xx()) {
  312. u32 v = (OMAP34XX_CLKSTCTRL_FORCE_SLEEP <<
  313. __ffs(clkdm->clktrctrl_mask));
  314. cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
  315. clkdm->pwrdm->prcm_offs, CM_CLKSTCTRL);
  316. } else {
  317. BUG();
  318. };
  319. return 0;
  320. }
  321. /**
  322. * omap2_clkdm_wakeup - force clockdomain wakeup transition
  323. * @clkdm: struct clockdomain *
  324. *
  325. * Instruct the CM to force a wakeup transition on the specified
  326. * clockdomain 'clkdm'. Returns -EINVAL if clkdm is NULL or if the
  327. * clockdomain does not support software-controlled wakeup; 0 upon
  328. * success.
  329. */
  330. int omap2_clkdm_wakeup(struct clockdomain *clkdm)
  331. {
  332. if (!clkdm)
  333. return -EINVAL;
  334. if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
  335. pr_debug("clockdomain: %s does not support forcing "
  336. "wakeup via software\n", clkdm->name);
  337. return -EINVAL;
  338. }
  339. pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
  340. if (cpu_is_omap24xx()) {
  341. cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE,
  342. clkdm->pwrdm->prcm_offs, PM_PWSTCTRL);
  343. } else if (cpu_is_omap34xx()) {
  344. u32 v = (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP <<
  345. __ffs(clkdm->clktrctrl_mask));
  346. cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
  347. clkdm->pwrdm->prcm_offs, CM_CLKSTCTRL);
  348. } else {
  349. BUG();
  350. };
  351. return 0;
  352. }
  353. /**
  354. * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
  355. * @clkdm: struct clockdomain *
  356. *
  357. * Allow the hardware to automatically switch the clockdomain into
  358. * active or idle states, as needed by downstream clocks. If the
  359. * clockdomain has any downstream clocks enabled in the clock
  360. * framework, wkdep/sleepdep autodependencies are added; this is so
  361. * device drivers can read and write to the device. No return value.
  362. */
  363. void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
  364. {
  365. u32 v;
  366. if (!clkdm)
  367. return;
  368. if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
  369. pr_debug("clock: automatic idle transitions cannot be enabled "
  370. "on clockdomain %s\n", clkdm->name);
  371. return;
  372. }
  373. pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
  374. clkdm->name);
  375. if (atomic_read(&clkdm->usecount) > 0)
  376. _clkdm_add_autodeps(clkdm);
  377. if (cpu_is_omap24xx())
  378. v = OMAP24XX_CLKSTCTRL_ENABLE_AUTO;
  379. else if (cpu_is_omap34xx())
  380. v = OMAP34XX_CLKSTCTRL_ENABLE_AUTO;
  381. else
  382. BUG();
  383. cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask,
  384. v << __ffs(clkdm->clktrctrl_mask),
  385. clkdm->pwrdm->prcm_offs,
  386. CM_CLKSTCTRL);
  387. }
  388. /**
  389. * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
  390. * @clkdm: struct clockdomain *
  391. *
  392. * Prevent the hardware from automatically switching the clockdomain
  393. * into inactive or idle states. If the clockdomain has downstream
  394. * clocks enabled in the clock framework, wkdep/sleepdep
  395. * autodependencies are removed. No return value.
  396. */
  397. void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
  398. {
  399. u32 v;
  400. if (!clkdm)
  401. return;
  402. if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
  403. pr_debug("clockdomain: automatic idle transitions cannot be "
  404. "disabled on %s\n", clkdm->name);
  405. return;
  406. }
  407. pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
  408. clkdm->name);
  409. if (cpu_is_omap24xx())
  410. v = OMAP24XX_CLKSTCTRL_DISABLE_AUTO;
  411. else if (cpu_is_omap34xx())
  412. v = OMAP34XX_CLKSTCTRL_DISABLE_AUTO;
  413. else
  414. BUG();
  415. cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask,
  416. v << __ffs(clkdm->clktrctrl_mask),
  417. clkdm->pwrdm->prcm_offs, CM_CLKSTCTRL);
  418. if (atomic_read(&clkdm->usecount) > 0)
  419. _clkdm_del_autodeps(clkdm);
  420. }
  421. /* Clockdomain-to-clock framework interface code */
  422. /**
  423. * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
  424. * @clkdm: struct clockdomain *
  425. * @clk: struct clk * of the enabled downstream clock
  426. *
  427. * Increment the usecount of this clockdomain 'clkdm' and ensure that
  428. * it is awake. Intended to be called by clk_enable() code. If the
  429. * clockdomain is in software-supervised idle mode, force the
  430. * clockdomain to wake. If the clockdomain is in hardware-supervised
  431. * idle mode, add clkdm-pwrdm autodependencies, to ensure that devices
  432. * in the clockdomain can be read from/written to by on-chip processors.
  433. * Returns -EINVAL if passed null pointers; returns 0 upon success or
  434. * if the clockdomain is in hwsup idle mode.
  435. */
  436. int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
  437. {
  438. int v;
  439. /*
  440. * XXX Rewrite this code to maintain a list of enabled
  441. * downstream clocks for debugging purposes?
  442. */
  443. if (!clkdm || !clk)
  444. return -EINVAL;
  445. if (atomic_inc_return(&clkdm->usecount) > 1)
  446. return 0;
  447. /* Clockdomain now has one enabled downstream clock */
  448. pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
  449. clk->name);
  450. v = omap2_clkdm_clktrctrl_read(clkdm);
  451. if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
  452. (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO))
  453. _clkdm_add_autodeps(clkdm);
  454. else
  455. omap2_clkdm_wakeup(clkdm);
  456. return 0;
  457. }
  458. /**
  459. * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
  460. * @clkdm: struct clockdomain *
  461. * @clk: struct clk * of the disabled downstream clock
  462. *
  463. * Decrement the usecount of this clockdomain 'clkdm'. Intended to be
  464. * called by clk_disable() code. If the usecount goes to 0, put the
  465. * clockdomain to sleep (software-supervised mode) or remove the
  466. * clkdm-pwrdm autodependencies (hardware-supervised mode). Returns
  467. * -EINVAL if passed null pointers; -ERANGE if the clkdm usecount
  468. * underflows and debugging is enabled; or returns 0 upon success or
  469. * if the clockdomain is in hwsup idle mode.
  470. */
  471. int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
  472. {
  473. int v;
  474. /*
  475. * XXX Rewrite this code to maintain a list of enabled
  476. * downstream clocks for debugging purposes?
  477. */
  478. if (!clkdm || !clk)
  479. return -EINVAL;
  480. #ifdef DEBUG
  481. if (atomic_read(&clkdm->usecount) == 0) {
  482. WARN_ON(1); /* underflow */
  483. return -ERANGE;
  484. }
  485. #endif
  486. if (atomic_dec_return(&clkdm->usecount) > 0)
  487. return 0;
  488. /* All downstream clocks of this clockdomain are now disabled */
  489. pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
  490. clk->name);
  491. v = omap2_clkdm_clktrctrl_read(clkdm);
  492. if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
  493. (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO))
  494. _clkdm_del_autodeps(clkdm);
  495. else
  496. omap2_clkdm_sleep(clkdm);
  497. return 0;
  498. }