clkt_clksel.c 15 KB

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