flash.c 19 KB

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