flash.c 19 KB

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