1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2012 Michael Walle
4  * Michael Walle <michael@walle.cc>
5  *
6  * Based on sheevaplug/sheevaplug.c by
7  *   Marvell Semiconductor <www.marvell.com>
8  */
9 
10 #include <common.h>
11 #include <bootstage.h>
12 #include <button.h>
13 #include <command.h>
14 #include <env.h>
15 #include <init.h>
16 #include <led.h>
17 #include <power/regulator.h>
18 #include <spi.h>
19 #include <spi_flash.h>
20 #include <asm/arch/cpu.h>
21 #include <asm/arch/mpp.h>
22 #include <asm/global_data.h>
23 #include <asm/io.h>
24 #include <linux/delay.h>
25 
26 #include "lsxl.h"
27 
28 /*
29  * Rescue mode
30  *
31  * Selected by holding the push button for 3 seconds, while powering on
32  * the device.
33  *
34  * These linkstations don't have a (populated) serial port. There is no
35  * way to access an (unmodified) board other than using the netconsole. If
36  * you want to recover from a bad environment setting or an empty environment,
37  * you can do this only with a working network connection. Therefore, a random
38  * ethernet address is generated if none is set and a DHCP request is sent.
39  * After a successful DHCP response is received, the network settings are
40  * configured and the ncip is unset. Therefore, all netconsole packets are
41  * broadcasted.
42  * Additionally, the bootsource is set to 'rescue'.
43  */
44 
45 DECLARE_GLOBAL_DATA_PTR;
46 
47 static bool force_rescue_mode;
48 
board_early_init_f(void)49 int board_early_init_f(void)
50 {
51 	/*
52 	 * default gpio configuration
53 	 * There are maximum 64 gpios controlled through 2 sets of registers
54 	 * the below configuration configures mainly initial LED status
55 	 */
56 	mvebu_config_gpio(LSXL_OE_VAL_LOW,
57 			  LSXL_OE_VAL_HIGH,
58 			  LSXL_OE_LOW, LSXL_OE_HIGH);
59 
60 	/*
61 	 * Multi-Purpose Pins Functionality configuration
62 	 * These strappings are taken from the original vendor uboot port.
63 	 */
64 	static const u32 kwmpp_config[] = {
65 		MPP0_SPI_SCn,
66 		MPP1_SPI_MOSI,
67 		MPP2_SPI_SCK,
68 		MPP3_SPI_MISO,
69 		MPP4_UART0_RXD,
70 		MPP5_UART0_TXD,
71 		MPP6_SYSRST_OUTn,
72 		MPP7_GPO,
73 		MPP8_GPIO,
74 		MPP9_GPIO,
75 		MPP10_GPO,		/* HDD power */
76 		MPP11_GPIO,		/* USB Vbus enable */
77 		MPP12_SD_CLK,
78 		MPP13_SD_CMD,
79 		MPP14_SD_D0,
80 		MPP15_SD_D1,
81 		MPP16_SD_D2,
82 		MPP17_SD_D3,
83 		MPP18_GPO,		/* fan speed high */
84 		MPP19_GPO,		/* fan speed low */
85 		MPP20_GE1_0,
86 		MPP21_GE1_1,
87 		MPP22_GE1_2,
88 		MPP23_GE1_3,
89 		MPP24_GE1_4,
90 		MPP25_GE1_5,
91 		MPP26_GE1_6,
92 		MPP27_GE1_7,
93 		MPP28_GPIO,
94 		MPP29_GPIO,
95 		MPP30_GE1_10,
96 		MPP31_GE1_11,
97 		MPP32_GE1_12,
98 		MPP33_GE1_13,
99 		MPP34_GPIO,
100 		MPP35_GPIO,
101 		MPP36_GPIO,		/* function LED */
102 		MPP37_GPIO,		/* alarm LED */
103 		MPP38_GPIO,		/* info LED */
104 		MPP39_GPIO,		/* power LED */
105 		MPP40_GPIO,		/* fan alarm */
106 		MPP41_GPIO,		/* funtion button */
107 		MPP42_GPIO,		/* power switch */
108 		MPP43_GPIO,		/* power auto switch */
109 		MPP44_GPIO,
110 		MPP45_GPIO,
111 		MPP46_GPIO,
112 		MPP47_GPIO,
113 		MPP48_GPIO,		/* function red LED */
114 		MPP49_GPIO,
115 		0
116 	};
117 
118 	kirkwood_mpp_conf(kwmpp_config, NULL);
119 
120 	return 0;
121 }
122 
123 enum {
124 	LSXL_LED_OFF,
125 	LSXL_LED_ALARM,
126 	LSXL_LED_POWER,
127 	LSXL_LED_INFO,
128 };
129 
__set_led(int alarm,int info,int power)130 static void __set_led(int alarm, int info, int power)
131 {
132 	struct udevice *led;
133 	int ret;
134 
135 	ret = led_get_by_label("lsxl:red:alarm", &led);
136 	if (!ret)
137 		led_set_state(led, alarm);
138 	ret = led_get_by_label("lsxl:amber:info", &led);
139 	if (!ret)
140 		led_set_state(led, info);
141 	ret = led_get_by_label("lsxl:blue:power", &led);
142 	if (!ret)
143 		led_set_state(led, power);
144 }
145 
set_led(int state)146 static void set_led(int state)
147 {
148 	switch (state) {
149 	case LSXL_LED_OFF:
150 		__set_led(0, 0, 0);
151 		break;
152 	case LSXL_LED_ALARM:
153 		__set_led(1, 0, 0);
154 		break;
155 	case LSXL_LED_INFO:
156 		__set_led(0, 1, 0);
157 		break;
158 	case LSXL_LED_POWER:
159 		__set_led(0, 0, 1);
160 		break;
161 	}
162 }
163 
board_init(void)164 int board_init(void)
165 {
166 	/* address of boot parameters */
167 	gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
168 
169 	set_led(LSXL_LED_POWER);
170 
171 	return 0;
172 }
173 
check_power_switch(void)174 static void check_power_switch(void)
175 {
176 	struct udevice *power_button, *hdd_power, *usb_power;
177 	int ret;
178 
179 	ret = button_get_by_label("Power-on Switch", &power_button);
180 	if (ret)
181 		goto err;
182 
183 	ret = regulator_get_by_platname("HDD Power", &hdd_power);
184 	if (ret)
185 		goto err;
186 
187 	ret = regulator_get_by_platname("USB Power", &usb_power);
188 	if (ret)
189 		goto err;
190 
191 	if (button_get_state(power_button) == BUTTON_OFF) {
192 		ret = regulator_set_enable(hdd_power, false);
193 		if (ret)
194 			goto err;
195 		ret = regulator_set_enable(usb_power, false);
196 		if (ret)
197 			goto err;
198 		/* TODO: fan off */
199 		set_led(LSXL_LED_OFF);
200 
201 		/* loop until released */
202 		while (button_get_state(power_button) == BUTTON_OFF)
203 			;
204 
205 		/* turn power on again */
206 		ret = regulator_set_enable(hdd_power, true);
207 		if (ret)
208 			goto err;
209 		ret = regulator_set_enable(usb_power, true);
210 		if (ret)
211 			goto err;
212 		/* TODO: fan on */
213 		set_led(LSXL_LED_POWER);
214 	};
215 
216 	return;
217 err:
218 	printf("error in %s\n", __func__);
219 }
220 
check_enetaddr(void)221 void check_enetaddr(void)
222 {
223 	uchar enetaddr[6];
224 
225 	if (!eth_env_get_enetaddr("ethaddr", enetaddr)) {
226 		/* signal unset/invalid ethaddr to user */
227 		set_led(LSXL_LED_INFO);
228 	}
229 }
230 
erase_environment(void)231 static void erase_environment(void)
232 {
233 	struct spi_flash *flash;
234 
235 	printf("Erasing environment..\n");
236 	flash = spi_flash_probe(0, 0, 1000000, SPI_MODE_3);
237 	if (!flash) {
238 		printf("Erasing flash failed\n");
239 		return;
240 	}
241 
242 	spi_flash_erase(flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE);
243 	spi_flash_free(flash);
244 	do_reset(NULL, 0, 0, NULL);
245 }
246 
rescue_mode(void)247 static void rescue_mode(void)
248 {
249 	printf("Entering rescue mode..\n");
250 	env_set("bootsource", "rescue");
251 }
252 
check_push_button(void)253 static void check_push_button(void)
254 {
255 	struct udevice *func_button;
256 	int i = 0;
257 
258 	int ret;
259 
260 	ret = button_get_by_label("Function Button", &func_button);
261 	if (ret)
262 		goto err;
263 
264 	while (button_get_state(func_button) == BUTTON_ON) {
265 		udelay(100000);
266 		i++;
267 
268 		if (i == 10)
269 			set_led(LSXL_LED_INFO);
270 
271 		if (i >= 100) {
272 			set_led(LSXL_LED_ALARM);
273 			break;
274 		}
275 	}
276 
277 	if (i >= 100)
278 		erase_environment();
279 	else if (i >= 10)
280 		force_rescue_mode = true;
281 
282 	return;
283 err:
284 	printf("error in %s\n", __func__);
285 }
286 
board_early_init_r(void)287 int board_early_init_r(void)
288 {
289 	check_push_button();
290 
291 	return 0;
292 }
293 
misc_init_r(void)294 int misc_init_r(void)
295 {
296 	check_power_switch();
297 	check_enetaddr();
298 	if (force_rescue_mode)
299 		rescue_mode();
300 
301 	return 0;
302 }
303 
304 #if CONFIG_IS_ENABLED(BOOTSTAGE)
show_boot_progress(int progress)305 void show_boot_progress(int progress)
306 {
307 	if (progress > 0)
308 		return;
309 
310 	/* this is not an error, eg. bootp with autoload=no will trigger this */
311 	if (progress == -BOOTSTAGE_ID_NET_LOADED)
312 		return;
313 
314 	set_led(LSXL_LED_ALARM);
315 }
316 #endif
317