flash.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. * (C) Copyright 2001
  3. * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
  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. * flash.c - flash support for the 512k, 8bit boot flash on the GEVB
  25. * most of this file was based on the existing U-Boot
  26. * flash drivers.
  27. */
  28. #include <common.h>
  29. #include <mpc8xx.h>
  30. #include <galileo/gt64260R.h>
  31. #include <galileo/memory.h>
  32. #include "intel_flash.h"
  33. #define FLASH_ROM 0xFFFD /* unknown flash type */
  34. #define FLASH_RAM 0xFFFE /* unknown flash type */
  35. #define FLASH_MAN_UNKNOWN 0xFFFF0000
  36. /* #define DEBUG */
  37. /* #define FLASH_ID_OVERRIDE */ /* Hack to set type to 040B if ROM emulator is installed.
  38. * Can be used to program a ROM in circuit if a programmer
  39. * is not available by swapping the rom out. */
  40. /* Intel flash commands */
  41. int flash_erase_intel(flash_info_t *info, int s_first, int s_last);
  42. int write_word_intel(bank_addr_t addr, bank_word_t value);
  43. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  44. /*-----------------------------------------------------------------------
  45. * Functions
  46. */
  47. static ulong flash_get_size (int portwidth, vu_long *addr, flash_info_t *info);
  48. static int write_word (flash_info_t *info, ulong dest, ulong data);
  49. static void flash_get_offsets (ulong base, flash_info_t *info);
  50. /*-----------------------------------------------------------------------
  51. */
  52. unsigned long
  53. flash_init (void)
  54. {
  55. unsigned int i;
  56. unsigned long size_b0 = 0, size_b1 = 0;
  57. unsigned long base, flash_size;
  58. /* Init: no FLASHes known */
  59. for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  60. flash_info[i].flash_id = FLASH_UNKNOWN;
  61. }
  62. /* the boot flash */
  63. base = CONFIG_SYS_FLASH_BASE;
  64. #ifndef CONFIG_SYS_BOOT_FLASH_WIDTH
  65. #define CONFIG_SYS_BOOT_FLASH_WIDTH 1
  66. #endif
  67. size_b0 = flash_get_size(CONFIG_SYS_BOOT_FLASH_WIDTH, (vu_long *)base,
  68. &flash_info[0]);
  69. #ifndef CONFIG_P3G4
  70. printf("[");
  71. print_size (size_b0, "");
  72. printf("@%08lX] ", base);
  73. #endif
  74. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  75. printf ("## Unknown FLASH at %08lx: Size = 0x%08lx = %ld MB\n",
  76. base, size_b0, size_b0<<20);
  77. }
  78. base = memoryGetDeviceBaseAddress(CONFIG_SYS_EXTRA_FLASH_DEVICE);
  79. for(i=1;i<CONFIG_SYS_MAX_FLASH_BANKS;i++) {
  80. unsigned long size = flash_get_size(CONFIG_SYS_EXTRA_FLASH_WIDTH, (vu_long *)base, &flash_info[i]);
  81. #ifndef CONFIG_P3G4
  82. printf("[");
  83. print_size (size, "");
  84. printf("@%08lX] ", base);
  85. #endif
  86. if (flash_info[i].flash_id == FLASH_UNKNOWN) {
  87. if(i==1) {
  88. printf ("## Unknown FLASH at %08lx: Size = 0x%08lx = %ld MB\n",
  89. base, size_b1, size_b1<<20);
  90. }
  91. break;
  92. }
  93. size_b1+=size;
  94. base+=size;
  95. }
  96. #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
  97. /* monitor protection ON by default */
  98. flash_protect(FLAG_PROTECT_SET,
  99. CONFIG_SYS_MONITOR_BASE,
  100. CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
  101. flash_get_info(CONFIG_SYS_MONITOR_BASE));
  102. #endif
  103. #ifdef CONFIG_ENV_IS_IN_FLASH
  104. /* ENV protection ON by default */
  105. flash_protect(FLAG_PROTECT_SET,
  106. CONFIG_ENV_ADDR,
  107. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
  108. flash_get_info(CONFIG_ENV_ADDR));
  109. #endif
  110. flash_size = size_b0 + size_b1;
  111. return flash_size;
  112. }
  113. /*-----------------------------------------------------------------------
  114. */
  115. static void
  116. flash_get_offsets (ulong base, flash_info_t *info)
  117. {
  118. int i;
  119. int sector_size;
  120. if(!info->sector_count) return;
  121. /* set up sector start address table */
  122. switch(info->flash_id & FLASH_TYPEMASK) {
  123. case FLASH_AM040:
  124. case FLASH_28F128J3A:
  125. case FLASH_28F640J3A:
  126. case FLASH_RAM:
  127. /* this chip has uniformly spaced sectors */
  128. sector_size=info->size/info->sector_count;
  129. for (i = 0; i < info->sector_count; i++)
  130. info->start[i] = base + (i * sector_size);
  131. break;
  132. default:
  133. if (info->flash_id & FLASH_BTYPE) {
  134. /* set sector offsets for bottom boot block type */
  135. info->start[0] = base + 0x00000000;
  136. info->start[1] = base + 0x00008000;
  137. info->start[2] = base + 0x0000C000;
  138. info->start[3] = base + 0x00010000;
  139. for (i = 4; i < info->sector_count; i++) {
  140. info->start[i] = base + (i * 0x00020000) - 0x00060000;
  141. }
  142. } else {
  143. /* set sector offsets for top boot block type */
  144. i = info->sector_count - 1;
  145. info->start[i--] = base + info->size - 0x00008000;
  146. info->start[i--] = base + info->size - 0x0000C000;
  147. info->start[i--] = base + info->size - 0x00010000;
  148. for (; i >= 0; i--) {
  149. info->start[i] = base + i * 0x00020000;
  150. }
  151. }
  152. }
  153. }
  154. /*-----------------------------------------------------------------------
  155. */
  156. flash_info_t *flash_get_info(ulong base)
  157. {
  158. int i;
  159. flash_info_t * info;
  160. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i ++) {
  161. info = & flash_info[i];
  162. if (info->start[0] <= base && base <= info->start[0] + info->size - 1)
  163. break;
  164. }
  165. return i == CONFIG_SYS_MAX_FLASH_BANKS ? 0 : info;
  166. }
  167. /*-----------------------------------------------------------------------
  168. */
  169. void
  170. flash_print_info (flash_info_t *info)
  171. {
  172. int i;
  173. if (info->flash_id == FLASH_UNKNOWN) {
  174. printf ("missing or unknown FLASH type\n");
  175. return;
  176. }
  177. switch (info->flash_id & FLASH_VENDMASK) {
  178. case FLASH_MAN_AMD: printf ("AMD "); break;
  179. case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
  180. case FLASH_MAN_INTEL: printf ("INTEL "); break;
  181. default: printf ("Unknown Vendor "); break;
  182. }
  183. switch (info->flash_id & FLASH_TYPEMASK) {
  184. case FLASH_AM040:
  185. printf ("AM29LV040B (4 Mbit, bottom boot sect)\n");
  186. break;
  187. case FLASH_AM400B:
  188. printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
  189. break;
  190. case FLASH_AM400T:
  191. printf ("AM29LV400T (4 Mbit, top boot sector)\n");
  192. break;
  193. case FLASH_AM800B:
  194. printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
  195. break;
  196. case FLASH_AM800T:
  197. printf ("AM29LV800T (8 Mbit, top boot sector)\n");
  198. break;
  199. case FLASH_AM160B:
  200. printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
  201. break;
  202. case FLASH_AM160T:
  203. printf ("AM29LV160T (16 Mbit, top boot sector)\n");
  204. break;
  205. case FLASH_AM320B:
  206. printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
  207. break;
  208. case FLASH_AM320T:
  209. printf ("AM29LV320T (32 Mbit, top boot sector)\n");
  210. break;
  211. case FLASH_28F640J3A:
  212. printf ("28F640J3A (64 Mbit)\n");
  213. break;
  214. case FLASH_28F128J3A:
  215. printf ("28F128J3A (128 Mbit)\n");
  216. break;
  217. case FLASH_ROM:
  218. printf ("ROM\n");
  219. break;
  220. case FLASH_RAM:
  221. printf ("RAM\n");
  222. break;
  223. default:
  224. printf ("Unknown Chip Type\n");
  225. break;
  226. }
  227. puts (" Size: ");
  228. print_size (info->size, "");
  229. printf (" in %d Sectors\n", info->sector_count);
  230. printf (" Sector Start Addresses:");
  231. for (i=0; i<info->sector_count; ++i) {
  232. if ((i % 5) == 0)
  233. printf ("\n ");
  234. printf (" %08lX%s",
  235. info->start[i],
  236. info->protect[i] ? " (RO)" : " "
  237. );
  238. }
  239. printf ("\n");
  240. return;
  241. }
  242. /*-----------------------------------------------------------------------
  243. */
  244. /*-----------------------------------------------------------------------
  245. */
  246. /*
  247. * The following code cannot be run from FLASH!
  248. */
  249. static inline void flash_cmd(int width, volatile unsigned char *addr, int offset, unsigned char cmd)
  250. {
  251. /* supports 1x8, 1x16, and 2x16 */
  252. /* 2x8 and 4x8 are not supported */
  253. if(width==4) {
  254. /* assuming chips are in 16 bit mode */
  255. /* 2x16 */
  256. unsigned long cmd32=(cmd<<16)|cmd;
  257. *(volatile unsigned long *)(addr+offset*2)=cmd32;
  258. } else if (width == 2) {
  259. /* 1x16 */
  260. *(volatile unsigned short *)((unsigned short*)addr+offset)=cmd;
  261. } else {
  262. /* 1x8 */
  263. *(volatile unsigned char *)(addr+offset)=cmd;
  264. }
  265. }
  266. static ulong
  267. flash_get_size (int portwidth, vu_long *addr, flash_info_t *info)
  268. {
  269. short i;
  270. volatile unsigned char *caddr = (unsigned char *)addr;
  271. volatile unsigned short *saddr = (unsigned short *)addr;
  272. volatile unsigned long *laddr = (unsigned long *)addr;
  273. char old[2], save;
  274. ulong id, manu, base = (ulong)addr;
  275. info->portwidth=portwidth;
  276. save = *caddr;
  277. flash_cmd(portwidth,caddr,0,0xf0);
  278. flash_cmd(portwidth,caddr,0,0xf0);
  279. udelay(10);
  280. old[0] = caddr[0];
  281. old[1] = caddr[1];
  282. if(old[0]!=0xf0) {
  283. flash_cmd(portwidth,caddr,0,0xf0);
  284. flash_cmd(portwidth,caddr,0,0xf0);
  285. udelay(10);
  286. if(*caddr==0xf0) {
  287. /* this area is ROM */
  288. *caddr=save;
  289. #ifndef FLASH_ID_OVERRIDE
  290. info->flash_id = FLASH_ROM + FLASH_MAN_UNKNOWN;
  291. info->sector_count = 8;
  292. info->size = 0x80000;
  293. #else
  294. info->flash_id = FLASH_MAN_AMD + FLASH_AM040;
  295. info->sector_count = 8;
  296. info->size = 0x80000;
  297. info->chipwidth=1;
  298. #endif
  299. flash_get_offsets(base, info);
  300. return info->size;
  301. }
  302. } else {
  303. *caddr=0;
  304. udelay(10);
  305. if(*caddr==0) {
  306. /* this area is RAM */
  307. *caddr=save;
  308. info->flash_id = FLASH_RAM + FLASH_MAN_UNKNOWN;
  309. info->sector_count = 8;
  310. info->size = 0x80000;
  311. flash_get_offsets(base, info);
  312. return info->size;
  313. }
  314. flash_cmd(portwidth,caddr,0,0xf0);
  315. udelay(10);
  316. }
  317. /* Write auto select command: read Manufacturer ID */
  318. flash_cmd(portwidth,caddr,0x555,0xAA);
  319. flash_cmd(portwidth,caddr,0x2AA,0x55);
  320. flash_cmd(portwidth,caddr,0x555,0x90);
  321. udelay(10);
  322. if ((caddr[0] == old[0]) &&
  323. (caddr[1] == old[1])) {
  324. /* this area is ROM */
  325. #ifndef FLASH_ID_OVERRIDE
  326. info->flash_id = FLASH_ROM + FLASH_MAN_UNKNOWN;
  327. info->sector_count = 8;
  328. info->size = 0x80000;
  329. #else
  330. info->flash_id = FLASH_MAN_AMD + FLASH_AM040;
  331. info->sector_count = 8;
  332. info->size = 0x80000;
  333. info->chipwidth=1;
  334. #endif
  335. flash_get_offsets(base, info);
  336. return info->size;
  337. #ifdef DEBUG
  338. } else {
  339. printf("%px%d: %02x:%02x -> %02x:%02x\n",
  340. caddr, portwidth, old[0], old[1],
  341. caddr[0], caddr[1]);
  342. #endif
  343. }
  344. switch(portwidth) {
  345. case 1:
  346. manu = caddr[0];
  347. manu |= manu<<16;
  348. id = caddr[1];
  349. break;
  350. case 2:
  351. manu = saddr[0];
  352. manu |= manu<<16;
  353. id = saddr[1];
  354. id |= id<<16;
  355. break;
  356. case 4:
  357. manu = laddr[0];
  358. id = laddr[1];
  359. break;
  360. default:
  361. id = manu = -1;
  362. break;
  363. }
  364. #ifdef DEBUG
  365. printf("\n%08lx:%08lx:%08lx\n", base, manu, id);
  366. printf("%08lx %08lx %08lx %08lx\n",
  367. laddr[0],laddr[1],laddr[2],laddr[3]);
  368. #endif
  369. switch (manu) {
  370. case AMD_MANUFACT:
  371. info->flash_id = FLASH_MAN_AMD;
  372. break;
  373. case FUJ_MANUFACT:
  374. info->flash_id = FLASH_MAN_FUJ;
  375. break;
  376. case INTEL_MANUFACT:
  377. info->flash_id = FLASH_MAN_INTEL;
  378. break;
  379. default:
  380. printf("Unknown Mfr [%08lx]:%08lx\n", manu, id);
  381. info->flash_id = FLASH_UNKNOWN;
  382. info->sector_count = 0;
  383. info->size = 0;
  384. return (0); /* no or unknown flash */
  385. }
  386. switch (id) {
  387. case AMD_ID_LV400T:
  388. info->flash_id += FLASH_AM400T;
  389. info->sector_count = 11;
  390. info->size = 0x00100000;
  391. info->chipwidth=1;
  392. break; /* => 1 MB */
  393. case AMD_ID_LV400B:
  394. info->flash_id += FLASH_AM400B;
  395. info->sector_count = 11;
  396. info->size = 0x00100000;
  397. info->chipwidth=1;
  398. break; /* => 1 MB */
  399. case AMD_ID_LV800T:
  400. info->flash_id += FLASH_AM800T;
  401. info->sector_count = 19;
  402. info->size = 0x00200000;
  403. info->chipwidth=1;
  404. break; /* => 2 MB */
  405. case AMD_ID_LV800B:
  406. info->flash_id += FLASH_AM800B;
  407. info->sector_count = 19;
  408. info->size = 0x00200000;
  409. info->chipwidth=1;
  410. break; /* => 2 MB */
  411. case AMD_ID_LV160T:
  412. info->flash_id += FLASH_AM160T;
  413. info->sector_count = 35;
  414. info->size = 0x00400000;
  415. info->chipwidth=1;
  416. break; /* => 4 MB */
  417. case AMD_ID_LV160B:
  418. info->flash_id += FLASH_AM160B;
  419. info->sector_count = 35;
  420. info->size = 0x00400000;
  421. info->chipwidth=1;
  422. break; /* => 4 MB */
  423. #if 0 /* enable when device IDs are available */
  424. case AMD_ID_LV320T:
  425. info->flash_id += FLASH_AM320T;
  426. info->sector_count = 67;
  427. info->size = 0x00800000;
  428. break; /* => 8 MB */
  429. case AMD_ID_LV320B:
  430. info->flash_id += FLASH_AM320B;
  431. info->sector_count = 67;
  432. info->size = 0x00800000;
  433. break; /* => 8 MB */
  434. #endif
  435. case AMD_ID_LV040B:
  436. info->flash_id += FLASH_AM040;
  437. info->sector_count = 8;
  438. info->size = 0x80000;
  439. info->chipwidth=1;
  440. break;
  441. case INTEL_ID_28F640J3A:
  442. info->flash_id += FLASH_28F640J3A;
  443. info->sector_count = 64;
  444. info->size = 128*1024 * 64; /* 128kbytes x 64 blocks */
  445. info->chipwidth=2;
  446. if(portwidth==4) info->size*=2; /* 2x16 */
  447. break;
  448. case INTEL_ID_28F128J3A:
  449. info->flash_id += FLASH_28F128J3A;
  450. info->sector_count = 128;
  451. info->size = 128*1024 * 128; /* 128kbytes x 128 blocks */
  452. info->chipwidth=2;
  453. if(portwidth==4) info->size*=2; /* 2x16 */
  454. break;
  455. default:
  456. printf("Unknown id %lx:[%lx]\n", manu, id);
  457. info->flash_id = FLASH_UNKNOWN;
  458. info->chipwidth=1;
  459. return (0); /* => no or unknown flash */
  460. }
  461. flash_get_offsets(base, info);
  462. #if 0
  463. /* set up sector start address table */
  464. if (info->flash_id & FLASH_AM040) {
  465. /* this chip has uniformly spaced sectors */
  466. for (i = 0; i < info->sector_count; i++)
  467. info->start[i] = base + (i * 0x00010000);
  468. } else if (info->flash_id & FLASH_BTYPE) {
  469. /* set sector offsets for bottom boot block type */
  470. info->start[0] = base + 0x00000000;
  471. info->start[1] = base + 0x00008000;
  472. info->start[2] = base + 0x0000C000;
  473. info->start[3] = base + 0x00010000;
  474. for (i = 4; i < info->sector_count; i++) {
  475. info->start[i] = base + (i * 0x00020000) - 0x00060000;
  476. }
  477. } else {
  478. /* set sector offsets for top boot block type */
  479. i = info->sector_count - 1;
  480. info->start[i--] = base + info->size - 0x00008000;
  481. info->start[i--] = base + info->size - 0x0000C000;
  482. info->start[i--] = base + info->size - 0x00010000;
  483. for (; i >= 0; i--) {
  484. info->start[i] = base + i * 0x00020000;
  485. }
  486. }
  487. #endif
  488. /* check for protected sectors */
  489. for (i = 0; i < info->sector_count; i++) {
  490. /* read sector protection at sector address, (A7 .. A0)=0x02 */
  491. /* D0 = 1 if protected */
  492. caddr = (volatile unsigned char *)(info->start[i]);
  493. saddr = (volatile unsigned short *)(info->start[i]);
  494. laddr = (volatile unsigned long *)(info->start[i]);
  495. if(portwidth==1)
  496. info->protect[i] = caddr[2] & 1;
  497. else if(portwidth==2)
  498. info->protect[i] = saddr[2] & 1;
  499. else
  500. info->protect[i] = laddr[2] & 1;
  501. }
  502. /*
  503. * Prevent writes to uninitialized FLASH.
  504. */
  505. if (info->flash_id != FLASH_UNKNOWN) {
  506. caddr = (volatile unsigned char *)info->start[0];
  507. flash_cmd(portwidth,caddr,0,0xF0); /* reset bank */
  508. }
  509. return (info->size);
  510. }
  511. /* TODO: 2x16 unsupported */
  512. int
  513. flash_erase (flash_info_t *info, int s_first, int s_last)
  514. {
  515. volatile unsigned char *addr = (uchar *)(info->start[0]);
  516. int flag, prot, sect, l_sect;
  517. ulong start, now, last;
  518. /* TODO: 2x16 unsupported */
  519. if(info->portwidth==4) return 1;
  520. if((info->flash_id & FLASH_TYPEMASK) == FLASH_ROM) return 1;
  521. if((info->flash_id & FLASH_TYPEMASK) == FLASH_RAM) {
  522. for (sect = s_first; sect<=s_last; sect++) {
  523. int sector_size=info->size/info->sector_count;
  524. addr = (uchar *)(info->start[sect]);
  525. memset((void *)addr, 0, sector_size);
  526. }
  527. return 0;
  528. }
  529. if ((s_first < 0) || (s_first > s_last)) {
  530. if (info->flash_id == FLASH_UNKNOWN) {
  531. printf ("- missing\n");
  532. } else {
  533. printf ("- no sectors to erase\n");
  534. }
  535. return 1;
  536. }
  537. if ((info->flash_id&FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  538. return flash_erase_intel(info,
  539. (unsigned short)s_first,
  540. (unsigned short)s_last);
  541. }
  542. #if 0
  543. if ((info->flash_id == FLASH_UNKNOWN) ||
  544. (info->flash_id > FLASH_AMD_COMP)) {
  545. printf ("Can't erase unknown flash type %08lx - aborted\n",
  546. info->flash_id);
  547. return 1;
  548. }
  549. #endif
  550. prot = 0;
  551. for (sect=s_first; sect<=s_last; ++sect) {
  552. if (info->protect[sect]) {
  553. prot++;
  554. }
  555. }
  556. if (prot) {
  557. printf ("- Warning: %d protected sectors will not be erased!\n",
  558. prot);
  559. } else {
  560. printf ("\n");
  561. }
  562. l_sect = -1;
  563. /* Disable interrupts which might cause a timeout here */
  564. flag = disable_interrupts();
  565. flash_cmd(info->portwidth,addr,0x555,0xAA);
  566. flash_cmd(info->portwidth,addr,0x2AA,0x55);
  567. flash_cmd(info->portwidth,addr,0x555,0x80);
  568. flash_cmd(info->portwidth,addr,0x555,0xAA);
  569. flash_cmd(info->portwidth,addr,0x2AA,0x55);
  570. /* Start erase on unprotected sectors */
  571. for (sect = s_first; sect<=s_last; sect++) {
  572. if (info->protect[sect] == 0) { /* not protected */
  573. addr = (uchar *)(info->start[sect]);
  574. flash_cmd(info->portwidth,addr,0,0x30);
  575. l_sect = sect;
  576. }
  577. }
  578. /* re-enable interrupts if necessary */
  579. if (flag)
  580. enable_interrupts();
  581. /* wait at least 80us - let's wait 1 ms */
  582. udelay (1000);
  583. /*
  584. * We wait for the last triggered sector
  585. */
  586. if (l_sect < 0)
  587. goto DONE;
  588. start = get_timer (0);
  589. last = start;
  590. addr = (volatile unsigned char *)(info->start[l_sect]);
  591. /* broken for 2x16: TODO */
  592. while ((addr[0] & 0x80) != 0x80) {
  593. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  594. printf ("Timeout\n");
  595. return 1;
  596. }
  597. /* show that we're waiting */
  598. if ((now - last) > 1000) { /* every second */
  599. putc ('.');
  600. last = now;
  601. }
  602. }
  603. DONE:
  604. /* reset to read mode */
  605. addr = (volatile unsigned char *)info->start[0];
  606. flash_cmd(info->portwidth,addr,0,0xf0);
  607. flash_cmd(info->portwidth,addr,0,0xf0);
  608. printf (" done\n");
  609. return 0;
  610. }
  611. /*-----------------------------------------------------------------------
  612. * Copy memory to flash, returns:
  613. * 0 - OK
  614. * 1 - write timeout
  615. * 2 - Flash not erased
  616. */
  617. /* broken for 2x16: TODO */
  618. int
  619. write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  620. {
  621. ulong cp, wp, data;
  622. int i, l, rc;
  623. if(info->portwidth==4) return 1;
  624. if((info->flash_id & FLASH_TYPEMASK) == FLASH_ROM) return 0;
  625. if((info->flash_id & FLASH_TYPEMASK) == FLASH_RAM) {
  626. memcpy((void *)addr, src, cnt);
  627. return 0;
  628. }
  629. wp = (addr & ~3); /* get lower word aligned address */
  630. /*
  631. * handle unaligned start bytes
  632. */
  633. if ((l = addr - wp) != 0) {
  634. data = 0;
  635. for (i=0, cp=wp; i<l; ++i, ++cp) {
  636. data = (data << 8) | (*(uchar *)cp);
  637. }
  638. for (; i<4 && cnt>0; ++i) {
  639. data = (data << 8) | *src++;
  640. --cnt;
  641. ++cp;
  642. }
  643. for (; cnt==0 && i<4; ++i, ++cp) {
  644. data = (data << 8) | (*(uchar *)cp);
  645. }
  646. if ((rc = write_word(info, wp, data)) != 0) {
  647. return (rc);
  648. }
  649. wp += 4;
  650. }
  651. /*
  652. * handle word aligned part
  653. */
  654. while (cnt >= 4) {
  655. data = 0;
  656. for (i=0; i<4; ++i) {
  657. data = (data << 8) | *src++;
  658. }
  659. if ((rc = write_word(info, wp, data)) != 0) {
  660. return (rc);
  661. }
  662. wp += 4;
  663. cnt -= 4;
  664. }
  665. if (cnt == 0) {
  666. return (0);
  667. }
  668. /*
  669. * handle unaligned tail bytes
  670. */
  671. data = 0;
  672. for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
  673. data = (data << 8) | *src++;
  674. --cnt;
  675. }
  676. for (; i<4; ++i, ++cp) {
  677. data = (data << 8) | (*(uchar *)cp);
  678. }
  679. return (write_word(info, wp, data));
  680. }
  681. /*-----------------------------------------------------------------------
  682. * Write a word to Flash, returns:
  683. * 0 - OK
  684. * 1 - write timeout
  685. * 2 - Flash not erased
  686. */
  687. /* broken for 2x16: TODO */
  688. static int
  689. write_word (flash_info_t *info, ulong dest, ulong data)
  690. {
  691. volatile unsigned char *addr = (uchar *)(info->start[0]);
  692. ulong start;
  693. int flag, i;
  694. if(info->portwidth==4) return 1;
  695. if((info->flash_id & FLASH_TYPEMASK) == FLASH_ROM) return 1;
  696. if((info->flash_id & FLASH_TYPEMASK) == FLASH_RAM) {
  697. *(unsigned long *)dest=data;
  698. return 0;
  699. }
  700. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  701. unsigned short low = data & 0xffff;
  702. unsigned short hi = (data >> 16) & 0xffff;
  703. int ret = write_word_intel((bank_addr_t)dest, hi);
  704. if (!ret) ret = write_word_intel((bank_addr_t)(dest+2), low);
  705. return ret;
  706. }
  707. /* Check if Flash is (sufficiently) erased */
  708. if ((*((vu_long *)dest) & data) != data) {
  709. return (2);
  710. }
  711. /* Disable interrupts which might cause a timeout here */
  712. flag = disable_interrupts();
  713. /* first, perform an unlock bypass command to speed up flash writes */
  714. addr[0x555] = 0xAA;
  715. addr[0x2AA] = 0x55;
  716. addr[0x555] = 0x20;
  717. /* write each byte out */
  718. for (i = 0; i < 4; i++) {
  719. char *data_ch = (char *)&data;
  720. addr[0] = 0xA0;
  721. *(((char *)dest)+i) = data_ch[i];
  722. udelay(10); /* XXX */
  723. }
  724. /* we're done, now do an unlock bypass reset */
  725. addr[0] = 0x90;
  726. addr[0] = 0x00;
  727. /* re-enable interrupts if necessary */
  728. if (flag)
  729. enable_interrupts();
  730. /* data polling for D7 */
  731. start = get_timer (0);
  732. while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
  733. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  734. return (1);
  735. }
  736. }
  737. return (0);
  738. }