flash.c 22 KB

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