flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * (C) Copyright 2003 Motorola Inc.
  3. * Xianghua Xiao,(X.Xiao@motorola.com)
  4. *
  5. * (C) Copyright 2000, 2001
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * (C) Copyright 2001, Stuart Hughes, Lineo Inc, stuarth@lineo.com
  9. * Add support the Sharp chips on the mpc8260ads.
  10. * I started with board/ip860/flash.c and made changes I found in
  11. * the MTD project by David Schleef.
  12. *
  13. * See file CREDITS for list of people who contributed to this
  14. * project.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  29. * MA 02111-1307 USA
  30. */
  31. #include <common.h>
  32. #if !defined(CFG_NO_FLASH)
  33. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  34. #if defined(CFG_ENV_IS_IN_FLASH)
  35. # ifndef CFG_ENV_ADDR
  36. # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
  37. # endif
  38. # ifndef CFG_ENV_SIZE
  39. # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
  40. # endif
  41. # ifndef CFG_ENV_SECT_SIZE
  42. # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
  43. # endif
  44. #endif
  45. #undef DEBUG
  46. /*-----------------------------------------------------------------------
  47. * Functions
  48. */
  49. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  50. static int write_word (flash_info_t *info, ulong dest, ulong data);
  51. static int clear_block_lock_bit(vu_long * addr);
  52. /*-----------------------------------------------------------------------
  53. */
  54. unsigned long flash_init (void)
  55. {
  56. unsigned long size;
  57. int i;
  58. /* Init: enable write,
  59. * or we cannot even write flash commands
  60. */
  61. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  62. flash_info[i].flash_id = FLASH_UNKNOWN;
  63. /* set the default sector offset */
  64. }
  65. /* Static FLASH Bank configuration here - FIXME XXX */
  66. size = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
  67. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  68. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  69. size, size<<20);
  70. }
  71. /* Re-do sizing to get full correct info */
  72. size = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
  73. flash_info[0].size = size;
  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. #ifdef CFG_ENV_IS_IN_FLASH
  81. /* ENV protection ON by default */
  82. flash_protect(FLAG_PROTECT_SET,
  83. CFG_ENV_ADDR,
  84. CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
  85. &flash_info[0]);
  86. #endif
  87. #endif
  88. return (size);
  89. }
  90. /*-----------------------------------------------------------------------
  91. */
  92. void flash_print_info (flash_info_t *info)
  93. {
  94. int i;
  95. if (info->flash_id == FLASH_UNKNOWN) {
  96. printf ("missing or unknown FLASH type\n");
  97. return;
  98. }
  99. switch (info->flash_id & FLASH_VENDMASK) {
  100. case FLASH_MAN_INTEL: printf ("Intel "); break;
  101. case FLASH_MAN_SHARP: printf ("Sharp "); break;
  102. default: printf ("Unknown Vendor "); break;
  103. }
  104. switch (info->flash_id & FLASH_TYPEMASK) {
  105. case FLASH_28F016SV: printf ("28F016SV (16 Mbit, 32 x 64k)\n");
  106. break;
  107. case FLASH_28F160S3: printf ("28F160S3 (16 Mbit, 32 x 512K)\n");
  108. break;
  109. case FLASH_28F320S3: printf ("28F320S3 (32 Mbit, 64 x 512K)\n");
  110. break;
  111. case FLASH_LH28F016SCT: printf ("28F016SC (16 Mbit, 32 x 64K)\n");
  112. break;
  113. case FLASH_28F640J3A: printf ("28F640J3A (64 Mbit, 64 x 128K)\n");
  114. break;
  115. default: printf ("Unknown Chip Type\n");
  116. break;
  117. }
  118. printf (" Size: %ld MB in %d Sectors\n",
  119. info->size >> 20, info->sector_count);
  120. printf (" Sector Start Addresses:");
  121. for (i=0; i<info->sector_count; ++i) {
  122. if ((i % 5) == 0)
  123. printf ("\n ");
  124. printf (" %08lX%s",
  125. info->start[i],
  126. info->protect[i] ? " (RO)" : " "
  127. );
  128. }
  129. printf ("\n");
  130. }
  131. /*
  132. * The following code cannot be run from FLASH!
  133. */
  134. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  135. {
  136. short i;
  137. ulong value;
  138. ulong base = (ulong)addr;
  139. ulong sector_offset;
  140. #ifdef DEBUG
  141. printf("Check flash at 0x%08x\n",(uint)addr);
  142. #endif
  143. /* Write "Intelligent Identifier" command: read Manufacturer ID */
  144. *addr = 0x90909090;
  145. udelay(20);
  146. asm("sync");
  147. value = addr[0] & 0x00FF00FF;
  148. #ifdef DEBUG
  149. printf("manufacturer=0x%x\n",(uint)value);
  150. #endif
  151. switch (value) {
  152. case MT_MANUFACT: /* SHARP, MT or => Intel */
  153. case INTEL_ALT_MANU:
  154. info->flash_id = FLASH_MAN_INTEL;
  155. break;
  156. default:
  157. #if defined(CONFIG_MPC8641_SIM)
  158. info->flash_id = FLASH_MAN_INTEL;
  159. break;
  160. #else
  161. printf("unknown manufacturer: %x\n", (unsigned int)value);
  162. info->flash_id = FLASH_UNKNOWN;
  163. info->sector_count = 0;
  164. info->size = 0;
  165. return (0); /* no or unknown flash */
  166. #endif
  167. }
  168. value = addr[1] & 0x00FF00FF; /* device ID */
  169. #ifdef DEBUG
  170. printf("deviceID=0x%x\n",(uint)value);
  171. #endif
  172. switch (value) {
  173. case (INTEL_ID_28F016S):
  174. info->flash_id += FLASH_28F016SV;
  175. info->sector_count = 32;
  176. info->size = 0x00400000;
  177. sector_offset = 0x20000;
  178. break; /* => 2x2 MB */
  179. case (INTEL_ID_28F160S3):
  180. info->flash_id += FLASH_28F160S3;
  181. info->sector_count = 32;
  182. info->size = 0x00400000;
  183. sector_offset = 0x20000;
  184. break; /* => 2x2 MB */
  185. case (INTEL_ID_28F320S3):
  186. info->flash_id += FLASH_28F320S3;
  187. info->sector_count = 64;
  188. info->size = 0x00800000;
  189. sector_offset = 0x20000;
  190. break; /* => 2x4 MB */
  191. case (INTEL_ID_28F640J3A):
  192. info->flash_id += FLASH_28F640J3A;
  193. info->sector_count = 64;
  194. info->size = 0x01000000;
  195. sector_offset = 0x40000;
  196. break; /* => 2x8 MB */
  197. case SHARP_ID_28F016SCL:
  198. case SHARP_ID_28F016SCZ:
  199. info->flash_id = FLASH_MAN_SHARP | FLASH_LH28F016SCT;
  200. info->sector_count = 32;
  201. info->size = 0x00800000;
  202. sector_offset = 0x40000;
  203. break; /* => 4x2 MB */
  204. default:
  205. #if defined(CONFIG_MPC8641_SIM)
  206. info->flash_id += FLASH_28F640J3A;
  207. info->sector_count = 64;
  208. info->size = 0x01000000;
  209. sector_offset = 0x40000;
  210. break; /* => 2x8 MB */
  211. #else
  212. info->flash_id = FLASH_UNKNOWN;
  213. return (0); /* => no or unknown flash */
  214. #endif
  215. }
  216. /* set up sector start address table */
  217. for (i = 0; i < info->sector_count; i++) {
  218. info->start[i] = base;
  219. base += sector_offset;
  220. /* don't know how to check sector protection */
  221. info->protect[i] = 0;
  222. }
  223. /*
  224. * Prevent writes to uninitialized FLASH.
  225. */
  226. if (info->flash_id != FLASH_UNKNOWN) {
  227. addr = (vu_long *)info->start[0];
  228. *addr = 0xFFFFFF; /* reset bank to read array mode */
  229. asm("sync");
  230. }
  231. return (info->size);
  232. }
  233. /*-----------------------------------------------------------------------
  234. */
  235. int flash_erase (flash_info_t *info, int s_first, int s_last)
  236. {
  237. int flag, prot, sect;
  238. ulong start, now, last;
  239. if ((s_first < 0) || (s_first > s_last)) {
  240. if (info->flash_id == FLASH_UNKNOWN) {
  241. printf ("- missing\n");
  242. } else {
  243. printf ("- no sectors to erase\n");
  244. }
  245. return 1;
  246. }
  247. if ( ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL)
  248. && ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_SHARP) ) {
  249. printf ("Can't erase unknown flash type %08lx - aborted\n",
  250. info->flash_id);
  251. return 1;
  252. }
  253. prot = 0;
  254. for (sect=s_first; sect<=s_last; ++sect) {
  255. if (info->protect[sect]) {
  256. prot++;
  257. }
  258. }
  259. if (prot) {
  260. printf ("- Warning: %d protected sectors will not be erased!\n",
  261. prot);
  262. } else {
  263. printf ("\n");
  264. }
  265. #ifdef DEBUG
  266. printf("\nFlash Erase:\n");
  267. #endif
  268. /* Make Sure Block Lock Bit is not set. */
  269. if(clear_block_lock_bit((vu_long *)(info->start[s_first]))){
  270. return 1;
  271. }
  272. /* Start erase on unprotected sectors */
  273. #if defined(DEBUG)
  274. printf("Begin to erase now,s_first=0x%x s_last=0x%x...\n",s_first,s_last);
  275. #endif
  276. for (sect = s_first; sect<=s_last; sect++) {
  277. if (info->protect[sect] == 0) { /* not protected */
  278. vu_long *addr = (vu_long *)(info->start[sect]);
  279. asm("sync");
  280. last = start = get_timer (0);
  281. /* Disable interrupts which might cause a timeout here */
  282. flag = disable_interrupts();
  283. /* Reset Array */
  284. *addr = 0xffffffff;
  285. asm("sync");
  286. /* Clear Status Register */
  287. *addr = 0x50505050;
  288. asm("sync");
  289. /* Single Block Erase Command */
  290. *addr = 0x20202020;
  291. asm("sync");
  292. /* Confirm */
  293. *addr = 0xD0D0D0D0;
  294. asm("sync");
  295. if((info->flash_id & FLASH_TYPEMASK) != FLASH_LH28F016SCT) {
  296. /* Resume Command, as per errata update */
  297. *addr = 0xD0D0D0D0;
  298. asm("sync");
  299. }
  300. /* re-enable interrupts if necessary */
  301. if (flag)
  302. enable_interrupts();
  303. /* wait at least 80us - let's wait 1 ms */
  304. udelay (1000);
  305. while ((*addr & 0x00800080) != 0x00800080) {
  306. if(*addr & 0x00200020){
  307. printf("Error in Block Erase - Lock Bit may be set!\n");
  308. printf("Status Register = 0x%X\n", (uint)*addr);
  309. *addr = 0xFFFFFFFF; /* reset bank */
  310. asm("sync");
  311. return 1;
  312. }
  313. if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  314. printf ("Timeout\n");
  315. *addr = 0xFFFFFFFF; /* reset bank */
  316. asm("sync");
  317. return 1;
  318. }
  319. /* show that we're waiting */
  320. if ((now - last) > 1000) { /* every second */
  321. putc ('.');
  322. last = now;
  323. }
  324. }
  325. /* reset to read mode */
  326. *addr = 0xFFFFFFFF;
  327. asm("sync");
  328. }
  329. }
  330. printf ("flash erase done\n");
  331. return 0;
  332. }
  333. /*-----------------------------------------------------------------------
  334. * Copy memory to flash, returns:
  335. * 0 - OK
  336. * 1 - write timeout
  337. * 2 - Flash not erased
  338. */
  339. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  340. {
  341. ulong cp, wp, data;
  342. int i, l, rc;
  343. wp = (addr & ~3); /* get lower word aligned address */
  344. /*
  345. * handle unaligned start bytes
  346. */
  347. if ((l = addr - wp) != 0) {
  348. data = 0;
  349. for (i=0, cp=wp; i<l; ++i, ++cp) {
  350. data = (data << 8) | (*(uchar *)cp);
  351. }
  352. for (; i<4 && cnt>0; ++i) {
  353. data = (data << 8) | *src++;
  354. --cnt;
  355. ++cp;
  356. }
  357. for (; cnt==0 && i<4; ++i, ++cp) {
  358. data = (data << 8) | (*(uchar *)cp);
  359. }
  360. if ((rc = write_word(info, wp, data)) != 0) {
  361. return (rc);
  362. }
  363. wp += 4;
  364. }
  365. /*
  366. * handle word aligned part
  367. */
  368. while (cnt >= 4) {
  369. data = 0;
  370. for (i=0; i<4; ++i) {
  371. data = (data << 8) | *src++;
  372. }
  373. if ((rc = write_word(info, wp, data)) != 0) {
  374. return (rc);
  375. }
  376. wp += 4;
  377. cnt -= 4;
  378. }
  379. if (cnt == 0) {
  380. return (0);
  381. }
  382. /*
  383. * handle unaligned tail bytes
  384. */
  385. data = 0;
  386. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  387. data = (data << 8) | *src++;
  388. --cnt;
  389. }
  390. for (; i<4; ++i, ++cp) {
  391. data = (data << 8) | (*(uchar *)cp);
  392. }
  393. return (write_word(info, wp, data));
  394. }
  395. /*-----------------------------------------------------------------------
  396. * Write a word to Flash, returns:
  397. * 0 - OK
  398. * 1 - write timeout
  399. * 2 - Flash not erased
  400. */
  401. static int write_word (flash_info_t *info, ulong dest, ulong data)
  402. {
  403. vu_long *addr = (vu_long *)dest;
  404. ulong start, csr;
  405. int flag;
  406. /* Check if Flash is (sufficiently) erased */
  407. if ((*addr & data) != data) {
  408. return (2);
  409. }
  410. /* Disable interrupts which might cause a timeout here */
  411. flag = disable_interrupts();
  412. /* Write Command */
  413. *addr = 0x10101010;
  414. asm("sync");
  415. /* Write Data */
  416. *addr = data;
  417. /* re-enable interrupts if necessary */
  418. if (flag)
  419. enable_interrupts();
  420. /* data polling for D7 */
  421. start = get_timer (0);
  422. flag = 0;
  423. while (((csr = *addr) & 0x00800080) != 0x00800080) {
  424. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  425. flag = 1;
  426. break;
  427. }
  428. }
  429. if (csr & 0x40404040) {
  430. printf ("CSR indicates write error (%08lx) at %08lx\n", csr, (ulong)addr);
  431. flag = 1;
  432. }
  433. /* Clear Status Registers Command */
  434. *addr = 0x50505050;
  435. asm("sync");
  436. /* Reset to read array mode */
  437. *addr = 0xFFFFFFFF;
  438. asm("sync");
  439. return (flag);
  440. }
  441. /*-----------------------------------------------------------------------
  442. * Clear Block Lock Bit, returns:
  443. * 0 - OK
  444. * 1 - Timeout
  445. */
  446. static int clear_block_lock_bit(vu_long * addr)
  447. {
  448. ulong start, now;
  449. /* Reset Array */
  450. *addr = 0xffffffff;
  451. asm("sync");
  452. /* Clear Status Register */
  453. *addr = 0x50505050;
  454. asm("sync");
  455. *addr = 0x60606060;
  456. asm("sync");
  457. *addr = 0xd0d0d0d0;
  458. asm("sync");
  459. start = get_timer (0);
  460. while((*addr & 0x00800080) != 0x00800080){
  461. if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  462. printf ("Timeout on clearing Block Lock Bit\n");
  463. *addr = 0xFFFFFFFF; /* reset bank */
  464. asm("sync");
  465. return 1;
  466. }
  467. }
  468. return 0;
  469. }
  470. #endif /* !CFG_NO_FLASH */