flash.c 18 KB

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