clock.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (C) 2009 by Sascha Hauer, Pengutronix
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. * MA 02110-1301, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/clk.h>
  22. #include <linux/io.h>
  23. #include <asm/clkdev.h>
  24. #include <mach/clock.h>
  25. #include <mach/hardware.h>
  26. #include <mach/common.h>
  27. #include <mach/mx25.h>
  28. #define CRM_BASE MX25_IO_ADDRESS(MX25_CRM_BASE_ADDR)
  29. #define CCM_MPCTL 0x00
  30. #define CCM_UPCTL 0x04
  31. #define CCM_CCTL 0x08
  32. #define CCM_CGCR0 0x0C
  33. #define CCM_CGCR1 0x10
  34. #define CCM_CGCR2 0x14
  35. #define CCM_PCDR0 0x18
  36. #define CCM_PCDR1 0x1C
  37. #define CCM_PCDR2 0x20
  38. #define CCM_PCDR3 0x24
  39. #define CCM_RCSR 0x28
  40. #define CCM_CRDR 0x2C
  41. #define CCM_DCVR0 0x30
  42. #define CCM_DCVR1 0x34
  43. #define CCM_DCVR2 0x38
  44. #define CCM_DCVR3 0x3c
  45. #define CCM_LTR0 0x40
  46. #define CCM_LTR1 0x44
  47. #define CCM_LTR2 0x48
  48. #define CCM_LTR3 0x4c
  49. static unsigned long get_rate_mpll(void)
  50. {
  51. ulong mpctl = __raw_readl(CRM_BASE + CCM_MPCTL);
  52. return mxc_decode_pll(mpctl, 24000000);
  53. }
  54. static unsigned long get_rate_upll(void)
  55. {
  56. ulong mpctl = __raw_readl(CRM_BASE + CCM_UPCTL);
  57. return mxc_decode_pll(mpctl, 24000000);
  58. }
  59. unsigned long get_rate_arm(struct clk *clk)
  60. {
  61. unsigned long cctl = readl(CRM_BASE + CCM_CCTL);
  62. unsigned long rate = get_rate_mpll();
  63. if (cctl & (1 << 14))
  64. rate = (rate * 3) >> 1;
  65. return rate / ((cctl >> 30) + 1);
  66. }
  67. static unsigned long get_rate_ahb(struct clk *clk)
  68. {
  69. unsigned long cctl = readl(CRM_BASE + CCM_CCTL);
  70. return get_rate_arm(NULL) / (((cctl >> 28) & 0x3) + 1);
  71. }
  72. static unsigned long get_rate_ipg(struct clk *clk)
  73. {
  74. return get_rate_ahb(NULL) >> 1;
  75. }
  76. static unsigned long get_rate_per(int per)
  77. {
  78. unsigned long ofs = (per & 0x3) * 8;
  79. unsigned long reg = per & ~0x3;
  80. unsigned long val = (readl(CRM_BASE + CCM_PCDR0 + reg) >> ofs) & 0x3f;
  81. unsigned long fref;
  82. if (readl(CRM_BASE + 0x64) & (1 << per))
  83. fref = get_rate_upll();
  84. else
  85. fref = get_rate_ipg(NULL);
  86. return fref / (val + 1);
  87. }
  88. static unsigned long get_rate_uart(struct clk *clk)
  89. {
  90. return get_rate_per(15);
  91. }
  92. static unsigned long get_rate_i2c(struct clk *clk)
  93. {
  94. return get_rate_per(6);
  95. }
  96. static unsigned long get_rate_nfc(struct clk *clk)
  97. {
  98. return get_rate_per(8);
  99. }
  100. static unsigned long get_rate_otg(struct clk *clk)
  101. {
  102. return 48000000; /* FIXME */
  103. }
  104. static int clk_cgcr_enable(struct clk *clk)
  105. {
  106. u32 reg;
  107. reg = __raw_readl(clk->enable_reg);
  108. reg |= 1 << clk->enable_shift;
  109. __raw_writel(reg, clk->enable_reg);
  110. return 0;
  111. }
  112. static void clk_cgcr_disable(struct clk *clk)
  113. {
  114. u32 reg;
  115. reg = __raw_readl(clk->enable_reg);
  116. reg &= ~(1 << clk->enable_shift);
  117. __raw_writel(reg, clk->enable_reg);
  118. }
  119. #define DEFINE_CLOCK(name, i, er, es, gr, sr) \
  120. static struct clk name = { \
  121. .id = i, \
  122. .enable_reg = CRM_BASE + er, \
  123. .enable_shift = es, \
  124. .get_rate = gr, \
  125. .set_rate = sr, \
  126. .enable = clk_cgcr_enable, \
  127. .disable = clk_cgcr_disable, \
  128. }
  129. DEFINE_CLOCK(gpt_clk, 0, CCM_CGCR0, 5, get_rate_ipg, NULL);
  130. DEFINE_CLOCK(cspi1_clk, 0, CCM_CGCR1, 5, get_rate_ipg, NULL);
  131. DEFINE_CLOCK(cspi2_clk, 0, CCM_CGCR1, 6, get_rate_ipg, NULL);
  132. DEFINE_CLOCK(cspi3_clk, 0, CCM_CGCR1, 7, get_rate_ipg, NULL);
  133. DEFINE_CLOCK(uart1_clk, 0, CCM_CGCR2, 14, get_rate_uart, NULL);
  134. DEFINE_CLOCK(uart2_clk, 0, CCM_CGCR2, 15, get_rate_uart, NULL);
  135. DEFINE_CLOCK(uart3_clk, 0, CCM_CGCR2, 16, get_rate_uart, NULL);
  136. DEFINE_CLOCK(uart4_clk, 0, CCM_CGCR2, 17, get_rate_uart, NULL);
  137. DEFINE_CLOCK(uart5_clk, 0, CCM_CGCR2, 18, get_rate_uart, NULL);
  138. DEFINE_CLOCK(nfc_clk, 0, CCM_CGCR0, 8, get_rate_nfc, NULL);
  139. DEFINE_CLOCK(usbotg_clk, 0, CCM_CGCR0, 28, get_rate_otg, NULL);
  140. DEFINE_CLOCK(pwm1_clk, 0, CCM_CGCR1, 31, get_rate_ipg, NULL);
  141. DEFINE_CLOCK(pwm2_clk, 0, CCM_CGCR2, 0, get_rate_ipg, NULL);
  142. DEFINE_CLOCK(pwm3_clk, 0, CCM_CGCR2, 1, get_rate_ipg, NULL);
  143. DEFINE_CLOCK(pwm4_clk, 0, CCM_CGCR2, 2, get_rate_ipg, NULL);
  144. DEFINE_CLOCK(kpp_clk, 0, CCM_CGCR1, 28, get_rate_ipg, NULL);
  145. DEFINE_CLOCK(tsc_clk, 0, CCM_CGCR2, 13, get_rate_ipg, NULL);
  146. DEFINE_CLOCK(i2c_clk, 0, CCM_CGCR0, 6, get_rate_i2c, NULL);
  147. #define _REGISTER_CLOCK(d, n, c) \
  148. { \
  149. .dev_id = d, \
  150. .con_id = n, \
  151. .clk = &c, \
  152. },
  153. static struct clk_lookup lookups[] = {
  154. _REGISTER_CLOCK("imx-uart.0", NULL, uart1_clk)
  155. _REGISTER_CLOCK("imx-uart.1", NULL, uart2_clk)
  156. _REGISTER_CLOCK("imx-uart.2", NULL, uart3_clk)
  157. _REGISTER_CLOCK("imx-uart.3", NULL, uart4_clk)
  158. _REGISTER_CLOCK("imx-uart.4", NULL, uart5_clk)
  159. _REGISTER_CLOCK("mxc-ehci.0", "usb", usbotg_clk)
  160. _REGISTER_CLOCK("mxc-ehci.1", "usb", usbotg_clk)
  161. _REGISTER_CLOCK("mxc-ehci.2", "usb", usbotg_clk)
  162. _REGISTER_CLOCK("fsl-usb2-udc", "usb", usbotg_clk)
  163. _REGISTER_CLOCK("mxc_nand.0", NULL, nfc_clk)
  164. _REGISTER_CLOCK("spi_imx.0", NULL, cspi1_clk)
  165. _REGISTER_CLOCK("spi_imx.1", NULL, cspi2_clk)
  166. _REGISTER_CLOCK("spi_imx.2", NULL, cspi3_clk)
  167. _REGISTER_CLOCK("mxc_pwm.0", NULL, pwm1_clk)
  168. _REGISTER_CLOCK("mxc_pwm.1", NULL, pwm2_clk)
  169. _REGISTER_CLOCK("mxc_pwm.2", NULL, pwm3_clk)
  170. _REGISTER_CLOCK("mxc_pwm.3", NULL, pwm4_clk)
  171. _REGISTER_CLOCK("mxc-keypad", NULL, kpp_clk)
  172. _REGISTER_CLOCK("mx25-adc", NULL, tsc_clk)
  173. _REGISTER_CLOCK("imx-i2c.0", NULL, i2c_clk)
  174. _REGISTER_CLOCK("imx-i2c.1", NULL, i2c_clk)
  175. _REGISTER_CLOCK("imx-i2c.2", NULL, i2c_clk)
  176. };
  177. int __init mx25_clocks_init(unsigned long fref)
  178. {
  179. int i;
  180. for (i = 0; i < ARRAY_SIZE(lookups); i++)
  181. clkdev_add(&lookups[i]);
  182. mxc_timer_init(&gpt_clk, MX25_IO_ADDRESS(MX25_GPT1_BASE_ADDR), 54);
  183. return 0;
  184. }