flash.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /*
  2. * (C) Copyright 2000, 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Modified 4/5/2001
  25. * Wait for completion of each sector erase command issued
  26. * 4/5/2001
  27. * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
  28. */
  29. /*
  30. * Modified 3/7/2001
  31. * - adopted for pip405, Denis Peter, MPL AG Switzerland
  32. * TODO:
  33. * clean-up
  34. */
  35. #include <common.h>
  36. #include <ppc4xx.h>
  37. #include <asm/processor.h>
  38. #ifdef CONFIG_PIP405
  39. #include "../pip405/pip405.h"
  40. #endif
  41. #ifdef CONFIG_MIP405
  42. #include "../mip405/mip405.h"
  43. #endif
  44. #include "common_util.h"
  45. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  46. /*-----------------------------------------------------------------------
  47. * Functions
  48. */
  49. static ulong flash_get_size (vu_long *addr, flash_info_t *info);
  50. static int write_word (flash_info_t *info, ulong dest, ulong data);
  51. static void flash_get_offsets (ulong base, flash_info_t *info);
  52. void unlock_intel_sectors(flash_info_t *info,ulong addr,ulong cnt);
  53. #ifdef CONFIG_ADCIOP
  54. #define ADDR0 0x0aa9
  55. #define ADDR1 0x0556
  56. #define FLASH_WORD_SIZE unsigned char
  57. #endif
  58. #ifdef CONFIG_CPCI405
  59. #define ADDR0 0x5555
  60. #define ADDR1 0x2aaa
  61. #define FLASH_WORD_SIZE unsigned short
  62. #endif
  63. #ifdef CONFIG_PIP405
  64. #define ADDR0 0x5555
  65. #define ADDR1 0x2aaa
  66. #define FLASH_WORD_SIZE unsigned short
  67. #endif
  68. #ifdef CONFIG_MIP405
  69. #define ADDR0 0x5555
  70. #define ADDR1 0x2aaa
  71. #define FLASH_WORD_SIZE unsigned short
  72. #endif
  73. #define FALSE 0
  74. #define TRUE 1
  75. /*-----------------------------------------------------------------------
  76. */
  77. unsigned long flash_init (void)
  78. {
  79. unsigned long size_b0, size_b1;
  80. int i;
  81. unsigned long pbcr;
  82. unsigned long base_b0, base_b1;
  83. unsigned char rc;
  84. rc=switch_cs(FALSE); /* map Flash High */
  85. if(rc)
  86. printf("(MPS Boot) ");
  87. else
  88. printf("(Flash Boot) ");
  89. /* Init: no FLASHes known */
  90. for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
  91. flash_info[i].flash_id = FLASH_UNKNOWN;
  92. }
  93. /* Static FLASH Bank configuration here - FIXME XXX */
  94. size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
  95. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  96. printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  97. size_b0, size_b0<<20);
  98. }
  99. /* Only one bank */
  100. if (CFG_MAX_FLASH_BANKS == 1)
  101. {
  102. /* Setup offsets */
  103. /* flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]); */
  104. /* Monitor protection ON by default */
  105. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  106. flash_protect(FLAG_PROTECT_SET,
  107. CFG_MONITOR_BASE,
  108. CFG_MONITOR_BASE+CFG_MONITOR_LEN-1,
  109. &flash_info[0]);
  110. #endif
  111. size_b1 = 0 ;
  112. flash_info[0].size = size_b0;
  113. }
  114. /* 2 banks */
  115. else
  116. {
  117. size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
  118. /* Re-do sizing to get full correct info */
  119. if (size_b1)
  120. {
  121. mtdcr(ebccfga, pb0cr);
  122. pbcr = mfdcr(ebccfgd);
  123. mtdcr(ebccfga, pb0cr);
  124. base_b1 = -size_b1;
  125. pbcr = (pbcr & 0x0001ffff) | base_b1 | (((size_b1/1024/1024)-1)<<17);
  126. mtdcr(ebccfgd, pbcr);
  127. /* printf("pb1cr = %x\n", pbcr); */
  128. }
  129. if (size_b0)
  130. {
  131. mtdcr(ebccfga, pb1cr);
  132. pbcr = mfdcr(ebccfgd);
  133. mtdcr(ebccfga, pb1cr);
  134. base_b0 = base_b1 - size_b0;
  135. pbcr = (pbcr & 0x0001ffff) | base_b0 | (((size_b0/1024/1024)-1)<<17);
  136. mtdcr(ebccfgd, pbcr);
  137. /* printf("pb0cr = %x\n", pbcr); */
  138. }
  139. size_b0 = flash_get_size((vu_long *)base_b0, &flash_info[0]);
  140. flash_get_offsets (base_b0, &flash_info[0]);
  141. /* monitor protection ON by default */
  142. (void)flash_protect(FLAG_PROTECT_SET,
  143. base_b0+size_b0-CFG_MONITOR_LEN,
  144. base_b0+size_b0-1,
  145. &flash_info[0]);
  146. if (size_b1) {
  147. /* Re-do sizing to get full correct info */
  148. size_b1 = flash_get_size((vu_long *)base_b1, &flash_info[1]);
  149. flash_get_offsets (base_b1, &flash_info[1]);
  150. /* monitor protection ON by default */
  151. (void)flash_protect(FLAG_PROTECT_SET,
  152. base_b1+size_b1-CFG_MONITOR_LEN,
  153. base_b1+size_b1-1,
  154. &flash_info[1]);
  155. /* monitor protection OFF by default (one is enough) */
  156. (void)flash_protect(FLAG_PROTECT_CLEAR,
  157. base_b0+size_b0-CFG_MONITOR_LEN,
  158. base_b0+size_b0-1,
  159. &flash_info[0]);
  160. } else {
  161. flash_info[1].flash_id = FLASH_UNKNOWN;
  162. flash_info[1].sector_count = -1;
  163. }
  164. flash_info[0].size = size_b0;
  165. flash_info[1].size = size_b1;
  166. }/* else 2 banks */
  167. switch_cs(rc); /* switch mode back */
  168. return (size_b0 + size_b1);
  169. }
  170. static void flash_get_offsets (ulong base, flash_info_t *info)
  171. {
  172. return;
  173. }
  174. #if 0
  175. /*-----------------------------------------------------------------------
  176. */
  177. static void flash_get_offsets (ulong base, flash_info_t *info)
  178. {
  179. int i;
  180. /* set up sector start address table */
  181. if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
  182. (info->flash_id == FLASH_AM040)){
  183. for (i = 0; i < info->sector_count; i++)
  184. info->start[i] = base + (i * 0x00010000);
  185. }
  186. else {
  187. if (info->flash_id & FLASH_BTYPE) {
  188. /* set sector offsets for bottom boot block type */
  189. info->start[0] = base + 0x00000000;
  190. info->start[1] = base + 0x00004000;
  191. info->start[2] = base + 0x00006000;
  192. info->start[3] = base + 0x00008000;
  193. for (i = 4; i < info->sector_count; i++) {
  194. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  195. }
  196. } else {
  197. /* set sector offsets for top boot block type */
  198. i = info->sector_count - 1;
  199. info->start[i--] = base + info->size - 0x00004000;
  200. info->start[i--] = base + info->size - 0x00006000;
  201. info->start[i--] = base + info->size - 0x00008000;
  202. for (; i >= 0; i--) {
  203. info->start[i] = base + i * 0x00010000;
  204. }
  205. }
  206. }
  207. }
  208. #endif
  209. /*-----------------------------------------------------------------------
  210. */
  211. void flash_print_info (flash_info_t *info)
  212. {
  213. int i;
  214. int k;
  215. int size;
  216. int erased;
  217. volatile unsigned long *flash;
  218. if (info->flash_id == FLASH_UNKNOWN) {
  219. printf ("missing or unknown FLASH type\n");
  220. return;
  221. }
  222. switch (info->flash_id & FLASH_VENDMASK) {
  223. case FLASH_MAN_AMD: printf ("AMD "); break;
  224. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  225. case FLASH_MAN_SST: printf ("SST "); break;
  226. case FLASH_MAN_INTEL: printf ("Intel "); break;
  227. default: printf ("Unknown Vendor "); break;
  228. }
  229. switch (info->flash_id & FLASH_TYPEMASK) {
  230. case FLASH_AM040: printf ("AM29F040 (512 Kbit, uniform sector size)\n");
  231. break;
  232. case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  233. break;
  234. case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  235. break;
  236. case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  237. break;
  238. case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  239. break;
  240. case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  241. break;
  242. case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  243. break;
  244. case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  245. break;
  246. case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  247. break;
  248. case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
  249. break;
  250. case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
  251. break;
  252. case FLASH_INTEL320T: printf ("TE28F320C3 (32 Mbit, top sector size)\n");
  253. break;
  254. default: printf ("Unknown Chip Type\n");
  255. break;
  256. }
  257. printf (" Size: %ld KB in %d Sectors\n",
  258. info->size >> 10, info->sector_count);
  259. printf (" Sector Start Addresses:");
  260. for (i=0; i<info->sector_count; ++i) {
  261. /*
  262. * Check if whole sector is erased
  263. */
  264. if (i != (info->sector_count-1))
  265. size = info->start[i+1] - info->start[i];
  266. else
  267. size = info->start[0] + info->size - info->start[i];
  268. erased = 1;
  269. flash = (volatile unsigned long *)info->start[i];
  270. size = size >> 2; /* divide by 4 for longword access */
  271. for (k=0; k<size; k++)
  272. {
  273. if (*flash++ != 0xffffffff)
  274. {
  275. erased = 0;
  276. break;
  277. }
  278. }
  279. if ((i % 5) == 0)
  280. printf ("\n ");
  281. #if 0 /* test-only */
  282. printf (" %08lX%s",
  283. info->start[i],
  284. info->protect[i] ? " (RO)" : " "
  285. #else
  286. printf (" %08lX%s%s",
  287. info->start[i],
  288. erased ? " E" : " ",
  289. info->protect[i] ? "RO " : " "
  290. #endif
  291. );
  292. }
  293. printf ("\n");
  294. }
  295. /*-----------------------------------------------------------------------
  296. */
  297. /*-----------------------------------------------------------------------
  298. */
  299. /*
  300. * The following code cannot be run from FLASH!
  301. */
  302. static ulong flash_get_size (vu_long *addr, flash_info_t *info)
  303. {
  304. short i;
  305. FLASH_WORD_SIZE value;
  306. ulong base = (ulong)addr;
  307. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
  308. /* Write auto select command: read Manufacturer ID */
  309. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  310. addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  311. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
  312. value = addr2[0];
  313. /* printf("flash_get_size value: %x\n",value); */
  314. switch (value) {
  315. case (FLASH_WORD_SIZE)AMD_MANUFACT:
  316. info->flash_id = FLASH_MAN_AMD;
  317. break;
  318. case (FLASH_WORD_SIZE)FUJ_MANUFACT:
  319. info->flash_id = FLASH_MAN_FUJ;
  320. break;
  321. case (FLASH_WORD_SIZE)INTEL_MANUFACT:
  322. info->flash_id = FLASH_MAN_INTEL;
  323. break;
  324. case (FLASH_WORD_SIZE)SST_MANUFACT:
  325. info->flash_id = FLASH_MAN_SST;
  326. break;
  327. default:
  328. info->flash_id = FLASH_UNKNOWN;
  329. info->sector_count = 0;
  330. info->size = 0;
  331. return (0); /* no or unknown flash */
  332. }
  333. value = addr2[1]; /* device ID */
  334. /* printf("Device value %x\n",value); */
  335. switch (value) {
  336. case (FLASH_WORD_SIZE)AMD_ID_F040B:
  337. info->flash_id += FLASH_AM040;
  338. info->sector_count = 8;
  339. info->size = 0x0080000; /* => 512 ko */
  340. break;
  341. case (FLASH_WORD_SIZE)AMD_ID_LV400T:
  342. info->flash_id += FLASH_AM400T;
  343. info->sector_count = 11;
  344. info->size = 0x00080000;
  345. break; /* => 0.5 MB */
  346. case (FLASH_WORD_SIZE)AMD_ID_LV400B:
  347. info->flash_id += FLASH_AM400B;
  348. info->sector_count = 11;
  349. info->size = 0x00080000;
  350. break; /* => 0.5 MB */
  351. case (FLASH_WORD_SIZE)AMD_ID_LV800T:
  352. info->flash_id += FLASH_AM800T;
  353. info->sector_count = 19;
  354. info->size = 0x00100000;
  355. break; /* => 1 MB */
  356. case (FLASH_WORD_SIZE)AMD_ID_LV800B:
  357. info->flash_id += FLASH_AM800B;
  358. info->sector_count = 19;
  359. info->size = 0x00100000;
  360. break; /* => 1 MB */
  361. case (FLASH_WORD_SIZE)AMD_ID_LV160T:
  362. info->flash_id += FLASH_AM160T;
  363. info->sector_count = 35;
  364. info->size = 0x00200000;
  365. break; /* => 2 MB */
  366. case (FLASH_WORD_SIZE)AMD_ID_LV160B:
  367. info->flash_id += FLASH_AM160B;
  368. info->sector_count = 35;
  369. info->size = 0x00200000;
  370. break; /* => 2 MB */
  371. #if 0 /* enable when device IDs are available */
  372. case (FLASH_WORD_SIZE)AMD_ID_LV320T:
  373. info->flash_id += FLASH_AM320T;
  374. info->sector_count = 67;
  375. info->size = 0x00400000;
  376. break; /* => 4 MB */
  377. case (FLASH_WORD_SIZE)AMD_ID_LV320B:
  378. info->flash_id += FLASH_AM320B;
  379. info->sector_count = 67;
  380. info->size = 0x00400000;
  381. break; /* => 4 MB */
  382. #endif
  383. case (FLASH_WORD_SIZE)SST_ID_xF800A:
  384. info->flash_id += FLASH_SST800A;
  385. info->sector_count = 16;
  386. info->size = 0x00100000;
  387. break; /* => 1 MB */
  388. case (FLASH_WORD_SIZE)INTEL_ID_28F320C3T:
  389. info->flash_id += FLASH_INTEL320T;
  390. info->sector_count = 71;
  391. info->size = 0x00400000;
  392. break; /* => 4 MB */
  393. case (FLASH_WORD_SIZE)SST_ID_xF160A:
  394. info->flash_id += FLASH_SST160A;
  395. info->sector_count = 32;
  396. info->size = 0x00200000;
  397. break; /* => 2 MB */
  398. default:
  399. info->flash_id = FLASH_UNKNOWN;
  400. return (0); /* => no or unknown flash */
  401. }
  402. /* set up sector start address table */
  403. if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
  404. (info->flash_id == FLASH_AM040)){
  405. for (i = 0; i < info->sector_count; i++)
  406. info->start[i] = base + (i * 0x00010000);
  407. } else {
  408. if (info->flash_id & FLASH_BTYPE) {
  409. /* set sector offsets for bottom boot block type */
  410. info->start[0] = base + 0x00000000;
  411. info->start[1] = base + 0x00004000;
  412. info->start[2] = base + 0x00006000;
  413. info->start[3] = base + 0x00008000;
  414. for (i = 4; i < info->sector_count; i++)
  415. info->start[i] = base + (i * 0x00010000) - 0x00030000;
  416. }
  417. else {
  418. /* set sector offsets for top boot block type */
  419. i = info->sector_count - 1;
  420. if(info->sector_count==71) {
  421. info->start[i--] = base + info->size - 0x00002000;
  422. info->start[i--] = base + info->size - 0x00004000;
  423. info->start[i--] = base + info->size - 0x00006000;
  424. info->start[i--] = base + info->size - 0x00008000;
  425. info->start[i--] = base + info->size - 0x0000A000;
  426. info->start[i--] = base + info->size - 0x0000C000;
  427. info->start[i--] = base + info->size - 0x0000E000;
  428. for (; i >= 0; i--)
  429. info->start[i] = base + i * 0x000010000;
  430. }
  431. else {
  432. info->start[i--] = base + info->size - 0x00004000;
  433. info->start[i--] = base + info->size - 0x00006000;
  434. info->start[i--] = base + info->size - 0x00008000;
  435. for (; i >= 0; i--)
  436. info->start[i] = base + i * 0x00010000;
  437. }
  438. }
  439. }
  440. /* check for protected sectors */
  441. for (i = 0; i < info->sector_count; i++) {
  442. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  443. /* D0 = 1 if protected */
  444. addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
  445. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
  446. info->protect[i] = 0;
  447. else
  448. info->protect[i] = addr2[2] & 1;
  449. }
  450. /*
  451. * Prevent writes to uninitialized FLASH.
  452. */
  453. if (info->flash_id != FLASH_UNKNOWN) {
  454. #if 0 /* test-only */
  455. #ifdef CONFIG_ADCIOP
  456. addr2 = (volatile unsigned char *)info->start[0];
  457. addr2[ADDR0] = 0xAA;
  458. addr2[ADDR1] = 0x55;
  459. addr2[ADDR0] = 0xF0; /* reset bank */
  460. #else
  461. addr2 = (FLASH_WORD_SIZE *)info->start[0];
  462. *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  463. #endif
  464. #else /* test-only */
  465. addr2 = (FLASH_WORD_SIZE *)info->start[0];
  466. *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  467. #endif /* test-only */
  468. }
  469. return (info->size);
  470. }
  471. int wait_for_DQ7(flash_info_t *info, int sect)
  472. {
  473. ulong start, now, last;
  474. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
  475. start = get_timer (0);
  476. last = start;
  477. while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
  478. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  479. printf ("Timeout\n");
  480. return -1;
  481. }
  482. /* show that we're waiting */
  483. if ((now - last) > 1000) { /* every second */
  484. putc ('.');
  485. last = now;
  486. }
  487. }
  488. return 0;
  489. }
  490. int intel_wait_for_DQ7(flash_info_t *info, int sect)
  491. {
  492. ulong start, now, last;
  493. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
  494. start = get_timer (0);
  495. last = start;
  496. while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
  497. if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
  498. printf ("Timeout\n");
  499. return -1;
  500. }
  501. /* show that we're waiting */
  502. if ((now - last) > 1000) { /* every second */
  503. putc ('.');
  504. last = now;
  505. }
  506. }
  507. addr[0]=(FLASH_WORD_SIZE)0x00500050;
  508. return 0;
  509. }
  510. /*-----------------------------------------------------------------------
  511. */
  512. int flash_erase (flash_info_t *info, int s_first, int s_last)
  513. {
  514. volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
  515. volatile FLASH_WORD_SIZE *addr2;
  516. int flag, prot, sect, l_sect;
  517. int i;
  518. if ((s_first < 0) || (s_first > s_last)) {
  519. if (info->flash_id == FLASH_UNKNOWN) {
  520. printf ("- missing\n");
  521. } else {
  522. printf ("- no sectors to erase\n");
  523. }
  524. return 1;
  525. }
  526. if (info->flash_id == FLASH_UNKNOWN) {
  527. printf ("Can't erase unknown flash type - aborted\n");
  528. return 1;
  529. }
  530. prot = 0;
  531. for (sect=s_first; sect<=s_last; ++sect) {
  532. if (info->protect[sect]) {
  533. prot++;
  534. }
  535. }
  536. if (prot) {
  537. printf ("- Warning: %d protected sectors will not be erased!\n",
  538. prot);
  539. } else {
  540. printf ("\n");
  541. }
  542. l_sect = -1;
  543. /* Disable interrupts which might cause a timeout here */
  544. flag = disable_interrupts();
  545. /* Start erase on unprotected sectors */
  546. for (sect = s_first; sect<=s_last; sect++) {
  547. if (info->protect[sect] == 0) { /* not protected */
  548. addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
  549. /* printf("Erasing sector %p\n", addr2); */ /* CLH */
  550. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  551. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  552. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  553. addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
  554. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  555. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  556. addr2[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
  557. for (i=0; i<50; i++)
  558. udelay(1000); /* wait 1 ms */
  559. wait_for_DQ7(info, sect);
  560. }
  561. else {
  562. if((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL){
  563. addr2[0] = (FLASH_WORD_SIZE)0x00600060; /* unlock sector */
  564. addr2[0] = (FLASH_WORD_SIZE)0x00D000D0; /* sector erase */
  565. intel_wait_for_DQ7(info, sect);
  566. addr2[0] = (FLASH_WORD_SIZE)0x00200020; /* sector erase */
  567. addr2[0] = (FLASH_WORD_SIZE)0x00D000D0; /* sector erase */
  568. intel_wait_for_DQ7(info, sect);
  569. }
  570. else {
  571. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  572. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  573. addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
  574. addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  575. addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  576. addr2[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
  577. wait_for_DQ7(info, sect);
  578. }
  579. }
  580. l_sect = sect;
  581. /*
  582. * Wait for each sector to complete, it's more
  583. * reliable. According to AMD Spec, you must
  584. * issue all erase commands within a specified
  585. * timeout. This has been seen to fail, especially
  586. * if printf()s are included (for debug)!!
  587. */
  588. /* wait_for_DQ7(info, sect); */
  589. }
  590. }
  591. /* re-enable interrupts if necessary */
  592. if (flag)
  593. enable_interrupts();
  594. /* wait at least 80us - let's wait 1 ms */
  595. udelay (1000);
  596. #if 0
  597. /*
  598. * We wait for the last triggered sector
  599. */
  600. if (l_sect < 0)
  601. goto DONE;
  602. wait_for_DQ7(info, l_sect);
  603. DONE:
  604. #endif
  605. /* reset to read mode */
  606. addr = (FLASH_WORD_SIZE *)info->start[0];
  607. addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
  608. printf (" done\n");
  609. return 0;
  610. }
  611. void unlock_intel_sectors(flash_info_t *info,ulong addr,ulong cnt)
  612. {
  613. int i;
  614. volatile FLASH_WORD_SIZE *addr2;
  615. long c;
  616. c= (long)cnt;
  617. for(i=info->sector_count-1;i>0;i--)
  618. {
  619. if(addr>=info->start[i])
  620. break;
  621. }
  622. do {
  623. addr2 = (FLASH_WORD_SIZE *)(info->start[i]);
  624. addr2[0] = (FLASH_WORD_SIZE)0x00600060; /* unlock sector setup */
  625. addr2[0] = (FLASH_WORD_SIZE)0x00D000D0; /* unlock sector */
  626. intel_wait_for_DQ7(info, i);
  627. i++;
  628. c-=(info->start[i]-info->start[i-1]);
  629. }while(c>0);
  630. }
  631. /*-----------------------------------------------------------------------
  632. * Copy memory to flash, returns:
  633. * 0 - OK
  634. * 1 - write timeout
  635. * 2 - Flash not erased
  636. */
  637. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  638. {
  639. ulong cp, wp, data;
  640. int i, l, rc;
  641. if((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL){
  642. unlock_intel_sectors(info,addr,cnt);
  643. }
  644. wp = (addr & ~3); /* get lower word aligned address */
  645. /*
  646. * handle unaligned start bytes
  647. */
  648. if ((l = addr - wp) != 0) {
  649. data = 0;
  650. for (i=0, cp=wp; i<l; ++i, ++cp) {
  651. data = (data << 8) | (*(uchar *)cp);
  652. }
  653. for (; i<4 && cnt>0; ++i) {
  654. data = (data << 8) | *src++;
  655. --cnt;
  656. ++cp;
  657. }
  658. for (; cnt==0 && i<4; ++i, ++cp) {
  659. data = (data << 8) | (*(uchar *)cp);
  660. }
  661. if ((rc = write_word(info, wp, data)) != 0) {
  662. return (rc);
  663. }
  664. wp += 4;
  665. }
  666. /*
  667. * handle word aligned part
  668. */
  669. while (cnt >= 4) {
  670. data = 0;
  671. for (i=0; i<4; ++i) {
  672. data = (data << 8) | *src++;
  673. }
  674. if ((rc = write_word(info, wp, data)) != 0) {
  675. return (rc);
  676. }
  677. wp += 4;
  678. if((wp % 0x10000)==0)
  679. printf("."); /* show Progress */
  680. cnt -= 4;
  681. }
  682. if (cnt == 0) {
  683. return (0);
  684. }
  685. /*
  686. * handle unaligned tail bytes
  687. */
  688. data = 0;
  689. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  690. data = (data << 8) | *src++;
  691. --cnt;
  692. }
  693. for (; i<4; ++i, ++cp) {
  694. data = (data << 8) | (*(uchar *)cp);
  695. }
  696. rc=write_word(info, wp, data);
  697. return rc;
  698. }
  699. /*-----------------------------------------------------------------------
  700. * Write a word to Flash, returns:
  701. * 0 - OK
  702. * 1 - write timeout
  703. * 2 - Flash not erased
  704. */
  705. static FLASH_WORD_SIZE *read_val = (FLASH_WORD_SIZE *)0x200000;
  706. static int write_word (flash_info_t *info, ulong dest, ulong data)
  707. {
  708. volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)(info->start[0]);
  709. volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *)dest;
  710. volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
  711. ulong start;
  712. int flag;
  713. int i;
  714. /* Check if Flash is (sufficiently) erased */
  715. if ((*((volatile FLASH_WORD_SIZE *)dest) &
  716. (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
  717. return (2);
  718. }
  719. /* Disable interrupts which might cause a timeout here */
  720. flag = disable_interrupts();
  721. for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
  722. {
  723. if((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL){
  724. /* intel style writting */
  725. dest2[i] = (FLASH_WORD_SIZE)0x00500050;
  726. dest2[i] = (FLASH_WORD_SIZE)0x00400040;
  727. *read_val++ = data2[i];
  728. dest2[i] = data2[i];
  729. if (flag)
  730. enable_interrupts();
  731. /* data polling for D7 */
  732. start = get_timer (0);
  733. udelay(10);
  734. while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080)
  735. {
  736. if (get_timer(start) > CFG_FLASH_WRITE_TOUT)
  737. return (1);
  738. }
  739. dest2[i] = (FLASH_WORD_SIZE)0x00FF00FF; /* return to read mode */
  740. udelay(10);
  741. dest2[i] = (FLASH_WORD_SIZE)0x00FF00FF; /* return to read mode */
  742. if(dest2[i]!=data2[i])
  743. printf("Error at %p 0x%04X != 0x%04X\n",&dest2[i],dest2[i],data2[i]);
  744. }
  745. else {
  746. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
  747. addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
  748. addr2[ADDR0] = (FLASH_WORD_SIZE)0x00A000A0;
  749. dest2[i] = data2[i];
  750. /* re-enable interrupts if necessary */
  751. if (flag)
  752. enable_interrupts();
  753. /* data polling for D7 */
  754. start = get_timer (0);
  755. while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) !=
  756. (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
  757. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  758. return (1);
  759. }
  760. }
  761. }
  762. }
  763. return (0);
  764. }
  765. /*-----------------------------------------------------------------------
  766. */