clock-dclk.c 4.3 KB

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