flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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, <garyj@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[CONFIG_SYS_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 < CONFIG_SYS_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 = CONFIG_SYS_MAX_FLASH_SECT;
  65. memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
  66. if (i == 0)
  67. flashbase = CONFIG_SYS_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. CONFIG_SYS_FLASH_BASE,
  82. CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
  83. &flash_info[0]);
  84. flash_protect ( FLAG_PROTECT_SET,
  85. CONFIG_ENV_ADDR,
  86. CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1, &flash_info[0]);
  87. #ifdef CONFIG_ENV_ADDR_REDUND
  88. flash_protect ( FLAG_PROTECT_SET,
  89. CONFIG_ENV_ADDR_REDUND,
  90. CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 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_masked();
  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. /* *addr = CMD_STATUS_RESET; */
  243. *addr = CMD_ERASE_SETUP;
  244. *addr = CMD_ERASE_CONFIRM;
  245. /* wait until flash is ready */
  246. do {
  247. /* check timeout */
  248. if (get_timer_masked () > CONFIG_SYS_FLASH_ERASE_TOUT) {
  249. *addr = CMD_STATUS_RESET;
  250. result = BIT_TIMEOUT;
  251. break;
  252. }
  253. *addr = CMD_STATUS_READ;
  254. result = *addr;
  255. bsR7 = result & (1 << 7);
  256. bsR7_2 = result & (1 << 23);
  257. } while (!bsR7 | !bsR7_2);
  258. *addr = CMD_STATUS_READ;
  259. result1 = *addr;
  260. bsR5 = result1 & (1 << 5);
  261. bsR5_2 = result1 & (1 << 21);
  262. #ifdef SAMSUNG_FLASH_DEBUG
  263. printf ("bsR5 %lx bsR5_2 %lx\n", bsR5, bsR5_2);
  264. if (bsR5 != 0 && bsR5_2 != 0)
  265. printf ("bsR5 %lx bsR5_2 %lx\n", bsR5, bsR5_2);
  266. #endif
  267. *addr = CMD_READ_ARRAY;
  268. *addr = CMD_RESUME;
  269. if ((rc = flash_error (result)) != ERR_OK)
  270. goto outahere;
  271. #if 0
  272. printf ("ok.\n");
  273. } else { /* it was protected */
  274. printf ("protected!\n");
  275. #endif
  276. }
  277. }
  278. outahere:
  279. /* allow flash to settle - wait 10 ms */
  280. udelay_masked (10000);
  281. if (iflag)
  282. enable_interrupts ();
  283. #ifdef USE_920T_MMU
  284. if (cflag)
  285. dcache_enable ();
  286. #endif
  287. return rc;
  288. }
  289. /*-----------------------------------------------------------------------
  290. * Copy memory to flash
  291. */
  292. static int write_word (flash_info_t * info, ulong dest, ulong data)
  293. {
  294. vu_long *addr = (vu_long *) dest;
  295. ulong result;
  296. int rc = ERR_OK;
  297. int iflag;
  298. #ifdef USE_920T_MMU
  299. int cflag;
  300. #endif
  301. /*
  302. * Check if Flash is (sufficiently) erased
  303. */
  304. result = *addr;
  305. if ((result & data) != data)
  306. return ERR_NOT_ERASED;
  307. /*
  308. * Disable interrupts which might cause a timeout
  309. * here. Remember that our exception vectors are
  310. * at address 0 in the flash, and we don't want a
  311. * (ticker) exception to happen while the flash
  312. * chip is in programming mode.
  313. */
  314. #ifdef USE_920T_MMU
  315. cflag = dcache_status ();
  316. dcache_disable ();
  317. #endif
  318. iflag = disable_interrupts ();
  319. /* *addr = CMD_STATUS_RESET; */
  320. *addr = CMD_PROGRAM;
  321. *addr = data;
  322. /* arm simple, non interrupt dependent timer */
  323. reset_timer_masked ();
  324. /* wait until flash is ready */
  325. do {
  326. /* check timeout */
  327. if (get_timer_masked () > CONFIG_SYS_FLASH_ERASE_TOUT) {
  328. *addr = CMD_SUSPEND;
  329. result = BIT_TIMEOUT;
  330. break;
  331. }
  332. *addr = CMD_STATUS_READ;
  333. result = *addr;
  334. } while (~result & BIT_BUSY);
  335. /* *addr = CMD_READ_ARRAY; */
  336. *addr = CMD_STATUS_READ;
  337. result = *addr;
  338. rc = flash_error (result);
  339. if (iflag)
  340. enable_interrupts ();
  341. #ifdef USE_920T_MMU
  342. if (cflag)
  343. dcache_enable ();
  344. #endif
  345. *addr = CMD_READ_ARRAY;
  346. *addr = CMD_RESUME;
  347. return rc;
  348. }
  349. /*-----------------------------------------------------------------------
  350. * Copy memory to flash.
  351. */
  352. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  353. {
  354. ulong cp, wp, data;
  355. int l;
  356. int i, rc;
  357. wp = (addr & ~3); /* get lower word aligned address */
  358. /*
  359. * handle unaligned start bytes
  360. */
  361. if ((l = addr - wp) != 0) {
  362. data = 0;
  363. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  364. data = (data >> 8) | (*(uchar *) cp << 24);
  365. }
  366. for (; i < 4 && cnt > 0; ++i) {
  367. data = (data >> 8) | (*src++ << 24);
  368. --cnt;
  369. ++cp;
  370. }
  371. for (; cnt == 0 && i < 4; ++i, ++cp) {
  372. data = (data >> 8) | (*(uchar *) cp << 24);
  373. }
  374. if ((rc = write_word (info, wp, data)) != 0) {
  375. return (rc);
  376. }
  377. wp += 4;
  378. }
  379. /*
  380. * handle word aligned part
  381. */
  382. while (cnt >= 4) {
  383. data = *((vu_long *) src);
  384. if ((rc = write_word (info, wp, data)) != 0) {
  385. return (rc);
  386. }
  387. src += 4;
  388. wp += 4;
  389. cnt -= 4;
  390. }
  391. if (cnt == 0) {
  392. return ERR_OK;
  393. }
  394. /*
  395. * handle unaligned tail bytes
  396. */
  397. data = 0;
  398. for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
  399. data = (data >> 8) | (*src++ << 24);
  400. --cnt;
  401. }
  402. for (; i < 4; ++i, ++cp) {
  403. data = (data >> 8) | (*(uchar *) cp << 24);
  404. }
  405. return write_word (info, wp, data);
  406. }