flash.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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[CFG_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. /*-----------------------------------------------------------------------
  65. */
  66. unsigned long flash_init (void)
  67. {
  68. int i;
  69. ulong size = 0;
  70. ulong fsize = 0;
  71. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  72. memset (&flash_info[i], 0, sizeof (flash_info_t));
  73. switch (i) {
  74. case 0:
  75. flash_get_size ((FPW *) CFG_FLASH1_BASE,
  76. &flash_info[i]);
  77. flash_get_offsets (CFG_FLASH1_BASE, &flash_info[i]);
  78. break;
  79. case 1:
  80. flash_get_size ((FPW *) CFG_FLASH1_BASE,
  81. &flash_info[i]);
  82. fsize = CFG_FLASH1_BASE + flash_info[i - 1].size;
  83. flash_get_offsets (fsize, &flash_info[i]);
  84. break;
  85. case 2:
  86. flash_get_size ((FPW *) CFG_FLASH0_BASE,
  87. &flash_info[i]);
  88. flash_get_offsets (CFG_FLASH0_BASE, &flash_info[i]);
  89. break;
  90. case 3:
  91. flash_get_size ((FPW *) CFG_FLASH0_BASE,
  92. &flash_info[i]);
  93. fsize = CFG_FLASH0_BASE + flash_info[i - 1].size;
  94. flash_get_offsets (fsize, &flash_info[i]);
  95. break;
  96. default:
  97. panic ("configured to many flash banks!\n");
  98. break;
  99. }
  100. size += flash_info[i].size;
  101. }
  102. /* Protect monitor and environment sectors
  103. */
  104. #if defined (CFG_AMD_BOOT)
  105. flash_protect (FLAG_PROTECT_SET,
  106. CFG_MONITOR_BASE,
  107. CFG_MONITOR_BASE + monitor_flash_len - 1,
  108. &flash_info[2]);
  109. flash_protect (FLAG_PROTECT_SET,
  110. CFG_INTEL_BASE,
  111. CFG_INTEL_BASE + monitor_flash_len - 1,
  112. &flash_info[1]);
  113. #else
  114. flash_protect (FLAG_PROTECT_SET,
  115. CFG_MONITOR_BASE,
  116. CFG_MONITOR_BASE + monitor_flash_len - 1,
  117. &flash_info[3]);
  118. flash_protect (FLAG_PROTECT_SET,
  119. CFG_AMD_BASE,
  120. CFG_AMD_BASE + monitor_flash_len - 1, &flash_info[0]);
  121. #endif
  122. flash_protect (FLAG_PROTECT_SET,
  123. CFG_ENV1_ADDR,
  124. CFG_ENV1_ADDR + CFG_ENV1_SIZE - 1, &flash_info[1]);
  125. flash_protect (FLAG_PROTECT_SET,
  126. CFG_ENV_ADDR,
  127. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[3]);
  128. return size;
  129. }
  130. /*-----------------------------------------------------------------------
  131. */
  132. static void flash_get_offsets (ulong base, flash_info_t * info)
  133. {
  134. int i;
  135. if (info->flash_id == FLASH_UNKNOWN)
  136. return;
  137. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD) {
  138. for (i = 0; i < info->sector_count; i++) {
  139. info->start[i] = base + (i * PHYS_AMD_SECT_SIZE);
  140. info->protect[i] = 0;
  141. }
  142. }
  143. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  144. for (i = 0; i < info->sector_count; i++) {
  145. info->start[i] = base + (i * PHYS_INTEL_SECT_SIZE);
  146. info->protect[i] = 0;
  147. }
  148. }
  149. }
  150. /*-----------------------------------------------------------------------
  151. */
  152. void flash_print_info (flash_info_t * info)
  153. {
  154. int i;
  155. if (info->flash_id == FLASH_UNKNOWN) {
  156. printf ("missing or unknown FLASH type\n");
  157. return;
  158. }
  159. switch (info->flash_id & FLASH_VENDMASK) {
  160. case FLASH_MAN_INTEL:
  161. printf ("INTEL ");
  162. break;
  163. case FLASH_MAN_AMD:
  164. printf ("AMD ");
  165. break;
  166. default:
  167. printf ("Unknown Vendor ");
  168. break;
  169. }
  170. switch (info->flash_id & FLASH_TYPEMASK) {
  171. case FLASH_28F128J3A:
  172. printf ("28F128J3A\n");
  173. break;
  174. case FLASH_AM040:
  175. printf ("AMD29F040B\n");
  176. break;
  177. default:
  178. printf ("Unknown Chip Type\n");
  179. break;
  180. }
  181. printf (" Size: %ld MB in %d Sectors\n",
  182. info->size >> 20, info->sector_count);
  183. printf (" Sector Start Addresses:");
  184. for (i = 0; i < info->sector_count; ++i) {
  185. if ((i % 5) == 0)
  186. printf ("\n ");
  187. printf (" %08lX%s",
  188. info->start[i], info->protect[i] ? " (RO)" : " ");
  189. }
  190. printf ("\n");
  191. return;
  192. }
  193. /*
  194. * The following code cannot be run from FLASH!
  195. */
  196. static ulong flash_get_size (FPW * addr, flash_info_t * info)
  197. {
  198. FPWV value;
  199. static int amd = 0;
  200. /* Write auto select command: read Manufacturer ID */
  201. /* Write auto select command sequence and test FLASH answer */
  202. addr[FLASH_CYCLE1] = (FPW) 0x00AA00AA; /* for AMD, Intel ignores this */
  203. __asm__ ("sync");
  204. addr[FLASH_CYCLE2] = (FPW) 0x00550055; /* for AMD, Intel ignores this */
  205. __asm__ ("sync");
  206. addr[FLASH_CYCLE1] = (FPW) 0x00900090; /* selects Intel or AMD */
  207. __asm__ ("sync");
  208. udelay (100);
  209. switch (addr[0] & 0xff) {
  210. case (uchar) AMD_MANUFACT:
  211. info->flash_id = FLASH_MAN_AMD;
  212. value = addr[1];
  213. break;
  214. case (uchar) INTEL_MANUFACT:
  215. info->flash_id = FLASH_MAN_INTEL;
  216. value = addr[2];
  217. break;
  218. default:
  219. printf ("unknown\n");
  220. info->flash_id = FLASH_UNKNOWN;
  221. info->sector_count = 0;
  222. info->size = 0;
  223. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  224. return (0); /* no or unknown flash */
  225. }
  226. switch (value) {
  227. case (FPW) INTEL_ID_28F128J3A:
  228. info->flash_id += FLASH_28F128J3A;
  229. info->sector_count = 64;
  230. info->size = 0x00800000; /* => 16 MB */
  231. break;
  232. case (FPW) AMD_ID_LV040B:
  233. info->flash_id += FLASH_AM040;
  234. if (amd == 0) {
  235. info->sector_count = 7;
  236. info->size = 0x00070000; /* => 448 KB */
  237. amd = 1;
  238. } else {
  239. /* for Environment settings */
  240. info->sector_count = 1;
  241. info->size = PHYS_AMD_SECT_SIZE; /* => 64 KB */
  242. amd = 0;
  243. }
  244. break;
  245. default:
  246. info->flash_id = FLASH_UNKNOWN;
  247. break;
  248. }
  249. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  250. printf ("** ERROR: sector count %d > max (%d) **\n",
  251. info->sector_count, CFG_MAX_FLASH_SECT);
  252. info->sector_count = CFG_MAX_FLASH_SECT;
  253. }
  254. if (value == (FPW) INTEL_ID_28F128J3A)
  255. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  256. else
  257. addr[0] = (FPW) 0x00F000F0; /* restore read mode */
  258. return (info->size);
  259. }
  260. /*-----------------------------------------------------------------------
  261. */
  262. int flash_erase (flash_info_t * info, int s_first, int s_last)
  263. {
  264. int flag, prot, sect;
  265. ulong type, start, last;
  266. int rcode = 0, intel = 0;
  267. if ((s_first < 0) || (s_first > s_last)) {
  268. if (info->flash_id == FLASH_UNKNOWN)
  269. printf ("- missing\n");
  270. else
  271. printf ("- no sectors to erase\n");
  272. return 1;
  273. }
  274. type = (info->flash_id & FLASH_VENDMASK);
  275. if ((type != FLASH_MAN_INTEL)) {
  276. type = (info->flash_id & FLASH_VENDMASK);
  277. if ((type != FLASH_MAN_AMD)) {
  278. printf ("Can't erase unknown flash type %08lx - aborted\n",
  279. info->flash_id);
  280. return 1;
  281. }
  282. }
  283. if (type == FLASH_MAN_INTEL)
  284. intel = 1;
  285. prot = 0;
  286. for (sect = s_first; sect <= s_last; ++sect) {
  287. if (info->protect[sect]) {
  288. prot++;
  289. }
  290. }
  291. if (prot) {
  292. printf ("- Warning: %d protected sectors will not be erased!\n", prot);
  293. } else {
  294. printf ("\n");
  295. }
  296. start = get_timer (0);
  297. last = start;
  298. /* Disable interrupts which might cause a timeout here */
  299. flag = disable_interrupts ();
  300. /* Start erase on unprotected sectors */
  301. for (sect = s_first; sect <= s_last; sect++) {
  302. if (info->protect[sect] == 0) { /* not protected */
  303. FPWV *addr = (FPWV *) (info->start[sect]);
  304. FPW status;
  305. printf ("Erasing sector %2d ... ", sect);
  306. /* arm simple, non interrupt dependent timer */
  307. start = get_timer (0);
  308. if (intel) {
  309. *addr = (FPW) 0x00500050; /* clear status register */
  310. *addr = (FPW) 0x00200020; /* erase setup */
  311. *addr = (FPW) 0x00D000D0; /* erase confirm */
  312. } else {
  313. FPWV *base; /* first address in bank */
  314. base = (FPWV *) (CFG_AMD_BASE);
  315. base[FLASH_CYCLE1] = (FPW) 0x00AA00AA; /* unlock */
  316. base[FLASH_CYCLE2] = (FPW) 0x00550055; /* unlock */
  317. base[FLASH_CYCLE1] = (FPW) 0x00800080; /* erase mode */
  318. base[FLASH_CYCLE1] = (FPW) 0x00AA00AA; /* unlock */
  319. base[FLASH_CYCLE2] = (FPW) 0x00550055; /* unlock */
  320. *addr = (FPW) 0x00300030; /* erase sector */
  321. }
  322. while (((status =
  323. *addr) & (FPW) 0x00800080) !=
  324. (FPW) 0x00800080) {
  325. if (get_timer (start) > CFG_FLASH_ERASE_TOUT) {
  326. printf ("Timeout\n");
  327. if (intel) {
  328. *addr = (FPW) 0x00B000B0; /* suspend erase */
  329. *addr = (FPW) 0x00FF00FF; /* reset to read mode */
  330. } else
  331. *addr = (FPW) 0x00F000F0; /* reset to read mode */
  332. rcode = 1;
  333. break;
  334. }
  335. }
  336. if (intel) {
  337. *addr = (FPW) 0x00500050; /* clear status register cmd. */
  338. *addr = (FPW) 0x00FF00FF; /* resest to read mode */
  339. } else
  340. *addr = (FPW) 0x00F000F0; /* reset to read mode */
  341. printf (" done\n");
  342. }
  343. }
  344. return rcode;
  345. }
  346. /*-----------------------------------------------------------------------
  347. * Copy memory to flash, returns:
  348. * 0 - OK
  349. * 1 - write timeout
  350. * 2 - Flash not erased
  351. * 4 - Flash not identified
  352. */
  353. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  354. {
  355. if (info->flash_id == FLASH_UNKNOWN) {
  356. return 4;
  357. }
  358. switch (info->flash_id & FLASH_VENDMASK) {
  359. case FLASH_MAN_AMD:
  360. {
  361. FPW data = 0; /* 16 or 32 bit word, matches flash bus width */
  362. int bytes; /* number of bytes to program in current word */
  363. int left; /* number of bytes left to program */
  364. int i, res;
  365. for (left = cnt, res = 0;
  366. left > 0 && res == 0;
  367. addr += sizeof (data), left -=
  368. sizeof (data) - bytes) {
  369. bytes = addr & (sizeof (data) - 1);
  370. addr &= ~(sizeof (data) - 1);
  371. /* combine source and destination data so can program
  372. * an entire word of 16 or 32 bits
  373. */
  374. for (i = 0; i < sizeof (data); i++) {
  375. data <<= 8;
  376. if (i < bytes || i - bytes >= left)
  377. data += *((uchar *) addr + i);
  378. else
  379. data += *src++;
  380. }
  381. res = write_word_amd (info, (FPWV *) addr,
  382. data);
  383. }
  384. return res;
  385. } /* case FLASH_MAN_AMD */
  386. case FLASH_MAN_INTEL:
  387. {
  388. ulong cp, wp;
  389. FPW data;
  390. int count, i, l, rc, port_width;
  391. /* get lower word aligned address */
  392. wp = addr;
  393. port_width = 1;
  394. /*
  395. * handle unaligned start bytes
  396. */
  397. if ((l = addr - wp) != 0) {
  398. data = 0;
  399. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  400. data = (data << 8) | (*(uchar *) cp);
  401. }
  402. for (; i < port_width && cnt > 0; ++i) {
  403. data = (data << 8) | *src++;
  404. --cnt;
  405. ++cp;
  406. }
  407. for (; cnt == 0 && i < port_width; ++i, ++cp)
  408. data = (data << 8) | (*(uchar *) cp);
  409. if ((rc =
  410. write_data (info, wp, SWAP (data))) != 0)
  411. return (rc);
  412. wp += port_width;
  413. }
  414. if (cnt > WR_BLOCK) {
  415. /*
  416. * handle word aligned part
  417. */
  418. count = 0;
  419. while (cnt >= WR_BLOCK) {
  420. if ((rc =
  421. write_data_block (info,
  422. (ulong) src,
  423. wp)) != 0)
  424. return (rc);
  425. wp += WR_BLOCK;
  426. src += WR_BLOCK;
  427. cnt -= WR_BLOCK;
  428. if (count++ > 0x800) {
  429. spin_wheel ();
  430. count = 0;
  431. }
  432. }
  433. }
  434. if (cnt < WR_BLOCK) {
  435. /*
  436. * handle word aligned part
  437. */
  438. count = 0;
  439. while (cnt >= port_width) {
  440. data = 0;
  441. for (i = 0; i < port_width; ++i)
  442. data = (data << 8) | *src++;
  443. if ((rc =
  444. write_data (info, wp,
  445. SWAP (data))) != 0)
  446. return (rc);
  447. wp += port_width;
  448. cnt -= port_width;
  449. if (count++ > 0x800) {
  450. spin_wheel ();
  451. count = 0;
  452. }
  453. }
  454. }
  455. if (cnt == 0)
  456. return (0);
  457. /*
  458. * handle unaligned tail bytes
  459. */
  460. data = 0;
  461. for (i = 0, cp = wp; i < port_width && cnt > 0;
  462. ++i, ++cp) {
  463. data = (data << 8) | *src++;
  464. --cnt;
  465. }
  466. for (; i < port_width; ++i, ++cp)
  467. data = (data << 8) | (*(uchar *) cp);
  468. return (write_data (info, wp, SWAP (data)));
  469. } /* case FLASH_MAN_INTEL */
  470. } /* switch */
  471. return (0);
  472. }
  473. /*-----------------------------------------------------------------------
  474. * Write a word or halfword to Flash, returns:
  475. * 0 - OK
  476. * 1 - write timeout
  477. * 2 - Flash not erased
  478. */
  479. static int write_data (flash_info_t * info, ulong dest, FPW data)
  480. {
  481. FPWV *addr = (FPWV *) dest;
  482. ulong start;
  483. int flag;
  484. /* Check if Flash is (sufficiently) erased */
  485. if ((*addr & data) != data) {
  486. printf ("not erased at %08lx (%lx)\n", (ulong) addr, *addr);
  487. return (2);
  488. }
  489. /* Disable interrupts which might cause a timeout here */
  490. flag = disable_interrupts ();
  491. *addr = (FPW) 0x00400040; /* write setup */
  492. *addr = data;
  493. /* arm simple, non interrupt dependent timer */
  494. start = get_timer (0);
  495. /* wait while polling the status register */
  496. while ((*addr & (FPW) 0x00800080) != (FPW) 0x00800080) {
  497. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  498. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  499. return (1);
  500. }
  501. }
  502. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  503. return (0);
  504. }
  505. /*-----------------------------------------------------------------------
  506. * Write a word or halfword to Flash, returns:
  507. * 0 - OK
  508. * 1 - write timeout
  509. * 2 - Flash not erased
  510. */
  511. static int write_data_block (flash_info_t * info, ulong src, ulong dest)
  512. {
  513. FPWV *srcaddr = (FPWV *) src;
  514. FPWV *dstaddr = (FPWV *) dest;
  515. ulong start;
  516. int flag, i;
  517. /* Check if Flash is (sufficiently) erased */
  518. for (i = 0; i < WR_BLOCK; i++)
  519. if ((*dstaddr++ & 0xff) != 0xff) {
  520. printf ("not erased at %08lx (%lx)\n",
  521. (ulong) dstaddr, *dstaddr);
  522. return (2);
  523. }
  524. dstaddr = (FPWV *) dest;
  525. /* Disable interrupts which might cause a timeout here */
  526. flag = disable_interrupts ();
  527. *dstaddr = (FPW) 0x00e800e8; /* write block setup */
  528. /* arm simple, non interrupt dependent timer */
  529. start = get_timer (0);
  530. /* wait while polling the status register */
  531. while ((*dstaddr & (FPW) 0x00800080) != (FPW) 0x00800080) {
  532. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  533. *dstaddr = (FPW) 0x00FF00FF; /* restore read mode */
  534. return (1);
  535. }
  536. }
  537. *dstaddr = (FPW) 0x001f001f; /* write 32 to buffer */
  538. for (i = 0; i < WR_BLOCK; i++)
  539. *dstaddr++ = *srcaddr++;
  540. dstaddr -= 1;
  541. *dstaddr = (FPW) 0x00d000d0; /* write 32 to buffer */
  542. /* arm simple, non interrupt dependent timer */
  543. start = get_timer (0);
  544. /* wait while polling the status register */
  545. while ((*dstaddr & (FPW) 0x00800080) != (FPW) 0x00800080) {
  546. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  547. *dstaddr = (FPW) 0x00FF00FF; /* restore read mode */
  548. return (1);
  549. }
  550. }
  551. *dstaddr = (FPW) 0x00FF00FF; /* restore read mode */
  552. return (0);
  553. }
  554. /*-----------------------------------------------------------------------
  555. * Write a word to Flash for AMD FLASH
  556. * A word is 16 or 32 bits, whichever the bus width of the flash bank
  557. * (not an individual chip) is.
  558. *
  559. * returns:
  560. * 0 - OK
  561. * 1 - write timeout
  562. * 2 - Flash not erased
  563. */
  564. static int write_word_amd (flash_info_t * info, FPWV * dest, FPW data)
  565. {
  566. ulong start;
  567. int flag;
  568. int res = 0; /* result, assume success */
  569. FPWV *base; /* first address in flash bank */
  570. /* Check if Flash is (sufficiently) erased */
  571. if ((*dest & data) != data) {
  572. return (2);
  573. }
  574. base = (FPWV *) (CFG_AMD_BASE);
  575. /* Disable interrupts which might cause a timeout here */
  576. flag = disable_interrupts ();
  577. base[FLASH_CYCLE1] = (FPW) 0x00AA00AA; /* unlock */
  578. base[FLASH_CYCLE2] = (FPW) 0x00550055; /* unlock */
  579. base[FLASH_CYCLE1] = (FPW) 0x00A000A0; /* selects program mode */
  580. *dest = data; /* start programming the data */
  581. /* re-enable interrupts if necessary */
  582. if (flag)
  583. enable_interrupts ();
  584. start = get_timer (0);
  585. /* data polling for D7 */
  586. while (res == 0
  587. && (*dest & (FPW) 0x00800080) != (data & (FPW) 0x00800080)) {
  588. if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
  589. *dest = (FPW) 0x00F000F0; /* reset bank */
  590. res = 1;
  591. }
  592. }
  593. return (res);
  594. }
  595. void inline spin_wheel (void)
  596. {
  597. static int p = 0;
  598. static char w[] = "\\/-";
  599. printf ("\010%c", w[p]);
  600. (++p == 3) ? (p = 0) : 0;
  601. }
  602. /*-----------------------------------------------------------------------
  603. * Set/Clear sector's lock bit, returns:
  604. * 0 - OK
  605. * 1 - Error (timeout, voltage problems, etc.)
  606. */
  607. int flash_real_protect (flash_info_t * info, long sector, int prot)
  608. {
  609. ulong start;
  610. int i;
  611. int rc = 0;
  612. FPWV *addr = (FPWV *) (info->start[sector]);
  613. int flag = disable_interrupts ();
  614. /*
  615. * 29F040B AMD flash does not support software protection/unprotection,
  616. * the only way to protect the AMD flash is marked it as prot bit.
  617. * This flash only support hardware protection, by supply or not supply
  618. * 12vpp to the flash
  619. */
  620. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD) {
  621. info->protect[sector] = prot;
  622. return 0;
  623. }
  624. *addr = INTEL_CLEAR; /* Clear status register */
  625. if (prot) { /* Set sector lock bit */
  626. *addr = INTEL_LOCKBIT; /* Sector lock bit */
  627. *addr = INTEL_PROTECT; /* set */
  628. } else { /* Clear sector lock bit */
  629. *addr = INTEL_LOCKBIT; /* All sectors lock bits */
  630. *addr = INTEL_CONFIRM; /* clear */
  631. }
  632. start = get_timer (0);
  633. while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
  634. if (get_timer (start) > CFG_FLASH_UNLOCK_TOUT) {
  635. printf ("Flash lock bit operation timed out\n");
  636. rc = 1;
  637. break;
  638. }
  639. }
  640. if (*addr != INTEL_OK) {
  641. printf ("Flash lock bit operation failed at %08X, CSR=%08X\n",
  642. (uint) addr, (uint) * addr);
  643. rc = 1;
  644. }
  645. if (!rc)
  646. info->protect[sector] = prot;
  647. /*
  648. * Clear lock bit command clears all sectors lock bits, so
  649. * we have to restore lock bits of protected sectors.
  650. */
  651. if (!prot) {
  652. for (i = 0; i < info->sector_count; i++) {
  653. if (info->protect[i]) {
  654. start = get_timer (0);
  655. addr = (FPWV *) (info->start[i]);
  656. *addr = INTEL_LOCKBIT; /* Sector lock bit */
  657. *addr = INTEL_PROTECT; /* set */
  658. while ((*addr & INTEL_FINISHED) !=
  659. INTEL_FINISHED) {
  660. if (get_timer (start) >
  661. CFG_FLASH_UNLOCK_TOUT) {
  662. printf ("Flash lock bit operation timed out\n");
  663. rc = 1;
  664. break;
  665. }
  666. }
  667. }
  668. }
  669. }
  670. if (flag)
  671. enable_interrupts ();
  672. *addr = INTEL_RESET; /* Reset to read array mode */
  673. return rc;
  674. }