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