flash.c 17 KB

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