]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/buffalo/lsxl/lsxl.c
Merge branch 'master' of git://git.denx.de/u-boot-microblaze
[karo-tx-uboot.git] / board / buffalo / lsxl / lsxl.c
1 /*
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
62 DECLARE_GLOBAL_DATA_PTR;
63
64 int 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
146 static 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
157 static void set_led(int state)
158 {
159         switch (state) {
160         case LED_OFF:
161                 __set_led(0, 0, 0, 0, 0, 0);
162                 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
184 int 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
195 void check_enetaddr(void)
196 {
197         uchar enetaddr[6];
198
199         if (!eth_getenv_enetaddr("ethaddr", enetaddr)) {
200                 /* signal unset/invalid ethaddr to user */
201                 set_led(LED_INFO_BLINKING);
202         }
203 }
204
205 static void erase_environment(void)
206 {
207         struct spi_flash *flash;
208
209         printf("Erasing environment..\n");
210         flash = spi_flash_probe(0, 0, 1000000, SPI_MODE_3);
211         if (!flash) {
212                 printf("Erasing flash failed\n");
213                 return;
214         }
215
216         spi_flash_erase(flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE);
217         spi_flash_free(flash);
218         do_reset(NULL, 0, 0, NULL);
219 }
220
221 static void rescue_mode(void)
222 {
223         uchar enetaddr[6];
224
225         printf("Entering rescue mode..\n");
226 #ifdef CONFIG_RANDOM_MACADDR
227         if (!eth_getenv_enetaddr("ethaddr", enetaddr)) {
228                 eth_random_enetaddr(enetaddr);
229                 if (eth_setenv_enetaddr("ethaddr", enetaddr)) {
230                         printf("Failed to set ethernet address\n");
231                                 set_led(LED_ALARM_BLINKING);
232                         return;
233                 }
234         }
235 #endif
236         setenv("bootsource", "rescue");
237 }
238
239 static void check_push_button(void)
240 {
241         int i = 0;
242
243         while (!kw_gpio_get_value(GPIO_FUNC_BUTTON)) {
244                 udelay(100000);
245                 i++;
246
247                 if (i == 10)
248                         set_led(LED_INFO_ON);
249
250                 if (i >= 100) {
251                         set_led(LED_INFO_BLINKING);
252                         break;
253                 }
254         }
255
256         if (i >= 100)
257                 erase_environment();
258         else if (i >= 10)
259                 rescue_mode();
260 }
261
262 int misc_init_r(void)
263 {
264         check_enetaddr();
265         check_push_button();
266
267         return 0;
268 }
269 #endif
270
271 #ifdef CONFIG_SHOW_BOOT_PROGRESS
272 void 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