generic.c 9.0 KB

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