flash.c 13 KB

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