flash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * (C) Copyright 2002-2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2002 Jun Gu <jung@artesyncp.com>
  6. * Add support for Am29F016D and dynamic switch setting.
  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. /*
  27. * Modified 4/5/2001
  28. * Wait for completion of each sector erase command issued
  29. * 4/5/2001
  30. * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
  31. */
  32. /*
  33. * Ported to XPedite1000, 1/2 mb boot flash only
  34. * Travis B. Sawyer, <travis.sawyer@sandburst.com>
  35. */
  36. #include <common.h>
  37. #include <ppc4xx.h>
  38. #include <asm/processor.h>
  39. #undef DEBUG
  40. #ifdef DEBUG
  41. #define DEBUGF(x...) printf(x)
  42. #else
  43. #define DEBUGF(x...)
  44. #endif /* DEBUG */
  45. #define BOOT_SMALL_FLASH 32 /* 00100000 */
  46. #define FLASH_ONBD_N 2 /* 00000010 */
  47. #define FLASH_SRAM_SEL 1 /* 00000001 */
  48. #define BOOT_SMALL_FLASH_VAL 4
  49. #define FLASH_ONBD_N_VAL 2
  50. #define FLASH_SRAM_SEL_VAL 1
  51. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  52. static unsigned long flash_addr_table[8][CONFIG_SYS_MAX_FLASH_BANKS] = {
  53. {0xfff80000}, /* 0:000: configuraton 3 */
  54. {0xfff90000}, /* 1:001: configuraton 4 */
  55. {0xfffa0000}, /* 2:010: configuraton 7 */
  56. {0xfffb0000}, /* 3:011: configuraton 8 */
  57. {0xfffc0000}, /* 4:100: configuraton 1 */
  58. {0xfffd0000}, /* 5:101: configuraton 2 */
  59. {0xfffe0000}, /* 6:110: configuraton 5 */
  60. {0xffff0000} /* 7:111: configuraton 6 */
  61. };
  62. /*-----------------------------------------------------------------------
  63. * Functions
  64. */
  65. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  66. static int write_word (flash_info_t *info, ulong dest, ulong data);
  67. #ifdef CONFIG_XPEDITE1K
  68. #define ADDR0 0x5555
  69. #define ADDR1 0x2aaa
  70. #define FLASH_WORD_SIZE unsigned char
  71. #endif
  72. /*-----------------------------------------------------------------------
  73. */
  74. unsigned long flash_init (void)
  75. {
  76. unsigned long total_b = 0;
  77. unsigned long size_b[CONFIG_SYS_MAX_FLASH_BANKS];
  78. unsigned short index = 0;
  79. int i;
  80. DEBUGF("\n");
  81. DEBUGF("FLASH: Index: %d\n", index);
  82. /* Init: no FLASHes known */
  83. for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  84. flash_info[i].flash_id = FLASH_UNKNOWN;
  85. flash_info[i].sector_count = -1;
  86. flash_info[i].size = 0;
  87. /* check whether the address is 0 */
  88. if (flash_addr_table[index][i] == 0) {
  89. continue;
  90. }
  91. /* call flash_get_size() to initialize sector address */
  92. size_b[i] = flash_get_size(
  93. (vu_long *)flash_addr_table[index][i], &flash_info[i]);
  94. flash_info[i].size = size_b[i];
  95. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  96. printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
  97. i, size_b[i], size_b[i]<<20);
  98. flash_info[i].sector_count = -1;
  99. flash_info[i].size = 0;
  100. }
  101. total_b += flash_info[i].size;
  102. }
  103. return total_b;
  104. }
  105. /*-----------------------------------------------------------------------
  106. */
  107. void flash_print_info (flash_info_t *info)
  108. {
  109. int i;
  110. int k;
  111. int size;
  112. int erased;
  113. volatile unsigned long *flash;
  114. if (info->flash_id == FLASH_UNKNOWN) {
  115. printf ("missing or unknown FLASH type\n");
  116. return;
  117. }
  118. switch (info->flash_id & FLASH_VENDMASK) {
  119. case FLASH_MAN_AMD: printf ("AMD "); break;
  120. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  121. case FLASH_MAN_SST: printf ("SST "); break;
  122. default: printf ("Unknown Vendor "); break;
  123. }
  124. switch (info->flash_id & FLASH_TYPEMASK) {
  125. case FLASH_AMD016: printf ("AM29F016D (16 Mbit, uniform sector size)\n");
  126. break;
  127. case FLASH_AM040: printf ("AM29F040 (512 Kbit, uniform sector size)\n");
  128. break;
  129. case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  130. break;
  131. case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  132. break;
  133. case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  134. break;
  135. case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  136. break;
  137. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  138. break;
  139. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  140. break;
  141. case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  142. break;
  143. case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  144. break;
  145. case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
  146. break;
  147. case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
  148. break;
  149. default: printf ("Unknown Chip Type\n");
  150. break;
  151. }
  152. printf (" Size: %ld KB in %d Sectors\n",
  153. info->size >> 10, info->sector_count);
  154. printf (" Sector Start Addresses:");
  155. for (i=0; i<info->sector_count; ++i) {
  156. /*
  157. * Check if whole sector is erased
  158. */
  159. if (i != (info->sector_count-1))
  160. size = info->start[i+1] - info->start[i];
  161. else
  162. size = info->start[0] + info->size - info->start[i];
  163. erased = 1;
  164. flash = (volatile unsigned long *)info->start[i];
  165. size = size >> 2; /* divide by 4 for longword access */
  166. for (k=0; k<size; k++)
  167. {
  168. if (*flash++ != 0xffffffff)
  169. {
  170. erased = 0;
  171. break;
  172. }
  173. }
  174. if ((i % 5) == 0)
  175. printf ("\n ");
  176. printf (" %08lX%s%s",
  177. info->start[i],
  178. erased ? " E" : " ",
  179. info->protect[i] ? "RO " : " "
  180. );
  181. }
  182. printf ("\n");
  183. return;
  184. }
  185. /*-----------------------------------------------------------------------
  186. */
  187. /*-----------------------------------------------------------------------
  188. */
  189. /*
  190. * The following code cannot be run from FLASH!
  191. */
  192. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  193. {
  194. short i;
  195. FLASH_WORD_SIZE value;
  196. ulong base = (ulong)addr;
  197. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
  198. DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr );
  199. /* Write auto select command: read Manufacturer ID */
  200. udelay(10000);
  201. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  202. udelay(1000);
  203. addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  204. udelay(1000);
  205. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
  206. udelay(1000);
  207. #ifdef CONFIG_ADCIOP
  208. value = addr2[2];
  209. #else
  210. value = addr2[0];
  211. #endif
  212. DEBUGF("FLASH MANUFACT: %x\n", value);
  213. switch (value) {
  214. case (FLASH_WORD_SIZE)AMD_MANUFACT:
  215. info->flash_id = FLASH_MAN_AMD;
  216. break;
  217. case (FLASH_WORD_SIZE)FUJ_MANUFACT:
  218. info->flash_id = FLASH_MAN_FUJ;
  219. break;
  220. case (FLASH_WORD_SIZE)SST_MANUFACT:
  221. info->flash_id = FLASH_MAN_SST;
  222. break;
  223. case (FLASH_WORD_SIZE)STM_MANUFACT:
  224. info->flash_id = FLASH_MAN_STM;
  225. break;
  226. default:
  227. info->flash_id = FLASH_UNKNOWN;
  228. info->sector_count = 0;
  229. info->size = 0;
  230. return (0); /* no or unknown flash */
  231. }
  232. #ifdef CONFIG_ADCIOP
  233. value = addr2[0]; /* device ID */
  234. debug ("\ndev_code=%x\n", value);
  235. #else
  236. value = addr2[1]; /* device ID */
  237. #endif
  238. DEBUGF("\nFLASH DEVICEID: %x\n", value);
  239. switch (value) {
  240. case (FLASH_WORD_SIZE)AMD_ID_LV040B:
  241. info->flash_id += FLASH_AM040;
  242. info->sector_count = 8;
  243. info->size = 0x00080000; /* => 512 kb */
  244. break;
  245. default:
  246. info->flash_id = FLASH_UNKNOWN;
  247. return (0); /* => no or unknown flash */
  248. }
  249. /* set up sector start address table */
  250. if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
  251. (info->flash_id == FLASH_AM040) ||
  252. (info->flash_id == FLASH_AMD016)) {
  253. for (i = 0; i < info->sector_count; i++)
  254. info->start[i] = base + (i * 0x00010000);
  255. } else {
  256. if (info->flash_id & FLASH_BTYPE) {
  257. /* set sector offsets for bottom boot block type */
  258. info->start[0] = base + 0x00000000;
  259. info->start[1] = base + 0x00004000;
  260. info->start[2] = base + 0x00006000;
  261. info->start[3] = base + 0x00008000;
  262. for (i = 4; i < info->sector_count; i++) {
  263. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  264. }
  265. } else {
  266. /* set sector offsets for top boot block type */
  267. i = info->sector_count - 1;
  268. info->start[i--] = base + info->size - 0x00004000;
  269. info->start[i--] = base + info->size - 0x00006000;
  270. info->start[i--] = base + info->size - 0x00008000;
  271. for (; i >= 0; i--) {
  272. info->start[i] = base + i * 0x00010000;
  273. }
  274. }
  275. }
  276. /* check for protected sectors */
  277. for (i = 0; i < info->sector_count; i++) {
  278. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  279. /* D0 = 1 if protected */
  280. #ifdef CONFIG_ADCIOP
  281. addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
  282. info->protect[i] = addr2[4] & 1;
  283. #else
  284. addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
  285. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
  286. info->protect[i] = 0;
  287. else
  288. info->protect[i] = addr2[2] & 1;
  289. #endif
  290. }
  291. /*
  292. * Prevent writes to uninitialized FLASH.
  293. */
  294. if (info->flash_id != FLASH_UNKNOWN) {
  295. #if 0 /* test-only */
  296. #ifdef CONFIG_ADCIOP
  297. addr2 = (volatile unsigned char *)info->start[0];
  298. addr2[ADDR0] = 0xAA;
  299. addr2[ADDR1] = 0x55;
  300. addr2[ADDR0] = 0xF0; /* reset bank */
  301. #else
  302. addr2 = (FLASH_WORD_SIZE *)info->start[0];
  303. *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  304. #endif
  305. #else /* test-only */
  306. addr2 = (FLASH_WORD_SIZE *)info->start[0];
  307. *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  308. #endif /* test-only */
  309. }
  310. return (info->size);
  311. }
  312. int wait_for_DQ7(flash_info_t *info, int sect)
  313. {
  314. ulong start, now, last;
  315. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
  316. start = get_timer (0);
  317. last = start;
  318. while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
  319. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  320. printf ("Timeout\n");
  321. return -1;
  322. }
  323. /* show that we're waiting */
  324. if ((now - last) > 1000) { /* every second */
  325. putc ('.');
  326. last = now;
  327. }
  328. }
  329. return 0;
  330. }
  331. /*-----------------------------------------------------------------------
  332. */
  333. int flash_erase (flash_info_t *info, int s_first, int s_last)
  334. {
  335. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
  336. volatile FLASH_WORD_SIZE *addr2;
  337. int flag, prot, sect, l_sect;
  338. int i;
  339. if ((s_first < 0) || (s_first > s_last)) {
  340. if (info->flash_id == FLASH_UNKNOWN) {
  341. printf ("- missing\n");
  342. } else {
  343. printf ("- no sectors to erase\n");
  344. }
  345. return 1;
  346. }
  347. if (info->flash_id == FLASH_UNKNOWN) {
  348. printf ("Can't erase unknown flash type - aborted\n");
  349. return 1;
  350. }
  351. prot = 0;
  352. for (sect=s_first; sect<=s_last; ++sect) {
  353. if (info->protect[sect]) {
  354. prot++;
  355. }
  356. }
  357. if (prot) {
  358. printf ("- Warning: %d protected sectors will not be erased!\n",
  359. prot);
  360. } else {
  361. printf ("\n");
  362. }
  363. l_sect = -1;
  364. /* Disable interrupts which might cause a timeout here */
  365. flag = disable_interrupts();
  366. /* Start erase on unprotected sectors */
  367. for (sect = s_first; sect<=s_last; sect++) {
  368. if (info->protect[sect] == 0) { /* not protected */
  369. addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
  370. printf("Erasing sector %p\n", addr2);
  371. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  372. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  373. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  374. addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
  375. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  376. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  377. addr2[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
  378. for (i=0; i<50; i++)
  379. udelay(1000); /* wait 1 ms */
  380. } else {
  381. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  382. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  383. addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
  384. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  385. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  386. addr2[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
  387. }
  388. l_sect = sect;
  389. /*
  390. * Wait for each sector to complete, it's more
  391. * reliable. According to AMD Spec, you must
  392. * issue all erase commands within a specified
  393. * timeout. This has been seen to fail, especially
  394. * if printf()s are included (for debug)!!
  395. */
  396. wait_for_DQ7(info, sect);
  397. }
  398. }
  399. /* re-enable interrupts if necessary */
  400. if (flag)
  401. enable_interrupts();
  402. /* wait at least 80us - let's wait 1 ms */
  403. udelay (1000);
  404. #if 0
  405. /*
  406. * We wait for the last triggered sector
  407. */
  408. if (l_sect < 0)
  409. goto DONE;
  410. wait_for_DQ7(info, l_sect);
  411. DONE:
  412. #endif
  413. /* reset to read mode */
  414. addr = (FLASH_WORD_SIZE *)info->start[0];
  415. addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  416. printf (" done\n");
  417. return 0;
  418. }
  419. /*-----------------------------------------------------------------------
  420. * Copy memory to flash, returns:
  421. * 0 - OK
  422. * 1 - write timeout
  423. * 2 - Flash not erased
  424. */
  425. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  426. {
  427. ulong cp, wp, data;
  428. int i, l, rc;
  429. wp = (addr & ~3); /* get lower word aligned address */
  430. /*
  431. * handle unaligned start bytes
  432. */
  433. if ((l = addr - wp) != 0) {
  434. data = 0;
  435. for (i=0, cp=wp; i<l; ++i, ++cp) {
  436. data = (data << 8) | (*(uchar *)cp);
  437. }
  438. for (; i<4 && cnt>0; ++i) {
  439. data = (data << 8) | *src++;
  440. --cnt;
  441. ++cp;
  442. }
  443. for (; cnt==0 && i<4; ++i, ++cp) {
  444. data = (data << 8) | (*(uchar *)cp);
  445. }
  446. if ((rc = write_word(info, wp, data)) != 0) {
  447. return (rc);
  448. }
  449. wp += 4;
  450. }
  451. /*
  452. * handle word aligned part
  453. */
  454. while (cnt >= 4) {
  455. data = 0;
  456. for (i=0; i<4; ++i) {
  457. data = (data << 8) | *src++;
  458. }
  459. if ((rc = write_word(info, wp, data)) != 0) {
  460. return (rc);
  461. }
  462. wp += 4;
  463. cnt -= 4;
  464. }
  465. if (cnt == 0) {
  466. return (0);
  467. }
  468. /*
  469. * handle unaligned tail bytes
  470. */
  471. data = 0;
  472. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  473. data = (data << 8) | *src++;
  474. --cnt;
  475. }
  476. for (; i<4; ++i, ++cp) {
  477. data = (data << 8) | (*(uchar *)cp);
  478. }
  479. return (write_word(info, wp, data));
  480. }
  481. /*-----------------------------------------------------------------------
  482. * Write a word to Flash, returns:
  483. * 0 - OK
  484. * 1 - write timeout
  485. * 2 - Flash not erased
  486. */
  487. static int write_word (flash_info_t * info, ulong dest, ulong data)
  488. {
  489. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) (info->start[0]);
  490. volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
  491. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
  492. ulong start;
  493. int i;
  494. /* Check if Flash is (sufficiently) erased */
  495. if ((*((volatile FLASH_WORD_SIZE *) dest) &
  496. (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
  497. return (2);
  498. }
  499. for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
  500. int flag;
  501. /* Disable interrupts which might cause a timeout here */
  502. flag = disable_interrupts ();
  503. addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
  504. addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
  505. addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00A000A0;
  506. dest2[i] = data2[i];
  507. /* re-enable interrupts if necessary */
  508. if (flag)
  509. enable_interrupts ();
  510. /* data polling for D7 */
  511. start = get_timer (0);
  512. while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
  513. (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
  514. if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  515. return (1);
  516. }
  517. }
  518. }
  519. return (0);
  520. }
  521. /*-----------------------------------------------------------------------
  522. */