clock-clksrc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* linux/arch/arm/plat-samsung/clock-clksrc.c
  2. *
  3. * Copyright 2008 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. * http://armlinux.simtec.co.uk/
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/list.h>
  15. #include <linux/errno.h>
  16. #include <linux/err.h>
  17. #include <linux/clk.h>
  18. #include <linux/sysdev.h>
  19. #include <linux/io.h>
  20. #include <plat/clock.h>
  21. #include <plat/clock-clksrc.h>
  22. #include <plat/cpu-freq.h>
  23. static inline struct clksrc_clk *to_clksrc(struct clk *clk)
  24. {
  25. return container_of(clk, struct clksrc_clk, clk);
  26. }
  27. static inline u32 bit_mask(u32 shift, u32 nr_bits)
  28. {
  29. u32 mask = 0xffffffff >> (32 - nr_bits);
  30. return mask << shift;
  31. }
  32. static unsigned long s3c_getrate_clksrc(struct clk *clk)
  33. {
  34. struct clksrc_clk *sclk = to_clksrc(clk);
  35. unsigned long rate = clk_get_rate(clk->parent);
  36. u32 clkdiv = __raw_readl(sclk->reg_div.reg);
  37. u32 mask = bit_mask(sclk->reg_div.shift, sclk->reg_div.size);
  38. clkdiv &= mask;
  39. clkdiv >>= sclk->reg_div.shift;
  40. clkdiv++;
  41. rate /= clkdiv;
  42. return rate;
  43. }
  44. static int s3c_setrate_clksrc(struct clk *clk, unsigned long rate)
  45. {
  46. struct clksrc_clk *sclk = to_clksrc(clk);
  47. void __iomem *reg = sclk->reg_div.reg;
  48. unsigned int div;
  49. u32 mask = bit_mask(sclk->reg_div.shift, sclk->reg_div.size);
  50. u32 val;
  51. rate = clk_round_rate(clk, rate);
  52. div = clk_get_rate(clk->parent) / rate;
  53. if (div > (1 << sclk->reg_div.size))
  54. return -EINVAL;
  55. val = __raw_readl(reg);
  56. val &= ~mask;
  57. val |= (div - 1) << sclk->reg_div.shift;
  58. __raw_writel(val, reg);
  59. return 0;
  60. }
  61. static int s3c_setparent_clksrc(struct clk *clk, struct clk *parent)
  62. {
  63. struct clksrc_clk *sclk = to_clksrc(clk);
  64. struct clksrc_sources *srcs = sclk->sources;
  65. u32 clksrc = __raw_readl(sclk->reg_src.reg);
  66. u32 mask = bit_mask(sclk->reg_src.shift, sclk->reg_src.size);
  67. int src_nr = -1;
  68. int ptr;
  69. for (ptr = 0; ptr < srcs->nr_sources; ptr++)
  70. if (srcs->sources[ptr] == parent) {
  71. src_nr = ptr;
  72. break;
  73. }
  74. if (src_nr >= 0) {
  75. clk->parent = parent;
  76. clksrc &= ~mask;
  77. clksrc |= src_nr << sclk->reg_src.shift;
  78. __raw_writel(clksrc, sclk->reg_src.reg);
  79. return 0;
  80. }
  81. return -EINVAL;
  82. }
  83. static unsigned long s3c_roundrate_clksrc(struct clk *clk,
  84. unsigned long rate)
  85. {
  86. struct clksrc_clk *sclk = to_clksrc(clk);
  87. unsigned long parent_rate = clk_get_rate(clk->parent);
  88. int max_div = 1 << sclk->reg_div.size;
  89. int div;
  90. if (rate >= parent_rate)
  91. rate = parent_rate;
  92. else {
  93. div = parent_rate / rate;
  94. if (parent_rate % rate)
  95. div++;
  96. if (div == 0)
  97. div = 1;
  98. if (div > max_div)
  99. div = max_div;
  100. rate = parent_rate / div;
  101. }
  102. return rate;
  103. }
  104. /* Clock initialisation code */
  105. void __init_or_cpufreq s3c_set_clksrc(struct clksrc_clk *clk, bool announce)
  106. {
  107. struct clksrc_sources *srcs = clk->sources;
  108. u32 mask = bit_mask(clk->reg_src.shift, clk->reg_src.size);
  109. u32 clksrc;
  110. if (!clk->reg_src.reg) {
  111. if (!clk->clk.parent)
  112. printk(KERN_ERR "%s: no parent clock specified\n",
  113. clk->clk.name);
  114. return;
  115. }
  116. clksrc = __raw_readl(clk->reg_src.reg);
  117. clksrc &= mask;
  118. clksrc >>= clk->reg_src.shift;
  119. if (clksrc > srcs->nr_sources || !srcs->sources[clksrc]) {
  120. printk(KERN_ERR "%s: bad source %d\n",
  121. clk->clk.name, clksrc);
  122. return;
  123. }
  124. clk->clk.parent = srcs->sources[clksrc];
  125. if (announce)
  126. printk(KERN_INFO "%s: source is %s (%d), rate is %ld\n",
  127. clk->clk.name, clk->clk.parent->name, clksrc,
  128. clk_get_rate(&clk->clk));
  129. }
  130. static struct clk_ops clksrc_ops = {
  131. .set_parent = s3c_setparent_clksrc,
  132. .get_rate = s3c_getrate_clksrc,
  133. .set_rate = s3c_setrate_clksrc,
  134. .round_rate = s3c_roundrate_clksrc,
  135. };
  136. static struct clk_ops clksrc_ops_nodiv = {
  137. .set_parent = s3c_setparent_clksrc,
  138. };
  139. static struct clk_ops clksrc_ops_nosrc = {
  140. .get_rate = s3c_getrate_clksrc,
  141. .set_rate = s3c_setrate_clksrc,
  142. .round_rate = s3c_roundrate_clksrc,
  143. };
  144. void __init s3c_register_clksrc(struct clksrc_clk *clksrc, int size)
  145. {
  146. int ret;
  147. for (; size > 0; size--, clksrc++) {
  148. if (!clksrc->reg_div.reg && !clksrc->reg_src.reg)
  149. printk(KERN_ERR "%s: clock %s has no registers set\n",
  150. __func__, clksrc->clk.name);
  151. /* fill in the default functions */
  152. if (!clksrc->clk.ops) {
  153. if (!clksrc->reg_div.reg)
  154. clksrc->clk.ops = &clksrc_ops_nodiv;
  155. else if (!clksrc->reg_src.reg)
  156. clksrc->clk.ops = &clksrc_ops_nosrc;
  157. else
  158. clksrc->clk.ops = &clksrc_ops;
  159. }
  160. /* setup the clocksource, but do not announce it
  161. * as it may be re-set by the setup routines
  162. * called after the rest of the clocks have been
  163. * registered
  164. */
  165. s3c_set_clksrc(clksrc, false);
  166. ret = s3c24xx_register_clock(&clksrc->clk);
  167. if (ret < 0) {
  168. printk(KERN_ERR "%s: failed to register %s (%d)\n",
  169. __func__, clksrc->clk.name, ret);
  170. }
  171. }
  172. }