nand_bbt.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. /*
  2. * drivers/mtd/nand_bbt.c
  3. *
  4. * Overview:
  5. * Bad block table support for the NAND driver
  6. *
  7. * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
  8. *
  9. * $Id: nand_bbt.c,v 1.36 2005/11/07 11:14:30 gleixner Exp $
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * Description:
  16. *
  17. * When nand_scan_bbt is called, then it tries to find the bad block table
  18. * depending on the options in the bbt descriptor(s). If a bbt is found
  19. * then the contents are read and the memory based bbt is created. If a
  20. * mirrored bbt is selected then the mirror is searched too and the
  21. * versions are compared. If the mirror has a greater version number
  22. * than the mirror bbt is used to build the memory based bbt.
  23. * If the tables are not versioned, then we "or" the bad block information.
  24. * If one of the bbt's is out of date or does not exist it is (re)created.
  25. * If no bbt exists at all then the device is scanned for factory marked
  26. * good / bad blocks and the bad block tables are created.
  27. *
  28. * For manufacturer created bbts like the one found on M-SYS DOC devices
  29. * the bbt is searched and read but never created
  30. *
  31. * The autogenerated bad block table is located in the last good blocks
  32. * of the device. The table is mirrored, so it can be updated eventually.
  33. * The table is marked in the oob area with an ident pattern and a version
  34. * number which indicates which of both tables is more up to date.
  35. *
  36. * The table uses 2 bits per block
  37. * 11b: block is good
  38. * 00b: block is factory marked bad
  39. * 01b, 10b: block is marked bad due to wear
  40. *
  41. * The memory bad block table uses the following scheme:
  42. * 00b: block is good
  43. * 01b: block is marked bad due to wear
  44. * 10b: block is reserved (to protect the bbt area)
  45. * 11b: block is factory marked bad
  46. *
  47. * Multichip devices like DOC store the bad block info per floor.
  48. *
  49. * Following assumptions are made:
  50. * - bbts start at a page boundary, if autolocated on a block boundary
  51. * - the space necessary for a bbt in FLASH does not exceed a block boundary
  52. *
  53. */
  54. #include <common.h>
  55. #if defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY)
  56. #include <malloc.h>
  57. #include <linux/mtd/compat.h>
  58. #include <linux/mtd/mtd.h>
  59. #include <linux/mtd/nand.h>
  60. #include <asm/errno.h>
  61. /* XXX U-BOOT XXX */
  62. #if 0
  63. #include <linux/slab.h>
  64. #include <linux/types.h>
  65. #include <linux/mtd/mtd.h>
  66. #include <linux/mtd/nand.h>
  67. #include <linux/mtd/nand_ecc.h>
  68. #include <linux/mtd/compatmac.h>
  69. #include <linux/bitops.h>
  70. #include <linux/delay.h>
  71. #include <linux/vmalloc.h>
  72. #endif
  73. /**
  74. * check_pattern - [GENERIC] check if a pattern is in the buffer
  75. * @buf: the buffer to search
  76. * @len: the length of buffer to search
  77. * @paglen: the pagelength
  78. * @td: search pattern descriptor
  79. *
  80. * Check for a pattern at the given place. Used to search bad block
  81. * tables and good / bad block identifiers.
  82. * If the SCAN_EMPTY option is set then check, if all bytes except the
  83. * pattern area contain 0xff
  84. *
  85. */
  86. static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
  87. {
  88. int i, end = 0;
  89. uint8_t *p = buf;
  90. end = paglen + td->offs;
  91. if (td->options & NAND_BBT_SCANEMPTY) {
  92. for (i = 0; i < end; i++) {
  93. if (p[i] != 0xff)
  94. return -1;
  95. }
  96. }
  97. p += end;
  98. /* Compare the pattern */
  99. for (i = 0; i < td->len; i++) {
  100. if (p[i] != td->pattern[i])
  101. return -1;
  102. }
  103. if (td->options & NAND_BBT_SCANEMPTY) {
  104. p += td->len;
  105. end += td->len;
  106. for (i = end; i < len; i++) {
  107. if (*p++ != 0xff)
  108. return -1;
  109. }
  110. }
  111. return 0;
  112. }
  113. /**
  114. * check_short_pattern - [GENERIC] check if a pattern is in the buffer
  115. * @buf: the buffer to search
  116. * @td: search pattern descriptor
  117. *
  118. * Check for a pattern at the given place. Used to search bad block
  119. * tables and good / bad block identifiers. Same as check_pattern, but
  120. * no optional empty check
  121. *
  122. */
  123. static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
  124. {
  125. int i;
  126. uint8_t *p = buf;
  127. /* Compare the pattern */
  128. for (i = 0; i < td->len; i++) {
  129. if (p[td->offs + i] != td->pattern[i])
  130. return -1;
  131. }
  132. return 0;
  133. }
  134. /**
  135. * read_bbt - [GENERIC] Read the bad block table starting from page
  136. * @mtd: MTD device structure
  137. * @buf: temporary buffer
  138. * @page: the starting page
  139. * @num: the number of bbt descriptors to read
  140. * @bits: number of bits per block
  141. * @offs: offset in the memory table
  142. * @reserved_block_code: Pattern to identify reserved blocks
  143. *
  144. * Read the bad block table starting from page.
  145. *
  146. */
  147. static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
  148. int bits, int offs, int reserved_block_code)
  149. {
  150. int res, i, j, act = 0;
  151. struct nand_chip *this = mtd->priv;
  152. size_t retlen, len, totlen;
  153. loff_t from;
  154. uint8_t msk = (uint8_t) ((1 << bits) - 1);
  155. totlen = (num * bits) >> 3;
  156. from = ((loff_t) page) << this->page_shift;
  157. while (totlen) {
  158. len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
  159. res = mtd->read(mtd, from, len, &retlen, buf);
  160. if (res < 0) {
  161. if (retlen != len) {
  162. printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
  163. return res;
  164. }
  165. printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
  166. }
  167. /* Analyse data */
  168. for (i = 0; i < len; i++) {
  169. uint8_t dat = buf[i];
  170. for (j = 0; j < 8; j += bits, act += 2) {
  171. uint8_t tmp = (dat >> j) & msk;
  172. if (tmp == msk)
  173. continue;
  174. if (reserved_block_code && (tmp == reserved_block_code)) {
  175. printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%08x\n",
  176. ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
  177. this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
  178. mtd->ecc_stats.bbtblocks++;
  179. continue;
  180. }
  181. /* Leave it for now, if its matured we can move this
  182. * message to MTD_DEBUG_LEVEL0 */
  183. printk(KERN_DEBUG "nand_read_bbt: Bad block at 0x%08x\n",
  184. ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
  185. /* Factory marked bad or worn out ? */
  186. if (tmp == 0)
  187. this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
  188. else
  189. this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
  190. mtd->ecc_stats.badblocks++;
  191. }
  192. }
  193. totlen -= len;
  194. from += len;
  195. }
  196. return 0;
  197. }
  198. /**
  199. * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
  200. * @mtd: MTD device structure
  201. * @buf: temporary buffer
  202. * @td: descriptor for the bad block table
  203. * @chip: read the table for a specific chip, -1 read all chips.
  204. * Applies only if NAND_BBT_PERCHIP option is set
  205. *
  206. * Read the bad block table for all chips starting at a given page
  207. * We assume that the bbt bits are in consecutive order.
  208. */
  209. static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
  210. {
  211. struct nand_chip *this = mtd->priv;
  212. int res = 0, i;
  213. int bits;
  214. bits = td->options & NAND_BBT_NRBITS_MSK;
  215. if (td->options & NAND_BBT_PERCHIP) {
  216. int offs = 0;
  217. for (i = 0; i < this->numchips; i++) {
  218. if (chip == -1 || chip == i)
  219. res = read_bbt (mtd, buf, td->pages[i], this->chipsize >> this->bbt_erase_shift, bits, offs, td->reserved_block_code);
  220. if (res)
  221. return res;
  222. offs += this->chipsize >> (this->bbt_erase_shift + 2);
  223. }
  224. } else {
  225. res = read_bbt (mtd, buf, td->pages[0], mtd->size >> this->bbt_erase_shift, bits, 0, td->reserved_block_code);
  226. if (res)
  227. return res;
  228. }
  229. return 0;
  230. }
  231. /*
  232. * Scan read raw data from flash
  233. */
  234. static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
  235. size_t len)
  236. {
  237. struct mtd_oob_ops ops;
  238. ops.mode = MTD_OOB_RAW;
  239. ops.ooboffs = 0;
  240. ops.ooblen = mtd->oobsize;
  241. ops.oobbuf = buf;
  242. ops.datbuf = buf;
  243. ops.len = len;
  244. return mtd->read_oob(mtd, offs, &ops);
  245. }
  246. /*
  247. * Scan write data with oob to flash
  248. */
  249. static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
  250. uint8_t *buf, uint8_t *oob)
  251. {
  252. struct mtd_oob_ops ops;
  253. ops.mode = MTD_OOB_PLACE;
  254. ops.ooboffs = 0;
  255. ops.ooblen = mtd->oobsize;
  256. ops.datbuf = buf;
  257. ops.oobbuf = oob;
  258. ops.len = len;
  259. return mtd->write_oob(mtd, offs, &ops);
  260. }
  261. /**
  262. * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
  263. * @mtd: MTD device structure
  264. * @buf: temporary buffer
  265. * @td: descriptor for the bad block table
  266. * @md: descriptor for the bad block table mirror
  267. *
  268. * Read the bad block table(s) for all chips starting at a given page
  269. * We assume that the bbt bits are in consecutive order.
  270. *
  271. */
  272. static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
  273. struct nand_bbt_descr *td, struct nand_bbt_descr *md)
  274. {
  275. struct nand_chip *this = mtd->priv;
  276. /* Read the primary version, if available */
  277. if (td->options & NAND_BBT_VERSION) {
  278. scan_read_raw(mtd, buf, td->pages[0] << this->page_shift,
  279. mtd->writesize);
  280. td->version[0] = buf[mtd->writesize + td->veroffs];
  281. printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
  282. td->pages[0], td->version[0]);
  283. }
  284. /* Read the mirror version, if available */
  285. if (md && (md->options & NAND_BBT_VERSION)) {
  286. scan_read_raw(mtd, buf, md->pages[0] << this->page_shift,
  287. mtd->writesize);
  288. md->version[0] = buf[mtd->writesize + md->veroffs];
  289. printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
  290. md->pages[0], md->version[0]);
  291. }
  292. return 1;
  293. }
  294. /*
  295. * Scan a given block full
  296. */
  297. static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
  298. loff_t offs, uint8_t *buf, size_t readlen,
  299. int scanlen, int len)
  300. {
  301. int ret, j;
  302. ret = scan_read_raw(mtd, buf, offs, readlen);
  303. if (ret)
  304. return ret;
  305. for (j = 0; j < len; j++, buf += scanlen) {
  306. if (check_pattern(buf, scanlen, mtd->writesize, bd))
  307. return 1;
  308. }
  309. return 0;
  310. }
  311. /*
  312. * Scan a given block partially
  313. */
  314. static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
  315. loff_t offs, uint8_t *buf, int len)
  316. {
  317. struct mtd_oob_ops ops;
  318. int j, ret;
  319. ops.ooblen = mtd->oobsize;
  320. ops.oobbuf = buf;
  321. ops.ooboffs = 0;
  322. ops.datbuf = NULL;
  323. ops.mode = MTD_OOB_PLACE;
  324. for (j = 0; j < len; j++) {
  325. /*
  326. * Read the full oob until read_oob is fixed to
  327. * handle single byte reads for 16 bit
  328. * buswidth
  329. */
  330. ret = mtd->read_oob(mtd, offs, &ops);
  331. if (ret)
  332. return ret;
  333. if (check_short_pattern(buf, bd))
  334. return 1;
  335. offs += mtd->writesize;
  336. }
  337. return 0;
  338. }
  339. /**
  340. * create_bbt - [GENERIC] Create a bad block table by scanning the device
  341. * @mtd: MTD device structure
  342. * @buf: temporary buffer
  343. * @bd: descriptor for the good/bad block search pattern
  344. * @chip: create the table for a specific chip, -1 read all chips.
  345. * Applies only if NAND_BBT_PERCHIP option is set
  346. *
  347. * Create a bad block table by scanning the device
  348. * for the given good/bad block identify pattern
  349. */
  350. static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
  351. struct nand_bbt_descr *bd, int chip)
  352. {
  353. struct nand_chip *this = mtd->priv;
  354. int i, numblocks, len, scanlen;
  355. int startblock;
  356. loff_t from;
  357. size_t readlen;
  358. MTDDEBUG (MTD_DEBUG_LEVEL0, "Scanning device for bad blocks\n");
  359. if (bd->options & NAND_BBT_SCANALLPAGES)
  360. len = 1 << (this->bbt_erase_shift - this->page_shift);
  361. else {
  362. if (bd->options & NAND_BBT_SCAN2NDPAGE)
  363. len = 2;
  364. else
  365. len = 1;
  366. }
  367. if (!(bd->options & NAND_BBT_SCANEMPTY)) {
  368. /* We need only read few bytes from the OOB area */
  369. scanlen = 0;
  370. readlen = bd->len;
  371. } else {
  372. /* Full page content should be read */
  373. scanlen = mtd->writesize + mtd->oobsize;
  374. readlen = len * mtd->writesize;
  375. }
  376. if (chip == -1) {
  377. /* Note that numblocks is 2 * (real numblocks) here, see i+=2
  378. * below as it makes shifting and masking less painful */
  379. numblocks = mtd->size >> (this->bbt_erase_shift - 1);
  380. startblock = 0;
  381. from = 0;
  382. } else {
  383. if (chip >= this->numchips) {
  384. printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
  385. chip + 1, this->numchips);
  386. return -EINVAL;
  387. }
  388. numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
  389. startblock = chip * numblocks;
  390. numblocks += startblock;
  391. from = startblock << (this->bbt_erase_shift - 1);
  392. }
  393. for (i = startblock; i < numblocks;) {
  394. int ret;
  395. if (bd->options & NAND_BBT_SCANALLPAGES)
  396. ret = scan_block_full(mtd, bd, from, buf, readlen,
  397. scanlen, len);
  398. else
  399. ret = scan_block_fast(mtd, bd, from, buf, len);
  400. if (ret < 0)
  401. return ret;
  402. if (ret) {
  403. this->bbt[i >> 3] |= 0x03 << (i & 0x6);
  404. MTDDEBUG (MTD_DEBUG_LEVEL0,
  405. "Bad eraseblock %d at 0x%08x\n",
  406. i >> 1, (unsigned int)from);
  407. mtd->ecc_stats.badblocks++;
  408. }
  409. i += 2;
  410. from += (1 << this->bbt_erase_shift);
  411. }
  412. return 0;
  413. }
  414. /**
  415. * search_bbt - [GENERIC] scan the device for a specific bad block table
  416. * @mtd: MTD device structure
  417. * @buf: temporary buffer
  418. * @td: descriptor for the bad block table
  419. *
  420. * Read the bad block table by searching for a given ident pattern.
  421. * Search is preformed either from the beginning up or from the end of
  422. * the device downwards. The search starts always at the start of a
  423. * block.
  424. * If the option NAND_BBT_PERCHIP is given, each chip is searched
  425. * for a bbt, which contains the bad block information of this chip.
  426. * This is necessary to provide support for certain DOC devices.
  427. *
  428. * The bbt ident pattern resides in the oob area of the first page
  429. * in a block.
  430. */
  431. static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
  432. {
  433. struct nand_chip *this = mtd->priv;
  434. int i, chips;
  435. int bits, startblock, block, dir;
  436. int scanlen = mtd->writesize + mtd->oobsize;
  437. int bbtblocks;
  438. int blocktopage = this->bbt_erase_shift - this->page_shift;
  439. /* Search direction top -> down ? */
  440. if (td->options & NAND_BBT_LASTBLOCK) {
  441. startblock = (mtd->size >> this->bbt_erase_shift) - 1;
  442. dir = -1;
  443. } else {
  444. startblock = 0;
  445. dir = 1;
  446. }
  447. /* Do we have a bbt per chip ? */
  448. if (td->options & NAND_BBT_PERCHIP) {
  449. chips = this->numchips;
  450. bbtblocks = this->chipsize >> this->bbt_erase_shift;
  451. startblock &= bbtblocks - 1;
  452. } else {
  453. chips = 1;
  454. bbtblocks = mtd->size >> this->bbt_erase_shift;
  455. }
  456. /* Number of bits for each erase block in the bbt */
  457. bits = td->options & NAND_BBT_NRBITS_MSK;
  458. for (i = 0; i < chips; i++) {
  459. /* Reset version information */
  460. td->version[i] = 0;
  461. td->pages[i] = -1;
  462. /* Scan the maximum number of blocks */
  463. for (block = 0; block < td->maxblocks; block++) {
  464. int actblock = startblock + dir * block;
  465. loff_t offs = actblock << this->bbt_erase_shift;
  466. /* Read first page */
  467. scan_read_raw(mtd, buf, offs, mtd->writesize);
  468. if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
  469. td->pages[i] = actblock << blocktopage;
  470. if (td->options & NAND_BBT_VERSION) {
  471. td->version[i] = buf[mtd->writesize + td->veroffs];
  472. }
  473. break;
  474. }
  475. }
  476. startblock += this->chipsize >> this->bbt_erase_shift;
  477. }
  478. /* Check, if we found a bbt for each requested chip */
  479. for (i = 0; i < chips; i++) {
  480. if (td->pages[i] == -1)
  481. printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
  482. else
  483. printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i],
  484. td->version[i]);
  485. }
  486. return 0;
  487. }
  488. /**
  489. * search_read_bbts - [GENERIC] scan the device for bad block table(s)
  490. * @mtd: MTD device structure
  491. * @buf: temporary buffer
  492. * @td: descriptor for the bad block table
  493. * @md: descriptor for the bad block table mirror
  494. *
  495. * Search and read the bad block table(s)
  496. */
  497. static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
  498. {
  499. /* Search the primary table */
  500. search_bbt(mtd, buf, td);
  501. /* Search the mirror table */
  502. if (md)
  503. search_bbt(mtd, buf, md);
  504. /* Force result check */
  505. return 1;
  506. }
  507. /**
  508. * write_bbt - [GENERIC] (Re)write the bad block table
  509. *
  510. * @mtd: MTD device structure
  511. * @buf: temporary buffer
  512. * @td: descriptor for the bad block table
  513. * @md: descriptor for the bad block table mirror
  514. * @chipsel: selector for a specific chip, -1 for all
  515. *
  516. * (Re)write the bad block table
  517. *
  518. */
  519. static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
  520. struct nand_bbt_descr *td, struct nand_bbt_descr *md,
  521. int chipsel)
  522. {
  523. struct nand_chip *this = mtd->priv;
  524. struct erase_info einfo;
  525. int i, j, res, chip = 0;
  526. int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
  527. int nrchips, bbtoffs, pageoffs, ooboffs;
  528. uint8_t msk[4];
  529. uint8_t rcode = td->reserved_block_code;
  530. size_t retlen, len = 0;
  531. loff_t to;
  532. struct mtd_oob_ops ops;
  533. ops.ooblen = mtd->oobsize;
  534. ops.ooboffs = 0;
  535. ops.datbuf = NULL;
  536. ops.mode = MTD_OOB_PLACE;
  537. if (!rcode)
  538. rcode = 0xff;
  539. /* Write bad block table per chip rather than per device ? */
  540. if (td->options & NAND_BBT_PERCHIP) {
  541. numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  542. /* Full device write or specific chip ? */
  543. if (chipsel == -1) {
  544. nrchips = this->numchips;
  545. } else {
  546. nrchips = chipsel + 1;
  547. chip = chipsel;
  548. }
  549. } else {
  550. numblocks = (int)(mtd->size >> this->bbt_erase_shift);
  551. nrchips = 1;
  552. }
  553. /* Loop through the chips */
  554. for (; chip < nrchips; chip++) {
  555. /* There was already a version of the table, reuse the page
  556. * This applies for absolute placement too, as we have the
  557. * page nr. in td->pages.
  558. */
  559. if (td->pages[chip] != -1) {
  560. page = td->pages[chip];
  561. goto write;
  562. }
  563. /* Automatic placement of the bad block table */
  564. /* Search direction top -> down ? */
  565. if (td->options & NAND_BBT_LASTBLOCK) {
  566. startblock = numblocks * (chip + 1) - 1;
  567. dir = -1;
  568. } else {
  569. startblock = chip * numblocks;
  570. dir = 1;
  571. }
  572. for (i = 0; i < td->maxblocks; i++) {
  573. int block = startblock + dir * i;
  574. /* Check, if the block is bad */
  575. switch ((this->bbt[block >> 2] >>
  576. (2 * (block & 0x03))) & 0x03) {
  577. case 0x01:
  578. case 0x03:
  579. continue;
  580. }
  581. page = block <<
  582. (this->bbt_erase_shift - this->page_shift);
  583. /* Check, if the block is used by the mirror table */
  584. if (!md || md->pages[chip] != page)
  585. goto write;
  586. }
  587. printk(KERN_ERR "No space left to write bad block table\n");
  588. return -ENOSPC;
  589. write:
  590. /* Set up shift count and masks for the flash table */
  591. bits = td->options & NAND_BBT_NRBITS_MSK;
  592. msk[2] = ~rcode;
  593. switch (bits) {
  594. case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
  595. msk[3] = 0x01;
  596. break;
  597. case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
  598. msk[3] = 0x03;
  599. break;
  600. case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
  601. msk[3] = 0x0f;
  602. break;
  603. case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
  604. msk[3] = 0xff;
  605. break;
  606. default: return -EINVAL;
  607. }
  608. bbtoffs = chip * (numblocks >> 2);
  609. to = ((loff_t) page) << this->page_shift;
  610. /* Must we save the block contents ? */
  611. if (td->options & NAND_BBT_SAVECONTENT) {
  612. /* Make it block aligned */
  613. to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
  614. len = 1 << this->bbt_erase_shift;
  615. res = mtd->read(mtd, to, len, &retlen, buf);
  616. if (res < 0) {
  617. if (retlen != len) {
  618. printk(KERN_INFO "nand_bbt: Error "
  619. "reading block for writing "
  620. "the bad block table\n");
  621. return res;
  622. }
  623. printk(KERN_WARNING "nand_bbt: ECC error "
  624. "while reading block for writing "
  625. "bad block table\n");
  626. }
  627. /* Read oob data */
  628. ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
  629. ops.oobbuf = &buf[len];
  630. res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
  631. if (res < 0 || ops.oobretlen != ops.ooblen)
  632. goto outerr;
  633. /* Calc the byte offset in the buffer */
  634. pageoffs = page - (int)(to >> this->page_shift);
  635. offs = pageoffs << this->page_shift;
  636. /* Preset the bbt area with 0xff */
  637. memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
  638. ooboffs = len + (pageoffs * mtd->oobsize);
  639. } else {
  640. /* Calc length */
  641. len = (size_t) (numblocks >> sft);
  642. /* Make it page aligned ! */
  643. len = (len + (mtd->writesize - 1)) &
  644. ~(mtd->writesize - 1);
  645. /* Preset the buffer with 0xff */
  646. memset(buf, 0xff, len +
  647. (len >> this->page_shift)* mtd->oobsize);
  648. offs = 0;
  649. ooboffs = len;
  650. /* Pattern is located in oob area of first page */
  651. memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
  652. }
  653. if (td->options & NAND_BBT_VERSION)
  654. buf[ooboffs + td->veroffs] = td->version[chip];
  655. /* walk through the memory table */
  656. for (i = 0; i < numblocks;) {
  657. uint8_t dat;
  658. dat = this->bbt[bbtoffs + (i >> 2)];
  659. for (j = 0; j < 4; j++, i++) {
  660. int sftcnt = (i << (3 - sft)) & sftmsk;
  661. /* Do not store the reserved bbt blocks ! */
  662. buf[offs + (i >> sft)] &=
  663. ~(msk[dat & 0x03] << sftcnt);
  664. dat >>= 2;
  665. }
  666. }
  667. memset(&einfo, 0, sizeof(einfo));
  668. einfo.mtd = mtd;
  669. einfo.addr = (unsigned long)to;
  670. einfo.len = 1 << this->bbt_erase_shift;
  671. res = nand_erase_nand(mtd, &einfo, 1);
  672. if (res < 0)
  673. goto outerr;
  674. res = scan_write_bbt(mtd, to, len, buf, &buf[len]);
  675. if (res < 0)
  676. goto outerr;
  677. printk(KERN_DEBUG "Bad block table written to 0x%08x, version "
  678. "0x%02X\n", (unsigned int)to, td->version[chip]);
  679. /* Mark it as used */
  680. td->pages[chip] = page;
  681. }
  682. return 0;
  683. outerr:
  684. printk(KERN_WARNING
  685. "nand_bbt: Error while writing bad block table %d\n", res);
  686. return res;
  687. }
  688. /**
  689. * nand_memory_bbt - [GENERIC] create a memory based bad block table
  690. * @mtd: MTD device structure
  691. * @bd: descriptor for the good/bad block search pattern
  692. *
  693. * The function creates a memory based bbt by scanning the device
  694. * for manufacturer / software marked good / bad blocks
  695. */
  696. static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  697. {
  698. struct nand_chip *this = mtd->priv;
  699. bd->options &= ~NAND_BBT_SCANEMPTY;
  700. return create_bbt(mtd, this->buffers->databuf, bd, -1);
  701. }
  702. /**
  703. * check_create - [GENERIC] create and write bbt(s) if necessary
  704. * @mtd: MTD device structure
  705. * @buf: temporary buffer
  706. * @bd: descriptor for the good/bad block search pattern
  707. *
  708. * The function checks the results of the previous call to read_bbt
  709. * and creates / updates the bbt(s) if necessary
  710. * Creation is necessary if no bbt was found for the chip/device
  711. * Update is necessary if one of the tables is missing or the
  712. * version nr. of one table is less than the other
  713. */
  714. static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
  715. {
  716. int i, chips, writeops, chipsel, res;
  717. struct nand_chip *this = mtd->priv;
  718. struct nand_bbt_descr *td = this->bbt_td;
  719. struct nand_bbt_descr *md = this->bbt_md;
  720. struct nand_bbt_descr *rd, *rd2;
  721. /* Do we have a bbt per chip ? */
  722. if (td->options & NAND_BBT_PERCHIP)
  723. chips = this->numchips;
  724. else
  725. chips = 1;
  726. for (i = 0; i < chips; i++) {
  727. writeops = 0;
  728. rd = NULL;
  729. rd2 = NULL;
  730. /* Per chip or per device ? */
  731. chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
  732. /* Mirrored table avilable ? */
  733. if (md) {
  734. if (td->pages[i] == -1 && md->pages[i] == -1) {
  735. writeops = 0x03;
  736. goto create;
  737. }
  738. if (td->pages[i] == -1) {
  739. rd = md;
  740. td->version[i] = md->version[i];
  741. writeops = 1;
  742. goto writecheck;
  743. }
  744. if (md->pages[i] == -1) {
  745. rd = td;
  746. md->version[i] = td->version[i];
  747. writeops = 2;
  748. goto writecheck;
  749. }
  750. if (td->version[i] == md->version[i]) {
  751. rd = td;
  752. if (!(td->options & NAND_BBT_VERSION))
  753. rd2 = md;
  754. goto writecheck;
  755. }
  756. if (((int8_t) (td->version[i] - md->version[i])) > 0) {
  757. rd = td;
  758. md->version[i] = td->version[i];
  759. writeops = 2;
  760. } else {
  761. rd = md;
  762. td->version[i] = md->version[i];
  763. writeops = 1;
  764. }
  765. goto writecheck;
  766. } else {
  767. if (td->pages[i] == -1) {
  768. writeops = 0x01;
  769. goto create;
  770. }
  771. rd = td;
  772. goto writecheck;
  773. }
  774. create:
  775. /* Create the bad block table by scanning the device ? */
  776. if (!(td->options & NAND_BBT_CREATE))
  777. continue;
  778. /* Create the table in memory by scanning the chip(s) */
  779. create_bbt(mtd, buf, bd, chipsel);
  780. td->version[i] = 1;
  781. if (md)
  782. md->version[i] = 1;
  783. writecheck:
  784. /* read back first ? */
  785. if (rd)
  786. read_abs_bbt(mtd, buf, rd, chipsel);
  787. /* If they weren't versioned, read both. */
  788. if (rd2)
  789. read_abs_bbt(mtd, buf, rd2, chipsel);
  790. /* Write the bad block table to the device ? */
  791. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  792. res = write_bbt(mtd, buf, td, md, chipsel);
  793. if (res < 0)
  794. return res;
  795. }
  796. /* Write the mirror bad block table to the device ? */
  797. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  798. res = write_bbt(mtd, buf, md, td, chipsel);
  799. if (res < 0)
  800. return res;
  801. }
  802. }
  803. return 0;
  804. }
  805. /**
  806. * mark_bbt_regions - [GENERIC] mark the bad block table regions
  807. * @mtd: MTD device structure
  808. * @td: bad block table descriptor
  809. *
  810. * The bad block table regions are marked as "bad" to prevent
  811. * accidental erasures / writes. The regions are identified by
  812. * the mark 0x02.
  813. */
  814. static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
  815. {
  816. struct nand_chip *this = mtd->priv;
  817. int i, j, chips, block, nrblocks, update;
  818. uint8_t oldval, newval;
  819. /* Do we have a bbt per chip ? */
  820. if (td->options & NAND_BBT_PERCHIP) {
  821. chips = this->numchips;
  822. nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
  823. } else {
  824. chips = 1;
  825. nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
  826. }
  827. for (i = 0; i < chips; i++) {
  828. if ((td->options & NAND_BBT_ABSPAGE) ||
  829. !(td->options & NAND_BBT_WRITE)) {
  830. if (td->pages[i] == -1)
  831. continue;
  832. block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
  833. block <<= 1;
  834. oldval = this->bbt[(block >> 3)];
  835. newval = oldval | (0x2 << (block & 0x06));
  836. this->bbt[(block >> 3)] = newval;
  837. if ((oldval != newval) && td->reserved_block_code)
  838. nand_update_bbt(mtd, block << (this->bbt_erase_shift - 1));
  839. continue;
  840. }
  841. update = 0;
  842. if (td->options & NAND_BBT_LASTBLOCK)
  843. block = ((i + 1) * nrblocks) - td->maxblocks;
  844. else
  845. block = i * nrblocks;
  846. block <<= 1;
  847. for (j = 0; j < td->maxblocks; j++) {
  848. oldval = this->bbt[(block >> 3)];
  849. newval = oldval | (0x2 << (block & 0x06));
  850. this->bbt[(block >> 3)] = newval;
  851. if (oldval != newval)
  852. update = 1;
  853. block += 2;
  854. }
  855. /* If we want reserved blocks to be recorded to flash, and some
  856. new ones have been marked, then we need to update the stored
  857. bbts. This should only happen once. */
  858. if (update && td->reserved_block_code)
  859. nand_update_bbt(mtd, (block - 2) << (this->bbt_erase_shift - 1));
  860. }
  861. }
  862. /**
  863. * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
  864. * @mtd: MTD device structure
  865. * @bd: descriptor for the good/bad block search pattern
  866. *
  867. * The function checks, if a bad block table(s) is/are already
  868. * available. If not it scans the device for manufacturer
  869. * marked good / bad blocks and writes the bad block table(s) to
  870. * the selected place.
  871. *
  872. * The bad block table memory is allocated here. It must be freed
  873. * by calling the nand_free_bbt function.
  874. *
  875. */
  876. int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  877. {
  878. struct nand_chip *this = mtd->priv;
  879. int len, res = 0;
  880. uint8_t *buf;
  881. struct nand_bbt_descr *td = this->bbt_td;
  882. struct nand_bbt_descr *md = this->bbt_md;
  883. len = mtd->size >> (this->bbt_erase_shift + 2);
  884. /* Allocate memory (2bit per block) and clear the memory bad block table */
  885. this->bbt = kzalloc(len, GFP_KERNEL);
  886. if (!this->bbt) {
  887. printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
  888. return -ENOMEM;
  889. }
  890. /* If no primary table decriptor is given, scan the device
  891. * to build a memory based bad block table
  892. */
  893. if (!td) {
  894. if ((res = nand_memory_bbt(mtd, bd))) {
  895. printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
  896. kfree(this->bbt);
  897. this->bbt = NULL;
  898. }
  899. return res;
  900. }
  901. /* Allocate a temporary buffer for one eraseblock incl. oob */
  902. len = (1 << this->bbt_erase_shift);
  903. len += (len >> this->page_shift) * mtd->oobsize;
  904. buf = vmalloc(len);
  905. if (!buf) {
  906. printk(KERN_ERR "nand_bbt: Out of memory\n");
  907. kfree(this->bbt);
  908. this->bbt = NULL;
  909. return -ENOMEM;
  910. }
  911. /* Is the bbt at a given page ? */
  912. if (td->options & NAND_BBT_ABSPAGE) {
  913. res = read_abs_bbts(mtd, buf, td, md);
  914. } else {
  915. /* Search the bad block table using a pattern in oob */
  916. res = search_read_bbts(mtd, buf, td, md);
  917. }
  918. if (res)
  919. res = check_create(mtd, buf, bd);
  920. /* Prevent the bbt regions from erasing / writing */
  921. mark_bbt_region(mtd, td);
  922. if (md)
  923. mark_bbt_region(mtd, md);
  924. vfree(buf);
  925. return res;
  926. }
  927. /**
  928. * nand_update_bbt - [NAND Interface] update bad block table(s)
  929. * @mtd: MTD device structure
  930. * @offs: the offset of the newly marked block
  931. *
  932. * The function updates the bad block table(s)
  933. */
  934. int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
  935. {
  936. struct nand_chip *this = mtd->priv;
  937. int len, res = 0, writeops = 0;
  938. int chip, chipsel;
  939. uint8_t *buf;
  940. struct nand_bbt_descr *td = this->bbt_td;
  941. struct nand_bbt_descr *md = this->bbt_md;
  942. if (!this->bbt || !td)
  943. return -EINVAL;
  944. len = mtd->size >> (this->bbt_erase_shift + 2);
  945. /* Allocate a temporary buffer for one eraseblock incl. oob */
  946. len = (1 << this->bbt_erase_shift);
  947. len += (len >> this->page_shift) * mtd->oobsize;
  948. buf = kmalloc(len, GFP_KERNEL);
  949. if (!buf) {
  950. printk(KERN_ERR "nand_update_bbt: Out of memory\n");
  951. return -ENOMEM;
  952. }
  953. writeops = md != NULL ? 0x03 : 0x01;
  954. /* Do we have a bbt per chip ? */
  955. if (td->options & NAND_BBT_PERCHIP) {
  956. chip = (int)(offs >> this->chip_shift);
  957. chipsel = chip;
  958. } else {
  959. chip = 0;
  960. chipsel = -1;
  961. }
  962. td->version[chip]++;
  963. if (md)
  964. md->version[chip]++;
  965. /* Write the bad block table to the device ? */
  966. if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
  967. res = write_bbt(mtd, buf, td, md, chipsel);
  968. if (res < 0)
  969. goto out;
  970. }
  971. /* Write the mirror bad block table to the device ? */
  972. if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
  973. res = write_bbt(mtd, buf, md, td, chipsel);
  974. }
  975. out:
  976. kfree(buf);
  977. return res;
  978. }
  979. /* Define some generic bad / good block scan pattern which are used
  980. * while scanning a device for factory marked good / bad blocks. */
  981. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  982. static struct nand_bbt_descr smallpage_memorybased = {
  983. .options = NAND_BBT_SCAN2NDPAGE,
  984. .offs = 5,
  985. .len = 1,
  986. .pattern = scan_ff_pattern
  987. };
  988. static struct nand_bbt_descr largepage_memorybased = {
  989. .options = 0,
  990. .offs = 0,
  991. .len = 2,
  992. .pattern = scan_ff_pattern
  993. };
  994. static struct nand_bbt_descr smallpage_flashbased = {
  995. .options = NAND_BBT_SCAN2NDPAGE,
  996. .offs = 5,
  997. .len = 1,
  998. .pattern = scan_ff_pattern
  999. };
  1000. static struct nand_bbt_descr largepage_flashbased = {
  1001. .options = NAND_BBT_SCAN2NDPAGE,
  1002. .offs = 0,
  1003. .len = 2,
  1004. .pattern = scan_ff_pattern
  1005. };
  1006. static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
  1007. static struct nand_bbt_descr agand_flashbased = {
  1008. .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
  1009. .offs = 0x20,
  1010. .len = 6,
  1011. .pattern = scan_agand_pattern
  1012. };
  1013. /* Generic flash bbt decriptors
  1014. */
  1015. static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
  1016. static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
  1017. static struct nand_bbt_descr bbt_main_descr = {
  1018. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1019. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1020. .offs = 8,
  1021. .len = 4,
  1022. .veroffs = 12,
  1023. .maxblocks = 4,
  1024. .pattern = bbt_pattern
  1025. };
  1026. static struct nand_bbt_descr bbt_mirror_descr = {
  1027. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1028. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1029. .offs = 8,
  1030. .len = 4,
  1031. .veroffs = 12,
  1032. .maxblocks = 4,
  1033. .pattern = mirror_pattern
  1034. };
  1035. /**
  1036. * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
  1037. * @mtd: MTD device structure
  1038. *
  1039. * This function selects the default bad block table
  1040. * support for the device and calls the nand_scan_bbt function
  1041. *
  1042. */
  1043. int nand_default_bbt(struct mtd_info *mtd)
  1044. {
  1045. struct nand_chip *this = mtd->priv;
  1046. /* Default for AG-AND. We must use a flash based
  1047. * bad block table as the devices have factory marked
  1048. * _good_ blocks. Erasing those blocks leads to loss
  1049. * of the good / bad information, so we _must_ store
  1050. * this information in a good / bad table during
  1051. * startup
  1052. */
  1053. if (this->options & NAND_IS_AND) {
  1054. /* Use the default pattern descriptors */
  1055. if (!this->bbt_td) {
  1056. this->bbt_td = &bbt_main_descr;
  1057. this->bbt_md = &bbt_mirror_descr;
  1058. }
  1059. this->options |= NAND_USE_FLASH_BBT;
  1060. return nand_scan_bbt(mtd, &agand_flashbased);
  1061. }
  1062. /* Is a flash based bad block table requested ? */
  1063. if (this->options & NAND_USE_FLASH_BBT) {
  1064. /* Use the default pattern descriptors */
  1065. if (!this->bbt_td) {
  1066. this->bbt_td = &bbt_main_descr;
  1067. this->bbt_md = &bbt_mirror_descr;
  1068. }
  1069. if (!this->badblock_pattern) {
  1070. this->badblock_pattern = (mtd->writesize > 512) ? &largepage_flashbased : &smallpage_flashbased;
  1071. }
  1072. } else {
  1073. this->bbt_td = NULL;
  1074. this->bbt_md = NULL;
  1075. if (!this->badblock_pattern) {
  1076. this->badblock_pattern = (mtd->writesize > 512) ?
  1077. &largepage_memorybased : &smallpage_memorybased;
  1078. }
  1079. }
  1080. return nand_scan_bbt(mtd, this->badblock_pattern);
  1081. }
  1082. /**
  1083. * nand_isbad_bbt - [NAND Interface] Check if a block is bad
  1084. * @mtd: MTD device structure
  1085. * @offs: offset in the device
  1086. * @allowbbt: allow access to bad block table region
  1087. *
  1088. */
  1089. int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
  1090. {
  1091. struct nand_chip *this = mtd->priv;
  1092. int block;
  1093. uint8_t res;
  1094. /* Get block number * 2 */
  1095. block = (int)(offs >> (this->bbt_erase_shift - 1));
  1096. res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
  1097. MTDDEBUG (MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: "
  1098. "(block %d) 0x%02x\n", (unsigned int)offs, res, block >> 1);
  1099. switch ((int)res) {
  1100. case 0x00:
  1101. return 0;
  1102. case 0x01:
  1103. return 1;
  1104. case 0x02:
  1105. return allowbbt ? 0 : 1;
  1106. }
  1107. return 1;
  1108. }
  1109. /* XXX U-BOOT XXX */
  1110. #if 0
  1111. EXPORT_SYMBOL(nand_scan_bbt);
  1112. EXPORT_SYMBOL(nand_default_bbt);
  1113. #endif
  1114. #endif