generic.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 <netdev.h>
  23. #include <asm/io.h>
  24. #include <asm/arch/imx-regs.h>
  25. /*
  26. * get the system pll clock in Hz
  27. *
  28. * mfi + mfn / (mfd +1)
  29. * f = 2 * f_ref * --------------------
  30. * pd + 1
  31. */
  32. unsigned int imx_decode_pll(unsigned int pll, unsigned int f_ref)
  33. {
  34. unsigned int mfi = (pll >> 10) & 0xf;
  35. unsigned int mfn = pll & 0x3ff;
  36. unsigned int mfd = (pll >> 16) & 0x3ff;
  37. unsigned int pd = (pll >> 26) & 0xf;
  38. mfi = mfi <= 5 ? 5 : mfi;
  39. return lldiv(2 * (u64)f_ref * (mfi * (mfd + 1) + mfn),
  40. (mfd + 1) * (pd + 1));
  41. }
  42. static ulong clk_in_32k(void)
  43. {
  44. return 1024 * CONFIG_MX27_CLK32;
  45. }
  46. static ulong clk_in_26m(void)
  47. {
  48. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  49. if (readl(&pll->cscr) & CSCR_OSC26M_DIV1P5) {
  50. /* divide by 1.5 */
  51. return 26000000 * 2 / 3;
  52. } else {
  53. return 26000000;
  54. }
  55. }
  56. ulong imx_get_mpllclk(void)
  57. {
  58. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  59. ulong cscr = readl(&pll->cscr);
  60. ulong fref;
  61. if (cscr & CSCR_MCU_SEL)
  62. fref = clk_in_26m();
  63. else
  64. fref = clk_in_32k();
  65. return imx_decode_pll(readl(&pll->mpctl0), fref);
  66. }
  67. ulong imx_get_armclk(void)
  68. {
  69. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  70. ulong cscr = readl(&pll->cscr);
  71. ulong fref = imx_get_mpllclk();
  72. ulong div;
  73. if (!(cscr & CSCR_ARM_SRC_MPLL))
  74. fref = lldiv((fref * 2), 3);
  75. div = ((cscr >> 12) & 0x3) + 1;
  76. return lldiv(fref, div);
  77. }
  78. ulong imx_get_ahbclk(void)
  79. {
  80. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  81. ulong cscr = readl(&pll->cscr);
  82. ulong fref = imx_get_mpllclk();
  83. ulong div;
  84. div = ((cscr >> 8) & 0x3) + 1;
  85. return lldiv(fref * 2, 3 * div);
  86. }
  87. ulong imx_get_spllclk(void)
  88. {
  89. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  90. ulong cscr = readl(&pll->cscr);
  91. ulong fref;
  92. if (cscr & CSCR_SP_SEL)
  93. fref = clk_in_26m();
  94. else
  95. fref = clk_in_32k();
  96. return imx_decode_pll(readl(&pll->spctl0), fref);
  97. }
  98. static ulong imx_decode_perclk(ulong div)
  99. {
  100. return lldiv((imx_get_mpllclk() * 2), (div * 3));
  101. }
  102. ulong imx_get_perclk1(void)
  103. {
  104. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  105. return imx_decode_perclk((readl(&pll->pcdr1) & 0x3f) + 1);
  106. }
  107. ulong imx_get_perclk2(void)
  108. {
  109. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  110. return imx_decode_perclk(((readl(&pll->pcdr1) >> 8) & 0x3f) + 1);
  111. }
  112. ulong imx_get_perclk3(void)
  113. {
  114. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  115. return imx_decode_perclk(((readl(&pll->pcdr1) >> 16) & 0x3f) + 1);
  116. }
  117. ulong imx_get_perclk4(void)
  118. {
  119. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  120. return imx_decode_perclk(((readl(&pll->pcdr1) >> 24) & 0x3f) + 1);
  121. }
  122. #if defined(CONFIG_DISPLAY_CPUINFO)
  123. int print_cpuinfo (void)
  124. {
  125. char buf[32];
  126. printf("CPU: Freescale i.MX27 at %s MHz\n\n",
  127. strmhz(buf, imx_get_mpllclk()));
  128. return 0;
  129. }
  130. #endif
  131. int cpu_eth_init(bd_t *bis)
  132. {
  133. #if defined(CONFIG_FEC_MXC)
  134. return fecmxc_initialize(bis);
  135. #else
  136. return 0;
  137. #endif
  138. }
  139. void imx_gpio_mode(int gpio_mode)
  140. {
  141. struct gpio_regs *regs = (struct gpio_regs *)IMX_GPIO_BASE;
  142. unsigned int pin = gpio_mode & GPIO_PIN_MASK;
  143. unsigned int port = (gpio_mode & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT;
  144. unsigned int ocr = (gpio_mode & GPIO_OCR_MASK) >> GPIO_OCR_SHIFT;
  145. unsigned int aout = (gpio_mode & GPIO_AOUT_MASK) >> GPIO_AOUT_SHIFT;
  146. unsigned int bout = (gpio_mode & GPIO_BOUT_MASK) >> GPIO_BOUT_SHIFT;
  147. unsigned int tmp;
  148. /* Pullup enable */
  149. if (gpio_mode & GPIO_PUEN) {
  150. writel(readl(&regs->port[port].puen) | (1 << pin),
  151. &regs->port[port].puen);
  152. } else {
  153. writel(readl(&regs->port[port].puen) & ~(1 << pin),
  154. &regs->port[port].puen);
  155. }
  156. /* Data direction */
  157. if (gpio_mode & GPIO_OUT) {
  158. writel(readl(&regs->port[port].ddir) | 1 << pin,
  159. &regs->port[port].ddir);
  160. } else {
  161. writel(readl(&regs->port[port].ddir) & ~(1 << pin),
  162. &regs->port[port].ddir);
  163. }
  164. /* Primary / alternate function */
  165. if (gpio_mode & GPIO_AF) {
  166. writel(readl(&regs->port[port].gpr) | (1 << pin),
  167. &regs->port[port].gpr);
  168. } else {
  169. writel(readl(&regs->port[port].gpr) & ~(1 << pin),
  170. &regs->port[port].gpr);
  171. }
  172. /* use as gpio? */
  173. if (!(gpio_mode & (GPIO_PF | GPIO_AF))) {
  174. writel(readl(&regs->port[port].gius) | (1 << pin),
  175. &regs->port[port].gius);
  176. } else {
  177. writel(readl(&regs->port[port].gius) & ~(1 << pin),
  178. &regs->port[port].gius);
  179. }
  180. /* Output / input configuration */
  181. if (pin < 16) {
  182. tmp = readl(&regs->port[port].ocr1);
  183. tmp &= ~(3 << (pin * 2));
  184. tmp |= (ocr << (pin * 2));
  185. writel(tmp, &regs->port[port].ocr1);
  186. writel(readl(&regs->port[port].iconfa1) & ~(3 << (pin * 2)),
  187. &regs->port[port].iconfa1);
  188. writel(readl(&regs->port[port].iconfa1) | aout << (pin * 2),
  189. &regs->port[port].iconfa1);
  190. writel(readl(&regs->port[port].iconfb1) & ~(3 << (pin * 2)),
  191. &regs->port[port].iconfb1);
  192. writel(readl(&regs->port[port].iconfb1) | bout << (pin * 2),
  193. &regs->port[port].iconfb1);
  194. } else {
  195. pin -= 16;
  196. tmp = readl(&regs->port[port].ocr2);
  197. tmp &= ~(3 << (pin * 2));
  198. tmp |= (ocr << (pin * 2));
  199. writel(tmp, &regs->port[port].ocr2);
  200. writel(readl(&regs->port[port].iconfa2) & ~(3 << (pin * 2)),
  201. &regs->port[port].iconfa2);
  202. writel(readl(&regs->port[port].iconfa2) | aout << (pin * 2),
  203. &regs->port[port].iconfa2);
  204. writel(readl(&regs->port[port].iconfb2) & ~(3 << (pin * 2)),
  205. &regs->port[port].iconfb2);
  206. writel(readl(&regs->port[port].iconfb2) | bout << (pin * 2),
  207. &regs->port[port].iconfb2);
  208. }
  209. }