flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. /* #define DEBUG */
  24. #include <common.h>
  25. #include <mpc8xx.h>
  26. #if defined(CFG_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. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  39. /*-----------------------------------------------------------------------
  40. * Functions
  41. */
  42. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  43. static int write_data (flash_info_t *info, ulong dest, ulong data);
  44. static void flash_get_offsets (ulong base, flash_info_t *info);
  45. /*-----------------------------------------------------------------------
  46. */
  47. unsigned long flash_init (void)
  48. {
  49. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  50. volatile memctl8xx_t *memctl = &immap->im_memctl;
  51. unsigned long size_b0, size_b1;
  52. int i;
  53. /* Init: no FLASHes known */
  54. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  55. flash_info[i].flash_id = FLASH_UNKNOWN;
  56. }
  57. /* Static FLASH Bank configuration here - FIXME XXX */
  58. debug ("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM);
  59. size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
  60. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  61. printf ("## Unknown FLASH on Bank 0: "
  62. "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
  63. flash_info[0].flash_id,
  64. size_b0, size_b0<<20);
  65. }
  66. debug ("## Get flash bank 2 size @ 0x%08x\n",FLASH_BASE1_PRELIM);
  67. size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
  68. debug ("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
  69. if (size_b1 > size_b0) {
  70. printf ("## ERROR: "
  71. "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
  72. size_b1, size_b1<<20,
  73. size_b0, size_b0<<20
  74. );
  75. flash_info[0].flash_id = FLASH_UNKNOWN;
  76. flash_info[1].flash_id = FLASH_UNKNOWN;
  77. flash_info[0].sector_count = -1;
  78. flash_info[1].sector_count = -1;
  79. flash_info[0].size = 0;
  80. flash_info[1].size = 0;
  81. return (0);
  82. }
  83. debug ("## Before remap: "
  84. "BR0: 0x%08x OR0: 0x%08x "
  85. "BR1: 0x%08x OR1: 0x%08x\n",
  86. memctl->memc_br0, memctl->memc_or0,
  87. memctl->memc_br1, memctl->memc_or1);
  88. /* Remap FLASH according to real size */
  89. memctl->memc_or0 = (-size_b0 & 0xFFFF8000) | CFG_OR_TIMING_FLASH |
  90. OR_CSNT_SAM | OR_ACS_DIV1;
  91. memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_PS_32 | BR_V;
  92. debug ("## BR0: 0x%08x OR0: 0x%08x\n",
  93. memctl->memc_br0, memctl->memc_or0);
  94. /* Re-do sizing to get full correct info */
  95. size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
  96. flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
  97. flash_info[0].size = size_b0;
  98. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  99. /* monitor protection ON by default */
  100. flash_protect(FLAG_PROTECT_SET,
  101. CFG_MONITOR_BASE,
  102. CFG_MONITOR_BASE+monitor_flash_len-1,
  103. &flash_info[0]);
  104. #endif
  105. #ifdef CFG_ENV_IS_IN_FLASH
  106. /* ENV protection ON by default */
  107. flash_protect(FLAG_PROTECT_SET,
  108. CFG_ENV_ADDR,
  109. CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
  110. &flash_info[0]);
  111. #endif
  112. if (size_b1) {
  113. memctl->memc_or1 = (-size_b1 & 0xFFFF8000) | CFG_OR_TIMING_FLASH |
  114. OR_CSNT_SAM | OR_ACS_DIV1;
  115. memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
  116. BR_PS_32 | BR_V;
  117. debug ("## BR1: 0x%08x OR1: 0x%08x\n",
  118. memctl->memc_br1, memctl->memc_or1);
  119. /* Re-do sizing to get full correct info */
  120. size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
  121. &flash_info[1]);
  122. flash_info[1].size = size_b1;
  123. flash_get_offsets (CFG_FLASH_BASE + size_b0, &flash_info[1]);
  124. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  125. /* monitor protection ON by default */
  126. flash_protect(FLAG_PROTECT_SET,
  127. CFG_MONITOR_BASE,
  128. CFG_MONITOR_BASE+monitor_flash_len-1,
  129. &flash_info[1]);
  130. #endif
  131. #ifdef CFG_ENV_IS_IN_FLASH
  132. /* ENV protection ON by default */
  133. flash_protect(FLAG_PROTECT_SET,
  134. CFG_ENV_ADDR,
  135. CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
  136. &flash_info[1]);
  137. #endif
  138. } else {
  139. memctl->memc_br1 = 0; /* invalidate bank */
  140. memctl->memc_or1 = 0; /* invalidate bank */
  141. debug ("## DISABLE BR1: 0x%08x OR1: 0x%08x\n",
  142. memctl->memc_br1, memctl->memc_or1);
  143. flash_info[1].flash_id = FLASH_UNKNOWN;
  144. flash_info[1].sector_count = -1;
  145. flash_info[1].size = 0;
  146. }
  147. debug ("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
  148. return (size_b0 + size_b1);
  149. }
  150. /*-----------------------------------------------------------------------
  151. */
  152. static void flash_get_offsets (ulong base, flash_info_t *info)
  153. {
  154. int i;
  155. if (info->flash_id == FLASH_UNKNOWN) {
  156. return;
  157. }
  158. switch (info->flash_id & FLASH_VENDMASK) {
  159. case FLASH_MAN_INTEL:
  160. for (i = 0; i < info->sector_count; i++) {
  161. info->start[i] = base;
  162. base += 0x00020000 * 2; /* 128k * 2 chips per bank */
  163. }
  164. return;
  165. default:
  166. printf ("Don't know sector ofsets for flash type 0x%lx\n",
  167. info->flash_id);
  168. return;
  169. }
  170. }
  171. /*-----------------------------------------------------------------------
  172. */
  173. void flash_print_info (flash_info_t *info)
  174. {
  175. int i;
  176. if (info->flash_id == FLASH_UNKNOWN) {
  177. printf ("missing or unknown FLASH type\n");
  178. return;
  179. }
  180. switch (info->flash_id & FLASH_VENDMASK) {
  181. case FLASH_MAN_AMD: printf ("AMD "); break;
  182. case FLASH_MAN_FUJ: printf ("Fujitsu "); break;
  183. case FLASH_MAN_SST: printf ("SST "); break;
  184. case FLASH_MAN_STM: printf ("STM "); break;
  185. case FLASH_MAN_INTEL: printf ("Intel "); break;
  186. case FLASH_MAN_MT: printf ("MT "); break;
  187. default: printf ("Unknown Vendor "); break;
  188. }
  189. switch (info->flash_id & FLASH_TYPEMASK) {
  190. case FLASH_28F320J3A: printf ("28F320J3A (32Mbit = 128K x 32)\n");
  191. break;
  192. case FLASH_28F640J3A: printf ("28F640J3A (64Mbit = 128K x 64)\n");
  193. break;
  194. case FLASH_28F128J3A: printf ("28F128J3A (128Mbit = 128K x 128)\n");
  195. break;
  196. default: printf ("Unknown Chip Type\n");
  197. break;
  198. }
  199. if (info->size >= (1 << 20)) {
  200. i = 20;
  201. } else {
  202. i = 10;
  203. }
  204. printf (" Size: %ld %cB in %d Sectors\n",
  205. info->size >> i,
  206. (i == 20) ? 'M' : 'k',
  207. info->sector_count);
  208. printf (" Sector Start Addresses:");
  209. for (i=0; i<info->sector_count; ++i) {
  210. if ((i % 5) == 0)
  211. printf ("\n ");
  212. printf (" %08lX%s",
  213. info->start[i],
  214. info->protect[i] ? " (RO)" : " "
  215. );
  216. }
  217. printf ("\n");
  218. return;
  219. }
  220. /*-----------------------------------------------------------------------
  221. */
  222. /*-----------------------------------------------------------------------
  223. */
  224. /*
  225. * The following code cannot be run from FLASH!
  226. */
  227. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  228. {
  229. ulong value;
  230. /* Read Manufacturer ID */
  231. addr[0] = 0x00900090;
  232. value = addr[0];
  233. debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
  234. switch (value) {
  235. case AMD_MANUFACT:
  236. info->flash_id = FLASH_MAN_AMD;
  237. break;
  238. case FUJ_MANUFACT:
  239. info->flash_id = FLASH_MAN_FUJ;
  240. break;
  241. case SST_MANUFACT:
  242. info->flash_id = FLASH_MAN_SST;
  243. break;
  244. case STM_MANUFACT:
  245. info->flash_id = FLASH_MAN_STM;
  246. break;
  247. case INTEL_MANUFACT:
  248. info->flash_id = FLASH_MAN_INTEL;
  249. break;
  250. default:
  251. info->flash_id = FLASH_UNKNOWN;
  252. info->sector_count = 0;
  253. info->size = 0;
  254. addr[0] = 0x00FF00FF; /* restore read mode */
  255. return (0); /* no or unknown flash */
  256. }
  257. value = addr[1]; /* device ID */
  258. debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
  259. switch (value) {
  260. case INTEL_ID_28F320J3A:
  261. info->flash_id += FLASH_28F320J3A;
  262. info->sector_count = 32;
  263. info->size = 0x00400000 * 2;
  264. break; /* => 8 MB */
  265. case INTEL_ID_28F640J3A:
  266. info->flash_id += FLASH_28F640J3A;
  267. info->sector_count = 64;
  268. info->size = 0x00800000 * 2;
  269. break; /* => 16 MB */
  270. case INTEL_ID_28F128J3A:
  271. info->flash_id += FLASH_28F128J3A;
  272. info->sector_count = 128;
  273. info->size = 0x01000000 * 2;
  274. break; /* => 32 MB */
  275. default:
  276. info->flash_id = FLASH_UNKNOWN;
  277. addr[0] = 0x00FF00FF; /* restore read mode */
  278. return (0); /* => no or unknown flash */
  279. }
  280. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  281. printf ("** ERROR: sector count %d > max (%d) **\n",
  282. info->sector_count, CFG_MAX_FLASH_SECT);
  283. info->sector_count = CFG_MAX_FLASH_SECT;
  284. }
  285. addr[0] = 0x00FF00FF; /* restore read mode */
  286. return (info->size);
  287. }
  288. /*-----------------------------------------------------------------------
  289. */
  290. int flash_erase (flash_info_t *info, int s_first, int s_last)
  291. {
  292. int flag, prot, sect;
  293. ulong start, now, last;
  294. debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
  295. if ((s_first < 0) || (s_first > s_last)) {
  296. if (info->flash_id == FLASH_UNKNOWN) {
  297. printf ("- missing\n");
  298. } else {
  299. printf ("- no sectors to erase\n");
  300. }
  301. return 1;
  302. }
  303. if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
  304. printf ("Can erase only Intel flash types - aborted\n");
  305. return 1;
  306. }
  307. prot = 0;
  308. for (sect=s_first; sect<=s_last; ++sect) {
  309. if (info->protect[sect]) {
  310. prot++;
  311. }
  312. }
  313. if (prot) {
  314. printf ("- Warning: %d protected sectors will not be erased!\n",
  315. prot);
  316. } else {
  317. printf ("\n");
  318. }
  319. start = get_timer (0);
  320. last = start;
  321. /* Start erase on unprotected sectors */
  322. for (sect = s_first; sect<=s_last; sect++) {
  323. if (info->protect[sect] == 0) { /* not protected */
  324. vu_long *addr = (vu_long *)(info->start[sect]);
  325. unsigned long status;
  326. /* Disable interrupts which might cause a timeout here */
  327. flag = disable_interrupts();
  328. *addr = 0x00500050; /* clear status register */
  329. *addr = 0x00200020; /* erase setup */
  330. *addr = 0x00D000D0; /* erase confirm */
  331. /* re-enable interrupts if necessary */
  332. if (flag)
  333. enable_interrupts();
  334. /* wait at least 80us - let's wait 1 ms */
  335. udelay (1000);
  336. while (((status = *addr) & 0x00800080) != 0x00800080) {
  337. if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  338. printf ("Timeout\n");
  339. *addr = 0x00B000B0; /* suspend erase */
  340. *addr = 0x00FF00FF; /* reset to read mode */
  341. return 1;
  342. }
  343. /* show that we're waiting */
  344. if ((now - last) > 1000) { /* every second */
  345. putc ('.');
  346. last = now;
  347. }
  348. }
  349. *addr = 0x00FF00FF; /* reset to read mode */
  350. }
  351. }
  352. printf (" done\n");
  353. return 0;
  354. }
  355. /*-----------------------------------------------------------------------
  356. * Copy memory to flash, returns:
  357. * 0 - OK
  358. * 1 - write timeout
  359. * 2 - Flash not erased
  360. * 4 - Flash not identified
  361. */
  362. #define FLASH_WIDTH 4 /* flash bus width in bytes */
  363. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  364. {
  365. ulong cp, wp, data;
  366. int i, l, rc;
  367. if (info->flash_id == FLASH_UNKNOWN) {
  368. return 4;
  369. }
  370. wp = (addr & ~(FLASH_WIDTH-1)); /* get lower FLASH_WIDTH aligned address */
  371. /*
  372. * handle unaligned start bytes
  373. */
  374. if ((l = addr - wp) != 0) {
  375. data = 0;
  376. for (i=0, cp=wp; i<l; ++i, ++cp) {
  377. data = (data << 8) | (*(uchar *)cp);
  378. }
  379. for (; i<FLASH_WIDTH && cnt>0; ++i) {
  380. data = (data << 8) | *src++;
  381. --cnt;
  382. ++cp;
  383. }
  384. for (; cnt==0 && i<FLASH_WIDTH; ++i, ++cp) {
  385. data = (data << 8) | (*(uchar *)cp);
  386. }
  387. if ((rc = write_data(info, wp, data)) != 0) {
  388. return (rc);
  389. }
  390. wp += FLASH_WIDTH;
  391. }
  392. /*
  393. * handle FLASH_WIDTH aligned part
  394. */
  395. while (cnt >= FLASH_WIDTH) {
  396. data = 0;
  397. for (i=0; i<FLASH_WIDTH; ++i) {
  398. data = (data << 8) | *src++;
  399. }
  400. if ((rc = write_data(info, wp, data)) != 0) {
  401. return (rc);
  402. }
  403. wp += FLASH_WIDTH;
  404. cnt -= FLASH_WIDTH;
  405. }
  406. if (cnt == 0) {
  407. return (0);
  408. }
  409. /*
  410. * handle unaligned tail bytes
  411. */
  412. data = 0;
  413. for (i=0, cp=wp; i<FLASH_WIDTH && cnt>0; ++i, ++cp) {
  414. data = (data << 8) | *src++;
  415. --cnt;
  416. }
  417. for (; i<FLASH_WIDTH; ++i, ++cp) {
  418. data = (data << 8) | (*(uchar *)cp);
  419. }
  420. return (write_data(info, wp, data));
  421. }
  422. /*-----------------------------------------------------------------------
  423. * Write a word to Flash, returns:
  424. * 0 - OK
  425. * 1 - write timeout
  426. * 2 - Flash not erased
  427. */
  428. static int write_data (flash_info_t *info, ulong dest, ulong data)
  429. {
  430. vu_long *addr = (vu_long *)dest;
  431. ulong status;
  432. ulong start;
  433. int flag;
  434. /* Check if Flash is (sufficiently) erased */
  435. if ((*addr & data) != data) {
  436. return (2);
  437. }
  438. /* Disable interrupts which might cause a timeout here */
  439. flag = disable_interrupts();
  440. *addr = 0x00400040; /* write setup */
  441. *addr = data;
  442. /* re-enable interrupts if necessary */
  443. if (flag)
  444. enable_interrupts();
  445. start = get_timer (0);
  446. while (((status = *addr) & 0x00800080) != 0x00800080) {
  447. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  448. *addr = 0x00FF00FF; /* restore read mode */
  449. return (1);
  450. }
  451. }
  452. *addr = 0x00FF00FF; /* restore read mode */
  453. return (0);
  454. }
  455. /*-----------------------------------------------------------------------
  456. */