blob: 6afb6bbdb5ca998e81c5f5c8563a2cced2c4970d [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 Glass9fb625c2019-08-01 09:46:51 -060012#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060013#include <env_internal.h>
Simon Glassb79fdc72020-05-10 11:39:54 -060014#include <flash.h>
Simon Glass691d7192020-05-10 11:40:02 -060015#include <init.h>
Michael Wallef214a202012-06-05 11:33:17 +000016#include <net.h>
17#include <malloc.h>
18#include <netdev.h>
19#include <miiphy.h>
Simon Glassff0960f2014-10-13 23:42:04 -060020#include <spi.h>
21#include <spi_flash.h>
Stefan Roese3dc23f72014-10-22 12:13:06 +020022#include <asm/arch/soc.h>
Michael Wallef214a202012-06-05 11:33:17 +000023#include <asm/arch/cpu.h>
24#include <asm/arch/mpp.h>
25#include <asm/arch/gpio.h>
Michael Wallef214a202012-06-05 11:33:17 +000026
27#include "lsxl.h"
28
29/*
30 * Rescue mode
31 *
32 * Selected by holding the push button for 3 seconds, while powering on
33 * the device.
34 *
35 * These linkstations don't have a (populated) serial port. There is no
36 * way to access an (unmodified) board other than using the netconsole. If
37 * you want to recover from a bad environment setting or an empty environment,
38 * you can do this only with a working network connection. Therefore, a random
39 * ethernet address is generated if none is set and a DHCP request is sent.
40 * After a successful DHCP response is received, the network settings are
Michael Walle23c99462012-10-04 06:54:25 +000041 * configured and the ncip is unset. Therefore, all netconsole packets are
42 * broadcasted.
Michael Wallef214a202012-06-05 11:33:17 +000043 * Additionally, the bootsource is set to 'rescue'.
44 */
45
46#ifndef CONFIG_ENV_OVERWRITE
47# error "You need to set CONFIG_ENV_OVERWRITE"
48#endif
49
50DECLARE_GLOBAL_DATA_PTR;
51
52int board_early_init_f(void)
53{
54 /*
55 * default gpio configuration
56 * There are maximum 64 gpios controlled through 2 sets of registers
57 * the below configuration configures mainly initial LED status
58 */
Stefan Roesed5c51322014-10-22 12:13:11 +020059 mvebu_config_gpio(LSXL_OE_VAL_LOW,
60 LSXL_OE_VAL_HIGH,
61 LSXL_OE_LOW, LSXL_OE_HIGH);
Michael Wallef214a202012-06-05 11:33:17 +000062
63 /*
64 * Multi-Purpose Pins Functionality configuration
65 * These strappings are taken from the original vendor uboot port.
66 */
Albert ARIBAUD9d86f0c2012-11-26 11:27:36 +000067 static const u32 kwmpp_config[] = {
Michael Wallef214a202012-06-05 11:33:17 +000068 MPP0_SPI_SCn,
69 MPP1_SPI_MOSI,
70 MPP2_SPI_SCK,
71 MPP3_SPI_MISO,
72 MPP4_UART0_RXD,
73 MPP5_UART0_TXD,
74 MPP6_SYSRST_OUTn,
75 MPP7_GPO,
76 MPP8_GPIO,
77 MPP9_GPIO,
78 MPP10_GPO, /* HDD power */
79 MPP11_GPIO, /* USB Vbus enable */
80 MPP12_SD_CLK,
81 MPP13_SD_CMD,
82 MPP14_SD_D0,
83 MPP15_SD_D1,
84 MPP16_SD_D2,
85 MPP17_SD_D3,
86 MPP18_GPO, /* fan speed high */
87 MPP19_GPO, /* fan speed low */
88 MPP20_GE1_0,
89 MPP21_GE1_1,
90 MPP22_GE1_2,
91 MPP23_GE1_3,
92 MPP24_GE1_4,
93 MPP25_GE1_5,
94 MPP26_GE1_6,
95 MPP27_GE1_7,
96 MPP28_GPIO,
97 MPP29_GPIO,
98 MPP30_GE1_10,
99 MPP31_GE1_11,
100 MPP32_GE1_12,
101 MPP33_GE1_13,
102 MPP34_GPIO,
103 MPP35_GPIO,
104 MPP36_GPIO, /* function LED */
105 MPP37_GPIO, /* alarm LED */
106 MPP38_GPIO, /* info LED */
107 MPP39_GPIO, /* power LED */
108 MPP40_GPIO, /* fan alarm */
109 MPP41_GPIO, /* funtion button */
110 MPP42_GPIO, /* power switch */
111 MPP43_GPIO, /* power auto switch */
112 MPP44_GPIO,
113 MPP45_GPIO,
114 MPP46_GPIO,
115 MPP47_GPIO,
116 MPP48_GPIO, /* function red LED */
117 MPP49_GPIO,
118 0
119 };
120
121 kirkwood_mpp_conf(kwmpp_config, NULL);
122
123 return 0;
124}
125
126#define LED_OFF 0
127#define LED_ALARM_ON 1
128#define LED_ALARM_BLINKING 2
129#define LED_POWER_ON 3
130#define LED_POWER_BLINKING 4
131#define LED_INFO_ON 5
132#define LED_INFO_BLINKING 6
133
134static void __set_led(int blink_alarm, int blink_info, int blink_power,
135 int value_alarm, int value_info, int value_power)
136{
137 kw_gpio_set_blink(GPIO_ALARM_LED, blink_alarm);
138 kw_gpio_set_blink(GPIO_INFO_LED, blink_info);
139 kw_gpio_set_blink(GPIO_POWER_LED, blink_power);
140 kw_gpio_set_value(GPIO_ALARM_LED, value_alarm);
141 kw_gpio_set_value(GPIO_INFO_LED, value_info);
142 kw_gpio_set_value(GPIO_POWER_LED, value_power);
143}
144
145static void set_led(int state)
146{
147 switch (state) {
148 case LED_OFF:
Michael Wallea9f1a482012-07-30 10:47:12 +0000149 __set_led(0, 0, 0, 1, 1, 1);
Michael Wallef214a202012-06-05 11:33:17 +0000150 break;
151 case LED_ALARM_ON:
152 __set_led(0, 0, 0, 0, 1, 1);
153 break;
154 case LED_ALARM_BLINKING:
155 __set_led(1, 0, 0, 1, 1, 1);
156 break;
157 case LED_INFO_ON:
158 __set_led(0, 0, 0, 1, 0, 1);
159 break;
160 case LED_INFO_BLINKING:
161 __set_led(0, 1, 0, 1, 1, 1);
162 break;
163 case LED_POWER_ON:
164 __set_led(0, 0, 0, 1, 1, 0);
165 break;
166 case LED_POWER_BLINKING:
167 __set_led(0, 0, 1, 1, 1, 1);
168 break;
169 }
170}
171
172int board_init(void)
173{
174 /* address of boot parameters */
Stefan Roese96c5f082014-10-22 12:13:13 +0200175 gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
Michael Wallef214a202012-06-05 11:33:17 +0000176
177 set_led(LED_POWER_BLINKING);
178
179 return 0;
180}
181
182#ifdef CONFIG_MISC_INIT_R
Michael Wallea9f1a482012-07-30 10:47:12 +0000183static void check_power_switch(void)
184{
185 if (kw_gpio_get_value(GPIO_POWER_SWITCH)) {
Michael Walle9bd23172012-09-30 03:11:05 +0000186 /* turn off fan, HDD and USB power */
Michael Wallea9f1a482012-07-30 10:47:12 +0000187 kw_gpio_set_value(GPIO_HDD_POWER, 0);
188 kw_gpio_set_value(GPIO_USB_VBUS, 0);
Michael Walle9bd23172012-09-30 03:11:05 +0000189 kw_gpio_set_value(GPIO_FAN_HIGH, 1);
190 kw_gpio_set_value(GPIO_FAN_LOW, 1);
Michael Wallea9f1a482012-07-30 10:47:12 +0000191 set_led(LED_OFF);
192
193 /* loop until released */
194 while (kw_gpio_get_value(GPIO_POWER_SWITCH))
195 ;
196
197 /* turn power on again */
198 kw_gpio_set_value(GPIO_HDD_POWER, 1);
199 kw_gpio_set_value(GPIO_USB_VBUS, 1);
Michael Walle9bd23172012-09-30 03:11:05 +0000200 kw_gpio_set_value(GPIO_FAN_HIGH, 0);
201 kw_gpio_set_value(GPIO_FAN_LOW, 0);
Michael Wallea9f1a482012-07-30 10:47:12 +0000202 set_led(LED_POWER_BLINKING);
203 }
204}
205
Michael Wallef214a202012-06-05 11:33:17 +0000206void check_enetaddr(void)
207{
208 uchar enetaddr[6];
209
Simon Glass35affd72017-08-03 12:22:14 -0600210 if (!eth_env_get_enetaddr("ethaddr", enetaddr)) {
Michael Wallef214a202012-06-05 11:33:17 +0000211 /* signal unset/invalid ethaddr to user */
212 set_led(LED_INFO_BLINKING);
213 }
214}
215
216static void erase_environment(void)
217{
218 struct spi_flash *flash;
219
220 printf("Erasing environment..\n");
221 flash = spi_flash_probe(0, 0, 1000000, SPI_MODE_3);
222 if (!flash) {
223 printf("Erasing flash failed\n");
224 return;
225 }
226
227 spi_flash_erase(flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE);
228 spi_flash_free(flash);
229 do_reset(NULL, 0, 0, NULL);
230}
231
232static void rescue_mode(void)
233{
Michael Wallef214a202012-06-05 11:33:17 +0000234 printf("Entering rescue mode..\n");
Simon Glass382bee52017-08-03 12:22:09 -0600235 env_set("bootsource", "rescue");
Michael Wallef214a202012-06-05 11:33:17 +0000236}
237
238static void check_push_button(void)
239{
240 int i = 0;
241
242 while (!kw_gpio_get_value(GPIO_FUNC_BUTTON)) {
243 udelay(100000);
244 i++;
245
246 if (i == 10)
247 set_led(LED_INFO_ON);
248
249 if (i >= 100) {
250 set_led(LED_INFO_BLINKING);
251 break;
252 }
253 }
254
255 if (i >= 100)
256 erase_environment();
257 else if (i >= 10)
258 rescue_mode();
259}
260
261int misc_init_r(void)
262{
Michael Wallea9f1a482012-07-30 10:47:12 +0000263 check_power_switch();
Michael Wallef214a202012-06-05 11:33:17 +0000264 check_enetaddr();
265 check_push_button();
266
267 return 0;
268}
269#endif
270
271#ifdef CONFIG_SHOW_BOOT_PROGRESS
272void show_boot_progress(int progress)
273{
274 if (progress > 0)
275 return;
276
277 /* this is not an error, eg. bootp with autoload=no will trigger this */
278 if (progress == -BOOTSTAGE_ID_NET_LOADED)
279 return;
280
281 set_led(LED_ALARM_BLINKING);
282}
283#endif