flash.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  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. /*
  24. * Modified 4/5/2001
  25. * Wait for completion of each sector erase command issued
  26. * 4/5/2001
  27. * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
  28. */
  29. #include <common.h>
  30. #include <ppc4xx.h>
  31. #include <asm/processor.h>
  32. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  33. /*-----------------------------------------------------------------------
  34. * Functions
  35. */
  36. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  37. static int write_word (flash_info_t *info, ulong dest, ulong data);
  38. static void flash_get_offsets (ulong base, flash_info_t *info);
  39. #ifdef CONFIG_ADCIOP
  40. #define ADDR0 0x0aa9
  41. #define ADDR1 0x0556
  42. #define FLASH_WORD_SIZE unsigned char
  43. #endif
  44. #ifdef CONFIG_CPCI405
  45. #define ADDR0 0x5555
  46. #define ADDR1 0x2aaa
  47. #define FLASH_WORD_SIZE unsigned short
  48. #endif
  49. #ifdef CONFIG_WALNUT405
  50. #define ADDR0 0x5555
  51. #define ADDR1 0x2aaa
  52. #define FLASH_WORD_SIZE unsigned char
  53. #endif
  54. #ifdef CONFIG_BUBINGA405EP
  55. #define ADDR0 0x5555
  56. #define ADDR1 0x2aaa
  57. #define FLASH_WORD_SIZE unsigned char
  58. #endif
  59. /*-----------------------------------------------------------------------
  60. */
  61. unsigned long flash_init (void)
  62. {
  63. unsigned long size_b0, size_b1;
  64. int i;
  65. uint pbcr;
  66. unsigned long base_b0, base_b1;
  67. /* Init: no FLASHes known */
  68. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  69. flash_info[i].flash_id = FLASH_UNKNOWN;
  70. }
  71. /* Static FLASH Bank configuration here - FIXME XXX */
  72. size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
  73. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  74. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  75. size_b0, size_b0<<20);
  76. }
  77. /* Only one bank */
  78. if (CFG_MAX_FLASH_BANKS == 1)
  79. {
  80. /* Setup offsets */
  81. flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]);
  82. /* Monitor protection ON by default */
  83. (void)flash_protect(FLAG_PROTECT_SET,
  84. FLASH_BASE0_PRELIM,
  85. FLASH_BASE0_PRELIM+CFG_MONITOR_LEN-1,
  86. &flash_info[0]);
  87. size_b1 = 0 ;
  88. flash_info[0].size = size_b0;
  89. }
  90. /* 2 banks */
  91. else
  92. {
  93. size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
  94. /* Re-do sizing to get full correct info */
  95. if (size_b1)
  96. {
  97. mtdcr(ebccfga, pb0cr);
  98. pbcr = mfdcr(ebccfgd);
  99. mtdcr(ebccfga, pb0cr);
  100. base_b1 = -size_b1;
  101. pbcr = (pbcr & 0x0001ffff) | base_b1 | (((size_b1/1024/1024)-1)<<17);
  102. mtdcr(ebccfgd, pbcr);
  103. /* printf("pb1cr = %x\n", pbcr); */
  104. }
  105. if (size_b0)
  106. {
  107. mtdcr(ebccfga, pb1cr);
  108. pbcr = mfdcr(ebccfgd);
  109. mtdcr(ebccfga, pb1cr);
  110. base_b0 = base_b1 - size_b0;
  111. pbcr = (pbcr & 0x0001ffff) | base_b0 | (((size_b0/1024/1024)-1)<<17);
  112. mtdcr(ebccfgd, pbcr);
  113. /* printf("pb0cr = %x\n", pbcr); */
  114. }
  115. size_b0 = flash_get_size((vu_long *)base_b0, &flash_info[0]);
  116. flash_get_offsets (base_b0, &flash_info[0]);
  117. /* monitor protection ON by default */
  118. (void)flash_protect(FLAG_PROTECT_SET,
  119. base_b0+size_b0-CFG_MONITOR_LEN,
  120. base_b0+size_b0-1,
  121. &flash_info[0]);
  122. if (size_b1) {
  123. /* Re-do sizing to get full correct info */
  124. size_b1 = flash_get_size((vu_long *)base_b1, &flash_info[1]);
  125. flash_get_offsets (base_b1, &flash_info[1]);
  126. /* monitor protection ON by default */
  127. (void)flash_protect(FLAG_PROTECT_SET,
  128. base_b1+size_b1-CFG_MONITOR_LEN,
  129. base_b1+size_b1-1,
  130. &flash_info[1]);
  131. /* monitor protection OFF by default (one is enough) */
  132. (void)flash_protect(FLAG_PROTECT_CLEAR,
  133. base_b0+size_b0-CFG_MONITOR_LEN,
  134. base_b0+size_b0-1,
  135. &flash_info[0]);
  136. } else {
  137. flash_info[1].flash_id = FLASH_UNKNOWN;
  138. flash_info[1].sector_count = -1;
  139. }
  140. flash_info[0].size = size_b0;
  141. flash_info[1].size = size_b1;
  142. }/* else 2 banks */
  143. return (size_b0 + size_b1);
  144. }
  145. /*-----------------------------------------------------------------------
  146. */
  147. static void flash_get_offsets (ulong base, flash_info_t *info)
  148. {
  149. int i;
  150. /* set up sector start address table */
  151. if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
  152. (info->flash_id == FLASH_AM040)){
  153. for (i = 0; i < info->sector_count; i++)
  154. info->start[i] = base + (i * 0x00010000);
  155. } else {
  156. if (info->flash_id & FLASH_BTYPE) {
  157. /* set sector offsets for bottom boot block type */
  158. info->start[0] = base + 0x00000000;
  159. info->start[1] = base + 0x00004000;
  160. info->start[2] = base + 0x00006000;
  161. info->start[3] = base + 0x00008000;
  162. for (i = 4; i < info->sector_count; i++) {
  163. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  164. }
  165. } else {
  166. /* set sector offsets for top boot block type */
  167. i = info->sector_count - 1;
  168. info->start[i--] = base + info->size - 0x00004000;
  169. info->start[i--] = base + info->size - 0x00006000;
  170. info->start[i--] = base + info->size - 0x00008000;
  171. for (; i >= 0; i--) {
  172. info->start[i] = base + i * 0x00010000;
  173. }
  174. }
  175. }
  176. }
  177. /*-----------------------------------------------------------------------
  178. */
  179. void flash_print_info (flash_info_t *info)
  180. {
  181. int i;
  182. int k;
  183. int size;
  184. int erased;
  185. volatile unsigned long *flash;
  186. if (info->flash_id == FLASH_UNKNOWN) {
  187. printf ("missing or unknown FLASH type\n");
  188. return;
  189. }
  190. switch (info->flash_id & FLASH_VENDMASK) {
  191. case FLASH_MAN_AMD: printf ("AMD "); break;
  192. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  193. case FLASH_MAN_SST: printf ("SST "); break;
  194. default: printf ("Unknown Vendor "); break;
  195. }
  196. switch (info->flash_id & FLASH_TYPEMASK) {
  197. case FLASH_AM040: printf ("AM29F040 (512 Kbit, uniform sector size)\n");
  198. break;
  199. case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  200. break;
  201. case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  202. break;
  203. case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  204. break;
  205. case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  206. break;
  207. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  208. break;
  209. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  210. break;
  211. case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  212. break;
  213. case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  214. break;
  215. case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
  216. break;
  217. case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
  218. break;
  219. default: printf ("Unknown Chip Type\n");
  220. break;
  221. }
  222. printf (" Size: %ld KB in %d Sectors\n",
  223. info->size >> 10, info->sector_count);
  224. printf (" Sector Start Addresses:");
  225. for (i=0; i<info->sector_count; ++i) {
  226. /*
  227. * Check if whole sector is erased
  228. */
  229. if (i != (info->sector_count-1))
  230. size = info->start[i+1] - info->start[i];
  231. else
  232. size = info->start[0] + info->size - info->start[i];
  233. erased = 1;
  234. flash = (volatile unsigned long *)info->start[i];
  235. size = size >> 2; /* divide by 4 for longword access */
  236. for (k=0; k<size; k++)
  237. {
  238. if (*flash++ != 0xffffffff)
  239. {
  240. erased = 0;
  241. break;
  242. }
  243. }
  244. if ((i % 5) == 0)
  245. printf ("\n ");
  246. #if 0 /* test-only */
  247. printf (" %08lX%s",
  248. info->start[i],
  249. info->protect[i] ? " (RO)" : " "
  250. #else
  251. printf (" %08lX%s%s",
  252. info->start[i],
  253. erased ? " E" : " ",
  254. info->protect[i] ? "RO " : " "
  255. #endif
  256. );
  257. }
  258. printf ("\n");
  259. return;
  260. }
  261. /*-----------------------------------------------------------------------
  262. */
  263. /*-----------------------------------------------------------------------
  264. */
  265. /*
  266. * The following code cannot be run from FLASH!
  267. */
  268. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  269. {
  270. short i;
  271. FLASH_WORD_SIZE value;
  272. ulong base = (ulong)addr;
  273. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
  274. /* Write auto select command: read Manufacturer ID */
  275. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  276. addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  277. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
  278. #ifdef CONFIG_ADCIOP
  279. value = addr2[2];
  280. #else
  281. value = addr2[0];
  282. #endif
  283. switch (value) {
  284. case (FLASH_WORD_SIZE)AMD_MANUFACT:
  285. info->flash_id = FLASH_MAN_AMD;
  286. break;
  287. case (FLASH_WORD_SIZE)FUJ_MANUFACT:
  288. info->flash_id = FLASH_MAN_FUJ;
  289. break;
  290. case (FLASH_WORD_SIZE)SST_MANUFACT:
  291. info->flash_id = FLASH_MAN_SST;
  292. break;
  293. default:
  294. info->flash_id = FLASH_UNKNOWN;
  295. info->sector_count = 0;
  296. info->size = 0;
  297. return (0); /* no or unknown flash */
  298. }
  299. #ifdef CONFIG_ADCIOP
  300. value = addr2[0]; /* device ID */
  301. /* printf("\ndev_code=%x\n", value); */
  302. #else
  303. value = addr2[1]; /* device ID */
  304. #endif
  305. switch (value) {
  306. case (FLASH_WORD_SIZE)AMD_ID_F040B:
  307. info->flash_id += FLASH_AM040;
  308. info->sector_count = 8;
  309. info->size = 0x0080000; /* => 512 ko */
  310. break;
  311. case (FLASH_WORD_SIZE)AMD_ID_LV400T:
  312. info->flash_id += FLASH_AM400T;
  313. info->sector_count = 11;
  314. info->size = 0x00080000;
  315. break; /* => 0.5 MB */
  316. case (FLASH_WORD_SIZE)AMD_ID_LV400B:
  317. info->flash_id += FLASH_AM400B;
  318. info->sector_count = 11;
  319. info->size = 0x00080000;
  320. break; /* => 0.5 MB */
  321. case (FLASH_WORD_SIZE)AMD_ID_LV800T:
  322. info->flash_id += FLASH_AM800T;
  323. info->sector_count = 19;
  324. info->size = 0x00100000;
  325. break; /* => 1 MB */
  326. case (FLASH_WORD_SIZE)AMD_ID_LV800B:
  327. info->flash_id += FLASH_AM800B;
  328. info->sector_count = 19;
  329. info->size = 0x00100000;
  330. break; /* => 1 MB */
  331. case (FLASH_WORD_SIZE)AMD_ID_LV160T:
  332. info->flash_id += FLASH_AM160T;
  333. info->sector_count = 35;
  334. info->size = 0x00200000;
  335. break; /* => 2 MB */
  336. case (FLASH_WORD_SIZE)AMD_ID_LV160B:
  337. info->flash_id += FLASH_AM160B;
  338. info->sector_count = 35;
  339. info->size = 0x00200000;
  340. break; /* => 2 MB */
  341. #if 0 /* enable when device IDs are available */
  342. case (FLASH_WORD_SIZE)AMD_ID_LV320T:
  343. info->flash_id += FLASH_AM320T;
  344. info->sector_count = 67;
  345. info->size = 0x00400000;
  346. break; /* => 4 MB */
  347. case (FLASH_WORD_SIZE)AMD_ID_LV320B:
  348. info->flash_id += FLASH_AM320B;
  349. info->sector_count = 67;
  350. info->size = 0x00400000;
  351. break; /* => 4 MB */
  352. #endif
  353. case (FLASH_WORD_SIZE)SST_ID_xF800A:
  354. info->flash_id += FLASH_SST800A;
  355. info->sector_count = 16;
  356. info->size = 0x00100000;
  357. break; /* => 1 MB */
  358. case (FLASH_WORD_SIZE)SST_ID_xF160A:
  359. info->flash_id += FLASH_SST160A;
  360. info->sector_count = 32;
  361. info->size = 0x00200000;
  362. break; /* => 2 MB */
  363. default:
  364. info->flash_id = FLASH_UNKNOWN;
  365. return (0); /* => no or unknown flash */
  366. }
  367. /* set up sector start address table */
  368. if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
  369. (info->flash_id == FLASH_AM040)){
  370. for (i = 0; i < info->sector_count; i++)
  371. info->start[i] = base + (i * 0x00010000);
  372. } else {
  373. if (info->flash_id & FLASH_BTYPE) {
  374. /* set sector offsets for bottom boot block type */
  375. info->start[0] = base + 0x00000000;
  376. info->start[1] = base + 0x00004000;
  377. info->start[2] = base + 0x00006000;
  378. info->start[3] = base + 0x00008000;
  379. for (i = 4; i < info->sector_count; i++) {
  380. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  381. }
  382. } else {
  383. /* set sector offsets for top boot block type */
  384. i = info->sector_count - 1;
  385. info->start[i--] = base + info->size - 0x00004000;
  386. info->start[i--] = base + info->size - 0x00006000;
  387. info->start[i--] = base + info->size - 0x00008000;
  388. for (; i >= 0; i--) {
  389. info->start[i] = base + i * 0x00010000;
  390. }
  391. }
  392. }
  393. /* check for protected sectors */
  394. for (i = 0; i < info->sector_count; i++) {
  395. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  396. /* D0 = 1 if protected */
  397. #ifdef CONFIG_ADCIOP
  398. addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
  399. info->protect[i] = addr2[4] & 1;
  400. #else
  401. addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
  402. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
  403. info->protect[i] = 0;
  404. else
  405. info->protect[i] = addr2[2] & 1;
  406. #endif
  407. }
  408. /*
  409. * Prevent writes to uninitialized FLASH.
  410. */
  411. if (info->flash_id != FLASH_UNKNOWN) {
  412. #if 0 /* test-only */
  413. #ifdef CONFIG_ADCIOP
  414. addr2 = (volatile unsigned char *)info->start[0];
  415. addr2[ADDR0] = 0xAA;
  416. addr2[ADDR1] = 0x55;
  417. addr2[ADDR0] = 0xF0; /* reset bank */
  418. #else
  419. addr2 = (FLASH_WORD_SIZE *)info->start[0];
  420. *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  421. #endif
  422. #else /* test-only */
  423. addr2 = (FLASH_WORD_SIZE *)info->start[0];
  424. *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  425. #endif /* test-only */
  426. }
  427. return (info->size);
  428. }
  429. int wait_for_DQ7(flash_info_t *info, int sect)
  430. {
  431. ulong start, now, last;
  432. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
  433. start = get_timer (0);
  434. last = start;
  435. while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
  436. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  437. printf ("Timeout\n");
  438. return -1;
  439. }
  440. /* show that we're waiting */
  441. if ((now - last) > 1000) { /* every second */
  442. putc ('.');
  443. last = now;
  444. }
  445. }
  446. return 0;
  447. }
  448. /*-----------------------------------------------------------------------
  449. */
  450. int flash_erase (flash_info_t *info, int s_first, int s_last)
  451. {
  452. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
  453. volatile FLASH_WORD_SIZE *addr2;
  454. int flag, prot, sect, l_sect;
  455. int i;
  456. if ((s_first < 0) || (s_first > s_last)) {
  457. if (info->flash_id == FLASH_UNKNOWN) {
  458. printf ("- missing\n");
  459. } else {
  460. printf ("- no sectors to erase\n");
  461. }
  462. return 1;
  463. }
  464. if (info->flash_id == FLASH_UNKNOWN) {
  465. printf ("Can't erase unknown flash type - aborted\n");
  466. return 1;
  467. }
  468. prot = 0;
  469. for (sect=s_first; sect<=s_last; ++sect) {
  470. if (info->protect[sect]) {
  471. prot++;
  472. }
  473. }
  474. if (prot) {
  475. printf ("- Warning: %d protected sectors will not be erased!\n",
  476. prot);
  477. } else {
  478. printf ("\n");
  479. }
  480. l_sect = -1;
  481. /* Disable interrupts which might cause a timeout here */
  482. flag = disable_interrupts();
  483. /* Start erase on unprotected sectors */
  484. for (sect = s_first; sect<=s_last; sect++) {
  485. if (info->protect[sect] == 0) { /* not protected */
  486. addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
  487. printf("Erasing sector %p\n", addr2); /* CLH */
  488. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  489. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  490. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  491. addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
  492. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  493. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  494. addr2[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
  495. for (i=0; i<50; i++)
  496. udelay(1000); /* wait 1 ms */
  497. } else {
  498. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  499. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  500. addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
  501. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  502. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  503. addr2[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
  504. }
  505. l_sect = sect;
  506. /*
  507. * Wait for each sector to complete, it's more
  508. * reliable. According to AMD Spec, you must
  509. * issue all erase commands within a specified
  510. * timeout. This has been seen to fail, especially
  511. * if printf()s are included (for debug)!!
  512. */
  513. wait_for_DQ7(info, sect);
  514. }
  515. }
  516. /* re-enable interrupts if necessary */
  517. if (flag)
  518. enable_interrupts();
  519. /* wait at least 80us - let's wait 1 ms */
  520. udelay (1000);
  521. #if 0
  522. /*
  523. * We wait for the last triggered sector
  524. */
  525. if (l_sect < 0)
  526. goto DONE;
  527. wait_for_DQ7(info, l_sect);
  528. DONE:
  529. #endif
  530. /* reset to read mode */
  531. addr = (FLASH_WORD_SIZE *)info->start[0];
  532. addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  533. printf (" done\n");
  534. return 0;
  535. }
  536. /*-----------------------------------------------------------------------
  537. * Copy memory to flash, returns:
  538. * 0 - OK
  539. * 1 - write timeout
  540. * 2 - Flash not erased
  541. */
  542. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  543. {
  544. ulong cp, wp, data;
  545. int i, l, rc;
  546. wp = (addr & ~3); /* get lower word aligned address */
  547. /*
  548. * handle unaligned start bytes
  549. */
  550. if ((l = addr - wp) != 0) {
  551. data = 0;
  552. for (i=0, cp=wp; i<l; ++i, ++cp) {
  553. data = (data << 8) | (*(uchar *)cp);
  554. }
  555. for (; i<4 && cnt>0; ++i) {
  556. data = (data << 8) | *src++;
  557. --cnt;
  558. ++cp;
  559. }
  560. for (; cnt==0 && i<4; ++i, ++cp) {
  561. data = (data << 8) | (*(uchar *)cp);
  562. }
  563. if ((rc = write_word(info, wp, data)) != 0) {
  564. return (rc);
  565. }
  566. wp += 4;
  567. }
  568. /*
  569. * handle word aligned part
  570. */
  571. while (cnt >= 4) {
  572. data = 0;
  573. for (i=0; i<4; ++i) {
  574. data = (data << 8) | *src++;
  575. }
  576. if ((rc = write_word(info, wp, data)) != 0) {
  577. return (rc);
  578. }
  579. wp += 4;
  580. cnt -= 4;
  581. }
  582. if (cnt == 0) {
  583. return (0);
  584. }
  585. /*
  586. * handle unaligned tail bytes
  587. */
  588. data = 0;
  589. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  590. data = (data << 8) | *src++;
  591. --cnt;
  592. }
  593. for (; i<4; ++i, ++cp) {
  594. data = (data << 8) | (*(uchar *)cp);
  595. }
  596. return (write_word(info, wp, data));
  597. }
  598. /*-----------------------------------------------------------------------
  599. * Write a word to Flash, returns:
  600. * 0 - OK
  601. * 1 - write timeout
  602. * 2 - Flash not erased
  603. */
  604. static int write_word (flash_info_t * info, ulong dest, ulong data)
  605. {
  606. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) (info->start[0]);
  607. volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
  608. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
  609. ulong start;
  610. int i;
  611. /* Check if Flash is (sufficiently) erased */
  612. if ((*((volatile FLASH_WORD_SIZE *) dest) &
  613. (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
  614. return (2);
  615. }
  616. for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
  617. int flag;
  618. /* Disable interrupts which might cause a timeout here */
  619. flag = disable_interrupts ();
  620. addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
  621. addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
  622. addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00A000A0;
  623. dest2[i] = data2[i];
  624. /* re-enable interrupts if necessary */
  625. if (flag)
  626. enable_interrupts ();
  627. /* data polling for D7 */
  628. start = get_timer (0);
  629. while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
  630. (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
  631. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  632. return (1);
  633. }
  634. }
  635. }
  636. return (0);
  637. }
  638. /*-----------------------------------------------------------------------
  639. */