flash.c 12 KB

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