flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 /* 2 x 8 MB */
  31. #define MAIN_SECT_SIZE 0x40000 /* 2 x 128 kB */
  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. goto Done;
  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. Done: ;
  130. }
  131. /*-----------------------------------------------------------------------
  132. */
  133. int flash_error (ulong code)
  134. {
  135. /* Check bit patterns */
  136. /* SR.7=0 is busy, SR.7=1 is ready */
  137. /* all other flags indicate error on 1 */
  138. /* SR.0 is undefined */
  139. /* Timeout is our faked flag */
  140. /* sequence is described in Intel 290644-005 document */
  141. /* check Timeout */
  142. if (code & BIT_TIMEOUT) {
  143. puts ("Timeout\n");
  144. return ERR_TIMOUT;
  145. }
  146. /* check Busy, SR.7 */
  147. if (~code & BIT_BUSY) {
  148. puts ("Busy\n");
  149. return ERR_PROG_ERROR;
  150. }
  151. /* check Vpp low, SR.3 */
  152. if (code & BIT_VPP_RANGE_ERROR) {
  153. puts ("Vpp range error\n");
  154. return ERR_PROG_ERROR;
  155. }
  156. /* check Device Protect Error, SR.1 */
  157. if (code & BIT_PROTECT_ERROR) {
  158. puts ("Device protect error\n");
  159. return ERR_PROG_ERROR;
  160. }
  161. /* check Command Seq Error, SR.4 & SR.5 */
  162. if (code & BIT_SEQUENCE_ERROR) {
  163. puts ("Command seqence error\n");
  164. return ERR_PROG_ERROR;
  165. }
  166. /* check Block Erase Error, SR.5 */
  167. if (code & BIT_ERASE_ERROR) {
  168. puts ("Block erase error\n");
  169. return ERR_PROG_ERROR;
  170. }
  171. /* check Program Error, SR.4 */
  172. if (code & BIT_PROGRAM_ERROR) {
  173. puts ("Program error\n");
  174. return ERR_PROG_ERROR;
  175. }
  176. /* check Block Erase Suspended, SR.6 */
  177. if (code & BIT_ERASE_SUSPEND) {
  178. puts ("Block erase suspended\n");
  179. return ERR_PROG_ERROR;
  180. }
  181. /* check Program Suspended, SR.2 */
  182. if (code & BIT_PROGRAM_SUSPEND) {
  183. puts ("Program suspended\n");
  184. return ERR_PROG_ERROR;
  185. }
  186. /* OK, no error */
  187. return ERR_OK;
  188. }
  189. /*-----------------------------------------------------------------------
  190. */
  191. int flash_erase (flash_info_t * info, int s_first, int s_last)
  192. {
  193. ulong result, result1;
  194. int iflag, prot, sect;
  195. int rc = ERR_OK;
  196. ulong start;
  197. #ifdef USE_920T_MMU
  198. int cflag;
  199. #endif
  200. debug ("flash_erase: s_first %d s_last %d\n", s_first, s_last);
  201. /* first look for protection bits */
  202. if (info->flash_id == FLASH_UNKNOWN)
  203. return ERR_UNKNOWN_FLASH_TYPE;
  204. if ((s_first < 0) || (s_first > s_last)) {
  205. return ERR_INVAL;
  206. }
  207. if ((info->flash_id & FLASH_VENDMASK) !=
  208. (INTEL_MANUFACT & FLASH_VENDMASK)) {
  209. return ERR_UNKNOWN_FLASH_VENDOR;
  210. }
  211. prot = 0;
  212. for (sect = s_first; sect <= s_last; ++sect) {
  213. if (info->protect[sect]) {
  214. prot++;
  215. }
  216. }
  217. if (prot) {
  218. printf ("- Warning: %d protected sectors will not be erased!\n",
  219. prot);
  220. } else {
  221. printf ("\n");
  222. }
  223. /*
  224. * Disable interrupts which might cause a timeout
  225. * here. Remember that our exception vectors are
  226. * at address 0 in the flash, and we don't want a
  227. * (ticker) exception to happen while the flash
  228. * chip is in programming mode.
  229. */
  230. #ifdef USE_920T_MMU
  231. cflag = dcache_status ();
  232. dcache_disable ();
  233. #endif
  234. iflag = disable_interrupts ();
  235. /* Start erase on unprotected sectors */
  236. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  237. debug ("Erasing sector %2d @ %08lX... ",
  238. sect, info->start[sect]);
  239. /* arm simple, non interrupt dependent timer */
  240. start = get_timer(0);
  241. if (info->protect[sect] == 0) { /* not protected */
  242. vu_long *addr = (vu_long *) (info->start[sect]);
  243. ulong bsR7, bsR7_2, bsR5, bsR5_2;
  244. /* *addr = CMD_STATUS_RESET; */
  245. *addr = CMD_ERASE_SETUP;
  246. *addr = CMD_ERASE_CONFIRM;
  247. /* wait until flash is ready */
  248. do {
  249. /* check timeout */
  250. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  251. *addr = CMD_STATUS_RESET;
  252. result = BIT_TIMEOUT;
  253. break;
  254. }
  255. *addr = CMD_STATUS_READ;
  256. result = *addr;
  257. bsR7 = result & (1 << 7);
  258. bsR7_2 = result & (1 << 23);
  259. } while (!bsR7 | !bsR7_2);
  260. *addr = CMD_STATUS_READ;
  261. result1 = *addr;
  262. bsR5 = result1 & (1 << 5);
  263. bsR5_2 = result1 & (1 << 21);
  264. #ifdef SAMSUNG_FLASH_DEBUG
  265. printf ("bsR5 %lx bsR5_2 %lx\n", bsR5, bsR5_2);
  266. if (bsR5 != 0 && bsR5_2 != 0)
  267. printf ("bsR5 %lx bsR5_2 %lx\n", bsR5, bsR5_2);
  268. #endif
  269. *addr = CMD_READ_ARRAY;
  270. *addr = CMD_RESUME;
  271. if ((rc = flash_error (result)) != ERR_OK)
  272. goto outahere;
  273. #if 0
  274. printf ("ok.\n");
  275. } else { /* it was protected */
  276. printf ("protected!\n");
  277. #endif
  278. }
  279. }
  280. outahere:
  281. /* allow flash to settle - wait 10 ms */
  282. udelay_masked (10000);
  283. if (iflag)
  284. enable_interrupts ();
  285. #ifdef USE_920T_MMU
  286. if (cflag)
  287. dcache_enable ();
  288. #endif
  289. return rc;
  290. }
  291. /*-----------------------------------------------------------------------
  292. * Copy memory to flash
  293. */
  294. static int write_word (flash_info_t * info, ulong dest, ulong data)
  295. {
  296. vu_long *addr = (vu_long *) dest;
  297. ulong result;
  298. int rc = ERR_OK;
  299. int iflag;
  300. ulong start;
  301. #ifdef USE_920T_MMU
  302. int cflag;
  303. #endif
  304. /*
  305. * Check if Flash is (sufficiently) erased
  306. */
  307. result = *addr;
  308. if ((result & data) != data)
  309. return ERR_NOT_ERASED;
  310. /*
  311. * Disable interrupts which might cause a timeout
  312. * here. Remember that our exception vectors are
  313. * at address 0 in the flash, and we don't want a
  314. * (ticker) exception to happen while the flash
  315. * chip is in programming mode.
  316. */
  317. #ifdef USE_920T_MMU
  318. cflag = dcache_status ();
  319. dcache_disable ();
  320. #endif
  321. iflag = disable_interrupts ();
  322. /* *addr = CMD_STATUS_RESET; */
  323. *addr = CMD_PROGRAM;
  324. *addr = data;
  325. /* arm simple, non interrupt dependent timer */
  326. start = get_timer(0);
  327. /* wait until flash is ready */
  328. do {
  329. /* check timeout */
  330. if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  331. *addr = CMD_SUSPEND;
  332. result = BIT_TIMEOUT;
  333. break;
  334. }
  335. *addr = CMD_STATUS_READ;
  336. result = *addr;
  337. } while (~result & BIT_BUSY);
  338. /* *addr = CMD_READ_ARRAY; */
  339. *addr = CMD_STATUS_READ;
  340. result = *addr;
  341. rc = flash_error (result);
  342. if (iflag)
  343. enable_interrupts ();
  344. #ifdef USE_920T_MMU
  345. if (cflag)
  346. dcache_enable ();
  347. #endif
  348. *addr = CMD_READ_ARRAY;
  349. *addr = CMD_RESUME;
  350. return rc;
  351. }
  352. /*-----------------------------------------------------------------------
  353. * Copy memory to flash.
  354. */
  355. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  356. {
  357. ulong cp, wp, data;
  358. int l;
  359. int i, rc;
  360. wp = (addr & ~3); /* get lower word aligned address */
  361. /*
  362. * handle unaligned start bytes
  363. */
  364. if ((l = addr - wp) != 0) {
  365. data = 0;
  366. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  367. data = (data >> 8) | (*(uchar *) cp << 24);
  368. }
  369. for (; i < 4 && cnt > 0; ++i) {
  370. data = (data >> 8) | (*src++ << 24);
  371. --cnt;
  372. ++cp;
  373. }
  374. for (; cnt == 0 && i < 4; ++i, ++cp) {
  375. data = (data >> 8) | (*(uchar *) cp << 24);
  376. }
  377. if ((rc = write_word (info, wp, data)) != 0) {
  378. return (rc);
  379. }
  380. wp += 4;
  381. }
  382. /*
  383. * handle word aligned part
  384. */
  385. while (cnt >= 4) {
  386. data = *((vu_long *) src);
  387. if ((rc = write_word (info, wp, data)) != 0) {
  388. return (rc);
  389. }
  390. src += 4;
  391. wp += 4;
  392. cnt -= 4;
  393. }
  394. if (cnt == 0) {
  395. return ERR_OK;
  396. }
  397. /*
  398. * handle unaligned tail bytes
  399. */
  400. data = 0;
  401. for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
  402. data = (data >> 8) | (*src++ << 24);
  403. --cnt;
  404. }
  405. for (; i < 4; ++i, ++cp) {
  406. data = (data >> 8) | (*(uchar *) cp << 24);
  407. }
  408. return write_word (info, wp, data);
  409. }