flash.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. #include <common.h>
  24. #include <mpc8xx.h>
  25. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  26. /*-----------------------------------------------------------------------
  27. * Functions
  28. */
  29. static ulong flash_get_size(vu_long *addr, flash_info_t *info);
  30. static int write_word(flash_info_t *info, ulong dest, ulong data);
  31. static void flash_get_offsets(ulong base, flash_info_t *info);
  32. /*-----------------------------------------------------------------------
  33. */
  34. unsigned long flash_init(void)
  35. {
  36. volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
  37. volatile memctl8xx_t *memctl = &immap->im_memctl;
  38. unsigned long size_b0;
  39. int i;
  40. /* Init: no FLASHes known */
  41. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
  42. flash_info[i].flash_id = FLASH_UNKNOWN;
  43. /* Static FLASH Bank configuration here - FIXME XXX */
  44. size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
  45. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  46. printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  47. size_b0, size_b0<<20);
  48. }
  49. /* Remap FLASH according to real size */
  50. memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
  51. #ifdef CONFIG_FLASH_16BIT
  52. memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
  53. BR_MS_GPCM | BR_V | BR_PS_16; /* 16 Bit data port */
  54. #else
  55. memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
  56. BR_MS_GPCM | BR_V;
  57. #endif
  58. /* Re-do sizing to get full correct info */
  59. size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE,
  60. &flash_info[0]);
  61. flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
  62. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
  63. /* monitor protection ON by default */
  64. flash_protect(FLAG_PROTECT_SET,
  65. CONFIG_SYS_MONITOR_BASE,
  66. CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
  67. &flash_info[0]);
  68. #endif
  69. memctl->memc_br1 = 0; /* invalidate bank 1 */
  70. flash_info[0].size = size_b0;
  71. return size_b0;
  72. }
  73. /*-----------------------------------------------------------------------
  74. */
  75. static void flash_get_offsets(ulong base, flash_info_t *info)
  76. {
  77. int i;
  78. if (info->flash_id == FLASH_UNKNOWN)
  79. return;
  80. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  81. for (i = 0; i < info->sector_count; i++)
  82. info->start[i] = base + (i * 0x00002000);
  83. return;
  84. }
  85. /* set up sector start address table */
  86. if (info->flash_id & FLASH_BTYPE) {
  87. /* set sector offsets for bottom boot block type */
  88. #ifdef CONFIG_FLASH_16BIT
  89. info->start[0] = base + 0x00000000;
  90. info->start[1] = base + 0x00004000;
  91. info->start[2] = base + 0x00006000;
  92. info->start[3] = base + 0x00008000;
  93. for (i = 4; i < info->sector_count; i++)
  94. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  95. #else
  96. info->start[0] = base + 0x00000000;
  97. info->start[1] = base + 0x00008000;
  98. info->start[2] = base + 0x0000C000;
  99. info->start[3] = base + 0x00010000;
  100. for (i = 4; i < info->sector_count; i++)
  101. info->start[i] = base + (i * 0x00020000) - 0x00060000;
  102. #endif
  103. } else {
  104. /* set sector offsets for top boot block type */
  105. i = info->sector_count - 1;
  106. info->start[i--] = base + info->size - 0x00008000;
  107. info->start[i--] = base + info->size - 0x0000C000;
  108. info->start[i--] = base + info->size - 0x00010000;
  109. for (; i >= 0; i--)
  110. info->start[i] = base + i * 0x00020000;
  111. }
  112. }
  113. /*-----------------------------------------------------------------------
  114. */
  115. void flash_print_info(flash_info_t *info)
  116. {
  117. int i;
  118. if (info->flash_id == FLASH_UNKNOWN) {
  119. printf("missing or unknown FLASH type\n");
  120. return;
  121. }
  122. switch (info->flash_id & FLASH_VENDMASK) {
  123. case FLASH_MAN_AMD:
  124. printf("AMD ");
  125. break;
  126. case FLASH_MAN_FUJ:
  127. printf("FUJITSU ");
  128. break;
  129. case FLASH_MAN_SST:
  130. printf("SST ");
  131. break;
  132. case FLASH_MAN_STM:
  133. printf("STM ");
  134. break;
  135. default:
  136. printf("Unknown Vendor ");
  137. break;
  138. }
  139. switch (info->flash_id & FLASH_TYPEMASK) {
  140. case FLASH_AM400B:
  141. printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
  142. break;
  143. case FLASH_AM400T:
  144. printf("AM29LV400T (4 Mbit, top boot sector)\n");
  145. break;
  146. case FLASH_AM800B:
  147. printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
  148. break;
  149. case FLASH_AM800T:
  150. printf("AM29LV800T (8 Mbit, top boot sector)\n");
  151. break;
  152. case FLASH_AM160B:
  153. printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
  154. break;
  155. case FLASH_AM160T:
  156. printf("AM29LV160T (16 Mbit, top boot sector)\n");
  157. break;
  158. case FLASH_AM320B:
  159. printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
  160. break;
  161. case FLASH_AM320T:
  162. printf("AM29LV320T (32 Mbit, top boot sector)\n");
  163. break;
  164. case FLASH_SST200A:
  165. printf("39xF200A (2M = 128K x 16)\n");
  166. break;
  167. case FLASH_SST400A:
  168. printf("39xF400A (4M = 256K x 16)\n");
  169. break;
  170. case FLASH_SST800A:
  171. printf("39xF800A (8M = 512K x 16)\n");
  172. break;
  173. case FLASH_STM800AB:
  174. printf("M29W800AB (8M = 512K x 16)\n");
  175. break;
  176. default:
  177. printf("Unknown Chip Type\n");
  178. break;
  179. }
  180. printf(" Size: %ld MB in %d Sectors\n",
  181. info->size >> 20, info->sector_count);
  182. printf(" Sector Start Addresses:");
  183. for (i = 0; i < info->sector_count; ++i) {
  184. if ((i % 5) == 0)
  185. printf("\n ");
  186. printf(" %08lX%s",
  187. info->start[i],
  188. info->protect[i] ? " (RO)" : " "
  189. );
  190. }
  191. printf("\n");
  192. return;
  193. }
  194. /*
  195. * The following code cannot be run from FLASH!
  196. */
  197. static ulong flash_get_size(vu_long *addr, flash_info_t *info)
  198. {
  199. short i;
  200. ulong value;
  201. ulong base = (ulong)addr;
  202. /* Write auto select command: read Manufacturer ID */
  203. #ifdef CONFIG_FLASH_16BIT
  204. vu_short *s_addr = (vu_short *)addr;
  205. s_addr[0x5555] = 0x00AA;
  206. s_addr[0x2AAA] = 0x0055;
  207. s_addr[0x5555] = 0x0090;
  208. value = s_addr[0];
  209. value = value|(value<<16);
  210. #else
  211. addr[0x5555] = 0x00AA00AA;
  212. addr[0x2AAA] = 0x00550055;
  213. addr[0x5555] = 0x00900090;
  214. value = addr[0];
  215. #endif
  216. switch (value) {
  217. case AMD_MANUFACT:
  218. info->flash_id = FLASH_MAN_AMD;
  219. break;
  220. case FUJ_MANUFACT:
  221. info->flash_id = FLASH_MAN_FUJ;
  222. break;
  223. case SST_MANUFACT:
  224. info->flash_id = FLASH_MAN_SST;
  225. break;
  226. case STM_MANUFACT:
  227. info->flash_id = FLASH_MAN_STM;
  228. break;
  229. default:
  230. info->flash_id = FLASH_UNKNOWN;
  231. info->sector_count = 0;
  232. info->size = 0;
  233. return 0; /* no or unknown flash */
  234. }
  235. #ifdef CONFIG_FLASH_16BIT
  236. value = s_addr[1];
  237. value = value|(value<<16);
  238. #else
  239. value = addr[1]; /* device ID */
  240. #endif
  241. switch (value) {
  242. case AMD_ID_LV400T:
  243. info->flash_id += FLASH_AM400T;
  244. info->sector_count = 11;
  245. info->size = 0x00100000;
  246. break; /* => 1 MB */
  247. case AMD_ID_LV400B:
  248. info->flash_id += FLASH_AM400B;
  249. info->sector_count = 11;
  250. info->size = 0x00100000;
  251. break; /* => 1 MB */
  252. case AMD_ID_LV800T:
  253. info->flash_id += FLASH_AM800T;
  254. info->sector_count = 19;
  255. info->size = 0x00200000;
  256. break; /* => 2 MB */
  257. case AMD_ID_LV800B:
  258. info->flash_id += FLASH_AM800B;
  259. #ifdef CONFIG_FLASH_16BIT
  260. info->sector_count = 19;
  261. info->size = 0x00100000; /* => 1 MB */
  262. #else
  263. info->sector_count = 19;
  264. info->size = 0x00200000; /* => 2 MB */
  265. #endif
  266. break;
  267. case AMD_ID_LV160T:
  268. info->flash_id += FLASH_AM160T;
  269. info->sector_count = 35;
  270. info->size = 0x00400000;
  271. break; /* => 4 MB */
  272. case AMD_ID_LV160B:
  273. info->flash_id += FLASH_AM160B;
  274. #ifdef CONFIG_FLASH_16BIT
  275. info->sector_count = 35;
  276. info->size = 0x00200000; /* => 2 MB */
  277. #else
  278. info->sector_count = 35;
  279. info->size = 0x00400000; /* => 4 MB */
  280. #endif
  281. break;
  282. case SST_ID_xF200A:
  283. info->flash_id += FLASH_SST200A;
  284. info->sector_count = 64; /* 39xF200A (2M = 128K x 16) */
  285. info->size = 0x00080000;
  286. break;
  287. case SST_ID_xF400A:
  288. info->flash_id += FLASH_SST400A;
  289. info->sector_count = 128; /* 39xF400A (4M = 256K x 16) */
  290. info->size = 0x00100000;
  291. break;
  292. case SST_ID_xF800A:
  293. info->flash_id += FLASH_SST800A;
  294. info->sector_count = 256; /* 39xF800A (8M = 512K x 16) */
  295. info->size = 0x00200000;
  296. break; /* => 2 MB */
  297. case STM_ID_x800AB:
  298. info->flash_id += FLASH_STM800AB;
  299. info->sector_count = 19;
  300. info->size = 0x00200000;
  301. break; /* => 2 MB */
  302. default:
  303. info->flash_id = FLASH_UNKNOWN;
  304. return 0; /* => no or unknown flash */
  305. }
  306. if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
  307. printf("** ERROR: sector count %d > max (%d) **\n",
  308. info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
  309. info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  310. }
  311. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  312. for (i = 0; i < info->sector_count; i++)
  313. info->start[i] = base + (i * 0x00002000);
  314. } else { /* AMD and Fujitsu types */
  315. /* set up sector start address table */
  316. if (info->flash_id & FLASH_BTYPE) {
  317. /* set sector offsets for bottom boot block type */
  318. #ifdef CONFIG_FLASH_16BIT
  319. info->start[0] = base + 0x00000000;
  320. info->start[1] = base + 0x00004000;
  321. info->start[2] = base + 0x00006000;
  322. info->start[3] = base + 0x00008000;
  323. for (i = 4; i < info->sector_count; i++)
  324. info->start[i] = base +
  325. (i * 0x00010000) - 0x00030000;
  326. #else
  327. info->start[0] = base + 0x00000000;
  328. info->start[1] = base + 0x00008000;
  329. info->start[2] = base + 0x0000C000;
  330. info->start[3] = base + 0x00010000;
  331. for (i = 4; i < info->sector_count; i++)
  332. info->start[i] = base +
  333. (i * 0x00020000) - 0x00060000;
  334. #endif
  335. } else {
  336. /* set sector offsets for top boot block type */
  337. i = info->sector_count - 1;
  338. info->start[i--] = base + info->size - 0x00008000;
  339. info->start[i--] = base + info->size - 0x0000C000;
  340. info->start[i--] = base + info->size - 0x00010000;
  341. for (; i >= 0; i--)
  342. info->start[i] = base + i * 0x00020000;
  343. }
  344. /* check for protected sectors */
  345. for (i = 0; i < info->sector_count; i++) {
  346. /*
  347. * read sector protection at sector address:
  348. * (A7 .. A0) = 0x02
  349. * D0 = 1 if protected
  350. */
  351. #ifdef CONFIG_FLASH_16BIT
  352. s_addr = (volatile unsigned short *)(info->start[i]);
  353. info->protect[i] = s_addr[2] & 1;
  354. #else
  355. addr = (volatile unsigned long *)(info->start[i]);
  356. info->protect[i] = addr[2] & 1;
  357. #endif
  358. }
  359. }
  360. /*
  361. * Prevent writes to uninitialized FLASH.
  362. */
  363. if (info->flash_id != FLASH_UNKNOWN) {
  364. #ifdef CONFIG_FLASH_16BIT
  365. s_addr = (volatile unsigned short *)(info->start[0]);
  366. *s_addr = 0x00F0; /* reset bank */
  367. #else
  368. addr = (volatile unsigned long *)info->start[0];
  369. *addr = 0x00F000F0; /* reset bank */
  370. #endif
  371. }
  372. return info->size;
  373. }
  374. int flash_erase(flash_info_t *info, int s_first, int s_last)
  375. {
  376. vu_long *addr = (vu_long *)(info->start[0]);
  377. int flag, prot, sect;
  378. ulong start, now, last;
  379. #ifdef CONFIG_FLASH_16BIT
  380. vu_short *s_addr = (vu_short *)addr;
  381. #endif
  382. if ((s_first < 0) || (s_first > s_last)) {
  383. if (info->flash_id == FLASH_UNKNOWN)
  384. printf("- missing\n");
  385. else
  386. printf("- no sectors to erase\n");
  387. return 1;
  388. }
  389. /*#ifndef CONFIG_FLASH_16BIT
  390. ulong type;
  391. type = (info->flash_id & FLASH_VENDMASK);
  392. if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) {
  393. printf ("Can't erase unknown flash type %08lx - aborted\n",
  394. info->flash_id);
  395. return;
  396. }
  397. #endif*/
  398. prot = 0;
  399. for (sect = s_first; sect <= s_last; ++sect) {
  400. if (info->protect[sect])
  401. prot++;
  402. }
  403. if (prot) {
  404. printf("- Warning: %d protected sectors will not be erased!\n",
  405. prot);
  406. } else {
  407. printf("\n");
  408. }
  409. start = get_timer(0);
  410. last = start;
  411. /* Start erase on unprotected sectors */
  412. for (sect = s_first; sect <= s_last; sect++) {
  413. if (info->protect[sect] == 0) { /* not protected */
  414. #ifdef CONFIG_FLASH_16BIT
  415. vu_short *s_sect_addr = (vu_short *)(info->start[sect]);
  416. #else
  417. vu_long *sect_addr = (vu_long *)(info->start[sect]);
  418. #endif
  419. /* Disable interrupts which might cause a timeout */
  420. flag = disable_interrupts();
  421. #ifdef CONFIG_FLASH_16BIT
  422. /*printf("\ns_sect_addr=%x",s_sect_addr);*/
  423. s_addr[0x5555] = 0x00AA;
  424. s_addr[0x2AAA] = 0x0055;
  425. s_addr[0x5555] = 0x0080;
  426. s_addr[0x5555] = 0x00AA;
  427. s_addr[0x2AAA] = 0x0055;
  428. s_sect_addr[0] = 0x0030;
  429. #else
  430. addr[0x5555] = 0x00AA00AA;
  431. addr[0x2AAA] = 0x00550055;
  432. addr[0x5555] = 0x00800080;
  433. addr[0x5555] = 0x00AA00AA;
  434. addr[0x2AAA] = 0x00550055;
  435. sect_addr[0] = 0x00300030;
  436. #endif
  437. /* re-enable interrupts if necessary */
  438. if (flag)
  439. enable_interrupts();
  440. /* wait at least 80us - let's wait 1 ms */
  441. udelay(1000);
  442. #ifdef CONFIG_FLASH_16BIT
  443. while ((s_sect_addr[0] & 0x0080) != 0x0080) {
  444. #else
  445. while ((sect_addr[0] & 0x00800080) != 0x00800080) {
  446. #endif
  447. now = get_timer(start);
  448. if (now > CONFIG_SYS_FLASH_ERASE_TOUT) {
  449. printf("Timeout\n");
  450. return 1;
  451. }
  452. /* show every second that we're waiting */
  453. if ((now - last) > 1000) {
  454. putc('.');
  455. last = now;
  456. }
  457. }
  458. }
  459. }
  460. /* reset to read mode */
  461. addr = (volatile unsigned long *)info->start[0];
  462. #ifdef CONFIG_FLASH_16BIT
  463. s_addr[0] = 0x00F0; /* reset bank */
  464. #else
  465. addr[0] = 0x00F000F0; /* reset bank */
  466. #endif
  467. printf(" done\n");
  468. return 0;
  469. }
  470. /*-----------------------------------------------------------------------
  471. * Copy memory to flash, returns:
  472. * 0 - OK
  473. * 1 - write timeout
  474. * 2 - Flash not erased
  475. * 4 - Flash not identified
  476. */
  477. int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  478. {
  479. ulong cp, wp, data;
  480. int i, l, rc;
  481. if (info->flash_id == FLASH_UNKNOWN)
  482. return 4;
  483. wp = (addr & ~3); /* get lower word aligned address */
  484. /*
  485. * handle unaligned start bytes
  486. */
  487. l = addr - wp;
  488. if (l != 0) {
  489. data = 0;
  490. for (i = 0, cp = wp; i < l; ++i, ++cp)
  491. data = (data << 8) | (*(uchar *)cp);
  492. for (; i < 4 && cnt > 0; ++i) {
  493. data = (data << 8) | *src++;
  494. --cnt;
  495. ++cp;
  496. }
  497. for (; cnt == 0 && i < 4; ++i, ++cp)
  498. data = (data << 8) | (*(uchar *)cp);
  499. rc = write_word(info, wp, data);
  500. if (rc != 0)
  501. return rc;
  502. wp += 4;
  503. }
  504. /*
  505. * handle word aligned part
  506. */
  507. while (cnt >= 4) {
  508. data = 0;
  509. for (i = 0; i < 4; ++i)
  510. data = (data << 8) | *src++;
  511. rc = write_word(info, wp, data);
  512. if (rc != 0)
  513. return rc;
  514. wp += 4;
  515. cnt -= 4;
  516. }
  517. if (cnt == 0)
  518. return 0;
  519. /*
  520. * handle unaligned tail bytes
  521. */
  522. data = 0;
  523. for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
  524. data = (data << 8) | *src++;
  525. --cnt;
  526. }
  527. for (; i < 4; ++i, ++cp)
  528. data = (data << 8) | (*(uchar *)cp);
  529. return write_word(info, wp, data);
  530. }
  531. /*-----------------------------------------------------------------------
  532. * Write a word to Flash, returns:
  533. * 0 - OK
  534. * 1 - write timeout
  535. * 2 - Flash not erased
  536. */
  537. static int write_word(flash_info_t *info, ulong dest, ulong data)
  538. {
  539. vu_long *addr = (vu_long *)(info->start[0]);
  540. #ifdef CONFIG_FLASH_16BIT
  541. vu_short high_data;
  542. vu_short low_data;
  543. vu_short *s_addr = (vu_short *)addr;
  544. #endif
  545. ulong start;
  546. int flag;
  547. /* Check if Flash is (sufficiently) erased */
  548. if ((*((vu_long *)dest) & data) != data)
  549. return 2;
  550. #ifdef CONFIG_FLASH_16BIT
  551. /* Write the 16 higher-bits */
  552. /* Disable interrupts which might cause a timeout here */
  553. flag = disable_interrupts();
  554. high_data = ((data>>16) & 0x0000ffff);
  555. s_addr[0x5555] = 0x00AA;
  556. s_addr[0x2AAA] = 0x0055;
  557. s_addr[0x5555] = 0x00A0;
  558. *((vu_short *)dest) = high_data;
  559. /* re-enable interrupts if necessary */
  560. if (flag)
  561. enable_interrupts();
  562. /* data polling for D7 */
  563. start = get_timer(0);
  564. while ((*((vu_short *)dest) & 0x0080) != (high_data & 0x0080)) {
  565. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
  566. return 1;
  567. }
  568. /* Write the 16 lower-bits */
  569. #endif
  570. /* Disable interrupts which might cause a timeout here */
  571. flag = disable_interrupts();
  572. #ifdef CONFIG_FLASH_16BIT
  573. dest += 0x2;
  574. low_data = (data & 0x0000ffff);
  575. s_addr[0x5555] = 0x00AA;
  576. s_addr[0x2AAA] = 0x0055;
  577. s_addr[0x5555] = 0x00A0;
  578. *((vu_short *)dest) = low_data;
  579. #else
  580. addr[0x5555] = 0x00AA00AA;
  581. addr[0x2AAA] = 0x00550055;
  582. addr[0x5555] = 0x00A000A0;
  583. *((vu_long *)dest) = data;
  584. #endif
  585. /* re-enable interrupts if necessary */
  586. if (flag)
  587. enable_interrupts();
  588. /* data polling for D7 */
  589. start = get_timer(0);
  590. #ifdef CONFIG_FLASH_16BIT
  591. while ((*((vu_short *)dest) & 0x0080) != (low_data & 0x0080)) {
  592. #else
  593. while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
  594. #endif
  595. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
  596. return 1;
  597. }
  598. return 0;
  599. }