flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * (C) Copyright 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <mpc824x.h>
  25. #if defined(CONFIG_ENV_IS_IN_FLASH)
  26. # ifndef CFG_ENV_ADDR
  27. # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
  28. # endif
  29. # ifndef CFG_ENV_SIZE
  30. # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
  31. # endif
  32. # ifndef CFG_ENV_SECT_SIZE
  33. # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
  34. # endif
  35. #endif
  36. /*---------------------------------------------------------------------*/
  37. #define DEBUG_FLASH
  38. #ifdef DEBUG_FLASH
  39. #define DEBUGF(fmt,args...) printf(fmt ,##args)
  40. #else
  41. #define DEBUGF(fmt,args...)
  42. #endif
  43. /*---------------------------------------------------------------------*/
  44. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  45. /*-----------------------------------------------------------------------
  46. * Functions
  47. */
  48. static ulong flash_get_size (vu_char *addr, flash_info_t *info);
  49. static int write_data (flash_info_t *info, uchar *dest, uchar data);
  50. static void flash_get_offsets (ulong base, flash_info_t *info);
  51. #define BS(b) (b)
  52. #define BYTEME(x) ((x) & 0xFF)
  53. /*-----------------------------------------------------------------------
  54. */
  55. unsigned long flash_init (void)
  56. {
  57. unsigned long flash_banks[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS;
  58. unsigned long size, size_b[CFG_MAX_FLASH_BANKS];
  59. int i;
  60. /* Init: no FLASHes known */
  61. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i)
  62. {
  63. flash_info[i].flash_id = FLASH_UNKNOWN;
  64. DEBUGF("Get flash bank %d @ 0x%08lx\n", i, flash_banks[i]);
  65. /*
  66. size_b[i] = flash_get_size((vu_char *)flash_banks[i], &flash_info[i]);
  67. */
  68. size_b[i] = flash_get_size((vu_char *) 0xff800000 , &flash_info[i]);
  69. if (flash_info[i].flash_id == FLASH_UNKNOWN)
  70. {
  71. printf ("## Unknown FLASH on Bank %d: "
  72. "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
  73. i, flash_info[i].flash_id,
  74. size_b[i], size_b[i]<<20);
  75. }
  76. else
  77. {
  78. DEBUGF("## Flash bank %d at 0x%08lx sizes: 0x%08lx \n",
  79. i, flash_banks[i], size_b[i]);
  80. flash_get_offsets (flash_banks[i], &flash_info[i]);
  81. flash_info[i].size = size_b[i];
  82. }
  83. }
  84. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  85. DEBUGF("protect monitor %x @ %x\n", CFG_MONITOR_BASE, CFG_MONITOR_LEN);
  86. /* monitor protection ON by default */
  87. flash_protect(FLAG_PROTECT_SET,
  88. CFG_MONITOR_BASE,
  89. CFG_MONITOR_BASE+CFG_MONITOR_LEN-1,
  90. &flash_info[0]);
  91. #endif
  92. #ifdef CONFIG_ENV_IS_IN_FLASH
  93. /* ENV protection ON by default */
  94. DEBUGF("protect environtment %x @ %x\n", CFG_ENV_ADDR, CFG_ENV_SECT_SIZE);
  95. flash_protect(FLAG_PROTECT_SET,
  96. CFG_ENV_ADDR,
  97. CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
  98. &flash_info[0]);
  99. #endif
  100. size = 0;
  101. DEBUGF("## Final Flash bank sizes: ");
  102. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i)
  103. {
  104. DEBUGF("%08lx ", size_b[i]);
  105. size += size_b[i];
  106. }
  107. DEBUGF("\n");
  108. return (size);
  109. }
  110. /*-----------------------------------------------------------------------
  111. */
  112. static void flash_get_offsets (ulong base, flash_info_t *info)
  113. {
  114. int i;
  115. if (info->flash_id == FLASH_UNKNOWN) {
  116. return;
  117. }
  118. switch (info->flash_id & FLASH_VENDMASK) {
  119. case FLASH_MAN_INTEL:
  120. for (i = 0; i < info->sector_count; i++) {
  121. info->start[i] = base;
  122. base += 0x00020000; /* 128k per bank */
  123. }
  124. return;
  125. default:
  126. printf ("Don't know sector ofsets for flash type 0x%lx\n", info->flash_id);
  127. return;
  128. }
  129. }
  130. /*-----------------------------------------------------------------------
  131. */
  132. void flash_print_info (flash_info_t *info)
  133. {
  134. int i;
  135. if (info->flash_id == FLASH_UNKNOWN) {
  136. printf ("missing or unknown FLASH type\n");
  137. return;
  138. }
  139. switch (info->flash_id & FLASH_VENDMASK) {
  140. case FLASH_MAN_AMD: printf ("AMD "); break;
  141. case FLASH_MAN_FUJ: printf ("Fujitsu "); break;
  142. case FLASH_MAN_SST: printf ("SST "); break;
  143. case FLASH_MAN_STM: printf ("STM "); break;
  144. case FLASH_MAN_INTEL: printf ("Intel "); break;
  145. case FLASH_MAN_MT: printf ("MT "); break;
  146. default: printf ("Unknown Vendor "); break;
  147. }
  148. switch (info->flash_id & FLASH_TYPEMASK) {
  149. case FLASH_28F320J3A:
  150. printf ("28F320J3A (32Mbit = 128K x 32)\n");
  151. break;
  152. case FLASH_28F640J3A:
  153. printf ("28F640J3A (64Mbit = 128K x 64)\n");
  154. break;
  155. case FLASH_28F128J3A:
  156. printf ("28F128J3A (128Mbit = 128K x 128)\n");
  157. break;
  158. default:
  159. printf ("Unknown Chip Type\n");
  160. break;
  161. }
  162. #if 1
  163. if (info->size >= (1 << 20)) {
  164. i = 20;
  165. } else {
  166. i = 10;
  167. }
  168. printf (" Size: %ld %cB in %d Sectors\n",
  169. info->size >> i,
  170. (i == 20) ? 'M' : 'k',
  171. info->sector_count);
  172. printf (" Sector Start Addresses:");
  173. for (i=0; i<info->sector_count; ++i) {
  174. if ((i % 5) == 0)
  175. printf ("\n ");
  176. printf (" %08lX%s",
  177. info->start[i],
  178. info->protect[i] ? " (RO)" : " "
  179. );
  180. }
  181. printf ("\n");
  182. #endif
  183. return;
  184. }
  185. /*-----------------------------------------------------------------------
  186. */
  187. /*-----------------------------------------------------------------------
  188. */
  189. /*
  190. * The following code cannot be run from FLASH!
  191. */
  192. static ulong flash_get_size (vu_char *addr, flash_info_t *info)
  193. {
  194. vu_char manuf, device;
  195. addr[0] = BS(0x90);
  196. manuf = BS(addr[0]);
  197. DEBUGF("Manuf. ID @ 0x%08lx: 0x%08x\n", (ulong)addr, manuf);
  198. switch (manuf) {
  199. case BYTEME(AMD_MANUFACT):
  200. info->flash_id = FLASH_MAN_AMD;
  201. break;
  202. case BYTEME(FUJ_MANUFACT):
  203. info->flash_id = FLASH_MAN_FUJ;
  204. break;
  205. case BYTEME(SST_MANUFACT):
  206. info->flash_id = FLASH_MAN_SST;
  207. break;
  208. case BYTEME(STM_MANUFACT):
  209. info->flash_id = FLASH_MAN_STM;
  210. break;
  211. case BYTEME(INTEL_MANUFACT):
  212. info->flash_id = FLASH_MAN_INTEL;
  213. break;
  214. default:
  215. info->flash_id = FLASH_UNKNOWN;
  216. info->sector_count = 0;
  217. info->size = 0;
  218. addr[0] = BS(0xFF); /* restore read mode, (yes, BS is a NOP) */
  219. return 0; /* no or unknown flash */
  220. }
  221. device = BS(addr[2]); /* device ID */
  222. DEBUGF("Device ID @ 0x%08lx: 0x%08x\n", (ulong)(&addr[1]), device);
  223. switch (device) {
  224. case BYTEME(INTEL_ID_28F320J3A):
  225. info->flash_id += FLASH_28F320J3A;
  226. info->sector_count = 32;
  227. info->size = 0x00400000;
  228. break; /* => 4 MB */
  229. case BYTEME(INTEL_ID_28F640J3A):
  230. info->flash_id += FLASH_28F640J3A;
  231. info->sector_count = 64;
  232. info->size = 0x00800000;
  233. break; /* => 8 MB */
  234. case BYTEME(INTEL_ID_28F128J3A):
  235. info->flash_id += FLASH_28F128J3A;
  236. info->sector_count = 128;
  237. info->size = 0x01000000;
  238. break; /* => 16 MB */
  239. default:
  240. info->flash_id = FLASH_UNKNOWN;
  241. addr[0] = BS(0xFF); /* restore read mode (yes, a NOP) */
  242. return 0; /* => no or unknown flash */
  243. }
  244. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  245. printf ("** ERROR: sector count %d > max (%d) **\n",
  246. info->sector_count, CFG_MAX_FLASH_SECT);
  247. info->sector_count = CFG_MAX_FLASH_SECT;
  248. }
  249. addr[0] = BS(0xFF); /* restore read mode */
  250. return (info->size);
  251. }
  252. /*-----------------------------------------------------------------------
  253. */
  254. int flash_erase (flash_info_t *info, int s_first, int s_last)
  255. {
  256. int flag, prot, sect;
  257. ulong start, now, last;
  258. if ((s_first < 0) || (s_first > s_last)) {
  259. if (info->flash_id == FLASH_UNKNOWN) {
  260. printf ("- missing\n");
  261. } else {
  262. printf ("- no sectors to erase\n");
  263. }
  264. return 1;
  265. }
  266. if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
  267. printf ("Can erase only Intel flash types - aborted\n");
  268. return 1;
  269. }
  270. prot = 0;
  271. for (sect=s_first; sect<=s_last; ++sect) {
  272. if (info->protect[sect]) {
  273. prot++;
  274. }
  275. }
  276. if (prot) {
  277. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  278. } else {
  279. printf ("\n");
  280. }
  281. start = get_timer (0);
  282. last = start;
  283. /* Start erase on unprotected sectors */
  284. for (sect = s_first; sect<=s_last; sect++) {
  285. if (info->protect[sect] == 0) { /* not protected */
  286. vu_char *addr = (vu_char *)(info->start[sect]);
  287. unsigned long status;
  288. /* Disable interrupts which might cause a timeout here */
  289. flag = disable_interrupts();
  290. *addr = BS(0x50); /* clear status register */
  291. *addr = BS(0x20); /* erase setup */
  292. *addr = BS(0xD0); /* erase confirm */
  293. /* re-enable interrupts if necessary */
  294. if (flag) {
  295. enable_interrupts();
  296. }
  297. /* wait at least 80us - let's wait 1 ms */
  298. udelay (1000);
  299. while (((status = BS(*addr)) & BYTEME(0x00800080)) != BYTEME(0x00800080)) {
  300. if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  301. printf ("Timeout\n");
  302. *addr = BS(0xB0); /* suspend erase */
  303. *addr = BS(0xFF); /* reset to read mode */
  304. return 1;
  305. }
  306. /* show that we're waiting */
  307. if ((now - last) > 1000) { /* every second */
  308. putc ('.');
  309. last = now;
  310. }
  311. }
  312. *addr = BS(0xFF); /* reset to read mode */
  313. }
  314. }
  315. printf (" done\n");
  316. return 0;
  317. }
  318. /*-----------------------------------------------------------------------
  319. * Copy memory to flash, returns:
  320. * 0 - OK
  321. * 1 - write timeout
  322. * 2 - Flash not erased
  323. * 4 - Flash not identified
  324. */
  325. #define FLASH_WIDTH 1 /* flash bus width in bytes */
  326. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  327. {
  328. uchar *wp = (uchar *)addr;
  329. int rc;
  330. if (info->flash_id == FLASH_UNKNOWN) {
  331. return 4;
  332. }
  333. while (cnt > 0) {
  334. if ((rc = write_data(info, wp, *src)) != 0) {
  335. return rc;
  336. }
  337. wp++;
  338. src++;
  339. cnt--;
  340. }
  341. return cnt;
  342. }
  343. /*-----------------------------------------------------------------------
  344. * Write a word to Flash, returns:
  345. * 0 - OK
  346. * 1 - write timeout
  347. * 2 - Flash not erased
  348. */
  349. static int write_data (flash_info_t *info, uchar *dest, uchar data)
  350. {
  351. vu_char *addr = (vu_char *)dest;
  352. ulong status;
  353. ulong start;
  354. int flag;
  355. /* Check if Flash is (sufficiently) erased */
  356. if ((BS(*addr) & data) != data) {
  357. return 2;
  358. }
  359. /* Disable interrupts which might cause a timeout here */
  360. flag = disable_interrupts();
  361. *addr = BS(0x40); /* write setup */
  362. *addr = data;
  363. /* re-enable interrupts if necessary */
  364. if (flag) {
  365. enable_interrupts();
  366. }
  367. start = get_timer (0);
  368. while (((status = BS(*addr)) & BYTEME(0x00800080)) != BYTEME(0x00800080)) {
  369. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  370. *addr = BS(0xFF); /* restore read mode */
  371. return 1;
  372. }
  373. }
  374. *addr = BS(0xFF); /* restore read mode */
  375. return 0;
  376. }
  377. /*-----------------------------------------------------------------------
  378. */