flash.c 12 KB

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