1cfcc706cSMiquel Raynal /* 2cfcc706cSMiquel Raynal * Overview: 3cfcc706cSMiquel Raynal * This is the generic MTD driver for NAND flash devices. It should be 4cfcc706cSMiquel Raynal * capable of working with almost all NAND chips currently available. 5cfcc706cSMiquel Raynal * 6cfcc706cSMiquel Raynal * Additional technical information is available on 7cfcc706cSMiquel Raynal * http://www.linux-mtd.infradead.org/doc/nand.html 8cfcc706cSMiquel Raynal * 9cfcc706cSMiquel Raynal * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) 10cfcc706cSMiquel Raynal * 2002-2006 Thomas Gleixner (tglx@linutronix.de) 11cfcc706cSMiquel Raynal * 12cfcc706cSMiquel Raynal * Credits: 13cfcc706cSMiquel Raynal * David Woodhouse for adding multichip support 14cfcc706cSMiquel Raynal * 15cfcc706cSMiquel Raynal * Aleph One Ltd. and Toby Churchill Ltd. for supporting the 16cfcc706cSMiquel Raynal * rework for 2K page size chips 17cfcc706cSMiquel Raynal * 18cfcc706cSMiquel Raynal * TODO: 19cfcc706cSMiquel Raynal * Enable cached programming for 2k page size chips 20cfcc706cSMiquel Raynal * Check, if mtd->ecctype should be set to MTD_ECC_HW 21cfcc706cSMiquel Raynal * if we have HW ECC support. 22cfcc706cSMiquel Raynal * BBT table is not serialized, has to be fixed 23cfcc706cSMiquel Raynal * 24cfcc706cSMiquel Raynal * This program is free software; you can redistribute it and/or modify 25cfcc706cSMiquel Raynal * it under the terms of the GNU General Public License version 2 as 26cfcc706cSMiquel Raynal * published by the Free Software Foundation. 27cfcc706cSMiquel Raynal * 28cfcc706cSMiquel Raynal */ 29cfcc706cSMiquel Raynal 30cfcc706cSMiquel Raynal #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 31cfcc706cSMiquel Raynal #include <common.h> 32cfcc706cSMiquel Raynal #if CONFIG_IS_ENABLED(OF_CONTROL) 33cfcc706cSMiquel Raynal #include <fdtdec.h> 34cfcc706cSMiquel Raynal #endif 35cfcc706cSMiquel Raynal #include <malloc.h> 36cfcc706cSMiquel Raynal #include <watchdog.h> 37cfcc706cSMiquel Raynal #include <linux/err.h> 38cfcc706cSMiquel Raynal #include <linux/compat.h> 39cfcc706cSMiquel Raynal #include <linux/mtd/mtd.h> 40cfcc706cSMiquel Raynal #include <linux/mtd/rawnand.h> 41cfcc706cSMiquel Raynal #include <linux/mtd/nand_ecc.h> 42cfcc706cSMiquel Raynal #include <linux/mtd/nand_bch.h> 43cfcc706cSMiquel Raynal #ifdef CONFIG_MTD_PARTITIONS 44cfcc706cSMiquel Raynal #include <linux/mtd/partitions.h> 45cfcc706cSMiquel Raynal #endif 46cfcc706cSMiquel Raynal #include <asm/io.h> 47cfcc706cSMiquel Raynal #include <linux/errno.h> 48cfcc706cSMiquel Raynal 49cfcc706cSMiquel Raynal /* Define default oob placement schemes for large and small page devices */ 50*50c9e2f7SStefan Agner #ifdef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT 51cfcc706cSMiquel Raynal static struct nand_ecclayout nand_oob_8 = { 52cfcc706cSMiquel Raynal .eccbytes = 3, 53cfcc706cSMiquel Raynal .eccpos = {0, 1, 2}, 54cfcc706cSMiquel Raynal .oobfree = { 55cfcc706cSMiquel Raynal {.offset = 3, 56cfcc706cSMiquel Raynal .length = 2}, 57cfcc706cSMiquel Raynal {.offset = 6, 58cfcc706cSMiquel Raynal .length = 2} } 59cfcc706cSMiquel Raynal }; 60cfcc706cSMiquel Raynal 61cfcc706cSMiquel Raynal static struct nand_ecclayout nand_oob_16 = { 62cfcc706cSMiquel Raynal .eccbytes = 6, 63cfcc706cSMiquel Raynal .eccpos = {0, 1, 2, 3, 6, 7}, 64cfcc706cSMiquel Raynal .oobfree = { 65cfcc706cSMiquel Raynal {.offset = 8, 66cfcc706cSMiquel Raynal . length = 8} } 67cfcc706cSMiquel Raynal }; 68cfcc706cSMiquel Raynal 69cfcc706cSMiquel Raynal static struct nand_ecclayout nand_oob_64 = { 70cfcc706cSMiquel Raynal .eccbytes = 24, 71cfcc706cSMiquel Raynal .eccpos = { 72cfcc706cSMiquel Raynal 40, 41, 42, 43, 44, 45, 46, 47, 73cfcc706cSMiquel Raynal 48, 49, 50, 51, 52, 53, 54, 55, 74cfcc706cSMiquel Raynal 56, 57, 58, 59, 60, 61, 62, 63}, 75cfcc706cSMiquel Raynal .oobfree = { 76cfcc706cSMiquel Raynal {.offset = 2, 77cfcc706cSMiquel Raynal .length = 38} } 78cfcc706cSMiquel Raynal }; 79cfcc706cSMiquel Raynal 80cfcc706cSMiquel Raynal static struct nand_ecclayout nand_oob_128 = { 81cfcc706cSMiquel Raynal .eccbytes = 48, 82cfcc706cSMiquel Raynal .eccpos = { 83cfcc706cSMiquel Raynal 80, 81, 82, 83, 84, 85, 86, 87, 84cfcc706cSMiquel Raynal 88, 89, 90, 91, 92, 93, 94, 95, 85cfcc706cSMiquel Raynal 96, 97, 98, 99, 100, 101, 102, 103, 86cfcc706cSMiquel Raynal 104, 105, 106, 107, 108, 109, 110, 111, 87cfcc706cSMiquel Raynal 112, 113, 114, 115, 116, 117, 118, 119, 88cfcc706cSMiquel Raynal 120, 121, 122, 123, 124, 125, 126, 127}, 89cfcc706cSMiquel Raynal .oobfree = { 90cfcc706cSMiquel Raynal {.offset = 2, 91cfcc706cSMiquel Raynal .length = 78} } 92cfcc706cSMiquel Raynal }; 93*50c9e2f7SStefan Agner #endif 94cfcc706cSMiquel Raynal 95cfcc706cSMiquel Raynal static int nand_get_device(struct mtd_info *mtd, int new_state); 96cfcc706cSMiquel Raynal 97cfcc706cSMiquel Raynal static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, 98cfcc706cSMiquel Raynal struct mtd_oob_ops *ops); 99cfcc706cSMiquel Raynal 100cfcc706cSMiquel Raynal /* 101cfcc706cSMiquel Raynal * For devices which display every fart in the system on a separate LED. Is 102cfcc706cSMiquel Raynal * compiled away when LED support is disabled. 103cfcc706cSMiquel Raynal */ 104cfcc706cSMiquel Raynal DEFINE_LED_TRIGGER(nand_led_trigger); 105cfcc706cSMiquel Raynal 106cfcc706cSMiquel Raynal static int check_offs_len(struct mtd_info *mtd, 107cfcc706cSMiquel Raynal loff_t ofs, uint64_t len) 108cfcc706cSMiquel Raynal { 109cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 110cfcc706cSMiquel Raynal int ret = 0; 111cfcc706cSMiquel Raynal 112cfcc706cSMiquel Raynal /* Start address must align on block boundary */ 113cfcc706cSMiquel Raynal if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { 114cfcc706cSMiquel Raynal pr_debug("%s: unaligned address\n", __func__); 115cfcc706cSMiquel Raynal ret = -EINVAL; 116cfcc706cSMiquel Raynal } 117cfcc706cSMiquel Raynal 118cfcc706cSMiquel Raynal /* Length must align on block boundary */ 119cfcc706cSMiquel Raynal if (len & ((1ULL << chip->phys_erase_shift) - 1)) { 120cfcc706cSMiquel Raynal pr_debug("%s: length not block aligned\n", __func__); 121cfcc706cSMiquel Raynal ret = -EINVAL; 122cfcc706cSMiquel Raynal } 123cfcc706cSMiquel Raynal 124cfcc706cSMiquel Raynal return ret; 125cfcc706cSMiquel Raynal } 126cfcc706cSMiquel Raynal 127cfcc706cSMiquel Raynal /** 128cfcc706cSMiquel Raynal * nand_release_device - [GENERIC] release chip 129cfcc706cSMiquel Raynal * @mtd: MTD device structure 130cfcc706cSMiquel Raynal * 131cfcc706cSMiquel Raynal * Release chip lock and wake up anyone waiting on the device. 132cfcc706cSMiquel Raynal */ 133cfcc706cSMiquel Raynal static void nand_release_device(struct mtd_info *mtd) 134cfcc706cSMiquel Raynal { 135cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 136cfcc706cSMiquel Raynal 137cfcc706cSMiquel Raynal /* De-select the NAND device */ 138cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 139cfcc706cSMiquel Raynal } 140cfcc706cSMiquel Raynal 141cfcc706cSMiquel Raynal /** 142cfcc706cSMiquel Raynal * nand_read_byte - [DEFAULT] read one byte from the chip 143cfcc706cSMiquel Raynal * @mtd: MTD device structure 144cfcc706cSMiquel Raynal * 145cfcc706cSMiquel Raynal * Default read function for 8bit buswidth 146cfcc706cSMiquel Raynal */ 147cfcc706cSMiquel Raynal uint8_t nand_read_byte(struct mtd_info *mtd) 148cfcc706cSMiquel Raynal { 149cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 150cfcc706cSMiquel Raynal return readb(chip->IO_ADDR_R); 151cfcc706cSMiquel Raynal } 152cfcc706cSMiquel Raynal 153cfcc706cSMiquel Raynal /** 154cfcc706cSMiquel Raynal * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip 155cfcc706cSMiquel Raynal * @mtd: MTD device structure 156cfcc706cSMiquel Raynal * 157cfcc706cSMiquel Raynal * Default read function for 16bit buswidth with endianness conversion. 158cfcc706cSMiquel Raynal * 159cfcc706cSMiquel Raynal */ 160cfcc706cSMiquel Raynal static uint8_t nand_read_byte16(struct mtd_info *mtd) 161cfcc706cSMiquel Raynal { 162cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 163cfcc706cSMiquel Raynal return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); 164cfcc706cSMiquel Raynal } 165cfcc706cSMiquel Raynal 166cfcc706cSMiquel Raynal /** 167cfcc706cSMiquel Raynal * nand_read_word - [DEFAULT] read one word from the chip 168cfcc706cSMiquel Raynal * @mtd: MTD device structure 169cfcc706cSMiquel Raynal * 170cfcc706cSMiquel Raynal * Default read function for 16bit buswidth without endianness conversion. 171cfcc706cSMiquel Raynal */ 172cfcc706cSMiquel Raynal static u16 nand_read_word(struct mtd_info *mtd) 173cfcc706cSMiquel Raynal { 174cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 175cfcc706cSMiquel Raynal return readw(chip->IO_ADDR_R); 176cfcc706cSMiquel Raynal } 177cfcc706cSMiquel Raynal 178cfcc706cSMiquel Raynal /** 179cfcc706cSMiquel Raynal * nand_select_chip - [DEFAULT] control CE line 180cfcc706cSMiquel Raynal * @mtd: MTD device structure 181cfcc706cSMiquel Raynal * @chipnr: chipnumber to select, -1 for deselect 182cfcc706cSMiquel Raynal * 183cfcc706cSMiquel Raynal * Default select function for 1 chip devices. 184cfcc706cSMiquel Raynal */ 185cfcc706cSMiquel Raynal static void nand_select_chip(struct mtd_info *mtd, int chipnr) 186cfcc706cSMiquel Raynal { 187cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 188cfcc706cSMiquel Raynal 189cfcc706cSMiquel Raynal switch (chipnr) { 190cfcc706cSMiquel Raynal case -1: 191cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); 192cfcc706cSMiquel Raynal break; 193cfcc706cSMiquel Raynal case 0: 194cfcc706cSMiquel Raynal break; 195cfcc706cSMiquel Raynal 196cfcc706cSMiquel Raynal default: 197cfcc706cSMiquel Raynal BUG(); 198cfcc706cSMiquel Raynal } 199cfcc706cSMiquel Raynal } 200cfcc706cSMiquel Raynal 201cfcc706cSMiquel Raynal /** 202cfcc706cSMiquel Raynal * nand_write_byte - [DEFAULT] write single byte to chip 203cfcc706cSMiquel Raynal * @mtd: MTD device structure 204cfcc706cSMiquel Raynal * @byte: value to write 205cfcc706cSMiquel Raynal * 206cfcc706cSMiquel Raynal * Default function to write a byte to I/O[7:0] 207cfcc706cSMiquel Raynal */ 208cfcc706cSMiquel Raynal static void nand_write_byte(struct mtd_info *mtd, uint8_t byte) 209cfcc706cSMiquel Raynal { 210cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 211cfcc706cSMiquel Raynal 212cfcc706cSMiquel Raynal chip->write_buf(mtd, &byte, 1); 213cfcc706cSMiquel Raynal } 214cfcc706cSMiquel Raynal 215cfcc706cSMiquel Raynal /** 216cfcc706cSMiquel Raynal * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16 217cfcc706cSMiquel Raynal * @mtd: MTD device structure 218cfcc706cSMiquel Raynal * @byte: value to write 219cfcc706cSMiquel Raynal * 220cfcc706cSMiquel Raynal * Default function to write a byte to I/O[7:0] on a 16-bit wide chip. 221cfcc706cSMiquel Raynal */ 222cfcc706cSMiquel Raynal static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte) 223cfcc706cSMiquel Raynal { 224cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 225cfcc706cSMiquel Raynal uint16_t word = byte; 226cfcc706cSMiquel Raynal 227cfcc706cSMiquel Raynal /* 228cfcc706cSMiquel Raynal * It's not entirely clear what should happen to I/O[15:8] when writing 229cfcc706cSMiquel Raynal * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads: 230cfcc706cSMiquel Raynal * 231cfcc706cSMiquel Raynal * When the host supports a 16-bit bus width, only data is 232cfcc706cSMiquel Raynal * transferred at the 16-bit width. All address and command line 233cfcc706cSMiquel Raynal * transfers shall use only the lower 8-bits of the data bus. During 234cfcc706cSMiquel Raynal * command transfers, the host may place any value on the upper 235cfcc706cSMiquel Raynal * 8-bits of the data bus. During address transfers, the host shall 236cfcc706cSMiquel Raynal * set the upper 8-bits of the data bus to 00h. 237cfcc706cSMiquel Raynal * 238cfcc706cSMiquel Raynal * One user of the write_byte callback is nand_onfi_set_features. The 239cfcc706cSMiquel Raynal * four parameters are specified to be written to I/O[7:0], but this is 240cfcc706cSMiquel Raynal * neither an address nor a command transfer. Let's assume a 0 on the 241cfcc706cSMiquel Raynal * upper I/O lines is OK. 242cfcc706cSMiquel Raynal */ 243cfcc706cSMiquel Raynal chip->write_buf(mtd, (uint8_t *)&word, 2); 244cfcc706cSMiquel Raynal } 245cfcc706cSMiquel Raynal 246cfcc706cSMiquel Raynal static void iowrite8_rep(void *addr, const uint8_t *buf, int len) 247cfcc706cSMiquel Raynal { 248cfcc706cSMiquel Raynal int i; 249cfcc706cSMiquel Raynal 250cfcc706cSMiquel Raynal for (i = 0; i < len; i++) 251cfcc706cSMiquel Raynal writeb(buf[i], addr); 252cfcc706cSMiquel Raynal } 253cfcc706cSMiquel Raynal static void ioread8_rep(void *addr, uint8_t *buf, int len) 254cfcc706cSMiquel Raynal { 255cfcc706cSMiquel Raynal int i; 256cfcc706cSMiquel Raynal 257cfcc706cSMiquel Raynal for (i = 0; i < len; i++) 258cfcc706cSMiquel Raynal buf[i] = readb(addr); 259cfcc706cSMiquel Raynal } 260cfcc706cSMiquel Raynal 261cfcc706cSMiquel Raynal static void ioread16_rep(void *addr, void *buf, int len) 262cfcc706cSMiquel Raynal { 263cfcc706cSMiquel Raynal int i; 264cfcc706cSMiquel Raynal u16 *p = (u16 *) buf; 265cfcc706cSMiquel Raynal 266cfcc706cSMiquel Raynal for (i = 0; i < len; i++) 267cfcc706cSMiquel Raynal p[i] = readw(addr); 268cfcc706cSMiquel Raynal } 269cfcc706cSMiquel Raynal 270cfcc706cSMiquel Raynal static void iowrite16_rep(void *addr, void *buf, int len) 271cfcc706cSMiquel Raynal { 272cfcc706cSMiquel Raynal int i; 273cfcc706cSMiquel Raynal u16 *p = (u16 *) buf; 274cfcc706cSMiquel Raynal 275cfcc706cSMiquel Raynal for (i = 0; i < len; i++) 276cfcc706cSMiquel Raynal writew(p[i], addr); 277cfcc706cSMiquel Raynal } 278cfcc706cSMiquel Raynal 279cfcc706cSMiquel Raynal /** 280cfcc706cSMiquel Raynal * nand_write_buf - [DEFAULT] write buffer to chip 281cfcc706cSMiquel Raynal * @mtd: MTD device structure 282cfcc706cSMiquel Raynal * @buf: data buffer 283cfcc706cSMiquel Raynal * @len: number of bytes to write 284cfcc706cSMiquel Raynal * 285cfcc706cSMiquel Raynal * Default write function for 8bit buswidth. 286cfcc706cSMiquel Raynal */ 287cfcc706cSMiquel Raynal void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) 288cfcc706cSMiquel Raynal { 289cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 290cfcc706cSMiquel Raynal 291cfcc706cSMiquel Raynal iowrite8_rep(chip->IO_ADDR_W, buf, len); 292cfcc706cSMiquel Raynal } 293cfcc706cSMiquel Raynal 294cfcc706cSMiquel Raynal /** 295cfcc706cSMiquel Raynal * nand_read_buf - [DEFAULT] read chip data into buffer 296cfcc706cSMiquel Raynal * @mtd: MTD device structure 297cfcc706cSMiquel Raynal * @buf: buffer to store date 298cfcc706cSMiquel Raynal * @len: number of bytes to read 299cfcc706cSMiquel Raynal * 300cfcc706cSMiquel Raynal * Default read function for 8bit buswidth. 301cfcc706cSMiquel Raynal */ 302cfcc706cSMiquel Raynal void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) 303cfcc706cSMiquel Raynal { 304cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 305cfcc706cSMiquel Raynal 306cfcc706cSMiquel Raynal ioread8_rep(chip->IO_ADDR_R, buf, len); 307cfcc706cSMiquel Raynal } 308cfcc706cSMiquel Raynal 309cfcc706cSMiquel Raynal /** 310cfcc706cSMiquel Raynal * nand_write_buf16 - [DEFAULT] write buffer to chip 311cfcc706cSMiquel Raynal * @mtd: MTD device structure 312cfcc706cSMiquel Raynal * @buf: data buffer 313cfcc706cSMiquel Raynal * @len: number of bytes to write 314cfcc706cSMiquel Raynal * 315cfcc706cSMiquel Raynal * Default write function for 16bit buswidth. 316cfcc706cSMiquel Raynal */ 317cfcc706cSMiquel Raynal void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) 318cfcc706cSMiquel Raynal { 319cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 320cfcc706cSMiquel Raynal u16 *p = (u16 *) buf; 321cfcc706cSMiquel Raynal 322cfcc706cSMiquel Raynal iowrite16_rep(chip->IO_ADDR_W, p, len >> 1); 323cfcc706cSMiquel Raynal } 324cfcc706cSMiquel Raynal 325cfcc706cSMiquel Raynal /** 326cfcc706cSMiquel Raynal * nand_read_buf16 - [DEFAULT] read chip data into buffer 327cfcc706cSMiquel Raynal * @mtd: MTD device structure 328cfcc706cSMiquel Raynal * @buf: buffer to store date 329cfcc706cSMiquel Raynal * @len: number of bytes to read 330cfcc706cSMiquel Raynal * 331cfcc706cSMiquel Raynal * Default read function for 16bit buswidth. 332cfcc706cSMiquel Raynal */ 333cfcc706cSMiquel Raynal void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) 334cfcc706cSMiquel Raynal { 335cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 336cfcc706cSMiquel Raynal u16 *p = (u16 *) buf; 337cfcc706cSMiquel Raynal 338cfcc706cSMiquel Raynal ioread16_rep(chip->IO_ADDR_R, p, len >> 1); 339cfcc706cSMiquel Raynal } 340cfcc706cSMiquel Raynal 341cfcc706cSMiquel Raynal /** 342cfcc706cSMiquel Raynal * nand_block_bad - [DEFAULT] Read bad block marker from the chip 343cfcc706cSMiquel Raynal * @mtd: MTD device structure 344cfcc706cSMiquel Raynal * @ofs: offset from device start 345cfcc706cSMiquel Raynal * 346cfcc706cSMiquel Raynal * Check, if the block is bad. 347cfcc706cSMiquel Raynal */ 348cfcc706cSMiquel Raynal static int nand_block_bad(struct mtd_info *mtd, loff_t ofs) 349cfcc706cSMiquel Raynal { 350cfcc706cSMiquel Raynal int page, res = 0, i = 0; 351cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 352cfcc706cSMiquel Raynal u16 bad; 353cfcc706cSMiquel Raynal 354cfcc706cSMiquel Raynal if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) 355cfcc706cSMiquel Raynal ofs += mtd->erasesize - mtd->writesize; 356cfcc706cSMiquel Raynal 357cfcc706cSMiquel Raynal page = (int)(ofs >> chip->page_shift) & chip->pagemask; 358cfcc706cSMiquel Raynal 359cfcc706cSMiquel Raynal do { 360cfcc706cSMiquel Raynal if (chip->options & NAND_BUSWIDTH_16) { 361cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READOOB, 362cfcc706cSMiquel Raynal chip->badblockpos & 0xFE, page); 363cfcc706cSMiquel Raynal bad = cpu_to_le16(chip->read_word(mtd)); 364cfcc706cSMiquel Raynal if (chip->badblockpos & 0x1) 365cfcc706cSMiquel Raynal bad >>= 8; 366cfcc706cSMiquel Raynal else 367cfcc706cSMiquel Raynal bad &= 0xFF; 368cfcc706cSMiquel Raynal } else { 369cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, 370cfcc706cSMiquel Raynal page); 371cfcc706cSMiquel Raynal bad = chip->read_byte(mtd); 372cfcc706cSMiquel Raynal } 373cfcc706cSMiquel Raynal 374cfcc706cSMiquel Raynal if (likely(chip->badblockbits == 8)) 375cfcc706cSMiquel Raynal res = bad != 0xFF; 376cfcc706cSMiquel Raynal else 377cfcc706cSMiquel Raynal res = hweight8(bad) < chip->badblockbits; 378cfcc706cSMiquel Raynal ofs += mtd->writesize; 379cfcc706cSMiquel Raynal page = (int)(ofs >> chip->page_shift) & chip->pagemask; 380cfcc706cSMiquel Raynal i++; 381cfcc706cSMiquel Raynal } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE)); 382cfcc706cSMiquel Raynal 383cfcc706cSMiquel Raynal return res; 384cfcc706cSMiquel Raynal } 385cfcc706cSMiquel Raynal 386cfcc706cSMiquel Raynal /** 387cfcc706cSMiquel Raynal * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker 388cfcc706cSMiquel Raynal * @mtd: MTD device structure 389cfcc706cSMiquel Raynal * @ofs: offset from device start 390cfcc706cSMiquel Raynal * 391cfcc706cSMiquel Raynal * This is the default implementation, which can be overridden by a hardware 392cfcc706cSMiquel Raynal * specific driver. It provides the details for writing a bad block marker to a 393cfcc706cSMiquel Raynal * block. 394cfcc706cSMiquel Raynal */ 395cfcc706cSMiquel Raynal static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) 396cfcc706cSMiquel Raynal { 397cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 398cfcc706cSMiquel Raynal struct mtd_oob_ops ops; 399cfcc706cSMiquel Raynal uint8_t buf[2] = { 0, 0 }; 400cfcc706cSMiquel Raynal int ret = 0, res, i = 0; 401cfcc706cSMiquel Raynal 402cfcc706cSMiquel Raynal memset(&ops, 0, sizeof(ops)); 403cfcc706cSMiquel Raynal ops.oobbuf = buf; 404cfcc706cSMiquel Raynal ops.ooboffs = chip->badblockpos; 405cfcc706cSMiquel Raynal if (chip->options & NAND_BUSWIDTH_16) { 406cfcc706cSMiquel Raynal ops.ooboffs &= ~0x01; 407cfcc706cSMiquel Raynal ops.len = ops.ooblen = 2; 408cfcc706cSMiquel Raynal } else { 409cfcc706cSMiquel Raynal ops.len = ops.ooblen = 1; 410cfcc706cSMiquel Raynal } 411cfcc706cSMiquel Raynal ops.mode = MTD_OPS_PLACE_OOB; 412cfcc706cSMiquel Raynal 413cfcc706cSMiquel Raynal /* Write to first/last page(s) if necessary */ 414cfcc706cSMiquel Raynal if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) 415cfcc706cSMiquel Raynal ofs += mtd->erasesize - mtd->writesize; 416cfcc706cSMiquel Raynal do { 417cfcc706cSMiquel Raynal res = nand_do_write_oob(mtd, ofs, &ops); 418cfcc706cSMiquel Raynal if (!ret) 419cfcc706cSMiquel Raynal ret = res; 420cfcc706cSMiquel Raynal 421cfcc706cSMiquel Raynal i++; 422cfcc706cSMiquel Raynal ofs += mtd->writesize; 423cfcc706cSMiquel Raynal } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); 424cfcc706cSMiquel Raynal 425cfcc706cSMiquel Raynal return ret; 426cfcc706cSMiquel Raynal } 427cfcc706cSMiquel Raynal 428cfcc706cSMiquel Raynal /** 429cfcc706cSMiquel Raynal * nand_block_markbad_lowlevel - mark a block bad 430cfcc706cSMiquel Raynal * @mtd: MTD device structure 431cfcc706cSMiquel Raynal * @ofs: offset from device start 432cfcc706cSMiquel Raynal * 433cfcc706cSMiquel Raynal * This function performs the generic NAND bad block marking steps (i.e., bad 434cfcc706cSMiquel Raynal * block table(s) and/or marker(s)). We only allow the hardware driver to 435cfcc706cSMiquel Raynal * specify how to write bad block markers to OOB (chip->block_markbad). 436cfcc706cSMiquel Raynal * 437cfcc706cSMiquel Raynal * We try operations in the following order: 438cfcc706cSMiquel Raynal * (1) erase the affected block, to allow OOB marker to be written cleanly 439cfcc706cSMiquel Raynal * (2) write bad block marker to OOB area of affected block (unless flag 440cfcc706cSMiquel Raynal * NAND_BBT_NO_OOB_BBM is present) 441cfcc706cSMiquel Raynal * (3) update the BBT 442cfcc706cSMiquel Raynal * Note that we retain the first error encountered in (2) or (3), finish the 443cfcc706cSMiquel Raynal * procedures, and dump the error in the end. 444cfcc706cSMiquel Raynal */ 445cfcc706cSMiquel Raynal static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs) 446cfcc706cSMiquel Raynal { 447cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 448cfcc706cSMiquel Raynal int res, ret = 0; 449cfcc706cSMiquel Raynal 450cfcc706cSMiquel Raynal if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { 451cfcc706cSMiquel Raynal struct erase_info einfo; 452cfcc706cSMiquel Raynal 453cfcc706cSMiquel Raynal /* Attempt erase before marking OOB */ 454cfcc706cSMiquel Raynal memset(&einfo, 0, sizeof(einfo)); 455cfcc706cSMiquel Raynal einfo.mtd = mtd; 456cfcc706cSMiquel Raynal einfo.addr = ofs; 457cfcc706cSMiquel Raynal einfo.len = 1ULL << chip->phys_erase_shift; 458cfcc706cSMiquel Raynal nand_erase_nand(mtd, &einfo, 0); 459cfcc706cSMiquel Raynal 460cfcc706cSMiquel Raynal /* Write bad block marker to OOB */ 461cfcc706cSMiquel Raynal nand_get_device(mtd, FL_WRITING); 462cfcc706cSMiquel Raynal ret = chip->block_markbad(mtd, ofs); 463cfcc706cSMiquel Raynal nand_release_device(mtd); 464cfcc706cSMiquel Raynal } 465cfcc706cSMiquel Raynal 466cfcc706cSMiquel Raynal /* Mark block bad in BBT */ 467cfcc706cSMiquel Raynal if (chip->bbt) { 468cfcc706cSMiquel Raynal res = nand_markbad_bbt(mtd, ofs); 469cfcc706cSMiquel Raynal if (!ret) 470cfcc706cSMiquel Raynal ret = res; 471cfcc706cSMiquel Raynal } 472cfcc706cSMiquel Raynal 473cfcc706cSMiquel Raynal if (!ret) 474cfcc706cSMiquel Raynal mtd->ecc_stats.badblocks++; 475cfcc706cSMiquel Raynal 476cfcc706cSMiquel Raynal return ret; 477cfcc706cSMiquel Raynal } 478cfcc706cSMiquel Raynal 479cfcc706cSMiquel Raynal /** 480cfcc706cSMiquel Raynal * nand_check_wp - [GENERIC] check if the chip is write protected 481cfcc706cSMiquel Raynal * @mtd: MTD device structure 482cfcc706cSMiquel Raynal * 483cfcc706cSMiquel Raynal * Check, if the device is write protected. The function expects, that the 484cfcc706cSMiquel Raynal * device is already selected. 485cfcc706cSMiquel Raynal */ 486cfcc706cSMiquel Raynal static int nand_check_wp(struct mtd_info *mtd) 487cfcc706cSMiquel Raynal { 488cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 489cfcc706cSMiquel Raynal 490cfcc706cSMiquel Raynal /* Broken xD cards report WP despite being writable */ 491cfcc706cSMiquel Raynal if (chip->options & NAND_BROKEN_XD) 492cfcc706cSMiquel Raynal return 0; 493cfcc706cSMiquel Raynal 494cfcc706cSMiquel Raynal /* Check the WP bit */ 495cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); 496cfcc706cSMiquel Raynal return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1; 497cfcc706cSMiquel Raynal } 498cfcc706cSMiquel Raynal 499cfcc706cSMiquel Raynal /** 500cfcc706cSMiquel Raynal * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. 501cfcc706cSMiquel Raynal * @mtd: MTD device structure 502cfcc706cSMiquel Raynal * @ofs: offset from device start 503cfcc706cSMiquel Raynal * 504cfcc706cSMiquel Raynal * Check if the block is marked as reserved. 505cfcc706cSMiquel Raynal */ 506cfcc706cSMiquel Raynal static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) 507cfcc706cSMiquel Raynal { 508cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 509cfcc706cSMiquel Raynal 510cfcc706cSMiquel Raynal if (!chip->bbt) 511cfcc706cSMiquel Raynal return 0; 512cfcc706cSMiquel Raynal /* Return info from the table */ 513cfcc706cSMiquel Raynal return nand_isreserved_bbt(mtd, ofs); 514cfcc706cSMiquel Raynal } 515cfcc706cSMiquel Raynal 516cfcc706cSMiquel Raynal /** 517cfcc706cSMiquel Raynal * nand_block_checkbad - [GENERIC] Check if a block is marked bad 518cfcc706cSMiquel Raynal * @mtd: MTD device structure 519cfcc706cSMiquel Raynal * @ofs: offset from device start 520cfcc706cSMiquel Raynal * @allowbbt: 1, if its allowed to access the bbt area 521cfcc706cSMiquel Raynal * 522cfcc706cSMiquel Raynal * Check, if the block is bad. Either by reading the bad block table or 523cfcc706cSMiquel Raynal * calling of the scan function. 524cfcc706cSMiquel Raynal */ 525cfcc706cSMiquel Raynal static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt) 526cfcc706cSMiquel Raynal { 527cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 528cfcc706cSMiquel Raynal 529cfcc706cSMiquel Raynal if (!(chip->options & NAND_SKIP_BBTSCAN) && 530cfcc706cSMiquel Raynal !(chip->options & NAND_BBT_SCANNED)) { 531cfcc706cSMiquel Raynal chip->options |= NAND_BBT_SCANNED; 532cfcc706cSMiquel Raynal chip->scan_bbt(mtd); 533cfcc706cSMiquel Raynal } 534cfcc706cSMiquel Raynal 535cfcc706cSMiquel Raynal if (!chip->bbt) 536cfcc706cSMiquel Raynal return chip->block_bad(mtd, ofs); 537cfcc706cSMiquel Raynal 538cfcc706cSMiquel Raynal /* Return info from the table */ 539cfcc706cSMiquel Raynal return nand_isbad_bbt(mtd, ofs, allowbbt); 540cfcc706cSMiquel Raynal } 541cfcc706cSMiquel Raynal 542cfcc706cSMiquel Raynal /** 543cfcc706cSMiquel Raynal * nand_wait_ready - [GENERIC] Wait for the ready pin after commands. 544cfcc706cSMiquel Raynal * @mtd: MTD device structure 545cfcc706cSMiquel Raynal * 546cfcc706cSMiquel Raynal * Wait for the ready pin after a command, and warn if a timeout occurs. 547cfcc706cSMiquel Raynal */ 548cfcc706cSMiquel Raynal void nand_wait_ready(struct mtd_info *mtd) 549cfcc706cSMiquel Raynal { 550cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 551cfcc706cSMiquel Raynal u32 timeo = (CONFIG_SYS_HZ * 400) / 1000; 552cfcc706cSMiquel Raynal u32 time_start; 553cfcc706cSMiquel Raynal 554cfcc706cSMiquel Raynal time_start = get_timer(0); 555cfcc706cSMiquel Raynal /* Wait until command is processed or timeout occurs */ 556cfcc706cSMiquel Raynal while (get_timer(time_start) < timeo) { 557cfcc706cSMiquel Raynal if (chip->dev_ready) 558cfcc706cSMiquel Raynal if (chip->dev_ready(mtd)) 559cfcc706cSMiquel Raynal break; 560cfcc706cSMiquel Raynal } 561cfcc706cSMiquel Raynal 562cfcc706cSMiquel Raynal if (!chip->dev_ready(mtd)) 563cfcc706cSMiquel Raynal pr_warn("timeout while waiting for chip to become ready\n"); 564cfcc706cSMiquel Raynal } 565cfcc706cSMiquel Raynal EXPORT_SYMBOL_GPL(nand_wait_ready); 566cfcc706cSMiquel Raynal 567cfcc706cSMiquel Raynal /** 568cfcc706cSMiquel Raynal * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands. 569cfcc706cSMiquel Raynal * @mtd: MTD device structure 570cfcc706cSMiquel Raynal * @timeo: Timeout in ms 571cfcc706cSMiquel Raynal * 572cfcc706cSMiquel Raynal * Wait for status ready (i.e. command done) or timeout. 573cfcc706cSMiquel Raynal */ 574cfcc706cSMiquel Raynal static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo) 575cfcc706cSMiquel Raynal { 576cfcc706cSMiquel Raynal register struct nand_chip *chip = mtd_to_nand(mtd); 577cfcc706cSMiquel Raynal u32 time_start; 578cfcc706cSMiquel Raynal 579cfcc706cSMiquel Raynal timeo = (CONFIG_SYS_HZ * timeo) / 1000; 580cfcc706cSMiquel Raynal time_start = get_timer(0); 581cfcc706cSMiquel Raynal while (get_timer(time_start) < timeo) { 582cfcc706cSMiquel Raynal if ((chip->read_byte(mtd) & NAND_STATUS_READY)) 583cfcc706cSMiquel Raynal break; 584cfcc706cSMiquel Raynal WATCHDOG_RESET(); 585cfcc706cSMiquel Raynal } 586cfcc706cSMiquel Raynal }; 587cfcc706cSMiquel Raynal 588cfcc706cSMiquel Raynal /** 589cfcc706cSMiquel Raynal * nand_command - [DEFAULT] Send command to NAND device 590cfcc706cSMiquel Raynal * @mtd: MTD device structure 591cfcc706cSMiquel Raynal * @command: the command to be sent 592cfcc706cSMiquel Raynal * @column: the column address for this command, -1 if none 593cfcc706cSMiquel Raynal * @page_addr: the page address for this command, -1 if none 594cfcc706cSMiquel Raynal * 595cfcc706cSMiquel Raynal * Send command to NAND device. This function is used for small page devices 596cfcc706cSMiquel Raynal * (512 Bytes per page). 597cfcc706cSMiquel Raynal */ 598cfcc706cSMiquel Raynal static void nand_command(struct mtd_info *mtd, unsigned int command, 599cfcc706cSMiquel Raynal int column, int page_addr) 600cfcc706cSMiquel Raynal { 601cfcc706cSMiquel Raynal register struct nand_chip *chip = mtd_to_nand(mtd); 602cfcc706cSMiquel Raynal int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; 603cfcc706cSMiquel Raynal 604cfcc706cSMiquel Raynal /* Write out the command to the device */ 605cfcc706cSMiquel Raynal if (command == NAND_CMD_SEQIN) { 606cfcc706cSMiquel Raynal int readcmd; 607cfcc706cSMiquel Raynal 608cfcc706cSMiquel Raynal if (column >= mtd->writesize) { 609cfcc706cSMiquel Raynal /* OOB area */ 610cfcc706cSMiquel Raynal column -= mtd->writesize; 611cfcc706cSMiquel Raynal readcmd = NAND_CMD_READOOB; 612cfcc706cSMiquel Raynal } else if (column < 256) { 613cfcc706cSMiquel Raynal /* First 256 bytes --> READ0 */ 614cfcc706cSMiquel Raynal readcmd = NAND_CMD_READ0; 615cfcc706cSMiquel Raynal } else { 616cfcc706cSMiquel Raynal column -= 256; 617cfcc706cSMiquel Raynal readcmd = NAND_CMD_READ1; 618cfcc706cSMiquel Raynal } 619cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, readcmd, ctrl); 620cfcc706cSMiquel Raynal ctrl &= ~NAND_CTRL_CHANGE; 621cfcc706cSMiquel Raynal } 622cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, command, ctrl); 623cfcc706cSMiquel Raynal 624cfcc706cSMiquel Raynal /* Address cycle, when necessary */ 625cfcc706cSMiquel Raynal ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; 626cfcc706cSMiquel Raynal /* Serially input address */ 627cfcc706cSMiquel Raynal if (column != -1) { 628cfcc706cSMiquel Raynal /* Adjust columns for 16 bit buswidth */ 629cfcc706cSMiquel Raynal if (chip->options & NAND_BUSWIDTH_16 && 630cfcc706cSMiquel Raynal !nand_opcode_8bits(command)) 631cfcc706cSMiquel Raynal column >>= 1; 632cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, column, ctrl); 633cfcc706cSMiquel Raynal ctrl &= ~NAND_CTRL_CHANGE; 634cfcc706cSMiquel Raynal } 635cfcc706cSMiquel Raynal if (page_addr != -1) { 636cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, page_addr, ctrl); 637cfcc706cSMiquel Raynal ctrl &= ~NAND_CTRL_CHANGE; 638cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); 639cfcc706cSMiquel Raynal if (chip->options & NAND_ROW_ADDR_3) 640cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); 641cfcc706cSMiquel Raynal } 642cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); 643cfcc706cSMiquel Raynal 644cfcc706cSMiquel Raynal /* 645cfcc706cSMiquel Raynal * Program and erase have their own busy handlers status and sequential 646cfcc706cSMiquel Raynal * in needs no delay 647cfcc706cSMiquel Raynal */ 648cfcc706cSMiquel Raynal switch (command) { 649cfcc706cSMiquel Raynal 650cfcc706cSMiquel Raynal case NAND_CMD_PAGEPROG: 651cfcc706cSMiquel Raynal case NAND_CMD_ERASE1: 652cfcc706cSMiquel Raynal case NAND_CMD_ERASE2: 653cfcc706cSMiquel Raynal case NAND_CMD_SEQIN: 654cfcc706cSMiquel Raynal case NAND_CMD_STATUS: 655cfcc706cSMiquel Raynal case NAND_CMD_READID: 656cfcc706cSMiquel Raynal case NAND_CMD_SET_FEATURES: 657cfcc706cSMiquel Raynal return; 658cfcc706cSMiquel Raynal 659cfcc706cSMiquel Raynal case NAND_CMD_RESET: 660cfcc706cSMiquel Raynal if (chip->dev_ready) 661cfcc706cSMiquel Raynal break; 662cfcc706cSMiquel Raynal udelay(chip->chip_delay); 663cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, NAND_CMD_STATUS, 664cfcc706cSMiquel Raynal NAND_CTRL_CLE | NAND_CTRL_CHANGE); 665cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, 666cfcc706cSMiquel Raynal NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); 667cfcc706cSMiquel Raynal /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ 668cfcc706cSMiquel Raynal nand_wait_status_ready(mtd, 250); 669cfcc706cSMiquel Raynal return; 670cfcc706cSMiquel Raynal 671cfcc706cSMiquel Raynal /* This applies to read commands */ 672cfcc706cSMiquel Raynal default: 673cfcc706cSMiquel Raynal /* 674cfcc706cSMiquel Raynal * If we don't have access to the busy pin, we apply the given 675cfcc706cSMiquel Raynal * command delay 676cfcc706cSMiquel Raynal */ 677cfcc706cSMiquel Raynal if (!chip->dev_ready) { 678cfcc706cSMiquel Raynal udelay(chip->chip_delay); 679cfcc706cSMiquel Raynal return; 680cfcc706cSMiquel Raynal } 681cfcc706cSMiquel Raynal } 682cfcc706cSMiquel Raynal /* 683cfcc706cSMiquel Raynal * Apply this short delay always to ensure that we do wait tWB in 684cfcc706cSMiquel Raynal * any case on any machine. 685cfcc706cSMiquel Raynal */ 686cfcc706cSMiquel Raynal ndelay(100); 687cfcc706cSMiquel Raynal 688cfcc706cSMiquel Raynal nand_wait_ready(mtd); 689cfcc706cSMiquel Raynal } 690cfcc706cSMiquel Raynal 691cfcc706cSMiquel Raynal /** 692cfcc706cSMiquel Raynal * nand_command_lp - [DEFAULT] Send command to NAND large page device 693cfcc706cSMiquel Raynal * @mtd: MTD device structure 694cfcc706cSMiquel Raynal * @command: the command to be sent 695cfcc706cSMiquel Raynal * @column: the column address for this command, -1 if none 696cfcc706cSMiquel Raynal * @page_addr: the page address for this command, -1 if none 697cfcc706cSMiquel Raynal * 698cfcc706cSMiquel Raynal * Send command to NAND device. This is the version for the new large page 699cfcc706cSMiquel Raynal * devices. We don't have the separate regions as we have in the small page 700cfcc706cSMiquel Raynal * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. 701cfcc706cSMiquel Raynal */ 702cfcc706cSMiquel Raynal static void nand_command_lp(struct mtd_info *mtd, unsigned int command, 703cfcc706cSMiquel Raynal int column, int page_addr) 704cfcc706cSMiquel Raynal { 705cfcc706cSMiquel Raynal register struct nand_chip *chip = mtd_to_nand(mtd); 706cfcc706cSMiquel Raynal 707cfcc706cSMiquel Raynal /* Emulate NAND_CMD_READOOB */ 708cfcc706cSMiquel Raynal if (command == NAND_CMD_READOOB) { 709cfcc706cSMiquel Raynal column += mtd->writesize; 710cfcc706cSMiquel Raynal command = NAND_CMD_READ0; 711cfcc706cSMiquel Raynal } 712cfcc706cSMiquel Raynal 713cfcc706cSMiquel Raynal /* Command latch cycle */ 714cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); 715cfcc706cSMiquel Raynal 716cfcc706cSMiquel Raynal if (column != -1 || page_addr != -1) { 717cfcc706cSMiquel Raynal int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; 718cfcc706cSMiquel Raynal 719cfcc706cSMiquel Raynal /* Serially input address */ 720cfcc706cSMiquel Raynal if (column != -1) { 721cfcc706cSMiquel Raynal /* Adjust columns for 16 bit buswidth */ 722cfcc706cSMiquel Raynal if (chip->options & NAND_BUSWIDTH_16 && 723cfcc706cSMiquel Raynal !nand_opcode_8bits(command)) 724cfcc706cSMiquel Raynal column >>= 1; 725cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, column, ctrl); 726cfcc706cSMiquel Raynal ctrl &= ~NAND_CTRL_CHANGE; 727cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, column >> 8, ctrl); 728cfcc706cSMiquel Raynal } 729cfcc706cSMiquel Raynal if (page_addr != -1) { 730cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, page_addr, ctrl); 731cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, page_addr >> 8, 732cfcc706cSMiquel Raynal NAND_NCE | NAND_ALE); 733cfcc706cSMiquel Raynal if (chip->options & NAND_ROW_ADDR_3) 734cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, page_addr >> 16, 735cfcc706cSMiquel Raynal NAND_NCE | NAND_ALE); 736cfcc706cSMiquel Raynal } 737cfcc706cSMiquel Raynal } 738cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); 739cfcc706cSMiquel Raynal 740cfcc706cSMiquel Raynal /* 741cfcc706cSMiquel Raynal * Program and erase have their own busy handlers status, sequential 742cfcc706cSMiquel Raynal * in and status need no delay. 743cfcc706cSMiquel Raynal */ 744cfcc706cSMiquel Raynal switch (command) { 745cfcc706cSMiquel Raynal 746cfcc706cSMiquel Raynal case NAND_CMD_CACHEDPROG: 747cfcc706cSMiquel Raynal case NAND_CMD_PAGEPROG: 748cfcc706cSMiquel Raynal case NAND_CMD_ERASE1: 749cfcc706cSMiquel Raynal case NAND_CMD_ERASE2: 750cfcc706cSMiquel Raynal case NAND_CMD_SEQIN: 751cfcc706cSMiquel Raynal case NAND_CMD_RNDIN: 752cfcc706cSMiquel Raynal case NAND_CMD_STATUS: 753cfcc706cSMiquel Raynal case NAND_CMD_READID: 754cfcc706cSMiquel Raynal case NAND_CMD_SET_FEATURES: 755cfcc706cSMiquel Raynal return; 756cfcc706cSMiquel Raynal 757cfcc706cSMiquel Raynal case NAND_CMD_RESET: 758cfcc706cSMiquel Raynal if (chip->dev_ready) 759cfcc706cSMiquel Raynal break; 760cfcc706cSMiquel Raynal udelay(chip->chip_delay); 761cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, NAND_CMD_STATUS, 762cfcc706cSMiquel Raynal NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); 763cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, NAND_CMD_NONE, 764cfcc706cSMiquel Raynal NAND_NCE | NAND_CTRL_CHANGE); 765cfcc706cSMiquel Raynal /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ 766cfcc706cSMiquel Raynal nand_wait_status_ready(mtd, 250); 767cfcc706cSMiquel Raynal return; 768cfcc706cSMiquel Raynal 769cfcc706cSMiquel Raynal case NAND_CMD_RNDOUT: 770cfcc706cSMiquel Raynal /* No ready / busy check necessary */ 771cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, 772cfcc706cSMiquel Raynal NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); 773cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, NAND_CMD_NONE, 774cfcc706cSMiquel Raynal NAND_NCE | NAND_CTRL_CHANGE); 775cfcc706cSMiquel Raynal return; 776cfcc706cSMiquel Raynal 777cfcc706cSMiquel Raynal case NAND_CMD_READ0: 778cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, NAND_CMD_READSTART, 779cfcc706cSMiquel Raynal NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); 780cfcc706cSMiquel Raynal chip->cmd_ctrl(mtd, NAND_CMD_NONE, 781cfcc706cSMiquel Raynal NAND_NCE | NAND_CTRL_CHANGE); 782cfcc706cSMiquel Raynal 783cfcc706cSMiquel Raynal /* This applies to read commands */ 784cfcc706cSMiquel Raynal default: 785cfcc706cSMiquel Raynal /* 786cfcc706cSMiquel Raynal * If we don't have access to the busy pin, we apply the given 787cfcc706cSMiquel Raynal * command delay. 788cfcc706cSMiquel Raynal */ 789cfcc706cSMiquel Raynal if (!chip->dev_ready) { 790cfcc706cSMiquel Raynal udelay(chip->chip_delay); 791cfcc706cSMiquel Raynal return; 792cfcc706cSMiquel Raynal } 793cfcc706cSMiquel Raynal } 794cfcc706cSMiquel Raynal 795cfcc706cSMiquel Raynal /* 796cfcc706cSMiquel Raynal * Apply this short delay always to ensure that we do wait tWB in 797cfcc706cSMiquel Raynal * any case on any machine. 798cfcc706cSMiquel Raynal */ 799cfcc706cSMiquel Raynal ndelay(100); 800cfcc706cSMiquel Raynal 801cfcc706cSMiquel Raynal nand_wait_ready(mtd); 802cfcc706cSMiquel Raynal } 803cfcc706cSMiquel Raynal 804cfcc706cSMiquel Raynal /** 805cfcc706cSMiquel Raynal * panic_nand_get_device - [GENERIC] Get chip for selected access 806cfcc706cSMiquel Raynal * @chip: the nand chip descriptor 807cfcc706cSMiquel Raynal * @mtd: MTD device structure 808cfcc706cSMiquel Raynal * @new_state: the state which is requested 809cfcc706cSMiquel Raynal * 810cfcc706cSMiquel Raynal * Used when in panic, no locks are taken. 811cfcc706cSMiquel Raynal */ 812cfcc706cSMiquel Raynal static void panic_nand_get_device(struct nand_chip *chip, 813cfcc706cSMiquel Raynal struct mtd_info *mtd, int new_state) 814cfcc706cSMiquel Raynal { 815cfcc706cSMiquel Raynal /* Hardware controller shared among independent devices */ 816cfcc706cSMiquel Raynal chip->controller->active = chip; 817cfcc706cSMiquel Raynal chip->state = new_state; 818cfcc706cSMiquel Raynal } 819cfcc706cSMiquel Raynal 820cfcc706cSMiquel Raynal /** 821cfcc706cSMiquel Raynal * nand_get_device - [GENERIC] Get chip for selected access 822cfcc706cSMiquel Raynal * @mtd: MTD device structure 823cfcc706cSMiquel Raynal * @new_state: the state which is requested 824cfcc706cSMiquel Raynal * 825cfcc706cSMiquel Raynal * Get the device and lock it for exclusive access 826cfcc706cSMiquel Raynal */ 827cfcc706cSMiquel Raynal static int 828cfcc706cSMiquel Raynal nand_get_device(struct mtd_info *mtd, int new_state) 829cfcc706cSMiquel Raynal { 830cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 831cfcc706cSMiquel Raynal chip->state = new_state; 832cfcc706cSMiquel Raynal return 0; 833cfcc706cSMiquel Raynal } 834cfcc706cSMiquel Raynal 835cfcc706cSMiquel Raynal /** 836cfcc706cSMiquel Raynal * panic_nand_wait - [GENERIC] wait until the command is done 837cfcc706cSMiquel Raynal * @mtd: MTD device structure 838cfcc706cSMiquel Raynal * @chip: NAND chip structure 839cfcc706cSMiquel Raynal * @timeo: timeout 840cfcc706cSMiquel Raynal * 841cfcc706cSMiquel Raynal * Wait for command done. This is a helper function for nand_wait used when 842cfcc706cSMiquel Raynal * we are in interrupt context. May happen when in panic and trying to write 843cfcc706cSMiquel Raynal * an oops through mtdoops. 844cfcc706cSMiquel Raynal */ 845cfcc706cSMiquel Raynal static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, 846cfcc706cSMiquel Raynal unsigned long timeo) 847cfcc706cSMiquel Raynal { 848cfcc706cSMiquel Raynal int i; 849cfcc706cSMiquel Raynal for (i = 0; i < timeo; i++) { 850cfcc706cSMiquel Raynal if (chip->dev_ready) { 851cfcc706cSMiquel Raynal if (chip->dev_ready(mtd)) 852cfcc706cSMiquel Raynal break; 853cfcc706cSMiquel Raynal } else { 854cfcc706cSMiquel Raynal if (chip->read_byte(mtd) & NAND_STATUS_READY) 855cfcc706cSMiquel Raynal break; 856cfcc706cSMiquel Raynal } 857cfcc706cSMiquel Raynal mdelay(1); 858cfcc706cSMiquel Raynal } 859cfcc706cSMiquel Raynal } 860cfcc706cSMiquel Raynal 861cfcc706cSMiquel Raynal /** 862cfcc706cSMiquel Raynal * nand_wait - [DEFAULT] wait until the command is done 863cfcc706cSMiquel Raynal * @mtd: MTD device structure 864cfcc706cSMiquel Raynal * @chip: NAND chip structure 865cfcc706cSMiquel Raynal * 866cfcc706cSMiquel Raynal * Wait for command done. This applies to erase and program only. 867cfcc706cSMiquel Raynal */ 868cfcc706cSMiquel Raynal static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) 869cfcc706cSMiquel Raynal { 870cfcc706cSMiquel Raynal int status; 871cfcc706cSMiquel Raynal unsigned long timeo = 400; 872cfcc706cSMiquel Raynal 873cfcc706cSMiquel Raynal led_trigger_event(nand_led_trigger, LED_FULL); 874cfcc706cSMiquel Raynal 875cfcc706cSMiquel Raynal /* 876cfcc706cSMiquel Raynal * Apply this short delay always to ensure that we do wait tWB in any 877cfcc706cSMiquel Raynal * case on any machine. 878cfcc706cSMiquel Raynal */ 879cfcc706cSMiquel Raynal ndelay(100); 880cfcc706cSMiquel Raynal 881cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); 882cfcc706cSMiquel Raynal 883cfcc706cSMiquel Raynal u32 timer = (CONFIG_SYS_HZ * timeo) / 1000; 884cfcc706cSMiquel Raynal u32 time_start; 885cfcc706cSMiquel Raynal 886cfcc706cSMiquel Raynal time_start = get_timer(0); 887cfcc706cSMiquel Raynal while (get_timer(time_start) < timer) { 888cfcc706cSMiquel Raynal if (chip->dev_ready) { 889cfcc706cSMiquel Raynal if (chip->dev_ready(mtd)) 890cfcc706cSMiquel Raynal break; 891cfcc706cSMiquel Raynal } else { 892cfcc706cSMiquel Raynal if (chip->read_byte(mtd) & NAND_STATUS_READY) 893cfcc706cSMiquel Raynal break; 894cfcc706cSMiquel Raynal } 895cfcc706cSMiquel Raynal } 896cfcc706cSMiquel Raynal led_trigger_event(nand_led_trigger, LED_OFF); 897cfcc706cSMiquel Raynal 898cfcc706cSMiquel Raynal status = (int)chip->read_byte(mtd); 899cfcc706cSMiquel Raynal /* This can happen if in case of timeout or buggy dev_ready */ 900cfcc706cSMiquel Raynal WARN_ON(!(status & NAND_STATUS_READY)); 901cfcc706cSMiquel Raynal return status; 902cfcc706cSMiquel Raynal } 903cfcc706cSMiquel Raynal 904cfcc706cSMiquel Raynal /** 905cfcc706cSMiquel Raynal * nand_reset_data_interface - Reset data interface and timings 906cfcc706cSMiquel Raynal * @chip: The NAND chip 907cfcc706cSMiquel Raynal * @chipnr: Internal die id 908cfcc706cSMiquel Raynal * 909cfcc706cSMiquel Raynal * Reset the Data interface and timings to ONFI mode 0. 910cfcc706cSMiquel Raynal * 911cfcc706cSMiquel Raynal * Returns 0 for success or negative error code otherwise. 912cfcc706cSMiquel Raynal */ 913cfcc706cSMiquel Raynal static int nand_reset_data_interface(struct nand_chip *chip, int chipnr) 914cfcc706cSMiquel Raynal { 915cfcc706cSMiquel Raynal struct mtd_info *mtd = nand_to_mtd(chip); 916cfcc706cSMiquel Raynal const struct nand_data_interface *conf; 917cfcc706cSMiquel Raynal int ret; 918cfcc706cSMiquel Raynal 919cfcc706cSMiquel Raynal if (!chip->setup_data_interface) 920cfcc706cSMiquel Raynal return 0; 921cfcc706cSMiquel Raynal 922cfcc706cSMiquel Raynal /* 923cfcc706cSMiquel Raynal * The ONFI specification says: 924cfcc706cSMiquel Raynal * " 925cfcc706cSMiquel Raynal * To transition from NV-DDR or NV-DDR2 to the SDR data 926cfcc706cSMiquel Raynal * interface, the host shall use the Reset (FFh) command 927cfcc706cSMiquel Raynal * using SDR timing mode 0. A device in any timing mode is 928cfcc706cSMiquel Raynal * required to recognize Reset (FFh) command issued in SDR 929cfcc706cSMiquel Raynal * timing mode 0. 930cfcc706cSMiquel Raynal * " 931cfcc706cSMiquel Raynal * 932cfcc706cSMiquel Raynal * Configure the data interface in SDR mode and set the 933cfcc706cSMiquel Raynal * timings to timing mode 0. 934cfcc706cSMiquel Raynal */ 935cfcc706cSMiquel Raynal 936cfcc706cSMiquel Raynal conf = nand_get_default_data_interface(); 937cfcc706cSMiquel Raynal ret = chip->setup_data_interface(mtd, chipnr, conf); 938cfcc706cSMiquel Raynal if (ret) 939cfcc706cSMiquel Raynal pr_err("Failed to configure data interface to SDR timing mode 0\n"); 940cfcc706cSMiquel Raynal 941cfcc706cSMiquel Raynal return ret; 942cfcc706cSMiquel Raynal } 943cfcc706cSMiquel Raynal 944cfcc706cSMiquel Raynal /** 945cfcc706cSMiquel Raynal * nand_setup_data_interface - Setup the best data interface and timings 946cfcc706cSMiquel Raynal * @chip: The NAND chip 947cfcc706cSMiquel Raynal * @chipnr: Internal die id 948cfcc706cSMiquel Raynal * 949cfcc706cSMiquel Raynal * Find and configure the best data interface and NAND timings supported by 950cfcc706cSMiquel Raynal * the chip and the driver. 951cfcc706cSMiquel Raynal * First tries to retrieve supported timing modes from ONFI information, 952cfcc706cSMiquel Raynal * and if the NAND chip does not support ONFI, relies on the 953cfcc706cSMiquel Raynal * ->onfi_timing_mode_default specified in the nand_ids table. 954cfcc706cSMiquel Raynal * 955cfcc706cSMiquel Raynal * Returns 0 for success or negative error code otherwise. 956cfcc706cSMiquel Raynal */ 957cfcc706cSMiquel Raynal static int nand_setup_data_interface(struct nand_chip *chip, int chipnr) 958cfcc706cSMiquel Raynal { 959cfcc706cSMiquel Raynal struct mtd_info *mtd = nand_to_mtd(chip); 960cfcc706cSMiquel Raynal int ret; 961cfcc706cSMiquel Raynal 962cfcc706cSMiquel Raynal if (!chip->setup_data_interface || !chip->data_interface) 963cfcc706cSMiquel Raynal return 0; 964cfcc706cSMiquel Raynal 965cfcc706cSMiquel Raynal /* 966cfcc706cSMiquel Raynal * Ensure the timing mode has been changed on the chip side 967cfcc706cSMiquel Raynal * before changing timings on the controller side. 968cfcc706cSMiquel Raynal */ 969cfcc706cSMiquel Raynal if (chip->onfi_version) { 970cfcc706cSMiquel Raynal u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { 971cfcc706cSMiquel Raynal chip->onfi_timing_mode_default, 972cfcc706cSMiquel Raynal }; 973cfcc706cSMiquel Raynal 974cfcc706cSMiquel Raynal ret = chip->onfi_set_features(mtd, chip, 975cfcc706cSMiquel Raynal ONFI_FEATURE_ADDR_TIMING_MODE, 976cfcc706cSMiquel Raynal tmode_param); 977cfcc706cSMiquel Raynal if (ret) 978cfcc706cSMiquel Raynal goto err; 979cfcc706cSMiquel Raynal } 980cfcc706cSMiquel Raynal 981cfcc706cSMiquel Raynal ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface); 982cfcc706cSMiquel Raynal err: 983cfcc706cSMiquel Raynal return ret; 984cfcc706cSMiquel Raynal } 985cfcc706cSMiquel Raynal 986cfcc706cSMiquel Raynal /** 987cfcc706cSMiquel Raynal * nand_init_data_interface - find the best data interface and timings 988cfcc706cSMiquel Raynal * @chip: The NAND chip 989cfcc706cSMiquel Raynal * 990cfcc706cSMiquel Raynal * Find the best data interface and NAND timings supported by the chip 991cfcc706cSMiquel Raynal * and the driver. 992cfcc706cSMiquel Raynal * First tries to retrieve supported timing modes from ONFI information, 993cfcc706cSMiquel Raynal * and if the NAND chip does not support ONFI, relies on the 994cfcc706cSMiquel Raynal * ->onfi_timing_mode_default specified in the nand_ids table. After this 995cfcc706cSMiquel Raynal * function nand_chip->data_interface is initialized with the best timing mode 996cfcc706cSMiquel Raynal * available. 997cfcc706cSMiquel Raynal * 998cfcc706cSMiquel Raynal * Returns 0 for success or negative error code otherwise. 999cfcc706cSMiquel Raynal */ 1000cfcc706cSMiquel Raynal static int nand_init_data_interface(struct nand_chip *chip) 1001cfcc706cSMiquel Raynal { 1002cfcc706cSMiquel Raynal struct mtd_info *mtd = nand_to_mtd(chip); 1003cfcc706cSMiquel Raynal int modes, mode, ret; 1004cfcc706cSMiquel Raynal 1005cfcc706cSMiquel Raynal if (!chip->setup_data_interface) 1006cfcc706cSMiquel Raynal return 0; 1007cfcc706cSMiquel Raynal 1008cfcc706cSMiquel Raynal /* 1009cfcc706cSMiquel Raynal * First try to identify the best timings from ONFI parameters and 1010cfcc706cSMiquel Raynal * if the NAND does not support ONFI, fallback to the default ONFI 1011cfcc706cSMiquel Raynal * timing mode. 1012cfcc706cSMiquel Raynal */ 1013cfcc706cSMiquel Raynal modes = onfi_get_async_timing_mode(chip); 1014cfcc706cSMiquel Raynal if (modes == ONFI_TIMING_MODE_UNKNOWN) { 1015cfcc706cSMiquel Raynal if (!chip->onfi_timing_mode_default) 1016cfcc706cSMiquel Raynal return 0; 1017cfcc706cSMiquel Raynal 1018cfcc706cSMiquel Raynal modes = GENMASK(chip->onfi_timing_mode_default, 0); 1019cfcc706cSMiquel Raynal } 1020cfcc706cSMiquel Raynal 1021cfcc706cSMiquel Raynal chip->data_interface = kzalloc(sizeof(*chip->data_interface), 1022cfcc706cSMiquel Raynal GFP_KERNEL); 1023cfcc706cSMiquel Raynal if (!chip->data_interface) 1024cfcc706cSMiquel Raynal return -ENOMEM; 1025cfcc706cSMiquel Raynal 1026cfcc706cSMiquel Raynal for (mode = fls(modes) - 1; mode >= 0; mode--) { 1027cfcc706cSMiquel Raynal ret = onfi_init_data_interface(chip, chip->data_interface, 1028cfcc706cSMiquel Raynal NAND_SDR_IFACE, mode); 1029cfcc706cSMiquel Raynal if (ret) 1030cfcc706cSMiquel Raynal continue; 1031cfcc706cSMiquel Raynal 1032cfcc706cSMiquel Raynal /* Pass -1 to only */ 1033cfcc706cSMiquel Raynal ret = chip->setup_data_interface(mtd, 1034cfcc706cSMiquel Raynal NAND_DATA_IFACE_CHECK_ONLY, 1035cfcc706cSMiquel Raynal chip->data_interface); 1036cfcc706cSMiquel Raynal if (!ret) { 1037cfcc706cSMiquel Raynal chip->onfi_timing_mode_default = mode; 1038cfcc706cSMiquel Raynal break; 1039cfcc706cSMiquel Raynal } 1040cfcc706cSMiquel Raynal } 1041cfcc706cSMiquel Raynal 1042cfcc706cSMiquel Raynal return 0; 1043cfcc706cSMiquel Raynal } 1044cfcc706cSMiquel Raynal 1045cfcc706cSMiquel Raynal static void __maybe_unused nand_release_data_interface(struct nand_chip *chip) 1046cfcc706cSMiquel Raynal { 1047cfcc706cSMiquel Raynal kfree(chip->data_interface); 1048cfcc706cSMiquel Raynal } 1049cfcc706cSMiquel Raynal 1050cfcc706cSMiquel Raynal /** 1051cfcc706cSMiquel Raynal * nand_reset - Reset and initialize a NAND device 1052cfcc706cSMiquel Raynal * @chip: The NAND chip 1053cfcc706cSMiquel Raynal * @chipnr: Internal die id 1054cfcc706cSMiquel Raynal * 1055cfcc706cSMiquel Raynal * Returns 0 for success or negative error code otherwise 1056cfcc706cSMiquel Raynal */ 1057cfcc706cSMiquel Raynal int nand_reset(struct nand_chip *chip, int chipnr) 1058cfcc706cSMiquel Raynal { 1059cfcc706cSMiquel Raynal struct mtd_info *mtd = nand_to_mtd(chip); 1060cfcc706cSMiquel Raynal int ret; 1061cfcc706cSMiquel Raynal 1062cfcc706cSMiquel Raynal ret = nand_reset_data_interface(chip, chipnr); 1063cfcc706cSMiquel Raynal if (ret) 1064cfcc706cSMiquel Raynal return ret; 1065cfcc706cSMiquel Raynal 1066cfcc706cSMiquel Raynal /* 1067cfcc706cSMiquel Raynal * The CS line has to be released before we can apply the new NAND 1068cfcc706cSMiquel Raynal * interface settings, hence this weird ->select_chip() dance. 1069cfcc706cSMiquel Raynal */ 1070cfcc706cSMiquel Raynal chip->select_chip(mtd, chipnr); 1071cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); 1072cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 1073cfcc706cSMiquel Raynal 1074cfcc706cSMiquel Raynal chip->select_chip(mtd, chipnr); 1075cfcc706cSMiquel Raynal ret = nand_setup_data_interface(chip, chipnr); 1076cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 1077cfcc706cSMiquel Raynal if (ret) 1078cfcc706cSMiquel Raynal return ret; 1079cfcc706cSMiquel Raynal 1080cfcc706cSMiquel Raynal return 0; 1081cfcc706cSMiquel Raynal } 1082cfcc706cSMiquel Raynal 1083cfcc706cSMiquel Raynal /** 1084cfcc706cSMiquel Raynal * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data 1085cfcc706cSMiquel Raynal * @buf: buffer to test 1086cfcc706cSMiquel Raynal * @len: buffer length 1087cfcc706cSMiquel Raynal * @bitflips_threshold: maximum number of bitflips 1088cfcc706cSMiquel Raynal * 1089cfcc706cSMiquel Raynal * Check if a buffer contains only 0xff, which means the underlying region 1090cfcc706cSMiquel Raynal * has been erased and is ready to be programmed. 1091cfcc706cSMiquel Raynal * The bitflips_threshold specify the maximum number of bitflips before 1092cfcc706cSMiquel Raynal * considering the region is not erased. 1093cfcc706cSMiquel Raynal * Note: The logic of this function has been extracted from the memweight 1094cfcc706cSMiquel Raynal * implementation, except that nand_check_erased_buf function exit before 1095cfcc706cSMiquel Raynal * testing the whole buffer if the number of bitflips exceed the 1096cfcc706cSMiquel Raynal * bitflips_threshold value. 1097cfcc706cSMiquel Raynal * 1098cfcc706cSMiquel Raynal * Returns a positive number of bitflips less than or equal to 1099cfcc706cSMiquel Raynal * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the 1100cfcc706cSMiquel Raynal * threshold. 1101cfcc706cSMiquel Raynal */ 1102cfcc706cSMiquel Raynal static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) 1103cfcc706cSMiquel Raynal { 1104cfcc706cSMiquel Raynal const unsigned char *bitmap = buf; 1105cfcc706cSMiquel Raynal int bitflips = 0; 1106cfcc706cSMiquel Raynal int weight; 1107cfcc706cSMiquel Raynal 1108cfcc706cSMiquel Raynal for (; len && ((uintptr_t)bitmap) % sizeof(long); 1109cfcc706cSMiquel Raynal len--, bitmap++) { 1110cfcc706cSMiquel Raynal weight = hweight8(*bitmap); 1111cfcc706cSMiquel Raynal bitflips += BITS_PER_BYTE - weight; 1112cfcc706cSMiquel Raynal if (unlikely(bitflips > bitflips_threshold)) 1113cfcc706cSMiquel Raynal return -EBADMSG; 1114cfcc706cSMiquel Raynal } 1115cfcc706cSMiquel Raynal 1116cfcc706cSMiquel Raynal for (; len >= 4; len -= 4, bitmap += 4) { 1117cfcc706cSMiquel Raynal weight = hweight32(*((u32 *)bitmap)); 1118cfcc706cSMiquel Raynal bitflips += 32 - weight; 1119cfcc706cSMiquel Raynal if (unlikely(bitflips > bitflips_threshold)) 1120cfcc706cSMiquel Raynal return -EBADMSG; 1121cfcc706cSMiquel Raynal } 1122cfcc706cSMiquel Raynal 1123cfcc706cSMiquel Raynal for (; len > 0; len--, bitmap++) { 1124cfcc706cSMiquel Raynal weight = hweight8(*bitmap); 1125cfcc706cSMiquel Raynal bitflips += BITS_PER_BYTE - weight; 1126cfcc706cSMiquel Raynal if (unlikely(bitflips > bitflips_threshold)) 1127cfcc706cSMiquel Raynal return -EBADMSG; 1128cfcc706cSMiquel Raynal } 1129cfcc706cSMiquel Raynal 1130cfcc706cSMiquel Raynal return bitflips; 1131cfcc706cSMiquel Raynal } 1132cfcc706cSMiquel Raynal 1133cfcc706cSMiquel Raynal /** 1134cfcc706cSMiquel Raynal * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only 1135cfcc706cSMiquel Raynal * 0xff data 1136cfcc706cSMiquel Raynal * @data: data buffer to test 1137cfcc706cSMiquel Raynal * @datalen: data length 1138cfcc706cSMiquel Raynal * @ecc: ECC buffer 1139cfcc706cSMiquel Raynal * @ecclen: ECC length 1140cfcc706cSMiquel Raynal * @extraoob: extra OOB buffer 1141cfcc706cSMiquel Raynal * @extraooblen: extra OOB length 1142cfcc706cSMiquel Raynal * @bitflips_threshold: maximum number of bitflips 1143cfcc706cSMiquel Raynal * 1144cfcc706cSMiquel Raynal * Check if a data buffer and its associated ECC and OOB data contains only 1145cfcc706cSMiquel Raynal * 0xff pattern, which means the underlying region has been erased and is 1146cfcc706cSMiquel Raynal * ready to be programmed. 1147cfcc706cSMiquel Raynal * The bitflips_threshold specify the maximum number of bitflips before 1148cfcc706cSMiquel Raynal * considering the region as not erased. 1149cfcc706cSMiquel Raynal * 1150cfcc706cSMiquel Raynal * Note: 1151cfcc706cSMiquel Raynal * 1/ ECC algorithms are working on pre-defined block sizes which are usually 1152cfcc706cSMiquel Raynal * different from the NAND page size. When fixing bitflips, ECC engines will 1153cfcc706cSMiquel Raynal * report the number of errors per chunk, and the NAND core infrastructure 1154cfcc706cSMiquel Raynal * expect you to return the maximum number of bitflips for the whole page. 1155cfcc706cSMiquel Raynal * This is why you should always use this function on a single chunk and 1156cfcc706cSMiquel Raynal * not on the whole page. After checking each chunk you should update your 1157cfcc706cSMiquel Raynal * max_bitflips value accordingly. 1158cfcc706cSMiquel Raynal * 2/ When checking for bitflips in erased pages you should not only check 1159cfcc706cSMiquel Raynal * the payload data but also their associated ECC data, because a user might 1160cfcc706cSMiquel Raynal * have programmed almost all bits to 1 but a few. In this case, we 1161cfcc706cSMiquel Raynal * shouldn't consider the chunk as erased, and checking ECC bytes prevent 1162cfcc706cSMiquel Raynal * this case. 1163cfcc706cSMiquel Raynal * 3/ The extraoob argument is optional, and should be used if some of your OOB 1164cfcc706cSMiquel Raynal * data are protected by the ECC engine. 1165cfcc706cSMiquel Raynal * It could also be used if you support subpages and want to attach some 1166cfcc706cSMiquel Raynal * extra OOB data to an ECC chunk. 1167cfcc706cSMiquel Raynal * 1168cfcc706cSMiquel Raynal * Returns a positive number of bitflips less than or equal to 1169cfcc706cSMiquel Raynal * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the 1170cfcc706cSMiquel Raynal * threshold. In case of success, the passed buffers are filled with 0xff. 1171cfcc706cSMiquel Raynal */ 1172cfcc706cSMiquel Raynal int nand_check_erased_ecc_chunk(void *data, int datalen, 1173cfcc706cSMiquel Raynal void *ecc, int ecclen, 1174cfcc706cSMiquel Raynal void *extraoob, int extraooblen, 1175cfcc706cSMiquel Raynal int bitflips_threshold) 1176cfcc706cSMiquel Raynal { 1177cfcc706cSMiquel Raynal int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; 1178cfcc706cSMiquel Raynal 1179cfcc706cSMiquel Raynal data_bitflips = nand_check_erased_buf(data, datalen, 1180cfcc706cSMiquel Raynal bitflips_threshold); 1181cfcc706cSMiquel Raynal if (data_bitflips < 0) 1182cfcc706cSMiquel Raynal return data_bitflips; 1183cfcc706cSMiquel Raynal 1184cfcc706cSMiquel Raynal bitflips_threshold -= data_bitflips; 1185cfcc706cSMiquel Raynal 1186cfcc706cSMiquel Raynal ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); 1187cfcc706cSMiquel Raynal if (ecc_bitflips < 0) 1188cfcc706cSMiquel Raynal return ecc_bitflips; 1189cfcc706cSMiquel Raynal 1190cfcc706cSMiquel Raynal bitflips_threshold -= ecc_bitflips; 1191cfcc706cSMiquel Raynal 1192cfcc706cSMiquel Raynal extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, 1193cfcc706cSMiquel Raynal bitflips_threshold); 1194cfcc706cSMiquel Raynal if (extraoob_bitflips < 0) 1195cfcc706cSMiquel Raynal return extraoob_bitflips; 1196cfcc706cSMiquel Raynal 1197cfcc706cSMiquel Raynal if (data_bitflips) 1198cfcc706cSMiquel Raynal memset(data, 0xff, datalen); 1199cfcc706cSMiquel Raynal 1200cfcc706cSMiquel Raynal if (ecc_bitflips) 1201cfcc706cSMiquel Raynal memset(ecc, 0xff, ecclen); 1202cfcc706cSMiquel Raynal 1203cfcc706cSMiquel Raynal if (extraoob_bitflips) 1204cfcc706cSMiquel Raynal memset(extraoob, 0xff, extraooblen); 1205cfcc706cSMiquel Raynal 1206cfcc706cSMiquel Raynal return data_bitflips + ecc_bitflips + extraoob_bitflips; 1207cfcc706cSMiquel Raynal } 1208cfcc706cSMiquel Raynal EXPORT_SYMBOL(nand_check_erased_ecc_chunk); 1209cfcc706cSMiquel Raynal 1210cfcc706cSMiquel Raynal /** 1211cfcc706cSMiquel Raynal * nand_read_page_raw - [INTERN] read raw page data without ecc 1212cfcc706cSMiquel Raynal * @mtd: mtd info structure 1213cfcc706cSMiquel Raynal * @chip: nand chip info structure 1214cfcc706cSMiquel Raynal * @buf: buffer to store read data 1215cfcc706cSMiquel Raynal * @oob_required: caller requires OOB data read to chip->oob_poi 1216cfcc706cSMiquel Raynal * @page: page number to read 1217cfcc706cSMiquel Raynal * 1218cfcc706cSMiquel Raynal * Not for syndrome calculating ECC controllers, which use a special oob layout. 1219cfcc706cSMiquel Raynal */ 1220cfcc706cSMiquel Raynal static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, 1221cfcc706cSMiquel Raynal uint8_t *buf, int oob_required, int page) 1222cfcc706cSMiquel Raynal { 1223cfcc706cSMiquel Raynal chip->read_buf(mtd, buf, mtd->writesize); 1224cfcc706cSMiquel Raynal if (oob_required) 1225cfcc706cSMiquel Raynal chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); 1226cfcc706cSMiquel Raynal return 0; 1227cfcc706cSMiquel Raynal } 1228cfcc706cSMiquel Raynal 1229cfcc706cSMiquel Raynal /** 1230cfcc706cSMiquel Raynal * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc 1231cfcc706cSMiquel Raynal * @mtd: mtd info structure 1232cfcc706cSMiquel Raynal * @chip: nand chip info structure 1233cfcc706cSMiquel Raynal * @buf: buffer to store read data 1234cfcc706cSMiquel Raynal * @oob_required: caller requires OOB data read to chip->oob_poi 1235cfcc706cSMiquel Raynal * @page: page number to read 1236cfcc706cSMiquel Raynal * 1237cfcc706cSMiquel Raynal * We need a special oob layout and handling even when OOB isn't used. 1238cfcc706cSMiquel Raynal */ 1239cfcc706cSMiquel Raynal static int nand_read_page_raw_syndrome(struct mtd_info *mtd, 1240cfcc706cSMiquel Raynal struct nand_chip *chip, uint8_t *buf, 1241cfcc706cSMiquel Raynal int oob_required, int page) 1242cfcc706cSMiquel Raynal { 1243cfcc706cSMiquel Raynal int eccsize = chip->ecc.size; 1244cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes; 1245cfcc706cSMiquel Raynal uint8_t *oob = chip->oob_poi; 1246cfcc706cSMiquel Raynal int steps, size; 1247cfcc706cSMiquel Raynal 1248cfcc706cSMiquel Raynal for (steps = chip->ecc.steps; steps > 0; steps--) { 1249cfcc706cSMiquel Raynal chip->read_buf(mtd, buf, eccsize); 1250cfcc706cSMiquel Raynal buf += eccsize; 1251cfcc706cSMiquel Raynal 1252cfcc706cSMiquel Raynal if (chip->ecc.prepad) { 1253cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, chip->ecc.prepad); 1254cfcc706cSMiquel Raynal oob += chip->ecc.prepad; 1255cfcc706cSMiquel Raynal } 1256cfcc706cSMiquel Raynal 1257cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, eccbytes); 1258cfcc706cSMiquel Raynal oob += eccbytes; 1259cfcc706cSMiquel Raynal 1260cfcc706cSMiquel Raynal if (chip->ecc.postpad) { 1261cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, chip->ecc.postpad); 1262cfcc706cSMiquel Raynal oob += chip->ecc.postpad; 1263cfcc706cSMiquel Raynal } 1264cfcc706cSMiquel Raynal } 1265cfcc706cSMiquel Raynal 1266cfcc706cSMiquel Raynal size = mtd->oobsize - (oob - chip->oob_poi); 1267cfcc706cSMiquel Raynal if (size) 1268cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, size); 1269cfcc706cSMiquel Raynal 1270cfcc706cSMiquel Raynal return 0; 1271cfcc706cSMiquel Raynal } 1272cfcc706cSMiquel Raynal 1273cfcc706cSMiquel Raynal /** 1274cfcc706cSMiquel Raynal * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function 1275cfcc706cSMiquel Raynal * @mtd: mtd info structure 1276cfcc706cSMiquel Raynal * @chip: nand chip info structure 1277cfcc706cSMiquel Raynal * @buf: buffer to store read data 1278cfcc706cSMiquel Raynal * @oob_required: caller requires OOB data read to chip->oob_poi 1279cfcc706cSMiquel Raynal * @page: page number to read 1280cfcc706cSMiquel Raynal */ 1281cfcc706cSMiquel Raynal static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, 1282cfcc706cSMiquel Raynal uint8_t *buf, int oob_required, int page) 1283cfcc706cSMiquel Raynal { 1284cfcc706cSMiquel Raynal int i, eccsize = chip->ecc.size; 1285cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes; 1286cfcc706cSMiquel Raynal int eccsteps = chip->ecc.steps; 1287cfcc706cSMiquel Raynal uint8_t *p = buf; 1288cfcc706cSMiquel Raynal uint8_t *ecc_calc = chip->buffers->ecccalc; 1289cfcc706cSMiquel Raynal uint8_t *ecc_code = chip->buffers->ecccode; 1290cfcc706cSMiquel Raynal uint32_t *eccpos = chip->ecc.layout->eccpos; 1291cfcc706cSMiquel Raynal unsigned int max_bitflips = 0; 1292cfcc706cSMiquel Raynal 1293cfcc706cSMiquel Raynal chip->ecc.read_page_raw(mtd, chip, buf, 1, page); 1294cfcc706cSMiquel Raynal 1295cfcc706cSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) 1296cfcc706cSMiquel Raynal chip->ecc.calculate(mtd, p, &ecc_calc[i]); 1297cfcc706cSMiquel Raynal 1298cfcc706cSMiquel Raynal for (i = 0; i < chip->ecc.total; i++) 1299cfcc706cSMiquel Raynal ecc_code[i] = chip->oob_poi[eccpos[i]]; 1300cfcc706cSMiquel Raynal 1301cfcc706cSMiquel Raynal eccsteps = chip->ecc.steps; 1302cfcc706cSMiquel Raynal p = buf; 1303cfcc706cSMiquel Raynal 1304cfcc706cSMiquel Raynal for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { 1305cfcc706cSMiquel Raynal int stat; 1306cfcc706cSMiquel Raynal 1307cfcc706cSMiquel Raynal stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); 1308cfcc706cSMiquel Raynal if (stat < 0) { 1309cfcc706cSMiquel Raynal mtd->ecc_stats.failed++; 1310cfcc706cSMiquel Raynal } else { 1311cfcc706cSMiquel Raynal mtd->ecc_stats.corrected += stat; 1312cfcc706cSMiquel Raynal max_bitflips = max_t(unsigned int, max_bitflips, stat); 1313cfcc706cSMiquel Raynal } 1314cfcc706cSMiquel Raynal } 1315cfcc706cSMiquel Raynal return max_bitflips; 1316cfcc706cSMiquel Raynal } 1317cfcc706cSMiquel Raynal 1318cfcc706cSMiquel Raynal /** 1319cfcc706cSMiquel Raynal * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function 1320cfcc706cSMiquel Raynal * @mtd: mtd info structure 1321cfcc706cSMiquel Raynal * @chip: nand chip info structure 1322cfcc706cSMiquel Raynal * @data_offs: offset of requested data within the page 1323cfcc706cSMiquel Raynal * @readlen: data length 1324cfcc706cSMiquel Raynal * @bufpoi: buffer to store read data 1325cfcc706cSMiquel Raynal * @page: page number to read 1326cfcc706cSMiquel Raynal */ 1327cfcc706cSMiquel Raynal static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, 1328cfcc706cSMiquel Raynal uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi, 1329cfcc706cSMiquel Raynal int page) 1330cfcc706cSMiquel Raynal { 1331cfcc706cSMiquel Raynal int start_step, end_step, num_steps; 1332cfcc706cSMiquel Raynal uint32_t *eccpos = chip->ecc.layout->eccpos; 1333cfcc706cSMiquel Raynal uint8_t *p; 1334cfcc706cSMiquel Raynal int data_col_addr, i, gaps = 0; 1335cfcc706cSMiquel Raynal int datafrag_len, eccfrag_len, aligned_len, aligned_pos; 1336cfcc706cSMiquel Raynal int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; 1337cfcc706cSMiquel Raynal int index; 1338cfcc706cSMiquel Raynal unsigned int max_bitflips = 0; 1339cfcc706cSMiquel Raynal 1340cfcc706cSMiquel Raynal /* Column address within the page aligned to ECC size (256bytes) */ 1341cfcc706cSMiquel Raynal start_step = data_offs / chip->ecc.size; 1342cfcc706cSMiquel Raynal end_step = (data_offs + readlen - 1) / chip->ecc.size; 1343cfcc706cSMiquel Raynal num_steps = end_step - start_step + 1; 1344cfcc706cSMiquel Raynal index = start_step * chip->ecc.bytes; 1345cfcc706cSMiquel Raynal 1346cfcc706cSMiquel Raynal /* Data size aligned to ECC ecc.size */ 1347cfcc706cSMiquel Raynal datafrag_len = num_steps * chip->ecc.size; 1348cfcc706cSMiquel Raynal eccfrag_len = num_steps * chip->ecc.bytes; 1349cfcc706cSMiquel Raynal 1350cfcc706cSMiquel Raynal data_col_addr = start_step * chip->ecc.size; 1351cfcc706cSMiquel Raynal /* If we read not a page aligned data */ 1352cfcc706cSMiquel Raynal if (data_col_addr != 0) 1353cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1); 1354cfcc706cSMiquel Raynal 1355cfcc706cSMiquel Raynal p = bufpoi + data_col_addr; 1356cfcc706cSMiquel Raynal chip->read_buf(mtd, p, datafrag_len); 1357cfcc706cSMiquel Raynal 1358cfcc706cSMiquel Raynal /* Calculate ECC */ 1359cfcc706cSMiquel Raynal for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) 1360cfcc706cSMiquel Raynal chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); 1361cfcc706cSMiquel Raynal 1362cfcc706cSMiquel Raynal /* 1363cfcc706cSMiquel Raynal * The performance is faster if we position offsets according to 1364cfcc706cSMiquel Raynal * ecc.pos. Let's make sure that there are no gaps in ECC positions. 1365cfcc706cSMiquel Raynal */ 1366cfcc706cSMiquel Raynal for (i = 0; i < eccfrag_len - 1; i++) { 1367cfcc706cSMiquel Raynal if (eccpos[i + index] + 1 != eccpos[i + index + 1]) { 1368cfcc706cSMiquel Raynal gaps = 1; 1369cfcc706cSMiquel Raynal break; 1370cfcc706cSMiquel Raynal } 1371cfcc706cSMiquel Raynal } 1372cfcc706cSMiquel Raynal if (gaps) { 1373cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); 1374cfcc706cSMiquel Raynal chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); 1375cfcc706cSMiquel Raynal } else { 1376cfcc706cSMiquel Raynal /* 1377cfcc706cSMiquel Raynal * Send the command to read the particular ECC bytes take care 1378cfcc706cSMiquel Raynal * about buswidth alignment in read_buf. 1379cfcc706cSMiquel Raynal */ 1380cfcc706cSMiquel Raynal aligned_pos = eccpos[index] & ~(busw - 1); 1381cfcc706cSMiquel Raynal aligned_len = eccfrag_len; 1382cfcc706cSMiquel Raynal if (eccpos[index] & (busw - 1)) 1383cfcc706cSMiquel Raynal aligned_len++; 1384cfcc706cSMiquel Raynal if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1)) 1385cfcc706cSMiquel Raynal aligned_len++; 1386cfcc706cSMiquel Raynal 1387cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_RNDOUT, 1388cfcc706cSMiquel Raynal mtd->writesize + aligned_pos, -1); 1389cfcc706cSMiquel Raynal chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len); 1390cfcc706cSMiquel Raynal } 1391cfcc706cSMiquel Raynal 1392cfcc706cSMiquel Raynal for (i = 0; i < eccfrag_len; i++) 1393cfcc706cSMiquel Raynal chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]]; 1394cfcc706cSMiquel Raynal 1395cfcc706cSMiquel Raynal p = bufpoi + data_col_addr; 1396cfcc706cSMiquel Raynal for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { 1397cfcc706cSMiquel Raynal int stat; 1398cfcc706cSMiquel Raynal 1399cfcc706cSMiquel Raynal stat = chip->ecc.correct(mtd, p, 1400cfcc706cSMiquel Raynal &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); 1401cfcc706cSMiquel Raynal if (stat == -EBADMSG && 1402cfcc706cSMiquel Raynal (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { 1403cfcc706cSMiquel Raynal /* check for empty pages with bitflips */ 1404cfcc706cSMiquel Raynal stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, 1405cfcc706cSMiquel Raynal &chip->buffers->ecccode[i], 1406cfcc706cSMiquel Raynal chip->ecc.bytes, 1407cfcc706cSMiquel Raynal NULL, 0, 1408cfcc706cSMiquel Raynal chip->ecc.strength); 1409cfcc706cSMiquel Raynal } 1410cfcc706cSMiquel Raynal 1411cfcc706cSMiquel Raynal if (stat < 0) { 1412cfcc706cSMiquel Raynal mtd->ecc_stats.failed++; 1413cfcc706cSMiquel Raynal } else { 1414cfcc706cSMiquel Raynal mtd->ecc_stats.corrected += stat; 1415cfcc706cSMiquel Raynal max_bitflips = max_t(unsigned int, max_bitflips, stat); 1416cfcc706cSMiquel Raynal } 1417cfcc706cSMiquel Raynal } 1418cfcc706cSMiquel Raynal return max_bitflips; 1419cfcc706cSMiquel Raynal } 1420cfcc706cSMiquel Raynal 1421cfcc706cSMiquel Raynal /** 1422cfcc706cSMiquel Raynal * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function 1423cfcc706cSMiquel Raynal * @mtd: mtd info structure 1424cfcc706cSMiquel Raynal * @chip: nand chip info structure 1425cfcc706cSMiquel Raynal * @buf: buffer to store read data 1426cfcc706cSMiquel Raynal * @oob_required: caller requires OOB data read to chip->oob_poi 1427cfcc706cSMiquel Raynal * @page: page number to read 1428cfcc706cSMiquel Raynal * 1429cfcc706cSMiquel Raynal * Not for syndrome calculating ECC controllers which need a special oob layout. 1430cfcc706cSMiquel Raynal */ 1431cfcc706cSMiquel Raynal static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, 1432cfcc706cSMiquel Raynal uint8_t *buf, int oob_required, int page) 1433cfcc706cSMiquel Raynal { 1434cfcc706cSMiquel Raynal int i, eccsize = chip->ecc.size; 1435cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes; 1436cfcc706cSMiquel Raynal int eccsteps = chip->ecc.steps; 1437cfcc706cSMiquel Raynal uint8_t *p = buf; 1438cfcc706cSMiquel Raynal uint8_t *ecc_calc = chip->buffers->ecccalc; 1439cfcc706cSMiquel Raynal uint8_t *ecc_code = chip->buffers->ecccode; 1440cfcc706cSMiquel Raynal uint32_t *eccpos = chip->ecc.layout->eccpos; 1441cfcc706cSMiquel Raynal unsigned int max_bitflips = 0; 1442cfcc706cSMiquel Raynal 1443cfcc706cSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { 1444cfcc706cSMiquel Raynal chip->ecc.hwctl(mtd, NAND_ECC_READ); 1445cfcc706cSMiquel Raynal chip->read_buf(mtd, p, eccsize); 1446cfcc706cSMiquel Raynal chip->ecc.calculate(mtd, p, &ecc_calc[i]); 1447cfcc706cSMiquel Raynal } 1448cfcc706cSMiquel Raynal chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); 1449cfcc706cSMiquel Raynal 1450cfcc706cSMiquel Raynal for (i = 0; i < chip->ecc.total; i++) 1451cfcc706cSMiquel Raynal ecc_code[i] = chip->oob_poi[eccpos[i]]; 1452cfcc706cSMiquel Raynal 1453cfcc706cSMiquel Raynal eccsteps = chip->ecc.steps; 1454cfcc706cSMiquel Raynal p = buf; 1455cfcc706cSMiquel Raynal 1456cfcc706cSMiquel Raynal for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { 1457cfcc706cSMiquel Raynal int stat; 1458cfcc706cSMiquel Raynal 1459cfcc706cSMiquel Raynal stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); 1460cfcc706cSMiquel Raynal if (stat == -EBADMSG && 1461cfcc706cSMiquel Raynal (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { 1462cfcc706cSMiquel Raynal /* check for empty pages with bitflips */ 1463cfcc706cSMiquel Raynal stat = nand_check_erased_ecc_chunk(p, eccsize, 1464cfcc706cSMiquel Raynal &ecc_code[i], eccbytes, 1465cfcc706cSMiquel Raynal NULL, 0, 1466cfcc706cSMiquel Raynal chip->ecc.strength); 1467cfcc706cSMiquel Raynal } 1468cfcc706cSMiquel Raynal 1469cfcc706cSMiquel Raynal if (stat < 0) { 1470cfcc706cSMiquel Raynal mtd->ecc_stats.failed++; 1471cfcc706cSMiquel Raynal } else { 1472cfcc706cSMiquel Raynal mtd->ecc_stats.corrected += stat; 1473cfcc706cSMiquel Raynal max_bitflips = max_t(unsigned int, max_bitflips, stat); 1474cfcc706cSMiquel Raynal } 1475cfcc706cSMiquel Raynal } 1476cfcc706cSMiquel Raynal return max_bitflips; 1477cfcc706cSMiquel Raynal } 1478cfcc706cSMiquel Raynal 1479cfcc706cSMiquel Raynal /** 1480cfcc706cSMiquel Raynal * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first 1481cfcc706cSMiquel Raynal * @mtd: mtd info structure 1482cfcc706cSMiquel Raynal * @chip: nand chip info structure 1483cfcc706cSMiquel Raynal * @buf: buffer to store read data 1484cfcc706cSMiquel Raynal * @oob_required: caller requires OOB data read to chip->oob_poi 1485cfcc706cSMiquel Raynal * @page: page number to read 1486cfcc706cSMiquel Raynal * 1487cfcc706cSMiquel Raynal * Hardware ECC for large page chips, require OOB to be read first. For this 1488cfcc706cSMiquel Raynal * ECC mode, the write_page method is re-used from ECC_HW. These methods 1489cfcc706cSMiquel Raynal * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with 1490cfcc706cSMiquel Raynal * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from 1491cfcc706cSMiquel Raynal * the data area, by overwriting the NAND manufacturer bad block markings. 1492cfcc706cSMiquel Raynal */ 1493cfcc706cSMiquel Raynal static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, 1494cfcc706cSMiquel Raynal struct nand_chip *chip, uint8_t *buf, int oob_required, int page) 1495cfcc706cSMiquel Raynal { 1496cfcc706cSMiquel Raynal int i, eccsize = chip->ecc.size; 1497cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes; 1498cfcc706cSMiquel Raynal int eccsteps = chip->ecc.steps; 1499cfcc706cSMiquel Raynal uint8_t *p = buf; 1500cfcc706cSMiquel Raynal uint8_t *ecc_code = chip->buffers->ecccode; 1501cfcc706cSMiquel Raynal uint32_t *eccpos = chip->ecc.layout->eccpos; 1502cfcc706cSMiquel Raynal uint8_t *ecc_calc = chip->buffers->ecccalc; 1503cfcc706cSMiquel Raynal unsigned int max_bitflips = 0; 1504cfcc706cSMiquel Raynal 1505cfcc706cSMiquel Raynal /* Read the OOB area first */ 1506cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); 1507cfcc706cSMiquel Raynal chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); 1508cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); 1509cfcc706cSMiquel Raynal 1510cfcc706cSMiquel Raynal for (i = 0; i < chip->ecc.total; i++) 1511cfcc706cSMiquel Raynal ecc_code[i] = chip->oob_poi[eccpos[i]]; 1512cfcc706cSMiquel Raynal 1513cfcc706cSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { 1514cfcc706cSMiquel Raynal int stat; 1515cfcc706cSMiquel Raynal 1516cfcc706cSMiquel Raynal chip->ecc.hwctl(mtd, NAND_ECC_READ); 1517cfcc706cSMiquel Raynal chip->read_buf(mtd, p, eccsize); 1518cfcc706cSMiquel Raynal chip->ecc.calculate(mtd, p, &ecc_calc[i]); 1519cfcc706cSMiquel Raynal 1520cfcc706cSMiquel Raynal stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); 1521cfcc706cSMiquel Raynal if (stat == -EBADMSG && 1522cfcc706cSMiquel Raynal (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { 1523cfcc706cSMiquel Raynal /* check for empty pages with bitflips */ 1524cfcc706cSMiquel Raynal stat = nand_check_erased_ecc_chunk(p, eccsize, 1525cfcc706cSMiquel Raynal &ecc_code[i], eccbytes, 1526cfcc706cSMiquel Raynal NULL, 0, 1527cfcc706cSMiquel Raynal chip->ecc.strength); 1528cfcc706cSMiquel Raynal } 1529cfcc706cSMiquel Raynal 1530cfcc706cSMiquel Raynal if (stat < 0) { 1531cfcc706cSMiquel Raynal mtd->ecc_stats.failed++; 1532cfcc706cSMiquel Raynal } else { 1533cfcc706cSMiquel Raynal mtd->ecc_stats.corrected += stat; 1534cfcc706cSMiquel Raynal max_bitflips = max_t(unsigned int, max_bitflips, stat); 1535cfcc706cSMiquel Raynal } 1536cfcc706cSMiquel Raynal } 1537cfcc706cSMiquel Raynal return max_bitflips; 1538cfcc706cSMiquel Raynal } 1539cfcc706cSMiquel Raynal 1540cfcc706cSMiquel Raynal /** 1541cfcc706cSMiquel Raynal * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read 1542cfcc706cSMiquel Raynal * @mtd: mtd info structure 1543cfcc706cSMiquel Raynal * @chip: nand chip info structure 1544cfcc706cSMiquel Raynal * @buf: buffer to store read data 1545cfcc706cSMiquel Raynal * @oob_required: caller requires OOB data read to chip->oob_poi 1546cfcc706cSMiquel Raynal * @page: page number to read 1547cfcc706cSMiquel Raynal * 1548cfcc706cSMiquel Raynal * The hw generator calculates the error syndrome automatically. Therefore we 1549cfcc706cSMiquel Raynal * need a special oob layout and handling. 1550cfcc706cSMiquel Raynal */ 1551cfcc706cSMiquel Raynal static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip, 1552cfcc706cSMiquel Raynal uint8_t *buf, int oob_required, int page) 1553cfcc706cSMiquel Raynal { 1554cfcc706cSMiquel Raynal int i, eccsize = chip->ecc.size; 1555cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes; 1556cfcc706cSMiquel Raynal int eccsteps = chip->ecc.steps; 1557cfcc706cSMiquel Raynal int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; 1558cfcc706cSMiquel Raynal uint8_t *p = buf; 1559cfcc706cSMiquel Raynal uint8_t *oob = chip->oob_poi; 1560cfcc706cSMiquel Raynal unsigned int max_bitflips = 0; 1561cfcc706cSMiquel Raynal 1562cfcc706cSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { 1563cfcc706cSMiquel Raynal int stat; 1564cfcc706cSMiquel Raynal 1565cfcc706cSMiquel Raynal chip->ecc.hwctl(mtd, NAND_ECC_READ); 1566cfcc706cSMiquel Raynal chip->read_buf(mtd, p, eccsize); 1567cfcc706cSMiquel Raynal 1568cfcc706cSMiquel Raynal if (chip->ecc.prepad) { 1569cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, chip->ecc.prepad); 1570cfcc706cSMiquel Raynal oob += chip->ecc.prepad; 1571cfcc706cSMiquel Raynal } 1572cfcc706cSMiquel Raynal 1573cfcc706cSMiquel Raynal chip->ecc.hwctl(mtd, NAND_ECC_READSYN); 1574cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, eccbytes); 1575cfcc706cSMiquel Raynal stat = chip->ecc.correct(mtd, p, oob, NULL); 1576cfcc706cSMiquel Raynal 1577cfcc706cSMiquel Raynal oob += eccbytes; 1578cfcc706cSMiquel Raynal 1579cfcc706cSMiquel Raynal if (chip->ecc.postpad) { 1580cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, chip->ecc.postpad); 1581cfcc706cSMiquel Raynal oob += chip->ecc.postpad; 1582cfcc706cSMiquel Raynal } 1583cfcc706cSMiquel Raynal 1584cfcc706cSMiquel Raynal if (stat == -EBADMSG && 1585cfcc706cSMiquel Raynal (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { 1586cfcc706cSMiquel Raynal /* check for empty pages with bitflips */ 1587cfcc706cSMiquel Raynal stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, 1588cfcc706cSMiquel Raynal oob - eccpadbytes, 1589cfcc706cSMiquel Raynal eccpadbytes, 1590cfcc706cSMiquel Raynal NULL, 0, 1591cfcc706cSMiquel Raynal chip->ecc.strength); 1592cfcc706cSMiquel Raynal } 1593cfcc706cSMiquel Raynal 1594cfcc706cSMiquel Raynal if (stat < 0) { 1595cfcc706cSMiquel Raynal mtd->ecc_stats.failed++; 1596cfcc706cSMiquel Raynal } else { 1597cfcc706cSMiquel Raynal mtd->ecc_stats.corrected += stat; 1598cfcc706cSMiquel Raynal max_bitflips = max_t(unsigned int, max_bitflips, stat); 1599cfcc706cSMiquel Raynal } 1600cfcc706cSMiquel Raynal } 1601cfcc706cSMiquel Raynal 1602cfcc706cSMiquel Raynal /* Calculate remaining oob bytes */ 1603cfcc706cSMiquel Raynal i = mtd->oobsize - (oob - chip->oob_poi); 1604cfcc706cSMiquel Raynal if (i) 1605cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, i); 1606cfcc706cSMiquel Raynal 1607cfcc706cSMiquel Raynal return max_bitflips; 1608cfcc706cSMiquel Raynal } 1609cfcc706cSMiquel Raynal 1610cfcc706cSMiquel Raynal /** 1611cfcc706cSMiquel Raynal * nand_transfer_oob - [INTERN] Transfer oob to client buffer 1612cfcc706cSMiquel Raynal * @chip: nand chip structure 1613cfcc706cSMiquel Raynal * @oob: oob destination address 1614cfcc706cSMiquel Raynal * @ops: oob ops structure 1615cfcc706cSMiquel Raynal * @len: size of oob to transfer 1616cfcc706cSMiquel Raynal */ 1617cfcc706cSMiquel Raynal static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob, 1618cfcc706cSMiquel Raynal struct mtd_oob_ops *ops, size_t len) 1619cfcc706cSMiquel Raynal { 1620cfcc706cSMiquel Raynal switch (ops->mode) { 1621cfcc706cSMiquel Raynal 1622cfcc706cSMiquel Raynal case MTD_OPS_PLACE_OOB: 1623cfcc706cSMiquel Raynal case MTD_OPS_RAW: 1624cfcc706cSMiquel Raynal memcpy(oob, chip->oob_poi + ops->ooboffs, len); 1625cfcc706cSMiquel Raynal return oob + len; 1626cfcc706cSMiquel Raynal 1627cfcc706cSMiquel Raynal case MTD_OPS_AUTO_OOB: { 1628cfcc706cSMiquel Raynal struct nand_oobfree *free = chip->ecc.layout->oobfree; 1629cfcc706cSMiquel Raynal uint32_t boffs = 0, roffs = ops->ooboffs; 1630cfcc706cSMiquel Raynal size_t bytes = 0; 1631cfcc706cSMiquel Raynal 1632cfcc706cSMiquel Raynal for (; free->length && len; free++, len -= bytes) { 1633cfcc706cSMiquel Raynal /* Read request not from offset 0? */ 1634cfcc706cSMiquel Raynal if (unlikely(roffs)) { 1635cfcc706cSMiquel Raynal if (roffs >= free->length) { 1636cfcc706cSMiquel Raynal roffs -= free->length; 1637cfcc706cSMiquel Raynal continue; 1638cfcc706cSMiquel Raynal } 1639cfcc706cSMiquel Raynal boffs = free->offset + roffs; 1640cfcc706cSMiquel Raynal bytes = min_t(size_t, len, 1641cfcc706cSMiquel Raynal (free->length - roffs)); 1642cfcc706cSMiquel Raynal roffs = 0; 1643cfcc706cSMiquel Raynal } else { 1644cfcc706cSMiquel Raynal bytes = min_t(size_t, len, free->length); 1645cfcc706cSMiquel Raynal boffs = free->offset; 1646cfcc706cSMiquel Raynal } 1647cfcc706cSMiquel Raynal memcpy(oob, chip->oob_poi + boffs, bytes); 1648cfcc706cSMiquel Raynal oob += bytes; 1649cfcc706cSMiquel Raynal } 1650cfcc706cSMiquel Raynal return oob; 1651cfcc706cSMiquel Raynal } 1652cfcc706cSMiquel Raynal default: 1653cfcc706cSMiquel Raynal BUG(); 1654cfcc706cSMiquel Raynal } 1655cfcc706cSMiquel Raynal return NULL; 1656cfcc706cSMiquel Raynal } 1657cfcc706cSMiquel Raynal 1658cfcc706cSMiquel Raynal /** 1659cfcc706cSMiquel Raynal * nand_setup_read_retry - [INTERN] Set the READ RETRY mode 1660cfcc706cSMiquel Raynal * @mtd: MTD device structure 1661cfcc706cSMiquel Raynal * @retry_mode: the retry mode to use 1662cfcc706cSMiquel Raynal * 1663cfcc706cSMiquel Raynal * Some vendors supply a special command to shift the Vt threshold, to be used 1664cfcc706cSMiquel Raynal * when there are too many bitflips in a page (i.e., ECC error). After setting 1665cfcc706cSMiquel Raynal * a new threshold, the host should retry reading the page. 1666cfcc706cSMiquel Raynal */ 1667cfcc706cSMiquel Raynal static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode) 1668cfcc706cSMiquel Raynal { 1669cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 1670cfcc706cSMiquel Raynal 1671cfcc706cSMiquel Raynal pr_debug("setting READ RETRY mode %d\n", retry_mode); 1672cfcc706cSMiquel Raynal 1673cfcc706cSMiquel Raynal if (retry_mode >= chip->read_retries) 1674cfcc706cSMiquel Raynal return -EINVAL; 1675cfcc706cSMiquel Raynal 1676cfcc706cSMiquel Raynal if (!chip->setup_read_retry) 1677cfcc706cSMiquel Raynal return -EOPNOTSUPP; 1678cfcc706cSMiquel Raynal 1679cfcc706cSMiquel Raynal return chip->setup_read_retry(mtd, retry_mode); 1680cfcc706cSMiquel Raynal } 1681cfcc706cSMiquel Raynal 1682cfcc706cSMiquel Raynal /** 1683cfcc706cSMiquel Raynal * nand_do_read_ops - [INTERN] Read data with ECC 1684cfcc706cSMiquel Raynal * @mtd: MTD device structure 1685cfcc706cSMiquel Raynal * @from: offset to read from 1686cfcc706cSMiquel Raynal * @ops: oob ops structure 1687cfcc706cSMiquel Raynal * 1688cfcc706cSMiquel Raynal * Internal function. Called with chip held. 1689cfcc706cSMiquel Raynal */ 1690cfcc706cSMiquel Raynal static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, 1691cfcc706cSMiquel Raynal struct mtd_oob_ops *ops) 1692cfcc706cSMiquel Raynal { 1693cfcc706cSMiquel Raynal int chipnr, page, realpage, col, bytes, aligned, oob_required; 1694cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 1695cfcc706cSMiquel Raynal int ret = 0; 1696cfcc706cSMiquel Raynal uint32_t readlen = ops->len; 1697cfcc706cSMiquel Raynal uint32_t oobreadlen = ops->ooblen; 1698cfcc706cSMiquel Raynal uint32_t max_oobsize = mtd_oobavail(mtd, ops); 1699cfcc706cSMiquel Raynal 1700cfcc706cSMiquel Raynal uint8_t *bufpoi, *oob, *buf; 1701cfcc706cSMiquel Raynal int use_bufpoi; 1702cfcc706cSMiquel Raynal unsigned int max_bitflips = 0; 1703cfcc706cSMiquel Raynal int retry_mode = 0; 1704cfcc706cSMiquel Raynal bool ecc_fail = false; 1705cfcc706cSMiquel Raynal 1706cfcc706cSMiquel Raynal chipnr = (int)(from >> chip->chip_shift); 1707cfcc706cSMiquel Raynal chip->select_chip(mtd, chipnr); 1708cfcc706cSMiquel Raynal 1709cfcc706cSMiquel Raynal realpage = (int)(from >> chip->page_shift); 1710cfcc706cSMiquel Raynal page = realpage & chip->pagemask; 1711cfcc706cSMiquel Raynal 1712cfcc706cSMiquel Raynal col = (int)(from & (mtd->writesize - 1)); 1713cfcc706cSMiquel Raynal 1714cfcc706cSMiquel Raynal buf = ops->datbuf; 1715cfcc706cSMiquel Raynal oob = ops->oobbuf; 1716cfcc706cSMiquel Raynal oob_required = oob ? 1 : 0; 1717cfcc706cSMiquel Raynal 1718cfcc706cSMiquel Raynal while (1) { 1719cfcc706cSMiquel Raynal unsigned int ecc_failures = mtd->ecc_stats.failed; 1720cfcc706cSMiquel Raynal 1721cfcc706cSMiquel Raynal WATCHDOG_RESET(); 1722cfcc706cSMiquel Raynal bytes = min(mtd->writesize - col, readlen); 1723cfcc706cSMiquel Raynal aligned = (bytes == mtd->writesize); 1724cfcc706cSMiquel Raynal 1725cfcc706cSMiquel Raynal if (!aligned) 1726cfcc706cSMiquel Raynal use_bufpoi = 1; 1727cfcc706cSMiquel Raynal else if (chip->options & NAND_USE_BOUNCE_BUFFER) 1728cfcc706cSMiquel Raynal use_bufpoi = !IS_ALIGNED((unsigned long)buf, 1729cfcc706cSMiquel Raynal chip->buf_align); 1730cfcc706cSMiquel Raynal else 1731cfcc706cSMiquel Raynal use_bufpoi = 0; 1732cfcc706cSMiquel Raynal 1733cfcc706cSMiquel Raynal /* Is the current page in the buffer? */ 1734cfcc706cSMiquel Raynal if (realpage != chip->pagebuf || oob) { 1735cfcc706cSMiquel Raynal bufpoi = use_bufpoi ? chip->buffers->databuf : buf; 1736cfcc706cSMiquel Raynal 1737cfcc706cSMiquel Raynal if (use_bufpoi && aligned) 1738cfcc706cSMiquel Raynal pr_debug("%s: using read bounce buffer for buf@%p\n", 1739cfcc706cSMiquel Raynal __func__, buf); 1740cfcc706cSMiquel Raynal 1741cfcc706cSMiquel Raynal read_retry: 1742cfcc706cSMiquel Raynal if (nand_standard_page_accessors(&chip->ecc)) 1743cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); 1744cfcc706cSMiquel Raynal 1745cfcc706cSMiquel Raynal /* 1746cfcc706cSMiquel Raynal * Now read the page into the buffer. Absent an error, 1747cfcc706cSMiquel Raynal * the read methods return max bitflips per ecc step. 1748cfcc706cSMiquel Raynal */ 1749cfcc706cSMiquel Raynal if (unlikely(ops->mode == MTD_OPS_RAW)) 1750cfcc706cSMiquel Raynal ret = chip->ecc.read_page_raw(mtd, chip, bufpoi, 1751cfcc706cSMiquel Raynal oob_required, 1752cfcc706cSMiquel Raynal page); 1753cfcc706cSMiquel Raynal else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && 1754cfcc706cSMiquel Raynal !oob) 1755cfcc706cSMiquel Raynal ret = chip->ecc.read_subpage(mtd, chip, 1756cfcc706cSMiquel Raynal col, bytes, bufpoi, 1757cfcc706cSMiquel Raynal page); 1758cfcc706cSMiquel Raynal else 1759cfcc706cSMiquel Raynal ret = chip->ecc.read_page(mtd, chip, bufpoi, 1760cfcc706cSMiquel Raynal oob_required, page); 1761cfcc706cSMiquel Raynal if (ret < 0) { 1762cfcc706cSMiquel Raynal if (use_bufpoi) 1763cfcc706cSMiquel Raynal /* Invalidate page cache */ 1764cfcc706cSMiquel Raynal chip->pagebuf = -1; 1765cfcc706cSMiquel Raynal break; 1766cfcc706cSMiquel Raynal } 1767cfcc706cSMiquel Raynal 1768cfcc706cSMiquel Raynal max_bitflips = max_t(unsigned int, max_bitflips, ret); 1769cfcc706cSMiquel Raynal 1770cfcc706cSMiquel Raynal /* Transfer not aligned data */ 1771cfcc706cSMiquel Raynal if (use_bufpoi) { 1772cfcc706cSMiquel Raynal if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && 1773cfcc706cSMiquel Raynal !(mtd->ecc_stats.failed - ecc_failures) && 1774cfcc706cSMiquel Raynal (ops->mode != MTD_OPS_RAW)) { 1775cfcc706cSMiquel Raynal chip->pagebuf = realpage; 1776cfcc706cSMiquel Raynal chip->pagebuf_bitflips = ret; 1777cfcc706cSMiquel Raynal } else { 1778cfcc706cSMiquel Raynal /* Invalidate page cache */ 1779cfcc706cSMiquel Raynal chip->pagebuf = -1; 1780cfcc706cSMiquel Raynal } 1781cfcc706cSMiquel Raynal memcpy(buf, chip->buffers->databuf + col, bytes); 1782cfcc706cSMiquel Raynal } 1783cfcc706cSMiquel Raynal 1784cfcc706cSMiquel Raynal if (unlikely(oob)) { 1785cfcc706cSMiquel Raynal int toread = min(oobreadlen, max_oobsize); 1786cfcc706cSMiquel Raynal 1787cfcc706cSMiquel Raynal if (toread) { 1788cfcc706cSMiquel Raynal oob = nand_transfer_oob(chip, 1789cfcc706cSMiquel Raynal oob, ops, toread); 1790cfcc706cSMiquel Raynal oobreadlen -= toread; 1791cfcc706cSMiquel Raynal } 1792cfcc706cSMiquel Raynal } 1793cfcc706cSMiquel Raynal 1794cfcc706cSMiquel Raynal if (chip->options & NAND_NEED_READRDY) { 1795cfcc706cSMiquel Raynal /* Apply delay or wait for ready/busy pin */ 1796cfcc706cSMiquel Raynal if (!chip->dev_ready) 1797cfcc706cSMiquel Raynal udelay(chip->chip_delay); 1798cfcc706cSMiquel Raynal else 1799cfcc706cSMiquel Raynal nand_wait_ready(mtd); 1800cfcc706cSMiquel Raynal } 1801cfcc706cSMiquel Raynal 1802cfcc706cSMiquel Raynal if (mtd->ecc_stats.failed - ecc_failures) { 1803cfcc706cSMiquel Raynal if (retry_mode + 1 < chip->read_retries) { 1804cfcc706cSMiquel Raynal retry_mode++; 1805cfcc706cSMiquel Raynal ret = nand_setup_read_retry(mtd, 1806cfcc706cSMiquel Raynal retry_mode); 1807cfcc706cSMiquel Raynal if (ret < 0) 1808cfcc706cSMiquel Raynal break; 1809cfcc706cSMiquel Raynal 1810cfcc706cSMiquel Raynal /* Reset failures; retry */ 1811cfcc706cSMiquel Raynal mtd->ecc_stats.failed = ecc_failures; 1812cfcc706cSMiquel Raynal goto read_retry; 1813cfcc706cSMiquel Raynal } else { 1814cfcc706cSMiquel Raynal /* No more retry modes; real failure */ 1815cfcc706cSMiquel Raynal ecc_fail = true; 1816cfcc706cSMiquel Raynal } 1817cfcc706cSMiquel Raynal } 1818cfcc706cSMiquel Raynal 1819cfcc706cSMiquel Raynal buf += bytes; 1820cfcc706cSMiquel Raynal } else { 1821cfcc706cSMiquel Raynal memcpy(buf, chip->buffers->databuf + col, bytes); 1822cfcc706cSMiquel Raynal buf += bytes; 1823cfcc706cSMiquel Raynal max_bitflips = max_t(unsigned int, max_bitflips, 1824cfcc706cSMiquel Raynal chip->pagebuf_bitflips); 1825cfcc706cSMiquel Raynal } 1826cfcc706cSMiquel Raynal 1827cfcc706cSMiquel Raynal readlen -= bytes; 1828cfcc706cSMiquel Raynal 1829cfcc706cSMiquel Raynal /* Reset to retry mode 0 */ 1830cfcc706cSMiquel Raynal if (retry_mode) { 1831cfcc706cSMiquel Raynal ret = nand_setup_read_retry(mtd, 0); 1832cfcc706cSMiquel Raynal if (ret < 0) 1833cfcc706cSMiquel Raynal break; 1834cfcc706cSMiquel Raynal retry_mode = 0; 1835cfcc706cSMiquel Raynal } 1836cfcc706cSMiquel Raynal 1837cfcc706cSMiquel Raynal if (!readlen) 1838cfcc706cSMiquel Raynal break; 1839cfcc706cSMiquel Raynal 1840cfcc706cSMiquel Raynal /* For subsequent reads align to page boundary */ 1841cfcc706cSMiquel Raynal col = 0; 1842cfcc706cSMiquel Raynal /* Increment page address */ 1843cfcc706cSMiquel Raynal realpage++; 1844cfcc706cSMiquel Raynal 1845cfcc706cSMiquel Raynal page = realpage & chip->pagemask; 1846cfcc706cSMiquel Raynal /* Check, if we cross a chip boundary */ 1847cfcc706cSMiquel Raynal if (!page) { 1848cfcc706cSMiquel Raynal chipnr++; 1849cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 1850cfcc706cSMiquel Raynal chip->select_chip(mtd, chipnr); 1851cfcc706cSMiquel Raynal } 1852cfcc706cSMiquel Raynal } 1853cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 1854cfcc706cSMiquel Raynal 1855cfcc706cSMiquel Raynal ops->retlen = ops->len - (size_t) readlen; 1856cfcc706cSMiquel Raynal if (oob) 1857cfcc706cSMiquel Raynal ops->oobretlen = ops->ooblen - oobreadlen; 1858cfcc706cSMiquel Raynal 1859cfcc706cSMiquel Raynal if (ret < 0) 1860cfcc706cSMiquel Raynal return ret; 1861cfcc706cSMiquel Raynal 1862cfcc706cSMiquel Raynal if (ecc_fail) 1863cfcc706cSMiquel Raynal return -EBADMSG; 1864cfcc706cSMiquel Raynal 1865cfcc706cSMiquel Raynal return max_bitflips; 1866cfcc706cSMiquel Raynal } 1867cfcc706cSMiquel Raynal 1868cfcc706cSMiquel Raynal /** 1869cfcc706cSMiquel Raynal * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function 1870cfcc706cSMiquel Raynal * @mtd: mtd info structure 1871cfcc706cSMiquel Raynal * @chip: nand chip info structure 1872cfcc706cSMiquel Raynal * @page: page number to read 1873cfcc706cSMiquel Raynal */ 1874cfcc706cSMiquel Raynal static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, 1875cfcc706cSMiquel Raynal int page) 1876cfcc706cSMiquel Raynal { 1877cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); 1878cfcc706cSMiquel Raynal chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); 1879cfcc706cSMiquel Raynal return 0; 1880cfcc706cSMiquel Raynal } 1881cfcc706cSMiquel Raynal 1882cfcc706cSMiquel Raynal /** 1883cfcc706cSMiquel Raynal * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC 1884cfcc706cSMiquel Raynal * with syndromes 1885cfcc706cSMiquel Raynal * @mtd: mtd info structure 1886cfcc706cSMiquel Raynal * @chip: nand chip info structure 1887cfcc706cSMiquel Raynal * @page: page number to read 1888cfcc706cSMiquel Raynal */ 1889cfcc706cSMiquel Raynal static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, 1890cfcc706cSMiquel Raynal int page) 1891cfcc706cSMiquel Raynal { 1892cfcc706cSMiquel Raynal int length = mtd->oobsize; 1893cfcc706cSMiquel Raynal int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; 1894cfcc706cSMiquel Raynal int eccsize = chip->ecc.size; 1895cfcc706cSMiquel Raynal uint8_t *bufpoi = chip->oob_poi; 1896cfcc706cSMiquel Raynal int i, toread, sndrnd = 0, pos; 1897cfcc706cSMiquel Raynal 1898cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page); 1899cfcc706cSMiquel Raynal for (i = 0; i < chip->ecc.steps; i++) { 1900cfcc706cSMiquel Raynal if (sndrnd) { 1901cfcc706cSMiquel Raynal pos = eccsize + i * (eccsize + chunk); 1902cfcc706cSMiquel Raynal if (mtd->writesize > 512) 1903cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1); 1904cfcc706cSMiquel Raynal else 1905cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page); 1906cfcc706cSMiquel Raynal } else 1907cfcc706cSMiquel Raynal sndrnd = 1; 1908cfcc706cSMiquel Raynal toread = min_t(int, length, chunk); 1909cfcc706cSMiquel Raynal chip->read_buf(mtd, bufpoi, toread); 1910cfcc706cSMiquel Raynal bufpoi += toread; 1911cfcc706cSMiquel Raynal length -= toread; 1912cfcc706cSMiquel Raynal } 1913cfcc706cSMiquel Raynal if (length > 0) 1914cfcc706cSMiquel Raynal chip->read_buf(mtd, bufpoi, length); 1915cfcc706cSMiquel Raynal 1916cfcc706cSMiquel Raynal return 0; 1917cfcc706cSMiquel Raynal } 1918cfcc706cSMiquel Raynal 1919cfcc706cSMiquel Raynal /** 1920cfcc706cSMiquel Raynal * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function 1921cfcc706cSMiquel Raynal * @mtd: mtd info structure 1922cfcc706cSMiquel Raynal * @chip: nand chip info structure 1923cfcc706cSMiquel Raynal * @page: page number to write 1924cfcc706cSMiquel Raynal */ 1925cfcc706cSMiquel Raynal static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, 1926cfcc706cSMiquel Raynal int page) 1927cfcc706cSMiquel Raynal { 1928cfcc706cSMiquel Raynal int status = 0; 1929cfcc706cSMiquel Raynal const uint8_t *buf = chip->oob_poi; 1930cfcc706cSMiquel Raynal int length = mtd->oobsize; 1931cfcc706cSMiquel Raynal 1932cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page); 1933cfcc706cSMiquel Raynal chip->write_buf(mtd, buf, length); 1934cfcc706cSMiquel Raynal /* Send command to program the OOB data */ 1935cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); 1936cfcc706cSMiquel Raynal 1937cfcc706cSMiquel Raynal status = chip->waitfunc(mtd, chip); 1938cfcc706cSMiquel Raynal 1939cfcc706cSMiquel Raynal return status & NAND_STATUS_FAIL ? -EIO : 0; 1940cfcc706cSMiquel Raynal } 1941cfcc706cSMiquel Raynal 1942cfcc706cSMiquel Raynal /** 1943cfcc706cSMiquel Raynal * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC 1944cfcc706cSMiquel Raynal * with syndrome - only for large page flash 1945cfcc706cSMiquel Raynal * @mtd: mtd info structure 1946cfcc706cSMiquel Raynal * @chip: nand chip info structure 1947cfcc706cSMiquel Raynal * @page: page number to write 1948cfcc706cSMiquel Raynal */ 1949cfcc706cSMiquel Raynal static int nand_write_oob_syndrome(struct mtd_info *mtd, 1950cfcc706cSMiquel Raynal struct nand_chip *chip, int page) 1951cfcc706cSMiquel Raynal { 1952cfcc706cSMiquel Raynal int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; 1953cfcc706cSMiquel Raynal int eccsize = chip->ecc.size, length = mtd->oobsize; 1954cfcc706cSMiquel Raynal int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps; 1955cfcc706cSMiquel Raynal const uint8_t *bufpoi = chip->oob_poi; 1956cfcc706cSMiquel Raynal 1957cfcc706cSMiquel Raynal /* 1958cfcc706cSMiquel Raynal * data-ecc-data-ecc ... ecc-oob 1959cfcc706cSMiquel Raynal * or 1960cfcc706cSMiquel Raynal * data-pad-ecc-pad-data-pad .... ecc-pad-oob 1961cfcc706cSMiquel Raynal */ 1962cfcc706cSMiquel Raynal if (!chip->ecc.prepad && !chip->ecc.postpad) { 1963cfcc706cSMiquel Raynal pos = steps * (eccsize + chunk); 1964cfcc706cSMiquel Raynal steps = 0; 1965cfcc706cSMiquel Raynal } else 1966cfcc706cSMiquel Raynal pos = eccsize; 1967cfcc706cSMiquel Raynal 1968cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page); 1969cfcc706cSMiquel Raynal for (i = 0; i < steps; i++) { 1970cfcc706cSMiquel Raynal if (sndcmd) { 1971cfcc706cSMiquel Raynal if (mtd->writesize <= 512) { 1972cfcc706cSMiquel Raynal uint32_t fill = 0xFFFFFFFF; 1973cfcc706cSMiquel Raynal 1974cfcc706cSMiquel Raynal len = eccsize; 1975cfcc706cSMiquel Raynal while (len > 0) { 1976cfcc706cSMiquel Raynal int num = min_t(int, len, 4); 1977cfcc706cSMiquel Raynal chip->write_buf(mtd, (uint8_t *)&fill, 1978cfcc706cSMiquel Raynal num); 1979cfcc706cSMiquel Raynal len -= num; 1980cfcc706cSMiquel Raynal } 1981cfcc706cSMiquel Raynal } else { 1982cfcc706cSMiquel Raynal pos = eccsize + i * (eccsize + chunk); 1983cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1); 1984cfcc706cSMiquel Raynal } 1985cfcc706cSMiquel Raynal } else 1986cfcc706cSMiquel Raynal sndcmd = 1; 1987cfcc706cSMiquel Raynal len = min_t(int, length, chunk); 1988cfcc706cSMiquel Raynal chip->write_buf(mtd, bufpoi, len); 1989cfcc706cSMiquel Raynal bufpoi += len; 1990cfcc706cSMiquel Raynal length -= len; 1991cfcc706cSMiquel Raynal } 1992cfcc706cSMiquel Raynal if (length > 0) 1993cfcc706cSMiquel Raynal chip->write_buf(mtd, bufpoi, length); 1994cfcc706cSMiquel Raynal 1995cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); 1996cfcc706cSMiquel Raynal status = chip->waitfunc(mtd, chip); 1997cfcc706cSMiquel Raynal 1998cfcc706cSMiquel Raynal return status & NAND_STATUS_FAIL ? -EIO : 0; 1999cfcc706cSMiquel Raynal } 2000cfcc706cSMiquel Raynal 2001cfcc706cSMiquel Raynal /** 2002cfcc706cSMiquel Raynal * nand_do_read_oob - [INTERN] NAND read out-of-band 2003cfcc706cSMiquel Raynal * @mtd: MTD device structure 2004cfcc706cSMiquel Raynal * @from: offset to read from 2005cfcc706cSMiquel Raynal * @ops: oob operations description structure 2006cfcc706cSMiquel Raynal * 2007cfcc706cSMiquel Raynal * NAND read out-of-band data from the spare area. 2008cfcc706cSMiquel Raynal */ 2009cfcc706cSMiquel Raynal static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, 2010cfcc706cSMiquel Raynal struct mtd_oob_ops *ops) 2011cfcc706cSMiquel Raynal { 2012cfcc706cSMiquel Raynal int page, realpage, chipnr; 2013cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 2014cfcc706cSMiquel Raynal struct mtd_ecc_stats stats; 2015cfcc706cSMiquel Raynal int readlen = ops->ooblen; 2016cfcc706cSMiquel Raynal int len; 2017cfcc706cSMiquel Raynal uint8_t *buf = ops->oobbuf; 2018cfcc706cSMiquel Raynal int ret = 0; 2019cfcc706cSMiquel Raynal 2020cfcc706cSMiquel Raynal pr_debug("%s: from = 0x%08Lx, len = %i\n", 2021cfcc706cSMiquel Raynal __func__, (unsigned long long)from, readlen); 2022cfcc706cSMiquel Raynal 2023cfcc706cSMiquel Raynal stats = mtd->ecc_stats; 2024cfcc706cSMiquel Raynal 2025cfcc706cSMiquel Raynal len = mtd_oobavail(mtd, ops); 2026cfcc706cSMiquel Raynal 2027cfcc706cSMiquel Raynal if (unlikely(ops->ooboffs >= len)) { 2028cfcc706cSMiquel Raynal pr_debug("%s: attempt to start read outside oob\n", 2029cfcc706cSMiquel Raynal __func__); 2030cfcc706cSMiquel Raynal return -EINVAL; 2031cfcc706cSMiquel Raynal } 2032cfcc706cSMiquel Raynal 2033cfcc706cSMiquel Raynal /* Do not allow reads past end of device */ 2034cfcc706cSMiquel Raynal if (unlikely(from >= mtd->size || 2035cfcc706cSMiquel Raynal ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - 2036cfcc706cSMiquel Raynal (from >> chip->page_shift)) * len)) { 2037cfcc706cSMiquel Raynal pr_debug("%s: attempt to read beyond end of device\n", 2038cfcc706cSMiquel Raynal __func__); 2039cfcc706cSMiquel Raynal return -EINVAL; 2040cfcc706cSMiquel Raynal } 2041cfcc706cSMiquel Raynal 2042cfcc706cSMiquel Raynal chipnr = (int)(from >> chip->chip_shift); 2043cfcc706cSMiquel Raynal chip->select_chip(mtd, chipnr); 2044cfcc706cSMiquel Raynal 2045cfcc706cSMiquel Raynal /* Shift to get page */ 2046cfcc706cSMiquel Raynal realpage = (int)(from >> chip->page_shift); 2047cfcc706cSMiquel Raynal page = realpage & chip->pagemask; 2048cfcc706cSMiquel Raynal 2049cfcc706cSMiquel Raynal while (1) { 2050cfcc706cSMiquel Raynal WATCHDOG_RESET(); 2051cfcc706cSMiquel Raynal 2052cfcc706cSMiquel Raynal if (ops->mode == MTD_OPS_RAW) 2053cfcc706cSMiquel Raynal ret = chip->ecc.read_oob_raw(mtd, chip, page); 2054cfcc706cSMiquel Raynal else 2055cfcc706cSMiquel Raynal ret = chip->ecc.read_oob(mtd, chip, page); 2056cfcc706cSMiquel Raynal 2057cfcc706cSMiquel Raynal if (ret < 0) 2058cfcc706cSMiquel Raynal break; 2059cfcc706cSMiquel Raynal 2060cfcc706cSMiquel Raynal len = min(len, readlen); 2061cfcc706cSMiquel Raynal buf = nand_transfer_oob(chip, buf, ops, len); 2062cfcc706cSMiquel Raynal 2063cfcc706cSMiquel Raynal if (chip->options & NAND_NEED_READRDY) { 2064cfcc706cSMiquel Raynal /* Apply delay or wait for ready/busy pin */ 2065cfcc706cSMiquel Raynal if (!chip->dev_ready) 2066cfcc706cSMiquel Raynal udelay(chip->chip_delay); 2067cfcc706cSMiquel Raynal else 2068cfcc706cSMiquel Raynal nand_wait_ready(mtd); 2069cfcc706cSMiquel Raynal } 2070cfcc706cSMiquel Raynal 2071cfcc706cSMiquel Raynal readlen -= len; 2072cfcc706cSMiquel Raynal if (!readlen) 2073cfcc706cSMiquel Raynal break; 2074cfcc706cSMiquel Raynal 2075cfcc706cSMiquel Raynal /* Increment page address */ 2076cfcc706cSMiquel Raynal realpage++; 2077cfcc706cSMiquel Raynal 2078cfcc706cSMiquel Raynal page = realpage & chip->pagemask; 2079cfcc706cSMiquel Raynal /* Check, if we cross a chip boundary */ 2080cfcc706cSMiquel Raynal if (!page) { 2081cfcc706cSMiquel Raynal chipnr++; 2082cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 2083cfcc706cSMiquel Raynal chip->select_chip(mtd, chipnr); 2084cfcc706cSMiquel Raynal } 2085cfcc706cSMiquel Raynal } 2086cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 2087cfcc706cSMiquel Raynal 2088cfcc706cSMiquel Raynal ops->oobretlen = ops->ooblen - readlen; 2089cfcc706cSMiquel Raynal 2090cfcc706cSMiquel Raynal if (ret < 0) 2091cfcc706cSMiquel Raynal return ret; 2092cfcc706cSMiquel Raynal 2093cfcc706cSMiquel Raynal if (mtd->ecc_stats.failed - stats.failed) 2094cfcc706cSMiquel Raynal return -EBADMSG; 2095cfcc706cSMiquel Raynal 2096cfcc706cSMiquel Raynal return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; 2097cfcc706cSMiquel Raynal } 2098cfcc706cSMiquel Raynal 2099cfcc706cSMiquel Raynal /** 2100cfcc706cSMiquel Raynal * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band 2101cfcc706cSMiquel Raynal * @mtd: MTD device structure 2102cfcc706cSMiquel Raynal * @from: offset to read from 2103cfcc706cSMiquel Raynal * @ops: oob operation description structure 2104cfcc706cSMiquel Raynal * 2105cfcc706cSMiquel Raynal * NAND read data and/or out-of-band data. 2106cfcc706cSMiquel Raynal */ 2107cfcc706cSMiquel Raynal static int nand_read_oob(struct mtd_info *mtd, loff_t from, 2108cfcc706cSMiquel Raynal struct mtd_oob_ops *ops) 2109cfcc706cSMiquel Raynal { 2110cfcc706cSMiquel Raynal int ret = -ENOTSUPP; 2111cfcc706cSMiquel Raynal 2112cfcc706cSMiquel Raynal ops->retlen = 0; 2113cfcc706cSMiquel Raynal 2114cfcc706cSMiquel Raynal /* Do not allow reads past end of device */ 2115cfcc706cSMiquel Raynal if (ops->datbuf && (from + ops->len) > mtd->size) { 2116cfcc706cSMiquel Raynal pr_debug("%s: attempt to read beyond end of device\n", 2117cfcc706cSMiquel Raynal __func__); 2118cfcc706cSMiquel Raynal return -EINVAL; 2119cfcc706cSMiquel Raynal } 2120cfcc706cSMiquel Raynal 2121cfcc706cSMiquel Raynal nand_get_device(mtd, FL_READING); 2122cfcc706cSMiquel Raynal 2123cfcc706cSMiquel Raynal switch (ops->mode) { 2124cfcc706cSMiquel Raynal case MTD_OPS_PLACE_OOB: 2125cfcc706cSMiquel Raynal case MTD_OPS_AUTO_OOB: 2126cfcc706cSMiquel Raynal case MTD_OPS_RAW: 2127cfcc706cSMiquel Raynal break; 2128cfcc706cSMiquel Raynal 2129cfcc706cSMiquel Raynal default: 2130cfcc706cSMiquel Raynal goto out; 2131cfcc706cSMiquel Raynal } 2132cfcc706cSMiquel Raynal 2133cfcc706cSMiquel Raynal if (!ops->datbuf) 2134cfcc706cSMiquel Raynal ret = nand_do_read_oob(mtd, from, ops); 2135cfcc706cSMiquel Raynal else 2136cfcc706cSMiquel Raynal ret = nand_do_read_ops(mtd, from, ops); 2137cfcc706cSMiquel Raynal 2138cfcc706cSMiquel Raynal out: 2139cfcc706cSMiquel Raynal nand_release_device(mtd); 2140cfcc706cSMiquel Raynal return ret; 2141cfcc706cSMiquel Raynal } 2142cfcc706cSMiquel Raynal 2143cfcc706cSMiquel Raynal 2144cfcc706cSMiquel Raynal /** 2145cfcc706cSMiquel Raynal * nand_write_page_raw - [INTERN] raw page write function 2146cfcc706cSMiquel Raynal * @mtd: mtd info structure 2147cfcc706cSMiquel Raynal * @chip: nand chip info structure 2148cfcc706cSMiquel Raynal * @buf: data buffer 2149cfcc706cSMiquel Raynal * @oob_required: must write chip->oob_poi to OOB 2150cfcc706cSMiquel Raynal * @page: page number to write 2151cfcc706cSMiquel Raynal * 2152cfcc706cSMiquel Raynal * Not for syndrome calculating ECC controllers, which use a special oob layout. 2153cfcc706cSMiquel Raynal */ 2154cfcc706cSMiquel Raynal static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, 2155cfcc706cSMiquel Raynal const uint8_t *buf, int oob_required, int page) 2156cfcc706cSMiquel Raynal { 2157cfcc706cSMiquel Raynal chip->write_buf(mtd, buf, mtd->writesize); 2158cfcc706cSMiquel Raynal if (oob_required) 2159cfcc706cSMiquel Raynal chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); 2160cfcc706cSMiquel Raynal 2161cfcc706cSMiquel Raynal return 0; 2162cfcc706cSMiquel Raynal } 2163cfcc706cSMiquel Raynal 2164cfcc706cSMiquel Raynal /** 2165cfcc706cSMiquel Raynal * nand_write_page_raw_syndrome - [INTERN] raw page write function 2166cfcc706cSMiquel Raynal * @mtd: mtd info structure 2167cfcc706cSMiquel Raynal * @chip: nand chip info structure 2168cfcc706cSMiquel Raynal * @buf: data buffer 2169cfcc706cSMiquel Raynal * @oob_required: must write chip->oob_poi to OOB 2170cfcc706cSMiquel Raynal * @page: page number to write 2171cfcc706cSMiquel Raynal * 2172cfcc706cSMiquel Raynal * We need a special oob layout and handling even when ECC isn't checked. 2173cfcc706cSMiquel Raynal */ 2174cfcc706cSMiquel Raynal static int nand_write_page_raw_syndrome(struct mtd_info *mtd, 2175cfcc706cSMiquel Raynal struct nand_chip *chip, 2176cfcc706cSMiquel Raynal const uint8_t *buf, int oob_required, 2177cfcc706cSMiquel Raynal int page) 2178cfcc706cSMiquel Raynal { 2179cfcc706cSMiquel Raynal int eccsize = chip->ecc.size; 2180cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes; 2181cfcc706cSMiquel Raynal uint8_t *oob = chip->oob_poi; 2182cfcc706cSMiquel Raynal int steps, size; 2183cfcc706cSMiquel Raynal 2184cfcc706cSMiquel Raynal for (steps = chip->ecc.steps; steps > 0; steps--) { 2185cfcc706cSMiquel Raynal chip->write_buf(mtd, buf, eccsize); 2186cfcc706cSMiquel Raynal buf += eccsize; 2187cfcc706cSMiquel Raynal 2188cfcc706cSMiquel Raynal if (chip->ecc.prepad) { 2189cfcc706cSMiquel Raynal chip->write_buf(mtd, oob, chip->ecc.prepad); 2190cfcc706cSMiquel Raynal oob += chip->ecc.prepad; 2191cfcc706cSMiquel Raynal } 2192cfcc706cSMiquel Raynal 2193cfcc706cSMiquel Raynal chip->write_buf(mtd, oob, eccbytes); 2194cfcc706cSMiquel Raynal oob += eccbytes; 2195cfcc706cSMiquel Raynal 2196cfcc706cSMiquel Raynal if (chip->ecc.postpad) { 2197cfcc706cSMiquel Raynal chip->write_buf(mtd, oob, chip->ecc.postpad); 2198cfcc706cSMiquel Raynal oob += chip->ecc.postpad; 2199cfcc706cSMiquel Raynal } 2200cfcc706cSMiquel Raynal } 2201cfcc706cSMiquel Raynal 2202cfcc706cSMiquel Raynal size = mtd->oobsize - (oob - chip->oob_poi); 2203cfcc706cSMiquel Raynal if (size) 2204cfcc706cSMiquel Raynal chip->write_buf(mtd, oob, size); 2205cfcc706cSMiquel Raynal 2206cfcc706cSMiquel Raynal return 0; 2207cfcc706cSMiquel Raynal } 2208cfcc706cSMiquel Raynal /** 2209cfcc706cSMiquel Raynal * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function 2210cfcc706cSMiquel Raynal * @mtd: mtd info structure 2211cfcc706cSMiquel Raynal * @chip: nand chip info structure 2212cfcc706cSMiquel Raynal * @buf: data buffer 2213cfcc706cSMiquel Raynal * @oob_required: must write chip->oob_poi to OOB 2214cfcc706cSMiquel Raynal * @page: page number to write 2215cfcc706cSMiquel Raynal */ 2216cfcc706cSMiquel Raynal static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, 2217cfcc706cSMiquel Raynal const uint8_t *buf, int oob_required, 2218cfcc706cSMiquel Raynal int page) 2219cfcc706cSMiquel Raynal { 2220cfcc706cSMiquel Raynal int i, eccsize = chip->ecc.size; 2221cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes; 2222cfcc706cSMiquel Raynal int eccsteps = chip->ecc.steps; 2223cfcc706cSMiquel Raynal uint8_t *ecc_calc = chip->buffers->ecccalc; 2224cfcc706cSMiquel Raynal const uint8_t *p = buf; 2225cfcc706cSMiquel Raynal uint32_t *eccpos = chip->ecc.layout->eccpos; 2226cfcc706cSMiquel Raynal 2227cfcc706cSMiquel Raynal /* Software ECC calculation */ 2228cfcc706cSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) 2229cfcc706cSMiquel Raynal chip->ecc.calculate(mtd, p, &ecc_calc[i]); 2230cfcc706cSMiquel Raynal 2231cfcc706cSMiquel Raynal for (i = 0; i < chip->ecc.total; i++) 2232cfcc706cSMiquel Raynal chip->oob_poi[eccpos[i]] = ecc_calc[i]; 2233cfcc706cSMiquel Raynal 2234cfcc706cSMiquel Raynal return chip->ecc.write_page_raw(mtd, chip, buf, 1, page); 2235cfcc706cSMiquel Raynal } 2236cfcc706cSMiquel Raynal 2237cfcc706cSMiquel Raynal /** 2238cfcc706cSMiquel Raynal * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function 2239cfcc706cSMiquel Raynal * @mtd: mtd info structure 2240cfcc706cSMiquel Raynal * @chip: nand chip info structure 2241cfcc706cSMiquel Raynal * @buf: data buffer 2242cfcc706cSMiquel Raynal * @oob_required: must write chip->oob_poi to OOB 2243cfcc706cSMiquel Raynal * @page: page number to write 2244cfcc706cSMiquel Raynal */ 2245cfcc706cSMiquel Raynal static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, 2246cfcc706cSMiquel Raynal const uint8_t *buf, int oob_required, 2247cfcc706cSMiquel Raynal int page) 2248cfcc706cSMiquel Raynal { 2249cfcc706cSMiquel Raynal int i, eccsize = chip->ecc.size; 2250cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes; 2251cfcc706cSMiquel Raynal int eccsteps = chip->ecc.steps; 2252cfcc706cSMiquel Raynal uint8_t *ecc_calc = chip->buffers->ecccalc; 2253cfcc706cSMiquel Raynal const uint8_t *p = buf; 2254cfcc706cSMiquel Raynal uint32_t *eccpos = chip->ecc.layout->eccpos; 2255cfcc706cSMiquel Raynal 2256cfcc706cSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { 2257cfcc706cSMiquel Raynal chip->ecc.hwctl(mtd, NAND_ECC_WRITE); 2258cfcc706cSMiquel Raynal chip->write_buf(mtd, p, eccsize); 2259cfcc706cSMiquel Raynal chip->ecc.calculate(mtd, p, &ecc_calc[i]); 2260cfcc706cSMiquel Raynal } 2261cfcc706cSMiquel Raynal 2262cfcc706cSMiquel Raynal for (i = 0; i < chip->ecc.total; i++) 2263cfcc706cSMiquel Raynal chip->oob_poi[eccpos[i]] = ecc_calc[i]; 2264cfcc706cSMiquel Raynal 2265cfcc706cSMiquel Raynal chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); 2266cfcc706cSMiquel Raynal 2267cfcc706cSMiquel Raynal return 0; 2268cfcc706cSMiquel Raynal } 2269cfcc706cSMiquel Raynal 2270cfcc706cSMiquel Raynal 2271cfcc706cSMiquel Raynal /** 2272cfcc706cSMiquel Raynal * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write 2273cfcc706cSMiquel Raynal * @mtd: mtd info structure 2274cfcc706cSMiquel Raynal * @chip: nand chip info structure 2275cfcc706cSMiquel Raynal * @offset: column address of subpage within the page 2276cfcc706cSMiquel Raynal * @data_len: data length 2277cfcc706cSMiquel Raynal * @buf: data buffer 2278cfcc706cSMiquel Raynal * @oob_required: must write chip->oob_poi to OOB 2279cfcc706cSMiquel Raynal * @page: page number to write 2280cfcc706cSMiquel Raynal */ 2281cfcc706cSMiquel Raynal static int nand_write_subpage_hwecc(struct mtd_info *mtd, 2282cfcc706cSMiquel Raynal struct nand_chip *chip, uint32_t offset, 2283cfcc706cSMiquel Raynal uint32_t data_len, const uint8_t *buf, 2284cfcc706cSMiquel Raynal int oob_required, int page) 2285cfcc706cSMiquel Raynal { 2286cfcc706cSMiquel Raynal uint8_t *oob_buf = chip->oob_poi; 2287cfcc706cSMiquel Raynal uint8_t *ecc_calc = chip->buffers->ecccalc; 2288cfcc706cSMiquel Raynal int ecc_size = chip->ecc.size; 2289cfcc706cSMiquel Raynal int ecc_bytes = chip->ecc.bytes; 2290cfcc706cSMiquel Raynal int ecc_steps = chip->ecc.steps; 2291cfcc706cSMiquel Raynal uint32_t *eccpos = chip->ecc.layout->eccpos; 2292cfcc706cSMiquel Raynal uint32_t start_step = offset / ecc_size; 2293cfcc706cSMiquel Raynal uint32_t end_step = (offset + data_len - 1) / ecc_size; 2294cfcc706cSMiquel Raynal int oob_bytes = mtd->oobsize / ecc_steps; 2295cfcc706cSMiquel Raynal int step, i; 2296cfcc706cSMiquel Raynal 2297cfcc706cSMiquel Raynal for (step = 0; step < ecc_steps; step++) { 2298cfcc706cSMiquel Raynal /* configure controller for WRITE access */ 2299cfcc706cSMiquel Raynal chip->ecc.hwctl(mtd, NAND_ECC_WRITE); 2300cfcc706cSMiquel Raynal 2301cfcc706cSMiquel Raynal /* write data (untouched subpages already masked by 0xFF) */ 2302cfcc706cSMiquel Raynal chip->write_buf(mtd, buf, ecc_size); 2303cfcc706cSMiquel Raynal 2304cfcc706cSMiquel Raynal /* mask ECC of un-touched subpages by padding 0xFF */ 2305cfcc706cSMiquel Raynal if ((step < start_step) || (step > end_step)) 2306cfcc706cSMiquel Raynal memset(ecc_calc, 0xff, ecc_bytes); 2307cfcc706cSMiquel Raynal else 2308cfcc706cSMiquel Raynal chip->ecc.calculate(mtd, buf, ecc_calc); 2309cfcc706cSMiquel Raynal 2310cfcc706cSMiquel Raynal /* mask OOB of un-touched subpages by padding 0xFF */ 2311cfcc706cSMiquel Raynal /* if oob_required, preserve OOB metadata of written subpage */ 2312cfcc706cSMiquel Raynal if (!oob_required || (step < start_step) || (step > end_step)) 2313cfcc706cSMiquel Raynal memset(oob_buf, 0xff, oob_bytes); 2314cfcc706cSMiquel Raynal 2315cfcc706cSMiquel Raynal buf += ecc_size; 2316cfcc706cSMiquel Raynal ecc_calc += ecc_bytes; 2317cfcc706cSMiquel Raynal oob_buf += oob_bytes; 2318cfcc706cSMiquel Raynal } 2319cfcc706cSMiquel Raynal 2320cfcc706cSMiquel Raynal /* copy calculated ECC for whole page to chip->buffer->oob */ 2321cfcc706cSMiquel Raynal /* this include masked-value(0xFF) for unwritten subpages */ 2322cfcc706cSMiquel Raynal ecc_calc = chip->buffers->ecccalc; 2323cfcc706cSMiquel Raynal for (i = 0; i < chip->ecc.total; i++) 2324cfcc706cSMiquel Raynal chip->oob_poi[eccpos[i]] = ecc_calc[i]; 2325cfcc706cSMiquel Raynal 2326cfcc706cSMiquel Raynal /* write OOB buffer to NAND device */ 2327cfcc706cSMiquel Raynal chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); 2328cfcc706cSMiquel Raynal 2329cfcc706cSMiquel Raynal return 0; 2330cfcc706cSMiquel Raynal } 2331cfcc706cSMiquel Raynal 2332cfcc706cSMiquel Raynal 2333cfcc706cSMiquel Raynal /** 2334cfcc706cSMiquel Raynal * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write 2335cfcc706cSMiquel Raynal * @mtd: mtd info structure 2336cfcc706cSMiquel Raynal * @chip: nand chip info structure 2337cfcc706cSMiquel Raynal * @buf: data buffer 2338cfcc706cSMiquel Raynal * @oob_required: must write chip->oob_poi to OOB 2339cfcc706cSMiquel Raynal * @page: page number to write 2340cfcc706cSMiquel Raynal * 2341cfcc706cSMiquel Raynal * The hw generator calculates the error syndrome automatically. Therefore we 2342cfcc706cSMiquel Raynal * need a special oob layout and handling. 2343cfcc706cSMiquel Raynal */ 2344cfcc706cSMiquel Raynal static int nand_write_page_syndrome(struct mtd_info *mtd, 2345cfcc706cSMiquel Raynal struct nand_chip *chip, 2346cfcc706cSMiquel Raynal const uint8_t *buf, int oob_required, 2347cfcc706cSMiquel Raynal int page) 2348cfcc706cSMiquel Raynal { 2349cfcc706cSMiquel Raynal int i, eccsize = chip->ecc.size; 2350cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes; 2351cfcc706cSMiquel Raynal int eccsteps = chip->ecc.steps; 2352cfcc706cSMiquel Raynal const uint8_t *p = buf; 2353cfcc706cSMiquel Raynal uint8_t *oob = chip->oob_poi; 2354cfcc706cSMiquel Raynal 2355cfcc706cSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { 2356cfcc706cSMiquel Raynal 2357cfcc706cSMiquel Raynal chip->ecc.hwctl(mtd, NAND_ECC_WRITE); 2358cfcc706cSMiquel Raynal chip->write_buf(mtd, p, eccsize); 2359cfcc706cSMiquel Raynal 2360cfcc706cSMiquel Raynal if (chip->ecc.prepad) { 2361cfcc706cSMiquel Raynal chip->write_buf(mtd, oob, chip->ecc.prepad); 2362cfcc706cSMiquel Raynal oob += chip->ecc.prepad; 2363cfcc706cSMiquel Raynal } 2364cfcc706cSMiquel Raynal 2365cfcc706cSMiquel Raynal chip->ecc.calculate(mtd, p, oob); 2366cfcc706cSMiquel Raynal chip->write_buf(mtd, oob, eccbytes); 2367cfcc706cSMiquel Raynal oob += eccbytes; 2368cfcc706cSMiquel Raynal 2369cfcc706cSMiquel Raynal if (chip->ecc.postpad) { 2370cfcc706cSMiquel Raynal chip->write_buf(mtd, oob, chip->ecc.postpad); 2371cfcc706cSMiquel Raynal oob += chip->ecc.postpad; 2372cfcc706cSMiquel Raynal } 2373cfcc706cSMiquel Raynal } 2374cfcc706cSMiquel Raynal 2375cfcc706cSMiquel Raynal /* Calculate remaining oob bytes */ 2376cfcc706cSMiquel Raynal i = mtd->oobsize - (oob - chip->oob_poi); 2377cfcc706cSMiquel Raynal if (i) 2378cfcc706cSMiquel Raynal chip->write_buf(mtd, oob, i); 2379cfcc706cSMiquel Raynal 2380cfcc706cSMiquel Raynal return 0; 2381cfcc706cSMiquel Raynal } 2382cfcc706cSMiquel Raynal 2383cfcc706cSMiquel Raynal /** 2384cfcc706cSMiquel Raynal * nand_write_page - [REPLACEABLE] write one page 2385cfcc706cSMiquel Raynal * @mtd: MTD device structure 2386cfcc706cSMiquel Raynal * @chip: NAND chip descriptor 2387cfcc706cSMiquel Raynal * @offset: address offset within the page 2388cfcc706cSMiquel Raynal * @data_len: length of actual data to be written 2389cfcc706cSMiquel Raynal * @buf: the data to write 2390cfcc706cSMiquel Raynal * @oob_required: must write chip->oob_poi to OOB 2391cfcc706cSMiquel Raynal * @page: page number to write 2392cfcc706cSMiquel Raynal * @raw: use _raw version of write_page 2393cfcc706cSMiquel Raynal */ 2394cfcc706cSMiquel Raynal static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, 2395cfcc706cSMiquel Raynal uint32_t offset, int data_len, const uint8_t *buf, 2396cfcc706cSMiquel Raynal int oob_required, int page, int raw) 2397cfcc706cSMiquel Raynal { 2398cfcc706cSMiquel Raynal int status, subpage; 2399cfcc706cSMiquel Raynal 2400cfcc706cSMiquel Raynal if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && 2401cfcc706cSMiquel Raynal chip->ecc.write_subpage) 2402cfcc706cSMiquel Raynal subpage = offset || (data_len < mtd->writesize); 2403cfcc706cSMiquel Raynal else 2404cfcc706cSMiquel Raynal subpage = 0; 2405cfcc706cSMiquel Raynal 2406cfcc706cSMiquel Raynal if (nand_standard_page_accessors(&chip->ecc)) 2407cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); 2408cfcc706cSMiquel Raynal 2409cfcc706cSMiquel Raynal if (unlikely(raw)) 2410cfcc706cSMiquel Raynal status = chip->ecc.write_page_raw(mtd, chip, buf, 2411cfcc706cSMiquel Raynal oob_required, page); 2412cfcc706cSMiquel Raynal else if (subpage) 2413cfcc706cSMiquel Raynal status = chip->ecc.write_subpage(mtd, chip, offset, data_len, 2414cfcc706cSMiquel Raynal buf, oob_required, page); 2415cfcc706cSMiquel Raynal else 2416cfcc706cSMiquel Raynal status = chip->ecc.write_page(mtd, chip, buf, oob_required, 2417cfcc706cSMiquel Raynal page); 2418cfcc706cSMiquel Raynal 2419cfcc706cSMiquel Raynal if (status < 0) 2420cfcc706cSMiquel Raynal return status; 2421cfcc706cSMiquel Raynal 2422cfcc706cSMiquel Raynal if (nand_standard_page_accessors(&chip->ecc)) { 2423cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); 2424cfcc706cSMiquel Raynal 2425cfcc706cSMiquel Raynal status = chip->waitfunc(mtd, chip); 2426cfcc706cSMiquel Raynal if (status & NAND_STATUS_FAIL) 2427cfcc706cSMiquel Raynal return -EIO; 2428cfcc706cSMiquel Raynal } 2429cfcc706cSMiquel Raynal 2430cfcc706cSMiquel Raynal return 0; 2431cfcc706cSMiquel Raynal } 2432cfcc706cSMiquel Raynal 2433cfcc706cSMiquel Raynal /** 2434cfcc706cSMiquel Raynal * nand_fill_oob - [INTERN] Transfer client buffer to oob 2435cfcc706cSMiquel Raynal * @mtd: MTD device structure 2436cfcc706cSMiquel Raynal * @oob: oob data buffer 2437cfcc706cSMiquel Raynal * @len: oob data write length 2438cfcc706cSMiquel Raynal * @ops: oob ops structure 2439cfcc706cSMiquel Raynal */ 2440cfcc706cSMiquel Raynal static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, 2441cfcc706cSMiquel Raynal struct mtd_oob_ops *ops) 2442cfcc706cSMiquel Raynal { 2443cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 2444cfcc706cSMiquel Raynal 2445cfcc706cSMiquel Raynal /* 2446cfcc706cSMiquel Raynal * Initialise to all 0xFF, to avoid the possibility of left over OOB 2447cfcc706cSMiquel Raynal * data from a previous OOB read. 2448cfcc706cSMiquel Raynal */ 2449cfcc706cSMiquel Raynal memset(chip->oob_poi, 0xff, mtd->oobsize); 2450cfcc706cSMiquel Raynal 2451cfcc706cSMiquel Raynal switch (ops->mode) { 2452cfcc706cSMiquel Raynal 2453cfcc706cSMiquel Raynal case MTD_OPS_PLACE_OOB: 2454cfcc706cSMiquel Raynal case MTD_OPS_RAW: 2455cfcc706cSMiquel Raynal memcpy(chip->oob_poi + ops->ooboffs, oob, len); 2456cfcc706cSMiquel Raynal return oob + len; 2457cfcc706cSMiquel Raynal 2458cfcc706cSMiquel Raynal case MTD_OPS_AUTO_OOB: { 2459cfcc706cSMiquel Raynal struct nand_oobfree *free = chip->ecc.layout->oobfree; 2460cfcc706cSMiquel Raynal uint32_t boffs = 0, woffs = ops->ooboffs; 2461cfcc706cSMiquel Raynal size_t bytes = 0; 2462cfcc706cSMiquel Raynal 2463cfcc706cSMiquel Raynal for (; free->length && len; free++, len -= bytes) { 2464cfcc706cSMiquel Raynal /* Write request not from offset 0? */ 2465cfcc706cSMiquel Raynal if (unlikely(woffs)) { 2466cfcc706cSMiquel Raynal if (woffs >= free->length) { 2467cfcc706cSMiquel Raynal woffs -= free->length; 2468cfcc706cSMiquel Raynal continue; 2469cfcc706cSMiquel Raynal } 2470cfcc706cSMiquel Raynal boffs = free->offset + woffs; 2471cfcc706cSMiquel Raynal bytes = min_t(size_t, len, 2472cfcc706cSMiquel Raynal (free->length - woffs)); 2473cfcc706cSMiquel Raynal woffs = 0; 2474cfcc706cSMiquel Raynal } else { 2475cfcc706cSMiquel Raynal bytes = min_t(size_t, len, free->length); 2476cfcc706cSMiquel Raynal boffs = free->offset; 2477cfcc706cSMiquel Raynal } 2478cfcc706cSMiquel Raynal memcpy(chip->oob_poi + boffs, oob, bytes); 2479cfcc706cSMiquel Raynal oob += bytes; 2480cfcc706cSMiquel Raynal } 2481cfcc706cSMiquel Raynal return oob; 2482cfcc706cSMiquel Raynal } 2483cfcc706cSMiquel Raynal default: 2484cfcc706cSMiquel Raynal BUG(); 2485cfcc706cSMiquel Raynal } 2486cfcc706cSMiquel Raynal return NULL; 2487cfcc706cSMiquel Raynal } 2488cfcc706cSMiquel Raynal 2489cfcc706cSMiquel Raynal #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) 2490cfcc706cSMiquel Raynal 2491cfcc706cSMiquel Raynal /** 2492cfcc706cSMiquel Raynal * nand_do_write_ops - [INTERN] NAND write with ECC 2493cfcc706cSMiquel Raynal * @mtd: MTD device structure 2494cfcc706cSMiquel Raynal * @to: offset to write to 2495cfcc706cSMiquel Raynal * @ops: oob operations description structure 2496cfcc706cSMiquel Raynal * 2497cfcc706cSMiquel Raynal * NAND write with ECC. 2498cfcc706cSMiquel Raynal */ 2499cfcc706cSMiquel Raynal static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, 2500cfcc706cSMiquel Raynal struct mtd_oob_ops *ops) 2501cfcc706cSMiquel Raynal { 2502cfcc706cSMiquel Raynal int chipnr, realpage, page, column; 2503cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 2504cfcc706cSMiquel Raynal uint32_t writelen = ops->len; 2505cfcc706cSMiquel Raynal 2506cfcc706cSMiquel Raynal uint32_t oobwritelen = ops->ooblen; 2507cfcc706cSMiquel Raynal uint32_t oobmaxlen = mtd_oobavail(mtd, ops); 2508cfcc706cSMiquel Raynal 2509cfcc706cSMiquel Raynal uint8_t *oob = ops->oobbuf; 2510cfcc706cSMiquel Raynal uint8_t *buf = ops->datbuf; 2511cfcc706cSMiquel Raynal int ret; 2512cfcc706cSMiquel Raynal int oob_required = oob ? 1 : 0; 2513cfcc706cSMiquel Raynal 2514cfcc706cSMiquel Raynal ops->retlen = 0; 2515cfcc706cSMiquel Raynal if (!writelen) 2516cfcc706cSMiquel Raynal return 0; 2517cfcc706cSMiquel Raynal 2518cfcc706cSMiquel Raynal /* Reject writes, which are not page aligned */ 2519cfcc706cSMiquel Raynal if (NOTALIGNED(to)) { 2520cfcc706cSMiquel Raynal pr_notice("%s: attempt to write non page aligned data\n", 2521cfcc706cSMiquel Raynal __func__); 2522cfcc706cSMiquel Raynal return -EINVAL; 2523cfcc706cSMiquel Raynal } 2524cfcc706cSMiquel Raynal 2525cfcc706cSMiquel Raynal column = to & (mtd->writesize - 1); 2526cfcc706cSMiquel Raynal 2527cfcc706cSMiquel Raynal chipnr = (int)(to >> chip->chip_shift); 2528cfcc706cSMiquel Raynal chip->select_chip(mtd, chipnr); 2529cfcc706cSMiquel Raynal 2530cfcc706cSMiquel Raynal /* Check, if it is write protected */ 2531cfcc706cSMiquel Raynal if (nand_check_wp(mtd)) { 2532cfcc706cSMiquel Raynal ret = -EIO; 2533cfcc706cSMiquel Raynal goto err_out; 2534cfcc706cSMiquel Raynal } 2535cfcc706cSMiquel Raynal 2536cfcc706cSMiquel Raynal realpage = (int)(to >> chip->page_shift); 2537cfcc706cSMiquel Raynal page = realpage & chip->pagemask; 2538cfcc706cSMiquel Raynal 2539cfcc706cSMiquel Raynal /* Invalidate the page cache, when we write to the cached page */ 2540cfcc706cSMiquel Raynal if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && 2541cfcc706cSMiquel Raynal ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) 2542cfcc706cSMiquel Raynal chip->pagebuf = -1; 2543cfcc706cSMiquel Raynal 2544cfcc706cSMiquel Raynal /* Don't allow multipage oob writes with offset */ 2545cfcc706cSMiquel Raynal if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { 2546cfcc706cSMiquel Raynal ret = -EINVAL; 2547cfcc706cSMiquel Raynal goto err_out; 2548cfcc706cSMiquel Raynal } 2549cfcc706cSMiquel Raynal 2550cfcc706cSMiquel Raynal while (1) { 2551cfcc706cSMiquel Raynal int bytes = mtd->writesize; 2552cfcc706cSMiquel Raynal uint8_t *wbuf = buf; 2553cfcc706cSMiquel Raynal int use_bufpoi; 2554cfcc706cSMiquel Raynal int part_pagewr = (column || writelen < mtd->writesize); 2555cfcc706cSMiquel Raynal 2556cfcc706cSMiquel Raynal if (part_pagewr) 2557cfcc706cSMiquel Raynal use_bufpoi = 1; 2558cfcc706cSMiquel Raynal else if (chip->options & NAND_USE_BOUNCE_BUFFER) 2559cfcc706cSMiquel Raynal use_bufpoi = !IS_ALIGNED((unsigned long)buf, 2560cfcc706cSMiquel Raynal chip->buf_align); 2561cfcc706cSMiquel Raynal else 2562cfcc706cSMiquel Raynal use_bufpoi = 0; 2563cfcc706cSMiquel Raynal 2564cfcc706cSMiquel Raynal WATCHDOG_RESET(); 2565cfcc706cSMiquel Raynal /* Partial page write?, or need to use bounce buffer */ 2566cfcc706cSMiquel Raynal if (use_bufpoi) { 2567cfcc706cSMiquel Raynal pr_debug("%s: using write bounce buffer for buf@%p\n", 2568cfcc706cSMiquel Raynal __func__, buf); 2569cfcc706cSMiquel Raynal if (part_pagewr) 2570cfcc706cSMiquel Raynal bytes = min_t(int, bytes - column, writelen); 2571cfcc706cSMiquel Raynal chip->pagebuf = -1; 2572cfcc706cSMiquel Raynal memset(chip->buffers->databuf, 0xff, mtd->writesize); 2573cfcc706cSMiquel Raynal memcpy(&chip->buffers->databuf[column], buf, bytes); 2574cfcc706cSMiquel Raynal wbuf = chip->buffers->databuf; 2575cfcc706cSMiquel Raynal } 2576cfcc706cSMiquel Raynal 2577cfcc706cSMiquel Raynal if (unlikely(oob)) { 2578cfcc706cSMiquel Raynal size_t len = min(oobwritelen, oobmaxlen); 2579cfcc706cSMiquel Raynal oob = nand_fill_oob(mtd, oob, len, ops); 2580cfcc706cSMiquel Raynal oobwritelen -= len; 2581cfcc706cSMiquel Raynal } else { 2582cfcc706cSMiquel Raynal /* We still need to erase leftover OOB data */ 2583cfcc706cSMiquel Raynal memset(chip->oob_poi, 0xff, mtd->oobsize); 2584cfcc706cSMiquel Raynal } 2585cfcc706cSMiquel Raynal ret = chip->write_page(mtd, chip, column, bytes, wbuf, 2586cfcc706cSMiquel Raynal oob_required, page, 2587cfcc706cSMiquel Raynal (ops->mode == MTD_OPS_RAW)); 2588cfcc706cSMiquel Raynal if (ret) 2589cfcc706cSMiquel Raynal break; 2590cfcc706cSMiquel Raynal 2591cfcc706cSMiquel Raynal writelen -= bytes; 2592cfcc706cSMiquel Raynal if (!writelen) 2593cfcc706cSMiquel Raynal break; 2594cfcc706cSMiquel Raynal 2595cfcc706cSMiquel Raynal column = 0; 2596cfcc706cSMiquel Raynal buf += bytes; 2597cfcc706cSMiquel Raynal realpage++; 2598cfcc706cSMiquel Raynal 2599cfcc706cSMiquel Raynal page = realpage & chip->pagemask; 2600cfcc706cSMiquel Raynal /* Check, if we cross a chip boundary */ 2601cfcc706cSMiquel Raynal if (!page) { 2602cfcc706cSMiquel Raynal chipnr++; 2603cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 2604cfcc706cSMiquel Raynal chip->select_chip(mtd, chipnr); 2605cfcc706cSMiquel Raynal } 2606cfcc706cSMiquel Raynal } 2607cfcc706cSMiquel Raynal 2608cfcc706cSMiquel Raynal ops->retlen = ops->len - writelen; 2609cfcc706cSMiquel Raynal if (unlikely(oob)) 2610cfcc706cSMiquel Raynal ops->oobretlen = ops->ooblen; 2611cfcc706cSMiquel Raynal 2612cfcc706cSMiquel Raynal err_out: 2613cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 2614cfcc706cSMiquel Raynal return ret; 2615cfcc706cSMiquel Raynal } 2616cfcc706cSMiquel Raynal 2617cfcc706cSMiquel Raynal /** 2618cfcc706cSMiquel Raynal * panic_nand_write - [MTD Interface] NAND write with ECC 2619cfcc706cSMiquel Raynal * @mtd: MTD device structure 2620cfcc706cSMiquel Raynal * @to: offset to write to 2621cfcc706cSMiquel Raynal * @len: number of bytes to write 2622cfcc706cSMiquel Raynal * @retlen: pointer to variable to store the number of written bytes 2623cfcc706cSMiquel Raynal * @buf: the data to write 2624cfcc706cSMiquel Raynal * 2625cfcc706cSMiquel Raynal * NAND write with ECC. Used when performing writes in interrupt context, this 2626cfcc706cSMiquel Raynal * may for example be called by mtdoops when writing an oops while in panic. 2627cfcc706cSMiquel Raynal */ 2628cfcc706cSMiquel Raynal static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, 2629cfcc706cSMiquel Raynal size_t *retlen, const uint8_t *buf) 2630cfcc706cSMiquel Raynal { 2631cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 2632cfcc706cSMiquel Raynal struct mtd_oob_ops ops; 2633cfcc706cSMiquel Raynal int ret; 2634cfcc706cSMiquel Raynal 2635cfcc706cSMiquel Raynal /* Wait for the device to get ready */ 2636cfcc706cSMiquel Raynal panic_nand_wait(mtd, chip, 400); 2637cfcc706cSMiquel Raynal 2638cfcc706cSMiquel Raynal /* Grab the device */ 2639cfcc706cSMiquel Raynal panic_nand_get_device(chip, mtd, FL_WRITING); 2640cfcc706cSMiquel Raynal 2641cfcc706cSMiquel Raynal memset(&ops, 0, sizeof(ops)); 2642cfcc706cSMiquel Raynal ops.len = len; 2643cfcc706cSMiquel Raynal ops.datbuf = (uint8_t *)buf; 2644cfcc706cSMiquel Raynal ops.mode = MTD_OPS_PLACE_OOB; 2645cfcc706cSMiquel Raynal 2646cfcc706cSMiquel Raynal ret = nand_do_write_ops(mtd, to, &ops); 2647cfcc706cSMiquel Raynal 2648cfcc706cSMiquel Raynal *retlen = ops.retlen; 2649cfcc706cSMiquel Raynal return ret; 2650cfcc706cSMiquel Raynal } 2651cfcc706cSMiquel Raynal 2652cfcc706cSMiquel Raynal /** 2653cfcc706cSMiquel Raynal * nand_do_write_oob - [MTD Interface] NAND write out-of-band 2654cfcc706cSMiquel Raynal * @mtd: MTD device structure 2655cfcc706cSMiquel Raynal * @to: offset to write to 2656cfcc706cSMiquel Raynal * @ops: oob operation description structure 2657cfcc706cSMiquel Raynal * 2658cfcc706cSMiquel Raynal * NAND write out-of-band. 2659cfcc706cSMiquel Raynal */ 2660cfcc706cSMiquel Raynal static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, 2661cfcc706cSMiquel Raynal struct mtd_oob_ops *ops) 2662cfcc706cSMiquel Raynal { 2663cfcc706cSMiquel Raynal int chipnr, page, status, len; 2664cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 2665cfcc706cSMiquel Raynal 2666cfcc706cSMiquel Raynal pr_debug("%s: to = 0x%08x, len = %i\n", 2667cfcc706cSMiquel Raynal __func__, (unsigned int)to, (int)ops->ooblen); 2668cfcc706cSMiquel Raynal 2669cfcc706cSMiquel Raynal len = mtd_oobavail(mtd, ops); 2670cfcc706cSMiquel Raynal 2671cfcc706cSMiquel Raynal /* Do not allow write past end of page */ 2672cfcc706cSMiquel Raynal if ((ops->ooboffs + ops->ooblen) > len) { 2673cfcc706cSMiquel Raynal pr_debug("%s: attempt to write past end of page\n", 2674cfcc706cSMiquel Raynal __func__); 2675cfcc706cSMiquel Raynal return -EINVAL; 2676cfcc706cSMiquel Raynal } 2677cfcc706cSMiquel Raynal 2678cfcc706cSMiquel Raynal if (unlikely(ops->ooboffs >= len)) { 2679cfcc706cSMiquel Raynal pr_debug("%s: attempt to start write outside oob\n", 2680cfcc706cSMiquel Raynal __func__); 2681cfcc706cSMiquel Raynal return -EINVAL; 2682cfcc706cSMiquel Raynal } 2683cfcc706cSMiquel Raynal 2684cfcc706cSMiquel Raynal /* Do not allow write past end of device */ 2685cfcc706cSMiquel Raynal if (unlikely(to >= mtd->size || 2686cfcc706cSMiquel Raynal ops->ooboffs + ops->ooblen > 2687cfcc706cSMiquel Raynal ((mtd->size >> chip->page_shift) - 2688cfcc706cSMiquel Raynal (to >> chip->page_shift)) * len)) { 2689cfcc706cSMiquel Raynal pr_debug("%s: attempt to write beyond end of device\n", 2690cfcc706cSMiquel Raynal __func__); 2691cfcc706cSMiquel Raynal return -EINVAL; 2692cfcc706cSMiquel Raynal } 2693cfcc706cSMiquel Raynal 2694cfcc706cSMiquel Raynal chipnr = (int)(to >> chip->chip_shift); 2695cfcc706cSMiquel Raynal 2696cfcc706cSMiquel Raynal /* 2697cfcc706cSMiquel Raynal * Reset the chip. Some chips (like the Toshiba TC5832DC found in one 2698cfcc706cSMiquel Raynal * of my DiskOnChip 2000 test units) will clear the whole data page too 2699cfcc706cSMiquel Raynal * if we don't do this. I have no clue why, but I seem to have 'fixed' 2700cfcc706cSMiquel Raynal * it in the doc2000 driver in August 1999. dwmw2. 2701cfcc706cSMiquel Raynal */ 2702cfcc706cSMiquel Raynal nand_reset(chip, chipnr); 2703cfcc706cSMiquel Raynal 2704cfcc706cSMiquel Raynal chip->select_chip(mtd, chipnr); 2705cfcc706cSMiquel Raynal 2706cfcc706cSMiquel Raynal /* Shift to get page */ 2707cfcc706cSMiquel Raynal page = (int)(to >> chip->page_shift); 2708cfcc706cSMiquel Raynal 2709cfcc706cSMiquel Raynal /* Check, if it is write protected */ 2710cfcc706cSMiquel Raynal if (nand_check_wp(mtd)) { 2711cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 2712cfcc706cSMiquel Raynal return -EROFS; 2713cfcc706cSMiquel Raynal } 2714cfcc706cSMiquel Raynal 2715cfcc706cSMiquel Raynal /* Invalidate the page cache, if we write to the cached page */ 2716cfcc706cSMiquel Raynal if (page == chip->pagebuf) 2717cfcc706cSMiquel Raynal chip->pagebuf = -1; 2718cfcc706cSMiquel Raynal 2719cfcc706cSMiquel Raynal nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); 2720cfcc706cSMiquel Raynal 2721cfcc706cSMiquel Raynal if (ops->mode == MTD_OPS_RAW) 2722cfcc706cSMiquel Raynal status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask); 2723cfcc706cSMiquel Raynal else 2724cfcc706cSMiquel Raynal status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); 2725cfcc706cSMiquel Raynal 2726cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 2727cfcc706cSMiquel Raynal 2728cfcc706cSMiquel Raynal if (status) 2729cfcc706cSMiquel Raynal return status; 2730cfcc706cSMiquel Raynal 2731cfcc706cSMiquel Raynal ops->oobretlen = ops->ooblen; 2732cfcc706cSMiquel Raynal 2733cfcc706cSMiquel Raynal return 0; 2734cfcc706cSMiquel Raynal } 2735cfcc706cSMiquel Raynal 2736cfcc706cSMiquel Raynal /** 2737cfcc706cSMiquel Raynal * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band 2738cfcc706cSMiquel Raynal * @mtd: MTD device structure 2739cfcc706cSMiquel Raynal * @to: offset to write to 2740cfcc706cSMiquel Raynal * @ops: oob operation description structure 2741cfcc706cSMiquel Raynal */ 2742cfcc706cSMiquel Raynal static int nand_write_oob(struct mtd_info *mtd, loff_t to, 2743cfcc706cSMiquel Raynal struct mtd_oob_ops *ops) 2744cfcc706cSMiquel Raynal { 2745cfcc706cSMiquel Raynal int ret = -ENOTSUPP; 2746cfcc706cSMiquel Raynal 2747cfcc706cSMiquel Raynal ops->retlen = 0; 2748cfcc706cSMiquel Raynal 2749cfcc706cSMiquel Raynal /* Do not allow writes past end of device */ 2750cfcc706cSMiquel Raynal if (ops->datbuf && (to + ops->len) > mtd->size) { 2751cfcc706cSMiquel Raynal pr_debug("%s: attempt to write beyond end of device\n", 2752cfcc706cSMiquel Raynal __func__); 2753cfcc706cSMiquel Raynal return -EINVAL; 2754cfcc706cSMiquel Raynal } 2755cfcc706cSMiquel Raynal 2756cfcc706cSMiquel Raynal nand_get_device(mtd, FL_WRITING); 2757cfcc706cSMiquel Raynal 2758cfcc706cSMiquel Raynal switch (ops->mode) { 2759cfcc706cSMiquel Raynal case MTD_OPS_PLACE_OOB: 2760cfcc706cSMiquel Raynal case MTD_OPS_AUTO_OOB: 2761cfcc706cSMiquel Raynal case MTD_OPS_RAW: 2762cfcc706cSMiquel Raynal break; 2763cfcc706cSMiquel Raynal 2764cfcc706cSMiquel Raynal default: 2765cfcc706cSMiquel Raynal goto out; 2766cfcc706cSMiquel Raynal } 2767cfcc706cSMiquel Raynal 2768cfcc706cSMiquel Raynal if (!ops->datbuf) 2769cfcc706cSMiquel Raynal ret = nand_do_write_oob(mtd, to, ops); 2770cfcc706cSMiquel Raynal else 2771cfcc706cSMiquel Raynal ret = nand_do_write_ops(mtd, to, ops); 2772cfcc706cSMiquel Raynal 2773cfcc706cSMiquel Raynal out: 2774cfcc706cSMiquel Raynal nand_release_device(mtd); 2775cfcc706cSMiquel Raynal return ret; 2776cfcc706cSMiquel Raynal } 2777cfcc706cSMiquel Raynal 2778cfcc706cSMiquel Raynal /** 2779cfcc706cSMiquel Raynal * single_erase - [GENERIC] NAND standard block erase command function 2780cfcc706cSMiquel Raynal * @mtd: MTD device structure 2781cfcc706cSMiquel Raynal * @page: the page address of the block which will be erased 2782cfcc706cSMiquel Raynal * 2783cfcc706cSMiquel Raynal * Standard erase command for NAND chips. Returns NAND status. 2784cfcc706cSMiquel Raynal */ 2785cfcc706cSMiquel Raynal static int single_erase(struct mtd_info *mtd, int page) 2786cfcc706cSMiquel Raynal { 2787cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 2788cfcc706cSMiquel Raynal /* Send commands to erase a block */ 2789cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); 2790cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); 2791cfcc706cSMiquel Raynal 2792cfcc706cSMiquel Raynal return chip->waitfunc(mtd, chip); 2793cfcc706cSMiquel Raynal } 2794cfcc706cSMiquel Raynal 2795cfcc706cSMiquel Raynal /** 2796cfcc706cSMiquel Raynal * nand_erase - [MTD Interface] erase block(s) 2797cfcc706cSMiquel Raynal * @mtd: MTD device structure 2798cfcc706cSMiquel Raynal * @instr: erase instruction 2799cfcc706cSMiquel Raynal * 2800cfcc706cSMiquel Raynal * Erase one ore more blocks. 2801cfcc706cSMiquel Raynal */ 2802cfcc706cSMiquel Raynal static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) 2803cfcc706cSMiquel Raynal { 2804cfcc706cSMiquel Raynal return nand_erase_nand(mtd, instr, 0); 2805cfcc706cSMiquel Raynal } 2806cfcc706cSMiquel Raynal 2807cfcc706cSMiquel Raynal /** 2808cfcc706cSMiquel Raynal * nand_erase_nand - [INTERN] erase block(s) 2809cfcc706cSMiquel Raynal * @mtd: MTD device structure 2810cfcc706cSMiquel Raynal * @instr: erase instruction 2811cfcc706cSMiquel Raynal * @allowbbt: allow erasing the bbt area 2812cfcc706cSMiquel Raynal * 2813cfcc706cSMiquel Raynal * Erase one ore more blocks. 2814cfcc706cSMiquel Raynal */ 2815cfcc706cSMiquel Raynal int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, 2816cfcc706cSMiquel Raynal int allowbbt) 2817cfcc706cSMiquel Raynal { 2818cfcc706cSMiquel Raynal int page, status, pages_per_block, ret, chipnr; 2819cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 2820cfcc706cSMiquel Raynal loff_t len; 2821cfcc706cSMiquel Raynal 2822cfcc706cSMiquel Raynal pr_debug("%s: start = 0x%012llx, len = %llu\n", 2823cfcc706cSMiquel Raynal __func__, (unsigned long long)instr->addr, 2824cfcc706cSMiquel Raynal (unsigned long long)instr->len); 2825cfcc706cSMiquel Raynal 2826cfcc706cSMiquel Raynal if (check_offs_len(mtd, instr->addr, instr->len)) 2827cfcc706cSMiquel Raynal return -EINVAL; 2828cfcc706cSMiquel Raynal 2829cfcc706cSMiquel Raynal /* Grab the lock and see if the device is available */ 2830cfcc706cSMiquel Raynal nand_get_device(mtd, FL_ERASING); 2831cfcc706cSMiquel Raynal 2832cfcc706cSMiquel Raynal /* Shift to get first page */ 2833cfcc706cSMiquel Raynal page = (int)(instr->addr >> chip->page_shift); 2834cfcc706cSMiquel Raynal chipnr = (int)(instr->addr >> chip->chip_shift); 2835cfcc706cSMiquel Raynal 2836cfcc706cSMiquel Raynal /* Calculate pages in each block */ 2837cfcc706cSMiquel Raynal pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); 2838cfcc706cSMiquel Raynal 2839cfcc706cSMiquel Raynal /* Select the NAND device */ 2840cfcc706cSMiquel Raynal chip->select_chip(mtd, chipnr); 2841cfcc706cSMiquel Raynal 2842cfcc706cSMiquel Raynal /* Check, if it is write protected */ 2843cfcc706cSMiquel Raynal if (nand_check_wp(mtd)) { 2844cfcc706cSMiquel Raynal pr_debug("%s: device is write protected!\n", 2845cfcc706cSMiquel Raynal __func__); 2846cfcc706cSMiquel Raynal instr->state = MTD_ERASE_FAILED; 2847cfcc706cSMiquel Raynal goto erase_exit; 2848cfcc706cSMiquel Raynal } 2849cfcc706cSMiquel Raynal 2850cfcc706cSMiquel Raynal /* Loop through the pages */ 2851cfcc706cSMiquel Raynal len = instr->len; 2852cfcc706cSMiquel Raynal 2853cfcc706cSMiquel Raynal instr->state = MTD_ERASING; 2854cfcc706cSMiquel Raynal 2855cfcc706cSMiquel Raynal while (len) { 2856cfcc706cSMiquel Raynal WATCHDOG_RESET(); 2857cfcc706cSMiquel Raynal 2858cfcc706cSMiquel Raynal /* Check if we have a bad block, we do not erase bad blocks! */ 2859cfcc706cSMiquel Raynal if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) << 2860cfcc706cSMiquel Raynal chip->page_shift, allowbbt)) { 2861cfcc706cSMiquel Raynal pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", 2862cfcc706cSMiquel Raynal __func__, page); 2863cfcc706cSMiquel Raynal instr->state = MTD_ERASE_FAILED; 2864cfcc706cSMiquel Raynal goto erase_exit; 2865cfcc706cSMiquel Raynal } 2866cfcc706cSMiquel Raynal 2867cfcc706cSMiquel Raynal /* 2868cfcc706cSMiquel Raynal * Invalidate the page cache, if we erase the block which 2869cfcc706cSMiquel Raynal * contains the current cached page. 2870cfcc706cSMiquel Raynal */ 2871cfcc706cSMiquel Raynal if (page <= chip->pagebuf && chip->pagebuf < 2872cfcc706cSMiquel Raynal (page + pages_per_block)) 2873cfcc706cSMiquel Raynal chip->pagebuf = -1; 2874cfcc706cSMiquel Raynal 2875cfcc706cSMiquel Raynal status = chip->erase(mtd, page & chip->pagemask); 2876cfcc706cSMiquel Raynal 2877cfcc706cSMiquel Raynal /* See if block erase succeeded */ 2878cfcc706cSMiquel Raynal if (status & NAND_STATUS_FAIL) { 2879cfcc706cSMiquel Raynal pr_debug("%s: failed erase, page 0x%08x\n", 2880cfcc706cSMiquel Raynal __func__, page); 2881cfcc706cSMiquel Raynal instr->state = MTD_ERASE_FAILED; 2882cfcc706cSMiquel Raynal instr->fail_addr = 2883cfcc706cSMiquel Raynal ((loff_t)page << chip->page_shift); 2884cfcc706cSMiquel Raynal goto erase_exit; 2885cfcc706cSMiquel Raynal } 2886cfcc706cSMiquel Raynal 2887cfcc706cSMiquel Raynal /* Increment page address and decrement length */ 2888cfcc706cSMiquel Raynal len -= (1ULL << chip->phys_erase_shift); 2889cfcc706cSMiquel Raynal page += pages_per_block; 2890cfcc706cSMiquel Raynal 2891cfcc706cSMiquel Raynal /* Check, if we cross a chip boundary */ 2892cfcc706cSMiquel Raynal if (len && !(page & chip->pagemask)) { 2893cfcc706cSMiquel Raynal chipnr++; 2894cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 2895cfcc706cSMiquel Raynal chip->select_chip(mtd, chipnr); 2896cfcc706cSMiquel Raynal } 2897cfcc706cSMiquel Raynal } 2898cfcc706cSMiquel Raynal instr->state = MTD_ERASE_DONE; 2899cfcc706cSMiquel Raynal 2900cfcc706cSMiquel Raynal erase_exit: 2901cfcc706cSMiquel Raynal 2902cfcc706cSMiquel Raynal ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; 2903cfcc706cSMiquel Raynal 2904cfcc706cSMiquel Raynal /* Deselect and wake up anyone waiting on the device */ 2905cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 2906cfcc706cSMiquel Raynal nand_release_device(mtd); 2907cfcc706cSMiquel Raynal 2908cfcc706cSMiquel Raynal /* Do call back function */ 2909cfcc706cSMiquel Raynal if (!ret) 2910cfcc706cSMiquel Raynal mtd_erase_callback(instr); 2911cfcc706cSMiquel Raynal 2912cfcc706cSMiquel Raynal /* Return more or less happy */ 2913cfcc706cSMiquel Raynal return ret; 2914cfcc706cSMiquel Raynal } 2915cfcc706cSMiquel Raynal 2916cfcc706cSMiquel Raynal /** 2917cfcc706cSMiquel Raynal * nand_sync - [MTD Interface] sync 2918cfcc706cSMiquel Raynal * @mtd: MTD device structure 2919cfcc706cSMiquel Raynal * 2920cfcc706cSMiquel Raynal * Sync is actually a wait for chip ready function. 2921cfcc706cSMiquel Raynal */ 2922cfcc706cSMiquel Raynal static void nand_sync(struct mtd_info *mtd) 2923cfcc706cSMiquel Raynal { 2924cfcc706cSMiquel Raynal pr_debug("%s: called\n", __func__); 2925cfcc706cSMiquel Raynal 2926cfcc706cSMiquel Raynal /* Grab the lock and see if the device is available */ 2927cfcc706cSMiquel Raynal nand_get_device(mtd, FL_SYNCING); 2928cfcc706cSMiquel Raynal /* Release it and go back */ 2929cfcc706cSMiquel Raynal nand_release_device(mtd); 2930cfcc706cSMiquel Raynal } 2931cfcc706cSMiquel Raynal 2932cfcc706cSMiquel Raynal /** 2933cfcc706cSMiquel Raynal * nand_block_isbad - [MTD Interface] Check if block at offset is bad 2934cfcc706cSMiquel Raynal * @mtd: MTD device structure 2935cfcc706cSMiquel Raynal * @offs: offset relative to mtd start 2936cfcc706cSMiquel Raynal */ 2937cfcc706cSMiquel Raynal static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) 2938cfcc706cSMiquel Raynal { 2939cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 2940cfcc706cSMiquel Raynal int chipnr = (int)(offs >> chip->chip_shift); 2941cfcc706cSMiquel Raynal int ret; 2942cfcc706cSMiquel Raynal 2943cfcc706cSMiquel Raynal /* Select the NAND device */ 2944cfcc706cSMiquel Raynal nand_get_device(mtd, FL_READING); 2945cfcc706cSMiquel Raynal chip->select_chip(mtd, chipnr); 2946cfcc706cSMiquel Raynal 2947cfcc706cSMiquel Raynal ret = nand_block_checkbad(mtd, offs, 0); 2948cfcc706cSMiquel Raynal 2949cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 2950cfcc706cSMiquel Raynal nand_release_device(mtd); 2951cfcc706cSMiquel Raynal 2952cfcc706cSMiquel Raynal return ret; 2953cfcc706cSMiquel Raynal } 2954cfcc706cSMiquel Raynal 2955cfcc706cSMiquel Raynal /** 2956cfcc706cSMiquel Raynal * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad 2957cfcc706cSMiquel Raynal * @mtd: MTD device structure 2958cfcc706cSMiquel Raynal * @ofs: offset relative to mtd start 2959cfcc706cSMiquel Raynal */ 2960cfcc706cSMiquel Raynal static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) 2961cfcc706cSMiquel Raynal { 2962cfcc706cSMiquel Raynal int ret; 2963cfcc706cSMiquel Raynal 2964cfcc706cSMiquel Raynal ret = nand_block_isbad(mtd, ofs); 2965cfcc706cSMiquel Raynal if (ret) { 2966cfcc706cSMiquel Raynal /* If it was bad already, return success and do nothing */ 2967cfcc706cSMiquel Raynal if (ret > 0) 2968cfcc706cSMiquel Raynal return 0; 2969cfcc706cSMiquel Raynal return ret; 2970cfcc706cSMiquel Raynal } 2971cfcc706cSMiquel Raynal 2972cfcc706cSMiquel Raynal return nand_block_markbad_lowlevel(mtd, ofs); 2973cfcc706cSMiquel Raynal } 2974cfcc706cSMiquel Raynal 2975cfcc706cSMiquel Raynal /** 2976cfcc706cSMiquel Raynal * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand 2977cfcc706cSMiquel Raynal * @mtd: MTD device structure 2978cfcc706cSMiquel Raynal * @chip: nand chip info structure 2979cfcc706cSMiquel Raynal * @addr: feature address. 2980cfcc706cSMiquel Raynal * @subfeature_param: the subfeature parameters, a four bytes array. 2981cfcc706cSMiquel Raynal */ 2982cfcc706cSMiquel Raynal static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip, 2983cfcc706cSMiquel Raynal int addr, uint8_t *subfeature_param) 2984cfcc706cSMiquel Raynal { 2985cfcc706cSMiquel Raynal int status; 2986cfcc706cSMiquel Raynal int i; 2987cfcc706cSMiquel Raynal 2988cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_ONFI_DETECTION 2989cfcc706cSMiquel Raynal if (!chip->onfi_version || 2990cfcc706cSMiquel Raynal !(le16_to_cpu(chip->onfi_params.opt_cmd) 2991cfcc706cSMiquel Raynal & ONFI_OPT_CMD_SET_GET_FEATURES)) 2992cfcc706cSMiquel Raynal return -ENOTSUPP; 2993cfcc706cSMiquel Raynal #endif 2994cfcc706cSMiquel Raynal 2995cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1); 2996cfcc706cSMiquel Raynal for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) 2997cfcc706cSMiquel Raynal chip->write_byte(mtd, subfeature_param[i]); 2998cfcc706cSMiquel Raynal 2999cfcc706cSMiquel Raynal status = chip->waitfunc(mtd, chip); 3000cfcc706cSMiquel Raynal if (status & NAND_STATUS_FAIL) 3001cfcc706cSMiquel Raynal return -EIO; 3002cfcc706cSMiquel Raynal return 0; 3003cfcc706cSMiquel Raynal } 3004cfcc706cSMiquel Raynal 3005cfcc706cSMiquel Raynal /** 3006cfcc706cSMiquel Raynal * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand 3007cfcc706cSMiquel Raynal * @mtd: MTD device structure 3008cfcc706cSMiquel Raynal * @chip: nand chip info structure 3009cfcc706cSMiquel Raynal * @addr: feature address. 3010cfcc706cSMiquel Raynal * @subfeature_param: the subfeature parameters, a four bytes array. 3011cfcc706cSMiquel Raynal */ 3012cfcc706cSMiquel Raynal static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip, 3013cfcc706cSMiquel Raynal int addr, uint8_t *subfeature_param) 3014cfcc706cSMiquel Raynal { 3015cfcc706cSMiquel Raynal int i; 3016cfcc706cSMiquel Raynal 3017cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_ONFI_DETECTION 3018cfcc706cSMiquel Raynal if (!chip->onfi_version || 3019cfcc706cSMiquel Raynal !(le16_to_cpu(chip->onfi_params.opt_cmd) 3020cfcc706cSMiquel Raynal & ONFI_OPT_CMD_SET_GET_FEATURES)) 3021cfcc706cSMiquel Raynal return -ENOTSUPP; 3022cfcc706cSMiquel Raynal #endif 3023cfcc706cSMiquel Raynal 3024cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1); 3025cfcc706cSMiquel Raynal for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) 3026cfcc706cSMiquel Raynal *subfeature_param++ = chip->read_byte(mtd); 3027cfcc706cSMiquel Raynal return 0; 3028cfcc706cSMiquel Raynal } 3029cfcc706cSMiquel Raynal 3030cfcc706cSMiquel Raynal /* Set default functions */ 3031cfcc706cSMiquel Raynal static void nand_set_defaults(struct nand_chip *chip, int busw) 3032cfcc706cSMiquel Raynal { 3033cfcc706cSMiquel Raynal /* check for proper chip_delay setup, set 20us if not */ 3034cfcc706cSMiquel Raynal if (!chip->chip_delay) 3035cfcc706cSMiquel Raynal chip->chip_delay = 20; 3036cfcc706cSMiquel Raynal 3037cfcc706cSMiquel Raynal /* check, if a user supplied command function given */ 3038cfcc706cSMiquel Raynal if (chip->cmdfunc == NULL) 3039cfcc706cSMiquel Raynal chip->cmdfunc = nand_command; 3040cfcc706cSMiquel Raynal 3041cfcc706cSMiquel Raynal /* check, if a user supplied wait function given */ 3042cfcc706cSMiquel Raynal if (chip->waitfunc == NULL) 3043cfcc706cSMiquel Raynal chip->waitfunc = nand_wait; 3044cfcc706cSMiquel Raynal 3045cfcc706cSMiquel Raynal if (!chip->select_chip) 3046cfcc706cSMiquel Raynal chip->select_chip = nand_select_chip; 3047cfcc706cSMiquel Raynal 3048cfcc706cSMiquel Raynal /* set for ONFI nand */ 3049cfcc706cSMiquel Raynal if (!chip->onfi_set_features) 3050cfcc706cSMiquel Raynal chip->onfi_set_features = nand_onfi_set_features; 3051cfcc706cSMiquel Raynal if (!chip->onfi_get_features) 3052cfcc706cSMiquel Raynal chip->onfi_get_features = nand_onfi_get_features; 3053cfcc706cSMiquel Raynal 3054cfcc706cSMiquel Raynal /* If called twice, pointers that depend on busw may need to be reset */ 3055cfcc706cSMiquel Raynal if (!chip->read_byte || chip->read_byte == nand_read_byte) 3056cfcc706cSMiquel Raynal chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; 3057cfcc706cSMiquel Raynal if (!chip->read_word) 3058cfcc706cSMiquel Raynal chip->read_word = nand_read_word; 3059cfcc706cSMiquel Raynal if (!chip->block_bad) 3060cfcc706cSMiquel Raynal chip->block_bad = nand_block_bad; 3061cfcc706cSMiquel Raynal if (!chip->block_markbad) 3062cfcc706cSMiquel Raynal chip->block_markbad = nand_default_block_markbad; 3063cfcc706cSMiquel Raynal if (!chip->write_buf || chip->write_buf == nand_write_buf) 3064cfcc706cSMiquel Raynal chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; 3065cfcc706cSMiquel Raynal if (!chip->write_byte || chip->write_byte == nand_write_byte) 3066cfcc706cSMiquel Raynal chip->write_byte = busw ? nand_write_byte16 : nand_write_byte; 3067cfcc706cSMiquel Raynal if (!chip->read_buf || chip->read_buf == nand_read_buf) 3068cfcc706cSMiquel Raynal chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; 3069cfcc706cSMiquel Raynal if (!chip->scan_bbt) 3070cfcc706cSMiquel Raynal chip->scan_bbt = nand_default_bbt; 3071cfcc706cSMiquel Raynal 3072cfcc706cSMiquel Raynal if (!chip->controller) { 3073cfcc706cSMiquel Raynal chip->controller = &chip->hwcontrol; 3074cfcc706cSMiquel Raynal spin_lock_init(&chip->controller->lock); 3075cfcc706cSMiquel Raynal init_waitqueue_head(&chip->controller->wq); 3076cfcc706cSMiquel Raynal } 3077cfcc706cSMiquel Raynal 3078cfcc706cSMiquel Raynal if (!chip->buf_align) 3079cfcc706cSMiquel Raynal chip->buf_align = 1; 3080cfcc706cSMiquel Raynal } 3081cfcc706cSMiquel Raynal 3082cfcc706cSMiquel Raynal /* Sanitize ONFI strings so we can safely print them */ 3083cfcc706cSMiquel Raynal static void sanitize_string(char *s, size_t len) 3084cfcc706cSMiquel Raynal { 3085cfcc706cSMiquel Raynal ssize_t i; 3086cfcc706cSMiquel Raynal 3087cfcc706cSMiquel Raynal /* Null terminate */ 3088cfcc706cSMiquel Raynal s[len - 1] = 0; 3089cfcc706cSMiquel Raynal 3090cfcc706cSMiquel Raynal /* Remove non printable chars */ 3091cfcc706cSMiquel Raynal for (i = 0; i < len - 1; i++) { 3092cfcc706cSMiquel Raynal if (s[i] < ' ' || s[i] > 127) 3093cfcc706cSMiquel Raynal s[i] = '?'; 3094cfcc706cSMiquel Raynal } 3095cfcc706cSMiquel Raynal 3096cfcc706cSMiquel Raynal /* Remove trailing spaces */ 3097cfcc706cSMiquel Raynal strim(s); 3098cfcc706cSMiquel Raynal } 3099cfcc706cSMiquel Raynal 3100cfcc706cSMiquel Raynal static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) 3101cfcc706cSMiquel Raynal { 3102cfcc706cSMiquel Raynal int i; 3103cfcc706cSMiquel Raynal while (len--) { 3104cfcc706cSMiquel Raynal crc ^= *p++ << 8; 3105cfcc706cSMiquel Raynal for (i = 0; i < 8; i++) 3106cfcc706cSMiquel Raynal crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); 3107cfcc706cSMiquel Raynal } 3108cfcc706cSMiquel Raynal 3109cfcc706cSMiquel Raynal return crc; 3110cfcc706cSMiquel Raynal } 3111cfcc706cSMiquel Raynal 3112cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_ONFI_DETECTION 3113cfcc706cSMiquel Raynal /* Parse the Extended Parameter Page. */ 3114cfcc706cSMiquel Raynal static int nand_flash_detect_ext_param_page(struct mtd_info *mtd, 3115cfcc706cSMiquel Raynal struct nand_chip *chip, struct nand_onfi_params *p) 3116cfcc706cSMiquel Raynal { 3117cfcc706cSMiquel Raynal struct onfi_ext_param_page *ep; 3118cfcc706cSMiquel Raynal struct onfi_ext_section *s; 3119cfcc706cSMiquel Raynal struct onfi_ext_ecc_info *ecc; 3120cfcc706cSMiquel Raynal uint8_t *cursor; 3121cfcc706cSMiquel Raynal int ret = -EINVAL; 3122cfcc706cSMiquel Raynal int len; 3123cfcc706cSMiquel Raynal int i; 3124cfcc706cSMiquel Raynal 3125cfcc706cSMiquel Raynal len = le16_to_cpu(p->ext_param_page_length) * 16; 3126cfcc706cSMiquel Raynal ep = kmalloc(len, GFP_KERNEL); 3127cfcc706cSMiquel Raynal if (!ep) 3128cfcc706cSMiquel Raynal return -ENOMEM; 3129cfcc706cSMiquel Raynal 3130cfcc706cSMiquel Raynal /* Send our own NAND_CMD_PARAM. */ 3131cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); 3132cfcc706cSMiquel Raynal 3133cfcc706cSMiquel Raynal /* Use the Change Read Column command to skip the ONFI param pages. */ 3134cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_RNDOUT, 3135cfcc706cSMiquel Raynal sizeof(*p) * p->num_of_param_pages , -1); 3136cfcc706cSMiquel Raynal 3137cfcc706cSMiquel Raynal /* Read out the Extended Parameter Page. */ 3138cfcc706cSMiquel Raynal chip->read_buf(mtd, (uint8_t *)ep, len); 3139cfcc706cSMiquel Raynal if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2) 3140cfcc706cSMiquel Raynal != le16_to_cpu(ep->crc))) { 3141cfcc706cSMiquel Raynal pr_debug("fail in the CRC.\n"); 3142cfcc706cSMiquel Raynal goto ext_out; 3143cfcc706cSMiquel Raynal } 3144cfcc706cSMiquel Raynal 3145cfcc706cSMiquel Raynal /* 3146cfcc706cSMiquel Raynal * Check the signature. 3147cfcc706cSMiquel Raynal * Do not strictly follow the ONFI spec, maybe changed in future. 3148cfcc706cSMiquel Raynal */ 3149cfcc706cSMiquel Raynal if (strncmp((char *)ep->sig, "EPPS", 4)) { 3150cfcc706cSMiquel Raynal pr_debug("The signature is invalid.\n"); 3151cfcc706cSMiquel Raynal goto ext_out; 3152cfcc706cSMiquel Raynal } 3153cfcc706cSMiquel Raynal 3154cfcc706cSMiquel Raynal /* find the ECC section. */ 3155cfcc706cSMiquel Raynal cursor = (uint8_t *)(ep + 1); 3156cfcc706cSMiquel Raynal for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) { 3157cfcc706cSMiquel Raynal s = ep->sections + i; 3158cfcc706cSMiquel Raynal if (s->type == ONFI_SECTION_TYPE_2) 3159cfcc706cSMiquel Raynal break; 3160cfcc706cSMiquel Raynal cursor += s->length * 16; 3161cfcc706cSMiquel Raynal } 3162cfcc706cSMiquel Raynal if (i == ONFI_EXT_SECTION_MAX) { 3163cfcc706cSMiquel Raynal pr_debug("We can not find the ECC section.\n"); 3164cfcc706cSMiquel Raynal goto ext_out; 3165cfcc706cSMiquel Raynal } 3166cfcc706cSMiquel Raynal 3167cfcc706cSMiquel Raynal /* get the info we want. */ 3168cfcc706cSMiquel Raynal ecc = (struct onfi_ext_ecc_info *)cursor; 3169cfcc706cSMiquel Raynal 3170cfcc706cSMiquel Raynal if (!ecc->codeword_size) { 3171cfcc706cSMiquel Raynal pr_debug("Invalid codeword size\n"); 3172cfcc706cSMiquel Raynal goto ext_out; 3173cfcc706cSMiquel Raynal } 3174cfcc706cSMiquel Raynal 3175cfcc706cSMiquel Raynal chip->ecc_strength_ds = ecc->ecc_bits; 3176cfcc706cSMiquel Raynal chip->ecc_step_ds = 1 << ecc->codeword_size; 3177cfcc706cSMiquel Raynal ret = 0; 3178cfcc706cSMiquel Raynal 3179cfcc706cSMiquel Raynal ext_out: 3180cfcc706cSMiquel Raynal kfree(ep); 3181cfcc706cSMiquel Raynal return ret; 3182cfcc706cSMiquel Raynal } 3183cfcc706cSMiquel Raynal 3184cfcc706cSMiquel Raynal static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode) 3185cfcc706cSMiquel Raynal { 3186cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 3187cfcc706cSMiquel Raynal uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode}; 3188cfcc706cSMiquel Raynal 3189cfcc706cSMiquel Raynal return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY, 3190cfcc706cSMiquel Raynal feature); 3191cfcc706cSMiquel Raynal } 3192cfcc706cSMiquel Raynal 3193cfcc706cSMiquel Raynal /* 3194cfcc706cSMiquel Raynal * Configure chip properties from Micron vendor-specific ONFI table 3195cfcc706cSMiquel Raynal */ 3196cfcc706cSMiquel Raynal static void nand_onfi_detect_micron(struct nand_chip *chip, 3197cfcc706cSMiquel Raynal struct nand_onfi_params *p) 3198cfcc706cSMiquel Raynal { 3199cfcc706cSMiquel Raynal struct nand_onfi_vendor_micron *micron = (void *)p->vendor; 3200cfcc706cSMiquel Raynal 3201cfcc706cSMiquel Raynal if (le16_to_cpu(p->vendor_revision) < 1) 3202cfcc706cSMiquel Raynal return; 3203cfcc706cSMiquel Raynal 3204cfcc706cSMiquel Raynal chip->read_retries = micron->read_retry_options; 3205cfcc706cSMiquel Raynal chip->setup_read_retry = nand_setup_read_retry_micron; 3206cfcc706cSMiquel Raynal } 3207cfcc706cSMiquel Raynal 3208cfcc706cSMiquel Raynal /* 3209cfcc706cSMiquel Raynal * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise. 3210cfcc706cSMiquel Raynal */ 3211cfcc706cSMiquel Raynal static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, 3212cfcc706cSMiquel Raynal int *busw) 3213cfcc706cSMiquel Raynal { 3214cfcc706cSMiquel Raynal struct nand_onfi_params *p = &chip->onfi_params; 3215cfcc706cSMiquel Raynal int i, j; 3216cfcc706cSMiquel Raynal int val; 3217cfcc706cSMiquel Raynal 3218cfcc706cSMiquel Raynal /* Try ONFI for unknown chip or LP */ 3219cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1); 3220cfcc706cSMiquel Raynal if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' || 3221cfcc706cSMiquel Raynal chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') 3222cfcc706cSMiquel Raynal return 0; 3223cfcc706cSMiquel Raynal 3224cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); 3225cfcc706cSMiquel Raynal for (i = 0; i < 3; i++) { 3226cfcc706cSMiquel Raynal for (j = 0; j < sizeof(*p); j++) 3227cfcc706cSMiquel Raynal ((uint8_t *)p)[j] = chip->read_byte(mtd); 3228cfcc706cSMiquel Raynal if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == 3229cfcc706cSMiquel Raynal le16_to_cpu(p->crc)) { 3230cfcc706cSMiquel Raynal break; 3231cfcc706cSMiquel Raynal } 3232cfcc706cSMiquel Raynal } 3233cfcc706cSMiquel Raynal 3234cfcc706cSMiquel Raynal if (i == 3) { 3235cfcc706cSMiquel Raynal pr_err("Could not find valid ONFI parameter page; aborting\n"); 3236cfcc706cSMiquel Raynal return 0; 3237cfcc706cSMiquel Raynal } 3238cfcc706cSMiquel Raynal 3239cfcc706cSMiquel Raynal /* Check version */ 3240cfcc706cSMiquel Raynal val = le16_to_cpu(p->revision); 3241cfcc706cSMiquel Raynal if (val & (1 << 5)) 3242cfcc706cSMiquel Raynal chip->onfi_version = 23; 3243cfcc706cSMiquel Raynal else if (val & (1 << 4)) 3244cfcc706cSMiquel Raynal chip->onfi_version = 22; 3245cfcc706cSMiquel Raynal else if (val & (1 << 3)) 3246cfcc706cSMiquel Raynal chip->onfi_version = 21; 3247cfcc706cSMiquel Raynal else if (val & (1 << 2)) 3248cfcc706cSMiquel Raynal chip->onfi_version = 20; 3249cfcc706cSMiquel Raynal else if (val & (1 << 1)) 3250cfcc706cSMiquel Raynal chip->onfi_version = 10; 3251cfcc706cSMiquel Raynal 3252cfcc706cSMiquel Raynal if (!chip->onfi_version) { 3253cfcc706cSMiquel Raynal pr_info("unsupported ONFI version: %d\n", val); 3254cfcc706cSMiquel Raynal return 0; 3255cfcc706cSMiquel Raynal } 3256cfcc706cSMiquel Raynal 3257cfcc706cSMiquel Raynal sanitize_string(p->manufacturer, sizeof(p->manufacturer)); 3258cfcc706cSMiquel Raynal sanitize_string(p->model, sizeof(p->model)); 3259cfcc706cSMiquel Raynal if (!mtd->name) 3260cfcc706cSMiquel Raynal mtd->name = p->model; 3261cfcc706cSMiquel Raynal 3262cfcc706cSMiquel Raynal mtd->writesize = le32_to_cpu(p->byte_per_page); 3263cfcc706cSMiquel Raynal 3264cfcc706cSMiquel Raynal /* 3265cfcc706cSMiquel Raynal * pages_per_block and blocks_per_lun may not be a power-of-2 size 3266cfcc706cSMiquel Raynal * (don't ask me who thought of this...). MTD assumes that these 3267cfcc706cSMiquel Raynal * dimensions will be power-of-2, so just truncate the remaining area. 3268cfcc706cSMiquel Raynal */ 3269cfcc706cSMiquel Raynal mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); 3270cfcc706cSMiquel Raynal mtd->erasesize *= mtd->writesize; 3271cfcc706cSMiquel Raynal 3272cfcc706cSMiquel Raynal mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); 3273cfcc706cSMiquel Raynal 3274cfcc706cSMiquel Raynal /* See erasesize comment */ 3275cfcc706cSMiquel Raynal chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); 3276cfcc706cSMiquel Raynal chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; 3277cfcc706cSMiquel Raynal chip->bits_per_cell = p->bits_per_cell; 3278cfcc706cSMiquel Raynal 3279cfcc706cSMiquel Raynal if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS) 3280cfcc706cSMiquel Raynal *busw = NAND_BUSWIDTH_16; 3281cfcc706cSMiquel Raynal else 3282cfcc706cSMiquel Raynal *busw = 0; 3283cfcc706cSMiquel Raynal 3284cfcc706cSMiquel Raynal if (p->ecc_bits != 0xff) { 3285cfcc706cSMiquel Raynal chip->ecc_strength_ds = p->ecc_bits; 3286cfcc706cSMiquel Raynal chip->ecc_step_ds = 512; 3287cfcc706cSMiquel Raynal } else if (chip->onfi_version >= 21 && 3288cfcc706cSMiquel Raynal (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) { 3289cfcc706cSMiquel Raynal 3290cfcc706cSMiquel Raynal /* 3291cfcc706cSMiquel Raynal * The nand_flash_detect_ext_param_page() uses the 3292cfcc706cSMiquel Raynal * Change Read Column command which maybe not supported 3293cfcc706cSMiquel Raynal * by the chip->cmdfunc. So try to update the chip->cmdfunc 3294cfcc706cSMiquel Raynal * now. We do not replace user supplied command function. 3295cfcc706cSMiquel Raynal */ 3296cfcc706cSMiquel Raynal if (mtd->writesize > 512 && chip->cmdfunc == nand_command) 3297cfcc706cSMiquel Raynal chip->cmdfunc = nand_command_lp; 3298cfcc706cSMiquel Raynal 3299cfcc706cSMiquel Raynal /* The Extended Parameter Page is supported since ONFI 2.1. */ 3300cfcc706cSMiquel Raynal if (nand_flash_detect_ext_param_page(mtd, chip, p)) 3301cfcc706cSMiquel Raynal pr_warn("Failed to detect ONFI extended param page\n"); 3302cfcc706cSMiquel Raynal } else { 3303cfcc706cSMiquel Raynal pr_warn("Could not retrieve ONFI ECC requirements\n"); 3304cfcc706cSMiquel Raynal } 3305cfcc706cSMiquel Raynal 3306cfcc706cSMiquel Raynal if (p->jedec_id == NAND_MFR_MICRON) 3307cfcc706cSMiquel Raynal nand_onfi_detect_micron(chip, p); 3308cfcc706cSMiquel Raynal 3309cfcc706cSMiquel Raynal return 1; 3310cfcc706cSMiquel Raynal } 3311cfcc706cSMiquel Raynal #else 3312cfcc706cSMiquel Raynal static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, 3313cfcc706cSMiquel Raynal int *busw) 3314cfcc706cSMiquel Raynal { 3315cfcc706cSMiquel Raynal return 0; 3316cfcc706cSMiquel Raynal } 3317cfcc706cSMiquel Raynal #endif 3318cfcc706cSMiquel Raynal 3319cfcc706cSMiquel Raynal /* 3320cfcc706cSMiquel Raynal * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise. 3321cfcc706cSMiquel Raynal */ 3322cfcc706cSMiquel Raynal static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip, 3323cfcc706cSMiquel Raynal int *busw) 3324cfcc706cSMiquel Raynal { 3325cfcc706cSMiquel Raynal struct nand_jedec_params *p = &chip->jedec_params; 3326cfcc706cSMiquel Raynal struct jedec_ecc_info *ecc; 3327cfcc706cSMiquel Raynal int val; 3328cfcc706cSMiquel Raynal int i, j; 3329cfcc706cSMiquel Raynal 3330cfcc706cSMiquel Raynal /* Try JEDEC for unknown chip or LP */ 3331cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1); 3332cfcc706cSMiquel Raynal if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' || 3333cfcc706cSMiquel Raynal chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' || 3334cfcc706cSMiquel Raynal chip->read_byte(mtd) != 'C') 3335cfcc706cSMiquel Raynal return 0; 3336cfcc706cSMiquel Raynal 3337cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1); 3338cfcc706cSMiquel Raynal for (i = 0; i < 3; i++) { 3339cfcc706cSMiquel Raynal for (j = 0; j < sizeof(*p); j++) 3340cfcc706cSMiquel Raynal ((uint8_t *)p)[j] = chip->read_byte(mtd); 3341cfcc706cSMiquel Raynal 3342cfcc706cSMiquel Raynal if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) == 3343cfcc706cSMiquel Raynal le16_to_cpu(p->crc)) 3344cfcc706cSMiquel Raynal break; 3345cfcc706cSMiquel Raynal } 3346cfcc706cSMiquel Raynal 3347cfcc706cSMiquel Raynal if (i == 3) { 3348cfcc706cSMiquel Raynal pr_err("Could not find valid JEDEC parameter page; aborting\n"); 3349cfcc706cSMiquel Raynal return 0; 3350cfcc706cSMiquel Raynal } 3351cfcc706cSMiquel Raynal 3352cfcc706cSMiquel Raynal /* Check version */ 3353cfcc706cSMiquel Raynal val = le16_to_cpu(p->revision); 3354cfcc706cSMiquel Raynal if (val & (1 << 2)) 3355cfcc706cSMiquel Raynal chip->jedec_version = 10; 3356cfcc706cSMiquel Raynal else if (val & (1 << 1)) 3357cfcc706cSMiquel Raynal chip->jedec_version = 1; /* vendor specific version */ 3358cfcc706cSMiquel Raynal 3359cfcc706cSMiquel Raynal if (!chip->jedec_version) { 3360cfcc706cSMiquel Raynal pr_info("unsupported JEDEC version: %d\n", val); 3361cfcc706cSMiquel Raynal return 0; 3362cfcc706cSMiquel Raynal } 3363cfcc706cSMiquel Raynal 3364cfcc706cSMiquel Raynal sanitize_string(p->manufacturer, sizeof(p->manufacturer)); 3365cfcc706cSMiquel Raynal sanitize_string(p->model, sizeof(p->model)); 3366cfcc706cSMiquel Raynal if (!mtd->name) 3367cfcc706cSMiquel Raynal mtd->name = p->model; 3368cfcc706cSMiquel Raynal 3369cfcc706cSMiquel Raynal mtd->writesize = le32_to_cpu(p->byte_per_page); 3370cfcc706cSMiquel Raynal 3371cfcc706cSMiquel Raynal /* Please reference to the comment for nand_flash_detect_onfi. */ 3372cfcc706cSMiquel Raynal mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); 3373cfcc706cSMiquel Raynal mtd->erasesize *= mtd->writesize; 3374cfcc706cSMiquel Raynal 3375cfcc706cSMiquel Raynal mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); 3376cfcc706cSMiquel Raynal 3377cfcc706cSMiquel Raynal /* Please reference to the comment for nand_flash_detect_onfi. */ 3378cfcc706cSMiquel Raynal chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); 3379cfcc706cSMiquel Raynal chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; 3380cfcc706cSMiquel Raynal chip->bits_per_cell = p->bits_per_cell; 3381cfcc706cSMiquel Raynal 3382cfcc706cSMiquel Raynal if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS) 3383cfcc706cSMiquel Raynal *busw = NAND_BUSWIDTH_16; 3384cfcc706cSMiquel Raynal else 3385cfcc706cSMiquel Raynal *busw = 0; 3386cfcc706cSMiquel Raynal 3387cfcc706cSMiquel Raynal /* ECC info */ 3388cfcc706cSMiquel Raynal ecc = &p->ecc_info[0]; 3389cfcc706cSMiquel Raynal 3390cfcc706cSMiquel Raynal if (ecc->codeword_size >= 9) { 3391cfcc706cSMiquel Raynal chip->ecc_strength_ds = ecc->ecc_bits; 3392cfcc706cSMiquel Raynal chip->ecc_step_ds = 1 << ecc->codeword_size; 3393cfcc706cSMiquel Raynal } else { 3394cfcc706cSMiquel Raynal pr_warn("Invalid codeword size\n"); 3395cfcc706cSMiquel Raynal } 3396cfcc706cSMiquel Raynal 3397cfcc706cSMiquel Raynal return 1; 3398cfcc706cSMiquel Raynal } 3399cfcc706cSMiquel Raynal 3400cfcc706cSMiquel Raynal /* 3401cfcc706cSMiquel Raynal * nand_id_has_period - Check if an ID string has a given wraparound period 3402cfcc706cSMiquel Raynal * @id_data: the ID string 3403cfcc706cSMiquel Raynal * @arrlen: the length of the @id_data array 3404cfcc706cSMiquel Raynal * @period: the period of repitition 3405cfcc706cSMiquel Raynal * 3406cfcc706cSMiquel Raynal * Check if an ID string is repeated within a given sequence of bytes at 3407cfcc706cSMiquel Raynal * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a 3408cfcc706cSMiquel Raynal * period of 3). This is a helper function for nand_id_len(). Returns non-zero 3409cfcc706cSMiquel Raynal * if the repetition has a period of @period; otherwise, returns zero. 3410cfcc706cSMiquel Raynal */ 3411cfcc706cSMiquel Raynal static int nand_id_has_period(u8 *id_data, int arrlen, int period) 3412cfcc706cSMiquel Raynal { 3413cfcc706cSMiquel Raynal int i, j; 3414cfcc706cSMiquel Raynal for (i = 0; i < period; i++) 3415cfcc706cSMiquel Raynal for (j = i + period; j < arrlen; j += period) 3416cfcc706cSMiquel Raynal if (id_data[i] != id_data[j]) 3417cfcc706cSMiquel Raynal return 0; 3418cfcc706cSMiquel Raynal return 1; 3419cfcc706cSMiquel Raynal } 3420cfcc706cSMiquel Raynal 3421cfcc706cSMiquel Raynal /* 3422cfcc706cSMiquel Raynal * nand_id_len - Get the length of an ID string returned by CMD_READID 3423cfcc706cSMiquel Raynal * @id_data: the ID string 3424cfcc706cSMiquel Raynal * @arrlen: the length of the @id_data array 3425cfcc706cSMiquel Raynal 3426cfcc706cSMiquel Raynal * Returns the length of the ID string, according to known wraparound/trailing 3427cfcc706cSMiquel Raynal * zero patterns. If no pattern exists, returns the length of the array. 3428cfcc706cSMiquel Raynal */ 3429cfcc706cSMiquel Raynal static int nand_id_len(u8 *id_data, int arrlen) 3430cfcc706cSMiquel Raynal { 3431cfcc706cSMiquel Raynal int last_nonzero, period; 3432cfcc706cSMiquel Raynal 3433cfcc706cSMiquel Raynal /* Find last non-zero byte */ 3434cfcc706cSMiquel Raynal for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) 3435cfcc706cSMiquel Raynal if (id_data[last_nonzero]) 3436cfcc706cSMiquel Raynal break; 3437cfcc706cSMiquel Raynal 3438cfcc706cSMiquel Raynal /* All zeros */ 3439cfcc706cSMiquel Raynal if (last_nonzero < 0) 3440cfcc706cSMiquel Raynal return 0; 3441cfcc706cSMiquel Raynal 3442cfcc706cSMiquel Raynal /* Calculate wraparound period */ 3443cfcc706cSMiquel Raynal for (period = 1; period < arrlen; period++) 3444cfcc706cSMiquel Raynal if (nand_id_has_period(id_data, arrlen, period)) 3445cfcc706cSMiquel Raynal break; 3446cfcc706cSMiquel Raynal 3447cfcc706cSMiquel Raynal /* There's a repeated pattern */ 3448cfcc706cSMiquel Raynal if (period < arrlen) 3449cfcc706cSMiquel Raynal return period; 3450cfcc706cSMiquel Raynal 3451cfcc706cSMiquel Raynal /* There are trailing zeros */ 3452cfcc706cSMiquel Raynal if (last_nonzero < arrlen - 1) 3453cfcc706cSMiquel Raynal return last_nonzero + 1; 3454cfcc706cSMiquel Raynal 3455cfcc706cSMiquel Raynal /* No pattern detected */ 3456cfcc706cSMiquel Raynal return arrlen; 3457cfcc706cSMiquel Raynal } 3458cfcc706cSMiquel Raynal 3459cfcc706cSMiquel Raynal /* Extract the bits of per cell from the 3rd byte of the extended ID */ 3460cfcc706cSMiquel Raynal static int nand_get_bits_per_cell(u8 cellinfo) 3461cfcc706cSMiquel Raynal { 3462cfcc706cSMiquel Raynal int bits; 3463cfcc706cSMiquel Raynal 3464cfcc706cSMiquel Raynal bits = cellinfo & NAND_CI_CELLTYPE_MSK; 3465cfcc706cSMiquel Raynal bits >>= NAND_CI_CELLTYPE_SHIFT; 3466cfcc706cSMiquel Raynal return bits + 1; 3467cfcc706cSMiquel Raynal } 3468cfcc706cSMiquel Raynal 3469cfcc706cSMiquel Raynal /* 3470cfcc706cSMiquel Raynal * Many new NAND share similar device ID codes, which represent the size of the 3471cfcc706cSMiquel Raynal * chip. The rest of the parameters must be decoded according to generic or 3472cfcc706cSMiquel Raynal * manufacturer-specific "extended ID" decoding patterns. 3473cfcc706cSMiquel Raynal */ 3474cfcc706cSMiquel Raynal static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip, 3475cfcc706cSMiquel Raynal u8 id_data[8], int *busw) 3476cfcc706cSMiquel Raynal { 3477cfcc706cSMiquel Raynal int extid, id_len; 3478cfcc706cSMiquel Raynal /* The 3rd id byte holds MLC / multichip data */ 3479cfcc706cSMiquel Raynal chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); 3480cfcc706cSMiquel Raynal /* The 4th id byte is the important one */ 3481cfcc706cSMiquel Raynal extid = id_data[3]; 3482cfcc706cSMiquel Raynal 3483cfcc706cSMiquel Raynal id_len = nand_id_len(id_data, 8); 3484cfcc706cSMiquel Raynal 3485cfcc706cSMiquel Raynal /* 3486cfcc706cSMiquel Raynal * Field definitions are in the following datasheets: 3487cfcc706cSMiquel Raynal * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32) 3488cfcc706cSMiquel Raynal * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44) 3489cfcc706cSMiquel Raynal * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22) 3490cfcc706cSMiquel Raynal * 3491cfcc706cSMiquel Raynal * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung 3492cfcc706cSMiquel Raynal * ID to decide what to do. 3493cfcc706cSMiquel Raynal */ 3494cfcc706cSMiquel Raynal if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG && 3495cfcc706cSMiquel Raynal !nand_is_slc(chip) && id_data[5] != 0x00) { 3496cfcc706cSMiquel Raynal /* Calc pagesize */ 3497cfcc706cSMiquel Raynal mtd->writesize = 2048 << (extid & 0x03); 3498cfcc706cSMiquel Raynal extid >>= 2; 3499cfcc706cSMiquel Raynal /* Calc oobsize */ 3500cfcc706cSMiquel Raynal switch (((extid >> 2) & 0x04) | (extid & 0x03)) { 3501cfcc706cSMiquel Raynal case 1: 3502cfcc706cSMiquel Raynal mtd->oobsize = 128; 3503cfcc706cSMiquel Raynal break; 3504cfcc706cSMiquel Raynal case 2: 3505cfcc706cSMiquel Raynal mtd->oobsize = 218; 3506cfcc706cSMiquel Raynal break; 3507cfcc706cSMiquel Raynal case 3: 3508cfcc706cSMiquel Raynal mtd->oobsize = 400; 3509cfcc706cSMiquel Raynal break; 3510cfcc706cSMiquel Raynal case 4: 3511cfcc706cSMiquel Raynal mtd->oobsize = 436; 3512cfcc706cSMiquel Raynal break; 3513cfcc706cSMiquel Raynal case 5: 3514cfcc706cSMiquel Raynal mtd->oobsize = 512; 3515cfcc706cSMiquel Raynal break; 3516cfcc706cSMiquel Raynal case 6: 3517cfcc706cSMiquel Raynal mtd->oobsize = 640; 3518cfcc706cSMiquel Raynal break; 3519cfcc706cSMiquel Raynal case 7: 3520cfcc706cSMiquel Raynal default: /* Other cases are "reserved" (unknown) */ 3521cfcc706cSMiquel Raynal mtd->oobsize = 1024; 3522cfcc706cSMiquel Raynal break; 3523cfcc706cSMiquel Raynal } 3524cfcc706cSMiquel Raynal extid >>= 2; 3525cfcc706cSMiquel Raynal /* Calc blocksize */ 3526cfcc706cSMiquel Raynal mtd->erasesize = (128 * 1024) << 3527cfcc706cSMiquel Raynal (((extid >> 1) & 0x04) | (extid & 0x03)); 3528cfcc706cSMiquel Raynal *busw = 0; 3529cfcc706cSMiquel Raynal } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX && 3530cfcc706cSMiquel Raynal !nand_is_slc(chip)) { 3531cfcc706cSMiquel Raynal unsigned int tmp; 3532cfcc706cSMiquel Raynal 3533cfcc706cSMiquel Raynal /* Calc pagesize */ 3534cfcc706cSMiquel Raynal mtd->writesize = 2048 << (extid & 0x03); 3535cfcc706cSMiquel Raynal extid >>= 2; 3536cfcc706cSMiquel Raynal /* Calc oobsize */ 3537cfcc706cSMiquel Raynal switch (((extid >> 2) & 0x04) | (extid & 0x03)) { 3538cfcc706cSMiquel Raynal case 0: 3539cfcc706cSMiquel Raynal mtd->oobsize = 128; 3540cfcc706cSMiquel Raynal break; 3541cfcc706cSMiquel Raynal case 1: 3542cfcc706cSMiquel Raynal mtd->oobsize = 224; 3543cfcc706cSMiquel Raynal break; 3544cfcc706cSMiquel Raynal case 2: 3545cfcc706cSMiquel Raynal mtd->oobsize = 448; 3546cfcc706cSMiquel Raynal break; 3547cfcc706cSMiquel Raynal case 3: 3548cfcc706cSMiquel Raynal mtd->oobsize = 64; 3549cfcc706cSMiquel Raynal break; 3550cfcc706cSMiquel Raynal case 4: 3551cfcc706cSMiquel Raynal mtd->oobsize = 32; 3552cfcc706cSMiquel Raynal break; 3553cfcc706cSMiquel Raynal case 5: 3554cfcc706cSMiquel Raynal mtd->oobsize = 16; 3555cfcc706cSMiquel Raynal break; 3556cfcc706cSMiquel Raynal default: 3557cfcc706cSMiquel Raynal mtd->oobsize = 640; 3558cfcc706cSMiquel Raynal break; 3559cfcc706cSMiquel Raynal } 3560cfcc706cSMiquel Raynal extid >>= 2; 3561cfcc706cSMiquel Raynal /* Calc blocksize */ 3562cfcc706cSMiquel Raynal tmp = ((extid >> 1) & 0x04) | (extid & 0x03); 3563cfcc706cSMiquel Raynal if (tmp < 0x03) 3564cfcc706cSMiquel Raynal mtd->erasesize = (128 * 1024) << tmp; 3565cfcc706cSMiquel Raynal else if (tmp == 0x03) 3566cfcc706cSMiquel Raynal mtd->erasesize = 768 * 1024; 3567cfcc706cSMiquel Raynal else 3568cfcc706cSMiquel Raynal mtd->erasesize = (64 * 1024) << tmp; 3569cfcc706cSMiquel Raynal *busw = 0; 3570cfcc706cSMiquel Raynal } else { 3571cfcc706cSMiquel Raynal /* Calc pagesize */ 3572cfcc706cSMiquel Raynal mtd->writesize = 1024 << (extid & 0x03); 3573cfcc706cSMiquel Raynal extid >>= 2; 3574cfcc706cSMiquel Raynal /* Calc oobsize */ 3575cfcc706cSMiquel Raynal mtd->oobsize = (8 << (extid & 0x01)) * 3576cfcc706cSMiquel Raynal (mtd->writesize >> 9); 3577cfcc706cSMiquel Raynal extid >>= 2; 3578cfcc706cSMiquel Raynal /* Calc blocksize. Blocksize is multiples of 64KiB */ 3579cfcc706cSMiquel Raynal mtd->erasesize = (64 * 1024) << (extid & 0x03); 3580cfcc706cSMiquel Raynal extid >>= 2; 3581cfcc706cSMiquel Raynal /* Get buswidth information */ 3582cfcc706cSMiquel Raynal *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0; 3583cfcc706cSMiquel Raynal 3584cfcc706cSMiquel Raynal /* 3585cfcc706cSMiquel Raynal * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per 3586cfcc706cSMiquel Raynal * 512B page. For Toshiba SLC, we decode the 5th/6th byte as 3587cfcc706cSMiquel Raynal * follows: 3588cfcc706cSMiquel Raynal * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm, 3589cfcc706cSMiquel Raynal * 110b -> 24nm 3590cfcc706cSMiquel Raynal * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC 3591cfcc706cSMiquel Raynal */ 3592cfcc706cSMiquel Raynal if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA && 3593cfcc706cSMiquel Raynal nand_is_slc(chip) && 3594cfcc706cSMiquel Raynal (id_data[5] & 0x7) == 0x6 /* 24nm */ && 3595cfcc706cSMiquel Raynal !(id_data[4] & 0x80) /* !BENAND */) { 3596cfcc706cSMiquel Raynal mtd->oobsize = 32 * mtd->writesize >> 9; 3597cfcc706cSMiquel Raynal } 3598cfcc706cSMiquel Raynal 3599cfcc706cSMiquel Raynal } 3600cfcc706cSMiquel Raynal } 3601cfcc706cSMiquel Raynal 3602cfcc706cSMiquel Raynal /* 3603cfcc706cSMiquel Raynal * Old devices have chip data hardcoded in the device ID table. nand_decode_id 3604cfcc706cSMiquel Raynal * decodes a matching ID table entry and assigns the MTD size parameters for 3605cfcc706cSMiquel Raynal * the chip. 3606cfcc706cSMiquel Raynal */ 3607cfcc706cSMiquel Raynal static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip, 3608cfcc706cSMiquel Raynal struct nand_flash_dev *type, u8 id_data[8], 3609cfcc706cSMiquel Raynal int *busw) 3610cfcc706cSMiquel Raynal { 3611cfcc706cSMiquel Raynal int maf_id = id_data[0]; 3612cfcc706cSMiquel Raynal 3613cfcc706cSMiquel Raynal mtd->erasesize = type->erasesize; 3614cfcc706cSMiquel Raynal mtd->writesize = type->pagesize; 3615cfcc706cSMiquel Raynal mtd->oobsize = mtd->writesize / 32; 3616cfcc706cSMiquel Raynal *busw = type->options & NAND_BUSWIDTH_16; 3617cfcc706cSMiquel Raynal 3618cfcc706cSMiquel Raynal /* All legacy ID NAND are small-page, SLC */ 3619cfcc706cSMiquel Raynal chip->bits_per_cell = 1; 3620cfcc706cSMiquel Raynal 3621cfcc706cSMiquel Raynal /* 3622cfcc706cSMiquel Raynal * Check for Spansion/AMD ID + repeating 5th, 6th byte since 3623cfcc706cSMiquel Raynal * some Spansion chips have erasesize that conflicts with size 3624cfcc706cSMiquel Raynal * listed in nand_ids table. 3625cfcc706cSMiquel Raynal * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39) 3626cfcc706cSMiquel Raynal */ 3627cfcc706cSMiquel Raynal if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00 3628cfcc706cSMiquel Raynal && id_data[6] == 0x00 && id_data[7] == 0x00 3629cfcc706cSMiquel Raynal && mtd->writesize == 512) { 3630cfcc706cSMiquel Raynal mtd->erasesize = 128 * 1024; 3631cfcc706cSMiquel Raynal mtd->erasesize <<= ((id_data[3] & 0x03) << 1); 3632cfcc706cSMiquel Raynal } 3633cfcc706cSMiquel Raynal } 3634cfcc706cSMiquel Raynal 3635cfcc706cSMiquel Raynal /* 3636cfcc706cSMiquel Raynal * Set the bad block marker/indicator (BBM/BBI) patterns according to some 3637cfcc706cSMiquel Raynal * heuristic patterns using various detected parameters (e.g., manufacturer, 3638cfcc706cSMiquel Raynal * page size, cell-type information). 3639cfcc706cSMiquel Raynal */ 3640cfcc706cSMiquel Raynal static void nand_decode_bbm_options(struct mtd_info *mtd, 3641cfcc706cSMiquel Raynal struct nand_chip *chip, u8 id_data[8]) 3642cfcc706cSMiquel Raynal { 3643cfcc706cSMiquel Raynal int maf_id = id_data[0]; 3644cfcc706cSMiquel Raynal 3645cfcc706cSMiquel Raynal /* Set the bad block position */ 3646cfcc706cSMiquel Raynal if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) 3647cfcc706cSMiquel Raynal chip->badblockpos = NAND_LARGE_BADBLOCK_POS; 3648cfcc706cSMiquel Raynal else 3649cfcc706cSMiquel Raynal chip->badblockpos = NAND_SMALL_BADBLOCK_POS; 3650cfcc706cSMiquel Raynal 3651cfcc706cSMiquel Raynal /* 3652cfcc706cSMiquel Raynal * Bad block marker is stored in the last page of each block on Samsung 3653cfcc706cSMiquel Raynal * and Hynix MLC devices; stored in first two pages of each block on 3654cfcc706cSMiquel Raynal * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba, 3655cfcc706cSMiquel Raynal * AMD/Spansion, and Macronix. All others scan only the first page. 3656cfcc706cSMiquel Raynal */ 3657cfcc706cSMiquel Raynal if (!nand_is_slc(chip) && 3658cfcc706cSMiquel Raynal (maf_id == NAND_MFR_SAMSUNG || 3659cfcc706cSMiquel Raynal maf_id == NAND_MFR_HYNIX)) 3660cfcc706cSMiquel Raynal chip->bbt_options |= NAND_BBT_SCANLASTPAGE; 3661cfcc706cSMiquel Raynal else if ((nand_is_slc(chip) && 3662cfcc706cSMiquel Raynal (maf_id == NAND_MFR_SAMSUNG || 3663cfcc706cSMiquel Raynal maf_id == NAND_MFR_HYNIX || 3664cfcc706cSMiquel Raynal maf_id == NAND_MFR_TOSHIBA || 3665cfcc706cSMiquel Raynal maf_id == NAND_MFR_AMD || 3666cfcc706cSMiquel Raynal maf_id == NAND_MFR_MACRONIX)) || 3667cfcc706cSMiquel Raynal (mtd->writesize == 2048 && 3668cfcc706cSMiquel Raynal maf_id == NAND_MFR_MICRON)) 3669cfcc706cSMiquel Raynal chip->bbt_options |= NAND_BBT_SCAN2NDPAGE; 3670cfcc706cSMiquel Raynal } 3671cfcc706cSMiquel Raynal 3672cfcc706cSMiquel Raynal static inline bool is_full_id_nand(struct nand_flash_dev *type) 3673cfcc706cSMiquel Raynal { 3674cfcc706cSMiquel Raynal return type->id_len; 3675cfcc706cSMiquel Raynal } 3676cfcc706cSMiquel Raynal 3677cfcc706cSMiquel Raynal static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip, 3678cfcc706cSMiquel Raynal struct nand_flash_dev *type, u8 *id_data, int *busw) 3679cfcc706cSMiquel Raynal { 3680cfcc706cSMiquel Raynal if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) { 3681cfcc706cSMiquel Raynal mtd->writesize = type->pagesize; 3682cfcc706cSMiquel Raynal mtd->erasesize = type->erasesize; 3683cfcc706cSMiquel Raynal mtd->oobsize = type->oobsize; 3684cfcc706cSMiquel Raynal 3685cfcc706cSMiquel Raynal chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); 3686cfcc706cSMiquel Raynal chip->chipsize = (uint64_t)type->chipsize << 20; 3687cfcc706cSMiquel Raynal chip->options |= type->options; 3688cfcc706cSMiquel Raynal chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); 3689cfcc706cSMiquel Raynal chip->ecc_step_ds = NAND_ECC_STEP(type); 3690cfcc706cSMiquel Raynal chip->onfi_timing_mode_default = 3691cfcc706cSMiquel Raynal type->onfi_timing_mode_default; 3692cfcc706cSMiquel Raynal 3693cfcc706cSMiquel Raynal *busw = type->options & NAND_BUSWIDTH_16; 3694cfcc706cSMiquel Raynal 3695cfcc706cSMiquel Raynal if (!mtd->name) 3696cfcc706cSMiquel Raynal mtd->name = type->name; 3697cfcc706cSMiquel Raynal 3698cfcc706cSMiquel Raynal return true; 3699cfcc706cSMiquel Raynal } 3700cfcc706cSMiquel Raynal return false; 3701cfcc706cSMiquel Raynal } 3702cfcc706cSMiquel Raynal 3703cfcc706cSMiquel Raynal /* 3704cfcc706cSMiquel Raynal * Get the flash and manufacturer id and lookup if the type is supported. 3705cfcc706cSMiquel Raynal */ 3706cfcc706cSMiquel Raynal struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, 3707cfcc706cSMiquel Raynal struct nand_chip *chip, 3708cfcc706cSMiquel Raynal int *maf_id, int *dev_id, 3709cfcc706cSMiquel Raynal struct nand_flash_dev *type) 3710cfcc706cSMiquel Raynal { 3711cfcc706cSMiquel Raynal int busw; 3712cfcc706cSMiquel Raynal int i, maf_idx; 3713cfcc706cSMiquel Raynal u8 id_data[8]; 3714cfcc706cSMiquel Raynal 3715cfcc706cSMiquel Raynal /* 3716cfcc706cSMiquel Raynal * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) 3717cfcc706cSMiquel Raynal * after power-up. 3718cfcc706cSMiquel Raynal */ 3719cfcc706cSMiquel Raynal nand_reset(chip, 0); 3720cfcc706cSMiquel Raynal 3721cfcc706cSMiquel Raynal /* Select the device */ 3722cfcc706cSMiquel Raynal chip->select_chip(mtd, 0); 3723cfcc706cSMiquel Raynal 3724cfcc706cSMiquel Raynal /* Send the command for reading device ID */ 3725cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); 3726cfcc706cSMiquel Raynal 3727cfcc706cSMiquel Raynal /* Read manufacturer and device IDs */ 3728cfcc706cSMiquel Raynal *maf_id = chip->read_byte(mtd); 3729cfcc706cSMiquel Raynal *dev_id = chip->read_byte(mtd); 3730cfcc706cSMiquel Raynal 3731cfcc706cSMiquel Raynal /* 3732cfcc706cSMiquel Raynal * Try again to make sure, as some systems the bus-hold or other 3733cfcc706cSMiquel Raynal * interface concerns can cause random data which looks like a 3734cfcc706cSMiquel Raynal * possibly credible NAND flash to appear. If the two results do 3735cfcc706cSMiquel Raynal * not match, ignore the device completely. 3736cfcc706cSMiquel Raynal */ 3737cfcc706cSMiquel Raynal 3738cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); 3739cfcc706cSMiquel Raynal 3740cfcc706cSMiquel Raynal /* Read entire ID string */ 3741cfcc706cSMiquel Raynal for (i = 0; i < 8; i++) 3742cfcc706cSMiquel Raynal id_data[i] = chip->read_byte(mtd); 3743cfcc706cSMiquel Raynal 3744cfcc706cSMiquel Raynal if (id_data[0] != *maf_id || id_data[1] != *dev_id) { 3745cfcc706cSMiquel Raynal pr_info("second ID read did not match %02x,%02x against %02x,%02x\n", 3746cfcc706cSMiquel Raynal *maf_id, *dev_id, id_data[0], id_data[1]); 3747cfcc706cSMiquel Raynal return ERR_PTR(-ENODEV); 3748cfcc706cSMiquel Raynal } 3749cfcc706cSMiquel Raynal 3750cfcc706cSMiquel Raynal if (!type) 3751cfcc706cSMiquel Raynal type = nand_flash_ids; 3752cfcc706cSMiquel Raynal 3753cfcc706cSMiquel Raynal for (; type->name != NULL; type++) { 3754cfcc706cSMiquel Raynal if (is_full_id_nand(type)) { 3755cfcc706cSMiquel Raynal if (find_full_id_nand(mtd, chip, type, id_data, &busw)) 3756cfcc706cSMiquel Raynal goto ident_done; 3757cfcc706cSMiquel Raynal } else if (*dev_id == type->dev_id) { 3758cfcc706cSMiquel Raynal break; 3759cfcc706cSMiquel Raynal } 3760cfcc706cSMiquel Raynal } 3761cfcc706cSMiquel Raynal 3762cfcc706cSMiquel Raynal chip->onfi_version = 0; 3763cfcc706cSMiquel Raynal if (!type->name || !type->pagesize) { 3764cfcc706cSMiquel Raynal /* Check if the chip is ONFI compliant */ 3765cfcc706cSMiquel Raynal if (nand_flash_detect_onfi(mtd, chip, &busw)) 3766cfcc706cSMiquel Raynal goto ident_done; 3767cfcc706cSMiquel Raynal 3768cfcc706cSMiquel Raynal /* Check if the chip is JEDEC compliant */ 3769cfcc706cSMiquel Raynal if (nand_flash_detect_jedec(mtd, chip, &busw)) 3770cfcc706cSMiquel Raynal goto ident_done; 3771cfcc706cSMiquel Raynal } 3772cfcc706cSMiquel Raynal 3773cfcc706cSMiquel Raynal if (!type->name) 3774cfcc706cSMiquel Raynal return ERR_PTR(-ENODEV); 3775cfcc706cSMiquel Raynal 3776cfcc706cSMiquel Raynal if (!mtd->name) 3777cfcc706cSMiquel Raynal mtd->name = type->name; 3778cfcc706cSMiquel Raynal 3779cfcc706cSMiquel Raynal chip->chipsize = (uint64_t)type->chipsize << 20; 3780cfcc706cSMiquel Raynal 3781cfcc706cSMiquel Raynal if (!type->pagesize) { 3782cfcc706cSMiquel Raynal /* Decode parameters from extended ID */ 3783cfcc706cSMiquel Raynal nand_decode_ext_id(mtd, chip, id_data, &busw); 3784cfcc706cSMiquel Raynal } else { 3785cfcc706cSMiquel Raynal nand_decode_id(mtd, chip, type, id_data, &busw); 3786cfcc706cSMiquel Raynal } 3787cfcc706cSMiquel Raynal /* Get chip options */ 3788cfcc706cSMiquel Raynal chip->options |= type->options; 3789cfcc706cSMiquel Raynal 3790cfcc706cSMiquel Raynal /* 3791cfcc706cSMiquel Raynal * Check if chip is not a Samsung device. Do not clear the 3792cfcc706cSMiquel Raynal * options for chips which do not have an extended id. 3793cfcc706cSMiquel Raynal */ 3794cfcc706cSMiquel Raynal if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize) 3795cfcc706cSMiquel Raynal chip->options &= ~NAND_SAMSUNG_LP_OPTIONS; 3796cfcc706cSMiquel Raynal ident_done: 3797cfcc706cSMiquel Raynal 3798cfcc706cSMiquel Raynal /* Try to identify manufacturer */ 3799cfcc706cSMiquel Raynal for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) { 3800cfcc706cSMiquel Raynal if (nand_manuf_ids[maf_idx].id == *maf_id) 3801cfcc706cSMiquel Raynal break; 3802cfcc706cSMiquel Raynal } 3803cfcc706cSMiquel Raynal 3804cfcc706cSMiquel Raynal if (chip->options & NAND_BUSWIDTH_AUTO) { 3805cfcc706cSMiquel Raynal WARN_ON(chip->options & NAND_BUSWIDTH_16); 3806cfcc706cSMiquel Raynal chip->options |= busw; 3807cfcc706cSMiquel Raynal nand_set_defaults(chip, busw); 3808cfcc706cSMiquel Raynal } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { 3809cfcc706cSMiquel Raynal /* 3810cfcc706cSMiquel Raynal * Check, if buswidth is correct. Hardware drivers should set 3811cfcc706cSMiquel Raynal * chip correct! 3812cfcc706cSMiquel Raynal */ 3813cfcc706cSMiquel Raynal pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", 3814cfcc706cSMiquel Raynal *maf_id, *dev_id); 3815cfcc706cSMiquel Raynal pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name); 3816cfcc706cSMiquel Raynal pr_warn("bus width %d instead %d bit\n", 3817cfcc706cSMiquel Raynal (chip->options & NAND_BUSWIDTH_16) ? 16 : 8, 3818cfcc706cSMiquel Raynal busw ? 16 : 8); 3819cfcc706cSMiquel Raynal return ERR_PTR(-EINVAL); 3820cfcc706cSMiquel Raynal } 3821cfcc706cSMiquel Raynal 3822cfcc706cSMiquel Raynal nand_decode_bbm_options(mtd, chip, id_data); 3823cfcc706cSMiquel Raynal 3824cfcc706cSMiquel Raynal /* Calculate the address shift from the page size */ 3825cfcc706cSMiquel Raynal chip->page_shift = ffs(mtd->writesize) - 1; 3826cfcc706cSMiquel Raynal /* Convert chipsize to number of pages per chip -1 */ 3827cfcc706cSMiquel Raynal chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; 3828cfcc706cSMiquel Raynal 3829cfcc706cSMiquel Raynal chip->bbt_erase_shift = chip->phys_erase_shift = 3830cfcc706cSMiquel Raynal ffs(mtd->erasesize) - 1; 3831cfcc706cSMiquel Raynal if (chip->chipsize & 0xffffffff) 3832cfcc706cSMiquel Raynal chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; 3833cfcc706cSMiquel Raynal else { 3834cfcc706cSMiquel Raynal chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); 3835cfcc706cSMiquel Raynal chip->chip_shift += 32 - 1; 3836cfcc706cSMiquel Raynal } 3837cfcc706cSMiquel Raynal 3838cfcc706cSMiquel Raynal if (chip->chip_shift - chip->page_shift > 16) 3839cfcc706cSMiquel Raynal chip->options |= NAND_ROW_ADDR_3; 3840cfcc706cSMiquel Raynal 3841cfcc706cSMiquel Raynal chip->badblockbits = 8; 3842cfcc706cSMiquel Raynal chip->erase = single_erase; 3843cfcc706cSMiquel Raynal 3844cfcc706cSMiquel Raynal /* Do not replace user supplied command function! */ 3845cfcc706cSMiquel Raynal if (mtd->writesize > 512 && chip->cmdfunc == nand_command) 3846cfcc706cSMiquel Raynal chip->cmdfunc = nand_command_lp; 3847cfcc706cSMiquel Raynal 3848cfcc706cSMiquel Raynal pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", 3849cfcc706cSMiquel Raynal *maf_id, *dev_id); 3850cfcc706cSMiquel Raynal 3851cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_ONFI_DETECTION 3852cfcc706cSMiquel Raynal if (chip->onfi_version) 3853cfcc706cSMiquel Raynal pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, 3854cfcc706cSMiquel Raynal chip->onfi_params.model); 3855cfcc706cSMiquel Raynal else if (chip->jedec_version) 3856cfcc706cSMiquel Raynal pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, 3857cfcc706cSMiquel Raynal chip->jedec_params.model); 3858cfcc706cSMiquel Raynal else 3859cfcc706cSMiquel Raynal pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, 3860cfcc706cSMiquel Raynal type->name); 3861cfcc706cSMiquel Raynal #else 3862cfcc706cSMiquel Raynal if (chip->jedec_version) 3863cfcc706cSMiquel Raynal pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, 3864cfcc706cSMiquel Raynal chip->jedec_params.model); 3865cfcc706cSMiquel Raynal else 3866cfcc706cSMiquel Raynal pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, 3867cfcc706cSMiquel Raynal type->name); 3868cfcc706cSMiquel Raynal 3869cfcc706cSMiquel Raynal pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, 3870cfcc706cSMiquel Raynal type->name); 3871cfcc706cSMiquel Raynal #endif 3872cfcc706cSMiquel Raynal 3873cfcc706cSMiquel Raynal pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n", 3874cfcc706cSMiquel Raynal (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", 3875cfcc706cSMiquel Raynal mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); 3876cfcc706cSMiquel Raynal return type; 3877cfcc706cSMiquel Raynal } 3878cfcc706cSMiquel Raynal EXPORT_SYMBOL(nand_get_flash_type); 3879cfcc706cSMiquel Raynal 3880cfcc706cSMiquel Raynal #if CONFIG_IS_ENABLED(OF_CONTROL) 3881cfcc706cSMiquel Raynal DECLARE_GLOBAL_DATA_PTR; 3882cfcc706cSMiquel Raynal 3883cfcc706cSMiquel Raynal static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node) 3884cfcc706cSMiquel Raynal { 3885cfcc706cSMiquel Raynal int ret, ecc_mode = -1, ecc_strength, ecc_step; 3886cfcc706cSMiquel Raynal const void *blob = gd->fdt_blob; 3887cfcc706cSMiquel Raynal const char *str; 3888cfcc706cSMiquel Raynal 3889cfcc706cSMiquel Raynal ret = fdtdec_get_int(blob, node, "nand-bus-width", -1); 3890cfcc706cSMiquel Raynal if (ret == 16) 3891cfcc706cSMiquel Raynal chip->options |= NAND_BUSWIDTH_16; 3892cfcc706cSMiquel Raynal 3893cfcc706cSMiquel Raynal if (fdtdec_get_bool(blob, node, "nand-on-flash-bbt")) 3894cfcc706cSMiquel Raynal chip->bbt_options |= NAND_BBT_USE_FLASH; 3895cfcc706cSMiquel Raynal 3896cfcc706cSMiquel Raynal str = fdt_getprop(blob, node, "nand-ecc-mode", NULL); 3897cfcc706cSMiquel Raynal if (str) { 3898cfcc706cSMiquel Raynal if (!strcmp(str, "none")) 3899cfcc706cSMiquel Raynal ecc_mode = NAND_ECC_NONE; 3900cfcc706cSMiquel Raynal else if (!strcmp(str, "soft")) 3901cfcc706cSMiquel Raynal ecc_mode = NAND_ECC_SOFT; 3902cfcc706cSMiquel Raynal else if (!strcmp(str, "hw")) 3903cfcc706cSMiquel Raynal ecc_mode = NAND_ECC_HW; 3904cfcc706cSMiquel Raynal else if (!strcmp(str, "hw_syndrome")) 3905cfcc706cSMiquel Raynal ecc_mode = NAND_ECC_HW_SYNDROME; 3906cfcc706cSMiquel Raynal else if (!strcmp(str, "hw_oob_first")) 3907cfcc706cSMiquel Raynal ecc_mode = NAND_ECC_HW_OOB_FIRST; 3908cfcc706cSMiquel Raynal else if (!strcmp(str, "soft_bch")) 3909cfcc706cSMiquel Raynal ecc_mode = NAND_ECC_SOFT_BCH; 3910cfcc706cSMiquel Raynal } 3911cfcc706cSMiquel Raynal 3912cfcc706cSMiquel Raynal 3913cfcc706cSMiquel Raynal ecc_strength = fdtdec_get_int(blob, node, "nand-ecc-strength", -1); 3914cfcc706cSMiquel Raynal ecc_step = fdtdec_get_int(blob, node, "nand-ecc-step-size", -1); 3915cfcc706cSMiquel Raynal 3916cfcc706cSMiquel Raynal if ((ecc_step >= 0 && !(ecc_strength >= 0)) || 3917cfcc706cSMiquel Raynal (!(ecc_step >= 0) && ecc_strength >= 0)) { 3918cfcc706cSMiquel Raynal pr_err("must set both strength and step size in DT\n"); 3919cfcc706cSMiquel Raynal return -EINVAL; 3920cfcc706cSMiquel Raynal } 3921cfcc706cSMiquel Raynal 3922cfcc706cSMiquel Raynal if (ecc_mode >= 0) 3923cfcc706cSMiquel Raynal chip->ecc.mode = ecc_mode; 3924cfcc706cSMiquel Raynal 3925cfcc706cSMiquel Raynal if (ecc_strength >= 0) 3926cfcc706cSMiquel Raynal chip->ecc.strength = ecc_strength; 3927cfcc706cSMiquel Raynal 3928cfcc706cSMiquel Raynal if (ecc_step > 0) 3929cfcc706cSMiquel Raynal chip->ecc.size = ecc_step; 3930cfcc706cSMiquel Raynal 3931cfcc706cSMiquel Raynal if (fdt_getprop(blob, node, "nand-ecc-maximize", NULL)) 3932cfcc706cSMiquel Raynal chip->ecc.options |= NAND_ECC_MAXIMIZE; 3933cfcc706cSMiquel Raynal 3934cfcc706cSMiquel Raynal return 0; 3935cfcc706cSMiquel Raynal } 3936cfcc706cSMiquel Raynal #else 3937cfcc706cSMiquel Raynal static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node) 3938cfcc706cSMiquel Raynal { 3939cfcc706cSMiquel Raynal return 0; 3940cfcc706cSMiquel Raynal } 3941cfcc706cSMiquel Raynal #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */ 3942cfcc706cSMiquel Raynal 3943cfcc706cSMiquel Raynal /** 3944cfcc706cSMiquel Raynal * nand_scan_ident - [NAND Interface] Scan for the NAND device 3945cfcc706cSMiquel Raynal * @mtd: MTD device structure 3946cfcc706cSMiquel Raynal * @maxchips: number of chips to scan for 3947cfcc706cSMiquel Raynal * @table: alternative NAND ID table 3948cfcc706cSMiquel Raynal * 3949cfcc706cSMiquel Raynal * This is the first phase of the normal nand_scan() function. It reads the 3950cfcc706cSMiquel Raynal * flash ID and sets up MTD fields accordingly. 3951cfcc706cSMiquel Raynal * 3952cfcc706cSMiquel Raynal */ 3953cfcc706cSMiquel Raynal int nand_scan_ident(struct mtd_info *mtd, int maxchips, 3954cfcc706cSMiquel Raynal struct nand_flash_dev *table) 3955cfcc706cSMiquel Raynal { 3956cfcc706cSMiquel Raynal int i, nand_maf_id, nand_dev_id; 3957cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 3958cfcc706cSMiquel Raynal struct nand_flash_dev *type; 3959cfcc706cSMiquel Raynal int ret; 3960cfcc706cSMiquel Raynal 3961cfcc706cSMiquel Raynal if (chip->flash_node) { 3962cfcc706cSMiquel Raynal ret = nand_dt_init(mtd, chip, chip->flash_node); 3963cfcc706cSMiquel Raynal if (ret) 3964cfcc706cSMiquel Raynal return ret; 3965cfcc706cSMiquel Raynal } 3966cfcc706cSMiquel Raynal 3967cfcc706cSMiquel Raynal /* Set the default functions */ 3968cfcc706cSMiquel Raynal nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16); 3969cfcc706cSMiquel Raynal 3970cfcc706cSMiquel Raynal /* Read the flash type */ 3971cfcc706cSMiquel Raynal type = nand_get_flash_type(mtd, chip, &nand_maf_id, 3972cfcc706cSMiquel Raynal &nand_dev_id, table); 3973cfcc706cSMiquel Raynal 3974cfcc706cSMiquel Raynal if (IS_ERR(type)) { 3975cfcc706cSMiquel Raynal if (!(chip->options & NAND_SCAN_SILENT_NODEV)) 3976cfcc706cSMiquel Raynal pr_warn("No NAND device found\n"); 3977cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 3978cfcc706cSMiquel Raynal return PTR_ERR(type); 3979cfcc706cSMiquel Raynal } 3980cfcc706cSMiquel Raynal 3981cfcc706cSMiquel Raynal /* Initialize the ->data_interface field. */ 3982cfcc706cSMiquel Raynal ret = nand_init_data_interface(chip); 3983cfcc706cSMiquel Raynal if (ret) 3984cfcc706cSMiquel Raynal return ret; 3985cfcc706cSMiquel Raynal 3986cfcc706cSMiquel Raynal /* 3987cfcc706cSMiquel Raynal * Setup the data interface correctly on the chip and controller side. 3988cfcc706cSMiquel Raynal * This explicit call to nand_setup_data_interface() is only required 3989cfcc706cSMiquel Raynal * for the first die, because nand_reset() has been called before 3990cfcc706cSMiquel Raynal * ->data_interface and ->default_onfi_timing_mode were set. 3991cfcc706cSMiquel Raynal * For the other dies, nand_reset() will automatically switch to the 3992cfcc706cSMiquel Raynal * best mode for us. 3993cfcc706cSMiquel Raynal */ 3994cfcc706cSMiquel Raynal ret = nand_setup_data_interface(chip, 0); 3995cfcc706cSMiquel Raynal if (ret) 3996cfcc706cSMiquel Raynal return ret; 3997cfcc706cSMiquel Raynal 3998cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 3999cfcc706cSMiquel Raynal 4000cfcc706cSMiquel Raynal /* Check for a chip array */ 4001cfcc706cSMiquel Raynal for (i = 1; i < maxchips; i++) { 4002cfcc706cSMiquel Raynal /* See comment in nand_get_flash_type for reset */ 4003cfcc706cSMiquel Raynal nand_reset(chip, i); 4004cfcc706cSMiquel Raynal 4005cfcc706cSMiquel Raynal chip->select_chip(mtd, i); 4006cfcc706cSMiquel Raynal /* Send the command for reading device ID */ 4007cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); 4008cfcc706cSMiquel Raynal /* Read manufacturer and device IDs */ 4009cfcc706cSMiquel Raynal if (nand_maf_id != chip->read_byte(mtd) || 4010cfcc706cSMiquel Raynal nand_dev_id != chip->read_byte(mtd)) { 4011cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 4012cfcc706cSMiquel Raynal break; 4013cfcc706cSMiquel Raynal } 4014cfcc706cSMiquel Raynal chip->select_chip(mtd, -1); 4015cfcc706cSMiquel Raynal } 4016cfcc706cSMiquel Raynal 4017cfcc706cSMiquel Raynal #ifdef DEBUG 4018cfcc706cSMiquel Raynal if (i > 1) 4019cfcc706cSMiquel Raynal pr_info("%d chips detected\n", i); 4020cfcc706cSMiquel Raynal #endif 4021cfcc706cSMiquel Raynal 4022cfcc706cSMiquel Raynal /* Store the number of chips and calc total size for mtd */ 4023cfcc706cSMiquel Raynal chip->numchips = i; 4024cfcc706cSMiquel Raynal mtd->size = i * chip->chipsize; 4025cfcc706cSMiquel Raynal 4026cfcc706cSMiquel Raynal return 0; 4027cfcc706cSMiquel Raynal } 4028cfcc706cSMiquel Raynal EXPORT_SYMBOL(nand_scan_ident); 4029cfcc706cSMiquel Raynal 4030cfcc706cSMiquel Raynal /** 4031cfcc706cSMiquel Raynal * nand_check_ecc_caps - check the sanity of preset ECC settings 4032cfcc706cSMiquel Raynal * @chip: nand chip info structure 4033cfcc706cSMiquel Raynal * @caps: ECC caps info structure 4034cfcc706cSMiquel Raynal * @oobavail: OOB size that the ECC engine can use 4035cfcc706cSMiquel Raynal * 4036cfcc706cSMiquel Raynal * When ECC step size and strength are already set, check if they are supported 4037cfcc706cSMiquel Raynal * by the controller and the calculated ECC bytes fit within the chip's OOB. 4038cfcc706cSMiquel Raynal * On success, the calculated ECC bytes is set. 4039cfcc706cSMiquel Raynal */ 4040cfcc706cSMiquel Raynal int nand_check_ecc_caps(struct nand_chip *chip, 4041cfcc706cSMiquel Raynal const struct nand_ecc_caps *caps, int oobavail) 4042cfcc706cSMiquel Raynal { 4043cfcc706cSMiquel Raynal struct mtd_info *mtd = nand_to_mtd(chip); 4044cfcc706cSMiquel Raynal const struct nand_ecc_step_info *stepinfo; 4045cfcc706cSMiquel Raynal int preset_step = chip->ecc.size; 4046cfcc706cSMiquel Raynal int preset_strength = chip->ecc.strength; 4047cfcc706cSMiquel Raynal int nsteps, ecc_bytes; 4048cfcc706cSMiquel Raynal int i, j; 4049cfcc706cSMiquel Raynal 4050cfcc706cSMiquel Raynal if (WARN_ON(oobavail < 0)) 4051cfcc706cSMiquel Raynal return -EINVAL; 4052cfcc706cSMiquel Raynal 4053cfcc706cSMiquel Raynal if (!preset_step || !preset_strength) 4054cfcc706cSMiquel Raynal return -ENODATA; 4055cfcc706cSMiquel Raynal 4056cfcc706cSMiquel Raynal nsteps = mtd->writesize / preset_step; 4057cfcc706cSMiquel Raynal 4058cfcc706cSMiquel Raynal for (i = 0; i < caps->nstepinfos; i++) { 4059cfcc706cSMiquel Raynal stepinfo = &caps->stepinfos[i]; 4060cfcc706cSMiquel Raynal 4061cfcc706cSMiquel Raynal if (stepinfo->stepsize != preset_step) 4062cfcc706cSMiquel Raynal continue; 4063cfcc706cSMiquel Raynal 4064cfcc706cSMiquel Raynal for (j = 0; j < stepinfo->nstrengths; j++) { 4065cfcc706cSMiquel Raynal if (stepinfo->strengths[j] != preset_strength) 4066cfcc706cSMiquel Raynal continue; 4067cfcc706cSMiquel Raynal 4068cfcc706cSMiquel Raynal ecc_bytes = caps->calc_ecc_bytes(preset_step, 4069cfcc706cSMiquel Raynal preset_strength); 4070cfcc706cSMiquel Raynal if (WARN_ON_ONCE(ecc_bytes < 0)) 4071cfcc706cSMiquel Raynal return ecc_bytes; 4072cfcc706cSMiquel Raynal 4073cfcc706cSMiquel Raynal if (ecc_bytes * nsteps > oobavail) { 4074cfcc706cSMiquel Raynal pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB", 4075cfcc706cSMiquel Raynal preset_step, preset_strength); 4076cfcc706cSMiquel Raynal return -ENOSPC; 4077cfcc706cSMiquel Raynal } 4078cfcc706cSMiquel Raynal 4079cfcc706cSMiquel Raynal chip->ecc.bytes = ecc_bytes; 4080cfcc706cSMiquel Raynal 4081cfcc706cSMiquel Raynal return 0; 4082cfcc706cSMiquel Raynal } 4083cfcc706cSMiquel Raynal } 4084cfcc706cSMiquel Raynal 4085cfcc706cSMiquel Raynal pr_err("ECC (step, strength) = (%d, %d) not supported on this controller", 4086cfcc706cSMiquel Raynal preset_step, preset_strength); 4087cfcc706cSMiquel Raynal 4088cfcc706cSMiquel Raynal return -ENOTSUPP; 4089cfcc706cSMiquel Raynal } 4090cfcc706cSMiquel Raynal EXPORT_SYMBOL_GPL(nand_check_ecc_caps); 4091cfcc706cSMiquel Raynal 4092cfcc706cSMiquel Raynal /** 4093cfcc706cSMiquel Raynal * nand_match_ecc_req - meet the chip's requirement with least ECC bytes 4094cfcc706cSMiquel Raynal * @chip: nand chip info structure 4095cfcc706cSMiquel Raynal * @caps: ECC engine caps info structure 4096cfcc706cSMiquel Raynal * @oobavail: OOB size that the ECC engine can use 4097cfcc706cSMiquel Raynal * 4098cfcc706cSMiquel Raynal * If a chip's ECC requirement is provided, try to meet it with the least 4099cfcc706cSMiquel Raynal * number of ECC bytes (i.e. with the largest number of OOB-free bytes). 4100cfcc706cSMiquel Raynal * On success, the chosen ECC settings are set. 4101cfcc706cSMiquel Raynal */ 4102cfcc706cSMiquel Raynal int nand_match_ecc_req(struct nand_chip *chip, 4103cfcc706cSMiquel Raynal const struct nand_ecc_caps *caps, int oobavail) 4104cfcc706cSMiquel Raynal { 4105cfcc706cSMiquel Raynal struct mtd_info *mtd = nand_to_mtd(chip); 4106cfcc706cSMiquel Raynal const struct nand_ecc_step_info *stepinfo; 4107cfcc706cSMiquel Raynal int req_step = chip->ecc_step_ds; 4108cfcc706cSMiquel Raynal int req_strength = chip->ecc_strength_ds; 4109cfcc706cSMiquel Raynal int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total; 4110cfcc706cSMiquel Raynal int best_step, best_strength, best_ecc_bytes; 4111cfcc706cSMiquel Raynal int best_ecc_bytes_total = INT_MAX; 4112cfcc706cSMiquel Raynal int i, j; 4113cfcc706cSMiquel Raynal 4114cfcc706cSMiquel Raynal if (WARN_ON(oobavail < 0)) 4115cfcc706cSMiquel Raynal return -EINVAL; 4116cfcc706cSMiquel Raynal 4117cfcc706cSMiquel Raynal /* No information provided by the NAND chip */ 4118cfcc706cSMiquel Raynal if (!req_step || !req_strength) 4119cfcc706cSMiquel Raynal return -ENOTSUPP; 4120cfcc706cSMiquel Raynal 4121cfcc706cSMiquel Raynal /* number of correctable bits the chip requires in a page */ 4122cfcc706cSMiquel Raynal req_corr = mtd->writesize / req_step * req_strength; 4123cfcc706cSMiquel Raynal 4124cfcc706cSMiquel Raynal for (i = 0; i < caps->nstepinfos; i++) { 4125cfcc706cSMiquel Raynal stepinfo = &caps->stepinfos[i]; 4126cfcc706cSMiquel Raynal step_size = stepinfo->stepsize; 4127cfcc706cSMiquel Raynal 4128cfcc706cSMiquel Raynal for (j = 0; j < stepinfo->nstrengths; j++) { 4129cfcc706cSMiquel Raynal strength = stepinfo->strengths[j]; 4130cfcc706cSMiquel Raynal 4131cfcc706cSMiquel Raynal /* 4132cfcc706cSMiquel Raynal * If both step size and strength are smaller than the 4133cfcc706cSMiquel Raynal * chip's requirement, it is not easy to compare the 4134cfcc706cSMiquel Raynal * resulted reliability. 4135cfcc706cSMiquel Raynal */ 4136cfcc706cSMiquel Raynal if (step_size < req_step && strength < req_strength) 4137cfcc706cSMiquel Raynal continue; 4138cfcc706cSMiquel Raynal 4139cfcc706cSMiquel Raynal if (mtd->writesize % step_size) 4140cfcc706cSMiquel Raynal continue; 4141cfcc706cSMiquel Raynal 4142cfcc706cSMiquel Raynal nsteps = mtd->writesize / step_size; 4143cfcc706cSMiquel Raynal 4144cfcc706cSMiquel Raynal ecc_bytes = caps->calc_ecc_bytes(step_size, strength); 4145cfcc706cSMiquel Raynal if (WARN_ON_ONCE(ecc_bytes < 0)) 4146cfcc706cSMiquel Raynal continue; 4147cfcc706cSMiquel Raynal ecc_bytes_total = ecc_bytes * nsteps; 4148cfcc706cSMiquel Raynal 4149cfcc706cSMiquel Raynal if (ecc_bytes_total > oobavail || 4150cfcc706cSMiquel Raynal strength * nsteps < req_corr) 4151cfcc706cSMiquel Raynal continue; 4152cfcc706cSMiquel Raynal 4153cfcc706cSMiquel Raynal /* 4154cfcc706cSMiquel Raynal * We assume the best is to meet the chip's requrement 4155cfcc706cSMiquel Raynal * with the least number of ECC bytes. 4156cfcc706cSMiquel Raynal */ 4157cfcc706cSMiquel Raynal if (ecc_bytes_total < best_ecc_bytes_total) { 4158cfcc706cSMiquel Raynal best_ecc_bytes_total = ecc_bytes_total; 4159cfcc706cSMiquel Raynal best_step = step_size; 4160cfcc706cSMiquel Raynal best_strength = strength; 4161cfcc706cSMiquel Raynal best_ecc_bytes = ecc_bytes; 4162cfcc706cSMiquel Raynal } 4163cfcc706cSMiquel Raynal } 4164cfcc706cSMiquel Raynal } 4165cfcc706cSMiquel Raynal 4166cfcc706cSMiquel Raynal if (best_ecc_bytes_total == INT_MAX) 4167cfcc706cSMiquel Raynal return -ENOTSUPP; 4168cfcc706cSMiquel Raynal 4169cfcc706cSMiquel Raynal chip->ecc.size = best_step; 4170cfcc706cSMiquel Raynal chip->ecc.strength = best_strength; 4171cfcc706cSMiquel Raynal chip->ecc.bytes = best_ecc_bytes; 4172cfcc706cSMiquel Raynal 4173cfcc706cSMiquel Raynal return 0; 4174cfcc706cSMiquel Raynal } 4175cfcc706cSMiquel Raynal EXPORT_SYMBOL_GPL(nand_match_ecc_req); 4176cfcc706cSMiquel Raynal 4177cfcc706cSMiquel Raynal /** 4178cfcc706cSMiquel Raynal * nand_maximize_ecc - choose the max ECC strength available 4179cfcc706cSMiquel Raynal * @chip: nand chip info structure 4180cfcc706cSMiquel Raynal * @caps: ECC engine caps info structure 4181cfcc706cSMiquel Raynal * @oobavail: OOB size that the ECC engine can use 4182cfcc706cSMiquel Raynal * 4183cfcc706cSMiquel Raynal * Choose the max ECC strength that is supported on the controller, and can fit 4184cfcc706cSMiquel Raynal * within the chip's OOB. On success, the chosen ECC settings are set. 4185cfcc706cSMiquel Raynal */ 4186cfcc706cSMiquel Raynal int nand_maximize_ecc(struct nand_chip *chip, 4187cfcc706cSMiquel Raynal const struct nand_ecc_caps *caps, int oobavail) 4188cfcc706cSMiquel Raynal { 4189cfcc706cSMiquel Raynal struct mtd_info *mtd = nand_to_mtd(chip); 4190cfcc706cSMiquel Raynal const struct nand_ecc_step_info *stepinfo; 4191cfcc706cSMiquel Raynal int step_size, strength, nsteps, ecc_bytes, corr; 4192cfcc706cSMiquel Raynal int best_corr = 0; 4193cfcc706cSMiquel Raynal int best_step = 0; 4194cfcc706cSMiquel Raynal int best_strength, best_ecc_bytes; 4195cfcc706cSMiquel Raynal int i, j; 4196cfcc706cSMiquel Raynal 4197cfcc706cSMiquel Raynal if (WARN_ON(oobavail < 0)) 4198cfcc706cSMiquel Raynal return -EINVAL; 4199cfcc706cSMiquel Raynal 4200cfcc706cSMiquel Raynal for (i = 0; i < caps->nstepinfos; i++) { 4201cfcc706cSMiquel Raynal stepinfo = &caps->stepinfos[i]; 4202cfcc706cSMiquel Raynal step_size = stepinfo->stepsize; 4203cfcc706cSMiquel Raynal 4204cfcc706cSMiquel Raynal /* If chip->ecc.size is already set, respect it */ 4205cfcc706cSMiquel Raynal if (chip->ecc.size && step_size != chip->ecc.size) 4206cfcc706cSMiquel Raynal continue; 4207cfcc706cSMiquel Raynal 4208cfcc706cSMiquel Raynal for (j = 0; j < stepinfo->nstrengths; j++) { 4209cfcc706cSMiquel Raynal strength = stepinfo->strengths[j]; 4210cfcc706cSMiquel Raynal 4211cfcc706cSMiquel Raynal if (mtd->writesize % step_size) 4212cfcc706cSMiquel Raynal continue; 4213cfcc706cSMiquel Raynal 4214cfcc706cSMiquel Raynal nsteps = mtd->writesize / step_size; 4215cfcc706cSMiquel Raynal 4216cfcc706cSMiquel Raynal ecc_bytes = caps->calc_ecc_bytes(step_size, strength); 4217cfcc706cSMiquel Raynal if (WARN_ON_ONCE(ecc_bytes < 0)) 4218cfcc706cSMiquel Raynal continue; 4219cfcc706cSMiquel Raynal 4220cfcc706cSMiquel Raynal if (ecc_bytes * nsteps > oobavail) 4221cfcc706cSMiquel Raynal continue; 4222cfcc706cSMiquel Raynal 4223cfcc706cSMiquel Raynal corr = strength * nsteps; 4224cfcc706cSMiquel Raynal 4225cfcc706cSMiquel Raynal /* 4226cfcc706cSMiquel Raynal * If the number of correctable bits is the same, 4227cfcc706cSMiquel Raynal * bigger step_size has more reliability. 4228cfcc706cSMiquel Raynal */ 4229cfcc706cSMiquel Raynal if (corr > best_corr || 4230cfcc706cSMiquel Raynal (corr == best_corr && step_size > best_step)) { 4231cfcc706cSMiquel Raynal best_corr = corr; 4232cfcc706cSMiquel Raynal best_step = step_size; 4233cfcc706cSMiquel Raynal best_strength = strength; 4234cfcc706cSMiquel Raynal best_ecc_bytes = ecc_bytes; 4235cfcc706cSMiquel Raynal } 4236cfcc706cSMiquel Raynal } 4237cfcc706cSMiquel Raynal } 4238cfcc706cSMiquel Raynal 4239cfcc706cSMiquel Raynal if (!best_corr) 4240cfcc706cSMiquel Raynal return -ENOTSUPP; 4241cfcc706cSMiquel Raynal 4242cfcc706cSMiquel Raynal chip->ecc.size = best_step; 4243cfcc706cSMiquel Raynal chip->ecc.strength = best_strength; 4244cfcc706cSMiquel Raynal chip->ecc.bytes = best_ecc_bytes; 4245cfcc706cSMiquel Raynal 4246cfcc706cSMiquel Raynal return 0; 4247cfcc706cSMiquel Raynal } 4248cfcc706cSMiquel Raynal EXPORT_SYMBOL_GPL(nand_maximize_ecc); 4249cfcc706cSMiquel Raynal 4250cfcc706cSMiquel Raynal /* 4251cfcc706cSMiquel Raynal * Check if the chip configuration meet the datasheet requirements. 4252cfcc706cSMiquel Raynal 4253cfcc706cSMiquel Raynal * If our configuration corrects A bits per B bytes and the minimum 4254cfcc706cSMiquel Raynal * required correction level is X bits per Y bytes, then we must ensure 4255cfcc706cSMiquel Raynal * both of the following are true: 4256cfcc706cSMiquel Raynal * 4257cfcc706cSMiquel Raynal * (1) A / B >= X / Y 4258cfcc706cSMiquel Raynal * (2) A >= X 4259cfcc706cSMiquel Raynal * 4260cfcc706cSMiquel Raynal * Requirement (1) ensures we can correct for the required bitflip density. 4261cfcc706cSMiquel Raynal * Requirement (2) ensures we can correct even when all bitflips are clumped 4262cfcc706cSMiquel Raynal * in the same sector. 4263cfcc706cSMiquel Raynal */ 4264cfcc706cSMiquel Raynal static bool nand_ecc_strength_good(struct mtd_info *mtd) 4265cfcc706cSMiquel Raynal { 4266cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 4267cfcc706cSMiquel Raynal struct nand_ecc_ctrl *ecc = &chip->ecc; 4268cfcc706cSMiquel Raynal int corr, ds_corr; 4269cfcc706cSMiquel Raynal 4270cfcc706cSMiquel Raynal if (ecc->size == 0 || chip->ecc_step_ds == 0) 4271cfcc706cSMiquel Raynal /* Not enough information */ 4272cfcc706cSMiquel Raynal return true; 4273cfcc706cSMiquel Raynal 4274cfcc706cSMiquel Raynal /* 4275cfcc706cSMiquel Raynal * We get the number of corrected bits per page to compare 4276cfcc706cSMiquel Raynal * the correction density. 4277cfcc706cSMiquel Raynal */ 4278cfcc706cSMiquel Raynal corr = (mtd->writesize * ecc->strength) / ecc->size; 4279cfcc706cSMiquel Raynal ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; 4280cfcc706cSMiquel Raynal 4281cfcc706cSMiquel Raynal return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; 4282cfcc706cSMiquel Raynal } 4283cfcc706cSMiquel Raynal 4284cfcc706cSMiquel Raynal static bool invalid_ecc_page_accessors(struct nand_chip *chip) 4285cfcc706cSMiquel Raynal { 4286cfcc706cSMiquel Raynal struct nand_ecc_ctrl *ecc = &chip->ecc; 4287cfcc706cSMiquel Raynal 4288cfcc706cSMiquel Raynal if (nand_standard_page_accessors(ecc)) 4289cfcc706cSMiquel Raynal return false; 4290cfcc706cSMiquel Raynal 4291cfcc706cSMiquel Raynal /* 4292cfcc706cSMiquel Raynal * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND 4293cfcc706cSMiquel Raynal * controller driver implements all the page accessors because 4294cfcc706cSMiquel Raynal * default helpers are not suitable when the core does not 4295cfcc706cSMiquel Raynal * send the READ0/PAGEPROG commands. 4296cfcc706cSMiquel Raynal */ 4297cfcc706cSMiquel Raynal return (!ecc->read_page || !ecc->write_page || 4298cfcc706cSMiquel Raynal !ecc->read_page_raw || !ecc->write_page_raw || 4299cfcc706cSMiquel Raynal (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) || 4300cfcc706cSMiquel Raynal (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage && 4301cfcc706cSMiquel Raynal ecc->hwctl && ecc->calculate)); 4302cfcc706cSMiquel Raynal } 4303cfcc706cSMiquel Raynal 4304cfcc706cSMiquel Raynal /** 4305cfcc706cSMiquel Raynal * nand_scan_tail - [NAND Interface] Scan for the NAND device 4306cfcc706cSMiquel Raynal * @mtd: MTD device structure 4307cfcc706cSMiquel Raynal * 4308cfcc706cSMiquel Raynal * This is the second phase of the normal nand_scan() function. It fills out 4309cfcc706cSMiquel Raynal * all the uninitialized function pointers with the defaults and scans for a 4310cfcc706cSMiquel Raynal * bad block table if appropriate. 4311cfcc706cSMiquel Raynal */ 4312cfcc706cSMiquel Raynal int nand_scan_tail(struct mtd_info *mtd) 4313cfcc706cSMiquel Raynal { 4314cfcc706cSMiquel Raynal int i; 4315cfcc706cSMiquel Raynal struct nand_chip *chip = mtd_to_nand(mtd); 4316cfcc706cSMiquel Raynal struct nand_ecc_ctrl *ecc = &chip->ecc; 4317cfcc706cSMiquel Raynal struct nand_buffers *nbuf; 4318cfcc706cSMiquel Raynal 4319cfcc706cSMiquel Raynal /* New bad blocks should be marked in OOB, flash-based BBT, or both */ 4320cfcc706cSMiquel Raynal BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && 4321cfcc706cSMiquel Raynal !(chip->bbt_options & NAND_BBT_USE_FLASH)); 4322cfcc706cSMiquel Raynal 4323cfcc706cSMiquel Raynal if (invalid_ecc_page_accessors(chip)) { 4324cfcc706cSMiquel Raynal pr_err("Invalid ECC page accessors setup\n"); 4325cfcc706cSMiquel Raynal return -EINVAL; 4326cfcc706cSMiquel Raynal } 4327cfcc706cSMiquel Raynal 4328cfcc706cSMiquel Raynal if (!(chip->options & NAND_OWN_BUFFERS)) { 4329cfcc706cSMiquel Raynal nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL); 4330cfcc706cSMiquel Raynal chip->buffers = nbuf; 4331cfcc706cSMiquel Raynal } else { 4332cfcc706cSMiquel Raynal if (!chip->buffers) 4333cfcc706cSMiquel Raynal return -ENOMEM; 4334cfcc706cSMiquel Raynal } 4335cfcc706cSMiquel Raynal 4336cfcc706cSMiquel Raynal /* Set the internal oob buffer location, just after the page data */ 4337cfcc706cSMiquel Raynal chip->oob_poi = chip->buffers->databuf + mtd->writesize; 4338cfcc706cSMiquel Raynal 4339cfcc706cSMiquel Raynal /* 4340cfcc706cSMiquel Raynal * If no default placement scheme is given, select an appropriate one. 4341cfcc706cSMiquel Raynal */ 4342cfcc706cSMiquel Raynal if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) { 4343cfcc706cSMiquel Raynal switch (mtd->oobsize) { 4344*50c9e2f7SStefan Agner #ifdef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT 4345cfcc706cSMiquel Raynal case 8: 4346cfcc706cSMiquel Raynal ecc->layout = &nand_oob_8; 4347cfcc706cSMiquel Raynal break; 4348cfcc706cSMiquel Raynal case 16: 4349cfcc706cSMiquel Raynal ecc->layout = &nand_oob_16; 4350cfcc706cSMiquel Raynal break; 4351cfcc706cSMiquel Raynal case 64: 4352cfcc706cSMiquel Raynal ecc->layout = &nand_oob_64; 4353cfcc706cSMiquel Raynal break; 4354cfcc706cSMiquel Raynal case 128: 4355cfcc706cSMiquel Raynal ecc->layout = &nand_oob_128; 4356cfcc706cSMiquel Raynal break; 4357*50c9e2f7SStefan Agner #endif 4358cfcc706cSMiquel Raynal default: 4359cfcc706cSMiquel Raynal pr_warn("No oob scheme defined for oobsize %d\n", 4360cfcc706cSMiquel Raynal mtd->oobsize); 4361cfcc706cSMiquel Raynal BUG(); 4362cfcc706cSMiquel Raynal } 4363cfcc706cSMiquel Raynal } 4364cfcc706cSMiquel Raynal 4365cfcc706cSMiquel Raynal if (!chip->write_page) 4366cfcc706cSMiquel Raynal chip->write_page = nand_write_page; 4367cfcc706cSMiquel Raynal 4368cfcc706cSMiquel Raynal /* 4369cfcc706cSMiquel Raynal * Check ECC mode, default to software if 3byte/512byte hardware ECC is 4370cfcc706cSMiquel Raynal * selected and we have 256 byte pagesize fallback to software ECC 4371cfcc706cSMiquel Raynal */ 4372cfcc706cSMiquel Raynal 4373cfcc706cSMiquel Raynal switch (ecc->mode) { 4374cfcc706cSMiquel Raynal case NAND_ECC_HW_OOB_FIRST: 4375cfcc706cSMiquel Raynal /* Similar to NAND_ECC_HW, but a separate read_page handle */ 4376cfcc706cSMiquel Raynal if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { 4377cfcc706cSMiquel Raynal pr_warn("No ECC functions supplied; hardware ECC not possible\n"); 4378cfcc706cSMiquel Raynal BUG(); 4379cfcc706cSMiquel Raynal } 4380cfcc706cSMiquel Raynal if (!ecc->read_page) 4381cfcc706cSMiquel Raynal ecc->read_page = nand_read_page_hwecc_oob_first; 4382cfcc706cSMiquel Raynal 4383cfcc706cSMiquel Raynal case NAND_ECC_HW: 4384cfcc706cSMiquel Raynal /* Use standard hwecc read page function? */ 4385cfcc706cSMiquel Raynal if (!ecc->read_page) 4386cfcc706cSMiquel Raynal ecc->read_page = nand_read_page_hwecc; 4387cfcc706cSMiquel Raynal if (!ecc->write_page) 4388cfcc706cSMiquel Raynal ecc->write_page = nand_write_page_hwecc; 4389cfcc706cSMiquel Raynal if (!ecc->read_page_raw) 4390cfcc706cSMiquel Raynal ecc->read_page_raw = nand_read_page_raw; 4391cfcc706cSMiquel Raynal if (!ecc->write_page_raw) 4392cfcc706cSMiquel Raynal ecc->write_page_raw = nand_write_page_raw; 4393cfcc706cSMiquel Raynal if (!ecc->read_oob) 4394cfcc706cSMiquel Raynal ecc->read_oob = nand_read_oob_std; 4395cfcc706cSMiquel Raynal if (!ecc->write_oob) 4396cfcc706cSMiquel Raynal ecc->write_oob = nand_write_oob_std; 4397cfcc706cSMiquel Raynal if (!ecc->read_subpage) 4398cfcc706cSMiquel Raynal ecc->read_subpage = nand_read_subpage; 4399cfcc706cSMiquel Raynal if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) 4400cfcc706cSMiquel Raynal ecc->write_subpage = nand_write_subpage_hwecc; 4401cfcc706cSMiquel Raynal 4402cfcc706cSMiquel Raynal case NAND_ECC_HW_SYNDROME: 4403cfcc706cSMiquel Raynal if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && 4404cfcc706cSMiquel Raynal (!ecc->read_page || 4405cfcc706cSMiquel Raynal ecc->read_page == nand_read_page_hwecc || 4406cfcc706cSMiquel Raynal !ecc->write_page || 4407cfcc706cSMiquel Raynal ecc->write_page == nand_write_page_hwecc)) { 4408cfcc706cSMiquel Raynal pr_warn("No ECC functions supplied; hardware ECC not possible\n"); 4409cfcc706cSMiquel Raynal BUG(); 4410cfcc706cSMiquel Raynal } 4411cfcc706cSMiquel Raynal /* Use standard syndrome read/write page function? */ 4412cfcc706cSMiquel Raynal if (!ecc->read_page) 4413cfcc706cSMiquel Raynal ecc->read_page = nand_read_page_syndrome; 4414cfcc706cSMiquel Raynal if (!ecc->write_page) 4415cfcc706cSMiquel Raynal ecc->write_page = nand_write_page_syndrome; 4416cfcc706cSMiquel Raynal if (!ecc->read_page_raw) 4417cfcc706cSMiquel Raynal ecc->read_page_raw = nand_read_page_raw_syndrome; 4418cfcc706cSMiquel Raynal if (!ecc->write_page_raw) 4419cfcc706cSMiquel Raynal ecc->write_page_raw = nand_write_page_raw_syndrome; 4420cfcc706cSMiquel Raynal if (!ecc->read_oob) 4421cfcc706cSMiquel Raynal ecc->read_oob = nand_read_oob_syndrome; 4422cfcc706cSMiquel Raynal if (!ecc->write_oob) 4423cfcc706cSMiquel Raynal ecc->write_oob = nand_write_oob_syndrome; 4424cfcc706cSMiquel Raynal 4425cfcc706cSMiquel Raynal if (mtd->writesize >= ecc->size) { 4426cfcc706cSMiquel Raynal if (!ecc->strength) { 4427cfcc706cSMiquel Raynal pr_warn("Driver must set ecc.strength when using hardware ECC\n"); 4428cfcc706cSMiquel Raynal BUG(); 4429cfcc706cSMiquel Raynal } 4430cfcc706cSMiquel Raynal break; 4431cfcc706cSMiquel Raynal } 4432cfcc706cSMiquel Raynal pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", 4433cfcc706cSMiquel Raynal ecc->size, mtd->writesize); 4434cfcc706cSMiquel Raynal ecc->mode = NAND_ECC_SOFT; 4435cfcc706cSMiquel Raynal 4436cfcc706cSMiquel Raynal case NAND_ECC_SOFT: 4437cfcc706cSMiquel Raynal ecc->calculate = nand_calculate_ecc; 4438cfcc706cSMiquel Raynal ecc->correct = nand_correct_data; 4439cfcc706cSMiquel Raynal ecc->read_page = nand_read_page_swecc; 4440cfcc706cSMiquel Raynal ecc->read_subpage = nand_read_subpage; 4441cfcc706cSMiquel Raynal ecc->write_page = nand_write_page_swecc; 4442cfcc706cSMiquel Raynal ecc->read_page_raw = nand_read_page_raw; 4443cfcc706cSMiquel Raynal ecc->write_page_raw = nand_write_page_raw; 4444cfcc706cSMiquel Raynal ecc->read_oob = nand_read_oob_std; 4445cfcc706cSMiquel Raynal ecc->write_oob = nand_write_oob_std; 4446cfcc706cSMiquel Raynal if (!ecc->size) 4447cfcc706cSMiquel Raynal ecc->size = 256; 4448cfcc706cSMiquel Raynal ecc->bytes = 3; 4449cfcc706cSMiquel Raynal ecc->strength = 1; 4450cfcc706cSMiquel Raynal break; 4451cfcc706cSMiquel Raynal 4452cfcc706cSMiquel Raynal case NAND_ECC_SOFT_BCH: 4453cfcc706cSMiquel Raynal if (!mtd_nand_has_bch()) { 4454cfcc706cSMiquel Raynal pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n"); 4455cfcc706cSMiquel Raynal BUG(); 4456cfcc706cSMiquel Raynal } 4457cfcc706cSMiquel Raynal ecc->calculate = nand_bch_calculate_ecc; 4458cfcc706cSMiquel Raynal ecc->correct = nand_bch_correct_data; 4459cfcc706cSMiquel Raynal ecc->read_page = nand_read_page_swecc; 4460cfcc706cSMiquel Raynal ecc->read_subpage = nand_read_subpage; 4461cfcc706cSMiquel Raynal ecc->write_page = nand_write_page_swecc; 4462cfcc706cSMiquel Raynal ecc->read_page_raw = nand_read_page_raw; 4463cfcc706cSMiquel Raynal ecc->write_page_raw = nand_write_page_raw; 4464cfcc706cSMiquel Raynal ecc->read_oob = nand_read_oob_std; 4465cfcc706cSMiquel Raynal ecc->write_oob = nand_write_oob_std; 4466cfcc706cSMiquel Raynal /* 4467cfcc706cSMiquel Raynal * Board driver should supply ecc.size and ecc.strength values 4468cfcc706cSMiquel Raynal * to select how many bits are correctable. Otherwise, default 4469cfcc706cSMiquel Raynal * to 4 bits for large page devices. 4470cfcc706cSMiquel Raynal */ 4471cfcc706cSMiquel Raynal if (!ecc->size && (mtd->oobsize >= 64)) { 4472cfcc706cSMiquel Raynal ecc->size = 512; 4473cfcc706cSMiquel Raynal ecc->strength = 4; 4474cfcc706cSMiquel Raynal } 4475cfcc706cSMiquel Raynal 4476cfcc706cSMiquel Raynal /* See nand_bch_init() for details. */ 4477cfcc706cSMiquel Raynal ecc->bytes = 0; 4478cfcc706cSMiquel Raynal ecc->priv = nand_bch_init(mtd); 4479cfcc706cSMiquel Raynal if (!ecc->priv) { 4480cfcc706cSMiquel Raynal pr_warn("BCH ECC initialization failed!\n"); 4481cfcc706cSMiquel Raynal BUG(); 4482cfcc706cSMiquel Raynal } 4483cfcc706cSMiquel Raynal break; 4484cfcc706cSMiquel Raynal 4485cfcc706cSMiquel Raynal case NAND_ECC_NONE: 4486cfcc706cSMiquel Raynal pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n"); 4487cfcc706cSMiquel Raynal ecc->read_page = nand_read_page_raw; 4488cfcc706cSMiquel Raynal ecc->write_page = nand_write_page_raw; 4489cfcc706cSMiquel Raynal ecc->read_oob = nand_read_oob_std; 4490cfcc706cSMiquel Raynal ecc->read_page_raw = nand_read_page_raw; 4491cfcc706cSMiquel Raynal ecc->write_page_raw = nand_write_page_raw; 4492cfcc706cSMiquel Raynal ecc->write_oob = nand_write_oob_std; 4493cfcc706cSMiquel Raynal ecc->size = mtd->writesize; 4494cfcc706cSMiquel Raynal ecc->bytes = 0; 4495cfcc706cSMiquel Raynal ecc->strength = 0; 4496cfcc706cSMiquel Raynal break; 4497cfcc706cSMiquel Raynal 4498cfcc706cSMiquel Raynal default: 4499cfcc706cSMiquel Raynal pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode); 4500cfcc706cSMiquel Raynal BUG(); 4501cfcc706cSMiquel Raynal } 4502cfcc706cSMiquel Raynal 4503cfcc706cSMiquel Raynal /* For many systems, the standard OOB write also works for raw */ 4504cfcc706cSMiquel Raynal if (!ecc->read_oob_raw) 4505cfcc706cSMiquel Raynal ecc->read_oob_raw = ecc->read_oob; 4506cfcc706cSMiquel Raynal if (!ecc->write_oob_raw) 4507cfcc706cSMiquel Raynal ecc->write_oob_raw = ecc->write_oob; 4508cfcc706cSMiquel Raynal 4509cfcc706cSMiquel Raynal /* 4510cfcc706cSMiquel Raynal * The number of bytes available for a client to place data into 4511cfcc706cSMiquel Raynal * the out of band area. 4512cfcc706cSMiquel Raynal */ 4513cfcc706cSMiquel Raynal mtd->oobavail = 0; 4514cfcc706cSMiquel Raynal if (ecc->layout) { 4515cfcc706cSMiquel Raynal for (i = 0; ecc->layout->oobfree[i].length; i++) 4516cfcc706cSMiquel Raynal mtd->oobavail += ecc->layout->oobfree[i].length; 4517cfcc706cSMiquel Raynal } 4518cfcc706cSMiquel Raynal 4519cfcc706cSMiquel Raynal /* ECC sanity check: warn if it's too weak */ 4520cfcc706cSMiquel Raynal if (!nand_ecc_strength_good(mtd)) 4521cfcc706cSMiquel Raynal pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", 4522cfcc706cSMiquel Raynal mtd->name); 4523cfcc706cSMiquel Raynal 4524cfcc706cSMiquel Raynal /* 4525cfcc706cSMiquel Raynal * Set the number of read / write steps for one page depending on ECC 4526cfcc706cSMiquel Raynal * mode. 4527cfcc706cSMiquel Raynal */ 4528cfcc706cSMiquel Raynal ecc->steps = mtd->writesize / ecc->size; 4529cfcc706cSMiquel Raynal if (ecc->steps * ecc->size != mtd->writesize) { 4530cfcc706cSMiquel Raynal pr_warn("Invalid ECC parameters\n"); 4531cfcc706cSMiquel Raynal BUG(); 4532cfcc706cSMiquel Raynal } 4533cfcc706cSMiquel Raynal ecc->total = ecc->steps * ecc->bytes; 4534cfcc706cSMiquel Raynal 4535cfcc706cSMiquel Raynal /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ 4536cfcc706cSMiquel Raynal if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { 4537cfcc706cSMiquel Raynal switch (ecc->steps) { 4538cfcc706cSMiquel Raynal case 2: 4539cfcc706cSMiquel Raynal mtd->subpage_sft = 1; 4540cfcc706cSMiquel Raynal break; 4541cfcc706cSMiquel Raynal case 4: 4542cfcc706cSMiquel Raynal case 8: 4543cfcc706cSMiquel Raynal case 16: 4544cfcc706cSMiquel Raynal mtd->subpage_sft = 2; 4545cfcc706cSMiquel Raynal break; 4546cfcc706cSMiquel Raynal } 4547cfcc706cSMiquel Raynal } 4548cfcc706cSMiquel Raynal chip->subpagesize = mtd->writesize >> mtd->subpage_sft; 4549cfcc706cSMiquel Raynal 4550cfcc706cSMiquel Raynal /* Initialize state */ 4551cfcc706cSMiquel Raynal chip->state = FL_READY; 4552cfcc706cSMiquel Raynal 4553cfcc706cSMiquel Raynal /* Invalidate the pagebuffer reference */ 4554cfcc706cSMiquel Raynal chip->pagebuf = -1; 4555cfcc706cSMiquel Raynal 4556cfcc706cSMiquel Raynal /* Large page NAND with SOFT_ECC should support subpage reads */ 4557cfcc706cSMiquel Raynal switch (ecc->mode) { 4558cfcc706cSMiquel Raynal case NAND_ECC_SOFT: 4559cfcc706cSMiquel Raynal case NAND_ECC_SOFT_BCH: 4560cfcc706cSMiquel Raynal if (chip->page_shift > 9) 4561cfcc706cSMiquel Raynal chip->options |= NAND_SUBPAGE_READ; 4562cfcc706cSMiquel Raynal break; 4563cfcc706cSMiquel Raynal 4564cfcc706cSMiquel Raynal default: 4565cfcc706cSMiquel Raynal break; 4566cfcc706cSMiquel Raynal } 4567cfcc706cSMiquel Raynal 4568cfcc706cSMiquel Raynal /* Fill in remaining MTD driver data */ 4569cfcc706cSMiquel Raynal mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; 4570cfcc706cSMiquel Raynal mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : 4571cfcc706cSMiquel Raynal MTD_CAP_NANDFLASH; 4572cfcc706cSMiquel Raynal mtd->_erase = nand_erase; 4573cfcc706cSMiquel Raynal mtd->_panic_write = panic_nand_write; 4574cfcc706cSMiquel Raynal mtd->_read_oob = nand_read_oob; 4575cfcc706cSMiquel Raynal mtd->_write_oob = nand_write_oob; 4576cfcc706cSMiquel Raynal mtd->_sync = nand_sync; 4577cfcc706cSMiquel Raynal mtd->_lock = NULL; 4578cfcc706cSMiquel Raynal mtd->_unlock = NULL; 4579cfcc706cSMiquel Raynal mtd->_block_isreserved = nand_block_isreserved; 4580cfcc706cSMiquel Raynal mtd->_block_isbad = nand_block_isbad; 4581cfcc706cSMiquel Raynal mtd->_block_markbad = nand_block_markbad; 4582cfcc706cSMiquel Raynal mtd->writebufsize = mtd->writesize; 4583cfcc706cSMiquel Raynal 4584cfcc706cSMiquel Raynal /* propagate ecc info to mtd_info */ 4585cfcc706cSMiquel Raynal mtd->ecclayout = ecc->layout; 4586cfcc706cSMiquel Raynal mtd->ecc_strength = ecc->strength; 4587cfcc706cSMiquel Raynal mtd->ecc_step_size = ecc->size; 4588cfcc706cSMiquel Raynal /* 4589cfcc706cSMiquel Raynal * Initialize bitflip_threshold to its default prior scan_bbt() call. 4590cfcc706cSMiquel Raynal * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be 4591cfcc706cSMiquel Raynal * properly set. 4592cfcc706cSMiquel Raynal */ 4593cfcc706cSMiquel Raynal if (!mtd->bitflip_threshold) 4594cfcc706cSMiquel Raynal mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); 4595cfcc706cSMiquel Raynal 4596cfcc706cSMiquel Raynal return 0; 4597cfcc706cSMiquel Raynal } 4598cfcc706cSMiquel Raynal EXPORT_SYMBOL(nand_scan_tail); 4599cfcc706cSMiquel Raynal 4600cfcc706cSMiquel Raynal /** 4601cfcc706cSMiquel Raynal * nand_scan - [NAND Interface] Scan for the NAND device 4602cfcc706cSMiquel Raynal * @mtd: MTD device structure 4603cfcc706cSMiquel Raynal * @maxchips: number of chips to scan for 4604cfcc706cSMiquel Raynal * 4605cfcc706cSMiquel Raynal * This fills out all the uninitialized function pointers with the defaults. 4606cfcc706cSMiquel Raynal * The flash ID is read and the mtd/chip structures are filled with the 4607cfcc706cSMiquel Raynal * appropriate values. 4608cfcc706cSMiquel Raynal */ 4609cfcc706cSMiquel Raynal int nand_scan(struct mtd_info *mtd, int maxchips) 4610cfcc706cSMiquel Raynal { 4611cfcc706cSMiquel Raynal int ret; 4612cfcc706cSMiquel Raynal 4613cfcc706cSMiquel Raynal ret = nand_scan_ident(mtd, maxchips, NULL); 4614cfcc706cSMiquel Raynal if (!ret) 4615cfcc706cSMiquel Raynal ret = nand_scan_tail(mtd); 4616cfcc706cSMiquel Raynal return ret; 4617cfcc706cSMiquel Raynal } 4618cfcc706cSMiquel Raynal EXPORT_SYMBOL(nand_scan); 4619cfcc706cSMiquel Raynal 4620cfcc706cSMiquel Raynal MODULE_LICENSE("GPL"); 4621cfcc706cSMiquel Raynal MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); 4622cfcc706cSMiquel Raynal MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); 4623cfcc706cSMiquel Raynal MODULE_DESCRIPTION("Generic NAND flash driver code"); 4624