flash.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. * (C) Copyright 2001
  3. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  4. *
  5. * (C) Copyright 2001-2006
  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. #include <flash.h>
  29. //###CHD: gehört wieder raus ist vom PXA271 file!
  30. #define FLASH_28F256L18T 0x00B0 /* Intel 28F256L18T ( 32M = 128K x 255 + 32k x 4) */
  31. #define FLASH_28F256L18B 0x00B1 /* Intel 28F256L18B ( 32M = 32k x 4 + 128K x 255) */
  32. #define FLASH_28F256P30B 0x001C /* Intel 28F256P30B ( 32M = 32k x 4 + 128K x 255) */
  33. #define FLASH_28F256P30T 0x0019 /* Intel 28F256P30T ( 128K x 255 + 32M = 32k x 4 + */
  34. #define INTEL_ID_28F016S 0x66a066a0 /* 28F016S[VS] ID (16M = 512k x 16) */
  35. #define INTEL_ID_28F800B3T 0x88928892 /* 8M = 512K x 16 top boot sector */
  36. #define INTEL_ID_28F800B3B 0x88938893 /* 8M = 512K x 16 bottom boot sector */
  37. #define INTEL_ID_28F160B3T 0x88908890 /* 16M = 1M x 16 top boot sector */
  38. #define INTEL_ID_28F160B3B 0x88918891 /* 16M = 1M x 16 bottom boot sector */
  39. #define INTEL_ID_28F320B3T 0x88968896 /* 32M = 2M x 16 top boot sector */
  40. #define INTEL_ID_28F320B3B 0x88978897 /* 32M = 2M x 16 bottom boot sector */
  41. #define INTEL_ID_28F640B3T 0x88988898 /* 64M = 4M x 16 top boot sector */
  42. #define INTEL_ID_28F640B3B 0x88998899 /* 64M = 4M x 16 bottom boot sector */
  43. #define INTEL_ID_28F160F3B 0x88F488F4 /* 16M = 1M x 16 bottom boot sector */
  44. #define INTEL_ID_28F800C3T 0x88C088C0 /* 8M = 512K x 16 top boot sector */
  45. #define INTEL_ID_28F800C3B 0x88C188C1 /* 8M = 512K x 16 bottom boot sector */
  46. #define INTEL_ID_28F160C3T 0x88C288C2 /* 16M = 1M x 16 top boot sector */
  47. #define INTEL_ID_28F160C3B 0x88C388C3 /* 16M = 1M x 16 bottom boot sector */
  48. #define INTEL_ID_28F320C3T 0x88C488C4 /* 32M = 2M x 16 top boot sector */
  49. #define INTEL_ID_28F320C3B 0x88C588C5 /* 32M = 2M x 16 bottom boot sector */
  50. #define INTEL_ID_28F640C3T 0x88CC88CC /* 64M = 4M x 16 top boot sector */
  51. #define INTEL_ID_28F640C3B 0x88CD88CD /* 64M = 4M x 16 bottom boot sector */
  52. #define INTEL_ID_28F128J3 0x89188918 /* 16M = 8M x 16 x 128 */
  53. #define INTEL_ID_28F320J5 0x00140014 /* 32M = 128K x 32 */
  54. #define INTEL_ID_28F640J5 0x00150015 /* 64M = 128K x 64 */
  55. #define INTEL_ID_28F320J3A 0x00160016 /* 32M = 128K x 32 */
  56. #define INTEL_ID_28F640J3A 0x00170017 /* 64M = 128K x 64 */
  57. #define INTEL_ID_28F256L18 0x88108810 /* 32M = 128K x 255 */
  58. #define INTEL_ID_28F128J3A 0x00180018 /* 128M = 128K x 128 */
  59. #define INTEL_ID_28F256L18T 0x880D880D /* 256M = 128K x 255 + 32k x 4 */
  60. #define INTEL_ID_28F256L18B 0x88108810 /* 256M = 32k x 4 + 128K x 255 */
  61. #define INTEL_ID_28F256P30B 0x891C891C /* 256M = 32k x 4 + 128K x 255 */
  62. #define INTEL_ID_28F256P30T 0x89198919 /* 256M = 128K x 255 + 32k x 4 */
  63. #define INTEL_ID_28F64K3 0x88018801 /* 64M = 32K x 255 + 32k x 4 */
  64. #define INTEL_ID_28F128K3 0x88028802 /* 128M = 64K x 255 + 32k x 4 */
  65. #define INTEL_ID_28F256K3 0x88038803 /* 256M = 128K x 255 + 32k x 4 */
  66. #define INTEL_ID_28F160S3 0x00D000D0 /* 16M = 512K x 32 (64kB x 32) */
  67. #define INTEL_ID_28F320S3 0x00D400D4 /* 32M = 512K x 64 (64kB x 64) */
  68. flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
  69. /* Board support for 1 or 2 flash devices */
  70. #define FLASH_PORT_WIDTH32
  71. #undef FLASH_PORT_WIDTH16
  72. #ifdef FLASH_PORT_WIDTH16
  73. #define FLASH_PORT_WIDTH ushort
  74. #define FLASH_PORT_WIDTHV vu_short
  75. #define SWAP(x) (x)
  76. #else
  77. #define FLASH_PORT_WIDTH ulong
  78. #define FLASH_PORT_WIDTHV vu_long
  79. #define SWAP(x) (x)
  80. #endif
  81. /* Intel-compatible flash ID */
  82. #define INTEL_COMPAT 0x00890089
  83. #define INTEL_ALT 0x00B000B0
  84. /* Intel-compatible flash commands */
  85. #define INTEL_PROGRAM 0x00100010
  86. #define INTEL_ERASE 0x00200020
  87. #define INTEL_CLEAR 0x00500050
  88. #define INTEL_LOCKBIT 0x00600060
  89. #define INTEL_PROTECT 0x00010001
  90. #define INTEL_STATUS 0x00700070
  91. #define INTEL_READID 0x00900090
  92. #define INTEL_CONFIRM 0x00D000D0
  93. #define INTEL_RESET 0xFFFFFFFF
  94. /* Intel-compatible flash status bits */
  95. #define INTEL_FINISHED 0x00800080
  96. #define INTEL_OK 0x00800080
  97. #define FPW FLASH_PORT_WIDTH
  98. #define FPWV FLASH_PORT_WIDTHV
  99. #define mb() __asm__ __volatile__ ("" : : : "memory")
  100. ///###CHD: auch wieder raus! + switch anweisung für den P30iger!
  101. /* Flash Organization Structure */
  102. typedef struct OrgDef {
  103. unsigned int sector_number;
  104. unsigned int sector_size;
  105. } OrgDef;
  106. /* Flash Organizations */
  107. OrgDef OrgIntel_28F256L18B[] = {
  108. {4, 0x8000}, /* 4 * 32kBytes sectors */
  109. {255, 0x20000}, /* 255 * 128kBytes sectors */
  110. };
  111. OrgDef OrgIntel_28F256L18T[] = {
  112. {255, 0x20000}, /* 255 * 128kBytes sectors */
  113. {4, 0x8000} /* 4 * 32kBytes sectors */
  114. };
  115. OrgDef OrgIntel_28F128J3A[] = {
  116. {128, 0x20000}, /* 128 * 128kBytes sectors */
  117. };
  118. static unsigned int OrgSectTypeNum_28F256L18 = 2;
  119. static unsigned int OrgSectTypeNum_28F256J3 = 1;
  120. /*-----------------------------------------------------------------------
  121. * Functions
  122. */
  123. static ulong flash_get_size (FPW *addr, flash_info_t *info);
  124. static int write_data (flash_info_t *info, ulong dest, FPW data);
  125. static void flash_get_offsets (ulong base, flash_info_t *info);
  126. void inline spin_wheel (void);
  127. static void flash_sync_real_protect (flash_info_t * info);
  128. static unsigned char intel_sector_protected (flash_info_t *info, ushort sector);
  129. /*-----------------------------------------------------------------------
  130. */
  131. unsigned long flash_init (void)
  132. {
  133. int i;
  134. ulong size = 0;
  135. extern void flash_preinit(void);
  136. extern void flash_afterinit(ulong, ulong);
  137. ulong flashbase = CFG_FLASH_BASE;
  138. flash_preinit();
  139. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  140. switch (i) {
  141. case 0:
  142. memset(&flash_info[i], 0, sizeof(flash_info_t));
  143. flash_get_size ((FPW *) flashbase, &flash_info[i]);
  144. flash_get_offsets (flash_info[i].start[0], &flash_info[i]);
  145. break;
  146. default:
  147. panic ("configured to many flash banks!\n");
  148. break;
  149. }
  150. size += flash_info[i].size;
  151. /* get the h/w and s/w protection status in sync */
  152. flash_sync_real_protect(&flash_info[i]);
  153. }
  154. /* Protect monitor and environment sectors
  155. */
  156. #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
  157. #ifndef CONFIG_BOOT_ROM
  158. flash_protect ( FLAG_PROTECT_SET,
  159. CFG_MONITOR_BASE,
  160. CFG_MONITOR_BASE + monitor_flash_len - 1,
  161. &flash_info[0] );
  162. #endif
  163. #endif
  164. #ifdef CFG_ENV_IS_IN_FLASH
  165. flash_protect ( FLAG_PROTECT_SET,
  166. CFG_ENV_ADDR,
  167. CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0] );
  168. #endif
  169. flash_afterinit(flash_info[0].start[0], flash_info[0].size);
  170. return size;
  171. }
  172. /*-----------------------------------------------------------------------
  173. */
  174. static void flash_get_offsets (ulong base, flash_info_t *info)
  175. {
  176. int i;
  177. if (info->flash_id == FLASH_UNKNOWN) {
  178. return;
  179. }
  180. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  181. for (i = 0; i < info->sector_count; i++) {
  182. info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
  183. }
  184. }
  185. }
  186. /*-----------------------------------------------------------------------
  187. */
  188. void flash_print_info (flash_info_t *info)
  189. {
  190. int i;
  191. if (info->flash_id == FLASH_UNKNOWN) {
  192. printf ("missing or unknown FLASH type\n");
  193. return;
  194. }
  195. switch (info->flash_id & FLASH_VENDMASK) {
  196. case FLASH_MAN_INTEL:
  197. printf ("INTEL ");
  198. break;
  199. default:
  200. printf ("Unknown Vendor ");
  201. break;
  202. }
  203. switch (info->flash_id & FLASH_TYPEMASK) {
  204. case FLASH_28F128J3A:
  205. printf ("28F128J3A\n");
  206. break;
  207. case FLASH_28F640J3A:
  208. printf ("28F640J3A\n");
  209. break;
  210. case FLASH_28F320J3A:
  211. printf ("28F320J3A\n");
  212. break;
  213. case FLASH_28F256P30T:
  214. printf ("FLASH_28F256P30T\n");
  215. break;
  216. default:
  217. printf ("Unknown Chip Type\n");
  218. break;
  219. }
  220. printf (" Size: %ld MB in %d Sectors\n",
  221. info->size >> 20, info->sector_count);
  222. printf (" Sector Start Addresses:");
  223. for (i = 0; i < info->sector_count; ++i) {
  224. if ((i % 5) == 0)
  225. printf ("\n ");
  226. printf (" %08lX%s",
  227. info->start[i],
  228. info->protect[i] ? " (RO)" : " ");
  229. }
  230. printf ("\n");
  231. return;
  232. }
  233. /*
  234. * The following code cannot be run from FLASH!
  235. */
  236. static ulong flash_get_size (FPW *addr, flash_info_t *info)
  237. {
  238. volatile FPW value;
  239. /* Write auto select command: read Manufacturer ID */
  240. addr[0x5555] = (FPW) 0x00AA00AA;
  241. addr[0x2AAA] = (FPW) 0x00550055;
  242. addr[0x5555] = (FPW) 0x00900090;
  243. mb ();
  244. udelay(100);
  245. value = addr[0];
  246. switch (value) {
  247. case (FPW) INTEL_MANUFACT:
  248. info->flash_id = FLASH_MAN_INTEL;
  249. break;
  250. default:
  251. info->flash_id = FLASH_UNKNOWN;
  252. info->sector_count = 0;
  253. info->size = 0;
  254. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  255. return (0); /* no or unknown flash */
  256. }
  257. mb ();
  258. value = addr[1]; /* device ID */
  259. switch (value) {
  260. case (FPW) INTEL_ID_28F128J3A:
  261. info->flash_id += FLASH_28F128J3A;
  262. info->sector_count = 128;
  263. info->size = 0x02000000;
  264. info->start[0] = CFG_FLASH_BASE;
  265. break; /* => 32 MB */
  266. case (FPW) INTEL_ID_28F640J3A:
  267. info->flash_id += FLASH_28F640J3A;
  268. info->sector_count = 64;
  269. info->size = 0x01000000;
  270. info->start[0] = CFG_FLASH_BASE + 0x01000000;
  271. break; /* => 16 MB */
  272. case (FPW) (INTEL_ID_28F256P30T):
  273. info->flash_id += FLASH_28F256L18B;
  274. info->sector_count = 128;//falsch!!!
  275. info->size = 0x02000000;
  276. info->start[0] = CFG_FLASH_BASE;
  277. debug ("Intel StrataFlash 28F256P30T device initialized\n");
  278. break; /* => 32 MB */
  279. case (FPW) INTEL_ID_28F320J3A:
  280. info->flash_id += FLASH_28F320J3A;
  281. info->sector_count = 32;
  282. info->size = 0x800000;
  283. info->start[0] = CFG_FLASH_BASE + 0x01800000;
  284. break; /* => 8 MB */
  285. default:
  286. info->flash_id = FLASH_UNKNOWN;
  287. break;
  288. }
  289. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  290. printf ("** ERROR: sector count %d > max (%d) **\n",
  291. info->sector_count, CFG_MAX_FLASH_SECT);
  292. info->sector_count = CFG_MAX_FLASH_SECT;
  293. }
  294. addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
  295. return (info->size);
  296. }
  297. /*
  298. * This function gets the u-boot flash sector protection status
  299. * (flash_info_t.protect[]) in sync with the sector protection
  300. * status stored in hardware.
  301. */
  302. static void flash_sync_real_protect (flash_info_t * info)
  303. {
  304. int i;
  305. switch (info->flash_id & FLASH_TYPEMASK) {
  306. case FLASH_28F128J3A:
  307. case FLASH_28F640J3A:
  308. case FLASH_28F320J3A:
  309. for (i = 0; i < info->sector_count; ++i) {
  310. info->protect[i] = intel_sector_protected(info, i);
  311. }
  312. break;
  313. default:
  314. /* no h/w protect support */
  315. break;
  316. }
  317. }
  318. /*
  319. * checks if "sector" in bank "info" is protected. Should work on intel
  320. * strata flash chips 28FxxxJ3x in 8-bit mode.
  321. * Returns 1 if sector is protected (or timed-out while trying to read
  322. * protection status), 0 if it is not.
  323. */
  324. static unsigned char intel_sector_protected (flash_info_t *info, ushort sector)
  325. {
  326. FPWV *addr;
  327. FPWV *lock_conf_addr;
  328. ulong start;
  329. unsigned char ret;
  330. /*
  331. * first, wait for the WSM to be finished. The rationale for
  332. * waiting for the WSM to become idle for at most
  333. * CFG_FLASH_ERASE_TOUT is as follows. The WSM can be busy
  334. * because of: (1) erase, (2) program or (3) lock bit
  335. * configuration. So we just wait for the longest timeout of
  336. * the (1)-(3), i.e. the erase timeout.
  337. */
  338. /* wait at least 35ns (W12) before issuing Read Status Register */
  339. udelay(1);
  340. addr = (FPWV *) info->start[sector];
  341. *addr = (FPW) INTEL_STATUS;
  342. start = get_timer (0);
  343. while ((*addr & (FPW) INTEL_FINISHED) != (FPW) INTEL_FINISHED) {
  344. if (get_timer (start) > CFG_FLASH_ERASE_TOUT) {
  345. *addr = (FPW) INTEL_RESET; /* restore read mode */
  346. printf("WSM busy too long, can't get prot status\n");
  347. return 1;
  348. }
  349. }
  350. /* issue the Read Identifier Codes command */
  351. *addr = (FPW) INTEL_READID;
  352. /* wait at least 35ns (W12) before reading */
  353. udelay(1);
  354. /* Intel example code uses offset of 2 for 16 bit flash */
  355. lock_conf_addr = (FPWV *) info->start[sector] + 2;
  356. ret = (*lock_conf_addr & (FPW) INTEL_PROTECT) ? 1 : 0;
  357. /* put flash back in read mode */
  358. *addr = (FPW) INTEL_RESET;
  359. return ret;
  360. }
  361. /*-----------------------------------------------------------------------
  362. */
  363. int flash_erase (flash_info_t *info, int s_first, int s_last)
  364. {
  365. int flag, prot, sect;
  366. ulong type, start, last;
  367. int rcode = 0;
  368. if ((s_first < 0) || (s_first > s_last)) {
  369. if (info->flash_id == FLASH_UNKNOWN) {
  370. printf ("- missing\n");
  371. } else {
  372. printf ("- no sectors to erase\n");
  373. }
  374. return 1;
  375. }
  376. type = (info->flash_id & FLASH_VENDMASK);
  377. if ((type != FLASH_MAN_INTEL)) {
  378. printf ("Can't erase unknown flash type %08lx - aborted\n",
  379. info->flash_id);
  380. return 1;
  381. }
  382. prot = 0;
  383. for (sect = s_first; sect <= s_last; ++sect) {
  384. if (info->protect[sect]) {
  385. prot++;
  386. }
  387. }
  388. if (prot) {
  389. printf ("- Warning: %d protected sectors will not be erased!\n",
  390. prot);
  391. } else {
  392. printf ("\n");
  393. }
  394. start = get_timer (0);
  395. last = start;
  396. /* Disable interrupts which might cause a timeout here */
  397. flag = disable_interrupts ();
  398. /* Start erase on unprotected sectors */
  399. for (sect = s_first; sect <= s_last; sect++) {
  400. if (info->protect[sect] == 0) { /* not protected */
  401. FPWV *addr = (FPWV *) (info->start[sect]);
  402. FPW status;
  403. printf ("Erasing sector %2d ... ", sect);
  404. /* arm simple, non interrupt dependent timer */
  405. start = get_timer(0);
  406. *addr = (FPW) 0x00500050; /* clear status register */
  407. *addr = (FPW) 0x00200020; /* erase setup */
  408. *addr = (FPW) 0x00D000D0; /* erase confirm */
  409. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  410. if (get_timer(start) > CFG_FLASH_ERASE_TOUT) {
  411. printf ("Timeout\n");
  412. *addr = (FPW) 0x00B000B0; /* suspend erase */
  413. *addr = (FPW) 0x00FF00FF; /* reset to read mode */
  414. rcode = 1;
  415. break;
  416. }
  417. }
  418. *addr = 0x00500050; /* clear status register cmd. */
  419. *addr = 0x00FF00FF; /* resest to read mode */
  420. printf (" done\n");
  421. }
  422. }
  423. return rcode;
  424. }
  425. /*-----------------------------------------------------------------------
  426. * Copy memory to flash, returns:
  427. * 0 - OK
  428. * 1 - write timeout
  429. * 2 - Flash not erased
  430. * 4 - Flash not identified
  431. */
  432. int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  433. {
  434. ulong cp, wp;
  435. FPW data;
  436. int count, i, l, rc, port_width;
  437. if (info->flash_id == FLASH_UNKNOWN) {
  438. return 4;
  439. }
  440. /* get lower word aligned address */
  441. #ifdef FLASH_PORT_WIDTH16
  442. wp = (addr & ~1);
  443. port_width = 2;
  444. #else
  445. wp = (addr & ~3);
  446. port_width = 4;
  447. #endif
  448. /*
  449. * handle unaligned start bytes
  450. */
  451. if ((l = addr - wp) != 0) {
  452. data = 0;
  453. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  454. data = (data << 8) | (*(uchar *) cp);
  455. }
  456. for (; i < port_width && cnt > 0; ++i) {
  457. data = (data << 8) | *src++;
  458. --cnt;
  459. ++cp;
  460. }
  461. for (; cnt == 0 && i < port_width; ++i, ++cp) {
  462. data = (data << 8) | (*(uchar *) cp);
  463. }
  464. if ((rc = write_data (info, wp, SWAP (data))) != 0) {
  465. return (rc);
  466. }
  467. wp += port_width;
  468. }
  469. /*
  470. * handle word aligned part
  471. */
  472. count = 0;
  473. while (cnt >= port_width) {
  474. data = 0;
  475. for (i = 0; i < port_width; ++i) {
  476. data = (data << 8) | *src++;
  477. }
  478. if ((rc = write_data (info, wp, SWAP (data))) != 0) {
  479. return (rc);
  480. }
  481. wp += port_width;
  482. cnt -= port_width;
  483. if (count++ > 0x800) {
  484. spin_wheel ();
  485. count = 0;
  486. }
  487. }
  488. if (cnt == 0) {
  489. return (0);
  490. }
  491. /*
  492. * handle unaligned tail bytes
  493. */
  494. data = 0;
  495. for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
  496. data = (data << 8) | *src++;
  497. --cnt;
  498. }
  499. for (; i < port_width; ++i, ++cp) {
  500. data = (data << 8) | (*(uchar *) cp);
  501. }
  502. return (write_data (info, wp, SWAP (data)));
  503. }
  504. /*-----------------------------------------------------------------------
  505. * Write a word or halfword to Flash, returns:
  506. * 0 - OK
  507. * 1 - write timeout
  508. * 2 - Flash not erased
  509. */
  510. static int write_data (flash_info_t *info, ulong dest, FPW data)
  511. {
  512. FPWV *addr = (FPWV *) dest;
  513. ulong status;
  514. ulong start;
  515. int flag;
  516. /* Check if Flash is (sufficiently) erased */
  517. if ((*addr & data) != data) {
  518. printf ("not erased at %08lx (%lx)\n", (ulong) addr, *addr);
  519. return (2);
  520. }
  521. /* Disable interrupts which might cause a timeout here */
  522. flag = disable_interrupts ();
  523. *addr = (FPW) 0x00400040; /* write setup */
  524. *addr = data;
  525. /* arm simple, non interrupt dependent timer */
  526. start = get_timer(0);
  527. /* wait while polling the status register */
  528. while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
  529. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  530. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  531. return (1);
  532. }
  533. }
  534. *addr = (FPW) 0x00FF00FF; /* restore read mode */
  535. return (0);
  536. }
  537. void inline spin_wheel (void)
  538. {
  539. static int p = 0;
  540. static char w[] = "\\/-";
  541. printf ("\010%c", w[p]);
  542. (++p == 3) ? (p = 0) : 0;
  543. }
  544. /*-----------------------------------------------------------------------
  545. * Set/Clear sector's lock bit, returns:
  546. * 0 - OK
  547. * 1 - Error (timeout, voltage problems, etc.)
  548. */
  549. int flash_real_protect (flash_info_t *info, long sector, int prot)
  550. {
  551. ulong start;
  552. int i;
  553. int rc = 0;
  554. vu_long *addr = (vu_long *)(info->start[sector]);
  555. int flag = disable_interrupts();
  556. *addr = INTEL_CLEAR; /* Clear status register */
  557. if (prot) { /* Set sector lock bit */
  558. *addr = INTEL_LOCKBIT; /* Sector lock bit */
  559. *addr = INTEL_PROTECT; /* set */
  560. }
  561. else { /* Clear sector lock bit */
  562. *addr = INTEL_LOCKBIT; /* All sectors lock bits */
  563. *addr = INTEL_CONFIRM; /* clear */
  564. }
  565. start = get_timer(0);
  566. while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
  567. if (get_timer(start) > CFG_FLASH_UNLOCK_TOUT) {
  568. printf("Flash lock bit operation timed out\n");
  569. rc = 1;
  570. break;
  571. }
  572. }
  573. if (*addr != INTEL_OK) {
  574. printf("Flash lock bit operation failed at %08X, CSR=%08X\n",
  575. (uint)addr, (uint)*addr);
  576. rc = 1;
  577. }
  578. if (!rc)
  579. info->protect[sector] = prot;
  580. /*
  581. * Clear lock bit command clears all sectors lock bits, so
  582. * we have to restore lock bits of protected sectors.
  583. * WARNING: code below re-locks sectors only for one bank (info).
  584. * This causes problems on boards where several banks share
  585. * the same chip, as sectors in othere banks will be unlocked
  586. * but not re-locked. It works fine on pm520 though, as there
  587. * is only one chip and one bank.
  588. */
  589. if (!prot)
  590. {
  591. for (i = 0; i < info->sector_count; i++)
  592. {
  593. if (info->protect[i])
  594. {
  595. start = get_timer(0);
  596. addr = (vu_long *)(info->start[i]);
  597. *addr = INTEL_LOCKBIT; /* Sector lock bit */
  598. *addr = INTEL_PROTECT; /* set */
  599. while ((*addr & INTEL_FINISHED) != INTEL_FINISHED)
  600. {
  601. if (get_timer(start) > CFG_FLASH_UNLOCK_TOUT)
  602. {
  603. printf("Flash lock bit operation timed out\n");
  604. rc = 1;
  605. break;
  606. }
  607. }
  608. }
  609. }
  610. /*
  611. * get the s/w sector protection status in sync with the h/w,
  612. * in case something went wrong during the re-locking.
  613. */
  614. flash_sync_real_protect(info); /* resets flash to read mode */
  615. }
  616. if (flag)
  617. enable_interrupts();
  618. *addr = INTEL_RESET; /* Reset to read array mode */
  619. return rc;
  620. }