flash.c 17 KB

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