PageRenderTime 197ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 1ms

/arch/arm/mach-omap2/clkt_clksel.c

https://bitbucket.org/sammyz/iscream_thunderc-2.6.35-rebase
C | 509 lines | 224 code | 83 blank | 202 comment | 56 complexity | 09176e4bc8c12f90d5e06174c12f7157 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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. *
  16. * clksel clocks are clocks that do not have a fixed parent, or that
  17. * can divide their parent's rate, or possibly both at the same time, based
  18. * on the contents of a hardware register bitfield.
  19. *
  20. * All of the various mux and divider settings can be encoded into
  21. * struct clksel* data structures, and then these can be autogenerated
  22. * from some hardware database for each new chip generation. This
  23. * should avoid the need to write, review, and validate a lot of new
  24. * clock code for each new chip, since it can be exported from the SoC
  25. * design flow. This is now done on OMAP4.
  26. *
  27. * The fusion of mux and divider clocks is a software creation. In
  28. * hardware reality, the multiplexer (parent selection) and the
  29. * divider exist separately. XXX At some point these clksel clocks
  30. * should be split into "divider" clocks and "mux" clocks to better
  31. * match the hardware.
  32. *
  33. * (The name "clksel" comes from the name of the corresponding
  34. * register field in the OMAP2/3 family of SoCs.)
  35. *
  36. * XXX Currently these clocks are only used in the OMAP2/3/4 code, but
  37. * many of the OMAP1 clocks should be convertible to use this
  38. * mechanism.
  39. */
  40. #undef DEBUG
  41. #include <linux/kernel.h>
  42. #include <linux/errno.h>
  43. #include <linux/clk.h>
  44. #include <linux/io.h>
  45. #include <plat/clock.h>
  46. #include "clock.h"
  47. /* Private functions */
  48. /**
  49. * _get_clksel_by_parent() - return clksel struct for a given clk & parent
  50. * @clk: OMAP struct clk ptr to inspect
  51. * @src_clk: OMAP struct clk ptr of the parent clk to search for
  52. *
  53. * Scan the struct clksel array associated with the clock to find
  54. * the element associated with the supplied parent clock address.
  55. * Returns a pointer to the struct clksel on success or NULL on error.
  56. */
  57. static const struct clksel *_get_clksel_by_parent(struct clk *clk,
  58. struct clk *src_clk)
  59. {
  60. const struct clksel *clks;
  61. for (clks = clk->clksel; clks->parent; clks++)
  62. if (clks->parent == src_clk)
  63. break; /* Found the requested parent */
  64. if (!clks->parent) {
  65. /* This indicates a data problem */
  66. WARN(1, "clock: Could not find parent clock %s in clksel array "
  67. "of clock %s\n", src_clk->name, clk->name);
  68. return NULL;
  69. }
  70. return clks;
  71. }
  72. /**
  73. * _get_div_and_fieldval() - find the new clksel divisor and field value to use
  74. * @src_clk: planned new parent struct clk *
  75. * @clk: struct clk * that is being reparented
  76. * @field_val: pointer to a u32 to contain the register data for the divisor
  77. *
  78. * Given an intended new parent struct clk * @src_clk, and the struct
  79. * clk * @clk to the clock that is being reparented, find the
  80. * appropriate rate divisor for the new clock (returned as the return
  81. * value), and the corresponding register bitfield data to program to
  82. * reach that divisor (returned in the u32 pointed to by @field_val).
  83. * Returns 0 on error, or returns the newly-selected divisor upon
  84. * success (in this latter case, the corresponding register bitfield
  85. * value is passed back in the variable pointed to by @field_val)
  86. */
  87. static u8 _get_div_and_fieldval(struct clk *src_clk, struct clk *clk,
  88. u32 *field_val)
  89. {
  90. const struct clksel *clks;
  91. const struct clksel_rate *clkr, *max_clkr;
  92. u8 max_div = 0;
  93. clks = _get_clksel_by_parent(clk, src_clk);
  94. if (!clks)
  95. return 0;
  96. /*
  97. * Find the highest divisor (e.g., the one resulting in the
  98. * lowest rate) to use as the default. This should avoid
  99. * clock rates that are too high for the device. XXX A better
  100. * solution here would be to try to determine if there is a
  101. * divisor matching the original clock rate before the parent
  102. * switch, and if it cannot be found, to fall back to the
  103. * highest divisor.
  104. */
  105. for (clkr = clks->rates; clkr->div; clkr++) {
  106. if (!(clkr->flags & cpu_mask))
  107. continue;
  108. if (clkr->div > max_div) {
  109. max_div = clkr->div;
  110. max_clkr = clkr;
  111. }
  112. }
  113. if (max_div == 0) {
  114. /* This indicates an error in the clksel data */
  115. WARN(1, "clock: Could not find divisor for clock %s parent %s"
  116. "\n", clk->name, src_clk->parent->name);
  117. return 0;
  118. }
  119. *field_val = max_clkr->val;
  120. return max_div;
  121. }
  122. /**
  123. * _write_clksel_reg() - program a clock's clksel register in hardware
  124. * @clk: struct clk * to program
  125. * @v: clksel bitfield value to program (with LSB at bit 0)
  126. *
  127. * Shift the clksel register bitfield value @v to its appropriate
  128. * location in the clksel register and write it in. This function
  129. * will ensure that the write to the clksel_reg reaches its
  130. * destination before returning -- important since PRM and CM register
  131. * accesses can be quite slow compared to ARM cycles -- but does not
  132. * take into account any time the hardware might take to switch the
  133. * clock source.
  134. */
  135. static void _write_clksel_reg(struct clk *clk, u32 field_val)
  136. {
  137. u32 v;
  138. v = __raw_readl(clk->clksel_reg);
  139. v &= ~clk->clksel_mask;
  140. v |= field_val << __ffs(clk->clksel_mask);
  141. __raw_writel(v, clk->clksel_reg);
  142. v = __raw_readl(clk->clksel_reg); /* OCP barrier */
  143. }
  144. /**
  145. * _clksel_to_divisor() - turn clksel field value into integer divider
  146. * @clk: OMAP struct clk to use
  147. * @field_val: register field value to find
  148. *
  149. * Given a struct clk of a rate-selectable clksel clock, and a register field
  150. * value to search for, find the corresponding clock divisor. The register
  151. * field value should be pre-masked and shifted down so the LSB is at bit 0
  152. * before calling. Returns 0 on error or returns the actual integer divisor
  153. * upon success.
  154. */
  155. static u32 _clksel_to_divisor(struct clk *clk, u32 field_val)
  156. {
  157. const struct clksel *clks;
  158. const struct clksel_rate *clkr;
  159. clks = _get_clksel_by_parent(clk, clk->parent);
  160. if (!clks)
  161. return 0;
  162. for (clkr = clks->rates; clkr->div; clkr++) {
  163. if (!(clkr->flags & cpu_mask))
  164. continue;
  165. if (clkr->val == field_val)
  166. break;
  167. }
  168. if (!clkr->div) {
  169. /* This indicates a data error */
  170. WARN(1, "clock: Could not find fieldval %d for clock %s parent "
  171. "%s\n", field_val, clk->name, clk->parent->name);
  172. return 0;
  173. }
  174. return clkr->div;
  175. }
  176. /**
  177. * _divisor_to_clksel() - turn clksel integer divisor into a field value
  178. * @clk: OMAP struct clk to use
  179. * @div: integer divisor to search for
  180. *
  181. * Given a struct clk of a rate-selectable clksel clock, and a clock
  182. * divisor, find the corresponding register field value. Returns the
  183. * register field value _before_ left-shifting (i.e., LSB is at bit
  184. * 0); or returns 0xFFFFFFFF (~0) upon error.
  185. */
  186. static u32 _divisor_to_clksel(struct clk *clk, u32 div)
  187. {
  188. const struct clksel *clks;
  189. const struct clksel_rate *clkr;
  190. /* should never happen */
  191. WARN_ON(div == 0);
  192. clks = _get_clksel_by_parent(clk, clk->parent);
  193. if (!clks)
  194. return ~0;
  195. for (clkr = clks->rates; clkr->div; clkr++) {
  196. if (!(clkr->flags & cpu_mask))
  197. continue;
  198. if (clkr->div == div)
  199. break;
  200. }
  201. if (!clkr->div) {
  202. pr_err("clock: Could not find divisor %d for clock %s parent "
  203. "%s\n", div, clk->name, clk->parent->name);
  204. return ~0;
  205. }
  206. return clkr->val;
  207. }
  208. /**
  209. * _read_divisor() - get current divisor applied to parent clock (from hdwr)
  210. * @clk: OMAP struct clk to use.
  211. *
  212. * Read the current divisor register value for @clk that is programmed
  213. * into the hardware, convert it into the actual divisor value, and
  214. * return it; or return 0 on error.
  215. */
  216. static u32 _read_divisor(struct clk *clk)
  217. {
  218. u32 v;
  219. if (!clk->clksel || !clk->clksel_mask)
  220. return 0;
  221. v = __raw_readl(clk->clksel_reg);
  222. v &= clk->clksel_mask;
  223. v >>= __ffs(clk->clksel_mask);
  224. return _clksel_to_divisor(clk, v);
  225. }
  226. /* Public functions */
  227. /**
  228. * omap2_clksel_round_rate_div() - find divisor for the given clock and rate
  229. * @clk: OMAP struct clk to use
  230. * @target_rate: desired clock rate
  231. * @new_div: ptr to where we should store the divisor
  232. *
  233. * Finds 'best' divider value in an array based on the source and target
  234. * rates. The divider array must be sorted with smallest divider first.
  235. * This function is also used by the DPLL3 M2 divider code.
  236. *
  237. * Returns the rounded clock rate or returns 0xffffffff on error.
  238. */
  239. u32 omap2_clksel_round_rate_div(struct clk *clk, unsigned long target_rate,
  240. u32 *new_div)
  241. {
  242. unsigned long test_rate;
  243. const struct clksel *clks;
  244. const struct clksel_rate *clkr;
  245. u32 last_div = 0;
  246. if (!clk->clksel || !clk->clksel_mask)
  247. return ~0;
  248. pr_debug("clock: clksel_round_rate_div: %s target_rate %ld\n",
  249. clk->name, target_rate);
  250. *new_div = 1;
  251. clks = _get_clksel_by_parent(clk, clk->parent);
  252. if (!clks)
  253. return ~0;
  254. for (clkr = clks->rates; clkr->div; clkr++) {
  255. if (!(clkr->flags & cpu_mask))
  256. continue;
  257. /* Sanity check */
  258. if (clkr->div <= last_div)
  259. pr_err("clock: clksel_rate table not sorted "
  260. "for clock %s", clk->name);
  261. last_div = clkr->div;
  262. test_rate = clk->parent->rate / clkr->div;
  263. if (test_rate <= target_rate)
  264. break; /* found it */
  265. }
  266. if (!clkr->div) {
  267. pr_err("clock: Could not find divisor for target "
  268. "rate %ld for clock %s parent %s\n", target_rate,
  269. clk->name, clk->parent->name);
  270. return ~0;
  271. }
  272. *new_div = clkr->div;
  273. pr_debug("clock: new_div = %d, new_rate = %ld\n", *new_div,
  274. (clk->parent->rate / clkr->div));
  275. return clk->parent->rate / clkr->div;
  276. }
  277. /*
  278. * Clocktype interface functions to the OMAP clock code
  279. * (i.e., those used in struct clk field function pointers, etc.)
  280. */
  281. /**
  282. * omap2_init_clksel_parent() - set a clksel clk's parent field from the hdwr
  283. * @clk: OMAP clock struct ptr to use
  284. *
  285. * Given a pointer @clk to a source-selectable struct clk, read the
  286. * hardware register and determine what its parent is currently set
  287. * to. Update @clk's .parent field with the appropriate clk ptr. No
  288. * return value.
  289. */
  290. void omap2_init_clksel_parent(struct clk *clk)
  291. {
  292. const struct clksel *clks;
  293. const struct clksel_rate *clkr;
  294. u32 r, found = 0;
  295. if (!clk->clksel || !clk->clksel_mask)
  296. return;
  297. r = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
  298. r >>= __ffs(clk->clksel_mask);
  299. for (clks = clk->clksel; clks->parent && !found; clks++) {
  300. for (clkr = clks->rates; clkr->div && !found; clkr++) {
  301. if (!(clkr->flags & cpu_mask))
  302. continue;
  303. if (clkr->val == r) {
  304. if (clk->parent != clks->parent) {
  305. pr_debug("clock: inited %s parent "
  306. "to %s (was %s)\n",
  307. clk->name, clks->parent->name,
  308. ((clk->parent) ?
  309. clk->parent->name : "NULL"));
  310. clk_reparent(clk, clks->parent);
  311. };
  312. found = 1;
  313. }
  314. }
  315. }
  316. /* This indicates a data error */
  317. WARN(!found, "clock: %s: init parent: could not find regval %0x\n",
  318. clk->name, r);
  319. return;
  320. }
  321. /**
  322. * omap2_clksel_recalc() - function ptr to pass via struct clk .recalc field
  323. * @clk: struct clk *
  324. *
  325. * This function is intended to be called only by the clock framework.
  326. * Each clksel clock should have its struct clk .recalc field set to this
  327. * function. Returns the clock's current rate, based on its parent's rate
  328. * and its current divisor setting in the hardware.
  329. */
  330. unsigned long omap2_clksel_recalc(struct clk *clk)
  331. {
  332. unsigned long rate;
  333. u32 div = 0;
  334. div = _read_divisor(clk);
  335. if (div == 0)
  336. return clk->rate;
  337. rate = clk->parent->rate / div;
  338. pr_debug("clock: %s: recalc'd rate is %ld (div %d)\n", clk->name,
  339. rate, div);
  340. return rate;
  341. }
  342. /**
  343. * omap2_clksel_round_rate() - find rounded rate for the given clock and rate
  344. * @clk: OMAP struct clk to use
  345. * @target_rate: desired clock rate
  346. *
  347. * This function is intended to be called only by the clock framework.
  348. * Finds best target rate based on the source clock and possible dividers.
  349. * rates. The divider array must be sorted with smallest divider first.
  350. *
  351. * Returns the rounded clock rate or returns 0xffffffff on error.
  352. */
  353. long omap2_clksel_round_rate(struct clk *clk, unsigned long target_rate)
  354. {
  355. u32 new_div;
  356. return omap2_clksel_round_rate_div(clk, target_rate, &new_div);
  357. }
  358. /**
  359. * omap2_clksel_set_rate() - program clock rate in hardware
  360. * @clk: struct clk * to program rate
  361. * @rate: target rate to program
  362. *
  363. * This function is intended to be called only by the clock framework.
  364. * Program @clk's rate to @rate in the hardware. The clock can be
  365. * either enabled or disabled when this happens, although if the clock
  366. * is enabled, some downstream devices may glitch or behave
  367. * unpredictably when the clock rate is changed - this depends on the
  368. * hardware. This function does not currently check the usecount of
  369. * the clock, so if multiple drivers are using the clock, and the rate
  370. * is changed, they will all be affected without any notification.
  371. * Returns -EINVAL upon error, or 0 upon success.
  372. */
  373. int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)
  374. {
  375. u32 field_val, validrate, new_div = 0;
  376. if (!clk->clksel || !clk->clksel_mask)
  377. return -EINVAL;
  378. validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
  379. if (validrate != rate)
  380. return -EINVAL;
  381. field_val = _divisor_to_clksel(clk, new_div);
  382. if (field_val == ~0)
  383. return -EINVAL;
  384. _write_clksel_reg(clk, field_val);
  385. clk->rate = clk->parent->rate / new_div;
  386. pr_debug("clock: %s: set rate to %ld\n", clk->name, clk->rate);
  387. return 0;
  388. }
  389. /*
  390. * Clksel parent setting function - not passed in struct clk function
  391. * pointer - instead, the OMAP clock code currently assumes that any
  392. * parent-setting clock is a clksel clock, and calls
  393. * omap2_clksel_set_parent() by default
  394. */
  395. /**
  396. * omap2_clksel_set_parent() - change a clock's parent clock
  397. * @clk: struct clk * of the child clock
  398. * @new_parent: struct clk * of the new parent clock
  399. *
  400. * This function is intended to be called only by the clock framework.
  401. * Change the parent clock of clock @clk to @new_parent. This is
  402. * intended to be used while @clk is disabled. This function does not
  403. * currently check the usecount of the clock, so if multiple drivers
  404. * are using the clock, and the parent is changed, they will all be
  405. * affected without any notification. Returns -EINVAL upon error, or
  406. * 0 upon success.
  407. */
  408. int omap2_clksel_set_parent(struct clk *clk, struct clk *new_parent)
  409. {
  410. u32 field_val = 0;
  411. u32 parent_div;
  412. if (!clk->clksel || !clk->clksel_mask)
  413. return -EINVAL;
  414. parent_div = _get_div_and_fieldval(new_parent, clk, &field_val);
  415. if (!parent_div)
  416. return -EINVAL;
  417. _write_clksel_reg(clk, field_val);
  418. clk_reparent(clk, new_parent);
  419. /* CLKSEL clocks follow their parents' rates, divided by a divisor */
  420. clk->rate = new_parent->rate;
  421. if (parent_div > 0)
  422. clk->rate /= parent_div;
  423. pr_debug("clock: %s: set parent to %s (new rate %ld)\n",
  424. clk->name, clk->parent->name, clk->rate);
  425. return 0;
  426. }