flash.c 20 KB

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