flash.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * (C) Copyright 2003
  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. #include <common.h>
  24. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  25. /* NOTE - CONFIG_FLASH_16BIT means the CPU interface is 16-bit, it
  26. * has nothing to do with the flash chip being 8-bit or 16-bit.
  27. */
  28. #ifdef CONFIG_FLASH_16BIT
  29. typedef unsigned short FLASH_PORT_WIDTH;
  30. typedef volatile unsigned short FLASH_PORT_WIDTHV;
  31. #define FLASH_ID_MASK 0xFFFF
  32. #else
  33. typedef unsigned long FLASH_PORT_WIDTH;
  34. typedef volatile unsigned long FLASH_PORT_WIDTHV;
  35. #define FLASH_ID_MASK 0xFFFFFFFF
  36. #endif
  37. #define FPW FLASH_PORT_WIDTH
  38. #define FPWV FLASH_PORT_WIDTHV
  39. #define ORMASK(size) ((-size) & OR_AM_MSK)
  40. #define FLASH_CYCLE1 0x0555
  41. #define FLASH_CYCLE2 0x02aa
  42. /*-----------------------------------------------------------------------
  43. * Functions
  44. */
  45. static ulong flash_get_size(FPWV *addr, flash_info_t *info);
  46. static void flash_reset(flash_info_t *info);
  47. static int write_word_intel(flash_info_t *info, FPWV *dest, FPW data);
  48. static int write_word_amd(flash_info_t *info, FPWV *dest, FPW data);
  49. static void flash_get_offsets(ulong base, flash_info_t *info);
  50. static flash_info_t *flash_get_info(ulong base);
  51. /*-----------------------------------------------------------------------
  52. * flash_init()
  53. *
  54. * sets up flash_info and returns size of FLASH (bytes)
  55. */
  56. unsigned long flash_init (void)
  57. {
  58. unsigned long size = 0;
  59. int i;
  60. /* Init: no FLASHes known */
  61. for (i=0; i < CFG_MAX_FLASH_BANKS; ++i) {
  62. #if 0
  63. ulong flashbase = (i == 0) ? PHYS_FLASH_1 : PHYS_FLASH_2;
  64. #else
  65. ulong flashbase = CFG_FLASH_BASE;
  66. #endif
  67. memset(&flash_info[i], 0, sizeof(flash_info_t));
  68. flash_info[i].size =
  69. flash_get_size((FPW *)flashbase, &flash_info[i]);
  70. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  71. printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx\n",
  72. i, flash_info[i].size);
  73. }
  74. size += flash_info[i].size;
  75. }
  76. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  77. /* monitor protection ON by default */
  78. flash_protect(FLAG_PROTECT_SET,
  79. CFG_MONITOR_BASE,
  80. CFG_MONITOR_BASE+CFG_MONITOR_LEN-1,
  81. flash_get_info(CFG_MONITOR_BASE));
  82. #endif
  83. #ifdef CFG_ENV_IS_IN_FLASH
  84. /* ENV protection ON by default */
  85. flash_protect(FLAG_PROTECT_SET,
  86. CFG_ENV_ADDR,
  87. CFG_ENV_ADDR+CFG_ENV_SIZE-1,
  88. flash_get_info(CFG_ENV_ADDR));
  89. #endif
  90. return size ? size : 1;
  91. }
  92. /*-----------------------------------------------------------------------
  93. */
  94. static void flash_reset(flash_info_t *info)
  95. {
  96. FPWV *base = (FPWV *)(info->start[0]);
  97. /* Put FLASH back in read mode */
  98. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
  99. *base = (FPW)0x00FF00FF; /* Intel Read Mode */
  100. else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
  101. *base = (FPW)0x00F000F0; /* AMD Read Mode */
  102. }
  103. /*-----------------------------------------------------------------------
  104. */
  105. static void flash_get_offsets (ulong base, flash_info_t *info)
  106. {
  107. int i;
  108. /* set up sector start address table */
  109. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL
  110. && (info->flash_id & FLASH_BTYPE)) {
  111. int bootsect_size; /* number of bytes/boot sector */
  112. int sect_size; /* number of bytes/regular sector */
  113. bootsect_size = 0x00002000 * (sizeof(FPW)/2);
  114. sect_size = 0x00010000 * (sizeof(FPW)/2);
  115. /* set sector offsets for bottom boot block type */
  116. for (i = 0; i < 8; ++i) {
  117. info->start[i] = base + (i * bootsect_size);
  118. }
  119. for (i = 8; i < info->sector_count; i++) {
  120. info->start[i] = base + ((i - 7) * sect_size);
  121. }
  122. }
  123. else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD
  124. && (info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U) {
  125. int sect_size; /* number of bytes/sector */
  126. sect_size = 0x00010000 * (sizeof(FPW)/2);
  127. /* set up sector start address table (uniform sector type) */
  128. for( i = 0; i < info->sector_count; i++ )
  129. info->start[i] = base + (i * sect_size);
  130. }
  131. }
  132. /*-----------------------------------------------------------------------
  133. */
  134. static flash_info_t *flash_get_info(ulong base)
  135. {
  136. int i;
  137. flash_info_t * info;
  138. for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
  139. info = & flash_info[i];
  140. if (info->start[0] <= base && base < info->start[0] + info->size)
  141. break;
  142. }
  143. return i == CFG_MAX_FLASH_BANKS ? 0 : info;
  144. }
  145. /*-----------------------------------------------------------------------
  146. */
  147. void flash_print_info (flash_info_t *info)
  148. {
  149. int i;
  150. uchar *boottype;
  151. uchar *bootletter;
  152. uchar *fmt;
  153. uchar botbootletter[] = "B";
  154. uchar topbootletter[] = "T";
  155. uchar botboottype[] = "bottom boot sector";
  156. uchar topboottype[] = "top boot sector";
  157. if (info->flash_id == FLASH_UNKNOWN) {
  158. printf ("missing or unknown FLASH type\n");
  159. return;
  160. }
  161. switch (info->flash_id & FLASH_VENDMASK) {
  162. case FLASH_MAN_AMD: printf ("AMD "); break;
  163. case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
  164. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  165. case FLASH_MAN_SST: printf ("SST "); break;
  166. case FLASH_MAN_STM: printf ("STM "); break;
  167. case FLASH_MAN_INTEL: printf ("INTEL "); break;
  168. default: printf ("Unknown Vendor "); break;
  169. }
  170. /* check for top or bottom boot, if it applies */
  171. if (info->flash_id & FLASH_BTYPE) {
  172. boottype = botboottype;
  173. bootletter = botbootletter;
  174. }
  175. else {
  176. boottype = topboottype;
  177. bootletter = topbootletter;
  178. }
  179. switch (info->flash_id & FLASH_TYPEMASK) {
  180. case FLASH_AM640U:
  181. fmt = "29LV641D (64 Mbit, uniform sectors)\n";
  182. break;
  183. case FLASH_28F800C3B:
  184. case FLASH_28F800C3T:
  185. fmt = "28F800C3%s (8 Mbit, %s)\n";
  186. break;
  187. case FLASH_INTEL800B:
  188. case FLASH_INTEL800T:
  189. fmt = "28F800B3%s (8 Mbit, %s)\n";
  190. break;
  191. case FLASH_28F160C3B:
  192. case FLASH_28F160C3T:
  193. fmt = "28F160C3%s (16 Mbit, %s)\n";
  194. break;
  195. case FLASH_INTEL160B:
  196. case FLASH_INTEL160T:
  197. fmt = "28F160B3%s (16 Mbit, %s)\n";
  198. break;
  199. case FLASH_28F320C3B:
  200. case FLASH_28F320C3T:
  201. fmt = "28F320C3%s (32 Mbit, %s)\n";
  202. break;
  203. case FLASH_INTEL320B:
  204. case FLASH_INTEL320T:
  205. fmt = "28F320B3%s (32 Mbit, %s)\n";
  206. break;
  207. case FLASH_28F640C3B:
  208. case FLASH_28F640C3T:
  209. fmt = "28F640C3%s (64 Mbit, %s)\n";
  210. break;
  211. case FLASH_INTEL640B:
  212. case FLASH_INTEL640T:
  213. fmt = "28F640B3%s (64 Mbit, %s)\n";
  214. break;
  215. default:
  216. fmt = "Unknown Chip Type\n";
  217. break;
  218. }
  219. printf (fmt, bootletter, boottype);
  220. printf (" Size: %ld MB in %d Sectors\n",
  221. info->size >> 20,
  222. info->sector_count);
  223. printf (" Sector Start Addresses:");
  224. for (i=0; i<info->sector_count; ++i) {
  225. if ((i % 5) == 0) {
  226. printf ("\n ");
  227. }
  228. printf (" %08lX%s", info->start[i],
  229. info->protect[i] ? " (RO)" : " ");
  230. }
  231. printf ("\n");
  232. }
  233. /*-----------------------------------------------------------------------
  234. */
  235. /*
  236. * The following code cannot be run from FLASH!
  237. */
  238. ulong flash_get_size (FPWV *addr, flash_info_t *info)
  239. {
  240. /* Write auto select command: read Manufacturer ID */
  241. /* Write auto select command sequence and test FLASH answer */
  242. addr[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* for AMD, Intel ignores this */
  243. addr[FLASH_CYCLE2] = (FPW)0x00550055; /* for AMD, Intel ignores this */
  244. addr[FLASH_CYCLE1] = (FPW)0x00900090; /* selects Intel or AMD */
  245. /* The manufacturer codes are only 1 byte, so just use 1 byte.
  246. * This works for any bus width and any FLASH device width.
  247. */
  248. udelay(1000000);//psl
  249. //psl switch (addr[1] & 0xff) {
  250. switch (addr[0] & 0xff) {//psl
  251. case (uchar)AMD_MANUFACT:
  252. info->flash_id = FLASH_MAN_AMD;
  253. break;
  254. case (uchar)INTEL_MANUFACT:
  255. info->flash_id = FLASH_MAN_INTEL;
  256. break;
  257. default:
  258. info->flash_id = FLASH_UNKNOWN;
  259. info->sector_count = 0;
  260. info->size = 0;
  261. break;
  262. }
  263. /* Check 16 bits or 32 bits of ID so work on 32 or 16 bit bus. */
  264. //psl if (info->flash_id != FLASH_UNKNOWN) switch (addr[0]) {
  265. if (info->flash_id != FLASH_UNKNOWN) switch (addr[1]) {
  266. case (FPW)AMD_ID_LV640U: /* 29LV640 and 29LV641 have same ID */
  267. info->flash_id += FLASH_AM640U;
  268. info->sector_count = 128;
  269. info->size = 0x00800000 * (sizeof(FPW)/2);
  270. break; /* => 8 or 16 MB */
  271. case (FPW)INTEL_ID_28F800C3B:
  272. info->flash_id += FLASH_28F800C3B;
  273. info->sector_count = 23;
  274. info->size = 0x00100000 * (sizeof(FPW)/2);
  275. break; /* => 1 or 2 MB */
  276. case (FPW)INTEL_ID_28F800B3B:
  277. info->flash_id += FLASH_INTEL800B;
  278. info->sector_count = 23;
  279. info->size = 0x00100000 * (sizeof(FPW)/2);
  280. break; /* => 1 or 2 MB */
  281. case (FPW)INTEL_ID_28F160C3B:
  282. info->flash_id += FLASH_28F160C3B;
  283. info->sector_count = 39;
  284. info->size = 0x00200000 * (sizeof(FPW)/2);
  285. break; /* => 2 or 4 MB */
  286. case (FPW)INTEL_ID_28F160B3B:
  287. info->flash_id += FLASH_INTEL160B;
  288. info->sector_count = 39;
  289. info->size = 0x00200000 * (sizeof(FPW)/2);
  290. break; /* => 2 or 4 MB */
  291. case (FPW)INTEL_ID_28F320C3B:
  292. info->flash_id += FLASH_28F320C3B;
  293. info->sector_count = 71;
  294. info->size = 0x00400000 * (sizeof(FPW)/2);
  295. break; /* => 4 or 8 MB */
  296. case (FPW)INTEL_ID_28F320B3B:
  297. info->flash_id += FLASH_INTEL320B;
  298. info->sector_count = 71;
  299. info->size = 0x00400000 * (sizeof(FPW)/2);
  300. break; /* => 4 or 8 MB */
  301. case (FPW)INTEL_ID_28F640C3B:
  302. info->flash_id += FLASH_28F640C3B;
  303. info->sector_count = 135;
  304. info->size = 0x00800000 * (sizeof(FPW)/2);
  305. break; /* => 8 or 16 MB */
  306. case (FPW)INTEL_ID_28F640B3B:
  307. info->flash_id += FLASH_INTEL640B;
  308. info->sector_count = 135;
  309. info->size = 0x00800000 * (sizeof(FPW)/2);
  310. break; /* => 8 or 16 MB */
  311. default:
  312. info->flash_id = FLASH_UNKNOWN;
  313. info->sector_count = 0;
  314. info->size = 0;
  315. return (0); /* => no or unknown flash */
  316. }
  317. flash_get_offsets((ulong)addr, info);
  318. /* Put FLASH back in read mode */
  319. flash_reset(info);
  320. return (info->size);
  321. }
  322. /*-----------------------------------------------------------------------
  323. */
  324. int flash_erase (flash_info_t *info, int s_first, int s_last)
  325. {
  326. FPWV *addr;
  327. int flag, prot, sect;
  328. int intel = (info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL;
  329. ulong start, now, last;
  330. int rcode = 0;
  331. if ((s_first < 0) || (s_first > s_last)) {
  332. if (info->flash_id == FLASH_UNKNOWN) {
  333. printf ("- missing\n");
  334. } else {
  335. printf ("- no sectors to erase\n");
  336. }
  337. return 1;
  338. }
  339. switch (info->flash_id & FLASH_TYPEMASK) {
  340. case FLASH_INTEL800B:
  341. case FLASH_INTEL160B:
  342. case FLASH_INTEL320B:
  343. case FLASH_INTEL640B:
  344. case FLASH_28F800C3B:
  345. case FLASH_28F160C3B:
  346. case FLASH_28F320C3B:
  347. case FLASH_28F640C3B:
  348. case FLASH_AM640U:
  349. break;
  350. case FLASH_UNKNOWN:
  351. default:
  352. printf ("Can't erase unknown flash type %08lx - aborted\n",
  353. info->flash_id);
  354. return 1;
  355. }
  356. prot = 0;
  357. for (sect=s_first; sect<=s_last; ++sect) {
  358. if (info->protect[sect]) {
  359. prot++;
  360. }
  361. }
  362. if (prot) {
  363. printf ("- Warning: %d protected sectors will not be erased!\n",
  364. prot);
  365. } else {
  366. printf ("\n");
  367. }
  368. last = get_timer(0);
  369. /* Start erase on unprotected sectors */
  370. for (sect = s_first; sect<=s_last && rcode == 0; sect++) {
  371. if (info->protect[sect] != 0) /* protected, skip it */
  372. continue;
  373. /* Disable interrupts which might cause a timeout here */
  374. flag = disable_interrupts();
  375. addr = (FPWV *)(info->start[sect]);
  376. if (intel) {
  377. *addr = (FPW)0x00500050; /* clear status register */
  378. *addr = (FPW)0x00200020; /* erase setup */
  379. *addr = (FPW)0x00D000D0; /* erase confirm */
  380. }
  381. else {
  382. /* must be AMD style if not Intel */
  383. FPWV *base; /* first address in bank */
  384. base = (FPWV *)(info->start[0]);
  385. base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
  386. base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
  387. base[FLASH_CYCLE1] = (FPW)0x00800080; /* erase mode */
  388. base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
  389. base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
  390. *addr = (FPW)0x00300030; /* erase sector */
  391. }
  392. /* re-enable interrupts if necessary */
  393. if (flag)
  394. enable_interrupts();
  395. start = get_timer(0);
  396. /* wait at least 50us for AMD, 80us for Intel.
  397. * Let's wait 1 ms.
  398. */
  399. udelay (1000);
  400. while ((*addr & (FPW)0x00800080) != (FPW)0x00800080) {
  401. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  402. printf ("Timeout\n");
  403. if (intel) {
  404. /* suspend erase */
  405. *addr = (FPW)0x00B000B0;
  406. }
  407. flash_reset(info); /* reset to read mode */
  408. rcode = 1; /* failed */
  409. break;
  410. }
  411. /* show that we're waiting */
  412. if ((get_timer(last)) > CFG_HZ) {/* every second */
  413. putc ('.');
  414. last = get_timer(0);
  415. }
  416. }
  417. /* show that we're waiting */
  418. if ((get_timer(last)) > CFG_HZ) { /* every second */
  419. putc ('.');
  420. last = get_timer(0);
  421. }
  422. flash_reset(info); /* reset to read mode */
  423. }
  424. printf (" done\n");
  425. return rcode;
  426. }
  427. /*-----------------------------------------------------------------------
  428. * Copy memory to flash, returns:
  429. * 0 - OK
  430. * 1 - write timeout
  431. * 2 - Flash not erased
  432. */
  433. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  434. {
  435. FPW data = 0; /* 16 or 32 bit word, matches flash bus width on MPC8XX */
  436. int bytes; /* number of bytes to program in current word */
  437. int left; /* number of bytes left to program */
  438. int i, res;
  439. for (left = cnt, res = 0;
  440. left > 0 && res == 0;
  441. addr += sizeof(data), left -= sizeof(data) - bytes) {
  442. bytes = addr & (sizeof(data) - 1);
  443. addr &= ~(sizeof(data) - 1);
  444. /* combine source and destination data so can program
  445. * an entire word of 16 or 32 bits
  446. */
  447. for (i = 0; i < sizeof(data); i++) {
  448. data <<= 8;
  449. if (i < bytes || i - bytes >= left )
  450. data += *((uchar *)addr + i);
  451. else
  452. data += *src++;
  453. }
  454. /* write one word to the flash */
  455. switch (info->flash_id & FLASH_VENDMASK) {
  456. case FLASH_MAN_AMD:
  457. res = write_word_amd(info, (FPWV *)addr, data);
  458. break;
  459. case FLASH_MAN_INTEL:
  460. res = write_word_intel(info, (FPWV *)addr, data);
  461. break;
  462. default:
  463. /* unknown flash type, error! */
  464. printf ("missing or unknown FLASH type\n");
  465. res = 1; /* not really a timeout, but gives error */
  466. break;
  467. }
  468. }
  469. return (res);
  470. }
  471. /*-----------------------------------------------------------------------
  472. * Write a word to Flash for AMD FLASH
  473. * A word is 16 or 32 bits, whichever the bus width of the flash bank
  474. * (not an individual chip) is.
  475. *
  476. * returns:
  477. * 0 - OK
  478. * 1 - write timeout
  479. * 2 - Flash not erased
  480. */
  481. static int write_word_amd (flash_info_t *info, FPWV *dest, FPW data)
  482. {
  483. ulong start;
  484. int flag;
  485. int res = 0; /* result, assume success */
  486. FPWV *base; /* first address in flash bank */
  487. /* Check if Flash is (sufficiently) erased */
  488. if ((*dest & data) != data) {
  489. return (2);
  490. }
  491. base = (FPWV *)(info->start[0]);
  492. /* Disable interrupts which might cause a timeout here */
  493. flag = disable_interrupts();
  494. base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
  495. base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
  496. base[FLASH_CYCLE1] = (FPW)0x00A000A0; /* selects program mode */
  497. *dest = data; /* start programming the data */
  498. /* re-enable interrupts if necessary */
  499. if (flag)
  500. enable_interrupts();
  501. start = get_timer (0);
  502. /* data polling for D7 */
  503. while (res == 0 && (*dest & (FPW)0x00800080) != (data & (FPW)0x00800080)) {
  504. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  505. *dest = (FPW)0x00F000F0; /* reset bank */
  506. res = 1;
  507. }
  508. }
  509. return (res);
  510. }
  511. /*-----------------------------------------------------------------------
  512. * Write a word to Flash for Intel FLASH
  513. * A word is 16 or 32 bits, whichever the bus width of the flash bank
  514. * (not an individual chip) is.
  515. *
  516. * returns:
  517. * 0 - OK
  518. * 1 - write timeout
  519. * 2 - Flash not erased
  520. */
  521. static int write_word_intel (flash_info_t *info, FPWV *dest, FPW data)
  522. {
  523. ulong start;
  524. int flag;
  525. int res = 0; /* result, assume success */
  526. /* Check if Flash is (sufficiently) erased */
  527. if ((*dest & data) != data) {
  528. return (2);
  529. }
  530. /* Disable interrupts which might cause a timeout here */
  531. flag = disable_interrupts();
  532. *dest = (FPW)0x00500050; /* clear status register */
  533. *dest = (FPW)0x00FF00FF; /* make sure in read mode */
  534. *dest = (FPW)0x00400040; /* program setup */
  535. *dest = data; /* start programming the data */
  536. /* re-enable interrupts if necessary */
  537. if (flag)
  538. enable_interrupts();
  539. start = get_timer (0);
  540. while (res == 0 && (*dest & (FPW)0x00800080) != (FPW)0x00800080) {
  541. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  542. *dest = (FPW)0x00B000B0; /* Suspend program */
  543. res = 1;
  544. }
  545. }
  546. if (res == 0 && (*dest & (FPW)0x00100010))
  547. res = 1; /* write failed, time out error is close enough */
  548. *dest = (FPW)0x00500050; /* clear status register */
  549. *dest = (FPW)0x00FF00FF; /* make sure in read mode */
  550. return (res);
  551. }