flash.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * (C) Copyright 2001-2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Flash Routines for Intel devices
  6. *
  7. *--------------------------------------------------------------------
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <mpc8xx.h>
  12. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  13. /*-----------------------------------------------------------------------
  14. */
  15. ulong flash_get_size (volatile unsigned long *baseaddr,
  16. flash_info_t * info)
  17. {
  18. short i;
  19. unsigned long flashtest_h, flashtest_l;
  20. info->sector_count = info->size = 0;
  21. info->flash_id = FLASH_UNKNOWN;
  22. /* Write query command sequence and test FLASH answer
  23. */
  24. baseaddr[0] = 0x00980098;
  25. baseaddr[1] = 0x00980098;
  26. flashtest_h = baseaddr[0]; /* manufacturer ID */
  27. flashtest_l = baseaddr[1];
  28. if (flashtest_h != INTEL_MANUFACT || flashtest_l != INTEL_MANUFACT)
  29. return (0); /* no or unknown flash */
  30. flashtest_h = baseaddr[2]; /* device ID */
  31. flashtest_l = baseaddr[3];
  32. if (flashtest_h != flashtest_l)
  33. return (0);
  34. switch (flashtest_h) {
  35. case INTEL_ID_28F160C3B:
  36. info->flash_id = FLASH_28F160C3B;
  37. info->sector_count = 39;
  38. info->size = 0x00800000; /* 4 * 2 MB = 8 MB */
  39. break;
  40. case INTEL_ID_28F160F3B:
  41. info->flash_id = FLASH_28F160F3B;
  42. info->sector_count = 39;
  43. info->size = 0x00800000; /* 4 * 2 MB = 8 MB */
  44. break;
  45. case INTEL_ID_28F640C3B:
  46. info->flash_id = FLASH_28F640C3B;
  47. info->sector_count = 135;
  48. info->size = 0x02000000; /* 16 * 2 MB = 32 MB */
  49. break;
  50. default:
  51. return (0); /* no or unknown flash */
  52. }
  53. info->flash_id |= INTEL_MANUFACT << 16; /* set manufacturer offset */
  54. if (info->flash_id & FLASH_BTYPE) {
  55. volatile unsigned long *tmp = baseaddr;
  56. /* set up sector start adress table (bottom sector type)
  57. * AND unlock the sectors (if our chip is 160C3 or 640c3)
  58. */
  59. for (i = 0; i < info->sector_count; i++) {
  60. if (((info->flash_id & FLASH_TYPEMASK) == FLASH_28F160C3B) ||
  61. ((info->flash_id & FLASH_TYPEMASK) == FLASH_28F640C3B)) {
  62. tmp[0] = 0x00600060;
  63. tmp[1] = 0x00600060;
  64. tmp[0] = 0x00D000D0;
  65. tmp[1] = 0x00D000D0;
  66. }
  67. info->start[i] = (uint) tmp;
  68. tmp += i < 8 ? 0x2000 : 0x10000; /* pointer arith */
  69. }
  70. }
  71. memset (info->protect, 0, info->sector_count);
  72. baseaddr[0] = 0x00FF00FF;
  73. baseaddr[1] = 0x00FF00FF;
  74. return (info->size);
  75. }
  76. /*-----------------------------------------------------------------------
  77. */
  78. unsigned long flash_init (void)
  79. {
  80. unsigned long size_b0 = 0;
  81. int i;
  82. /* Init: no FLASHes known
  83. */
  84. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  85. flash_info[i].flash_id = FLASH_UNKNOWN;
  86. }
  87. /* Static FLASH Bank configuration here (only one bank) */
  88. size_b0 = flash_get_size ((ulong *) CONFIG_SYS_FLASH0_BASE, &flash_info[0]);
  89. if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
  90. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  91. size_b0, size_b0 >> 20);
  92. }
  93. /* protect monitor and environment sectors
  94. */
  95. #ifndef CONFIG_BOOT_ROM
  96. /* If U-Boot is booted from ROM the CONFIG_SYS_MONITOR_BASE > CONFIG_SYS_FLASH0_BASE
  97. * but we shouldn't protect it.
  98. */
  99. # if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH0_BASE
  100. flash_protect (FLAG_PROTECT_SET,
  101. CONFIG_SYS_MONITOR_BASE,
  102. CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]
  103. );
  104. # endif
  105. #endif /* CONFIG_BOOT_ROM */
  106. #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
  107. # ifndef CONFIG_ENV_SIZE
  108. # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  109. # endif
  110. flash_protect (FLAG_PROTECT_SET,
  111. CONFIG_ENV_ADDR,
  112. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
  113. #endif
  114. return (size_b0);
  115. }
  116. /*-----------------------------------------------------------------------
  117. */
  118. void flash_print_info (flash_info_t * info)
  119. {
  120. int i;
  121. if (info->flash_id == FLASH_UNKNOWN) {
  122. printf ("missing or unknown FLASH type\n");
  123. return;
  124. }
  125. switch ((info->flash_id >> 16) & 0xff) {
  126. case 0x89:
  127. printf ("INTEL ");
  128. break;
  129. default:
  130. printf ("Unknown Vendor ");
  131. break;
  132. }
  133. switch (info->flash_id & FLASH_TYPEMASK) {
  134. case FLASH_28F160C3B:
  135. printf ("28F160C3B (16 M, bottom sector)\n");
  136. break;
  137. case FLASH_28F160F3B:
  138. printf ("28F160F3B (16 M, bottom sector)\n");
  139. break;
  140. case FLASH_28F640C3B:
  141. printf ("28F640C3B (64 M, bottom sector)\n");
  142. break;
  143. default:
  144. printf ("Unknown Chip Type\n");
  145. break;
  146. }
  147. printf (" Size: %ld MB in %d Sectors\n",
  148. info->size >> 20, info->sector_count);
  149. printf (" Sector Start Addresses:");
  150. for (i = 0; i < info->sector_count; ++i) {
  151. if ((i % 5) == 0)
  152. printf ("\n ");
  153. printf (" %08lX%s",
  154. info->start[i],
  155. info->protect[i] ? " (RO)" : " "
  156. );
  157. }
  158. printf ("\n");
  159. }
  160. /*-----------------------------------------------------------------------
  161. */
  162. int flash_erase (flash_info_t * info, int s_first, int s_last)
  163. {
  164. int flag, prot, sect;
  165. ulong start, now, last;
  166. if ((s_first < 0) || (s_first > s_last)) {
  167. if (info->flash_id == FLASH_UNKNOWN) {
  168. printf ("- missing\n");
  169. } else {
  170. printf ("- no sectors to erase\n");
  171. }
  172. return 1;
  173. }
  174. prot = 0;
  175. for (sect = s_first; sect <= s_last; sect++) {
  176. if (info->protect[sect])
  177. prot++;
  178. }
  179. if (prot) {
  180. printf ("- Warning: %d protected sectors will not be erased!\n",
  181. prot);
  182. } else {
  183. printf ("\n");
  184. }
  185. /* Start erase on unprotected sectors
  186. */
  187. for (sect = s_first; sect <= s_last; sect++) {
  188. volatile ulong *addr =
  189. (volatile unsigned long *) info->start[sect];
  190. start = get_timer (0);
  191. last = start;
  192. if (info->protect[sect] == 0) {
  193. /* Disable interrupts which might cause a timeout here
  194. */
  195. flag = disable_interrupts ();
  196. /* Erase the block
  197. */
  198. addr[0] = 0x00200020;
  199. addr[1] = 0x00200020;
  200. addr[0] = 0x00D000D0;
  201. addr[1] = 0x00D000D0;
  202. /* re-enable interrupts if necessary
  203. */
  204. if (flag)
  205. enable_interrupts ();
  206. /* wait at least 80us - let's wait 1 ms
  207. */
  208. udelay (1000);
  209. last = start;
  210. while ((addr[0] & 0x00800080) != 0x00800080 ||
  211. (addr[1] & 0x00800080) != 0x00800080) {
  212. if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  213. printf ("Timeout (erase suspended!)\n");
  214. /* Suspend erase
  215. */
  216. addr[0] = 0x00B000B0;
  217. addr[1] = 0x00B000B0;
  218. goto DONE;
  219. }
  220. /* show that we're waiting
  221. */
  222. if ((now - last) > 1000) { /* every second */
  223. serial_putc ('.');
  224. last = now;
  225. }
  226. }
  227. if (addr[0] & 0x00220022 || addr[1] & 0x00220022) {
  228. printf ("*** ERROR: erase failed!\n");
  229. goto DONE;
  230. }
  231. }
  232. /* Clear status register and reset to read mode
  233. */
  234. addr[0] = 0x00500050;
  235. addr[1] = 0x00500050;
  236. addr[0] = 0x00FF00FF;
  237. addr[1] = 0x00FF00FF;
  238. }
  239. printf (" done\n");
  240. DONE:
  241. return 0;
  242. }
  243. static int write_word (flash_info_t *, volatile unsigned long *, ulong);
  244. /*-----------------------------------------------------------------------
  245. * Copy memory to flash, returns:
  246. * 0 - OK
  247. * 1 - write timeout
  248. * 2 - Flash not erased
  249. */
  250. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  251. {
  252. ulong v;
  253. int i, l, cc = cnt, res = 0;
  254. for (v=0; cc > 0; addr += 4, cc -= 4 - l) {
  255. l = (addr & 3);
  256. addr &= ~3;
  257. for (i = 0; i < 4; i++) {
  258. v = (v << 8) + (i < l || i - l >= cc ?
  259. *((unsigned char *) addr + i) : *src++);
  260. }
  261. if ((res = write_word (info, (volatile unsigned long *) addr, v)) != 0)
  262. break;
  263. }
  264. return (res);
  265. }
  266. /*-----------------------------------------------------------------------
  267. * Write a word to Flash, returns:
  268. * 0 - OK
  269. * 1 - write timeout
  270. * 2 - Flash not erased
  271. */
  272. static int write_word (flash_info_t * info, volatile unsigned long *addr,
  273. ulong data)
  274. {
  275. int flag, res = 0;
  276. ulong start;
  277. /* Check if Flash is (sufficiently) erased
  278. */
  279. if ((*addr & data) != data)
  280. return (2);
  281. /* Disable interrupts which might cause a timeout here
  282. */
  283. flag = disable_interrupts ();
  284. *addr = 0x00400040;
  285. *addr = data;
  286. /* re-enable interrupts if necessary
  287. */
  288. if (flag)
  289. enable_interrupts ();
  290. start = get_timer (0);
  291. while ((*addr & 0x00800080) != 0x00800080) {
  292. if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  293. /* Suspend program
  294. */
  295. *addr = 0x00B000B0;
  296. res = 1;
  297. goto OUT;
  298. }
  299. }
  300. if (*addr & 0x00220022) {
  301. printf ("*** ERROR: program failed!\n");
  302. res = 1;
  303. }
  304. OUT:
  305. /* Clear status register and reset to read mode
  306. */
  307. *addr = 0x00500050;
  308. *addr = 0x00FF00FF;
  309. return (res);
  310. }