flash.c 20 KB

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