flash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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_data (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;
  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: "
  59. "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
  60. flash_info[0].flash_id,
  61. size_b0, size_b0<<20);
  62. }
  63. /* Remap FLASH according to real size */
  64. memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
  65. memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | \
  66. BR_MS_GPCM | BR_PS_16 | BR_V;
  67. /* Re-do sizing to get full correct info */
  68. size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
  69. flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
  70. flash_info[0].size = size_b0;
  71. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  72. /* monitor protection ON by default */
  73. flash_protect(FLAG_PROTECT_SET,
  74. CFG_MONITOR_BASE,
  75. CFG_MONITOR_BASE+monitor_flash_len-1,
  76. &flash_info[0]);
  77. #endif
  78. #ifdef CFG_ENV_IS_IN_FLASH
  79. /* ENV protection ON by default */
  80. flash_protect(FLAG_PROTECT_SET,
  81. CFG_ENV_ADDR,
  82. CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
  83. &flash_info[0]);
  84. #endif
  85. return (size_b0);
  86. }
  87. /*-----------------------------------------------------------------------
  88. */
  89. static void flash_get_offsets (ulong base, flash_info_t *info)
  90. {
  91. int i;
  92. if (info->flash_id == FLASH_UNKNOWN) {
  93. return;
  94. }
  95. switch (info->flash_id & FLASH_VENDMASK) {
  96. case FLASH_MAN_MT:
  97. if (info->flash_id & FLASH_BTYPE) {
  98. /* set sector offsets for bottom boot block type */
  99. info->start[0] = base + 0x00000000;
  100. info->start[1] = base + 0x00004000;
  101. info->start[2] = base + 0x00006000;
  102. info->start[3] = base + 0x00008000;
  103. for (i = 4; i < info->sector_count; i++) {
  104. info->start[i] = base + ((i-3) * 0x00020000);
  105. }
  106. } else {
  107. /* set sector offsets for top boot block type */
  108. i = info->sector_count - 1;
  109. info->start[i--] = base + info->size - 0x00004000;
  110. info->start[i--] = base + info->size - 0x00006000;
  111. info->start[i--] = base + info->size - 0x00008000;
  112. for (; i >= 0; i--) {
  113. info->start[i] = base + i * 0x00020000;
  114. }
  115. }
  116. return;
  117. case FLASH_MAN_SST:
  118. for (i = 0; i < info->sector_count; i++) {
  119. info->start[i] = base + (i * 0x00002000);
  120. }
  121. return;
  122. case FLASH_MAN_AMD:
  123. case FLASH_MAN_FUJ:
  124. /* set up sector start address table */
  125. if (info->flash_id & FLASH_BTYPE) {
  126. /* set sector offsets for bottom boot block type */
  127. info->start[0] = base + 0x00000000;
  128. info->start[1] = base + 0x00008000;
  129. info->start[2] = base + 0x0000C000;
  130. info->start[3] = base + 0x00010000;
  131. for (i = 4; i < info->sector_count; i++) {
  132. info->start[i] = base + (i * 0x00020000) - 0x00060000;
  133. }
  134. } else {
  135. /* set sector offsets for top boot block type */
  136. i = info->sector_count - 1;
  137. info->start[i--] = base + info->size - 0x00008000;
  138. info->start[i--] = base + info->size - 0x0000C000;
  139. info->start[i--] = base + info->size - 0x00010000;
  140. for (; i >= 0; i--) {
  141. info->start[i] = base + i * 0x00020000;
  142. }
  143. }
  144. return;
  145. default:
  146. printf ("Don't know sector ofsets for flash type 0x%lx\n",
  147. info->flash_id);
  148. return;
  149. }
  150. }
  151. /*-----------------------------------------------------------------------
  152. */
  153. void flash_print_info (flash_info_t *info)
  154. {
  155. int i;
  156. if (info->flash_id == FLASH_UNKNOWN) {
  157. printf ("missing or unknown FLASH type\n");
  158. return;
  159. }
  160. switch (info->flash_id & FLASH_VENDMASK) {
  161. case FLASH_MAN_AMD: printf ("AMD "); break;
  162. case FLASH_MAN_FUJ: printf ("Fujitsu "); break;
  163. case FLASH_MAN_SST: printf ("SST "); break;
  164. case FLASH_MAN_STM: printf ("STM "); break;
  165. case FLASH_MAN_MT: printf ("MT "); break;
  166. case FLASH_MAN_INTEL: printf ("Intel "); break;
  167. default: printf ("Unknown Vendor "); break;
  168. }
  169. switch (info->flash_id & FLASH_TYPEMASK) {
  170. case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  171. break;
  172. case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  173. break;
  174. case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  175. break;
  176. case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  177. break;
  178. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  179. break;
  180. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  181. break;
  182. case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  183. break;
  184. case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  185. break;
  186. case FLASH_SST200A: printf ("39xF200A (2M = 128K x 16)\n");
  187. break;
  188. case FLASH_SST400A: printf ("39xF400A (4M = 256K x 16)\n");
  189. break;
  190. case FLASH_SST800A: printf ("39xF800A (8M = 512K x 16)\n");
  191. break;
  192. case FLASH_STM800AB: printf ("M29W800AB (8M = 512K x 16)\n");
  193. break;
  194. case FLASH_28F008S5: printf ("28F008S5 (1M = 64K x 16)\n");
  195. break;
  196. case FLASH_28F400_T: printf ("28F400B3 (4Mbit, top boot sector)\n");
  197. break;
  198. case FLASH_28F400_B: printf ("28F400B3 (4Mbit, bottom boot sector)\n");
  199. break;
  200. default: printf ("Unknown Chip Type\n");
  201. break;
  202. }
  203. if (info->size >= (1 << 20)) {
  204. i = 20;
  205. } else {
  206. i = 10;
  207. }
  208. printf (" Size: %ld %cB in %d Sectors\n",
  209. info->size >> i,
  210. (i == 20) ? 'M' : 'k',
  211. info->sector_count);
  212. printf (" Sector Start Addresses:");
  213. for (i=0; i<info->sector_count; ++i) {
  214. if ((i % 5) == 0)
  215. printf ("\n ");
  216. printf (" %08lX%s",
  217. info->start[i],
  218. info->protect[i] ? " (RO)" : " "
  219. );
  220. }
  221. printf ("\n");
  222. return;
  223. }
  224. /*-----------------------------------------------------------------------
  225. */
  226. /*-----------------------------------------------------------------------
  227. */
  228. /*
  229. * The following code cannot be run from FLASH!
  230. */
  231. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  232. {
  233. ushort value;
  234. vu_short *saddr = (vu_short *)addr;
  235. /* Read Manufacturer ID */
  236. saddr[0] = 0x0090;
  237. value = saddr[0];
  238. switch (value) {
  239. case (AMD_MANUFACT & 0xFFFF):
  240. info->flash_id = FLASH_MAN_AMD;
  241. break;
  242. case (FUJ_MANUFACT & 0xFFFF):
  243. info->flash_id = FLASH_MAN_FUJ;
  244. break;
  245. case (SST_MANUFACT & 0xFFFF):
  246. info->flash_id = FLASH_MAN_SST;
  247. break;
  248. case (STM_MANUFACT & 0xFFFF):
  249. info->flash_id = FLASH_MAN_STM;
  250. break;
  251. case (MT_MANUFACT & 0xFFFF):
  252. info->flash_id = FLASH_MAN_MT;
  253. break;
  254. default:
  255. info->flash_id = FLASH_UNKNOWN;
  256. info->sector_count = 0;
  257. info->size = 0;
  258. saddr[0] = 0x00FF; /* restore read mode */
  259. return (0); /* no or unknown flash */
  260. }
  261. value = saddr[1]; /* device ID */
  262. switch (value) {
  263. case (AMD_ID_LV400T & 0xFFFF):
  264. info->flash_id += FLASH_AM400T;
  265. info->sector_count = 11;
  266. info->size = 0x00100000;
  267. break; /* => 1 MB */
  268. case (AMD_ID_LV400B & 0xFFFF):
  269. info->flash_id += FLASH_AM400B;
  270. info->sector_count = 11;
  271. info->size = 0x00100000;
  272. break; /* => 1 MB */
  273. case (AMD_ID_LV800T & 0xFFFF):
  274. info->flash_id += FLASH_AM800T;
  275. info->sector_count = 19;
  276. info->size = 0x00200000;
  277. break; /* => 2 MB */
  278. case (AMD_ID_LV800B & 0xFFFF):
  279. info->flash_id += FLASH_AM800B;
  280. info->sector_count = 19;
  281. info->size = 0x00200000;
  282. break; /* => 2 MB */
  283. case (AMD_ID_LV160T & 0xFFFF):
  284. info->flash_id += FLASH_AM160T;
  285. info->sector_count = 35;
  286. info->size = 0x00400000;
  287. break; /* => 4 MB */
  288. case (AMD_ID_LV160B & 0xFFFF):
  289. info->flash_id += FLASH_AM160B;
  290. info->sector_count = 35;
  291. info->size = 0x00400000;
  292. break; /* => 4 MB */
  293. #if 0 /* enable when device IDs are available */
  294. case (AMD_ID_LV320T & 0xFFFF):
  295. info->flash_id += FLASH_AM320T;
  296. info->sector_count = 67;
  297. info->size = 0x00800000;
  298. break; /* => 8 MB */
  299. case (AMD_ID_LV320B & 0xFFFF):
  300. info->flash_id += FLASH_AM320B;
  301. info->sector_count = 67;
  302. info->size = 0x00800000;
  303. break; /* => 8 MB */
  304. #endif
  305. case (SST_ID_xF200A & 0xFFFF):
  306. info->flash_id += FLASH_SST200A;
  307. info->sector_count = 64; /* 39xF200A ID ( 2M = 128K x 16 ) */
  308. info->size = 0x00080000;
  309. break;
  310. case (SST_ID_xF400A & 0xFFFF):
  311. info->flash_id += FLASH_SST400A;
  312. info->sector_count = 128; /* 39xF400A ID ( 4M = 256K x 16 ) */
  313. info->size = 0x00100000;
  314. break;
  315. case (SST_ID_xF800A & 0xFFFF):
  316. info->flash_id += FLASH_SST800A;
  317. info->sector_count = 256; /* 39xF800A ID ( 8M = 512K x 16 ) */
  318. info->size = 0x00200000;
  319. break; /* => 2 MB */
  320. case (STM_ID_x800AB & 0xFFFF):
  321. info->flash_id += FLASH_STM800AB;
  322. info->sector_count = 19;
  323. info->size = 0x00200000;
  324. break; /* => 2 MB */
  325. case (MT_ID_28F400_T & 0xFFFF):
  326. info->flash_id += FLASH_28F400_T;
  327. info->sector_count = 7;
  328. info->size = 0x00080000;
  329. break; /* => 512 kB */
  330. case (MT_ID_28F400_B & 0xFFFF):
  331. info->flash_id += FLASH_28F400_B;
  332. info->sector_count = 7;
  333. info->size = 0x00080000;
  334. break; /* => 512 kB */
  335. default:
  336. info->flash_id = FLASH_UNKNOWN;
  337. saddr[0] = 0x00FF; /* restore read mode */
  338. return (0); /* => no or unknown flash */
  339. }
  340. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  341. printf ("** ERROR: sector count %d > max (%d) **\n",
  342. info->sector_count, CFG_MAX_FLASH_SECT);
  343. info->sector_count = CFG_MAX_FLASH_SECT;
  344. }
  345. saddr[0] = 0x00FF; /* restore read mode */
  346. return (info->size);
  347. }
  348. /*-----------------------------------------------------------------------
  349. */
  350. int flash_erase (flash_info_t *info, int s_first, int s_last)
  351. {
  352. int flag, prot, sect;
  353. ulong start, now, last;
  354. if ((s_first < 0) || (s_first > s_last)) {
  355. if (info->flash_id == FLASH_UNKNOWN) {
  356. printf ("- missing\n");
  357. } else {
  358. printf ("- no sectors to erase\n");
  359. }
  360. return 1;
  361. }
  362. if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_MT) {
  363. printf ("Can erase only MT flash types - 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",
  374. prot);
  375. } else {
  376. printf ("\n");
  377. }
  378. start = get_timer (0);
  379. last = start;
  380. /* Start erase on unprotected sectors */
  381. for (sect = s_first; sect<=s_last; sect++) {
  382. if (info->protect[sect] == 0) { /* not protected */
  383. vu_short *addr = (vu_short *)(info->start[sect]);
  384. unsigned short status;
  385. /* Disable interrupts which might cause a timeout here */
  386. flag = disable_interrupts();
  387. *addr = 0x0050; /* clear status register */
  388. *addr = 0x0020; /* erase setup */
  389. *addr = 0x00D0; /* erase confirm */
  390. /* re-enable interrupts if necessary */
  391. if (flag)
  392. enable_interrupts();
  393. /* wait at least 80us - let's wait 1 ms */
  394. udelay (1000);
  395. while (((status = *addr) & 0x0080) != 0x0080) {
  396. if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  397. printf ("Timeout\n");
  398. *addr = 0x00FF; /* reset to read mode */
  399. return 1;
  400. }
  401. /* show that we're waiting */
  402. if ((now - last) > 1000) { /* every second */
  403. putc ('.');
  404. last = now;
  405. }
  406. }
  407. *addr = 0x00FF; /* reset to read mode */
  408. }
  409. }
  410. printf (" done\n");
  411. return 0;
  412. }
  413. /*-----------------------------------------------------------------------
  414. * Copy memory to flash, returns:
  415. * 0 - OK
  416. * 1 - write timeout
  417. * 2 - Flash not erased
  418. * 4 - Flash not identified
  419. */
  420. #define FLASH_WIDTH 2 /* flash bus width in bytes */
  421. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  422. {
  423. ulong cp, wp, data;
  424. int i, l, rc;
  425. if (info->flash_id == FLASH_UNKNOWN) {
  426. return 4;
  427. }
  428. wp = (addr & ~(FLASH_WIDTH-1)); /* get lower FLASH_WIDTH aligned address */
  429. /*
  430. * handle unaligned start bytes
  431. */
  432. if ((l = addr - wp) != 0) {
  433. data = 0;
  434. for (i=0, cp=wp; i<l; ++i, ++cp) {
  435. data = (data << 8) | (*(uchar *)cp);
  436. }
  437. for (; i<FLASH_WIDTH && cnt>0; ++i) {
  438. data = (data << 8) | *src++;
  439. --cnt;
  440. ++cp;
  441. }
  442. for (; cnt==0 && i<FLASH_WIDTH; ++i, ++cp) {
  443. data = (data << 8) | (*(uchar *)cp);
  444. }
  445. if ((rc = write_data(info, wp, data)) != 0) {
  446. return (rc);
  447. }
  448. wp += FLASH_WIDTH;
  449. }
  450. /*
  451. * handle FLASH_WIDTH aligned part
  452. */
  453. while (cnt >= FLASH_WIDTH) {
  454. data = 0;
  455. for (i=0; i<FLASH_WIDTH; ++i) {
  456. data = (data << 8) | *src++;
  457. }
  458. if ((rc = write_data(info, wp, data)) != 0) {
  459. return (rc);
  460. }
  461. wp += FLASH_WIDTH;
  462. cnt -= FLASH_WIDTH;
  463. }
  464. if (cnt == 0) {
  465. return (0);
  466. }
  467. /*
  468. * handle unaligned tail bytes
  469. */
  470. data = 0;
  471. for (i=0, cp=wp; i<FLASH_WIDTH && cnt>0; ++i, ++cp) {
  472. data = (data << 8) | *src++;
  473. --cnt;
  474. }
  475. for (; i<FLASH_WIDTH; ++i, ++cp) {
  476. data = (data << 8) | (*(uchar *)cp);
  477. }
  478. return (write_data(info, wp, data));
  479. }
  480. /*-----------------------------------------------------------------------
  481. * Write a word to Flash, returns:
  482. * 0 - OK
  483. * 1 - write timeout
  484. * 2 - Flash not erased
  485. */
  486. static int write_data (flash_info_t *info, ulong dest, ulong data)
  487. {
  488. vu_short *addr = (vu_short *)dest;
  489. ushort sdata = (ushort)data;
  490. ushort status;
  491. ulong start;
  492. int flag;
  493. /* Check if Flash is (sufficiently) erased */
  494. if ((*addr & sdata) != sdata) {
  495. return (2);
  496. }
  497. /* Disable interrupts which might cause a timeout here */
  498. flag = disable_interrupts();
  499. *addr = 0x0040; /* write setup */
  500. *addr = sdata;
  501. /* re-enable interrupts if necessary */
  502. if (flag)
  503. enable_interrupts();
  504. start = get_timer (0);
  505. while (((status = *addr) & 0x0080) != 0x0080) {
  506. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  507. *addr = 0x00FF; /* restore read mode */
  508. return (1);
  509. }
  510. }
  511. *addr = 0x00FF; /* restore read mode */
  512. return (0);
  513. }
  514. /*-----------------------------------------------------------------------
  515. */