common_util.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
  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. */
  24. #include <common.h>
  25. #include <command.h>
  26. #include <video_fb.h>
  27. #include "common_util.h"
  28. #include <asm/processor.h>
  29. #include <asm/byteorder.h>
  30. #include <i2c.h>
  31. #include <devices.h>
  32. #include <pci.h>
  33. #include <malloc.h>
  34. #include <bzlib.h>
  35. #ifdef CONFIG_PIP405
  36. #include "../pip405/pip405.h"
  37. #include <asm/4xx_pci.h>
  38. #endif
  39. #ifdef CONFIG_MIP405
  40. #include "../mip405/mip405.h"
  41. #include <asm/4xx_pci.h>
  42. #endif
  43. DECLARE_GLOBAL_DATA_PTR;
  44. #if defined(CONFIG_PATI)
  45. #define FIRM_START 0xFFF00000
  46. #endif
  47. extern int gunzip(void *, int, uchar *, unsigned long *);
  48. extern int mem_test(ulong start, ulong ramsize, int quiet);
  49. #define I2C_BACKUP_ADDR 0x7C00 /* 0x200 bytes for backup */
  50. #define IMAGE_SIZE CFG_MONITOR_LEN /* ugly, but it works for now */
  51. extern flash_info_t flash_info[]; /* info for FLASH chips */
  52. static int
  53. mpl_prg(uchar *src, ulong size)
  54. {
  55. ulong start;
  56. flash_info_t *info;
  57. int i, rc;
  58. #if defined(CONFIG_PATI)
  59. int start_sect;
  60. #endif
  61. #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
  62. char *copystr = (char *)src;
  63. ulong *magic = (ulong *)src;
  64. #endif
  65. info = &flash_info[0];
  66. #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
  67. if (image_to_cpu (magic[0]) != IH_MAGIC) {
  68. puts("Bad Magic number\n");
  69. return -1;
  70. }
  71. /* some more checks before we delete the Flash... */
  72. /* Checking the ISO_STRING prevents to program a
  73. * wrong Firmware Image into the flash.
  74. */
  75. i = 4; /* skip Magic number */
  76. while (1) {
  77. if (strncmp(&copystr[i], "MEV-", 4) == 0)
  78. break;
  79. if (i++ >= 0x100) {
  80. puts("Firmware Image for unknown Target\n");
  81. return -1;
  82. }
  83. }
  84. /* we have the ISO STRING, check */
  85. if (strncmp(&copystr[i], CONFIG_ISO_STRING, sizeof(CONFIG_ISO_STRING)-1) != 0) {
  86. printf("Wrong Firmware Image: %s\n", &copystr[i]);
  87. return -1;
  88. }
  89. #if !defined(CONFIG_PATI)
  90. start = 0 - size;
  91. for (i = info->sector_count-1; i > 0; i--) {
  92. info->protect[i] = 0; /* unprotect this sector */
  93. if (start >= info->start[i])
  94. break;
  95. }
  96. /* set-up flash location */
  97. /* now erase flash */
  98. printf("Erasing at %lx (sector %d) (start %lx)\n",
  99. start,i,info->start[i]);
  100. if ((rc = flash_erase (info, i, info->sector_count-1)) != 0) {
  101. puts("ERROR ");
  102. flash_perror(rc);
  103. return (1);
  104. }
  105. #else /* #if !defined(CONFIG_PATI */
  106. start = FIRM_START;
  107. start_sect = -1;
  108. for (i = 0; i < info->sector_count; i++) {
  109. if (start < info->start[i]) {
  110. start_sect = i - 1;
  111. break;
  112. }
  113. }
  114. info->protect[i - 1] = 0; /* unprotect this sector */
  115. for (; i < info->sector_count; i++) {
  116. if ((start + size) < info->start[i])
  117. break;
  118. info->protect[i] = 0; /* unprotect this sector */
  119. }
  120. i--;
  121. /* set-up flash location */
  122. /* now erase flash */
  123. printf ("Erasing at %lx to %lx (sector %d to %d) (%lx to %lx)\n",
  124. start, start + size, start_sect, i,
  125. info->start[start_sect], info->start[i]);
  126. if ((rc = flash_erase (info, start_sect, i)) != 0) {
  127. puts ("ERROR ");
  128. flash_perror (rc);
  129. return (1);
  130. }
  131. #endif /* defined(CONFIG_PATI) */
  132. #elif defined(CONFIG_VCMA9)
  133. start = 0;
  134. for (i = 0; i <info->sector_count; i++) {
  135. info->protect[i] = 0; /* unprotect this sector */
  136. if (size < info->start[i])
  137. break;
  138. }
  139. /* set-up flash location */
  140. /* now erase flash */
  141. printf("Erasing at %lx (sector %d) (start %lx)\n",
  142. start,0,info->start[0]);
  143. if ((rc = flash_erase (info, 0, i)) != 0) {
  144. puts("ERROR ");
  145. flash_perror(rc);
  146. return (1);
  147. }
  148. #endif
  149. printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",
  150. (ulong)src, size);
  151. if ((rc = flash_write ((char *)src, start, size)) != 0) {
  152. puts("ERROR ");
  153. flash_perror(rc);
  154. return (1);
  155. }
  156. puts("OK programming done\n");
  157. return 0;
  158. }
  159. static int
  160. mpl_prg_image(uchar *ld_addr)
  161. {
  162. unsigned long len;
  163. uchar *data;
  164. image_header_t *hdr = (image_header_t *)ld_addr;
  165. int rc;
  166. if (!image_check_magic (hdr)) {
  167. puts("Bad Magic Number\n");
  168. return 1;
  169. }
  170. print_image_hdr(hdr);
  171. if (!image_check_os (hdr, IH_OS_U_BOOT)) {
  172. puts("No U-Boot Image\n");
  173. return 1;
  174. }
  175. if (!image_check_type (hdr, IH_TYPE_FIRMWARE)) {
  176. puts("No Firmware Image\n");
  177. return 1;
  178. }
  179. if (!image_check_hcrc (hdr)) {
  180. puts("Bad Header Checksum\n");
  181. return 1;
  182. }
  183. puts("Verifying Checksum ... ");
  184. if (!image_check_dcrc (hdr)) {
  185. puts("Bad Data CRC\n");
  186. return 1;
  187. }
  188. puts("OK\n");
  189. data = (uchar *)image_get_data (hdr);
  190. len = image_get_data_size (hdr);
  191. if (image_get_comp (hdr) != IH_COMP_NONE) {
  192. uchar *buf;
  193. /* reserve space for uncompressed image */
  194. if ((buf = malloc(IMAGE_SIZE)) == NULL) {
  195. puts("Insufficient space for decompression\n");
  196. return 1;
  197. }
  198. switch (image_get_comp (hdr)) {
  199. case IH_COMP_GZIP:
  200. puts("Uncompressing (GZIP) ... ");
  201. rc = gunzip ((void *)(buf), IMAGE_SIZE, data, &len);
  202. if (rc != 0) {
  203. puts("GUNZIP ERROR\n");
  204. free(buf);
  205. return 1;
  206. }
  207. puts("OK\n");
  208. break;
  209. #ifdef CONFIG_BZIP2
  210. case IH_COMP_BZIP2:
  211. puts("Uncompressing (BZIP2) ... ");
  212. {
  213. uint retlen = IMAGE_SIZE;
  214. rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen,
  215. (char *)data, len, 0, 0);
  216. len = retlen;
  217. }
  218. if (rc != BZ_OK) {
  219. printf ("BUNZIP2 ERROR: %d\n", rc);
  220. free(buf);
  221. return 1;
  222. }
  223. puts("OK\n");
  224. break;
  225. #endif
  226. default:
  227. printf ("Unimplemented compression type %d\n",
  228. image_get_comp (hdr));
  229. free(buf);
  230. return 1;
  231. }
  232. rc = mpl_prg(buf, len);
  233. free(buf);
  234. } else {
  235. rc = mpl_prg(data, len);
  236. }
  237. return(rc);
  238. }
  239. #if !defined(CONFIG_PATI)
  240. void get_backup_values(backup_t *buf)
  241. {
  242. i2c_read(CFG_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)buf,sizeof(backup_t));
  243. }
  244. void set_backup_values(int overwrite)
  245. {
  246. backup_t back;
  247. int i;
  248. get_backup_values(&back);
  249. if(!overwrite) {
  250. if(strncmp(back.signature,"MPL\0",4)==0) {
  251. puts("Not possible to write Backup\n");
  252. return;
  253. }
  254. }
  255. memcpy(back.signature,"MPL\0",4);
  256. i = getenv_r("serial#",back.serial_name,16);
  257. if(i < 0) {
  258. puts("Not possible to write Backup\n");
  259. return;
  260. }
  261. back.serial_name[16]=0;
  262. i = getenv_r("ethaddr",back.eth_addr,20);
  263. if(i < 0) {
  264. puts("Not possible to write Backup\n");
  265. return;
  266. }
  267. back.eth_addr[20]=0;
  268. i2c_write(CFG_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
  269. }
  270. void clear_env_values(void)
  271. {
  272. backup_t back;
  273. unsigned char env_crc[4];
  274. memset(&back,0xff,sizeof(backup_t));
  275. memset(env_crc,0x00,4);
  276. i2c_write(CFG_DEF_EEPROM_ADDR,I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
  277. i2c_write(CFG_DEF_EEPROM_ADDR,CFG_ENV_OFFSET,2,(void *)env_crc,4);
  278. }
  279. /*
  280. * check crc of "older" environment
  281. */
  282. int check_env_old_size(ulong oldsize)
  283. {
  284. ulong crc, len, new;
  285. unsigned off;
  286. uchar buf[64];
  287. /* read old CRC */
  288. eeprom_read (CFG_DEF_EEPROM_ADDR,
  289. CFG_ENV_OFFSET,
  290. (uchar *)&crc, sizeof(ulong));
  291. new = 0;
  292. len = oldsize;
  293. off = sizeof(long);
  294. len = oldsize-off;
  295. while (len > 0) {
  296. int n = (len > sizeof(buf)) ? sizeof(buf) : len;
  297. eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, buf, n);
  298. new = crc32 (new, buf, n);
  299. len -= n;
  300. off += n;
  301. }
  302. return (crc == new);
  303. }
  304. static ulong oldsizes[] = {
  305. 0x200,
  306. 0x800,
  307. 0
  308. };
  309. void copy_old_env(ulong size)
  310. {
  311. uchar name_buf[64];
  312. uchar value_buf[0x800];
  313. uchar c;
  314. ulong len;
  315. unsigned off;
  316. uchar *name, *value;
  317. name=&name_buf[0];
  318. value=&value_buf[0];
  319. len=size;
  320. off = sizeof(long);
  321. while (len > off) {
  322. eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, &c, 1);
  323. if(c != '=') {
  324. *name++=c;
  325. off++;
  326. }
  327. else {
  328. *name++='\0';
  329. off++;
  330. do {
  331. eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, &c, 1);
  332. *value++=c;
  333. off++;
  334. if(c == '\0')
  335. break;
  336. } while(len > off);
  337. name=&name_buf[0];
  338. value=&value_buf[0];
  339. if(strncmp((char *)name,"baudrate",8)!=0) {
  340. setenv((char *)name,(char *)value);
  341. }
  342. }
  343. }
  344. }
  345. void check_env(void)
  346. {
  347. char *s;
  348. int i=0;
  349. char buf[32];
  350. backup_t back;
  351. s=getenv("serial#");
  352. if(!s) {
  353. while(oldsizes[i]) {
  354. if(check_env_old_size(oldsizes[i]))
  355. break;
  356. i++;
  357. }
  358. if(!oldsizes[i]) {
  359. /* no old environment has been found */
  360. get_backup_values (&back);
  361. if (strncmp (back.signature, "MPL\0", 4) == 0) {
  362. sprintf (buf, "%s", back.serial_name);
  363. setenv ("serial#", buf);
  364. sprintf (buf, "%s", back.eth_addr);
  365. setenv ("ethaddr", buf);
  366. printf ("INFO: serial# and ethaddr recovered, use saveenv\n");
  367. return;
  368. }
  369. }
  370. else {
  371. copy_old_env(oldsizes[i]);
  372. puts("INFO: old environment ajusted, use saveenv\n");
  373. }
  374. }
  375. else {
  376. /* check if back up is set */
  377. get_backup_values(&back);
  378. if(strncmp(back.signature,"MPL\0",4)!=0) {
  379. set_backup_values(0);
  380. }
  381. }
  382. }
  383. extern device_t *stdio_devices[];
  384. extern char *stdio_names[];
  385. void show_stdio_dev(void)
  386. {
  387. /* Print information */
  388. puts("In: ");
  389. if (stdio_devices[stdin] == NULL) {
  390. puts("No input devices available!\n");
  391. } else {
  392. printf ("%s\n", stdio_devices[stdin]->name);
  393. }
  394. puts("Out: ");
  395. if (stdio_devices[stdout] == NULL) {
  396. puts("No output devices available!\n");
  397. } else {
  398. printf ("%s\n", stdio_devices[stdout]->name);
  399. }
  400. puts("Err: ");
  401. if (stdio_devices[stderr] == NULL) {
  402. puts("No error devices available!\n");
  403. } else {
  404. printf ("%s\n", stdio_devices[stderr]->name);
  405. }
  406. }
  407. #endif /* #if !defined(CONFIG_PATI) */
  408. int do_mplcommon(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  409. {
  410. ulong size,src,ld_addr;
  411. int result;
  412. #if !defined(CONFIG_PATI)
  413. backup_t back;
  414. src = MULTI_PURPOSE_SOCKET_ADDR;
  415. size = IMAGE_SIZE;
  416. #endif
  417. if (strcmp(argv[1], "flash") == 0)
  418. {
  419. #if defined(CONFIG_CMD_FDC)
  420. if (strcmp(argv[2], "floppy") == 0) {
  421. char *local_args[3];
  422. extern int do_fdcboot (cmd_tbl_t *, int, int, char *[]);
  423. puts("\nupdating bootloader image from floppy\n");
  424. local_args[0] = argv[0];
  425. if(argc==4) {
  426. local_args[1] = argv[3];
  427. local_args[2] = NULL;
  428. ld_addr=simple_strtoul(argv[3], NULL, 16);
  429. result=do_fdcboot(cmdtp, 0, 2, local_args);
  430. }
  431. else {
  432. local_args[1] = NULL;
  433. ld_addr=CFG_LOAD_ADDR;
  434. result=do_fdcboot(cmdtp, 0, 1, local_args);
  435. }
  436. result=mpl_prg_image((uchar *)ld_addr);
  437. return result;
  438. }
  439. #endif
  440. if (strcmp(argv[2], "mem") == 0) {
  441. if(argc==4) {
  442. ld_addr=simple_strtoul(argv[3], NULL, 16);
  443. }
  444. else {
  445. ld_addr=load_addr;
  446. }
  447. printf ("\nupdating bootloader image from memory at %lX\n",ld_addr);
  448. result=mpl_prg_image((uchar *)ld_addr);
  449. return result;
  450. }
  451. #if !defined(CONFIG_PATI)
  452. if (strcmp(argv[2], "mps") == 0) {
  453. puts("\nupdating bootloader image from MPS\n");
  454. result=mpl_prg((uchar *)src,size);
  455. return result;
  456. }
  457. #endif /* #if !defined(CONFIG_PATI) */
  458. }
  459. if (strcmp(argv[1], "mem") == 0)
  460. {
  461. result=0;
  462. if(argc==3)
  463. {
  464. result = (int)simple_strtol(argv[2], NULL, 16);
  465. }
  466. src=(unsigned long)&result;
  467. src-=CFG_MEMTEST_START;
  468. src-=(100*1024); /* - 100k */
  469. src&=0xfff00000;
  470. size=0;
  471. do {
  472. size++;
  473. printf("\n\nPass %ld\n",size);
  474. mem_test(CFG_MEMTEST_START,src,1);
  475. if(ctrlc())
  476. break;
  477. if(result>0)
  478. result--;
  479. }while(result);
  480. return 0;
  481. }
  482. #if !defined(CONFIG_PATI)
  483. if (strcmp(argv[1], "clearenvvalues") == 0)
  484. {
  485. if (strcmp(argv[2], "yes") == 0)
  486. {
  487. clear_env_values();
  488. return 0;
  489. }
  490. }
  491. if (strcmp(argv[1], "getback") == 0) {
  492. get_backup_values(&back);
  493. back.signature[3]=0;
  494. back.serial_name[16]=0;
  495. back.eth_addr[20]=0;
  496. printf("GetBackUp: signature: %s\n",back.signature);
  497. printf(" serial#: %s\n",back.serial_name);
  498. printf(" ethaddr: %s\n",back.eth_addr);
  499. return 0;
  500. }
  501. if (strcmp(argv[1], "setback") == 0) {
  502. set_backup_values(1);
  503. return 0;
  504. }
  505. #endif
  506. printf("Usage:\n%s\n", cmdtp->usage);
  507. return 1;
  508. }
  509. #if defined(CONFIG_CMD_DOC)
  510. extern void doc_probe(ulong physadr);
  511. void doc_init (void)
  512. {
  513. doc_probe(MULTI_PURPOSE_SOCKET_ADDR);
  514. }
  515. #endif
  516. #ifdef CONFIG_VIDEO
  517. /******************************************************
  518. * Routines to display the Board information
  519. * to the screen (since the VGA will be initialized as last,
  520. * we must resend the infos)
  521. */
  522. #ifdef CONFIG_CONSOLE_EXTRA_INFO
  523. extern GraphicDevice ctfb;
  524. extern int get_boot_mode(void);
  525. void video_get_info_str (int line_number, char *info)
  526. {
  527. /* init video info strings for graphic console */
  528. PPC4xx_SYS_INFO sys_info;
  529. char rev;
  530. int i,boot;
  531. unsigned long pvr;
  532. char buf[64];
  533. char tmp[16];
  534. char cpustr[16];
  535. char *s, *e, bc;
  536. switch (line_number)
  537. {
  538. case 2:
  539. /* CPU and board infos */
  540. pvr=get_pvr();
  541. get_sys_info (&sys_info);
  542. switch (pvr) {
  543. case PVR_405GP_RB: rev='B'; break;
  544. case PVR_405GP_RC: rev='C'; break;
  545. case PVR_405GP_RD: rev='D'; break;
  546. case PVR_405GP_RE: rev='E'; break;
  547. case PVR_405GPR_RB: rev='B'; break;
  548. default: rev='?'; break;
  549. }
  550. if(pvr==PVR_405GPR_RB)
  551. sprintf(cpustr,"PPC405GPr %c",rev);
  552. else
  553. sprintf(cpustr,"PPC405GP %c",rev);
  554. /* Board info */
  555. i=0;
  556. s=getenv ("serial#");
  557. #ifdef CONFIG_PIP405
  558. if (!s || strncmp (s, "PIP405", 6)) {
  559. sprintf(buf,"### No HW ID - assuming PIP405");
  560. }
  561. #endif
  562. #ifdef CONFIG_MIP405
  563. if (!s || strncmp (s, "MIP405", 6)) {
  564. sprintf(buf,"### No HW ID - assuming MIP405");
  565. }
  566. #endif
  567. else {
  568. for (e = s; *e; ++e) {
  569. if (*e == ' ')
  570. break;
  571. }
  572. for (; s < e; ++s) {
  573. if (*s == '_') {
  574. ++s;
  575. break;
  576. }
  577. buf[i++]=*s;
  578. }
  579. sprintf(&buf[i]," SN ");
  580. i+=4;
  581. for (; s < e; ++s) {
  582. buf[i++]=*s;
  583. }
  584. buf[i++]=0;
  585. }
  586. sprintf (info," %s %s %s MHz (%lu/%lu/%lu MHz)",
  587. buf, cpustr,
  588. strmhz (tmp, gd->cpu_clk), sys_info.freqPLB / 1000000,
  589. sys_info.freqPLB / sys_info.pllOpbDiv / 1000000,
  590. sys_info.freqPLB / sys_info.pllExtBusDiv / 1000000);
  591. return;
  592. case 3:
  593. /* Memory Info */
  594. boot = get_boot_mode();
  595. bc = in8 (CONFIG_PORT_ADDR);
  596. sprintf(info, " %luMB RAM, %luMB Flash Cfg 0x%02X %s %s",
  597. gd->bd->bi_memsize / 0x100000,
  598. gd->bd->bi_flashsize / 0x100000,
  599. bc,
  600. (boot & BOOT_MPS) ? "MPS boot" : "Flash boot",
  601. ctfb.modeIdent);
  602. return;
  603. case 1:
  604. sprintf (buf, "%s",CONFIG_IDENT_STRING);
  605. sprintf (info, " %s", &buf[1]);
  606. return;
  607. }
  608. /* no more info lines */
  609. *info = 0;
  610. return;
  611. }
  612. #endif /* CONFIG_CONSOLE_EXTRA_INFO */
  613. #endif /* CONFIG_VIDEO */