flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. extern void flash_afterinit(ulong);
  60. ulong flashbase = CFG_FLASH_BASE;
  61. flash_preinit();
  62. /* Init: no FLASHes known */
  63. for (i=0; i < CFG_MAX_FLASH_BANKS; ++i) {
  64. memset(&flash_info[i], 0, sizeof(flash_info_t));
  65. flash_info[i].size =
  66. flash_get_size((FPW *)flashbase, &flash_info[i]);
  67. size += flash_info[i].size;
  68. flashbase += 0x800000;
  69. }
  70. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  71. /* monitor protection ON by default */
  72. flash_protect(FLAG_PROTECT_SET,
  73. CFG_MONITOR_BASE,
  74. CFG_MONITOR_BASE+monitor_flash_len-1,
  75. flash_get_info(CFG_MONITOR_BASE));
  76. #endif
  77. #ifdef CFG_ENV_IS_IN_FLASH
  78. /* ENV protection ON by default */
  79. flash_protect(FLAG_PROTECT_SET,
  80. CFG_ENV_ADDR,
  81. CFG_ENV_ADDR+CFG_ENV_SIZE-1,
  82. flash_get_info(CFG_ENV_ADDR));
  83. #endif
  84. flash_afterinit(size);
  85. return size ? size : 1;
  86. }
  87. /*-----------------------------------------------------------------------
  88. */
  89. static void flash_reset(flash_info_t *info)
  90. {
  91. FPWV *base = (FPWV *)(info->start[0]);
  92. /* Put FLASH back in read mode */
  93. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
  94. *base = (FPW)0x00FF00FF; /* Intel Read Mode */
  95. else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
  96. *base = (FPW)0x00F000F0; /* AMD Read Mode */
  97. }
  98. /*-----------------------------------------------------------------------
  99. */
  100. static flash_info_t *flash_get_info(ulong base)
  101. {
  102. int i;
  103. flash_info_t * info;
  104. for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
  105. info = & flash_info[i];
  106. if (info->size &&
  107. 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. FPWV* addr2;
  177. /* Write auto select command: read Manufacturer ID */
  178. /* Write auto select command sequence and test FLASH answer */
  179. addr[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* for AMD, Intel ignores this */
  180. addr[FLASH_CYCLE2] = (FPW)0x00550055; /* for AMD, Intel ignores this */
  181. addr[FLASH_CYCLE1] = (FPW)0x00900090; /* selects Intel or AMD */
  182. /* The manufacturer codes are only 1 byte, so just use 1 byte.
  183. * This works for any bus width and any FLASH device width.
  184. */
  185. udelay(100);
  186. switch (addr[0] & 0xff) {
  187. case (uchar)AMD_MANUFACT:
  188. info->flash_id = FLASH_MAN_AMD;
  189. break;
  190. case (uchar)INTEL_MANUFACT:
  191. info->flash_id = FLASH_MAN_INTEL;
  192. break;
  193. default:
  194. info->flash_id = FLASH_UNKNOWN;
  195. info->sector_count = 0;
  196. info->size = 0;
  197. break;
  198. }
  199. /* Check 16 bits or 32 bits of ID so work on 32 or 16 bit bus. */
  200. if (info->flash_id != FLASH_UNKNOWN) switch ((FPW)addr[1]) {
  201. case (FPW)AMD_ID_LV065D:
  202. info->flash_id += FLASH_AMDLV065D;
  203. info->sector_count = 128;
  204. info->size = 0x00800000;
  205. for( i = 0; i < info->sector_count; i++ )
  206. info->start[i] = (ulong)addr + (i * 0x10000);
  207. break; /* => 8 or 16 MB */
  208. default:
  209. info->flash_id = FLASH_UNKNOWN;
  210. info->sector_count = 0;
  211. info->size = 0;
  212. return (0); /* => no or unknown flash */
  213. }
  214. /* test for real flash at bank 1 */
  215. addr2 = (FPW *)((ulong)addr | 0x800000);
  216. if (addr2 != addr &&
  217. ((addr2[0] & 0xff) == (addr[0] & 0xff)) && ((FPW)addr2[1] == (FPW)addr[1])) {
  218. /* Seems 2 banks are the same space (8Mb chip is installed,
  219. * J24 in default position (CS0)). Disable this (first) bank.
  220. */
  221. info->flash_id = FLASH_UNKNOWN;
  222. info->sector_count = 0;
  223. info->size = 0;
  224. }
  225. /* Put FLASH back in read mode */
  226. flash_reset(info);
  227. return (info->size);
  228. }
  229. /*-----------------------------------------------------------------------
  230. */
  231. int flash_erase (flash_info_t *info, int s_first, int s_last)
  232. {
  233. FPWV *addr;
  234. int flag, prot, sect;
  235. int intel = (info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL;
  236. ulong start, now, last;
  237. int rcode = 0;
  238. if ((s_first < 0) || (s_first > s_last)) {
  239. if (info->flash_id == FLASH_UNKNOWN) {
  240. printf ("- missing\n");
  241. } else {
  242. printf ("- no sectors to erase\n");
  243. }
  244. return 1;
  245. }
  246. switch (info->flash_id & FLASH_TYPEMASK) {
  247. case FLASH_AMDLV065D:
  248. break;
  249. case FLASH_UNKNOWN:
  250. default:
  251. printf ("Can't erase unknown flash type %08lx - aborted\n",
  252. info->flash_id);
  253. return 1;
  254. }
  255. prot = 0;
  256. for (sect=s_first; sect<=s_last; ++sect) {
  257. if (info->protect[sect]) {
  258. prot++;
  259. }
  260. }
  261. if (prot) {
  262. printf ("- Warning: %d protected sectors will not be erased!\n",
  263. prot);
  264. } else {
  265. printf ("\n");
  266. }
  267. last = get_timer(0);
  268. /* Start erase on unprotected sectors */
  269. for (sect = s_first; sect<=s_last && rcode == 0; sect++) {
  270. if (info->protect[sect] != 0) /* protected, skip it */
  271. continue;
  272. /* Disable interrupts which might cause a timeout here */
  273. flag = disable_interrupts();
  274. addr = (FPWV *)(info->start[sect]);
  275. if (intel) {
  276. *addr = (FPW)0x00500050; /* clear status register */
  277. *addr = (FPW)0x00200020; /* erase setup */
  278. *addr = (FPW)0x00D000D0; /* erase confirm */
  279. }
  280. else {
  281. /* must be AMD style if not Intel */
  282. FPWV *base; /* first address in bank */
  283. base = (FPWV *)(info->start[0]);
  284. base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
  285. base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
  286. base[FLASH_CYCLE1] = (FPW)0x00800080; /* erase mode */
  287. base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
  288. base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
  289. *addr = (FPW)0x00300030; /* erase sector */
  290. }
  291. /* re-enable interrupts if necessary */
  292. if (flag)
  293. enable_interrupts();
  294. start = get_timer(0);
  295. /* wait at least 50us for AMD, 80us for Intel.
  296. * Let's wait 1 ms.
  297. */
  298. udelay (1000);
  299. while ((*addr & (FPW)0x00800080) != (FPW)0x00800080) {
  300. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  301. printf ("Timeout\n");
  302. if (intel) {
  303. /* suspend erase */
  304. *addr = (FPW)0x00B000B0;
  305. }
  306. flash_reset(info); /* reset to read mode */
  307. rcode = 1; /* failed */
  308. break;
  309. }
  310. /* show that we're waiting */
  311. if ((get_timer(last)) > CFG_HZ) {/* every second */
  312. putc ('.');
  313. last = get_timer(0);
  314. }
  315. }
  316. /* show that we're waiting */
  317. if ((get_timer(last)) > CFG_HZ) { /* every second */
  318. putc ('.');
  319. last = get_timer(0);
  320. }
  321. flash_reset(info); /* reset to read mode */
  322. }
  323. printf (" done\n");
  324. return rcode;
  325. }
  326. /*-----------------------------------------------------------------------
  327. * Copy memory to flash, returns:
  328. * 0 - OK
  329. * 1 - write timeout
  330. * 2 - Flash not erased
  331. */
  332. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  333. {
  334. FPW data = 0; /* 16 or 32 bit word, matches flash bus width on MPC8XX */
  335. int bytes; /* number of bytes to program in current word */
  336. int left; /* number of bytes left to program */
  337. int i, res;
  338. for (left = cnt, res = 0;
  339. left > 0 && res == 0;
  340. addr += sizeof(data), left -= sizeof(data) - bytes) {
  341. bytes = addr & (sizeof(data) - 1);
  342. addr &= ~(sizeof(data) - 1);
  343. /* combine source and destination data so can program
  344. * an entire word of 16 or 32 bits
  345. */
  346. for (i = 0; i < sizeof(data); i++) {
  347. data <<= 8;
  348. if (i < bytes || i - bytes >= left )
  349. data += *((uchar *)addr + i);
  350. else
  351. data += *src++;
  352. }
  353. /* write one word to the flash */
  354. switch (info->flash_id & FLASH_VENDMASK) {
  355. case FLASH_MAN_AMD:
  356. res = write_word_amd(info, (FPWV *)addr, data);
  357. break;
  358. default:
  359. /* unknown flash type, error! */
  360. printf ("missing or unknown FLASH type\n");
  361. res = 1; /* not really a timeout, but gives error */
  362. break;
  363. }
  364. }
  365. return (res);
  366. }
  367. /*-----------------------------------------------------------------------
  368. * Write a word to Flash for AMD FLASH
  369. * A word is 16 or 32 bits, whichever the bus width of the flash bank
  370. * (not an individual chip) is.
  371. *
  372. * returns:
  373. * 0 - OK
  374. * 1 - write timeout
  375. * 2 - Flash not erased
  376. */
  377. static int write_word_amd (flash_info_t *info, FPWV *dest, FPW data)
  378. {
  379. ulong start;
  380. int flag;
  381. int res = 0; /* result, assume success */
  382. FPWV *base; /* first address in flash bank */
  383. /* Check if Flash is (sufficiently) erased */
  384. if ((*dest & data) != data) {
  385. return (2);
  386. }
  387. base = (FPWV *)(info->start[0]);
  388. /* Disable interrupts which might cause a timeout here */
  389. flag = disable_interrupts();
  390. base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
  391. base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
  392. base[FLASH_CYCLE1] = (FPW)0x00A000A0; /* selects program mode */
  393. *dest = data; /* start programming the data */
  394. /* re-enable interrupts if necessary */
  395. if (flag)
  396. enable_interrupts();
  397. start = get_timer (0);
  398. /* data polling for D7 */
  399. while (res == 0 && (*dest & (FPW)0x00800080) != (data & (FPW)0x00800080)) {
  400. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  401. *dest = (FPW)0x00F000F0; /* reset bank */
  402. res = 1;
  403. }
  404. }
  405. return (res);
  406. }