board.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * board.c
  4. *
  5. * Board functions for EETS PDU001 board
  6. *
  7. * Copyright (C) 2018, EETS GmbH, http://www.eets.ch/
  8. *
  9. * Copyright (C) 2011, Texas Instruments, Incorporated - http://www.ti.com/
  10. */
  11. #include <common.h>
  12. #include <env.h>
  13. #include <errno.h>
  14. #include <init.h>
  15. #include <log.h>
  16. #include <spl.h>
  17. #include <i2c.h>
  18. #include <watchdog.h>
  19. #include <debug_uart.h>
  20. #include <asm/global_data.h>
  21. #include <dm/ofnode.h>
  22. #include <power/pmic.h>
  23. #include <power/regulator.h>
  24. #include <asm/arch/cpu.h>
  25. #include <asm/arch/hardware.h>
  26. #include <asm/arch/omap.h>
  27. #include <asm/arch/ddr_defs.h>
  28. #include <asm/arch/clock.h>
  29. #include <asm/arch/gpio.h>
  30. #include <asm/arch/mmc_host_def.h>
  31. #include <asm/arch/sys_proto.h>
  32. #include <asm/arch/mem.h>
  33. #include <asm/io.h>
  34. #include <asm/emif.h>
  35. #include <asm/gpio.h>
  36. #include "board.h"
  37. DECLARE_GLOBAL_DATA_PTR;
  38. #define I2C_ADDR_NODE_ID 0x50
  39. #define I2C_REG_NODE_ID_BASE 0xfa
  40. #define NODE_ID_BYTE_COUNT 6
  41. #define I2C_ADDR_LEDS 0x60
  42. #define I2C_REG_RUN_LED 0x06
  43. #define RUN_LED_OFF 0x0
  44. #define RUN_LED_RED 0x1
  45. #define RUN_LED_GREEN (0x1 << 2)
  46. #define VDD_MPU_REGULATOR "regulator@2"
  47. #define VDD_CORE_REGULATOR "regulator@3"
  48. #define DEFAULT_CORE_VOLTAGE 1137500
  49. /*
  50. * boot device save register
  51. * -------------------------
  52. * The boot device can be quired by 'spl_boot_device()' in
  53. * 'am33xx_spl_board_init'. However it can't be saved in the u-boot
  54. * environment here. In turn 'spl_boot_device' can't be called in
  55. * 'board_late_init' which allows writing to u-boot environment.
  56. * To get the boot device from 'am33xx_spl_board_init' to
  57. * 'board_late_init' we therefore use a scratch register from the RTC.
  58. */
  59. #define CONFIG_SYS_RTC_SCRATCH0 0x60
  60. #define BOOT_DEVICE_SAVE_REGISTER (RTC_BASE + CONFIG_SYS_RTC_SCRATCH0)
  61. #ifdef CONFIG_SPL_BUILD
  62. static void save_boot_device(void)
  63. {
  64. *((u32 *)(BOOT_DEVICE_SAVE_REGISTER)) = spl_boot_device();
  65. }
  66. #endif
  67. u32 boot_device(void)
  68. {
  69. return *((u32 *)(BOOT_DEVICE_SAVE_REGISTER));
  70. }
  71. /* Store the boot device in the environment variable 'boot_device' */
  72. static void env_set_boot_device(void)
  73. {
  74. switch (boot_device()) {
  75. case BOOT_DEVICE_MMC1: {
  76. env_set("boot_device", "emmc");
  77. break;
  78. }
  79. case BOOT_DEVICE_MMC2: {
  80. env_set("boot_device", "sdcard");
  81. break;
  82. }
  83. default: {
  84. env_set("boot_device", "unknown");
  85. break;
  86. }
  87. }
  88. }
  89. static void set_run_led(struct udevice *dev)
  90. {
  91. int val = RUN_LED_OFF;
  92. if (IS_ENABLED(CONFIG_RUN_LED_RED))
  93. val = RUN_LED_RED;
  94. else if (IS_ENABLED(CONFIG_RUN_LED_GREEN))
  95. val = RUN_LED_GREEN;
  96. dm_i2c_reg_write(dev, I2C_REG_RUN_LED, val);
  97. }
  98. /* Set 'serial#' to the EUI-48 value of board node ID chip */
  99. static void env_set_serial(struct udevice *dev)
  100. {
  101. int val;
  102. char serial[2 * NODE_ID_BYTE_COUNT + 1];
  103. int n;
  104. for (n = 0; n < sizeof(serial); n += 2) {
  105. val = dm_i2c_reg_read(dev, I2C_REG_NODE_ID_BASE + n / 2);
  106. sprintf(serial + n, "%02X", val);
  107. }
  108. serial[2 * NODE_ID_BYTE_COUNT] = '\0';
  109. env_set("serial#", serial);
  110. }
  111. static void set_mpu_and_core_voltage(void)
  112. {
  113. int mpu_vdd;
  114. int sil_rev;
  115. struct udevice *dev;
  116. struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
  117. /*
  118. * The PDU001 (more precisely the computing module m2) uses a
  119. * TPS65910 PMIC. For all MPU frequencies we support we use a CORE
  120. * voltage of 1.1375V. For MPU voltage we need to switch based on
  121. * the frequency we are running at.
  122. */
  123. /*
  124. * Depending on MPU clock and PG we will need a different VDD
  125. * to drive at that speed.
  126. */
  127. sil_rev = readl(&cdev->deviceid) >> 28;
  128. mpu_vdd = am335x_get_mpu_vdd(sil_rev, dpll_mpu_opp100.m);
  129. /* first update the MPU voltage */
  130. if (!regulator_get_by_devname(VDD_MPU_REGULATOR, &dev)) {
  131. if (regulator_set_value(dev, mpu_vdd))
  132. debug("failed to set MPU voltage\n");
  133. } else {
  134. debug("invalid MPU voltage ragulator %s\n", VDD_MPU_REGULATOR);
  135. }
  136. /* second update the CORE voltage */
  137. if (!regulator_get_by_devname(VDD_CORE_REGULATOR, &dev)) {
  138. if (regulator_set_value(dev, DEFAULT_CORE_VOLTAGE))
  139. debug("failed to set CORE voltage\n");
  140. } else {
  141. debug("invalid CORE voltage ragulator %s\n",
  142. VDD_CORE_REGULATOR);
  143. }
  144. }
  145. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  146. static const struct ddr_data ddr2_data = {
  147. .datardsratio0 = MT47H128M16RT25E_RD_DQS,
  148. .datafwsratio0 = MT47H128M16RT25E_PHY_FIFO_WE,
  149. .datawrsratio0 = MT47H128M16RT25E_PHY_WR_DATA,
  150. };
  151. static const struct cmd_control ddr2_cmd_ctrl_data = {
  152. .cmd0csratio = MT47H128M16RT25E_RATIO,
  153. .cmd1csratio = MT47H128M16RT25E_RATIO,
  154. .cmd2csratio = MT47H128M16RT25E_RATIO,
  155. };
  156. static const struct emif_regs ddr2_emif_reg_data = {
  157. .sdram_config = MT47H128M16RT25E_EMIF_SDCFG,
  158. .ref_ctrl = MT47H128M16RT25E_EMIF_SDREF,
  159. .sdram_tim1 = MT47H128M16RT25E_EMIF_TIM1,
  160. .sdram_tim2 = MT47H128M16RT25E_EMIF_TIM2,
  161. .sdram_tim3 = MT47H128M16RT25E_EMIF_TIM3,
  162. .emif_ddr_phy_ctlr_1 = MT47H128M16RT25E_EMIF_READ_LATENCY,
  163. };
  164. #define OSC (V_OSCK / 1000000)
  165. const struct dpll_params dpll_ddr = {
  166. 266, OSC - 1, 1, -1, -1, -1, -1};
  167. const struct dpll_params dpll_ddr_evm_sk = {
  168. 303, OSC - 1, 1, -1, -1, -1, -1};
  169. const struct dpll_params dpll_ddr_bone_black = {
  170. 400, OSC - 1, 1, -1, -1, -1, -1};
  171. void am33xx_spl_board_init(void)
  172. {
  173. struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
  174. /* Get the frequency */
  175. dpll_mpu_opp100.m = am335x_get_efuse_mpu_max_freq(cdev);
  176. /* Set CORE Frequencies to OPP100 */
  177. do_setup_dpll(&dpll_core_regs, &dpll_core_opp100);
  178. /* Set MPU Frequency to what we detected now that voltages are set */
  179. do_setup_dpll(&dpll_mpu_regs, &dpll_mpu_opp100);
  180. /* save boot device for later use by 'board_late_init' */
  181. save_boot_device();
  182. }
  183. const struct dpll_params *get_dpll_ddr_params(void)
  184. {
  185. enable_i2c0_pin_mux();
  186. return &dpll_ddr;
  187. }
  188. void set_mux_conf_regs(void)
  189. {
  190. /* done first by the ROM and afterwards by the pin controller driver */
  191. enable_i2c0_pin_mux();
  192. }
  193. const struct ctrl_ioregs ioregs = {
  194. .cm0ioctl = MT47H128M16RT25E_IOCTRL_VALUE,
  195. .cm1ioctl = MT47H128M16RT25E_IOCTRL_VALUE,
  196. .cm2ioctl = MT47H128M16RT25E_IOCTRL_VALUE,
  197. .dt0ioctl = MT47H128M16RT25E_IOCTRL_VALUE,
  198. .dt1ioctl = MT47H128M16RT25E_IOCTRL_VALUE,
  199. };
  200. void sdram_init(void)
  201. {
  202. config_ddr(266, &ioregs, &ddr2_data,
  203. &ddr2_cmd_ctrl_data, &ddr2_emif_reg_data, 0);
  204. }
  205. #endif /* CONFIG_SKIP_LOWLEVEL_INIT */
  206. #ifdef CONFIG_DEBUG_UART
  207. void board_debug_uart_init(void)
  208. {
  209. /* done by pin controller driver if not debugging */
  210. enable_uart_pin_mux(CONFIG_DEBUG_UART_BASE);
  211. }
  212. #endif
  213. /*
  214. * Basic board specific setup. Pinmux has been handled already.
  215. */
  216. int board_init(void)
  217. {
  218. #ifdef CONFIG_HW_WATCHDOG
  219. hw_watchdog_init();
  220. #endif
  221. gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
  222. return 0;
  223. }
  224. #ifdef CONFIG_BOARD_LATE_INIT
  225. int board_late_init(void)
  226. {
  227. struct udevice *dev;
  228. set_mpu_and_core_voltage();
  229. env_set_boot_device();
  230. /* second I2C bus connects to node ID and front panel LED chip */
  231. if (!i2c_get_chip_for_busnum(1, I2C_ADDR_LEDS, 1, &dev))
  232. set_run_led(dev);
  233. if (!i2c_get_chip_for_busnum(1, I2C_ADDR_NODE_ID, 1, &dev))
  234. env_set_serial(dev);
  235. return 0;
  236. }
  237. #endif