flash.c 21 KB

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