blob: eca7da2f6dac5895dd07106ee1260d03ddb84c04 [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>
Simon Glass09140112020-05-10 11:40:03 -060012#include <command.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060013#include <env.h>
Simon Glass691d7192020-05-10 11:40:02 -060014#include <init.h>
Simon Glassff0960f2014-10-13 23:42:04 -060015#include <spi.h>
16#include <spi_flash.h>
Stefan Roese3dc23f72014-10-22 12:13:06 +020017#include <asm/arch/soc.h>
Michael Wallef214a202012-06-05 11:33:17 +000018#include <asm/arch/cpu.h>
19#include <asm/arch/mpp.h>
20#include <asm/arch/gpio.h>
Simon Glass401d1c42020-10-30 21:38:53 -060021#include <asm/global_data.h>
Simon Glassc05ed002020-05-10 11:40:11 -060022#include <linux/delay.h>
Michael Wallef214a202012-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 Walle23c99462012-10-04 06:54:25 +000038 * configured and the ncip is unset. Therefore, all netconsole packets are
39 * broadcasted.
Michael Wallef214a202012-06-05 11:33:17 +000040 * Additionally, the bootsource is set to 'rescue'.
41 */
42
Michael Wallef214a202012-06-05 11:33:17 +000043DECLARE_GLOBAL_DATA_PTR;
44
45int board_early_init_f(void)
46{
47 /*
48 * default gpio configuration
49 * There are maximum 64 gpios controlled through 2 sets of registers
50 * the below configuration configures mainly initial LED status
51 */
Stefan Roesed5c51322014-10-22 12:13:11 +020052 mvebu_config_gpio(LSXL_OE_VAL_LOW,
53 LSXL_OE_VAL_HIGH,
54 LSXL_OE_LOW, LSXL_OE_HIGH);
Michael Wallef214a202012-06-05 11:33:17 +000055
56 /*
57 * Multi-Purpose Pins Functionality configuration
58 * These strappings are taken from the original vendor uboot port.
59 */
Albert ARIBAUD9d86f0c2012-11-26 11:27:36 +000060 static const u32 kwmpp_config[] = {
Michael Wallef214a202012-06-05 11:33:17 +000061 MPP0_SPI_SCn,
62 MPP1_SPI_MOSI,
63 MPP2_SPI_SCK,
64 MPP3_SPI_MISO,
65 MPP4_UART0_RXD,
66 MPP5_UART0_TXD,
67 MPP6_SYSRST_OUTn,
68 MPP7_GPO,
69 MPP8_GPIO,
70 MPP9_GPIO,
71 MPP10_GPO, /* HDD power */
72 MPP11_GPIO, /* USB Vbus enable */
73 MPP12_SD_CLK,
74 MPP13_SD_CMD,
75 MPP14_SD_D0,
76 MPP15_SD_D1,
77 MPP16_SD_D2,
78 MPP17_SD_D3,
79 MPP18_GPO, /* fan speed high */
80 MPP19_GPO, /* fan speed low */
81 MPP20_GE1_0,
82 MPP21_GE1_1,
83 MPP22_GE1_2,
84 MPP23_GE1_3,
85 MPP24_GE1_4,
86 MPP25_GE1_5,
87 MPP26_GE1_6,
88 MPP27_GE1_7,
89 MPP28_GPIO,
90 MPP29_GPIO,
91 MPP30_GE1_10,
92 MPP31_GE1_11,
93 MPP32_GE1_12,
94 MPP33_GE1_13,
95 MPP34_GPIO,
96 MPP35_GPIO,
97 MPP36_GPIO, /* function LED */
98 MPP37_GPIO, /* alarm LED */
99 MPP38_GPIO, /* info LED */
100 MPP39_GPIO, /* power LED */
101 MPP40_GPIO, /* fan alarm */
102 MPP41_GPIO, /* funtion button */
103 MPP42_GPIO, /* power switch */
104 MPP43_GPIO, /* power auto switch */
105 MPP44_GPIO,
106 MPP45_GPIO,
107 MPP46_GPIO,
108 MPP47_GPIO,
109 MPP48_GPIO, /* function red LED */
110 MPP49_GPIO,
111 0
112 };
113
114 kirkwood_mpp_conf(kwmpp_config, NULL);
115
116 return 0;
117}
118
119#define LED_OFF 0
120#define LED_ALARM_ON 1
121#define LED_ALARM_BLINKING 2
122#define LED_POWER_ON 3
123#define LED_POWER_BLINKING 4
124#define LED_INFO_ON 5
125#define LED_INFO_BLINKING 6
126
127static void __set_led(int blink_alarm, int blink_info, int blink_power,
128 int value_alarm, int value_info, int value_power)
129{
130 kw_gpio_set_blink(GPIO_ALARM_LED, blink_alarm);
131 kw_gpio_set_blink(GPIO_INFO_LED, blink_info);
132 kw_gpio_set_blink(GPIO_POWER_LED, blink_power);
133 kw_gpio_set_value(GPIO_ALARM_LED, value_alarm);
134 kw_gpio_set_value(GPIO_INFO_LED, value_info);
135 kw_gpio_set_value(GPIO_POWER_LED, value_power);
136}
137
138static void set_led(int state)
139{
140 switch (state) {
141 case LED_OFF:
Michael Wallea9f1a482012-07-30 10:47:12 +0000142 __set_led(0, 0, 0, 1, 1, 1);
Michael Wallef214a202012-06-05 11:33:17 +0000143 break;
144 case LED_ALARM_ON:
145 __set_led(0, 0, 0, 0, 1, 1);
146 break;
147 case LED_ALARM_BLINKING:
148 __set_led(1, 0, 0, 1, 1, 1);
149 break;
150 case LED_INFO_ON:
151 __set_led(0, 0, 0, 1, 0, 1);
152 break;
153 case LED_INFO_BLINKING:
154 __set_led(0, 1, 0, 1, 1, 1);
155 break;
156 case LED_POWER_ON:
157 __set_led(0, 0, 0, 1, 1, 0);
158 break;
159 case LED_POWER_BLINKING:
160 __set_led(0, 0, 1, 1, 1, 1);
161 break;
162 }
163}
164
165int board_init(void)
166{
167 /* address of boot parameters */
Stefan Roese96c5f082014-10-22 12:13:13 +0200168 gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
Michael Wallef214a202012-06-05 11:33:17 +0000169
170 set_led(LED_POWER_BLINKING);
171
172 return 0;
173}
174
Michael Wallea9f1a482012-07-30 10:47:12 +0000175static void check_power_switch(void)
176{
177 if (kw_gpio_get_value(GPIO_POWER_SWITCH)) {
Michael Walle9bd23172012-09-30 03:11:05 +0000178 /* turn off fan, HDD and USB power */
Michael Wallea9f1a482012-07-30 10:47:12 +0000179 kw_gpio_set_value(GPIO_HDD_POWER, 0);
180 kw_gpio_set_value(GPIO_USB_VBUS, 0);
Michael Walle9bd23172012-09-30 03:11:05 +0000181 kw_gpio_set_value(GPIO_FAN_HIGH, 1);
182 kw_gpio_set_value(GPIO_FAN_LOW, 1);
Michael Wallea9f1a482012-07-30 10:47:12 +0000183 set_led(LED_OFF);
184
185 /* loop until released */
186 while (kw_gpio_get_value(GPIO_POWER_SWITCH))
187 ;
188
189 /* turn power on again */
190 kw_gpio_set_value(GPIO_HDD_POWER, 1);
191 kw_gpio_set_value(GPIO_USB_VBUS, 1);
Michael Walle9bd23172012-09-30 03:11:05 +0000192 kw_gpio_set_value(GPIO_FAN_HIGH, 0);
193 kw_gpio_set_value(GPIO_FAN_LOW, 0);
Michael Wallea9f1a482012-07-30 10:47:12 +0000194 set_led(LED_POWER_BLINKING);
195 }
196}
197
Michael Wallef214a202012-06-05 11:33:17 +0000198void check_enetaddr(void)
199{
200 uchar enetaddr[6];
201
Simon Glass35affd72017-08-03 12:22:14 -0600202 if (!eth_env_get_enetaddr("ethaddr", enetaddr)) {
Michael Wallef214a202012-06-05 11:33:17 +0000203 /* signal unset/invalid ethaddr to user */
204 set_led(LED_INFO_BLINKING);
205 }
206}
207
208static void erase_environment(void)
209{
210 struct spi_flash *flash;
211
212 printf("Erasing environment..\n");
213 flash = spi_flash_probe(0, 0, 1000000, SPI_MODE_3);
214 if (!flash) {
215 printf("Erasing flash failed\n");
216 return;
217 }
218
219 spi_flash_erase(flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE);
220 spi_flash_free(flash);
221 do_reset(NULL, 0, 0, NULL);
222}
223
224static void rescue_mode(void)
225{
Michael Wallef214a202012-06-05 11:33:17 +0000226 printf("Entering rescue mode..\n");
Simon Glass382bee52017-08-03 12:22:09 -0600227 env_set("bootsource", "rescue");
Michael Wallef214a202012-06-05 11:33:17 +0000228}
229
230static void check_push_button(void)
231{
232 int i = 0;
233
234 while (!kw_gpio_get_value(GPIO_FUNC_BUTTON)) {
235 udelay(100000);
236 i++;
237
238 if (i == 10)
239 set_led(LED_INFO_ON);
240
241 if (i >= 100) {
242 set_led(LED_INFO_BLINKING);
243 break;
244 }
245 }
246
247 if (i >= 100)
248 erase_environment();
249 else if (i >= 10)
250 rescue_mode();
251}
252
253int misc_init_r(void)
254{
Michael Wallea9f1a482012-07-30 10:47:12 +0000255 check_power_switch();
Michael Wallef214a202012-06-05 11:33:17 +0000256 check_enetaddr();
257 check_push_button();
258
259 return 0;
260}
Michael Wallef214a202012-06-05 11:33:17 +0000261
Tom Rinicb80ff22021-05-03 16:48:58 -0400262#if CONFIG_IS_ENABLED(BOOTSTAGE)
Michael Wallef214a202012-06-05 11:33:17 +0000263void show_boot_progress(int progress)
264{
265 if (progress > 0)
266 return;
267
268 /* this is not an error, eg. bootp with autoload=no will trigger this */
269 if (progress == -BOOTSTAGE_ID_NET_LOADED)
270 return;
271
272 set_led(LED_ALARM_BLINKING);
273}
274#endif