zeus.c 9.1 KB

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