flash.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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. #if defined(CONFIG_SERIAL_FLASH) && defined(CONFIG_CF_DSPI)
  87. #define STM_ID_M25P16 0x20152015
  88. #define FLASH_M25P16 0x0055
  89. #endif
  90. #define SYNC __asm__("nop")
  91. /*-----------------------------------------------------------------------
  92. * Functions
  93. */
  94. ulong flash_get_size(FPWV * addr, flash_info_t * info);
  95. int flash_get_offsets(ulong base, flash_info_t * info);
  96. int flash_cmd_rd(volatile u16 * addr, int index);
  97. int write_data(flash_info_t * info, ulong dest, FPW data);
  98. int write_data_block(flash_info_t * info, ulong src, ulong dest);
  99. int write_word_atm(flash_info_t * info, volatile u8 * dest, u16 data);
  100. void inline spin_wheel(void);
  101. void flash_sync_real_protect(flash_info_t * info);
  102. uchar intel_sector_protected(flash_info_t * info, ushort sector);
  103. #if defined(CONFIG_SERIAL_FLASH) && defined(CONFIG_CF_DSPI)
  104. int write_ser_data(flash_info_t * info, ulong dest, uchar * data, ulong cnt);
  105. int serial_flash_read_status(int chipsel);
  106. static int ser_flash_cs = 0;
  107. #endif
  108. flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  109. ulong flash_init(void)
  110. {
  111. int i;
  112. ulong size = 0;
  113. ulong fbase = 0;
  114. #if defined(CONFIG_SERIAL_FLASH) && defined(CONFIG_CF_DSPI)
  115. dspi_init();
  116. #endif
  117. for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
  118. memset(&flash_info[i], 0, sizeof(flash_info_t));
  119. switch (i) {
  120. case 0:
  121. fbase = (ulong) CFG_FLASH0_BASE;
  122. break;
  123. case 1:
  124. fbase = (ulong) CFG_FLASH1_BASE;
  125. break;
  126. #if defined(CONFIG_SERIAL_FLASH) && defined(CONFIG_CF_DSPI)
  127. case 2:
  128. fbase = (ulong) CFG_FLASH2_BASE;
  129. break;
  130. #endif
  131. }
  132. flash_get_size((FPWV *) fbase, &flash_info[i]);
  133. flash_get_offsets((ulong) fbase, &flash_info[i]);
  134. fbase += flash_info[i].size;
  135. size += flash_info[i].size;
  136. /* get the h/w and s/w protection status in sync */
  137. flash_sync_real_protect(&flash_info[i]);
  138. }
  139. /* Protect monitor and environment sectors */
  140. flash_protect(FLAG_PROTECT_SET,
  141. CFG_MONITOR_BASE,
  142. CFG_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]);
  143. return size;
  144. }
  145. int flash_get_offsets(ulong base, flash_info_t * info)
  146. {
  147. int i, j, k;
  148. int sectors, bs, banks;
  149. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_ATM) {
  150. int sect[] = CFG_ATMEL_SECT;
  151. int sectsz[] = CFG_ATMEL_SECTSZ;
  152. info->start[0] = base;
  153. for (k = 0, i = 0; i < CFG_ATMEL_REGION; i++) {
  154. for (j = 0; j < sect[i]; j++, k++) {
  155. info->start[k + 1] = info->start[k] + sectsz[i];
  156. info->protect[k] = 0;
  157. }
  158. }
  159. }
  160. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
  161. volatile u16 *addr16 = (volatile u16 *)base;
  162. *addr16 = (FPW) INTEL_RESET; /* restore read mode */
  163. *addr16 = (FPW) INTEL_READID;
  164. banks = addr16[INTEL_CFI_BANK] & 0xff;
  165. sectors = 0;
  166. info->start[0] = base;
  167. for (k = 0, i = 0; i < banks; i++) {
  168. /* Geometry y1 = y1 + 1, y2 = y2 + 1, CFI spec.
  169. * To be exact, Z = [0x2f 0x30] (LE) * 256 bytes * [0x2D 0x2E] block count
  170. * Z = [0x33 0x34] (LE) * 256 bytes * [0x31 0x32] block count
  171. */
  172. bs = ((((addr16[INTEL_CFI_SZ1B + (i * 4)] & 0xff) << 8)
  173. | (addr16[INTEL_CFI_SZ1A + (i * 4)] & 0xff)) *
  174. 0x100);
  175. sectors =
  176. (addr16[INTEL_CFI_BLK1A + (i * 4)] & 0xff) + 1;
  177. for (j = 0; j < sectors; j++, k++) {
  178. info->start[k + 1] = info->start[k] + bs;
  179. }
  180. }
  181. *addr16 = (FPW) INTEL_RESET; /* restore read mode */
  182. }
  183. #if defined(CONFIG_SERIAL_FLASH) && defined(CONFIG_CF_DSPI)
  184. if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_STM) {
  185. info->start[0] = CFG_FLASH2_BASE;
  186. for (k = 0, i = 0; i < CFG_STM_SECT; i++, k++) {
  187. info->start[k + 1] = info->start[k] + CFG_STM_SECTSZ;
  188. info->protect[k] = 0;
  189. }
  190. }
  191. #endif
  192. return ERR_OK;
  193. }
  194. void flash_print_info(flash_info_t * info)
  195. {
  196. int i;
  197. switch (info->flash_id & FLASH_VENDMASK) {
  198. case FLASH_MAN_INTEL:
  199. printf("INTEL ");
  200. break;
  201. case FLASH_MAN_ATM:
  202. printf("ATMEL ");
  203. break;
  204. #if defined(CONFIG_SERIAL_FLASH) && defined(CONFIG_CF_DSPI)
  205. case FLASH_MAN_STM:
  206. printf("ST ");
  207. break;
  208. #endif
  209. default:
  210. printf("Unknown Vendor ");
  211. break;
  212. }
  213. switch (info->flash_id & FLASH_TYPEMASK) {
  214. case FLASH_AT040:
  215. printf("AT49BV040A\n");
  216. break;
  217. case FLASH_28F128J3A:
  218. printf("28F128J3A\n");
  219. break;
  220. #if defined(CONFIG_SERIAL_FLASH) && defined(CONFIG_CF_DSPI)
  221. case FLASH_M25P16:
  222. printf("M25P16\n");
  223. break;
  224. #endif
  225. default:
  226. printf("Unknown Chip Type\n");
  227. return;
  228. }
  229. if (info->size > 0x100000) {
  230. int remainder;
  231. printf(" Size: %ld", info->size >> 20);
  232. remainder = (info->size % 0x100000);
  233. if (remainder) {
  234. remainder >>= 10;
  235. remainder = (int)((float)
  236. (((float)remainder / (float)1024) *
  237. 10000));
  238. printf(".%d ", remainder);
  239. }
  240. printf("MB in %d Sectors\n", info->sector_count);
  241. } else
  242. printf(" Size: %ld KB in %d Sectors\n",
  243. info->size >> 10, info->sector_count);
  244. printf(" Sector Start Addresses:");
  245. for (i = 0; i < info->sector_count; ++i) {
  246. if ((i % 5) == 0)
  247. printf("\n ");
  248. printf(" %08lX%s",
  249. info->start[i], info->protect[i] ? " (RO)" : " ");
  250. }
  251. printf("\n");
  252. }
  253. /*
  254. * The following code cannot be run from FLASH!
  255. */
  256. ulong flash_get_size(FPWV * addr, flash_info_t * info)
  257. {
  258. volatile u16 *addr16 = (volatile u16 *)addr;
  259. int intel = 0, banks = 0;
  260. u16 value;
  261. int i;
  262. #if defined(CONFIG_SERIAL_FLASH) && defined(CONFIG_CF_DSPI)
  263. if ((ulong) addr == CFG_FLASH2_BASE) {
  264. int manufactId = 0;
  265. int deviceId = 0;
  266. ser_flash_cs = 1;
  267. dspi_tx(ser_flash_cs, 0x80, SER_RDID);
  268. dspi_tx(ser_flash_cs, 0x80, 0);
  269. dspi_tx(ser_flash_cs, 0x80, 0);
  270. dspi_tx(ser_flash_cs, 0x80, 0);
  271. dspi_rx();
  272. manufactId = dspi_rx();
  273. deviceId = dspi_rx() << 8;
  274. deviceId |= dspi_rx();
  275. dspi_tx(ser_flash_cs, 0x00, 0);
  276. dspi_rx();
  277. switch (manufactId) {
  278. case (u8) STM_MANUFACT:
  279. info->flash_id = FLASH_MAN_STM;
  280. break;
  281. }
  282. switch (deviceId) {
  283. case (u16) STM_ID_M25P16:
  284. info->flash_id += FLASH_M25P16;
  285. break;
  286. }
  287. info->sector_count = CFG_STM_SECT;
  288. info->size = CFG_STM_SECT * CFG_STM_SECTSZ;
  289. return (info->size);
  290. }
  291. #endif
  292. addr[FLASH_CYCLE1] = (FPWV) 0x00AA00AA; /* for Atmel, Intel ignores this */
  293. addr[FLASH_CYCLE2] = (FPWV) 0x00550055; /* for Atmel, Intel ignores this */
  294. addr[FLASH_CYCLE1] = (FPWV) 0x00900090; /* selects Intel or Atmel */
  295. switch (addr[0] & 0xff) {
  296. case (u8) ATM_MANUFACT:
  297. info->flash_id = FLASH_MAN_ATM;
  298. value = addr[1];
  299. break;
  300. case (u8) INTEL_MANUFACT:
  301. /* Terminate Atmel ID read */
  302. addr[0] = (FPWV) 0x00F000F0;
  303. /* Write auto select command: read Manufacturer ID */
  304. /* Write auto select command sequence and test FLASH answer */
  305. *addr16 = (FPW) INTEL_RESET; /* restore read mode */
  306. *addr16 = (FPW) INTEL_READID;
  307. info->flash_id = FLASH_MAN_INTEL;
  308. value = (addr16[INTEL_CFI_MFG] << 8);
  309. value |= addr16[INTEL_CFI_PART] & 0xff;
  310. intel = 1;
  311. break;
  312. default:
  313. printf("Unknown Flash\n");
  314. info->flash_id = FLASH_UNKNOWN;
  315. info->sector_count = 0;
  316. info->size = 0;
  317. *addr = (FPW) 0x00F000F0;
  318. *addr = (FPW) INTEL_RESET; /* restore read mode */
  319. return (0); /* no or unknown flash */
  320. }
  321. switch (value) {
  322. case (u8) ATM_ID_LV040:
  323. info->flash_id += FLASH_AT040;
  324. break;
  325. case (u16) INTEL_ID_28F128J3:
  326. info->flash_id += FLASH_28F128J3A;
  327. break;
  328. case (u16) INTEL_ID_28F64P30T:
  329. info->flash_id += FLASH_28F64P30T;
  330. break;
  331. case (u16) INTEL_ID_28F64P30B:
  332. info->flash_id += FLASH_28F64P30B;
  333. break;
  334. case (u16) INTEL_ID_28F128P30T:
  335. info->flash_id += FLASH_28F128P30T;
  336. break;
  337. case (u16) INTEL_ID_28F128P30B:
  338. info->flash_id += FLASH_28F128P30B;
  339. break;
  340. case (u16) INTEL_ID_28F256P30T:
  341. info->flash_id += FLASH_28F256P30T;
  342. break;
  343. case (u16) INTEL_ID_28F256P30B:
  344. info->flash_id += FLASH_28F256P30B;
  345. break;
  346. default:
  347. info->flash_id = FLASH_UNKNOWN;
  348. break;
  349. }
  350. if (intel) {
  351. /* Intel spec. under CFI section */
  352. u32 sz;
  353. int sectors, bs;
  354. banks = addr16[INTEL_CFI_BANK] & 0xff;
  355. sectors = sz = 0;
  356. for (i = 0; i < banks; i++) {
  357. /* Geometry y1 = y1 + 1, y2 = y2 + 1, CFI spec.
  358. * To be exact, Z = [0x2f 0x30] (LE) * 256 bytes * [0x2D 0x2E] block count
  359. * Z = [0x33 0x34] (LE) * 256 bytes * [0x31 0x32] block count
  360. */
  361. bs = ((((addr16[INTEL_CFI_SZ1B + (i * 4)] & 0xff) << 8)
  362. | (addr16[INTEL_CFI_SZ1A + (i * 4)] & 0xff)) *
  363. 0x100);
  364. sectors +=
  365. (addr16[INTEL_CFI_BLK1A + (i * 4)] & 0xff) + 1;
  366. sz += (bs * sectors);
  367. }
  368. info->sector_count = sectors;
  369. info->size = sz;
  370. *addr = (FPW) INTEL_RESET; /* restore read mode */
  371. } else {
  372. int sect[] = CFG_ATMEL_SECT;
  373. int sectsz[] = CFG_ATMEL_SECTSZ;
  374. info->sector_count = 0;
  375. info->size = 0;
  376. for (i = 0; i < CFG_ATMEL_REGION; i++) {
  377. info->sector_count += sect[i];
  378. info->size += sect[i] * sectsz[i];
  379. }
  380. /* reset ID mode */
  381. addr[0] = (FPWV) 0x00F000F0;
  382. }
  383. if (info->sector_count > CFG_MAX_FLASH_SECT) {
  384. printf("** ERROR: sector count %d > max (%d) **\n",
  385. info->sector_count, CFG_MAX_FLASH_SECT);
  386. info->sector_count = CFG_MAX_FLASH_SECT;
  387. }
  388. return (info->size);
  389. }
  390. int flash_cmd_rd(volatile u16 * addr, int index)
  391. {
  392. return (int)addr[index];
  393. }
  394. #if defined(CONFIG_SERIAL_FLASH) && defined(CONFIG_CF_DSPI)
  395. int serial_flash_read_status(int chipsel)
  396. {
  397. u16 status;
  398. dspi_tx(chipsel, 0x80, SER_RDSR);
  399. dspi_rx();
  400. dspi_tx(chipsel, 0x00, 0);
  401. status = dspi_rx();
  402. return status;
  403. }
  404. #endif
  405. /*
  406. * This function gets the u-boot flash sector protection status
  407. * (flash_info_t.protect[]) in sync with the sector protection
  408. * status stored in hardware.
  409. */
  410. void flash_sync_real_protect(flash_info_t * info)
  411. {
  412. int i;
  413. switch (info->flash_id & FLASH_TYPEMASK) {
  414. case FLASH_28F160C3B:
  415. case FLASH_28F160C3T:
  416. case FLASH_28F320C3B:
  417. case FLASH_28F320C3T:
  418. case FLASH_28F640C3B:
  419. case FLASH_28F640C3T:
  420. for (i = 0; i < info->sector_count; ++i) {
  421. info->protect[i] = intel_sector_protected(info, i);
  422. }
  423. break;
  424. default:
  425. /* no h/w protect support */
  426. break;
  427. }
  428. }
  429. /*
  430. * checks if "sector" in bank "info" is protected. Should work on intel
  431. * strata flash chips 28FxxxJ3x in 8-bit mode.
  432. * Returns 1 if sector is protected (or timed-out while trying to read
  433. * protection status), 0 if it is not.
  434. */
  435. uchar intel_sector_protected(flash_info_t * info, ushort sector)
  436. {
  437. FPWV *addr;
  438. FPWV *lock_conf_addr;
  439. ulong start;
  440. unsigned char ret;
  441. /*
  442. * first, wait for the WSM to be finished. The rationale for
  443. * waiting for the WSM to become idle for at most
  444. * CFG_FLASH_ERASE_TOUT is as follows. The WSM can be busy
  445. * because of: (1) erase, (2) program or (3) lock bit
  446. * configuration. So we just wait for the longest timeout of
  447. * the (1)-(3), i.e. the erase timeout.
  448. */
  449. /* wait at least 35ns (W12) before issuing Read Status Register */
  450. /*udelay(1); */
  451. addr = (FPWV *) info->start[sector];
  452. *addr = (FPW) INTEL_STATUS;
  453. start = get_timer(0);
  454. while ((*addr & (FPW) INTEL_FINISHED) != (FPW) INTEL_FINISHED) {
  455. if (get_timer(start) > CFG_FLASH_UNLOCK_TOUT) {
  456. *addr = (FPW) INTEL_RESET; /* restore read mode */
  457. printf("WSM busy too long, can't get prot status\n");
  458. return 1;
  459. }
  460. }
  461. /* issue the Read Identifier Codes command */
  462. *addr = (FPW) INTEL_READID;
  463. /* Intel example code uses offset of 4 for 8-bit flash */
  464. lock_conf_addr = (FPWV *) info->start[sector];
  465. ret = (lock_conf_addr[INTEL_CFI_LOCK] & (FPW) INTEL_PROTECT) ? 1 : 0;
  466. /* put flash back in read mode */
  467. *addr = (FPW) INTEL_RESET;
  468. return ret;
  469. }
  470. int flash_erase(flash_info_t * info, int s_first, int s_last)
  471. {
  472. int flag, prot, sect;
  473. ulong type, start, last;
  474. int rcode = 0, flashtype = 0;
  475. #if defined(CONFIG_SERIAL_FLASH) && defined(CONFIG_CF_DSPI)
  476. int count;
  477. u16 status;
  478. #endif
  479. if ((s_first < 0) || (s_first > s_last)) {
  480. if (info->flash_id == FLASH_UNKNOWN)
  481. printf("- missing\n");
  482. else
  483. printf("- no sectors to erase\n");
  484. return 1;
  485. }
  486. type = (info->flash_id & FLASH_VENDMASK);
  487. switch (type) {
  488. case FLASH_MAN_ATM:
  489. flashtype = 1;
  490. break;
  491. case FLASH_MAN_INTEL:
  492. flashtype = 2;
  493. break;
  494. #if defined(CONFIG_SERIAL_FLASH) && defined(CONFIG_CF_DSPI)
  495. case FLASH_MAN_STM:
  496. flashtype = 3;
  497. break;
  498. #endif
  499. default:
  500. type = (info->flash_id & FLASH_VENDMASK);
  501. printf("Can't erase unknown flash type %08lx - aborted\n",
  502. info->flash_id);
  503. return 1;
  504. }
  505. prot = 0;
  506. for (sect = s_first; sect <= s_last; ++sect) {
  507. if (info->protect[sect]) {
  508. prot++;
  509. }
  510. }
  511. if (prot)
  512. printf("- Warning: %d protected sectors will not be erased!\n",
  513. prot);
  514. else
  515. printf("\n");
  516. start = get_timer(0);
  517. last = start;
  518. #if defined(CONFIG_SERIAL_FLASH) && defined(CONFIG_CF_DSPI)
  519. /* Perform bulk erase */
  520. if (flashtype == 3) {
  521. if ((s_last - s_first) == (CFG_STM_SECT - 1)) {
  522. if (prot == 0) {
  523. dspi_tx(ser_flash_cs, 0x00, SER_WREN);
  524. dspi_rx();
  525. status = serial_flash_read_status(ser_flash_cs);
  526. if (((status & 0x9C) != 0)
  527. && ((status & 0x02) != 0x02)) {
  528. printf("Can't erase flash\n");
  529. return 1;
  530. }
  531. dspi_tx(ser_flash_cs, 0x00, SER_BULK_ERASE);
  532. dspi_rx();
  533. count = 0;
  534. start = get_timer(0);
  535. do {
  536. status =
  537. serial_flash_read_status
  538. (ser_flash_cs);
  539. if (count++ > 0x10000) {
  540. spin_wheel();
  541. count = 0;
  542. }
  543. if (get_timer(start) >
  544. CFG_FLASH_ERASE_TOUT) {
  545. printf("Timeout\n");
  546. return 1;
  547. }
  548. } while (status & 0x01);
  549. printf("\b. done\n");
  550. return 0;
  551. } else if (prot == CFG_STM_SECT) {
  552. return 1;
  553. }
  554. }
  555. }
  556. #endif
  557. /* Start erase on unprotected sectors */
  558. for (sect = s_first; sect <= s_last; sect++) {
  559. if (info->protect[sect] == 0) { /* not protected */
  560. FPWV *addr = (FPWV *) (info->start[sect]);
  561. int min = 0;
  562. printf(".");
  563. /* arm simple, non interrupt dependent timer */
  564. start = get_timer(0);
  565. switch (flashtype) {
  566. case 1:
  567. {
  568. FPWV *base; /* first address in bank */
  569. FPWV *atmeladdr;
  570. flag = disable_interrupts();
  571. atmeladdr = (FPWV *) addr; /* concatenate to 8 bit */
  572. base = (FPWV *) (CFG_ATMEL_BASE); /* First sector */
  573. base[FLASH_CYCLE1] = (u8) 0x00AA00AA; /* unlock */
  574. base[FLASH_CYCLE2] = (u8) 0x00550055; /* unlock */
  575. base[FLASH_CYCLE1] = (u8) 0x00800080; /* erase mode */
  576. base[FLASH_CYCLE1] = (u8) 0x00AA00AA; /* unlock */
  577. base[FLASH_CYCLE2] = (u8) 0x00550055; /* unlock */
  578. *atmeladdr = (u8) 0x00300030; /* erase sector */
  579. if (flag)
  580. enable_interrupts();
  581. while ((*atmeladdr & (u8) 0x00800080) !=
  582. (u8) 0x00800080) {
  583. if (get_timer(start) >
  584. CFG_FLASH_ERASE_TOUT) {
  585. printf("Timeout\n");
  586. *atmeladdr = (u8) 0x00F000F0; /* reset to read mode */
  587. rcode = 1;
  588. break;
  589. }
  590. }
  591. *atmeladdr = (u8) 0x00F000F0; /* reset to read mode */
  592. break;
  593. }
  594. case 2:
  595. {
  596. *addr = (FPW) INTEL_READID;
  597. min = addr[INTEL_CFI_TERB] & 0xff;
  598. min = 1 << min; /* ms */
  599. min = (min / info->sector_count) * 1000;
  600. /* start erase block */
  601. *addr = (FPW) INTEL_CLEAR; /* clear status register */
  602. *addr = (FPW) INTEL_ERASE; /* erase setup */
  603. *addr = (FPW) INTEL_CONFIRM; /* erase confirm */
  604. while ((*addr & (FPW) INTEL_FINISHED) !=
  605. (FPW) INTEL_FINISHED) {
  606. if (get_timer(start) >
  607. CFG_FLASH_ERASE_TOUT) {
  608. printf("Timeout\n");
  609. *addr = (FPW) INTEL_SUSERASE; /* suspend erase */
  610. *addr = (FPW) INTEL_RESET; /* reset to read mode */
  611. rcode = 1;
  612. break;
  613. }
  614. }
  615. *addr = (FPW) INTEL_RESET; /* resest to read mode */
  616. break;
  617. }
  618. #if defined(CONFIG_SERIAL_FLASH) && defined(CONFIG_CF_DSPI)
  619. case 3:
  620. {
  621. u8 sec = ((ulong) addr >> 16) & 0xFF;
  622. dspi_tx(ser_flash_cs, 0x00, SER_WREN);
  623. dspi_rx();
  624. status =
  625. serial_flash_read_status
  626. (ser_flash_cs);
  627. if (((status & 0x9C) != 0)
  628. && ((status & 0x02) != 0x02)) {
  629. printf("Error Programming\n");
  630. return 1;
  631. }
  632. dspi_tx(ser_flash_cs, 0x80,
  633. SER_SECT_ERASE);
  634. dspi_tx(ser_flash_cs, 0x80, sec);
  635. dspi_tx(ser_flash_cs, 0x80, 0);
  636. dspi_tx(ser_flash_cs, 0x00, 0);
  637. dspi_rx();
  638. dspi_rx();
  639. dspi_rx();
  640. dspi_rx();
  641. do {
  642. status =
  643. serial_flash_read_status
  644. (ser_flash_cs);
  645. if (get_timer(start) >
  646. CFG_FLASH_ERASE_TOUT) {
  647. printf("Timeout\n");
  648. return 1;
  649. }
  650. } while (status & 0x01);
  651. break;
  652. }
  653. #endif
  654. } /* switch (flashtype) */
  655. }
  656. }
  657. printf(" done\n");
  658. return rcode;
  659. }
  660. int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  661. {
  662. int count;
  663. if (info->flash_id == FLASH_UNKNOWN)
  664. return 4;
  665. switch (info->flash_id & FLASH_VENDMASK) {
  666. case FLASH_MAN_ATM:
  667. {
  668. u16 data = 0;
  669. int bytes; /* number of bytes to program in current word */
  670. int left; /* number of bytes left to program */
  671. int i, res;
  672. for (left = cnt, res = 0;
  673. left > 0 && res == 0;
  674. addr += sizeof(data), left -=
  675. sizeof(data) - bytes) {
  676. bytes = addr & (sizeof(data) - 1);
  677. addr &= ~(sizeof(data) - 1);
  678. /* combine source and destination data so can program
  679. * an entire word of 16 or 32 bits
  680. */
  681. for (i = 0; i < sizeof(data); i++) {
  682. data <<= 8;
  683. if (i < bytes || i - bytes >= left)
  684. data += *((uchar *) addr + i);
  685. else
  686. data += *src++;
  687. }
  688. data = (data >> 8) | (data << 8);
  689. res = write_word_atm(info, (FPWV *) addr, data);
  690. }
  691. return res;
  692. } /* case FLASH_MAN_ATM */
  693. case FLASH_MAN_INTEL:
  694. {
  695. ulong cp, wp;
  696. u16 data;
  697. int i, l, rc, port_width;
  698. /* get lower word aligned address */
  699. wp = addr;
  700. port_width = sizeof(FPW);
  701. /*
  702. * handle unaligned start bytes
  703. */
  704. if ((l = addr - wp) != 0) {
  705. data = 0;
  706. for (i = 0, cp = wp; i < l; ++i, ++cp) {
  707. data = (data << 8) | (*(uchar *) cp);
  708. }
  709. for (; i < port_width && cnt > 0; ++i) {
  710. data = (data << 8) | *src++;
  711. --cnt;
  712. ++cp;
  713. }
  714. for (; cnt == 0 && i < port_width; ++i, ++cp)
  715. data = (data << 8) | (*(uchar *) cp);
  716. if ((rc = write_data(info, wp, data)) != 0)
  717. return (rc);
  718. wp += port_width;
  719. }
  720. if (cnt > WR_BLOCK) {
  721. /*
  722. * handle word aligned part
  723. */
  724. count = 0;
  725. while (cnt >= WR_BLOCK) {
  726. if ((rc =
  727. write_data_block(info,
  728. (ulong) src,
  729. wp)) != 0)
  730. return (rc);
  731. wp += WR_BLOCK;
  732. src += WR_BLOCK;
  733. cnt -= WR_BLOCK;
  734. if (count++ > 0x800) {
  735. spin_wheel();
  736. count = 0;
  737. }
  738. }
  739. }
  740. /* handle word aligned part */
  741. if (cnt < WR_BLOCK) {
  742. /*
  743. * handle word aligned part
  744. */
  745. count = 0;
  746. while (cnt >= port_width) {
  747. data = 0;
  748. for (i = 0; i < port_width; ++i)
  749. data = (data << 8) | *src++;
  750. if ((rc =
  751. write_data(info,
  752. (ulong) ((FPWV *) wp),
  753. (FPW) (data))) != 0)
  754. return (rc);
  755. wp += port_width;
  756. cnt -= port_width;
  757. if (count++ > 0x800) {
  758. spin_wheel();
  759. count = 0;
  760. }
  761. }
  762. }
  763. if (cnt == 0)
  764. return ERR_OK;
  765. /*
  766. * handle unaligned tail bytes
  767. */
  768. data = 0;
  769. for (i = 0, cp = wp; i < port_width && cnt > 0;
  770. ++i, ++cp) {
  771. data = (data << 8) | (*src++);
  772. --cnt;
  773. }
  774. for (; i < port_width; ++i, ++cp) {
  775. data = (data << 8) | (*(uchar *) cp);
  776. }
  777. return write_data(info, (ulong) ((FPWV *) wp),
  778. (FPW) data);
  779. } /* case FLASH_MAN_INTEL */
  780. #if defined(CONFIG_SERIAL_FLASH) && defined(CONFIG_CF_DSPI)
  781. case FLASH_MAN_STM:
  782. {
  783. ulong wp;
  784. u8 *data = (u8 *) src;
  785. int left; /* number of bytes left to program */
  786. wp = addr;
  787. /* page align, each page is 256 bytes */
  788. if ((wp % 0x100) != 0) {
  789. left = (0x100 - (wp & 0xFF));
  790. write_ser_data(info, wp, data, left);
  791. cnt -= left;
  792. wp += left;
  793. data += left;
  794. }
  795. /* page program - 256 bytes at a time */
  796. if (cnt > 255) {
  797. count = 0;
  798. while (cnt >= 0x100) {
  799. write_ser_data(info, wp, data, 0x100);
  800. cnt -= 0x100;
  801. wp += 0x100;
  802. data += 0x100;
  803. if (count++ > 0x400) {
  804. spin_wheel();
  805. count = 0;
  806. }
  807. }
  808. }
  809. /* remainint bytes */
  810. if (cnt && (cnt < 256)) {
  811. write_ser_data(info, wp, data, cnt);
  812. wp += cnt;
  813. data += cnt;
  814. cnt -= cnt;
  815. }
  816. printf("\b.");
  817. }
  818. #endif
  819. } /* switch */
  820. return ERR_OK;
  821. }
  822. /*-----------------------------------------------------------------------
  823. * Write a word or halfword to Flash, returns:
  824. * 0 - OK
  825. * 1 - write timeout
  826. * 2 - Flash not erased
  827. */
  828. int write_data_block(flash_info_t * info, ulong src, ulong dest)
  829. {
  830. FPWV *srcaddr = (FPWV *) src;
  831. FPWV *dstaddr = (FPWV *) dest;
  832. ulong start;
  833. int flag, i;
  834. /* Check if Flash is (sufficiently) erased */
  835. for (i = 0; i < WR_BLOCK; i++)
  836. if ((*dstaddr++ & 0xff) != 0xff) {
  837. printf("not erased at %08lx (%lx)\n",
  838. (ulong) dstaddr, *dstaddr);
  839. return (2);
  840. }
  841. dstaddr = (FPWV *) dest;
  842. /* Disable interrupts which might cause a timeout here */
  843. flag = disable_interrupts();
  844. *dstaddr = (FPW) INTEL_WRBLK; /* write block setup */
  845. if (flag)
  846. enable_interrupts();
  847. /* arm simple, non interrupt dependent timer */
  848. start = get_timer(0);
  849. /* wait while polling the status register */
  850. while ((*dstaddr & (FPW) INTEL_FINISHED) != (FPW) INTEL_OK) {
  851. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  852. *dstaddr = (FPW) INTEL_RESET; /* restore read mode */
  853. return (1);
  854. }
  855. }
  856. *dstaddr = (FPW) WR_BLOCK - 1; /* write 32 to buffer */
  857. for (i = 0; i < WR_BLOCK; i++)
  858. *dstaddr++ = *srcaddr++;
  859. dstaddr -= 1;
  860. *dstaddr = (FPW) INTEL_CONFIRM; /* write 32 to buffer */
  861. /* arm simple, non interrupt dependent timer */
  862. start = get_timer(0);
  863. /* wait while polling the status register */
  864. while ((*dstaddr & (FPW) INTEL_FINISHED) != (FPW) INTEL_OK) {
  865. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  866. *dstaddr = (FPW) INTEL_RESET; /* restore read mode */
  867. return (1);
  868. }
  869. }
  870. *dstaddr = (FPW) INTEL_RESET; /* restore read mode */
  871. return (0);
  872. }
  873. /*-----------------------------------------------------------------------
  874. * Write a word or halfword to Flash, returns:
  875. * 0 - OK
  876. * 1 - write timeout
  877. * 2 - Flash not erased
  878. */
  879. int write_data(flash_info_t * info, ulong dest, FPW data)
  880. {
  881. FPWV *addr = (FPWV *) dest;
  882. ulong start;
  883. int flag;
  884. /* Check if Flash is (sufficiently) erased */
  885. if ((*addr & data) != data) {
  886. printf("not erased at %08lx (%lx)\n", (ulong) addr,
  887. (ulong) * addr);
  888. return (2);
  889. }
  890. /* Disable interrupts which might cause a timeout here */
  891. flag = (int)disable_interrupts();
  892. *addr = (FPW) INTEL_CLEAR;
  893. *addr = (FPW) INTEL_RESET;
  894. *addr = (FPW) INTEL_WRSETUP; /* write setup */
  895. *addr = data;
  896. if (flag)
  897. enable_interrupts();
  898. /* arm simple, non interrupt dependent timer */
  899. start = get_timer(0);
  900. /* wait while polling the status register */
  901. while ((*addr & (FPW) INTEL_OK) != (FPW) INTEL_OK) {
  902. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  903. *addr = (FPW) INTEL_SUSERASE; /* suspend mode */
  904. *addr = (FPW) INTEL_CLEAR; /* clear status */
  905. *addr = (FPW) INTEL_RESET; /* reset */
  906. return (1);
  907. }
  908. }
  909. *addr = (FPW) INTEL_CLEAR; /* clear status */
  910. *addr = (FPW) INTEL_RESET; /* restore read mode */
  911. return (0);
  912. }
  913. #if defined(CONFIG_SERIAL_FLASH) && defined(CONFIG_CF_DSPI)
  914. int write_ser_data(flash_info_t * info, ulong dest, uchar * data, ulong cnt)
  915. {
  916. ulong start;
  917. int status, i;
  918. u8 flashdata;
  919. /* Check if Flash is (sufficiently) erased */
  920. dspi_tx(ser_flash_cs, 0x80, SER_READ);
  921. dspi_tx(ser_flash_cs, 0x80, (dest >> 16) & 0xFF);
  922. dspi_tx(ser_flash_cs, 0x80, (dest >> 8) & 0xFF);
  923. dspi_tx(ser_flash_cs, 0x80, dest & 0xFF);
  924. dspi_rx();
  925. dspi_rx();
  926. dspi_rx();
  927. dspi_rx();
  928. dspi_tx(ser_flash_cs, 0x80, 0);
  929. flashdata = dspi_rx();
  930. dspi_tx(ser_flash_cs, 0x00, 0);
  931. dspi_rx();
  932. if ((flashdata & *data) != *data) {
  933. printf("not erased at %08lx (%lx)\n", (ulong) dest,
  934. (ulong) flashdata);
  935. return (2);
  936. }
  937. dspi_tx(ser_flash_cs, 0x00, SER_WREN);
  938. dspi_rx();
  939. status = serial_flash_read_status(ser_flash_cs);
  940. if (((status & 0x9C) != 0) && ((status & 0x02) != 0x02)) {
  941. printf("Error Programming\n");
  942. return 1;
  943. }
  944. start = get_timer(0);
  945. dspi_tx(ser_flash_cs, 0x80, SER_PAGE_PROG);
  946. dspi_tx(ser_flash_cs, 0x80, ((dest & 0xFF0000) >> 16));
  947. dspi_tx(ser_flash_cs, 0x80, ((dest & 0xFF00) >> 8));
  948. dspi_tx(ser_flash_cs, 0x80, (dest & 0xFF));
  949. dspi_rx();
  950. dspi_rx();
  951. dspi_rx();
  952. dspi_rx();
  953. for (i = 0; i < (cnt - 1); i++) {
  954. dspi_tx(ser_flash_cs, 0x80, *data);
  955. dspi_rx();
  956. data++;
  957. }
  958. dspi_tx(ser_flash_cs, 0x00, *data);
  959. dspi_rx();
  960. do {
  961. status = serial_flash_read_status(ser_flash_cs);
  962. if (get_timer(start) > CFG_FLASH_ERASE_TOUT) {
  963. printf("Timeout\n");
  964. return 1;
  965. }
  966. } while (status & 0x01);
  967. return (0);
  968. }
  969. #endif
  970. /*-----------------------------------------------------------------------
  971. * Write a word to Flash for ATMEL FLASH
  972. * A word is 16 bits, whichever the bus width of the flash bank
  973. * (not an individual chip) is.
  974. *
  975. * returns:
  976. * 0 - OK
  977. * 1 - write timeout
  978. * 2 - Flash not erased
  979. */
  980. int write_word_atm(flash_info_t * info, volatile u8 * dest, u16 data)
  981. {
  982. ulong start;
  983. int flag, i;
  984. int res = 0; /* result, assume success */
  985. FPWV *base; /* first address in flash bank */
  986. /* Check if Flash is (sufficiently) erased */
  987. if ((*((volatile u16 *)dest) & data) != data) {
  988. return (2);
  989. }
  990. base = (FPWV *) (CFG_ATMEL_BASE);
  991. for (i = 0; i < sizeof(u16); i++) {
  992. /* Disable interrupts which might cause a timeout here */
  993. flag = disable_interrupts();
  994. base[FLASH_CYCLE1] = (u8) 0x00AA00AA; /* unlock */
  995. base[FLASH_CYCLE2] = (u8) 0x00550055; /* unlock */
  996. base[FLASH_CYCLE1] = (u8) 0x00A000A0; /* selects program mode */
  997. *dest = data; /* start programming the data */
  998. /* re-enable interrupts if necessary */
  999. if (flag)
  1000. enable_interrupts();
  1001. start = get_timer(0);
  1002. /* data polling for D7 */
  1003. while (res == 0
  1004. && (*dest & (u8) 0x00800080) !=
  1005. (data & (u8) 0x00800080)) {
  1006. if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
  1007. *dest = (u8) 0x00F000F0; /* reset bank */
  1008. res = 1;
  1009. }
  1010. }
  1011. *dest++ = (u8) 0x00F000F0; /* reset bank */
  1012. data >>= 8;
  1013. }
  1014. return (res);
  1015. }
  1016. void inline spin_wheel(void)
  1017. {
  1018. static int p = 0;
  1019. static char w[] = "\\/-";
  1020. printf("\010%c", w[p]);
  1021. (++p == 3) ? (p = 0) : 0;
  1022. }
  1023. #ifdef CFG_FLASH_PROTECTION
  1024. /*-----------------------------------------------------------------------
  1025. */
  1026. int flash_real_protect(flash_info_t * info, long sector, int prot)
  1027. {
  1028. int rcode = 0; /* assume success */
  1029. FPWV *addr; /* address of sector */
  1030. FPW value;
  1031. addr = (FPWV *) (info->start[sector]);
  1032. switch (info->flash_id & FLASH_TYPEMASK) {
  1033. case FLASH_28F160C3B:
  1034. case FLASH_28F160C3T:
  1035. case FLASH_28F320C3B:
  1036. case FLASH_28F320C3T:
  1037. case FLASH_28F640C3B:
  1038. case FLASH_28F640C3T:
  1039. *addr = (FPW) INTEL_RESET; /* make sure in read mode */
  1040. *addr = (FPW) INTEL_LOCKBIT; /* lock command setup */
  1041. if (prot)
  1042. *addr = (FPW) INTEL_PROTECT; /* lock sector */
  1043. else
  1044. *addr = (FPW) INTEL_CONFIRM; /* unlock sector */
  1045. /* now see if it really is locked/unlocked as requested */
  1046. *addr = (FPW) INTEL_READID;
  1047. /* read sector protection at sector address, (A7 .. A0) = 0x02.
  1048. * D0 = 1 for each device if protected.
  1049. * If at least one device is protected the sector is marked
  1050. * protected, but return failure. Mixed protected and
  1051. * unprotected devices within a sector should never happen.
  1052. */
  1053. value = addr[2] & (FPW) INTEL_PROTECT;
  1054. if (value == 0)
  1055. info->protect[sector] = 0;
  1056. else if (value == (FPW) INTEL_PROTECT)
  1057. info->protect[sector] = 1;
  1058. else {
  1059. /* error, mixed protected and unprotected */
  1060. rcode = 1;
  1061. info->protect[sector] = 1;
  1062. }
  1063. if (info->protect[sector] != prot)
  1064. rcode = 1; /* failed to protect/unprotect as requested */
  1065. /* reload all protection bits from hardware for now */
  1066. flash_sync_real_protect(info);
  1067. break;
  1068. default:
  1069. /* no hardware protect that we support */
  1070. info->protect[sector] = prot;
  1071. break;
  1072. }
  1073. return rcode;
  1074. }
  1075. #endif
  1076. #endif