flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. #undef 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. /*
  52. * don't ask. its stupid, but more than one soul has had to live with this mistake
  53. * "swaptab[i]" is the value of "i" with the bits reversed.
  54. */
  55. #define MUSENKI_BROKEN_FLASH 1
  56. #ifdef MUSENKI_BROKEN_FLASH
  57. unsigned char swaptab[256] = {
  58. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  59. 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
  60. 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
  61. 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
  62. 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
  63. 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
  64. 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
  65. 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
  66. 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
  67. 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
  68. 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
  69. 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
  70. 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
  71. 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
  72. 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
  73. 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
  74. 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
  75. 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
  76. 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
  77. 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
  78. 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
  79. 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
  80. 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
  81. 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
  82. 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
  83. 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
  84. 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
  85. 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
  86. 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
  87. 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
  88. 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
  89. 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
  90. };
  91. #define BS(b) (swaptab[b])
  92. #else
  93. #define BS(b) (b)
  94. #endif
  95. #define BYTEME(x) ((x) & 0xFF)
  96. /*-----------------------------------------------------------------------
  97. */
  98. unsigned long flash_init (void)
  99. {
  100. unsigned long size_b0, size_b1;
  101. int i;
  102. /* Init: no FLASHes known */
  103. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  104. flash_info[i].flash_id = FLASH_UNKNOWN;
  105. }
  106. /* Static FLASH Bank configuration here - FIXME XXX */
  107. DEBUGF("\n## Get flash bank 1 size @ 0x%08x\n",CFG_FLASH_BASE0_PRELIM);
  108. size_b0 = flash_get_size((vu_char *)CFG_FLASH_BASE0_PRELIM, &flash_info[0]);
  109. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  110. printf ("## Unknown FLASH on Bank 0: "
  111. "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
  112. flash_info[0].flash_id,
  113. size_b0, size_b0<<20);
  114. }
  115. DEBUGF("## Get flash bank 2 size @ 0x%08x\n",CFG_FLASH_BASE1_PRELIM);
  116. size_b1 = flash_get_size((vu_char *)CFG_FLASH_BASE1_PRELIM, &flash_info[1]);
  117. DEBUGF("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
  118. flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
  119. flash_info[0].size = size_b0;
  120. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  121. DEBUGF("protect monitor %x @ %x\n", CFG_MONITOR_BASE, monitor_flash_len);
  122. /* monitor protection ON by default */
  123. flash_protect(FLAG_PROTECT_SET,
  124. CFG_MONITOR_BASE,
  125. CFG_MONITOR_BASE+monitor_flash_len-1,
  126. &flash_info[0]);
  127. #endif
  128. #ifdef CONFIG_ENV_IS_IN_FLASH
  129. /* ENV protection ON by default */
  130. DEBUGF("protect environtment %x @ %x\n", CFG_ENV_ADDR, CFG_ENV_SECT_SIZE);
  131. flash_protect(FLAG_PROTECT_SET,
  132. CFG_ENV_ADDR,
  133. CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
  134. &flash_info[0]);
  135. #endif
  136. if (size_b1) {
  137. flash_info[1].size = size_b1;
  138. flash_get_offsets (CFG_FLASH_BASE + size_b0, &flash_info[1]);
  139. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  140. /* monitor protection ON by default */
  141. flash_protect(FLAG_PROTECT_SET,
  142. CFG_MONITOR_BASE,
  143. CFG_MONITOR_BASE+monitor_flash_len-1,
  144. &flash_info[1]);
  145. #endif
  146. #ifdef CONFIG_ENV_IS_IN_FLASH
  147. /* ENV protection ON by default */
  148. flash_protect(FLAG_PROTECT_SET,
  149. CFG_ENV_ADDR,
  150. CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
  151. &flash_info[1]);
  152. #endif
  153. } else {
  154. flash_info[1].flash_id = FLASH_UNKNOWN;
  155. flash_info[1].sector_count = -1;
  156. flash_info[1].size = 0;
  157. }
  158. DEBUGF("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
  159. return (size_b0 + size_b1);
  160. }
  161. /*-----------------------------------------------------------------------
  162. */
  163. static void flash_get_offsets (ulong base, flash_info_t *info)
  164. {
  165. int i;
  166. if (info->flash_id == FLASH_UNKNOWN) {
  167. return;
  168. }
  169. switch (info->flash_id & FLASH_VENDMASK) {
  170. case FLASH_MAN_INTEL:
  171. for (i = 0; i < info->sector_count; i++) {
  172. info->start[i] = base;
  173. base += 0x00020000; /* 128k per bank */
  174. }
  175. return;
  176. default:
  177. printf ("Don't know sector ofsets for flash type 0x%lx\n", info->flash_id);
  178. return;
  179. }
  180. }
  181. /*-----------------------------------------------------------------------
  182. */
  183. void flash_print_info (flash_info_t *info)
  184. {
  185. int i;
  186. if (info->flash_id == FLASH_UNKNOWN) {
  187. printf ("missing or unknown FLASH type\n");
  188. return;
  189. }
  190. switch (info->flash_id & FLASH_VENDMASK) {
  191. case FLASH_MAN_AMD: printf ("AMD "); break;
  192. case FLASH_MAN_FUJ: printf ("Fujitsu "); break;
  193. case FLASH_MAN_SST: printf ("SST "); break;
  194. case FLASH_MAN_STM: printf ("STM "); break;
  195. case FLASH_MAN_INTEL: printf ("Intel "); break;
  196. case FLASH_MAN_MT: printf ("MT "); break;
  197. default: printf ("Unknown Vendor "); break;
  198. }
  199. switch (info->flash_id & FLASH_TYPEMASK) {
  200. case FLASH_28F320J3A: printf ("28F320J3A (32Mbit = 128K x 32)\n");
  201. break;
  202. case FLASH_28F640J3A: printf ("28F640J3A (64Mbit = 128K x 64)\n");
  203. break;
  204. case FLASH_28F128J3A: printf ("28F128J3A (128Mbit = 128K x 128)\n");
  205. break;
  206. default: printf ("Unknown Chip Type\n");
  207. break;
  208. }
  209. if (info->size >= (1 << 20)) {
  210. i = 20;
  211. } else {
  212. i = 10;
  213. }
  214. printf (" Size: %ld %cB in %d Sectors\n",
  215. info->size >> i,
  216. (i == 20) ? 'M' : 'k',
  217. info->sector_count);
  218. printf (" Sector Start Addresses:");
  219. for (i=0; i<info->sector_count; ++i) {
  220. if ((i % 5) == 0)
  221. printf ("\n ");
  222. printf (" %08lX%s",
  223. info->start[i],
  224. info->protect[i] ? " (RO)" : " "
  225. );
  226. }
  227. printf ("\n");
  228. return;
  229. }
  230. /*-----------------------------------------------------------------------
  231. */
  232. /*-----------------------------------------------------------------------
  233. */
  234. /*
  235. * The following code cannot be run from FLASH!
  236. */
  237. static ulong flash_get_size (vu_char *addr, flash_info_t *info)
  238. {
  239. vu_char manuf, device;
  240. addr[0] = BS(0x90);
  241. manuf = BS(addr[0]);
  242. DEBUGF("Manuf. ID @ 0x%08lx: 0x%08x\n", (vu_char *)addr, manuf);
  243. switch (manuf) {
  244. case BYTEME(AMD_MANUFACT):
  245. info->flash_id = FLASH_MAN_AMD;
  246. break;
  247. case BYTEME(FUJ_MANUFACT):
  248. info->flash_id = FLASH_MAN_FUJ;
  249. break;
  250. case BYTEME(SST_MANUFACT):
  251. info->flash_id = FLASH_MAN_SST;
  252. break;
  253. case BYTEME(STM_MANUFACT):
  254. info->flash_id = FLASH_MAN_STM;
  255. break;
  256. case BYTEME(INTEL_MANUFACT):
  257. info->flash_id = FLASH_MAN_INTEL;
  258. break;
  259. default:
  260. info->flash_id = FLASH_UNKNOWN;
  261. info->sector_count = 0;
  262. info->size = 0;
  263. addr[0] = BS(0xFF); /* restore read mode, (yes, BS is a NOP) */
  264. return 0; /* no or unknown flash */
  265. }
  266. device = BS(addr[2]); /* device ID */
  267. DEBUGF("Device ID @ 0x%08x: 0x%08x\n", (&addr[1]), device);
  268. switch (device) {
  269. case BYTEME(INTEL_ID_28F320J3A):
  270. info->flash_id += FLASH_28F320J3A;
  271. info->sector_count = 32;
  272. info->size = 0x00400000;
  273. break; /* => 4 MB */
  274. case BYTEME(INTEL_ID_28F640J3A):
  275. info->flash_id += FLASH_28F640J3A;
  276. info->sector_count = 64;
  277. info->size = 0x00800000;
  278. break; /* => 8 MB */
  279. case BYTEME(INTEL_ID_28F128J3A):
  280. info->flash_id += FLASH_28F128J3A;
  281. info->sector_count = 128;
  282. info->size = 0x01000000;
  283. break; /* => 16 MB */
  284. default:
  285. info->flash_id = FLASH_UNKNOWN;
  286. addr[0] = BS(0xFF); /* restore read mode (yes, a NOP) */
  287. return 0; /* => no or unknown flash */
  288. }
  289. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  290. printf ("** ERROR: sector count %d > max (%d) **\n",
  291. info->sector_count, CFG_MAX_FLASH_SECT);
  292. info->sector_count = CFG_MAX_FLASH_SECT;
  293. }
  294. addr[0] = BS(0xFF); /* restore read mode */
  295. return (info->size);
  296. }
  297. /*-----------------------------------------------------------------------
  298. */
  299. int flash_erase (flash_info_t *info, int s_first, int s_last)
  300. {
  301. int flag, prot, sect;
  302. ulong start, now, last;
  303. if ((s_first < 0) || (s_first > s_last)) {
  304. if (info->flash_id == FLASH_UNKNOWN) {
  305. printf ("- missing\n");
  306. } else {
  307. printf ("- no sectors to erase\n");
  308. }
  309. return 1;
  310. }
  311. if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
  312. printf ("Can erase only Intel flash types - aborted\n");
  313. return 1;
  314. }
  315. prot = 0;
  316. for (sect=s_first; sect<=s_last; ++sect) {
  317. if (info->protect[sect]) {
  318. prot++;
  319. }
  320. }
  321. if (prot) {
  322. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  323. } else {
  324. printf ("\n");
  325. }
  326. start = get_timer (0);
  327. last = start;
  328. /* Start erase on unprotected sectors */
  329. for (sect = s_first; sect<=s_last; sect++) {
  330. if (info->protect[sect] == 0) { /* not protected */
  331. vu_char *addr = (vu_char *)(info->start[sect]);
  332. unsigned long status;
  333. /* Disable interrupts which might cause a timeout here */
  334. flag = disable_interrupts();
  335. *addr = BS(0x50); /* clear status register */
  336. *addr = BS(0x20); /* erase setup */
  337. *addr = BS(0xD0); /* erase confirm */
  338. /* re-enable interrupts if necessary */
  339. if (flag) {
  340. enable_interrupts();
  341. }
  342. /* wait at least 80us - let's wait 1 ms */
  343. udelay (1000);
  344. while (((status = BS(*addr)) & BYTEME(0x00800080)) != BYTEME(0x00800080)) {
  345. if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  346. printf ("Timeout\n");
  347. *addr = BS(0xB0); /* suspend erase */
  348. *addr = BS(0xFF); /* reset to read mode */
  349. return 1;
  350. }
  351. /* show that we're waiting */
  352. if ((now - last) > 1000) { /* every second */
  353. putc ('.');
  354. last = now;
  355. }
  356. }
  357. *addr = BS(0xFF); /* reset to read mode */
  358. }
  359. }
  360. printf (" done\n");
  361. return 0;
  362. }
  363. /*-----------------------------------------------------------------------
  364. * Copy memory to flash, returns:
  365. * 0 - OK
  366. * 1 - write timeout
  367. * 2 - Flash not erased
  368. * 4 - Flash not identified
  369. */
  370. #define FLASH_WIDTH 1 /* flash bus width in bytes */
  371. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  372. {
  373. uchar *wp = (uchar *)addr;
  374. int rc;
  375. if (info->flash_id == FLASH_UNKNOWN) {
  376. return 4;
  377. }
  378. while (cnt > 0) {
  379. if ((rc = write_data(info, wp, *src)) != 0) {
  380. return rc;
  381. }
  382. wp++;
  383. src++;
  384. cnt--;
  385. }
  386. return cnt;
  387. }
  388. /*-----------------------------------------------------------------------
  389. * Write a word to Flash, returns:
  390. * 0 - OK
  391. * 1 - write timeout
  392. * 2 - Flash not erased
  393. */
  394. static int write_data (flash_info_t *info, uchar *dest, uchar data)
  395. {
  396. vu_char *addr = (vu_char *)dest;
  397. ulong status;
  398. ulong start;
  399. int flag;
  400. /* Check if Flash is (sufficiently) erased */
  401. if ((BS(*addr) & data) != data) {
  402. return 2;
  403. }
  404. /* Disable interrupts which might cause a timeout here */
  405. flag = disable_interrupts();
  406. *addr = BS(0x40); /* write setup */
  407. *addr = data;
  408. /* re-enable interrupts if necessary */
  409. if (flag) {
  410. enable_interrupts();
  411. }
  412. start = get_timer (0);
  413. while (((status = BS(*addr)) & BYTEME(0x00800080)) != BYTEME(0x00800080)) {
  414. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  415. *addr = BS(0xFF); /* restore read mode */
  416. return 1;
  417. }
  418. }
  419. *addr = BS(0xFF); /* restore read mode */
  420. return 0;
  421. }
  422. /*-----------------------------------------------------------------------
  423. */