flash.c 19 KB

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