flash.c 19 KB

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