flash.c 15 KB

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