flash.c 15 KB

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