flash.c 19 KB

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