flash.c 14 KB

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