flash.c 26 KB

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