flash.c 25 KB

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