flash.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*
  2. * (C) Copyright 2004-2005
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2002 Jun Gu <jung@artesyncp.com>
  6. * Add support for Am29F016D and dynamic switch setting.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. /*
  27. * Modified 4/5/2001
  28. * Wait for completion of each sector erase command issued
  29. * 4/5/2001
  30. * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
  31. */
  32. #include <common.h>
  33. #include <ppc4xx.h>
  34. #include <asm/processor.h>
  35. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  36. /*-----------------------------------------------------------------------
  37. * Functions
  38. */
  39. static int write_word(flash_info_t * info, ulong dest, ulong data);
  40. #ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
  41. static int write_word_1(flash_info_t * info, ulong dest, ulong data);
  42. static int write_word_2(flash_info_t * info, ulong dest, ulong data);
  43. static int flash_erase_1(flash_info_t * info, int s_first, int s_last);
  44. static int flash_erase_2(flash_info_t * info, int s_first, int s_last);
  45. static ulong flash_get_size_1(vu_long * addr, flash_info_t * info);
  46. static ulong flash_get_size_2(vu_long * addr, flash_info_t * info);
  47. #endif
  48. void flash_print_info(flash_info_t * info)
  49. {
  50. int i;
  51. int k;
  52. int size;
  53. int erased;
  54. volatile unsigned long *flash;
  55. if (info->flash_id == FLASH_UNKNOWN) {
  56. printf("missing or unknown FLASH type\n");
  57. return;
  58. }
  59. switch (info->flash_id & FLASH_VENDMASK) {
  60. case FLASH_MAN_AMD:
  61. printf("AMD ");
  62. break;
  63. case FLASH_MAN_STM:
  64. printf("STM ");
  65. break;
  66. case FLASH_MAN_FUJ:
  67. printf("FUJITSU ");
  68. break;
  69. case FLASH_MAN_SST:
  70. printf("SST ");
  71. break;
  72. case FLASH_MAN_MX:
  73. printf ("MACRONIX ");
  74. break;
  75. default:
  76. printf("Unknown Vendor ");
  77. break;
  78. }
  79. switch (info->flash_id & FLASH_TYPEMASK) {
  80. case FLASH_AM040:
  81. printf("AM29F040 (512 Kbit, uniform sector size)\n");
  82. break;
  83. case FLASH_AM400B:
  84. printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
  85. break;
  86. case FLASH_AM400T:
  87. printf("AM29LV400T (4 Mbit, top boot sector)\n");
  88. break;
  89. case FLASH_AM800B:
  90. printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
  91. break;
  92. case FLASH_AM800T:
  93. printf("AM29LV800T (8 Mbit, top boot sector)\n");
  94. break;
  95. case FLASH_AMD016:
  96. printf("AM29F016D (16 Mbit, uniform sector size)\n");
  97. break;
  98. case FLASH_AM160B:
  99. printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
  100. break;
  101. case FLASH_AM160T:
  102. printf("AM29LV160T (16 Mbit, top boot sector)\n");
  103. break;
  104. case FLASH_AM320B:
  105. printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
  106. break;
  107. case FLASH_AM320T:
  108. printf("AM29LV320T (32 Mbit, top boot sector)\n");
  109. break;
  110. case FLASH_AM033C:
  111. printf("AM29LV033C (32 Mbit, top boot sector)\n");
  112. break;
  113. case FLASH_SST800A:
  114. printf("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
  115. break;
  116. case FLASH_SST160A:
  117. printf("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
  118. break;
  119. case FLASH_STMW320DT:
  120. printf ("M29W320DT (32 M, top sector)\n");
  121. break;
  122. case FLASH_MXLV320T:
  123. printf ("MXLV320T (32 Mbit, top sector)\n");
  124. break;
  125. default:
  126. printf("Unknown Chip Type\n");
  127. break;
  128. }
  129. printf(" Size: %ld KB in %d Sectors\n",
  130. info->size >> 10, info->sector_count);
  131. printf(" Sector Start Addresses:");
  132. for (i = 0; i < info->sector_count; ++i) {
  133. /*
  134. * Check if whole sector is erased
  135. */
  136. if (i != (info->sector_count - 1))
  137. size = info->start[i + 1] - info->start[i];
  138. else
  139. size = info->start[0] + info->size - info->start[i];
  140. erased = 1;
  141. flash = (volatile unsigned long *)info->start[i];
  142. size = size >> 2; /* divide by 4 for longword access */
  143. for (k = 0; k < size; k++) {
  144. if (*flash++ != 0xffffffff) {
  145. erased = 0;
  146. break;
  147. }
  148. }
  149. if ((i % 5) == 0)
  150. printf("\n ");
  151. printf(" %08lX%s%s",
  152. info->start[i],
  153. erased ? " E" : " ", info->protect[i] ? "RO " : " ");
  154. }
  155. printf("\n");
  156. return;
  157. }
  158. /*
  159. * The following code cannot be run from FLASH!
  160. */
  161. #ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
  162. static ulong flash_get_size(vu_long * addr, flash_info_t * info)
  163. {
  164. /* bit 0 used for big flash marking */
  165. if ((ulong)addr & 0x1) {
  166. return flash_get_size_2((vu_long *)((ulong)addr & 0xfffffffe), info);
  167. } else {
  168. return flash_get_size_1(addr, info);
  169. }
  170. }
  171. static ulong flash_get_size_1(vu_long * addr, flash_info_t * info)
  172. #else
  173. static ulong flash_get_size(vu_long * addr, flash_info_t * info)
  174. #endif
  175. {
  176. short i;
  177. CONFIG_SYS_FLASH_WORD_SIZE value;
  178. ulong base = (ulong) addr;
  179. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
  180. DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
  181. /* Write auto select command: read Manufacturer ID */
  182. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  183. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  184. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00900090;
  185. udelay(1000);
  186. value = addr2[0];
  187. DEBUGF("FLASH MANUFACT: %x\n", value);
  188. switch (value) {
  189. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_MANUFACT:
  190. info->flash_id = FLASH_MAN_AMD;
  191. break;
  192. case (CONFIG_SYS_FLASH_WORD_SIZE) FUJ_MANUFACT:
  193. info->flash_id = FLASH_MAN_FUJ;
  194. break;
  195. case (CONFIG_SYS_FLASH_WORD_SIZE) SST_MANUFACT:
  196. info->flash_id = FLASH_MAN_SST;
  197. break;
  198. case (CONFIG_SYS_FLASH_WORD_SIZE) STM_MANUFACT:
  199. info->flash_id = FLASH_MAN_STM;
  200. break;
  201. default:
  202. info->flash_id = FLASH_UNKNOWN;
  203. info->sector_count = 0;
  204. info->size = 0;
  205. return (0); /* no or unknown flash */
  206. }
  207. value = addr2[1]; /* device ID */
  208. DEBUGF("\nFLASH DEVICEID: %x\n", value);
  209. switch (value) {
  210. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV040B:
  211. info->flash_id += FLASH_AM040;
  212. info->sector_count = 8;
  213. info->size = 0x0080000; /* => 512 KiB */
  214. break;
  215. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F040B:
  216. info->flash_id += FLASH_AM040;
  217. info->sector_count = 8;
  218. info->size = 0x0080000; /* => 512 KiB */
  219. break;
  220. case (CONFIG_SYS_FLASH_WORD_SIZE) STM_ID_M29W040B:
  221. info->flash_id += FLASH_AM040;
  222. info->sector_count = 8;
  223. info->size = 0x0080000; /* => 512 KiB */
  224. break;
  225. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F016D:
  226. info->flash_id += FLASH_AMD016;
  227. info->sector_count = 32;
  228. info->size = 0x00200000; /* => 2 MiB */
  229. break;
  230. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV033C:
  231. info->flash_id += FLASH_AMDLV033C;
  232. info->sector_count = 64;
  233. info->size = 0x00400000; /* => 4 MiB */
  234. break;
  235. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400T:
  236. info->flash_id += FLASH_AM400T;
  237. info->sector_count = 11;
  238. info->size = 0x00080000; /* => 512 KiB */
  239. break;
  240. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400B:
  241. info->flash_id += FLASH_AM400B;
  242. info->sector_count = 11;
  243. info->size = 0x00080000; /* => 512 KiB */
  244. break;
  245. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800T:
  246. info->flash_id += FLASH_AM800T;
  247. info->sector_count = 19;
  248. info->size = 0x00100000; /* => 1 MiB */
  249. break;
  250. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800B:
  251. info->flash_id += FLASH_AM800B;
  252. info->sector_count = 19;
  253. info->size = 0x00100000; /* => 1 MiB */
  254. break;
  255. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160T:
  256. info->flash_id += FLASH_AM160T;
  257. info->sector_count = 35;
  258. info->size = 0x00200000; /* => 2 MiB */
  259. break;
  260. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160B:
  261. info->flash_id += FLASH_AM160B;
  262. info->sector_count = 35;
  263. info->size = 0x00200000; /* => 2 MiB */
  264. break;
  265. default:
  266. info->flash_id = FLASH_UNKNOWN;
  267. return (0); /* => no or unknown flash */
  268. }
  269. /* set up sector start address table */
  270. if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
  271. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
  272. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
  273. for (i = 0; i < info->sector_count; i++)
  274. info->start[i] = base + (i * 0x00010000);
  275. } else {
  276. if (info->flash_id & FLASH_BTYPE) {
  277. /* set sector offsets for bottom boot block type */
  278. info->start[0] = base + 0x00000000;
  279. info->start[1] = base + 0x00004000;
  280. info->start[2] = base + 0x00006000;
  281. info->start[3] = base + 0x00008000;
  282. for (i = 4; i < info->sector_count; i++) {
  283. info->start[i] =
  284. base + (i * 0x00010000) - 0x00030000;
  285. }
  286. } else {
  287. /* set sector offsets for top boot block type */
  288. i = info->sector_count - 1;
  289. info->start[i--] = base + info->size - 0x00004000;
  290. info->start[i--] = base + info->size - 0x00006000;
  291. info->start[i--] = base + info->size - 0x00008000;
  292. for (; i >= 0; i--) {
  293. info->start[i] = base + i * 0x00010000;
  294. }
  295. }
  296. }
  297. /* check for protected sectors */
  298. for (i = 0; i < info->sector_count; i++) {
  299. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  300. /* D0 = 1 if protected */
  301. addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
  302. /* For AMD29033C flash we need to resend the command of *
  303. * reading flash protection for upper 8 Mb of flash */
  304. if (i == 32) {
  305. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
  306. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
  307. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
  308. }
  309. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
  310. info->protect[i] = 0;
  311. else
  312. info->protect[i] = addr2[2] & 1;
  313. }
  314. /* issue bank reset to return to read mode */
  315. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
  316. return (info->size);
  317. }
  318. static int wait_for_DQ7_1(flash_info_t * info, int sect)
  319. {
  320. ulong start, now, last;
  321. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
  322. (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
  323. start = get_timer(0);
  324. last = start;
  325. while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
  326. (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) {
  327. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  328. printf("Timeout\n");
  329. return -1;
  330. }
  331. /* show that we're waiting */
  332. if ((now - last) > 1000) { /* every second */
  333. putc('.');
  334. last = now;
  335. }
  336. }
  337. return 0;
  338. }
  339. #ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
  340. int flash_erase(flash_info_t * info, int s_first, int s_last)
  341. {
  342. if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
  343. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
  344. ((info->flash_id & FLASH_TYPEMASK) == FLASH_MXLV320T) ||
  345. ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT)) {
  346. return flash_erase_2(info, s_first, s_last);
  347. } else {
  348. return flash_erase_1(info, s_first, s_last);
  349. }
  350. }
  351. static int flash_erase_1(flash_info_t * info, int s_first, int s_last)
  352. #else
  353. int flash_erase(flash_info_t * info, int s_first, int s_last)
  354. #endif
  355. {
  356. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
  357. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
  358. int flag, prot, sect, l_sect;
  359. int i;
  360. if ((s_first < 0) || (s_first > s_last)) {
  361. if (info->flash_id == FLASH_UNKNOWN) {
  362. printf("- missing\n");
  363. } else {
  364. printf("- no sectors to erase\n");
  365. }
  366. return 1;
  367. }
  368. if (info->flash_id == FLASH_UNKNOWN) {
  369. printf("Can't erase unknown flash type - aborted\n");
  370. return 1;
  371. }
  372. prot = 0;
  373. for (sect = s_first; sect <= s_last; ++sect) {
  374. if (info->protect[sect]) {
  375. prot++;
  376. }
  377. }
  378. if (prot) {
  379. printf("- Warning: %d protected sectors will not be erased!\n",
  380. prot);
  381. } else {
  382. printf("\n");
  383. }
  384. l_sect = -1;
  385. /* Disable interrupts which might cause a timeout here */
  386. flag = disable_interrupts();
  387. /* Start erase on unprotected sectors */
  388. for (sect = s_first; sect <= s_last; sect++) {
  389. if (info->protect[sect] == 0) { /* not protected */
  390. addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
  391. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  392. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  393. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  394. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
  395. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  396. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  397. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00500050; /* block erase */
  398. for (i = 0; i < 50; i++)
  399. udelay(1000); /* wait 1 ms */
  400. } else {
  401. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  402. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  403. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
  404. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  405. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  406. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
  407. }
  408. l_sect = sect;
  409. /*
  410. * Wait for each sector to complete, it's more
  411. * reliable. According to AMD Spec, you must
  412. * issue all erase commands within a specified
  413. * timeout. This has been seen to fail, especially
  414. * if printf()s are included (for debug)!!
  415. */
  416. wait_for_DQ7_1(info, sect);
  417. }
  418. }
  419. /* re-enable interrupts if necessary */
  420. if (flag)
  421. enable_interrupts();
  422. /* wait at least 80us - let's wait 1 ms */
  423. udelay(1000);
  424. /* reset to read mode */
  425. addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
  426. addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
  427. printf(" done\n");
  428. return 0;
  429. }
  430. /*-----------------------------------------------------------------------
  431. * Copy memory to flash, returns:
  432. * 0 - OK
  433. * 1 - write timeout
  434. * 2 - Flash not erased
  435. */
  436. int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  437. {
  438. ulong cp, wp, data;
  439. int i, l, rc;
  440. wp = (addr & ~3); /* get lower word aligned address */
  441. /*
  442. * handle unaligned start bytes
  443. */
  444. if ((l = addr - wp) != 0) {
  445. data = 0;
  446. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  447. data = (data << 8) | (*(uchar *) cp);
  448. }
  449. for (; i < 4 && cnt > 0; ++i) {
  450. data = (data << 8) | *src++;
  451. --cnt;
  452. ++cp;
  453. }
  454. for (; cnt == 0 && i < 4; ++i, ++cp) {
  455. data = (data << 8) | (*(uchar *) cp);
  456. }
  457. if ((rc = write_word(info, wp, data)) != 0) {
  458. return (rc);
  459. }
  460. wp += 4;
  461. }
  462. /*
  463. * handle word aligned part
  464. */
  465. while (cnt >= 4) {
  466. data = 0;
  467. for (i = 0; i < 4; ++i) {
  468. data = (data << 8) | *src++;
  469. }
  470. if ((rc = write_word(info, wp, data)) != 0) {
  471. return (rc);
  472. }
  473. wp += 4;
  474. cnt -= 4;
  475. }
  476. if (cnt == 0) {
  477. return (0);
  478. }
  479. /*
  480. * handle unaligned tail bytes
  481. */
  482. data = 0;
  483. for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
  484. data = (data << 8) | *src++;
  485. --cnt;
  486. }
  487. for (; i < 4; ++i, ++cp) {
  488. data = (data << 8) | (*(uchar *) cp);
  489. }
  490. return (write_word(info, wp, data));
  491. }
  492. /*-----------------------------------------------------------------------
  493. * Copy memory to flash, returns:
  494. * 0 - OK
  495. * 1 - write timeout
  496. * 2 - Flash not erased
  497. */
  498. #ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
  499. static int write_word(flash_info_t * info, ulong dest, ulong data)
  500. {
  501. if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
  502. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
  503. ((info->flash_id & FLASH_TYPEMASK) == FLASH_MXLV320T) ||
  504. ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT)) {
  505. return write_word_2(info, dest, data);
  506. } else {
  507. return write_word_1(info, dest, data);
  508. }
  509. }
  510. static int write_word_1(flash_info_t * info, ulong dest, ulong data)
  511. #else
  512. static int write_word(flash_info_t * info, ulong dest, ulong data)
  513. #endif
  514. {
  515. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
  516. volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *) dest;
  517. volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *) & data;
  518. ulong start;
  519. int i;
  520. /* Check if Flash is (sufficiently) erased */
  521. if ((*((vu_long *)dest) & data) != data) {
  522. return (2);
  523. }
  524. for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
  525. int flag;
  526. /* Disable interrupts which might cause a timeout here */
  527. flag = disable_interrupts();
  528. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  529. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  530. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00A000A0;
  531. dest2[i] = data2[i];
  532. /* re-enable interrupts if necessary */
  533. if (flag)
  534. enable_interrupts();
  535. /* data polling for D7 */
  536. start = get_timer(0);
  537. while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
  538. (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080)) {
  539. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  540. return (1);
  541. }
  542. }
  543. }
  544. return (0);
  545. }
  546. #ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
  547. #undef CONFIG_SYS_FLASH_WORD_SIZE
  548. #define CONFIG_SYS_FLASH_WORD_SIZE unsigned short
  549. /*
  550. * The following code cannot be run from FLASH!
  551. */
  552. static ulong flash_get_size_2(vu_long * addr, flash_info_t * info)
  553. {
  554. short i;
  555. int n;
  556. CONFIG_SYS_FLASH_WORD_SIZE value;
  557. ulong base = (ulong) addr;
  558. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
  559. DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
  560. /* Write auto select command: read Manufacturer ID */
  561. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  562. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  563. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00900090;
  564. udelay(1000);
  565. value = addr2[0];
  566. DEBUGF("FLASH MANUFACT: %x\n", value);
  567. switch (value) {
  568. case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_MANUFACT:
  569. info->flash_id = FLASH_MAN_AMD;
  570. break;
  571. case (CONFIG_SYS_FLASH_WORD_SIZE) FUJ_MANUFACT:
  572. info->flash_id = FLASH_MAN_FUJ;
  573. break;
  574. case (CONFIG_SYS_FLASH_WORD_SIZE) SST_MANUFACT:
  575. info->flash_id = FLASH_MAN_SST;
  576. break;
  577. case (CONFIG_SYS_FLASH_WORD_SIZE) STM_MANUFACT:
  578. info->flash_id = FLASH_MAN_STM;
  579. break;
  580. case (CONFIG_SYS_FLASH_WORD_SIZE) MX_MANUFACT:
  581. info->flash_id = FLASH_MAN_MX;
  582. break;
  583. default:
  584. info->flash_id = FLASH_UNKNOWN;
  585. info->sector_count = 0;
  586. info->size = 0;
  587. return (0); /* no or unknown flash */
  588. }
  589. value = addr2[1]; /* device ID */
  590. DEBUGF("\nFLASH DEVICEID: %x\n", value);
  591. switch (value) {
  592. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320T:
  593. info->flash_id += FLASH_AM320T;
  594. info->sector_count = 71;
  595. info->size = 0x00400000; break; /* => 4 MiB */
  596. case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320B:
  597. info->flash_id += FLASH_AM320B;
  598. info->sector_count = 71;
  599. info->size = 0x00400000; break; /* => 4 MiB */
  600. case (CONFIG_SYS_FLASH_WORD_SIZE)STM_ID_29W320DT:
  601. info->flash_id += FLASH_STMW320DT;
  602. info->sector_count = 67;
  603. info->size = 0x00400000; break; /* => 4 MiB */
  604. case (CONFIG_SYS_FLASH_WORD_SIZE)MX_ID_LV320T:
  605. info->flash_id += FLASH_MXLV320T;
  606. info->sector_count = 71;
  607. info->size = 0x00400000;
  608. break; /* => 4 MB */
  609. default:
  610. info->flash_id = FLASH_UNKNOWN;
  611. return (0); /* => no or unknown flash */
  612. }
  613. /* set up sector start address table */
  614. if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
  615. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
  616. ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
  617. for (i = 0; i < info->sector_count; i++)
  618. info->start[i] = base + (i * 0x00010000);
  619. } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) {
  620. /* set sector offsets for top boot block type */
  621. base += info->size;
  622. i = info->sector_count;
  623. /* 1 x 16k boot sector */
  624. base -= 16 << 10;
  625. --i;
  626. info->start[i] = base;
  627. /* 2 x 8k boot sectors */
  628. for (n=0; n<2; ++n) {
  629. base -= 8 << 10;
  630. --i;
  631. info->start[i] = base;
  632. }
  633. /* 1 x 32k boot sector */
  634. base -= 32 << 10;
  635. --i;
  636. info->start[i] = base;
  637. while (i > 0) { /* 64k regular sectors */
  638. base -= 64 << 10;
  639. --i;
  640. info->start[i] = base;
  641. }
  642. } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_MXLV320T) {
  643. i = info->sector_count - 1;
  644. info->start[i--] = base + info->size - 0x00002000;
  645. info->start[i--] = base + info->size - 0x00004000;
  646. info->start[i--] = base + info->size - 0x00006000;
  647. info->start[i--] = base + info->size - 0x00008000;
  648. info->start[i--] = base + info->size - 0x0000a000;
  649. info->start[i--] = base + info->size - 0x0000c000;
  650. info->start[i--] = base + info->size - 0x0000e000;
  651. info->start[i--] = base + info->size - 0x00010000;
  652. for (; i >= 0; i--)
  653. info->start[i] = base + i * 0x00010000;
  654. } else {
  655. if (info->flash_id & FLASH_BTYPE) {
  656. /* set sector offsets for bottom boot block type */
  657. info->start[0] = base + 0x00000000;
  658. info->start[1] = base + 0x00002000;
  659. info->start[2] = base + 0x00004000;
  660. info->start[3] = base + 0x00006000;
  661. info->start[4] = base + 0x00008000;
  662. info->start[5] = base + 0x0000a000;
  663. info->start[6] = base + 0x0000c000;
  664. info->start[7] = base + 0x0000e000;
  665. for (i = 8; i < info->sector_count; i++) {
  666. info->start[i] =
  667. base + ((i-7) * 0x00010000);
  668. }
  669. } else {
  670. /* set sector offsets for top boot block type */
  671. i = info->sector_count - 1;
  672. info->start[i--] = base + info->size - 0x00002000;
  673. info->start[i--] = base + info->size - 0x00004000;
  674. info->start[i--] = base + info->size - 0x00006000;
  675. info->start[i--] = base + info->size - 0x00008000;
  676. info->start[i--] = base + info->size - 0x0000a000;
  677. info->start[i--] = base + info->size - 0x0000c000;
  678. info->start[i--] = base + info->size - 0x0000e000;
  679. for (; i >= 0; i--) {
  680. info->start[i] = base + i * 0x00010000;
  681. }
  682. }
  683. }
  684. /* check for protected sectors */
  685. for (i = 0; i < info->sector_count; i++) {
  686. /* read sector protection at sector address, (A7 .. A0) = 0x02 */
  687. /* D0 = 1 if protected */
  688. addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
  689. /* For AMD29033C flash we need to resend the command of *
  690. * reading flash protection for upper 8 Mb of flash */
  691. if (i == 32) {
  692. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
  693. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
  694. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
  695. }
  696. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
  697. info->protect[i] = 0;
  698. else
  699. info->protect[i] = addr2[2] & 1;
  700. }
  701. /* issue bank reset to return to read mode */
  702. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
  703. return (info->size);
  704. }
  705. static int wait_for_DQ7_2(flash_info_t * info, int sect)
  706. {
  707. ulong start, now, last;
  708. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
  709. (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
  710. start = get_timer(0);
  711. last = start;
  712. while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
  713. (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) {
  714. if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  715. printf("Timeout\n");
  716. return -1;
  717. }
  718. /* show that we're waiting */
  719. if ((now - last) > 1000) { /* every second */
  720. putc('.');
  721. last = now;
  722. }
  723. }
  724. return 0;
  725. }
  726. static int flash_erase_2(flash_info_t * info, int s_first, int s_last)
  727. {
  728. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
  729. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
  730. int flag, prot, sect, l_sect;
  731. int i;
  732. if ((s_first < 0) || (s_first > s_last)) {
  733. if (info->flash_id == FLASH_UNKNOWN) {
  734. printf("- missing\n");
  735. } else {
  736. printf("- no sectors to erase\n");
  737. }
  738. return 1;
  739. }
  740. if (info->flash_id == FLASH_UNKNOWN) {
  741. printf("Can't erase unknown flash type - aborted\n");
  742. return 1;
  743. }
  744. prot = 0;
  745. for (sect = s_first; sect <= s_last; ++sect) {
  746. if (info->protect[sect]) {
  747. prot++;
  748. }
  749. }
  750. if (prot) {
  751. printf("- Warning: %d protected sectors will not be erased!\n",
  752. prot);
  753. } else {
  754. printf("\n");
  755. }
  756. l_sect = -1;
  757. /* Disable interrupts which might cause a timeout here */
  758. flag = disable_interrupts();
  759. /* Start erase on unprotected sectors */
  760. for (sect = s_first; sect <= s_last; sect++) {
  761. if (info->protect[sect] == 0) { /* not protected */
  762. addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
  763. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
  764. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  765. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  766. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
  767. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  768. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  769. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00500050; /* block erase */
  770. for (i = 0; i < 50; i++)
  771. udelay(1000); /* wait 1 ms */
  772. } else {
  773. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  774. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  775. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
  776. addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  777. addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  778. addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
  779. }
  780. l_sect = sect;
  781. /*
  782. * Wait for each sector to complete, it's more
  783. * reliable. According to AMD Spec, you must
  784. * issue all erase commands within a specified
  785. * timeout. This has been seen to fail, especially
  786. * if printf()s are included (for debug)!!
  787. */
  788. wait_for_DQ7_2(info, sect);
  789. }
  790. }
  791. /* re-enable interrupts if necessary */
  792. if (flag)
  793. enable_interrupts();
  794. /* wait at least 80us - let's wait 1 ms */
  795. udelay(1000);
  796. /* reset to read mode */
  797. addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
  798. addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
  799. printf(" done\n");
  800. return 0;
  801. }
  802. static int write_word_2(flash_info_t * info, ulong dest, ulong data)
  803. {
  804. volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
  805. volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *) dest;
  806. volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *) & data;
  807. ulong start;
  808. int i;
  809. /* Check if Flash is (sufficiently) erased */
  810. if ((*((vu_long *)dest) & data) != data) {
  811. return (2);
  812. }
  813. for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
  814. int flag;
  815. /* Disable interrupts which might cause a timeout here */
  816. flag = disable_interrupts();
  817. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
  818. addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
  819. addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00A000A0;
  820. dest2[i] = data2[i];
  821. /* re-enable interrupts if necessary */
  822. if (flag)
  823. enable_interrupts();
  824. /* data polling for D7 */
  825. start = get_timer(0);
  826. while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
  827. (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080)) {
  828. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  829. return (1);
  830. }
  831. }
  832. }
  833. return (0);
  834. }
  835. #endif /* CONFIG_SYS_FLASH_2ND_16BIT_DEV */