clkt_clksel.c 15 KB

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