cmd_flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. if (argc < 3) {
  268. printf ("Usage:\n%s\n", cmdtp->usage);
  269. return 1;
  270. }
  271. if (strcmp(argv[1], "off") == 0) {
  272. p = 0;
  273. } else if (strcmp(argv[1], "on") == 0) {
  274. p = 1;
  275. } else {
  276. printf ("Usage:\n%s\n", cmdtp->usage);
  277. return 1;
  278. }
  279. if (strcmp(argv[2], "all") == 0) {
  280. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  281. info = &flash_info[bank-1];
  282. if (info->flash_id == FLASH_UNKNOWN) {
  283. continue;
  284. }
  285. printf ("%sProtect Flash Bank # %ld\n",
  286. p ? "" : "Un-", bank);
  287. for (i=0; i<info->sector_count; ++i) {
  288. #if defined(CFG_FLASH_PROTECTION)
  289. if (flash_real_protect(info, i, p))
  290. rcode = 1;
  291. putc ('.');
  292. #else
  293. info->protect[i] = p;
  294. #endif /* CFG_FLASH_PROTECTION */
  295. }
  296. }
  297. #if defined(CFG_FLASH_PROTECTION)
  298. if (!rcode) puts (" done\n");
  299. #endif /* CFG_FLASH_PROTECTION */
  300. return rcode;
  301. }
  302. if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
  303. if (n < 0) {
  304. printf("Bad sector specification\n");
  305. return 1;
  306. }
  307. printf("%sProtect Flash Sectors %d-%d in Bank # %d\n",
  308. p ? "" : "Un-", sect_first, sect_last,
  309. (info-flash_info)+1);
  310. for (i = sect_first; i <= sect_last; 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. #if defined(CFG_FLASH_PROTECTION)
  320. if (!rcode) puts (" done\n");
  321. #endif /* CFG_FLASH_PROTECTION */
  322. return rcode;
  323. }
  324. if (argc != 4) {
  325. printf ("Usage:\n%s\n", cmdtp->usage);
  326. return 1;
  327. }
  328. if (strcmp(argv[2], "bank") == 0) {
  329. bank = simple_strtoul(argv[3], NULL, 16);
  330. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  331. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  332. CFG_MAX_FLASH_BANKS);
  333. return 1;
  334. }
  335. printf ("%sProtect Flash Bank # %ld\n",
  336. p ? "" : "Un-", bank);
  337. info = &flash_info[bank-1];
  338. if (info->flash_id == FLASH_UNKNOWN) {
  339. printf ("missing or unknown FLASH type\n");
  340. return 1;
  341. }
  342. for (i=0; i<info->sector_count; ++i) {
  343. #if defined(CFG_FLASH_PROTECTION)
  344. if (flash_real_protect(info, i, p))
  345. rcode = 1;
  346. putc ('.');
  347. #else
  348. info->protect[i] = p;
  349. #endif /* CFG_FLASH_PROTECTION */
  350. }
  351. #if defined(CFG_FLASH_PROTECTION)
  352. if (!rcode) puts (" done\n");
  353. #endif /* CFG_FLASH_PROTECTION */
  354. return rcode;
  355. }
  356. addr_first = simple_strtoul(argv[2], NULL, 16);
  357. addr_last = simple_strtoul(argv[3], NULL, 16);
  358. if (addr_first >= addr_last) {
  359. printf ("Usage:\n%s\n", cmdtp->usage);
  360. return 1;
  361. }
  362. rcode = flash_sect_protect (p, addr_first, addr_last);
  363. return rcode;
  364. }
  365. int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
  366. {
  367. flash_info_t *info;
  368. ulong bank;
  369. int s_first[CFG_MAX_FLASH_BANKS], s_last[CFG_MAX_FLASH_BANKS];
  370. int protected, i;
  371. int planned;
  372. int rcode;
  373. rcode = flash_fill_sect_ranges( addr_first, addr_last, s_first, s_last, &planned );
  374. protected = 0;
  375. if (planned && (rcode == 0)) {
  376. for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) {
  377. if (info->flash_id == FLASH_UNKNOWN) {
  378. continue;
  379. }
  380. if (s_first[bank]>=0 && s_first[bank]<=s_last[bank]) {
  381. debug ("Protecting sectors %d..%d in bank %ld\n",
  382. s_first[bank], s_last[bank], bank+1);
  383. protected += s_last[bank] - s_first[bank] + 1;
  384. for (i=s_first[bank]; i<=s_last[bank]; ++i) {
  385. #if defined(CFG_FLASH_PROTECTION)
  386. if (flash_real_protect(info, i, p))
  387. rcode = 1;
  388. putc ('.');
  389. #else
  390. info->protect[i] = p;
  391. #endif /* CFG_FLASH_PROTECTION */
  392. }
  393. }
  394. #if defined(CFG_FLASH_PROTECTION)
  395. if (!rcode) putc ('\n');
  396. #endif /* CFG_FLASH_PROTECTION */
  397. }
  398. printf ("%sProtected %d sectors\n",
  399. p ? "" : "Un-", protected);
  400. } else if (rcode == 0) {
  401. printf ("Error: start and/or end address"
  402. " not on sector boundary\n");
  403. rcode = 1;
  404. }
  405. return rcode;
  406. }
  407. /**************************************************/
  408. U_BOOT_CMD(
  409. flinfo, 2, 1, do_flinfo,
  410. "flinfo - print FLASH memory information\n",
  411. "\n - print information for all FLASH memory banks\n"
  412. "flinfo N\n - print information for FLASH memory bank # N\n"
  413. );
  414. U_BOOT_CMD(
  415. erase, 3, 1, do_flerase,
  416. "erase - erase FLASH memory\n",
  417. "start end\n"
  418. " - erase FLASH from addr 'start' to addr 'end'\n"
  419. "erase N:SF[-SL]\n - erase sectors SF-SL in FLASH bank # N\n"
  420. "erase bank N\n - erase FLASH bank # N\n"
  421. "erase all\n - erase all FLASH banks\n"
  422. );
  423. U_BOOT_CMD(
  424. protect, 4, 1, do_protect,
  425. "protect - enable or disable FLASH write protection\n",
  426. "on start end\n"
  427. " - protect FLASH from addr 'start' to addr 'end'\n"
  428. "protect on N:SF[-SL]\n"
  429. " - protect sectors SF-SL in FLASH bank # N\n"
  430. "protect on bank N\n - protect FLASH bank # N\n"
  431. "protect on all\n - protect all FLASH banks\n"
  432. "protect off start end\n"
  433. " - make FLASH from addr 'start' to addr 'end' writable\n"
  434. "protect off N:SF[-SL]\n"
  435. " - make sectors SF-SL writable in FLASH bank # N\n"
  436. "protect off bank N\n - make FLASH bank # N writable\n"
  437. "protect off all\n - make all FLASH banks writable\n"
  438. );
  439. #endif /* CFG_CMD_FLASH */