lsxl.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #include <common.h>
  27. #include <net.h>
  28. #include <malloc.h>
  29. #include <netdev.h>
  30. #include <miiphy.h>
  31. #include <asm/arch/kirkwood.h>
  32. #include <asm/arch/cpu.h>
  33. #include <asm/arch/mpp.h>
  34. #include <asm/arch/gpio.h>
  35. #include <spi_flash.h>
  36. #include "lsxl.h"
  37. /*
  38. * Rescue mode
  39. *
  40. * Selected by holding the push button for 3 seconds, while powering on
  41. * the device.
  42. *
  43. * These linkstations don't have a (populated) serial port. There is no
  44. * way to access an (unmodified) board other than using the netconsole. If
  45. * you want to recover from a bad environment setting or an empty environment,
  46. * you can do this only with a working network connection. Therefore, a random
  47. * ethernet address is generated if none is set and a DHCP request is sent.
  48. * After a successful DHCP response is received, the network settings are
  49. * configured and the ncip is unset. Therefore, all netconsole packets are
  50. * broadcasted.
  51. * Additionally, the bootsource is set to 'rescue'.
  52. */
  53. #ifndef CONFIG_ENV_OVERWRITE
  54. # error "You need to set CONFIG_ENV_OVERWRITE"
  55. #endif
  56. DECLARE_GLOBAL_DATA_PTR;
  57. int board_early_init_f(void)
  58. {
  59. /*
  60. * default gpio configuration
  61. * There are maximum 64 gpios controlled through 2 sets of registers
  62. * the below configuration configures mainly initial LED status
  63. */
  64. kw_config_gpio(LSXL_OE_VAL_LOW,
  65. LSXL_OE_VAL_HIGH,
  66. LSXL_OE_LOW, LSXL_OE_HIGH);
  67. /*
  68. * Multi-Purpose Pins Functionality configuration
  69. * These strappings are taken from the original vendor uboot port.
  70. */
  71. static const u32 kwmpp_config[] = {
  72. MPP0_SPI_SCn,
  73. MPP1_SPI_MOSI,
  74. MPP2_SPI_SCK,
  75. MPP3_SPI_MISO,
  76. MPP4_UART0_RXD,
  77. MPP5_UART0_TXD,
  78. MPP6_SYSRST_OUTn,
  79. MPP7_GPO,
  80. MPP8_GPIO,
  81. MPP9_GPIO,
  82. MPP10_GPO, /* HDD power */
  83. MPP11_GPIO, /* USB Vbus enable */
  84. MPP12_SD_CLK,
  85. MPP13_SD_CMD,
  86. MPP14_SD_D0,
  87. MPP15_SD_D1,
  88. MPP16_SD_D2,
  89. MPP17_SD_D3,
  90. MPP18_GPO, /* fan speed high */
  91. MPP19_GPO, /* fan speed low */
  92. MPP20_GE1_0,
  93. MPP21_GE1_1,
  94. MPP22_GE1_2,
  95. MPP23_GE1_3,
  96. MPP24_GE1_4,
  97. MPP25_GE1_5,
  98. MPP26_GE1_6,
  99. MPP27_GE1_7,
  100. MPP28_GPIO,
  101. MPP29_GPIO,
  102. MPP30_GE1_10,
  103. MPP31_GE1_11,
  104. MPP32_GE1_12,
  105. MPP33_GE1_13,
  106. MPP34_GPIO,
  107. MPP35_GPIO,
  108. MPP36_GPIO, /* function LED */
  109. MPP37_GPIO, /* alarm LED */
  110. MPP38_GPIO, /* info LED */
  111. MPP39_GPIO, /* power LED */
  112. MPP40_GPIO, /* fan alarm */
  113. MPP41_GPIO, /* funtion button */
  114. MPP42_GPIO, /* power switch */
  115. MPP43_GPIO, /* power auto switch */
  116. MPP44_GPIO,
  117. MPP45_GPIO,
  118. MPP46_GPIO,
  119. MPP47_GPIO,
  120. MPP48_GPIO, /* function red LED */
  121. MPP49_GPIO,
  122. 0
  123. };
  124. kirkwood_mpp_conf(kwmpp_config, NULL);
  125. return 0;
  126. }
  127. #define LED_OFF 0
  128. #define LED_ALARM_ON 1
  129. #define LED_ALARM_BLINKING 2
  130. #define LED_POWER_ON 3
  131. #define LED_POWER_BLINKING 4
  132. #define LED_INFO_ON 5
  133. #define LED_INFO_BLINKING 6
  134. static void __set_led(int blink_alarm, int blink_info, int blink_power,
  135. int value_alarm, int value_info, int value_power)
  136. {
  137. kw_gpio_set_blink(GPIO_ALARM_LED, blink_alarm);
  138. kw_gpio_set_blink(GPIO_INFO_LED, blink_info);
  139. kw_gpio_set_blink(GPIO_POWER_LED, blink_power);
  140. kw_gpio_set_value(GPIO_ALARM_LED, value_alarm);
  141. kw_gpio_set_value(GPIO_INFO_LED, value_info);
  142. kw_gpio_set_value(GPIO_POWER_LED, value_power);
  143. }
  144. static void set_led(int state)
  145. {
  146. switch (state) {
  147. case LED_OFF:
  148. __set_led(0, 0, 0, 1, 1, 1);
  149. break;
  150. case LED_ALARM_ON:
  151. __set_led(0, 0, 0, 0, 1, 1);
  152. break;
  153. case LED_ALARM_BLINKING:
  154. __set_led(1, 0, 0, 1, 1, 1);
  155. break;
  156. case LED_INFO_ON:
  157. __set_led(0, 0, 0, 1, 0, 1);
  158. break;
  159. case LED_INFO_BLINKING:
  160. __set_led(0, 1, 0, 1, 1, 1);
  161. break;
  162. case LED_POWER_ON:
  163. __set_led(0, 0, 0, 1, 1, 0);
  164. break;
  165. case LED_POWER_BLINKING:
  166. __set_led(0, 0, 1, 1, 1, 1);
  167. break;
  168. }
  169. }
  170. int board_init(void)
  171. {
  172. /* address of boot parameters */
  173. gd->bd->bi_boot_params = kw_sdram_bar(0) + 0x100;
  174. set_led(LED_POWER_BLINKING);
  175. return 0;
  176. }
  177. #ifdef CONFIG_MISC_INIT_R
  178. static void check_power_switch(void)
  179. {
  180. if (kw_gpio_get_value(GPIO_POWER_SWITCH)) {
  181. /* turn off fan, HDD and USB power */
  182. kw_gpio_set_value(GPIO_HDD_POWER, 0);
  183. kw_gpio_set_value(GPIO_USB_VBUS, 0);
  184. kw_gpio_set_value(GPIO_FAN_HIGH, 1);
  185. kw_gpio_set_value(GPIO_FAN_LOW, 1);
  186. set_led(LED_OFF);
  187. /* loop until released */
  188. while (kw_gpio_get_value(GPIO_POWER_SWITCH))
  189. ;
  190. /* turn power on again */
  191. kw_gpio_set_value(GPIO_HDD_POWER, 1);
  192. kw_gpio_set_value(GPIO_USB_VBUS, 1);
  193. kw_gpio_set_value(GPIO_FAN_HIGH, 0);
  194. kw_gpio_set_value(GPIO_FAN_LOW, 0);
  195. set_led(LED_POWER_BLINKING);
  196. }
  197. }
  198. void check_enetaddr(void)
  199. {
  200. uchar enetaddr[6];
  201. if (!eth_getenv_enetaddr("ethaddr", enetaddr)) {
  202. /* signal unset/invalid ethaddr to user */
  203. set_led(LED_INFO_BLINKING);
  204. }
  205. }
  206. static void erase_environment(void)
  207. {
  208. struct spi_flash *flash;
  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. spi_flash_erase(flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE);
  216. spi_flash_free(flash);
  217. do_reset(NULL, 0, 0, NULL);
  218. }
  219. static void rescue_mode(void)
  220. {
  221. uchar enetaddr[6];
  222. printf("Entering rescue mode..\n");
  223. #ifdef CONFIG_RANDOM_MACADDR
  224. if (!eth_getenv_enetaddr("ethaddr", enetaddr)) {
  225. eth_random_enetaddr(enetaddr);
  226. if (eth_setenv_enetaddr("ethaddr", enetaddr)) {
  227. printf("Failed to set ethernet address\n");
  228. set_led(LED_ALARM_BLINKING);
  229. return;
  230. }
  231. }
  232. #endif
  233. setenv("bootsource", "rescue");
  234. }
  235. static void check_push_button(void)
  236. {
  237. int i = 0;
  238. while (!kw_gpio_get_value(GPIO_FUNC_BUTTON)) {
  239. udelay(100000);
  240. i++;
  241. if (i == 10)
  242. set_led(LED_INFO_ON);
  243. if (i >= 100) {
  244. set_led(LED_INFO_BLINKING);
  245. break;
  246. }
  247. }
  248. if (i >= 100)
  249. erase_environment();
  250. else if (i >= 10)
  251. rescue_mode();
  252. }
  253. int misc_init_r(void)
  254. {
  255. check_power_switch();
  256. check_enetaddr();
  257. check_push_button();
  258. return 0;
  259. }
  260. #endif
  261. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  262. void show_boot_progress(int progress)
  263. {
  264. if (progress > 0)
  265. return;
  266. /* this is not an error, eg. bootp with autoload=no will trigger this */
  267. if (progress == -BOOTSTAGE_ID_NET_LOADED)
  268. return;
  269. set_led(LED_ALARM_BLINKING);
  270. }
  271. #endif