clkt_clksel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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-provider.h>
  44. #include <linux/io.h>
  45. #include <linux/bug.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_hw_omap *clk,
  58. struct clk *src_clk)
  59. {
  60. const struct clksel *clks;
  61. if (!src_clk)
  62. return NULL;
  63. for (clks = clk->clksel; clks->parent; clks++)
  64. if (clks->parent == src_clk)
  65. break; /* Found the requested parent */
  66. if (!clks->parent) {
  67. /* This indicates a data problem */
  68. WARN(1, "clock: %s: could not find parent clock %s in clksel array\n",
  69. __clk_get_name(clk->hw.clk), __clk_get_name(src_clk));
  70. return NULL;
  71. }
  72. return clks;
  73. }
  74. /**
  75. * _write_clksel_reg() - program a clock's clksel register in hardware
  76. * @clk: struct clk * to program
  77. * @v: clksel bitfield value to program (with LSB at bit 0)
  78. *
  79. * Shift the clksel register bitfield value @v to its appropriate
  80. * location in the clksel register and write it in. This function
  81. * will ensure that the write to the clksel_reg reaches its
  82. * destination before returning -- important since PRM and CM register
  83. * accesses can be quite slow compared to ARM cycles -- but does not
  84. * take into account any time the hardware might take to switch the
  85. * clock source.
  86. */
  87. static void _write_clksel_reg(struct clk_hw_omap *clk, u32 field_val)
  88. {
  89. u32 v;
  90. v = __raw_readl(clk->clksel_reg);
  91. v &= ~clk->clksel_mask;
  92. v |= field_val << __ffs(clk->clksel_mask);
  93. __raw_writel(v, clk->clksel_reg);
  94. v = __raw_readl(clk->clksel_reg); /* OCP barrier */
  95. }
  96. /**
  97. * _clksel_to_divisor() - turn clksel field value into integer divider
  98. * @clk: OMAP struct clk to use
  99. * @field_val: register field value to find
  100. *
  101. * Given a struct clk of a rate-selectable clksel clock, and a register field
  102. * value to search for, find the corresponding clock divisor. The register
  103. * field value should be pre-masked and shifted down so the LSB is at bit 0
  104. * before calling. Returns 0 on error or returns the actual integer divisor
  105. * upon success.
  106. */
  107. static u32 _clksel_to_divisor(struct clk_hw_omap *clk, u32 field_val)
  108. {
  109. const struct clksel *clks;
  110. const struct clksel_rate *clkr;
  111. struct clk *parent;
  112. parent = __clk_get_parent(clk->hw.clk);
  113. clks = _get_clksel_by_parent(clk, parent);
  114. if (!clks)
  115. return 0;
  116. for (clkr = clks->rates; clkr->div; clkr++) {
  117. if (!(clkr->flags & cpu_mask))
  118. continue;
  119. if (clkr->val == field_val)
  120. break;
  121. }
  122. if (!clkr->div) {
  123. /* This indicates a data error */
  124. WARN(1, "clock: %s: could not find fieldval %d for parent %s\n",
  125. __clk_get_name(clk->hw.clk), field_val,
  126. __clk_get_name(parent));
  127. return 0;
  128. }
  129. return clkr->div;
  130. }
  131. /**
  132. * _divisor_to_clksel() - turn clksel integer divisor into a field value
  133. * @clk: OMAP struct clk to use
  134. * @div: integer divisor to search for
  135. *
  136. * Given a struct clk of a rate-selectable clksel clock, and a clock
  137. * divisor, find the corresponding register field value. Returns the
  138. * register field value _before_ left-shifting (i.e., LSB is at bit
  139. * 0); or returns 0xFFFFFFFF (~0) upon error.
  140. */
  141. static u32 _divisor_to_clksel(struct clk_hw_omap *clk, u32 div)
  142. {
  143. const struct clksel *clks;
  144. const struct clksel_rate *clkr;
  145. struct clk *parent;
  146. /* should never happen */
  147. WARN_ON(div == 0);
  148. parent = __clk_get_parent(clk->hw.clk);
  149. clks = _get_clksel_by_parent(clk, parent);
  150. if (!clks)
  151. return ~0;
  152. for (clkr = clks->rates; clkr->div; clkr++) {
  153. if (!(clkr->flags & cpu_mask))
  154. continue;
  155. if (clkr->div == div)
  156. break;
  157. }
  158. if (!clkr->div) {
  159. pr_err("clock: %s: could not find divisor %d for parent %s\n",
  160. __clk_get_name(clk->hw.clk), div,
  161. __clk_get_name(parent));
  162. return ~0;
  163. }
  164. return clkr->val;
  165. }
  166. /**
  167. * _read_divisor() - get current divisor applied to parent clock (from hdwr)
  168. * @clk: OMAP struct clk to use.
  169. *
  170. * Read the current divisor register value for @clk that is programmed
  171. * into the hardware, convert it into the actual divisor value, and
  172. * return it; or return 0 on error.
  173. */
  174. static u32 _read_divisor(struct clk_hw_omap *clk)
  175. {
  176. u32 v;
  177. if (!clk->clksel || !clk->clksel_mask)
  178. return 0;
  179. v = __raw_readl(clk->clksel_reg);
  180. v &= clk->clksel_mask;
  181. v >>= __ffs(clk->clksel_mask);
  182. return _clksel_to_divisor(clk, v);
  183. }
  184. /* Public functions */
  185. /**
  186. * omap2_clksel_round_rate_div() - find divisor for the given clock and rate
  187. * @clk: OMAP struct clk to use
  188. * @target_rate: desired clock rate
  189. * @new_div: ptr to where we should store the divisor
  190. *
  191. * Finds 'best' divider value in an array based on the source and target
  192. * rates. The divider array must be sorted with smallest divider first.
  193. * This function is also used by the DPLL3 M2 divider code.
  194. *
  195. * Returns the rounded clock rate or returns 0xffffffff on error.
  196. */
  197. u32 omap2_clksel_round_rate_div(struct clk_hw_omap *clk,
  198. unsigned long target_rate,
  199. u32 *new_div)
  200. {
  201. unsigned long test_rate;
  202. const struct clksel *clks;
  203. const struct clksel_rate *clkr;
  204. u32 last_div = 0;
  205. struct clk *parent;
  206. unsigned long parent_rate;
  207. const char *clk_name;
  208. parent = __clk_get_parent(clk->hw.clk);
  209. clk_name = __clk_get_name(clk->hw.clk);
  210. parent_rate = __clk_get_rate(parent);
  211. if (!clk->clksel || !clk->clksel_mask)
  212. return ~0;
  213. pr_debug("clock: clksel_round_rate_div: %s target_rate %ld\n",
  214. clk_name, target_rate);
  215. *new_div = 1;
  216. clks = _get_clksel_by_parent(clk, parent);
  217. if (!clks)
  218. return ~0;
  219. for (clkr = clks->rates; clkr->div; clkr++) {
  220. if (!(clkr->flags & cpu_mask))
  221. continue;
  222. /* Sanity check */
  223. if (clkr->div <= last_div)
  224. pr_err("clock: %s: clksel_rate table not sorted\n",
  225. clk_name);
  226. last_div = clkr->div;
  227. test_rate = parent_rate / clkr->div;
  228. if (test_rate <= target_rate)
  229. break; /* found it */
  230. }
  231. if (!clkr->div) {
  232. pr_err("clock: %s: could not find divisor for target rate %ld for parent %s\n",
  233. clk_name, target_rate, __clk_get_name(parent));
  234. return ~0;
  235. }
  236. *new_div = clkr->div;
  237. pr_debug("clock: new_div = %d, new_rate = %ld\n", *new_div,
  238. (parent_rate / clkr->div));
  239. return parent_rate / clkr->div;
  240. }
  241. /*
  242. * Clocktype interface functions to the OMAP clock code
  243. * (i.e., those used in struct clk field function pointers, etc.)
  244. */
  245. /**
  246. * omap2_clksel_find_parent_index() - return the array index of the current
  247. * hardware parent of @hw
  248. * @hw: struct clk_hw * to find the current hardware parent of
  249. *
  250. * Given a struct clk_hw pointer @hw to the 'hw' member of a struct
  251. * clk_hw_omap record representing a source-selectable hardware clock,
  252. * read the hardware register and determine what its parent is
  253. * currently set to. Intended to be called only by the common clock
  254. * framework struct clk_hw_ops.get_parent function pointer. Return
  255. * the array index of this parent clock upon success -- there is no
  256. * way to return an error, so if we encounter an error, just WARN()
  257. * and pretend that we know that we're doing.
  258. */
  259. u8 omap2_clksel_find_parent_index(struct clk_hw *hw)
  260. {
  261. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  262. const struct clksel *clks;
  263. const struct clksel_rate *clkr;
  264. u32 r, found = 0;
  265. struct clk *parent;
  266. const char *clk_name;
  267. int ret = 0, f = 0;
  268. parent = __clk_get_parent(hw->clk);
  269. clk_name = __clk_get_name(hw->clk);
  270. /* XXX should be able to return an error */
  271. WARN((!clk->clksel || !clk->clksel_mask),
  272. "clock: %s: attempt to call on a non-clksel clock", clk_name);
  273. r = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
  274. r >>= __ffs(clk->clksel_mask);
  275. for (clks = clk->clksel; clks->parent && !found; clks++) {
  276. for (clkr = clks->rates; clkr->div && !found; clkr++) {
  277. if (!(clkr->flags & cpu_mask))
  278. continue;
  279. if (clkr->val == r) {
  280. found = 1;
  281. ret = f;
  282. }
  283. }
  284. f++;
  285. }
  286. /* This indicates a data error */
  287. WARN(!found, "clock: %s: init parent: could not find regval %0x\n",
  288. clk_name, r);
  289. return ret;
  290. }
  291. /**
  292. * omap2_clksel_recalc() - function ptr to pass via struct clk .recalc field
  293. * @clk: struct clk *
  294. *
  295. * This function is intended to be called only by the clock framework.
  296. * Each clksel clock should have its struct clk .recalc field set to this
  297. * function. Returns the clock's current rate, based on its parent's rate
  298. * and its current divisor setting in the hardware.
  299. */
  300. unsigned long omap2_clksel_recalc(struct clk_hw *hw, unsigned long parent_rate)
  301. {
  302. unsigned long rate;
  303. u32 div = 0;
  304. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  305. if (!parent_rate)
  306. return 0;
  307. div = _read_divisor(clk);
  308. if (!div)
  309. rate = parent_rate;
  310. else
  311. rate = parent_rate / div;
  312. pr_debug("%s: recalc'd %s's rate to %lu (div %d)\n", __func__,
  313. __clk_get_name(hw->clk), rate, div);
  314. return rate;
  315. }
  316. /**
  317. * omap2_clksel_round_rate() - find rounded rate for the given clock and rate
  318. * @clk: OMAP struct clk to use
  319. * @target_rate: desired clock rate
  320. *
  321. * This function is intended to be called only by the clock framework.
  322. * Finds best target rate based on the source clock and possible dividers.
  323. * rates. The divider array must be sorted with smallest divider first.
  324. *
  325. * Returns the rounded clock rate or returns 0xffffffff on error.
  326. */
  327. long omap2_clksel_round_rate(struct clk_hw *hw, unsigned long target_rate,
  328. unsigned long *parent_rate)
  329. {
  330. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  331. u32 new_div;
  332. return omap2_clksel_round_rate_div(clk, target_rate, &new_div);
  333. }
  334. /**
  335. * omap2_clksel_set_rate() - program clock rate in hardware
  336. * @clk: struct clk * to program rate
  337. * @rate: target rate to program
  338. *
  339. * This function is intended to be called only by the clock framework.
  340. * Program @clk's rate to @rate in the hardware. The clock can be
  341. * either enabled or disabled when this happens, although if the clock
  342. * is enabled, some downstream devices may glitch or behave
  343. * unpredictably when the clock rate is changed - this depends on the
  344. * hardware. This function does not currently check the usecount of
  345. * the clock, so if multiple drivers are using the clock, and the rate
  346. * is changed, they will all be affected without any notification.
  347. * Returns -EINVAL upon error, or 0 upon success.
  348. */
  349. int omap2_clksel_set_rate(struct clk_hw *hw, unsigned long rate,
  350. unsigned long parent_rate)
  351. {
  352. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  353. u32 field_val, validrate, new_div = 0;
  354. if (!clk->clksel || !clk->clksel_mask)
  355. return -EINVAL;
  356. validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
  357. if (validrate != rate)
  358. return -EINVAL;
  359. field_val = _divisor_to_clksel(clk, new_div);
  360. if (field_val == ~0)
  361. return -EINVAL;
  362. _write_clksel_reg(clk, field_val);
  363. pr_debug("clock: %s: set rate to %ld\n", __clk_get_name(hw->clk),
  364. __clk_get_rate(hw->clk));
  365. return 0;
  366. }
  367. /*
  368. * Clksel parent setting function - not passed in struct clk function
  369. * pointer - instead, the OMAP clock code currently assumes that any
  370. * parent-setting clock is a clksel clock, and calls
  371. * omap2_clksel_set_parent() by default
  372. */
  373. /**
  374. * omap2_clksel_set_parent() - change a clock's parent clock
  375. * @clk: struct clk * of the child clock
  376. * @new_parent: struct clk * of the new parent clock
  377. *
  378. * This function is intended to be called only by the clock framework.
  379. * Change the parent clock of clock @clk to @new_parent. This is
  380. * intended to be used while @clk is disabled. This function does not
  381. * currently check the usecount of the clock, so if multiple drivers
  382. * are using the clock, and the parent is changed, they will all be
  383. * affected without any notification. Returns -EINVAL upon error, or
  384. * 0 upon success.
  385. */
  386. int omap2_clksel_set_parent(struct clk_hw *hw, u8 field_val)
  387. {
  388. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  389. if (!clk->clksel || !clk->clksel_mask)
  390. return -EINVAL;
  391. _write_clksel_reg(clk, field_val);
  392. return 0;
  393. }