flash.c 19 KB

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