flash.c 12 KB

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