flash.c 14 KB

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