generic.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2008 Eric Jarrige <eric.jarrige@armadeus.org>
  3. * Copyright (c) 2009 Ilya Yanok <yanok@emcraft.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. * MA 02111-1307 USA
  19. */
  20. #include <common.h>
  21. #include <div64.h>
  22. #include <asm/io.h>
  23. #include <asm/arch/imx-regs.h>
  24. /*
  25. * get the system pll clock in Hz
  26. *
  27. * mfi + mfn / (mfd +1)
  28. * f = 2 * f_ref * --------------------
  29. * pd + 1
  30. */
  31. unsigned int imx_decode_pll(unsigned int pll, unsigned int f_ref)
  32. {
  33. unsigned int mfi = (pll >> 10) & 0xf;
  34. unsigned int mfn = pll & 0x3ff;
  35. unsigned int mfd = (pll >> 16) & 0x3ff;
  36. unsigned int pd = (pll >> 26) & 0xf;
  37. mfi = mfi <= 5 ? 5 : mfi;
  38. return lldiv(2 * (u64)f_ref * (mfi * (mfd + 1) + mfn),
  39. (mfd + 1) * (pd + 1));
  40. }
  41. static ulong clk_in_32k(void)
  42. {
  43. return 1024 * CONFIG_MX27_CLK32;
  44. }
  45. static ulong clk_in_26m(void)
  46. {
  47. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  48. if (readl(&pll->cscr) & CSCR_OSC26M_DIV1P5) {
  49. /* divide by 1.5 */
  50. return 26000000 * 2 / 3;
  51. } else {
  52. return 26000000;
  53. }
  54. }
  55. ulong imx_get_mpllclk(void)
  56. {
  57. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  58. ulong cscr = readl(&pll->cscr);
  59. ulong fref;
  60. if (cscr & CSCR_MCU_SEL)
  61. fref = clk_in_26m();
  62. else
  63. fref = clk_in_32k();
  64. return imx_decode_pll(readl(&pll->mpctl0), fref);
  65. }
  66. ulong imx_get_armclk(void)
  67. {
  68. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  69. ulong cscr = readl(&pll->cscr);
  70. ulong fref = imx_get_mpllclk();
  71. ulong div;
  72. if (!(cscr & CSCR_ARM_SRC_MPLL))
  73. fref = lldiv((fref * 2), 3);
  74. div = ((cscr >> 12) & 0x3) + 1;
  75. return lldiv(fref, div);
  76. }
  77. ulong imx_get_ahbclk(void)
  78. {
  79. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  80. ulong cscr = readl(&pll->cscr);
  81. ulong fref = imx_get_mpllclk();
  82. ulong div;
  83. div = ((cscr >> 8) & 0x3) + 1;
  84. return lldiv(fref * 2, 3 * div);
  85. }
  86. ulong imx_get_spllclk(void)
  87. {
  88. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  89. ulong cscr = readl(&pll->cscr);
  90. ulong fref;
  91. if (cscr & CSCR_SP_SEL)
  92. fref = clk_in_26m();
  93. else
  94. fref = clk_in_32k();
  95. return imx_decode_pll(readl(&pll->spctl0), fref);
  96. }
  97. static ulong imx_decode_perclk(ulong div)
  98. {
  99. return lldiv((imx_get_mpllclk() * 2), (div * 3));
  100. }
  101. ulong imx_get_perclk1(void)
  102. {
  103. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  104. return imx_decode_perclk((readl(&pll->pcdr1) & 0x3f) + 1);
  105. }
  106. ulong imx_get_perclk2(void)
  107. {
  108. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  109. return imx_decode_perclk(((readl(&pll->pcdr1) >> 8) & 0x3f) + 1);
  110. }
  111. ulong imx_get_perclk3(void)
  112. {
  113. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  114. return imx_decode_perclk(((readl(&pll->pcdr1) >> 16) & 0x3f) + 1);
  115. }
  116. ulong imx_get_perclk4(void)
  117. {
  118. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  119. return imx_decode_perclk(((readl(&pll->pcdr1) >> 24) & 0x3f) + 1);
  120. }
  121. #if defined(CONFIG_DISPLAY_CPUINFO)
  122. int print_cpuinfo (void)
  123. {
  124. char buf[32];
  125. printf("CPU: Freescale i.MX27 at %s MHz\n\n",
  126. strmhz(buf, imx_get_mpllclk()));
  127. return 0;
  128. }
  129. #endif
  130. void imx_gpio_mode(int gpio_mode)
  131. {
  132. struct gpio_regs *regs = (struct gpio_regs *)IMX_GPIO_BASE;
  133. unsigned int pin = gpio_mode & GPIO_PIN_MASK;
  134. unsigned int port = (gpio_mode & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT;
  135. unsigned int ocr = (gpio_mode & GPIO_OCR_MASK) >> GPIO_OCR_SHIFT;
  136. unsigned int aout = (gpio_mode & GPIO_AOUT_MASK) >> GPIO_AOUT_SHIFT;
  137. unsigned int bout = (gpio_mode & GPIO_BOUT_MASK) >> GPIO_BOUT_SHIFT;
  138. unsigned int tmp;
  139. /* Pullup enable */
  140. if (gpio_mode & GPIO_PUEN) {
  141. writel(readl(&regs->port[port].puen) | (1 << pin),
  142. &regs->port[port].puen);
  143. } else {
  144. writel(readl(&regs->port[port].puen) & ~(1 << pin),
  145. &regs->port[port].puen);
  146. }
  147. /* Data direction */
  148. if (gpio_mode & GPIO_OUT) {
  149. writel(readl(&regs->port[port].ddir) | 1 << pin,
  150. &regs->port[port].ddir);
  151. } else {
  152. writel(readl(&regs->port[port].ddir) & ~(1 << pin),
  153. &regs->port[port].ddir);
  154. }
  155. /* Primary / alternate function */
  156. if (gpio_mode & GPIO_AF) {
  157. writel(readl(&regs->port[port].gpr) | (1 << pin),
  158. &regs->port[port].gpr);
  159. } else {
  160. writel(readl(&regs->port[port].gpr) & ~(1 << pin),
  161. &regs->port[port].gpr);
  162. }
  163. /* use as gpio? */
  164. if (!(gpio_mode & (GPIO_PF | GPIO_AF))) {
  165. writel(readl(&regs->port[port].gius) | (1 << pin),
  166. &regs->port[port].gius);
  167. } else {
  168. writel(readl(&regs->port[port].gius) & ~(1 << pin),
  169. &regs->port[port].gius);
  170. }
  171. /* Output / input configuration */
  172. if (pin < 16) {
  173. tmp = readl(&regs->port[port].ocr1);
  174. tmp &= ~(3 << (pin * 2));
  175. tmp |= (ocr << (pin * 2));
  176. writel(tmp, &regs->port[port].ocr1);
  177. writel(readl(&regs->port[port].iconfa1) & ~(3 << (pin * 2)),
  178. &regs->port[port].iconfa1);
  179. writel(readl(&regs->port[port].iconfa1) | aout << (pin * 2),
  180. &regs->port[port].iconfa1);
  181. writel(readl(&regs->port[port].iconfb1) & ~(3 << (pin * 2)),
  182. &regs->port[port].iconfb1);
  183. writel(readl(&regs->port[port].iconfb1) | bout << (pin * 2),
  184. &regs->port[port].iconfb1);
  185. } else {
  186. pin -= 16;
  187. tmp = readl(&regs->port[port].ocr2);
  188. tmp &= ~(3 << (pin * 2));
  189. tmp |= (ocr << (pin * 2));
  190. writel(tmp, &regs->port[port].ocr2);
  191. writel(readl(&regs->port[port].iconfa2) & ~(3 << (pin * 2)),
  192. &regs->port[port].iconfa2);
  193. writel(readl(&regs->port[port].iconfa2) | aout << (pin * 2),
  194. &regs->port[port].iconfa2);
  195. writel(readl(&regs->port[port].iconfb2) & ~(3 << (pin * 2)),
  196. &regs->port[port].iconfb2);
  197. writel(readl(&regs->port[port].iconfb2) | bout << (pin * 2),
  198. &regs->port[port].iconfb2);
  199. }
  200. }