flash.c 21 KB

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