flash.c 20 KB

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