generic.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #ifdef CONFIG_MXC_MMC
  26. #include <asm/arch/mxcmmc.h>
  27. #endif
  28. /*
  29. * get the system pll clock in Hz
  30. *
  31. * mfi + mfn / (mfd +1)
  32. * f = 2 * f_ref * --------------------
  33. * pd + 1
  34. */
  35. unsigned int imx_decode_pll(unsigned int pll, unsigned int f_ref)
  36. {
  37. unsigned int mfi = (pll >> 10) & 0xf;
  38. unsigned int mfn = pll & 0x3ff;
  39. unsigned int mfd = (pll >> 16) & 0x3ff;
  40. unsigned int pd = (pll >> 26) & 0xf;
  41. mfi = mfi <= 5 ? 5 : mfi;
  42. return lldiv(2 * (u64)f_ref * (mfi * (mfd + 1) + mfn),
  43. (mfd + 1) * (pd + 1));
  44. }
  45. static ulong clk_in_32k(void)
  46. {
  47. return 1024 * CONFIG_MX27_CLK32;
  48. }
  49. static ulong clk_in_26m(void)
  50. {
  51. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  52. if (readl(&pll->cscr) & CSCR_OSC26M_DIV1P5) {
  53. /* divide by 1.5 */
  54. return 26000000 * 2 / 3;
  55. } else {
  56. return 26000000;
  57. }
  58. }
  59. ulong imx_get_mpllclk(void)
  60. {
  61. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  62. ulong cscr = readl(&pll->cscr);
  63. ulong fref;
  64. if (cscr & CSCR_MCU_SEL)
  65. fref = clk_in_26m();
  66. else
  67. fref = clk_in_32k();
  68. return imx_decode_pll(readl(&pll->mpctl0), fref);
  69. }
  70. ulong imx_get_armclk(void)
  71. {
  72. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  73. ulong cscr = readl(&pll->cscr);
  74. ulong fref = imx_get_mpllclk();
  75. ulong div;
  76. if (!(cscr & CSCR_ARM_SRC_MPLL))
  77. fref = lldiv((fref * 2), 3);
  78. div = ((cscr >> 12) & 0x3) + 1;
  79. return lldiv(fref, div);
  80. }
  81. ulong imx_get_ahbclk(void)
  82. {
  83. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  84. ulong cscr = readl(&pll->cscr);
  85. ulong fref = imx_get_mpllclk();
  86. ulong div;
  87. div = ((cscr >> 8) & 0x3) + 1;
  88. return lldiv(fref * 2, 3 * div);
  89. }
  90. ulong imx_get_spllclk(void)
  91. {
  92. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  93. ulong cscr = readl(&pll->cscr);
  94. ulong fref;
  95. if (cscr & CSCR_SP_SEL)
  96. fref = clk_in_26m();
  97. else
  98. fref = clk_in_32k();
  99. return imx_decode_pll(readl(&pll->spctl0), fref);
  100. }
  101. static ulong imx_decode_perclk(ulong div)
  102. {
  103. return lldiv((imx_get_mpllclk() * 2), (div * 3));
  104. }
  105. ulong imx_get_perclk1(void)
  106. {
  107. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  108. return imx_decode_perclk((readl(&pll->pcdr1) & 0x3f) + 1);
  109. }
  110. ulong imx_get_perclk2(void)
  111. {
  112. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  113. return imx_decode_perclk(((readl(&pll->pcdr1) >> 8) & 0x3f) + 1);
  114. }
  115. ulong imx_get_perclk3(void)
  116. {
  117. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  118. return imx_decode_perclk(((readl(&pll->pcdr1) >> 16) & 0x3f) + 1);
  119. }
  120. ulong imx_get_perclk4(void)
  121. {
  122. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  123. return imx_decode_perclk(((readl(&pll->pcdr1) >> 24) & 0x3f) + 1);
  124. }
  125. #if defined(CONFIG_DISPLAY_CPUINFO)
  126. int print_cpuinfo (void)
  127. {
  128. char buf[32];
  129. printf("CPU: Freescale i.MX27 at %s MHz\n\n",
  130. strmhz(buf, imx_get_mpllclk()));
  131. return 0;
  132. }
  133. #endif
  134. int cpu_eth_init(bd_t *bis)
  135. {
  136. #if defined(CONFIG_FEC_MXC)
  137. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  138. /* enable FEC clock */
  139. writel(readl(&pll->pccr1) | PCCR1_HCLK_FEC, &pll->pccr1);
  140. writel(readl(&pll->pccr0) | PCCR0_FEC_EN, &pll->pccr0);
  141. return fecmxc_initialize(bis);
  142. #else
  143. return 0;
  144. #endif
  145. }
  146. /*
  147. * Initializes on-chip MMC controllers.
  148. * to override, implement board_mmc_init()
  149. */
  150. int cpu_mmc_init(bd_t *bis)
  151. {
  152. #ifdef CONFIG_MXC_MMC
  153. return mxc_mmc_init(bis);
  154. #else
  155. return 0;
  156. #endif
  157. }
  158. void imx_gpio_mode(int gpio_mode)
  159. {
  160. struct gpio_regs *regs = (struct gpio_regs *)IMX_GPIO_BASE;
  161. unsigned int pin = gpio_mode & GPIO_PIN_MASK;
  162. unsigned int port = (gpio_mode & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT;
  163. unsigned int ocr = (gpio_mode & GPIO_OCR_MASK) >> GPIO_OCR_SHIFT;
  164. unsigned int aout = (gpio_mode & GPIO_AOUT_MASK) >> GPIO_AOUT_SHIFT;
  165. unsigned int bout = (gpio_mode & GPIO_BOUT_MASK) >> GPIO_BOUT_SHIFT;
  166. unsigned int tmp;
  167. /* Pullup enable */
  168. if (gpio_mode & GPIO_PUEN) {
  169. writel(readl(&regs->port[port].puen) | (1 << pin),
  170. &regs->port[port].puen);
  171. } else {
  172. writel(readl(&regs->port[port].puen) & ~(1 << pin),
  173. &regs->port[port].puen);
  174. }
  175. /* Data direction */
  176. if (gpio_mode & GPIO_OUT) {
  177. writel(readl(&regs->port[port].ddir) | 1 << pin,
  178. &regs->port[port].ddir);
  179. } else {
  180. writel(readl(&regs->port[port].ddir) & ~(1 << pin),
  181. &regs->port[port].ddir);
  182. }
  183. /* Primary / alternate function */
  184. if (gpio_mode & GPIO_AF) {
  185. writel(readl(&regs->port[port].gpr) | (1 << pin),
  186. &regs->port[port].gpr);
  187. } else {
  188. writel(readl(&regs->port[port].gpr) & ~(1 << pin),
  189. &regs->port[port].gpr);
  190. }
  191. /* use as gpio? */
  192. if (!(gpio_mode & (GPIO_PF | GPIO_AF))) {
  193. writel(readl(&regs->port[port].gius) | (1 << pin),
  194. &regs->port[port].gius);
  195. } else {
  196. writel(readl(&regs->port[port].gius) & ~(1 << pin),
  197. &regs->port[port].gius);
  198. }
  199. /* Output / input configuration */
  200. if (pin < 16) {
  201. tmp = readl(&regs->port[port].ocr1);
  202. tmp &= ~(3 << (pin * 2));
  203. tmp |= (ocr << (pin * 2));
  204. writel(tmp, &regs->port[port].ocr1);
  205. writel(readl(&regs->port[port].iconfa1) & ~(3 << (pin * 2)),
  206. &regs->port[port].iconfa1);
  207. writel(readl(&regs->port[port].iconfa1) | aout << (pin * 2),
  208. &regs->port[port].iconfa1);
  209. writel(readl(&regs->port[port].iconfb1) & ~(3 << (pin * 2)),
  210. &regs->port[port].iconfb1);
  211. writel(readl(&regs->port[port].iconfb1) | bout << (pin * 2),
  212. &regs->port[port].iconfb1);
  213. } else {
  214. pin -= 16;
  215. tmp = readl(&regs->port[port].ocr2);
  216. tmp &= ~(3 << (pin * 2));
  217. tmp |= (ocr << (pin * 2));
  218. writel(tmp, &regs->port[port].ocr2);
  219. writel(readl(&regs->port[port].iconfa2) & ~(3 << (pin * 2)),
  220. &regs->port[port].iconfa2);
  221. writel(readl(&regs->port[port].iconfa2) | aout << (pin * 2),
  222. &regs->port[port].iconfa2);
  223. writel(readl(&regs->port[port].iconfb2) & ~(3 << (pin * 2)),
  224. &regs->port[port].iconfb2);
  225. writel(readl(&regs->port[port].iconfb2) | bout << (pin * 2),
  226. &regs->port[port].iconfb2);
  227. }
  228. }
  229. #ifdef CONFIG_MXC_UART
  230. void mx27_uart_init_pins(void)
  231. {
  232. int i;
  233. unsigned int mode[] = {
  234. PE12_PF_UART1_TXD,
  235. PE13_PF_UART1_RXD,
  236. };
  237. for (i = 0; i < ARRAY_SIZE(mode); i++)
  238. imx_gpio_mode(mode[i]);
  239. }
  240. #endif /* CONFIG_MXC_UART */
  241. #ifdef CONFIG_FEC_MXC
  242. void mx27_fec_init_pins(void)
  243. {
  244. int i;
  245. unsigned int mode[] = {
  246. PD0_AIN_FEC_TXD0,
  247. PD1_AIN_FEC_TXD1,
  248. PD2_AIN_FEC_TXD2,
  249. PD3_AIN_FEC_TXD3,
  250. PD4_AOUT_FEC_RX_ER,
  251. PD5_AOUT_FEC_RXD1,
  252. PD6_AOUT_FEC_RXD2,
  253. PD7_AOUT_FEC_RXD3,
  254. PD8_AF_FEC_MDIO,
  255. PD9_AIN_FEC_MDC | GPIO_PUEN,
  256. PD10_AOUT_FEC_CRS,
  257. PD11_AOUT_FEC_TX_CLK,
  258. PD12_AOUT_FEC_RXD0,
  259. PD13_AOUT_FEC_RX_DV,
  260. PD14_AOUT_FEC_CLR,
  261. PD15_AOUT_FEC_COL,
  262. PD16_AIN_FEC_TX_ER,
  263. PF23_AIN_FEC_TX_EN,
  264. };
  265. for (i = 0; i < ARRAY_SIZE(mode); i++)
  266. imx_gpio_mode(mode[i]);
  267. }
  268. #endif /* CONFIG_FEC_MXC */
  269. #ifdef CONFIG_MXC_MMC
  270. void mx27_sd1_init_pins(void)
  271. {
  272. int i;
  273. unsigned int mode[] = {
  274. PE18_PF_SD1_D0,
  275. PE19_PF_SD1_D1,
  276. PE20_PF_SD1_D2,
  277. PE21_PF_SD1_D3,
  278. PE22_PF_SD1_CMD,
  279. PE23_PF_SD1_CLK,
  280. };
  281. for (i = 0; i < ARRAY_SIZE(mode); i++)
  282. imx_gpio_mode(mode[i]);
  283. }
  284. void mx27_sd2_init_pins(void)
  285. {
  286. int i;
  287. unsigned int mode[] = {
  288. PB4_PF_SD2_D0,
  289. PB5_PF_SD2_D1,
  290. PB6_PF_SD2_D2,
  291. PB7_PF_SD2_D3,
  292. PB8_PF_SD2_CMD,
  293. PB9_PF_SD2_CLK,
  294. };
  295. for (i = 0; i < ARRAY_SIZE(mode); i++)
  296. imx_gpio_mode(mode[i]);
  297. }
  298. #endif /* CONFIG_MXC_MMC */