flash.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * (C) Copyright 2001
  3. * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
  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 <asm/processor.h>
  25. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  26. /*-----------------------------------------------------------------------
  27. * Functions
  28. */
  29. static int write_word (flash_info_t *info, ulong dest, ulong data);
  30. /*-----------------------------------------------------------------------
  31. */
  32. static void flash_get_offsets (ulong base, flash_info_t *info)
  33. {
  34. int i;
  35. short n;
  36. /* set up sector start address table */
  37. if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
  38. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
  39. for (i = 0; i < info->sector_count; i++)
  40. info->start[i] = base + (i * 0x00010000);
  41. } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
  42. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
  43. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
  44. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
  45. /* set sector offsets for bottom boot block type */
  46. for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
  47. info->start[i] = base;
  48. base += 8 << 10;
  49. }
  50. while (i < info->sector_count) { /* 64k regular sectors */
  51. info->start[i] = base;
  52. base += 64 << 10;
  53. ++i;
  54. }
  55. } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
  56. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
  57. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
  58. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
  59. /* set sector offsets for top boot block type */
  60. base += info->size;
  61. i = info->sector_count;
  62. for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
  63. base -= 8 << 10;
  64. --i;
  65. info->start[i] = base;
  66. }
  67. while (i > 0) { /* 64k regular sectors */
  68. base -= 64 << 10;
  69. --i;
  70. info->start[i] = base;
  71. }
  72. } else {
  73. if (info->flash_id & FLASH_BTYPE) {
  74. /* set sector offsets for bottom boot block type */
  75. info->start[0] = base + 0x00000000;
  76. info->start[1] = base + 0x00004000;
  77. info->start[2] = base + 0x00006000;
  78. info->start[3] = base + 0x00008000;
  79. for (i = 4; i < info->sector_count; i++) {
  80. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  81. }
  82. } else {
  83. /* set sector offsets for top boot block type */
  84. i = info->sector_count - 1;
  85. info->start[i--] = base + info->size - 0x00004000;
  86. info->start[i--] = base + info->size - 0x00006000;
  87. info->start[i--] = base + info->size - 0x00008000;
  88. for (; i >= 0; i--) {
  89. info->start[i] = base + i * 0x00010000;
  90. }
  91. }
  92. }
  93. }
  94. /*-----------------------------------------------------------------------
  95. */
  96. void flash_print_info (flash_info_t *info)
  97. {
  98. int i;
  99. int k;
  100. int size;
  101. int erased;
  102. volatile unsigned long *flash;
  103. if (info->flash_id == FLASH_UNKNOWN) {
  104. printf ("missing or unknown FLASH type\n");
  105. return;
  106. }
  107. switch (info->flash_id & FLASH_VENDMASK) {
  108. case FLASH_MAN_AMD: printf ("AMD "); break;
  109. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  110. case FLASH_MAN_SST: printf ("SST "); break;
  111. case FLASH_MAN_STM: printf ("ST "); break;
  112. default: printf ("Unknown Vendor "); break;
  113. }
  114. switch (info->flash_id & FLASH_TYPEMASK) {
  115. case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  116. break;
  117. case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  118. break;
  119. case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  120. break;
  121. case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  122. break;
  123. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  124. break;
  125. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  126. break;
  127. case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)\n");
  128. break;
  129. case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)\n");
  130. break;
  131. case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)\n");
  132. break;
  133. case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)\n");
  134. break;
  135. case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)\n");
  136. break;
  137. case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)\n");
  138. break;
  139. case FLASH_AM640U: printf ("AM29LV640D (64 M, uniform sector)\n");
  140. break;
  141. case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
  142. break;
  143. case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
  144. break;
  145. case FLASH_STMW320DT: printf ("M29W320DT (32 M, top sector)\n");
  146. break;
  147. default: printf ("Unknown Chip Type\n");
  148. break;
  149. }
  150. printf (" Size: %ld MB in %d Sectors\n",
  151. info->size >> 20, info->sector_count);
  152. printf (" Sector Start Addresses:");
  153. for (i=0; i<info->sector_count; ++i) {
  154. #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
  155. /*
  156. * Check if whole sector is erased
  157. */
  158. if (i != (info->sector_count-1))
  159. size = info->start[i+1] - info->start[i];
  160. else
  161. size = info->start[0] + info->size - info->start[i];
  162. erased = 1;
  163. flash = (volatile unsigned long *)info->start[i];
  164. size = size >> 2; /* divide by 4 for longword access */
  165. for (k=0; k<size; k++)
  166. {
  167. if (*flash++ != 0xffffffff)
  168. {
  169. erased = 0;
  170. break;
  171. }
  172. }
  173. if ((i % 5) == 0)
  174. printf ("\n ");
  175. /* print empty and read-only info */
  176. printf (" %08lX%s%s",
  177. info->start[i],
  178. erased ? " E" : " ",
  179. info->protect[i] ? "RO " : " ");
  180. #else
  181. if ((i % 5) == 0)
  182. printf ("\n ");
  183. printf (" %08lX%s",
  184. info->start[i],
  185. info->protect[i] ? " (RO)" : " ");
  186. #endif
  187. }
  188. printf ("\n");
  189. return;
  190. }
  191. /*-----------------------------------------------------------------------
  192. */
  193. /*-----------------------------------------------------------------------
  194. */
  195. /*
  196. * The following code cannot be run from FLASH!
  197. */
  198. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  199. {
  200. short i;
  201. short n;
  202. CONFIG_SYS_FLASH_WORD_SIZE value;
  203. ulong base = (ulong)addr;
  204. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)addr;
  205. debug("[%s, %d] Entering ...\n", __FUNCTION__, __LINE__);
  206. /* Write auto select command: read Manufacturer ID */
  207. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
  208. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
  209. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00900090;
  210. value = addr2[CONFIG_SYS_FLASH_READ0];
  211. switch (value) {
  212. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_MANUFACT:
  213. info->flash_id = FLASH_MAN_AMD;
  214. break;
  215. case (CONFIG_SYS_FLASH_WORD_SIZE)FUJ_MANUFACT:
  216. info->flash_id = FLASH_MAN_FUJ;
  217. break;
  218. case (CONFIG_SYS_FLASH_WORD_SIZE)SST_MANUFACT:
  219. info->flash_id = FLASH_MAN_SST;
  220. break;
  221. case (CONFIG_SYS_FLASH_WORD_SIZE)STM_MANUFACT:
  222. info->flash_id = FLASH_MAN_STM;
  223. break;
  224. default:
  225. info->flash_id = FLASH_UNKNOWN;
  226. info->sector_count = 0;
  227. info->size = 0;
  228. return (0); /* no or unknown flash */
  229. }
  230. value = addr2[CONFIG_SYS_FLASH_READ1]; /* device ID */
  231. switch (value) {
  232. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400T:
  233. info->flash_id += FLASH_AM400T;
  234. info->sector_count = 11;
  235. info->size = 0x00080000;
  236. break; /* => 0.5 MB */
  237. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400B:
  238. info->flash_id += FLASH_AM400B;
  239. info->sector_count = 11;
  240. info->size = 0x00080000;
  241. break; /* => 0.5 MB */
  242. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800T:
  243. info->flash_id += FLASH_AM800T;
  244. info->sector_count = 19;
  245. info->size = 0x00100000;
  246. break; /* => 1 MB */
  247. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800B:
  248. info->flash_id += FLASH_AM800B;
  249. info->sector_count = 19;
  250. info->size = 0x00100000;
  251. break; /* => 1 MB */
  252. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160T:
  253. info->flash_id += FLASH_AM160T;
  254. info->sector_count = 35;
  255. info->size = 0x00200000;
  256. break; /* => 2 MB */
  257. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160B:
  258. info->flash_id += FLASH_AM160B;
  259. info->sector_count = 35;
  260. info->size = 0x00200000;
  261. break; /* => 2 MB */
  262. case (CONFIG_SYS_FLASH_WORD_SIZE)STM_ID_29W320DT:
  263. info->flash_id += FLASH_STMW320DT;
  264. info->sector_count = 67;
  265. info->size = 0x00400000; break; /* => 4 MB */
  266. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320T:
  267. info->flash_id += FLASH_AM320T;
  268. info->sector_count = 71;
  269. info->size = 0x00400000; break; /* => 4 MB */
  270. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320B:
  271. info->flash_id += FLASH_AM320B;
  272. info->sector_count = 71;
  273. info->size = 0x00400000; break; /* => 4 MB */
  274. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322T:
  275. info->flash_id += FLASH_AMDL322T;
  276. info->sector_count = 71;
  277. info->size = 0x00400000; break; /* => 4 MB */
  278. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322B:
  279. info->flash_id += FLASH_AMDL322B;
  280. info->sector_count = 71;
  281. info->size = 0x00400000; break; /* => 4 MB */
  282. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323T:
  283. info->flash_id += FLASH_AMDL323T;
  284. info->sector_count = 71;
  285. info->size = 0x00400000; break; /* => 4 MB */
  286. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323B:
  287. info->flash_id += FLASH_AMDL323B;
  288. info->sector_count = 71;
  289. info->size = 0x00400000; break; /* => 4 MB */
  290. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV640U:
  291. info->flash_id += FLASH_AM640U;
  292. info->sector_count = 128;
  293. info->size = 0x00800000; break; /* => 8 MB */
  294. case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF800A:
  295. info->flash_id += FLASH_SST800A;
  296. info->sector_count = 16;
  297. info->size = 0x00100000;
  298. break; /* => 1 MB */
  299. case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF160A:
  300. info->flash_id += FLASH_SST160A;
  301. info->sector_count = 32;
  302. info->size = 0x00200000;
  303. break; /* => 2 MB */
  304. default:
  305. info->flash_id = FLASH_UNKNOWN;
  306. return (0); /* => no or unknown flash */
  307. }
  308. /* set up sector start address table */
  309. if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
  310. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
  311. for (i = 0; i < info->sector_count; i++)
  312. info->start[i] = base + (i * 0x00010000);
  313. } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
  314. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
  315. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
  316. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
  317. /* set sector offsets for bottom boot block type */
  318. for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
  319. info->start[i] = base;
  320. base += 8 << 10;
  321. }
  322. while (i < info->sector_count) { /* 64k regular sectors */
  323. info->start[i] = base;
  324. base += 64 << 10;
  325. ++i;
  326. }
  327. } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
  328. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
  329. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
  330. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
  331. /* set sector offsets for top boot block type */
  332. base += info->size;
  333. i = info->sector_count;
  334. for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
  335. base -= 8 << 10;
  336. --i;
  337. info->start[i] = base;
  338. }
  339. while (i > 0) { /* 64k regular sectors */
  340. base -= 64 << 10;
  341. --i;
  342. info->start[i] = base;
  343. }
  344. } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) {
  345. /* set sector offsets for top boot block type */
  346. base += info->size;
  347. i = info->sector_count;
  348. /* 1 x 16k boot sector */
  349. base -= 16 << 10;
  350. --i;
  351. info->start[i] = base;
  352. /* 2 x 8k boot sectors */
  353. for (n=0; n<2; ++n) {
  354. base -= 8 << 10;
  355. --i;
  356. info->start[i] = base;
  357. }
  358. /* 1 x 32k boot sector */
  359. base -= 32 << 10;
  360. --i;
  361. info->start[i] = base;
  362. while (i > 0) { /* 64k regular sectors */
  363. base -= 64 << 10;
  364. --i;
  365. info->start[i] = base;
  366. }
  367. } else {
  368. if (info->flash_id & FLASH_BTYPE) {
  369. /* set sector offsets for bottom boot block type */
  370. info->start[0] = base + 0x00000000;
  371. info->start[1] = base + 0x00004000;
  372. info->start[2] = base + 0x00006000;
  373. info->start[3] = base + 0x00008000;
  374. for (i = 4; i < info->sector_count; i++) {
  375. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  376. }
  377. } else {
  378. /* set sector offsets for top boot block type */
  379. i = info->sector_count - 1;
  380. info->start[i--] = base + info->size - 0x00004000;
  381. info->start[i--] = base + info->size - 0x00006000;
  382. info->start[i--] = base + info->size - 0x00008000;
  383. for (; i >= 0; i--) {
  384. info->start[i] = base + i * 0x00010000;
  385. }
  386. }
  387. }
  388. /* check for protected sectors */
  389. for (i = 0; i < info->sector_count; i++) {
  390. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  391. /* D0 = 1 if protected */
  392. addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
  393. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
  394. info->protect[i] = 0;
  395. else
  396. info->protect[i] = addr2[CONFIG_SYS_FLASH_READ2] & 1;
  397. }
  398. /*
  399. * Prevent writes to uninitialized FLASH.
  400. */
  401. if (info->flash_id != FLASH_UNKNOWN) {
  402. addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
  403. *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  404. }
  405. return (info->size);
  406. }
  407. /*-----------------------------------------------------------------------
  408. */
  409. int flash_erase (flash_info_t *info, int s_first, int s_last)
  410. {
  411. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
  412. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
  413. int flag, prot, sect, l_sect;
  414. ulong start, now, last;
  415. int i;
  416. if ((s_first < 0) || (s_first > s_last)) {
  417. if (info->flash_id == FLASH_UNKNOWN) {
  418. printf ("- missing\n");
  419. } else {
  420. printf ("- no sectors to erase\n");
  421. }
  422. return 1;
  423. }
  424. if (info->flash_id == FLASH_UNKNOWN) {
  425. printf ("Can't erase unknown flash type - aborted\n");
  426. return 1;
  427. }
  428. prot = 0;
  429. for (sect=s_first; sect<=s_last; ++sect) {
  430. if (info->protect[sect]) {
  431. prot++;
  432. }
  433. }
  434. if (prot) {
  435. printf ("- Warning: %d protected sectors will not be erased!\n",
  436. prot);
  437. } else {
  438. printf ("\n");
  439. }
  440. l_sect = -1;
  441. /* Disable interrupts which might cause a timeout here */
  442. flag = disable_interrupts();
  443. /* Start erase on unprotected sectors */
  444. for (sect = s_first; sect<=s_last; sect++) {
  445. if (info->protect[sect] == 0) { /* not protected */
  446. addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[sect]);
  447. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  448. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
  449. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
  450. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
  451. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
  452. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
  453. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00500050; /* block erase */
  454. for (i=0; i<50; i++)
  455. udelay(1000); /* wait 1 ms */
  456. } else {
  457. if (sect == s_first) {
  458. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
  459. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
  460. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
  461. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
  462. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
  463. }
  464. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00300030; /* sector erase */
  465. }
  466. l_sect = sect;
  467. }
  468. }
  469. /* re-enable interrupts if necessary */
  470. if (flag)
  471. enable_interrupts();
  472. /* wait at least 80us - let's wait 1 ms */
  473. udelay (1000);
  474. /*
  475. * We wait for the last triggered sector
  476. */
  477. if (l_sect < 0)
  478. goto DONE;
  479. start = get_timer (0);
  480. last = start;
  481. addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[l_sect]);
  482. while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) != (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) {
  483. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  484. printf ("Timeout\n");
  485. return 1;
  486. }
  487. /* show that we're waiting */
  488. if ((now - last) > 1000) { /* every second */
  489. putc ('.');
  490. last = now;
  491. }
  492. }
  493. DONE:
  494. /* reset to read mode */
  495. addr = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
  496. addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  497. printf (" done\n");
  498. return 0;
  499. }
  500. /*-----------------------------------------------------------------------
  501. * Copy memory to flash, returns:
  502. * 0 - OK
  503. * 1 - write timeout
  504. * 2 - Flash not erased
  505. */
  506. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  507. {
  508. ulong cp, wp, data;
  509. int i, l, rc;
  510. wp = (addr & ~3); /* get lower word aligned address */
  511. /*
  512. * handle unaligned start bytes
  513. */
  514. if ((l = addr - wp) != 0) {
  515. data = 0;
  516. for (i=0, cp=wp; i<l; ++i, ++cp) {
  517. #ifdef CONFIG_B2
  518. data = data | ((*(uchar *)cp)<<(8*i));
  519. #else
  520. data = (data << 8) | (*(uchar *)cp);
  521. #endif
  522. }
  523. for (; i<4 && cnt>0; ++i) {
  524. #ifdef CONFIG_B2
  525. data = data | ((*src++)<<(8*i));
  526. #else
  527. data = (data << 8) | *src++;
  528. #endif
  529. --cnt;
  530. ++cp;
  531. }
  532. for (; cnt==0 && i<4; ++i, ++cp) {
  533. #ifdef CONFIG_B2
  534. data = data | ((*(uchar *)cp)<<(8*i));
  535. #else
  536. data = (data << 8) | (*(uchar *)cp);
  537. #endif
  538. }
  539. if ((rc = write_word(info, wp, data)) != 0) {
  540. return (rc);
  541. }
  542. wp += 4;
  543. }
  544. /*
  545. * handle word aligned part
  546. */
  547. while (cnt >= 4) {
  548. data = 0;
  549. #ifdef CONFIG_B2
  550. data = (*(ulong*)src);
  551. src += 4;
  552. #else
  553. for (i=0; i<4; ++i) {
  554. data = (data << 8) | *src++;
  555. }
  556. #endif
  557. if ((rc = write_word(info, wp, data)) != 0) {
  558. return (rc);
  559. }
  560. wp += 4;
  561. cnt -= 4;
  562. }
  563. if (cnt == 0) {
  564. return (0);
  565. }
  566. /*
  567. * handle unaligned tail bytes
  568. */
  569. data = 0;
  570. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  571. #ifdef CONFIG_B2
  572. data = data | ((*src++)<<(8*i));
  573. #else
  574. data = (data << 8) | *src++;
  575. #endif
  576. --cnt;
  577. }
  578. for (; i<4; ++i, ++cp) {
  579. #ifdef CONFIG_B2
  580. data = data | ((*(uchar *)cp)<<(8*i));
  581. #else
  582. data = (data << 8) | (*(uchar *)cp);
  583. #endif
  584. }
  585. return (write_word(info, wp, data));
  586. }
  587. /*-----------------------------------------------------------------------
  588. * Write a word to Flash, returns:
  589. * 0 - OK
  590. * 1 - write timeout
  591. * 2 - Flash not erased
  592. */
  593. static int write_word (flash_info_t *info, ulong dest, ulong data)
  594. {
  595. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
  596. volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
  597. volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)&data;
  598. ulong start;
  599. int flag;
  600. int i;
  601. /* Check if Flash is (sufficiently) erased */
  602. if ((*((volatile ulong *)dest) & data) != data) {
  603. return (2);
  604. }
  605. /* Disable interrupts which might cause a timeout here */
  606. flag = disable_interrupts();
  607. for (i=0; i<4/sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++)
  608. {
  609. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
  610. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
  611. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00A000A0;
  612. dest2[i] = data2[i];
  613. /* re-enable interrupts if necessary */
  614. if (flag)
  615. enable_interrupts();
  616. /* data polling for D7 */
  617. start = get_timer (0);
  618. while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) !=
  619. (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080)) {
  620. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  621. return (1);
  622. }
  623. }
  624. }
  625. return (0);
  626. }
  627. /*-----------------------------------------------------------------------
  628. */