flash.c 12 KB

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