core.c 16 KB

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