blob: 1e501a09813dbc9516d97cd9a189c783d061013e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michael Walledccfa462012-06-05 11:33:17 +00002/*
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>
Michael Walledccfa462012-06-05 11:33:17 +00008 */
9
Simon Glass1ea97892020-05-10 11:40:00 -060010#include <bootstage.h>
Michael Walleb8666dd2022-08-17 21:38:04 +020011#include <button.h>
Simon Glassed38aef2020-05-10 11:40:03 -060012#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060013#include <env.h>
Simon Glass97589732020-05-10 11:40:02 -060014#include <init.h>
Michael Walleb8666dd2022-08-17 21:38:04 +020015#include <led.h>
16#include <power/regulator.h>
Simon Glassd34b4562014-10-13 23:42:04 -060017#include <spi.h>
18#include <spi_flash.h>
Michael Walledccfa462012-06-05 11:33:17 +000019#include <asm/arch/cpu.h>
20#include <asm/arch/mpp.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060021#include <asm/global_data.h>
Michael Walleb8666dd2022-08-17 21:38:04 +020022#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060023#include <linux/delay.h>
Michael Walledccfa462012-06-05 11:33:17 +000024
25#include "lsxl.h"
26
27/*
28 * Rescue mode
29 *
30 * Selected by holding the push button for 3 seconds, while powering on
31 * the device.
32 *
33 * These linkstations don't have a (populated) serial port. There is no
34 * way to access an (unmodified) board other than using the netconsole. If
35 * you want to recover from a bad environment setting or an empty environment,
36 * you can do this only with a working network connection. Therefore, a random
37 * ethernet address is generated if none is set and a DHCP request is sent.
38 * After a successful DHCP response is received, the network settings are
Michael Walledc3ba8e2012-10-04 06:54:25 +000039 * configured and the ncip is unset. Therefore, all netconsole packets are
40 * broadcasted.
Michael Walledccfa462012-06-05 11:33:17 +000041 * Additionally, the bootsource is set to 'rescue'.
42 */
43
Michael Walledccfa462012-06-05 11:33:17 +000044DECLARE_GLOBAL_DATA_PTR;
45
Michael Walle2e5bab12022-08-17 21:38:03 +020046static bool force_rescue_mode;
47
Michael Walledccfa462012-06-05 11:33:17 +000048int board_early_init_f(void)
49{
50 /*
51 * default gpio configuration
52 * There are maximum 64 gpios controlled through 2 sets of registers
53 * the below configuration configures mainly initial LED status
54 */
Stefan Roesec50ab392014-10-22 12:13:11 +020055 mvebu_config_gpio(LSXL_OE_VAL_LOW,
56 LSXL_OE_VAL_HIGH,
57 LSXL_OE_LOW, LSXL_OE_HIGH);
Michael Walledccfa462012-06-05 11:33:17 +000058
59 /*
60 * Multi-Purpose Pins Functionality configuration
61 * These strappings are taken from the original vendor uboot port.
62 */
Albert ARIBAUD4d424312012-11-26 11:27:36 +000063 static const u32 kwmpp_config[] = {
Michael Walledccfa462012-06-05 11:33:17 +000064 MPP0_SPI_SCn,
65 MPP1_SPI_MOSI,
66 MPP2_SPI_SCK,
67 MPP3_SPI_MISO,
68 MPP4_UART0_RXD,
69 MPP5_UART0_TXD,
70 MPP6_SYSRST_OUTn,
71 MPP7_GPO,
72 MPP8_GPIO,
73 MPP9_GPIO,
74 MPP10_GPO, /* HDD power */
75 MPP11_GPIO, /* USB Vbus enable */
76 MPP12_SD_CLK,
77 MPP13_SD_CMD,
78 MPP14_SD_D0,
79 MPP15_SD_D1,
80 MPP16_SD_D2,
81 MPP17_SD_D3,
82 MPP18_GPO, /* fan speed high */
83 MPP19_GPO, /* fan speed low */
84 MPP20_GE1_0,
85 MPP21_GE1_1,
86 MPP22_GE1_2,
87 MPP23_GE1_3,
88 MPP24_GE1_4,
89 MPP25_GE1_5,
90 MPP26_GE1_6,
91 MPP27_GE1_7,
92 MPP28_GPIO,
93 MPP29_GPIO,
94 MPP30_GE1_10,
95 MPP31_GE1_11,
96 MPP32_GE1_12,
97 MPP33_GE1_13,
98 MPP34_GPIO,
99 MPP35_GPIO,
100 MPP36_GPIO, /* function LED */
101 MPP37_GPIO, /* alarm LED */
102 MPP38_GPIO, /* info LED */
103 MPP39_GPIO, /* power LED */
104 MPP40_GPIO, /* fan alarm */
105 MPP41_GPIO, /* funtion button */
106 MPP42_GPIO, /* power switch */
107 MPP43_GPIO, /* power auto switch */
108 MPP44_GPIO,
109 MPP45_GPIO,
110 MPP46_GPIO,
111 MPP47_GPIO,
112 MPP48_GPIO, /* function red LED */
113 MPP49_GPIO,
114 0
115 };
116
117 kirkwood_mpp_conf(kwmpp_config, NULL);
118
119 return 0;
120}
121
Michael Walleb8666dd2022-08-17 21:38:04 +0200122enum {
123 LSXL_LED_OFF,
124 LSXL_LED_ALARM,
125 LSXL_LED_POWER,
126 LSXL_LED_INFO,
127};
Michael Walledccfa462012-06-05 11:33:17 +0000128
Michael Walleb8666dd2022-08-17 21:38:04 +0200129static void __set_led(int alarm, int info, int power)
Michael Walledccfa462012-06-05 11:33:17 +0000130{
Michael Walleb8666dd2022-08-17 21:38:04 +0200131 struct udevice *led;
132 int ret;
133
134 ret = led_get_by_label("lsxl:red:alarm", &led);
135 if (!ret)
136 led_set_state(led, alarm);
137 ret = led_get_by_label("lsxl:amber:info", &led);
138 if (!ret)
139 led_set_state(led, info);
140 ret = led_get_by_label("lsxl:blue:power", &led);
141 if (!ret)
142 led_set_state(led, power);
Michael Walledccfa462012-06-05 11:33:17 +0000143}
144
145static void set_led(int state)
146{
147 switch (state) {
Michael Walleb8666dd2022-08-17 21:38:04 +0200148 case LSXL_LED_OFF:
149 __set_led(0, 0, 0);
Michael Walledccfa462012-06-05 11:33:17 +0000150 break;
Michael Walleb8666dd2022-08-17 21:38:04 +0200151 case LSXL_LED_ALARM:
152 __set_led(1, 0, 0);
Michael Walledccfa462012-06-05 11:33:17 +0000153 break;
Michael Walleb8666dd2022-08-17 21:38:04 +0200154 case LSXL_LED_INFO:
155 __set_led(0, 1, 0);
Michael Walledccfa462012-06-05 11:33:17 +0000156 break;
Michael Walleb8666dd2022-08-17 21:38:04 +0200157 case LSXL_LED_POWER:
158 __set_led(0, 0, 1);
Michael Walledccfa462012-06-05 11:33:17 +0000159 break;
160 }
161}
162
163int board_init(void)
164{
165 /* address of boot parameters */
Stefan Roese0b741752014-10-22 12:13:13 +0200166 gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
Michael Walledccfa462012-06-05 11:33:17 +0000167
Michael Walleb8666dd2022-08-17 21:38:04 +0200168 set_led(LSXL_LED_POWER);
Michael Walledccfa462012-06-05 11:33:17 +0000169
170 return 0;
171}
172
Michael Walleb31f3842012-07-30 10:47:12 +0000173static void check_power_switch(void)
174{
Michael Walleb8666dd2022-08-17 21:38:04 +0200175 struct udevice *power_button, *hdd_power, *usb_power;
176 int ret;
177
178 ret = button_get_by_label("Power-on Switch", &power_button);
179 if (ret)
180 goto err;
181
182 ret = regulator_get_by_platname("HDD Power", &hdd_power);
183 if (ret)
184 goto err;
185
186 ret = regulator_get_by_platname("USB Power", &usb_power);
187 if (ret)
188 goto err;
189
190 if (button_get_state(power_button) == BUTTON_OFF) {
191 ret = regulator_set_enable(hdd_power, false);
192 if (ret)
193 goto err;
194 ret = regulator_set_enable(usb_power, false);
195 if (ret)
196 goto err;
197 /* TODO: fan off */
198 set_led(LSXL_LED_OFF);
Michael Walleb31f3842012-07-30 10:47:12 +0000199
200 /* loop until released */
Michael Walleb8666dd2022-08-17 21:38:04 +0200201 while (button_get_state(power_button) == BUTTON_OFF)
Michael Walleb31f3842012-07-30 10:47:12 +0000202 ;
203
204 /* turn power on again */
Michael Walleb8666dd2022-08-17 21:38:04 +0200205 ret = regulator_set_enable(hdd_power, true);
206 if (ret)
207 goto err;
208 ret = regulator_set_enable(usb_power, true);
209 if (ret)
210 goto err;
211 /* TODO: fan on */
212 set_led(LSXL_LED_POWER);
213 };
214
215 return;
216err:
217 printf("error in %s\n", __func__);
Michael Walleb31f3842012-07-30 10:47:12 +0000218}
219
Michael Walledccfa462012-06-05 11:33:17 +0000220void check_enetaddr(void)
221{
222 uchar enetaddr[6];
223
Simon Glass399a9ce2017-08-03 12:22:14 -0600224 if (!eth_env_get_enetaddr("ethaddr", enetaddr)) {
Michael Walledccfa462012-06-05 11:33:17 +0000225 /* signal unset/invalid ethaddr to user */
Michael Walleb8666dd2022-08-17 21:38:04 +0200226 set_led(LSXL_LED_INFO);
Michael Walledccfa462012-06-05 11:33:17 +0000227 }
228}
229
230static void erase_environment(void)
231{
232 struct spi_flash *flash;
233
234 printf("Erasing environment..\n");
235 flash = spi_flash_probe(0, 0, 1000000, SPI_MODE_3);
236 if (!flash) {
237 printf("Erasing flash failed\n");
238 return;
239 }
240
241 spi_flash_erase(flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE);
242 spi_flash_free(flash);
243 do_reset(NULL, 0, 0, NULL);
244}
245
246static void rescue_mode(void)
247{
Michael Walledccfa462012-06-05 11:33:17 +0000248 printf("Entering rescue mode..\n");
Simon Glass6a38e412017-08-03 12:22:09 -0600249 env_set("bootsource", "rescue");
Michael Walledccfa462012-06-05 11:33:17 +0000250}
251
252static void check_push_button(void)
253{
Michael Walleb8666dd2022-08-17 21:38:04 +0200254 struct udevice *func_button;
Michael Walledccfa462012-06-05 11:33:17 +0000255 int i = 0;
256
Michael Walleb8666dd2022-08-17 21:38:04 +0200257 int ret;
258
259 ret = button_get_by_label("Function Button", &func_button);
260 if (ret)
261 goto err;
262
263 while (button_get_state(func_button) == BUTTON_ON) {
Michael Walledccfa462012-06-05 11:33:17 +0000264 udelay(100000);
265 i++;
266
267 if (i == 10)
Michael Walleb8666dd2022-08-17 21:38:04 +0200268 set_led(LSXL_LED_INFO);
Michael Walledccfa462012-06-05 11:33:17 +0000269
270 if (i >= 100) {
Michael Walleb8666dd2022-08-17 21:38:04 +0200271 set_led(LSXL_LED_ALARM);
Michael Walledccfa462012-06-05 11:33:17 +0000272 break;
273 }
274 }
275
276 if (i >= 100)
277 erase_environment();
278 else if (i >= 10)
Michael Walle2e5bab12022-08-17 21:38:03 +0200279 force_rescue_mode = true;
Michael Walleb8666dd2022-08-17 21:38:04 +0200280
281 return;
282err:
283 printf("error in %s\n", __func__);
Michael Walle2e5bab12022-08-17 21:38:03 +0200284}
285
286int board_early_init_r(void)
287{
288 check_push_button();
289
290 return 0;
Michael Walledccfa462012-06-05 11:33:17 +0000291}
292
293int misc_init_r(void)
294{
Michael Walleb31f3842012-07-30 10:47:12 +0000295 check_power_switch();
Michael Walledccfa462012-06-05 11:33:17 +0000296 check_enetaddr();
Michael Walle2e5bab12022-08-17 21:38:03 +0200297 if (force_rescue_mode)
298 rescue_mode();
Michael Walledccfa462012-06-05 11:33:17 +0000299
300 return 0;
301}
Michael Walledccfa462012-06-05 11:33:17 +0000302
Tom Rinia9765d02021-05-03 16:48:58 -0400303#if CONFIG_IS_ENABLED(BOOTSTAGE)
Michael Walledccfa462012-06-05 11:33:17 +0000304void show_boot_progress(int progress)
305{
306 if (progress > 0)
307 return;
308
309 /* this is not an error, eg. bootp with autoload=no will trigger this */
310 if (progress == -BOOTSTAGE_ID_NET_LOADED)
311 return;
312
Michael Walleb8666dd2022-08-17 21:38:04 +0200313 set_led(LSXL_LED_ALARM);
Michael Walledccfa462012-06-05 11:33:17 +0000314}
315#endif