blob: c55ca983bdd74354e34474b2f60a43c2e968375c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocherc0dcece2013-08-19 16:39:01 +02002/*
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 Menona94a4072023-11-01 15:56:03 -05009 * Copyright (C) 2011, Texas Instruments, Incorporated - https://www.ti.com/
Heiko Schocherc0dcece2013-08-19 16:39:01 +020010 */
11
12#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -060013#include <command.h>
Simon Glass7b51b572019-08-01 09:46:52 -060014#include <env.h>
Heiko Schocherc0dcece2013-08-19 16:39:01 +020015#include <errno.h>
Simon Glass691d7192020-05-10 11:40:02 -060016#include <init.h>
Simon Glass336d4612020-02-03 07:36:16 -070017#include <malloc.h>
Simon Glassb03e0512019-11-14 12:57:24 -070018#include <serial.h>
Heiko Schocherc0dcece2013-08-19 16:39:01 +020019#include <spl.h>
20#include <asm/arch/cpu.h>
21#include <asm/arch/hardware.h>
22#include <asm/arch/omap.h>
23#include <asm/arch/ddr_defs.h>
24#include <asm/arch/clock.h>
25#include <asm/arch/gpio.h>
26#include <asm/arch/mmc_host_def.h>
27#include <asm/arch/sys_proto.h>
Simon Glass401d1c42020-10-30 21:38:53 -060028#include <asm/global_data.h>
Heiko Schocherc0dcece2013-08-19 16:39:01 +020029#include <asm/io.h>
30#include <asm/emif.h>
31#include <asm/gpio.h>
Heiko Schocherc0dcece2013-08-19 16:39:01 +020032#include <miiphy.h>
33#include <cpsw.h>
34#include <watchdog.h>
Simon Glassc62db352017-05-31 19:47:48 -060035#include <asm/mach-types.h>
Enrico Letod89a97e2024-01-24 15:43:53 +010036#include "board_am335x.h"
Enrico Leto5ae54612024-01-24 15:43:49 +010037#include "eeprom.h"
38#include "factoryset.h"
Heiko Schocherc0dcece2013-08-19 16:39:01 +020039
40DECLARE_GLOBAL_DATA_PTR;
41
Enrico Letoe9ef9a12024-01-24 15:43:50 +010042
Heiko Schocherc0dcece2013-08-19 16:39:01 +020043#ifdef CONFIG_SPL_BUILD
44void set_uart_mux_conf(void)
45{
46 enable_uart0_pin_mux();
47}
48
49void set_mux_conf_regs(void)
50{
51 /* Initalize the board header */
52 enable_i2c0_pin_mux();
Heiko Schocher012681b2015-05-28 07:27:36 +020053
54 /* enable early the console */
55 gd->baudrate = CONFIG_BAUDRATE;
56 serial_init();
57 gd->have_console = 1;
Enrico Letoe9ef9a12024-01-24 15:43:50 +010058
59 siemens_ee_setup();
Enrico Letod89a97e2024-01-24 15:43:53 +010060 if (draco_read_eeprom() < 0)
Heiko Schocherc0dcece2013-08-19 16:39:01 +020061 puts("Could not get board ID.\n");
62
63 enable_board_pin_mux();
64}
65
66void sdram_init(void)
67{
Enrico Letod89a97e2024-01-24 15:43:53 +010068 spl_draco_board_init();
69 draco_init_ddr();
Heiko Schocherc0dcece2013-08-19 16:39:01 +020070
71 return;
72}
73#endif /* #ifdef CONFIG_SPL_BUILD */
74
75#ifndef CONFIG_SPL_BUILD
76/*
77 * Basic board specific setup. Pinmux has been handled already.
78 */
79int board_init(void)
80{
81#if defined(CONFIG_HW_WATCHDOG)
82 hw_watchdog_init();
83#endif /* defined(CONFIG_HW_WATCHDOG) */
Enrico Letoe9ef9a12024-01-24 15:43:50 +010084 if (siemens_ee_setup() < 0)
Heiko Schocherc0dcece2013-08-19 16:39:01 +020085 puts("Could not get board ID.\n");
Heiko Schocher61159b72015-06-16 14:59:34 +020086#ifdef CONFIG_MACH_TYPE
Heiko Schocherc0dcece2013-08-19 16:39:01 +020087 gd->bd->bi_arch_number = CONFIG_MACH_TYPE;
Heiko Schocher61159b72015-06-16 14:59:34 +020088#endif
Tom Riniaa6e94d2022-11-16 13:10:37 -050089 gd->bd->bi_boot_params = CFG_SYS_SDRAM_BASE + 0x100;
Heiko Schocherc0dcece2013-08-19 16:39:01 +020090
91#ifdef CONFIG_FACTORYSET
Enrico Leto5ae54612024-01-24 15:43:49 +010092 factoryset_read_eeprom(SIEMENS_EE_I2C_ADDR);
Heiko Schocherc0dcece2013-08-19 16:39:01 +020093#endif
Heiko Schocher6b3943f2016-06-07 08:55:45 +020094
Heiko Schocherc0dcece2013-08-19 16:39:01 +020095 gpmc_init();
96
Heiko Schocherc0dcece2013-08-19 16:39:01 +020097 return 0;
98}
99#endif /* #ifndef CONFIG_SPL_BUILD */
100
101#define OSC (V_OSCK/1000000)
102const struct dpll_params dpll_ddr = {
103 DDR_PLL_FREQ, OSC-1, 1, -1, -1, -1, -1};
104
105const struct dpll_params *get_dpll_ddr_params(void)
106{
107 return &dpll_ddr;
108}
109
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200110#ifndef CONFIG_SPL_BUILD
Heiko Schocher61159b72015-06-16 14:59:34 +0200111
112#define MAX_NR_LEDS 10
113#define MAX_PIN_NUMBER 128
114#define STARTUP 0
115
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200116#if defined(BOARD_DFU_BUTTON_GPIO)
Heiko Schocher61159b72015-06-16 14:59:34 +0200117unsigned char get_button_state(char * const envname, unsigned char def)
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200118{
119 int button = 0;
120 int gpio;
Heiko Schocher61159b72015-06-16 14:59:34 +0200121 char *ptr_env;
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200122
Heiko Schocher61159b72015-06-16 14:59:34 +0200123 /* If button is not found we take default */
Simon Glass00caae62017-08-03 12:22:12 -0600124 ptr_env = env_get(envname);
Heiko Schocher61159b72015-06-16 14:59:34 +0200125 if (NULL == ptr_env) {
126 gpio = def;
127 } else {
128 gpio = (unsigned char)simple_strtoul(ptr_env, NULL, 0);
129 if (gpio > MAX_PIN_NUMBER)
130 gpio = def;
131 }
132
133 gpio_request(gpio, "");
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200134 gpio_direction_input(gpio);
135 if (gpio_get_value(gpio))
136 button = 1;
137 else
138 button = 0;
139
140 gpio_free(gpio);
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200141
142 return button;
143}
Heiko Schocher61159b72015-06-16 14:59:34 +0200144/**
145 * This command returns the status of the user button on
146 * Input - none
147 * Returns - 1 if button is held down
148 * 0 if button is not held down
149 */
150static int
Simon Glass09140112020-05-10 11:40:03 -0600151do_userbutton(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Heiko Schocher61159b72015-06-16 14:59:34 +0200152{
153 int button = 0;
154 button = get_button_state("button_dfu0", BOARD_DFU_BUTTON_GPIO);
155 button |= get_button_state("button_dfu1", BOARD_DFU_BUTTON_GPIO);
156 return button;
157}
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200158
159U_BOOT_CMD(
160 dfubutton, CONFIG_SYS_MAXARGS, 1, do_userbutton,
161 "Return the status of the DFU button",
162 ""
163);
164#endif
165
166static int
Simon Glass09140112020-05-10 11:40:03 -0600167do_usertestwdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200168{
169 printf("\n\n\n Go into infinite loop\n\n\n");
170 while (1)
171 ;
172 return 0;
173};
174
175U_BOOT_CMD(
176 testwdt, CONFIG_SYS_MAXARGS, 1, do_usertestwdt,
177 "Sends U-Boot into infinite loop",
178 ""
179);
Heiko Schocher61159b72015-06-16 14:59:34 +0200180
181/**
182 * Get led gpios from env and set them.
183 * The led define in environment need to need to be of the form ledN=NN,S0,S1
184 * where N is an unsigned integer from 0 to 9 and S0 and S1 is 0 or 1. S0
185 * defines the startup state of the led, S1 the special state of the led when
186 * it enters e.g. dfu mode.
187 */
188void set_env_gpios(unsigned char state)
189{
190 char *ptr_env;
191 char str_tmp[5]; /* must contain "ledX"*/
Heiko Schocher61159b72015-06-16 14:59:34 +0200192 unsigned char i, idx, pos1, pos2, ccount;
193 unsigned char gpio_n, gpio_s0, gpio_s1;
194
195 for (i = 0; i < MAX_NR_LEDS; i++) {
Heinrich Schuchardt3c7166d2019-08-22 21:58:26 +0200196 sprintf(str_tmp, "led%d", i);
Heiko Schocher61159b72015-06-16 14:59:34 +0200197
198 /* If env var is not found we stop */
Simon Glass00caae62017-08-03 12:22:12 -0600199 ptr_env = env_get(str_tmp);
Heiko Schocher61159b72015-06-16 14:59:34 +0200200 if (NULL == ptr_env)
201 break;
202
203 /* Find sperators position */
204 pos1 = 0;
205 pos2 = 0;
206 ccount = 0;
207 for (idx = 0; ptr_env[idx] != '\0'; idx++) {
208 if (ptr_env[idx] == ',') {
209 if (ccount++ < 1)
210 pos1 = idx;
211 else
212 pos2 = idx;
213 }
214 }
215 /* Bad led description skip this definition */
216 if (pos2 <= pos1 || ccount > 2)
217 continue;
218
219 /* Get pin number and request gpio */
220 memset(str_tmp, 0, sizeof(str_tmp));
221 strncpy(str_tmp, ptr_env, pos1*sizeof(char));
222 gpio_n = (unsigned char)simple_strtoul(str_tmp, NULL, 0);
223
224 /* Invalid gpio number skip definition */
225 if (gpio_n > MAX_PIN_NUMBER)
226 continue;
227
228 gpio_request(gpio_n, "");
229
230 if (state == STARTUP) {
231 /* get pin state 0 and set */
232 memset(str_tmp, 0, sizeof(str_tmp));
233 strncpy(str_tmp, ptr_env+pos1+1,
234 (pos2-pos1-1)*sizeof(char));
235 gpio_s0 = (unsigned char)simple_strtoul(str_tmp, NULL,
236 0);
237
238 gpio_direction_output(gpio_n, gpio_s0);
239
240 } else {
241 /* get pin state 1 and set */
242 memset(str_tmp, 0, sizeof(str_tmp));
243 strcpy(str_tmp, ptr_env+pos2+1);
244 gpio_s1 = (unsigned char)simple_strtoul(str_tmp, NULL,
245 0);
246 gpio_direction_output(gpio_n, gpio_s1);
247 }
248 } /* loop through defined led in environment */
249}
250
Simon Glass09140112020-05-10 11:40:03 -0600251static int do_board_led(struct cmd_tbl *cmdtp, int flag, int argc,
252 char *const argv[])
Heiko Schocher61159b72015-06-16 14:59:34 +0200253{
254 if (argc != 2)
255 return CMD_RET_USAGE;
256 if ((unsigned char)simple_strtoul(argv[1], NULL, 0) == STARTUP)
257 set_env_gpios(0);
258 else
259 set_env_gpios(1);
260 return 0;
261};
262
263U_BOOT_CMD(
264 draco_led, CONFIG_SYS_MAXARGS, 2, do_board_led,
265 "Set LEDs defined in environment",
266 "<0|1>"
267);
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200268#endif /* !CONFIG_SPL_BUILD */