flash.c 12 KB

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