Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 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> |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | 1ea9789 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 11 | #include <bootstage.h> |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 12 | #include <button.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 13 | #include <command.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 14 | #include <env.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 15 | #include <init.h> |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 16 | #include <led.h> |
| 17 | #include <power/regulator.h> |
Simon Glass | d34b456 | 2014-10-13 23:42:04 -0600 | [diff] [blame] | 18 | #include <spi.h> |
| 19 | #include <spi_flash.h> |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 20 | #include <asm/arch/cpu.h> |
| 21 | #include <asm/arch/mpp.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 22 | #include <asm/global_data.h> |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 23 | #include <asm/io.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 24 | #include <linux/delay.h> |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 25 | |
| 26 | #include "lsxl.h" |
| 27 | |
| 28 | /* |
| 29 | * Rescue mode |
| 30 | * |
| 31 | * Selected by holding the push button for 3 seconds, while powering on |
| 32 | * the device. |
| 33 | * |
| 34 | * These linkstations don't have a (populated) serial port. There is no |
| 35 | * way to access an (unmodified) board other than using the netconsole. If |
| 36 | * you want to recover from a bad environment setting or an empty environment, |
| 37 | * you can do this only with a working network connection. Therefore, a random |
| 38 | * ethernet address is generated if none is set and a DHCP request is sent. |
| 39 | * After a successful DHCP response is received, the network settings are |
Michael Walle | dc3ba8e | 2012-10-04 06:54:25 +0000 | [diff] [blame] | 40 | * configured and the ncip is unset. Therefore, all netconsole packets are |
| 41 | * broadcasted. |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 42 | * Additionally, the bootsource is set to 'rescue'. |
| 43 | */ |
| 44 | |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 45 | DECLARE_GLOBAL_DATA_PTR; |
| 46 | |
Michael Walle | 2e5bab1 | 2022-08-17 21:38:03 +0200 | [diff] [blame] | 47 | static bool force_rescue_mode; |
| 48 | |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 49 | int board_early_init_f(void) |
| 50 | { |
| 51 | /* |
| 52 | * default gpio configuration |
| 53 | * There are maximum 64 gpios controlled through 2 sets of registers |
| 54 | * the below configuration configures mainly initial LED status |
| 55 | */ |
Stefan Roese | c50ab39 | 2014-10-22 12:13:11 +0200 | [diff] [blame] | 56 | mvebu_config_gpio(LSXL_OE_VAL_LOW, |
| 57 | LSXL_OE_VAL_HIGH, |
| 58 | LSXL_OE_LOW, LSXL_OE_HIGH); |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * Multi-Purpose Pins Functionality configuration |
| 62 | * These strappings are taken from the original vendor uboot port. |
| 63 | */ |
Albert ARIBAUD | 4d42431 | 2012-11-26 11:27:36 +0000 | [diff] [blame] | 64 | static const u32 kwmpp_config[] = { |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 65 | MPP0_SPI_SCn, |
| 66 | MPP1_SPI_MOSI, |
| 67 | MPP2_SPI_SCK, |
| 68 | MPP3_SPI_MISO, |
| 69 | MPP4_UART0_RXD, |
| 70 | MPP5_UART0_TXD, |
| 71 | MPP6_SYSRST_OUTn, |
| 72 | MPP7_GPO, |
| 73 | MPP8_GPIO, |
| 74 | MPP9_GPIO, |
| 75 | MPP10_GPO, /* HDD power */ |
| 76 | MPP11_GPIO, /* USB Vbus enable */ |
| 77 | MPP12_SD_CLK, |
| 78 | MPP13_SD_CMD, |
| 79 | MPP14_SD_D0, |
| 80 | MPP15_SD_D1, |
| 81 | MPP16_SD_D2, |
| 82 | MPP17_SD_D3, |
| 83 | MPP18_GPO, /* fan speed high */ |
| 84 | MPP19_GPO, /* fan speed low */ |
| 85 | MPP20_GE1_0, |
| 86 | MPP21_GE1_1, |
| 87 | MPP22_GE1_2, |
| 88 | MPP23_GE1_3, |
| 89 | MPP24_GE1_4, |
| 90 | MPP25_GE1_5, |
| 91 | MPP26_GE1_6, |
| 92 | MPP27_GE1_7, |
| 93 | MPP28_GPIO, |
| 94 | MPP29_GPIO, |
| 95 | MPP30_GE1_10, |
| 96 | MPP31_GE1_11, |
| 97 | MPP32_GE1_12, |
| 98 | MPP33_GE1_13, |
| 99 | MPP34_GPIO, |
| 100 | MPP35_GPIO, |
| 101 | MPP36_GPIO, /* function LED */ |
| 102 | MPP37_GPIO, /* alarm LED */ |
| 103 | MPP38_GPIO, /* info LED */ |
| 104 | MPP39_GPIO, /* power LED */ |
| 105 | MPP40_GPIO, /* fan alarm */ |
| 106 | MPP41_GPIO, /* funtion button */ |
| 107 | MPP42_GPIO, /* power switch */ |
| 108 | MPP43_GPIO, /* power auto switch */ |
| 109 | MPP44_GPIO, |
| 110 | MPP45_GPIO, |
| 111 | MPP46_GPIO, |
| 112 | MPP47_GPIO, |
| 113 | MPP48_GPIO, /* function red LED */ |
| 114 | MPP49_GPIO, |
| 115 | 0 |
| 116 | }; |
| 117 | |
| 118 | kirkwood_mpp_conf(kwmpp_config, NULL); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 123 | enum { |
| 124 | LSXL_LED_OFF, |
| 125 | LSXL_LED_ALARM, |
| 126 | LSXL_LED_POWER, |
| 127 | LSXL_LED_INFO, |
| 128 | }; |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 129 | |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 130 | static void __set_led(int alarm, int info, int power) |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 131 | { |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 132 | struct udevice *led; |
| 133 | int ret; |
| 134 | |
| 135 | ret = led_get_by_label("lsxl:red:alarm", &led); |
| 136 | if (!ret) |
| 137 | led_set_state(led, alarm); |
| 138 | ret = led_get_by_label("lsxl:amber:info", &led); |
| 139 | if (!ret) |
| 140 | led_set_state(led, info); |
| 141 | ret = led_get_by_label("lsxl:blue:power", &led); |
| 142 | if (!ret) |
| 143 | led_set_state(led, power); |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static void set_led(int state) |
| 147 | { |
| 148 | switch (state) { |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 149 | case LSXL_LED_OFF: |
| 150 | __set_led(0, 0, 0); |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 151 | break; |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 152 | case LSXL_LED_ALARM: |
| 153 | __set_led(1, 0, 0); |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 154 | break; |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 155 | case LSXL_LED_INFO: |
| 156 | __set_led(0, 1, 0); |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 157 | break; |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 158 | case LSXL_LED_POWER: |
| 159 | __set_led(0, 0, 1); |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 160 | break; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | int board_init(void) |
| 165 | { |
| 166 | /* address of boot parameters */ |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 167 | gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100; |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 168 | |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 169 | set_led(LSXL_LED_POWER); |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
Michael Walle | b31f384 | 2012-07-30 10:47:12 +0000 | [diff] [blame] | 174 | static void check_power_switch(void) |
| 175 | { |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 176 | struct udevice *power_button, *hdd_power, *usb_power; |
| 177 | int ret; |
| 178 | |
| 179 | ret = button_get_by_label("Power-on Switch", &power_button); |
| 180 | if (ret) |
| 181 | goto err; |
| 182 | |
| 183 | ret = regulator_get_by_platname("HDD Power", &hdd_power); |
| 184 | if (ret) |
| 185 | goto err; |
| 186 | |
| 187 | ret = regulator_get_by_platname("USB Power", &usb_power); |
| 188 | if (ret) |
| 189 | goto err; |
| 190 | |
| 191 | if (button_get_state(power_button) == BUTTON_OFF) { |
| 192 | ret = regulator_set_enable(hdd_power, false); |
| 193 | if (ret) |
| 194 | goto err; |
| 195 | ret = regulator_set_enable(usb_power, false); |
| 196 | if (ret) |
| 197 | goto err; |
| 198 | /* TODO: fan off */ |
| 199 | set_led(LSXL_LED_OFF); |
Michael Walle | b31f384 | 2012-07-30 10:47:12 +0000 | [diff] [blame] | 200 | |
| 201 | /* loop until released */ |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 202 | while (button_get_state(power_button) == BUTTON_OFF) |
Michael Walle | b31f384 | 2012-07-30 10:47:12 +0000 | [diff] [blame] | 203 | ; |
| 204 | |
| 205 | /* turn power on again */ |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 206 | ret = regulator_set_enable(hdd_power, true); |
| 207 | if (ret) |
| 208 | goto err; |
| 209 | ret = regulator_set_enable(usb_power, true); |
| 210 | if (ret) |
| 211 | goto err; |
| 212 | /* TODO: fan on */ |
| 213 | set_led(LSXL_LED_POWER); |
| 214 | }; |
| 215 | |
| 216 | return; |
| 217 | err: |
| 218 | printf("error in %s\n", __func__); |
Michael Walle | b31f384 | 2012-07-30 10:47:12 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 221 | void check_enetaddr(void) |
| 222 | { |
| 223 | uchar enetaddr[6]; |
| 224 | |
Simon Glass | 399a9ce | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 225 | if (!eth_env_get_enetaddr("ethaddr", enetaddr)) { |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 226 | /* signal unset/invalid ethaddr to user */ |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 227 | set_led(LSXL_LED_INFO); |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | static void erase_environment(void) |
| 232 | { |
| 233 | struct spi_flash *flash; |
| 234 | |
| 235 | printf("Erasing environment..\n"); |
| 236 | flash = spi_flash_probe(0, 0, 1000000, SPI_MODE_3); |
| 237 | if (!flash) { |
| 238 | printf("Erasing flash failed\n"); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | spi_flash_erase(flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE); |
| 243 | spi_flash_free(flash); |
| 244 | do_reset(NULL, 0, 0, NULL); |
| 245 | } |
| 246 | |
| 247 | static void rescue_mode(void) |
| 248 | { |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 249 | printf("Entering rescue mode..\n"); |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 250 | env_set("bootsource", "rescue"); |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static void check_push_button(void) |
| 254 | { |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 255 | struct udevice *func_button; |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 256 | int i = 0; |
| 257 | |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 258 | int ret; |
| 259 | |
| 260 | ret = button_get_by_label("Function Button", &func_button); |
| 261 | if (ret) |
| 262 | goto err; |
| 263 | |
| 264 | while (button_get_state(func_button) == BUTTON_ON) { |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 265 | udelay(100000); |
| 266 | i++; |
| 267 | |
| 268 | if (i == 10) |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 269 | set_led(LSXL_LED_INFO); |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 270 | |
| 271 | if (i >= 100) { |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 272 | set_led(LSXL_LED_ALARM); |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 273 | break; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | if (i >= 100) |
| 278 | erase_environment(); |
| 279 | else if (i >= 10) |
Michael Walle | 2e5bab1 | 2022-08-17 21:38:03 +0200 | [diff] [blame] | 280 | force_rescue_mode = true; |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 281 | |
| 282 | return; |
| 283 | err: |
| 284 | printf("error in %s\n", __func__); |
Michael Walle | 2e5bab1 | 2022-08-17 21:38:03 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | int board_early_init_r(void) |
| 288 | { |
| 289 | check_push_button(); |
| 290 | |
| 291 | return 0; |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | int misc_init_r(void) |
| 295 | { |
Michael Walle | b31f384 | 2012-07-30 10:47:12 +0000 | [diff] [blame] | 296 | check_power_switch(); |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 297 | check_enetaddr(); |
Michael Walle | 2e5bab1 | 2022-08-17 21:38:03 +0200 | [diff] [blame] | 298 | if (force_rescue_mode) |
| 299 | rescue_mode(); |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 300 | |
| 301 | return 0; |
| 302 | } |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 303 | |
Tom Rini | a9765d0 | 2021-05-03 16:48:58 -0400 | [diff] [blame] | 304 | #if CONFIG_IS_ENABLED(BOOTSTAGE) |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 305 | void show_boot_progress(int progress) |
| 306 | { |
| 307 | if (progress > 0) |
| 308 | return; |
| 309 | |
| 310 | /* this is not an error, eg. bootp with autoload=no will trigger this */ |
| 311 | if (progress == -BOOTSTAGE_ID_NET_LOADED) |
| 312 | return; |
| 313 | |
Michael Walle | b8666dd | 2022-08-17 21:38:04 +0200 | [diff] [blame] | 314 | set_led(LSXL_LED_ALARM); |
Michael Walle | dccfa46 | 2012-06-05 11:33:17 +0000 | [diff] [blame] | 315 | } |
| 316 | #endif |