flash.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. * (C) 2000 Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  3. * (C) 2003 August Hoeraendl, Logotronic GmbH
  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. #undef CONFIG_FLASH_16BIT
  24. #include <common.h>
  25. #define FLASH_BANK_SIZE 0x1000000
  26. #define MAIN_SECT_SIZE 0x20000 /* 2x64k = 128k per sector */
  27. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  28. /* NOTE - CONFIG_FLASH_16BIT means the CPU interface is 16-bit, it
  29. * has nothing to do with the flash chip being 8-bit or 16-bit.
  30. */
  31. #ifdef CONFIG_FLASH_16BIT
  32. typedef unsigned short FLASH_PORT_WIDTH;
  33. typedef volatile unsigned short FLASH_PORT_WIDTHV;
  34. #define FLASH_ID_MASK 0xFFFF
  35. #else
  36. typedef unsigned long FLASH_PORT_WIDTH;
  37. typedef volatile unsigned long FLASH_PORT_WIDTHV;
  38. #define FLASH_ID_MASK 0xFFFFFFFF
  39. #endif
  40. #define FPW FLASH_PORT_WIDTH
  41. #define FPWV FLASH_PORT_WIDTHV
  42. #define ORMASK(size) ((-size) & OR_AM_MSK)
  43. /*-----------------------------------------------------------------------
  44. * Functions
  45. */
  46. static ulong flash_get_size (FPWV * addr, flash_info_t * info);
  47. static void flash_reset (flash_info_t * info);
  48. static int write_word_intel (flash_info_t * info, FPWV * dest, FPW data);
  49. static int write_word_amd (flash_info_t * info, FPWV * dest, FPW data);
  50. #define write_word(in, de, da) write_word_amd(in, de, da)
  51. static void flash_get_offsets (ulong base, flash_info_t * info);
  52. #ifdef CFG_FLASH_PROTECTION
  53. static void flash_sync_real_protect (flash_info_t * info);
  54. #endif
  55. /*-----------------------------------------------------------------------
  56. * flash_init()
  57. *
  58. * sets up flash_info and returns size of FLASH (bytes)
  59. */
  60. ulong flash_init (void)
  61. {
  62. int i, j;
  63. ulong size = 0;
  64. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  65. ulong flashbase = 0;
  66. flash_info[i].flash_id =
  67. (FLASH_MAN_AMD & FLASH_VENDMASK) |
  68. (FLASH_AM640U & FLASH_TYPEMASK);
  69. flash_info[i].size = FLASH_BANK_SIZE;
  70. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  71. memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  72. switch (i) {
  73. case 0:
  74. flashbase = PHYS_FLASH_1;
  75. break;
  76. case 1:
  77. flashbase = PHYS_FLASH_2;
  78. break;
  79. default:
  80. panic ("configured to many flash banks!\n");
  81. break;
  82. }
  83. for (j = 0; j < flash_info[i].sector_count; j++) {
  84. flash_info[i].start[j] = flashbase + j * MAIN_SECT_SIZE;
  85. }
  86. size += flash_info[i].size;
  87. }
  88. /* Protect monitor and environment sectors
  89. */
  90. flash_protect (FLAG_PROTECT_SET,
  91. CFG_FLASH_BASE,
  92. CFG_FLASH_BASE + _armboot_end_data - _armboot_start,
  93. &flash_info[0]);
  94. flash_protect (FLAG_PROTECT_SET,
  95. CFG_ENV_ADDR,
  96. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
  97. return size;
  98. }
  99. /*-----------------------------------------------------------------------
  100. */
  101. static void flash_reset (flash_info_t * info)
  102. {
  103. FPWV *base = (FPWV *) (info->start[0]);
  104. /* Put FLASH back in read mode */
  105. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
  106. *base = (FPW) 0x00FF00FF; /* Intel Read Mode */
  107. else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
  108. *base = (FPW) 0x00F000F0; /* AMD Read Mode */
  109. }
  110. /*-----------------------------------------------------------------------
  111. */
  112. static void flash_get_offsets (ulong base, flash_info_t * info)
  113. {
  114. int i;
  115. /* set up sector start address table */
  116. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL
  117. && (info->flash_id & FLASH_BTYPE)) {
  118. int bootsect_size; /* number of bytes/boot sector */
  119. int sect_size; /* number of bytes/regular sector */
  120. bootsect_size = 0x00002000 * (sizeof (FPW) / 2);
  121. sect_size = 0x00010000 * (sizeof (FPW) / 2);
  122. /* set sector offsets for bottom boot block type */
  123. for (i = 0; i < 8; ++i) {
  124. info->start[i] = base + (i * bootsect_size);
  125. }
  126. for (i = 8; i < info->sector_count; i++) {
  127. info->start[i] = base + ((i - 7) * sect_size);
  128. }
  129. } else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD
  130. && (info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U) {
  131. int sect_size; /* number of bytes/sector */
  132. sect_size = 0x00010000 * (sizeof (FPW) / 2);
  133. /* set up sector start address table (uniform sector type) */
  134. for (i = 0; i < info->sector_count; i++)
  135. info->start[i] = base + (i * sect_size);
  136. }
  137. }
  138. /*-----------------------------------------------------------------------
  139. */
  140. void flash_print_info (flash_info_t * info)
  141. {
  142. int i;
  143. uchar *boottype;
  144. uchar *bootletter;
  145. uchar *fmt;
  146. uchar botbootletter[] = "B";
  147. uchar topbootletter[] = "T";
  148. uchar botboottype[] = "bottom boot sector";
  149. uchar topboottype[] = "top boot sector";
  150. if (info->flash_id == FLASH_UNKNOWN) {
  151. printf ("missing or unknown FLASH type\n");
  152. return;
  153. }
  154. switch (info->flash_id & FLASH_VENDMASK) {
  155. case FLASH_MAN_AMD:
  156. printf ("AMD ");
  157. break;
  158. case FLASH_MAN_BM:
  159. printf ("BRIGHT MICRO ");
  160. break;
  161. case FLASH_MAN_FUJ:
  162. printf ("FUJITSU ");
  163. break;
  164. case FLASH_MAN_SST:
  165. printf ("SST ");
  166. break;
  167. case FLASH_MAN_STM:
  168. printf ("STM ");
  169. break;
  170. case FLASH_MAN_INTEL:
  171. printf ("INTEL ");
  172. break;
  173. default:
  174. printf ("Unknown Vendor ");
  175. break;
  176. }
  177. /* check for top or bottom boot, if it applies */
  178. if (info->flash_id & FLASH_BTYPE) {
  179. boottype = botboottype;
  180. bootletter = botbootletter;
  181. } else {
  182. boottype = topboottype;
  183. bootletter = topbootletter;
  184. }
  185. switch (info->flash_id & FLASH_TYPEMASK) {
  186. case FLASH_AM640U:
  187. fmt = "29LV641D (64 Mbit, uniform sectors)\n";
  188. break;
  189. case FLASH_28F800C3B:
  190. case FLASH_28F800C3T:
  191. fmt = "28F800C3%s (8 Mbit, %s)\n";
  192. break;
  193. case FLASH_INTEL800B:
  194. case FLASH_INTEL800T:
  195. fmt = "28F800B3%s (8 Mbit, %s)\n";
  196. break;
  197. case FLASH_28F160C3B:
  198. case FLASH_28F160C3T:
  199. fmt = "28F160C3%s (16 Mbit, %s)\n";
  200. break;
  201. case FLASH_INTEL160B:
  202. case FLASH_INTEL160T:
  203. fmt = "28F160B3%s (16 Mbit, %s)\n";
  204. break;
  205. case FLASH_28F320C3B:
  206. case FLASH_28F320C3T:
  207. fmt = "28F320C3%s (32 Mbit, %s)\n";
  208. break;
  209. case FLASH_INTEL320B:
  210. case FLASH_INTEL320T:
  211. fmt = "28F320B3%s (32 Mbit, %s)\n";
  212. break;
  213. case FLASH_28F640C3B:
  214. case FLASH_28F640C3T:
  215. fmt = "28F640C3%s (64 Mbit, %s)\n";
  216. break;
  217. case FLASH_INTEL640B:
  218. case FLASH_INTEL640T:
  219. fmt = "28F640B3%s (64 Mbit, %s)\n";
  220. break;
  221. default:
  222. fmt = "Unknown Chip Type\n";
  223. break;
  224. }
  225. printf (fmt, bootletter, boottype);
  226. printf (" Size: %ld MB in %d Sectors\n",
  227. info->size >> 20, info->sector_count);
  228. printf (" Sector Start Addresses:");
  229. for (i = 0; i < info->sector_count; ++i) {
  230. if ((i % 5) == 0) {
  231. printf ("\n ");
  232. }
  233. printf (" %08lX%s", info->start[i],
  234. info->protect[i] ? " (RO)" : " ");
  235. }
  236. printf ("\n");
  237. }
  238. /*-----------------------------------------------------------------------
  239. */
  240. /*
  241. * The following code cannot be run from FLASH!
  242. */
  243. ulong flash_get_size (FPWV * addr, flash_info_t * info)
  244. {
  245. /* Write auto select command: read Manufacturer ID */
  246. /* Write auto select command sequence and test FLASH answer */
  247. addr[0x0555] = (FPW) 0x00AA00AA; /* for AMD, Intel ignores this */
  248. addr[0x02AA] = (FPW) 0x00550055; /* for AMD, Intel ignores this */
  249. addr[0x0555] = (FPW) 0x00900090; /* selects Intel or AMD */
  250. /* The manufacturer codes are only 1 byte, so just use 1 byte.
  251. * This works for any bus width and any FLASH device width.
  252. */
  253. switch (addr[0] & 0xff) {
  254. case (uchar) AMD_MANUFACT:
  255. info->flash_id = FLASH_MAN_AMD;
  256. break;
  257. case (uchar) INTEL_MANUFACT:
  258. info->flash_id = FLASH_MAN_INTEL;
  259. break;
  260. default:
  261. info->flash_id = FLASH_UNKNOWN;
  262. info->sector_count = 0;
  263. info->size = 0;
  264. break;
  265. }
  266. /* Check 16 bits or 32 bits of ID so work on 32 or 16 bit bus. */
  267. if (info->flash_id != FLASH_UNKNOWN)
  268. switch (addr[1]) {
  269. case (FPW) AMD_ID_LV640U: /* 29LV640 and 29LV641 have same ID */
  270. info->flash_id += FLASH_AM640U;
  271. info->sector_count = 128;
  272. info->size = 0x00800000 * (sizeof (FPW) / 2);
  273. break; /* => 8 or 16 MB */
  274. case (FPW) INTEL_ID_28F800C3B:
  275. info->flash_id += FLASH_28F800C3B;
  276. info->sector_count = 23;
  277. info->size = 0x00100000 * (sizeof (FPW) / 2);
  278. break; /* => 1 or 2 MB */
  279. case (FPW) INTEL_ID_28F800B3B:
  280. info->flash_id += FLASH_INTEL800B;
  281. info->sector_count = 23;
  282. info->size = 0x00100000 * (sizeof (FPW) / 2);
  283. break; /* => 1 or 2 MB */
  284. case (FPW) INTEL_ID_28F160C3B:
  285. info->flash_id += FLASH_28F160C3B;
  286. info->sector_count = 39;
  287. info->size = 0x00200000 * (sizeof (FPW) / 2);
  288. break; /* => 2 or 4 MB */
  289. case (FPW) INTEL_ID_28F160B3B:
  290. info->flash_id += FLASH_INTEL160B;
  291. info->sector_count = 39;
  292. info->size = 0x00200000 * (sizeof (FPW) / 2);
  293. break; /* => 2 or 4 MB */
  294. case (FPW) INTEL_ID_28F320C3B:
  295. info->flash_id += FLASH_28F320C3B;
  296. info->sector_count = 71;
  297. info->size = 0x00400000 * (sizeof (FPW) / 2);
  298. break; /* => 4 or 8 MB */
  299. case (FPW) INTEL_ID_28F320B3B:
  300. info->flash_id += FLASH_INTEL320B;
  301. info->sector_count = 71;
  302. info->size = 0x00400000 * (sizeof (FPW) / 2);
  303. break; /* => 4 or 8 MB */
  304. case (FPW) INTEL_ID_28F640C3B:
  305. info->flash_id += FLASH_28F640C3B;
  306. info->sector_count = 135;
  307. info->size = 0x00800000 * (sizeof (FPW) / 2);
  308. break; /* => 8 or 16 MB */
  309. case (FPW) INTEL_ID_28F640B3B:
  310. info->flash_id += FLASH_INTEL640B;
  311. info->sector_count = 135;
  312. info->size = 0x00800000 * (sizeof (FPW) / 2);
  313. break; /* => 8 or 16 MB */
  314. default:
  315. info->flash_id = FLASH_UNKNOWN;
  316. info->sector_count = 0;
  317. info->size = 0;
  318. return (0); /* => no or unknown flash */
  319. }
  320. flash_get_offsets ((ulong) addr, info);
  321. /* Put FLASH back in read mode */
  322. flash_reset (info);
  323. return (info->size);
  324. }
  325. #ifdef CFG_FLASH_PROTECTION
  326. /*-----------------------------------------------------------------------
  327. */
  328. static void flash_sync_real_protect (flash_info_t * info)
  329. {
  330. FPWV *addr = (FPWV *) (info->start[0]);
  331. FPWV *sect;
  332. int i;
  333. switch (info->flash_id & FLASH_TYPEMASK) {
  334. case FLASH_28F800C3B:
  335. case FLASH_28F800C3T:
  336. case FLASH_28F160C3B:
  337. case FLASH_28F160C3T:
  338. case FLASH_28F320C3B:
  339. case FLASH_28F320C3T:
  340. case FLASH_28F640C3B:
  341. case FLASH_28F640C3T:
  342. /* check for protected sectors */
  343. *addr = (FPW) 0x00900090;
  344. for (i = 0; i < info->sector_count; i++) {
  345. /* read sector protection at sector address, (A7 .. A0) = 0x02.
  346. * D0 = 1 for each device if protected.
  347. * If at least one device is protected the sector is marked
  348. * protected, but mixed protected and unprotected devices
  349. * within a sector should never happen.
  350. */
  351. sect = (FPWV *) (info->start[i]);
  352. info->protect[i] = (sect[2] & (FPW) (0x00010001)) ? 1 : 0;
  353. }
  354. /* Put FLASH back in read mode */
  355. flash_reset (info);
  356. break;
  357. case FLASH_AM640U:
  358. default:
  359. /* no hardware protect that we support */
  360. break;
  361. }
  362. }
  363. #endif
  364. /*-----------------------------------------------------------------------
  365. */
  366. int flash_erase (flash_info_t * info, int s_first, int s_last)
  367. {
  368. FPWV *addr;
  369. int flag, prot, sect;
  370. int intel = (info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL;
  371. ulong start, now, last;
  372. int rcode = 0;
  373. if ((s_first < 0) || (s_first > s_last)) {
  374. if (info->flash_id == FLASH_UNKNOWN) {
  375. printf ("- missing\n");
  376. } else {
  377. printf ("- no sectors to erase\n");
  378. }
  379. return 1;
  380. }
  381. switch (info->flash_id & FLASH_TYPEMASK) {
  382. case FLASH_INTEL800B:
  383. case FLASH_INTEL160B:
  384. case FLASH_INTEL320B:
  385. case FLASH_INTEL640B:
  386. case FLASH_28F800C3B:
  387. case FLASH_28F160C3B:
  388. case FLASH_28F320C3B:
  389. case FLASH_28F640C3B:
  390. case FLASH_AM640U:
  391. break;
  392. case FLASH_UNKNOWN:
  393. default:
  394. printf ("Can't erase unknown flash type %08lx - aborted\n",
  395. info->flash_id);
  396. return 1;
  397. }
  398. prot = 0;
  399. for (sect = s_first; sect <= s_last; ++sect) {
  400. if (info->protect[sect]) {
  401. prot++;
  402. }
  403. }
  404. if (prot) {
  405. printf ("- Warning: %d protected sectors will not be erased!\n",
  406. prot);
  407. } else {
  408. printf ("\n");
  409. }
  410. start = get_timer (0);
  411. last = start;
  412. /* Start erase on unprotected sectors */
  413. for (sect = s_first; sect <= s_last && rcode == 0; sect++) {
  414. if (info->protect[sect] != 0) /* protected, skip it */
  415. continue;
  416. /* Disable interrupts which might cause a timeout here */
  417. flag = disable_interrupts ();
  418. addr = (FPWV *) (info->start[sect]);
  419. if (intel) {
  420. *addr = (FPW) 0x00500050; /* clear status register */
  421. *addr = (FPW) 0x00200020; /* erase setup */
  422. *addr = (FPW) 0x00D000D0; /* erase confirm */
  423. } else {
  424. /* must be AMD style if not Intel */
  425. FPWV *base; /* first address in bank */
  426. base = (FPWV *) (info->start[0]);
  427. base[0x0555] = (FPW) 0x00AA00AA; /* unlock */
  428. base[0x02AA] = (FPW) 0x00550055; /* unlock */
  429. base[0x0555] = (FPW) 0x00800080; /* erase mode */
  430. base[0x0555] = (FPW) 0x00AA00AA; /* unlock */
  431. base[0x02AA] = (FPW) 0x00550055; /* unlock */
  432. *addr = (FPW) 0x00300030; /* erase sector */
  433. }
  434. /* re-enable interrupts if necessary */
  435. if (flag)
  436. enable_interrupts ();
  437. /* wait at least 50us for AMD, 80us for Intel.
  438. * Let's wait 1 ms.
  439. */
  440. udelay (1000);
  441. while ((*addr & (FPW) 0x00800080) != (FPW) 0x00800080) {
  442. if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
  443. printf ("Timeout\n");
  444. if (intel) {
  445. /* suspend erase */
  446. *addr = (FPW) 0x00B000B0;
  447. }
  448. flash_reset (info); /* reset to read mode */
  449. rcode = 1; /* failed */
  450. break;
  451. }
  452. /* show that we're waiting */
  453. if ((now - last) > 1000) { /* every second */
  454. putc ('.');
  455. last = now;
  456. }
  457. }
  458. flash_reset (info); /* reset to read mode */
  459. }
  460. printf (" done\n");
  461. return rcode;
  462. }
  463. /*-----------------------------------------------------------------------
  464. * Copy memory to flash, returns:
  465. * 0 - OK
  466. * 1 - write timeout
  467. * 2 - Flash not erased
  468. */
  469. int bad_write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  470. {
  471. FPW data = 0; /* 16 or 32 bit word, matches flash bus width */
  472. int bytes; /* number of bytes to program in current word */
  473. int left; /* number of bytes left to program */
  474. int i, res;
  475. /* printf("write_buff: src: %8p addr %08lx count: %ld\n", src, addr, cnt); */
  476. for (left = cnt, res = 0;
  477. left > 0 && res == 0;
  478. addr += sizeof (data), left -= sizeof (data) - bytes) {
  479. bytes = addr & (sizeof (data) - 1);
  480. addr &= ~(sizeof (data) - 1);
  481. /* combine source and destination data so can program
  482. * an entire word of 16 or 32 bits
  483. */
  484. for (i = 0; i < sizeof (data); i++) {
  485. data <<= 8;
  486. if (i < bytes || i - bytes >= left)
  487. data += *((uchar *) addr + i);
  488. else
  489. data += *src++;
  490. }
  491. /* write one word to the flash */
  492. switch (info->flash_id & FLASH_VENDMASK) {
  493. case FLASH_MAN_AMD:
  494. res = write_word_amd (info, (FPWV *) addr, data);
  495. break;
  496. case FLASH_MAN_INTEL:
  497. res = write_word_intel (info, (FPWV *) addr, data);
  498. break;
  499. default:
  500. /* unknown flash type, error! */
  501. printf ("missing or unknown FLASH type\n");
  502. res = 1; /* not really a timeout, but gives error */
  503. break;
  504. }
  505. }
  506. return (res);
  507. }
  508. /**
  509. * write_buf: - Copy memory to flash.
  510. *
  511. * @param info:
  512. * @param src: source of copy transaction
  513. * @param addr: where to copy to
  514. * @param cnt: number of bytes to copy
  515. *
  516. * @return error code
  517. */
  518. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  519. {
  520. ulong cp, wp;
  521. FPW data;
  522. int l;
  523. int i, rc;
  524. wp = (addr & ~1); /* get lower word aligned address */
  525. /*
  526. * handle unaligned start bytes
  527. */
  528. if ((l = addr - wp) != 0) {
  529. data = 0;
  530. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  531. data = (data >> 8) | (*(uchar *) cp << 8);
  532. }
  533. for (; i < 2 && cnt > 0; ++i) {
  534. data = (data >> 8) | (*src++ << 8);
  535. --cnt;
  536. ++cp;
  537. }
  538. for (; cnt == 0 && i < 2; ++i, ++cp) {
  539. data = (data >> 8) | (*(uchar *) cp << 8);
  540. }
  541. if ((rc = write_word (info, wp, data)) != 0) {
  542. return (rc);
  543. }
  544. wp += 2;
  545. }
  546. /*
  547. * handle word aligned part
  548. */
  549. while (cnt >= 2) {
  550. /* data = *((vushort*)src); */
  551. data = *((FPW *) src);
  552. if ((rc = write_word (info, wp, data)) != 0) {
  553. return (rc);
  554. }
  555. src += sizeof (FPW);
  556. wp += sizeof (FPW);
  557. cnt -= sizeof (FPW);
  558. }
  559. if (cnt == 0)
  560. return ERR_OK;
  561. /*
  562. * handle unaligned tail bytes
  563. */
  564. data = 0;
  565. for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
  566. data = (data >> 8) | (*src++ << 8);
  567. --cnt;
  568. }
  569. for (; i < 2; ++i, ++cp) {
  570. data = (data >> 8) | (*(uchar *) cp << 8);
  571. }
  572. return write_word (info, wp, data);
  573. }
  574. /*-----------------------------------------------------------------------
  575. * Write a word to Flash for AMD FLASH
  576. * A word is 16 or 32 bits, whichever the bus width of the flash bank
  577. * (not an individual chip) is.
  578. *
  579. * returns:
  580. * 0 - OK
  581. * 1 - write timeout
  582. * 2 - Flash not erased
  583. */
  584. static int write_word_amd (flash_info_t * info, FPWV * dest, FPW data)
  585. {
  586. ulong start;
  587. int flag;
  588. int res = 0; /* result, assume success */
  589. FPWV *base; /* first address in flash bank */
  590. /* Check if Flash is (sufficiently) erased */
  591. if ((*dest & data) != data) {
  592. return (2);
  593. }
  594. base = (FPWV *) (info->start[0]);
  595. /* Disable interrupts which might cause a timeout here */
  596. flag = disable_interrupts ();
  597. base[0x0555] = (FPW) 0x00AA00AA; /* unlock */
  598. base[0x02AA] = (FPW) 0x00550055; /* unlock */
  599. base[0x0555] = (FPW) 0x00A000A0; /* selects program mode */
  600. *dest = data; /* start programming the data */
  601. /* re-enable interrupts if necessary */
  602. if (flag)
  603. enable_interrupts ();
  604. start = get_timer (0);
  605. /* data polling for D7 */
  606. while (res == 0
  607. && (*dest & (FPW) 0x00800080) != (data & (FPW) 0x00800080)) {
  608. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  609. *dest = (FPW) 0x00F000F0; /* reset bank */
  610. res = 1;
  611. }
  612. }
  613. return (res);
  614. }
  615. /*-----------------------------------------------------------------------
  616. * Write a word to Flash for Intel FLASH
  617. * A word is 16 or 32 bits, whichever the bus width of the flash bank
  618. * (not an individual chip) is.
  619. *
  620. * returns:
  621. * 0 - OK
  622. * 1 - write timeout
  623. * 2 - Flash not erased
  624. */
  625. static int write_word_intel (flash_info_t * info, FPWV * dest, FPW data)
  626. {
  627. ulong start;
  628. int flag;
  629. int res = 0; /* result, assume success */
  630. /* Check if Flash is (sufficiently) erased */
  631. if ((*dest & data) != data) {
  632. return (2);
  633. }
  634. /* Disable interrupts which might cause a timeout here */
  635. flag = disable_interrupts ();
  636. *dest = (FPW) 0x00500050; /* clear status register */
  637. *dest = (FPW) 0x00FF00FF; /* make sure in read mode */
  638. *dest = (FPW) 0x00400040; /* program setup */
  639. *dest = data; /* start programming the data */
  640. /* re-enable interrupts if necessary */
  641. if (flag)
  642. enable_interrupts ();
  643. start = get_timer (0);
  644. while (res == 0 && (*dest & (FPW) 0x00800080) != (FPW) 0x00800080) {
  645. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  646. *dest = (FPW) 0x00B000B0; /* Suspend program */
  647. res = 1;
  648. }
  649. }
  650. if (res == 0 && (*dest & (FPW) 0x00100010))
  651. res = 1; /* write failed, time out error is close enough */
  652. *dest = (FPW) 0x00500050; /* clear status register */
  653. *dest = (FPW) 0x00FF00FF; /* make sure in read mode */
  654. return (res);
  655. }
  656. #ifdef CFG_FLASH_PROTECTION
  657. /*-----------------------------------------------------------------------
  658. */
  659. int flash_real_protect (flash_info_t * info, long sector, int prot)
  660. {
  661. int rcode = 0; /* assume success */
  662. FPWV *addr; /* address of sector */
  663. FPW value;
  664. addr = (FPWV *) (info->start[sector]);
  665. switch (info->flash_id & FLASH_TYPEMASK) {
  666. case FLASH_28F800C3B:
  667. case FLASH_28F800C3T:
  668. case FLASH_28F160C3B:
  669. case FLASH_28F160C3T:
  670. case FLASH_28F320C3B:
  671. case FLASH_28F320C3T:
  672. case FLASH_28F640C3B:
  673. case FLASH_28F640C3T:
  674. flash_reset (info); /* make sure in read mode */
  675. *addr = (FPW) 0x00600060L; /* lock command setup */
  676. if (prot)
  677. *addr = (FPW) 0x00010001L; /* lock sector */
  678. else
  679. *addr = (FPW) 0x00D000D0L; /* unlock sector */
  680. flash_reset (info); /* reset to read mode */
  681. /* now see if it really is locked/unlocked as requested */
  682. *addr = (FPW) 0x00900090;
  683. /* read sector protection at sector address, (A7 .. A0) = 0x02.
  684. * D0 = 1 for each device if protected.
  685. * If at least one device is protected the sector is marked
  686. * protected, but return failure. Mixed protected and
  687. * unprotected devices within a sector should never happen.
  688. */
  689. value = addr[2] & (FPW) 0x00010001;
  690. if (value == 0)
  691. info->protect[sector] = 0;
  692. else if (value == (FPW) 0x00010001)
  693. info->protect[sector] = 1;
  694. else {
  695. /* error, mixed protected and unprotected */
  696. rcode = 1;
  697. info->protect[sector] = 1;
  698. }
  699. if (info->protect[sector] != prot)
  700. rcode = 1; /* failed to protect/unprotect as requested */
  701. /* reload all protection bits from hardware for now */
  702. flash_sync_real_protect (info);
  703. break;
  704. case FLASH_AM640U:
  705. default:
  706. /* no hardware protect that we support */
  707. info->protect[sector] = prot;
  708. break;
  709. }
  710. return rcode;
  711. }
  712. #endif