flash.c 15 KB

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