flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 !defined(CONFIG_RAM_AS_FLASH)
  75. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  76. /* monitor protection ON by default */
  77. flash_protect(FLAG_PROTECT_SET,
  78. CFG_MONITOR_BASE,
  79. CFG_MONITOR_BASE+monitor_flash_len-1,
  80. &flash_info[0]);
  81. #endif
  82. #ifdef CFG_ENV_IS_IN_FLASH
  83. /* ENV protection ON by default */
  84. flash_protect(FLAG_PROTECT_SET,
  85. CFG_ENV_ADDR,
  86. CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
  87. &flash_info[0]);
  88. #endif
  89. #endif
  90. return (size);
  91. }
  92. /*-----------------------------------------------------------------------
  93. */
  94. void flash_print_info (flash_info_t *info)
  95. {
  96. int i;
  97. if (info->flash_id == FLASH_UNKNOWN) {
  98. printf ("missing or unknown FLASH type\n");
  99. return;
  100. }
  101. switch (info->flash_id & FLASH_VENDMASK) {
  102. case FLASH_MAN_INTEL: printf ("Intel "); break;
  103. case FLASH_MAN_SHARP: printf ("Sharp "); break;
  104. default: printf ("Unknown Vendor "); break;
  105. }
  106. switch (info->flash_id & FLASH_TYPEMASK) {
  107. case FLASH_28F016SV: printf ("28F016SV (16 Mbit, 32 x 64k)\n");
  108. break;
  109. case FLASH_28F160S3: printf ("28F160S3 (16 Mbit, 32 x 512K)\n");
  110. break;
  111. case FLASH_28F320S3: printf ("28F320S3 (32 Mbit, 64 x 512K)\n");
  112. break;
  113. case FLASH_LH28F016SCT: printf ("28F016SC (16 Mbit, 32 x 64K)\n");
  114. break;
  115. case FLASH_28F640J3A: printf ("28F640J3A (64 Mbit, 64 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 SHARP_ID_28F016SCL:
  195. case SHARP_ID_28F016SCZ:
  196. info->flash_id = FLASH_MAN_SHARP | FLASH_LH28F016SCT;
  197. info->sector_count = 32;
  198. info->size = 0x00800000;
  199. sector_offset = 0x40000;
  200. break; /* => 4x2 MB */
  201. default:
  202. info->flash_id = FLASH_UNKNOWN;
  203. return (0); /* => no or unknown flash */
  204. }
  205. /* set up sector start address table */
  206. for (i = 0; i < info->sector_count; i++) {
  207. info->start[i] = base;
  208. base += sector_offset;
  209. /* don't know how to check sector protection */
  210. info->protect[i] = 0;
  211. }
  212. /*
  213. * Prevent writes to uninitialized FLASH.
  214. */
  215. if (info->flash_id != FLASH_UNKNOWN) {
  216. addr = (vu_long *)info->start[0];
  217. *addr = 0xFFFFFF; /* reset bank to read array mode */
  218. asm("sync");
  219. }
  220. return (info->size);
  221. }
  222. /*-----------------------------------------------------------------------
  223. */
  224. int flash_erase (flash_info_t *info, int s_first, int s_last)
  225. {
  226. int flag, prot, sect;
  227. ulong start, now, last;
  228. if ((s_first < 0) || (s_first > s_last)) {
  229. if (info->flash_id == FLASH_UNKNOWN) {
  230. printf ("- missing\n");
  231. } else {
  232. printf ("- no sectors to erase\n");
  233. }
  234. return 1;
  235. }
  236. if ( ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL)
  237. && ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_SHARP) ) {
  238. printf ("Can't erase unknown flash type %08lx - aborted\n",
  239. info->flash_id);
  240. return 1;
  241. }
  242. prot = 0;
  243. for (sect=s_first; sect<=s_last; ++sect) {
  244. if (info->protect[sect]) {
  245. prot++;
  246. }
  247. }
  248. if (prot) {
  249. printf ("- Warning: %d protected sectors will not be erased!\n",
  250. prot);
  251. } else {
  252. printf ("\n");
  253. }
  254. #ifdef DEBUG
  255. printf("\nFlash Erase:\n");
  256. #endif
  257. /* Make Sure Block Lock Bit is not set. */
  258. if(clear_block_lock_bit((vu_long *)(info->start[s_first]))){
  259. return 1;
  260. }
  261. /* Start erase on unprotected sectors */
  262. #if defined(DEBUG)
  263. printf("Begin to erase now,s_first=0x%x s_last=0x%x...\n",s_first,s_last);
  264. #endif
  265. for (sect = s_first; sect<=s_last; sect++) {
  266. if (info->protect[sect] == 0) { /* not protected */
  267. vu_long *addr = (vu_long *)(info->start[sect]);
  268. asm("sync");
  269. last = start = get_timer (0);
  270. /* Disable interrupts which might cause a timeout here */
  271. flag = disable_interrupts();
  272. /* Reset Array */
  273. *addr = 0xffffffff;
  274. asm("sync");
  275. /* Clear Status Register */
  276. *addr = 0x50505050;
  277. asm("sync");
  278. /* Single Block Erase Command */
  279. *addr = 0x20202020;
  280. asm("sync");
  281. /* Confirm */
  282. *addr = 0xD0D0D0D0;
  283. asm("sync");
  284. if((info->flash_id & FLASH_TYPEMASK) != FLASH_LH28F016SCT) {
  285. /* Resume Command, as per errata update */
  286. *addr = 0xD0D0D0D0;
  287. asm("sync");
  288. }
  289. /* re-enable interrupts if necessary */
  290. if (flag)
  291. enable_interrupts();
  292. /* wait at least 80us - let's wait 1 ms */
  293. udelay (1000);
  294. while ((*addr & 0x00800080) != 0x00800080) {
  295. if(*addr & 0x00200020){
  296. printf("Error in Block Erase - Lock Bit may be set!\n");
  297. printf("Status Register = 0x%X\n", (uint)*addr);
  298. *addr = 0xFFFFFFFF; /* reset bank */
  299. asm("sync");
  300. return 1;
  301. }
  302. if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  303. printf ("Timeout\n");
  304. *addr = 0xFFFFFFFF; /* reset bank */
  305. asm("sync");
  306. return 1;
  307. }
  308. /* show that we're waiting */
  309. if ((now - last) > 1000) { /* every second */
  310. putc ('.');
  311. last = now;
  312. }
  313. }
  314. /* reset to read mode */
  315. *addr = 0xFFFFFFFF;
  316. asm("sync");
  317. }
  318. }
  319. printf ("flash erase done\n");
  320. return 0;
  321. }
  322. /*-----------------------------------------------------------------------
  323. * Copy memory to flash, returns:
  324. * 0 - OK
  325. * 1 - write timeout
  326. * 2 - Flash not erased
  327. */
  328. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  329. {
  330. ulong cp, wp, data;
  331. int i, l, rc;
  332. wp = (addr & ~3); /* get lower word aligned address */
  333. /*
  334. * handle unaligned start bytes
  335. */
  336. if ((l = addr - wp) != 0) {
  337. data = 0;
  338. for (i=0, cp=wp; i<l; ++i, ++cp) {
  339. data = (data << 8) | (*(uchar *)cp);
  340. }
  341. for (; i<4 && cnt>0; ++i) {
  342. data = (data << 8) | *src++;
  343. --cnt;
  344. ++cp;
  345. }
  346. for (; cnt==0 && i<4; ++i, ++cp) {
  347. data = (data << 8) | (*(uchar *)cp);
  348. }
  349. if ((rc = write_word(info, wp, data)) != 0) {
  350. return (rc);
  351. }
  352. wp += 4;
  353. }
  354. /*
  355. * handle word aligned part
  356. */
  357. while (cnt >= 4) {
  358. data = 0;
  359. for (i=0; i<4; ++i) {
  360. data = (data << 8) | *src++;
  361. }
  362. if ((rc = write_word(info, wp, data)) != 0) {
  363. return (rc);
  364. }
  365. wp += 4;
  366. cnt -= 4;
  367. }
  368. if (cnt == 0) {
  369. return (0);
  370. }
  371. /*
  372. * handle unaligned tail bytes
  373. */
  374. data = 0;
  375. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  376. data = (data << 8) | *src++;
  377. --cnt;
  378. }
  379. for (; i<4; ++i, ++cp) {
  380. data = (data << 8) | (*(uchar *)cp);
  381. }
  382. return (write_word(info, wp, data));
  383. }
  384. /*-----------------------------------------------------------------------
  385. * Write a word to Flash, returns:
  386. * 0 - OK
  387. * 1 - write timeout
  388. * 2 - Flash not erased
  389. */
  390. static int write_word (flash_info_t *info, ulong dest, ulong data)
  391. {
  392. vu_long *addr = (vu_long *)dest;
  393. ulong start, csr;
  394. int flag;
  395. /* Check if Flash is (sufficiently) erased */
  396. if ((*addr & data) != data) {
  397. return (2);
  398. }
  399. /* Disable interrupts which might cause a timeout here */
  400. flag = disable_interrupts();
  401. /* Write Command */
  402. *addr = 0x10101010;
  403. asm("sync");
  404. /* Write Data */
  405. *addr = data;
  406. /* re-enable interrupts if necessary */
  407. if (flag)
  408. enable_interrupts();
  409. /* data polling for D7 */
  410. start = get_timer (0);
  411. flag = 0;
  412. while (((csr = *addr) & 0x00800080) != 0x00800080) {
  413. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  414. flag = 1;
  415. break;
  416. }
  417. }
  418. if (csr & 0x40404040) {
  419. printf ("CSR indicates write error (%08lx) at %08lx\n", csr, (ulong)addr);
  420. flag = 1;
  421. }
  422. /* Clear Status Registers Command */
  423. *addr = 0x50505050;
  424. asm("sync");
  425. /* Reset to read array mode */
  426. *addr = 0xFFFFFFFF;
  427. asm("sync");
  428. return (flag);
  429. }
  430. /*-----------------------------------------------------------------------
  431. * Clear Block Lock Bit, returns:
  432. * 0 - OK
  433. * 1 - Timeout
  434. */
  435. static int clear_block_lock_bit(vu_long * addr)
  436. {
  437. ulong start, now;
  438. /* Reset Array */
  439. *addr = 0xffffffff;
  440. asm("sync");
  441. /* Clear Status Register */
  442. *addr = 0x50505050;
  443. asm("sync");
  444. *addr = 0x60606060;
  445. asm("sync");
  446. *addr = 0xd0d0d0d0;
  447. asm("sync");
  448. start = get_timer (0);
  449. while((*addr & 0x00800080) != 0x00800080){
  450. if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  451. printf ("Timeout on clearing Block Lock Bit\n");
  452. *addr = 0xFFFFFFFF; /* reset bank */
  453. asm("sync");
  454. return 1;
  455. }
  456. }
  457. return 0;
  458. }
  459. #endif /* !CFG_NO_FLASH */