clkt_clksel.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * clkt_clksel.c - OMAP2/3/4 clksel clock functions
  3. *
  4. * Copyright (C) 2005-2008 Texas Instruments, Inc.
  5. * Copyright (C) 2004-2010 Nokia Corporation
  6. *
  7. * Contacts:
  8. * Richard Woodruff <r-woodruff2@ti.com>
  9. * Paul Walmsley
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * XXX At some point these clksel clocks should be split into
  16. * "divider" clocks and "mux" clocks to better match the hardware.
  17. *
  18. * XXX Currently these clocks are only used in the OMAP2/3/4 code, but
  19. * many of the OMAP1 clocks should be convertible to use this
  20. * mechanism.
  21. */
  22. #undef DEBUG
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/clk.h>
  26. #include <linux/io.h>
  27. #include <plat/clock.h>
  28. #include "clock.h"
  29. #include "cm.h"
  30. #include "cm-regbits-24xx.h"
  31. #include "cm-regbits-34xx.h"
  32. /* Private functions */
  33. /**
  34. * _omap2_get_clksel_by_parent - return clksel struct for a given clk & parent
  35. * @clk: OMAP struct clk ptr to inspect
  36. * @src_clk: OMAP struct clk ptr of the parent clk to search for
  37. *
  38. * Scan the struct clksel array associated with the clock to find
  39. * the element associated with the supplied parent clock address.
  40. * Returns a pointer to the struct clksel on success or NULL on error.
  41. */
  42. static const struct clksel *_omap2_get_clksel_by_parent(struct clk *clk,
  43. struct clk *src_clk)
  44. {
  45. const struct clksel *clks;
  46. if (!clk->clksel)
  47. return NULL;
  48. for (clks = clk->clksel; clks->parent; clks++) {
  49. if (clks->parent == src_clk)
  50. break; /* Found the requested parent */
  51. }
  52. if (!clks->parent) {
  53. printk(KERN_ERR "clock: Could not find parent clock %s in "
  54. "clksel array of clock %s\n", src_clk->name,
  55. clk->name);
  56. return NULL;
  57. }
  58. return clks;
  59. }
  60. /*
  61. * Converts encoded control register address into a full address
  62. * On error, the return value (parent_div) will be 0.
  63. */
  64. static u32 _omap2_clksel_get_src_field(struct clk *src_clk, struct clk *clk,
  65. u32 *field_val)
  66. {
  67. const struct clksel *clks;
  68. const struct clksel_rate *clkr;
  69. clks = _omap2_get_clksel_by_parent(clk, src_clk);
  70. if (!clks)
  71. return 0;
  72. for (clkr = clks->rates; clkr->div; clkr++) {
  73. if (clkr->flags & cpu_mask && clkr->flags & DEFAULT_RATE)
  74. break; /* Found the default rate for this platform */
  75. }
  76. if (!clkr->div) {
  77. printk(KERN_ERR "clock: Could not find default rate for "
  78. "clock %s parent %s\n", clk->name,
  79. src_clk->parent->name);
  80. return 0;
  81. }
  82. /* Should never happen. Add a clksel mask to the struct clk. */
  83. WARN_ON(clk->clksel_mask == 0);
  84. *field_val = clkr->val;
  85. return clkr->div;
  86. }
  87. /* Public functions */
  88. /**
  89. * omap2_init_clksel_parent - set a clksel clk's parent field from the hardware
  90. * @clk: OMAP clock struct ptr to use
  91. *
  92. * Given a pointer to a source-selectable struct clk, read the hardware
  93. * register and determine what its parent is currently set to. Update the
  94. * clk->parent field with the appropriate clk ptr.
  95. */
  96. void omap2_init_clksel_parent(struct clk *clk)
  97. {
  98. const struct clksel *clks;
  99. const struct clksel_rate *clkr;
  100. u32 r, found = 0;
  101. if (!clk->clksel)
  102. return;
  103. r = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
  104. r >>= __ffs(clk->clksel_mask);
  105. for (clks = clk->clksel; clks->parent && !found; clks++) {
  106. for (clkr = clks->rates; clkr->div && !found; clkr++) {
  107. if ((clkr->flags & cpu_mask) && (clkr->val == r)) {
  108. if (clk->parent != clks->parent) {
  109. pr_debug("clock: inited %s parent "
  110. "to %s (was %s)\n",
  111. clk->name, clks->parent->name,
  112. ((clk->parent) ?
  113. clk->parent->name : "NULL"));
  114. clk_reparent(clk, clks->parent);
  115. };
  116. found = 1;
  117. }
  118. }
  119. }
  120. if (!found)
  121. printk(KERN_ERR "clock: init parent: could not find "
  122. "regval %0x for clock %s\n", r, clk->name);
  123. return;
  124. }
  125. /*
  126. * Used for clocks that are part of CLKSEL_xyz governed clocks.
  127. * REVISIT: Maybe change to use clk->enable() functions like on omap1?
  128. */
  129. unsigned long omap2_clksel_recalc(struct clk *clk)
  130. {
  131. unsigned long rate;
  132. u32 div = 0;
  133. pr_debug("clock: recalc'ing clksel clk %s\n", clk->name);
  134. div = omap2_clksel_get_divisor(clk);
  135. if (div == 0)
  136. return clk->rate;
  137. rate = clk->parent->rate / div;
  138. pr_debug("clock: new clock rate is %ld (div %d)\n", rate, div);
  139. return rate;
  140. }
  141. /**
  142. * omap2_clksel_round_rate_div - find divisor for the given clock and rate
  143. * @clk: OMAP struct clk to use
  144. * @target_rate: desired clock rate
  145. * @new_div: ptr to where we should store the divisor
  146. *
  147. * Finds 'best' divider value in an array based on the source and target
  148. * rates. The divider array must be sorted with smallest divider first.
  149. * Note that this will not work for clocks which are part of CONFIG_PARTICIPANT,
  150. * they are only settable as part of virtual_prcm set.
  151. *
  152. * Returns the rounded clock rate or returns 0xffffffff on error.
  153. */
  154. u32 omap2_clksel_round_rate_div(struct clk *clk, unsigned long target_rate,
  155. u32 *new_div)
  156. {
  157. unsigned long test_rate;
  158. const struct clksel *clks;
  159. const struct clksel_rate *clkr;
  160. u32 last_div = 0;
  161. pr_debug("clock: clksel_round_rate_div: %s target_rate %ld\n",
  162. clk->name, target_rate);
  163. *new_div = 1;
  164. clks = _omap2_get_clksel_by_parent(clk, clk->parent);
  165. if (!clks)
  166. return ~0;
  167. for (clkr = clks->rates; clkr->div; clkr++) {
  168. if (!(clkr->flags & cpu_mask))
  169. continue;
  170. /* Sanity check */
  171. if (clkr->div <= last_div)
  172. pr_err("clock: clksel_rate table not sorted "
  173. "for clock %s", clk->name);
  174. last_div = clkr->div;
  175. test_rate = clk->parent->rate / clkr->div;
  176. if (test_rate <= target_rate)
  177. break; /* found it */
  178. }
  179. if (!clkr->div) {
  180. pr_err("clock: Could not find divisor for target "
  181. "rate %ld for clock %s parent %s\n", target_rate,
  182. clk->name, clk->parent->name);
  183. return ~0;
  184. }
  185. *new_div = clkr->div;
  186. pr_debug("clock: new_div = %d, new_rate = %ld\n", *new_div,
  187. (clk->parent->rate / clkr->div));
  188. return clk->parent->rate / clkr->div;
  189. }
  190. /**
  191. * omap2_clksel_round_rate - find rounded rate for the given clock and rate
  192. * @clk: OMAP struct clk to use
  193. * @target_rate: desired clock rate
  194. *
  195. * Compatibility wrapper for OMAP clock framework
  196. * Finds best target rate based on the source clock and possible dividers.
  197. * rates. The divider array must be sorted with smallest divider first.
  198. * Note that this will not work for clocks which are part of CONFIG_PARTICIPANT,
  199. * they are only settable as part of virtual_prcm set.
  200. *
  201. * Returns the rounded clock rate or returns 0xffffffff on error.
  202. */
  203. long omap2_clksel_round_rate(struct clk *clk, unsigned long target_rate)
  204. {
  205. u32 new_div;
  206. return omap2_clksel_round_rate_div(clk, target_rate, &new_div);
  207. }
  208. /* Given a clock and a rate apply a clock specific rounding function */
  209. long omap2_clk_round_rate(struct clk *clk, unsigned long rate)
  210. {
  211. if (clk->round_rate)
  212. return clk->round_rate(clk, rate);
  213. return clk->rate;
  214. }
  215. /**
  216. * omap2_clksel_to_divisor() - turn clksel field value into integer divider
  217. * @clk: OMAP struct clk to use
  218. * @field_val: register field value to find
  219. *
  220. * Given a struct clk of a rate-selectable clksel clock, and a register field
  221. * value to search for, find the corresponding clock divisor. The register
  222. * field value should be pre-masked and shifted down so the LSB is at bit 0
  223. * before calling. Returns 0 on error
  224. */
  225. u32 omap2_clksel_to_divisor(struct clk *clk, u32 field_val)
  226. {
  227. const struct clksel *clks;
  228. const struct clksel_rate *clkr;
  229. clks = _omap2_get_clksel_by_parent(clk, clk->parent);
  230. if (!clks)
  231. return 0;
  232. for (clkr = clks->rates; clkr->div; clkr++) {
  233. if ((clkr->flags & cpu_mask) && (clkr->val == field_val))
  234. break;
  235. }
  236. if (!clkr->div) {
  237. printk(KERN_ERR "clock: Could not find fieldval %d for "
  238. "clock %s parent %s\n", field_val, clk->name,
  239. clk->parent->name);
  240. return 0;
  241. }
  242. return clkr->div;
  243. }
  244. /**
  245. * omap2_divisor_to_clksel() - turn clksel integer divisor into a field value
  246. * @clk: OMAP struct clk to use
  247. * @div: integer divisor to search for
  248. *
  249. * Given a struct clk of a rate-selectable clksel clock, and a clock divisor,
  250. * find the corresponding register field value. The return register value is
  251. * the value before left-shifting. Returns ~0 on error
  252. */
  253. u32 omap2_divisor_to_clksel(struct clk *clk, u32 div)
  254. {
  255. const struct clksel *clks;
  256. const struct clksel_rate *clkr;
  257. /* should never happen */
  258. WARN_ON(div == 0);
  259. clks = _omap2_get_clksel_by_parent(clk, clk->parent);
  260. if (!clks)
  261. return ~0;
  262. for (clkr = clks->rates; clkr->div; clkr++) {
  263. if ((clkr->flags & cpu_mask) && (clkr->div == div))
  264. break;
  265. }
  266. if (!clkr->div) {
  267. printk(KERN_ERR "clock: Could not find divisor %d for "
  268. "clock %s parent %s\n", div, clk->name,
  269. clk->parent->name);
  270. return ~0;
  271. }
  272. return clkr->val;
  273. }
  274. /**
  275. * omap2_clksel_get_divisor - get current divider applied to parent clock.
  276. * @clk: OMAP struct clk to use.
  277. *
  278. * Returns the integer divisor upon success or 0 on error.
  279. */
  280. u32 omap2_clksel_get_divisor(struct clk *clk)
  281. {
  282. u32 v;
  283. if (!clk->clksel_mask)
  284. return 0;
  285. v = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
  286. v >>= __ffs(clk->clksel_mask);
  287. return omap2_clksel_to_divisor(clk, v);
  288. }
  289. int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)
  290. {
  291. u32 v, field_val, validrate, new_div = 0;
  292. if (!clk->clksel_mask)
  293. return -EINVAL;
  294. validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
  295. if (validrate != rate)
  296. return -EINVAL;
  297. field_val = omap2_divisor_to_clksel(clk, new_div);
  298. if (field_val == ~0)
  299. return -EINVAL;
  300. v = __raw_readl(clk->clksel_reg);
  301. v &= ~clk->clksel_mask;
  302. v |= field_val << __ffs(clk->clksel_mask);
  303. __raw_writel(v, clk->clksel_reg);
  304. v = __raw_readl(clk->clksel_reg); /* OCP barrier */
  305. clk->rate = clk->parent->rate / new_div;
  306. return 0;
  307. }
  308. int omap2_clksel_set_parent(struct clk *clk, struct clk *new_parent)
  309. {
  310. u32 field_val, v, parent_div;
  311. if (!clk->clksel)
  312. return -EINVAL;
  313. parent_div = _omap2_clksel_get_src_field(new_parent, clk, &field_val);
  314. if (!parent_div)
  315. return -EINVAL;
  316. /* Set new source value (previous dividers if any in effect) */
  317. v = __raw_readl(clk->clksel_reg);
  318. v &= ~clk->clksel_mask;
  319. v |= field_val << __ffs(clk->clksel_mask);
  320. __raw_writel(v, clk->clksel_reg);
  321. v = __raw_readl(clk->clksel_reg); /* OCP barrier */
  322. clk_reparent(clk, new_parent);
  323. /* CLKSEL clocks follow their parents' rates, divided by a divisor */
  324. clk->rate = new_parent->rate;
  325. if (parent_div > 0)
  326. clk->rate /= parent_div;
  327. pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
  328. clk->name, clk->parent->name, clk->rate);
  329. return 0;
  330. }