flash.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /*
  2. * (C) Copyright 2001
  3. * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com.
  4. * Based on code by:
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <ppc4xx.h>
  27. #include <asm/processor.h>
  28. #include <watchdog.h>
  29. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  30. /*-----------------------------------------------------------------------
  31. * Functions
  32. */
  33. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  34. static int write_word8(flash_info_t *info, ulong dest, ulong data);
  35. static int write_word32 (flash_info_t *info, ulong dest, ulong data);
  36. static void flash_get_offsets (ulong base, flash_info_t *info);
  37. /*-----------------------------------------------------------------------
  38. */
  39. unsigned long flash_init (void)
  40. {
  41. int i;
  42. unsigned long size_b0, base_b0;
  43. unsigned long size_b1, base_b1;
  44. /* Init: no FLASHes known */
  45. for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
  46. flash_info[i].flash_id = FLASH_UNKNOWN;
  47. }
  48. /* Get Size of Boot and Main Flashes */
  49. size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
  50. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  51. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  52. size_b0, size_b0<<20);
  53. return 0;
  54. }
  55. size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
  56. if (flash_info[1].flash_id == FLASH_UNKNOWN) {
  57. printf ("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n",
  58. size_b1, size_b1<<20);
  59. return 0;
  60. }
  61. /* Calculate base addresses */
  62. base_b0 = -size_b0;
  63. base_b1 = -size_b1;
  64. /* Setup offsets for Boot Flash */
  65. flash_get_offsets (base_b0, &flash_info[0]);
  66. /* Protect board level data */
  67. (void)flash_protect(FLAG_PROTECT_SET,
  68. base_b0,
  69. flash_info[0].start[1] - 1,
  70. &flash_info[0]);
  71. /* Monitor protection ON by default */
  72. (void)flash_protect(FLAG_PROTECT_SET,
  73. base_b0 + size_b0 - monitor_flash_len,
  74. base_b0 + size_b0 - 1,
  75. &flash_info[0]);
  76. /* Protect the FPGA image */
  77. (void)flash_protect(FLAG_PROTECT_SET,
  78. FLASH_BASE1_PRELIM,
  79. FLASH_BASE1_PRELIM + CFG_FPGA_IMAGE_LEN - 1,
  80. &flash_info[1]);
  81. /* Protect the default boot image */
  82. (void)flash_protect(FLAG_PROTECT_SET,
  83. FLASH_BASE1_PRELIM + CFG_FPGA_IMAGE_LEN,
  84. FLASH_BASE1_PRELIM + CFG_FPGA_IMAGE_LEN + 0x600000 - 1,
  85. &flash_info[1]);
  86. /* Setup offsets for Main Flash */
  87. flash_get_offsets (FLASH_BASE1_PRELIM, &flash_info[1]);
  88. return (size_b0 + size_b1);
  89. } /* end flash_init() */
  90. /*-----------------------------------------------------------------------
  91. */
  92. static void flash_get_offsets (ulong base, flash_info_t *info)
  93. {
  94. int i;
  95. /* set up sector start address table - FOR BOOT ROM ONLY!!! */
  96. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
  97. for (i = 0; i < info->sector_count; i++)
  98. info->start[i] = base + (i * 0x00010000);
  99. }
  100. } /* end flash_get_offsets() */
  101. /*-----------------------------------------------------------------------
  102. */
  103. void flash_print_info (flash_info_t *info)
  104. {
  105. int i;
  106. int k;
  107. int size;
  108. int erased;
  109. volatile unsigned long *flash;
  110. if (info->flash_id == FLASH_UNKNOWN) {
  111. printf ("missing or unknown FLASH type\n");
  112. return;
  113. }
  114. switch (info->flash_id & FLASH_VENDMASK) {
  115. case FLASH_MAN_AMD: printf ("1 x AMD "); break;
  116. case FLASH_MAN_STM: printf ("1 x STM "); break;
  117. case FLASH_MAN_INTEL: printf ("2 x Intel "); break;
  118. default: printf ("Unknown Vendor ");
  119. }
  120. switch (info->flash_id & FLASH_TYPEMASK) {
  121. case FLASH_AM040:
  122. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
  123. printf ("AM29LV040 (4096 Kbit, uniform sector size)\n");
  124. else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_STM)
  125. printf ("M29W040B (4096 Kbit, uniform block size)\n");
  126. else
  127. printf ("UNKNOWN 29x040x (4096 Kbit, uniform sector size)\n");
  128. break;
  129. case FLASH_28F320J3A:
  130. printf ("28F320J3A (32 Mbit = 128K x 32)\n");
  131. break;
  132. case FLASH_28F640J3A:
  133. printf ("28F640J3A (64 Mbit = 128K x 64)\n");
  134. break;
  135. case FLASH_28F128J3A:
  136. printf ("28F128J3A (128 Mbit = 128K x 128)\n");
  137. break;
  138. default:
  139. printf ("Unknown Chip Type\n");
  140. }
  141. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_STM) {
  142. printf (" Size: %ld KB in %d Blocks\n",
  143. info->size >> 10, info->sector_count);
  144. } else {
  145. printf (" Size: %ld KB in %d Sectors\n",
  146. info->size >> 10, info->sector_count);
  147. }
  148. printf (" Sector Start Addresses:");
  149. for (i=0; i<info->sector_count; ++i) {
  150. /*
  151. * Check if whole sector is erased
  152. */
  153. if (i != (info->sector_count-1))
  154. size = info->start[i+1] - info->start[i];
  155. else
  156. size = info->start[0] + info->size - info->start[i];
  157. erased = 1;
  158. flash = (volatile unsigned long *)info->start[i];
  159. size = size >> 2; /* divide by 4 for longword access */
  160. for (k=0; k<size; k++)
  161. {
  162. if (*flash++ != 0xffffffff)
  163. {
  164. erased = 0;
  165. break;
  166. }
  167. }
  168. if ((i % 5) == 0)
  169. printf ("\n ");
  170. printf (" %08lX%s%s",
  171. info->start[i],
  172. erased ? " E" : " ",
  173. info->protect[i] ? "RO " : " "
  174. );
  175. }
  176. printf ("\n");
  177. } /* end flash_print_info() */
  178. /*
  179. * The following code cannot be run from FLASH!
  180. */
  181. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  182. {
  183. short i;
  184. ulong base = (ulong)addr;
  185. /* Setup default type */
  186. info->flash_id = FLASH_UNKNOWN;
  187. info->sector_count =0;
  188. info->size = 0;
  189. /* Test for Boot Flash */
  190. if (base == FLASH_BASE0_PRELIM) {
  191. unsigned char value;
  192. volatile unsigned char * addr2 = (unsigned char *)addr;
  193. /* Write auto select command: read Manufacturer ID */
  194. *(addr2 + 0x555) = 0xaa;
  195. *(addr2 + 0x2aa) = 0x55;
  196. *(addr2 + 0x555) = 0x90;
  197. /* Manufacture ID */
  198. value = *addr2;
  199. switch (value) {
  200. case (unsigned char)AMD_MANUFACT:
  201. info->flash_id = FLASH_MAN_AMD;
  202. break;
  203. case (unsigned char)STM_MANUFACT:
  204. info->flash_id = FLASH_MAN_STM;
  205. break;
  206. default:
  207. *addr2 = 0xf0; /* no or unknown flash */
  208. return 0;
  209. }
  210. /* Device ID */
  211. value = *(addr2 + 1);
  212. switch (value) {
  213. case (unsigned char)AMD_ID_LV040B:
  214. case (unsigned char)STM_ID_29W040B:
  215. info->flash_id += FLASH_AM040;
  216. info->sector_count = 8;
  217. info->size = 0x00080000;
  218. break; /* => 512Kb */
  219. default:
  220. *addr2 = 0xf0; /* => no or unknown flash */
  221. return 0;
  222. }
  223. }
  224. else { /* MAIN Flash */
  225. unsigned long value;
  226. volatile unsigned long * addr2 = (unsigned long *)addr;
  227. /* Write auto select command: read Manufacturer ID */
  228. *addr2 = 0x90909090;
  229. /* Manufacture ID */
  230. value = *addr2;
  231. switch (value) {
  232. case (unsigned long)INTEL_MANUFACT:
  233. info->flash_id = FLASH_MAN_INTEL;
  234. break;
  235. default:
  236. *addr2 = 0xff; /* no or unknown flash */
  237. return 0;
  238. }
  239. /* Device ID - This shit is interleaved... */
  240. value = *(addr2 + 1);
  241. switch (value) {
  242. case (unsigned long)INTEL_ID_28F320J3A:
  243. info->flash_id += FLASH_28F320J3A;
  244. info->sector_count = 32;
  245. info->size = 0x00400000 * 2;
  246. break; /* => 2 X 4 MB */
  247. case (unsigned long)INTEL_ID_28F640J3A:
  248. info->flash_id += FLASH_28F640J3A;
  249. info->sector_count = 64;
  250. info->size = 0x00800000 * 2;
  251. break; /* => 2 X 8 MB */
  252. case (unsigned long)INTEL_ID_28F128J3A:
  253. info->flash_id += FLASH_28F128J3A;
  254. info->sector_count = 128;
  255. info->size = 0x01000000 * 2;
  256. break; /* => 2 X 16 MB */
  257. default:
  258. *addr2 = 0xff; /* => no or unknown flash */
  259. }
  260. }
  261. /* Make sure we don't exceed CFG_MAX_FLASH_SECT */
  262. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  263. printf ("** ERROR: sector count %d > max (%d) **\n",
  264. info->sector_count, CFG_MAX_FLASH_SECT);
  265. info->sector_count = CFG_MAX_FLASH_SECT;
  266. }
  267. /* set up sector start address table */
  268. switch (info->flash_id & FLASH_TYPEMASK) {
  269. case FLASH_AM040:
  270. for (i = 0; i < info->sector_count; i++)
  271. info->start[i] = base + (i * 0x00010000);
  272. break;
  273. case FLASH_28F320J3A:
  274. case FLASH_28F640J3A:
  275. case FLASH_28F128J3A:
  276. for (i = 0; i < info->sector_count; i++)
  277. info->start[i] = base + (i * 0x00020000 * 2); /* 2 Banks */
  278. break;
  279. }
  280. /* Test for Boot Flash */
  281. if (base == FLASH_BASE0_PRELIM) {
  282. volatile unsigned char *addr2;
  283. /* check for protected sectors */
  284. for (i = 0; i < info->sector_count; i++) {
  285. /* read sector protection at sector address, (AX .. A0) = 0x02 */
  286. /* D0 = 1 if protected */
  287. addr2 = (volatile unsigned char *)(info->start[i]);
  288. info->protect[i] = *(addr2 + 2) & 1;
  289. }
  290. /* Restore read mode */
  291. *(unsigned char *)base = 0xF0; /* Reset NORMAL Flash */
  292. }
  293. else { /* Main Flash */
  294. volatile unsigned long *addr2;
  295. /* check for protected sectors */
  296. for (i = 0; i < info->sector_count; i++) {
  297. /* read sector protection at sector address, (AX .. A0) = 0x02 */
  298. /* D0 = 1 if protected */
  299. addr2 = (volatile unsigned long *)(info->start[i]);
  300. info->protect[i] = *(addr2 + 2) & 0x1;
  301. }
  302. /* Restore read mode */
  303. *(unsigned long *)base = 0xFFFFFFFF; /* Reset Flash */
  304. }
  305. return (info->size);
  306. } /* end flash_get_size() */
  307. /*-----------------------------------------------------------------------
  308. */
  309. static int wait_for_DQ7(ulong addr, uchar cmp_val, ulong tout)
  310. {
  311. int i;
  312. volatile uchar *vaddr = (uchar *)addr;
  313. /* Loop X times */
  314. for (i = 1; i <= (100 * tout); i++) { /* Wait up to tout ms */
  315. udelay(10);
  316. /* Pause 10 us */
  317. /* Check for completion */
  318. if ((vaddr[0] & 0x80) == (cmp_val & 0x80)) {
  319. return 0;
  320. }
  321. /* KEEP THE LUSER HAPPY - Print a dot every 1.1 seconds */
  322. if (!(i % 110000))
  323. putc('.');
  324. /* Kick the dog if needed */
  325. WATCHDOG_RESET();
  326. }
  327. return 1;
  328. } /* wait_for_DQ7() */
  329. /*-----------------------------------------------------------------------
  330. */
  331. static int flash_erase8(flash_info_t *info, int s_first, int s_last)
  332. {
  333. int tcode, rcode = 0;
  334. volatile uchar *addr = (uchar *)(info->start[0]);
  335. volatile uchar *sector_addr;
  336. int flag, prot, sect;
  337. /* Validate arguments */
  338. if ((s_first < 0) || (s_first > s_last)) {
  339. if (info->flash_id == FLASH_UNKNOWN)
  340. printf ("- missing\n");
  341. else
  342. printf ("- no sectors to erase\n");
  343. return 1;
  344. }
  345. /* Check for KNOWN flash type */
  346. if (info->flash_id == FLASH_UNKNOWN) {
  347. printf ("Can't erase unknown flash type - aborted\n");
  348. return 1;
  349. }
  350. /* Check for protected sectors */
  351. prot = 0;
  352. for (sect = s_first; sect <= s_last; ++sect) {
  353. if (info->protect[sect])
  354. prot++;
  355. }
  356. if (prot)
  357. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  358. else
  359. printf ("\n");
  360. /* Start erase on unprotected sectors */
  361. for (sect = s_first; sect <= s_last; sect++) {
  362. if (info->protect[sect] == 0) { /* not protected */
  363. sector_addr = (uchar *)(info->start[sect]);
  364. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_STM)
  365. printf("Erasing block %p\n", sector_addr);
  366. else
  367. printf("Erasing sector %p\n", sector_addr);
  368. /* Disable interrupts which might cause Flash to timeout */
  369. flag = disable_interrupts();
  370. *(addr + 0x555) = (uchar)0xAA;
  371. *(addr + 0x2aa) = (uchar)0x55;
  372. *(addr + 0x555) = (uchar)0x80;
  373. *(addr + 0x555) = (uchar)0xAA;
  374. *(addr + 0x2aa) = (uchar)0x55;
  375. *sector_addr = (uchar)0x30; /* sector erase */
  376. /*
  377. * Wait for each sector to complete, it's more
  378. * reliable. According to AMD Spec, you must
  379. * issue all erase commands within a specified
  380. * timeout. This has been seen to fail, especially
  381. * if printf()s are included (for debug)!!
  382. * Takes up to 6 seconds.
  383. */
  384. tcode = wait_for_DQ7((ulong)sector_addr, 0x80, 6000);
  385. /* re-enable interrupts if necessary */
  386. if (flag)
  387. enable_interrupts();
  388. /* Make sure we didn't timeout */
  389. if (tcode) {
  390. printf ("Timeout\n");
  391. rcode = 1;
  392. }
  393. }
  394. }
  395. /* wait at least 80us - let's wait 1 ms */
  396. udelay (1000);
  397. /* reset to read mode */
  398. addr = (uchar *)info->start[0];
  399. *addr = (uchar)0xF0; /* reset bank */
  400. printf (" done\n");
  401. return rcode;
  402. } /* end flash_erase8() */
  403. static int flash_erase32(flash_info_t *info, int s_first, int s_last)
  404. {
  405. int flag, sect;
  406. ulong start, now, last;
  407. int prot = 0;
  408. /* Validate arguments */
  409. if ((s_first < 0) || (s_first > s_last)) {
  410. if (info->flash_id == FLASH_UNKNOWN)
  411. printf ("- missing\n");
  412. else
  413. printf ("- no sectors to erase\n");
  414. return 1;
  415. }
  416. /* Check for KNOWN flash type */
  417. if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
  418. printf ("Can erase only Intel flash types - aborted\n");
  419. return 1;
  420. }
  421. /* Check for protected sectors */
  422. for (sect = s_first; sect <= s_last; ++sect) {
  423. if (info->protect[sect])
  424. prot++;
  425. }
  426. if (prot)
  427. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  428. else
  429. printf ("\n");
  430. start = get_timer (0);
  431. last = start;
  432. /* Start erase on unprotected sectors */
  433. for (sect = s_first; sect <= s_last; sect++) {
  434. WATCHDOG_RESET();
  435. if (info->protect[sect] == 0) { /* not protected */
  436. vu_long *addr = (vu_long *)(info->start[sect]);
  437. unsigned long status;
  438. /* Disable interrupts which might cause a timeout here */
  439. flag = disable_interrupts();
  440. *addr = 0x00500050; /* clear status register */
  441. *addr = 0x00200020; /* erase setup */
  442. *addr = 0x00D000D0; /* erase confirm */
  443. /* re-enable interrupts if necessary */
  444. if (flag)
  445. enable_interrupts();
  446. /* Wait at least 80us - let's wait 1 ms */
  447. udelay (1000);
  448. while (((status = *addr) & 0x00800080) != 0x00800080) {
  449. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  450. printf ("Timeout\n");
  451. *addr = 0x00B000B0; /* suspend erase */
  452. *addr = 0x00FF00FF; /* reset to read mode */
  453. return 1;
  454. }
  455. /* show that we're waiting */
  456. if ((now - last) > 990) { /* every second */
  457. putc ('.');
  458. last = now;
  459. }
  460. }
  461. *addr = 0x00FF00FF; /* reset to read mode */
  462. }
  463. }
  464. printf (" done\n");
  465. return 0;
  466. } /* end flash_erase32() */
  467. int flash_erase(flash_info_t *info, int s_first, int s_last)
  468. {
  469. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040)
  470. return flash_erase8(info, s_first, s_last);
  471. else
  472. return flash_erase32(info, s_first, s_last);
  473. } /* end flash_erase() */
  474. /*-----------------------------------------------------------------------
  475. * Copy memory to flash, returns:
  476. * 0 - OK
  477. * 1 - write timeout
  478. * 2 - Flash not erased
  479. */
  480. static int write_buff8(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  481. {
  482. ulong cp, wp, data;
  483. ulong start;
  484. int i, l, rc;
  485. start = get_timer (0);
  486. wp = (addr & ~3); /* get lower word
  487. aligned address */
  488. /*
  489. * handle unaligned start bytes
  490. */
  491. if ((l = addr - wp) != 0) {
  492. data = 0;
  493. for (i=0, cp=wp; i<l; ++i, ++cp) {
  494. data = (data << 8) | (*(uchar *)cp);
  495. }
  496. for (; i<4 && cnt>0; ++i) {
  497. data = (data << 8) | *src++;
  498. --cnt;
  499. ++cp;
  500. }
  501. for (; cnt==0 && i<4; ++i, ++cp) {
  502. data = (data << 8) | (*(uchar *)cp);
  503. }
  504. if ((rc = write_word8(info, wp, data)) != 0) {
  505. return (rc);
  506. }
  507. wp += 4;
  508. }
  509. /*
  510. * handle word aligned part
  511. */
  512. while (cnt >= 4) {
  513. data = 0;
  514. for (i=0; i<4; ++i) {
  515. data = (data << 8) | *src++;
  516. }
  517. if ((rc = write_word8(info, wp, data)) != 0) {
  518. return (rc);
  519. }
  520. wp += 4;
  521. cnt -= 4;
  522. if (get_timer(start) > 1000) { /* every second */
  523. WATCHDOG_RESET();
  524. putc ('.');
  525. start = get_timer(0);
  526. }
  527. }
  528. if (cnt == 0) {
  529. return (0);
  530. }
  531. /*
  532. * handle unaligned tail bytes
  533. */
  534. data = 0;
  535. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  536. data = (data << 8) | *src++;
  537. --cnt;
  538. }
  539. for (; i<4; ++i, ++cp) {
  540. data = (data << 8) | (*(uchar *)cp);
  541. }
  542. return (write_word8(info, wp, data));
  543. } /* end write_buff8() */
  544. #define FLASH_WIDTH 4 /* flash bus width in bytes */
  545. static int write_buff32 (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  546. {
  547. ulong cp, wp, data;
  548. int i, l, rc;
  549. ulong start;
  550. start = get_timer (0);
  551. if (info->flash_id == FLASH_UNKNOWN) {
  552. return 4;
  553. }
  554. wp = (addr & ~(FLASH_WIDTH-1)); /* get lower FLASH_WIDTH aligned address */
  555. /*
  556. * handle unaligned start bytes
  557. */
  558. if ((l = addr - wp) != 0) {
  559. data = 0;
  560. for (i=0, cp=wp; i<l; ++i, ++cp) {
  561. data = (data << 8) | (*(uchar *)cp);
  562. }
  563. for (; i<FLASH_WIDTH && cnt>0; ++i) {
  564. data = (data << 8) | *src++;
  565. --cnt;
  566. ++cp;
  567. }
  568. for (; cnt==0 && i<FLASH_WIDTH; ++i, ++cp) {
  569. data = (data << 8) | (*(uchar *)cp);
  570. }
  571. if ((rc = write_word32(info, wp, data)) != 0) {
  572. return (rc);
  573. }
  574. wp += FLASH_WIDTH;
  575. }
  576. /*
  577. * handle FLASH_WIDTH aligned part
  578. */
  579. while (cnt >= FLASH_WIDTH) {
  580. data = 0;
  581. for (i=0; i<FLASH_WIDTH; ++i) {
  582. data = (data << 8) | *src++;
  583. }
  584. if ((rc = write_word32(info, wp, data)) != 0) {
  585. return (rc);
  586. }
  587. wp += FLASH_WIDTH;
  588. cnt -= FLASH_WIDTH;
  589. if (get_timer(start) > 990) { /* every second */
  590. putc ('.');
  591. start = get_timer(0);
  592. }
  593. }
  594. if (cnt == 0) {
  595. return (0);
  596. }
  597. /*
  598. * handle unaligned tail bytes
  599. */
  600. data = 0;
  601. for (i=0, cp=wp; i<FLASH_WIDTH && cnt>0; ++i, ++cp) {
  602. data = (data << 8) | *src++;
  603. --cnt;
  604. }
  605. for (; i<FLASH_WIDTH; ++i, ++cp) {
  606. data = (data << 8) | (*(uchar *)cp);
  607. }
  608. return (write_word32(info, wp, data));
  609. } /* write_buff32() */
  610. int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  611. {
  612. int retval;
  613. if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040)
  614. retval = write_buff8(info, src, addr, cnt);
  615. else
  616. retval = write_buff32(info, src, addr, cnt);
  617. return retval;
  618. } /* end write_buff() */
  619. /*-----------------------------------------------------------------------
  620. * Write a word to Flash, returns:
  621. * 0 - OK
  622. * 1 - write timeout
  623. * 2 - Flash not erased
  624. */
  625. static int write_word8(flash_info_t *info, ulong dest, ulong data)
  626. {
  627. volatile uchar *addr2 = (uchar *)(info->start[0]);
  628. volatile uchar *dest2 = (uchar *)dest;
  629. volatile uchar *data2 = (uchar *)&data;
  630. int flag;
  631. int i, tcode, rcode = 0;
  632. /* Check if Flash is (sufficently) erased */
  633. if ((*((volatile uchar *)dest) &
  634. (uchar)data) != (uchar)data) {
  635. return (2);
  636. }
  637. for (i=0; i < (4 / sizeof(uchar)); i++) {
  638. /* Disable interrupts which might cause a timeout here */
  639. flag = disable_interrupts();
  640. *(addr2 + 0x555) = (uchar)0xAA;
  641. *(addr2 + 0x2aa) = (uchar)0x55;
  642. *(addr2 + 0x555) = (uchar)0xA0;
  643. dest2[i] = data2[i];
  644. /* Wait for write to complete, up to 1ms */
  645. tcode = wait_for_DQ7((ulong)&dest2[i], data2[i], 1);
  646. /* re-enable interrupts if necessary */
  647. if (flag)
  648. enable_interrupts();
  649. /* Make sure we didn't timeout */
  650. if (tcode) {
  651. rcode = 1;
  652. }
  653. }
  654. return rcode;
  655. } /* end write_word8() */
  656. static int write_word32(flash_info_t *info, ulong dest, ulong data)
  657. {
  658. vu_long *addr = (vu_long *)dest;
  659. ulong status;
  660. ulong start;
  661. int flag;
  662. /* Check if Flash is (sufficiently) erased */
  663. if ((*addr & data) != data) {
  664. return (2);
  665. }
  666. /* Disable interrupts which might cause a timeout here */
  667. flag = disable_interrupts();
  668. *addr = 0x00400040; /* write setup */
  669. *addr = data;
  670. /* re-enable interrupts if necessary */
  671. if (flag)
  672. enable_interrupts();
  673. start = get_timer (0);
  674. while (((status = *addr) & 0x00800080) != 0x00800080) {
  675. WATCHDOG_RESET();
  676. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  677. *addr = 0x00FF00FF; /* restore read mode */
  678. return (1);
  679. }
  680. }
  681. *addr = 0x00FF00FF; /* restore read mode */
  682. return (0);
  683. } /* end write_word32() */
  684. static int _flash_protect(flash_info_t *info, long sector)
  685. {
  686. int i;
  687. int flag;
  688. ulong status;
  689. int rcode = 0;
  690. volatile long *addr = (unsigned long *)sector;
  691. switch(info->flash_id & FLASH_TYPEMASK) {
  692. case FLASH_28F320J3A:
  693. case FLASH_28F640J3A:
  694. case FLASH_28F128J3A:
  695. /* Disable interrupts which might cause Flash to timeout */
  696. flag = disable_interrupts();
  697. /* Issue command */
  698. *addr = 0x00500050L; /* Clear the status register */
  699. *addr = 0x00600060L; /* Set lock bit setup */
  700. *addr = 0x00010001L; /* Set lock bit confirm */
  701. /* Wait for command completion */
  702. for (i = 0; i < 10; i++) { /* 75us timeout, wait 100us */
  703. udelay(10);
  704. if ((*addr & 0x00800080L) == 0x00800080L)
  705. break;
  706. }
  707. /* Not successful? */
  708. status = *addr;
  709. if (status != 0x00800080L) {
  710. printf("Protect %x sector failed: %x\n",
  711. (uint)sector, (uint)status);
  712. rcode = 1;
  713. }
  714. /* Restore read mode */
  715. *addr = 0x00ff00ffL;
  716. /* re-enable interrupts if necessary */
  717. if (flag)
  718. enable_interrupts();
  719. break;
  720. case FLASH_AM040: /* No soft sector protection */
  721. break;
  722. }
  723. /* Turn protection on for this sector */
  724. for (i = 0; i < info->sector_count; i++) {
  725. if (info->start[i] == sector) {
  726. info->protect[i] = 1;
  727. break;
  728. }
  729. }
  730. return rcode;
  731. } /* end _flash_protect() */
  732. static int _flash_unprotect(flash_info_t *info, long sector)
  733. {
  734. int i;
  735. int flag;
  736. ulong status;
  737. int rcode = 0;
  738. volatile long *addr = (unsigned long *)sector;
  739. switch(info->flash_id & FLASH_TYPEMASK) {
  740. case FLASH_28F320J3A:
  741. case FLASH_28F640J3A:
  742. case FLASH_28F128J3A:
  743. /* Disable interrupts which might cause Flash to timeout */
  744. flag = disable_interrupts();
  745. *addr = 0x00500050L; /* Clear the status register */
  746. *addr = 0x00600060L; /* Clear lock bit setup */
  747. *addr = 0x00D000D0L; /* Clear lock bit confirm */
  748. /* Wait for command completion */
  749. for (i = 0; i < 80 ; i++) { /* 700ms timeout, wait 800 */
  750. udelay(10000); /* Delay 10ms */
  751. if ((*addr & 0x00800080L) == 0x00800080L)
  752. break;
  753. }
  754. /* Not successful? */
  755. status = *addr;
  756. if (status != 0x00800080L) {
  757. printf("Un-protect %x sector failed: %x\n",
  758. (uint)sector, (uint)status);
  759. *addr = 0x00ff00ffL;
  760. rcode = 1;
  761. }
  762. /* restore read mode */
  763. *addr = 0x00ff00ffL;
  764. /* re-enable interrupts if necessary */
  765. if (flag)
  766. enable_interrupts();
  767. break;
  768. case FLASH_AM040: /* No soft sector protection */
  769. break;
  770. }
  771. /*
  772. * Fix Intel's little red wagon. Reprotect
  773. * sectors that were protected before we undid
  774. * protection on a specific sector.
  775. */
  776. for (i = 0; i < info->sector_count; i++) {
  777. if (info->start[i] != sector) {
  778. if (info->protect[i]) {
  779. if (_flash_protect(info, info->start[i]))
  780. rcode = 1;
  781. }
  782. }
  783. else /* Turn protection off for this sector */
  784. info->protect[i] = 0;
  785. }
  786. return rcode;
  787. } /* end _flash_unprotect() */
  788. int flash_real_protect(flash_info_t *info, long sector, int prot)
  789. {
  790. int rcode;
  791. if (prot)
  792. rcode = _flash_protect(info, info->start[sector]);
  793. else
  794. rcode = _flash_unprotect(info, info->start[sector]);
  795. return rcode;
  796. } /* end flash_real_protect() */
  797. /*-----------------------------------------------------------------------
  798. */