generic.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * (C) Copyright 2009 DENX Software Engineering
  3. * Author: John Rigby <jrigby@gmail.com>
  4. *
  5. * Based on mx27/generic.c:
  6. * Copyright (c) 2008 Eric Jarrige <eric.jarrige@armadeus.org>
  7. * Copyright (c) 2009 Ilya Yanok <yanok@emcraft.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <div64.h>
  26. #include <netdev.h>
  27. #include <asm/io.h>
  28. #include <asm/arch/imx-regs.h>
  29. #include <asm/arch/imx25-pinmux.h>
  30. #include <asm/arch/clock.h>
  31. #ifdef CONFIG_FSL_ESDHC
  32. #include <fsl_esdhc.h>
  33. DECLARE_GLOBAL_DATA_PTR;
  34. #endif
  35. /*
  36. * get the system pll clock in Hz
  37. *
  38. * mfi + mfn / (mfd +1)
  39. * f = 2 * f_ref * --------------------
  40. * pd + 1
  41. */
  42. static unsigned int imx_decode_pll(unsigned int pll, unsigned int f_ref)
  43. {
  44. unsigned int mfi = (pll >> CCM_PLL_MFI_SHIFT)
  45. & CCM_PLL_MFI_MASK;
  46. int mfn = (pll >> CCM_PLL_MFN_SHIFT)
  47. & CCM_PLL_MFN_MASK;
  48. unsigned int mfd = (pll >> CCM_PLL_MFD_SHIFT)
  49. & CCM_PLL_MFD_MASK;
  50. unsigned int pd = (pll >> CCM_PLL_PD_SHIFT)
  51. & CCM_PLL_PD_MASK;
  52. mfi = mfi <= 5 ? 5 : mfi;
  53. mfn = mfn >= 512 ? mfn - 1024 : mfn;
  54. mfd += 1;
  55. pd += 1;
  56. return lldiv(2 * (u64) f_ref * (mfi * mfd + mfn),
  57. mfd * pd);
  58. }
  59. static ulong imx_get_mpllclk(void)
  60. {
  61. struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
  62. ulong fref = MXC_HCLK;
  63. return imx_decode_pll(readl(&ccm->mpctl), fref);
  64. }
  65. static ulong imx_get_armclk(void)
  66. {
  67. struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
  68. ulong cctl = readl(&ccm->cctl);
  69. ulong fref = imx_get_mpllclk();
  70. ulong div;
  71. if (cctl & CCM_CCTL_ARM_SRC)
  72. fref = lldiv((u64) fref * 3, 4);
  73. div = ((cctl >> CCM_CCTL_ARM_DIV_SHIFT)
  74. & CCM_CCTL_ARM_DIV_MASK) + 1;
  75. return fref / div;
  76. }
  77. static ulong imx_get_ahbclk(void)
  78. {
  79. struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
  80. ulong cctl = readl(&ccm->cctl);
  81. ulong fref = imx_get_armclk();
  82. ulong div;
  83. div = ((cctl >> CCM_CCTL_AHB_DIV_SHIFT)
  84. & CCM_CCTL_AHB_DIV_MASK) + 1;
  85. return fref / div;
  86. }
  87. static ulong imx_get_ipgclk(void)
  88. {
  89. return imx_get_ahbclk() / 2;
  90. }
  91. static ulong imx_get_perclk(int clk)
  92. {
  93. struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
  94. ulong fref = imx_get_ahbclk();
  95. ulong div;
  96. div = readl(&ccm->pcdr[CCM_PERCLK_REG(clk)]);
  97. div = ((div >> CCM_PERCLK_SHIFT(clk)) & CCM_PERCLK_MASK) + 1;
  98. return fref / div;
  99. }
  100. unsigned int mxc_get_clock(enum mxc_clock clk)
  101. {
  102. if (clk >= MXC_CLK_NUM)
  103. return -1;
  104. switch (clk) {
  105. case MXC_ARM_CLK:
  106. return imx_get_armclk();
  107. case MXC_AHB_CLK:
  108. return imx_get_ahbclk();
  109. case MXC_IPG_CLK:
  110. case MXC_CSPI_CLK:
  111. case MXC_FEC_CLK:
  112. return imx_get_ipgclk();
  113. default:
  114. return imx_get_perclk(clk);
  115. }
  116. }
  117. u32 get_cpu_rev(void)
  118. {
  119. u32 srev;
  120. u32 system_rev = 0x25000;
  121. /* read SREV register from IIM module */
  122. struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE;
  123. srev = readl(&iim->iim_srev);
  124. switch (srev) {
  125. case 0x00:
  126. system_rev |= CHIP_REV_1_0;
  127. break;
  128. case 0x01:
  129. system_rev |= CHIP_REV_1_1;
  130. break;
  131. case 0x02:
  132. system_rev |= CHIP_REV_1_2;
  133. break;
  134. default:
  135. system_rev |= 0x8000;
  136. break;
  137. }
  138. return system_rev;
  139. }
  140. #if defined(CONFIG_DISPLAY_CPUINFO)
  141. static char *get_reset_cause(void)
  142. {
  143. /* read RCSR register from CCM module */
  144. struct ccm_regs *ccm =
  145. (struct ccm_regs *)IMX_CCM_BASE;
  146. u32 cause = readl(&ccm->rcsr) & 0x0f;
  147. if (cause == 0)
  148. return "POR";
  149. else if (cause == 1)
  150. return "RST";
  151. else if ((cause & 2) == 2)
  152. return "WDOG";
  153. else if ((cause & 4) == 4)
  154. return "SW RESET";
  155. else if ((cause & 8) == 8)
  156. return "JTAG";
  157. else
  158. return "unknown reset";
  159. }
  160. int print_cpuinfo(void)
  161. {
  162. char buf[32];
  163. u32 cpurev = get_cpu_rev();
  164. printf("CPU: Freescale i.MX25 rev%d.%d%s at %s MHz\n",
  165. (cpurev & 0xF0) >> 4, (cpurev & 0x0F),
  166. ((cpurev & 0x8000) ? " unknown" : ""),
  167. strmhz(buf, imx_get_armclk()));
  168. printf("Reset cause: %s\n\n", get_reset_cause());
  169. return 0;
  170. }
  171. #endif
  172. void enable_caches(void)
  173. {
  174. #ifndef CONFIG_SYS_DCACHE_OFF
  175. /* Enable D-cache. I-cache is already enabled in start.S */
  176. dcache_enable();
  177. #endif
  178. }
  179. #if defined(CONFIG_FEC_MXC)
  180. /*
  181. * Initializes on-chip ethernet controllers.
  182. * to override, implement board_eth_init()
  183. */
  184. int cpu_eth_init(bd_t *bis)
  185. {
  186. struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
  187. ulong val;
  188. val = readl(&ccm->cgr0);
  189. val |= (1 << 23);
  190. writel(val, &ccm->cgr0);
  191. return fecmxc_initialize(bis);
  192. }
  193. #endif
  194. int get_clocks(void)
  195. {
  196. #ifdef CONFIG_FSL_ESDHC
  197. #if CONFIG_SYS_FSL_ESDHC_ADDR == IMX_MMC_SDHC2_BASE
  198. gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC2_CLK);
  199. #else
  200. gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC1_CLK);
  201. #endif
  202. #endif
  203. return 0;
  204. }
  205. #ifdef CONFIG_FSL_ESDHC
  206. /*
  207. * Initializes on-chip MMC controllers.
  208. * to override, implement board_mmc_init()
  209. */
  210. int cpu_mmc_init(bd_t *bis)
  211. {
  212. return fsl_esdhc_mmc_init(bis);
  213. }
  214. #endif
  215. #ifdef CONFIG_MXC_UART
  216. void mx25_uart1_init_pins(void)
  217. {
  218. struct iomuxc_mux_ctl *muxctl;
  219. struct iomuxc_pad_ctl *padctl;
  220. u32 inpadctl;
  221. u32 outpadctl;
  222. u32 muxmode0;
  223. muxctl = (struct iomuxc_mux_ctl *)IMX_IOPADMUX_BASE;
  224. padctl = (struct iomuxc_pad_ctl *)IMX_IOPADCTL_BASE;
  225. muxmode0 = MX25_PIN_MUX_MODE(0);
  226. /*
  227. * set up input pins with hysteresis and 100K pull-ups
  228. */
  229. inpadctl = MX25_PIN_PAD_CTL_HYS
  230. | MX25_PIN_PAD_CTL_PKE
  231. | MX25_PIN_PAD_CTL_PUE | MX25_PIN_PAD_CTL_100K_PU;
  232. /*
  233. * set up output pins with 100K pull-downs
  234. * FIXME: need to revisit this
  235. * PUE is ignored if PKE is not set
  236. * so the right value here is likely
  237. * 0x0 for no pull up/down
  238. * or
  239. * 0xc0 for 100k pull down
  240. */
  241. outpadctl = MX25_PIN_PAD_CTL_PUE | MX25_PIN_PAD_CTL_100K_PD;
  242. /* UART1 */
  243. /* rxd */
  244. writel(muxmode0, &muxctl->pad_uart1_rxd);
  245. writel(inpadctl, &padctl->pad_uart1_rxd);
  246. /* txd */
  247. writel(muxmode0, &muxctl->pad_uart1_txd);
  248. writel(outpadctl, &padctl->pad_uart1_txd);
  249. /* rts */
  250. writel(muxmode0, &muxctl->pad_uart1_rts);
  251. writel(outpadctl, &padctl->pad_uart1_rts);
  252. /* cts */
  253. writel(muxmode0, &muxctl->pad_uart1_cts);
  254. writel(inpadctl, &padctl->pad_uart1_cts);
  255. }
  256. #endif /* CONFIG_MXC_UART */
  257. #ifdef CONFIG_FEC_MXC
  258. void mx25_fec_init_pins(void)
  259. {
  260. struct iomuxc_mux_ctl *muxctl;
  261. struct iomuxc_pad_ctl *padctl;
  262. u32 inpadctl_100kpd;
  263. u32 inpadctl_22kpu;
  264. u32 outpadctl;
  265. u32 muxmode0;
  266. muxctl = (struct iomuxc_mux_ctl *)IMX_IOPADMUX_BASE;
  267. padctl = (struct iomuxc_pad_ctl *)IMX_IOPADCTL_BASE;
  268. muxmode0 = MX25_PIN_MUX_MODE(0);
  269. inpadctl_100kpd = MX25_PIN_PAD_CTL_HYS
  270. | MX25_PIN_PAD_CTL_PKE
  271. | MX25_PIN_PAD_CTL_PUE | MX25_PIN_PAD_CTL_100K_PD;
  272. inpadctl_22kpu = MX25_PIN_PAD_CTL_HYS
  273. | MX25_PIN_PAD_CTL_PKE
  274. | MX25_PIN_PAD_CTL_PUE | MX25_PIN_PAD_CTL_22K_PU;
  275. /*
  276. * set up output pins with 100K pull-downs
  277. * FIXME: need to revisit this
  278. * PUE is ignored if PKE is not set
  279. * so the right value here is likely
  280. * 0x0 for no pull
  281. * or
  282. * 0xc0 for 100k pull down
  283. */
  284. outpadctl = MX25_PIN_PAD_CTL_PUE | MX25_PIN_PAD_CTL_100K_PD;
  285. /* FEC_TX_CLK */
  286. writel(muxmode0, &muxctl->pad_fec_tx_clk);
  287. writel(inpadctl_100kpd, &padctl->pad_fec_tx_clk);
  288. /* FEC_RX_DV */
  289. writel(muxmode0, &muxctl->pad_fec_rx_dv);
  290. writel(inpadctl_100kpd, &padctl->pad_fec_rx_dv);
  291. /* FEC_RDATA0 */
  292. writel(muxmode0, &muxctl->pad_fec_rdata0);
  293. writel(inpadctl_100kpd, &padctl->pad_fec_rdata0);
  294. /* FEC_TDATA0 */
  295. writel(muxmode0, &muxctl->pad_fec_tdata0);
  296. writel(outpadctl, &padctl->pad_fec_tdata0);
  297. /* FEC_TX_EN */
  298. writel(muxmode0, &muxctl->pad_fec_tx_en);
  299. writel(outpadctl, &padctl->pad_fec_tx_en);
  300. /* FEC_MDC */
  301. writel(muxmode0, &muxctl->pad_fec_mdc);
  302. writel(outpadctl, &padctl->pad_fec_mdc);
  303. /* FEC_MDIO */
  304. writel(muxmode0, &muxctl->pad_fec_mdio);
  305. writel(inpadctl_22kpu, &padctl->pad_fec_mdio);
  306. /* FEC_RDATA1 */
  307. writel(muxmode0, &muxctl->pad_fec_rdata1);
  308. writel(inpadctl_100kpd, &padctl->pad_fec_rdata1);
  309. /* FEC_TDATA1 */
  310. writel(muxmode0, &muxctl->pad_fec_tdata1);
  311. writel(outpadctl, &padctl->pad_fec_tdata1);
  312. }
  313. void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
  314. {
  315. int i;
  316. struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE;
  317. struct fuse_bank *bank = &iim->bank[0];
  318. struct fuse_bank0_regs *fuse =
  319. (struct fuse_bank0_regs *)bank->fuse_regs;
  320. for (i = 0; i < 6; i++)
  321. mac[i] = readl(&fuse->mac_addr[i]) & 0xff;
  322. }
  323. #endif /* CONFIG_FEC_MXC */