generic.c 8.4 KB

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