flash.c 11 KB

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