zeus.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  42. static u32 start_time;
  43. int board_early_init_f(void)
  44. {
  45. mtdcr(UIC0SR, 0xFFFFFFFF); /* clear all ints */
  46. mtdcr(UIC0ER, 0x00000000); /* disable all ints */
  47. mtdcr(UIC0CR, 0x00000000);
  48. mtdcr(UIC0PR, 0xFFFF7F00); /* set int polarities */
  49. mtdcr(UIC0TR, 0x00000000); /* set int trigger levels */
  50. mtdcr(UIC0SR, 0xFFFFFFFF); /* clear all ints */
  51. mtdcr(UIC0VCR, 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. set_default_env("");
  192. gd->env_valid = 1;
  193. /*
  194. * Read board specific values from I2C EEPROM
  195. * and set env variables accordingly
  196. * -> ethaddr, eth1addr, serial#
  197. */
  198. buf = buf_save = malloc(FACTORY_RESET_ENV_SIZE);
  199. if (buf == NULL) {
  200. printf("ERROR: malloc() failed\n");
  201. return -1;
  202. }
  203. if (eeprom_read(FACTORY_RESET_I2C_EEPROM, FACTORY_RESET_ENV_OFFS,
  204. (u8 *)buf, FACTORY_RESET_ENV_SIZE)) {
  205. puts("\nError reading EEPROM!\n");
  206. } else {
  207. crc = crc32(0, (u8 *)(buf + 4), FACTORY_RESET_ENV_SIZE - 4);
  208. if (crc != *(u32 *)buf) {
  209. printf("ERROR: crc mismatch %08x %08x\n", crc, *(u32 *)buf);
  210. return -1;
  211. }
  212. default_env_var(buf, "ethaddr");
  213. buf += 8 + 18;
  214. default_env_var(buf, "eth1addr");
  215. buf += 9 + 18;
  216. default_env_var(buf, "serial#");
  217. }
  218. /*
  219. * Finally save updated env variables back to flash
  220. */
  221. saveenv();
  222. free(buf_save);
  223. return 0;
  224. }
  225. int do_set_default(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  226. {
  227. char *buf;
  228. char *buf_save;
  229. char str[32];
  230. u32 crc;
  231. char var[32];
  232. if (argc < 4) {
  233. puts("ERROR!\n");
  234. return -1;
  235. }
  236. buf = buf_save = malloc(FACTORY_RESET_ENV_SIZE);
  237. memset(buf, 0, FACTORY_RESET_ENV_SIZE);
  238. strcpy(var, "ethaddr");
  239. printf("Setting %s to %s\n", var, argv[1]);
  240. sprintf(str, "%s=%s", var, argv[1]);
  241. strcpy(buf + 4, str);
  242. buf += strlen(str) + 1;
  243. strcpy(var, "eth1addr");
  244. printf("Setting %s to %s\n", var, argv[2]);
  245. sprintf(str, "%s=%s", var, argv[2]);
  246. strcpy(buf + 4, str);
  247. buf += strlen(str) + 1;
  248. strcpy(var, "serial#");
  249. printf("Setting %s to %s\n", var, argv[3]);
  250. sprintf(str, "%s=%s", var, argv[3]);
  251. strcpy(buf + 4, str);
  252. crc = crc32(0, (u8 *)(buf_save + 4), FACTORY_RESET_ENV_SIZE - 4);
  253. *(u32 *)buf_save = crc;
  254. if (eeprom_write(FACTORY_RESET_I2C_EEPROM, FACTORY_RESET_ENV_OFFS,
  255. (u8 *)buf_save, FACTORY_RESET_ENV_SIZE)) {
  256. puts("\nError writing EEPROM!\n");
  257. return -1;
  258. }
  259. free(buf_save);
  260. return 0;
  261. }
  262. U_BOOT_CMD(
  263. setdef, 4, 1, do_set_default,
  264. "write board-specific values to EEPROM (ethaddr...)",
  265. "ethaddr eth1addr serial#\n - write board-specific values to EEPROM"
  266. );
  267. static inline int sw_reset_pressed(void)
  268. {
  269. return !(in_be32((void *)GPIO0_IR) & GPIO_VAL(CONFIG_SYS_GPIO_SW_RESET));
  270. }
  271. int do_chkreset(cmd_tbl_t* cmdtp, int flag, int argc, char * const argv[])
  272. {
  273. int delta;
  274. int count = 0;
  275. int post = 0;
  276. int factory_reset = 0;
  277. if (!sw_reset_pressed()) {
  278. printf("SW-Reset already high (Button released)\n");
  279. printf("-> No action taken!\n");
  280. return 0;
  281. }
  282. printf("Waiting for SW-Reset button to be released.");
  283. while (1) {
  284. delta = get_timer(start_time);
  285. if (!sw_reset_pressed())
  286. break;
  287. if ((delta > CONFIG_SYS_TIME_POST) && !post) {
  288. printf("\nWhen released now, POST tests will be started.");
  289. gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 0);
  290. post = 1;
  291. }
  292. if ((delta > CONFIG_SYS_TIME_FACTORY_RESET) && !factory_reset) {
  293. printf("\nWhen released now, factory default values"
  294. " will be restored.");
  295. gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 0);
  296. factory_reset = 1;
  297. }
  298. udelay(1000);
  299. if (!(count++ % 1000))
  300. printf(".");
  301. }
  302. printf("\nSW-Reset Button released after %d milli-seconds!\n", delta);
  303. if (delta > CONFIG_SYS_TIME_FACTORY_RESET) {
  304. printf("Starting factory reset value restoration...\n");
  305. /*
  306. * Restore default setting
  307. */
  308. restore_default();
  309. /*
  310. * Reset the board for default to become valid
  311. */
  312. do_reset(NULL, 0, 0, NULL);
  313. return 0;
  314. }
  315. if (delta > CONFIG_SYS_TIME_POST) {
  316. printf("Starting POST configuration...\n");
  317. /*
  318. * Enable POST upon next bootup
  319. */
  320. out_be32((void *)CONFIG_SYS_POST_MAGIC, REBOOT_MAGIC);
  321. out_be32((void *)CONFIG_SYS_POST_VAL, REBOOT_DO_POST);
  322. post_bootmode_init();
  323. /*
  324. * Reset the logbuffer for a clean start
  325. */
  326. logbuff_reset();
  327. do_reset(NULL, 0, 0, NULL);
  328. return 0;
  329. }
  330. return 0;
  331. }
  332. U_BOOT_CMD (
  333. chkreset, 1, 1, do_chkreset,
  334. "Check for status of SW-reset button and act accordingly",
  335. ""
  336. );
  337. #if defined(CONFIG_POST)
  338. /*
  339. * Returns 1 if keys pressed to start the power-on long-running tests
  340. * Called from board_init_f().
  341. */
  342. int post_hotkeys_pressed(void)
  343. {
  344. u32 post_magic;
  345. u32 post_val;
  346. post_magic = in_be32((void *)CONFIG_SYS_POST_MAGIC);
  347. post_val = in_be32((void *)CONFIG_SYS_POST_VAL);
  348. if ((post_magic == REBOOT_MAGIC) && (post_val == REBOOT_DO_POST))
  349. return 1;
  350. else
  351. return 0;
  352. }
  353. #endif /* CONFIG_POST */