flash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * (C) Copyright 2001-2005
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Flash Routines for Intel devices
  6. *
  7. *--------------------------------------------------------------------
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <mpc8xx.h>
  28. #include "cpu87.h"
  29. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  30. /*-----------------------------------------------------------------------
  31. */
  32. ulong flash_int_get_size (volatile unsigned long *baseaddr,
  33. flash_info_t * info)
  34. {
  35. short i;
  36. unsigned long flashtest_h, flashtest_l;
  37. info->sector_count = info->size = 0;
  38. info->flash_id = FLASH_UNKNOWN;
  39. /* Write identify command sequence and test FLASH answer
  40. */
  41. baseaddr[0] = 0x00900090;
  42. baseaddr[1] = 0x00900090;
  43. flashtest_h = baseaddr[0]; /* manufacturer ID */
  44. flashtest_l = baseaddr[1];
  45. if (flashtest_h != INTEL_MANUFACT || flashtest_l != INTEL_MANUFACT)
  46. return (0); /* no or unknown flash */
  47. flashtest_h = baseaddr[2]; /* device ID */
  48. flashtest_l = baseaddr[3];
  49. if (flashtest_h != flashtest_l)
  50. return (0);
  51. switch (flashtest_h) {
  52. case INTEL_ID_28F160C3B:
  53. info->flash_id = FLASH_28F160C3B;
  54. info->sector_count = 39;
  55. info->size = 0x00800000; /* 4 * 2 MB = 8 MB */
  56. break;
  57. case INTEL_ID_28F160F3B:
  58. info->flash_id = FLASH_28F160F3B;
  59. info->sector_count = 39;
  60. info->size = 0x00800000; /* 4 * 2 MB = 8 MB */
  61. break;
  62. case INTEL_ID_28F640C3B:
  63. info->flash_id = FLASH_28F640C3B;
  64. info->sector_count = 135;
  65. info->size = 0x02000000; /* 16 * 2 MB = 32 MB */
  66. break;
  67. default:
  68. return (0); /* no or unknown flash */
  69. }
  70. info->flash_id |= INTEL_MANUFACT << 16; /* set manufacturer offset */
  71. if (info->flash_id & FLASH_BTYPE) {
  72. volatile unsigned long *tmp = baseaddr;
  73. /* set up sector start adress table (bottom sector type)
  74. * AND unlock the sectors (if our chip is 160C3)
  75. */
  76. for (i = 0; i < info->sector_count; i++) {
  77. if (((info->flash_id & FLASH_TYPEMASK) == FLASH_28F160C3B) ||
  78. ((info->flash_id & FLASH_TYPEMASK) == FLASH_28F640C3B)) {
  79. tmp[0] = 0x00600060;
  80. tmp[1] = 0x00600060;
  81. tmp[0] = 0x00D000D0;
  82. tmp[1] = 0x00D000D0;
  83. }
  84. info->start[i] = (uint) tmp;
  85. tmp += i < 8 ? 0x2000 : 0x10000; /* pointer arith */
  86. }
  87. }
  88. memset (info->protect, 0, info->sector_count);
  89. baseaddr[0] = 0x00FF00FF;
  90. baseaddr[1] = 0x00FF00FF;
  91. return (info->size);
  92. }
  93. static ulong flash_amd_get_size (vu_char *addr, flash_info_t *info)
  94. {
  95. short i;
  96. uchar vendor, devid;
  97. ulong base = (ulong)addr;
  98. /* Write auto select command: read Manufacturer ID */
  99. addr[0x0555] = 0xAA;
  100. addr[0x02AA] = 0x55;
  101. addr[0x0555] = 0x90;
  102. udelay(1000);
  103. vendor = addr[0];
  104. devid = addr[1] & 0xff;
  105. /* only support AMD */
  106. if (vendor != 0x01) {
  107. return 0;
  108. }
  109. vendor &= 0xf;
  110. devid &= 0xff;
  111. if (devid == AMD_ID_F040B) {
  112. info->flash_id = vendor << 16 | devid;
  113. info->sector_count = 8;
  114. info->size = info->sector_count * 0x10000;
  115. }
  116. else if (devid == AMD_ID_F080B) {
  117. info->flash_id = vendor << 16 | devid;
  118. info->sector_count = 16;
  119. info->size = 4 * info->sector_count * 0x10000;
  120. }
  121. else if (devid == AMD_ID_F016D) {
  122. info->flash_id = vendor << 16 | devid;
  123. info->sector_count = 32;
  124. info->size = 4 * info->sector_count * 0x10000;
  125. }
  126. else {
  127. printf ("## Unknown Flash Type: %02x\n", devid);
  128. return 0;
  129. }
  130. /* check for protected sectors */
  131. for (i = 0; i < info->sector_count; i++) {
  132. /* sector base address */
  133. info->start[i] = base + i * (info->size / info->sector_count);
  134. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  135. /* D0 = 1 if protected */
  136. addr = (volatile unsigned char *)(info->start[i]);
  137. info->protect[i] = addr[2] & 1;
  138. }
  139. /*
  140. * Prevent writes to uninitialized FLASH.
  141. */
  142. if (info->flash_id != FLASH_UNKNOWN) {
  143. addr = (vu_char *)info->start[0];
  144. addr[0] = 0xF0; /* reset bank */
  145. }
  146. return (info->size);
  147. }
  148. /*-----------------------------------------------------------------------
  149. */
  150. unsigned long flash_init (void)
  151. {
  152. unsigned long size_b0 = 0;
  153. unsigned long size_b1 = 0;
  154. int i;
  155. /* Init: no FLASHes known
  156. */
  157. for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
  158. flash_info[i].flash_id = FLASH_UNKNOWN;
  159. }
  160. /* Disable flash protection */
  161. CPU86_BCR |= (CPU86_BCR_FWPT | CPU86_BCR_FWRE);
  162. /* Static FLASH Bank configuration here (only one bank) */
  163. size_b0 = flash_int_get_size ((ulong *) CFG_FLASH_BASE, &flash_info[0]);
  164. size_b1 = flash_amd_get_size ((uchar *) CFG_BOOTROM_BASE, &flash_info[1]);
  165. if (size_b0 > 0 || size_b1 > 0) {
  166. printf("(");
  167. if (size_b0 > 0) {
  168. puts ("Bank#1 - ");
  169. print_size (size_b0, (size_b1 > 0) ? ", " : ") ");
  170. }
  171. if (size_b1 > 0) {
  172. puts ("Bank#2 - ");
  173. print_size (size_b1, ") ");
  174. }
  175. }
  176. else {
  177. printf ("## No FLASH found.\n");
  178. return 0;
  179. }
  180. /* protect monitor and environment sectors
  181. */
  182. #if CFG_MONITOR_BASE >= CFG_BOOTROM_BASE
  183. if (size_b1) {
  184. /* If U-Boot is booted from ROM the CFG_MONITOR_BASE > CFG_FLASH_BASE
  185. * but we shouldn't protect it.
  186. */
  187. flash_protect (FLAG_PROTECT_SET,
  188. CFG_MONITOR_BASE,
  189. CFG_MONITOR_BASE + monitor_flash_len - 1, &flash_info[1]
  190. );
  191. }
  192. #else
  193. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  194. flash_protect (FLAG_PROTECT_SET,
  195. CFG_MONITOR_BASE,
  196. CFG_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]
  197. );
  198. #endif
  199. #endif
  200. #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CFG_ENV_ADDR)
  201. # ifndef CFG_ENV_SIZE
  202. # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
  203. # endif
  204. # if CFG_ENV_ADDR >= CFG_BOOTROM_BASE
  205. if (size_b1) {
  206. flash_protect (FLAG_PROTECT_SET,
  207. CFG_ENV_ADDR,
  208. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[1]);
  209. }
  210. # else
  211. flash_protect (FLAG_PROTECT_SET,
  212. CFG_ENV_ADDR,
  213. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
  214. # endif
  215. #endif
  216. return (size_b0 + size_b1);
  217. }
  218. /*-----------------------------------------------------------------------
  219. */
  220. void flash_print_info (flash_info_t * info)
  221. {
  222. int i;
  223. if (info->flash_id == FLASH_UNKNOWN) {
  224. printf ("missing or unknown FLASH type\n");
  225. return;
  226. }
  227. switch ((info->flash_id >> 16) & 0xff) {
  228. case 0x89:
  229. printf ("INTEL ");
  230. break;
  231. case 0x1:
  232. printf ("AMD ");
  233. break;
  234. default:
  235. printf ("Unknown Vendor ");
  236. break;
  237. }
  238. switch (info->flash_id & FLASH_TYPEMASK) {
  239. case FLASH_28F160C3B:
  240. printf ("28F160C3B (16 Mbit, bottom sector)\n");
  241. break;
  242. case FLASH_28F160F3B:
  243. printf ("28F160F3B (16 Mbit, bottom sector)\n");
  244. break;
  245. case FLASH_28F640C3B:
  246. printf ("28F640C3B (64 M, bottom sector)\n");
  247. break;
  248. case AMD_ID_F040B:
  249. printf ("AM29F040B (4 Mbit)\n");
  250. break;
  251. default:
  252. printf ("Unknown Chip Type\n");
  253. break;
  254. }
  255. if (info->size < 0x100000)
  256. printf (" Size: %ld KB in %d Sectors\n",
  257. info->size >> 10, info->sector_count);
  258. else
  259. printf (" Size: %ld MB in %d Sectors\n",
  260. info->size >> 20, info->sector_count);
  261. printf (" Sector Start Addresses:");
  262. for (i = 0; i < info->sector_count; ++i) {
  263. if ((i % 5) == 0)
  264. printf ("\n ");
  265. printf (" %08lX%s",
  266. info->start[i],
  267. info->protect[i] ? " (RO)" : " "
  268. );
  269. }
  270. printf ("\n");
  271. }
  272. /*-----------------------------------------------------------------------
  273. */
  274. int flash_erase (flash_info_t * info, int s_first, int s_last)
  275. {
  276. vu_char *addr = (vu_char *)(info->start[0]);
  277. int flag, prot, sect, l_sect;
  278. ulong start, now, last;
  279. if ((s_first < 0) || (s_first > s_last)) {
  280. if (info->flash_id == FLASH_UNKNOWN) {
  281. printf ("- missing\n");
  282. } else {
  283. printf ("- no sectors to erase\n");
  284. }
  285. return 1;
  286. }
  287. prot = 0;
  288. for (sect = s_first; sect <= s_last; sect++) {
  289. if (info->protect[sect])
  290. prot++;
  291. }
  292. if (prot) {
  293. printf ("- Warning: %d protected sectors will not be erased!\n",
  294. prot);
  295. } else {
  296. printf ("\n");
  297. }
  298. /* Check the type of erased flash
  299. */
  300. if (info->flash_id >> 16 == 0x1) {
  301. /* Erase AMD flash
  302. */
  303. l_sect = -1;
  304. /* Disable interrupts which might cause a timeout here */
  305. flag = disable_interrupts();
  306. addr[0x0555] = 0xAA;
  307. addr[0x02AA] = 0x55;
  308. addr[0x0555] = 0x80;
  309. addr[0x0555] = 0xAA;
  310. addr[0x02AA] = 0x55;
  311. /* wait at least 80us - let's wait 1 ms */
  312. udelay (1000);
  313. /* Start erase on unprotected sectors */
  314. for (sect = s_first; sect<=s_last; sect++) {
  315. if (info->protect[sect] == 0) { /* not protected */
  316. addr = (vu_char *)(info->start[sect]);
  317. addr[0] = 0x30;
  318. l_sect = sect;
  319. }
  320. }
  321. /* re-enable interrupts if necessary */
  322. if (flag)
  323. enable_interrupts();
  324. /* wait at least 80us - let's wait 1 ms */
  325. udelay (1000);
  326. /*
  327. * We wait for the last triggered sector
  328. */
  329. if (l_sect < 0)
  330. goto AMD_DONE;
  331. start = get_timer (0);
  332. last = start;
  333. addr = (vu_char *)(info->start[l_sect]);
  334. while ((addr[0] & 0x80) != 0x80) {
  335. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  336. printf ("Timeout\n");
  337. return 1;
  338. }
  339. /* show that we're waiting */
  340. if ((now - last) > 1000) { /* every second */
  341. serial_putc ('.');
  342. last = now;
  343. }
  344. }
  345. AMD_DONE:
  346. /* reset to read mode */
  347. addr = (volatile unsigned char *)info->start[0];
  348. addr[0] = 0xF0; /* reset bank */
  349. } else {
  350. /* Erase Intel flash
  351. */
  352. /* Start erase on unprotected sectors
  353. */
  354. for (sect = s_first; sect <= s_last; sect++) {
  355. volatile ulong *addr =
  356. (volatile unsigned long *) info->start[sect];
  357. start = get_timer (0);
  358. last = start;
  359. if (info->protect[sect] == 0) {
  360. /* Disable interrupts which might cause a timeout here
  361. */
  362. flag = disable_interrupts ();
  363. /* Erase the block
  364. */
  365. addr[0] = 0x00200020;
  366. addr[1] = 0x00200020;
  367. addr[0] = 0x00D000D0;
  368. addr[1] = 0x00D000D0;
  369. /* re-enable interrupts if necessary
  370. */
  371. if (flag)
  372. enable_interrupts ();
  373. /* wait at least 80us - let's wait 1 ms
  374. */
  375. udelay (1000);
  376. last = start;
  377. while ((addr[0] & 0x00800080) != 0x00800080 ||
  378. (addr[1] & 0x00800080) != 0x00800080) {
  379. if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
  380. printf ("Timeout (erase suspended!)\n");
  381. /* Suspend erase
  382. */
  383. addr[0] = 0x00B000B0;
  384. addr[1] = 0x00B000B0;
  385. goto DONE;
  386. }
  387. /* show that we're waiting
  388. */
  389. if ((now - last) > 1000) { /* every second */
  390. serial_putc ('.');
  391. last = now;
  392. }
  393. }
  394. if (addr[0] & 0x00220022 || addr[1] & 0x00220022) {
  395. printf ("*** ERROR: erase failed!\n");
  396. goto DONE;
  397. }
  398. }
  399. /* Clear status register and reset to read mode
  400. */
  401. addr[0] = 0x00500050;
  402. addr[1] = 0x00500050;
  403. addr[0] = 0x00FF00FF;
  404. addr[1] = 0x00FF00FF;
  405. }
  406. }
  407. printf (" done\n");
  408. DONE:
  409. return 0;
  410. }
  411. static int write_word (flash_info_t *, volatile unsigned long *, ulong);
  412. static int write_byte (flash_info_t *info, ulong dest, uchar data);
  413. /*-----------------------------------------------------------------------
  414. * Copy memory to flash, returns:
  415. * 0 - OK
  416. * 1 - write timeout
  417. * 2 - Flash not erased
  418. */
  419. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  420. {
  421. ulong v;
  422. int i, l, rc, cc = cnt, res = 0;
  423. if (info->flash_id >> 16 == 0x1) {
  424. /* Write to AMD 8-bit flash
  425. */
  426. while (cnt > 0) {
  427. if ((rc = write_byte(info, addr, *src)) != 0) {
  428. return (rc);
  429. }
  430. addr++;
  431. src++;
  432. cnt--;
  433. }
  434. return (0);
  435. } else {
  436. /* Write to Intel 64-bit flash
  437. */
  438. for (v=0; cc > 0; addr += 4, cc -= 4 - l) {
  439. l = (addr & 3);
  440. addr &= ~3;
  441. for (i = 0; i < 4; i++) {
  442. v = (v << 8) + (i < l || i - l >= cc ?
  443. *((unsigned char *) addr + i) : *src++);
  444. }
  445. if ((res = write_word (info, (volatile unsigned long *) addr, v)) != 0)
  446. break;
  447. }
  448. }
  449. return (res);
  450. }
  451. /*-----------------------------------------------------------------------
  452. * Write a word to Flash, returns:
  453. * 0 - OK
  454. * 1 - write timeout
  455. * 2 - Flash not erased
  456. */
  457. static int write_word (flash_info_t * info, volatile unsigned long *addr,
  458. ulong data)
  459. {
  460. int flag, res = 0;
  461. ulong start;
  462. /* Check if Flash is (sufficiently) erased
  463. */
  464. if ((*addr & data) != data)
  465. return (2);
  466. /* Disable interrupts which might cause a timeout here
  467. */
  468. flag = disable_interrupts ();
  469. *addr = 0x00400040;
  470. *addr = data;
  471. /* re-enable interrupts if necessary
  472. */
  473. if (flag)
  474. enable_interrupts ();
  475. start = get_timer (0);
  476. while ((*addr & 0x00800080) != 0x00800080) {
  477. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  478. /* Suspend program
  479. */
  480. *addr = 0x00B000B0;
  481. res = 1;
  482. goto OUT;
  483. }
  484. }
  485. if (*addr & 0x00220022) {
  486. printf ("*** ERROR: program failed!\n");
  487. res = 1;
  488. }
  489. OUT:
  490. /* Clear status register and reset to read mode
  491. */
  492. *addr = 0x00500050;
  493. *addr = 0x00FF00FF;
  494. return (res);
  495. }
  496. /*-----------------------------------------------------------------------
  497. * Write a byte to Flash, returns:
  498. * 0 - OK
  499. * 1 - write timeout
  500. * 2 - Flash not erased
  501. */
  502. static int write_byte (flash_info_t *info, ulong dest, uchar data)
  503. {
  504. vu_char *addr = (vu_char *)(info->start[0]);
  505. ulong start;
  506. int flag;
  507. /* Check if Flash is (sufficiently) erased */
  508. if ((*((vu_char *)dest) & data) != data) {
  509. return (2);
  510. }
  511. /* Disable interrupts which might cause a timeout here */
  512. flag = disable_interrupts();
  513. addr[0x0555] = 0xAA;
  514. addr[0x02AA] = 0x55;
  515. addr[0x0555] = 0xA0;
  516. *((vu_char *)dest) = data;
  517. /* re-enable interrupts if necessary */
  518. if (flag)
  519. enable_interrupts();
  520. /* data polling for D7 */
  521. start = get_timer (0);
  522. while ((*((vu_char *)dest) & 0x80) != (data & 0x80)) {
  523. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  524. return (1);
  525. }
  526. }
  527. return (0);
  528. }
  529. /*-----------------------------------------------------------------------
  530. */