zeus.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * (C) Copyright 2007
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <malloc.h>
  26. #include <environment.h>
  27. #include <logbuff.h>
  28. #include <post.h>
  29. #include <asm/processor.h>
  30. #include <asm/io.h>
  31. #include <asm/gpio.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. #define REBOOT_MAGIC 0x07081967
  34. #define REBOOT_NOP 0x00000000
  35. #define REBOOT_DO_POST 0x00000001
  36. extern flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  37. extern env_t *env_ptr;
  38. extern uchar default_environment[];
  39. ulong flash_get_size(ulong base, int banknum);
  40. void env_crc_update(void);
  41. int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  42. static u32 start_time;
  43. int board_early_init_f(void)
  44. {
  45. mtdcr(uicsr, 0xFFFFFFFF); /* clear all ints */
  46. mtdcr(uicer, 0x00000000); /* disable all ints */
  47. mtdcr(uiccr, 0x00000000);
  48. mtdcr(uicpr, 0xFFFF7F00); /* set int polarities */
  49. mtdcr(uictr, 0x00000000); /* set int trigger levels */
  50. mtdcr(uicsr, 0xFFFFFFFF); /* clear all ints */
  51. mtdcr(uicvcr, 0x00000001); /* set vect base=0,INT0 highest priority */
  52. /*
  53. * Configure CPC0_PCI to enable PerWE as output
  54. */
  55. mtdcr(cpc0_pci, CPC0_PCI_SPE);
  56. return 0;
  57. }
  58. int misc_init_r(void)
  59. {
  60. u32 pbcr;
  61. int size_val = 0;
  62. u32 post_magic;
  63. u32 post_val;
  64. post_magic = in_be32((void *)CONFIG_SYS_POST_MAGIC);
  65. post_val = in_be32((void *)CONFIG_SYS_POST_VAL);
  66. if ((post_magic == REBOOT_MAGIC) && (post_val == REBOOT_DO_POST)) {
  67. /*
  68. * Set special bootline bootparameter to pass this POST boot
  69. * mode to Linux to reset the username/password
  70. */
  71. setenv("addmisc", "setenv bootargs \\${bootargs} factory_reset=yes");
  72. /*
  73. * Normally don't run POST tests, only when enabled
  74. * via the sw-reset button. So disable further tests
  75. * upon next bootup here.
  76. */
  77. out_be32((void *)CONFIG_SYS_POST_VAL, REBOOT_NOP);
  78. } else {
  79. /*
  80. * Only run POST when initiated via the sw-reset button mechanism
  81. */
  82. post_word_store(0);
  83. }
  84. /*
  85. * Get current time
  86. */
  87. start_time = get_timer(0);
  88. /*
  89. * FLASH stuff...
  90. */
  91. /* Re-do sizing to get full correct info */
  92. /* adjust flash start and offset */
  93. mfebc(pb0cr, pbcr);
  94. switch (gd->bd->bi_flashsize) {
  95. case 1 << 20:
  96. size_val = 0;
  97. break;
  98. case 2 << 20:
  99. size_val = 1;
  100. break;
  101. case 4 << 20:
  102. size_val = 2;
  103. break;
  104. case 8 << 20:
  105. size_val = 3;
  106. break;
  107. case 16 << 20:
  108. size_val = 4;
  109. break;
  110. case 32 << 20:
  111. size_val = 5;
  112. break;
  113. case 64 << 20:
  114. size_val = 6;
  115. break;
  116. case 128 << 20:
  117. size_val = 7;
  118. break;
  119. }
  120. pbcr = (pbcr & 0x0001ffff) | gd->bd->bi_flashstart | (size_val << 17);
  121. mtebc(pb0cr, pbcr);
  122. /*
  123. * Re-check to get correct base address
  124. */
  125. flash_get_size(gd->bd->bi_flashstart, 0);
  126. /* Monitor protection ON by default */
  127. (void)flash_protect(FLAG_PROTECT_SET,
  128. -CONFIG_SYS_MONITOR_LEN,
  129. 0xffffffff,
  130. &flash_info[0]);
  131. /* Env protection ON by default */
  132. (void)flash_protect(FLAG_PROTECT_SET,
  133. CONFIG_ENV_ADDR_REDUND,
  134. CONFIG_ENV_ADDR_REDUND + 2*CONFIG_ENV_SECT_SIZE - 1,
  135. &flash_info[0]);
  136. return 0;
  137. }
  138. /*
  139. * Check Board Identity:
  140. */
  141. int checkboard(void)
  142. {
  143. char *s = getenv("serial#");
  144. puts("Board: Zeus-");
  145. if (in_be32((void *)GPIO0_IR) & GPIO_VAL(CONFIG_SYS_GPIO_ZEUS_PE))
  146. puts("PE");
  147. else
  148. puts("CE");
  149. puts(" of BulletEndPoint");
  150. if (s != NULL) {
  151. puts(", serial# ");
  152. puts(s);
  153. }
  154. putc('\n');
  155. /* both LED's off */
  156. gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 0);
  157. gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 0);
  158. udelay(10000);
  159. /* and on again */
  160. gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 1);
  161. gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 1);
  162. return (0);
  163. }
  164. static int default_env_var(char *buf, char *var)
  165. {
  166. char *ptr;
  167. char *val;
  168. /*
  169. * Find env variable
  170. */
  171. ptr = strstr(buf + 4, var);
  172. if (ptr == NULL) {
  173. printf("ERROR: %s not found!\n", var);
  174. return -1;
  175. }
  176. ptr += strlen(var) + 1;
  177. /*
  178. * Now the ethaddr needs to be updated in the "normal"
  179. * environment storage -> redundant flash.
  180. */
  181. val = ptr;
  182. setenv(var, val);
  183. printf("Updated %s from eeprom to %s!\n", var, val);
  184. return 0;
  185. }
  186. static int restore_default(void)
  187. {
  188. char *buf;
  189. char *buf_save;
  190. u32 crc;
  191. /*
  192. * Unprotect and erase environment area
  193. */
  194. flash_protect(FLAG_PROTECT_CLEAR,
  195. CONFIG_ENV_ADDR_REDUND,
  196. CONFIG_ENV_ADDR_REDUND + 2*CONFIG_ENV_SECT_SIZE - 1,
  197. &flash_info[0]);
  198. flash_sect_erase(CONFIG_ENV_ADDR_REDUND,
  199. CONFIG_ENV_ADDR_REDUND + 2*CONFIG_ENV_SECT_SIZE - 1);
  200. /*
  201. * Now restore default environment from U-Boot image
  202. * -> ipaddr, serverip...
  203. */
  204. memset(env_ptr, 0, sizeof(env_t));
  205. memcpy(env_ptr->data, default_environment, ENV_SIZE);
  206. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  207. env_ptr->flags = 0xFF;
  208. #endif
  209. env_crc_update();
  210. gd->env_valid = 1;
  211. /*
  212. * Read board specific values from I2C EEPROM
  213. * and set env variables accordingly
  214. * -> ethaddr, eth1addr, serial#
  215. */
  216. buf = buf_save = malloc(FACTORY_RESET_ENV_SIZE);
  217. if (eeprom_read(FACTORY_RESET_I2C_EEPROM, FACTORY_RESET_ENV_OFFS,
  218. (u8 *)buf, FACTORY_RESET_ENV_SIZE)) {
  219. puts("\nError reading EEPROM!\n");
  220. } else {
  221. crc = crc32(0, (u8 *)(buf + 4), FACTORY_RESET_ENV_SIZE - 4);
  222. if (crc != *(u32 *)buf) {
  223. printf("ERROR: crc mismatch %08x %08x\n", crc, *(u32 *)buf);
  224. return -1;
  225. }
  226. default_env_var(buf, "ethaddr");
  227. buf += 8 + 18;
  228. default_env_var(buf, "eth1addr");
  229. buf += 9 + 18;
  230. default_env_var(buf, "serial#");
  231. }
  232. /*
  233. * Finally save updated env variables back to flash
  234. */
  235. saveenv();
  236. free(buf_save);
  237. return 0;
  238. }
  239. int do_set_default(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  240. {
  241. char *buf;
  242. char *buf_save;
  243. char str[32];
  244. u32 crc;
  245. char var[32];
  246. if (argc < 4) {
  247. puts("ERROR!\n");
  248. return -1;
  249. }
  250. buf = buf_save = malloc(FACTORY_RESET_ENV_SIZE);
  251. memset(buf, 0, FACTORY_RESET_ENV_SIZE);
  252. strcpy(var, "ethaddr");
  253. printf("Setting %s to %s\n", var, argv[1]);
  254. sprintf(str, "%s=%s", var, argv[1]);
  255. strcpy(buf + 4, str);
  256. buf += strlen(str) + 1;
  257. strcpy(var, "eth1addr");
  258. printf("Setting %s to %s\n", var, argv[2]);
  259. sprintf(str, "%s=%s", var, argv[2]);
  260. strcpy(buf + 4, str);
  261. buf += strlen(str) + 1;
  262. strcpy(var, "serial#");
  263. printf("Setting %s to %s\n", var, argv[3]);
  264. sprintf(str, "%s=%s", var, argv[3]);
  265. strcpy(buf + 4, str);
  266. crc = crc32(0, (u8 *)(buf_save + 4), FACTORY_RESET_ENV_SIZE - 4);
  267. *(u32 *)buf_save = crc;
  268. if (eeprom_write(FACTORY_RESET_I2C_EEPROM, FACTORY_RESET_ENV_OFFS,
  269. (u8 *)buf_save, FACTORY_RESET_ENV_SIZE)) {
  270. puts("\nError writing EEPROM!\n");
  271. return -1;
  272. }
  273. free(buf_save);
  274. return 0;
  275. }
  276. U_BOOT_CMD(
  277. setdef, 4, 1, do_set_default,
  278. "write board-specific values to EEPROM (ethaddr...)",
  279. "ethaddr eth1addr serial#\n - write board-specific values to EEPROM\n"
  280. );
  281. static inline int sw_reset_pressed(void)
  282. {
  283. return !(in_be32((void *)GPIO0_IR) & GPIO_VAL(CONFIG_SYS_GPIO_SW_RESET));
  284. }
  285. int do_chkreset(cmd_tbl_t* cmdtp, int flag, int argc, char* argv[])
  286. {
  287. int delta;
  288. int count = 0;
  289. int post = 0;
  290. int factory_reset = 0;
  291. if (!sw_reset_pressed()) {
  292. printf("SW-Reset already high (Button released)\n");
  293. printf("-> No action taken!\n");
  294. return 0;
  295. }
  296. printf("Waiting for SW-Reset button to be released.");
  297. while (1) {
  298. delta = get_timer(start_time);
  299. if (!sw_reset_pressed())
  300. break;
  301. if ((delta > CONFIG_SYS_TIME_POST) && !post) {
  302. printf("\nWhen released now, POST tests will be started.");
  303. gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 0);
  304. post = 1;
  305. }
  306. if ((delta > CONFIG_SYS_TIME_FACTORY_RESET) && !factory_reset) {
  307. printf("\nWhen released now, factory default values"
  308. " will be restored.");
  309. gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 0);
  310. factory_reset = 1;
  311. }
  312. udelay(1000);
  313. if (!(count++ % 1000))
  314. printf(".");
  315. }
  316. printf("\nSW-Reset Button released after %d milli-seconds!\n", delta);
  317. if (delta > CONFIG_SYS_TIME_FACTORY_RESET) {
  318. printf("Starting factory reset value restoration...\n");
  319. /*
  320. * Restore default setting
  321. */
  322. restore_default();
  323. /*
  324. * Reset the board for default to become valid
  325. */
  326. do_reset(NULL, 0, 0, NULL);
  327. return 0;
  328. }
  329. if (delta > CONFIG_SYS_TIME_POST) {
  330. printf("Starting POST configuration...\n");
  331. /*
  332. * Enable POST upon next bootup
  333. */
  334. out_be32((void *)CONFIG_SYS_POST_MAGIC, REBOOT_MAGIC);
  335. out_be32((void *)CONFIG_SYS_POST_VAL, REBOOT_DO_POST);
  336. post_bootmode_init();
  337. /*
  338. * Reset the logbuffer for a clean start
  339. */
  340. logbuff_reset();
  341. do_reset(NULL, 0, 0, NULL);
  342. return 0;
  343. }
  344. return 0;
  345. }
  346. U_BOOT_CMD (
  347. chkreset, 1, 1, do_chkreset,
  348. "Check for status of SW-reset button and act accordingly",
  349. NULL
  350. );
  351. #if defined(CONFIG_POST)
  352. /*
  353. * Returns 1 if keys pressed to start the power-on long-running tests
  354. * Called from board_init_f().
  355. */
  356. int post_hotkeys_pressed(void)
  357. {
  358. u32 post_magic;
  359. u32 post_val;
  360. post_magic = in_be32((void *)CONFIG_SYS_POST_MAGIC);
  361. post_val = in_be32((void *)CONFIG_SYS_POST_VAL);
  362. if ((post_magic == REBOOT_MAGIC) && (post_val == REBOOT_DO_POST))
  363. return 1;
  364. else
  365. return 0;
  366. }
  367. #endif /* CONFIG_POST */