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