flash.c 17 KB

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