flash.c 14 KB

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