flash.c 12 KB

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