board.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * board.c
  3. *
  4. * Board functions for TI AM335X based boards
  5. *
  6. * Copyright (C) 2011, Texas Instruments, Incorporated - http://www.ti.com/
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR /PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <common.h>
  19. #include <errno.h>
  20. #include <spl.h>
  21. #include <asm/arch/cpu.h>
  22. #include <asm/arch/hardware.h>
  23. #include <asm/arch/omap.h>
  24. #include <asm/arch/ddr_defs.h>
  25. #include <asm/arch/clock.h>
  26. #include <asm/arch/gpio.h>
  27. #include <asm/arch/mmc_host_def.h>
  28. #include <asm/arch/sys_proto.h>
  29. #include <asm/io.h>
  30. #include <asm/emif.h>
  31. #include <asm/gpio.h>
  32. #include <i2c.h>
  33. #include <miiphy.h>
  34. #include <cpsw.h>
  35. #include "board.h"
  36. DECLARE_GLOBAL_DATA_PTR;
  37. static struct wd_timer *wdtimer = (struct wd_timer *)WDT_BASE;
  38. #ifdef CONFIG_SPL_BUILD
  39. static struct uart_sys *uart_base = (struct uart_sys *)DEFAULT_UART_BASE;
  40. #endif
  41. /* MII mode defines */
  42. #define MII_MODE_ENABLE 0x0
  43. #define RGMII_MODE_ENABLE 0xA
  44. /* GPIO that controls power to DDR on EVM-SK */
  45. #define GPIO_DDR_VTT_EN 7
  46. static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
  47. static struct am335x_baseboard_id __attribute__((section (".data"))) header;
  48. static inline int board_is_bone(void)
  49. {
  50. return !strncmp(header.name, "A335BONE", HDR_NAME_LEN);
  51. }
  52. static inline int board_is_bone_lt(void)
  53. {
  54. return !strncmp(header.name, "A335BNLT", HDR_NAME_LEN);
  55. }
  56. static inline int board_is_evm_sk(void)
  57. {
  58. return !strncmp("A335X_SK", header.name, HDR_NAME_LEN);
  59. }
  60. static inline int board_is_idk(void)
  61. {
  62. return !strncmp(header.config, "SKU#02", 6);
  63. }
  64. /*
  65. * Read header information from EEPROM into global structure.
  66. */
  67. static int read_eeprom(void)
  68. {
  69. /* Check if baseboard eeprom is available */
  70. if (i2c_probe(CONFIG_SYS_I2C_EEPROM_ADDR)) {
  71. puts("Could not probe the EEPROM; something fundamentally "
  72. "wrong on the I2C bus.\n");
  73. return -ENODEV;
  74. }
  75. /* read the eeprom using i2c */
  76. if (i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, 2, (uchar *)&header,
  77. sizeof(header))) {
  78. puts("Could not read the EEPROM; something fundamentally"
  79. " wrong on the I2C bus.\n");
  80. return -EIO;
  81. }
  82. if (header.magic != 0xEE3355AA) {
  83. /*
  84. * read the eeprom using i2c again,
  85. * but use only a 1 byte address
  86. */
  87. if (i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, 1,
  88. (uchar *)&header, sizeof(header))) {
  89. puts("Could not read the EEPROM; something "
  90. "fundamentally wrong on the I2C bus.\n");
  91. return -EIO;
  92. }
  93. if (header.magic != 0xEE3355AA) {
  94. printf("Incorrect magic number (0x%x) in EEPROM\n",
  95. header.magic);
  96. return -EINVAL;
  97. }
  98. }
  99. return 0;
  100. }
  101. /* UART Defines */
  102. #ifdef CONFIG_SPL_BUILD
  103. #define UART_RESET (0x1 << 1)
  104. #define UART_CLK_RUNNING_MASK 0x1
  105. #define UART_SMART_IDLE_EN (0x1 << 0x3)
  106. static void rtc32k_enable(void)
  107. {
  108. struct rtc_regs *rtc = (struct rtc_regs *)AM335X_RTC_BASE;
  109. /*
  110. * Unlock the RTC's registers. For more details please see the
  111. * RTC_SS section of the TRM. In order to unlock we need to
  112. * write these specific values (keys) in this order.
  113. */
  114. writel(0x83e70b13, &rtc->kick0r);
  115. writel(0x95a4f1e0, &rtc->kick1r);
  116. /* Enable the RTC 32K OSC by setting bits 3 and 6. */
  117. writel((1 << 3) | (1 << 6), &rtc->osc);
  118. }
  119. static const struct ddr_data ddr2_data = {
  120. .datardsratio0 = ((MT47H128M16RT25E_RD_DQS<<30) |
  121. (MT47H128M16RT25E_RD_DQS<<20) |
  122. (MT47H128M16RT25E_RD_DQS<<10) |
  123. (MT47H128M16RT25E_RD_DQS<<0)),
  124. .datawdsratio0 = ((MT47H128M16RT25E_WR_DQS<<30) |
  125. (MT47H128M16RT25E_WR_DQS<<20) |
  126. (MT47H128M16RT25E_WR_DQS<<10) |
  127. (MT47H128M16RT25E_WR_DQS<<0)),
  128. .datawiratio0 = ((MT47H128M16RT25E_PHY_WRLVL<<30) |
  129. (MT47H128M16RT25E_PHY_WRLVL<<20) |
  130. (MT47H128M16RT25E_PHY_WRLVL<<10) |
  131. (MT47H128M16RT25E_PHY_WRLVL<<0)),
  132. .datagiratio0 = ((MT47H128M16RT25E_PHY_GATELVL<<30) |
  133. (MT47H128M16RT25E_PHY_GATELVL<<20) |
  134. (MT47H128M16RT25E_PHY_GATELVL<<10) |
  135. (MT47H128M16RT25E_PHY_GATELVL<<0)),
  136. .datafwsratio0 = ((MT47H128M16RT25E_PHY_FIFO_WE<<30) |
  137. (MT47H128M16RT25E_PHY_FIFO_WE<<20) |
  138. (MT47H128M16RT25E_PHY_FIFO_WE<<10) |
  139. (MT47H128M16RT25E_PHY_FIFO_WE<<0)),
  140. .datawrsratio0 = ((MT47H128M16RT25E_PHY_WR_DATA<<30) |
  141. (MT47H128M16RT25E_PHY_WR_DATA<<20) |
  142. (MT47H128M16RT25E_PHY_WR_DATA<<10) |
  143. (MT47H128M16RT25E_PHY_WR_DATA<<0)),
  144. .datauserank0delay = MT47H128M16RT25E_PHY_RANK0_DELAY,
  145. .datadldiff0 = PHY_DLL_LOCK_DIFF,
  146. };
  147. static const struct cmd_control ddr2_cmd_ctrl_data = {
  148. .cmd0csratio = MT47H128M16RT25E_RATIO,
  149. .cmd0dldiff = MT47H128M16RT25E_DLL_LOCK_DIFF,
  150. .cmd0iclkout = MT47H128M16RT25E_INVERT_CLKOUT,
  151. .cmd1csratio = MT47H128M16RT25E_RATIO,
  152. .cmd1dldiff = MT47H128M16RT25E_DLL_LOCK_DIFF,
  153. .cmd1iclkout = MT47H128M16RT25E_INVERT_CLKOUT,
  154. .cmd2csratio = MT47H128M16RT25E_RATIO,
  155. .cmd2dldiff = MT47H128M16RT25E_DLL_LOCK_DIFF,
  156. .cmd2iclkout = MT47H128M16RT25E_INVERT_CLKOUT,
  157. };
  158. static const struct emif_regs ddr2_emif_reg_data = {
  159. .sdram_config = MT47H128M16RT25E_EMIF_SDCFG,
  160. .ref_ctrl = MT47H128M16RT25E_EMIF_SDREF,
  161. .sdram_tim1 = MT47H128M16RT25E_EMIF_TIM1,
  162. .sdram_tim2 = MT47H128M16RT25E_EMIF_TIM2,
  163. .sdram_tim3 = MT47H128M16RT25E_EMIF_TIM3,
  164. .emif_ddr_phy_ctlr_1 = MT47H128M16RT25E_EMIF_READ_LATENCY,
  165. };
  166. static const struct ddr_data ddr3_data = {
  167. .datardsratio0 = MT41J128MJT125_RD_DQS,
  168. .datawdsratio0 = MT41J128MJT125_WR_DQS,
  169. .datafwsratio0 = MT41J128MJT125_PHY_FIFO_WE,
  170. .datawrsratio0 = MT41J128MJT125_PHY_WR_DATA,
  171. .datadldiff0 = PHY_DLL_LOCK_DIFF,
  172. };
  173. static const struct cmd_control ddr3_cmd_ctrl_data = {
  174. .cmd0csratio = MT41J128MJT125_RATIO,
  175. .cmd0dldiff = MT41J128MJT125_DLL_LOCK_DIFF,
  176. .cmd0iclkout = MT41J128MJT125_INVERT_CLKOUT,
  177. .cmd1csratio = MT41J128MJT125_RATIO,
  178. .cmd1dldiff = MT41J128MJT125_DLL_LOCK_DIFF,
  179. .cmd1iclkout = MT41J128MJT125_INVERT_CLKOUT,
  180. .cmd2csratio = MT41J128MJT125_RATIO,
  181. .cmd2dldiff = MT41J128MJT125_DLL_LOCK_DIFF,
  182. .cmd2iclkout = MT41J128MJT125_INVERT_CLKOUT,
  183. };
  184. static struct emif_regs ddr3_emif_reg_data = {
  185. .sdram_config = MT41J128MJT125_EMIF_SDCFG,
  186. .ref_ctrl = MT41J128MJT125_EMIF_SDREF,
  187. .sdram_tim1 = MT41J128MJT125_EMIF_TIM1,
  188. .sdram_tim2 = MT41J128MJT125_EMIF_TIM2,
  189. .sdram_tim3 = MT41J128MJT125_EMIF_TIM3,
  190. .zq_config = MT41J128MJT125_ZQ_CFG,
  191. .emif_ddr_phy_ctlr_1 = MT41J128MJT125_EMIF_READ_LATENCY,
  192. };
  193. #endif
  194. /*
  195. * early system init of muxing and clocks.
  196. */
  197. void s_init(void)
  198. {
  199. /* WDT1 is already running when the bootloader gets control
  200. * Disable it to avoid "random" resets
  201. */
  202. writel(0xAAAA, &wdtimer->wdtwspr);
  203. while (readl(&wdtimer->wdtwwps) != 0x0)
  204. ;
  205. writel(0x5555, &wdtimer->wdtwspr);
  206. while (readl(&wdtimer->wdtwwps) != 0x0)
  207. ;
  208. #ifdef CONFIG_SPL_BUILD
  209. /* Setup the PLLs and the clocks for the peripherals */
  210. pll_init();
  211. /* Enable RTC32K clock */
  212. rtc32k_enable();
  213. /* UART softreset */
  214. u32 regVal;
  215. #ifdef CONFIG_SERIAL1
  216. enable_uart0_pin_mux();
  217. #endif /* CONFIG_SERIAL1 */
  218. #ifdef CONFIG_SERIAL2
  219. enable_uart1_pin_mux();
  220. #endif /* CONFIG_SERIAL2 */
  221. #ifdef CONFIG_SERIAL3
  222. enable_uart2_pin_mux();
  223. #endif /* CONFIG_SERIAL3 */
  224. #ifdef CONFIG_SERIAL4
  225. enable_uart3_pin_mux();
  226. #endif /* CONFIG_SERIAL4 */
  227. #ifdef CONFIG_SERIAL5
  228. enable_uart4_pin_mux();
  229. #endif /* CONFIG_SERIAL5 */
  230. #ifdef CONFIG_SERIAL6
  231. enable_uart5_pin_mux();
  232. #endif /* CONFIG_SERIAL6 */
  233. regVal = readl(&uart_base->uartsyscfg);
  234. regVal |= UART_RESET;
  235. writel(regVal, &uart_base->uartsyscfg);
  236. while ((readl(&uart_base->uartsyssts) &
  237. UART_CLK_RUNNING_MASK) != UART_CLK_RUNNING_MASK)
  238. ;
  239. /* Disable smart idle */
  240. regVal = readl(&uart_base->uartsyscfg);
  241. regVal |= UART_SMART_IDLE_EN;
  242. writel(regVal, &uart_base->uartsyscfg);
  243. gd = &gdata;
  244. preloader_console_init();
  245. /* Initalize the board header */
  246. enable_i2c0_pin_mux();
  247. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  248. if (read_eeprom() < 0)
  249. puts("Could not get board ID.\n");
  250. enable_board_pin_mux(&header);
  251. if (board_is_evm_sk()) {
  252. /*
  253. * EVM SK 1.2A and later use gpio0_7 to enable DDR3.
  254. * This is safe enough to do on older revs.
  255. */
  256. gpio_request(GPIO_DDR_VTT_EN, "ddr_vtt_en");
  257. gpio_direction_output(GPIO_DDR_VTT_EN, 1);
  258. }
  259. if (board_is_evm_sk() || board_is_bone_lt())
  260. config_ddr(303, MT41J128MJT125_IOCTRL_VALUE, &ddr3_data,
  261. &ddr3_cmd_ctrl_data, &ddr3_emif_reg_data);
  262. else
  263. config_ddr(266, MT47H128M16RT25E_IOCTRL_VALUE, &ddr2_data,
  264. &ddr2_cmd_ctrl_data, &ddr2_emif_reg_data);
  265. #endif
  266. }
  267. /*
  268. * Basic board specific setup. Pinmux has been handled already.
  269. */
  270. int board_init(void)
  271. {
  272. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  273. if (read_eeprom() < 0)
  274. puts("Could not get board ID.\n");
  275. gd->bd->bi_boot_params = PHYS_DRAM_1 + 0x100;
  276. gpmc_init();
  277. return 0;
  278. }
  279. #ifdef CONFIG_BOARD_LATE_INIT
  280. int board_late_init(void)
  281. {
  282. #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
  283. char safe_string[HDR_NAME_LEN + 1];
  284. /* Now set variables based on the header. */
  285. strncpy(safe_string, (char *)header.name, sizeof(header.name));
  286. safe_string[sizeof(header.name)] = 0;
  287. setenv("board_name", safe_string);
  288. strncpy(safe_string, (char *)header.version, sizeof(header.version));
  289. safe_string[sizeof(header.version)] = 0;
  290. setenv("board_rev", safe_string);
  291. #endif
  292. return 0;
  293. }
  294. #endif
  295. #ifdef CONFIG_DRIVER_TI_CPSW
  296. static void cpsw_control(int enabled)
  297. {
  298. /* VTP can be added here */
  299. return;
  300. }
  301. static struct cpsw_slave_data cpsw_slaves[] = {
  302. {
  303. .slave_reg_ofs = 0x208,
  304. .sliver_reg_ofs = 0xd80,
  305. .phy_id = 0,
  306. },
  307. {
  308. .slave_reg_ofs = 0x308,
  309. .sliver_reg_ofs = 0xdc0,
  310. .phy_id = 1,
  311. },
  312. };
  313. static struct cpsw_platform_data cpsw_data = {
  314. .mdio_base = AM335X_CPSW_MDIO_BASE,
  315. .cpsw_base = AM335X_CPSW_BASE,
  316. .mdio_div = 0xff,
  317. .channels = 8,
  318. .cpdma_reg_ofs = 0x800,
  319. .slaves = 1,
  320. .slave_data = cpsw_slaves,
  321. .ale_reg_ofs = 0xd00,
  322. .ale_entries = 1024,
  323. .host_port_reg_ofs = 0x108,
  324. .hw_stats_reg_ofs = 0x900,
  325. .mac_control = (1 << 5),
  326. .control = cpsw_control,
  327. .host_port_num = 0,
  328. .version = CPSW_CTRL_VERSION_2,
  329. };
  330. int board_eth_init(bd_t *bis)
  331. {
  332. uint8_t mac_addr[6];
  333. uint32_t mac_hi, mac_lo;
  334. if (!eth_getenv_enetaddr("ethaddr", mac_addr)) {
  335. debug("<ethaddr> not set. Reading from E-fuse\n");
  336. /* try reading mac address from efuse */
  337. mac_lo = readl(&cdev->macid0l);
  338. mac_hi = readl(&cdev->macid0h);
  339. mac_addr[0] = mac_hi & 0xFF;
  340. mac_addr[1] = (mac_hi & 0xFF00) >> 8;
  341. mac_addr[2] = (mac_hi & 0xFF0000) >> 16;
  342. mac_addr[3] = (mac_hi & 0xFF000000) >> 24;
  343. mac_addr[4] = mac_lo & 0xFF;
  344. mac_addr[5] = (mac_lo & 0xFF00) >> 8;
  345. if (is_valid_ether_addr(mac_addr))
  346. eth_setenv_enetaddr("ethaddr", mac_addr);
  347. else
  348. return -1;
  349. }
  350. if (board_is_bone() || board_is_bone_lt() || board_is_idk()) {
  351. writel(MII_MODE_ENABLE, &cdev->miisel);
  352. cpsw_slaves[0].phy_if = cpsw_slaves[1].phy_if =
  353. PHY_INTERFACE_MODE_MII;
  354. } else {
  355. writel(RGMII_MODE_ENABLE, &cdev->miisel);
  356. cpsw_slaves[0].phy_if = cpsw_slaves[1].phy_if =
  357. PHY_INTERFACE_MODE_RGMII;
  358. }
  359. return cpsw_register(&cpsw_data);
  360. }
  361. #endif