flash.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. * (C) Copyright 2003
  3. * Wolfgang Denk, DENX Software Engineering, [email protected].
  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. //joelin 10/07/2004 for MXIC MX29LV320ABTC-90
  24. #include <common.h>
  25. #include <asm/danube.h>
  26. /*
  27. #ifdef CONFIG_AMAZON
  28. #define FLASH_DELAY {int i; \
  29. for(i=0;i<800;i++) \
  30. *((volatile u32 *)CFG_SDRAM_BASE_UNCACHE); \
  31. }
  32. #else
  33. #define FLASH_DELAY
  34. #endif
  35. */
  36. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  37. /* NOTE - CONFIG_FLASH_16BIT means the CPU interface is 16-bit, it
  38. * has nothing to do with the flash chip being 8-bit or 16-bit.
  39. */
  40. #ifdef CONFIG_FLASH_16BIT
  41. typedef unsigned short FLASH_PORT_WIDTH;
  42. typedef volatile unsigned short FLASH_PORT_WIDTHV;
  43. #define FLASH_ID_MASK 0xFFFF
  44. #else
  45. typedef unsigned long FLASH_PORT_WIDTH;
  46. typedef volatile unsigned long FLASH_PORT_WIDTHV;
  47. #define FLASH_ID_MASK 0xFFFFFFFF
  48. #endif
  49. #define FPW FLASH_PORT_WIDTH
  50. #define FPWV FLASH_PORT_WIDTHV
  51. #define ORMASK(size) ((-size) & OR_AM_MSK) // 0xffff8000
  52. #if 0
  53. #define FLASH_CYCLE1 0x0555
  54. #define FLASH_CYCLE2 0x02aa
  55. #else
  56. #define FLASH_CYCLE1 0x0554 //joelin for MX29LV320AT/B 0x0555
  57. #define FLASH_CYCLE2 0x02ab //joelin for MX29LV320AT/B 0x02aa
  58. #endif
  59. /*-----------------------------------------------------------------------
  60. * Functions
  61. */
  62. static ulong flash_get_size(FPWV *addr, flash_info_t *info);
  63. static void flash_reset(flash_info_t *info);
  64. static int write_word_intel(flash_info_t *info, FPWV *dest, FPW data);
  65. static int write_word_amd(flash_info_t *info, FPWV *dest, FPW data);
  66. static void flash_get_offsets(ulong base, flash_info_t *info);
  67. static flash_info_t *flash_get_info(ulong base);
  68. /*-----------------------------------------------------------------------
  69. * flash_init()
  70. *
  71. * sets up flash_info and returns size of FLASH (bytes)
  72. */
  73. unsigned long flash_init (void)
  74. {
  75. unsigned long size = 0;
  76. int i;
  77. /* Init: no FLASHes known */
  78. for (i=0; i < CFG_MAX_FLASH_BANKS; ++i) { // 1 bank
  79. ulong flashbase = (i == 0) ? PHYS_FLASH_1 : PHYS_FLASH_2; // 0xb0000000, 0xb4000000
  80. volatile ulong * buscon = (ulong *)
  81. ((i == 0) ? DANUBE_EBU_BUSCON0 : DANUBE_EBU_BUSCON1);
  82. /* Disable write protection */
  83. // *buscon &= ~AMAZON_EBU_BUSCON0_WRDIS;
  84. /* Enable write protection */
  85. *buscon |= DANUBE_EBU_BUSCON0_WRDIS;
  86. #if 1
  87. memset(&flash_info[i], 0, sizeof(flash_info_t));
  88. #endif
  89. flash_info[i].size =
  90. flash_get_size((FPW *)flashbase, &flash_info[i]);
  91. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  92. printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx\n",
  93. i, flash_info[i].size);
  94. }
  95. size += flash_info[i].size;
  96. }
  97. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE // TEXT_BASE >= 0xB3000000
  98. /* monitor protection ON by default */ /* only use software protection, info->protect[i]=0/1 */
  99. /* flash_protect(FLAG_PROTECT_SET,
  100. CFG_MONITOR_BASE,
  101. CFG_MONITOR_BASE+CFG_MONITOR_LEN-1,
  102. flash_get_info(CFG_MONITOR_BASE));
  103. */
  104. flash_protect(FLAG_PROTECT_CLEAR, // clear protect
  105. CFG_MONITOR_BASE,
  106. CFG_MONITOR_BASE+CFG_MONITOR_LEN-1,
  107. flash_get_info(CFG_MONITOR_BASE));
  108. #endif
  109. #ifdef CFG_ENV_IS_IN_FLASH /* 1 */
  110. /* ENV protection ON by default */
  111. /* flash_protect(FLAG_PROTECT_SET,
  112. CFG_ENV_ADDR,
  113. CFG_ENV_ADDR+CFG_ENV_SIZE-1,
  114. flash_get_info(CFG_ENV_ADDR));
  115. */
  116. flash_protect(FLAG_PROTECT_CLEAR,
  117. CFG_ENV_ADDR,
  118. CFG_ENV_ADDR+CFG_ENV_SIZE-1,
  119. flash_get_info(CFG_ENV_ADDR));
  120. #endif
  121. return size;
  122. }
  123. /*-----------------------------------------------------------------------
  124. */
  125. static void flash_reset(flash_info_t *info)
  126. {
  127. FPWV *base = (FPWV *)(info->start[0]);
  128. (*DANUBE_EBU_BUSCON0)&=(~0x80000000); // enable writing
  129. (*DANUBE_EBU_BUSCON1)&=(~0x80000000); // enable writing
  130. (*EBU_NAND_CON)=0;
  131. /* Put FLASH back in read mode */
  132. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL){
  133. *base = (FPW)0x00FF00FF; /* Intel Read Mode */
  134. asm("SYNC");
  135. }
  136. else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD){
  137. *base = (FPW)0x00F000F0; /* AMD Read Mode */
  138. asm("SYNC"); //joelin
  139. }
  140. else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_MX){
  141. *base = (FPW)0x00F000F0; /* MXIC Read Mode */
  142. asm("SYNC"); //joelin
  143. }
  144. (*DANUBE_EBU_BUSCON0)|=0x80000000; // disable writing
  145. (*DANUBE_EBU_BUSCON1)|=0x80000000; // disable writing
  146. }
  147. /*-----------------------------------------------------------------------
  148. */
  149. static void flash_get_offsets (ulong base, flash_info_t *info)
  150. {
  151. int i;
  152. /* set up sector start address table */
  153. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL
  154. && (info->flash_id & FLASH_BTYPE)) {
  155. int bootsect_size; /* number of bytes/boot sector */
  156. int sect_size; /* number of bytes/regular sector */
  157. bootsect_size = 0x00002000 * (sizeof(FPW)/2);
  158. sect_size = 0x00010000 * (sizeof(FPW)/2);
  159. /* set sector offsets for bottom boot block type */
  160. for (i = 0; i < 8; ++i) {
  161. info->start[i] = base + (i * bootsect_size);
  162. }
  163. for (i = 8; i < info->sector_count; i++) {
  164. info->start[i] = base + ((i - 7) * sect_size);
  165. }
  166. }
  167. else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD
  168. && (info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U) {
  169. int sect_size; /* number of bytes/sector */
  170. sect_size = 0x00010000 * (sizeof(FPW)/2);
  171. /* set up sector start address table (uniform sector type) */
  172. for( i = 0; i < info->sector_count; i++ )
  173. info->start[i] = base + (i * sect_size);
  174. }
  175. else if(((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
  176. && ((info->flash_id & FLASH_TYPEMASK)==FLASH_28F128J3A)){
  177. int sect_size;
  178. sect_size = 0x20000;
  179. for(i=0;i < info->sector_count; i++)
  180. info->start[i]= base + (i*sect_size);
  181. }
  182. else if(((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
  183. && ((info->flash_id & FLASH_TYPEMASK)==FLASH_28F320J3A)){
  184. int sect_size;
  185. sect_size = 0x20000;
  186. for(i=0;i < info->sector_count; i++)
  187. info->start[i]= base + (i*sect_size);
  188. }
  189. //joelin add for MX29LV320AB-- SA0~SA7:sector size=8K bytes ,SA9~SA70 :sector size=64k bytes
  190. else if(((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_MX)
  191. && ((info->flash_id & FLASH_TYPEMASK)==FLASH_29LV320AB)){
  192. int bootsect_size; /* number of bytes/boot sector */
  193. int sect_size; /* number of bytes/regular sector */
  194. bootsect_size = 0x00002000 * (sizeof(FPW)/2);
  195. sect_size = 0x00010000 * (sizeof(FPW)/2);
  196. /* set sector offsets for bottom boot block type */
  197. for (i = 0; i < 8; ++i) {
  198. info->start[i] = base + (i * bootsect_size);
  199. }
  200. for (i = 8; i < info->sector_count; i++) {
  201. info->start[i] = base + ((i - 7) * sect_size);
  202. }
  203. }
  204. else if(((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
  205. && ((info->flash_id & FLASH_TYPEMASK)==FLASH_29LV320B)){
  206. int bootsect_size; /* number of bytes/boot sector */
  207. int sect_size; /* number of bytes/regular sector */
  208. bootsect_size = 0x00002000 * (sizeof(FPW)/2);
  209. sect_size = 0x00010000 * (sizeof(FPW)/2);
  210. /* set sector offsets for bottom boot block type */
  211. for (i = 0; i < 8; ++i) {
  212. info->start[i] = base + (i * bootsect_size);
  213. }
  214. for (i = 8; i < info->sector_count; i++) {
  215. info->start[i] = base + ((i - 7) * sect_size);
  216. }
  217. }
  218. //joelin add for MX29LV160BB-- SA0=16K,SA1,SA2=8K,SA3=32K bytes ,SA4~SA34 :sector size=64k bytes
  219. else if(((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_MX)
  220. && ((info->flash_id & FLASH_TYPEMASK)==FLASH_29LV160BB)){
  221. int bootsect_size; /* number of bytes/boot sector */
  222. int sect_size; /* number of bytes/regular sector */
  223. bootsect_size = 0x00002000 * (sizeof(FPW)/2);
  224. sect_size = 0x00010000 * (sizeof(FPW)/2);
  225. /* set sector offsets for bottom boot block type */
  226. //MX29LV160BB
  227. info->start[0] = base ; //SA0=16K bytes
  228. info->start[1] = info->start[0] + (1 * 0x00004000 * (sizeof(FPW)/2)); //SA1=8K bytes
  229. info->start[2] = info->start[1] + (1 * 0x00002000 * (sizeof(FPW)/2)); //SA2=8K bytes
  230. info->start[3] = info->start[2] + (1 * 0x00002000 * (sizeof(FPW)/2)); //SA3=32K bytes
  231. for (i = 4; i < info->sector_count; i++) {
  232. info->start[i] = base + ((i - 3) * sect_size);
  233. }
  234. }
  235. //liupeng add for MX29LV640BB-- SA0~SA7:sector size=8k bytes ,SA8~SA134 :sector size=64k bytes
  236. else if(((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_MX)
  237. && ((info->flash_id & FLASH_TYPEMASK)==FLASH_29LV640BB)){
  238. int bootsect_size; /* number of bytes/boot sector */
  239. int sect_size; /* number of bytes/regular sector */
  240. bootsect_size = 0x00002000 * (sizeof(FPW)/2);
  241. sect_size = 0x00010000 * (sizeof(FPW)/2);
  242. /* set sector offsets for bottom boot block type */
  243. for (i = 0; i < 8; ++i) {
  244. info->start[i] = base + (i * bootsect_size);
  245. }
  246. for (i = 8; i < info->sector_count; i++) {
  247. info->start[i] = base + ((i - 7) * sect_size);
  248. }
  249. }
  250. else{
  251. printf("flash get offsets fail\n");
  252. }
  253. }
  254. /*-----------------------------------------------------------------------
  255. */
  256. static flash_info_t *flash_get_info(ulong base)
  257. {
  258. int i;
  259. flash_info_t * info;
  260. for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
  261. info = & flash_info[i];
  262. if (info->start[0] <= base && base < info->start[0] + info->size)
  263. break;
  264. }
  265. return i == CFG_MAX_FLASH_BANKS ? 0 : info;
  266. }
  267. /*-----------------------------------------------------------------------
  268. */
  269. void flash_print_info (flash_info_t *info)
  270. {
  271. int i;
  272. uchar *boottype;
  273. uchar *bootletter;
  274. uchar *fmt;
  275. uchar botbootletter[] = "B";
  276. uchar topbootletter[] = "T";
  277. uchar botboottype[] = "bottom boot sector";
  278. uchar topboottype[] = "top boot sector";
  279. if (info->flash_id == FLASH_UNKNOWN) {
  280. printf ("missing or unknown FLASH type\n");
  281. return;
  282. }
  283. switch (info->flash_id & FLASH_VENDMASK) {
  284. case FLASH_MAN_AMD: printf ("AMD "); break;
  285. case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
  286. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  287. case FLASH_MAN_SST: printf ("SST "); break;
  288. case FLASH_MAN_STM: printf ("STM "); break;
  289. case FLASH_MAN_INTEL: printf ("INTEL "); break;
  290. case FLASH_MAN_MX: printf ("MXIC "); break;
  291. default: printf ("Unknown Vendor "); break;
  292. }
  293. /* check for top or bottom boot, if it applies */
  294. if (info->flash_id & FLASH_BTYPE) {
  295. boottype = botboottype;
  296. bootletter = botbootletter;
  297. }
  298. else {
  299. boottype = topboottype;
  300. bootletter = topbootletter;
  301. }
  302. switch (info->flash_id & FLASH_TYPEMASK) {
  303. case FLASH_AM640U:
  304. fmt = "29LV641D (64 Mbit, uniform sectors)\n";
  305. break;
  306. case FLASH_28F800C3B:
  307. case FLASH_28F800C3T:
  308. fmt = "28F800C3%s (8 Mbit, %s)\n";
  309. break;
  310. case FLASH_INTEL800B:
  311. case FLASH_INTEL800T:
  312. fmt = "28F800B3%s (8 Mbit, %s)\n";
  313. break;
  314. case FLASH_28F160C3B:
  315. case FLASH_28F160C3T:
  316. fmt = "28F160C3%s (16 Mbit, %s)\n";
  317. break;
  318. case FLASH_INTEL160B:
  319. case FLASH_INTEL160T:
  320. fmt = "28F160B3%s (16 Mbit, %s)\n";
  321. break;
  322. case FLASH_28F320C3B:
  323. case FLASH_28F320C3T:
  324. fmt = "28F320C3%s (32 Mbit, %s)\n";
  325. break;
  326. case FLASH_INTEL320B:
  327. case FLASH_INTEL320T:
  328. fmt = "28F320B3%s (32 Mbit, %s)\n";
  329. break;
  330. case FLASH_28F640C3B:
  331. case FLASH_28F640C3T:
  332. fmt = "28F640C3%s (64 Mbit, %s)\n";
  333. break;
  334. case FLASH_INTEL640B:
  335. case FLASH_INTEL640T:
  336. fmt = "28F640B3%s (64 Mbit, %s)\n";
  337. break;
  338. case FLASH_28F128J3A:
  339. fmt = "28F128J3A (128 Mbit, 128 uniform sectors)\n";
  340. break;
  341. case FLASH_28F320J3A:
  342. fmt = "28F320J3A (32 Mbit, 32 uniform sectors)\n";
  343. break;
  344. case FLASH_29LV640BB: //liupeng for MXIC FLASH_29LV640BB
  345. fmt = "29LV640BB (64 Mbit, boot sector SA0~SA126 size 64k bytes,other sectors SA127~SA135 size 8k bytes)\n";
  346. break;
  347. case FLASH_29LV320B: //joelin for MXIC FLASH_29LV320AB
  348. case FLASH_29LV320AB: //joelin for MXIC FLASH_29LV320AB
  349. fmt = "29LV320AB (32 Mbit, boot sector SA0~SA7 size 8K bytes,other sectors SA8~SA70 size 64K bytes)\n";
  350. break;
  351. case FLASH_29LV160BB: //joelin for MXIC FLASH_29LV160BB
  352. fmt = "29LV160BB (16 Mbit, boot sector SA0 size 16K bytes,SA1,SA2 size 8K bytes,SA3 size 32k bytes,other sectors SA4~SA34 size 64K bytes)\n";
  353. break;
  354. default:
  355. fmt = "Unknown Chip Type\n";
  356. break;
  357. }
  358. printf (fmt, bootletter, boottype);
  359. printf (" Size: %ld MB in %d Sectors\n",
  360. info->size >> 20,
  361. info->sector_count);
  362. printf (" Sector Start Addresses:");
  363. for (i=0; i<info->sector_count; ++i) {
  364. if ((i % 5) == 0) {
  365. printf ("\n ");
  366. }
  367. printf (" %08lX%s", info->start[i],
  368. info->protect[i] ? " (RO)" : " ");
  369. }
  370. printf ("\n");
  371. }
  372. /*-----------------------------------------------------------------------
  373. */
  374. /*
  375. * The following code cannot be run from FLASH!
  376. */
  377. ulong flash_get_size (FPWV *addr, flash_info_t *info)
  378. {
  379. (*DANUBE_EBU_BUSCON0)=0x1d7ff; //value from Aikann, should be used on the real chip
  380. (*EBU_ADDR_SEL_0) = 0x10000031; //starting address from 0xb0000000
  381. (*EBU_NAND_CON)=0;
  382. (*DANUBE_EBU_BUSCON0)&=(~0x80000000); // enable writing
  383. (*DANUBE_EBU_BUSCON1)&=(~0x80000000); // enable writing
  384. /* Write auto select command: read Manufacturer ID */
  385. /* Write auto select command sequence and test FLASH answer */
  386. addr[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* for AMD, Intel ignores this */
  387. asm("SYNC");
  388. addr[FLASH_CYCLE2] = (FPW)0x00550055; /* for AMD, Intel ignores this */
  389. asm("SYNC");
  390. addr[FLASH_CYCLE1] = (FPW)0x00900090; /* selects Intel or AMD */
  391. asm("SYNC");
  392. /* The manufacturer codes are only 1 byte, so just use 1 byte.
  393. * This works for any bus width and any FLASH device width.
  394. */
  395. printf("\n type is %08lx", addr[1] & 0xff); //joelin 10/06/2004 flash type
  396. printf("\n type is %08lx", addr[0] & 0xff); //joelin 10/06/2004 flash type
  397. // asm("SYNC");
  398. switch (addr[1] & 0xff) {
  399. case (uchar)AMD_MANUFACT:
  400. info->flash_id = FLASH_MAN_AMD;
  401. break;
  402. case (uchar)INTEL_MANUFACT: // 0x0089
  403. info->flash_id = FLASH_MAN_INTEL; //0x00300000
  404. break;
  405. //joelin for MXIC
  406. case (uchar)MX_MANUFACT: // 0x00c2
  407. info->flash_id = FLASH_MAN_MX ;//0x00030000
  408. break;
  409. default:
  410. info->flash_id = FLASH_UNKNOWN;
  411. info->sector_count = 0;
  412. info->size = 0;
  413. break;
  414. /* default:
  415. info->flash_id = FLASH_MAN_INTEL; //0x00300000
  416. break;*/
  417. }
  418. /* Check 16 bits or 32 bits of ID so work on 32 or 16 bit bus. */
  419. if (info->flash_id != FLASH_UNKNOWN) switch (addr[0]) {
  420. case (FPW)EON_ID_EN29LV320B:
  421. info->flash_id += FLASH_29LV320B;
  422. info->sector_count = 71;
  423. info->size = 0x00400000 * (sizeof(FPW)/2);
  424. break;
  425. case (FPW)AMD_ID_LV640U: /* 29LV640 and 29LV641 have same ID */
  426. info->flash_id += FLASH_AM640U;
  427. info->sector_count = 128;
  428. info->size = 0x00800000 * (sizeof(FPW)/2);
  429. break; /* => 8 or 16 MB */
  430. case (FPW)INTEL_ID_28F800C3B:
  431. info->flash_id += FLASH_28F800C3B;
  432. info->sector_count = 23;
  433. info->size = 0x00100000 * (sizeof(FPW)/2);
  434. break; /* => 1 or 2 MB */
  435. case (FPW)INTEL_ID_28F800B3B:
  436. info->flash_id += FLASH_INTEL800B;
  437. info->sector_count = 23;
  438. info->size = 0x00100000 * (sizeof(FPW)/2);
  439. break; /* => 1 or 2 MB */
  440. case (FPW)INTEL_ID_28F160C3B:
  441. info->flash_id += FLASH_28F160C3B;
  442. info->sector_count = 39;
  443. info->size = 0x00200000 * (sizeof(FPW)/2);
  444. break; /* => 2 or 4 MB */
  445. case (FPW)INTEL_ID_28F160B3B:
  446. info->flash_id += FLASH_INTEL160B;
  447. info->sector_count = 39;
  448. info->size = 0x00200000 * (sizeof(FPW)/2);
  449. break; /* => 2 or 4 MB */
  450. case (FPW)INTEL_ID_28F320C3B:
  451. info->flash_id += FLASH_28F320C3B;
  452. info->sector_count = 71;
  453. info->size = 0x00400000 * (sizeof(FPW)/2);
  454. break; /* => 4 or 8 MB */
  455. case (FPW)INTEL_ID_28F320B3B:
  456. info->flash_id += FLASH_INTEL320B;
  457. info->sector_count = 71;
  458. info->size = 0x00400000 * (sizeof(FPW)/2);
  459. break; /* => 4 or 8 MB */
  460. case (FPW)INTEL_ID_28F640C3B:
  461. info->flash_id += FLASH_28F640C3B;
  462. info->sector_count = 135;
  463. info->size = 0x00800000 * (sizeof(FPW)/2);
  464. break; /* => 8 or 16 MB */
  465. case (FPW)INTEL_ID_28F640B3B:
  466. info->flash_id += FLASH_INTEL640B;
  467. info->sector_count = 135;
  468. info->size = 0x00800000 * (sizeof(FPW)/2);
  469. break; /* => 8 or 16 MB */
  470. case (FPW)INTEL_ID_28F128J3A:
  471. info->flash_id +=FLASH_28F128J3A;
  472. info->sector_count = 128;
  473. info->size = 0x01000000 * (sizeof(FPW)/2);
  474. break; /* => 16 MB */
  475. case (FPW)INTEL_ID_28F320J3A:
  476. info->flash_id += FLASH_28F320J3A;
  477. info->sector_count = 32;
  478. info->size = 0x00400000 * (sizeof(FPW)/2);
  479. break;
  480. //joelin for MXIC
  481. case (FPW)MX_ID_29LV320AB:
  482. info->flash_id += FLASH_29LV320AB;
  483. info->sector_count = 71;
  484. info->size = 0x00400000 * (sizeof(FPW)/2);
  485. break; /* => 4 MB */
  486. /* => 4 MB */
  487. //joelin for MXIC
  488. case (FPW)MX_ID_29LV160BB:
  489. info->flash_id += FLASH_29LV160BB;
  490. info->sector_count = 35;
  491. info->size = 0x00200000 * (sizeof(FPW)/2);
  492. break; /* => 2 MB */
  493. /* => 2 MB */
  494. /* liupeng*/
  495. case (FPW)MX_ID_29LV640BB:
  496. info->flash_id += FLASH_29LV640BB;
  497. info->sector_count = 135;
  498. info->size = 0x00800000 * (sizeof(FPW)/2);
  499. break; /* => 2 MB */
  500. default:
  501. info->flash_id = FLASH_UNKNOWN;
  502. info->sector_count = 0;
  503. info->size = 0;
  504. return (0); /* => no or unknown flash */
  505. /* default:
  506. info->flash_id += FLASH_28F320J3A;
  507. info->sector_count = 32;
  508. info->size = 0x00400000 * (sizeof(FPW)/2);
  509. break;*/
  510. }
  511. (*DANUBE_EBU_BUSCON0)|=0x80000000; // disable writing
  512. (*DANUBE_EBU_BUSCON1)|=0x80000000; // disable writing
  513. flash_get_offsets((ulong)addr, info);
  514. /* Put FLASH back in read mode */
  515. flash_reset(info);
  516. return (info->size);
  517. }
  518. /*-----------------------------------------------------------------------
  519. */
  520. int flash_erase (flash_info_t *info, int s_first, int s_last)
  521. {
  522. FPWV *addr;
  523. int flag, prot, sect;
  524. int intel = (info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL;
  525. ulong start, now, last;
  526. int rcode = 0;
  527. if ((s_first < 0) || (s_first > s_last)) {
  528. if (info->flash_id == FLASH_UNKNOWN) {
  529. printf ("- missing\n");
  530. } else {
  531. printf ("- no sectors to erase\n");
  532. }
  533. return 1;
  534. }
  535. switch (info->flash_id & FLASH_TYPEMASK) {
  536. case FLASH_INTEL800B:
  537. case FLASH_INTEL160B:
  538. case FLASH_INTEL320B:
  539. case FLASH_INTEL640B:
  540. case FLASH_28F800C3B:
  541. case FLASH_28F160C3B:
  542. case FLASH_28F320C3B:
  543. case FLASH_28F640C3B:
  544. case FLASH_28F128J3A:
  545. case FLASH_28F320J3A:
  546. case FLASH_AM640U:
  547. case FLASH_29LV640BB: //liupeng for MXIC MX29LV640BB
  548. case FLASH_29LV320B:
  549. case FLASH_29LV320AB: //joelin for MXIC MX29LV320AB
  550. case FLASH_29LV160BB: //joelin for MXIC MX29LV160BB
  551. break;
  552. case FLASH_UNKNOWN:
  553. default:
  554. printf ("Can't erase unknown flash type %08lx - aborted\n",
  555. info->flash_id);
  556. return 1;
  557. }
  558. prot = 0;
  559. for (sect=s_first; sect<=s_last; ++sect) {
  560. if (info->protect[sect]) {
  561. prot++;
  562. }
  563. }
  564. if (prot) {
  565. printf ("- Warning: %d protected sectors will not be erased!\n",
  566. prot);
  567. } else {
  568. printf ("\n");
  569. }
  570. last = get_timer(0);
  571. /* Start erase on unprotected sectors */
  572. for (sect = s_first; sect<=s_last && rcode == 0; sect++) {
  573. if (info->protect[sect] != 0) /* protected, skip it */
  574. continue;
  575. /* Disable interrupts which might cause a timeout here */
  576. flag = disable_interrupts();
  577. (*DANUBE_EBU_BUSCON0)&=(~0x80000000); // enable writing
  578. (*DANUBE_EBU_BUSCON1)&=(~0x80000000); // enable writing
  579. (*EBU_NAND_CON)=0;
  580. addr = (FPWV *)(info->start[sect]);
  581. if (intel) {
  582. *addr = (FPW)0x00500050; /* clear status register */
  583. *addr = (FPW)0x00200020; /* erase setup */
  584. *addr = (FPW)0x00D000D0; /* erase confirm */
  585. asm("SYNC");
  586. }
  587. else {
  588. /* must be AMD style if not Intel */
  589. FPWV *base; /* first address in bank */
  590. base = (FPWV *)(info->start[0]);
  591. base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
  592. base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
  593. base[FLASH_CYCLE1] = (FPW)0x00800080; /* erase mode */
  594. base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
  595. base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
  596. *addr = (FPW)0x00300030; /* erase sector */
  597. }
  598. /* re-enable interrupts if necessary */
  599. if (flag)
  600. enable_interrupts();
  601. start = get_timer(0);
  602. /* wait at least 50us for AMD, 80us for Intel.
  603. * Let's wait 1 ms.
  604. */
  605. udelay (1000);
  606. while ((*addr & (FPW)0x00800080) != (FPW)0x00800080) {
  607. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  608. printf ("Erase Timeout\n");
  609. if (intel) {
  610. /* suspend erase */
  611. *addr = (FPW)0x00B000B0;
  612. }
  613. flash_reset(info); /* reset to read mode */
  614. rcode = 1; /* failed */
  615. break;
  616. }
  617. /* show that we're waiting */
  618. if ((get_timer(last)) > CFG_HZ) {/* every second */
  619. putc ('.');
  620. last = get_timer(0);
  621. }
  622. }
  623. //joelin for MXIC
  624. switch (info->flash_id & FLASH_VENDMASK) {
  625. case FLASH_MAN_MX: //joelin for MXIC
  626. break;
  627. default:
  628. if((*addr & (FPW)0x00200020) != (FPW)0x0)
  629. printf("Erase Error\n");
  630. break;
  631. }
  632. /* show that we're waiting */
  633. if ((get_timer(last)) > CFG_HZ) { /* every second */
  634. putc ('.');
  635. last = get_timer(0);
  636. }
  637. //flash_reset(info); /* reset to read mode */
  638. }
  639. (*DANUBE_EBU_BUSCON0)|=0x80000000; // disable writing
  640. (*DANUBE_EBU_BUSCON1)|=0x80000000; // disable writing
  641. flash_reset(info); /* Homebox Black with JS28F128J3D75 had trouble reading after erase */
  642. printf (" done\n");
  643. return rcode;
  644. }
  645. /*-----------------------------------------------------------------------
  646. * Copy memory to flash, returns:
  647. * 0 - OK
  648. * 1 - write timeout
  649. * 2 - Flash not erased
  650. */
  651. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  652. {
  653. FPW data = 0; /* 16 or 32 bit word, matches flash bus width on MPC8XX */
  654. int bytes; /* number of bytes to program in current word */
  655. int left; /* number of bytes left to program */
  656. int i, res;
  657. for (left = cnt, res = 0;
  658. left > 0 && res == 0;
  659. addr += sizeof(data), left -= sizeof(data) - bytes) {
  660. bytes = addr & (sizeof(data) - 1);
  661. addr &= ~(sizeof(data) - 1);
  662. /* combine source and destination data so can program
  663. * an entire word of 16 or 32 bits
  664. */
  665. for (i = 0; i < sizeof(data); i++) {
  666. data <<= 8;
  667. if (i < bytes || i - bytes >= left )
  668. data += *((uchar *)addr + i);
  669. else
  670. data += *src++;
  671. }
  672. /* write one word to the flash */
  673. switch (info->flash_id & FLASH_VENDMASK) {
  674. case FLASH_MAN_AMD:
  675. case FLASH_MAN_MX: //joelin for MXIC
  676. res = write_word_amd(info, (FPWV *)addr, data);
  677. break;
  678. case FLASH_MAN_INTEL:
  679. res = write_word_intel(info, (FPWV *)addr, data);
  680. break;
  681. default:
  682. /* unknown flash type, error! */
  683. printf ("missing or unknown FLASH type\n");
  684. res = 1; /* not really a timeout, but gives error */
  685. break;
  686. }
  687. }
  688. return (res);
  689. }
  690. /*-----------------------------------------------------------------------
  691. * Write a word to Flash for AMD FLASH
  692. * A word is 16 or 32 bits, whichever the bus width of the flash bank
  693. * (not an individual chip) is.
  694. *
  695. * returns:
  696. * 0 - OK
  697. * 1 - write timeout
  698. * 2 - Flash not erased
  699. */
  700. static int write_word_amd (flash_info_t *info, FPWV *dest, FPW data)
  701. {
  702. ulong start;
  703. int flag;
  704. int res = 0; /* result, assume success */
  705. FPWV *base; /* first address in flash bank */
  706. /* Check if Flash is (sufficiently) erased */
  707. if ((*dest & data) != data) {
  708. return (2);
  709. }
  710. base = (FPWV *)(info->start[0]);
  711. /* Disable interrupts which might cause a timeout here */
  712. flag = disable_interrupts();
  713. (*DANUBE_EBU_BUSCON0)&=(~0x80000000); // enable writing
  714. (*DANUBE_EBU_BUSCON1)&=(~0x80000000); // enable writing
  715. (*EBU_NAND_CON)=0;
  716. base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
  717. base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
  718. base[FLASH_CYCLE1] = (FPW)0x00A000A0; /* selects program mode */
  719. *dest = data; /* start programming the data */
  720. /* re-enable interrupts if necessary */
  721. if (flag)
  722. enable_interrupts();
  723. start = get_timer (0);
  724. /* data polling for D7 */
  725. while (res == 0 && (*dest & (FPW)0x00800080) != (data & (FPW)0x00800080)) {
  726. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  727. *dest = (FPW)0x00F000F0; /* reset bank */
  728. res = 1;
  729. }
  730. }
  731. (*DANUBE_EBU_BUSCON0)|=0x80000000; // disable writing
  732. (*DANUBE_EBU_BUSCON1)|=0x80000000; // disable writing
  733. return (res);
  734. }
  735. /*-----------------------------------------------------------------------
  736. * Write a word to Flash for Intel FLASH
  737. * A word is 16 or 32 bits, whichever the bus width of the flash bank
  738. * (not an individual chip) is.
  739. *
  740. * returns:
  741. * 0 - OK
  742. * 1 - write timeout
  743. * 2 - Flash not erased
  744. */
  745. static int write_word_intel (flash_info_t *info, FPWV *dest, FPW data)
  746. {
  747. ulong start;
  748. int flag;
  749. int res = 0; /* result, assume success */
  750. /* Check if Flash is (sufficiently) erased */
  751. if ((*dest & data) != data) {
  752. return (2);
  753. }
  754. /* Disable interrupts which might cause a timeout here */
  755. flag = disable_interrupts();
  756. (*DANUBE_EBU_BUSCON0)&=(~0x80000000); // enable writing
  757. (*DANUBE_EBU_BUSCON1)&=(~0x80000000); // enable writing
  758. (*EBU_NAND_CON)=0;
  759. *dest = (FPW)0x00500050; /* clear status register */
  760. *dest = (FPW)0x00FF00FF; /* make sure in read mode */
  761. *dest = (FPW)0x00400040; /* program setup */
  762. *dest = data; /* start programming the data */
  763. asm("SYNC");
  764. /* re-enable interrupts if necessary */
  765. if (flag)
  766. enable_interrupts();
  767. start = get_timer (0);
  768. while (res == 0 && (*dest & (FPW)0x00800080) != (FPW)0x00800080) {
  769. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  770. *dest = (FPW)0x00B000B0; /* Suspend program */
  771. res = 1;
  772. }
  773. }
  774. if (res == 0 && (*dest & (FPW)0x00100010))
  775. res = 1; /* write failed, time out error is close enough */
  776. *dest = (FPW)0x00500050; /* clear status register */
  777. flash_reset(info);
  778. (*DANUBE_EBU_BUSCON0)|=0x80000000; // disable writing
  779. (*DANUBE_EBU_BUSCON1)|=0x80000000; // disable writing
  780. return (res);
  781. }