clock.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. int sw_locked;
  25. void __iomem *enable_reg;
  26. u32 enable_mask;
  27. unsigned long (*get_rate)(struct clk *clk);
  28. int (*set_rate)(struct clk *clk, unsigned long rate);
  29. };
  30. static unsigned long get_uart_rate(struct clk *clk);
  31. static int set_keytchclk_rate(struct clk *clk, unsigned long rate);
  32. static struct clk clk_uart1 = {
  33. .sw_locked = 1,
  34. .enable_reg = EP93XX_SYSCON_DEVCFG,
  35. .enable_mask = EP93XX_SYSCON_DEVCFG_U1EN,
  36. .get_rate = get_uart_rate,
  37. };
  38. static struct clk clk_uart2 = {
  39. .sw_locked = 1,
  40. .enable_reg = EP93XX_SYSCON_DEVCFG,
  41. .enable_mask = EP93XX_SYSCON_DEVCFG_U2EN,
  42. .get_rate = get_uart_rate,
  43. };
  44. static struct clk clk_uart3 = {
  45. .sw_locked = 1,
  46. .enable_reg = EP93XX_SYSCON_DEVCFG,
  47. .enable_mask = EP93XX_SYSCON_DEVCFG_U3EN,
  48. .get_rate = get_uart_rate,
  49. };
  50. static struct clk clk_pll1;
  51. static struct clk clk_f;
  52. static struct clk clk_h;
  53. static struct clk clk_p;
  54. static struct clk clk_pll2;
  55. static struct clk clk_usb_host = {
  56. .enable_reg = EP93XX_SYSCON_PWRCNT,
  57. .enable_mask = EP93XX_SYSCON_PWRCNT_USH_EN,
  58. };
  59. static struct clk clk_keypad = {
  60. .sw_locked = 1,
  61. .enable_reg = EP93XX_SYSCON_KEYTCHCLKDIV,
  62. .enable_mask = EP93XX_SYSCON_KEYTCHCLKDIV_KEN,
  63. .set_rate = set_keytchclk_rate,
  64. };
  65. static struct clk clk_pwm = {
  66. .rate = EP93XX_EXT_CLK_RATE,
  67. };
  68. /* DMA Clocks */
  69. static struct clk clk_m2p0 = {
  70. .enable_reg = EP93XX_SYSCON_PWRCNT,
  71. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P0,
  72. };
  73. static struct clk clk_m2p1 = {
  74. .enable_reg = EP93XX_SYSCON_PWRCNT,
  75. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P1,
  76. };
  77. static struct clk clk_m2p2 = {
  78. .enable_reg = EP93XX_SYSCON_PWRCNT,
  79. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P2,
  80. };
  81. static struct clk clk_m2p3 = {
  82. .enable_reg = EP93XX_SYSCON_PWRCNT,
  83. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P3,
  84. };
  85. static struct clk clk_m2p4 = {
  86. .enable_reg = EP93XX_SYSCON_PWRCNT,
  87. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P4,
  88. };
  89. static struct clk clk_m2p5 = {
  90. .enable_reg = EP93XX_SYSCON_PWRCNT,
  91. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P5,
  92. };
  93. static struct clk clk_m2p6 = {
  94. .enable_reg = EP93XX_SYSCON_PWRCNT,
  95. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P6,
  96. };
  97. static struct clk clk_m2p7 = {
  98. .enable_reg = EP93XX_SYSCON_PWRCNT,
  99. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P7,
  100. };
  101. static struct clk clk_m2p8 = {
  102. .enable_reg = EP93XX_SYSCON_PWRCNT,
  103. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P8,
  104. };
  105. static struct clk clk_m2p9 = {
  106. .enable_reg = EP93XX_SYSCON_PWRCNT,
  107. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2P9,
  108. };
  109. static struct clk clk_m2m0 = {
  110. .enable_reg = EP93XX_SYSCON_PWRCNT,
  111. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2M0,
  112. };
  113. static struct clk clk_m2m1 = {
  114. .enable_reg = EP93XX_SYSCON_PWRCNT,
  115. .enable_mask = EP93XX_SYSCON_PWRCNT_DMA_M2M1,
  116. };
  117. #define INIT_CK(dev,con,ck) \
  118. { .dev_id = dev, .con_id = con, .clk = ck }
  119. static struct clk_lookup clocks[] = {
  120. INIT_CK("apb:uart1", NULL, &clk_uart1),
  121. INIT_CK("apb:uart2", NULL, &clk_uart2),
  122. INIT_CK("apb:uart3", NULL, &clk_uart3),
  123. INIT_CK(NULL, "pll1", &clk_pll1),
  124. INIT_CK(NULL, "fclk", &clk_f),
  125. INIT_CK(NULL, "hclk", &clk_h),
  126. INIT_CK(NULL, "pclk", &clk_p),
  127. INIT_CK(NULL, "pll2", &clk_pll2),
  128. INIT_CK("ep93xx-ohci", NULL, &clk_usb_host),
  129. INIT_CK("ep93xx-keypad", NULL, &clk_keypad),
  130. INIT_CK(NULL, "pwm_clk", &clk_pwm),
  131. INIT_CK(NULL, "m2p0", &clk_m2p0),
  132. INIT_CK(NULL, "m2p1", &clk_m2p1),
  133. INIT_CK(NULL, "m2p2", &clk_m2p2),
  134. INIT_CK(NULL, "m2p3", &clk_m2p3),
  135. INIT_CK(NULL, "m2p4", &clk_m2p4),
  136. INIT_CK(NULL, "m2p5", &clk_m2p5),
  137. INIT_CK(NULL, "m2p6", &clk_m2p6),
  138. INIT_CK(NULL, "m2p7", &clk_m2p7),
  139. INIT_CK(NULL, "m2p8", &clk_m2p8),
  140. INIT_CK(NULL, "m2p9", &clk_m2p9),
  141. INIT_CK(NULL, "m2m0", &clk_m2m0),
  142. INIT_CK(NULL, "m2m1", &clk_m2m1),
  143. };
  144. int clk_enable(struct clk *clk)
  145. {
  146. if (!clk->users++ && clk->enable_reg) {
  147. u32 value;
  148. value = __raw_readl(clk->enable_reg);
  149. value |= clk->enable_mask;
  150. if (clk->sw_locked)
  151. ep93xx_syscon_swlocked_write(value, clk->enable_reg);
  152. else
  153. __raw_writel(value, clk->enable_reg);
  154. }
  155. return 0;
  156. }
  157. EXPORT_SYMBOL(clk_enable);
  158. void clk_disable(struct clk *clk)
  159. {
  160. if (!--clk->users && clk->enable_reg) {
  161. u32 value;
  162. value = __raw_readl(clk->enable_reg);
  163. value &= ~clk->enable_mask;
  164. if (clk->sw_locked)
  165. ep93xx_syscon_swlocked_write(value, clk->enable_reg);
  166. else
  167. __raw_writel(value, clk->enable_reg);
  168. }
  169. }
  170. EXPORT_SYMBOL(clk_disable);
  171. static unsigned long get_uart_rate(struct clk *clk)
  172. {
  173. u32 value;
  174. value = __raw_readl(EP93XX_SYSCON_PWRCNT);
  175. if (value & EP93XX_SYSCON_PWRCNT_UARTBAUD)
  176. return EP93XX_EXT_CLK_RATE;
  177. else
  178. return EP93XX_EXT_CLK_RATE / 2;
  179. }
  180. unsigned long clk_get_rate(struct clk *clk)
  181. {
  182. if (clk->get_rate)
  183. return clk->get_rate(clk);
  184. return clk->rate;
  185. }
  186. EXPORT_SYMBOL(clk_get_rate);
  187. static int set_keytchclk_rate(struct clk *clk, unsigned long rate)
  188. {
  189. u32 val;
  190. u32 div_bit;
  191. val = __raw_readl(clk->enable_reg);
  192. /*
  193. * The Key Matrix and ADC clocks are configured using the same
  194. * System Controller register. The clock used will be either
  195. * 1/4 or 1/16 the external clock rate depending on the
  196. * EP93XX_SYSCON_KEYTCHCLKDIV_KDIV/EP93XX_SYSCON_KEYTCHCLKDIV_ADIV
  197. * bit being set or cleared.
  198. */
  199. div_bit = clk->enable_mask >> 15;
  200. if (rate == EP93XX_KEYTCHCLK_DIV4)
  201. val |= div_bit;
  202. else if (rate == EP93XX_KEYTCHCLK_DIV16)
  203. val &= ~div_bit;
  204. else
  205. return -EINVAL;
  206. ep93xx_syscon_swlocked_write(val, clk->enable_reg);
  207. clk->rate = rate;
  208. return 0;
  209. }
  210. int clk_set_rate(struct clk *clk, unsigned long rate)
  211. {
  212. if (clk->set_rate)
  213. return clk->set_rate(clk, rate);
  214. return -EINVAL;
  215. }
  216. EXPORT_SYMBOL(clk_set_rate);
  217. static char fclk_divisors[] = { 1, 2, 4, 8, 16, 1, 1, 1 };
  218. static char hclk_divisors[] = { 1, 2, 4, 5, 6, 8, 16, 32 };
  219. static char pclk_divisors[] = { 1, 2, 4, 8 };
  220. /*
  221. * PLL rate = 14.7456 MHz * (X1FBD + 1) * (X2FBD + 1) / (X2IPD + 1) / 2^PS
  222. */
  223. static unsigned long calc_pll_rate(u32 config_word)
  224. {
  225. unsigned long long rate;
  226. int i;
  227. rate = EP93XX_EXT_CLK_RATE;
  228. rate *= ((config_word >> 11) & 0x1f) + 1; /* X1FBD */
  229. rate *= ((config_word >> 5) & 0x3f) + 1; /* X2FBD */
  230. do_div(rate, (config_word & 0x1f) + 1); /* X2IPD */
  231. for (i = 0; i < ((config_word >> 16) & 3); i++) /* PS */
  232. rate >>= 1;
  233. return (unsigned long)rate;
  234. }
  235. static void __init ep93xx_dma_clock_init(void)
  236. {
  237. clk_m2p0.rate = clk_h.rate;
  238. clk_m2p1.rate = clk_h.rate;
  239. clk_m2p2.rate = clk_h.rate;
  240. clk_m2p3.rate = clk_h.rate;
  241. clk_m2p4.rate = clk_h.rate;
  242. clk_m2p5.rate = clk_h.rate;
  243. clk_m2p6.rate = clk_h.rate;
  244. clk_m2p7.rate = clk_h.rate;
  245. clk_m2p8.rate = clk_h.rate;
  246. clk_m2p9.rate = clk_h.rate;
  247. clk_m2m0.rate = clk_h.rate;
  248. clk_m2m1.rate = clk_h.rate;
  249. }
  250. static int __init ep93xx_clock_init(void)
  251. {
  252. u32 value;
  253. int i;
  254. value = __raw_readl(EP93XX_SYSCON_CLOCK_SET1);
  255. if (!(value & 0x00800000)) { /* PLL1 bypassed? */
  256. clk_pll1.rate = EP93XX_EXT_CLK_RATE;
  257. } else {
  258. clk_pll1.rate = calc_pll_rate(value);
  259. }
  260. clk_f.rate = clk_pll1.rate / fclk_divisors[(value >> 25) & 0x7];
  261. clk_h.rate = clk_pll1.rate / hclk_divisors[(value >> 20) & 0x7];
  262. clk_p.rate = clk_h.rate / pclk_divisors[(value >> 18) & 0x3];
  263. ep93xx_dma_clock_init();
  264. value = __raw_readl(EP93XX_SYSCON_CLOCK_SET2);
  265. if (!(value & 0x00080000)) { /* PLL2 bypassed? */
  266. clk_pll2.rate = EP93XX_EXT_CLK_RATE;
  267. } else if (value & 0x00040000) { /* PLL2 enabled? */
  268. clk_pll2.rate = calc_pll_rate(value);
  269. } else {
  270. clk_pll2.rate = 0;
  271. }
  272. clk_usb_host.rate = clk_pll2.rate / (((value >> 28) & 0xf) + 1);
  273. printk(KERN_INFO "ep93xx: PLL1 running at %ld MHz, PLL2 at %ld MHz\n",
  274. clk_pll1.rate / 1000000, clk_pll2.rate / 1000000);
  275. printk(KERN_INFO "ep93xx: FCLK %ld MHz, HCLK %ld MHz, PCLK %ld MHz\n",
  276. clk_f.rate / 1000000, clk_h.rate / 1000000,
  277. clk_p.rate / 1000000);
  278. for (i = 0; i < ARRAY_SIZE(clocks); i++)
  279. clkdev_add(&clocks[i]);
  280. return 0;
  281. }
  282. arch_initcall(ep93xx_clock_init);