flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * (C) Copyright 2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@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. #include <common.h>
  24. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  25. /* NOTE - CONFIG_FLASH_16BIT means the CPU interface is 16-bit, it
  26. * has nothing to do with the flash chip being 8-bit or 16-bit.
  27. */
  28. #ifdef CONFIG_FLASH_16BIT
  29. typedef unsigned short FLASH_PORT_WIDTH;
  30. typedef volatile unsigned short FLASH_PORT_WIDTHV;
  31. #define FLASH_ID_MASK 0xFFFF
  32. #else
  33. typedef unsigned char FLASH_PORT_WIDTH;
  34. typedef volatile unsigned char FLASH_PORT_WIDTHV;
  35. #define FLASH_ID_MASK 0xFF
  36. #endif
  37. #define FPW FLASH_PORT_WIDTH
  38. #define FPWV FLASH_PORT_WIDTHV
  39. #define ORMASK(size) ((-size) & OR_AM_MSK)
  40. #define FLASH_CYCLE1 0x0555
  41. #define FLASH_CYCLE2 0x02aa
  42. /*-----------------------------------------------------------------------
  43. * Functions
  44. */
  45. static ulong flash_get_size(FPWV *addr, flash_info_t *info);
  46. static void flash_reset(flash_info_t *info);
  47. static int write_word_amd(flash_info_t *info, FPWV *dest, FPW data);
  48. static flash_info_t *flash_get_info(ulong base);
  49. /*-----------------------------------------------------------------------
  50. * flash_init()
  51. *
  52. * sets up flash_info and returns size of FLASH (bytes)
  53. */
  54. unsigned long flash_init (void)
  55. {
  56. unsigned long size = 0;
  57. int i;
  58. extern void flash_preinit(void);
  59. flash_preinit();
  60. /* Init: no FLASHes known */
  61. for (i=0; i < CFG_MAX_FLASH_BANKS; ++i) {
  62. ulong flashbase = CFG_FLASH_BASE;
  63. memset(&flash_info[i], 0, sizeof(flash_info_t));
  64. flash_info[i].size =
  65. flash_get_size((FPW *)flashbase, &flash_info[i]);
  66. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  67. printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx\n",
  68. i, flash_info[i].size);
  69. }
  70. size += flash_info[i].size;
  71. }
  72. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  73. /* monitor protection ON by default */
  74. flash_protect(FLAG_PROTECT_SET,
  75. CFG_MONITOR_BASE,
  76. CFG_MONITOR_BASE+monitor_flash_len-1,
  77. flash_get_info(CFG_MONITOR_BASE));
  78. #endif
  79. #ifdef CFG_ENV_IS_IN_FLASH
  80. /* ENV protection ON by default */
  81. flash_protect(FLAG_PROTECT_SET,
  82. CFG_ENV_ADDR,
  83. CFG_ENV_ADDR+CFG_ENV_SIZE-1,
  84. flash_get_info(CFG_ENV_ADDR));
  85. #endif
  86. return size ? size : 1;
  87. }
  88. /*-----------------------------------------------------------------------
  89. */
  90. static void flash_reset(flash_info_t *info)
  91. {
  92. FPWV *base = (FPWV *)(info->start[0]);
  93. /* Put FLASH back in read mode */
  94. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
  95. *base = (FPW)0x00FF00FF; /* Intel Read Mode */
  96. else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
  97. *base = (FPW)0x00F000F0; /* AMD Read Mode */
  98. }
  99. /*-----------------------------------------------------------------------
  100. */
  101. static flash_info_t *flash_get_info(ulong base)
  102. {
  103. int i;
  104. flash_info_t * info;
  105. for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
  106. info = & flash_info[i];
  107. if (info->start[0] <= base && base <= info->start[0] + info->size - 1)
  108. break;
  109. }
  110. return i == CFG_MAX_FLASH_BANKS ? 0 : info;
  111. }
  112. /*-----------------------------------------------------------------------
  113. */
  114. void flash_print_info (flash_info_t *info)
  115. {
  116. int i;
  117. uchar *boottype;
  118. uchar *bootletter;
  119. uchar *fmt;
  120. uchar botbootletter[] = "B";
  121. uchar topbootletter[] = "T";
  122. uchar botboottype[] = "bottom boot sector";
  123. uchar topboottype[] = "top boot sector";
  124. if (info->flash_id == FLASH_UNKNOWN) {
  125. printf ("missing or unknown FLASH type\n");
  126. return;
  127. }
  128. switch (info->flash_id & FLASH_VENDMASK) {
  129. case FLASH_MAN_AMD: printf ("AMD "); break;
  130. case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
  131. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  132. case FLASH_MAN_SST: printf ("SST "); break;
  133. case FLASH_MAN_STM: printf ("STM "); break;
  134. case FLASH_MAN_INTEL: printf ("INTEL "); break;
  135. default: printf ("Unknown Vendor "); break;
  136. }
  137. /* check for top or bottom boot, if it applies */
  138. if (info->flash_id & FLASH_BTYPE) {
  139. boottype = botboottype;
  140. bootletter = botbootletter;
  141. }
  142. else {
  143. boottype = topboottype;
  144. bootletter = topbootletter;
  145. }
  146. switch (info->flash_id & FLASH_TYPEMASK) {
  147. case FLASH_AMDLV065D:
  148. fmt = "29LV065 (64 Mbit, uniform sectors)\n";
  149. break;
  150. default:
  151. fmt = "Unknown Chip Type\n";
  152. break;
  153. }
  154. printf (fmt, bootletter, boottype);
  155. printf (" Size: %ld MB in %d Sectors\n",
  156. info->size >> 20,
  157. info->sector_count);
  158. printf (" Sector Start Addresses:");
  159. for (i=0; i<info->sector_count; ++i) {
  160. if ((i % 5) == 0) {
  161. printf ("\n ");
  162. }
  163. printf (" %08lX%s", info->start[i],
  164. info->protect[i] ? " (RO)" : " ");
  165. }
  166. printf ("\n");
  167. }
  168. /*-----------------------------------------------------------------------
  169. */
  170. /*
  171. * The following code cannot be run from FLASH!
  172. */
  173. ulong flash_get_size (FPWV *addr, flash_info_t *info)
  174. {
  175. int i;
  176. /* Write auto select command: read Manufacturer ID */
  177. /* Write auto select command sequence and test FLASH answer */
  178. addr[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* for AMD, Intel ignores this */
  179. addr[FLASH_CYCLE2] = (FPW)0x00550055; /* for AMD, Intel ignores this */
  180. addr[FLASH_CYCLE1] = (FPW)0x00900090; /* selects Intel or AMD */
  181. /* The manufacturer codes are only 1 byte, so just use 1 byte.
  182. * This works for any bus width and any FLASH device width.
  183. */
  184. udelay(100);
  185. switch (addr[0] & 0xff) {
  186. case (uchar)AMD_MANUFACT:
  187. info->flash_id = FLASH_MAN_AMD;
  188. break;
  189. case (uchar)INTEL_MANUFACT:
  190. info->flash_id = FLASH_MAN_INTEL;
  191. break;
  192. default:
  193. info->flash_id = FLASH_UNKNOWN;
  194. info->sector_count = 0;
  195. info->size = 0;
  196. break;
  197. }
  198. /* Check 16 bits or 32 bits of ID so work on 32 or 16 bit bus. */
  199. if (info->flash_id != FLASH_UNKNOWN) switch ((FPW)addr[1]) {
  200. case (FPW)AMD_ID_LV065D:
  201. info->flash_id += FLASH_AMDLV065D;
  202. info->sector_count = 128;
  203. info->size = 0x00800000;
  204. for( i = 0; i < info->sector_count; i++ )
  205. info->start[i] = (ulong)addr + (i * 0x10000);
  206. break; /* => 8 or 16 MB */
  207. default:
  208. info->flash_id = FLASH_UNKNOWN;
  209. info->sector_count = 0;
  210. info->size = 0;
  211. return (0); /* => no or unknown flash */
  212. }
  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. FPWV *addr;
  222. int flag, prot, sect;
  223. int intel = (info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL;
  224. ulong start, now, last;
  225. int rcode = 0;
  226. if ((s_first < 0) || (s_first > s_last)) {
  227. if (info->flash_id == FLASH_UNKNOWN) {
  228. printf ("- missing\n");
  229. } else {
  230. printf ("- no sectors to erase\n");
  231. }
  232. return 1;
  233. }
  234. switch (info->flash_id & FLASH_TYPEMASK) {
  235. case FLASH_AMDLV065D:
  236. break;
  237. case FLASH_UNKNOWN:
  238. default:
  239. printf ("Can't erase unknown flash type %08lx - aborted\n",
  240. info->flash_id);
  241. return 1;
  242. }
  243. prot = 0;
  244. for (sect=s_first; sect<=s_last; ++sect) {
  245. if (info->protect[sect]) {
  246. prot++;
  247. }
  248. }
  249. if (prot) {
  250. printf ("- Warning: %d protected sectors will not be erased!\n",
  251. prot);
  252. } else {
  253. printf ("\n");
  254. }
  255. last = get_timer(0);
  256. /* Start erase on unprotected sectors */
  257. for (sect = s_first; sect<=s_last && rcode == 0; sect++) {
  258. if (info->protect[sect] != 0) /* protected, skip it */
  259. continue;
  260. /* Disable interrupts which might cause a timeout here */
  261. flag = disable_interrupts();
  262. addr = (FPWV *)(info->start[sect]);
  263. if (intel) {
  264. *addr = (FPW)0x00500050; /* clear status register */
  265. *addr = (FPW)0x00200020; /* erase setup */
  266. *addr = (FPW)0x00D000D0; /* erase confirm */
  267. }
  268. else {
  269. /* must be AMD style if not Intel */
  270. FPWV *base; /* first address in bank */
  271. base = (FPWV *)(info->start[0]);
  272. base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
  273. base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
  274. base[FLASH_CYCLE1] = (FPW)0x00800080; /* erase mode */
  275. base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
  276. base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
  277. *addr = (FPW)0x00300030; /* erase sector */
  278. }
  279. /* re-enable interrupts if necessary */
  280. if (flag)
  281. enable_interrupts();
  282. start = get_timer(0);
  283. /* wait at least 50us for AMD, 80us for Intel.
  284. * Let's wait 1 ms.
  285. */
  286. udelay (1000);
  287. while ((*addr & (FPW)0x00800080) != (FPW)0x00800080) {
  288. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  289. printf ("Timeout\n");
  290. if (intel) {
  291. /* suspend erase */
  292. *addr = (FPW)0x00B000B0;
  293. }
  294. flash_reset(info); /* reset to read mode */
  295. rcode = 1; /* failed */
  296. break;
  297. }
  298. /* show that we're waiting */
  299. if ((get_timer(last)) > CFG_HZ) {/* every second */
  300. putc ('.');
  301. last = get_timer(0);
  302. }
  303. }
  304. /* show that we're waiting */
  305. if ((get_timer(last)) > CFG_HZ) { /* every second */
  306. putc ('.');
  307. last = get_timer(0);
  308. }
  309. flash_reset(info); /* reset to read mode */
  310. }
  311. printf (" done\n");
  312. return rcode;
  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. FPW data = 0; /* 16 or 32 bit word, matches flash bus width on MPC8XX */
  323. int bytes; /* number of bytes to program in current word */
  324. int left; /* number of bytes left to program */
  325. int i, res;
  326. for (left = cnt, res = 0;
  327. left > 0 && res == 0;
  328. addr += sizeof(data), left -= sizeof(data) - bytes) {
  329. bytes = addr & (sizeof(data) - 1);
  330. addr &= ~(sizeof(data) - 1);
  331. /* combine source and destination data so can program
  332. * an entire word of 16 or 32 bits
  333. */
  334. for (i = 0; i < sizeof(data); i++) {
  335. data <<= 8;
  336. if (i < bytes || i - bytes >= left )
  337. data += *((uchar *)addr + i);
  338. else
  339. data += *src++;
  340. }
  341. /* write one word to the flash */
  342. switch (info->flash_id & FLASH_VENDMASK) {
  343. case FLASH_MAN_AMD:
  344. res = write_word_amd(info, (FPWV *)addr, data);
  345. break;
  346. default:
  347. /* unknown flash type, error! */
  348. printf ("missing or unknown FLASH type\n");
  349. res = 1; /* not really a timeout, but gives error */
  350. break;
  351. }
  352. }
  353. return (res);
  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, FPWV *dest, FPW data)
  366. {
  367. ulong start;
  368. int flag;
  369. int res = 0; /* result, assume success */
  370. FPWV *base; /* first address in flash bank */
  371. /* Check if Flash is (sufficiently) erased */
  372. if ((*dest & data) != data) {
  373. return (2);
  374. }
  375. base = (FPWV *)(info->start[0]);
  376. /* Disable interrupts which might cause a timeout here */
  377. flag = disable_interrupts();
  378. base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
  379. base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
  380. base[FLASH_CYCLE1] = (FPW)0x00A000A0; /* selects program mode */
  381. *dest = data; /* start programming the data */
  382. /* re-enable interrupts if necessary */
  383. if (flag)
  384. enable_interrupts();
  385. start = get_timer (0);
  386. /* data polling for D7 */
  387. while (res == 0 && (*dest & (FPW)0x00800080) != (data & (FPW)0x00800080)) {
  388. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  389. *dest = (FPW)0x00F000F0; /* reset bank */
  390. res = 1;
  391. }
  392. }
  393. return (res);
  394. }