flash.c 8.9 KB

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