Sjoerd Simons | 4512380 | 2019-02-25 15:33:00 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * board.c |
| 4 | * |
| 5 | * Board functions for Bosch Guardian |
| 6 | * |
| 7 | * Copyright (C) 2011, Texas Instruments, Incorporated - http://www.ti.com/ |
| 8 | * Copyright (C) 2018 Robert Bosch Power Tools GmbH |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Sjoerd Simons | 4512380 | 2019-02-25 15:33:00 +0000 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | f3998fd | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 13 | #include <env_internal.h> |
Sjoerd Simons | 4512380 | 2019-02-25 15:33:00 +0000 | [diff] [blame] | 14 | #include <errno.h> |
| 15 | #include <i2c.h> |
Moses Christopher | f233069 | 2020-03-25 06:45:45 +0000 | [diff] [blame] | 16 | #include <led.h> |
Sjoerd Simons | 4512380 | 2019-02-25 15:33:00 +0000 | [diff] [blame] | 17 | #include <panel.h> |
Gireesh Hiremath | d364926 | 2021-06-11 16:13:43 +0000 | [diff] [blame] | 18 | #include <linux/delay.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 19 | #include <asm/global_data.h> |
Sjoerd Simons | 4512380 | 2019-02-25 15:33:00 +0000 | [diff] [blame] | 20 | #include <power/tps65217.h> |
Sjoerd Simons | 4512380 | 2019-02-25 15:33:00 +0000 | [diff] [blame] | 21 | #include <spl.h> |
| 22 | #include <watchdog.h> |
| 23 | #include <asm/arch/clock.h> |
| 24 | #include <asm/arch/cpu.h> |
| 25 | #include <asm/arch/ddr_defs.h> |
| 26 | #include <asm/arch/gpio.h> |
| 27 | #include <asm/arch/hardware.h> |
Moses Christopher | 050531d | 2021-06-11 16:13:34 +0000 | [diff] [blame] | 28 | #include <asm/arch/mem-guardian.h> |
Sjoerd Simons | 4512380 | 2019-02-25 15:33:00 +0000 | [diff] [blame] | 29 | #include <asm/arch/omap.h> |
| 30 | #include <asm/arch/sys_proto.h> |
| 31 | #include <asm/emif.h> |
| 32 | #include <asm/gpio.h> |
| 33 | #include <asm/io.h> |
Gireesh Hiremath | 9cd380e | 2021-06-11 16:13:47 +0000 | [diff] [blame] | 34 | #include <jffs2/load_kernel.h> |
| 35 | #include <mtd.h> |
| 36 | #include <nand.h> |
| 37 | #include <video.h> |
| 38 | #include <video_console.h> |
Sjoerd Simons | 4512380 | 2019-02-25 15:33:00 +0000 | [diff] [blame] | 39 | #include "board.h" |
| 40 | |
| 41 | DECLARE_GLOBAL_DATA_PTR; |
| 42 | |
| 43 | #ifndef CONFIG_SKIP_LOWLEVEL_INIT |
| 44 | static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE; |
| 45 | |
| 46 | static const struct ddr_data ddr3_data = { |
| 47 | .datardsratio0 = MT41K128M16JT125K_RD_DQS, |
| 48 | .datawdsratio0 = MT41K128M16JT125K_WR_DQS, |
| 49 | .datafwsratio0 = MT41K128M16JT125K_PHY_FIFO_WE, |
| 50 | .datawrsratio0 = MT41K128M16JT125K_PHY_WR_DATA, |
| 51 | }; |
| 52 | |
| 53 | static const struct cmd_control ddr3_cmd_ctrl_data = { |
| 54 | .cmd0csratio = MT41K128M16JT125K_RATIO, |
| 55 | .cmd0iclkout = MT41K128M16JT125K_INVERT_CLKOUT, |
| 56 | |
| 57 | .cmd1csratio = MT41K128M16JT125K_RATIO, |
| 58 | .cmd1iclkout = MT41K128M16JT125K_INVERT_CLKOUT, |
| 59 | |
| 60 | .cmd2csratio = MT41K128M16JT125K_RATIO, |
| 61 | .cmd2iclkout = MT41K128M16JT125K_INVERT_CLKOUT, |
| 62 | }; |
| 63 | |
| 64 | static struct emif_regs ddr3_emif_reg_data = { |
| 65 | .sdram_config = MT41K128M16JT125K_EMIF_SDCFG, |
| 66 | .ref_ctrl = MT41K128M16JT125K_EMIF_SDREF, |
| 67 | .sdram_tim1 = MT41K128M16JT125K_EMIF_TIM1, |
| 68 | .sdram_tim2 = MT41K128M16JT125K_EMIF_TIM2, |
| 69 | .sdram_tim3 = MT41K128M16JT125K_EMIF_TIM3, |
| 70 | .zq_config = MT41K128M16JT125K_ZQ_CFG, |
| 71 | .emif_ddr_phy_ctlr_1 = MT41K128M16JT125K_EMIF_READ_LATENCY, |
| 72 | }; |
| 73 | |
| 74 | #define OSC (V_OSCK / 1000000) |
| 75 | const struct dpll_params dpll_ddr = { |
| 76 | 400, OSC - 1, 1, -1, -1, -1, -1}; |
| 77 | |
| 78 | void am33xx_spl_board_init(void) |
| 79 | { |
| 80 | int mpu_vdd; |
| 81 | int usb_cur_lim; |
| 82 | |
| 83 | /* Get the frequency */ |
| 84 | dpll_mpu_opp100.m = am335x_get_efuse_mpu_max_freq(cdev); |
| 85 | |
| 86 | if (i2c_probe(TPS65217_CHIP_PM)) |
| 87 | return; |
| 88 | |
| 89 | /* |
| 90 | * Increase USB current limit to 1300mA or 1800mA and set |
| 91 | * the MPU voltage controller as needed. |
| 92 | */ |
| 93 | if (dpll_mpu_opp100.m == MPUPLL_M_1000) { |
| 94 | usb_cur_lim = TPS65217_USB_INPUT_CUR_LIMIT_1800MA; |
| 95 | mpu_vdd = TPS65217_DCDC_VOLT_SEL_1325MV; |
| 96 | } else { |
| 97 | usb_cur_lim = TPS65217_USB_INPUT_CUR_LIMIT_1300MA; |
| 98 | mpu_vdd = TPS65217_DCDC_VOLT_SEL_1275MV; |
| 99 | } |
| 100 | |
| 101 | if (tps65217_reg_write(TPS65217_PROT_LEVEL_NONE, |
| 102 | TPS65217_POWER_PATH, |
| 103 | usb_cur_lim, |
| 104 | TPS65217_USB_INPUT_CUR_LIMIT_MASK)) |
| 105 | puts("tps65217_reg_write failure\n"); |
| 106 | |
| 107 | /* Set DCDC3 (CORE) voltage to 1.125V */ |
| 108 | if (tps65217_voltage_update(TPS65217_DEFDCDC3, |
| 109 | TPS65217_DCDC_VOLT_SEL_1125MV)) { |
| 110 | puts("tps65217_voltage_update failure\n"); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | /* Set CORE Frequencies to OPP100 */ |
| 115 | do_setup_dpll(&dpll_core_regs, &dpll_core_opp100); |
| 116 | |
| 117 | /* Set DCDC2 (MPU) voltage */ |
| 118 | if (tps65217_voltage_update(TPS65217_DEFDCDC2, mpu_vdd)) { |
| 119 | puts("tps65217_voltage_update failure\n"); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Set LDO3 to 1.8V and LDO4 to 3.3V |
| 125 | */ |
| 126 | if (tps65217_reg_write(TPS65217_PROT_LEVEL_2, |
| 127 | TPS65217_DEFLS1, |
| 128 | TPS65217_LDO_VOLTAGE_OUT_1_8, |
| 129 | TPS65217_LDO_MASK)) |
| 130 | puts("tps65217_reg_write failure\n"); |
| 131 | |
| 132 | if (tps65217_reg_write(TPS65217_PROT_LEVEL_2, |
| 133 | TPS65217_DEFLS2, |
| 134 | TPS65217_LDO_VOLTAGE_OUT_3_3, |
| 135 | TPS65217_LDO_MASK)) |
| 136 | puts("tps65217_reg_write failure\n"); |
| 137 | |
| 138 | /* Set MPU Frequency to what we detected now that voltages are set */ |
| 139 | do_setup_dpll(&dpll_mpu_regs, &dpll_mpu_opp100); |
| 140 | } |
| 141 | |
| 142 | const struct dpll_params *get_dpll_ddr_params(void) |
| 143 | { |
| 144 | enable_i2c0_pin_mux(); |
| 145 | i2c_init(CONFIG_SYS_OMAP24_I2C_SPEED, CONFIG_SYS_OMAP24_I2C_SLAVE); |
| 146 | |
| 147 | return &dpll_ddr; |
| 148 | } |
| 149 | |
| 150 | void set_uart_mux_conf(void) |
| 151 | { |
| 152 | enable_uart0_pin_mux(); |
| 153 | } |
| 154 | |
| 155 | void set_mux_conf_regs(void) |
| 156 | { |
| 157 | enable_board_pin_mux(); |
| 158 | } |
| 159 | |
| 160 | const struct ctrl_ioregs ioregs = { |
| 161 | .cm0ioctl = MT41K128M16JT125K_IOCTRL_VALUE, |
| 162 | .cm1ioctl = MT41K128M16JT125K_IOCTRL_VALUE, |
| 163 | .cm2ioctl = MT41K128M16JT125K_IOCTRL_VALUE, |
| 164 | .dt0ioctl = MT41K128M16JT125K_IOCTRL_VALUE, |
| 165 | .dt1ioctl = MT41K128M16JT125K_IOCTRL_VALUE, |
| 166 | }; |
| 167 | |
| 168 | void sdram_init(void) |
| 169 | { |
| 170 | config_ddr(400, &ioregs, |
| 171 | &ddr3_data, |
| 172 | &ddr3_cmd_ctrl_data, |
| 173 | &ddr3_emif_reg_data, 0); |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | int board_init(void) |
| 178 | { |
Moses Christopher | b1476b5 | 2019-09-17 14:25:37 +0000 | [diff] [blame] | 179 | save_omap_boot_params(); |
| 180 | |
Sjoerd Simons | 4512380 | 2019-02-25 15:33:00 +0000 | [diff] [blame] | 181 | #if defined(CONFIG_HW_WATCHDOG) |
| 182 | hw_watchdog_init(); |
| 183 | #endif |
| 184 | |
| 185 | gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100; |
| 186 | |
Miquel Raynal | 88718be | 2019-10-03 19:50:03 +0200 | [diff] [blame] | 187 | #ifdef CONFIG_MTD_RAW_NAND |
Sjoerd Simons | 4512380 | 2019-02-25 15:33:00 +0000 | [diff] [blame] | 188 | gpmc_init(); |
| 189 | #endif |
| 190 | return 0; |
| 191 | } |
Moses Christopher | b1476b5 | 2019-09-17 14:25:37 +0000 | [diff] [blame] | 192 | |
| 193 | #ifdef CONFIG_BOARD_LATE_INIT |
| 194 | static void set_bootmode_env(void) |
| 195 | { |
Moses Christopher | b1476b5 | 2019-09-17 14:25:37 +0000 | [diff] [blame] | 196 | char *boot_mode_gpio = "gpio@44e07000_14"; |
| 197 | int ret; |
Moses Christopher | b1476b5 | 2019-09-17 14:25:37 +0000 | [diff] [blame] | 198 | |
| 199 | struct gpio_desc boot_mode_desc; |
| 200 | |
Moses Christopher | b1476b5 | 2019-09-17 14:25:37 +0000 | [diff] [blame] | 201 | ret = dm_gpio_lookup_name(boot_mode_gpio, &boot_mode_desc); |
| 202 | if (ret) { |
| 203 | printf("%s is not found\n", boot_mode_gpio); |
| 204 | goto err; |
| 205 | } |
| 206 | |
| 207 | ret = dm_gpio_request(&boot_mode_desc, "setup_bootmode_env"); |
| 208 | if (ret && ret != -EBUSY) { |
| 209 | printf("requesting gpio: %s failed\n", boot_mode_gpio); |
| 210 | goto err; |
| 211 | } |
| 212 | |
Gireesh Hiremath | d364926 | 2021-06-11 16:13:43 +0000 | [diff] [blame] | 213 | dm_gpio_set_dir_flags(&boot_mode_desc, GPIOD_IS_IN); |
| 214 | udelay(10); |
| 215 | |
| 216 | ret = dm_gpio_get_value(&boot_mode_desc); |
| 217 | if (ret == 0) { |
| 218 | env_set("swi_status", "1"); |
| 219 | } else if (ret == 1) { |
| 220 | env_set("swi_status", "0"); |
| 221 | } else { |
| 222 | printf("swi status gpio error\n"); |
| 223 | goto err; |
| 224 | } |
| 225 | |
Moses Christopher | b1476b5 | 2019-09-17 14:25:37 +0000 | [diff] [blame] | 226 | return; |
| 227 | |
| 228 | err: |
| 229 | env_set("swi_status", "err"); |
| 230 | } |
| 231 | |
Gireesh Hiremath | ae628fb | 2021-06-11 16:13:44 +0000 | [diff] [blame] | 232 | void lcdbacklight_en(void) |
| 233 | { |
| 234 | unsigned long brightness = env_get_ulong("backlight_brightness", 10, 50); |
| 235 | |
| 236 | if (brightness > 99 || brightness == 0) |
| 237 | brightness = 99; |
| 238 | |
| 239 | /* |
| 240 | * Brightness range: |
| 241 | * WLEDCTRL2 DUTY[6:0] |
| 242 | * |
| 243 | * 000 0000b = 1% |
| 244 | * 000 0001b = 2% |
| 245 | * ... |
| 246 | * 110 0010b = 99% |
| 247 | * 110 0011b = 100% |
| 248 | * |
| 249 | */ |
| 250 | |
| 251 | tps65217_reg_write(TPS65217_PROT_LEVEL_NONE, TPS65217_WLEDCTRL2, |
| 252 | brightness, 0xFF); |
| 253 | tps65217_reg_write(TPS65217_PROT_LEVEL_NONE, TPS65217_WLEDCTRL1, |
| 254 | brightness != 0 ? 0x0A : 0x02, 0xFF); |
| 255 | } |
| 256 | |
Gireesh Hiremath | 9cd380e | 2021-06-11 16:13:47 +0000 | [diff] [blame] | 257 | #if IS_ENABLED(CONFIG_AM335X_LCD) |
| 258 | static void splash_screen(void) |
| 259 | { |
| 260 | struct udevice *video_dev; |
| 261 | struct udevice *console_dev; |
| 262 | struct video_priv *vid_priv; |
| 263 | struct mtd_info *mtd; |
| 264 | size_t len; |
| 265 | int ret; |
| 266 | |
| 267 | struct mtd_device *mtd_dev; |
| 268 | struct part_info *part; |
| 269 | u8 pnum; |
| 270 | |
| 271 | ret = uclass_get_device(UCLASS_VIDEO, 0, &video_dev); |
| 272 | if (ret != 0) { |
| 273 | debug("video device not found\n"); |
| 274 | goto exit; |
| 275 | } |
| 276 | |
| 277 | vid_priv = dev_get_uclass_priv(video_dev); |
| 278 | mtdparts_init(); |
| 279 | |
| 280 | if (find_dev_and_part(SPLASH_SCREEN_NAND_PART, &mtd_dev, &pnum, &part)) { |
| 281 | debug("Could not find nand partition\n"); |
| 282 | goto splash_screen_text; |
| 283 | } |
| 284 | |
| 285 | mtd = get_nand_dev_by_index(mtd_dev->id->num); |
| 286 | if (!mtd) { |
| 287 | debug("MTD partition is not valid\n"); |
| 288 | goto splash_screen_text; |
| 289 | } |
| 290 | |
| 291 | len = SPLASH_SCREEN_BMP_FILE_SIZE; |
| 292 | ret = nand_read_skip_bad(mtd, part->offset, &len, NULL, |
| 293 | SPLASH_SCREEN_BMP_FILE_SIZE, |
| 294 | (u_char *)SPLASH_SCREEN_BMP_LOAD_ADDR); |
| 295 | if (ret != 0) { |
| 296 | debug("Reading NAND partition failed\n"); |
| 297 | goto splash_screen_text; |
| 298 | } |
| 299 | |
| 300 | ret = video_bmp_display(video_dev, SPLASH_SCREEN_BMP_LOAD_ADDR, 0, 0, false); |
| 301 | if (ret != 0) { |
| 302 | debug("No valid bmp image found!!\n"); |
| 303 | goto splash_screen_text; |
| 304 | } else { |
| 305 | goto exit; |
| 306 | } |
| 307 | |
| 308 | splash_screen_text: |
| 309 | vid_priv->colour_fg = CONSOLE_COLOR_RED; |
| 310 | vid_priv->colour_bg = CONSOLE_COLOR_BLACK; |
| 311 | |
| 312 | if (!uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &console_dev)) { |
| 313 | debug("Found console\n"); |
| 314 | vidconsole_position_cursor(console_dev, 17, 7); |
| 315 | vidconsole_put_string(console_dev, SPLASH_SCREEN_TEXT); |
| 316 | } else { |
| 317 | debug("No console device found\n"); |
| 318 | } |
| 319 | |
| 320 | exit: |
| 321 | return; |
| 322 | } |
| 323 | #endif /* CONFIG_AM335X_LCD */ |
| 324 | |
Moses Christopher | b1476b5 | 2019-09-17 14:25:37 +0000 | [diff] [blame] | 325 | int board_late_init(void) |
| 326 | { |
Gireesh Hiremath | 9cd380e | 2021-06-11 16:13:47 +0000 | [diff] [blame] | 327 | int ret; |
| 328 | struct udevice *cdev; |
| 329 | |
Moses Christopher | f233069 | 2020-03-25 06:45:45 +0000 | [diff] [blame] | 330 | #ifdef CONFIG_LED_GPIO |
| 331 | led_default_state(); |
| 332 | #endif |
Moses Christopher | b1476b5 | 2019-09-17 14:25:37 +0000 | [diff] [blame] | 333 | set_bootmode_env(); |
Gireesh Hiremath | 9cd380e | 2021-06-11 16:13:47 +0000 | [diff] [blame] | 334 | |
| 335 | ret = uclass_get_device(UCLASS_PANEL, 0, &cdev); |
| 336 | if (ret) { |
| 337 | debug("video panel not found: %d\n", ret); |
| 338 | return ret; |
| 339 | } |
| 340 | |
Gireesh Hiremath | ae628fb | 2021-06-11 16:13:44 +0000 | [diff] [blame] | 341 | lcdbacklight_en(); |
Gireesh Hiremath | 9cd380e | 2021-06-11 16:13:47 +0000 | [diff] [blame] | 342 | if (IS_ENABLED(CONFIG_AM335X_LCD)) |
| 343 | splash_screen(); |
| 344 | |
Moses Christopher | b1476b5 | 2019-09-17 14:25:37 +0000 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | #endif /* CONFIG_BOARD_LATE_INIT */ |