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