flash.c 14 KB

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