flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * (C) Copyright 2002
  3. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  4. * Marius Groeger <mgroeger@sysgo.de>
  5. *
  6. * (C) Copyright 2002
  7. * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. /* #define DEBUG */
  28. #include <common.h>
  29. #include <environment.h>
  30. #define FLASH_BANK_SIZE 0x1000000 /* 16MB (2 x 8 MB) */
  31. #define MAIN_SECT_SIZE 0x40000 /* 256KB (2 x 128kB) */
  32. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  33. #define CMD_READ_ARRAY 0x00FF00FF
  34. #define CMD_IDENTIFY 0x00900090
  35. #define CMD_ERASE_SETUP 0x00200020
  36. #define CMD_ERASE_CONFIRM 0x00D000D0
  37. #define CMD_PROGRAM 0x00400040
  38. #define CMD_RESUME 0x00D000D0
  39. #define CMD_SUSPEND 0x00B000B0
  40. #define CMD_STATUS_READ 0x00700070
  41. #define CMD_STATUS_RESET 0x00500050
  42. #define BIT_BUSY 0x00800080
  43. #define BIT_ERASE_SUSPEND 0x00400040
  44. #define BIT_ERASE_ERROR 0x00200020
  45. #define BIT_PROGRAM_ERROR 0x00100010
  46. #define BIT_VPP_RANGE_ERROR 0x00080008
  47. #define BIT_PROGRAM_SUSPEND 0x00040004
  48. #define BIT_PROTECT_ERROR 0x00020002
  49. #define BIT_UNDEFINED 0x00010001
  50. #define BIT_SEQUENCE_ERROR 0x00300030
  51. #define BIT_TIMEOUT 0x80000000
  52. /*-----------------------------------------------------------------------
  53. */
  54. ulong flash_init (void)
  55. {
  56. int i, j;
  57. ulong size = 0;
  58. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  59. ulong flashbase = 0;
  60. flash_info[i].flash_id =
  61. (INTEL_MANUFACT & FLASH_VENDMASK) |
  62. (INTEL_ID_28F640J3A & FLASH_TYPEMASK);
  63. flash_info[i].size = FLASH_BANK_SIZE;
  64. flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
  65. memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
  66. if (i == 0)
  67. flashbase = CFG_FLASH_BASE;
  68. else
  69. panic ("configured too many flash banks!\n");
  70. for (j = 0; j < flash_info[i].sector_count; j++) {
  71. flash_info[i].start[j] = flashbase;
  72. /* uniform sector size */
  73. flashbase += MAIN_SECT_SIZE;
  74. }
  75. size += flash_info[i].size;
  76. }
  77. /*
  78. * Protect monitor and environment sectors
  79. */
  80. flash_protect ( FLAG_PROTECT_SET,
  81. CFG_FLASH_BASE,
  82. CFG_FLASH_BASE + monitor_flash_len - 1,
  83. &flash_info[0]);
  84. flash_protect ( FLAG_PROTECT_SET,
  85. CFG_ENV_ADDR,
  86. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
  87. #ifdef CFG_ENV_ADDR_REDUND
  88. flash_protect ( FLAG_PROTECT_SET,
  89. CFG_ENV_ADDR_REDUND,
  90. CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
  91. &flash_info[0]);
  92. #endif
  93. return size;
  94. }
  95. /*-----------------------------------------------------------------------
  96. */
  97. void flash_print_info (flash_info_t * info)
  98. {
  99. int i;
  100. switch (info->flash_id & FLASH_VENDMASK) {
  101. case (INTEL_MANUFACT & FLASH_VENDMASK):
  102. printf ("Intel: ");
  103. break;
  104. default:
  105. printf ("Unknown Vendor ");
  106. break;
  107. }
  108. switch (info->flash_id & FLASH_TYPEMASK) {
  109. case (INTEL_ID_28F640J3A & FLASH_TYPEMASK):
  110. printf ("2x 28F640J3A (64Mbit)\n");
  111. break;
  112. default:
  113. printf ("Unknown Chip Type\n");
  114. return;
  115. break;
  116. }
  117. printf (" Size: %ld MB in %d Sectors\n",
  118. info->size >> 20, info->sector_count);
  119. printf (" Sector Start Addresses:");
  120. for (i = 0; i < info->sector_count; i++) {
  121. if ((i % 5) == 0) {
  122. printf ("\n ");
  123. }
  124. printf (" %08lX%s",
  125. info->start[i],
  126. info->protect[i] ? " (RO)" : " ");
  127. }
  128. printf ("\n");
  129. }
  130. /*-----------------------------------------------------------------------
  131. */
  132. int flash_error (ulong code)
  133. {
  134. /* Check bit patterns */
  135. /* SR.7=0 is busy, SR.7=1 is ready */
  136. /* all other flags indicate error on 1 */
  137. /* SR.0 is undefined */
  138. /* Timeout is our faked flag */
  139. /* sequence is described in Intel 290644-005 document */
  140. /* check Timeout */
  141. if (code & BIT_TIMEOUT) {
  142. puts ("Timeout\n");
  143. return ERR_TIMOUT;
  144. }
  145. /* check Busy, SR.7 */
  146. if (~code & BIT_BUSY) {
  147. puts ("Busy\n");
  148. return ERR_PROG_ERROR;
  149. }
  150. /* check Vpp low, SR.3 */
  151. if (code & BIT_VPP_RANGE_ERROR) {
  152. puts ("Vpp range error\n");
  153. return ERR_PROG_ERROR;
  154. }
  155. /* check Device Protect Error, SR.1 */
  156. if (code & BIT_PROTECT_ERROR) {
  157. puts ("Device protect error\n");
  158. return ERR_PROG_ERROR;
  159. }
  160. /* check Command Seq Error, SR.4 & SR.5 */
  161. if (code & BIT_SEQUENCE_ERROR) {
  162. puts ("Command seqence error\n");
  163. return ERR_PROG_ERROR;
  164. }
  165. /* check Block Erase Error, SR.5 */
  166. if (code & BIT_ERASE_ERROR) {
  167. puts ("Block erase error\n");
  168. return ERR_PROG_ERROR;
  169. }
  170. /* check Program Error, SR.4 */
  171. if (code & BIT_PROGRAM_ERROR) {
  172. puts ("Program error\n");
  173. return ERR_PROG_ERROR;
  174. }
  175. /* check Block Erase Suspended, SR.6 */
  176. if (code & BIT_ERASE_SUSPEND) {
  177. puts ("Block erase suspended\n");
  178. return ERR_PROG_ERROR;
  179. }
  180. /* check Program Suspended, SR.2 */
  181. if (code & BIT_PROGRAM_SUSPEND) {
  182. puts ("Program suspended\n");
  183. return ERR_PROG_ERROR;
  184. }
  185. /* OK, no error */
  186. return ERR_OK;
  187. }
  188. /*-----------------------------------------------------------------------
  189. */
  190. int flash_erase (flash_info_t * info, int s_first, int s_last)
  191. {
  192. ulong result, result1;
  193. int iflag, prot, sect;
  194. int rc = ERR_OK;
  195. #ifdef USE_920T_MMU
  196. int cflag;
  197. #endif
  198. debug ("flash_erase: s_first %d s_last %d\n", s_first, s_last);
  199. /* first look for protection bits */
  200. if (info->flash_id == FLASH_UNKNOWN)
  201. return ERR_UNKNOWN_FLASH_TYPE;
  202. if ((s_first < 0) || (s_first > s_last)) {
  203. return ERR_INVAL;
  204. }
  205. if ((info->flash_id & FLASH_VENDMASK) !=
  206. (INTEL_MANUFACT & FLASH_VENDMASK)) {
  207. return ERR_UNKNOWN_FLASH_VENDOR;
  208. }
  209. prot = 0;
  210. for (sect = s_first; sect <= s_last; ++sect) {
  211. if (info->protect[sect]) {
  212. prot++;
  213. }
  214. }
  215. if (prot) {
  216. printf ("- Warning: %d protected sectors will not be erased!\n",
  217. prot);
  218. } else {
  219. printf ("\n");
  220. }
  221. /*
  222. * Disable interrupts which might cause a timeout
  223. * here. Remember that our exception vectors are
  224. * at address 0 in the flash, and we don't want a
  225. * (ticker) exception to happen while the flash
  226. * chip is in programming mode.
  227. */
  228. #ifdef USE_920T_MMU
  229. cflag = dcache_status ();
  230. dcache_disable ();
  231. #endif
  232. iflag = disable_interrupts ();
  233. /* Start erase on unprotected sectors */
  234. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  235. debug ("Erasing sector %2d @ %08lX... ",
  236. sect, info->start[sect]);
  237. /* arm simple, non interrupt dependent timer */
  238. reset_timer();
  239. if (info->protect[sect] == 0) { /* not protected */
  240. vu_long *addr = (vu_long *) (info->start[sect]);
  241. ulong bsR7, bsR7_2, bsR5, bsR5_2;
  242. ulong tstart;
  243. /* *addr = CMD_STATUS_RESET; */
  244. *addr = CMD_ERASE_SETUP;
  245. *addr = CMD_ERASE_CONFIRM;
  246. /* wait until flash is ready */
  247. tstart = get_timer(0);
  248. do {
  249. ulong now;
  250. /* check timeout */
  251. //if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
  252. if ((now = get_timer(tstart)) > CFG_FLASH_ERASE_TOUT) {
  253. printf("tstart = 0x%08lx, now = 0x%08lx\n", tstart, now);
  254. *addr = CMD_STATUS_RESET;
  255. result = BIT_TIMEOUT;
  256. break;
  257. }
  258. *addr = CMD_STATUS_READ;
  259. result = *addr;
  260. bsR7 = result & (1 << 7);
  261. bsR7_2 = result & (1 << 23);
  262. } while (!bsR7 | !bsR7_2);
  263. *addr = CMD_STATUS_READ;
  264. result1 = *addr;
  265. bsR5 = result1 & (1 << 5);
  266. bsR5_2 = result1 & (1 << 21);
  267. #ifdef SAMSUNG_FLASH_DEBUG
  268. printf ("bsR5 %lx bsR5_2 %lx\n", bsR5, bsR5_2);
  269. if (bsR5 != 0 && bsR5_2 != 0)
  270. printf ("bsR5 %lx bsR5_2 %lx\n", bsR5, bsR5_2);
  271. #endif
  272. *addr = CMD_READ_ARRAY;
  273. *addr = CMD_RESUME;
  274. if ((rc = flash_error (result)) != ERR_OK)
  275. goto outahere;
  276. #if 0
  277. printf ("ok.\n");
  278. } else { /* it was protected */
  279. printf ("protected!\n");
  280. #endif
  281. }
  282. }
  283. outahere:
  284. /* allow flash to settle - wait 10 ms */
  285. udelay_masked (10000);
  286. if (iflag)
  287. enable_interrupts ();
  288. #ifdef USE_920T_MMU
  289. if (cflag)
  290. dcache_enable ();
  291. #endif
  292. return rc;
  293. }
  294. /*-----------------------------------------------------------------------
  295. * Copy memory to flash
  296. */
  297. volatile static int write_word (flash_info_t * info, ulong dest,
  298. ulong data)
  299. {
  300. vu_long *addr = (vu_long *) dest;
  301. ulong result;
  302. int rc = ERR_OK;
  303. int iflag;
  304. #ifdef USE_920T_MMU
  305. int cflag;
  306. #endif
  307. /*
  308. * Check if Flash is (sufficiently) erased
  309. */
  310. result = *addr;
  311. if ((result & data) != data)
  312. return ERR_NOT_ERASED;
  313. /*
  314. * Disable interrupts which might cause a timeout
  315. * here. Remember that our exception vectors are
  316. * at address 0 in the flash, and we don't want a
  317. * (ticker) exception to happen while the flash
  318. * chip is in programming mode.
  319. */
  320. #ifdef USE_920T_MMU
  321. cflag = dcache_status ();
  322. dcache_disable ();
  323. #endif
  324. iflag = disable_interrupts ();
  325. /* *addr = CMD_STATUS_RESET; */
  326. *addr = CMD_PROGRAM;
  327. *addr = data;
  328. /* arm simple, non interrupt dependent timer */
  329. reset_timer_masked ();
  330. /* wait until flash is ready */
  331. do {
  332. /* check timeout */
  333. if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
  334. *addr = CMD_SUSPEND;
  335. result = BIT_TIMEOUT;
  336. break;
  337. }
  338. *addr = CMD_STATUS_READ;
  339. result = *addr;
  340. } while (~result & BIT_BUSY);
  341. /* *addr = CMD_READ_ARRAY; */
  342. *addr = CMD_STATUS_READ;
  343. result = *addr;
  344. rc = flash_error (result);
  345. if (iflag)
  346. enable_interrupts ();
  347. #ifdef USE_920T_MMU
  348. if (cflag)
  349. dcache_enable ();
  350. #endif
  351. *addr = CMD_READ_ARRAY;
  352. *addr = CMD_RESUME;
  353. return rc;
  354. }
  355. /*-----------------------------------------------------------------------
  356. * Copy memory to flash.
  357. */
  358. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  359. {
  360. ulong cp, wp, data;
  361. int l;
  362. int i, rc;
  363. wp = (addr & ~3); /* get lower word aligned address */
  364. /*
  365. * handle unaligned start bytes
  366. */
  367. if ((l = addr - wp) != 0) {
  368. data = 0;
  369. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  370. data = (data >> 8) | (*(uchar *) cp << 24);
  371. }
  372. for (; i < 4 && cnt > 0; ++i) {
  373. data = (data >> 8) | (*src++ << 24);
  374. --cnt;
  375. ++cp;
  376. }
  377. for (; cnt == 0 && i < 4; ++i, ++cp) {
  378. data = (data >> 8) | (*(uchar *) cp << 24);
  379. }
  380. if ((rc = write_word (info, wp, data)) != 0) {
  381. return (rc);
  382. }
  383. wp += 4;
  384. }
  385. /*
  386. * handle word aligned part
  387. */
  388. while (cnt >= 4) {
  389. data = *((vu_long *) src);
  390. if ((rc = write_word (info, wp, data)) != 0) {
  391. return (rc);
  392. }
  393. src += 4;
  394. wp += 4;
  395. cnt -= 4;
  396. }
  397. if (cnt == 0) {
  398. return ERR_OK;
  399. }
  400. /*
  401. * handle unaligned tail bytes
  402. */
  403. data = 0;
  404. for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
  405. data = (data >> 8) | (*src++ << 24);
  406. --cnt;
  407. }
  408. for (; i < 4; ++i, ++cp) {
  409. data = (data >> 8) | (*(uchar *) cp << 24);
  410. }
  411. return write_word (info, wp, data);
  412. }