clockdomain.c 16 KB

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