flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * (C) Copyright 2003-2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2004
  6. * Martin Krause, TQ-Systems GmbH, martin.krause@tqs.de
  7. *
  8. * Modified for the CMC PU2 by (C) Copyright 2004 Gary Jennejohn
  9. * garyj@denx.de
  10. *
  11. * See file CREDITS for list of people who contributed to this
  12. * project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. #include <common.h>
  30. #ifndef CFG_ENV_ADDR
  31. #define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
  32. #endif
  33. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  34. /*
  35. * CPU to flash interface is 32-bit, so make declaration accordingly
  36. */
  37. typedef unsigned short FLASH_PORT_WIDTH;
  38. typedef volatile unsigned short FLASH_PORT_WIDTHV;
  39. #define FPW FLASH_PORT_WIDTH
  40. #define FPWV FLASH_PORT_WIDTHV
  41. #define FLASH_CYCLE1 0x0555
  42. #define FLASH_CYCLE2 0x02AA
  43. /*-----------------------------------------------------------------------
  44. * Functions
  45. */
  46. static ulong flash_get_size(FPWV *addr, flash_info_t *info);
  47. static void flash_reset(flash_info_t *info);
  48. static int write_word_amd(flash_info_t *info, FPWV *dest, FPW data);
  49. static flash_info_t *flash_get_info(ulong base);
  50. /*-----------------------------------------------------------------------
  51. * flash_init()
  52. *
  53. * sets up flash_info and returns size of FLASH (bytes)
  54. */
  55. unsigned long flash_init (void)
  56. {
  57. unsigned long size = 0;
  58. ulong flashbase = CFG_FLASH_BASE;
  59. /* Init: no FLASHes known */
  60. memset(&flash_info[0], 0, sizeof(flash_info_t));
  61. flash_info[0].size =
  62. flash_get_size((FPW *)flashbase, &flash_info[0]);
  63. size = flash_info[0].size;
  64. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  65. /* monitor protection ON by default */
  66. flash_protect(FLAG_PROTECT_SET,
  67. CFG_MONITOR_BASE,
  68. CFG_MONITOR_BASE+monitor_flash_len-1,
  69. flash_get_info(CFG_MONITOR_BASE));
  70. #endif
  71. #ifdef CFG_ENV_IS_IN_FLASH
  72. /* ENV protection ON by default */
  73. flash_protect(FLAG_PROTECT_SET,
  74. CFG_ENV_ADDR,
  75. CFG_ENV_ADDR+CFG_ENV_SIZE-1,
  76. flash_get_info(CFG_ENV_ADDR));
  77. #endif
  78. return size ? size : 1;
  79. }
  80. /*-----------------------------------------------------------------------
  81. */
  82. static void flash_reset(flash_info_t *info)
  83. {
  84. FPWV *base = (FPWV *)(info->start[0]);
  85. /* Put FLASH back in read mode */
  86. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
  87. *base = (FPW)0x00FF; /* Intel Read Mode */
  88. else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
  89. *base = (FPW)0x00F0; /* AMD Read Mode */
  90. }
  91. /*-----------------------------------------------------------------------
  92. */
  93. static flash_info_t *flash_get_info(ulong base)
  94. {
  95. int i;
  96. flash_info_t * info;
  97. info = NULL;
  98. for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
  99. info = & flash_info[i];
  100. if (info->size && info->start[0] <= base &&
  101. base <= info->start[0] + info->size - 1)
  102. break;
  103. }
  104. return i == CFG_MAX_FLASH_BANKS ? 0 : info;
  105. }
  106. /*-----------------------------------------------------------------------
  107. */
  108. void flash_print_info (flash_info_t *info)
  109. {
  110. int i;
  111. if (info->flash_id == FLASH_UNKNOWN) {
  112. printf ("missing or unknown FLASH type\n");
  113. return;
  114. }
  115. switch (info->flash_id & FLASH_VENDMASK) {
  116. case FLASH_MAN_AMD: printf ("AMD "); break;
  117. case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
  118. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  119. case FLASH_MAN_SST: printf ("SST "); break;
  120. case FLASH_MAN_STM: printf ("STM "); break;
  121. case FLASH_MAN_INTEL: printf ("INTEL "); break;
  122. default: printf ("Unknown Vendor "); break;
  123. }
  124. switch (info->flash_id & FLASH_TYPEMASK) {
  125. case FLASH_S29GL064M:
  126. printf ("S29GL064M-R6 (64Mbit, uniform sector size)\n");
  127. break;
  128. default:
  129. printf ("Unknown Chip Type\n");
  130. break;
  131. }
  132. printf (" Size: %ld MB in %d Sectors\n",
  133. info->size >> 20,
  134. info->sector_count);
  135. printf (" Sector Start Addresses:");
  136. for (i=0; i<info->sector_count; ++i) {
  137. if ((i % 5) == 0) {
  138. printf ("\n ");
  139. }
  140. printf (" %08lX%s",
  141. info->start[i],
  142. info->protect[i] ? " (RO)" : " ");
  143. }
  144. printf ("\n");
  145. return;
  146. }
  147. /*-----------------------------------------------------------------------
  148. */
  149. /*
  150. * The following code cannot be run from FLASH!
  151. */
  152. ulong flash_get_size (FPWV *addr, flash_info_t *info)
  153. {
  154. int i;
  155. ulong base = (ulong)addr;
  156. /* Write auto select command: read Manufacturer ID */
  157. /* Write auto select command sequence and test FLASH answer */
  158. addr[FLASH_CYCLE1] = (FPW)0x00AA; /* for AMD, Intel ignores this */
  159. addr[FLASH_CYCLE2] = (FPW)0x0055; /* for AMD, Intel ignores this */
  160. addr[FLASH_CYCLE1] = (FPW)0x0090; /* selects Intel or AMD */
  161. /* The manufacturer codes are only 1 byte, so just use 1 byte.
  162. * This works for any bus width and any FLASH device width.
  163. */
  164. udelay(100);
  165. switch (addr[0] & 0xff) {
  166. case (uchar)AMD_MANUFACT:
  167. debug ("Manufacturer: AMD (Spansion)\n");
  168. info->flash_id = FLASH_MAN_AMD;
  169. break;
  170. case (uchar)INTEL_MANUFACT:
  171. debug ("Manufacturer: Intel (not supported yet)\n");
  172. info->flash_id = FLASH_MAN_INTEL;
  173. break;
  174. default:
  175. info->flash_id = FLASH_UNKNOWN;
  176. info->sector_count = 0;
  177. info->size = 0;
  178. break;
  179. }
  180. /* Check 16 bits or 32 bits of ID so work on 32 or 16 bit bus. */
  181. if (info->flash_id != FLASH_UNKNOWN) switch ((FPW)addr[1]) {
  182. case AMD_ID_MIRROR:
  183. debug ("Mirror Bit flash: addr[14] = %08X addr[15] = %08X\n",
  184. addr[14], addr[15]);
  185. switch(addr[14] & 0xffff) {
  186. case (AMD_ID_GL064M_2 & 0xffff):
  187. if (addr[15] != (AMD_ID_GL064M_3 & 0xffff)) {
  188. printf ("Chip: S29GLxxxM -> unknown\n");
  189. info->flash_id = FLASH_UNKNOWN;
  190. info->sector_count = 0;
  191. info->size = 0;
  192. } else {
  193. debug ("Chip: S29GL064M-R6\n");
  194. info->flash_id += FLASH_S29GL064M;
  195. info->sector_count = 128;
  196. info->size = 0x00800000;
  197. for (i = 0; i < info->sector_count; i++) {
  198. info->start[i] = base;
  199. base += 0x10000;
  200. }
  201. }
  202. break; /* => 16 MB */
  203. default:
  204. printf ("Chip: *** unknown ***\n");
  205. info->flash_id = FLASH_UNKNOWN;
  206. info->sector_count = 0;
  207. info->size = 0;
  208. break;
  209. }
  210. break;
  211. default:
  212. info->flash_id = FLASH_UNKNOWN;
  213. info->sector_count = 0;
  214. info->size = 0;
  215. }
  216. /* Put FLASH back in read mode */
  217. flash_reset(info);
  218. return (info->size);
  219. }
  220. /*-----------------------------------------------------------------------
  221. */
  222. int flash_erase (flash_info_t *info, int s_first, int s_last)
  223. {
  224. FPWV *addr = (FPWV *)(info->start[0]);
  225. int flag, prot, sect, ssect, l_sect;
  226. ulong start, now, last;
  227. debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
  228. if ((s_first < 0) || (s_first > s_last)) {
  229. if (info->flash_id == FLASH_UNKNOWN) {
  230. printf ("- missing\n");
  231. } else {
  232. printf ("- no sectors to erase\n");
  233. }
  234. return 1;
  235. }
  236. if ((info->flash_id == FLASH_UNKNOWN) ||
  237. (info->flash_id > FLASH_AMD_COMP)) {
  238. printf ("Can't erase unknown flash type %08lx - aborted\n",
  239. info->flash_id);
  240. return 1;
  241. }
  242. prot = 0;
  243. for (sect=s_first; sect<=s_last; ++sect) {
  244. if (info->protect[sect]) {
  245. prot++;
  246. }
  247. }
  248. if (prot) {
  249. printf ("- Warning: %d protected sectors will not be erased!\n",
  250. prot);
  251. } else {
  252. printf ("\n");
  253. }
  254. /* Disable interrupts which might cause a timeout here */
  255. flag = disable_interrupts();
  256. /*
  257. * Start erase on unprotected sectors.
  258. * Since the flash can erase multiple sectors with one command
  259. * we take advantage of that by doing the erase in chunks of
  260. * 3 sectors.
  261. */
  262. for (sect = s_first; sect <= s_last; ) {
  263. l_sect = -1;
  264. addr[FLASH_CYCLE1] = 0x00AA;
  265. addr[FLASH_CYCLE2] = 0x0055;
  266. addr[FLASH_CYCLE1] = 0x0080;
  267. addr[FLASH_CYCLE1] = 0x00AA;
  268. addr[FLASH_CYCLE2] = 0x0055;
  269. /* do the erase in chunks of at most 3 sectors */
  270. for (ssect = 0; ssect < 3; ssect++) {
  271. if ((sect + ssect) > s_last)
  272. break;
  273. if (info->protect[sect + ssect] == 0) { /* not protected */
  274. addr = (FPWV *)(info->start[sect + ssect]);
  275. addr[0] = 0x0030;
  276. l_sect = sect + ssect;
  277. }
  278. }
  279. /* wait at least 80us - let's wait 1 ms */
  280. udelay (1000);
  281. /*
  282. * We wait for the last triggered sector
  283. */
  284. if (l_sect < 0)
  285. goto DONE;
  286. start = get_timer (0);
  287. last = start;
  288. addr = (FPWV *)(info->start[l_sect]);
  289. while ((addr[0] & 0x0080) != 0x0080) {
  290. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  291. printf ("Timeout\n");
  292. return 1;
  293. }
  294. /* show that we're waiting */
  295. if ((now - last) > 1000) { /* every second */
  296. putc ('.');
  297. last = now;
  298. }
  299. }
  300. addr = (FPWV *)info->start[0];
  301. addr[0] = 0x00F0; /* reset bank */
  302. sect += ssect;
  303. }
  304. /* re-enable interrupts if necessary */
  305. if (flag)
  306. enable_interrupts();
  307. DONE:
  308. /* reset to read mode */
  309. addr = (FPWV *)info->start[0];
  310. addr[0] = 0x00F0; /* reset bank */
  311. printf (" done\n");
  312. return 0;
  313. }
  314. /*-----------------------------------------------------------------------
  315. * Copy memory to flash, returns:
  316. * 0 - OK
  317. * 1 - write timeout
  318. * 2 - Flash not erased
  319. */
  320. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  321. {
  322. ulong wp, data;
  323. int rc;
  324. if (addr & 1) {
  325. printf ("unaligned destination not supported\n");
  326. return ERR_ALIGN;
  327. };
  328. if ((int) src & 1) {
  329. printf ("unaligned source not supported\n");
  330. return ERR_ALIGN;
  331. };
  332. wp = addr;
  333. while (cnt >= 2) {
  334. data = *((FPWV *)src);
  335. if ((rc = write_word_amd(info, (FPW *)wp, data)) != 0) {
  336. return (rc);
  337. }
  338. src += 2;
  339. wp += 2;
  340. cnt -= 2;
  341. }
  342. if (cnt == 0) {
  343. return (0);
  344. }
  345. if (cnt == 1) {
  346. data = (*((volatile u8 *) src)) | (*((volatile u8 *) (wp + 1))
  347. << 8);
  348. if ((rc = write_word_amd(info, (FPW *)wp, data)) != 0) {
  349. return (rc);
  350. }
  351. src += 1;
  352. wp += 1;
  353. cnt -= 1;
  354. }
  355. return ERR_OK;
  356. }
  357. /*-----------------------------------------------------------------------
  358. * Write a word to Flash for AMD FLASH
  359. * A word is 16 or 32 bits, whichever the bus width of the flash bank
  360. * (not an individual chip) is.
  361. *
  362. * returns:
  363. * 0 - OK
  364. * 1 - write timeout
  365. * 2 - Flash not erased
  366. */
  367. static int write_word_amd (flash_info_t *info, FPWV *dest, FPW data)
  368. {
  369. ulong start;
  370. int flag;
  371. FPWV *base; /* first address in flash bank */
  372. /* Check if Flash is (sufficiently) erased */
  373. if ((*dest & data) != data) {
  374. return (2);
  375. }
  376. base = (FPWV *)(info->start[0]);
  377. /* Disable interrupts which might cause a timeout here */
  378. flag = disable_interrupts();
  379. base[FLASH_CYCLE1] = (FPW)0x00AA; /* unlock */
  380. base[FLASH_CYCLE2] = (FPW)0x0055; /* unlock */
  381. base[FLASH_CYCLE1] = (FPW)0x00A0; /* selects program mode */
  382. *dest = data; /* start programming the data */
  383. /* re-enable interrupts if necessary */
  384. if (flag)
  385. enable_interrupts();
  386. start = get_timer (0);
  387. /* data polling for D7 */
  388. while ((*dest & (FPW)0x0080) != (data & (FPW)0x0080)) {
  389. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  390. *dest = (FPW)0x00F0; /* reset bank */
  391. return (1);
  392. }
  393. }
  394. return (0);
  395. }