flash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * (C) Copyright 2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Modified 4/5/2001
  25. * Wait for completion of each sector erase command issued
  26. * 4/5/2001
  27. * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
  28. */
  29. #include <asm/types.h>
  30. #include <asm/u-boot.h>
  31. #include <asm/processor.h>
  32. #include <ppc4xx.h>
  33. #include <common.h>
  34. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  35. /*-----------------------------------------------------------------------
  36. * Functions
  37. */
  38. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  39. static int write_word (flash_info_t *info, ulong dest, ulong data);
  40. #ifdef MEIGSBOARD_ONBOARD_FLASH /* onboard = 2MB */
  41. # ifdef CONFIG_EXBITGEN
  42. # define FLASH_WORD_SIZE unsigned long
  43. # endif
  44. #else /* Meigsboard socket flash = 512KB */
  45. # ifdef CONFIG_EXBITGEN
  46. # define FLASH_WORD_SIZE unsigned char
  47. # endif
  48. #endif
  49. #ifdef CONFIG_EXBITGEN
  50. #define ADDR0 0x5555
  51. #define ADDR1 0x2aaa
  52. #define FLASH_WORD_SIZE unsigned char
  53. #endif
  54. /*-----------------------------------------------------------------------
  55. */
  56. unsigned long flash_init (void)
  57. {
  58. unsigned long bank_size;
  59. unsigned long tot_size;
  60. unsigned long bank_addr;
  61. int i;
  62. /* Init: no FLASHes known */
  63. for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
  64. flash_info[i].flash_id = FLASH_UNKNOWN;
  65. flash_info[i].size = 0;
  66. }
  67. tot_size = 0;
  68. /* Detect Boot Flash */
  69. bank_addr = CFG_FLASH0_BASE;
  70. bank_size = flash_get_size((vu_long *)bank_addr, &flash_info[0]);
  71. if (bank_size > 0) {
  72. (void)flash_protect(FLAG_PROTECT_CLEAR,
  73. bank_addr,
  74. bank_addr + bank_size - 1,
  75. &flash_info[0]);
  76. }
  77. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  78. printf ("## Unknown FLASH on Boot Flash Bank\n");
  79. }
  80. flash_info[0].size = bank_size;
  81. tot_size += bank_size;
  82. /* Detect Application Flash */
  83. bank_addr = CFG_FLASH1_BASE;
  84. for (i = 1; i < CFG_MAX_FLASH_BANKS; ++i) {
  85. bank_size = flash_get_size((vu_long *)bank_addr, &flash_info[i]);
  86. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  87. break;
  88. }
  89. if (bank_size > 0) {
  90. (void)flash_protect(FLAG_PROTECT_CLEAR,
  91. bank_addr,
  92. bank_addr + bank_size - 1,
  93. &flash_info[i]);
  94. }
  95. flash_info[i].size = bank_size;
  96. tot_size += bank_size;
  97. bank_addr += bank_size;
  98. }
  99. if (flash_info[1].flash_id == FLASH_UNKNOWN) {
  100. printf ("## Unknown FLASH on Application Flash Bank\n");
  101. }
  102. /* Protect monitor and environment sectors */
  103. #if CFG_MONITOR_BASE >= CFG_FLASH0_BASE
  104. flash_protect(FLAG_PROTECT_SET,
  105. CFG_MONITOR_BASE,
  106. CFG_MONITOR_BASE + monitor_flash_len - 1,
  107. &flash_info[0]);
  108. #if 0xfffffffc >= CFG_FLASH0_BASE
  109. #if 0xfffffffc <= CFG_FLASH0_BASE + CFG_FLASH0_SIZE - 1
  110. flash_protect(FLAG_PROTECT_SET,
  111. 0xfffffffc, 0xffffffff,
  112. &flash_info[0]);
  113. #endif
  114. #endif
  115. #endif
  116. #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
  117. flash_protect(FLAG_PROTECT_SET,
  118. CFG_ENV_ADDR,
  119. CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
  120. &flash_info[0]);
  121. #endif
  122. return tot_size;
  123. }
  124. /*-----------------------------------------------------------------------
  125. */
  126. void flash_print_info (flash_info_t *info)
  127. {
  128. int i;
  129. if (info->flash_id == FLASH_UNKNOWN) {
  130. printf ("missing or unknown FLASH type\n");
  131. return;
  132. }
  133. switch (info->flash_id & FLASH_VENDMASK) {
  134. case FLASH_MAN_AMD: printf ("AMD "); break;
  135. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  136. case FLASH_MAN_SST: printf ("SST "); break;
  137. default: printf ("Unknown Vendor "); break;
  138. }
  139. switch (info->flash_id & FLASH_TYPEMASK) {
  140. case FLASH_AM040: printf ("AM29F040 (512 Kbit, uniform sector size)\n");
  141. break;
  142. case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  143. break;
  144. case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  145. break;
  146. case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  147. break;
  148. case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  149. break;
  150. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  151. break;
  152. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  153. break;
  154. case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  155. break;
  156. case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  157. break;
  158. case FLASH_AMDLV033C: printf ("AM29LV033C (32 Mbit, uniform sector size)\n");
  159. break;
  160. case FLASH_AMDLV065D: printf ("AM29LV065D (64 Mbit, uniform sector size)\n");
  161. break;
  162. case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
  163. break;
  164. case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
  165. break;
  166. case FLASH_SST040: printf ("SST39LF/VF040 (4 Mbit, uniform sector size)\n");
  167. break;
  168. default: printf ("Unknown Chip Type\n");
  169. break;
  170. }
  171. printf (" Size: %ld KB in %d Sectors\n",
  172. info->size >> 10, info->sector_count);
  173. printf (" Sector Start Addresses:");
  174. for (i=0; i<info->sector_count; ++i) {
  175. if ((i % 5) == 0)
  176. printf ("\n ");
  177. printf (" %08lX%s",
  178. info->start[i],
  179. info->protect[i] ? " (RO)" : " "
  180. );
  181. }
  182. printf ("\n");
  183. }
  184. /*-----------------------------------------------------------------------
  185. */
  186. /*-----------------------------------------------------------------------
  187. */
  188. /*
  189. * The following code cannot be run from FLASH!
  190. */
  191. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  192. {
  193. short i;
  194. FLASH_WORD_SIZE value;
  195. ulong base = (ulong)addr;
  196. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
  197. /* Write auto select command: read Manufacturer ID */
  198. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  199. addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  200. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
  201. value = addr2[0];
  202. switch (value) {
  203. case (FLASH_WORD_SIZE)AMD_MANUFACT:
  204. info->flash_id = FLASH_MAN_AMD;
  205. break;
  206. case (FLASH_WORD_SIZE)FUJ_MANUFACT:
  207. info->flash_id = FLASH_MAN_FUJ;
  208. break;
  209. case (FLASH_WORD_SIZE)SST_MANUFACT:
  210. info->flash_id = FLASH_MAN_SST;
  211. break;
  212. default:
  213. info->flash_id = FLASH_UNKNOWN;
  214. info->sector_count = 0;
  215. info->size = 0;
  216. return (0); /* no or unknown flash */
  217. }
  218. value = addr2[1]; /* device ID */
  219. switch (value) {
  220. case (FLASH_WORD_SIZE)AMD_ID_F040B:
  221. info->flash_id += FLASH_AM040;
  222. info->sector_count = 8;
  223. info->size = 0x0080000; /* => 512 ko */
  224. break;
  225. case (FLASH_WORD_SIZE)AMD_ID_LV400T:
  226. info->flash_id += FLASH_AM400T;
  227. info->sector_count = 11;
  228. info->size = 0x00080000;
  229. break; /* => 0.5 MB */
  230. case (FLASH_WORD_SIZE)AMD_ID_LV400B:
  231. info->flash_id += FLASH_AM400B;
  232. info->sector_count = 11;
  233. info->size = 0x00080000;
  234. break; /* => 0.5 MB */
  235. case (FLASH_WORD_SIZE)AMD_ID_LV800T:
  236. info->flash_id += FLASH_AM800T;
  237. info->sector_count = 19;
  238. info->size = 0x00100000;
  239. break; /* => 1 MB */
  240. case (FLASH_WORD_SIZE)AMD_ID_LV800B:
  241. info->flash_id += FLASH_AM800B;
  242. info->sector_count = 19;
  243. info->size = 0x00100000;
  244. break; /* => 1 MB */
  245. case (FLASH_WORD_SIZE)AMD_ID_LV160T:
  246. info->flash_id += FLASH_AM160T;
  247. info->sector_count = 35;
  248. info->size = 0x00200000;
  249. break; /* => 2 MB */
  250. case (FLASH_WORD_SIZE)AMD_ID_LV160B:
  251. info->flash_id += FLASH_AM160B;
  252. info->sector_count = 35;
  253. info->size = 0x00200000;
  254. break; /* => 2 MB */
  255. case (FLASH_WORD_SIZE)AMD_ID_LV033C:
  256. info->flash_id += FLASH_AMDLV033C;
  257. info->sector_count = 64;
  258. info->size = 0x00400000;
  259. break; /* => 4 MB */
  260. case (FLASH_WORD_SIZE)AMD_ID_LV065D:
  261. info->flash_id += FLASH_AMDLV065D;
  262. info->sector_count = 128;
  263. info->size = 0x00800000;
  264. break; /* => 8 MB */
  265. case (FLASH_WORD_SIZE)AMD_ID_LV320T:
  266. info->flash_id += FLASH_AM320T;
  267. info->sector_count = 67;
  268. info->size = 0x00400000;
  269. break; /* => 4 MB */
  270. case (FLASH_WORD_SIZE)AMD_ID_LV320B:
  271. info->flash_id += FLASH_AM320B;
  272. info->sector_count = 67;
  273. info->size = 0x00400000;
  274. break; /* => 4 MB */
  275. case (FLASH_WORD_SIZE)SST_ID_xF800A:
  276. info->flash_id += FLASH_SST800A;
  277. info->sector_count = 16;
  278. info->size = 0x00100000;
  279. break; /* => 1 MB */
  280. case (FLASH_WORD_SIZE)SST_ID_xF160A:
  281. info->flash_id += FLASH_SST160A;
  282. info->sector_count = 32;
  283. info->size = 0x00200000;
  284. break; /* => 2 MB */
  285. case (FLASH_WORD_SIZE)SST_ID_xF040:
  286. info->flash_id += FLASH_SST040;
  287. info->sector_count = 128;
  288. info->size = 0x00080000;
  289. break; /* => 512KB */
  290. default:
  291. info->flash_id = FLASH_UNKNOWN;
  292. return (0); /* => no or unknown flash */
  293. }
  294. /* set up sector start address table */
  295. if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
  296. (info->flash_id == FLASH_AM040) ||
  297. (info->flash_id == FLASH_AMDLV033C) ||
  298. (info->flash_id == FLASH_AMDLV065D)) {
  299. ulong sectsize = info->size / info->sector_count;
  300. for (i = 0; i < info->sector_count; i++)
  301. info->start[i] = base + (i * sectsize);
  302. } else {
  303. if (info->flash_id & FLASH_BTYPE) {
  304. /* set sector offsets for bottom boot block type */
  305. info->start[0] = base + 0x00000000;
  306. info->start[1] = base + 0x00004000;
  307. info->start[2] = base + 0x00006000;
  308. info->start[3] = base + 0x00008000;
  309. for (i = 4; i < info->sector_count; i++) {
  310. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  311. }
  312. } else {
  313. /* set sector offsets for top boot block type */
  314. i = info->sector_count - 1;
  315. info->start[i--] = base + info->size - 0x00004000;
  316. info->start[i--] = base + info->size - 0x00006000;
  317. info->start[i--] = base + info->size - 0x00008000;
  318. for (; i >= 0; i--) {
  319. info->start[i] = base + i * 0x00010000;
  320. }
  321. }
  322. }
  323. /* check for protected sectors */
  324. for (i = 0; i < info->sector_count; i++) {
  325. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  326. /* D0 = 1 if protected */
  327. addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
  328. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
  329. info->protect[i] = 0;
  330. else
  331. info->protect[i] = addr2[2] & 1;
  332. }
  333. /* switch to the read mode */
  334. if (info->flash_id != FLASH_UNKNOWN) {
  335. addr2 = (FLASH_WORD_SIZE *)info->start[0];
  336. *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  337. }
  338. return (info->size);
  339. }
  340. /*-----------------------------------------------------------------------
  341. */
  342. int flash_erase (flash_info_t *info, int s_first, int s_last)
  343. {
  344. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
  345. volatile FLASH_WORD_SIZE *addr2;
  346. int flag, prot, sect;
  347. ulong start, now, last;
  348. if ((s_first < 0) || (s_first > s_last)) {
  349. if (info->flash_id == FLASH_UNKNOWN) {
  350. printf ("- missing\n");
  351. } else {
  352. printf ("- no sectors to erase\n");
  353. }
  354. return 1;
  355. }
  356. if (info->flash_id == FLASH_UNKNOWN) {
  357. printf ("Can't erase unknown flash type - aborted\n");
  358. return 1;
  359. }
  360. prot = 0;
  361. for (sect=s_first; sect<=s_last; ++sect) {
  362. if (info->protect[sect]) {
  363. prot++;
  364. }
  365. }
  366. if (prot) {
  367. printf ("- Warning: %d protected sectors will not be erased!\n",
  368. prot);
  369. } else {
  370. printf ("\n");
  371. }
  372. start = get_timer (0);
  373. last = start;
  374. /* Start erase on unprotected sectors */
  375. for (sect = s_first; sect <= s_last; sect++) {
  376. if (info->protect[sect] == 0) { /* not protected */
  377. addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
  378. /* Disable interrupts which might cause a timeout here */
  379. flag = disable_interrupts();
  380. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  381. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  382. addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
  383. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  384. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  385. addr2[0] = (FLASH_WORD_SIZE)0x00300030;
  386. /* re-enable interrupts if necessary */
  387. if (flag)
  388. enable_interrupts();
  389. /* wait at least 80us - let's wait 1 ms */
  390. udelay (1000);
  391. while ((addr2[0] & 0x00800080) !=
  392. (FLASH_WORD_SIZE) 0x00800080) {
  393. if ((now=get_timer(start)) >
  394. CFG_FLASH_ERASE_TOUT) {
  395. printf ("Timeout\n");
  396. addr[0] = (FLASH_WORD_SIZE)0x00F000F0;
  397. return 1;
  398. }
  399. /* show that we're waiting */
  400. if ((now - last) > 1000) { /* every second */
  401. putc ('.');
  402. last = now;
  403. }
  404. }
  405. addr[0] = (FLASH_WORD_SIZE)0x00F000F0;
  406. }
  407. }
  408. printf (" done\n");
  409. return 0;
  410. }
  411. /*-----------------------------------------------------------------------
  412. * Copy memory to flash, returns:
  413. * 0 - OK
  414. * 1 - write timeout
  415. * 2 - Flash not erased
  416. */
  417. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  418. {
  419. ulong cp, wp, data;
  420. int i, l, rc;
  421. wp = (addr & ~3); /* get lower word aligned address */
  422. /*
  423. * handle unaligned start bytes
  424. */
  425. if ((l = addr - wp) != 0) {
  426. data = 0;
  427. for (i=0, cp=wp; i<l; ++i, ++cp) {
  428. data = (data << 8) | (*(uchar *)cp);
  429. }
  430. for (; i<4 && cnt>0; ++i) {
  431. data = (data << 8) | *src++;
  432. --cnt;
  433. ++cp;
  434. }
  435. for (; cnt==0 && i<4; ++i, ++cp) {
  436. data = (data << 8) | (*(uchar *)cp);
  437. }
  438. if ((rc = write_word(info, wp, data)) != 0) {
  439. return (rc);
  440. }
  441. wp += 4;
  442. }
  443. /*
  444. * handle word aligned part
  445. */
  446. while (cnt >= 4) {
  447. data = 0;
  448. for (i=0; i<4; ++i) {
  449. data = (data << 8) | *src++;
  450. }
  451. if ((rc = write_word(info, wp, data)) != 0) {
  452. return (rc);
  453. }
  454. wp += 4;
  455. cnt -= 4;
  456. }
  457. if (cnt == 0) {
  458. return (0);
  459. }
  460. /*
  461. * handle unaligned tail bytes
  462. */
  463. data = 0;
  464. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  465. data = (data << 8) | *src++;
  466. --cnt;
  467. }
  468. for (; i<4; ++i, ++cp) {
  469. data = (data << 8) | (*(uchar *)cp);
  470. }
  471. return (write_word(info, wp, data));
  472. }
  473. /*-----------------------------------------------------------------------
  474. * Write a word to Flash, returns:
  475. * 0 - OK
  476. * 1 - write timeout
  477. * 2 - Flash not erased
  478. */
  479. static int write_word (flash_info_t *info, ulong dest, ulong data)
  480. {
  481. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)(info->start[0]);
  482. volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *)dest;
  483. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
  484. ulong start;
  485. int flag;
  486. int i;
  487. /* Check if Flash is (sufficiently) erased */
  488. if ((*((volatile ulong *)dest) & data) != data) {
  489. printf("dest = %08lx, *dest = %08lx, data = %08lx\n",
  490. dest, *(volatile ulong *)dest, data);
  491. return 2;
  492. }
  493. for (i=0; i < 4/sizeof(FLASH_WORD_SIZE); i++) {
  494. /* Disable interrupts which might cause a timeout here */
  495. flag = disable_interrupts();
  496. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  497. addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  498. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00A000A0;
  499. dest2[i] = data2[i];
  500. /* re-enable interrupts if necessary */
  501. if (flag)
  502. enable_interrupts();
  503. /* data polling for D7 */
  504. start = get_timer (0);
  505. while ((dest2[i] & 0x00800080) != (data2[i] & 0x00800080)) {
  506. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  507. addr2[0] = (FLASH_WORD_SIZE)0x00F000F0;
  508. return (1);
  509. }
  510. }
  511. }
  512. addr2[0] = (FLASH_WORD_SIZE)0x00F000F0;
  513. return (0);
  514. }
  515. /*-----------------------------------------------------------------------
  516. */