generic.c 8.4 KB

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