clock.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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_DEVCFG,
  43. .enable_mask = EP93XX_SYSCON_DEVCFG_U1EN,
  44. .get_rate = get_uart_rate,
  45. };
  46. static struct clk clk_uart2 = {
  47. .sw_locked = 1,
  48. .enable_reg = EP93XX_SYSCON_DEVCFG,
  49. .enable_mask = EP93XX_SYSCON_DEVCFG_U2EN,
  50. .get_rate = get_uart_rate,
  51. };
  52. static struct clk clk_uart3 = {
  53. .sw_locked = 1,
  54. .enable_reg = EP93XX_SYSCON_DEVCFG,
  55. .enable_mask = EP93XX_SYSCON_DEVCFG_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_PWRCNT,
  65. .enable_mask = EP93XX_SYSCON_PWRCNT_USH_EN,
  66. };
  67. /* DMA Clocks */
  68. static struct clk clk_m2p0 = {
  69. .enable_reg = EP93XX_SYSCON_PWRCNT,
  70. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P0,
  71. };
  72. static struct clk clk_m2p1 = {
  73. .enable_reg = EP93XX_SYSCON_PWRCNT,
  74. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P1,
  75. };
  76. static struct clk clk_m2p2 = {
  77. .enable_reg = EP93XX_SYSCON_PWRCNT,
  78. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P2,
  79. };
  80. static struct clk clk_m2p3 = {
  81. .enable_reg = EP93XX_SYSCON_PWRCNT,
  82. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P3,
  83. };
  84. static struct clk clk_m2p4 = {
  85. .enable_reg = EP93XX_SYSCON_PWRCNT,
  86. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P4,
  87. };
  88. static struct clk clk_m2p5 = {
  89. .enable_reg = EP93XX_SYSCON_PWRCNT,
  90. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P5,
  91. };
  92. static struct clk clk_m2p6 = {
  93. .enable_reg = EP93XX_SYSCON_PWRCNT,
  94. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P6,
  95. };
  96. static struct clk clk_m2p7 = {
  97. .enable_reg = EP93XX_SYSCON_PWRCNT,
  98. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P7,
  99. };
  100. static struct clk clk_m2p8 = {
  101. .enable_reg = EP93XX_SYSCON_PWRCNT,
  102. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P8,
  103. };
  104. static struct clk clk_m2p9 = {
  105. .enable_reg = EP93XX_SYSCON_PWRCNT,
  106. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P9,
  107. };
  108. static struct clk clk_m2m0 = {
  109. .enable_reg = EP93XX_SYSCON_PWRCNT,
  110. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2M0,
  111. };
  112. static struct clk clk_m2m1 = {
  113. .enable_reg = EP93XX_SYSCON_PWRCNT,
  114. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2M1,
  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("ep93xx-ohci", NULL, &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. value |= clk->enable_mask;
  147. if (clk->sw_locked)
  148. ep93xx_syscon_swlocked_write(value, clk->enable_reg);
  149. else
  150. __raw_writel(value, clk->enable_reg);
  151. }
  152. return 0;
  153. }
  154. EXPORT_SYMBOL(clk_enable);
  155. void clk_disable(struct clk *clk)
  156. {
  157. if (!--clk->users && clk->enable_reg) {
  158. u32 value;
  159. value = __raw_readl(clk->enable_reg);
  160. value &= ~clk->enable_mask;
  161. if (clk->sw_locked)
  162. ep93xx_syscon_swlocked_write(value, clk->enable_reg);
  163. else
  164. __raw_writel(value, clk->enable_reg);
  165. }
  166. }
  167. EXPORT_SYMBOL(clk_disable);
  168. static unsigned long get_uart_rate(struct clk *clk)
  169. {
  170. u32 value;
  171. value = __raw_readl(EP93XX_SYSCON_PWRCNT);
  172. if (value & EP93XX_SYSCON_PWRCNT_UARTBAUD)
  173. return EP93XX_EXT_CLK_RATE;
  174. else
  175. return EP93XX_EXT_CLK_RATE / 2;
  176. }
  177. unsigned long clk_get_rate(struct clk *clk)
  178. {
  179. if (clk->get_rate)
  180. return clk->get_rate(clk);
  181. return clk->rate;
  182. }
  183. EXPORT_SYMBOL(clk_get_rate);
  184. static char fclk_divisors[] = { 1, 2, 4, 8, 16, 1, 1, 1 };
  185. static char hclk_divisors[] = { 1, 2, 4, 5, 6, 8, 16, 32 };
  186. static char pclk_divisors[] = { 1, 2, 4, 8 };
  187. /*
  188. * PLL rate = 14.7456 MHz * (X1FBD + 1) * (X2FBD + 1) / (X2IPD + 1) / 2^PS
  189. */
  190. static unsigned long calc_pll_rate(u32 config_word)
  191. {
  192. unsigned long long rate;
  193. int i;
  194. rate = EP93XX_EXT_CLK_RATE;
  195. rate *= ((config_word >> 11) & 0x1f) + 1; /* X1FBD */
  196. rate *= ((config_word >> 5) & 0x3f) + 1; /* X2FBD */
  197. do_div(rate, (config_word & 0x1f) + 1); /* X2IPD */
  198. for (i = 0; i < ((config_word >> 16) & 3); i++) /* PS */
  199. rate >>= 1;
  200. return (unsigned long)rate;
  201. }
  202. static void __init ep93xx_dma_clock_init(void)
  203. {
  204. clk_m2p0.rate = clk_h.rate;
  205. clk_m2p1.rate = clk_h.rate;
  206. clk_m2p2.rate = clk_h.rate;
  207. clk_m2p3.rate = clk_h.rate;
  208. clk_m2p4.rate = clk_h.rate;
  209. clk_m2p5.rate = clk_h.rate;
  210. clk_m2p6.rate = clk_h.rate;
  211. clk_m2p7.rate = clk_h.rate;
  212. clk_m2p8.rate = clk_h.rate;
  213. clk_m2p9.rate = clk_h.rate;
  214. clk_m2m0.rate = clk_h.rate;
  215. clk_m2m1.rate = clk_h.rate;
  216. }
  217. static int __init ep93xx_clock_init(void)
  218. {
  219. u32 value;
  220. int i;
  221. value = __raw_readl(EP93XX_SYSCON_CLOCK_SET1);
  222. if (!(value & 0x00800000)) { /* PLL1 bypassed? */
  223. clk_pll1.rate = EP93XX_EXT_CLK_RATE;
  224. } else {
  225. clk_pll1.rate = calc_pll_rate(value);
  226. }
  227. clk_f.rate = clk_pll1.rate / fclk_divisors[(value >> 25) & 0x7];
  228. clk_h.rate = clk_pll1.rate / hclk_divisors[(value >> 20) & 0x7];
  229. clk_p.rate = clk_h.rate / pclk_divisors[(value >> 18) & 0x3];
  230. ep93xx_dma_clock_init();
  231. value = __raw_readl(EP93XX_SYSCON_CLOCK_SET2);
  232. if (!(value & 0x00080000)) { /* PLL2 bypassed? */
  233. clk_pll2.rate = EP93XX_EXT_CLK_RATE;
  234. } else if (value & 0x00040000) { /* PLL2 enabled? */
  235. clk_pll2.rate = calc_pll_rate(value);
  236. } else {
  237. clk_pll2.rate = 0;
  238. }
  239. clk_usb_host.rate = clk_pll2.rate / (((value >> 28) & 0xf) + 1);
  240. printk(KERN_INFO "ep93xx: PLL1 running at %ld MHz, PLL2 at %ld MHz\n",
  241. clk_pll1.rate / 1000000, clk_pll2.rate / 1000000);
  242. printk(KERN_INFO "ep93xx: FCLK %ld MHz, HCLK %ld MHz, PCLK %ld MHz\n",
  243. clk_f.rate / 1000000, clk_h.rate / 1000000,
  244. clk_p.rate / 1000000);
  245. for (i = 0; i < ARRAY_SIZE(clocks); i++)
  246. clkdev_add(&clocks[i]);
  247. return 0;
  248. }
  249. arch_initcall(ep93xx_clock_init);