flash.c 16 KB

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