zeus.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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/ppc4xx-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. static u32 start_time;
  42. int board_early_init_f(void)
  43. {
  44. mtdcr(UIC0SR, 0xFFFFFFFF); /* clear all ints */
  45. mtdcr(UIC0ER, 0x00000000); /* disable all ints */
  46. mtdcr(UIC0CR, 0x00000000);
  47. mtdcr(UIC0PR, 0xFFFF7F00); /* set int polarities */
  48. mtdcr(UIC0TR, 0x00000000); /* set int trigger levels */
  49. mtdcr(UIC0SR, 0xFFFFFFFF); /* clear all ints */
  50. mtdcr(UIC0VCR, 0x00000001); /* set vect base=0,INT0 highest priority */
  51. /*
  52. * Configure CPC0_PCI to enable PerWE as output
  53. */
  54. mtdcr(CPC0_PCI, CPC0_PCI_SPE);
  55. return 0;
  56. }
  57. int misc_init_r(void)
  58. {
  59. u32 pbcr;
  60. int size_val = 0;
  61. u32 post_magic;
  62. u32 post_val;
  63. post_magic = in_be32((void *)CONFIG_SYS_POST_MAGIC);
  64. post_val = in_be32((void *)CONFIG_SYS_POST_VAL);
  65. if ((post_magic == REBOOT_MAGIC) && (post_val == REBOOT_DO_POST)) {
  66. /*
  67. * Set special bootline bootparameter to pass this POST boot
  68. * mode to Linux to reset the username/password
  69. */
  70. setenv("addmisc", "setenv bootargs \\${bootargs} factory_reset=yes");
  71. /*
  72. * Normally don't run POST tests, only when enabled
  73. * via the sw-reset button. So disable further tests
  74. * upon next bootup here.
  75. */
  76. out_be32((void *)CONFIG_SYS_POST_VAL, REBOOT_NOP);
  77. } else {
  78. /*
  79. * Only run POST when initiated via the sw-reset button mechanism
  80. */
  81. post_word_store(0);
  82. }
  83. /*
  84. * Get current time
  85. */
  86. start_time = get_timer(0);
  87. /*
  88. * FLASH stuff...
  89. */
  90. /* Re-do sizing to get full correct info */
  91. /* adjust flash start and offset */
  92. mfebc(PB0CR, pbcr);
  93. switch (gd->bd->bi_flashsize) {
  94. case 1 << 20:
  95. size_val = 0;
  96. break;
  97. case 2 << 20:
  98. size_val = 1;
  99. break;
  100. case 4 << 20:
  101. size_val = 2;
  102. break;
  103. case 8 << 20:
  104. size_val = 3;
  105. break;
  106. case 16 << 20:
  107. size_val = 4;
  108. break;
  109. case 32 << 20:
  110. size_val = 5;
  111. break;
  112. case 64 << 20:
  113. size_val = 6;
  114. break;
  115. case 128 << 20:
  116. size_val = 7;
  117. break;
  118. }
  119. pbcr = (pbcr & 0x0001ffff) | gd->bd->bi_flashstart | (size_val << 17);
  120. mtebc(PB0CR, pbcr);
  121. /*
  122. * Re-check to get correct base address
  123. */
  124. flash_get_size(gd->bd->bi_flashstart, 0);
  125. /* Monitor protection ON by default */
  126. (void)flash_protect(FLAG_PROTECT_SET,
  127. -CONFIG_SYS_MONITOR_LEN,
  128. 0xffffffff,
  129. &flash_info[0]);
  130. /* Env protection ON by default */
  131. (void)flash_protect(FLAG_PROTECT_SET,
  132. CONFIG_ENV_ADDR_REDUND,
  133. CONFIG_ENV_ADDR_REDUND + 2*CONFIG_ENV_SECT_SIZE - 1,
  134. &flash_info[0]);
  135. return 0;
  136. }
  137. /*
  138. * Check Board Identity:
  139. */
  140. int checkboard(void)
  141. {
  142. char *s = getenv("serial#");
  143. puts("Board: Zeus-");
  144. if (in_be32((void *)GPIO0_IR) & GPIO_VAL(CONFIG_SYS_GPIO_ZEUS_PE))
  145. puts("PE");
  146. else
  147. puts("CE");
  148. puts(" of BulletEndPoint");
  149. if (s != NULL) {
  150. puts(", serial# ");
  151. puts(s);
  152. }
  153. putc('\n');
  154. /* both LED's off */
  155. gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 0);
  156. gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 0);
  157. udelay(10000);
  158. /* and on again */
  159. gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 1);
  160. gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 1);
  161. return (0);
  162. }
  163. static int default_env_var(char *buf, char *var)
  164. {
  165. char *ptr;
  166. char *val;
  167. /*
  168. * Find env variable
  169. */
  170. ptr = strstr(buf + 4, var);
  171. if (ptr == NULL) {
  172. printf("ERROR: %s not found!\n", var);
  173. return -1;
  174. }
  175. ptr += strlen(var) + 1;
  176. /*
  177. * Now the ethaddr needs to be updated in the "normal"
  178. * environment storage -> redundant flash.
  179. */
  180. val = ptr;
  181. setenv(var, val);
  182. printf("Updated %s from eeprom to %s!\n", var, val);
  183. return 0;
  184. }
  185. static int restore_default(void)
  186. {
  187. char *buf;
  188. char *buf_save;
  189. u32 crc;
  190. set_default_env("");
  191. gd->env_valid = 1;
  192. /*
  193. * Read board specific values from I2C EEPROM
  194. * and set env variables accordingly
  195. * -> ethaddr, eth1addr, serial#
  196. */
  197. buf = buf_save = malloc(FACTORY_RESET_ENV_SIZE);
  198. if (buf == NULL) {
  199. printf("ERROR: malloc() failed\n");
  200. return -1;
  201. }
  202. if (eeprom_read(FACTORY_RESET_I2C_EEPROM, FACTORY_RESET_ENV_OFFS,
  203. (u8 *)buf, FACTORY_RESET_ENV_SIZE)) {
  204. puts("\nError reading EEPROM!\n");
  205. } else {
  206. crc = crc32(0, (u8 *)(buf + 4), FACTORY_RESET_ENV_SIZE - 4);
  207. if (crc != *(u32 *)buf) {
  208. printf("ERROR: crc mismatch %08x %08x\n", crc, *(u32 *)buf);
  209. return -1;
  210. }
  211. default_env_var(buf, "ethaddr");
  212. buf += 8 + 18;
  213. default_env_var(buf, "eth1addr");
  214. buf += 9 + 18;
  215. default_env_var(buf, "serial#");
  216. }
  217. /*
  218. * Finally save updated env variables back to flash
  219. */
  220. saveenv();
  221. free(buf_save);
  222. return 0;
  223. }
  224. int do_set_default(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  225. {
  226. char *buf;
  227. char *buf_save;
  228. char str[32];
  229. u32 crc;
  230. char var[32];
  231. if (argc < 4) {
  232. puts("ERROR!\n");
  233. return -1;
  234. }
  235. buf = buf_save = malloc(FACTORY_RESET_ENV_SIZE);
  236. memset(buf, 0, FACTORY_RESET_ENV_SIZE);
  237. strcpy(var, "ethaddr");
  238. printf("Setting %s to %s\n", var, argv[1]);
  239. sprintf(str, "%s=%s", var, argv[1]);
  240. strcpy(buf + 4, str);
  241. buf += strlen(str) + 1;
  242. strcpy(var, "eth1addr");
  243. printf("Setting %s to %s\n", var, argv[2]);
  244. sprintf(str, "%s=%s", var, argv[2]);
  245. strcpy(buf + 4, str);
  246. buf += strlen(str) + 1;
  247. strcpy(var, "serial#");
  248. printf("Setting %s to %s\n", var, argv[3]);
  249. sprintf(str, "%s=%s", var, argv[3]);
  250. strcpy(buf + 4, str);
  251. crc = crc32(0, (u8 *)(buf_save + 4), FACTORY_RESET_ENV_SIZE - 4);
  252. *(u32 *)buf_save = crc;
  253. if (eeprom_write(FACTORY_RESET_I2C_EEPROM, FACTORY_RESET_ENV_OFFS,
  254. (u8 *)buf_save, FACTORY_RESET_ENV_SIZE)) {
  255. puts("\nError writing EEPROM!\n");
  256. return -1;
  257. }
  258. free(buf_save);
  259. return 0;
  260. }
  261. U_BOOT_CMD(
  262. setdef, 4, 1, do_set_default,
  263. "write board-specific values to EEPROM (ethaddr...)",
  264. "ethaddr eth1addr serial#\n - write board-specific values to EEPROM"
  265. );
  266. static inline int sw_reset_pressed(void)
  267. {
  268. return !(in_be32((void *)GPIO0_IR) & GPIO_VAL(CONFIG_SYS_GPIO_SW_RESET));
  269. }
  270. int do_chkreset(cmd_tbl_t* cmdtp, int flag, int argc, char * const argv[])
  271. {
  272. int delta;
  273. int count = 0;
  274. int post = 0;
  275. int factory_reset = 0;
  276. if (!sw_reset_pressed()) {
  277. printf("SW-Reset already high (Button released)\n");
  278. printf("-> No action taken!\n");
  279. return 0;
  280. }
  281. printf("Waiting for SW-Reset button to be released.");
  282. while (1) {
  283. delta = get_timer(start_time);
  284. if (!sw_reset_pressed())
  285. break;
  286. if ((delta > CONFIG_SYS_TIME_POST) && !post) {
  287. printf("\nWhen released now, POST tests will be started.");
  288. gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 0);
  289. post = 1;
  290. }
  291. if ((delta > CONFIG_SYS_TIME_FACTORY_RESET) && !factory_reset) {
  292. printf("\nWhen released now, factory default values"
  293. " will be restored.");
  294. gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 0);
  295. factory_reset = 1;
  296. }
  297. udelay(1000);
  298. if (!(count++ % 1000))
  299. printf(".");
  300. }
  301. printf("\nSW-Reset Button released after %d milli-seconds!\n", delta);
  302. if (delta > CONFIG_SYS_TIME_FACTORY_RESET) {
  303. printf("Starting factory reset value restoration...\n");
  304. /*
  305. * Restore default setting
  306. */
  307. restore_default();
  308. /*
  309. * Reset the board for default to become valid
  310. */
  311. do_reset(NULL, 0, 0, NULL);
  312. return 0;
  313. }
  314. if (delta > CONFIG_SYS_TIME_POST) {
  315. printf("Starting POST configuration...\n");
  316. /*
  317. * Enable POST upon next bootup
  318. */
  319. out_be32((void *)CONFIG_SYS_POST_MAGIC, REBOOT_MAGIC);
  320. out_be32((void *)CONFIG_SYS_POST_VAL, REBOOT_DO_POST);
  321. post_bootmode_init();
  322. /*
  323. * Reset the logbuffer for a clean start
  324. */
  325. logbuff_reset();
  326. do_reset(NULL, 0, 0, NULL);
  327. return 0;
  328. }
  329. return 0;
  330. }
  331. U_BOOT_CMD (
  332. chkreset, 1, 1, do_chkreset,
  333. "Check for status of SW-reset button and act accordingly",
  334. ""
  335. );
  336. #if defined(CONFIG_POST)
  337. /*
  338. * Returns 1 if keys pressed to start the power-on long-running tests
  339. * Called from board_init_f().
  340. */
  341. int post_hotkeys_pressed(void)
  342. {
  343. u32 post_magic;
  344. u32 post_val;
  345. post_magic = in_be32((void *)CONFIG_SYS_POST_MAGIC);
  346. post_val = in_be32((void *)CONFIG_SYS_POST_VAL);
  347. if ((post_magic == REBOOT_MAGIC) && (post_val == REBOOT_DO_POST))
  348. return 1;
  349. else
  350. return 0;
  351. }
  352. #endif /* CONFIG_POST */