blob: 6a866b5470d6e1fd3746c6cb047c47cb24b35a8c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michael Wallef214a202012-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 Wallef214a202012-06-05 11:33:17 +00008 */
9
10#include <common.h>
Simon Glass52f24232020-05-10 11:40:00 -060011#include <bootstage.h>
Michael Walle7c9bd922022-08-17 21:38:04 +020012#include <button.h>
Simon Glass09140112020-05-10 11:40:03 -060013#include <command.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060014#include <env.h>
Simon Glass691d7192020-05-10 11:40:02 -060015#include <init.h>
Michael Walle7c9bd922022-08-17 21:38:04 +020016#include <led.h>
17#include <power/regulator.h>
Simon Glassff0960f2014-10-13 23:42:04 -060018#include <spi.h>
19#include <spi_flash.h>
Michael Wallef214a202012-06-05 11:33:17 +000020#include <asm/arch/cpu.h>
21#include <asm/arch/mpp.h>
Simon Glass401d1c42020-10-30 21:38:53 -060022#include <asm/global_data.h>
Michael Walle7c9bd922022-08-17 21:38:04 +020023#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060024#include <linux/delay.h>
Michael Wallef214a202012-06-05 11:33:17 +000025
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
Michael Walle23c99462012-10-04 06:54:25 +000040 * configured and the ncip is unset. Therefore, all netconsole packets are
41 * broadcasted.
Michael Wallef214a202012-06-05 11:33:17 +000042 * Additionally, the bootsource is set to 'rescue'.
43 */
44
Michael Wallef214a202012-06-05 11:33:17 +000045DECLARE_GLOBAL_DATA_PTR;
46
Michael Walleaa088be2022-08-17 21:38:03 +020047static bool force_rescue_mode;
48
Michael Wallef214a202012-06-05 11:33:17 +000049int 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 */
Stefan Roesed5c51322014-10-22 12:13:11 +020056 mvebu_config_gpio(LSXL_OE_VAL_LOW,
57 LSXL_OE_VAL_HIGH,
58 LSXL_OE_LOW, LSXL_OE_HIGH);
Michael Wallef214a202012-06-05 11:33:17 +000059
60 /*
61 * Multi-Purpose Pins Functionality configuration
62 * These strappings are taken from the original vendor uboot port.
63 */
Albert ARIBAUD9d86f0c2012-11-26 11:27:36 +000064 static const u32 kwmpp_config[] = {
Michael Wallef214a202012-06-05 11:33:17 +000065 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
Michael Walle7c9bd922022-08-17 21:38:04 +0200123enum {
124 LSXL_LED_OFF,
125 LSXL_LED_ALARM,
126 LSXL_LED_POWER,
127 LSXL_LED_INFO,
128};
Michael Wallef214a202012-06-05 11:33:17 +0000129
Michael Walle7c9bd922022-08-17 21:38:04 +0200130static void __set_led(int alarm, int info, int power)
Michael Wallef214a202012-06-05 11:33:17 +0000131{
Michael Walle7c9bd922022-08-17 21:38:04 +0200132 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);
Michael Wallef214a202012-06-05 11:33:17 +0000144}
145
146static void set_led(int state)
147{
148 switch (state) {
Michael Walle7c9bd922022-08-17 21:38:04 +0200149 case LSXL_LED_OFF:
150 __set_led(0, 0, 0);
Michael Wallef214a202012-06-05 11:33:17 +0000151 break;
Michael Walle7c9bd922022-08-17 21:38:04 +0200152 case LSXL_LED_ALARM:
153 __set_led(1, 0, 0);
Michael Wallef214a202012-06-05 11:33:17 +0000154 break;
Michael Walle7c9bd922022-08-17 21:38:04 +0200155 case LSXL_LED_INFO:
156 __set_led(0, 1, 0);
Michael Wallef214a202012-06-05 11:33:17 +0000157 break;
Michael Walle7c9bd922022-08-17 21:38:04 +0200158 case LSXL_LED_POWER:
159 __set_led(0, 0, 1);
Michael Wallef214a202012-06-05 11:33:17 +0000160 break;
161 }
162}
163
164int board_init(void)
165{
166 /* address of boot parameters */
Stefan Roese96c5f082014-10-22 12:13:13 +0200167 gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
Michael Wallef214a202012-06-05 11:33:17 +0000168
Michael Walle7c9bd922022-08-17 21:38:04 +0200169 set_led(LSXL_LED_POWER);
Michael Wallef214a202012-06-05 11:33:17 +0000170
171 return 0;
172}
173
Michael Wallea9f1a482012-07-30 10:47:12 +0000174static void check_power_switch(void)
175{
Michael Walle7c9bd922022-08-17 21:38:04 +0200176 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);
Michael Wallea9f1a482012-07-30 10:47:12 +0000200
201 /* loop until released */
Michael Walle7c9bd922022-08-17 21:38:04 +0200202 while (button_get_state(power_button) == BUTTON_OFF)
Michael Wallea9f1a482012-07-30 10:47:12 +0000203 ;
204
205 /* turn power on again */
Michael Walle7c9bd922022-08-17 21:38:04 +0200206 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;
217err:
218 printf("error in %s\n", __func__);
Michael Wallea9f1a482012-07-30 10:47:12 +0000219}
220
Michael Wallef214a202012-06-05 11:33:17 +0000221void check_enetaddr(void)
222{
223 uchar enetaddr[6];
224
Simon Glass35affd72017-08-03 12:22:14 -0600225 if (!eth_env_get_enetaddr("ethaddr", enetaddr)) {
Michael Wallef214a202012-06-05 11:33:17 +0000226 /* signal unset/invalid ethaddr to user */
Michael Walle7c9bd922022-08-17 21:38:04 +0200227 set_led(LSXL_LED_INFO);
Michael Wallef214a202012-06-05 11:33:17 +0000228 }
229}
230
231static 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
247static void rescue_mode(void)
248{
Michael Wallef214a202012-06-05 11:33:17 +0000249 printf("Entering rescue mode..\n");
Simon Glass382bee52017-08-03 12:22:09 -0600250 env_set("bootsource", "rescue");
Michael Wallef214a202012-06-05 11:33:17 +0000251}
252
253static void check_push_button(void)
254{
Michael Walle7c9bd922022-08-17 21:38:04 +0200255 struct udevice *func_button;
Michael Wallef214a202012-06-05 11:33:17 +0000256 int i = 0;
257
Michael Walle7c9bd922022-08-17 21:38:04 +0200258 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) {
Michael Wallef214a202012-06-05 11:33:17 +0000265 udelay(100000);
266 i++;
267
268 if (i == 10)
Michael Walle7c9bd922022-08-17 21:38:04 +0200269 set_led(LSXL_LED_INFO);
Michael Wallef214a202012-06-05 11:33:17 +0000270
271 if (i >= 100) {
Michael Walle7c9bd922022-08-17 21:38:04 +0200272 set_led(LSXL_LED_ALARM);
Michael Wallef214a202012-06-05 11:33:17 +0000273 break;
274 }
275 }
276
277 if (i >= 100)
278 erase_environment();
279 else if (i >= 10)
Michael Walleaa088be2022-08-17 21:38:03 +0200280 force_rescue_mode = true;
Michael Walle7c9bd922022-08-17 21:38:04 +0200281
282 return;
283err:
284 printf("error in %s\n", __func__);
Michael Walleaa088be2022-08-17 21:38:03 +0200285}
286
287int board_early_init_r(void)
288{
289 check_push_button();
290
291 return 0;
Michael Wallef214a202012-06-05 11:33:17 +0000292}
293
294int misc_init_r(void)
295{
Michael Wallea9f1a482012-07-30 10:47:12 +0000296 check_power_switch();
Michael Wallef214a202012-06-05 11:33:17 +0000297 check_enetaddr();
Michael Walleaa088be2022-08-17 21:38:03 +0200298 if (force_rescue_mode)
299 rescue_mode();
Michael Wallef214a202012-06-05 11:33:17 +0000300
301 return 0;
302}
Michael Wallef214a202012-06-05 11:33:17 +0000303
Tom Rinicb80ff22021-05-03 16:48:58 -0400304#if CONFIG_IS_ENABLED(BOOTSTAGE)
Michael Wallef214a202012-06-05 11:33:17 +0000305void 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
Michael Walle7c9bd922022-08-17 21:38:04 +0200314 set_led(LSXL_LED_ALARM);
Michael Wallef214a202012-06-05 11:33:17 +0000315}
316#endif