flash.c 14 KB

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