lsxl.c 7.0 KB

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