blob: 7fab5fbe444420690878377bd61a3b4debc3b1c8 [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
10#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -060011#include <bootstage.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>
Simon Glassd34b4562014-10-13 23:42:04 -060015#include <spi.h>
16#include <spi_flash.h>
Stefan Roesec2437842014-10-22 12:13:06 +020017#include <asm/arch/soc.h>
Michael Walledccfa462012-06-05 11:33:17 +000018#include <asm/arch/cpu.h>
19#include <asm/arch/mpp.h>
20#include <asm/arch/gpio.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060021#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Michael Walledccfa462012-06-05 11:33:17 +000023
24#include "lsxl.h"
25
26/*
27 * Rescue mode
28 *
29 * Selected by holding the push button for 3 seconds, while powering on
30 * the device.
31 *
32 * These linkstations don't have a (populated) serial port. There is no
33 * way to access an (unmodified) board other than using the netconsole. If
34 * you want to recover from a bad environment setting or an empty environment,
35 * you can do this only with a working network connection. Therefore, a random
36 * ethernet address is generated if none is set and a DHCP request is sent.
37 * After a successful DHCP response is received, the network settings are
Michael Walledc3ba8e2012-10-04 06:54:25 +000038 * configured and the ncip is unset. Therefore, all netconsole packets are
39 * broadcasted.
Michael Walledccfa462012-06-05 11:33:17 +000040 * Additionally, the bootsource is set to 'rescue'.
41 */
42
Michael Walledccfa462012-06-05 11:33:17 +000043DECLARE_GLOBAL_DATA_PTR;
44
Michael Walle2e5bab12022-08-17 21:38:03 +020045static bool force_rescue_mode;
46
Michael Walledccfa462012-06-05 11:33:17 +000047int board_early_init_f(void)
48{
49 /*
50 * default gpio configuration
51 * There are maximum 64 gpios controlled through 2 sets of registers
52 * the below configuration configures mainly initial LED status
53 */
Stefan Roesec50ab392014-10-22 12:13:11 +020054 mvebu_config_gpio(LSXL_OE_VAL_LOW,
55 LSXL_OE_VAL_HIGH,
56 LSXL_OE_LOW, LSXL_OE_HIGH);
Michael Walledccfa462012-06-05 11:33:17 +000057
58 /*
59 * Multi-Purpose Pins Functionality configuration
60 * These strappings are taken from the original vendor uboot port.
61 */
Albert ARIBAUD4d424312012-11-26 11:27:36 +000062 static const u32 kwmpp_config[] = {
Michael Walledccfa462012-06-05 11:33:17 +000063 MPP0_SPI_SCn,
64 MPP1_SPI_MOSI,
65 MPP2_SPI_SCK,
66 MPP3_SPI_MISO,
67 MPP4_UART0_RXD,
68 MPP5_UART0_TXD,
69 MPP6_SYSRST_OUTn,
70 MPP7_GPO,
71 MPP8_GPIO,
72 MPP9_GPIO,
73 MPP10_GPO, /* HDD power */
74 MPP11_GPIO, /* USB Vbus enable */
75 MPP12_SD_CLK,
76 MPP13_SD_CMD,
77 MPP14_SD_D0,
78 MPP15_SD_D1,
79 MPP16_SD_D2,
80 MPP17_SD_D3,
81 MPP18_GPO, /* fan speed high */
82 MPP19_GPO, /* fan speed low */
83 MPP20_GE1_0,
84 MPP21_GE1_1,
85 MPP22_GE1_2,
86 MPP23_GE1_3,
87 MPP24_GE1_4,
88 MPP25_GE1_5,
89 MPP26_GE1_6,
90 MPP27_GE1_7,
91 MPP28_GPIO,
92 MPP29_GPIO,
93 MPP30_GE1_10,
94 MPP31_GE1_11,
95 MPP32_GE1_12,
96 MPP33_GE1_13,
97 MPP34_GPIO,
98 MPP35_GPIO,
99 MPP36_GPIO, /* function LED */
100 MPP37_GPIO, /* alarm LED */
101 MPP38_GPIO, /* info LED */
102 MPP39_GPIO, /* power LED */
103 MPP40_GPIO, /* fan alarm */
104 MPP41_GPIO, /* funtion button */
105 MPP42_GPIO, /* power switch */
106 MPP43_GPIO, /* power auto switch */
107 MPP44_GPIO,
108 MPP45_GPIO,
109 MPP46_GPIO,
110 MPP47_GPIO,
111 MPP48_GPIO, /* function red LED */
112 MPP49_GPIO,
113 0
114 };
115
116 kirkwood_mpp_conf(kwmpp_config, NULL);
117
118 return 0;
119}
120
121#define LED_OFF 0
122#define LED_ALARM_ON 1
123#define LED_ALARM_BLINKING 2
124#define LED_POWER_ON 3
125#define LED_POWER_BLINKING 4
126#define LED_INFO_ON 5
127#define LED_INFO_BLINKING 6
128
129static void __set_led(int blink_alarm, int blink_info, int blink_power,
130 int value_alarm, int value_info, int value_power)
131{
132 kw_gpio_set_blink(GPIO_ALARM_LED, blink_alarm);
133 kw_gpio_set_blink(GPIO_INFO_LED, blink_info);
134 kw_gpio_set_blink(GPIO_POWER_LED, blink_power);
135 kw_gpio_set_value(GPIO_ALARM_LED, value_alarm);
136 kw_gpio_set_value(GPIO_INFO_LED, value_info);
137 kw_gpio_set_value(GPIO_POWER_LED, value_power);
138}
139
140static void set_led(int state)
141{
142 switch (state) {
143 case LED_OFF:
Michael Walleb31f3842012-07-30 10:47:12 +0000144 __set_led(0, 0, 0, 1, 1, 1);
Michael Walledccfa462012-06-05 11:33:17 +0000145 break;
146 case LED_ALARM_ON:
147 __set_led(0, 0, 0, 0, 1, 1);
148 break;
149 case LED_ALARM_BLINKING:
150 __set_led(1, 0, 0, 1, 1, 1);
151 break;
152 case LED_INFO_ON:
153 __set_led(0, 0, 0, 1, 0, 1);
154 break;
155 case LED_INFO_BLINKING:
156 __set_led(0, 1, 0, 1, 1, 1);
157 break;
158 case LED_POWER_ON:
159 __set_led(0, 0, 0, 1, 1, 0);
160 break;
161 case LED_POWER_BLINKING:
162 __set_led(0, 0, 1, 1, 1, 1);
163 break;
164 }
165}
166
167int board_init(void)
168{
169 /* address of boot parameters */
Stefan Roese0b741752014-10-22 12:13:13 +0200170 gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
Michael Walledccfa462012-06-05 11:33:17 +0000171
172 set_led(LED_POWER_BLINKING);
173
174 return 0;
175}
176
Michael Walleb31f3842012-07-30 10:47:12 +0000177static void check_power_switch(void)
178{
179 if (kw_gpio_get_value(GPIO_POWER_SWITCH)) {
Michael Walle61aa89f2012-09-30 03:11:05 +0000180 /* turn off fan, HDD and USB power */
Michael Walleb31f3842012-07-30 10:47:12 +0000181 kw_gpio_set_value(GPIO_HDD_POWER, 0);
182 kw_gpio_set_value(GPIO_USB_VBUS, 0);
Michael Walle61aa89f2012-09-30 03:11:05 +0000183 kw_gpio_set_value(GPIO_FAN_HIGH, 1);
184 kw_gpio_set_value(GPIO_FAN_LOW, 1);
Michael Walleb31f3842012-07-30 10:47:12 +0000185 set_led(LED_OFF);
186
187 /* loop until released */
188 while (kw_gpio_get_value(GPIO_POWER_SWITCH))
189 ;
190
191 /* turn power on again */
192 kw_gpio_set_value(GPIO_HDD_POWER, 1);
193 kw_gpio_set_value(GPIO_USB_VBUS, 1);
Michael Walle61aa89f2012-09-30 03:11:05 +0000194 kw_gpio_set_value(GPIO_FAN_HIGH, 0);
195 kw_gpio_set_value(GPIO_FAN_LOW, 0);
Michael Walleb31f3842012-07-30 10:47:12 +0000196 set_led(LED_POWER_BLINKING);
197 }
198}
199
Michael Walledccfa462012-06-05 11:33:17 +0000200void check_enetaddr(void)
201{
202 uchar enetaddr[6];
203
Simon Glass399a9ce2017-08-03 12:22:14 -0600204 if (!eth_env_get_enetaddr("ethaddr", enetaddr)) {
Michael Walledccfa462012-06-05 11:33:17 +0000205 /* signal unset/invalid ethaddr to user */
206 set_led(LED_INFO_BLINKING);
207 }
208}
209
210static void erase_environment(void)
211{
212 struct spi_flash *flash;
213
214 printf("Erasing environment..\n");
215 flash = spi_flash_probe(0, 0, 1000000, SPI_MODE_3);
216 if (!flash) {
217 printf("Erasing flash failed\n");
218 return;
219 }
220
221 spi_flash_erase(flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE);
222 spi_flash_free(flash);
223 do_reset(NULL, 0, 0, NULL);
224}
225
226static void rescue_mode(void)
227{
Michael Walledccfa462012-06-05 11:33:17 +0000228 printf("Entering rescue mode..\n");
Simon Glass6a38e412017-08-03 12:22:09 -0600229 env_set("bootsource", "rescue");
Michael Walledccfa462012-06-05 11:33:17 +0000230}
231
232static void check_push_button(void)
233{
234 int i = 0;
235
236 while (!kw_gpio_get_value(GPIO_FUNC_BUTTON)) {
237 udelay(100000);
238 i++;
239
240 if (i == 10)
241 set_led(LED_INFO_ON);
242
243 if (i >= 100) {
244 set_led(LED_INFO_BLINKING);
245 break;
246 }
247 }
248
249 if (i >= 100)
250 erase_environment();
251 else if (i >= 10)
Michael Walle2e5bab12022-08-17 21:38:03 +0200252 force_rescue_mode = true;
253}
254
255int board_early_init_r(void)
256{
257 check_push_button();
258
259 return 0;
Michael Walledccfa462012-06-05 11:33:17 +0000260}
261
262int misc_init_r(void)
263{
Michael Walleb31f3842012-07-30 10:47:12 +0000264 check_power_switch();
Michael Walledccfa462012-06-05 11:33:17 +0000265 check_enetaddr();
Michael Walle2e5bab12022-08-17 21:38:03 +0200266 if (force_rescue_mode)
267 rescue_mode();
Michael Walledccfa462012-06-05 11:33:17 +0000268
269 return 0;
270}
Michael Walledccfa462012-06-05 11:33:17 +0000271
Tom Rinia9765d02021-05-03 16:48:58 -0400272#if CONFIG_IS_ENABLED(BOOTSTAGE)
Michael Walledccfa462012-06-05 11:33:17 +0000273void show_boot_progress(int progress)
274{
275 if (progress > 0)
276 return;
277
278 /* this is not an error, eg. bootp with autoload=no will trigger this */
279 if (progress == -BOOTSTAGE_ID_NET_LOADED)
280 return;
281
282 set_led(LED_ALARM_BLINKING);
283}
284#endif