flash.c 15 KB

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