Michael Walle | f214a20 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 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: |
Michael Walle | a9f1a48 | 2012-07-30 10:47:12 +0000 | [diff] [blame] | 161 | __set_led(0, 0, 0, 1, 1, 1); |
Michael Walle | f214a20 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 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 |
Michael Walle | a9f1a48 | 2012-07-30 10:47:12 +0000 | [diff] [blame] | 195 | static void check_power_switch(void) |
| 196 | { |
| 197 | if (kw_gpio_get_value(GPIO_POWER_SWITCH)) { |
Michael Walle | 9bd2317 | 2012-09-30 03:11:05 +0000 | [diff] [blame] | 198 | /* turn off fan, HDD and USB power */ |
Michael Walle | a9f1a48 | 2012-07-30 10:47:12 +0000 | [diff] [blame] | 199 | kw_gpio_set_value(GPIO_HDD_POWER, 0); |
| 200 | kw_gpio_set_value(GPIO_USB_VBUS, 0); |
Michael Walle | 9bd2317 | 2012-09-30 03:11:05 +0000 | [diff] [blame] | 201 | kw_gpio_set_value(GPIO_FAN_HIGH, 1); |
| 202 | kw_gpio_set_value(GPIO_FAN_LOW, 1); |
Michael Walle | a9f1a48 | 2012-07-30 10:47:12 +0000 | [diff] [blame] | 203 | set_led(LED_OFF); |
| 204 | |
| 205 | /* loop until released */ |
| 206 | while (kw_gpio_get_value(GPIO_POWER_SWITCH)) |
| 207 | ; |
| 208 | |
| 209 | /* turn power on again */ |
| 210 | kw_gpio_set_value(GPIO_HDD_POWER, 1); |
| 211 | kw_gpio_set_value(GPIO_USB_VBUS, 1); |
Michael Walle | 9bd2317 | 2012-09-30 03:11:05 +0000 | [diff] [blame] | 212 | kw_gpio_set_value(GPIO_FAN_HIGH, 0); |
| 213 | kw_gpio_set_value(GPIO_FAN_LOW, 0); |
Michael Walle | a9f1a48 | 2012-07-30 10:47:12 +0000 | [diff] [blame] | 214 | set_led(LED_POWER_BLINKING); |
| 215 | } |
| 216 | } |
| 217 | |
Michael Walle | f214a20 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 218 | void check_enetaddr(void) |
| 219 | { |
| 220 | uchar enetaddr[6]; |
| 221 | |
| 222 | if (!eth_getenv_enetaddr("ethaddr", enetaddr)) { |
| 223 | /* signal unset/invalid ethaddr to user */ |
| 224 | set_led(LED_INFO_BLINKING); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static void erase_environment(void) |
| 229 | { |
| 230 | struct spi_flash *flash; |
| 231 | |
| 232 | printf("Erasing environment..\n"); |
| 233 | flash = spi_flash_probe(0, 0, 1000000, SPI_MODE_3); |
| 234 | if (!flash) { |
| 235 | printf("Erasing flash failed\n"); |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | spi_flash_erase(flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE); |
| 240 | spi_flash_free(flash); |
| 241 | do_reset(NULL, 0, 0, NULL); |
| 242 | } |
| 243 | |
| 244 | static void rescue_mode(void) |
| 245 | { |
| 246 | uchar enetaddr[6]; |
| 247 | |
| 248 | printf("Entering rescue mode..\n"); |
| 249 | #ifdef CONFIG_RANDOM_MACADDR |
| 250 | if (!eth_getenv_enetaddr("ethaddr", enetaddr)) { |
| 251 | eth_random_enetaddr(enetaddr); |
| 252 | if (eth_setenv_enetaddr("ethaddr", enetaddr)) { |
| 253 | printf("Failed to set ethernet address\n"); |
| 254 | set_led(LED_ALARM_BLINKING); |
| 255 | return; |
| 256 | } |
| 257 | } |
| 258 | #endif |
| 259 | setenv("bootsource", "rescue"); |
| 260 | } |
| 261 | |
| 262 | static void check_push_button(void) |
| 263 | { |
| 264 | int i = 0; |
| 265 | |
| 266 | while (!kw_gpio_get_value(GPIO_FUNC_BUTTON)) { |
| 267 | udelay(100000); |
| 268 | i++; |
| 269 | |
| 270 | if (i == 10) |
| 271 | set_led(LED_INFO_ON); |
| 272 | |
| 273 | if (i >= 100) { |
| 274 | set_led(LED_INFO_BLINKING); |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | if (i >= 100) |
| 280 | erase_environment(); |
| 281 | else if (i >= 10) |
| 282 | rescue_mode(); |
| 283 | } |
| 284 | |
| 285 | int misc_init_r(void) |
| 286 | { |
Michael Walle | a9f1a48 | 2012-07-30 10:47:12 +0000 | [diff] [blame] | 287 | check_power_switch(); |
Michael Walle | f214a20 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 288 | check_enetaddr(); |
| 289 | check_push_button(); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | #endif |
| 294 | |
| 295 | #ifdef CONFIG_SHOW_BOOT_PROGRESS |
| 296 | void show_boot_progress(int progress) |
| 297 | { |
| 298 | if (progress > 0) |
| 299 | return; |
| 300 | |
| 301 | /* this is not an error, eg. bootp with autoload=no will trigger this */ |
| 302 | if (progress == -BOOTSTAGE_ID_NET_LOADED) |
| 303 | return; |
| 304 | |
| 305 | set_led(LED_ALARM_BLINKING); |
| 306 | } |
| 307 | #endif |