blob: e0bd8590bc9d45a16e950ad93b18c8063df17bae [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
9 * Copyright (C) 2011, Texas Instruments, Incorporated - http://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>
28#include <asm/io.h>
29#include <asm/emif.h>
30#include <asm/gpio.h>
31#include <i2c.h>
32#include <miiphy.h>
33#include <cpsw.h>
34#include <watchdog.h>
Simon Glass0ffb9d62017-05-31 19:47:48 -060035#include <asm/mach-types.h>
Heiko Schocher499c4982013-08-19 16:39:01 +020036#include "../common/factoryset.h"
37
38DECLARE_GLOBAL_DATA_PTR;
39
40#ifdef CONFIG_SPL_BUILD
41void set_uart_mux_conf(void)
42{
43 enable_uart0_pin_mux();
44}
45
46void set_mux_conf_regs(void)
47{
48 /* Initalize the board header */
49 enable_i2c0_pin_mux();
Heiko Schocherf53f2b82013-10-22 11:03:18 +020050 i2c_set_bus_num(0);
Heiko Schocherb5e7d372015-05-28 07:27:36 +020051
52 /* enable early the console */
53 gd->baudrate = CONFIG_BAUDRATE;
54 serial_init();
55 gd->have_console = 1;
Heiko Schocher499c4982013-08-19 16:39:01 +020056 if (read_eeprom() < 0)
57 puts("Could not get board ID.\n");
58
59 enable_board_pin_mux();
60}
61
62void sdram_init(void)
63{
64 spl_siemens_board_init();
65 board_init_ddr();
66
67 return;
68}
69#endif /* #ifdef CONFIG_SPL_BUILD */
70
71#ifndef CONFIG_SPL_BUILD
72/*
73 * Basic board specific setup. Pinmux has been handled already.
74 */
75int board_init(void)
76{
77#if defined(CONFIG_HW_WATCHDOG)
78 hw_watchdog_init();
79#endif /* defined(CONFIG_HW_WATCHDOG) */
Heiko Schocherf53f2b82013-10-22 11:03:18 +020080 i2c_set_bus_num(0);
Heiko Schocher499c4982013-08-19 16:39:01 +020081 if (read_eeprom() < 0)
82 puts("Could not get board ID.\n");
Heiko Schocherd17c3fc2015-06-16 14:59:34 +020083#ifdef CONFIG_MACH_TYPE
Heiko Schocher499c4982013-08-19 16:39:01 +020084 gd->bd->bi_arch_number = CONFIG_MACH_TYPE;
Heiko Schocherd17c3fc2015-06-16 14:59:34 +020085#endif
Heiko Schocher499c4982013-08-19 16:39:01 +020086 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
87
88#ifdef CONFIG_FACTORYSET
89 factoryset_read_eeprom(CONFIG_SYS_I2C_EEPROM_ADDR);
90#endif
Heiko Schochercbec11a2016-06-07 08:55:45 +020091
Heiko Schocher499c4982013-08-19 16:39:01 +020092 gpmc_init();
93
Heiko Schochercbec11a2016-06-07 08:55:45 +020094#ifdef CONFIG_NAND_CS_INIT
95 board_nand_cs_init();
96#endif
Heiko Schocher499c4982013-08-19 16:39:01 +020097#ifdef CONFIG_VIDEO
98 board_video_init();
99#endif
100
101 return 0;
102}
103#endif /* #ifndef CONFIG_SPL_BUILD */
104
105#define OSC (V_OSCK/1000000)
106const struct dpll_params dpll_ddr = {
107 DDR_PLL_FREQ, OSC-1, 1, -1, -1, -1, -1};
108
109const struct dpll_params *get_dpll_ddr_params(void)
110{
111 return &dpll_ddr;
112}
113
Heiko Schocher499c4982013-08-19 16:39:01 +0200114#ifndef CONFIG_SPL_BUILD
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200115
116#define MAX_NR_LEDS 10
117#define MAX_PIN_NUMBER 128
118#define STARTUP 0
119
Heiko Schocher499c4982013-08-19 16:39:01 +0200120#if defined(BOARD_DFU_BUTTON_GPIO)
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200121unsigned char get_button_state(char * const envname, unsigned char def)
Heiko Schocher499c4982013-08-19 16:39:01 +0200122{
123 int button = 0;
124 int gpio;
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200125 char *ptr_env;
Heiko Schocher499c4982013-08-19 16:39:01 +0200126
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200127 /* If button is not found we take default */
Simon Glass64b723f2017-08-03 12:22:12 -0600128 ptr_env = env_get(envname);
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200129 if (NULL == ptr_env) {
130 gpio = def;
131 } else {
132 gpio = (unsigned char)simple_strtoul(ptr_env, NULL, 0);
133 if (gpio > MAX_PIN_NUMBER)
134 gpio = def;
135 }
136
137 gpio_request(gpio, "");
Heiko Schocher499c4982013-08-19 16:39:01 +0200138 gpio_direction_input(gpio);
139 if (gpio_get_value(gpio))
140 button = 1;
141 else
142 button = 0;
143
144 gpio_free(gpio);
Heiko Schocher499c4982013-08-19 16:39:01 +0200145
146 return button;
147}
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200148/**
149 * This command returns the status of the user button on
150 * Input - none
151 * Returns - 1 if button is held down
152 * 0 if button is not held down
Egli, Samuelfdb5e262014-04-24 17:57:54 +0200153 */
154static int
Simon Glassed38aef2020-05-10 11:40:03 -0600155do_userbutton(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Egli, Samuelfdb5e262014-04-24 17:57:54 +0200156{
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200157 int button = 0;
158 button = get_button_state("button_dfu0", BOARD_DFU_BUTTON_GPIO);
159 button |= get_button_state("button_dfu1", BOARD_DFU_BUTTON_GPIO);
160 return button;
Egli, Samuelfdb5e262014-04-24 17:57:54 +0200161}
162
163U_BOOT_CMD(
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200164 dfubutton, CONFIG_SYS_MAXARGS, 1, do_userbutton,
165 "Return the status of the DFU button",
166 ""
Egli, Samuelfdb5e262014-04-24 17:57:54 +0200167);
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200168#endif
Heiko Schocher499c4982013-08-19 16:39:01 +0200169
170static int
Simon Glassed38aef2020-05-10 11:40:03 -0600171do_usertestwdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Heiko Schocher499c4982013-08-19 16:39:01 +0200172{
173 printf("\n\n\n Go into infinite loop\n\n\n");
174 while (1)
175 ;
176 return 0;
177};
178
179U_BOOT_CMD(
180 testwdt, CONFIG_SYS_MAXARGS, 1, do_usertestwdt,
181 "Sends U-Boot into infinite loop",
182 ""
183);
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200184
185/**
186 * Get led gpios from env and set them.
187 * The led define in environment need to need to be of the form ledN=NN,S0,S1
188 * where N is an unsigned integer from 0 to 9 and S0 and S1 is 0 or 1. S0
189 * defines the startup state of the led, S1 the special state of the led when
190 * it enters e.g. dfu mode.
191 */
192void set_env_gpios(unsigned char state)
193{
194 char *ptr_env;
195 char str_tmp[5]; /* must contain "ledX"*/
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200196 unsigned char i, idx, pos1, pos2, ccount;
197 unsigned char gpio_n, gpio_s0, gpio_s1;
198
199 for (i = 0; i < MAX_NR_LEDS; i++) {
Heinrich Schuchardt32c63b62019-08-22 21:58:26 +0200200 sprintf(str_tmp, "led%d", i);
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200201
202 /* If env var is not found we stop */
Simon Glass64b723f2017-08-03 12:22:12 -0600203 ptr_env = env_get(str_tmp);
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200204 if (NULL == ptr_env)
205 break;
206
207 /* Find sperators position */
208 pos1 = 0;
209 pos2 = 0;
210 ccount = 0;
211 for (idx = 0; ptr_env[idx] != '\0'; idx++) {
212 if (ptr_env[idx] == ',') {
213 if (ccount++ < 1)
214 pos1 = idx;
215 else
216 pos2 = idx;
217 }
218 }
219 /* Bad led description skip this definition */
220 if (pos2 <= pos1 || ccount > 2)
221 continue;
222
223 /* Get pin number and request gpio */
224 memset(str_tmp, 0, sizeof(str_tmp));
225 strncpy(str_tmp, ptr_env, pos1*sizeof(char));
226 gpio_n = (unsigned char)simple_strtoul(str_tmp, NULL, 0);
227
228 /* Invalid gpio number skip definition */
229 if (gpio_n > MAX_PIN_NUMBER)
230 continue;
231
232 gpio_request(gpio_n, "");
233
234 if (state == STARTUP) {
235 /* get pin state 0 and set */
236 memset(str_tmp, 0, sizeof(str_tmp));
237 strncpy(str_tmp, ptr_env+pos1+1,
238 (pos2-pos1-1)*sizeof(char));
239 gpio_s0 = (unsigned char)simple_strtoul(str_tmp, NULL,
240 0);
241
242 gpio_direction_output(gpio_n, gpio_s0);
243
244 } else {
245 /* get pin state 1 and set */
246 memset(str_tmp, 0, sizeof(str_tmp));
247 strcpy(str_tmp, ptr_env+pos2+1);
248 gpio_s1 = (unsigned char)simple_strtoul(str_tmp, NULL,
249 0);
250 gpio_direction_output(gpio_n, gpio_s1);
251 }
252 } /* loop through defined led in environment */
253}
254
Simon Glassed38aef2020-05-10 11:40:03 -0600255static int do_board_led(struct cmd_tbl *cmdtp, int flag, int argc,
256 char *const argv[])
Heiko Schocherd17c3fc2015-06-16 14:59:34 +0200257{
258 if (argc != 2)
259 return CMD_RET_USAGE;
260 if ((unsigned char)simple_strtoul(argv[1], NULL, 0) == STARTUP)
261 set_env_gpios(0);
262 else
263 set_env_gpios(1);
264 return 0;
265};
266
267U_BOOT_CMD(
268 draco_led, CONFIG_SYS_MAXARGS, 2, do_board_led,
269 "Set LEDs defined in environment",
270 "<0|1>"
271);
Heiko Schocher499c4982013-08-19 16:39:01 +0200272#endif /* !CONFIG_SPL_BUILD */