flash.c 12 KB

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