flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * (C) Copyright 2002
  3. * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /* #define DEBUG */
  24. #include <common.h>
  25. #include <environment.h>
  26. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  27. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  28. #define CMD_READ_ARRAY 0x00F000F0
  29. #define CMD_UNLOCK1 0x00AA00AA
  30. #define CMD_UNLOCK2 0x00550055
  31. #define CMD_ERASE_SETUP 0x00800080
  32. #define CMD_ERASE_CONFIRM 0x00300030
  33. #define CMD_PROGRAM 0x00A000A0
  34. #define CMD_UNLOCK_BYPASS 0x00200020
  35. #define CMD_READ_MANF_ID 0x00900090
  36. #define CMD_UNLOCK_BYPASS_RES1 0x00900090
  37. #define CMD_UNLOCK_BYPASS_RES2 0x00000000
  38. #define MEM_FLASH_ADDR (*(volatile u32 *)CFG_FLASH_BASE)
  39. #define MEM_FLASH_ADDR1 (*(volatile u32 *)(CFG_FLASH_BASE + (0x00000555 << 2)))
  40. #define MEM_FLASH_ADDR2 (*(volatile u32 *)(CFG_FLASH_BASE + (0x000002AA << 2)))
  41. #define BIT_ERASE_DONE 0x00800080
  42. #define BIT_RDY_MASK 0x00800080
  43. #define BIT_PROGRAM_ERROR 0x00200020
  44. #define BIT_TIMEOUT 0x80000000 /* our flag */
  45. #define READY 1
  46. #define ERR 2
  47. #define TMO 4
  48. /*-----------------------------------------------------------------------
  49. */
  50. ulong flash_init (void)
  51. {
  52. int i, j;
  53. ulong size = 0;
  54. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  55. ulong flashbase = 0;
  56. flash_info_t *info = &flash_info[i];
  57. /* Init: no FLASHes known */
  58. info->flash_id = FLASH_UNKNOWN;
  59. size += flash_get_size (CFG_FLASH_BASE, info);
  60. if (i == 0)
  61. flashbase = CFG_FLASH_BASE;
  62. else
  63. panic ("configured too many flash banks!\n");
  64. for (j = 0; j < info->sector_count; j++) {
  65. info->protect[j] = 0;
  66. info->start[j] = flashbase;
  67. switch (info->flash_id & FLASH_TYPEMASK) {
  68. case (FLASH_AM320B & FLASH_TYPEMASK):
  69. case (FLASH_MXLV320B & FLASH_TYPEMASK):
  70. /* Boot sector type: 8 x 8 + N x 128 kB */
  71. flashbase += (j < 8) ? 0x4000 : 0x20000;
  72. break;
  73. case (FLASH_AM640U & FLASH_TYPEMASK):
  74. /* Uniform sector type: 128 kB */
  75. flashbase += 0x20000;
  76. break;
  77. default:
  78. printf ("## Bad flash chip type 0x%04lX\n",
  79. info->flash_id & FLASH_TYPEMASK);
  80. }
  81. }
  82. }
  83. /*
  84. * Protect monitor and environment sectors
  85. */
  86. flash_protect ( FLAG_PROTECT_SET,
  87. CFG_FLASH_BASE,
  88. CFG_FLASH_BASE + monitor_flash_len - 1,
  89. &flash_info[0]);
  90. flash_protect ( FLAG_PROTECT_SET,
  91. CFG_ENV_ADDR,
  92. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
  93. #ifdef CFG_ENV_ADDR_REDUND
  94. flash_protect ( FLAG_PROTECT_SET,
  95. CFG_ENV_ADDR_REDUND,
  96. CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
  97. &flash_info[0]);
  98. #endif
  99. return size;
  100. }
  101. /*-----------------------------------------------------------------------
  102. */
  103. void flash_print_info (flash_info_t * info)
  104. {
  105. int i;
  106. switch (info->flash_id & FLASH_VENDMASK) {
  107. case (FLASH_MAN_AMD & FLASH_VENDMASK):
  108. printf ("AMD "); break;
  109. case (FLASH_MAN_FUJ & FLASH_VENDMASK):
  110. printf ("FUJITSU "); break;
  111. case (FLASH_MAN_MX & FLASH_VENDMASK):
  112. printf ("MACRONIX "); break;
  113. default: printf ("Unknown Vendor "); break;
  114. }
  115. switch (info->flash_id & FLASH_TYPEMASK) {
  116. case (FLASH_AM320B & FLASH_TYPEMASK):
  117. printf ("2x Am29LV320DB (32Mbit)\n");
  118. break;
  119. case (FLASH_MXLV320B & FLASH_TYPEMASK):
  120. printf ("2x MX29LV320DB (32Mbit)\n");
  121. break;
  122. case (FLASH_AM640U & FLASH_TYPEMASK):
  123. printf ("2x Am29LV640D (64Mbit)\n");
  124. break;
  125. default:
  126. printf ("Unknown Chip Type\n");
  127. goto Done;
  128. break;
  129. }
  130. printf (" Size: %ld MB in %d Sectors\n",
  131. info->size >> 20, info->sector_count);
  132. printf (" Sector Start Addresses:");
  133. for (i = 0; i < info->sector_count; i++) {
  134. if ((i % 5) == 0) {
  135. printf ("\n ");
  136. }
  137. printf (" %08lX%s",
  138. info->start[i],
  139. info->protect[i] ? " (RO)" : " ");
  140. }
  141. printf ("\n");
  142. Done: ;
  143. }
  144. /*-----------------------------------------------------------------------
  145. */
  146. int flash_erase (flash_info_t * info, int s_first, int s_last)
  147. {
  148. ulong result;
  149. #if 0
  150. int cflag;
  151. #endif
  152. int iflag, prot, sect;
  153. int rc = ERR_OK;
  154. int chip1, chip2;
  155. debug ("flash_erase: s_first %d s_last %d\n", s_first, s_last);
  156. /* first look for protection bits */
  157. if (info->flash_id == FLASH_UNKNOWN)
  158. return ERR_UNKNOWN_FLASH_TYPE;
  159. if ((s_first < 0) || (s_first > s_last)) {
  160. return ERR_INVAL;
  161. }
  162. switch (info->flash_id & FLASH_VENDMASK) {
  163. case (FLASH_MAN_AMD & FLASH_VENDMASK): break; /* OK */
  164. case (FLASH_MAN_FUJ & FLASH_VENDMASK): break; /* OK */
  165. case (FLASH_MAN_MX & FLASH_VENDMASK): break; /* OK */
  166. default:
  167. debug ("## flash_erase: unknown manufacturer\n");
  168. return (ERR_UNKNOWN_FLASH_VENDOR);
  169. }
  170. prot = 0;
  171. for (sect = s_first; sect <= s_last; ++sect) {
  172. if (info->protect[sect]) {
  173. prot++;
  174. }
  175. }
  176. if (prot) {
  177. printf ("- Warning: %d protected sectors will not be erased!\n",
  178. prot);
  179. } else {
  180. printf ("\n");
  181. }
  182. /*
  183. * Disable interrupts which might cause a timeout
  184. * here. Remember that our exception vectors are
  185. * at address 0 in the flash, and we don't want a
  186. * (ticker) exception to happen while the flash
  187. * chip is in programming mode.
  188. */
  189. #if 0
  190. cflag = icache_status ();
  191. icache_disable ();
  192. #endif
  193. iflag = disable_interrupts ();
  194. /* Start erase on unprotected sectors */
  195. for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
  196. debug ("Erasing sector %2d @ %08lX... ",
  197. sect, info->start[sect]);
  198. /* arm simple, non interrupt dependent timer */
  199. reset_timer_masked ();
  200. if (info->protect[sect] == 0) { /* not protected */
  201. vu_long *addr = (vu_long *) (info->start[sect]);
  202. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  203. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  204. MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
  205. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  206. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  207. *addr = CMD_ERASE_CONFIRM;
  208. /* wait until flash is ready */
  209. chip1 = chip2 = 0;
  210. do {
  211. result = *addr;
  212. /* check timeout */
  213. if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
  214. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  215. chip1 = TMO;
  216. break;
  217. }
  218. if (!chip1 && (result & 0xFFFF) & BIT_ERASE_DONE)
  219. chip1 = READY;
  220. if (!chip1 && (result & 0xFFFF) & BIT_PROGRAM_ERROR)
  221. chip1 = ERR;
  222. if (!chip2 && (result >> 16) & BIT_ERASE_DONE)
  223. chip2 = READY;
  224. if (!chip2 && (result >> 16) & BIT_PROGRAM_ERROR)
  225. chip2 = ERR;
  226. } while (!chip1 || !chip2);
  227. MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
  228. if (chip1 == ERR || chip2 == ERR) {
  229. rc = ERR_PROG_ERROR;
  230. printf ("Flash erase error\n");
  231. goto outahere;
  232. }
  233. if (chip1 == TMO) {
  234. rc = ERR_TIMOUT;
  235. printf ("Flash erase timeout error\n");
  236. goto outahere;
  237. }
  238. }
  239. }
  240. outahere:
  241. /* allow flash to settle - wait 10 ms */
  242. udelay_masked (10000);
  243. if (iflag)
  244. enable_interrupts ();
  245. #if 0
  246. if (cflag)
  247. icache_enable ();
  248. #endif
  249. return rc;
  250. }
  251. /*-----------------------------------------------------------------------
  252. * Copy memory to flash
  253. */
  254. static int write_word (flash_info_t * info, ulong dest, ulong data)
  255. {
  256. vu_long *addr = (vu_long *) dest;
  257. ulong result;
  258. int rc = ERR_OK;
  259. #if 0
  260. int cflag;
  261. #endif
  262. int iflag;
  263. int chip1, chip2;
  264. /*
  265. * Check if Flash is (sufficiently) erased
  266. */
  267. result = *addr;
  268. if ((result & data) != data)
  269. return ERR_NOT_ERASED;
  270. /*
  271. * Disable interrupts which might cause a timeout
  272. * here. Remember that our exception vectors are
  273. * at address 0 in the flash, and we don't want a
  274. * (ticker) exception to happen while the flash
  275. * chip is in programming mode.
  276. */
  277. #if 0
  278. cflag = icache_status ();
  279. icache_disable ();
  280. #endif
  281. iflag = disable_interrupts ();
  282. *addr = CMD_PROGRAM;
  283. *addr = data;
  284. /* arm simple, non interrupt dependent timer */
  285. reset_timer_masked ();
  286. /* wait until flash is ready */
  287. chip1 = chip2 = 0;
  288. do {
  289. result = *addr;
  290. /* check timeout */
  291. if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
  292. chip1 = ERR | TMO;
  293. break;
  294. }
  295. if (!chip1 && ((result & 0x80) == (data & 0x80)))
  296. chip1 = READY;
  297. if (!chip1 && ((result & 0xFFFF) & BIT_PROGRAM_ERROR)) {
  298. result = *addr;
  299. if ((result & 0x80) == (data & 0x80))
  300. chip1 = READY;
  301. else
  302. chip1 = ERR;
  303. }
  304. if (!chip2 && ((result & (0x80 << 16)) == (data & (0x80 << 16))))
  305. chip2 = READY;
  306. if (!chip2 && ((result >> 16) & BIT_PROGRAM_ERROR)) {
  307. result = *addr;
  308. if ((result & (0x80 << 16)) == (data & (0x80 << 16)))
  309. chip2 = READY;
  310. else
  311. chip2 = ERR;
  312. }
  313. } while (!chip1 || !chip2);
  314. *addr = CMD_READ_ARRAY;
  315. if (chip1 == ERR || chip2 == ERR || *addr != data) {
  316. rc = ERR_PROG_ERROR;
  317. printf ("Flash program error\n");
  318. debug ("chip1: %#x, chip2: %#x, addr: %#lx *addr: %#lx, "
  319. "data: %#lx\n",
  320. chip1, chip2, addr, *addr, data);
  321. }
  322. if (iflag)
  323. enable_interrupts ();
  324. #if 0
  325. if (cflag)
  326. icache_enable ();
  327. #endif
  328. return rc;
  329. }
  330. /*-----------------------------------------------------------------------
  331. * Copy memory to flash.
  332. */
  333. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  334. {
  335. ulong cp, wp, data;
  336. int l;
  337. int i, rc;
  338. MEM_FLASH_ADDR1 = CMD_UNLOCK1;
  339. MEM_FLASH_ADDR2 = CMD_UNLOCK2;
  340. MEM_FLASH_ADDR1 = CMD_UNLOCK_BYPASS;
  341. wp = (addr & ~3); /* get lower word aligned address */
  342. /*
  343. * handle unaligned start bytes
  344. */
  345. if ((l = addr - wp) != 0) {
  346. data = 0;
  347. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  348. data = (data >> 8) | (*(uchar *) cp << 24);
  349. }
  350. for (; i < 4 && cnt > 0; ++i) {
  351. data = (data >> 8) | (*src++ << 24);
  352. --cnt;
  353. ++cp;
  354. }
  355. for (; cnt == 0 && i < 4; ++i, ++cp) {
  356. data = (data >> 8) | (*(uchar *) cp << 24);
  357. }
  358. if ((rc = write_word (info, wp, data)) != 0) {
  359. goto Done;
  360. }
  361. wp += 4;
  362. }
  363. /*
  364. * handle word aligned part
  365. */
  366. while (cnt >= 4) {
  367. if (((ulong)src) & 0x3) {
  368. for (i = 0; i < 4; i++) {
  369. ((char *)&data)[i] = ((vu_char *)src)[i];
  370. }
  371. }
  372. else {
  373. data = *((vu_long *) src);
  374. }
  375. if ((rc = write_word (info, wp, data)) != 0) {
  376. goto Done;
  377. }
  378. src += 4;
  379. wp += 4;
  380. cnt -= 4;
  381. }
  382. if (cnt == 0) {
  383. rc = ERR_OK;
  384. goto Done;
  385. }
  386. /*
  387. * handle unaligned tail bytes
  388. */
  389. data = 0;
  390. for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
  391. data = (data >> 8) | (*src++ << 24);
  392. --cnt;
  393. }
  394. for (; i < 4; ++i, ++cp) {
  395. data = (data >> 8) | (*(uchar *) cp << 24);
  396. }
  397. rc = write_word (info, wp, data);
  398. Done:
  399. MEM_FLASH_ADDR = CMD_UNLOCK_BYPASS_RES1;
  400. MEM_FLASH_ADDR = CMD_UNLOCK_BYPASS_RES2;
  401. return (rc);
  402. }
  403. /*-----------------------------------------------------------------------
  404. */
  405. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  406. {
  407. ulong value;
  408. /* Write auto select command sequence and read Manufacturer ID */
  409. addr[0x0555] = CMD_UNLOCK1;
  410. addr[0x02AA] = CMD_UNLOCK2;
  411. addr[0x0555] = CMD_READ_MANF_ID;
  412. value = addr[0];
  413. debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
  414. switch (value) {
  415. case AMD_MANUFACT:
  416. info->flash_id = FLASH_MAN_AMD;
  417. break;
  418. case FUJ_MANUFACT:
  419. info->flash_id = FLASH_MAN_FUJ;
  420. break;
  421. case MX_MANUFACT:
  422. info->flash_id = FLASH_MAN_MX;
  423. break;
  424. default:
  425. info->flash_id = FLASH_UNKNOWN;
  426. info->sector_count = 0;
  427. info->size = 0;
  428. addr[0] = 0x00FF00FF; /* restore read mode */
  429. debug ("## flash_init: unknown manufacturer\n");
  430. return (0); /* no or unknown flash */
  431. }
  432. value = addr[1]; /* device ID */
  433. debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
  434. switch (value) {
  435. case AMD_ID_LV320B:
  436. info->flash_id += FLASH_AM320B;
  437. info->sector_count = 71;
  438. info->size = 0x00800000;
  439. addr[0] = 0x00FF00FF; /* restore read mode */
  440. break; /* => 8 MB */
  441. case AMD_ID_LV640U:
  442. info->flash_id += FLASH_AM640U;
  443. info->sector_count = 128;
  444. info->size = 0x01000000;
  445. addr[0] = 0x00F000F0; /* restore read mode */
  446. break; /* => 16 MB */
  447. case MX_ID_LV320B:
  448. info->flash_id += FLASH_MXLV320B;
  449. info->sector_count = 71;
  450. info->size = 0x00800000;
  451. addr[0] = 0x00FF00FF; /* restore read mode */
  452. break; /* => 8 MB */
  453. default:
  454. debug ("## flash_init: unknown flash chip\n");
  455. info->flash_id = FLASH_UNKNOWN;
  456. addr[0] = 0x00FF00FF; /* restore read mode */
  457. return (0); /* => no or unknown flash */
  458. }
  459. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  460. printf ("** ERROR: sector count %d > max (%d) **\n",
  461. info->sector_count, CFG_MAX_FLASH_SECT);
  462. info->sector_count = CFG_MAX_FLASH_SECT;
  463. }
  464. return (info->size);
  465. }