flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * (C) Copyright 2003 Motorola Inc.
  3. * Xianghua Xiao,(X.Xiao@motorola.com)
  4. *
  5. * (C) Copyright 2000-2005
  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. case FLASH_28F128J3A: printf ("28F128J3A (128 Mbit, 128 x 128K)\n");
  116. break;
  117. default: printf ("Unknown Chip Type\n");
  118. break;
  119. }
  120. printf (" Size: %ld MB in %d Sectors\n",
  121. info->size >> 20, info->sector_count);
  122. printf (" Sector Start Addresses:");
  123. for (i=0; i<info->sector_count; ++i) {
  124. if ((i % 5) == 0)
  125. printf ("\n ");
  126. printf (" %08lX%s",
  127. info->start[i],
  128. info->protect[i] ? " (RO)" : " "
  129. );
  130. }
  131. printf ("\n");
  132. }
  133. /*
  134. * The following code cannot be run from FLASH!
  135. */
  136. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  137. {
  138. short i;
  139. ulong value;
  140. ulong base = (ulong)addr;
  141. ulong sector_offset;
  142. #ifdef DEBUG
  143. printf("Check flash at 0x%08x\n",(uint)addr);
  144. #endif
  145. /* Write "Intelligent Identifier" command: read Manufacturer ID */
  146. *addr = 0x90909090;
  147. udelay(20);
  148. asm("sync");
  149. value = addr[0] & 0x00FF00FF;
  150. #ifdef DEBUG
  151. printf("manufacturer=0x%x\n",(uint)value);
  152. #endif
  153. switch (value) {
  154. case MT_MANUFACT: /* SHARP, MT or => Intel */
  155. case INTEL_ALT_MANU:
  156. info->flash_id = FLASH_MAN_INTEL;
  157. break;
  158. default:
  159. printf("unknown manufacturer: %x\n", (unsigned int)value);
  160. info->flash_id = FLASH_UNKNOWN;
  161. info->sector_count = 0;
  162. info->size = 0;
  163. return (0); /* no or unknown flash */
  164. }
  165. value = addr[1] & 0x00FF00FF; /* device ID */
  166. #ifdef DEBUG
  167. printf("deviceID=0x%x\n",(uint)value);
  168. #endif
  169. switch (value) {
  170. case (INTEL_ID_28F016S):
  171. info->flash_id += FLASH_28F016SV;
  172. info->sector_count = 32;
  173. info->size = 0x00400000;
  174. sector_offset = 0x20000;
  175. break; /* => 2x2 MB */
  176. case (INTEL_ID_28F160S3):
  177. info->flash_id += FLASH_28F160S3;
  178. info->sector_count = 32;
  179. info->size = 0x00400000;
  180. sector_offset = 0x20000;
  181. break; /* => 2x2 MB */
  182. case (INTEL_ID_28F320S3):
  183. info->flash_id += FLASH_28F320S3;
  184. info->sector_count = 64;
  185. info->size = 0x00800000;
  186. sector_offset = 0x20000;
  187. break; /* => 2x4 MB */
  188. case (INTEL_ID_28F640J3A):
  189. info->flash_id += FLASH_28F640J3A;
  190. info->sector_count = 64;
  191. info->size = 0x01000000;
  192. sector_offset = 0x40000;
  193. break; /* => 2x8 MB */
  194. case (INTEL_ID_28F128J3A):
  195. info->flash_id += FLASH_28F128J3A;
  196. info->sector_count = 128;
  197. info->size = 0x02000000;
  198. sector_offset = 0x40000;
  199. break; /* => 2x16 MB */
  200. case SHARP_ID_28F016SCL:
  201. case SHARP_ID_28F016SCZ:
  202. info->flash_id = FLASH_MAN_SHARP | FLASH_LH28F016SCT;
  203. info->sector_count = 32;
  204. info->size = 0x00800000;
  205. sector_offset = 0x40000;
  206. break; /* => 4x2 MB */
  207. default:
  208. info->flash_id = FLASH_UNKNOWN;
  209. return (0); /* => no or unknown flash */
  210. }
  211. /* set up sector start address table */
  212. for (i = 0; i < info->sector_count; i++) {
  213. info->start[i] = base;
  214. base += sector_offset;
  215. /* don't know how to check sector protection */
  216. info->protect[i] = 0;
  217. }
  218. /*
  219. * Prevent writes to uninitialized FLASH.
  220. */
  221. if (info->flash_id != FLASH_UNKNOWN) {
  222. addr = (vu_long *)info->start[0];
  223. *addr = 0xFFFFFF; /* reset bank to read array mode */
  224. asm("sync");
  225. }
  226. return (info->size);
  227. }
  228. /*-----------------------------------------------------------------------
  229. */
  230. int flash_erase (flash_info_t *info, int s_first, int s_last)
  231. {
  232. int flag, prot, sect;
  233. ulong start, now, last;
  234. if ((s_first < 0) || (s_first > s_last)) {
  235. if (info->flash_id == FLASH_UNKNOWN) {
  236. printf ("- missing\n");
  237. } else {
  238. printf ("- no sectors to erase\n");
  239. }
  240. return 1;
  241. }
  242. if ( ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL)
  243. && ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_SHARP) ) {
  244. printf ("Can't erase unknown flash type %08lx - aborted\n",
  245. info->flash_id);
  246. return 1;
  247. }
  248. prot = 0;
  249. for (sect=s_first; sect<=s_last; ++sect) {
  250. if (info->protect[sect]) {
  251. prot++;
  252. }
  253. }
  254. if (prot) {
  255. printf ("- Warning: %d protected sectors will not be erased!\n",
  256. prot);
  257. } else {
  258. printf ("\n");
  259. }
  260. #ifdef DEBUG
  261. printf("\nFlash Erase:\n");
  262. #endif
  263. /* Make Sure Block Lock Bit is not set. */
  264. if(clear_block_lock_bit((vu_long *)(info->start[s_first]))){
  265. return 1;
  266. }
  267. /* Start erase on unprotected sectors */
  268. #if defined(DEBUG)
  269. printf("Begin to erase now,s_first=0x%x s_last=0x%x...\n",s_first,s_last);
  270. #endif
  271. for (sect = s_first; sect<=s_last; sect++) {
  272. if (info->protect[sect] == 0) { /* not protected */
  273. vu_long *addr = (vu_long *)(info->start[sect]);
  274. asm("sync");
  275. last = start = get_timer (0);
  276. /* Disable interrupts which might cause a timeout here */
  277. flag = disable_interrupts();
  278. /* Reset Array */
  279. *addr = 0xffffffff;
  280. asm("sync");
  281. /* Clear Status Register */
  282. *addr = 0x50505050;
  283. asm("sync");
  284. /* Single Block Erase Command */
  285. *addr = 0x20202020;
  286. asm("sync");
  287. /* Confirm */
  288. *addr = 0xD0D0D0D0;
  289. asm("sync");
  290. if((info->flash_id & FLASH_TYPEMASK) != FLASH_LH28F016SCT) {
  291. /* Resume Command, as per errata update */
  292. *addr = 0xD0D0D0D0;
  293. asm("sync");
  294. }
  295. /* re-enable interrupts if necessary */
  296. if (flag)
  297. enable_interrupts();
  298. /* wait at least 80us - let's wait 1 ms */
  299. udelay (1000);
  300. while ((*addr & 0x00800080) != 0x00800080) {
  301. if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  302. printf ("Timeout\n");
  303. *addr = 0xFFFFFFFF; /* reset bank */
  304. asm("sync");
  305. return 1;
  306. }
  307. /* show that we're waiting */
  308. if ((now - last) > 1000) { /* every second */
  309. putc ('.');
  310. last = now;
  311. }
  312. }
  313. /* reset to read mode */
  314. *addr = 0xFFFFFFFF;
  315. asm("sync");
  316. }
  317. }
  318. printf ("flash erase done\n");
  319. return 0;
  320. }
  321. /*-----------------------------------------------------------------------
  322. * Copy memory to flash, returns:
  323. * 0 - OK
  324. * 1 - write timeout
  325. * 2 - Flash not erased
  326. */
  327. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  328. {
  329. ulong cp, wp, data;
  330. int i, l, rc;
  331. wp = (addr & ~3); /* get lower word aligned address */
  332. /*
  333. * handle unaligned start bytes
  334. */
  335. if ((l = addr - wp) != 0) {
  336. data = 0;
  337. for (i=0, cp=wp; i<l; ++i, ++cp) {
  338. data = (data << 8) | (*(uchar *)cp);
  339. }
  340. for (; i<4 && cnt>0; ++i) {
  341. data = (data << 8) | *src++;
  342. --cnt;
  343. ++cp;
  344. }
  345. for (; cnt==0 && i<4; ++i, ++cp) {
  346. data = (data << 8) | (*(uchar *)cp);
  347. }
  348. if ((rc = write_word(info, wp, data)) != 0) {
  349. return (rc);
  350. }
  351. wp += 4;
  352. }
  353. /*
  354. * handle word aligned part
  355. */
  356. while (cnt >= 4) {
  357. data = 0;
  358. for (i=0; i<4; ++i) {
  359. data = (data << 8) | *src++;
  360. }
  361. if ((rc = write_word(info, wp, data)) != 0) {
  362. return (rc);
  363. }
  364. wp += 4;
  365. cnt -= 4;
  366. }
  367. if (cnt == 0) {
  368. return (0);
  369. }
  370. /*
  371. * handle unaligned tail bytes
  372. */
  373. data = 0;
  374. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  375. data = (data << 8) | *src++;
  376. --cnt;
  377. }
  378. for (; i<4; ++i, ++cp) {
  379. data = (data << 8) | (*(uchar *)cp);
  380. }
  381. return (write_word(info, wp, data));
  382. }
  383. /*-----------------------------------------------------------------------
  384. * Write a word to Flash, returns:
  385. * 0 - OK
  386. * 1 - write timeout
  387. * 2 - Flash not erased
  388. */
  389. static int write_word (flash_info_t *info, ulong dest, ulong data)
  390. {
  391. vu_long *addr = (vu_long *)dest;
  392. ulong start, csr;
  393. int flag;
  394. /* Check if Flash is (sufficiently) erased */
  395. if ((*addr & data) != data) {
  396. return (2);
  397. }
  398. /* Disable interrupts which might cause a timeout here */
  399. flag = disable_interrupts();
  400. /* Write Command */
  401. *addr = 0x10101010;
  402. asm("sync");
  403. /* Write Data */
  404. *addr = data;
  405. /* re-enable interrupts if necessary */
  406. if (flag)
  407. enable_interrupts();
  408. /* data polling for D7 */
  409. start = get_timer (0);
  410. flag = 0;
  411. while (((csr = *addr) & 0x00800080) != 0x00800080) {
  412. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  413. flag = 1;
  414. break;
  415. }
  416. }
  417. if (csr & 0x40404040) {
  418. printf ("CSR indicates write error (%08lx) at %08lx\n", csr, (ulong)addr);
  419. flag = 1;
  420. }
  421. /* Clear Status Registers Command */
  422. *addr = 0x50505050;
  423. asm("sync");
  424. /* Reset to read array mode */
  425. *addr = 0xFFFFFFFF;
  426. asm("sync");
  427. return (flag);
  428. }
  429. /*-----------------------------------------------------------------------
  430. * Clear Block Lock Bit, returns:
  431. * 0 - OK
  432. * 1 - Timeout
  433. */
  434. static int clear_block_lock_bit(vu_long * addr)
  435. {
  436. ulong start, now;
  437. /* Reset Array */
  438. *addr = 0xffffffff;
  439. asm("sync");
  440. /* Clear Status Register */
  441. *addr = 0x50505050;
  442. asm("sync");
  443. *addr = 0x60606060;
  444. asm("sync");
  445. *addr = 0xd0d0d0d0;
  446. asm("sync");
  447. start = get_timer (0);
  448. while((*addr & 0x00800080) != 0x00800080){
  449. if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  450. printf ("Timeout on clearing Block Lock Bit\n");
  451. *addr = 0xFFFFFFFF; /* reset bank */
  452. asm("sync");
  453. return 1;
  454. }
  455. }
  456. return 0;
  457. }
  458. #endif /* !CFG_NO_FLASH */