flash.c 21 KB

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