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