core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * SuperH clock framework
  3. *
  4. * Copyright (C) 2005 - 2010 Paul Mundt
  5. *
  6. * This clock framework is derived from the OMAP version by:
  7. *
  8. * Copyright (C) 2004 - 2008 Nokia Corporation
  9. * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
  10. *
  11. * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
  12. *
  13. * This file is subject to the terms and conditions of the GNU General Public
  14. * License. See the file "COPYING" in the main directory of this archive
  15. * for more details.
  16. */
  17. #define pr_fmt(fmt) "clock: " fmt
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/mutex.h>
  22. #include <linux/list.h>
  23. #include <linux/syscore_ops.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/err.h>
  26. #include <linux/io.h>
  27. #include <linux/cpufreq.h>
  28. #include <linux/clk.h>
  29. #include <linux/sh_clk.h>
  30. static LIST_HEAD(clock_list);
  31. static DEFINE_SPINLOCK(clock_lock);
  32. static DEFINE_MUTEX(clock_list_sem);
  33. /* clock disable operations are not passed on to hardware during boot */
  34. static int allow_disable;
  35. void clk_rate_table_build(struct clk *clk,
  36. struct cpufreq_frequency_table *freq_table,
  37. int nr_freqs,
  38. struct clk_div_mult_table *src_table,
  39. unsigned long *bitmap)
  40. {
  41. unsigned long mult, div;
  42. unsigned long freq;
  43. int i;
  44. clk->nr_freqs = nr_freqs;
  45. for (i = 0; i < nr_freqs; i++) {
  46. div = 1;
  47. mult = 1;
  48. if (src_table->divisors && i < src_table->nr_divisors)
  49. div = src_table->divisors[i];
  50. if (src_table->multipliers && i < src_table->nr_multipliers)
  51. mult = src_table->multipliers[i];
  52. if (!div || !mult || (bitmap && !test_bit(i, bitmap)))
  53. freq = CPUFREQ_ENTRY_INVALID;
  54. else
  55. freq = clk->parent->rate * mult / div;
  56. freq_table[i].index = i;
  57. freq_table[i].frequency = freq;
  58. }
  59. /* Termination entry */
  60. freq_table[i].index = i;
  61. freq_table[i].frequency = CPUFREQ_TABLE_END;
  62. }
  63. struct clk_rate_round_data;
  64. struct clk_rate_round_data {
  65. unsigned long rate;
  66. unsigned int min, max;
  67. long (*func)(unsigned int, struct clk_rate_round_data *);
  68. void *arg;
  69. };
  70. #define for_each_frequency(pos, r, freq) \
  71. for (pos = r->min, freq = r->func(pos, r); \
  72. pos <= r->max; pos++, freq = r->func(pos, r)) \
  73. if (unlikely(freq == 0)) \
  74. ; \
  75. else
  76. static long clk_rate_round_helper(struct clk_rate_round_data *rounder)
  77. {
  78. unsigned long rate_error, rate_error_prev = ~0UL;
  79. unsigned long highest, lowest, freq;
  80. long rate_best_fit = -ENOENT;
  81. int i;
  82. highest = 0;
  83. lowest = ~0UL;
  84. for_each_frequency(i, rounder, freq) {
  85. if (freq > highest)
  86. highest = freq;
  87. if (freq < lowest)
  88. lowest = freq;
  89. rate_error = abs(freq - rounder->rate);
  90. if (rate_error < rate_error_prev) {
  91. rate_best_fit = freq;
  92. rate_error_prev = rate_error;
  93. }
  94. if (rate_error == 0)
  95. break;
  96. }
  97. if (rounder->rate >= highest)
  98. rate_best_fit = highest;
  99. if (rounder->rate <= lowest)
  100. rate_best_fit = lowest;
  101. return rate_best_fit;
  102. }
  103. static long clk_rate_table_iter(unsigned int pos,
  104. struct clk_rate_round_data *rounder)
  105. {
  106. struct cpufreq_frequency_table *freq_table = rounder->arg;
  107. unsigned long freq = freq_table[pos].frequency;
  108. if (freq == CPUFREQ_ENTRY_INVALID)
  109. freq = 0;
  110. return freq;
  111. }
  112. long clk_rate_table_round(struct clk *clk,
  113. struct cpufreq_frequency_table *freq_table,
  114. unsigned long rate)
  115. {
  116. struct clk_rate_round_data table_round = {
  117. .min = 0,
  118. .max = clk->nr_freqs - 1,
  119. .func = clk_rate_table_iter,
  120. .arg = freq_table,
  121. .rate = rate,
  122. };
  123. if (clk->nr_freqs < 1)
  124. return -ENOSYS;
  125. return clk_rate_round_helper(&table_round);
  126. }
  127. static long clk_rate_div_range_iter(unsigned int pos,
  128. struct clk_rate_round_data *rounder)
  129. {
  130. return clk_get_rate(rounder->arg) / pos;
  131. }
  132. long clk_rate_div_range_round(struct clk *clk, unsigned int div_min,
  133. unsigned int div_max, unsigned long rate)
  134. {
  135. struct clk_rate_round_data div_range_round = {
  136. .min = div_min,
  137. .max = div_max,
  138. .func = clk_rate_div_range_iter,
  139. .arg = clk_get_parent(clk),
  140. .rate = rate,
  141. };
  142. return clk_rate_round_helper(&div_range_round);
  143. }
  144. static long clk_rate_mult_range_iter(unsigned int pos,
  145. struct clk_rate_round_data *rounder)
  146. {
  147. return clk_get_rate(rounder->arg) * pos;
  148. }
  149. long clk_rate_mult_range_round(struct clk *clk, unsigned int mult_min,
  150. unsigned int mult_max, unsigned long rate)
  151. {
  152. struct clk_rate_round_data mult_range_round = {
  153. .min = mult_min,
  154. .max = mult_max,
  155. .func = clk_rate_mult_range_iter,
  156. .arg = clk_get_parent(clk),
  157. .rate = rate,
  158. };
  159. return clk_rate_round_helper(&mult_range_round);
  160. }
  161. int clk_rate_table_find(struct clk *clk,
  162. struct cpufreq_frequency_table *freq_table,
  163. unsigned long rate)
  164. {
  165. int i;
  166. for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
  167. unsigned long freq = freq_table[i].frequency;
  168. if (freq == CPUFREQ_ENTRY_INVALID)
  169. continue;
  170. if (freq == rate)
  171. return i;
  172. }
  173. return -ENOENT;
  174. }
  175. /* Used for clocks that always have same value as the parent clock */
  176. unsigned long followparent_recalc(struct clk *clk)
  177. {
  178. return clk->parent ? clk->parent->rate : 0;
  179. }
  180. int clk_reparent(struct clk *child, struct clk *parent)
  181. {
  182. list_del_init(&child->sibling);
  183. if (parent)
  184. list_add(&child->sibling, &parent->children);
  185. child->parent = parent;
  186. return 0;
  187. }
  188. /* Propagate rate to children */
  189. void propagate_rate(struct clk *tclk)
  190. {
  191. struct clk *clkp;
  192. list_for_each_entry(clkp, &tclk->children, sibling) {
  193. if (clkp->ops && clkp->ops->recalc)
  194. clkp->rate = clkp->ops->recalc(clkp);
  195. propagate_rate(clkp);
  196. }
  197. }
  198. static void __clk_disable(struct clk *clk)
  199. {
  200. if (WARN(!clk->usecount, "Trying to disable clock %p with 0 usecount\n",
  201. clk))
  202. return;
  203. if (!(--clk->usecount)) {
  204. if (likely(allow_disable && clk->ops && clk->ops->disable))
  205. clk->ops->disable(clk);
  206. if (likely(clk->parent))
  207. __clk_disable(clk->parent);
  208. }
  209. }
  210. void clk_disable(struct clk *clk)
  211. {
  212. unsigned long flags;
  213. if (!clk)
  214. return;
  215. spin_lock_irqsave(&clock_lock, flags);
  216. __clk_disable(clk);
  217. spin_unlock_irqrestore(&clock_lock, flags);
  218. }
  219. EXPORT_SYMBOL_GPL(clk_disable);
  220. static int __clk_enable(struct clk *clk)
  221. {
  222. int ret = 0;
  223. if (clk->usecount++ == 0) {
  224. if (clk->parent) {
  225. ret = __clk_enable(clk->parent);
  226. if (unlikely(ret))
  227. goto err;
  228. }
  229. if (clk->ops && clk->ops->enable) {
  230. ret = clk->ops->enable(clk);
  231. if (ret) {
  232. if (clk->parent)
  233. __clk_disable(clk->parent);
  234. goto err;
  235. }
  236. }
  237. }
  238. return ret;
  239. err:
  240. clk->usecount--;
  241. return ret;
  242. }
  243. int clk_enable(struct clk *clk)
  244. {
  245. unsigned long flags;
  246. int ret;
  247. if (!clk)
  248. return -EINVAL;
  249. spin_lock_irqsave(&clock_lock, flags);
  250. ret = __clk_enable(clk);
  251. spin_unlock_irqrestore(&clock_lock, flags);
  252. return ret;
  253. }
  254. EXPORT_SYMBOL_GPL(clk_enable);
  255. static LIST_HEAD(root_clks);
  256. /**
  257. * recalculate_root_clocks - recalculate and propagate all root clocks
  258. *
  259. * Recalculates all root clocks (clocks with no parent), which if the
  260. * clock's .recalc is set correctly, should also propagate their rates.
  261. * Called at init.
  262. */
  263. void recalculate_root_clocks(void)
  264. {
  265. struct clk *clkp;
  266. list_for_each_entry(clkp, &root_clks, sibling) {
  267. if (clkp->ops && clkp->ops->recalc)
  268. clkp->rate = clkp->ops->recalc(clkp);
  269. propagate_rate(clkp);
  270. }
  271. }
  272. static struct clk_mapping dummy_mapping;
  273. static struct clk *lookup_root_clock(struct clk *clk)
  274. {
  275. while (clk->parent)
  276. clk = clk->parent;
  277. return clk;
  278. }
  279. static int clk_establish_mapping(struct clk *clk)
  280. {
  281. struct clk_mapping *mapping = clk->mapping;
  282. /*
  283. * Propagate mappings.
  284. */
  285. if (!mapping) {
  286. struct clk *clkp;
  287. /*
  288. * dummy mapping for root clocks with no specified ranges
  289. */
  290. if (!clk->parent) {
  291. clk->mapping = &dummy_mapping;
  292. goto out;
  293. }
  294. /*
  295. * If we're on a child clock and it provides no mapping of its
  296. * own, inherit the mapping from its root clock.
  297. */
  298. clkp = lookup_root_clock(clk);
  299. mapping = clkp->mapping;
  300. BUG_ON(!mapping);
  301. }
  302. /*
  303. * Establish initial mapping.
  304. */
  305. if (!mapping->base && mapping->phys) {
  306. kref_init(&mapping->ref);
  307. mapping->base = ioremap_nocache(mapping->phys, mapping->len);
  308. if (unlikely(!mapping->base))
  309. return -ENXIO;
  310. } else if (mapping->base) {
  311. /*
  312. * Bump the refcount for an existing mapping
  313. */
  314. kref_get(&mapping->ref);
  315. }
  316. clk->mapping = mapping;
  317. out:
  318. clk->mapped_reg = clk->mapping->base;
  319. clk->mapped_reg += (phys_addr_t)clk->enable_reg - clk->mapping->phys;
  320. return 0;
  321. }
  322. static void clk_destroy_mapping(struct kref *kref)
  323. {
  324. struct clk_mapping *mapping;
  325. mapping = container_of(kref, struct clk_mapping, ref);
  326. iounmap(mapping->base);
  327. }
  328. static void clk_teardown_mapping(struct clk *clk)
  329. {
  330. struct clk_mapping *mapping = clk->mapping;
  331. /* Nothing to do */
  332. if (mapping == &dummy_mapping)
  333. goto out;
  334. kref_put(&mapping->ref, clk_destroy_mapping);
  335. clk->mapping = NULL;
  336. out:
  337. clk->mapped_reg = NULL;
  338. }
  339. int clk_register(struct clk *clk)
  340. {
  341. int ret;
  342. if (IS_ERR_OR_NULL(clk))
  343. return -EINVAL;
  344. /*
  345. * trap out already registered clocks
  346. */
  347. if (clk->node.next || clk->node.prev)
  348. return 0;
  349. mutex_lock(&clock_list_sem);
  350. INIT_LIST_HEAD(&clk->children);
  351. clk->usecount = 0;
  352. ret = clk_establish_mapping(clk);
  353. if (unlikely(ret))
  354. goto out_unlock;
  355. if (clk->parent)
  356. list_add(&clk->sibling, &clk->parent->children);
  357. else
  358. list_add(&clk->sibling, &root_clks);
  359. list_add(&clk->node, &clock_list);
  360. #ifdef CONFIG_SH_CLK_CPG_LEGACY
  361. if (clk->ops && clk->ops->init)
  362. clk->ops->init(clk);
  363. #endif
  364. out_unlock:
  365. mutex_unlock(&clock_list_sem);
  366. return ret;
  367. }
  368. EXPORT_SYMBOL_GPL(clk_register);
  369. void clk_unregister(struct clk *clk)
  370. {
  371. mutex_lock(&clock_list_sem);
  372. list_del(&clk->sibling);
  373. list_del(&clk->node);
  374. clk_teardown_mapping(clk);
  375. mutex_unlock(&clock_list_sem);
  376. }
  377. EXPORT_SYMBOL_GPL(clk_unregister);
  378. void clk_enable_init_clocks(void)
  379. {
  380. struct clk *clkp;
  381. list_for_each_entry(clkp, &clock_list, node)
  382. if (clkp->flags & CLK_ENABLE_ON_INIT)
  383. clk_enable(clkp);
  384. }
  385. unsigned long clk_get_rate(struct clk *clk)
  386. {
  387. return clk->rate;
  388. }
  389. EXPORT_SYMBOL_GPL(clk_get_rate);
  390. int clk_set_rate(struct clk *clk, unsigned long rate)
  391. {
  392. int ret = -EOPNOTSUPP;
  393. unsigned long flags;
  394. spin_lock_irqsave(&clock_lock, flags);
  395. if (likely(clk->ops && clk->ops->set_rate)) {
  396. ret = clk->ops->set_rate(clk, rate);
  397. if (ret != 0)
  398. goto out_unlock;
  399. } else {
  400. clk->rate = rate;
  401. ret = 0;
  402. }
  403. if (clk->ops && clk->ops->recalc)
  404. clk->rate = clk->ops->recalc(clk);
  405. propagate_rate(clk);
  406. out_unlock:
  407. spin_unlock_irqrestore(&clock_lock, flags);
  408. return ret;
  409. }
  410. EXPORT_SYMBOL_GPL(clk_set_rate);
  411. int clk_set_parent(struct clk *clk, struct clk *parent)
  412. {
  413. unsigned long flags;
  414. int ret = -EINVAL;
  415. if (!parent || !clk)
  416. return ret;
  417. if (clk->parent == parent)
  418. return 0;
  419. spin_lock_irqsave(&clock_lock, flags);
  420. if (clk->usecount == 0) {
  421. if (clk->ops->set_parent)
  422. ret = clk->ops->set_parent(clk, parent);
  423. else
  424. ret = clk_reparent(clk, parent);
  425. if (ret == 0) {
  426. if (clk->ops->recalc)
  427. clk->rate = clk->ops->recalc(clk);
  428. pr_debug("set parent of %p to %p (new rate %ld)\n",
  429. clk, clk->parent, clk->rate);
  430. propagate_rate(clk);
  431. }
  432. } else
  433. ret = -EBUSY;
  434. spin_unlock_irqrestore(&clock_lock, flags);
  435. return ret;
  436. }
  437. EXPORT_SYMBOL_GPL(clk_set_parent);
  438. struct clk *clk_get_parent(struct clk *clk)
  439. {
  440. return clk->parent;
  441. }
  442. EXPORT_SYMBOL_GPL(clk_get_parent);
  443. long clk_round_rate(struct clk *clk, unsigned long rate)
  444. {
  445. if (likely(clk->ops && clk->ops->round_rate)) {
  446. unsigned long flags, rounded;
  447. spin_lock_irqsave(&clock_lock, flags);
  448. rounded = clk->ops->round_rate(clk, rate);
  449. spin_unlock_irqrestore(&clock_lock, flags);
  450. return rounded;
  451. }
  452. return clk_get_rate(clk);
  453. }
  454. EXPORT_SYMBOL_GPL(clk_round_rate);
  455. long clk_round_parent(struct clk *clk, unsigned long target,
  456. unsigned long *best_freq, unsigned long *parent_freq,
  457. unsigned int div_min, unsigned int div_max)
  458. {
  459. struct cpufreq_frequency_table *freq, *best = NULL;
  460. unsigned long error = ULONG_MAX, freq_high, freq_low, div;
  461. struct clk *parent = clk_get_parent(clk);
  462. if (!parent) {
  463. *parent_freq = 0;
  464. *best_freq = clk_round_rate(clk, target);
  465. return abs(target - *best_freq);
  466. }
  467. for (freq = parent->freq_table; freq->frequency != CPUFREQ_TABLE_END;
  468. freq++) {
  469. if (freq->frequency == CPUFREQ_ENTRY_INVALID)
  470. continue;
  471. if (unlikely(freq->frequency / target <= div_min - 1)) {
  472. unsigned long freq_max;
  473. freq_max = (freq->frequency + div_min / 2) / div_min;
  474. if (error > target - freq_max) {
  475. error = target - freq_max;
  476. best = freq;
  477. if (best_freq)
  478. *best_freq = freq_max;
  479. }
  480. pr_debug("too low freq %u, error %lu\n", freq->frequency,
  481. target - freq_max);
  482. if (!error)
  483. break;
  484. continue;
  485. }
  486. if (unlikely(freq->frequency / target >= div_max)) {
  487. unsigned long freq_min;
  488. freq_min = (freq->frequency + div_max / 2) / div_max;
  489. if (error > freq_min - target) {
  490. error = freq_min - target;
  491. best = freq;
  492. if (best_freq)
  493. *best_freq = freq_min;
  494. }
  495. pr_debug("too high freq %u, error %lu\n", freq->frequency,
  496. freq_min - target);
  497. if (!error)
  498. break;
  499. continue;
  500. }
  501. div = freq->frequency / target;
  502. freq_high = freq->frequency / div;
  503. freq_low = freq->frequency / (div + 1);
  504. if (freq_high - target < error) {
  505. error = freq_high - target;
  506. best = freq;
  507. if (best_freq)
  508. *best_freq = freq_high;
  509. }
  510. if (target - freq_low < error) {
  511. error = target - freq_low;
  512. best = freq;
  513. if (best_freq)
  514. *best_freq = freq_low;
  515. }
  516. pr_debug("%u / %lu = %lu, / %lu = %lu, best %lu, parent %u\n",
  517. freq->frequency, div, freq_high, div + 1, freq_low,
  518. *best_freq, best->frequency);
  519. if (!error)
  520. break;
  521. }
  522. if (parent_freq)
  523. *parent_freq = best->frequency;
  524. return error;
  525. }
  526. EXPORT_SYMBOL_GPL(clk_round_parent);
  527. #ifdef CONFIG_PM
  528. static void clks_core_resume(void)
  529. {
  530. struct clk *clkp;
  531. list_for_each_entry(clkp, &clock_list, node) {
  532. if (likely(clkp->usecount && clkp->ops)) {
  533. unsigned long rate = clkp->rate;
  534. if (likely(clkp->ops->set_parent))
  535. clkp->ops->set_parent(clkp,
  536. clkp->parent);
  537. if (likely(clkp->ops->set_rate))
  538. clkp->ops->set_rate(clkp, rate);
  539. else if (likely(clkp->ops->recalc))
  540. clkp->rate = clkp->ops->recalc(clkp);
  541. }
  542. }
  543. }
  544. static struct syscore_ops clks_syscore_ops = {
  545. .resume = clks_core_resume,
  546. };
  547. static int __init clk_syscore_init(void)
  548. {
  549. register_syscore_ops(&clks_syscore_ops);
  550. return 0;
  551. }
  552. subsys_initcall(clk_syscore_init);
  553. #endif
  554. static int __init clk_late_init(void)
  555. {
  556. unsigned long flags;
  557. struct clk *clk;
  558. /* disable all clocks with zero use count */
  559. mutex_lock(&clock_list_sem);
  560. spin_lock_irqsave(&clock_lock, flags);
  561. list_for_each_entry(clk, &clock_list, node)
  562. if (!clk->usecount && clk->ops && clk->ops->disable)
  563. clk->ops->disable(clk);
  564. /* from now on allow clock disable operations */
  565. allow_disable = 1;
  566. spin_unlock_irqrestore(&clock_lock, flags);
  567. mutex_unlock(&clock_list_sem);
  568. return 0;
  569. }
  570. late_initcall(clk_late_init);