flash.c 13 KB

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