flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * (C) Copyright 2000, 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2001, Stuart Hughes, Lineo Inc, stuarth@lineo.com
  6. * Add support the Sharp chips on the mpc8260ads.
  7. * I started with board/ip860/flash.c and made changes I found in
  8. * the MTD project by David Schleef.
  9. *
  10. * (C) Copyright 2003 Arabella Software Ltd.
  11. * Yuli Barcohen <yuli@arabellasw.com>
  12. * Re-written to support multi-bank flash SIMMs.
  13. * Added support for real protection and JFFS2.
  14. *
  15. * SPDX-License-Identifier: GPL-2.0+
  16. */
  17. #include <common.h>
  18. /* Intel-compatible flash ID */
  19. #define INTEL_COMPAT 0x89898989
  20. #define INTEL_ALT 0xB0B0B0B0
  21. /* Intel-compatible flash commands */
  22. #define INTEL_PROGRAM 0x10101010
  23. #define INTEL_ERASE 0x20202020
  24. #define INTEL_CLEAR 0x50505050
  25. #define INTEL_LOCKBIT 0x60606060
  26. #define INTEL_PROTECT 0x01010101
  27. #define INTEL_STATUS 0x70707070
  28. #define INTEL_READID 0x90909090
  29. #define INTEL_CONFIRM 0xD0D0D0D0
  30. #define INTEL_RESET 0xFFFFFFFF
  31. /* Intel-compatible flash status bits */
  32. #define INTEL_FINISHED 0x80808080
  33. #define INTEL_OK 0x80808080
  34. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  35. /*-----------------------------------------------------------------------
  36. * This board supports 32-bit wide flash SIMMs (4x8-bit configuration.)
  37. * Up to 32MB of flash supported (up to 4 banks.)
  38. * BCSR is used for flash presence detect (page 4-65 of the User's Manual)
  39. *
  40. * The following code can not run from flash!
  41. */
  42. unsigned long flash_init (void)
  43. {
  44. ulong size = 0, sect_start, sect_size = 0, bank_size;
  45. ushort sect_count = 0;
  46. int i, j, nbanks;
  47. vu_long *addr = (vu_long *)CONFIG_SYS_FLASH_BASE;
  48. vu_long *bcsr = (vu_long *)CONFIG_SYS_BCSR;
  49. switch (bcsr[2] & 0xF) {
  50. case 0:
  51. nbanks = 4;
  52. break;
  53. case 1:
  54. nbanks = 2;
  55. break;
  56. case 2:
  57. nbanks = 1;
  58. break;
  59. default: /* Unsupported configurations */
  60. nbanks = CONFIG_SYS_MAX_FLASH_BANKS;
  61. }
  62. if (nbanks > CONFIG_SYS_MAX_FLASH_BANKS)
  63. nbanks = CONFIG_SYS_MAX_FLASH_BANKS;
  64. for (i = 0; i < nbanks; i++) {
  65. *addr = INTEL_READID; /* Read Intelligent Identifier */
  66. if ((addr[0] == INTEL_COMPAT) || (addr[0] == INTEL_ALT)) {
  67. switch (addr[1]) {
  68. case SHARP_ID_28F016SCL:
  69. case SHARP_ID_28F016SCZ:
  70. flash_info[i].flash_id = FLASH_MAN_SHARP | FLASH_LH28F016SCT;
  71. sect_count = 32;
  72. sect_size = 0x40000;
  73. break;
  74. default:
  75. flash_info[i].flash_id = FLASH_UNKNOWN;
  76. sect_count = CONFIG_SYS_MAX_FLASH_SECT;
  77. sect_size =
  78. CONFIG_SYS_FLASH_SIZE / CONFIG_SYS_MAX_FLASH_BANKS / CONFIG_SYS_MAX_FLASH_SECT;
  79. }
  80. }
  81. else
  82. flash_info[i].flash_id = FLASH_UNKNOWN;
  83. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  84. printf("### Unknown flash ID %08lX %08lX at address %08lX ###\n",
  85. addr[0], addr[1], (ulong)addr);
  86. size = 0;
  87. *addr = INTEL_RESET; /* Reset bank to Read Array mode */
  88. break;
  89. }
  90. flash_info[i].sector_count = sect_count;
  91. flash_info[i].size = bank_size = sect_size * sect_count;
  92. size += bank_size;
  93. sect_start = (ulong)addr;
  94. for (j = 0; j < sect_count; j++) {
  95. addr = (vu_long *)sect_start;
  96. flash_info[i].start[j] = sect_start;
  97. flash_info[i].protect[j] = (addr[2] == 0x01010101);
  98. sect_start += sect_size;
  99. }
  100. *addr = INTEL_RESET; /* Reset bank to Read Array mode */
  101. addr = (vu_long *)sect_start;
  102. }
  103. if (size == 0) { /* Unknown flash, fill with hard-coded values */
  104. sect_start = CONFIG_SYS_FLASH_BASE;
  105. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  106. flash_info[i].flash_id = FLASH_UNKNOWN;
  107. flash_info[i].size = CONFIG_SYS_FLASH_SIZE / CONFIG_SYS_MAX_FLASH_BANKS;
  108. flash_info[i].sector_count = sect_count;
  109. for (j = 0; j < sect_count; j++) {
  110. flash_info[i].start[j] = sect_start;
  111. flash_info[i].protect[j] = 0;
  112. sect_start += sect_size;
  113. }
  114. }
  115. size = CONFIG_SYS_FLASH_SIZE;
  116. }
  117. else
  118. for (i = nbanks; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  119. flash_info[i].flash_id = FLASH_UNKNOWN;
  120. flash_info[i].size = 0;
  121. flash_info[i].sector_count = 0;
  122. }
  123. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
  124. /* monitor protection ON by default */
  125. flash_protect(FLAG_PROTECT_SET,
  126. CONFIG_SYS_MONITOR_BASE,
  127. CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
  128. &flash_info[0]);
  129. #endif
  130. #ifdef CONFIG_ENV_IS_IN_FLASH
  131. /* ENV protection ON by default */
  132. flash_protect(FLAG_PROTECT_SET,
  133. CONFIG_ENV_ADDR,
  134. CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
  135. &flash_info[0]);
  136. #endif
  137. return (size);
  138. }
  139. /*-----------------------------------------------------------------------
  140. */
  141. void flash_print_info (flash_info_t *info)
  142. {
  143. int i;
  144. if (info->flash_id == FLASH_UNKNOWN) {
  145. printf ("missing or unknown FLASH type\n");
  146. return;
  147. }
  148. switch (info->flash_id & FLASH_VENDMASK) {
  149. case FLASH_MAN_INTEL: printf ("Intel "); break;
  150. case FLASH_MAN_SHARP: printf ("Sharp "); break;
  151. default: printf ("Unknown Vendor "); break;
  152. }
  153. switch (info->flash_id & FLASH_TYPEMASK) {
  154. case FLASH_28F016SV: printf ("28F016SV (16 Mbit, 32 x 64k)\n");
  155. break;
  156. case FLASH_28F160S3: printf ("28F160S3 (16 Mbit, 32 x 512K)\n");
  157. break;
  158. case FLASH_28F320S3: printf ("28F320S3 (32 Mbit, 64 x 512K)\n");
  159. break;
  160. case FLASH_LH28F016SCT: printf ("28F016SC (16 Mbit, 32 x 64K)\n");
  161. break;
  162. default: printf ("Unknown Chip Type\n");
  163. break;
  164. }
  165. printf (" Size: %ld MB in %d Sectors\n",
  166. info->size >> 20, info->sector_count);
  167. printf (" Sector Start Addresses:");
  168. for (i=0; i<info->sector_count; ++i) {
  169. if ((i % 5) == 0)
  170. printf ("\n ");
  171. printf (" %08lX%s",
  172. info->start[i],
  173. info->protect[i] ? " (RO)" : " "
  174. );
  175. }
  176. printf ("\n");
  177. }
  178. /*-----------------------------------------------------------------------
  179. */
  180. int flash_erase (flash_info_t *info, int s_first, int s_last)
  181. {
  182. int flag, prot, sect;
  183. ulong start, now, last;
  184. if ((s_first < 0) || (s_first > s_last)) {
  185. if (info->flash_id == FLASH_UNKNOWN) {
  186. printf ("- missing\n");
  187. } else {
  188. printf ("- no sectors to erase\n");
  189. }
  190. return 1;
  191. }
  192. if ( ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL)
  193. && ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_SHARP) ) {
  194. printf ("Can't erase unknown flash type %08lx - aborted\n",
  195. info->flash_id);
  196. return 1;
  197. }
  198. prot = 0;
  199. for (sect=s_first; sect<=s_last; ++sect) {
  200. if (info->protect[sect]) {
  201. prot++;
  202. }
  203. }
  204. if (prot) {
  205. printf ("- Warning: %d protected sectors will not be erased!\n",
  206. prot);
  207. } else {
  208. printf ("\n");
  209. }
  210. /* Start erase on unprotected sectors */
  211. for (sect = s_first; sect<=s_last; sect++) {
  212. if (info->protect[sect] == 0) { /* not protected */
  213. vu_long *addr = (vu_long *)(info->start[sect]);
  214. last = start = get_timer (0);
  215. /* Disable interrupts which might cause a timeout here */
  216. flag = disable_interrupts();
  217. /* Clear Status Register */
  218. *addr = INTEL_CLEAR;
  219. /* Single Block Erase Command */
  220. *addr = INTEL_ERASE;
  221. /* Confirm */
  222. *addr = INTEL_CONFIRM;
  223. if((info->flash_id & FLASH_TYPEMASK) != FLASH_LH28F016SCT) {
  224. /* Resume Command, as per errata update */
  225. *addr = INTEL_CONFIRM;
  226. }
  227. /* re-enable interrupts if necessary */
  228. if (flag)
  229. enable_interrupts();
  230. while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
  231. if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  232. printf ("Timeout\n");
  233. *addr = INTEL_RESET; /* reset bank */
  234. return 1;
  235. }
  236. /* show that we're waiting */
  237. if ((now - last) > 1000) { /* every second */
  238. putc ('.');
  239. last = now;
  240. }
  241. }
  242. if (*addr != INTEL_OK) {
  243. printf("Block erase failed at %08X, CSR=%08X\n",
  244. (uint)addr, (uint)*addr);
  245. *addr = INTEL_RESET; /* reset bank */
  246. return 1;
  247. }
  248. /* reset to read mode */
  249. *addr = INTEL_RESET;
  250. }
  251. }
  252. printf (" done\n");
  253. return 0;
  254. }
  255. /*-----------------------------------------------------------------------
  256. * Write a word to Flash, returns:
  257. * 0 - OK
  258. * 1 - write timeout
  259. * 2 - Flash not erased
  260. */
  261. static int write_word (flash_info_t *info, ulong dest, ulong data)
  262. {
  263. ulong start;
  264. int rc = 0;
  265. int flag;
  266. vu_long *addr = (vu_long *)dest;
  267. /* Check if Flash is (sufficiently) erased */
  268. if ((*addr & data) != data) {
  269. return (2);
  270. }
  271. *addr = INTEL_CLEAR; /* Clear status register */
  272. /* Disable interrupts which might cause a timeout here */
  273. flag = disable_interrupts();
  274. /* Write Command */
  275. *addr = INTEL_PROGRAM;
  276. /* Write Data */
  277. *addr = data;
  278. /* re-enable interrupts if necessary */
  279. if (flag)
  280. enable_interrupts();
  281. /* data polling for D7 */
  282. start = get_timer (0);
  283. while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
  284. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  285. printf("Write timed out\n");
  286. rc = 1;
  287. break;
  288. }
  289. }
  290. if (*addr != INTEL_OK) {
  291. printf ("Write failed at %08X, CSR=%08X\n", (uint)addr, (uint)*addr);
  292. rc = 1;
  293. }
  294. *addr = INTEL_RESET; /* Reset to read array mode */
  295. return rc;
  296. }
  297. /*-----------------------------------------------------------------------
  298. * Copy memory to flash, returns:
  299. * 0 - OK
  300. * 1 - write timeout
  301. * 2 - Flash not erased
  302. */
  303. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  304. {
  305. ulong cp, wp, data;
  306. int i, l, rc;
  307. wp = (addr & ~3); /* get lower word aligned address */
  308. *(vu_long *)wp = INTEL_RESET; /* Reset to read array mode */
  309. /*
  310. * handle unaligned start bytes
  311. */
  312. if ((l = addr - wp) != 0) {
  313. data = 0;
  314. for (i=0, cp=wp; i<l; ++i, ++cp) {
  315. data = (data << 8) | (*(uchar *)cp);
  316. }
  317. for (; i<4 && cnt>0; ++i) {
  318. data = (data << 8) | *src++;
  319. --cnt;
  320. ++cp;
  321. }
  322. for (; cnt==0 && i<4; ++i, ++cp) {
  323. data = (data << 8) | (*(uchar *)cp);
  324. }
  325. if ((rc = write_word(info, wp, data)) != 0) {
  326. return (rc);
  327. }
  328. wp += 4;
  329. }
  330. /*
  331. * handle word aligned part
  332. */
  333. while (cnt >= 4) {
  334. data = 0;
  335. for (i=0; i<4; ++i) {
  336. data = (data << 8) | *src++;
  337. }
  338. if ((rc = write_word(info, wp, data)) != 0) {
  339. return (rc);
  340. }
  341. wp += 4;
  342. cnt -= 4;
  343. }
  344. if (cnt == 0) {
  345. return (0);
  346. }
  347. /*
  348. * handle unaligned tail bytes
  349. */
  350. data = 0;
  351. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  352. data = (data << 8) | *src++;
  353. --cnt;
  354. }
  355. for (; i<4; ++i, ++cp) {
  356. data = (data << 8) | (*(uchar *)cp);
  357. }
  358. rc = write_word(info, wp, data);
  359. return rc;
  360. }
  361. /*-----------------------------------------------------------------------
  362. * Set/Clear sector's lock bit, returns:
  363. * 0 - OK
  364. * 1 - Error (timeout, voltage problems, etc.)
  365. */
  366. int flash_real_protect(flash_info_t *info, long sector, int prot)
  367. {
  368. ulong start;
  369. int i;
  370. int rc = 0;
  371. vu_long *addr = (vu_long *)(info->start[sector]);
  372. int flag = disable_interrupts();
  373. *addr = INTEL_CLEAR; /* Clear status register */
  374. if (prot) { /* Set sector lock bit */
  375. *addr = INTEL_LOCKBIT; /* Sector lock bit */
  376. *addr = INTEL_PROTECT; /* set */
  377. }
  378. else { /* Clear sector lock bit */
  379. *addr = INTEL_LOCKBIT; /* All sectors lock bits */
  380. *addr = INTEL_CONFIRM; /* clear */
  381. }
  382. start = get_timer(0);
  383. while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
  384. if (get_timer(start) > CONFIG_SYS_FLASH_UNLOCK_TOUT) {
  385. printf("Flash lock bit operation timed out\n");
  386. rc = 1;
  387. break;
  388. }
  389. }
  390. if (*addr != INTEL_OK) {
  391. printf("Flash lock bit operation failed at %08X, CSR=%08X\n",
  392. (uint)addr, (uint)*addr);
  393. rc = 1;
  394. }
  395. if (!rc)
  396. info->protect[sector] = prot;
  397. /*
  398. * Clear lock bit command clears all sectors lock bits, so
  399. * we have to restore lock bits of protected sectors.
  400. */
  401. if (!prot)
  402. for (i = 0; i < info->sector_count; i++)
  403. if (info->protect[i]) {
  404. addr = (vu_long *)(info->start[i]);
  405. *addr = INTEL_LOCKBIT; /* Sector lock bit */
  406. *addr = INTEL_PROTECT; /* set */
  407. udelay(CONFIG_SYS_FLASH_LOCK_TOUT * 1000);
  408. }
  409. if (flag)
  410. enable_interrupts();
  411. *addr = INTEL_RESET; /* Reset to read array mode */
  412. return rc;
  413. }