flash.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*
  2. * (C) Copyright 2000-2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
  6. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  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 <asm/immap.h>
  28. #ifndef CFG_FLASH_CFI
  29. typedef unsigned char FLASH_PORT_WIDTH;
  30. typedef volatile unsigned char FLASH_PORT_WIDTHV;
  31. #define FPW FLASH_PORT_WIDTH
  32. #define FPWV FLASH_PORT_WIDTHV
  33. #define CFG_FLASH_CFI_WIDTH FLASH_CFI_8BIT
  34. #define CFG_FLASH_NONCFI_WIDTH FLASH_CFI_8BIT
  35. /* Intel-compatible flash commands */
  36. #define INTEL_PROGRAM 0x00100010
  37. #define INTEL_ERASE 0x00200020
  38. #define INTEL_WRSETUP 0x00400040
  39. #define INTEL_CLEAR 0x00500050
  40. #define INTEL_LOCKBIT 0x00600060
  41. #define INTEL_PROTECT 0x00010001
  42. #define INTEL_STATUS 0x00700070
  43. #define INTEL_READID 0x00900090
  44. #define INTEL_CFIQRY 0x00980098
  45. #define INTEL_SUSERASE 0x00B000B0
  46. #define INTEL_PROTPROG 0x00C000C0
  47. #define INTEL_CONFIRM 0x00D000D0
  48. #define INTEL_WRBLK 0x00e800e8
  49. #define INTEL_RESET 0x00FF00FF
  50. /* Intel-compatible flash status bits */
  51. #define INTEL_FINISHED 0x00800080
  52. #define INTEL_OK 0x00800080
  53. #define INTEL_ERASESUS 0x00600060
  54. #define INTEL_WSM_SUS (INTEL_FINISHED | INTEL_ERASESUS)
  55. /* 28F160C3B CFI Data offset - This could vary */
  56. #define INTEL_CFI_MFG 0x00 /* Manufacturer ID */
  57. #define INTEL_CFI_PART 0x01 /* Product ID */
  58. #define INTEL_CFI_LOCK 0x02 /* */
  59. #define INTEL_CFI_TWPRG 0x1F /* Typical Single Word Program Timeout 2^n us */
  60. #define INTEL_CFI_MBUFW 0x20 /* Typical Max Buffer Write Timeout 2^n us */
  61. #define INTEL_CFI_TERB 0x21 /* Typical Block Erase Timeout 2^n ms */
  62. #define INTEL_CFI_MWPRG 0x23 /* Maximum Word program timeout 2^n us */
  63. #define INTEL_CFI_MERB 0x25 /* Maximum Block Erase Timeout 2^n s */
  64. #define INTEL_CFI_SIZE 0x27 /* Device size 2^n bytes */
  65. #define INTEL_CFI_CAP 0x28
  66. #define INTEL_CFI_WRBUF 0x2A
  67. #define INTEL_CFI_BANK 0x2C /* Number of Bank */
  68. #define INTEL_CFI_BLK1A 0x2D /* Number of Blocks */
  69. #define INTEL_CFI_BLK1B 0x2E /* Number of Blocks */
  70. #define INTEL_CFI_SZ1A 0x2F /* Block Region Size */
  71. #define INTEL_CFI_SZ1B 0x30
  72. #define INTEL_CFI_BLK2A 0x31
  73. #define INTEL_CFI_BLK2B 0x32
  74. #define INTEL_CFI_SZ2A 0x33
  75. #define INTEL_CFI_SZ2B 0x34
  76. #define FLASH_CYCLE1 0x0555
  77. #define FLASH_CYCLE2 0x0aaa
  78. #define WR_BLOCK 0x20
  79. /* not in the flash.h yet */
  80. #define FLASH_28F64P30T 0x00B9 /* Intel 28F64P30T ( 64M) */
  81. #define FLASH_28F64P30B 0x00BA /* Intel 28F64P30B ( 64M) */
  82. #define FLASH_28F128P30T 0x00BB /* Intel 28F128P30T ( 128M = 8M x 16 ) */
  83. #define FLASH_28F128P30B 0x00BC /* Intel 28F128P30B ( 128M = 8M x 16 ) */
  84. #define FLASH_28F256P30T 0x00BD /* Intel 28F256P30T ( 256M = 16M x 16 ) */
  85. #define FLASH_28F256P30B 0x00BE /* Intel 28F256P30B ( 256M = 16M x 16 ) */
  86. #define SYNC __asm__("nop")
  87. /*-----------------------------------------------------------------------
  88. * Functions
  89. */
  90. ulong flash_get_size(FPWV * addr, flash_info_t * info);
  91. int flash_get_offsets(ulong base, flash_info_t * info);
  92. int flash_cmd_rd(volatile u16 * addr, int index);
  93. int write_data(flash_info_t * info, ulong dest, FPW data);
  94. int write_data_block(flash_info_t * info, ulong src, ulong dest);
  95. int write_word_atm(flash_info_t * info, volatile u8 * dest, u16 data);
  96. void inline spin_wheel(void);
  97. void flash_sync_real_protect(flash_info_t * info);
  98. uchar intel_sector_protected(flash_info_t * info, ushort sector);
  99. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  100. ulong flash_init(void)
  101. {
  102. int i;
  103. ulong size = 0;
  104. ulong fbase = 0;
  105. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  106. memset(&flash_info[i], 0, sizeof(flash_info_t));
  107. switch (i) {
  108. case 0:
  109. fbase = (ulong) CFG_FLASH0_BASE;
  110. break;
  111. case 1:
  112. fbase = (ulong) CFG_FLASH1_BASE;
  113. break;
  114. }
  115. flash_get_size((FPWV *) fbase, &flash_info[i]);
  116. flash_get_offsets((ulong) fbase, &flash_info[i]);
  117. fbase += flash_info[i].size;
  118. size += flash_info[i].size;
  119. /* get the h/w and s/w protection status in sync */
  120. flash_sync_real_protect(&flash_info[i]);
  121. }
  122. /* Protect monitor and environment sectors */
  123. flash_protect(FLAG_PROTECT_SET,
  124. CFG_MONITOR_BASE,
  125. CFG_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]);
  126. return size;
  127. }
  128. int flash_get_offsets(ulong base, flash_info_t * info)
  129. {
  130. int i, j, k;
  131. int sectors, bs, banks;
  132. ulong start;
  133. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_ATM) {
  134. int sect[] = CFG_ATMEL_SECT;
  135. int sectsz[] = CFG_ATMEL_SECTSZ;
  136. info->start[0] = base;
  137. for (k = 0, i = 0; i < CFG_ATMEL_REGION; i++) {
  138. for (j = 0; j < sect[i]; j++, k++) {
  139. info->start[k + 1] = info->start[k] + sectsz[i];
  140. info->protect[k] = 0;
  141. }
  142. }
  143. }
  144. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  145. volatile u16 *addr16 = (volatile u16 *)base;
  146. *addr16 = (FPW) INTEL_RESET; /* restore read mode */
  147. *addr16 = (FPW) INTEL_READID;
  148. banks = addr16[INTEL_CFI_BANK] & 0xff;
  149. sectors = 0;
  150. info->start[0] = base;
  151. for (k = 0, i = 0; i < banks; i++) {
  152. /* Geometry y1 = y1 + 1, y2 = y2 + 1, CFI spec.
  153. * To be exact, Z = [0x2f 0x30] (LE) * 256 bytes * [0x2D 0x2E] block count
  154. * Z = [0x33 0x34] (LE) * 256 bytes * [0x31 0x32] block count
  155. */
  156. bs = ((((addr16[INTEL_CFI_SZ1B + (i * 4)] & 0xff) << 8)
  157. | (addr16[INTEL_CFI_SZ1A + (i * 4)] & 0xff)) *
  158. 0x100);
  159. sectors =
  160. (addr16[INTEL_CFI_BLK1A + (i * 4)] & 0xff) + 1;
  161. for (j = 0; j < sectors; j++, k++) {
  162. info->start[k + 1] = info->start[k] + bs;
  163. }
  164. }
  165. *addr16 = (FPW) INTEL_RESET; /* restore read mode */
  166. }
  167. return ERR_OK;
  168. }
  169. void flash_print_info(flash_info_t * info)
  170. {
  171. int i;
  172. switch (info->flash_id & FLASH_VENDMASK) {
  173. case FLASH_MAN_INTEL:
  174. printf("INTEL ");
  175. break;
  176. case FLASH_MAN_ATM:
  177. printf("ATMEL ");
  178. break;
  179. default:
  180. printf("Unknown Vendor ");
  181. break;
  182. }
  183. switch (info->flash_id & FLASH_TYPEMASK) {
  184. case FLASH_AT040:
  185. printf("AT49BV040A\n");
  186. break;
  187. case FLASH_28F128J3A:
  188. printf("Intel 28F128J3A\n");
  189. break;
  190. default:
  191. printf("Unknown Chip Type\n");
  192. return;
  193. }
  194. if (info->size > 0x100000) {
  195. int remainder;
  196. printf(" Size: %ld", info->size >> 20);
  197. remainder = (info->size % 0x100000);
  198. if (remainder) {
  199. remainder >>= 10;
  200. remainder = (int)((float)
  201. (((float)remainder / (float)1024) *
  202. 10000));
  203. printf(".%d ", remainder);
  204. }
  205. printf("MB in %d Sectors\n", info->sector_count);
  206. } else
  207. printf(" Size: %ld KB in %d Sectors\n",
  208. info->size >> 10, info->sector_count);
  209. printf(" Sector Start Addresses:");
  210. for (i = 0; i < info->sector_count; ++i) {
  211. if ((i % 5) == 0)
  212. printf("\n ");
  213. printf(" %08lX%s",
  214. info->start[i], info->protect[i] ? " (RO)" : " ");
  215. }
  216. printf("\n");
  217. }
  218. /*
  219. * The following code cannot be run from FLASH!
  220. */
  221. ulong flash_get_size(FPWV * addr, flash_info_t * info)
  222. {
  223. volatile u16 *addr16 = (volatile u16 *)addr;
  224. int intel = 0, banks = 0;
  225. u16 value;
  226. int i;
  227. addr[FLASH_CYCLE1] = (FPWV) 0x00AA00AA; /* for Atmel, Intel ignores this */
  228. addr[FLASH_CYCLE2] = (FPWV) 0x00550055; /* for Atmel, Intel ignores this */
  229. addr[FLASH_CYCLE1] = (FPWV) 0x00900090; /* selects Intel or Atmel */
  230. switch (addr[0] & 0xff) {
  231. case (u8) ATM_MANUFACT:
  232. info->flash_id = FLASH_MAN_ATM;
  233. value = addr[1];
  234. break;
  235. case (u8) INTEL_MANUFACT:
  236. /* Terminate Atmel ID read */
  237. addr[0] = (FPWV) 0x00F000F0;
  238. /* Write auto select command: read Manufacturer ID */
  239. /* Write auto select command sequence and test FLASH answer */
  240. *addr16 = (FPW) INTEL_RESET; /* restore read mode */
  241. *addr16 = (FPW) INTEL_READID;
  242. info->flash_id = FLASH_MAN_INTEL;
  243. value = (addr16[INTEL_CFI_MFG] << 8);
  244. value |= addr16[INTEL_CFI_PART] & 0xff;
  245. intel = 1;
  246. break;
  247. default:
  248. printf("Unknown Flash\n");
  249. info->flash_id = FLASH_UNKNOWN;
  250. info->sector_count = 0;
  251. info->size = 0;
  252. *addr = (FPW) 0x00F000F0;
  253. *addr = (FPW) INTEL_RESET; /* restore read mode */
  254. return (0); /* no or unknown flash */
  255. }
  256. switch (value) {
  257. case (u8) ATM_ID_LV040:
  258. info->flash_id += FLASH_AT040;
  259. break;
  260. case (u16) INTEL_ID_28F128J3:
  261. info->flash_id += FLASH_28F128J3A;
  262. break;
  263. case (u16) INTEL_ID_28F64P30T:
  264. info->flash_id += FLASH_28F64P30T;
  265. break;
  266. case (u16) INTEL_ID_28F64P30B:
  267. info->flash_id += FLASH_28F64P30B;
  268. break;
  269. case (u16) INTEL_ID_28F128P30T:
  270. info->flash_id += FLASH_28F128P30T;
  271. break;
  272. case (u16) INTEL_ID_28F128P30B:
  273. info->flash_id += FLASH_28F128P30B;
  274. break;
  275. case (u16) INTEL_ID_28F256P30T:
  276. info->flash_id += FLASH_28F256P30T;
  277. break;
  278. case (u16) INTEL_ID_28F256P30B:
  279. info->flash_id += FLASH_28F256P30B;
  280. break;
  281. default:
  282. info->flash_id = FLASH_UNKNOWN;
  283. break;
  284. }
  285. if (intel) {
  286. /* Intel spec. under CFI section */
  287. u32 sz;
  288. int sectors, bs;
  289. banks = addr16[INTEL_CFI_BANK] & 0xff;
  290. sectors = sz = 0;
  291. for (i = 0; i < banks; i++) {
  292. /* Geometry y1 = y1 + 1, y2 = y2 + 1, CFI spec.
  293. * To be exact, Z = [0x2f 0x30] (LE) * 256 bytes * [0x2D 0x2E] block count
  294. * Z = [0x33 0x34] (LE) * 256 bytes * [0x31 0x32] block count
  295. */
  296. bs = ((((addr16[INTEL_CFI_SZ1B + (i * 4)] & 0xff) << 8)
  297. | (addr16[INTEL_CFI_SZ1A + (i * 4)] & 0xff)) *
  298. 0x100);
  299. sectors +=
  300. (addr16[INTEL_CFI_BLK1A + (i * 4)] & 0xff) + 1;
  301. sz += (bs * sectors);
  302. }
  303. info->sector_count = sectors;
  304. info->size = sz;
  305. *addr = (FPW) INTEL_RESET; /* restore read mode */
  306. } else {
  307. int sect[] = CFG_ATMEL_SECT;
  308. int sectsz[] = CFG_ATMEL_SECTSZ;
  309. info->sector_count = 0;
  310. info->size = 0;
  311. for (i = 0; i < CFG_ATMEL_REGION; i++) {
  312. info->sector_count += sect[i];
  313. info->size += sect[i] * sectsz[i];
  314. }
  315. /* reset ID mode */
  316. addr[0] = (FPWV) 0x00F000F0;
  317. }
  318. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  319. printf("** ERROR: sector count %d > max (%d) **\n",
  320. info->sector_count, CFG_MAX_FLASH_SECT);
  321. info->sector_count = CFG_MAX_FLASH_SECT;
  322. }
  323. return (info->size);
  324. }
  325. int flash_cmd_rd(volatile u16 * addr, int index)
  326. {
  327. return (int)addr[index];
  328. }
  329. /*
  330. * This function gets the u-boot flash sector protection status
  331. * (flash_info_t.protect[]) in sync with the sector protection
  332. * status stored in hardware.
  333. */
  334. void flash_sync_real_protect(flash_info_t * info)
  335. {
  336. int i;
  337. switch (info->flash_id & FLASH_TYPEMASK) {
  338. case FLASH_28F160C3B:
  339. case FLASH_28F160C3T:
  340. case FLASH_28F320C3B:
  341. case FLASH_28F320C3T:
  342. case FLASH_28F640C3B:
  343. case FLASH_28F640C3T:
  344. for (i = 0; i < info->sector_count; ++i) {
  345. info->protect[i] = intel_sector_protected(info, i);
  346. }
  347. break;
  348. default:
  349. /* no h/w protect support */
  350. break;
  351. }
  352. }
  353. /*
  354. * checks if "sector" in bank "info" is protected. Should work on intel
  355. * strata flash chips 28FxxxJ3x in 8-bit mode.
  356. * Returns 1 if sector is protected (or timed-out while trying to read
  357. * protection status), 0 if it is not.
  358. */
  359. uchar intel_sector_protected(flash_info_t * info, ushort sector)
  360. {
  361. FPWV *addr;
  362. FPWV *lock_conf_addr;
  363. ulong start;
  364. unsigned char ret;
  365. /*
  366. * first, wait for the WSM to be finished. The rationale for
  367. * waiting for the WSM to become idle for at most
  368. * CFG_FLASH_ERASE_TOUT is as follows. The WSM can be busy
  369. * because of: (1) erase, (2) program or (3) lock bit
  370. * configuration. So we just wait for the longest timeout of
  371. * the (1)-(3), i.e. the erase timeout.
  372. */
  373. /* wait at least 35ns (W12) before issuing Read Status Register */
  374. /*udelay(1); */
  375. addr = (FPWV *) info->start[sector];
  376. *addr = (FPW) INTEL_STATUS;
  377. start = get_timer(0);
  378. while ((*addr & (FPW) INTEL_FINISHED) != (FPW) INTEL_FINISHED) {
  379. if (get_timer(start) > CFG_FLASH_UNLOCK_TOUT) {
  380. *addr = (FPW) INTEL_RESET; /* restore read mode */
  381. printf("WSM busy too long, can't get prot status\n");
  382. return 1;
  383. }
  384. }
  385. /* issue the Read Identifier Codes command */
  386. *addr = (FPW) INTEL_READID;
  387. /* Intel example code uses offset of 4 for 8-bit flash */
  388. lock_conf_addr = (FPWV *) info->start[sector];
  389. ret = (lock_conf_addr[INTEL_CFI_LOCK] & (FPW) INTEL_PROTECT) ? 1 : 0;
  390. /* put flash back in read mode */
  391. *addr = (FPW) INTEL_RESET;
  392. return ret;
  393. }
  394. int flash_erase(flash_info_t * info, int s_first, int s_last)
  395. {
  396. int flag, prot, sect;
  397. ulong type, start, last;
  398. int rcode = 0, intel = 0;
  399. if ((s_first < 0) || (s_first > s_last)) {
  400. if (info->flash_id == FLASH_UNKNOWN)
  401. printf("- missing\n");
  402. else
  403. printf("- no sectors to erase\n");
  404. return 1;
  405. }
  406. type = (info->flash_id & FLASH_VENDMASK);
  407. if (type != (FLASH_MAN_INTEL & FLASH_VENDMASK)) {
  408. if (type != (FLASH_MAN_ATM & FLASH_VENDMASK)) {
  409. type = (info->flash_id & FLASH_VENDMASK);
  410. printf
  411. ("Can't erase unknown flash type %08lx - aborted\n",
  412. info->flash_id);
  413. return 1;
  414. }
  415. }
  416. if (type == FLASH_MAN_INTEL)
  417. intel = 1;
  418. prot = 0;
  419. for (sect = s_first; sect <= s_last; ++sect) {
  420. if (info->protect[sect]) {
  421. prot++;
  422. }
  423. }
  424. if (prot)
  425. printf("- Warning: %d protected sectors will not be erased!\n",
  426. prot);
  427. else
  428. printf("\n");
  429. start = get_timer(0);
  430. last = start;
  431. /* Start erase on unprotected sectors */
  432. for (sect = s_first; sect <= s_last; sect++) {
  433. if (info->protect[sect] == 0) { /* not protected */
  434. FPWV *addr = (FPWV *) (info->start[sect]);
  435. int min = 0;
  436. printf(".");
  437. /* arm simple, non interrupt dependent timer */
  438. start = get_timer(0);
  439. if (intel) {
  440. *addr = (FPW) INTEL_READID;
  441. min = addr[INTEL_CFI_TERB] & 0xff;
  442. min = 1 << min; /* ms */
  443. min = (min / info->sector_count) * 1000;
  444. /* start erase block */
  445. *addr = (FPW) INTEL_CLEAR; /* clear status register */
  446. *addr = (FPW) INTEL_ERASE; /* erase setup */
  447. *addr = (FPW) INTEL_CONFIRM; /* erase confirm */
  448. while ((*addr & (FPW) INTEL_FINISHED) !=
  449. (FPW) INTEL_FINISHED) {
  450. if (get_timer(start) >
  451. CFG_FLASH_ERASE_TOUT) {
  452. printf("Timeout\n");
  453. *addr = (FPW) INTEL_SUSERASE; /* suspend erase */
  454. *addr = (FPW) INTEL_RESET; /* reset to read mode */
  455. rcode = 1;
  456. break;
  457. }
  458. }
  459. *addr = (FPW) INTEL_RESET; /* resest to read mode */
  460. } else {
  461. FPWV *base; /* first address in bank */
  462. FPWV *atmeladdr;
  463. flag = disable_interrupts();
  464. atmeladdr = (FPWV *) addr; /* concatenate to 8 bit */
  465. base = (FPWV *) (CFG_ATMEL_BASE); /* First sector */
  466. base[FLASH_CYCLE1] = (u8) 0x00AA00AA; /* unlock */
  467. base[FLASH_CYCLE2] = (u8) 0x00550055; /* unlock */
  468. base[FLASH_CYCLE1] = (u8) 0x00800080; /* erase mode */
  469. base[FLASH_CYCLE1] = (u8) 0x00AA00AA; /* unlock */
  470. base[FLASH_CYCLE2] = (u8) 0x00550055; /* unlock */
  471. *atmeladdr = (u8) 0x00300030; /* erase sector */
  472. if (flag)
  473. enable_interrupts();
  474. while ((*atmeladdr & (u8) 0x00800080) !=
  475. (u8) 0x00800080) {
  476. if (get_timer(start) >
  477. CFG_FLASH_ERASE_TOUT) {
  478. printf("Timeout\n");
  479. *atmeladdr = (u8) 0x00F000F0; /* reset to read mode */
  480. rcode = 1;
  481. break;
  482. }
  483. }
  484. *atmeladdr = (u8) 0x00F000F0; /* reset to read mode */
  485. } /* Atmel or Intel */
  486. }
  487. }
  488. printf(" done\n");
  489. return rcode;
  490. }
  491. int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  492. {
  493. if (info->flash_id == FLASH_UNKNOWN)
  494. return 4;
  495. switch (info->flash_id & FLASH_VENDMASK) {
  496. case FLASH_MAN_ATM:
  497. {
  498. u16 data = 0;
  499. int bytes; /* number of bytes to program in current word */
  500. int left; /* number of bytes left to program */
  501. int i, res;
  502. for (left = cnt, res = 0;
  503. left > 0 && res == 0;
  504. addr += sizeof(data), left -=
  505. sizeof(data) - bytes) {
  506. bytes = addr & (sizeof(data) - 1);
  507. addr &= ~(sizeof(data) - 1);
  508. /* combine source and destination data so can program
  509. * an entire word of 16 or 32 bits
  510. */
  511. for (i = 0; i < sizeof(data); i++) {
  512. data <<= 8;
  513. if (i < bytes || i - bytes >= left)
  514. data += *((uchar *) addr + i);
  515. else
  516. data += *src++;
  517. }
  518. data = (data >> 8) | (data << 8);
  519. res = write_word_atm(info, (FPWV *) addr, data);
  520. }
  521. return res;
  522. } /* case FLASH_MAN_ATM */
  523. case FLASH_MAN_INTEL:
  524. {
  525. ulong cp, wp;
  526. u16 data;
  527. int count, i, l, rc, port_width;
  528. /* get lower word aligned address */
  529. wp = addr;
  530. port_width = sizeof(FPW);
  531. /*
  532. * handle unaligned start bytes
  533. */
  534. if ((l = addr - wp) != 0) {
  535. data = 0;
  536. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  537. data = (data << 8) | (*(uchar *) cp);
  538. }
  539. for (; i < port_width && cnt > 0; ++i) {
  540. data = (data << 8) | *src++;
  541. --cnt;
  542. ++cp;
  543. }
  544. for (; cnt == 0 && i < port_width; ++i, ++cp)
  545. data = (data << 8) | (*(uchar *) cp);
  546. if ((rc = write_data(info, wp, data)) != 0)
  547. return (rc);
  548. wp += port_width;
  549. }
  550. if (cnt > WR_BLOCK) {
  551. /*
  552. * handle word aligned part
  553. */
  554. count = 0;
  555. while (cnt >= WR_BLOCK) {
  556. if ((rc =
  557. write_data_block(info,
  558. (ulong) src,
  559. wp)) != 0)
  560. return (rc);
  561. wp += WR_BLOCK;
  562. src += WR_BLOCK;
  563. cnt -= WR_BLOCK;
  564. if (count++ > 0x800) {
  565. spin_wheel();
  566. count = 0;
  567. }
  568. }
  569. }
  570. /* handle word aligned part */
  571. if (cnt < WR_BLOCK) {
  572. /*
  573. * handle word aligned part
  574. */
  575. count = 0;
  576. while (cnt >= port_width) {
  577. data = 0;
  578. for (i = 0; i < port_width; ++i)
  579. data = (data << 8) | *src++;
  580. if ((rc =
  581. write_data(info,
  582. (ulong) ((FPWV *) wp),
  583. (FPW) (data))) != 0)
  584. return (rc);
  585. wp += port_width;
  586. cnt -= port_width;
  587. if (count++ > 0x800) {
  588. spin_wheel();
  589. count = 0;
  590. }
  591. }
  592. }
  593. if (cnt == 0)
  594. return ERR_OK;
  595. /*
  596. * handle unaligned tail bytes
  597. */
  598. data = 0;
  599. for (i = 0, cp = wp; i < port_width && cnt > 0;
  600. ++i, ++cp) {
  601. data = (data << 8) | (*src++);
  602. --cnt;
  603. }
  604. for (; i < port_width; ++i, ++cp) {
  605. data = (data << 8) | (*(uchar *) cp);
  606. }
  607. return write_data(info, (ulong) ((FPWV *) wp),
  608. (FPW) data);
  609. } /* case FLASH_MAN_INTEL */
  610. } /* switch */
  611. return ERR_OK;
  612. }
  613. /*-----------------------------------------------------------------------
  614. * Write a word or halfword to Flash, returns:
  615. * 0 - OK
  616. * 1 - write timeout
  617. * 2 - Flash not erased
  618. */
  619. int write_data_block(flash_info_t * info, ulong src, ulong dest)
  620. {
  621. FPWV *srcaddr = (FPWV *) src;
  622. FPWV *dstaddr = (FPWV *) dest;
  623. ulong start;
  624. int flag, i;
  625. /* Check if Flash is (sufficiently) erased */
  626. for (i = 0; i < WR_BLOCK; i++)
  627. if ((*dstaddr++ & 0xff) != 0xff) {
  628. printf("not erased at %08lx (%lx)\n",
  629. (ulong) dstaddr, *dstaddr);
  630. return (2);
  631. }
  632. dstaddr = (FPWV *) dest;
  633. /* Disable interrupts which might cause a timeout here */
  634. flag = disable_interrupts();
  635. *dstaddr = (FPW) INTEL_WRBLK; /* write block setup */
  636. if (flag)
  637. enable_interrupts();
  638. /* arm simple, non interrupt dependent timer */
  639. start = get_timer(0);
  640. /* wait while polling the status register */
  641. while ((*dstaddr & (FPW) INTEL_FINISHED) != (FPW) INTEL_OK) {
  642. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  643. *dstaddr = (FPW) INTEL_RESET; /* restore read mode */
  644. return (1);
  645. }
  646. }
  647. *dstaddr = (FPW) WR_BLOCK - 1; /* write 32 to buffer */
  648. for (i = 0; i < WR_BLOCK; i++)
  649. *dstaddr++ = *srcaddr++;
  650. dstaddr -= 1;
  651. *dstaddr = (FPW) INTEL_CONFIRM; /* write 32 to buffer */
  652. /* arm simple, non interrupt dependent timer */
  653. start = get_timer(0);
  654. /* wait while polling the status register */
  655. while ((*dstaddr & (FPW) INTEL_FINISHED) != (FPW) INTEL_OK) {
  656. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  657. *dstaddr = (FPW) INTEL_RESET; /* restore read mode */
  658. return (1);
  659. }
  660. }
  661. *dstaddr = (FPW) INTEL_RESET; /* restore read mode */
  662. return (0);
  663. }
  664. /*-----------------------------------------------------------------------
  665. * Write a word or halfword to Flash, returns:
  666. * 0 - OK
  667. * 1 - write timeout
  668. * 2 - Flash not erased
  669. */
  670. int write_data(flash_info_t * info, ulong dest, FPW data)
  671. {
  672. FPWV *addr = (FPWV *) dest;
  673. ulong start;
  674. int flag;
  675. /* Check if Flash is (sufficiently) erased */
  676. if ((*addr & data) != data) {
  677. printf("not erased at %08lx (%lx)\n", (ulong) addr,
  678. (ulong) * addr);
  679. return (2);
  680. }
  681. /* Disable interrupts which might cause a timeout here */
  682. flag = (int)disable_interrupts();
  683. *addr = (FPW) INTEL_CLEAR;
  684. *addr = (FPW) INTEL_RESET;
  685. *addr = (FPW) INTEL_WRSETUP; /* write setup */
  686. *addr = data;
  687. if (flag)
  688. enable_interrupts();
  689. /* arm simple, non interrupt dependent timer */
  690. start = get_timer(0);
  691. /* wait while polling the status register */
  692. while ((*addr & (FPW) INTEL_OK) != (FPW) INTEL_OK) {
  693. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  694. *addr = (FPW) INTEL_SUSERASE; /* suspend mode */
  695. *addr = (FPW) INTEL_CLEAR; /* clear status */
  696. *addr = (FPW) INTEL_RESET; /* reset */
  697. return (1);
  698. }
  699. }
  700. *addr = (FPW) INTEL_CLEAR; /* clear status */
  701. *addr = (FPW) INTEL_RESET; /* restore read mode */
  702. return (0);
  703. }
  704. /*-----------------------------------------------------------------------
  705. * Write a word to Flash for ATMEL FLASH
  706. * A word is 16 bits, whichever the bus width of the flash bank
  707. * (not an individual chip) is.
  708. *
  709. * returns:
  710. * 0 - OK
  711. * 1 - write timeout
  712. * 2 - Flash not erased
  713. */
  714. int write_word_atm(flash_info_t * info, volatile u8 * dest, u16 data)
  715. {
  716. ulong start;
  717. int flag, i;
  718. int res = 0; /* result, assume success */
  719. FPWV *base; /* first address in flash bank */
  720. /* Check if Flash is (sufficiently) erased */
  721. if ((*((volatile u16 *)dest) & data) != data) {
  722. return (2);
  723. }
  724. base = (FPWV *) (CFG_ATMEL_BASE);
  725. for (i = 0; i < sizeof(u16); i++) {
  726. /* Disable interrupts which might cause a timeout here */
  727. flag = disable_interrupts();
  728. base[FLASH_CYCLE1] = (u8) 0x00AA00AA; /* unlock */
  729. base[FLASH_CYCLE2] = (u8) 0x00550055; /* unlock */
  730. base[FLASH_CYCLE1] = (u8) 0x00A000A0; /* selects program mode */
  731. *dest = data; /* start programming the data */
  732. /* re-enable interrupts if necessary */
  733. if (flag)
  734. enable_interrupts();
  735. start = get_timer(0);
  736. /* data polling for D7 */
  737. while (res == 0
  738. && (*dest & (u8) 0x00800080) !=
  739. (data & (u8) 0x00800080)) {
  740. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  741. *dest = (u8) 0x00F000F0; /* reset bank */
  742. res = 1;
  743. }
  744. }
  745. *dest++ = (u8) 0x00F000F0; /* reset bank */
  746. data >>= 8;
  747. }
  748. return (res);
  749. }
  750. void inline spin_wheel(void)
  751. {
  752. static int p = 0;
  753. static char w[] = "\\/-";
  754. printf("\010%c", w[p]);
  755. (++p == 3) ? (p = 0) : 0;
  756. }
  757. #ifdef CFG_FLASH_PROTECTION
  758. /*-----------------------------------------------------------------------
  759. */
  760. int flash_real_protect(flash_info_t * info, long sector, int prot)
  761. {
  762. int rcode = 0; /* assume success */
  763. FPWV *addr; /* address of sector */
  764. FPW value;
  765. addr = (FPWV *) (info->start[sector]);
  766. switch (info->flash_id & FLASH_TYPEMASK) {
  767. case FLASH_28F160C3B:
  768. case FLASH_28F160C3T:
  769. case FLASH_28F320C3B:
  770. case FLASH_28F320C3T:
  771. case FLASH_28F640C3B:
  772. case FLASH_28F640C3T:
  773. *addr = (FPW) INTEL_RESET; /* make sure in read mode */
  774. *addr = (FPW) INTEL_LOCKBIT; /* lock command setup */
  775. if (prot)
  776. *addr = (FPW) INTEL_PROTECT; /* lock sector */
  777. else
  778. *addr = (FPW) INTEL_CONFIRM; /* unlock sector */
  779. /* now see if it really is locked/unlocked as requested */
  780. *addr = (FPW) INTEL_READID;
  781. /* read sector protection at sector address, (A7 .. A0) = 0x02.
  782. * D0 = 1 for each device if protected.
  783. * If at least one device is protected the sector is marked
  784. * protected, but return failure. Mixed protected and
  785. * unprotected devices within a sector should never happen.
  786. */
  787. value = addr[2] & (FPW) INTEL_PROTECT;
  788. if (value == 0)
  789. info->protect[sector] = 0;
  790. else if (value == (FPW) INTEL_PROTECT)
  791. info->protect[sector] = 1;
  792. else {
  793. /* error, mixed protected and unprotected */
  794. rcode = 1;
  795. info->protect[sector] = 1;
  796. }
  797. if (info->protect[sector] != prot)
  798. rcode = 1; /* failed to protect/unprotect as requested */
  799. /* reload all protection bits from hardware for now */
  800. flash_sync_real_protect(info);
  801. break;
  802. default:
  803. /* no hardware protect that we support */
  804. info->protect[sector] = prot;
  805. break;
  806. }
  807. return rcode;
  808. }
  809. #endif
  810. #endif