clock-dclk.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2004-2008 Simtec Electronics
  3. * Ben Dooks <ben@simtec.co.uk>
  4. * http://armlinux.simtec.co.uk/
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * S3C24XX - definitions for DCLK and CLKOUT registers
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/clk.h>
  15. #include <linux/io.h>
  16. #include <mach/regs-clock.h>
  17. #include <mach/regs-gpio.h>
  18. #include <plat/clock.h>
  19. #include <plat/cpu.h>
  20. /* clocks that could be registered by external code */
  21. static int s3c24xx_dclk_enable(struct clk *clk, int enable)
  22. {
  23. unsigned long dclkcon = __raw_readl(S3C24XX_DCLKCON);
  24. if (enable)
  25. dclkcon |= clk->ctrlbit;
  26. else
  27. dclkcon &= ~clk->ctrlbit;
  28. __raw_writel(dclkcon, S3C24XX_DCLKCON);
  29. return 0;
  30. }
  31. static int s3c24xx_dclk_setparent(struct clk *clk, struct clk *parent)
  32. {
  33. unsigned long dclkcon;
  34. unsigned int uclk;
  35. if (parent == &clk_upll)
  36. uclk = 1;
  37. else if (parent == &clk_p)
  38. uclk = 0;
  39. else
  40. return -EINVAL;
  41. clk->parent = parent;
  42. dclkcon = __raw_readl(S3C24XX_DCLKCON);
  43. if (clk->ctrlbit == S3C2410_DCLKCON_DCLK0EN) {
  44. if (uclk)
  45. dclkcon |= S3C2410_DCLKCON_DCLK0_UCLK;
  46. else
  47. dclkcon &= ~S3C2410_DCLKCON_DCLK0_UCLK;
  48. } else {
  49. if (uclk)
  50. dclkcon |= S3C2410_DCLKCON_DCLK1_UCLK;
  51. else
  52. dclkcon &= ~S3C2410_DCLKCON_DCLK1_UCLK;
  53. }
  54. __raw_writel(dclkcon, S3C24XX_DCLKCON);
  55. return 0;
  56. }
  57. static unsigned long s3c24xx_calc_div(struct clk *clk, unsigned long rate)
  58. {
  59. unsigned long div;
  60. if ((rate == 0) || !clk->parent)
  61. return 0;
  62. div = clk_get_rate(clk->parent) / rate;
  63. if (div < 2)
  64. div = 2;
  65. else if (div > 16)
  66. div = 16;
  67. return div;
  68. }
  69. static unsigned long s3c24xx_round_dclk_rate(struct clk *clk,
  70. unsigned long rate)
  71. {
  72. unsigned long div = s3c24xx_calc_div(clk, rate);
  73. if (div == 0)
  74. return 0;
  75. return clk_get_rate(clk->parent) / div;
  76. }
  77. static int s3c24xx_set_dclk_rate(struct clk *clk, unsigned long rate)
  78. {
  79. unsigned long mask, data, div = s3c24xx_calc_div(clk, rate);
  80. if (div == 0)
  81. return -EINVAL;
  82. if (clk == &s3c24xx_dclk0) {
  83. mask = S3C2410_DCLKCON_DCLK0_DIV_MASK |
  84. S3C2410_DCLKCON_DCLK0_CMP_MASK;
  85. data = S3C2410_DCLKCON_DCLK0_DIV(div) |
  86. S3C2410_DCLKCON_DCLK0_CMP((div + 1) / 2);
  87. } else if (clk == &s3c24xx_dclk1) {
  88. mask = S3C2410_DCLKCON_DCLK1_DIV_MASK |
  89. S3C2410_DCLKCON_DCLK1_CMP_MASK;
  90. data = S3C2410_DCLKCON_DCLK1_DIV(div) |
  91. S3C2410_DCLKCON_DCLK1_CMP((div + 1) / 2);
  92. } else
  93. return -EINVAL;
  94. clk->rate = clk_get_rate(clk->parent) / div;
  95. __raw_writel(((__raw_readl(S3C24XX_DCLKCON) & ~mask) | data),
  96. S3C24XX_DCLKCON);
  97. return clk->rate;
  98. }
  99. static int s3c24xx_clkout_setparent(struct clk *clk, struct clk *parent)
  100. {
  101. unsigned long mask;
  102. unsigned long source;
  103. /* calculate the MISCCR setting for the clock */
  104. if (parent == &clk_mpll)
  105. source = S3C2410_MISCCR_CLK0_MPLL;
  106. else if (parent == &clk_upll)
  107. source = S3C2410_MISCCR_CLK0_UPLL;
  108. else if (parent == &clk_f)
  109. source = S3C2410_MISCCR_CLK0_FCLK;
  110. else if (parent == &clk_h)
  111. source = S3C2410_MISCCR_CLK0_HCLK;
  112. else if (parent == &clk_p)
  113. source = S3C2410_MISCCR_CLK0_PCLK;
  114. else if (clk == &s3c24xx_clkout0 && parent == &s3c24xx_dclk0)
  115. source = S3C2410_MISCCR_CLK0_DCLK0;
  116. else if (clk == &s3c24xx_clkout1 && parent == &s3c24xx_dclk1)
  117. source = S3C2410_MISCCR_CLK0_DCLK0;
  118. else
  119. return -EINVAL;
  120. clk->parent = parent;
  121. if (clk == &s3c24xx_clkout0)
  122. mask = S3C2410_MISCCR_CLK0_MASK;
  123. else {
  124. source <<= 4;
  125. mask = S3C2410_MISCCR_CLK1_MASK;
  126. }
  127. s3c2410_modify_misccr(mask, source);
  128. return 0;
  129. }
  130. /* external clock definitions */
  131. static struct clk_ops dclk_ops = {
  132. .set_parent = s3c24xx_dclk_setparent,
  133. .set_rate = s3c24xx_set_dclk_rate,
  134. .round_rate = s3c24xx_round_dclk_rate,
  135. };
  136. struct clk s3c24xx_dclk0 = {
  137. .name = "dclk0",
  138. .ctrlbit = S3C2410_DCLKCON_DCLK0EN,
  139. .enable = s3c24xx_dclk_enable,
  140. .ops = &dclk_ops,
  141. };
  142. struct clk s3c24xx_dclk1 = {
  143. .name = "dclk1",
  144. .ctrlbit = S3C2410_DCLKCON_DCLK1EN,
  145. .enable = s3c24xx_dclk_enable,
  146. .ops = &dclk_ops,
  147. };
  148. static struct clk_ops clkout_ops = {
  149. .set_parent = s3c24xx_clkout_setparent,
  150. };
  151. struct clk s3c24xx_clkout0 = {
  152. .name = "clkout0",
  153. .ops = &clkout_ops,
  154. };
  155. struct clk s3c24xx_clkout1 = {
  156. .name = "clkout1",
  157. .ops = &clkout_ops,
  158. };