lsxl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: GPL-2.0+
  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>
  8. */
  9. #include <common.h>
  10. #include <bootstage.h>
  11. #include <command.h>
  12. #include <env.h>
  13. #include <env_internal.h>
  14. #include <flash.h>
  15. #include <init.h>
  16. #include <net.h>
  17. #include <malloc.h>
  18. #include <netdev.h>
  19. #include <miiphy.h>
  20. #include <spi.h>
  21. #include <spi_flash.h>
  22. #include <asm/arch/soc.h>
  23. #include <asm/arch/cpu.h>
  24. #include <asm/arch/mpp.h>
  25. #include <asm/arch/gpio.h>
  26. #include <asm/global_data.h>
  27. #include <linux/delay.h>
  28. #include "lsxl.h"
  29. /*
  30. * Rescue mode
  31. *
  32. * Selected by holding the push button for 3 seconds, while powering on
  33. * the device.
  34. *
  35. * These linkstations don't have a (populated) serial port. There is no
  36. * way to access an (unmodified) board other than using the netconsole. If
  37. * you want to recover from a bad environment setting or an empty environment,
  38. * you can do this only with a working network connection. Therefore, a random
  39. * ethernet address is generated if none is set and a DHCP request is sent.
  40. * After a successful DHCP response is received, the network settings are
  41. * configured and the ncip is unset. Therefore, all netconsole packets are
  42. * broadcasted.
  43. * Additionally, the bootsource is set to 'rescue'.
  44. */
  45. #ifndef CONFIG_ENV_OVERWRITE
  46. # error "You need to set CONFIG_ENV_OVERWRITE"
  47. #endif
  48. DECLARE_GLOBAL_DATA_PTR;
  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. */
  56. mvebu_config_gpio(LSXL_OE_VAL_LOW,
  57. LSXL_OE_VAL_HIGH,
  58. LSXL_OE_LOW, LSXL_OE_HIGH);
  59. /*
  60. * Multi-Purpose Pins Functionality configuration
  61. * These strappings are taken from the original vendor uboot port.
  62. */
  63. static const u32 kwmpp_config[] = {
  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. kirkwood_mpp_conf(kwmpp_config, NULL);
  117. return 0;
  118. }
  119. #define LED_OFF 0
  120. #define LED_ALARM_ON 1
  121. #define LED_ALARM_BLINKING 2
  122. #define LED_POWER_ON 3
  123. #define LED_POWER_BLINKING 4
  124. #define LED_INFO_ON 5
  125. #define LED_INFO_BLINKING 6
  126. static void __set_led(int blink_alarm, int blink_info, int blink_power,
  127. int value_alarm, int value_info, int value_power)
  128. {
  129. kw_gpio_set_blink(GPIO_ALARM_LED, blink_alarm);
  130. kw_gpio_set_blink(GPIO_INFO_LED, blink_info);
  131. kw_gpio_set_blink(GPIO_POWER_LED, blink_power);
  132. kw_gpio_set_value(GPIO_ALARM_LED, value_alarm);
  133. kw_gpio_set_value(GPIO_INFO_LED, value_info);
  134. kw_gpio_set_value(GPIO_POWER_LED, value_power);
  135. }
  136. static void set_led(int state)
  137. {
  138. switch (state) {
  139. case LED_OFF:
  140. __set_led(0, 0, 0, 1, 1, 1);
  141. break;
  142. case LED_ALARM_ON:
  143. __set_led(0, 0, 0, 0, 1, 1);
  144. break;
  145. case LED_ALARM_BLINKING:
  146. __set_led(1, 0, 0, 1, 1, 1);
  147. break;
  148. case LED_INFO_ON:
  149. __set_led(0, 0, 0, 1, 0, 1);
  150. break;
  151. case LED_INFO_BLINKING:
  152. __set_led(0, 1, 0, 1, 1, 1);
  153. break;
  154. case LED_POWER_ON:
  155. __set_led(0, 0, 0, 1, 1, 0);
  156. break;
  157. case LED_POWER_BLINKING:
  158. __set_led(0, 0, 1, 1, 1, 1);
  159. break;
  160. }
  161. }
  162. int board_init(void)
  163. {
  164. /* address of boot parameters */
  165. gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
  166. set_led(LED_POWER_BLINKING);
  167. return 0;
  168. }
  169. #ifdef CONFIG_MISC_INIT_R
  170. static void check_power_switch(void)
  171. {
  172. if (kw_gpio_get_value(GPIO_POWER_SWITCH)) {
  173. /* turn off fan, HDD and USB power */
  174. kw_gpio_set_value(GPIO_HDD_POWER, 0);
  175. kw_gpio_set_value(GPIO_USB_VBUS, 0);
  176. kw_gpio_set_value(GPIO_FAN_HIGH, 1);
  177. kw_gpio_set_value(GPIO_FAN_LOW, 1);
  178. set_led(LED_OFF);
  179. /* loop until released */
  180. while (kw_gpio_get_value(GPIO_POWER_SWITCH))
  181. ;
  182. /* turn power on again */
  183. kw_gpio_set_value(GPIO_HDD_POWER, 1);
  184. kw_gpio_set_value(GPIO_USB_VBUS, 1);
  185. kw_gpio_set_value(GPIO_FAN_HIGH, 0);
  186. kw_gpio_set_value(GPIO_FAN_LOW, 0);
  187. set_led(LED_POWER_BLINKING);
  188. }
  189. }
  190. void check_enetaddr(void)
  191. {
  192. uchar enetaddr[6];
  193. if (!eth_env_get_enetaddr("ethaddr", enetaddr)) {
  194. /* signal unset/invalid ethaddr to user */
  195. set_led(LED_INFO_BLINKING);
  196. }
  197. }
  198. static void erase_environment(void)
  199. {
  200. struct spi_flash *flash;
  201. printf("Erasing environment..\n");
  202. flash = spi_flash_probe(0, 0, 1000000, SPI_MODE_3);
  203. if (!flash) {
  204. printf("Erasing flash failed\n");
  205. return;
  206. }
  207. spi_flash_erase(flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE);
  208. spi_flash_free(flash);
  209. do_reset(NULL, 0, 0, NULL);
  210. }
  211. static void rescue_mode(void)
  212. {
  213. printf("Entering rescue mode..\n");
  214. env_set("bootsource", "rescue");
  215. }
  216. static void check_push_button(void)
  217. {
  218. int i = 0;
  219. while (!kw_gpio_get_value(GPIO_FUNC_BUTTON)) {
  220. udelay(100000);
  221. i++;
  222. if (i == 10)
  223. set_led(LED_INFO_ON);
  224. if (i >= 100) {
  225. set_led(LED_INFO_BLINKING);
  226. break;
  227. }
  228. }
  229. if (i >= 100)
  230. erase_environment();
  231. else if (i >= 10)
  232. rescue_mode();
  233. }
  234. int misc_init_r(void)
  235. {
  236. check_power_switch();
  237. check_enetaddr();
  238. check_push_button();
  239. return 0;
  240. }
  241. #endif
  242. #if CONFIG_IS_ENABLED(BOOTSTAGE)
  243. void show_boot_progress(int progress)
  244. {
  245. if (progress > 0)
  246. return;
  247. /* this is not an error, eg. bootp with autoload=no will trigger this */
  248. if (progress == -BOOTSTAGE_ID_NET_LOADED)
  249. return;
  250. set_led(LED_ALARM_BLINKING);
  251. }
  252. #endif