flash.c 14 KB

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