clock.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * arch/arm/mach-ep93xx/clock.c
  3. * Clock control for Cirrus EP93xx chips.
  4. *
  5. * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
  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 as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/clk.h>
  14. #include <linux/err.h>
  15. #include <linux/module.h>
  16. #include <linux/string.h>
  17. #include <linux/io.h>
  18. #include <asm/clkdev.h>
  19. #include <asm/div64.h>
  20. #include <mach/hardware.h>
  21. struct clk {
  22. unsigned long rate;
  23. int users;
  24. u32 enable_reg;
  25. u32 enable_mask;
  26. };
  27. static struct clk clk_uart = {
  28. .rate = 14745600,
  29. };
  30. static struct clk clk_pll1;
  31. static struct clk clk_f;
  32. static struct clk clk_h;
  33. static struct clk clk_p;
  34. static struct clk clk_pll2;
  35. static struct clk clk_usb_host = {
  36. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  37. .enable_mask = EP93XX_SYSCON_CLOCK_USH_EN,
  38. };
  39. /* DMA Clocks */
  40. static struct clk clk_m2p0 = {
  41. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  42. .enable_mask = 0x00020000,
  43. };
  44. static struct clk clk_m2p1 = {
  45. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  46. .enable_mask = 0x00010000,
  47. };
  48. static struct clk clk_m2p2 = {
  49. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  50. .enable_mask = 0x00080000,
  51. };
  52. static struct clk clk_m2p3 = {
  53. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  54. .enable_mask = 0x00040000,
  55. };
  56. static struct clk clk_m2p4 = {
  57. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  58. .enable_mask = 0x00200000,
  59. };
  60. static struct clk clk_m2p5 = {
  61. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  62. .enable_mask = 0x00100000,
  63. };
  64. static struct clk clk_m2p6 = {
  65. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  66. .enable_mask = 0x00800000,
  67. };
  68. static struct clk clk_m2p7 = {
  69. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  70. .enable_mask = 0x00400000,
  71. };
  72. static struct clk clk_m2p8 = {
  73. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  74. .enable_mask = 0x02000000,
  75. };
  76. static struct clk clk_m2p9 = {
  77. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  78. .enable_mask = 0x01000000,
  79. };
  80. static struct clk clk_m2m0 = {
  81. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  82. .enable_mask = 0x04000000,
  83. };
  84. static struct clk clk_m2m1 = {
  85. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  86. .enable_mask = 0x08000000,
  87. };
  88. #define INIT_CK(dev,con,ck) \
  89. { .dev_id = dev, .con_id = con, .clk = ck }
  90. static struct clk_lookup clocks[] = {
  91. INIT_CK("apb:uart1", NULL, &clk_uart),
  92. INIT_CK("apb:uart2", NULL, &clk_uart),
  93. INIT_CK("apb:uart3", NULL, &clk_uart),
  94. INIT_CK(NULL, "pll1", &clk_pll1),
  95. INIT_CK(NULL, "fclk", &clk_f),
  96. INIT_CK(NULL, "hclk", &clk_h),
  97. INIT_CK(NULL, "pclk", &clk_p),
  98. INIT_CK(NULL, "pll2", &clk_pll2),
  99. INIT_CK(NULL, "usb_host", &clk_usb_host),
  100. INIT_CK(NULL, "m2p0", &clk_m2p0),
  101. INIT_CK(NULL, "m2p1", &clk_m2p1),
  102. INIT_CK(NULL, "m2p2", &clk_m2p2),
  103. INIT_CK(NULL, "m2p3", &clk_m2p3),
  104. INIT_CK(NULL, "m2p4", &clk_m2p4),
  105. INIT_CK(NULL, "m2p5", &clk_m2p5),
  106. INIT_CK(NULL, "m2p6", &clk_m2p6),
  107. INIT_CK(NULL, "m2p7", &clk_m2p7),
  108. INIT_CK(NULL, "m2p8", &clk_m2p8),
  109. INIT_CK(NULL, "m2p9", &clk_m2p9),
  110. INIT_CK(NULL, "m2m0", &clk_m2m0),
  111. INIT_CK(NULL, "m2m1", &clk_m2m1),
  112. };
  113. int clk_enable(struct clk *clk)
  114. {
  115. if (!clk->users++ && clk->enable_reg) {
  116. u32 value;
  117. value = __raw_readl(clk->enable_reg);
  118. __raw_writel(value | clk->enable_mask, clk->enable_reg);
  119. }
  120. return 0;
  121. }
  122. EXPORT_SYMBOL(clk_enable);
  123. void clk_disable(struct clk *clk)
  124. {
  125. if (!--clk->users && clk->enable_reg) {
  126. u32 value;
  127. value = __raw_readl(clk->enable_reg);
  128. __raw_writel(value & ~clk->enable_mask, clk->enable_reg);
  129. }
  130. }
  131. EXPORT_SYMBOL(clk_disable);
  132. unsigned long clk_get_rate(struct clk *clk)
  133. {
  134. return clk->rate;
  135. }
  136. EXPORT_SYMBOL(clk_get_rate);
  137. static char fclk_divisors[] = { 1, 2, 4, 8, 16, 1, 1, 1 };
  138. static char hclk_divisors[] = { 1, 2, 4, 5, 6, 8, 16, 32 };
  139. static char pclk_divisors[] = { 1, 2, 4, 8 };
  140. /*
  141. * PLL rate = 14.7456 MHz * (X1FBD + 1) * (X2FBD + 1) / (X2IPD + 1) / 2^PS
  142. */
  143. static unsigned long calc_pll_rate(u32 config_word)
  144. {
  145. unsigned long long rate;
  146. int i;
  147. rate = 14745600;
  148. rate *= ((config_word >> 11) & 0x1f) + 1; /* X1FBD */
  149. rate *= ((config_word >> 5) & 0x3f) + 1; /* X2FBD */
  150. do_div(rate, (config_word & 0x1f) + 1); /* X2IPD */
  151. for (i = 0; i < ((config_word >> 16) & 3); i++) /* PS */
  152. rate >>= 1;
  153. return (unsigned long)rate;
  154. }
  155. static void __init ep93xx_dma_clock_init(void)
  156. {
  157. clk_m2p0.rate = clk_h.rate;
  158. clk_m2p1.rate = clk_h.rate;
  159. clk_m2p2.rate = clk_h.rate;
  160. clk_m2p3.rate = clk_h.rate;
  161. clk_m2p4.rate = clk_h.rate;
  162. clk_m2p5.rate = clk_h.rate;
  163. clk_m2p6.rate = clk_h.rate;
  164. clk_m2p7.rate = clk_h.rate;
  165. clk_m2p8.rate = clk_h.rate;
  166. clk_m2p9.rate = clk_h.rate;
  167. clk_m2m0.rate = clk_h.rate;
  168. clk_m2m1.rate = clk_h.rate;
  169. }
  170. static int __init ep93xx_clock_init(void)
  171. {
  172. u32 value;
  173. int i;
  174. value = __raw_readl(EP93XX_SYSCON_CLOCK_SET1);
  175. if (!(value & 0x00800000)) { /* PLL1 bypassed? */
  176. clk_pll1.rate = 14745600;
  177. } else {
  178. clk_pll1.rate = calc_pll_rate(value);
  179. }
  180. clk_f.rate = clk_pll1.rate / fclk_divisors[(value >> 25) & 0x7];
  181. clk_h.rate = clk_pll1.rate / hclk_divisors[(value >> 20) & 0x7];
  182. clk_p.rate = clk_h.rate / pclk_divisors[(value >> 18) & 0x3];
  183. ep93xx_dma_clock_init();
  184. value = __raw_readl(EP93XX_SYSCON_CLOCK_SET2);
  185. if (!(value & 0x00080000)) { /* PLL2 bypassed? */
  186. clk_pll2.rate = 14745600;
  187. } else if (value & 0x00040000) { /* PLL2 enabled? */
  188. clk_pll2.rate = calc_pll_rate(value);
  189. } else {
  190. clk_pll2.rate = 0;
  191. }
  192. clk_usb_host.rate = clk_pll2.rate / (((value >> 28) & 0xf) + 1);
  193. printk(KERN_INFO "ep93xx: PLL1 running at %ld MHz, PLL2 at %ld MHz\n",
  194. clk_pll1.rate / 1000000, clk_pll2.rate / 1000000);
  195. printk(KERN_INFO "ep93xx: FCLK %ld MHz, HCLK %ld MHz, PCLK %ld MHz\n",
  196. clk_f.rate / 1000000, clk_h.rate / 1000000,
  197. clk_p.rate / 1000000);
  198. for (i = 0; i < ARRAY_SIZE(clocks); i++)
  199. clkdev_add(&clocks[i]);
  200. return 0;
  201. }
  202. arch_initcall(ep93xx_clock_init);