clock.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. /*
  22. * The EP93xx has two external crystal oscillators. To generate the
  23. * required high-frequency clocks, the processor uses two phase-locked-
  24. * loops (PLLs) to multiply the incoming external clock signal to much
  25. * higher frequencies that are then divided down by programmable dividers
  26. * to produce the needed clocks. The PLLs operate independently of one
  27. * another.
  28. */
  29. #define EP93XX_EXT_CLK_RATE 14745600
  30. #define EP93XX_EXT_RTC_RATE 32768
  31. struct clk {
  32. unsigned long rate;
  33. int users;
  34. int sw_locked;
  35. u32 enable_reg;
  36. u32 enable_mask;
  37. unsigned long (*get_rate)(struct clk *clk);
  38. };
  39. static unsigned long get_uart_rate(struct clk *clk);
  40. static struct clk clk_uart1 = {
  41. .sw_locked = 1,
  42. .enable_reg = EP93XX_SYSCON_DEVICE_CONFIG,
  43. .enable_mask = EP93XX_SYSCON_DEVICE_CONFIG_U1EN,
  44. .get_rate = get_uart_rate,
  45. };
  46. static struct clk clk_uart2 = {
  47. .sw_locked = 1,
  48. .enable_reg = EP93XX_SYSCON_DEVICE_CONFIG,
  49. .enable_mask = EP93XX_SYSCON_DEVICE_CONFIG_U2EN,
  50. .get_rate = get_uart_rate,
  51. };
  52. static struct clk clk_uart3 = {
  53. .sw_locked = 1,
  54. .enable_reg = EP93XX_SYSCON_DEVICE_CONFIG,
  55. .enable_mask = EP93XX_SYSCON_DEVICE_CONFIG_U3EN,
  56. .get_rate = get_uart_rate,
  57. };
  58. static struct clk clk_pll1;
  59. static struct clk clk_f;
  60. static struct clk clk_h;
  61. static struct clk clk_p;
  62. static struct clk clk_pll2;
  63. static struct clk clk_usb_host = {
  64. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  65. .enable_mask = EP93XX_SYSCON_CLOCK_USH_EN,
  66. };
  67. /* DMA Clocks */
  68. static struct clk clk_m2p0 = {
  69. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  70. .enable_mask = 0x00020000,
  71. };
  72. static struct clk clk_m2p1 = {
  73. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  74. .enable_mask = 0x00010000,
  75. };
  76. static struct clk clk_m2p2 = {
  77. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  78. .enable_mask = 0x00080000,
  79. };
  80. static struct clk clk_m2p3 = {
  81. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  82. .enable_mask = 0x00040000,
  83. };
  84. static struct clk clk_m2p4 = {
  85. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  86. .enable_mask = 0x00200000,
  87. };
  88. static struct clk clk_m2p5 = {
  89. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  90. .enable_mask = 0x00100000,
  91. };
  92. static struct clk clk_m2p6 = {
  93. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  94. .enable_mask = 0x00800000,
  95. };
  96. static struct clk clk_m2p7 = {
  97. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  98. .enable_mask = 0x00400000,
  99. };
  100. static struct clk clk_m2p8 = {
  101. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  102. .enable_mask = 0x02000000,
  103. };
  104. static struct clk clk_m2p9 = {
  105. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  106. .enable_mask = 0x01000000,
  107. };
  108. static struct clk clk_m2m0 = {
  109. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  110. .enable_mask = 0x04000000,
  111. };
  112. static struct clk clk_m2m1 = {
  113. .enable_reg = EP93XX_SYSCON_CLOCK_CONTROL,
  114. .enable_mask = 0x08000000,
  115. };
  116. #define INIT_CK(dev,con,ck) \
  117. { .dev_id = dev, .con_id = con, .clk = ck }
  118. static struct clk_lookup clocks[] = {
  119. INIT_CK("apb:uart1", NULL, &clk_uart1),
  120. INIT_CK("apb:uart2", NULL, &clk_uart2),
  121. INIT_CK("apb:uart3", NULL, &clk_uart3),
  122. INIT_CK(NULL, "pll1", &clk_pll1),
  123. INIT_CK(NULL, "fclk", &clk_f),
  124. INIT_CK(NULL, "hclk", &clk_h),
  125. INIT_CK(NULL, "pclk", &clk_p),
  126. INIT_CK(NULL, "pll2", &clk_pll2),
  127. INIT_CK(NULL, "usb_host", &clk_usb_host),
  128. INIT_CK(NULL, "m2p0", &clk_m2p0),
  129. INIT_CK(NULL, "m2p1", &clk_m2p1),
  130. INIT_CK(NULL, "m2p2", &clk_m2p2),
  131. INIT_CK(NULL, "m2p3", &clk_m2p3),
  132. INIT_CK(NULL, "m2p4", &clk_m2p4),
  133. INIT_CK(NULL, "m2p5", &clk_m2p5),
  134. INIT_CK(NULL, "m2p6", &clk_m2p6),
  135. INIT_CK(NULL, "m2p7", &clk_m2p7),
  136. INIT_CK(NULL, "m2p8", &clk_m2p8),
  137. INIT_CK(NULL, "m2p9", &clk_m2p9),
  138. INIT_CK(NULL, "m2m0", &clk_m2m0),
  139. INIT_CK(NULL, "m2m1", &clk_m2m1),
  140. };
  141. int clk_enable(struct clk *clk)
  142. {
  143. if (!clk->users++ && clk->enable_reg) {
  144. u32 value;
  145. value = __raw_readl(clk->enable_reg);
  146. if (clk->sw_locked)
  147. __raw_writel(0xaa, EP93XX_SYSCON_SWLOCK);
  148. __raw_writel(value | clk->enable_mask, clk->enable_reg);
  149. }
  150. return 0;
  151. }
  152. EXPORT_SYMBOL(clk_enable);
  153. void clk_disable(struct clk *clk)
  154. {
  155. if (!--clk->users && clk->enable_reg) {
  156. u32 value;
  157. value = __raw_readl(clk->enable_reg);
  158. if (clk->sw_locked)
  159. __raw_writel(0xaa, EP93XX_SYSCON_SWLOCK);
  160. __raw_writel(value & ~clk->enable_mask, clk->enable_reg);
  161. }
  162. }
  163. EXPORT_SYMBOL(clk_disable);
  164. static unsigned long get_uart_rate(struct clk *clk)
  165. {
  166. u32 value;
  167. value = __raw_readl(EP93XX_SYSCON_CLOCK_CONTROL);
  168. if (value & EP93XX_SYSCON_CLOCK_UARTBAUD)
  169. return EP93XX_EXT_CLK_RATE;
  170. else
  171. return EP93XX_EXT_CLK_RATE / 2;
  172. }
  173. unsigned long clk_get_rate(struct clk *clk)
  174. {
  175. if (clk->get_rate)
  176. return clk->get_rate(clk);
  177. return clk->rate;
  178. }
  179. EXPORT_SYMBOL(clk_get_rate);
  180. static char fclk_divisors[] = { 1, 2, 4, 8, 16, 1, 1, 1 };
  181. static char hclk_divisors[] = { 1, 2, 4, 5, 6, 8, 16, 32 };
  182. static char pclk_divisors[] = { 1, 2, 4, 8 };
  183. /*
  184. * PLL rate = 14.7456 MHz * (X1FBD + 1) * (X2FBD + 1) / (X2IPD + 1) / 2^PS
  185. */
  186. static unsigned long calc_pll_rate(u32 config_word)
  187. {
  188. unsigned long long rate;
  189. int i;
  190. rate = EP93XX_EXT_CLK_RATE;
  191. rate *= ((config_word >> 11) & 0x1f) + 1; /* X1FBD */
  192. rate *= ((config_word >> 5) & 0x3f) + 1; /* X2FBD */
  193. do_div(rate, (config_word & 0x1f) + 1); /* X2IPD */
  194. for (i = 0; i < ((config_word >> 16) & 3); i++) /* PS */
  195. rate >>= 1;
  196. return (unsigned long)rate;
  197. }
  198. static void __init ep93xx_dma_clock_init(void)
  199. {
  200. clk_m2p0.rate = clk_h.rate;
  201. clk_m2p1.rate = clk_h.rate;
  202. clk_m2p2.rate = clk_h.rate;
  203. clk_m2p3.rate = clk_h.rate;
  204. clk_m2p4.rate = clk_h.rate;
  205. clk_m2p5.rate = clk_h.rate;
  206. clk_m2p6.rate = clk_h.rate;
  207. clk_m2p7.rate = clk_h.rate;
  208. clk_m2p8.rate = clk_h.rate;
  209. clk_m2p9.rate = clk_h.rate;
  210. clk_m2m0.rate = clk_h.rate;
  211. clk_m2m1.rate = clk_h.rate;
  212. }
  213. static int __init ep93xx_clock_init(void)
  214. {
  215. u32 value;
  216. int i;
  217. value = __raw_readl(EP93XX_SYSCON_CLOCK_SET1);
  218. if (!(value & 0x00800000)) { /* PLL1 bypassed? */
  219. clk_pll1.rate = EP93XX_EXT_CLK_RATE;
  220. } else {
  221. clk_pll1.rate = calc_pll_rate(value);
  222. }
  223. clk_f.rate = clk_pll1.rate / fclk_divisors[(value >> 25) & 0x7];
  224. clk_h.rate = clk_pll1.rate / hclk_divisors[(value >> 20) & 0x7];
  225. clk_p.rate = clk_h.rate / pclk_divisors[(value >> 18) & 0x3];
  226. ep93xx_dma_clock_init();
  227. value = __raw_readl(EP93XX_SYSCON_CLOCK_SET2);
  228. if (!(value & 0x00080000)) { /* PLL2 bypassed? */
  229. clk_pll2.rate = EP93XX_EXT_CLK_RATE;
  230. } else if (value & 0x00040000) { /* PLL2 enabled? */
  231. clk_pll2.rate = calc_pll_rate(value);
  232. } else {
  233. clk_pll2.rate = 0;
  234. }
  235. clk_usb_host.rate = clk_pll2.rate / (((value >> 28) & 0xf) + 1);
  236. printk(KERN_INFO "ep93xx: PLL1 running at %ld MHz, PLL2 at %ld MHz\n",
  237. clk_pll1.rate / 1000000, clk_pll2.rate / 1000000);
  238. printk(KERN_INFO "ep93xx: FCLK %ld MHz, HCLK %ld MHz, PCLK %ld MHz\n",
  239. clk_f.rate / 1000000, clk_h.rate / 1000000,
  240. clk_p.rate / 1000000);
  241. for (i = 0; i < ARRAY_SIZE(clocks); i++)
  242. clkdev_add(&clocks[i]);
  243. return 0;
  244. }
  245. arch_initcall(ep93xx_clock_init);