]> git.ipfire.org Git - thirdparty/u-boot.git/blob - board/buffalo/lsxl/lsxl.c
common: Drop flash.h from common header
[thirdparty/u-boot.git] / board / buffalo / lsxl / lsxl.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
8 */
9
10 #include <common.h>
11 #include <env.h>
12 #include <env_internal.h>
13 #include <flash.h>
14 #include <net.h>
15 #include <malloc.h>
16 #include <netdev.h>
17 #include <miiphy.h>
18 #include <spi.h>
19 #include <spi_flash.h>
20 #include <asm/arch/soc.h>
21 #include <asm/arch/cpu.h>
22 #include <asm/arch/mpp.h>
23 #include <asm/arch/gpio.h>
24
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
39 * configured and the ncip is unset. Therefore, all netconsole packets are
40 * broadcasted.
41 * Additionally, the bootsource is set to 'rescue'.
42 */
43
44 #ifndef CONFIG_ENV_OVERWRITE
45 # error "You need to set CONFIG_ENV_OVERWRITE"
46 #endif
47
48 DECLARE_GLOBAL_DATA_PTR;
49
50 int board_early_init_f(void)
51 {
52 /*
53 * default gpio configuration
54 * There are maximum 64 gpios controlled through 2 sets of registers
55 * the below configuration configures mainly initial LED status
56 */
57 mvebu_config_gpio(LSXL_OE_VAL_LOW,
58 LSXL_OE_VAL_HIGH,
59 LSXL_OE_LOW, LSXL_OE_HIGH);
60
61 /*
62 * Multi-Purpose Pins Functionality configuration
63 * These strappings are taken from the original vendor uboot port.
64 */
65 static const u32 kwmpp_config[] = {
66 MPP0_SPI_SCn,
67 MPP1_SPI_MOSI,
68 MPP2_SPI_SCK,
69 MPP3_SPI_MISO,
70 MPP4_UART0_RXD,
71 MPP5_UART0_TXD,
72 MPP6_SYSRST_OUTn,
73 MPP7_GPO,
74 MPP8_GPIO,
75 MPP9_GPIO,
76 MPP10_GPO, /* HDD power */
77 MPP11_GPIO, /* USB Vbus enable */
78 MPP12_SD_CLK,
79 MPP13_SD_CMD,
80 MPP14_SD_D0,
81 MPP15_SD_D1,
82 MPP16_SD_D2,
83 MPP17_SD_D3,
84 MPP18_GPO, /* fan speed high */
85 MPP19_GPO, /* fan speed low */
86 MPP20_GE1_0,
87 MPP21_GE1_1,
88 MPP22_GE1_2,
89 MPP23_GE1_3,
90 MPP24_GE1_4,
91 MPP25_GE1_5,
92 MPP26_GE1_6,
93 MPP27_GE1_7,
94 MPP28_GPIO,
95 MPP29_GPIO,
96 MPP30_GE1_10,
97 MPP31_GE1_11,
98 MPP32_GE1_12,
99 MPP33_GE1_13,
100 MPP34_GPIO,
101 MPP35_GPIO,
102 MPP36_GPIO, /* function LED */
103 MPP37_GPIO, /* alarm LED */
104 MPP38_GPIO, /* info LED */
105 MPP39_GPIO, /* power LED */
106 MPP40_GPIO, /* fan alarm */
107 MPP41_GPIO, /* funtion button */
108 MPP42_GPIO, /* power switch */
109 MPP43_GPIO, /* power auto switch */
110 MPP44_GPIO,
111 MPP45_GPIO,
112 MPP46_GPIO,
113 MPP47_GPIO,
114 MPP48_GPIO, /* function red LED */
115 MPP49_GPIO,
116 0
117 };
118
119 kirkwood_mpp_conf(kwmpp_config, NULL);
120
121 return 0;
122 }
123
124 #define LED_OFF 0
125 #define LED_ALARM_ON 1
126 #define LED_ALARM_BLINKING 2
127 #define LED_POWER_ON 3
128 #define LED_POWER_BLINKING 4
129 #define LED_INFO_ON 5
130 #define LED_INFO_BLINKING 6
131
132 static void __set_led(int blink_alarm, int blink_info, int blink_power,
133 int value_alarm, int value_info, int value_power)
134 {
135 kw_gpio_set_blink(GPIO_ALARM_LED, blink_alarm);
136 kw_gpio_set_blink(GPIO_INFO_LED, blink_info);
137 kw_gpio_set_blink(GPIO_POWER_LED, blink_power);
138 kw_gpio_set_value(GPIO_ALARM_LED, value_alarm);
139 kw_gpio_set_value(GPIO_INFO_LED, value_info);
140 kw_gpio_set_value(GPIO_POWER_LED, value_power);
141 }
142
143 static void set_led(int state)
144 {
145 switch (state) {
146 case LED_OFF:
147 __set_led(0, 0, 0, 1, 1, 1);
148 break;
149 case LED_ALARM_ON:
150 __set_led(0, 0, 0, 0, 1, 1);
151 break;
152 case LED_ALARM_BLINKING:
153 __set_led(1, 0, 0, 1, 1, 1);
154 break;
155 case LED_INFO_ON:
156 __set_led(0, 0, 0, 1, 0, 1);
157 break;
158 case LED_INFO_BLINKING:
159 __set_led(0, 1, 0, 1, 1, 1);
160 break;
161 case LED_POWER_ON:
162 __set_led(0, 0, 0, 1, 1, 0);
163 break;
164 case LED_POWER_BLINKING:
165 __set_led(0, 0, 1, 1, 1, 1);
166 break;
167 }
168 }
169
170 int board_init(void)
171 {
172 /* address of boot parameters */
173 gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
174
175 set_led(LED_POWER_BLINKING);
176
177 return 0;
178 }
179
180 #ifdef CONFIG_MISC_INIT_R
181 static void check_power_switch(void)
182 {
183 if (kw_gpio_get_value(GPIO_POWER_SWITCH)) {
184 /* turn off fan, HDD and USB power */
185 kw_gpio_set_value(GPIO_HDD_POWER, 0);
186 kw_gpio_set_value(GPIO_USB_VBUS, 0);
187 kw_gpio_set_value(GPIO_FAN_HIGH, 1);
188 kw_gpio_set_value(GPIO_FAN_LOW, 1);
189 set_led(LED_OFF);
190
191 /* loop until released */
192 while (kw_gpio_get_value(GPIO_POWER_SWITCH))
193 ;
194
195 /* turn power on again */
196 kw_gpio_set_value(GPIO_HDD_POWER, 1);
197 kw_gpio_set_value(GPIO_USB_VBUS, 1);
198 kw_gpio_set_value(GPIO_FAN_HIGH, 0);
199 kw_gpio_set_value(GPIO_FAN_LOW, 0);
200 set_led(LED_POWER_BLINKING);
201 }
202 }
203
204 void check_enetaddr(void)
205 {
206 uchar enetaddr[6];
207
208 if (!eth_env_get_enetaddr("ethaddr", enetaddr)) {
209 /* signal unset/invalid ethaddr to user */
210 set_led(LED_INFO_BLINKING);
211 }
212 }
213
214 static void erase_environment(void)
215 {
216 struct spi_flash *flash;
217
218 printf("Erasing environment..\n");
219 flash = spi_flash_probe(0, 0, 1000000, SPI_MODE_3);
220 if (!flash) {
221 printf("Erasing flash failed\n");
222 return;
223 }
224
225 spi_flash_erase(flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE);
226 spi_flash_free(flash);
227 do_reset(NULL, 0, 0, NULL);
228 }
229
230 static void rescue_mode(void)
231 {
232 printf("Entering rescue mode..\n");
233 env_set("bootsource", "rescue");
234 }
235
236 static void check_push_button(void)
237 {
238 int i = 0;
239
240 while (!kw_gpio_get_value(GPIO_FUNC_BUTTON)) {
241 udelay(100000);
242 i++;
243
244 if (i == 10)
245 set_led(LED_INFO_ON);
246
247 if (i >= 100) {
248 set_led(LED_INFO_BLINKING);
249 break;
250 }
251 }
252
253 if (i >= 100)
254 erase_environment();
255 else if (i >= 10)
256 rescue_mode();
257 }
258
259 int misc_init_r(void)
260 {
261 check_power_switch();
262 check_enetaddr();
263 check_push_button();
264
265 return 0;
266 }
267 #endif
268
269 #ifdef CONFIG_SHOW_BOOT_PROGRESS
270 void show_boot_progress(int progress)
271 {
272 if (progress > 0)
273 return;
274
275 /* this is not an error, eg. bootp with autoload=no will trigger this */
276 if (progress == -BOOTSTAGE_ID_NET_LOADED)
277 return;
278
279 set_led(LED_ALARM_BLINKING);
280 }
281 #endif