board.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. /*
  61. * Read header information from EEPROM into global structure.
  62. */
  63. static int read_eeprom(void)
  64. {
  65. /* Check if baseboard eeprom is available */
  66. if (i2c_probe(CONFIG_SYS_I2C_EEPROM_ADDR)) {
  67. puts("Could not probe the EEPROM; something fundamentally "
  68. "wrong on the I2C bus.\n");
  69. return -ENODEV;
  70. }
  71. /* read the eeprom using i2c */
  72. if (i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, 2, (uchar *)&header,
  73. sizeof(header))) {
  74. puts("Could not read the EEPROM; something fundamentally"
  75. " wrong on the I2C bus.\n");
  76. return -EIO;
  77. }
  78. if (header.magic != 0xEE3355AA) {
  79. /*
  80. * read the eeprom using i2c again,
  81. * but use only a 1 byte address
  82. */
  83. if (i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, 1,
  84. (uchar *)&header, sizeof(header))) {
  85. puts("Could not read the EEPROM; something "
  86. "fundamentally wrong on the I2C bus.\n");
  87. return -EIO;
  88. }
  89. if (header.magic != 0xEE3355AA) {
  90. printf("Incorrect magic number (0x%x) in EEPROM\n",
  91. header.magic);
  92. return -EINVAL;
  93. }
  94. }
  95. return 0;
  96. }
  97. /* UART Defines */
  98. #ifdef CONFIG_SPL_BUILD
  99. #define UART_RESET (0x1 << 1)
  100. #define UART_CLK_RUNNING_MASK 0x1
  101. #define UART_SMART_IDLE_EN (0x1 << 0x3)
  102. static void rtc32k_enable(void)
  103. {
  104. struct rtc_regs *rtc = (struct rtc_regs *)AM335X_RTC_BASE;
  105. /*
  106. * Unlock the RTC's registers. For more details please see the
  107. * RTC_SS section of the TRM. In order to unlock we need to
  108. * write these specific values (keys) in this order.
  109. */
  110. writel(0x83e70b13, &rtc->kick0r);
  111. writel(0x95a4f1e0, &rtc->kick1r);
  112. /* Enable the RTC 32K OSC by setting bits 3 and 6. */
  113. writel((1 << 3) | (1 << 6), &rtc->osc);
  114. }
  115. static const struct ddr_data ddr2_data = {
  116. .datardsratio0 = ((DDR2_RD_DQS<<30)|(DDR2_RD_DQS<<20)
  117. |(DDR2_RD_DQS<<10)|(DDR2_RD_DQS<<0)),
  118. .datawdsratio0 = ((DDR2_WR_DQS<<30)|(DDR2_WR_DQS<<20)
  119. |(DDR2_WR_DQS<<10)|(DDR2_WR_DQS<<0)),
  120. .datawiratio0 = ((DDR2_PHY_WRLVL<<30)|(DDR2_PHY_WRLVL<<20)
  121. |(DDR2_PHY_WRLVL<<10)|(DDR2_PHY_WRLVL<<0)),
  122. .datagiratio0 = ((DDR2_PHY_GATELVL<<30)|(DDR2_PHY_GATELVL<<20)
  123. |(DDR2_PHY_GATELVL<<10)|(DDR2_PHY_GATELVL<<0)),
  124. .datafwsratio0 = ((DDR2_PHY_FIFO_WE<<30)|(DDR2_PHY_FIFO_WE<<20)
  125. |(DDR2_PHY_FIFO_WE<<10)|(DDR2_PHY_FIFO_WE<<0)),
  126. .datawrsratio0 = ((DDR2_PHY_WR_DATA<<30)|(DDR2_PHY_WR_DATA<<20)
  127. |(DDR2_PHY_WR_DATA<<10)|(DDR2_PHY_WR_DATA<<0)),
  128. .datauserank0delay = DDR2_PHY_RANK0_DELAY,
  129. .datadldiff0 = PHY_DLL_LOCK_DIFF,
  130. };
  131. static const struct cmd_control ddr2_cmd_ctrl_data = {
  132. .cmd0csratio = DDR2_RATIO,
  133. .cmd0dldiff = DDR2_DLL_LOCK_DIFF,
  134. .cmd0iclkout = DDR2_INVERT_CLKOUT,
  135. .cmd1csratio = DDR2_RATIO,
  136. .cmd1dldiff = DDR2_DLL_LOCK_DIFF,
  137. .cmd1iclkout = DDR2_INVERT_CLKOUT,
  138. .cmd2csratio = DDR2_RATIO,
  139. .cmd2dldiff = DDR2_DLL_LOCK_DIFF,
  140. .cmd2iclkout = DDR2_INVERT_CLKOUT,
  141. };
  142. static const struct emif_regs ddr2_emif_reg_data = {
  143. .sdram_config = DDR2_EMIF_SDCFG,
  144. .ref_ctrl = DDR2_EMIF_SDREF,
  145. .sdram_tim1 = DDR2_EMIF_TIM1,
  146. .sdram_tim2 = DDR2_EMIF_TIM2,
  147. .sdram_tim3 = DDR2_EMIF_TIM3,
  148. .emif_ddr_phy_ctlr_1 = DDR2_EMIF_READ_LATENCY,
  149. };
  150. static const struct ddr_data ddr3_data = {
  151. .datardsratio0 = DDR3_RD_DQS,
  152. .datawdsratio0 = DDR3_WR_DQS,
  153. .datafwsratio0 = DDR3_PHY_FIFO_WE,
  154. .datawrsratio0 = DDR3_PHY_WR_DATA,
  155. .datadldiff0 = PHY_DLL_LOCK_DIFF,
  156. };
  157. static const struct cmd_control ddr3_cmd_ctrl_data = {
  158. .cmd0csratio = DDR3_RATIO,
  159. .cmd0dldiff = DDR3_DLL_LOCK_DIFF,
  160. .cmd0iclkout = DDR3_INVERT_CLKOUT,
  161. .cmd1csratio = DDR3_RATIO,
  162. .cmd1dldiff = DDR3_DLL_LOCK_DIFF,
  163. .cmd1iclkout = DDR3_INVERT_CLKOUT,
  164. .cmd2csratio = DDR3_RATIO,
  165. .cmd2dldiff = DDR3_DLL_LOCK_DIFF,
  166. .cmd2iclkout = DDR3_INVERT_CLKOUT,
  167. };
  168. static struct emif_regs ddr3_emif_reg_data = {
  169. .sdram_config = DDR3_EMIF_SDCFG,
  170. .ref_ctrl = DDR3_EMIF_SDREF,
  171. .sdram_tim1 = DDR3_EMIF_TIM1,
  172. .sdram_tim2 = DDR3_EMIF_TIM2,
  173. .sdram_tim3 = DDR3_EMIF_TIM3,
  174. .zq_config = DDR3_ZQ_CFG,
  175. .emif_ddr_phy_ctlr_1 = DDR3_EMIF_READ_LATENCY,
  176. };
  177. #endif
  178. /*
  179. * early system init of muxing and clocks.
  180. */
  181. void s_init(void)
  182. {
  183. /* WDT1 is already running when the bootloader gets control
  184. * Disable it to avoid "random" resets
  185. */
  186. writel(0xAAAA, &wdtimer->wdtwspr);
  187. while (readl(&wdtimer->wdtwwps) != 0x0)
  188. ;
  189. writel(0x5555, &wdtimer->wdtwspr);
  190. while (readl(&wdtimer->wdtwwps) != 0x0)
  191. ;
  192. #ifdef CONFIG_SPL_BUILD
  193. /* Setup the PLLs and the clocks for the peripherals */
  194. pll_init();
  195. /* Enable RTC32K clock */
  196. rtc32k_enable();
  197. /* UART softreset */
  198. u32 regVal;
  199. enable_uart0_pin_mux();
  200. regVal = readl(&uart_base->uartsyscfg);
  201. regVal |= UART_RESET;
  202. writel(regVal, &uart_base->uartsyscfg);
  203. while ((readl(&uart_base->uartsyssts) &
  204. UART_CLK_RUNNING_MASK) != UART_CLK_RUNNING_MASK)
  205. ;
  206. /* Disable smart idle */
  207. regVal = readl(&uart_base->uartsyscfg);
  208. regVal |= UART_SMART_IDLE_EN;
  209. writel(regVal, &uart_base->uartsyscfg);
  210. gd = &gdata;
  211. preloader_console_init();
  212. /* Initalize the board header */
  213. enable_i2c0_pin_mux();
  214. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  215. if (read_eeprom() < 0)
  216. puts("Could not get board ID.\n");
  217. enable_board_pin_mux(&header);
  218. if (board_is_evm_sk()) {
  219. /*
  220. * EVM SK 1.2A and later use gpio0_7 to enable DDR3.
  221. * This is safe enough to do on older revs.
  222. */
  223. gpio_request(GPIO_DDR_VTT_EN, "ddr_vtt_en");
  224. gpio_direction_output(GPIO_DDR_VTT_EN, 1);
  225. }
  226. if (board_is_evm_sk() || board_is_bone_lt())
  227. config_ddr(303, DDR3_IOCTRL_VALUE, &ddr3_data,
  228. &ddr3_cmd_ctrl_data, &ddr3_emif_reg_data);
  229. else
  230. config_ddr(266, DDR2_IOCTRL_VALUE, &ddr2_data,
  231. &ddr2_cmd_ctrl_data, &ddr2_emif_reg_data);
  232. #endif
  233. }
  234. /*
  235. * Basic board specific setup. Pinmux has been handled already.
  236. */
  237. int board_init(void)
  238. {
  239. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  240. if (read_eeprom() < 0)
  241. puts("Could not get board ID.\n");
  242. gd->bd->bi_boot_params = PHYS_DRAM_1 + 0x100;
  243. return 0;
  244. }
  245. #ifdef CONFIG_DRIVER_TI_CPSW
  246. static void cpsw_control(int enabled)
  247. {
  248. /* VTP can be added here */
  249. return;
  250. }
  251. static struct cpsw_slave_data cpsw_slaves[] = {
  252. {
  253. .slave_reg_ofs = 0x208,
  254. .sliver_reg_ofs = 0xd80,
  255. .phy_id = 0,
  256. },
  257. {
  258. .slave_reg_ofs = 0x308,
  259. .sliver_reg_ofs = 0xdc0,
  260. .phy_id = 1,
  261. },
  262. };
  263. static struct cpsw_platform_data cpsw_data = {
  264. .mdio_base = AM335X_CPSW_MDIO_BASE,
  265. .cpsw_base = AM335X_CPSW_BASE,
  266. .mdio_div = 0xff,
  267. .channels = 8,
  268. .cpdma_reg_ofs = 0x800,
  269. .slaves = 1,
  270. .slave_data = cpsw_slaves,
  271. .ale_reg_ofs = 0xd00,
  272. .ale_entries = 1024,
  273. .host_port_reg_ofs = 0x108,
  274. .hw_stats_reg_ofs = 0x900,
  275. .mac_control = (1 << 5),
  276. .control = cpsw_control,
  277. .host_port_num = 0,
  278. .version = CPSW_CTRL_VERSION_2,
  279. };
  280. int board_eth_init(bd_t *bis)
  281. {
  282. uint8_t mac_addr[6];
  283. uint32_t mac_hi, mac_lo;
  284. if (!eth_getenv_enetaddr("ethaddr", mac_addr)) {
  285. debug("<ethaddr> not set. Reading from E-fuse\n");
  286. /* try reading mac address from efuse */
  287. mac_lo = readl(&cdev->macid0l);
  288. mac_hi = readl(&cdev->macid0h);
  289. mac_addr[0] = mac_hi & 0xFF;
  290. mac_addr[1] = (mac_hi & 0xFF00) >> 8;
  291. mac_addr[2] = (mac_hi & 0xFF0000) >> 16;
  292. mac_addr[3] = (mac_hi & 0xFF000000) >> 24;
  293. mac_addr[4] = mac_lo & 0xFF;
  294. mac_addr[5] = (mac_lo & 0xFF00) >> 8;
  295. if (is_valid_ether_addr(mac_addr))
  296. eth_setenv_enetaddr("ethaddr", mac_addr);
  297. else
  298. return -1;
  299. }
  300. if (board_is_bone() || board_is_bone_lt()) {
  301. writel(MII_MODE_ENABLE, &cdev->miisel);
  302. cpsw_slaves[0].phy_if = cpsw_slaves[1].phy_if =
  303. PHY_INTERFACE_MODE_MII;
  304. } else {
  305. writel(RGMII_MODE_ENABLE, &cdev->miisel);
  306. cpsw_slaves[0].phy_if = cpsw_slaves[1].phy_if =
  307. PHY_INTERFACE_MODE_RGMII;
  308. }
  309. return cpsw_register(&cpsw_data);
  310. }
  311. #endif