flash.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /*
  2. * (C) Copyright 2001
  3. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  4. *
  5. * (C) Copyright 2001-2004
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  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. #include <common.h>
  27. #include <linux/byteorder/swab.h>
  28. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  29. /* Board support for 1 or 2 flash devices */
  30. #define FLASH_PORT_WIDTH8
  31. typedef unsigned char FLASH_PORT_WIDTH;
  32. typedef volatile unsigned char FLASH_PORT_WIDTHV;
  33. #define SWAP(x) (x)
  34. /* Intel-compatible flash ID */
  35. #define INTEL_COMPAT 0x89
  36. #define INTEL_ALT 0xB0
  37. /* Intel-compatible flash commands */
  38. #define INTEL_PROGRAM 0x10
  39. #define INTEL_ERASE 0x20
  40. #define INTEL_CLEAR 0x50
  41. #define INTEL_LOCKBIT 0x60
  42. #define INTEL_PROTECT 0x01
  43. #define INTEL_STATUS 0x70
  44. #define INTEL_READID 0x90
  45. #define INTEL_CONFIRM 0xD0
  46. #define INTEL_RESET 0xFF
  47. /* Intel-compatible flash status bits */
  48. #define INTEL_FINISHED 0x80
  49. #define INTEL_OK 0x80
  50. #define FPW FLASH_PORT_WIDTH
  51. #define FPWV FLASH_PORT_WIDTHV
  52. #define FLASH_CYCLE1 0x0555
  53. #define FLASH_CYCLE2 0x02aa
  54. #define WR_BLOCK 0x20
  55. /*-----------------------------------------------------------------------
  56. * Functions
  57. */
  58. static ulong flash_get_size (FPW * addr, flash_info_t * info);
  59. static int write_data (flash_info_t * info, ulong dest, FPW data);
  60. static int write_data_block (flash_info_t * info, ulong src, ulong dest);
  61. static int write_word_amd (flash_info_t * info, FPWV * dest, FPW data);
  62. static void flash_get_offsets (ulong base, flash_info_t * info);
  63. void inline spin_wheel (void);
  64. static void flash_sync_real_protect (flash_info_t * info);
  65. static unsigned char intel_sector_protected (flash_info_t *info, ushort sector);
  66. static unsigned char same_chip_banks (int bank1, int bank2);
  67. /*-----------------------------------------------------------------------
  68. */
  69. unsigned long flash_init (void)
  70. {
  71. int i;
  72. ulong size = 0;
  73. ulong fsize = 0;
  74. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  75. memset (&flash_info[i], 0, sizeof (flash_info_t));
  76. switch (i) {
  77. case 0:
  78. flash_get_size ((FPW *) CONFIG_SYS_FLASH1_BASE,
  79. &flash_info[i]);
  80. flash_get_offsets (CONFIG_SYS_FLASH1_BASE, &flash_info[i]);
  81. break;
  82. case 1:
  83. flash_get_size ((FPW *) CONFIG_SYS_FLASH1_BASE,
  84. &flash_info[i]);
  85. fsize = CONFIG_SYS_FLASH1_BASE + flash_info[i - 1].size;
  86. flash_get_offsets (fsize, &flash_info[i]);
  87. break;
  88. case 2:
  89. flash_get_size ((FPW *) CONFIG_SYS_FLASH0_BASE,
  90. &flash_info[i]);
  91. flash_get_offsets (CONFIG_SYS_FLASH0_BASE, &flash_info[i]);
  92. break;
  93. case 3:
  94. flash_get_size ((FPW *) CONFIG_SYS_FLASH0_BASE,
  95. &flash_info[i]);
  96. fsize = CONFIG_SYS_FLASH0_BASE + flash_info[i - 1].size;
  97. flash_get_offsets (fsize, &flash_info[i]);
  98. break;
  99. default:
  100. panic ("configured to many flash banks!\n");
  101. break;
  102. }
  103. size += flash_info[i].size;
  104. /* get the h/w and s/w protection status in sync */
  105. flash_sync_real_protect(&flash_info[i]);
  106. }
  107. /* Protect monitor and environment sectors
  108. */
  109. #if defined (CONFIG_SYS_AMD_BOOT)
  110. flash_protect (FLAG_PROTECT_SET,
  111. CONFIG_SYS_MONITOR_BASE,
  112. CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
  113. &flash_info[2]);
  114. flash_protect (FLAG_PROTECT_SET,
  115. CONFIG_SYS_INTEL_BASE,
  116. CONFIG_SYS_INTEL_BASE + monitor_flash_len - 1,
  117. &flash_info[1]);
  118. #else
  119. flash_protect (FLAG_PROTECT_SET,
  120. CONFIG_SYS_MONITOR_BASE,
  121. CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
  122. &flash_info[3]);
  123. flash_protect (FLAG_PROTECT_SET,
  124. CONFIG_SYS_AMD_BASE,
  125. CONFIG_SYS_AMD_BASE + monitor_flash_len - 1, &flash_info[0]);
  126. #endif
  127. flash_protect (FLAG_PROTECT_SET,
  128. CONFIG_ENV1_ADDR,
  129. CONFIG_ENV1_ADDR + CONFIG_ENV1_SIZE - 1, &flash_info[1]);
  130. flash_protect (FLAG_PROTECT_SET,
  131. CONFIG_ENV_ADDR,
  132. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[3]);
  133. return size;
  134. }
  135. /*-----------------------------------------------------------------------
  136. */
  137. static void flash_get_offsets (ulong base, flash_info_t * info)
  138. {
  139. int i;
  140. if (info->flash_id == FLASH_UNKNOWN)
  141. return;
  142. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD) {
  143. for (i = 0; i < info->sector_count; i++) {
  144. info->start[i] = base + (i * PHYS_AMD_SECT_SIZE);
  145. info->protect[i] = 0;
  146. }
  147. }
  148. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  149. for (i = 0; i < info->sector_count; i++) {
  150. info->start[i] = base + (i * PHYS_INTEL_SECT_SIZE);
  151. }
  152. }
  153. }
  154. /*-----------------------------------------------------------------------
  155. */
  156. void flash_print_info (flash_info_t * info)
  157. {
  158. int i;
  159. if (info->flash_id == FLASH_UNKNOWN) {
  160. printf ("missing or unknown FLASH type\n");
  161. return;
  162. }
  163. switch (info->flash_id & FLASH_VENDMASK) {
  164. case FLASH_MAN_INTEL:
  165. printf ("INTEL ");
  166. break;
  167. case FLASH_MAN_AMD:
  168. printf ("AMD ");
  169. break;
  170. default:
  171. printf ("Unknown Vendor ");
  172. break;
  173. }
  174. switch (info->flash_id & FLASH_TYPEMASK) {
  175. case FLASH_28F128J3A:
  176. printf ("28F128J3A\n");
  177. break;
  178. case FLASH_AM040:
  179. printf ("AMD29F040B\n");
  180. break;
  181. default:
  182. printf ("Unknown Chip Type\n");
  183. break;
  184. }
  185. printf (" Size: %ld MB in %d Sectors\n",
  186. info->size >> 20, info->sector_count);
  187. printf (" Sector Start Addresses:");
  188. for (i = 0; i < info->sector_count; ++i) {
  189. if ((i % 5) == 0)
  190. printf ("\n ");
  191. printf (" %08lX%s",
  192. info->start[i], info->protect[i] ? " (RO)" : " ");
  193. }
  194. printf ("\n");
  195. return;
  196. }
  197. /*
  198. * The following code cannot be run from FLASH!
  199. */
  200. static ulong flash_get_size (FPW * addr, flash_info_t * info)
  201. {
  202. FPWV value;
  203. static int amd = 0;
  204. /* Write auto select command: read Manufacturer ID */
  205. /* Write auto select command sequence and test FLASH answer */
  206. addr[FLASH_CYCLE1] = (FPW) 0x00AA00AA; /* for AMD, Intel ignores this */
  207. __asm__ ("sync");
  208. addr[FLASH_CYCLE2] = (FPW) 0x00550055; /* for AMD, Intel ignores this */
  209. __asm__ ("sync");
  210. addr[FLASH_CYCLE1] = (FPW) 0x00900090; /* selects Intel or AMD */
  211. __asm__ ("sync");
  212. udelay (100);
  213. switch (addr[0] & 0xff) {
  214. case (uchar) AMD_MANUFACT:
  215. info->flash_id = FLASH_MAN_AMD;
  216. value = addr[1];
  217. break;
  218. case (uchar) INTEL_MANUFACT:
  219. info->flash_id = FLASH_MAN_INTEL;
  220. value = addr[2];
  221. break;
  222. default:
  223. printf ("unknown\n");
  224. info->flash_id = FLASH_UNKNOWN;
  225. info->sector_count = 0;
  226. info->size = 0;
  227. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  228. return (0); /* no or unknown flash */
  229. }
  230. switch (value) {
  231. case (FPW) INTEL_ID_28F128J3A:
  232. info->flash_id += FLASH_28F128J3A;
  233. info->sector_count = 64;
  234. info->size = 0x00800000; /* => 16 MB */
  235. break;
  236. case (FPW) AMD_ID_LV040B:
  237. info->flash_id += FLASH_AM040;
  238. if (amd == 0) {
  239. info->sector_count = 7;
  240. info->size = 0x00070000; /* => 448 KB */
  241. amd = 1;
  242. } else {
  243. /* for Environment settings */
  244. info->sector_count = 1;
  245. info->size = PHYS_AMD_SECT_SIZE; /* => 64 KB */
  246. amd = 0;
  247. }
  248. break;
  249. default:
  250. info->flash_id = FLASH_UNKNOWN;
  251. break;
  252. }
  253. if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
  254. printf ("** ERROR: sector count %d > max (%d) **\n",
  255. info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
  256. info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  257. }
  258. if (value == (FPW) INTEL_ID_28F128J3A)
  259. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  260. else
  261. addr[0] = (FPW) 0x00F000F0; /* restore read mode */
  262. return (info->size);
  263. }
  264. /*
  265. * This function gets the u-boot flash sector protection status
  266. * (flash_info_t.protect[]) in sync with the sector protection
  267. * status stored in hardware.
  268. */
  269. static void flash_sync_real_protect (flash_info_t * info)
  270. {
  271. int i;
  272. switch (info->flash_id & FLASH_TYPEMASK) {
  273. case FLASH_28F128J3A:
  274. for (i = 0; i < info->sector_count; ++i) {
  275. info->protect[i] = intel_sector_protected(info, i);
  276. }
  277. break;
  278. case FLASH_AM040:
  279. default:
  280. /* no h/w protect support */
  281. break;
  282. }
  283. }
  284. /*
  285. * checks if "sector" in bank "info" is protected. Should work on intel
  286. * strata flash chips 28FxxxJ3x in 8-bit mode.
  287. * Returns 1 if sector is protected (or timed-out while trying to read
  288. * protection status), 0 if it is not.
  289. */
  290. static unsigned char intel_sector_protected (flash_info_t *info, ushort sector)
  291. {
  292. FPWV *addr;
  293. FPWV *lock_conf_addr;
  294. ulong start;
  295. unsigned char ret;
  296. /*
  297. * first, wait for the WSM to be finished. The rationale for
  298. * waiting for the WSM to become idle for at most
  299. * CONFIG_SYS_FLASH_ERASE_TOUT is as follows. The WSM can be busy
  300. * because of: (1) erase, (2) program or (3) lock bit
  301. * configuration. So we just wait for the longest timeout of
  302. * the (1)-(3), i.e. the erase timeout.
  303. */
  304. /* wait at least 35ns (W12) before issuing Read Status Register */
  305. udelay(1);
  306. addr = (FPWV *) info->start[sector];
  307. *addr = (FPW) INTEL_STATUS;
  308. start = get_timer (0);
  309. while ((*addr & (FPW) INTEL_FINISHED) != (FPW) INTEL_FINISHED) {
  310. if (get_timer (start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  311. *addr = (FPW) INTEL_RESET; /* restore read mode */
  312. printf("WSM busy too long, can't get prot status\n");
  313. return 1;
  314. }
  315. }
  316. /* issue the Read Identifier Codes command */
  317. *addr = (FPW) INTEL_READID;
  318. /* wait at least 35ns (W12) before reading */
  319. udelay(1);
  320. /* Intel example code uses offset of 4 for 8-bit flash */
  321. lock_conf_addr = (FPWV *) info->start[sector] + 4;
  322. ret = (*lock_conf_addr & (FPW) INTEL_PROTECT) ? 1 : 0;
  323. /* put flash back in read mode */
  324. *addr = (FPW) INTEL_RESET;
  325. return ret;
  326. }
  327. /*
  328. * Checks if "bank1" and "bank2" are on the same chip. Returns 1 if they
  329. * are and 0 otherwise.
  330. */
  331. static unsigned char same_chip_banks (int bank1, int bank2)
  332. {
  333. unsigned char same_chip[CONFIG_SYS_MAX_FLASH_BANKS][CONFIG_SYS_MAX_FLASH_BANKS] = {
  334. {1, 1, 0, 0},
  335. {1, 1, 0, 0},
  336. {0, 0, 1, 1},
  337. {0, 0, 1, 1}
  338. };
  339. return same_chip[bank1][bank2];
  340. }
  341. /*-----------------------------------------------------------------------
  342. */
  343. int flash_erase (flash_info_t * info, int s_first, int s_last)
  344. {
  345. int flag, prot, sect;
  346. ulong type, start;
  347. int rcode = 0, intel = 0;
  348. if ((s_first < 0) || (s_first > s_last)) {
  349. if (info->flash_id == FLASH_UNKNOWN)
  350. printf ("- missing\n");
  351. else
  352. printf ("- no sectors to erase\n");
  353. return 1;
  354. }
  355. type = (info->flash_id & FLASH_VENDMASK);
  356. if ((type != FLASH_MAN_INTEL)) {
  357. type = (info->flash_id & FLASH_VENDMASK);
  358. if ((type != FLASH_MAN_AMD)) {
  359. printf ("Can't erase unknown flash type %08lx - aborted\n",
  360. info->flash_id);
  361. return 1;
  362. }
  363. }
  364. if (type == FLASH_MAN_INTEL)
  365. intel = 1;
  366. prot = 0;
  367. for (sect = s_first; sect <= s_last; ++sect) {
  368. if (info->protect[sect]) {
  369. prot++;
  370. }
  371. }
  372. if (prot) {
  373. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  374. } else {
  375. printf ("\n");
  376. }
  377. start = get_timer (0);
  378. /* Disable interrupts which might cause a timeout here */
  379. flag = disable_interrupts ();
  380. /* Start erase on unprotected sectors */
  381. for (sect = s_first; sect <= s_last; sect++) {
  382. if (info->protect[sect] == 0) { /* not protected */
  383. FPWV *addr = (FPWV *) (info->start[sect]);
  384. FPW status;
  385. printf ("Erasing sector %2d ... ", sect);
  386. /* arm simple, non interrupt dependent timer */
  387. start = get_timer (0);
  388. if (intel) {
  389. *addr = (FPW) 0x00500050; /* clear status register */
  390. *addr = (FPW) 0x00200020; /* erase setup */
  391. *addr = (FPW) 0x00D000D0; /* erase confirm */
  392. } else {
  393. FPWV *base; /* first address in bank */
  394. base = (FPWV *) (CONFIG_SYS_AMD_BASE);
  395. base[FLASH_CYCLE1] = (FPW) 0x00AA00AA; /* unlock */
  396. base[FLASH_CYCLE2] = (FPW) 0x00550055; /* unlock */
  397. base[FLASH_CYCLE1] = (FPW) 0x00800080; /* erase mode */
  398. base[FLASH_CYCLE1] = (FPW) 0x00AA00AA; /* unlock */
  399. base[FLASH_CYCLE2] = (FPW) 0x00550055; /* unlock */
  400. *addr = (FPW) 0x00300030; /* erase sector */
  401. }
  402. while (((status =
  403. *addr) & (FPW) 0x00800080) !=
  404. (FPW) 0x00800080) {
  405. if (get_timer (start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
  406. printf ("Timeout\n");
  407. if (intel) {
  408. *addr = (FPW) 0x00B000B0; /* suspend erase */
  409. *addr = (FPW) 0x00FF00FF; /* reset to read mode */
  410. } else
  411. *addr = (FPW) 0x00F000F0; /* reset to read mode */
  412. rcode = 1;
  413. break;
  414. }
  415. }
  416. if (intel) {
  417. *addr = (FPW) 0x00500050; /* clear status register cmd. */
  418. *addr = (FPW) 0x00FF00FF; /* resest to read mode */
  419. } else
  420. *addr = (FPW) 0x00F000F0; /* reset to read mode */
  421. printf (" done\n");
  422. }
  423. }
  424. if (flag)
  425. enable_interrupts();
  426. return rcode;
  427. }
  428. /*-----------------------------------------------------------------------
  429. * Copy memory to flash, returns:
  430. * 0 - OK
  431. * 1 - write timeout
  432. * 2 - Flash not erased
  433. * 4 - Flash not identified
  434. */
  435. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  436. {
  437. if (info->flash_id == FLASH_UNKNOWN) {
  438. return 4;
  439. }
  440. switch (info->flash_id & FLASH_VENDMASK) {
  441. case FLASH_MAN_AMD:
  442. {
  443. FPW data = 0; /* 16 or 32 bit word, matches flash bus width */
  444. int bytes; /* number of bytes to program in current word */
  445. int left; /* number of bytes left to program */
  446. int i, res;
  447. for (left = cnt, res = 0;
  448. left > 0 && res == 0;
  449. addr += sizeof (data), left -=
  450. sizeof (data) - bytes) {
  451. bytes = addr & (sizeof (data) - 1);
  452. addr &= ~(sizeof (data) - 1);
  453. /* combine source and destination data so can program
  454. * an entire word of 16 or 32 bits
  455. */
  456. for (i = 0; i < sizeof (data); i++) {
  457. data <<= 8;
  458. if (i < bytes || i - bytes >= left)
  459. data += *((uchar *) addr + i);
  460. else
  461. data += *src++;
  462. }
  463. res = write_word_amd (info, (FPWV *) addr,
  464. data);
  465. }
  466. return res;
  467. } /* case FLASH_MAN_AMD */
  468. case FLASH_MAN_INTEL:
  469. {
  470. ulong cp, wp;
  471. FPW data;
  472. int count, i, l, rc, port_width;
  473. /* get lower word aligned address */
  474. wp = addr;
  475. port_width = 1;
  476. /*
  477. * handle unaligned start bytes
  478. */
  479. if ((l = addr - wp) != 0) {
  480. data = 0;
  481. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  482. data = (data << 8) | (*(uchar *) cp);
  483. }
  484. for (; i < port_width && cnt > 0; ++i) {
  485. data = (data << 8) | *src++;
  486. --cnt;
  487. ++cp;
  488. }
  489. for (; cnt == 0 && i < port_width; ++i, ++cp)
  490. data = (data << 8) | (*(uchar *) cp);
  491. if ((rc =
  492. write_data (info, wp, SWAP (data))) != 0)
  493. return (rc);
  494. wp += port_width;
  495. }
  496. if (cnt > WR_BLOCK) {
  497. /*
  498. * handle word aligned part
  499. */
  500. count = 0;
  501. while (cnt >= WR_BLOCK) {
  502. if ((rc =
  503. write_data_block (info,
  504. (ulong) src,
  505. wp)) != 0)
  506. return (rc);
  507. wp += WR_BLOCK;
  508. src += WR_BLOCK;
  509. cnt -= WR_BLOCK;
  510. if (count++ > 0x800) {
  511. spin_wheel ();
  512. count = 0;
  513. }
  514. }
  515. }
  516. if (cnt < WR_BLOCK) {
  517. /*
  518. * handle word aligned part
  519. */
  520. count = 0;
  521. while (cnt >= port_width) {
  522. data = 0;
  523. for (i = 0; i < port_width; ++i)
  524. data = (data << 8) | *src++;
  525. if ((rc =
  526. write_data (info, wp,
  527. SWAP (data))) != 0)
  528. return (rc);
  529. wp += port_width;
  530. cnt -= port_width;
  531. if (count++ > 0x800) {
  532. spin_wheel ();
  533. count = 0;
  534. }
  535. }
  536. }
  537. if (cnt == 0)
  538. return (0);
  539. /*
  540. * handle unaligned tail bytes
  541. */
  542. data = 0;
  543. for (i = 0, cp = wp; i < port_width && cnt > 0;
  544. ++i, ++cp) {
  545. data = (data << 8) | *src++;
  546. --cnt;
  547. }
  548. for (; i < port_width; ++i, ++cp)
  549. data = (data << 8) | (*(uchar *) cp);
  550. return (write_data (info, wp, SWAP (data)));
  551. } /* case FLASH_MAN_INTEL */
  552. } /* switch */
  553. return (0);
  554. }
  555. /*-----------------------------------------------------------------------
  556. * Write a word or halfword to Flash, returns:
  557. * 0 - OK
  558. * 1 - write timeout
  559. * 2 - Flash not erased
  560. */
  561. static int write_data (flash_info_t * info, ulong dest, FPW data)
  562. {
  563. FPWV *addr = (FPWV *) dest;
  564. ulong start;
  565. int flag, rc = 0;
  566. /* Check if Flash is (sufficiently) erased */
  567. if ((*addr & data) != data) {
  568. printf ("not erased at %08lx (%lx)\n", (ulong)addr, (ulong)*addr);
  569. return (2);
  570. }
  571. /* Disable interrupts which might cause a timeout here */
  572. flag = disable_interrupts ();
  573. *addr = (FPW) 0x00400040; /* write setup */
  574. *addr = data;
  575. /* arm simple, non interrupt dependent timer */
  576. start = get_timer (0);
  577. /* wait while polling the status register */
  578. while ((*addr & (FPW) 0x00800080) != (FPW) 0x00800080) {
  579. if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  580. rc = 1;
  581. goto OUT;
  582. }
  583. }
  584. OUT:
  585. *addr = (FPW)0x00FF00FF; /* restore read mode */
  586. if (flag)
  587. enable_interrupts();
  588. return rc;
  589. }
  590. /*-----------------------------------------------------------------------
  591. * Write a word or halfword to Flash, returns:
  592. * 0 - OK
  593. * 1 - write timeout
  594. * 2 - Flash not erased
  595. */
  596. static int write_data_block (flash_info_t * info, ulong src, ulong dest)
  597. {
  598. FPWV *srcaddr = (FPWV *) src;
  599. FPWV *dstaddr = (FPWV *) dest;
  600. ulong start;
  601. int flag, i, rc = 0;
  602. /* Check if Flash is (sufficiently) erased */
  603. for (i = 0; i < WR_BLOCK; i++)
  604. if ((*dstaddr++ & 0xff) != 0xff) {
  605. printf ("not erased at %08lx (%lx)\n",
  606. (ulong)dstaddr, (ulong)*dstaddr);
  607. return (2);
  608. }
  609. dstaddr = (FPWV *) dest;
  610. /* Disable interrupts which might cause a timeout here */
  611. flag = disable_interrupts ();
  612. *dstaddr = (FPW) 0x00e800e8; /* write block setup */
  613. /* arm simple, non interrupt dependent timer */
  614. start = get_timer (0);
  615. /* wait while polling the status register */
  616. while ((*dstaddr & (FPW)0x00800080) != (FPW)0x00800080) {
  617. if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  618. rc = 1;
  619. goto OUT;
  620. }
  621. }
  622. *dstaddr = (FPW) 0x001f001f; /* write 32 to buffer */
  623. for (i = 0; i < WR_BLOCK; i++)
  624. *dstaddr++ = *srcaddr++;
  625. dstaddr -= 1;
  626. *dstaddr = (FPW) 0x00d000d0; /* write 32 to buffer */
  627. /* arm simple, non interrupt dependent timer */
  628. start = get_timer (0);
  629. /* wait while polling the status register */
  630. while ((*dstaddr & (FPW) 0x00800080) != (FPW) 0x00800080) {
  631. if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  632. *dstaddr = (FPW) 0x00FF00FF; /* restore read mode */
  633. return (1);
  634. }
  635. }
  636. OUT:
  637. *dstaddr = (FPW)0x00FF00FF; /* restore read mode */
  638. if (flag)
  639. enable_interrupts();
  640. return rc;
  641. }
  642. /*-----------------------------------------------------------------------
  643. * Write a word to Flash for AMD FLASH
  644. * A word is 16 or 32 bits, whichever the bus width of the flash bank
  645. * (not an individual chip) is.
  646. *
  647. * returns:
  648. * 0 - OK
  649. * 1 - write timeout
  650. * 2 - Flash not erased
  651. */
  652. static int write_word_amd (flash_info_t * info, FPWV * dest, FPW data)
  653. {
  654. ulong start;
  655. int flag;
  656. int res = 0; /* result, assume success */
  657. FPWV *base; /* first address in flash bank */
  658. /* Check if Flash is (sufficiently) erased */
  659. if ((*dest & data) != data) {
  660. return (2);
  661. }
  662. base = (FPWV *) (CONFIG_SYS_AMD_BASE);
  663. /* Disable interrupts which might cause a timeout here */
  664. flag = disable_interrupts ();
  665. base[FLASH_CYCLE1] = (FPW) 0x00AA00AA; /* unlock */
  666. base[FLASH_CYCLE2] = (FPW) 0x00550055; /* unlock */
  667. base[FLASH_CYCLE1] = (FPW) 0x00A000A0; /* selects program mode */
  668. *dest = data; /* start programming the data */
  669. /* re-enable interrupts if necessary */
  670. if (flag)
  671. enable_interrupts ();
  672. start = get_timer (0);
  673. /* data polling for D7 */
  674. while (res == 0
  675. && (*dest & (FPW) 0x00800080) != (data & (FPW) 0x00800080)) {
  676. if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
  677. *dest = (FPW) 0x00F000F0; /* reset bank */
  678. res = 1;
  679. }
  680. }
  681. return (res);
  682. }
  683. void inline spin_wheel (void)
  684. {
  685. static int p = 0;
  686. static char w[] = "\\/-";
  687. printf ("\010%c", w[p]);
  688. (++p == 3) ? (p = 0) : 0;
  689. }
  690. /*-----------------------------------------------------------------------
  691. * Set/Clear sector's lock bit, returns:
  692. * 0 - OK
  693. * 1 - Error (timeout, voltage problems, etc.)
  694. */
  695. int flash_real_protect (flash_info_t * info, long sector, int prot)
  696. {
  697. ulong start;
  698. int i, j;
  699. int curr_bank;
  700. int bank;
  701. int rc = 0;
  702. FPWV *addr = (FPWV *) (info->start[sector]);
  703. int flag = disable_interrupts ();
  704. /*
  705. * 29F040B AMD flash does not support software protection/unprotection,
  706. * the only way to protect the AMD flash is marked it as prot bit.
  707. * This flash only support hardware protection, by supply or not supply
  708. * 12vpp to the flash
  709. */
  710. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD) {
  711. info->protect[sector] = prot;
  712. return 0;
  713. }
  714. *addr = INTEL_CLEAR; /* Clear status register */
  715. if (prot) { /* Set sector lock bit */
  716. *addr = INTEL_LOCKBIT; /* Sector lock bit */
  717. *addr = INTEL_PROTECT; /* set */
  718. } else { /* Clear sector lock bit */
  719. *addr = INTEL_LOCKBIT; /* All sectors lock bits */
  720. *addr = INTEL_CONFIRM; /* clear */
  721. }
  722. start = get_timer (0);
  723. while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
  724. if (get_timer (start) > CONFIG_SYS_FLASH_UNLOCK_TOUT) {
  725. printf ("Flash lock bit operation timed out\n");
  726. rc = 1;
  727. break;
  728. }
  729. }
  730. if (*addr != INTEL_OK) {
  731. printf ("Flash lock bit operation failed at %08X, CSR=%08X\n",
  732. (uint) addr, (uint) * addr);
  733. rc = 1;
  734. }
  735. if (!rc)
  736. info->protect[sector] = prot;
  737. /*
  738. * Clear lock bit command clears all sectors lock bits, so
  739. * we have to restore lock bits of protected sectors.
  740. */
  741. if (!prot) {
  742. /*
  743. * re-locking must be done for all banks that belong on one
  744. * FLASH chip, as all the sectors on the chip were unlocked
  745. * by INTEL_LOCKBIT/INTEL_CONFIRM commands. (let's hope
  746. * that banks never span chips, in particular chips which
  747. * support h/w protection differently).
  748. */
  749. /* find the current bank number */
  750. curr_bank = CONFIG_SYS_MAX_FLASH_BANKS + 1;
  751. for (j = 0; j < CONFIG_SYS_MAX_FLASH_BANKS; ++j) {
  752. if (&flash_info[j] == info) {
  753. curr_bank = j;
  754. }
  755. }
  756. if (curr_bank == CONFIG_SYS_MAX_FLASH_BANKS + 1) {
  757. printf("Error: can't determine bank number!\n");
  758. }
  759. for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
  760. if (!same_chip_banks(curr_bank, bank)) {
  761. continue;
  762. }
  763. info = &flash_info[bank];
  764. for (i = 0; i < info->sector_count; i++) {
  765. if (info->protect[i]) {
  766. start = get_timer (0);
  767. addr = (FPWV *) (info->start[i]);
  768. *addr = INTEL_LOCKBIT; /* Sector lock bit */
  769. *addr = INTEL_PROTECT; /* set */
  770. while ((*addr & INTEL_FINISHED) !=
  771. INTEL_FINISHED) {
  772. if (get_timer (start) >
  773. CONFIG_SYS_FLASH_UNLOCK_TOUT) {
  774. printf ("Flash lock bit operation timed out\n");
  775. rc = 1;
  776. break;
  777. }
  778. }
  779. }
  780. }
  781. }
  782. /*
  783. * get the s/w sector protection status in sync with the h/w,
  784. * in case something went wrong during the re-locking.
  785. */
  786. flash_sync_real_protect(info); /* resets flash to read mode */
  787. }
  788. if (flag)
  789. enable_interrupts ();
  790. *addr = INTEL_RESET; /* Reset to read array mode */
  791. return rc;
  792. }