flash.c 20 KB

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