blob: d077751cbe155e215c2b6d58822eb88583c42c9d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher499c4982013-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 Menoneaa39c62023-11-01 15:56:03 -05009 * Copyright (C) 2011, Texas Instruments, Incorporated - https://www.ti.com/
Heiko Schocher499c4982013-08-19 16:39:01 +020010 */
11
12#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -060013#include <command.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060014#include <env.h>
Heiko Schocher499c4982013-08-19 16:39:01 +020015#include <errno.h>
Simon Glass97589732020-05-10 11:40:02 -060016#include <init.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <malloc.h>
Simon Glass36736182019-11-14 12:57:24 -070018#include <serial.h>
Heiko Schocher499c4982013-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 Glass3ba929a2020-10-30 21:38:53 -060028#include <asm/global_data.h>
Heiko Schocher499c4982013-08-19 16:39:01 +020029#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 <watchdog.h>
Simon Glass0ffb9d62017-05-31 19:47:48 -060036#include <asm/mach-types.h>
Heiko Schocher499c4982013-08-19 16:39:01 +020037#include "../common/factoryset.h"
38
39DECLARE_GLOBAL_DATA_PTR;
40
41#ifdef CONFIG_SPL_BUILD
42void set_uart_mux_conf(void)
43{
44 enable_uart0_pin_mux();
45}
46
47void set_mux_conf_regs(void)
48{
49 /* Initalize the board header */
50 enable_i2c0_pin_mux();
Heiko Schocherf53f2b82013-10-22 11:03:18 +020051 i2c_set_bus_num(0);
Heiko Schocherb5e7d372015-05-28 07:27:36 +020052
53 /* enable early the console */
54 gd->baudrate = CONFIG_BAUDRATE;
55 serial_init();
56 gd->have_console = 1;
Heiko Schocher499c4982013-08-19 16:39:01 +020057 if (read_eeprom() < 0)
58 puts("Could not get board ID.\n");
59
60 enable_board_pin_mux();
61}
62
63void sdram_init(void)
64{
65 spl_siemens_board_init();
66 board_init_ddr();
67
68 return;
69}
70#endif /* #ifdef CONFIG_SPL_BUILD */
71
72#ifndef CONFIG_SPL_BUILD
Tom Rini1cfc1272021-08-17 17:59:40 -040073#define FACTORYSET_EEPROM_ADDR 0x50
Heiko Schocher499c4982013-08-19 16:39:01 +020074/*
75 * Basic board specific setup. Pinmux has been handled already.
76 */
77int board_init(void)
78{
79#if defined(CONFIG_HW_WATCHDOG)
80 hw_watchdog_init();
81#endif /* defined(CONFIG_HW_WATCHDOG) */
Heiko Schocherf53f2b82013-10-22 11:03:18 +020082 i2c_set_bus_num(0);
Heiko Schocher499c4982013-08-19 16:39:01 +020083 if (read_eeprom() < 0)
84 puts("Could not get board ID.\n");
Heiko Schocherd17c3fc2015-06-16 14:59:34 +020085#ifdef CONFIG_MACH_TYPE
Heiko Schocher499c4982013-08-19 16:39:01 +020086 gd->bd->bi_arch_number = CONFIG_MACH_TYPE;
Heiko Schocherd17c3fc2015-06-16 14:59:34 +020087#endif
Tom Rinibb4dd962022-11-16 13:10:37 -050088 gd->bd->bi_boot_params = CFG_SYS_SDRAM_BASE + 0x100;
Heiko Schocher499c4982013-08-19 16:39:01 +020089
90#ifdef CONFIG_FACTORYSET
Tom Rini1cfc1272021-08-17 17:59:40 -040091 factoryset_read_eeprom(FACTORYSET_EEPROM_ADDR);
Heiko Schocher499c4982013-08-19 16:39:01 +020092#endif
Heiko Schochercbec11a2016-06-07 08:55:45 +020093
Heiko Schocher499c4982013-08-19 16:39:01 +020094 gpmc_init();
95
Tom Rini4044a7a2022-12-02 16:42:38 -050096#if CONFIG_IS_ENABLED(NAND_CS_INIT)
Heiko Schochercbec11a2016-06-07 08:55:45 +020097 board_nand_cs_init();
98#endif
Heiko Schocher499c4982013-08-19 16:39:01 +020099
100 return 0;
101}
102#endif /* #ifndef CONFIG_SPL_BUILD */
103
104#define OSC (V_OSCK/1000000)
105const struct dpll_params dpll_ddr = {
106 DDR_PLL_FREQ, OSC-1, 1, -1, -1, -1, -1};
107
108const struct dpll_params *get_dpll_ddr_params(void)
109{
110 return &dpll_ddr;
111}
112
Heiko Schocher499c4982013-08-19 16:39:01 +0200113#ifndef CONFIG_SPL_BUILD
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200114
115#define MAX_NR_LEDS 10
116#define MAX_PIN_NUMBER 128
117#define STARTUP 0
118
Heiko Schocher499c4982013-08-19 16:39:01 +0200119#if defined(BOARD_DFU_BUTTON_GPIO)
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200120unsigned char get_button_state(char * const envname, unsigned char def)
Heiko Schocher499c4982013-08-19 16:39:01 +0200121{
122 int button = 0;
123 int gpio;
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200124 char *ptr_env;
Heiko Schocher499c4982013-08-19 16:39:01 +0200125
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200126 /* If button is not found we take default */
Simon Glass64b723f2017-08-03 12:22:12 -0600127 ptr_env = env_get(envname);
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200128 if (NULL == ptr_env) {
129 gpio = def;
130 } else {
131 gpio = (unsigned char)simple_strtoul(ptr_env, NULL, 0);
132 if (gpio > MAX_PIN_NUMBER)
133 gpio = def;
134 }
135
136 gpio_request(gpio, "");
Heiko Schocher499c4982013-08-19 16:39:01 +0200137 gpio_direction_input(gpio);
138 if (gpio_get_value(gpio))
139 button = 1;
140 else
141 button = 0;
142
143 gpio_free(gpio);
Heiko Schocher499c4982013-08-19 16:39:01 +0200144
145 return button;
146}
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200147/**
148 * This command returns the status of the user button on
149 * Input - none
150 * Returns - 1 if button is held down
151 * 0 if button is not held down
Egli, Samuelfdb5e262014-04-24 17:57:54 +0200152 */
153static int
Simon Glassed38aef2020-05-10 11:40:03 -0600154do_userbutton(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Egli, Samuelfdb5e262014-04-24 17:57:54 +0200155{
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200156 int button = 0;
157 button = get_button_state("button_dfu0", BOARD_DFU_BUTTON_GPIO);
158 button |= get_button_state("button_dfu1", BOARD_DFU_BUTTON_GPIO);
159 return button;
Egli, Samuelfdb5e262014-04-24 17:57:54 +0200160}
161
162U_BOOT_CMD(
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200163 dfubutton, CONFIG_SYS_MAXARGS, 1, do_userbutton,
164 "Return the status of the DFU button",
165 ""
Egli, Samuelfdb5e262014-04-24 17:57:54 +0200166);
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200167#endif
Heiko Schocher499c4982013-08-19 16:39:01 +0200168
169static int
Simon Glassed38aef2020-05-10 11:40:03 -0600170do_usertestwdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Heiko Schocher499c4982013-08-19 16:39:01 +0200171{
172 printf("\n\n\n Go into infinite loop\n\n\n");
173 while (1)
174 ;
175 return 0;
176};
177
178U_BOOT_CMD(
179 testwdt, CONFIG_SYS_MAXARGS, 1, do_usertestwdt,
180 "Sends U-Boot into infinite loop",
181 ""
182);
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200183
184/**
185 * Get led gpios from env and set them.
186 * The led define in environment need to need to be of the form ledN=NN,S0,S1
187 * where N is an unsigned integer from 0 to 9 and S0 and S1 is 0 or 1. S0
188 * defines the startup state of the led, S1 the special state of the led when
189 * it enters e.g. dfu mode.
190 */
191void set_env_gpios(unsigned char state)
192{
193 char *ptr_env;
194 char str_tmp[5]; /* must contain "ledX"*/
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200195 unsigned char i, idx, pos1, pos2, ccount;
196 unsigned char gpio_n, gpio_s0, gpio_s1;
197
198 for (i = 0; i < MAX_NR_LEDS; i++) {
Heinrich Schuchardt32c63b62019-08-22 21:58:26 +0200199 sprintf(str_tmp, "led%d", i);
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200200
201 /* If env var is not found we stop */
Simon Glass64b723f2017-08-03 12:22:12 -0600202 ptr_env = env_get(str_tmp);
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200203 if (NULL == ptr_env)
204 break;
205
206 /* Find sperators position */
207 pos1 = 0;
208 pos2 = 0;
209 ccount = 0;
210 for (idx = 0; ptr_env[idx] != '\0'; idx++) {
211 if (ptr_env[idx] == ',') {
212 if (ccount++ < 1)
213 pos1 = idx;
214 else
215 pos2 = idx;
216 }
217 }
218 /* Bad led description skip this definition */
219 if (pos2 <= pos1 || ccount > 2)
220 continue;
221
222 /* Get pin number and request gpio */
223 memset(str_tmp, 0, sizeof(str_tmp));
224 strncpy(str_tmp, ptr_env, pos1*sizeof(char));
225 gpio_n = (unsigned char)simple_strtoul(str_tmp, NULL, 0);
226
227 /* Invalid gpio number skip definition */
228 if (gpio_n > MAX_PIN_NUMBER)
229 continue;
230
231 gpio_request(gpio_n, "");
232
233 if (state == STARTUP) {
234 /* get pin state 0 and set */
235 memset(str_tmp, 0, sizeof(str_tmp));
236 strncpy(str_tmp, ptr_env+pos1+1,
237 (pos2-pos1-1)*sizeof(char));
238 gpio_s0 = (unsigned char)simple_strtoul(str_tmp, NULL,
239 0);
240
241 gpio_direction_output(gpio_n, gpio_s0);
242
243 } else {
244 /* get pin state 1 and set */
245 memset(str_tmp, 0, sizeof(str_tmp));
246 strcpy(str_tmp, ptr_env+pos2+1);
247 gpio_s1 = (unsigned char)simple_strtoul(str_tmp, NULL,
248 0);
249 gpio_direction_output(gpio_n, gpio_s1);
250 }
251 } /* loop through defined led in environment */
252}
253
Simon Glassed38aef2020-05-10 11:40:03 -0600254static int do_board_led(struct cmd_tbl *cmdtp, int flag, int argc,
255 char *const argv[])
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200256{
257 if (argc != 2)
258 return CMD_RET_USAGE;
259 if ((unsigned char)simple_strtoul(argv[1], NULL, 0) == STARTUP)
260 set_env_gpios(0);
261 else
262 set_env_gpios(1);
263 return 0;
264};
265
266U_BOOT_CMD(
267 draco_led, CONFIG_SYS_MAXARGS, 2, do_board_led,
268 "Set LEDs defined in environment",
269 "<0|1>"
270);
Heiko Schocher499c4982013-08-19 16:39:01 +0200271#endif /* !CONFIG_SPL_BUILD */