flash.c 23 KB

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