flash.c 8.6 KB

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