flash.c 22 KB

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