clkt_clksel.c 15 KB

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