Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Common board functions for siemens AM335X based boards |
| 4 | * (C) Copyright 2013 Siemens Schweiz AG |
| 5 | * (C) Heiko Schocher, DENX Software Engineering, hs@denx.de. |
| 6 | * |
| 7 | * Based on: |
| 8 | * U-Boot file:/board/ti/am335x/board.c |
Nishanth Menon | eaa39c6 | 2023-11-01 15:56:03 -0500 | [diff] [blame] | 9 | * Copyright (C) 2011, Texas Instruments, Incorporated - https://www.ti.com/ |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 12 | #include <command.h> |
Simon Glass | 3673618 | 2019-11-14 12:57:24 -0700 | [diff] [blame] | 13 | #include <serial.h> |
Enrico Leto | fce9179 | 2024-01-24 15:43:54 +0100 | [diff] [blame] | 14 | #include <watchdog.h> |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 15 | #include <asm/arch/clock.h> |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 16 | #include <asm/arch/sys_proto.h> |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 17 | #include <asm/gpio.h> |
Simon Glass | 0ffb9d6 | 2017-05-31 19:47:48 -0600 | [diff] [blame] | 18 | #include <asm/mach-types.h> |
Enrico Leto | 2e74050 | 2024-01-24 15:43:53 +0100 | [diff] [blame] | 19 | #include "board_am335x.h" |
Enrico Leto | 096bfdc | 2024-01-24 15:43:49 +0100 | [diff] [blame] | 20 | #include "eeprom.h" |
| 21 | #include "factoryset.h" |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 22 | |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Simon Glass | 49c24a8 | 2024-09-29 19:49:47 -0600 | [diff] [blame] | 25 | #ifdef CONFIG_XPL_BUILD |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 26 | void set_uart_mux_conf(void) |
| 27 | { |
| 28 | enable_uart0_pin_mux(); |
| 29 | } |
| 30 | |
| 31 | void set_mux_conf_regs(void) |
| 32 | { |
| 33 | /* Initalize the board header */ |
| 34 | enable_i2c0_pin_mux(); |
Heiko Schocher | b5e7d37 | 2015-05-28 07:27:36 +0200 | [diff] [blame] | 35 | |
| 36 | /* enable early the console */ |
| 37 | gd->baudrate = CONFIG_BAUDRATE; |
| 38 | serial_init(); |
Simon Glass | d4b0fdb | 2024-08-21 10:19:04 -0600 | [diff] [blame] | 39 | gd->flags |= GD_FLG_HAVE_CONSOLE; |
Enrico Leto | 32f433f | 2024-01-24 15:43:50 +0100 | [diff] [blame] | 40 | |
| 41 | siemens_ee_setup(); |
Enrico Leto | 2e74050 | 2024-01-24 15:43:53 +0100 | [diff] [blame] | 42 | if (draco_read_eeprom() < 0) |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 43 | puts("Could not get board ID.\n"); |
| 44 | |
| 45 | enable_board_pin_mux(); |
| 46 | } |
| 47 | |
| 48 | void sdram_init(void) |
| 49 | { |
Enrico Leto | 2e74050 | 2024-01-24 15:43:53 +0100 | [diff] [blame] | 50 | spl_draco_board_init(); |
| 51 | draco_init_ddr(); |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 52 | |
| 53 | return; |
| 54 | } |
Simon Glass | 49c24a8 | 2024-09-29 19:49:47 -0600 | [diff] [blame] | 55 | #endif /* #ifdef CONFIG_XPL_BUILD */ |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 56 | |
Simon Glass | 49c24a8 | 2024-09-29 19:49:47 -0600 | [diff] [blame] | 57 | #ifndef CONFIG_XPL_BUILD |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 58 | /* |
| 59 | * Basic board specific setup. Pinmux has been handled already. |
| 60 | */ |
| 61 | int board_init(void) |
| 62 | { |
| 63 | #if defined(CONFIG_HW_WATCHDOG) |
| 64 | hw_watchdog_init(); |
| 65 | #endif /* defined(CONFIG_HW_WATCHDOG) */ |
Enrico Leto | 32f433f | 2024-01-24 15:43:50 +0100 | [diff] [blame] | 66 | if (siemens_ee_setup() < 0) |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 67 | puts("Could not get board ID.\n"); |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 68 | #ifdef CONFIG_MACH_TYPE |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 69 | gd->bd->bi_arch_number = CONFIG_MACH_TYPE; |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 70 | #endif |
Tom Rini | bb4dd96 | 2022-11-16 13:10:37 -0500 | [diff] [blame] | 71 | gd->bd->bi_boot_params = CFG_SYS_SDRAM_BASE + 0x100; |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 72 | |
| 73 | #ifdef CONFIG_FACTORYSET |
Enrico Leto | 096bfdc | 2024-01-24 15:43:49 +0100 | [diff] [blame] | 74 | factoryset_read_eeprom(SIEMENS_EE_I2C_ADDR); |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 75 | #endif |
Heiko Schocher | cbec11a | 2016-06-07 08:55:45 +0200 | [diff] [blame] | 76 | |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 77 | gpmc_init(); |
| 78 | |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 79 | return 0; |
| 80 | } |
Simon Glass | 49c24a8 | 2024-09-29 19:49:47 -0600 | [diff] [blame] | 81 | #endif /* #ifndef CONFIG_XPL_BUILD */ |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 82 | |
| 83 | #define OSC (V_OSCK/1000000) |
| 84 | const struct dpll_params dpll_ddr = { |
| 85 | DDR_PLL_FREQ, OSC-1, 1, -1, -1, -1, -1}; |
| 86 | |
| 87 | const struct dpll_params *get_dpll_ddr_params(void) |
| 88 | { |
| 89 | return &dpll_ddr; |
| 90 | } |
| 91 | |
Simon Glass | 49c24a8 | 2024-09-29 19:49:47 -0600 | [diff] [blame] | 92 | #ifndef CONFIG_XPL_BUILD |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 93 | |
| 94 | #define MAX_NR_LEDS 10 |
| 95 | #define MAX_PIN_NUMBER 128 |
| 96 | #define STARTUP 0 |
| 97 | |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 98 | #if defined(BOARD_DFU_BUTTON_GPIO) |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 99 | unsigned char get_button_state(char * const envname, unsigned char def) |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 100 | { |
| 101 | int button = 0; |
| 102 | int gpio; |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 103 | char *ptr_env; |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 104 | |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 105 | /* If button is not found we take default */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 106 | ptr_env = env_get(envname); |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 107 | if (NULL == ptr_env) { |
| 108 | gpio = def; |
| 109 | } else { |
| 110 | gpio = (unsigned char)simple_strtoul(ptr_env, NULL, 0); |
| 111 | if (gpio > MAX_PIN_NUMBER) |
| 112 | gpio = def; |
| 113 | } |
| 114 | |
| 115 | gpio_request(gpio, ""); |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 116 | gpio_direction_input(gpio); |
| 117 | if (gpio_get_value(gpio)) |
| 118 | button = 1; |
| 119 | else |
| 120 | button = 0; |
| 121 | |
| 122 | gpio_free(gpio); |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 123 | |
| 124 | return button; |
| 125 | } |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 126 | /** |
| 127 | * This command returns the status of the user button on |
| 128 | * Input - none |
| 129 | * Returns - 1 if button is held down |
| 130 | * 0 if button is not held down |
Egli, Samuel | fdb5e26 | 2014-04-24 17:57:54 +0200 | [diff] [blame] | 131 | */ |
| 132 | static int |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 133 | do_userbutton(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Egli, Samuel | fdb5e26 | 2014-04-24 17:57:54 +0200 | [diff] [blame] | 134 | { |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 135 | int button = 0; |
| 136 | button = get_button_state("button_dfu0", BOARD_DFU_BUTTON_GPIO); |
| 137 | button |= get_button_state("button_dfu1", BOARD_DFU_BUTTON_GPIO); |
| 138 | return button; |
Egli, Samuel | fdb5e26 | 2014-04-24 17:57:54 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | U_BOOT_CMD( |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 142 | dfubutton, CONFIG_SYS_MAXARGS, 1, do_userbutton, |
| 143 | "Return the status of the DFU button", |
| 144 | "" |
Egli, Samuel | fdb5e26 | 2014-04-24 17:57:54 +0200 | [diff] [blame] | 145 | ); |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 146 | #endif |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 147 | |
| 148 | static int |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 149 | do_usertestwdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 150 | { |
| 151 | printf("\n\n\n Go into infinite loop\n\n\n"); |
| 152 | while (1) |
| 153 | ; |
| 154 | return 0; |
| 155 | }; |
| 156 | |
| 157 | U_BOOT_CMD( |
| 158 | testwdt, CONFIG_SYS_MAXARGS, 1, do_usertestwdt, |
| 159 | "Sends U-Boot into infinite loop", |
| 160 | "" |
| 161 | ); |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * Get led gpios from env and set them. |
| 165 | * The led define in environment need to need to be of the form ledN=NN,S0,S1 |
| 166 | * where N is an unsigned integer from 0 to 9 and S0 and S1 is 0 or 1. S0 |
| 167 | * defines the startup state of the led, S1 the special state of the led when |
| 168 | * it enters e.g. dfu mode. |
| 169 | */ |
| 170 | void set_env_gpios(unsigned char state) |
| 171 | { |
| 172 | char *ptr_env; |
| 173 | char str_tmp[5]; /* must contain "ledX"*/ |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 174 | unsigned char i, idx, pos1, pos2, ccount; |
| 175 | unsigned char gpio_n, gpio_s0, gpio_s1; |
| 176 | |
| 177 | for (i = 0; i < MAX_NR_LEDS; i++) { |
Heinrich Schuchardt | 32c63b6 | 2019-08-22 21:58:26 +0200 | [diff] [blame] | 178 | sprintf(str_tmp, "led%d", i); |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 179 | |
| 180 | /* If env var is not found we stop */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 181 | ptr_env = env_get(str_tmp); |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 182 | if (NULL == ptr_env) |
| 183 | break; |
| 184 | |
| 185 | /* Find sperators position */ |
| 186 | pos1 = 0; |
| 187 | pos2 = 0; |
| 188 | ccount = 0; |
| 189 | for (idx = 0; ptr_env[idx] != '\0'; idx++) { |
| 190 | if (ptr_env[idx] == ',') { |
| 191 | if (ccount++ < 1) |
| 192 | pos1 = idx; |
| 193 | else |
| 194 | pos2 = idx; |
| 195 | } |
| 196 | } |
| 197 | /* Bad led description skip this definition */ |
| 198 | if (pos2 <= pos1 || ccount > 2) |
| 199 | continue; |
| 200 | |
| 201 | /* Get pin number and request gpio */ |
| 202 | memset(str_tmp, 0, sizeof(str_tmp)); |
| 203 | strncpy(str_tmp, ptr_env, pos1*sizeof(char)); |
| 204 | gpio_n = (unsigned char)simple_strtoul(str_tmp, NULL, 0); |
| 205 | |
| 206 | /* Invalid gpio number skip definition */ |
| 207 | if (gpio_n > MAX_PIN_NUMBER) |
| 208 | continue; |
| 209 | |
| 210 | gpio_request(gpio_n, ""); |
| 211 | |
| 212 | if (state == STARTUP) { |
| 213 | /* get pin state 0 and set */ |
| 214 | memset(str_tmp, 0, sizeof(str_tmp)); |
| 215 | strncpy(str_tmp, ptr_env+pos1+1, |
| 216 | (pos2-pos1-1)*sizeof(char)); |
| 217 | gpio_s0 = (unsigned char)simple_strtoul(str_tmp, NULL, |
| 218 | 0); |
| 219 | |
| 220 | gpio_direction_output(gpio_n, gpio_s0); |
| 221 | |
| 222 | } else { |
| 223 | /* get pin state 1 and set */ |
| 224 | memset(str_tmp, 0, sizeof(str_tmp)); |
| 225 | strcpy(str_tmp, ptr_env+pos2+1); |
| 226 | gpio_s1 = (unsigned char)simple_strtoul(str_tmp, NULL, |
| 227 | 0); |
| 228 | gpio_direction_output(gpio_n, gpio_s1); |
| 229 | } |
| 230 | } /* loop through defined led in environment */ |
| 231 | } |
| 232 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 233 | static int do_board_led(struct cmd_tbl *cmdtp, int flag, int argc, |
| 234 | char *const argv[]) |
Heiko Schocher | d17c3fc | 2015-06-16 14:59:34 +0200 | [diff] [blame] | 235 | { |
| 236 | if (argc != 2) |
| 237 | return CMD_RET_USAGE; |
| 238 | if ((unsigned char)simple_strtoul(argv[1], NULL, 0) == STARTUP) |
| 239 | set_env_gpios(0); |
| 240 | else |
| 241 | set_env_gpios(1); |
| 242 | return 0; |
| 243 | }; |
| 244 | |
| 245 | U_BOOT_CMD( |
| 246 | draco_led, CONFIG_SYS_MAXARGS, 2, do_board_led, |
| 247 | "Set LEDs defined in environment", |
| 248 | "<0|1>" |
| 249 | ); |
Simon Glass | 49c24a8 | 2024-09-29 19:49:47 -0600 | [diff] [blame] | 250 | #endif /* !CONFIG_XPL_BUILD */ |