flash.c 19 KB

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