flash.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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[CFG_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 CFG_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. CFG_FLASH_WORD_SIZE value;
  203. ulong base = (ulong)addr;
  204. volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)addr;
  205. /* Write auto select command: read Manufacturer ID */
  206. addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
  207. addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
  208. addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00900090;
  209. value = addr2[CFG_FLASH_READ0];
  210. switch (value) {
  211. case (CFG_FLASH_WORD_SIZE)AMD_MANUFACT:
  212. info->flash_id = FLASH_MAN_AMD;
  213. break;
  214. case (CFG_FLASH_WORD_SIZE)FUJ_MANUFACT:
  215. info->flash_id = FLASH_MAN_FUJ;
  216. break;
  217. case (CFG_FLASH_WORD_SIZE)SST_MANUFACT:
  218. info->flash_id = FLASH_MAN_SST;
  219. break;
  220. case (CFG_FLASH_WORD_SIZE)STM_MANUFACT:
  221. info->flash_id = FLASH_MAN_STM;
  222. break;
  223. default:
  224. info->flash_id = FLASH_UNKNOWN;
  225. info->sector_count = 0;
  226. info->size = 0;
  227. return (0); /* no or unknown flash */
  228. }
  229. value = addr2[CFG_FLASH_READ1]; /* device ID */
  230. switch (value) {
  231. case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400T:
  232. info->flash_id += FLASH_AM400T;
  233. info->sector_count = 11;
  234. info->size = 0x00080000;
  235. break; /* => 0.5 MB */
  236. case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400B:
  237. info->flash_id += FLASH_AM400B;
  238. info->sector_count = 11;
  239. info->size = 0x00080000;
  240. break; /* => 0.5 MB */
  241. case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800T:
  242. info->flash_id += FLASH_AM800T;
  243. info->sector_count = 19;
  244. info->size = 0x00100000;
  245. break; /* => 1 MB */
  246. case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800B:
  247. info->flash_id += FLASH_AM800B;
  248. info->sector_count = 19;
  249. info->size = 0x00100000;
  250. break; /* => 1 MB */
  251. case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160T:
  252. info->flash_id += FLASH_AM160T;
  253. info->sector_count = 35;
  254. info->size = 0x00200000;
  255. break; /* => 2 MB */
  256. case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160B:
  257. info->flash_id += FLASH_AM160B;
  258. info->sector_count = 35;
  259. info->size = 0x00200000;
  260. break; /* => 2 MB */
  261. case (CFG_FLASH_WORD_SIZE)STM_ID_29W320DT:
  262. info->flash_id += FLASH_STMW320DT;
  263. info->sector_count = 67;
  264. info->size = 0x00400000; break; /* => 4 MB */
  265. case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320T:
  266. info->flash_id += FLASH_AM320T;
  267. info->sector_count = 71;
  268. info->size = 0x00400000; break; /* => 4 MB */
  269. case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320B:
  270. info->flash_id += FLASH_AM320B;
  271. info->sector_count = 71;
  272. info->size = 0x00400000; break; /* => 4 MB */
  273. case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322T:
  274. info->flash_id += FLASH_AMDL322T;
  275. info->sector_count = 71;
  276. info->size = 0x00400000; break; /* => 4 MB */
  277. case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322B:
  278. info->flash_id += FLASH_AMDL322B;
  279. info->sector_count = 71;
  280. info->size = 0x00400000; break; /* => 4 MB */
  281. case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323T:
  282. info->flash_id += FLASH_AMDL323T;
  283. info->sector_count = 71;
  284. info->size = 0x00400000; break; /* => 4 MB */
  285. case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323B:
  286. info->flash_id += FLASH_AMDL323B;
  287. info->sector_count = 71;
  288. info->size = 0x00400000; break; /* => 4 MB */
  289. case (CFG_FLASH_WORD_SIZE)AMD_ID_LV640U:
  290. info->flash_id += FLASH_AM640U;
  291. info->sector_count = 128;
  292. info->size = 0x00800000; break; /* => 8 MB */
  293. case (CFG_FLASH_WORD_SIZE)SST_ID_xF800A:
  294. info->flash_id += FLASH_SST800A;
  295. info->sector_count = 16;
  296. info->size = 0x00100000;
  297. break; /* => 1 MB */
  298. case (CFG_FLASH_WORD_SIZE)SST_ID_xF160A:
  299. info->flash_id += FLASH_SST160A;
  300. info->sector_count = 32;
  301. info->size = 0x00200000;
  302. break; /* => 2 MB */
  303. default:
  304. info->flash_id = FLASH_UNKNOWN;
  305. return (0); /* => no or unknown flash */
  306. }
  307. /* set up sector start address table */
  308. if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
  309. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
  310. for (i = 0; i < info->sector_count; i++)
  311. info->start[i] = base + (i * 0x00010000);
  312. } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
  313. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
  314. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
  315. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
  316. /* set sector offsets for bottom boot block type */
  317. for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
  318. info->start[i] = base;
  319. base += 8 << 10;
  320. }
  321. while (i < info->sector_count) { /* 64k regular sectors */
  322. info->start[i] = base;
  323. base += 64 << 10;
  324. ++i;
  325. }
  326. } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
  327. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
  328. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
  329. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
  330. /* set sector offsets for top boot block type */
  331. base += info->size;
  332. i = info->sector_count;
  333. for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
  334. base -= 8 << 10;
  335. --i;
  336. info->start[i] = base;
  337. }
  338. while (i > 0) { /* 64k regular sectors */
  339. base -= 64 << 10;
  340. --i;
  341. info->start[i] = base;
  342. }
  343. } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) {
  344. /* set sector offsets for top boot block type */
  345. base += info->size;
  346. i = info->sector_count;
  347. /* 1 x 16k boot sector */
  348. base -= 16 << 10;
  349. --i;
  350. info->start[i] = base;
  351. /* 2 x 8k boot sectors */
  352. for (n=0; n<2; ++n) {
  353. base -= 8 << 10;
  354. --i;
  355. info->start[i] = base;
  356. }
  357. /* 1 x 32k boot sector */
  358. base -= 32 << 10;
  359. --i;
  360. info->start[i] = base;
  361. while (i > 0) { /* 64k regular sectors */
  362. base -= 64 << 10;
  363. --i;
  364. info->start[i] = base;
  365. }
  366. } else {
  367. if (info->flash_id & FLASH_BTYPE) {
  368. /* set sector offsets for bottom boot block type */
  369. info->start[0] = base + 0x00000000;
  370. info->start[1] = base + 0x00004000;
  371. info->start[2] = base + 0x00006000;
  372. info->start[3] = base + 0x00008000;
  373. for (i = 4; i < info->sector_count; i++) {
  374. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  375. }
  376. } else {
  377. /* set sector offsets for top boot block type */
  378. i = info->sector_count - 1;
  379. info->start[i--] = base + info->size - 0x00004000;
  380. info->start[i--] = base + info->size - 0x00006000;
  381. info->start[i--] = base + info->size - 0x00008000;
  382. for (; i >= 0; i--) {
  383. info->start[i] = base + i * 0x00010000;
  384. }
  385. }
  386. }
  387. /* check for protected sectors */
  388. for (i = 0; i < info->sector_count; i++) {
  389. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  390. /* D0 = 1 if protected */
  391. addr2 = (volatile CFG_FLASH_WORD_SIZE *)(info->start[i]);
  392. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
  393. info->protect[i] = 0;
  394. else
  395. info->protect[i] = addr2[CFG_FLASH_READ2] & 1;
  396. }
  397. /*
  398. * Prevent writes to uninitialized FLASH.
  399. */
  400. if (info->flash_id != FLASH_UNKNOWN) {
  401. addr2 = (CFG_FLASH_WORD_SIZE *)info->start[0];
  402. *addr2 = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  403. }
  404. return (info->size);
  405. }
  406. /*-----------------------------------------------------------------------
  407. */
  408. int flash_erase (flash_info_t *info, int s_first, int s_last)
  409. {
  410. volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
  411. volatile CFG_FLASH_WORD_SIZE *addr2;
  412. int flag, prot, sect, l_sect;
  413. ulong start, now, last;
  414. int i;
  415. if ((s_first < 0) || (s_first > s_last)) {
  416. if (info->flash_id == FLASH_UNKNOWN) {
  417. printf ("- missing\n");
  418. } else {
  419. printf ("- no sectors to erase\n");
  420. }
  421. return 1;
  422. }
  423. if (info->flash_id == FLASH_UNKNOWN) {
  424. printf ("Can't erase unknown flash type - aborted\n");
  425. return 1;
  426. }
  427. prot = 0;
  428. for (sect=s_first; sect<=s_last; ++sect) {
  429. if (info->protect[sect]) {
  430. prot++;
  431. }
  432. }
  433. if (prot) {
  434. printf ("- Warning: %d protected sectors will not be erased!\n",
  435. prot);
  436. } else {
  437. printf ("\n");
  438. }
  439. l_sect = -1;
  440. /* Disable interrupts which might cause a timeout here */
  441. flag = disable_interrupts();
  442. /* Start erase on unprotected sectors */
  443. for (sect = s_first; sect<=s_last; sect++) {
  444. if (info->protect[sect] == 0) { /* not protected */
  445. addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[sect]);
  446. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  447. addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
  448. addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
  449. addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
  450. addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
  451. addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
  452. addr2[0] = (CFG_FLASH_WORD_SIZE)0x00500050; /* block erase */
  453. for (i=0; i<50; i++)
  454. udelay(1000); /* wait 1 ms */
  455. } else {
  456. if (sect == s_first) {
  457. addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
  458. addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
  459. addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
  460. addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
  461. addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
  462. }
  463. addr2[0] = (CFG_FLASH_WORD_SIZE)0x00300030; /* sector erase */
  464. }
  465. l_sect = sect;
  466. }
  467. }
  468. /* re-enable interrupts if necessary */
  469. if (flag)
  470. enable_interrupts();
  471. /* wait at least 80us - let's wait 1 ms */
  472. udelay (1000);
  473. /*
  474. * We wait for the last triggered sector
  475. */
  476. if (l_sect < 0)
  477. goto DONE;
  478. start = get_timer (0);
  479. last = start;
  480. addr = (CFG_FLASH_WORD_SIZE *)(info->start[l_sect]);
  481. while ((addr[0] & (CFG_FLASH_WORD_SIZE)0x00800080) != (CFG_FLASH_WORD_SIZE)0x00800080) {
  482. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  483. printf ("Timeout\n");
  484. return 1;
  485. }
  486. /* show that we're waiting */
  487. if ((now - last) > 1000) { /* every second */
  488. putc ('.');
  489. last = now;
  490. }
  491. }
  492. DONE:
  493. /* reset to read mode */
  494. addr = (CFG_FLASH_WORD_SIZE *)info->start[0];
  495. addr[0] = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  496. printf (" done\n");
  497. return 0;
  498. }
  499. /*-----------------------------------------------------------------------
  500. * Copy memory to flash, returns:
  501. * 0 - OK
  502. * 1 - write timeout
  503. * 2 - Flash not erased
  504. */
  505. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  506. {
  507. ulong cp, wp, data;
  508. int i, l, rc;
  509. wp = (addr & ~3); /* get lower word aligned address */
  510. /*
  511. * handle unaligned start bytes
  512. */
  513. if ((l = addr - wp) != 0) {
  514. data = 0;
  515. for (i=0, cp=wp; i<l; ++i, ++cp) {
  516. #ifdef CONFIG_B2
  517. data = data | ((*(uchar *)cp)<<(8*i));
  518. #else
  519. data = (data << 8) | (*(uchar *)cp);
  520. #endif
  521. }
  522. for (; i<4 && cnt>0; ++i) {
  523. #ifdef CONFIG_B2
  524. data = data | ((*src++)<<(8*i));
  525. #else
  526. data = (data << 8) | *src++;
  527. #endif
  528. --cnt;
  529. ++cp;
  530. }
  531. for (; cnt==0 && i<4; ++i, ++cp) {
  532. #ifdef CONFIG_B2
  533. data = data | ((*(uchar *)cp)<<(8*i));
  534. #else
  535. data = (data << 8) | (*(uchar *)cp);
  536. #endif
  537. }
  538. if ((rc = write_word(info, wp, data)) != 0) {
  539. return (rc);
  540. }
  541. wp += 4;
  542. }
  543. /*
  544. * handle word aligned part
  545. */
  546. while (cnt >= 4) {
  547. data = 0;
  548. #ifdef CONFIG_B2
  549. data = (*(ulong*)src);
  550. src += 4;
  551. #else
  552. for (i=0; i<4; ++i) {
  553. data = (data << 8) | *src++;
  554. }
  555. #endif
  556. if ((rc = write_word(info, wp, data)) != 0) {
  557. return (rc);
  558. }
  559. wp += 4;
  560. cnt -= 4;
  561. }
  562. if (cnt == 0) {
  563. return (0);
  564. }
  565. /*
  566. * handle unaligned tail bytes
  567. */
  568. data = 0;
  569. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  570. #ifdef CONFIG_B2
  571. data = data | ((*src++)<<(8*i));
  572. #else
  573. data = (data << 8) | *src++;
  574. #endif
  575. --cnt;
  576. }
  577. for (; i<4; ++i, ++cp) {
  578. #ifdef CONFIG_B2
  579. data = data | ((*(uchar *)cp)<<(8*i));
  580. #else
  581. data = (data << 8) | (*(uchar *)cp);
  582. #endif
  583. }
  584. return (write_word(info, wp, data));
  585. }
  586. /*-----------------------------------------------------------------------
  587. * Write a word to Flash, returns:
  588. * 0 - OK
  589. * 1 - write timeout
  590. * 2 - Flash not erased
  591. */
  592. static int write_word (flash_info_t *info, ulong dest, ulong data)
  593. {
  594. volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
  595. volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *)dest;
  596. volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *)&data;
  597. ulong start;
  598. int flag;
  599. int i;
  600. /* Check if Flash is (sufficiently) erased */
  601. if ((*((volatile ulong *)dest) & data) != data) {
  602. return (2);
  603. }
  604. /* Disable interrupts which might cause a timeout here */
  605. flag = disable_interrupts();
  606. for (i=0; i<4/sizeof(CFG_FLASH_WORD_SIZE); i++)
  607. {
  608. addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
  609. addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
  610. addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00A000A0;
  611. dest2[i] = data2[i];
  612. /* re-enable interrupts if necessary */
  613. if (flag)
  614. enable_interrupts();
  615. /* data polling for D7 */
  616. start = get_timer (0);
  617. while ((dest2[i] & (CFG_FLASH_WORD_SIZE)0x00800080) !=
  618. (data2[i] & (CFG_FLASH_WORD_SIZE)0x00800080)) {
  619. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  620. return (1);
  621. }
  622. }
  623. }
  624. return (0);
  625. }
  626. /*-----------------------------------------------------------------------
  627. */