flash.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <mpc8xx.h>
  25. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  26. /*-----------------------------------------------------------------------
  27. * Functions
  28. */
  29. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  30. static int write_word (flash_info_t *info, ulong dest, ulong data);
  31. static void flash_get_offsets (ulong base, flash_info_t *info);
  32. /*-----------------------------------------------------------------------
  33. */
  34. unsigned long flash_init (void)
  35. {
  36. volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
  37. volatile memctl8xx_t *memctl = &immap->im_memctl;
  38. unsigned long size_b0, size_b1;
  39. int i;
  40. /* Init: no FLASHes known */
  41. for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  42. flash_info[i].flash_id = FLASH_UNKNOWN;
  43. }
  44. /* Static FLASH Bank configuration here - FIXME XXX */
  45. size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
  46. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  47. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  48. size_b0, size_b0<<20);
  49. }
  50. size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
  51. if (size_b1 > size_b0) {
  52. printf ("## ERROR: "
  53. "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
  54. size_b1, size_b1<<20,
  55. size_b0, size_b0<<20
  56. );
  57. flash_info[0].flash_id = FLASH_UNKNOWN;
  58. flash_info[1].flash_id = FLASH_UNKNOWN;
  59. flash_info[0].sector_count = -1;
  60. flash_info[1].sector_count = -1;
  61. flash_info[0].size = 0;
  62. flash_info[1].size = 0;
  63. return (0);
  64. }
  65. /* Remap FLASH according to real size */
  66. memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
  67. #ifdef CONFIG_FLASH_16BIT
  68. memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V | BR_PS_16; /* 16 Bit data port */
  69. #else
  70. memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
  71. #endif
  72. /* Re-do sizing to get full correct info */
  73. size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
  74. flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
  75. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
  76. /* monitor protection ON by default */
  77. flash_protect(FLAG_PROTECT_SET,
  78. CONFIG_SYS_MONITOR_BASE,
  79. CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
  80. &flash_info[0]);
  81. #endif
  82. if (size_b1) {
  83. memctl->memc_or1 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
  84. #ifdef CONFIG_FLASH_16BIT
  85. memctl->memc_br1 = ((CONFIG_SYS_FLASH_BASE + size_b0) & BR_BA_MSK) |
  86. BR_MS_GPCM | BR_V | BR_PS_16;
  87. #else
  88. memctl->memc_br1 = ((CONFIG_SYS_FLASH_BASE + size_b0) & BR_BA_MSK) |
  89. BR_MS_GPCM | BR_V;
  90. #endif
  91. /* Re-do sizing to get full correct info */
  92. size_b1 = flash_get_size((vu_long *)(CONFIG_SYS_FLASH_BASE + size_b0),
  93. &flash_info[1]);
  94. flash_get_offsets (CONFIG_SYS_FLASH_BASE + size_b0, &flash_info[1]);
  95. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
  96. /* monitor protection ON by default */
  97. flash_protect(FLAG_PROTECT_SET,
  98. CONFIG_SYS_MONITOR_BASE,
  99. CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
  100. &flash_info[1]);
  101. #endif
  102. } else {
  103. memctl->memc_br1 = 0; /* invalidate bank */
  104. flash_info[1].flash_id = FLASH_UNKNOWN;
  105. flash_info[1].sector_count = -1;
  106. }
  107. flash_info[0].size = size_b0;
  108. flash_info[1].size = size_b1;
  109. return (size_b0 + size_b1);
  110. }
  111. /*-----------------------------------------------------------------------
  112. */
  113. static void flash_get_offsets (ulong base, flash_info_t *info)
  114. {
  115. int i;
  116. if (info->flash_id == FLASH_UNKNOWN) {
  117. return;
  118. }
  119. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  120. for (i = 0; i < info->sector_count; i++) {
  121. info->start[i] = base + (i * 0x00002000);
  122. }
  123. return;
  124. }
  125. /* set up sector start address table */
  126. if (info->flash_id & FLASH_BTYPE) {
  127. /* set sector offsets for bottom boot block type */
  128. #ifdef CONFIG_FLASH_16BIT
  129. info->start[0] = base + 0x00000000;
  130. info->start[1] = base + 0x00004000;
  131. info->start[2] = base + 0x00006000;
  132. info->start[3] = base + 0x00008000;
  133. for (i = 4; i < info->sector_count; i++) {
  134. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  135. #else
  136. info->start[0] = base + 0x00000000;
  137. info->start[1] = base + 0x00008000;
  138. info->start[2] = base + 0x0000C000;
  139. info->start[3] = base + 0x00010000;
  140. for (i = 4; i < info->sector_count; i++) {
  141. info->start[i] = base + (i * 0x00020000) - 0x00060000;
  142. #endif
  143. }
  144. } else {
  145. /* set sector offsets for top boot block type */
  146. i = info->sector_count - 1;
  147. info->start[i--] = base + info->size - 0x00008000;
  148. info->start[i--] = base + info->size - 0x0000C000;
  149. info->start[i--] = base + info->size - 0x00010000;
  150. for (; i >= 0; i--) {
  151. info->start[i] = base + i * 0x00020000;
  152. }
  153. }
  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. case FLASH_MAN_SST: printf ("SST "); break;
  168. case FLASH_MAN_STM: printf ("STM "); break;
  169. default: printf ("Unknown Vendor "); break;
  170. }
  171. switch (info->flash_id & FLASH_TYPEMASK) {
  172. case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  173. break;
  174. case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  175. break;
  176. case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  177. break;
  178. case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  179. break;
  180. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  181. break;
  182. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  183. break;
  184. case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  185. break;
  186. case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  187. break;
  188. case FLASH_SST200A: printf ("39xF200A (2M = 128K x 16)\n");
  189. break;
  190. case FLASH_SST400A: printf ("39xF400A (4M = 256K x 16)\n");
  191. break;
  192. case FLASH_SST800A: printf ("39xF800A (8M = 512K x 16)\n");
  193. break;
  194. case FLASH_STM800AB: printf ("M29W800AB (8M = 512K x 16)\n");
  195. break;
  196. default: printf ("Unknown Chip Type\n");
  197. break;
  198. }
  199. printf (" Size: %ld MB in %d Sectors\n",
  200. info->size >> 20, info->sector_count);
  201. printf (" Sector Start Addresses:");
  202. for (i=0; i<info->sector_count; ++i) {
  203. if ((i % 5) == 0)
  204. printf ("\n ");
  205. printf (" %08lX%s",
  206. info->start[i],
  207. info->protect[i] ? " (RO)" : " "
  208. );
  209. }
  210. printf ("\n");
  211. return;
  212. }
  213. /*-----------------------------------------------------------------------
  214. */
  215. /*-----------------------------------------------------------------------
  216. */
  217. /*
  218. * The following code cannot be run from FLASH!
  219. */
  220. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  221. {
  222. short i;
  223. ulong value;
  224. ulong base = (ulong)addr;
  225. /* Write auto select command: read Manufacturer ID */
  226. #ifdef CONFIG_FLASH_16BIT
  227. vu_short *s_addr = (vu_short*)addr;
  228. s_addr[0x5555] = 0x00AA;
  229. s_addr[0x2AAA] = 0x0055;
  230. s_addr[0x5555] = 0x0090;
  231. value = s_addr[0];
  232. value = value|(value<<16);
  233. #else
  234. addr[0x5555] = 0x00AA00AA;
  235. addr[0x2AAA] = 0x00550055;
  236. addr[0x5555] = 0x00900090;
  237. value = addr[0];
  238. #endif
  239. switch (value) {
  240. case AMD_MANUFACT:
  241. info->flash_id = FLASH_MAN_AMD;
  242. break;
  243. case FUJ_MANUFACT:
  244. info->flash_id = FLASH_MAN_FUJ;
  245. break;
  246. case SST_MANUFACT:
  247. info->flash_id = FLASH_MAN_SST;
  248. break;
  249. case STM_MANUFACT:
  250. info->flash_id = FLASH_MAN_STM;
  251. break;
  252. default:
  253. info->flash_id = FLASH_UNKNOWN;
  254. info->sector_count = 0;
  255. info->size = 0;
  256. return (0); /* no or unknown flash */
  257. }
  258. #ifdef CONFIG_FLASH_16BIT
  259. value = s_addr[1];
  260. value = value|(value<<16);
  261. #else
  262. value = addr[1]; /* device ID */
  263. #endif
  264. switch (value) {
  265. case AMD_ID_LV400T:
  266. info->flash_id += FLASH_AM400T;
  267. info->sector_count = 11;
  268. info->size = 0x00100000;
  269. break; /* => 1 MB */
  270. case AMD_ID_LV400B:
  271. info->flash_id += FLASH_AM400B;
  272. info->sector_count = 11;
  273. info->size = 0x00100000;
  274. break; /* => 1 MB */
  275. case AMD_ID_LV800T:
  276. info->flash_id += FLASH_AM800T;
  277. info->sector_count = 19;
  278. info->size = 0x00200000;
  279. break; /* => 2 MB */
  280. case AMD_ID_LV800B:
  281. info->flash_id += FLASH_AM800B;
  282. #ifdef CONFIG_FLASH_16BIT
  283. info->sector_count = 19;
  284. info->size = 0x00100000; /* => 1 MB */
  285. #else
  286. info->sector_count = 19;
  287. info->size = 0x00200000; /* => 2 MB */
  288. #endif
  289. break;
  290. case AMD_ID_LV160T:
  291. info->flash_id += FLASH_AM160T;
  292. info->sector_count = 35;
  293. info->size = 0x00400000;
  294. break; /* => 4 MB */
  295. case AMD_ID_LV160B:
  296. info->flash_id += FLASH_AM160B;
  297. #ifdef CONFIG_FLASH_16BIT
  298. info->sector_count = 35;
  299. info->size = 0x00200000; /* => 2 MB */
  300. #else
  301. info->sector_count = 35;
  302. info->size = 0x00400000; /* => 4 MB */
  303. #endif
  304. break;
  305. #if 0 /* enable when device IDs are available */
  306. case AMD_ID_LV320T:
  307. info->flash_id += FLASH_AM320T;
  308. info->sector_count = 67;
  309. info->size = 0x00800000;
  310. break; /* => 8 MB */
  311. case AMD_ID_LV320B:
  312. info->flash_id += FLASH_AM320B;
  313. info->sector_count = 67;
  314. info->size = 0x00800000;
  315. break; /* => 8 MB */
  316. #endif
  317. case SST_ID_xF200A:
  318. info->flash_id += FLASH_SST200A;
  319. info->sector_count = 64; /* 39xF200A ID ( 2M = 128K x 16 ) */
  320. info->size = 0x00080000;
  321. break;
  322. case SST_ID_xF400A:
  323. info->flash_id += FLASH_SST400A;
  324. info->sector_count = 128; /* 39xF400A ID ( 4M = 256K x 16 ) */
  325. info->size = 0x00100000;
  326. break;
  327. case SST_ID_xF800A:
  328. info->flash_id += FLASH_SST800A;
  329. info->sector_count = 256; /* 39xF800A ID ( 8M = 512K x 16 ) */
  330. info->size = 0x00200000;
  331. break; /* => 2 MB */
  332. case STM_ID_x800AB:
  333. info->flash_id += FLASH_STM800AB;
  334. info->sector_count = 19;
  335. info->size = 0x00200000;
  336. break; /* => 2 MB */
  337. default:
  338. info->flash_id = FLASH_UNKNOWN;
  339. return (0); /* => no or unknown flash */
  340. }
  341. if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
  342. printf ("** ERROR: sector count %d > max (%d) **\n",
  343. info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
  344. info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  345. }
  346. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  347. for (i = 0; i < info->sector_count; i++) {
  348. info->start[i] = base + (i * 0x00002000);
  349. }
  350. } else { /* AMD and Fujitsu types */
  351. /* set up sector start address table */
  352. if (info->flash_id & FLASH_BTYPE) {
  353. /* set sector offsets for bottom boot block type */
  354. #ifdef CONFIG_FLASH_16BIT
  355. info->start[0] = base + 0x00000000;
  356. info->start[1] = base + 0x00004000;
  357. info->start[2] = base + 0x00006000;
  358. info->start[3] = base + 0x00008000;
  359. for (i = 4; i < info->sector_count; i++) {
  360. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  361. #else
  362. info->start[0] = base + 0x00000000;
  363. info->start[1] = base + 0x00008000;
  364. info->start[2] = base + 0x0000C000;
  365. info->start[3] = base + 0x00010000;
  366. for (i = 4; i < info->sector_count; i++) {
  367. info->start[i] = base + (i * 0x00020000) - 0x00060000;
  368. #endif
  369. }
  370. } else {
  371. /* set sector offsets for top boot block type */
  372. i = info->sector_count - 1;
  373. info->start[i--] = base + info->size - 0x00008000;
  374. info->start[i--] = base + info->size - 0x0000C000;
  375. info->start[i--] = base + info->size - 0x00010000;
  376. for (; i >= 0; i--) {
  377. info->start[i] = base + i * 0x00020000;
  378. }
  379. }
  380. /* check for protected sectors */
  381. for (i = 0; i < info->sector_count; i++) {
  382. /* read sector protection at sector address:
  383. * (A7 .. A0) = 0x02
  384. * D0 = 1 if protected
  385. */
  386. #ifdef CONFIG_FLASH_16BIT
  387. s_addr = (volatile unsigned short *)(info->start[i]);
  388. info->protect[i] = s_addr[2] & 1;
  389. #else
  390. addr = (volatile unsigned long *)(info->start[i]);
  391. info->protect[i] = addr[2] & 1;
  392. #endif
  393. }
  394. }
  395. /*
  396. * Prevent writes to uninitialized FLASH.
  397. */
  398. if (info->flash_id != FLASH_UNKNOWN) {
  399. #ifdef CONFIG_FLASH_16BIT
  400. s_addr = (volatile unsigned short *)(info->start[0]);
  401. *s_addr = 0x00F0; /* reset bank */
  402. #else
  403. addr = (volatile unsigned long *)info->start[0];
  404. *addr = 0x00F000F0; /* reset bank */
  405. #endif
  406. }
  407. return (info->size);
  408. }
  409. /*-----------------------------------------------------------------------
  410. */
  411. int flash_erase (flash_info_t *info, int s_first, int s_last)
  412. {
  413. vu_long *addr = (vu_long*)(info->start[0]);
  414. int flag, prot, sect;
  415. ulong start, now, last;
  416. #ifdef CONFIG_FLASH_16BIT
  417. vu_short *s_addr = (vu_short*)addr;
  418. #endif
  419. if ((s_first < 0) || (s_first > s_last)) {
  420. if (info->flash_id == FLASH_UNKNOWN) {
  421. printf ("- missing\n");
  422. } else {
  423. printf ("- no sectors to erase\n");
  424. }
  425. return 1;
  426. }
  427. /*#ifndef CONFIG_FLASH_16BIT
  428. ulong type;
  429. type = (info->flash_id & FLASH_VENDMASK);
  430. if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) {
  431. printf ("Can't erase unknown flash type %08lx - aborted\n",
  432. info->flash_id);
  433. return;
  434. }
  435. #endif*/
  436. prot = 0;
  437. for (sect=s_first; sect<=s_last; ++sect) {
  438. if (info->protect[sect]) {
  439. prot++;
  440. }
  441. }
  442. if (prot) {
  443. printf ("- Warning: %d protected sectors will not be erased!\n",
  444. prot);
  445. } else {
  446. printf ("\n");
  447. }
  448. start = get_timer (0);
  449. last = start;
  450. /* Start erase on unprotected sectors */
  451. for (sect = s_first; sect<=s_last; sect++) {
  452. if (info->protect[sect] == 0) { /* not protected */
  453. #ifdef CONFIG_FLASH_16BIT
  454. vu_short *s_sect_addr = (vu_short*)(info->start[sect]);
  455. #else
  456. vu_long *sect_addr = (vu_long*)(info->start[sect]);
  457. #endif
  458. /* Disable interrupts which might cause a timeout here */
  459. flag = disable_interrupts();
  460. #ifdef CONFIG_FLASH_16BIT
  461. /*printf("\ns_sect_addr=%x",s_sect_addr);*/
  462. s_addr[0x5555] = 0x00AA;
  463. s_addr[0x2AAA] = 0x0055;
  464. s_addr[0x5555] = 0x0080;
  465. s_addr[0x5555] = 0x00AA;
  466. s_addr[0x2AAA] = 0x0055;
  467. s_sect_addr[0] = 0x0030;
  468. #else
  469. addr[0x5555] = 0x00AA00AA;
  470. addr[0x2AAA] = 0x00550055;
  471. addr[0x5555] = 0x00800080;
  472. addr[0x5555] = 0x00AA00AA;
  473. addr[0x2AAA] = 0x00550055;
  474. sect_addr[0] = 0x00300030;
  475. #endif
  476. /* re-enable interrupts if necessary */
  477. if (flag)
  478. enable_interrupts();
  479. /* wait at least 80us - let's wait 1 ms */
  480. udelay (1000);
  481. #ifdef CONFIG_FLASH_16BIT
  482. while ((s_sect_addr[0] & 0x0080) != 0x0080) {
  483. #else
  484. while ((sect_addr[0] & 0x00800080) != 0x00800080) {
  485. #endif
  486. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  487. printf ("Timeout\n");
  488. return 1;
  489. }
  490. /* show that we're waiting */
  491. if ((now - last) > 1000) { /* every second */
  492. putc ('.');
  493. last = now;
  494. }
  495. }
  496. }
  497. }
  498. /* reset to read mode */
  499. addr = (volatile unsigned long *)info->start[0];
  500. #ifdef CONFIG_FLASH_16BIT
  501. s_addr[0] = 0x00F0; /* reset bank */
  502. #else
  503. addr[0] = 0x00F000F0; /* reset bank */
  504. #endif
  505. printf (" done\n");
  506. return 0;
  507. }
  508. /*-----------------------------------------------------------------------
  509. * Copy memory to flash, returns:
  510. * 0 - OK
  511. * 1 - write timeout
  512. * 2 - Flash not erased
  513. * 4 - Flash not identified
  514. */
  515. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  516. {
  517. ulong cp, wp, data;
  518. int i, l, rc;
  519. if (info->flash_id == FLASH_UNKNOWN) {
  520. return 4;
  521. }
  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. #ifdef CONFIG_FLASH_16BIT
  584. vu_short high_data;
  585. vu_short low_data;
  586. vu_short *s_addr = (vu_short*)addr;
  587. #endif
  588. ulong start;
  589. int flag;
  590. /* Check if Flash is (sufficiently) erased */
  591. if ((*((vu_long *)dest) & data) != data) {
  592. return (2);
  593. }
  594. #ifdef CONFIG_FLASH_16BIT
  595. /* Write the 16 higher-bits */
  596. /* Disable interrupts which might cause a timeout here */
  597. flag = disable_interrupts();
  598. high_data = ((data>>16) & 0x0000ffff);
  599. s_addr[0x5555] = 0x00AA;
  600. s_addr[0x2AAA] = 0x0055;
  601. s_addr[0x5555] = 0x00A0;
  602. *((vu_short *)dest) = high_data;
  603. /* re-enable interrupts if necessary */
  604. if (flag)
  605. enable_interrupts();
  606. /* data polling for D7 */
  607. start = get_timer (0);
  608. while ((*((vu_short *)dest) & 0x0080) != (high_data & 0x0080)) {
  609. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  610. return (1);
  611. }
  612. }
  613. /* Write the 16 lower-bits */
  614. #endif
  615. /* Disable interrupts which might cause a timeout here */
  616. flag = disable_interrupts();
  617. #ifdef CONFIG_FLASH_16BIT
  618. dest += 0x2;
  619. low_data = (data & 0x0000ffff);
  620. s_addr[0x5555] = 0x00AA;
  621. s_addr[0x2AAA] = 0x0055;
  622. s_addr[0x5555] = 0x00A0;
  623. *((vu_short *)dest) = low_data;
  624. #else
  625. addr[0x5555] = 0x00AA00AA;
  626. addr[0x2AAA] = 0x00550055;
  627. addr[0x5555] = 0x00A000A0;
  628. *((vu_long *)dest) = data;
  629. #endif
  630. /* re-enable interrupts if necessary */
  631. if (flag)
  632. enable_interrupts();
  633. /* data polling for D7 */
  634. start = get_timer (0);
  635. #ifdef CONFIG_FLASH_16BIT
  636. while ((*((vu_short *)dest) & 0x0080) != (low_data & 0x0080)) {
  637. #else
  638. while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
  639. #endif
  640. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  641. return (1);
  642. }
  643. }
  644. return (0);
  645. }
  646. /*-----------------------------------------------------------------------
  647. */