clockdomain.c 16 KB

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