blob: b3f31d6b697921e5f4ba5a923d9adcf7566ebc93 [file] [log] [blame]
Michael Walledccfa462012-06-05 11:33:17 +00001/*
2 * Copyright (c) 2012 Michael Walle
3 * Michael Walle <michael@walle.cc>
4 *
5 * Based on sheevaplug/sheevaplug.c by
6 * Marvell Semiconductor <www.marvell.com>
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 * MA 02110-1301 USA
25 */
26
27#include <common.h>
28#include <net.h>
29#include <malloc.h>
30#include <netdev.h>
31#include <miiphy.h>
32#include <asm/arch/kirkwood.h>
33#include <asm/arch/cpu.h>
34#include <asm/arch/mpp.h>
35#include <asm/arch/gpio.h>
36#include <spi_flash.h>
37
38#include "lsxl.h"
39
40/*
41 * Rescue mode
42 *
43 * Selected by holding the push button for 3 seconds, while powering on
44 * the device.
45 *
46 * These linkstations don't have a (populated) serial port. There is no
47 * way to access an (unmodified) board other than using the netconsole. If
48 * you want to recover from a bad environment setting or an empty environment,
49 * you can do this only with a working network connection. Therefore, a random
50 * ethernet address is generated if none is set and a DHCP request is sent.
51 * After a successful DHCP response is received, the network settings are
52 * configured and the ncip parameter is set to the serverip. Eg. for a working
53 * resuce mode, you should set 'next-server' to the host where the netconsole
54 * client is started.
55 * Additionally, the bootsource is set to 'rescue'.
56 */
57
58#ifndef CONFIG_ENV_OVERWRITE
59# error "You need to set CONFIG_ENV_OVERWRITE"
60#endif
61
62DECLARE_GLOBAL_DATA_PTR;
63
64int board_early_init_f(void)
65{
66 /*
67 * default gpio configuration
68 * There are maximum 64 gpios controlled through 2 sets of registers
69 * the below configuration configures mainly initial LED status
70 */
71 kw_config_gpio(LSXL_OE_VAL_LOW,
72 LSXL_OE_VAL_HIGH,
73 LSXL_OE_LOW, LSXL_OE_HIGH);
74
75 /*
76 * Multi-Purpose Pins Functionality configuration
77 * These strappings are taken from the original vendor uboot port.
78 */
79 u32 kwmpp_config[] = {
80 MPP0_SPI_SCn,
81 MPP1_SPI_MOSI,
82 MPP2_SPI_SCK,
83 MPP3_SPI_MISO,
84 MPP4_UART0_RXD,
85 MPP5_UART0_TXD,
86 MPP6_SYSRST_OUTn,
87 MPP7_GPO,
88 MPP8_GPIO,
89 MPP9_GPIO,
90 MPP10_GPO, /* HDD power */
91 MPP11_GPIO, /* USB Vbus enable */
92 MPP12_SD_CLK,
93 MPP13_SD_CMD,
94 MPP14_SD_D0,
95 MPP15_SD_D1,
96 MPP16_SD_D2,
97 MPP17_SD_D3,
98 MPP18_GPO, /* fan speed high */
99 MPP19_GPO, /* fan speed low */
100 MPP20_GE1_0,
101 MPP21_GE1_1,
102 MPP22_GE1_2,
103 MPP23_GE1_3,
104 MPP24_GE1_4,
105 MPP25_GE1_5,
106 MPP26_GE1_6,
107 MPP27_GE1_7,
108 MPP28_GPIO,
109 MPP29_GPIO,
110 MPP30_GE1_10,
111 MPP31_GE1_11,
112 MPP32_GE1_12,
113 MPP33_GE1_13,
114 MPP34_GPIO,
115 MPP35_GPIO,
116 MPP36_GPIO, /* function LED */
117 MPP37_GPIO, /* alarm LED */
118 MPP38_GPIO, /* info LED */
119 MPP39_GPIO, /* power LED */
120 MPP40_GPIO, /* fan alarm */
121 MPP41_GPIO, /* funtion button */
122 MPP42_GPIO, /* power switch */
123 MPP43_GPIO, /* power auto switch */
124 MPP44_GPIO,
125 MPP45_GPIO,
126 MPP46_GPIO,
127 MPP47_GPIO,
128 MPP48_GPIO, /* function red LED */
129 MPP49_GPIO,
130 0
131 };
132
133 kirkwood_mpp_conf(kwmpp_config, NULL);
134
135 return 0;
136}
137
138#define LED_OFF 0
139#define LED_ALARM_ON 1
140#define LED_ALARM_BLINKING 2
141#define LED_POWER_ON 3
142#define LED_POWER_BLINKING 4
143#define LED_INFO_ON 5
144#define LED_INFO_BLINKING 6
145
146static void __set_led(int blink_alarm, int blink_info, int blink_power,
147 int value_alarm, int value_info, int value_power)
148{
149 kw_gpio_set_blink(GPIO_ALARM_LED, blink_alarm);
150 kw_gpio_set_blink(GPIO_INFO_LED, blink_info);
151 kw_gpio_set_blink(GPIO_POWER_LED, blink_power);
152 kw_gpio_set_value(GPIO_ALARM_LED, value_alarm);
153 kw_gpio_set_value(GPIO_INFO_LED, value_info);
154 kw_gpio_set_value(GPIO_POWER_LED, value_power);
155}
156
157static void set_led(int state)
158{
159 switch (state) {
160 case LED_OFF:
Michael Walleb31f3842012-07-30 10:47:12 +0000161 __set_led(0, 0, 0, 1, 1, 1);
Michael Walledccfa462012-06-05 11:33:17 +0000162 break;
163 case LED_ALARM_ON:
164 __set_led(0, 0, 0, 0, 1, 1);
165 break;
166 case LED_ALARM_BLINKING:
167 __set_led(1, 0, 0, 1, 1, 1);
168 break;
169 case LED_INFO_ON:
170 __set_led(0, 0, 0, 1, 0, 1);
171 break;
172 case LED_INFO_BLINKING:
173 __set_led(0, 1, 0, 1, 1, 1);
174 break;
175 case LED_POWER_ON:
176 __set_led(0, 0, 0, 1, 1, 0);
177 break;
178 case LED_POWER_BLINKING:
179 __set_led(0, 0, 1, 1, 1, 1);
180 break;
181 }
182}
183
184int board_init(void)
185{
186 /* address of boot parameters */
187 gd->bd->bi_boot_params = kw_sdram_bar(0) + 0x100;
188
189 set_led(LED_POWER_BLINKING);
190
191 return 0;
192}
193
194#ifdef CONFIG_MISC_INIT_R
Michael Walleb31f3842012-07-30 10:47:12 +0000195static void check_power_switch(void)
196{
197 if (kw_gpio_get_value(GPIO_POWER_SWITCH)) {
198 /* turn off HDD and USB power */
199 kw_gpio_set_value(GPIO_HDD_POWER, 0);
200 kw_gpio_set_value(GPIO_USB_VBUS, 0);
201 set_led(LED_OFF);
202
203 /* loop until released */
204 while (kw_gpio_get_value(GPIO_POWER_SWITCH))
205 ;
206
207 /* turn power on again */
208 kw_gpio_set_value(GPIO_HDD_POWER, 1);
209 kw_gpio_set_value(GPIO_USB_VBUS, 1);
210 set_led(LED_POWER_BLINKING);
211 }
212}
213
Michael Walledccfa462012-06-05 11:33:17 +0000214void check_enetaddr(void)
215{
216 uchar enetaddr[6];
217
218 if (!eth_getenv_enetaddr("ethaddr", enetaddr)) {
219 /* signal unset/invalid ethaddr to user */
220 set_led(LED_INFO_BLINKING);
221 }
222}
223
224static void erase_environment(void)
225{
226 struct spi_flash *flash;
227
228 printf("Erasing environment..\n");
229 flash = spi_flash_probe(0, 0, 1000000, SPI_MODE_3);
230 if (!flash) {
231 printf("Erasing flash failed\n");
232 return;
233 }
234
235 spi_flash_erase(flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE);
236 spi_flash_free(flash);
237 do_reset(NULL, 0, 0, NULL);
238}
239
240static void rescue_mode(void)
241{
242 uchar enetaddr[6];
243
244 printf("Entering rescue mode..\n");
245#ifdef CONFIG_RANDOM_MACADDR
246 if (!eth_getenv_enetaddr("ethaddr", enetaddr)) {
247 eth_random_enetaddr(enetaddr);
248 if (eth_setenv_enetaddr("ethaddr", enetaddr)) {
249 printf("Failed to set ethernet address\n");
250 set_led(LED_ALARM_BLINKING);
251 return;
252 }
253 }
254#endif
255 setenv("bootsource", "rescue");
256}
257
258static void check_push_button(void)
259{
260 int i = 0;
261
262 while (!kw_gpio_get_value(GPIO_FUNC_BUTTON)) {
263 udelay(100000);
264 i++;
265
266 if (i == 10)
267 set_led(LED_INFO_ON);
268
269 if (i >= 100) {
270 set_led(LED_INFO_BLINKING);
271 break;
272 }
273 }
274
275 if (i >= 100)
276 erase_environment();
277 else if (i >= 10)
278 rescue_mode();
279}
280
281int misc_init_r(void)
282{
Michael Walleb31f3842012-07-30 10:47:12 +0000283 check_power_switch();
Michael Walledccfa462012-06-05 11:33:17 +0000284 check_enetaddr();
285 check_push_button();
286
287 return 0;
288}
289#endif
290
291#ifdef CONFIG_SHOW_BOOT_PROGRESS
292void show_boot_progress(int progress)
293{
294 if (progress > 0)
295 return;
296
297 /* this is not an error, eg. bootp with autoload=no will trigger this */
298 if (progress == -BOOTSTAGE_ID_NET_LOADED)
299 return;
300
301 set_led(LED_ALARM_BLINKING);
302}
303#endif