cmd_flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. printf ("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. printf ("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 (s_last[bank] >= 0) {
  143. printf("Error: cannot span across banks when they are"
  144. " mapped in reverse order\n");
  145. rcode = 1;
  146. break;
  147. }
  148. }
  149. return rcode;
  150. }
  151. int do_flinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  152. {
  153. ulong bank;
  154. #ifdef CONFIG_HAS_DATAFLASH
  155. dataflash_print_info();
  156. #endif
  157. if (argc == 1) { /* print info for all FLASH banks */
  158. for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) {
  159. printf ("\nBank # %ld: ", bank+1);
  160. flash_print_info (&flash_info[bank]);
  161. }
  162. return 0;
  163. }
  164. bank = simple_strtoul(argv[1], NULL, 16);
  165. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  166. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  167. CFG_MAX_FLASH_BANKS);
  168. return 1;
  169. }
  170. printf ("\nBank # %ld: ", bank);
  171. flash_print_info (&flash_info[bank-1]);
  172. return 0;
  173. }
  174. int do_flerase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  175. {
  176. flash_info_t *info;
  177. ulong bank, addr_first, addr_last;
  178. int n, sect_first, sect_last;
  179. int rcode = 0;
  180. if (argc < 2) {
  181. printf ("Usage:\n%s\n", cmdtp->usage);
  182. return 1;
  183. }
  184. if (strcmp(argv[1], "all") == 0) {
  185. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  186. printf ("Erase Flash Bank # %ld ", bank);
  187. info = &flash_info[bank-1];
  188. rcode = flash_erase (info, 0, info->sector_count-1);
  189. }
  190. return rcode;
  191. }
  192. if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
  193. if (n < 0) {
  194. printf("Bad sector specification\n");
  195. return 1;
  196. }
  197. printf ("Erase Flash Sectors %d-%d in Bank # %d ",
  198. sect_first, sect_last, (info-flash_info)+1);
  199. rcode = flash_erase(info, sect_first, sect_last);
  200. return rcode;
  201. }
  202. if (argc != 3) {
  203. printf ("Usage:\n%s\n", cmdtp->usage);
  204. return 1;
  205. }
  206. if (strcmp(argv[1], "bank") == 0) {
  207. bank = simple_strtoul(argv[2], NULL, 16);
  208. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  209. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  210. CFG_MAX_FLASH_BANKS);
  211. return 1;
  212. }
  213. printf ("Erase Flash Bank # %ld ", bank);
  214. info = &flash_info[bank-1];
  215. rcode = flash_erase (info, 0, info->sector_count-1);
  216. return rcode;
  217. }
  218. addr_first = simple_strtoul(argv[1], NULL, 16);
  219. addr_last = simple_strtoul(argv[2], NULL, 16);
  220. if (addr_first >= addr_last) {
  221. printf ("Usage:\n%s\n", cmdtp->usage);
  222. return 1;
  223. }
  224. rcode = flash_sect_erase(addr_first, addr_last);
  225. return rcode;
  226. }
  227. int flash_sect_erase (ulong addr_first, ulong addr_last)
  228. {
  229. flash_info_t *info;
  230. ulong bank;
  231. int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
  232. int erased = 0;
  233. int planned;
  234. int rcode = 0;
  235. rcode = flash_fill_sect_ranges (addr_first, addr_last,
  236. s_first, s_last, &planned );
  237. if (planned && (rcode == 0)) {
  238. for (bank=0,info=&flash_info[0];
  239. (bank < CFG_MAX_FLASH_BANKS) && (rcode == 0);
  240. ++bank, ++info) {
  241. if (s_first[bank]>=0) {
  242. erased += s_last[bank] - s_first[bank] + 1;
  243. debug ("Erase Flash from 0x%08lx to 0x%08lx "
  244. "in Bank # %ld ",
  245. info->start[s_first[bank]],
  246. (s_last[bank] == info->sector_count) ?
  247. info->start[0] + info->size - 1:
  248. info->start[s_last[bank]+1] - 1,
  249. bank+1);
  250. rcode = flash_erase (info, s_first[bank], s_last[bank]);
  251. }
  252. }
  253. printf ("Erased %d sectors\n", erased);
  254. } else if (rcode == 0) {
  255. printf ("Error: start and/or end address"
  256. " not on sector boundary\n");
  257. rcode = 1;
  258. }
  259. return rcode;
  260. }
  261. int do_protect (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  262. {
  263. flash_info_t *info;
  264. ulong bank, addr_first, addr_last;
  265. int i, p, n, sect_first, sect_last;
  266. int rcode = 0;
  267. #ifdef CONFIG_HAS_DATAFLASH
  268. int status;
  269. #endif
  270. if (argc < 3) {
  271. printf ("Usage:\n%s\n", cmdtp->usage);
  272. return 1;
  273. }
  274. if (strcmp(argv[1], "off") == 0) {
  275. p = 0;
  276. } else if (strcmp(argv[1], "on") == 0) {
  277. p = 1;
  278. } else {
  279. printf ("Usage:\n%s\n", cmdtp->usage);
  280. return 1;
  281. }
  282. #ifdef CONFIG_HAS_DATAFLASH
  283. if ((strcmp(argv[2], "all") != 0) && (strcmp(argv[2], "bank") != 0)) {
  284. addr_first = simple_strtoul(argv[2], NULL, 16);
  285. addr_last = simple_strtoul(argv[3], NULL, 16);
  286. if (addr_dataflash(addr_first) && addr_dataflash(addr_last)) {
  287. status = dataflash_real_protect(p,addr_first,addr_last);
  288. if (status < 0){
  289. printf("Bad DataFlash sector specification\n");
  290. return 1;
  291. }
  292. printf("%sProtect %d DataFlash Sectors\n",
  293. p ? "" : "Un-", status);
  294. return 0;
  295. }
  296. }
  297. #endif
  298. if (strcmp(argv[2], "all") == 0) {
  299. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  300. info = &flash_info[bank-1];
  301. if (info->flash_id == FLASH_UNKNOWN) {
  302. continue;
  303. }
  304. printf ("%sProtect Flash Bank # %ld\n",
  305. p ? "" : "Un-", bank);
  306. for (i=0; i<info->sector_count; ++i) {
  307. #if defined(CFG_FLASH_PROTECTION)
  308. if (flash_real_protect(info, i, p))
  309. rcode = 1;
  310. putc ('.');
  311. #else
  312. info->protect[i] = p;
  313. #endif /* CFG_FLASH_PROTECTION */
  314. }
  315. }
  316. #if defined(CFG_FLASH_PROTECTION)
  317. if (!rcode) puts (" done\n");
  318. #endif /* CFG_FLASH_PROTECTION */
  319. return rcode;
  320. }
  321. if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
  322. if (n < 0) {
  323. printf("Bad sector specification\n");
  324. return 1;
  325. }
  326. printf("%sProtect Flash Sectors %d-%d in Bank # %d\n",
  327. p ? "" : "Un-", sect_first, sect_last,
  328. (info-flash_info)+1);
  329. for (i = sect_first; i <= sect_last; i++) {
  330. #if defined(CFG_FLASH_PROTECTION)
  331. if (flash_real_protect(info, i, p))
  332. rcode = 1;
  333. putc ('.');
  334. #else
  335. info->protect[i] = p;
  336. #endif /* CFG_FLASH_PROTECTION */
  337. }
  338. #if defined(CFG_FLASH_PROTECTION)
  339. if (!rcode) puts (" done\n");
  340. #endif /* CFG_FLASH_PROTECTION */
  341. return rcode;
  342. }
  343. if (argc != 4) {
  344. printf ("Usage:\n%s\n", cmdtp->usage);
  345. return 1;
  346. }
  347. if (strcmp(argv[2], "bank") == 0) {
  348. bank = simple_strtoul(argv[3], NULL, 16);
  349. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  350. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  351. CFG_MAX_FLASH_BANKS);
  352. return 1;
  353. }
  354. printf ("%sProtect Flash Bank # %ld\n",
  355. p ? "" : "Un-", bank);
  356. info = &flash_info[bank-1];
  357. if (info->flash_id == FLASH_UNKNOWN) {
  358. printf ("missing or unknown FLASH type\n");
  359. return 1;
  360. }
  361. for (i=0; i<info->sector_count; ++i) {
  362. #if defined(CFG_FLASH_PROTECTION)
  363. if (flash_real_protect(info, i, p))
  364. rcode = 1;
  365. putc ('.');
  366. #else
  367. info->protect[i] = p;
  368. #endif /* CFG_FLASH_PROTECTION */
  369. }
  370. #if defined(CFG_FLASH_PROTECTION)
  371. if (!rcode) puts (" done\n");
  372. #endif /* CFG_FLASH_PROTECTION */
  373. return rcode;
  374. }
  375. addr_first = simple_strtoul(argv[2], NULL, 16);
  376. addr_last = simple_strtoul(argv[3], NULL, 16);
  377. if (addr_first >= addr_last) {
  378. printf ("Usage:\n%s\n", cmdtp->usage);
  379. return 1;
  380. }
  381. rcode = flash_sect_protect (p, addr_first, addr_last);
  382. return rcode;
  383. }
  384. int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
  385. {
  386. flash_info_t *info;
  387. ulong bank;
  388. int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
  389. int protected, i;
  390. int planned;
  391. int rcode;
  392. rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
  393. protected = 0;
  394. if (planned && (rcode == 0)) {
  395. for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) {
  396. if (info->flash_id == FLASH_UNKNOWN) {
  397. continue;
  398. }
  399. if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
  400. debug ("%sProtecting sectors %d..%d in bank %ld\n",
  401. p ? "" : "Un-",
  402. s_first[bank], s_last[bank], bank+1);
  403. protected += s_last[bank] - s_first[bank] + 1;
  404. for (i=s_first[bank]; i<=s_last[bank]; ++i) {
  405. #if defined(CFG_FLASH_PROTECTION)
  406. if (flash_real_protect(info, i, p))
  407. rcode = 1;
  408. putc ('.');
  409. #else
  410. info->protect[i] = p;
  411. #endif /* CFG_FLASH_PROTECTION */
  412. }
  413. }
  414. #if defined(CFG_FLASH_PROTECTION)
  415. if (!rcode) putc ('\n');
  416. #endif /* CFG_FLASH_PROTECTION */
  417. }
  418. printf ("%sProtected %d sectors\n",
  419. p ? "" : "Un-", protected);
  420. } else if (rcode == 0) {
  421. printf ("Error: start and/or end address"
  422. " not on sector boundary\n");
  423. rcode = 1;
  424. }
  425. return rcode;
  426. }
  427. /**************************************************/
  428. U_BOOT_CMD(
  429. flinfo, 2, 1, do_flinfo,
  430. "flinfo - print FLASH memory information\n",
  431. "\n - print information for all FLASH memory banks\n"
  432. "flinfo N\n - print information for FLASH memory bank # N\n"
  433. );
  434. U_BOOT_CMD(
  435. erase, 3, 1, do_flerase,
  436. "erase - erase FLASH memory\n",
  437. "start end\n"
  438. " - erase FLASH from addr 'start' to addr 'end'\n"
  439. "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
  440. "erase bank N\n - erase FLASH bank # N\n"
  441. "erase all\n - erase all FLASH banks\n"
  442. );
  443. U_BOOT_CMD(
  444. protect, 4, 1, do_protect,
  445. "protect - enable or disable FLASH write protection\n",
  446. "on start end\n"
  447. " - protect FLASH from addr 'start' to addr 'end'\n"
  448. "protect on N:SF[-SL]\n"
  449. " - protect sectors SF-SL in FLASH bank # N\n"
  450. "protect on bank N\n - protect FLASH bank # N\n"
  451. "protect on all\n - protect all FLASH banks\n"
  452. "protect off start end\n"
  453. " - make FLASH from addr 'start' to addr 'end' writable\n"
  454. "protect off N:SF[-SL]\n"
  455. " - make sectors SF-SL writable in FLASH bank # N\n"
  456. "protect off bank N\n - make FLASH bank # N writable\n"
  457. "protect off all\n - make all FLASH banks writable\n"
  458. );
  459. #endif /* CFG_CMD_FLASH */