cmd_flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@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. /*
  24. * FLASH support
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #ifdef CONFIG_HAS_DATAFLASH
  29. #include <dataflash.h>
  30. #endif
  31. #if (CONFIG_COMMANDS & CFG_CMD_FLASH)
  32. extern flash_info_t flash_info[]; /* info for FLASH chips */
  33. /*
  34. * The user interface starts numbering for Flash banks with 1
  35. * for historical reasons.
  36. */
  37. /*
  38. * this routine looks for an abbreviated flash range specification.
  39. * the syntax is B:SF[-SL], where B is the bank number, SF is the first
  40. * sector to erase, and SL is the last sector to erase (defaults to SF).
  41. * bank numbers start at 1 to be consistent with other specs, sector numbers
  42. * start at zero.
  43. *
  44. * returns: 1 - correct spec; *pinfo, *psf and *psl are
  45. * set appropriately
  46. * 0 - doesn't look like an abbreviated spec
  47. * -1 - looks like an abbreviated spec, but got
  48. * a parsing error, a number out of range,
  49. * or an invalid flash bank.
  50. */
  51. static int
  52. abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
  53. {
  54. flash_info_t *fp;
  55. int bank, first, last;
  56. char *p, *ep;
  57. if ((p = strchr (str, ':')) == NULL)
  58. return 0;
  59. *p++ = '\0';
  60. bank = simple_strtoul (str, &ep, 10);
  61. if (ep == str || *ep != '\0' ||
  62. bank < 1 || bank > CFG_MAX_FLASH_BANKS ||
  63. (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
  64. return -1;
  65. str = p;
  66. if ((p = strchr (str, '-')) != NULL)
  67. *p++ = '\0';
  68. first = simple_strtoul (str, &ep, 10);
  69. if (ep == str || *ep != '\0' || first >= fp->sector_count)
  70. return -1;
  71. if (p != NULL) {
  72. last = simple_strtoul (p, &ep, 10);
  73. if (ep == p || *ep != '\0' ||
  74. last < first || last >= fp->sector_count)
  75. return -1;
  76. } else {
  77. last = first;
  78. }
  79. *pinfo = fp;
  80. *psf = first;
  81. *psl = last;
  82. return 1;
  83. }
  84. static int
  85. flash_fill_sect_ranges (ulong addr_first, ulong addr_last,
  86. int *s_first, int *s_last,
  87. int *s_count )
  88. {
  89. flash_info_t *info;
  90. ulong bank;
  91. int rcode = 0;
  92. *s_count = 0;
  93. for (bank=0; bank < CFG_MAX_FLASH_BANKS; ++bank) {
  94. s_first[bank] = -1; /* first sector to erase */
  95. s_last [bank] = -1; /* last sector to erase */
  96. }
  97. for (bank=0,info=&flash_info[0];
  98. (bank < CFG_MAX_FLASH_BANKS) && (addr_first <= addr_last);
  99. ++bank, ++info) {
  100. ulong b_end;
  101. int sect;
  102. short s_end;
  103. if (info->flash_id == FLASH_UNKNOWN) {
  104. continue;
  105. }
  106. b_end = info->start[0] + info->size - 1; /* bank end addr */
  107. s_end = info->sector_count - 1; /* last sector */
  108. for (sect=0; sect < info->sector_count; ++sect) {
  109. ulong end; /* last address in current sect */
  110. end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
  111. if (addr_first > end)
  112. continue;
  113. if (addr_last < info->start[sect])
  114. continue;
  115. if (addr_first == info->start[sect]) {
  116. s_first[bank] = sect;
  117. }
  118. if (addr_last == end) {
  119. s_last[bank] = sect;
  120. }
  121. }
  122. if (s_first[bank] >= 0) {
  123. if (s_last[bank] < 0) {
  124. if (addr_last > b_end) {
  125. s_last[bank] = s_end;
  126. } else {
  127. puts ("Error: end address"
  128. " not on sector boundary\n");
  129. rcode = 1;
  130. break;
  131. }
  132. }
  133. if (s_last[bank] < s_first[bank]) {
  134. puts ("Error: end sector"
  135. " precedes start sector\n");
  136. rcode = 1;
  137. break;
  138. }
  139. sect = s_last[bank];
  140. addr_first = (sect == s_end) ? b_end + 1: info->start[sect + 1];
  141. (*s_count) += s_last[bank] - s_first[bank] + 1;
  142. } else if (addr_first >= info->start[0] && addr_first < b_end) {
  143. puts ("Error: start address not on sector boundary\n");
  144. rcode = 1;
  145. break;
  146. } else if (s_last[bank] >= 0) {
  147. puts ("Error: cannot span across banks when they are"
  148. " mapped in reverse order\n");
  149. rcode = 1;
  150. break;
  151. }
  152. }
  153. return rcode;
  154. }
  155. int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  156. {
  157. ulong bank;
  158. #ifdef CONFIG_HAS_DATAFLASH
  159. dataflash_print_info();
  160. #endif
  161. if (argc == 1) { /* print info for all FLASH banks */
  162. for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) {
  163. printf ("\nBank # %ld: ", bank+1);
  164. flash_print_info (&flash_info[bank]);
  165. }
  166. return 0;
  167. }
  168. bank = simple_strtoul(argv[1], NULL, 16);
  169. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  170. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  171. CFG_MAX_FLASH_BANKS);
  172. return 1;
  173. }
  174. printf ("\nBank # %ld: ", bank);
  175. flash_print_info (&flash_info[bank-1]);
  176. return 0;
  177. }
  178. int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  179. {
  180. flash_info_t *info;
  181. ulong bank, addr_first, addr_last;
  182. int n, sect_first, sect_last;
  183. int rcode = 0;
  184. if (argc < 2) {
  185. printf ("Usage:\n%s\n", cmdtp->usage);
  186. return 1;
  187. }
  188. if (strcmp(argv[1], "all") == 0) {
  189. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  190. printf ("Erase Flash Bank # %ld ", bank);
  191. info = &flash_info[bank-1];
  192. rcode = flash_erase (info, 0, info->sector_count-1);
  193. }
  194. return rcode;
  195. }
  196. if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
  197. if (n < 0) {
  198. puts ("Bad sector specification\n");
  199. return 1;
  200. }
  201. printf ("Erase Flash Sectors %d-%d in Bank # %d ",
  202. sect_first, sect_last, (info-flash_info)+1);
  203. rcode = flash_erase(info, sect_first, sect_last);
  204. return rcode;
  205. }
  206. if (argc != 3) {
  207. printf ("Usage:\n%s\n", cmdtp->usage);
  208. return 1;
  209. }
  210. if (strcmp(argv[1], "bank") == 0) {
  211. bank = simple_strtoul(argv[2], NULL, 16);
  212. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  213. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  214. CFG_MAX_FLASH_BANKS);
  215. return 1;
  216. }
  217. printf ("Erase Flash Bank # %ld ", bank);
  218. info = &flash_info[bank-1];
  219. rcode = flash_erase (info, 0, info->sector_count-1);
  220. return rcode;
  221. }
  222. addr_first = simple_strtoul(argv[1], NULL, 16);
  223. addr_last = simple_strtoul(argv[2], NULL, 16);
  224. if (addr_first >= addr_last) {
  225. printf ("Usage:\n%s\n", cmdtp->usage);
  226. return 1;
  227. }
  228. rcode = flash_sect_erase(addr_first, addr_last);
  229. return rcode;
  230. }
  231. int flash_sect_erase (ulong addr_first, ulong addr_last)
  232. {
  233. flash_info_t *info;
  234. ulong bank;
  235. int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
  236. int erased = 0;
  237. int planned;
  238. int rcode = 0;
  239. rcode = flash_fill_sect_ranges (addr_first, addr_last,
  240. s_first, s_last, &planned );
  241. if (planned && (rcode == 0)) {
  242. for (bank=0,info=&flash_info[0];
  243. (bank < CFG_MAX_FLASH_BANKS) && (rcode == 0);
  244. ++bank, ++info) {
  245. if (s_first[bank]>=0) {
  246. erased += s_last[bank] - s_first[bank] + 1;
  247. debug ("Erase Flash from 0x%08lx to 0x%08lx "
  248. "in Bank # %ld ",
  249. info->start[s_first[bank]],
  250. (s_last[bank] == info->sector_count) ?
  251. info->start[0] + info->size - 1:
  252. info->start[s_last[bank]+1] - 1,
  253. bank+1);
  254. rcode = flash_erase (info, s_first[bank], s_last[bank]);
  255. }
  256. }
  257. printf ("Erased %d sectors\n", erased);
  258. } else if (rcode == 0) {
  259. puts ("Error: start and/or end address"
  260. " not on sector boundary\n");
  261. rcode = 1;
  262. }
  263. return rcode;
  264. }
  265. int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  266. {
  267. flash_info_t *info;
  268. ulong bank, addr_first, addr_last;
  269. int i, p, n, sect_first, sect_last;
  270. int rcode = 0;
  271. #ifdef CONFIG_HAS_DATAFLASH
  272. int status;
  273. #endif
  274. if (argc < 3) {
  275. printf ("Usage:\n%s\n", cmdtp->usage);
  276. return 1;
  277. }
  278. if (strcmp(argv[1], "off") == 0) {
  279. p = 0;
  280. } else if (strcmp(argv[1], "on") == 0) {
  281. p = 1;
  282. } else {
  283. printf ("Usage:\n%s\n", cmdtp->usage);
  284. return 1;
  285. }
  286. #ifdef CONFIG_HAS_DATAFLASH
  287. if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
  288. addr_first = simple_strtoul(argv[2], NULL, 16);
  289. addr_last = simple_strtoul(argv[3], NULL, 16);
  290. if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
  291. status = dataflash_real_protect(p,addr_first,addr_last);
  292. if (status < 0){
  293. puts ("Bad DataFlash sector specification\n");
  294. return 1;
  295. }
  296. printf("%sProtect %d DataFlash Sectors\n",
  297. p ? "" : "Un-", status);
  298. return 0;
  299. }
  300. }
  301. #endif
  302. if (strcmp(argv[2], "all") == 0) {
  303. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  304. info = &flash_info[bank-1];
  305. if (info->flash_id == FLASH_UNKNOWN) {
  306. continue;
  307. }
  308. printf ("%sProtect Flash Bank # %ld\n",
  309. p ? "" : "Un-", bank);
  310. for (i=0; i<info->sector_count; ++i) {
  311. #if defined(CFG_FLASH_PROTECTION)
  312. if (flash_real_protect(info, i, p))
  313. rcode = 1;
  314. putc ('.');
  315. #else
  316. info->protect[i] = p;
  317. #endif /* CFG_FLASH_PROTECTION */
  318. }
  319. }
  320. #if defined(CFG_FLASH_PROTECTION)
  321. if (!rcode) puts (" done\n");
  322. #endif /* CFG_FLASH_PROTECTION */
  323. return rcode;
  324. }
  325. if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
  326. if (n < 0) {
  327. puts ("Bad sector specification\n");
  328. return 1;
  329. }
  330. printf("%sProtect Flash Sectors %d-%d in Bank # %d\n",
  331. p ? "" : "Un-", sect_first, sect_last,
  332. (info-flash_info)+1);
  333. for (i = sect_first; i <= sect_last; i++) {
  334. #if defined(CFG_FLASH_PROTECTION)
  335. if (flash_real_protect(info, i, p))
  336. rcode = 1;
  337. putc ('.');
  338. #else
  339. info->protect[i] = p;
  340. #endif /* CFG_FLASH_PROTECTION */
  341. }
  342. #if defined(CFG_FLASH_PROTECTION)
  343. if (!rcode) puts (" done\n");
  344. #endif /* CFG_FLASH_PROTECTION */
  345. return rcode;
  346. }
  347. if (argc != 4) {
  348. printf ("Usage:\n%s\n", cmdtp->usage);
  349. return 1;
  350. }
  351. if (strcmp(argv[2], "bank") == 0) {
  352. bank = simple_strtoul(argv[3], NULL, 16);
  353. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  354. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  355. CFG_MAX_FLASH_BANKS);
  356. return 1;
  357. }
  358. printf ("%sProtect Flash Bank # %ld\n",
  359. p ? "" : "Un-", bank);
  360. info = &flash_info[bank-1];
  361. if (info->flash_id == FLASH_UNKNOWN) {
  362. puts ("missing or unknown FLASH type\n");
  363. return 1;
  364. }
  365. for (i=0; i<info->sector_count; ++i) {
  366. #if defined(CFG_FLASH_PROTECTION)
  367. if (flash_real_protect(info, i, p))
  368. rcode = 1;
  369. putc ('.');
  370. #else
  371. info->protect[i] = p;
  372. #endif /* CFG_FLASH_PROTECTION */
  373. }
  374. #if defined(CFG_FLASH_PROTECTION)
  375. if (!rcode) puts (" done\n");
  376. #endif /* CFG_FLASH_PROTECTION */
  377. return rcode;
  378. }
  379. addr_first = simple_strtoul(argv[2], NULL, 16);
  380. addr_last = simple_strtoul(argv[3], NULL, 16);
  381. if (addr_first >= addr_last) {
  382. printf ("Usage:\n%s\n", cmdtp->usage);
  383. return 1;
  384. }
  385. rcode = flash_sect_protect (p, addr_first, addr_last);
  386. return rcode;
  387. }
  388. int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
  389. {
  390. flash_info_t *info;
  391. ulong bank;
  392. int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
  393. int protected, i;
  394. int planned;
  395. int rcode;
  396. rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
  397. protected = 0;
  398. if (planned && (rcode == 0)) {
  399. for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) {
  400. if (info->flash_id == FLASH_UNKNOWN) {
  401. continue;
  402. }
  403. if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
  404. debug ("%sProtecting sectors %d..%d in bank %ld\n",
  405. p ? "" : "Un-",
  406. s_first[bank], s_last[bank], bank+1);
  407. protected += s_last[bank] - s_first[bank] + 1;
  408. for (i=s_first[bank]; i<=s_last[bank]; ++i) {
  409. #if defined(CFG_FLASH_PROTECTION)
  410. if (flash_real_protect(info, i, p))
  411. rcode = 1;
  412. putc ('.');
  413. #else
  414. info->protect[i] = p;
  415. #endif /* CFG_FLASH_PROTECTION */
  416. }
  417. }
  418. #if defined(CFG_FLASH_PROTECTION)
  419. if (!rcode) putc ('\n');
  420. #endif /* CFG_FLASH_PROTECTION */
  421. }
  422. printf ("%sProtected %d sectors\n",
  423. p ? "" : "Un-", protected);
  424. } else if (rcode == 0) {
  425. puts ("Error: start and/or end address"
  426. " not on sector boundary\n");
  427. rcode = 1;
  428. }
  429. return rcode;
  430. }
  431. /**************************************************/
  432. U_BOOT_CMD(
  433. flinfo, 2, 1, do_flinfo,
  434. "flinfo - print FLASH memory information\n",
  435. "\n - print information for all FLASH memory banks\n"
  436. "flinfo N\n - print information for FLASH memory bank # N\n"
  437. );
  438. U_BOOT_CMD(
  439. erase, 3, 1, do_flerase,
  440. "erase - erase FLASH memory\n",
  441. "start end\n"
  442. " - erase FLASH from addr 'start' to addr 'end'\n"
  443. "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
  444. "erase bank N\n - erase FLASH bank # N\n"
  445. "erase all\n - erase all FLASH banks\n"
  446. );
  447. U_BOOT_CMD(
  448. protect, 4, 1, do_protect,
  449. "protect - enable or disable FLASH write protection\n",
  450. "on start end\n"
  451. " - protect FLASH from addr 'start' to addr 'end'\n"
  452. "protect on N:SF[-SL]\n"
  453. " - protect sectors SF-SL in FLASH bank # N\n"
  454. "protect on bank N\n - protect FLASH bank # N\n"
  455. "protect on all\n - protect all FLASH banks\n"
  456. "protect off start end\n"
  457. " - make FLASH from addr 'start' to addr 'end' writable\n"
  458. "protect off N:SF[-SL]\n"
  459. " - make sectors SF-SL writable in FLASH bank # N\n"
  460. "protect off bank N\n - make FLASH bank # N writable\n"
  461. "protect off all\n - make all FLASH banks writable\n"
  462. );
  463. #endif /* CFG_CMD_FLASH */