Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Overview: |
| 3 | * This is the generic MTD driver for NAND flash devices. It should be |
| 4 | * capable of working with almost all NAND chips currently available. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Additional technical information is available on |
maximilian attems | 8b2b403 | 2007-07-28 13:07:16 +0200 | [diff] [blame] | 7 | * http://www.linux-mtd.infradead.org/doc/nand.html |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 10 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 12 | * Credits: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 13 | * David Woodhouse for adding multichip support |
| 14 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
| 16 | * rework for 2K page size chips |
| 17 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 18 | * TODO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * Enable cached programming for 2k page size chips |
| 20 | * Check, if mtd->ecctype should be set to MTD_ECC_HW |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 21 | * if we have HW ECC support. |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 22 | * BBT table is not serialized, has to be fixed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * This program is free software; you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License version 2 as |
| 26 | * published by the Free Software Foundation. |
| 27 | * |
| 28 | */ |
| 29 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 31 | |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 32 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/delay.h> |
| 34 | #include <linux/errno.h> |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 35 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/sched.h> |
| 37 | #include <linux/slab.h> |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 38 | #include <linux/mm.h> |
Ingo Molnar | 38b8d20 | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 39 | #include <linux/nmi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/types.h> |
| 41 | #include <linux/mtd/mtd.h> |
Boris Brezillon | d4092d7 | 2017-08-04 17:29:10 +0200 | [diff] [blame] | 42 | #include <linux/mtd/rawnand.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/mtd/nand_ecc.h> |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 44 | #include <linux/mtd/nand_bch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/interrupt.h> |
| 46 | #include <linux/bitops.h> |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 47 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/mtd/partitions.h> |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 49 | #include <linux/of.h> |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 50 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 51 | static int nand_get_device(struct mtd_info *mtd, int new_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 53 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 54 | struct mtd_oob_ops *ops); |
| 55 | |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 56 | /* Define default oob placement schemes for large and small page devices */ |
| 57 | static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section, |
| 58 | struct mtd_oob_region *oobregion) |
| 59 | { |
| 60 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 61 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 62 | |
| 63 | if (section > 1) |
| 64 | return -ERANGE; |
| 65 | |
| 66 | if (!section) { |
| 67 | oobregion->offset = 0; |
Miquel Raynal | f7f8c17 | 2017-07-05 08:51:09 +0200 | [diff] [blame] | 68 | if (mtd->oobsize == 16) |
| 69 | oobregion->length = 4; |
| 70 | else |
| 71 | oobregion->length = 3; |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 72 | } else { |
Miquel Raynal | f7f8c17 | 2017-07-05 08:51:09 +0200 | [diff] [blame] | 73 | if (mtd->oobsize == 8) |
| 74 | return -ERANGE; |
| 75 | |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 76 | oobregion->offset = 6; |
| 77 | oobregion->length = ecc->total - 4; |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section, |
| 84 | struct mtd_oob_region *oobregion) |
| 85 | { |
| 86 | if (section > 1) |
| 87 | return -ERANGE; |
| 88 | |
| 89 | if (mtd->oobsize == 16) { |
| 90 | if (section) |
| 91 | return -ERANGE; |
| 92 | |
| 93 | oobregion->length = 8; |
| 94 | oobregion->offset = 8; |
| 95 | } else { |
| 96 | oobregion->length = 2; |
| 97 | if (!section) |
| 98 | oobregion->offset = 3; |
| 99 | else |
| 100 | oobregion->offset = 6; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = { |
| 107 | .ecc = nand_ooblayout_ecc_sp, |
| 108 | .free = nand_ooblayout_free_sp, |
| 109 | }; |
| 110 | EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops); |
| 111 | |
| 112 | static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section, |
| 113 | struct mtd_oob_region *oobregion) |
| 114 | { |
| 115 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 116 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 117 | |
Miquel Raynal | 882fd15 | 2017-08-26 17:19:15 +0200 | [diff] [blame] | 118 | if (section || !ecc->total) |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 119 | return -ERANGE; |
| 120 | |
| 121 | oobregion->length = ecc->total; |
| 122 | oobregion->offset = mtd->oobsize - oobregion->length; |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section, |
| 128 | struct mtd_oob_region *oobregion) |
| 129 | { |
| 130 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 131 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 132 | |
| 133 | if (section) |
| 134 | return -ERANGE; |
| 135 | |
| 136 | oobregion->length = mtd->oobsize - ecc->total - 2; |
| 137 | oobregion->offset = 2; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = { |
| 143 | .ecc = nand_ooblayout_ecc_lp, |
| 144 | .free = nand_ooblayout_free_lp, |
| 145 | }; |
| 146 | EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops); |
Thomas Gleixner | d470a97 | 2006-05-23 23:48:57 +0200 | [diff] [blame] | 147 | |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 148 | /* |
| 149 | * Support the old "large page" layout used for 1-bit Hamming ECC where ECC |
| 150 | * are placed at a fixed offset. |
| 151 | */ |
| 152 | static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section, |
| 153 | struct mtd_oob_region *oobregion) |
| 154 | { |
| 155 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 156 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 157 | |
| 158 | if (section) |
| 159 | return -ERANGE; |
| 160 | |
| 161 | switch (mtd->oobsize) { |
| 162 | case 64: |
| 163 | oobregion->offset = 40; |
| 164 | break; |
| 165 | case 128: |
| 166 | oobregion->offset = 80; |
| 167 | break; |
| 168 | default: |
| 169 | return -EINVAL; |
| 170 | } |
| 171 | |
| 172 | oobregion->length = ecc->total; |
| 173 | if (oobregion->offset + oobregion->length > mtd->oobsize) |
| 174 | return -ERANGE; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section, |
| 180 | struct mtd_oob_region *oobregion) |
| 181 | { |
| 182 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 183 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 184 | int ecc_offset = 0; |
| 185 | |
| 186 | if (section < 0 || section > 1) |
| 187 | return -ERANGE; |
| 188 | |
| 189 | switch (mtd->oobsize) { |
| 190 | case 64: |
| 191 | ecc_offset = 40; |
| 192 | break; |
| 193 | case 128: |
| 194 | ecc_offset = 80; |
| 195 | break; |
| 196 | default: |
| 197 | return -EINVAL; |
| 198 | } |
| 199 | |
| 200 | if (section == 0) { |
| 201 | oobregion->offset = 2; |
| 202 | oobregion->length = ecc_offset - 2; |
| 203 | } else { |
| 204 | oobregion->offset = ecc_offset + ecc->total; |
| 205 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Colin Ian King | d4ed3b9 | 2017-05-04 13:11:00 +0100 | [diff] [blame] | 211 | static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = { |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 212 | .ecc = nand_ooblayout_ecc_lp_hamming, |
| 213 | .free = nand_ooblayout_free_lp_hamming, |
| 214 | }; |
| 215 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 216 | static int check_offs_len(struct mtd_info *mtd, |
| 217 | loff_t ofs, uint64_t len) |
| 218 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 219 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 220 | int ret = 0; |
| 221 | |
| 222 | /* Start address must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 223 | if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 224 | pr_debug("%s: unaligned address\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 225 | ret = -EINVAL; |
| 226 | } |
| 227 | |
| 228 | /* Length must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 229 | if (len & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 230 | pr_debug("%s: length not block aligned\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 231 | ret = -EINVAL; |
| 232 | } |
| 233 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 234 | return ret; |
| 235 | } |
| 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | /** |
| 238 | * nand_release_device - [GENERIC] release chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 239 | * @mtd: MTD device structure |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 240 | * |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 241 | * Release chip lock and wake up anyone waiting on the device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 243 | static void nand_release_device(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 245 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 247 | /* Release the controller and the chip */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 248 | spin_lock(&chip->controller->lock); |
| 249 | chip->controller->active = NULL; |
| 250 | chip->state = FL_READY; |
| 251 | wake_up(&chip->controller->wq); |
| 252 | spin_unlock(&chip->controller->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** |
| 256 | * nand_read_byte - [DEFAULT] read one byte from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 257 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 259 | * Default read function for 8bit buswidth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 261 | static uint8_t nand_read_byte(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 263 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 264 | return readb(chip->IO_ADDR_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /** |
Masanari Iida | 064a769 | 2012-11-09 23:20:58 +0900 | [diff] [blame] | 268 | * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 269 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 271 | * Default read function for 16bit buswidth with endianness conversion. |
| 272 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 274 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 276 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 277 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | * nand_read_word - [DEFAULT] read one word from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 282 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 284 | * Default read function for 16bit buswidth without endianness conversion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | */ |
| 286 | static u16 nand_read_word(struct mtd_info *mtd) |
| 287 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 288 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 289 | return readw(chip->IO_ADDR_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | * nand_select_chip - [DEFAULT] control CE line |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 294 | * @mtd: MTD device structure |
| 295 | * @chipnr: chipnumber to select, -1 for deselect |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | * |
| 297 | * Default select function for 1 chip devices. |
| 298 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 299 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 301 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 302 | |
| 303 | switch (chipnr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | case -1: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 305 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | break; |
| 307 | case 0: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | break; |
| 309 | |
| 310 | default: |
| 311 | BUG(); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 316 | * nand_write_byte - [DEFAULT] write single byte to chip |
| 317 | * @mtd: MTD device structure |
| 318 | * @byte: value to write |
| 319 | * |
| 320 | * Default function to write a byte to I/O[7:0] |
| 321 | */ |
| 322 | static void nand_write_byte(struct mtd_info *mtd, uint8_t byte) |
| 323 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 324 | struct nand_chip *chip = mtd_to_nand(mtd); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 325 | |
| 326 | chip->write_buf(mtd, &byte, 1); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16 |
| 331 | * @mtd: MTD device structure |
| 332 | * @byte: value to write |
| 333 | * |
| 334 | * Default function to write a byte to I/O[7:0] on a 16-bit wide chip. |
| 335 | */ |
| 336 | static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte) |
| 337 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 338 | struct nand_chip *chip = mtd_to_nand(mtd); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 339 | uint16_t word = byte; |
| 340 | |
| 341 | /* |
| 342 | * It's not entirely clear what should happen to I/O[15:8] when writing |
| 343 | * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads: |
| 344 | * |
| 345 | * When the host supports a 16-bit bus width, only data is |
| 346 | * transferred at the 16-bit width. All address and command line |
| 347 | * transfers shall use only the lower 8-bits of the data bus. During |
| 348 | * command transfers, the host may place any value on the upper |
| 349 | * 8-bits of the data bus. During address transfers, the host shall |
| 350 | * set the upper 8-bits of the data bus to 00h. |
| 351 | * |
| 352 | * One user of the write_byte callback is nand_onfi_set_features. The |
| 353 | * four parameters are specified to be written to I/O[7:0], but this is |
| 354 | * neither an address nor a command transfer. Let's assume a 0 on the |
| 355 | * upper I/O lines is OK. |
| 356 | */ |
| 357 | chip->write_buf(mtd, (uint8_t *)&word, 2); |
| 358 | } |
| 359 | |
| 360 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | * nand_write_buf - [DEFAULT] write buffer to chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 362 | * @mtd: MTD device structure |
| 363 | * @buf: data buffer |
| 364 | * @len: number of bytes to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 366 | * Default write function for 8bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 368 | static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 370 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 372 | iowrite8_rep(chip->IO_ADDR_W, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 376 | * nand_read_buf - [DEFAULT] read chip data into buffer |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 377 | * @mtd: MTD device structure |
| 378 | * @buf: buffer to store date |
| 379 | * @len: number of bytes to read |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 381 | * Default read function for 8bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 383 | static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 385 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 387 | ioread8_rep(chip->IO_ADDR_R, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | * nand_write_buf16 - [DEFAULT] write buffer to chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 392 | * @mtd: MTD device structure |
| 393 | * @buf: data buffer |
| 394 | * @len: number of bytes to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 396 | * Default write function for 16bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 398 | static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 400 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | u16 *p = (u16 *) buf; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 402 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 403 | iowrite16_rep(chip->IO_ADDR_W, p, len >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 407 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 408 | * @mtd: MTD device structure |
| 409 | * @buf: buffer to store date |
| 410 | * @len: number of bytes to read |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 412 | * Default read function for 16bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 414 | static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 416 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | u16 *p = (u16 *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 419 | ioread16_rep(chip->IO_ADDR_R, p, len >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 424 | * @mtd: MTD device structure |
| 425 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 427 | * Check, if the block is bad. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | */ |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 429 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 431 | int page, page_end, res; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 432 | struct nand_chip *chip = mtd_to_nand(mtd); |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 433 | u8 bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Brian Norris | 5fb1549 | 2011-05-31 16:31:21 -0700 | [diff] [blame] | 435 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 436 | ofs += mtd->erasesize - mtd->writesize; |
| 437 | |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 438 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 439 | page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1); |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 440 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 441 | for (; page < page_end; page++) { |
| 442 | res = chip->ecc.read_oob(mtd, chip, page); |
| 443 | if (res) |
| 444 | return res; |
| 445 | |
| 446 | bad = chip->oob_poi[chip->badblockpos]; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 447 | |
Brian Norris | cdbec05 | 2012-01-13 18:11:48 -0800 | [diff] [blame] | 448 | if (likely(chip->badblockbits == 8)) |
| 449 | res = bad != 0xFF; |
| 450 | else |
| 451 | res = hweight8(bad) < chip->badblockbits; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 452 | if (res) |
| 453 | return res; |
| 454 | } |
Maxim Levitsky | e0b58d0 | 2010-02-22 20:39:38 +0200 | [diff] [blame] | 455 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 456 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /** |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 460 | * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 461 | * @mtd: MTD device structure |
| 462 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 464 | * This is the default implementation, which can be overridden by a hardware |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 465 | * specific driver. It provides the details for writing a bad block marker to a |
| 466 | * block. |
| 467 | */ |
| 468 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 469 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 470 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 471 | struct mtd_oob_ops ops; |
| 472 | uint8_t buf[2] = { 0, 0 }; |
| 473 | int ret = 0, res, i = 0; |
| 474 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 475 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 476 | ops.oobbuf = buf; |
| 477 | ops.ooboffs = chip->badblockpos; |
| 478 | if (chip->options & NAND_BUSWIDTH_16) { |
| 479 | ops.ooboffs &= ~0x01; |
| 480 | ops.len = ops.ooblen = 2; |
| 481 | } else { |
| 482 | ops.len = ops.ooblen = 1; |
| 483 | } |
| 484 | ops.mode = MTD_OPS_PLACE_OOB; |
| 485 | |
| 486 | /* Write to first/last page(s) if necessary */ |
| 487 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 488 | ofs += mtd->erasesize - mtd->writesize; |
| 489 | do { |
| 490 | res = nand_do_write_oob(mtd, ofs, &ops); |
| 491 | if (!ret) |
| 492 | ret = res; |
| 493 | |
| 494 | i++; |
| 495 | ofs += mtd->writesize; |
| 496 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
| 497 | |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * nand_block_markbad_lowlevel - mark a block bad |
| 503 | * @mtd: MTD device structure |
| 504 | * @ofs: offset from device start |
| 505 | * |
| 506 | * This function performs the generic NAND bad block marking steps (i.e., bad |
| 507 | * block table(s) and/or marker(s)). We only allow the hardware driver to |
| 508 | * specify how to write bad block markers to OOB (chip->block_markbad). |
| 509 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 510 | * We try operations in the following order: |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 511 | * |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 512 | * (1) erase the affected block, to allow OOB marker to be written cleanly |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 513 | * (2) write bad block marker to OOB area of affected block (unless flag |
| 514 | * NAND_BBT_NO_OOB_BBM is present) |
| 515 | * (3) update the BBT |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 516 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 517 | * Note that we retain the first error encountered in (2) or (3), finish the |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 518 | * procedures, and dump the error in the end. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | */ |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 520 | static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 522 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 523 | int res, ret = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 524 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 525 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 526 | struct erase_info einfo; |
| 527 | |
| 528 | /* Attempt erase before marking OOB */ |
| 529 | memset(&einfo, 0, sizeof(einfo)); |
| 530 | einfo.mtd = mtd; |
| 531 | einfo.addr = ofs; |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 532 | einfo.len = 1ULL << chip->phys_erase_shift; |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 533 | nand_erase_nand(mtd, &einfo, 0); |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 534 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 535 | /* Write bad block marker to OOB */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 536 | nand_get_device(mtd, FL_WRITING); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 537 | ret = chip->block_markbad(mtd, ofs); |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 538 | nand_release_device(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 539 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 540 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 541 | /* Mark block bad in BBT */ |
| 542 | if (chip->bbt) { |
| 543 | res = nand_markbad_bbt(mtd, ofs); |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 544 | if (!ret) |
| 545 | ret = res; |
| 546 | } |
| 547 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 548 | if (!ret) |
| 549 | mtd->ecc_stats.badblocks++; |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 550 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 551 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 554 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | * nand_check_wp - [GENERIC] check if the chip is write protected |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 556 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 558 | * Check, if the device is write protected. The function expects, that the |
| 559 | * device is already selected. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 561 | static int nand_check_wp(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 563 | struct nand_chip *chip = mtd_to_nand(mtd); |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 564 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 565 | /* Broken xD cards report WP despite being writable */ |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 566 | if (chip->options & NAND_BROKEN_XD) |
| 567 | return 0; |
| 568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | /* Check the WP bit */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 570 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 571 | return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /** |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 575 | * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 576 | * @mtd: MTD device structure |
| 577 | * @ofs: offset from device start |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 578 | * |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 579 | * Check if the block is marked as reserved. |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 580 | */ |
| 581 | static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 582 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 583 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 584 | |
| 585 | if (!chip->bbt) |
| 586 | return 0; |
| 587 | /* Return info from the table */ |
| 588 | return nand_isreserved_bbt(mtd, ofs); |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 593 | * @mtd: MTD device structure |
| 594 | * @ofs: offset from device start |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 595 | * @allowbbt: 1, if its allowed to access the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | * |
| 597 | * Check, if the block is bad. Either by reading the bad block table or |
| 598 | * calling of the scan function. |
| 599 | */ |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 600 | static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 602 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 603 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 604 | if (!chip->bbt) |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 605 | return chip->block_bad(mtd, ofs); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | /* Return info from the table */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 608 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 611 | /** |
| 612 | * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 613 | * @mtd: MTD device structure |
| 614 | * @timeo: Timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 615 | * |
| 616 | * Helper function for nand_wait_ready used when needing to wait in interrupt |
| 617 | * context. |
| 618 | */ |
| 619 | static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo) |
| 620 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 621 | struct nand_chip *chip = mtd_to_nand(mtd); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 622 | int i; |
| 623 | |
| 624 | /* Wait for the device to get ready */ |
| 625 | for (i = 0; i < timeo; i++) { |
| 626 | if (chip->dev_ready(mtd)) |
| 627 | break; |
| 628 | touch_softlockup_watchdog(); |
| 629 | mdelay(1); |
| 630 | } |
| 631 | } |
| 632 | |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 633 | /** |
| 634 | * nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
| 635 | * @mtd: MTD device structure |
| 636 | * |
| 637 | * Wait for the ready pin after a command, and warn if a timeout occurs. |
| 638 | */ |
David Woodhouse | 4b648b0 | 2006-09-25 17:05:24 +0100 | [diff] [blame] | 639 | void nand_wait_ready(struct mtd_info *mtd) |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 640 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 641 | struct nand_chip *chip = mtd_to_nand(mtd); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 642 | unsigned long timeo = 400; |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 643 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 644 | if (in_interrupt() || oops_in_progress) |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 645 | return panic_nand_wait_ready(mtd, timeo); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 646 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 647 | /* Wait until command is processed or timeout occurs */ |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 648 | timeo = jiffies + msecs_to_jiffies(timeo); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 649 | do { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 650 | if (chip->dev_ready(mtd)) |
Ezequiel Garcia | 4c7e054 | 2016-04-12 17:46:41 -0300 | [diff] [blame] | 651 | return; |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 652 | cond_resched(); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 653 | } while (time_before(jiffies, timeo)); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 654 | |
Brian Norris | 9ebfdf5 | 2016-03-04 17:19:23 -0800 | [diff] [blame] | 655 | if (!chip->dev_ready(mtd)) |
| 656 | pr_warn_ratelimited("timeout while waiting for chip to become ready\n"); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 657 | } |
David Woodhouse | 4b648b0 | 2006-09-25 17:05:24 +0100 | [diff] [blame] | 658 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 659 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | /** |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 661 | * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands. |
| 662 | * @mtd: MTD device structure |
| 663 | * @timeo: Timeout in ms |
| 664 | * |
| 665 | * Wait for status ready (i.e. command done) or timeout. |
| 666 | */ |
| 667 | static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo) |
| 668 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 669 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 670 | |
| 671 | timeo = jiffies + msecs_to_jiffies(timeo); |
| 672 | do { |
| 673 | if ((chip->read_byte(mtd) & NAND_STATUS_READY)) |
| 674 | break; |
| 675 | touch_softlockup_watchdog(); |
| 676 | } while (time_before(jiffies, timeo)); |
| 677 | }; |
| 678 | |
| 679 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | * nand_command - [DEFAULT] Send command to NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 681 | * @mtd: MTD device structure |
| 682 | * @command: the command to be sent |
| 683 | * @column: the column address for this command, -1 if none |
| 684 | * @page_addr: the page address for this command, -1 if none |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 686 | * Send command to NAND device. This function is used for small page devices |
Artem Bityutskiy | 51148f1 | 2013-03-05 15:00:51 +0200 | [diff] [blame] | 687 | * (512 Bytes per page). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 689 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
| 690 | int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 692 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 693 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 695 | /* Write out the command to the device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | if (command == NAND_CMD_SEQIN) { |
| 697 | int readcmd; |
| 698 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 699 | if (column >= mtd->writesize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | /* OOB area */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 701 | column -= mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | readcmd = NAND_CMD_READOOB; |
| 703 | } else if (column < 256) { |
| 704 | /* First 256 bytes --> READ0 */ |
| 705 | readcmd = NAND_CMD_READ0; |
| 706 | } else { |
| 707 | column -= 256; |
| 708 | readcmd = NAND_CMD_READ1; |
| 709 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 710 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 711 | ctrl &= ~NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 713 | chip->cmd_ctrl(mtd, command, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 715 | /* Address cycle, when necessary */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 716 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
| 717 | /* Serially input address */ |
| 718 | if (column != -1) { |
| 719 | /* Adjust columns for 16 bit buswidth */ |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 720 | if (chip->options & NAND_BUSWIDTH_16 && |
| 721 | !nand_opcode_8bits(command)) |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 722 | column >>= 1; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 723 | chip->cmd_ctrl(mtd, column, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 724 | ctrl &= ~NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | } |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 726 | if (page_addr != -1) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 727 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 728 | ctrl &= ~NAND_CTRL_CHANGE; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 729 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame^] | 730 | if (chip->options & NAND_ROW_ADDR_3) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 731 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 732 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 733 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 734 | |
| 735 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 736 | * Program and erase have their own busy handlers status and sequential |
| 737 | * in needs no delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 738 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | case NAND_CMD_PAGEPROG: |
| 742 | case NAND_CMD_ERASE1: |
| 743 | case NAND_CMD_ERASE2: |
| 744 | case NAND_CMD_SEQIN: |
| 745 | case NAND_CMD_STATUS: |
Masahiro Yamada | 3158fa0 | 2017-03-23 09:17:49 +0900 | [diff] [blame] | 746 | case NAND_CMD_READID: |
Masahiro Yamada | c5d664a | 2017-03-23 09:17:50 +0900 | [diff] [blame] | 747 | case NAND_CMD_SET_FEATURES: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | return; |
| 749 | |
| 750 | case NAND_CMD_RESET: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 751 | if (chip->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | break; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 753 | udelay(chip->chip_delay); |
| 754 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 755 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 756 | chip->cmd_ctrl(mtd, |
| 757 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 758 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 759 | nand_wait_status_ready(mtd, 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | return; |
| 761 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 762 | /* This applies to read commands */ |
Boris Brezillon | 2165c4a | 2017-05-16 18:35:45 +0200 | [diff] [blame] | 763 | case NAND_CMD_READ0: |
| 764 | /* |
| 765 | * READ0 is sometimes used to exit GET STATUS mode. When this |
| 766 | * is the case no address cycles are requested, and we can use |
| 767 | * this information to detect that we should not wait for the |
| 768 | * device to be ready. |
| 769 | */ |
| 770 | if (column == -1 && page_addr == -1) |
| 771 | return; |
| 772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 774 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | * If we don't have access to the busy pin, we apply the given |
| 776 | * command delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 777 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 778 | if (!chip->dev_ready) { |
| 779 | udelay(chip->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 781 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 783 | /* |
| 784 | * Apply this short delay always to ensure that we do wait tWB in |
| 785 | * any case on any machine. |
| 786 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 787 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 788 | |
| 789 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 792 | static void nand_ccs_delay(struct nand_chip *chip) |
| 793 | { |
| 794 | /* |
| 795 | * The controller already takes care of waiting for tCCS when the RNDIN |
| 796 | * or RNDOUT command is sent, return directly. |
| 797 | */ |
| 798 | if (!(chip->options & NAND_WAIT_TCCS)) |
| 799 | return; |
| 800 | |
| 801 | /* |
| 802 | * Wait tCCS_min if it is correctly defined, otherwise wait 500ns |
| 803 | * (which should be safe for all NANDs). |
| 804 | */ |
| 805 | if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min) |
| 806 | ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000); |
| 807 | else |
| 808 | ndelay(500); |
| 809 | } |
| 810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | /** |
| 812 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 813 | * @mtd: MTD device structure |
| 814 | * @command: the command to be sent |
| 815 | * @column: the column address for this command, -1 if none |
| 816 | * @page_addr: the page address for this command, -1 if none |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | * |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 818 | * Send command to NAND device. This is the version for the new large page |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 819 | * devices. We don't have the separate regions as we have in the small page |
| 820 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 822 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
| 823 | int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 825 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | |
| 827 | /* Emulate NAND_CMD_READOOB */ |
| 828 | if (command == NAND_CMD_READOOB) { |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 829 | column += mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | command = NAND_CMD_READ0; |
| 831 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 832 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 833 | /* Command latch cycle */ |
Alexander Shiyan | fb066ad | 2013-02-28 12:02:19 +0400 | [diff] [blame] | 834 | chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
| 836 | if (column != -1 || page_addr != -1) { |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 837 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | |
| 839 | /* Serially input address */ |
| 840 | if (column != -1) { |
| 841 | /* Adjust columns for 16 bit buswidth */ |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 842 | if (chip->options & NAND_BUSWIDTH_16 && |
| 843 | !nand_opcode_8bits(command)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | column >>= 1; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 845 | chip->cmd_ctrl(mtd, column, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 846 | ctrl &= ~NAND_CTRL_CHANGE; |
Boris Brezillon | fde85cf | 2016-06-15 13:09:51 +0200 | [diff] [blame] | 847 | |
Brian Norris | f5b88de | 2016-10-03 09:49:35 -0700 | [diff] [blame] | 848 | /* Only output a single addr cycle for 8bits opcodes. */ |
Boris Brezillon | fde85cf | 2016-06-15 13:09:51 +0200 | [diff] [blame] | 849 | if (!nand_opcode_8bits(command)) |
| 850 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 851 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | if (page_addr != -1) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 853 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 854 | chip->cmd_ctrl(mtd, page_addr >> 8, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 855 | NAND_NCE | NAND_ALE); |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame^] | 856 | if (chip->options & NAND_ROW_ADDR_3) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 857 | chip->cmd_ctrl(mtd, page_addr >> 16, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 858 | NAND_NCE | NAND_ALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 861 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 862 | |
| 863 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 864 | * Program and erase have their own busy handlers status, sequential |
Gerhard Sittig | 7a442f1 | 2014-03-29 14:36:22 +0100 | [diff] [blame] | 865 | * in and status need no delay. |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 866 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | case NAND_CMD_CACHEDPROG: |
| 870 | case NAND_CMD_PAGEPROG: |
| 871 | case NAND_CMD_ERASE1: |
| 872 | case NAND_CMD_ERASE2: |
| 873 | case NAND_CMD_SEQIN: |
| 874 | case NAND_CMD_STATUS: |
Masahiro Yamada | 3158fa0 | 2017-03-23 09:17:49 +0900 | [diff] [blame] | 875 | case NAND_CMD_READID: |
Masahiro Yamada | c5d664a | 2017-03-23 09:17:50 +0900 | [diff] [blame] | 876 | case NAND_CMD_SET_FEATURES: |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 877 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 879 | case NAND_CMD_RNDIN: |
| 880 | nand_ccs_delay(chip); |
| 881 | return; |
| 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | case NAND_CMD_RESET: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 884 | if (chip->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | break; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 886 | udelay(chip->chip_delay); |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 887 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 888 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 889 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 890 | NAND_NCE | NAND_CTRL_CHANGE); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 891 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 892 | nand_wait_status_ready(mtd, 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | return; |
| 894 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 895 | case NAND_CMD_RNDOUT: |
| 896 | /* No ready / busy check necessary */ |
| 897 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, |
| 898 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 899 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 900 | NAND_NCE | NAND_CTRL_CHANGE); |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 901 | |
| 902 | nand_ccs_delay(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 903 | return; |
| 904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | case NAND_CMD_READ0: |
Boris Brezillon | 2165c4a | 2017-05-16 18:35:45 +0200 | [diff] [blame] | 906 | /* |
| 907 | * READ0 is sometimes used to exit GET STATUS mode. When this |
| 908 | * is the case no address cycles are requested, and we can use |
| 909 | * this information to detect that READSTART should not be |
| 910 | * issued. |
| 911 | */ |
| 912 | if (column == -1 && page_addr == -1) |
| 913 | return; |
| 914 | |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 915 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
| 916 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 917 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 918 | NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 919 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 920 | /* This applies to read commands */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 922 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | * If we don't have access to the busy pin, we apply the given |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 924 | * command delay. |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 925 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 926 | if (!chip->dev_ready) { |
| 927 | udelay(chip->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 929 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | } |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 931 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 932 | /* |
| 933 | * Apply this short delay always to ensure that we do wait tWB in |
| 934 | * any case on any machine. |
| 935 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 936 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 937 | |
| 938 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 942 | * panic_nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 943 | * @chip: the nand chip descriptor |
| 944 | * @mtd: MTD device structure |
| 945 | * @new_state: the state which is requested |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 946 | * |
| 947 | * Used when in panic, no locks are taken. |
| 948 | */ |
| 949 | static void panic_nand_get_device(struct nand_chip *chip, |
| 950 | struct mtd_info *mtd, int new_state) |
| 951 | { |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 952 | /* Hardware controller shared among independent devices */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 953 | chip->controller->active = chip; |
| 954 | chip->state = new_state; |
| 955 | } |
| 956 | |
| 957 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | * nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 959 | * @mtd: MTD device structure |
| 960 | * @new_state: the state which is requested |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | * |
| 962 | * Get the device and lock it for exclusive access |
| 963 | */ |
Thomas Gleixner | 2c0a2be | 2006-05-23 11:50:56 +0200 | [diff] [blame] | 964 | static int |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 965 | nand_get_device(struct mtd_info *mtd, int new_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 967 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 968 | spinlock_t *lock = &chip->controller->lock; |
| 969 | wait_queue_head_t *wq = &chip->controller->wq; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 970 | DECLARE_WAITQUEUE(wait, current); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 971 | retry: |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 972 | spin_lock(lock); |
| 973 | |
vimal singh | b8b3ee9 | 2009-07-09 20:41:22 +0530 | [diff] [blame] | 974 | /* Hardware controller shared among independent devices */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 975 | if (!chip->controller->active) |
| 976 | chip->controller->active = chip; |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 977 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 978 | if (chip->controller->active == chip && chip->state == FL_READY) { |
| 979 | chip->state = new_state; |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 980 | spin_unlock(lock); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 981 | return 0; |
| 982 | } |
| 983 | if (new_state == FL_PM_SUSPENDED) { |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 984 | if (chip->controller->active->state == FL_PM_SUSPENDED) { |
| 985 | chip->state = FL_PM_SUSPENDED; |
| 986 | spin_unlock(lock); |
| 987 | return 0; |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 988 | } |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 989 | } |
| 990 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 991 | add_wait_queue(wq, &wait); |
| 992 | spin_unlock(lock); |
| 993 | schedule(); |
| 994 | remove_wait_queue(wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | goto retry; |
| 996 | } |
| 997 | |
| 998 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 999 | * panic_nand_wait - [GENERIC] wait until the command is done |
| 1000 | * @mtd: MTD device structure |
| 1001 | * @chip: NAND chip structure |
| 1002 | * @timeo: timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1003 | * |
| 1004 | * Wait for command done. This is a helper function for nand_wait used when |
| 1005 | * we are in interrupt context. May happen when in panic and trying to write |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 1006 | * an oops through mtdoops. |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1007 | */ |
| 1008 | static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, |
| 1009 | unsigned long timeo) |
| 1010 | { |
| 1011 | int i; |
| 1012 | for (i = 0; i < timeo; i++) { |
| 1013 | if (chip->dev_ready) { |
| 1014 | if (chip->dev_ready(mtd)) |
| 1015 | break; |
| 1016 | } else { |
| 1017 | if (chip->read_byte(mtd) & NAND_STATUS_READY) |
| 1018 | break; |
| 1019 | } |
| 1020 | mdelay(1); |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1021 | } |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1025 | * nand_wait - [DEFAULT] wait until the command is done |
| 1026 | * @mtd: MTD device structure |
| 1027 | * @chip: NAND chip structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | * |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1029 | * Wait for command done. This applies to erase and program only. |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 1030 | */ |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 1031 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | { |
| 1033 | |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1034 | int status; |
| 1035 | unsigned long timeo = 400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1037 | /* |
| 1038 | * Apply this short delay always to ensure that we do wait tWB in any |
| 1039 | * case on any machine. |
| 1040 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1041 | ndelay(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
Artem Bityutskiy | 14c6578 | 2013-03-04 14:21:34 +0200 | [diff] [blame] | 1043 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1045 | if (in_interrupt() || oops_in_progress) |
| 1046 | panic_nand_wait(mtd, chip, timeo); |
| 1047 | else { |
Huang Shijie | 6d2559f | 2013-01-30 10:03:56 +0800 | [diff] [blame] | 1048 | timeo = jiffies + msecs_to_jiffies(timeo); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1049 | do { |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1050 | if (chip->dev_ready) { |
| 1051 | if (chip->dev_ready(mtd)) |
| 1052 | break; |
| 1053 | } else { |
| 1054 | if (chip->read_byte(mtd) & NAND_STATUS_READY) |
| 1055 | break; |
| 1056 | } |
| 1057 | cond_resched(); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1058 | } while (time_before(jiffies, timeo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | } |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 1060 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1061 | status = (int)chip->read_byte(mtd); |
Matthieu CASTET | f251b8d | 2012-11-05 15:00:44 +0100 | [diff] [blame] | 1062 | /* This can happen if in case of timeout or buggy dev_ready */ |
| 1063 | WARN_ON(!(status & NAND_STATUS_READY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | return status; |
| 1065 | } |
| 1066 | |
| 1067 | /** |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1068 | * nand_reset_data_interface - Reset data interface and timings |
| 1069 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1070 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1071 | * |
| 1072 | * Reset the Data interface and timings to ONFI mode 0. |
| 1073 | * |
| 1074 | * Returns 0 for success or negative error code otherwise. |
| 1075 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1076 | static int nand_reset_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1077 | { |
| 1078 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1079 | const struct nand_data_interface *conf; |
| 1080 | int ret; |
| 1081 | |
| 1082 | if (!chip->setup_data_interface) |
| 1083 | return 0; |
| 1084 | |
| 1085 | /* |
| 1086 | * The ONFI specification says: |
| 1087 | * " |
| 1088 | * To transition from NV-DDR or NV-DDR2 to the SDR data |
| 1089 | * interface, the host shall use the Reset (FFh) command |
| 1090 | * using SDR timing mode 0. A device in any timing mode is |
| 1091 | * required to recognize Reset (FFh) command issued in SDR |
| 1092 | * timing mode 0. |
| 1093 | * " |
| 1094 | * |
| 1095 | * Configure the data interface in SDR mode and set the |
| 1096 | * timings to timing mode 0. |
| 1097 | */ |
| 1098 | |
| 1099 | conf = nand_get_default_data_interface(); |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1100 | ret = chip->setup_data_interface(mtd, chipnr, conf); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1101 | if (ret) |
| 1102 | pr_err("Failed to configure data interface to SDR timing mode 0\n"); |
| 1103 | |
| 1104 | return ret; |
| 1105 | } |
| 1106 | |
| 1107 | /** |
| 1108 | * nand_setup_data_interface - Setup the best data interface and timings |
| 1109 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1110 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1111 | * |
| 1112 | * Find and configure the best data interface and NAND timings supported by |
| 1113 | * the chip and the driver. |
| 1114 | * First tries to retrieve supported timing modes from ONFI information, |
| 1115 | * and if the NAND chip does not support ONFI, relies on the |
| 1116 | * ->onfi_timing_mode_default specified in the nand_ids table. |
| 1117 | * |
| 1118 | * Returns 0 for success or negative error code otherwise. |
| 1119 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1120 | static int nand_setup_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1121 | { |
| 1122 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1123 | int ret; |
| 1124 | |
| 1125 | if (!chip->setup_data_interface || !chip->data_interface) |
| 1126 | return 0; |
| 1127 | |
| 1128 | /* |
| 1129 | * Ensure the timing mode has been changed on the chip side |
| 1130 | * before changing timings on the controller side. |
| 1131 | */ |
Boris Brezillon | a11bf5e | 2017-07-31 10:29:56 +0200 | [diff] [blame] | 1132 | if (chip->onfi_version && |
| 1133 | (le16_to_cpu(chip->onfi_params.opt_cmd) & |
| 1134 | ONFI_OPT_CMD_SET_GET_FEATURES)) { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1135 | u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { |
| 1136 | chip->onfi_timing_mode_default, |
| 1137 | }; |
| 1138 | |
| 1139 | ret = chip->onfi_set_features(mtd, chip, |
| 1140 | ONFI_FEATURE_ADDR_TIMING_MODE, |
| 1141 | tmode_param); |
| 1142 | if (ret) |
| 1143 | goto err; |
| 1144 | } |
| 1145 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1146 | ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1147 | err: |
| 1148 | return ret; |
| 1149 | } |
| 1150 | |
| 1151 | /** |
| 1152 | * nand_init_data_interface - find the best data interface and timings |
| 1153 | * @chip: The NAND chip |
| 1154 | * |
| 1155 | * Find the best data interface and NAND timings supported by the chip |
| 1156 | * and the driver. |
| 1157 | * First tries to retrieve supported timing modes from ONFI information, |
| 1158 | * and if the NAND chip does not support ONFI, relies on the |
| 1159 | * ->onfi_timing_mode_default specified in the nand_ids table. After this |
| 1160 | * function nand_chip->data_interface is initialized with the best timing mode |
| 1161 | * available. |
| 1162 | * |
| 1163 | * Returns 0 for success or negative error code otherwise. |
| 1164 | */ |
| 1165 | static int nand_init_data_interface(struct nand_chip *chip) |
| 1166 | { |
| 1167 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1168 | int modes, mode, ret; |
| 1169 | |
| 1170 | if (!chip->setup_data_interface) |
| 1171 | return 0; |
| 1172 | |
| 1173 | /* |
| 1174 | * First try to identify the best timings from ONFI parameters and |
| 1175 | * if the NAND does not support ONFI, fallback to the default ONFI |
| 1176 | * timing mode. |
| 1177 | */ |
| 1178 | modes = onfi_get_async_timing_mode(chip); |
| 1179 | if (modes == ONFI_TIMING_MODE_UNKNOWN) { |
| 1180 | if (!chip->onfi_timing_mode_default) |
| 1181 | return 0; |
| 1182 | |
| 1183 | modes = GENMASK(chip->onfi_timing_mode_default, 0); |
| 1184 | } |
| 1185 | |
| 1186 | chip->data_interface = kzalloc(sizeof(*chip->data_interface), |
| 1187 | GFP_KERNEL); |
| 1188 | if (!chip->data_interface) |
| 1189 | return -ENOMEM; |
| 1190 | |
| 1191 | for (mode = fls(modes) - 1; mode >= 0; mode--) { |
| 1192 | ret = onfi_init_data_interface(chip, chip->data_interface, |
| 1193 | NAND_SDR_IFACE, mode); |
| 1194 | if (ret) |
| 1195 | continue; |
| 1196 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1197 | /* Pass -1 to only */ |
| 1198 | ret = chip->setup_data_interface(mtd, |
| 1199 | NAND_DATA_IFACE_CHECK_ONLY, |
| 1200 | chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1201 | if (!ret) { |
| 1202 | chip->onfi_timing_mode_default = mode; |
| 1203 | break; |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | static void nand_release_data_interface(struct nand_chip *chip) |
| 1211 | { |
| 1212 | kfree(chip->data_interface); |
| 1213 | } |
| 1214 | |
| 1215 | /** |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1216 | * nand_reset - Reset and initialize a NAND device |
| 1217 | * @chip: The NAND chip |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1218 | * @chipnr: Internal die id |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1219 | * |
| 1220 | * Returns 0 for success or negative error code otherwise |
| 1221 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1222 | int nand_reset(struct nand_chip *chip, int chipnr) |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1223 | { |
| 1224 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1225 | int ret; |
| 1226 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1227 | ret = nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1228 | if (ret) |
| 1229 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1230 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1231 | /* |
| 1232 | * The CS line has to be released before we can apply the new NAND |
| 1233 | * interface settings, hence this weird ->select_chip() dance. |
| 1234 | */ |
| 1235 | chip->select_chip(mtd, chipnr); |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1236 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1237 | chip->select_chip(mtd, -1); |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1238 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1239 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1240 | ret = nand_setup_data_interface(chip, chipnr); |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1241 | chip->select_chip(mtd, -1); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1242 | if (ret) |
| 1243 | return ret; |
| 1244 | |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1245 | return 0; |
| 1246 | } |
| 1247 | |
| 1248 | /** |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 1249 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 1250 | * @buf: buffer to test |
| 1251 | * @len: buffer length |
| 1252 | * @bitflips_threshold: maximum number of bitflips |
| 1253 | * |
| 1254 | * Check if a buffer contains only 0xff, which means the underlying region |
| 1255 | * has been erased and is ready to be programmed. |
| 1256 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1257 | * considering the region is not erased. |
| 1258 | * Note: The logic of this function has been extracted from the memweight |
| 1259 | * implementation, except that nand_check_erased_buf function exit before |
| 1260 | * testing the whole buffer if the number of bitflips exceed the |
| 1261 | * bitflips_threshold value. |
| 1262 | * |
| 1263 | * Returns a positive number of bitflips less than or equal to |
| 1264 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1265 | * threshold. |
| 1266 | */ |
| 1267 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 1268 | { |
| 1269 | const unsigned char *bitmap = buf; |
| 1270 | int bitflips = 0; |
| 1271 | int weight; |
| 1272 | |
| 1273 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 1274 | len--, bitmap++) { |
| 1275 | weight = hweight8(*bitmap); |
| 1276 | bitflips += BITS_PER_BYTE - weight; |
| 1277 | if (unlikely(bitflips > bitflips_threshold)) |
| 1278 | return -EBADMSG; |
| 1279 | } |
| 1280 | |
| 1281 | for (; len >= sizeof(long); |
| 1282 | len -= sizeof(long), bitmap += sizeof(long)) { |
Pavel Machek | 086567f | 2017-04-21 12:51:07 +0200 | [diff] [blame] | 1283 | unsigned long d = *((unsigned long *)bitmap); |
| 1284 | if (d == ~0UL) |
| 1285 | continue; |
| 1286 | weight = hweight_long(d); |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 1287 | bitflips += BITS_PER_LONG - weight; |
| 1288 | if (unlikely(bitflips > bitflips_threshold)) |
| 1289 | return -EBADMSG; |
| 1290 | } |
| 1291 | |
| 1292 | for (; len > 0; len--, bitmap++) { |
| 1293 | weight = hweight8(*bitmap); |
| 1294 | bitflips += BITS_PER_BYTE - weight; |
| 1295 | if (unlikely(bitflips > bitflips_threshold)) |
| 1296 | return -EBADMSG; |
| 1297 | } |
| 1298 | |
| 1299 | return bitflips; |
| 1300 | } |
| 1301 | |
| 1302 | /** |
| 1303 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 1304 | * 0xff data |
| 1305 | * @data: data buffer to test |
| 1306 | * @datalen: data length |
| 1307 | * @ecc: ECC buffer |
| 1308 | * @ecclen: ECC length |
| 1309 | * @extraoob: extra OOB buffer |
| 1310 | * @extraooblen: extra OOB length |
| 1311 | * @bitflips_threshold: maximum number of bitflips |
| 1312 | * |
| 1313 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 1314 | * 0xff pattern, which means the underlying region has been erased and is |
| 1315 | * ready to be programmed. |
| 1316 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1317 | * considering the region as not erased. |
| 1318 | * |
| 1319 | * Note: |
| 1320 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 1321 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 1322 | * report the number of errors per chunk, and the NAND core infrastructure |
| 1323 | * expect you to return the maximum number of bitflips for the whole page. |
| 1324 | * This is why you should always use this function on a single chunk and |
| 1325 | * not on the whole page. After checking each chunk you should update your |
| 1326 | * max_bitflips value accordingly. |
| 1327 | * 2/ When checking for bitflips in erased pages you should not only check |
| 1328 | * the payload data but also their associated ECC data, because a user might |
| 1329 | * have programmed almost all bits to 1 but a few. In this case, we |
| 1330 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 1331 | * this case. |
| 1332 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 1333 | * data are protected by the ECC engine. |
| 1334 | * It could also be used if you support subpages and want to attach some |
| 1335 | * extra OOB data to an ECC chunk. |
| 1336 | * |
| 1337 | * Returns a positive number of bitflips less than or equal to |
| 1338 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1339 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 1340 | */ |
| 1341 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 1342 | void *ecc, int ecclen, |
| 1343 | void *extraoob, int extraooblen, |
| 1344 | int bitflips_threshold) |
| 1345 | { |
| 1346 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 1347 | |
| 1348 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 1349 | bitflips_threshold); |
| 1350 | if (data_bitflips < 0) |
| 1351 | return data_bitflips; |
| 1352 | |
| 1353 | bitflips_threshold -= data_bitflips; |
| 1354 | |
| 1355 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 1356 | if (ecc_bitflips < 0) |
| 1357 | return ecc_bitflips; |
| 1358 | |
| 1359 | bitflips_threshold -= ecc_bitflips; |
| 1360 | |
| 1361 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 1362 | bitflips_threshold); |
| 1363 | if (extraoob_bitflips < 0) |
| 1364 | return extraoob_bitflips; |
| 1365 | |
| 1366 | if (data_bitflips) |
| 1367 | memset(data, 0xff, datalen); |
| 1368 | |
| 1369 | if (ecc_bitflips) |
| 1370 | memset(ecc, 0xff, ecclen); |
| 1371 | |
| 1372 | if (extraoob_bitflips) |
| 1373 | memset(extraoob, 0xff, extraooblen); |
| 1374 | |
| 1375 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 1376 | } |
| 1377 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
| 1378 | |
| 1379 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1380 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1381 | * @mtd: mtd info structure |
| 1382 | * @chip: nand chip info structure |
| 1383 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1384 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1385 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1386 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1387 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1388 | */ |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 1389 | int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 1390 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1391 | { |
| 1392 | chip->read_buf(mtd, buf, mtd->writesize); |
Brian Norris | 279f08d | 2012-05-02 10:15:03 -0700 | [diff] [blame] | 1393 | if (oob_required) |
| 1394 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1395 | return 0; |
| 1396 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 1397 | EXPORT_SYMBOL(nand_read_page_raw); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1398 | |
| 1399 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1400 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1401 | * @mtd: mtd info structure |
| 1402 | * @chip: nand chip info structure |
| 1403 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1404 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1405 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1406 | * |
| 1407 | * We need a special oob layout and handling even when OOB isn't used. |
| 1408 | */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1409 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1410 | struct nand_chip *chip, uint8_t *buf, |
| 1411 | int oob_required, int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1412 | { |
| 1413 | int eccsize = chip->ecc.size; |
| 1414 | int eccbytes = chip->ecc.bytes; |
| 1415 | uint8_t *oob = chip->oob_poi; |
| 1416 | int steps, size; |
| 1417 | |
| 1418 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
| 1419 | chip->read_buf(mtd, buf, eccsize); |
| 1420 | buf += eccsize; |
| 1421 | |
| 1422 | if (chip->ecc.prepad) { |
| 1423 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 1424 | oob += chip->ecc.prepad; |
| 1425 | } |
| 1426 | |
| 1427 | chip->read_buf(mtd, oob, eccbytes); |
| 1428 | oob += eccbytes; |
| 1429 | |
| 1430 | if (chip->ecc.postpad) { |
| 1431 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 1432 | oob += chip->ecc.postpad; |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 1437 | if (size) |
| 1438 | chip->read_buf(mtd, oob, size); |
| 1439 | |
| 1440 | return 0; |
| 1441 | } |
| 1442 | |
| 1443 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1444 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1445 | * @mtd: mtd info structure |
| 1446 | * @chip: nand chip info structure |
| 1447 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1448 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1449 | * @page: page number to read |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1450 | */ |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1451 | static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1452 | uint8_t *buf, int oob_required, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1454 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1455 | int eccbytes = chip->ecc.bytes; |
| 1456 | int eccsteps = chip->ecc.steps; |
| 1457 | uint8_t *p = buf; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1458 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1459 | uint8_t *ecc_code = chip->buffers->ecccode; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1460 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1461 | |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1462 | chip->ecc.read_page_raw(mtd, chip, buf, 1, page); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1463 | |
| 1464 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 1465 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1466 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1467 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 1468 | chip->ecc.total); |
| 1469 | if (ret) |
| 1470 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1471 | |
| 1472 | eccsteps = chip->ecc.steps; |
| 1473 | p = buf; |
| 1474 | |
| 1475 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1476 | int stat; |
| 1477 | |
| 1478 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1479 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1480 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1481 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1482 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1483 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1484 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1485 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1486 | return max_bitflips; |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 1487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | /** |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 1490 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1491 | * @mtd: mtd info structure |
| 1492 | * @chip: nand chip info structure |
| 1493 | * @data_offs: offset of requested data within the page |
| 1494 | * @readlen: data length |
| 1495 | * @bufpoi: buffer to store read data |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 1496 | * @page: page number to read |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1497 | */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1498 | static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 1499 | uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi, |
| 1500 | int page) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1501 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1502 | int start_step, end_step, num_steps, ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1503 | uint8_t *p; |
| 1504 | int data_col_addr, i, gaps = 0; |
| 1505 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 1506 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1507 | int index, section = 0; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1508 | unsigned int max_bitflips = 0; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1509 | struct mtd_oob_region oobregion = { }; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1510 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1511 | /* Column address within the page aligned to ECC size (256bytes) */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1512 | start_step = data_offs / chip->ecc.size; |
| 1513 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 1514 | num_steps = end_step - start_step + 1; |
Ron | 4a4163ca | 2014-03-16 04:01:07 +1030 | [diff] [blame] | 1515 | index = start_step * chip->ecc.bytes; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1516 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1517 | /* Data size aligned to ECC ecc.size */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1518 | datafrag_len = num_steps * chip->ecc.size; |
| 1519 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 1520 | |
| 1521 | data_col_addr = start_step * chip->ecc.size; |
| 1522 | /* If we read not a page aligned data */ |
| 1523 | if (data_col_addr != 0) |
| 1524 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1); |
| 1525 | |
| 1526 | p = bufpoi + data_col_addr; |
| 1527 | chip->read_buf(mtd, p, datafrag_len); |
| 1528 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1529 | /* Calculate ECC */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1530 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
| 1531 | chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); |
| 1532 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1533 | /* |
| 1534 | * The performance is faster if we position offsets according to |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1535 | * ecc.pos. Let's make sure that there are no gaps in ECC positions. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1536 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1537 | ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion); |
| 1538 | if (ret) |
| 1539 | return ret; |
| 1540 | |
| 1541 | if (oobregion.length < eccfrag_len) |
| 1542 | gaps = 1; |
| 1543 | |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1544 | if (gaps) { |
| 1545 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); |
| 1546 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1547 | } else { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1548 | /* |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1549 | * Send the command to read the particular ECC bytes take care |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1550 | * about buswidth alignment in read_buf. |
| 1551 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1552 | aligned_pos = oobregion.offset & ~(busw - 1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1553 | aligned_len = eccfrag_len; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1554 | if (oobregion.offset & (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1555 | aligned_len++; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1556 | if ((oobregion.offset + (num_steps * chip->ecc.bytes)) & |
| 1557 | (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1558 | aligned_len++; |
| 1559 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1560 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1561 | mtd->writesize + aligned_pos, -1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1562 | chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len); |
| 1563 | } |
| 1564 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1565 | ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode, |
| 1566 | chip->oob_poi, index, eccfrag_len); |
| 1567 | if (ret) |
| 1568 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1569 | |
| 1570 | p = bufpoi + data_col_addr; |
| 1571 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 1572 | int stat; |
| 1573 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1574 | stat = chip->ecc.correct(mtd, p, |
| 1575 | &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1576 | if (stat == -EBADMSG && |
| 1577 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1578 | /* check for empty pages with bitflips */ |
| 1579 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 1580 | &chip->buffers->ecccode[i], |
| 1581 | chip->ecc.bytes, |
| 1582 | NULL, 0, |
| 1583 | chip->ecc.strength); |
| 1584 | } |
| 1585 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1586 | if (stat < 0) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1587 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1588 | } else { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1589 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1590 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1591 | } |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1592 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1593 | return max_bitflips; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1597 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1598 | * @mtd: mtd info structure |
| 1599 | * @chip: nand chip info structure |
| 1600 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1601 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1602 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1603 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1604 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1605 | */ |
| 1606 | static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1607 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1608 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1609 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1610 | int eccbytes = chip->ecc.bytes; |
| 1611 | int eccsteps = chip->ecc.steps; |
| 1612 | uint8_t *p = buf; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1613 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1614 | uint8_t *ecc_code = chip->buffers->ecccode; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1615 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1616 | |
| 1617 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1618 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1619 | chip->read_buf(mtd, p, eccsize); |
| 1620 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1621 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1622 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1623 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1624 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 1625 | chip->ecc.total); |
| 1626 | if (ret) |
| 1627 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1628 | |
| 1629 | eccsteps = chip->ecc.steps; |
| 1630 | p = buf; |
| 1631 | |
| 1632 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1633 | int stat; |
| 1634 | |
| 1635 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1636 | if (stat == -EBADMSG && |
| 1637 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1638 | /* check for empty pages with bitflips */ |
| 1639 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 1640 | &ecc_code[i], eccbytes, |
| 1641 | NULL, 0, |
| 1642 | chip->ecc.strength); |
| 1643 | } |
| 1644 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1645 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1646 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1647 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1648 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1649 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1650 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1651 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1652 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1656 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1657 | * @mtd: mtd info structure |
| 1658 | * @chip: nand chip info structure |
| 1659 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1660 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1661 | * @page: page number to read |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1662 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1663 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 1664 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 1665 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 1666 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 1667 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1668 | */ |
| 1669 | static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1670 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1671 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1672 | int i, eccsize = chip->ecc.size, ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1673 | int eccbytes = chip->ecc.bytes; |
| 1674 | int eccsteps = chip->ecc.steps; |
| 1675 | uint8_t *p = buf; |
| 1676 | uint8_t *ecc_code = chip->buffers->ecccode; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1677 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1678 | unsigned int max_bitflips = 0; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1679 | |
| 1680 | /* Read the OOB area first */ |
| 1681 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
| 1682 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1683 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 1684 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1685 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 1686 | chip->ecc.total); |
| 1687 | if (ret) |
| 1688 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1689 | |
| 1690 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1691 | int stat; |
| 1692 | |
| 1693 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1694 | chip->read_buf(mtd, p, eccsize); |
| 1695 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1696 | |
| 1697 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1698 | if (stat == -EBADMSG && |
| 1699 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1700 | /* check for empty pages with bitflips */ |
| 1701 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 1702 | &ecc_code[i], eccbytes, |
| 1703 | NULL, 0, |
| 1704 | chip->ecc.strength); |
| 1705 | } |
| 1706 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1707 | if (stat < 0) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1708 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1709 | } else { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1710 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1711 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1712 | } |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1713 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1714 | return max_bitflips; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1718 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1719 | * @mtd: mtd info structure |
| 1720 | * @chip: nand chip info structure |
| 1721 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1722 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1723 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1724 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1725 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 1726 | * need a special oob layout and handling. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1727 | */ |
| 1728 | static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1729 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1730 | { |
| 1731 | int i, eccsize = chip->ecc.size; |
| 1732 | int eccbytes = chip->ecc.bytes; |
| 1733 | int eccsteps = chip->ecc.steps; |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1734 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1735 | uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1736 | uint8_t *oob = chip->oob_poi; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1737 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1738 | |
| 1739 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1740 | int stat; |
| 1741 | |
| 1742 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1743 | chip->read_buf(mtd, p, eccsize); |
| 1744 | |
| 1745 | if (chip->ecc.prepad) { |
| 1746 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 1747 | oob += chip->ecc.prepad; |
| 1748 | } |
| 1749 | |
| 1750 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
| 1751 | chip->read_buf(mtd, oob, eccbytes); |
| 1752 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
| 1753 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1754 | oob += eccbytes; |
| 1755 | |
| 1756 | if (chip->ecc.postpad) { |
| 1757 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 1758 | oob += chip->ecc.postpad; |
| 1759 | } |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1760 | |
| 1761 | if (stat == -EBADMSG && |
| 1762 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1763 | /* check for empty pages with bitflips */ |
| 1764 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 1765 | oob - eccpadbytes, |
| 1766 | eccpadbytes, |
| 1767 | NULL, 0, |
| 1768 | chip->ecc.strength); |
| 1769 | } |
| 1770 | |
| 1771 | if (stat < 0) { |
| 1772 | mtd->ecc_stats.failed++; |
| 1773 | } else { |
| 1774 | mtd->ecc_stats.corrected += stat; |
| 1775 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1776 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 1780 | i = mtd->oobsize - (oob - chip->oob_poi); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1781 | if (i) |
| 1782 | chip->read_buf(mtd, oob, i); |
| 1783 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1784 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1788 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1789 | * @mtd: mtd info structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1790 | * @oob: oob destination address |
| 1791 | * @ops: oob ops structure |
| 1792 | * @len: size of oob to transfer |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1793 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1794 | static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob, |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 1795 | struct mtd_oob_ops *ops, size_t len) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1796 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1797 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1798 | int ret; |
| 1799 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1800 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1801 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 1802 | case MTD_OPS_PLACE_OOB: |
| 1803 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1804 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 1805 | return oob + len; |
| 1806 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1807 | case MTD_OPS_AUTO_OOB: |
| 1808 | ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi, |
| 1809 | ops->ooboffs, len); |
| 1810 | BUG_ON(ret); |
| 1811 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1812 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1813 | default: |
| 1814 | BUG(); |
| 1815 | } |
| 1816 | return NULL; |
| 1817 | } |
| 1818 | |
| 1819 | /** |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1820 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
| 1821 | * @mtd: MTD device structure |
| 1822 | * @retry_mode: the retry mode to use |
| 1823 | * |
| 1824 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 1825 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 1826 | * a new threshold, the host should retry reading the page. |
| 1827 | */ |
| 1828 | static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode) |
| 1829 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 1830 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1831 | |
| 1832 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 1833 | |
| 1834 | if (retry_mode >= chip->read_retries) |
| 1835 | return -EINVAL; |
| 1836 | |
| 1837 | if (!chip->setup_read_retry) |
| 1838 | return -EOPNOTSUPP; |
| 1839 | |
| 1840 | return chip->setup_read_retry(mtd, retry_mode); |
| 1841 | } |
| 1842 | |
| 1843 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1844 | * nand_do_read_ops - [INTERN] Read data with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1845 | * @mtd: MTD device structure |
| 1846 | * @from: offset to read from |
| 1847 | * @ops: oob ops structure |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1848 | * |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1849 | * Internal function. Called with chip held. |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1850 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1851 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 1852 | struct mtd_oob_ops *ops) |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1853 | { |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 1854 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 1855 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1856 | int ret = 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1857 | uint32_t readlen = ops->len; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 1858 | uint32_t oobreadlen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 1859 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 9aca334 | 2010-02-22 20:39:35 +0200 | [diff] [blame] | 1860 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1861 | uint8_t *bufpoi, *oob, *buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1862 | int use_bufpoi; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1863 | unsigned int max_bitflips = 0; |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1864 | int retry_mode = 0; |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 1865 | bool ecc_fail = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1867 | chipnr = (int)(from >> chip->chip_shift); |
| 1868 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1870 | realpage = (int)(from >> chip->page_shift); |
| 1871 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1873 | col = (int)(from & (mtd->writesize - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1875 | buf = ops->datbuf; |
| 1876 | oob = ops->oobbuf; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 1877 | oob_required = oob ? 1 : 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1878 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1879 | while (1) { |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 1880 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
| 1881 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1882 | bytes = min(mtd->writesize - col, readlen); |
| 1883 | aligned = (bytes == mtd->writesize); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1884 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1885 | if (!aligned) |
| 1886 | use_bufpoi = 1; |
| 1887 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 1888 | use_bufpoi = !virt_addr_valid(buf) || |
| 1889 | !IS_ALIGNED((unsigned long)buf, |
| 1890 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1891 | else |
| 1892 | use_bufpoi = 0; |
| 1893 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1894 | /* Is the current page in the buffer? */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1895 | if (realpage != chip->pagebuf || oob) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1896 | bufpoi = use_bufpoi ? chip->buffers->databuf : buf; |
| 1897 | |
| 1898 | if (use_bufpoi && aligned) |
| 1899 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 1900 | __func__, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1902 | read_retry: |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 1903 | if (nand_standard_page_accessors(&chip->ecc)) |
| 1904 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1906 | /* |
| 1907 | * Now read the page into the buffer. Absent an error, |
| 1908 | * the read methods return max bitflips per ecc step. |
| 1909 | */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 1910 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1911 | ret = chip->ecc.read_page_raw(mtd, chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 1912 | oob_required, |
| 1913 | page); |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 1914 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
| 1915 | !oob) |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1916 | ret = chip->ecc.read_subpage(mtd, chip, |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 1917 | col, bytes, bufpoi, |
| 1918 | page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 1919 | else |
Sneha Narnakaje | 46a8cf2 | 2009-09-18 12:51:46 -0700 | [diff] [blame] | 1920 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 1921 | oob_required, page); |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 1922 | if (ret < 0) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1923 | if (use_bufpoi) |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 1924 | /* Invalidate page cache */ |
| 1925 | chip->pagebuf = -1; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1926 | break; |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 1927 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1928 | |
| 1929 | /* Transfer not aligned data */ |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1930 | if (use_bufpoi) { |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 1931 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 1932 | !(mtd->ecc_stats.failed - ecc_failures) && |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1933 | (ops->mode != MTD_OPS_RAW)) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1934 | chip->pagebuf = realpage; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1935 | chip->pagebuf_bitflips = ret; |
| 1936 | } else { |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 1937 | /* Invalidate page cache */ |
| 1938 | chip->pagebuf = -1; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1939 | } |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1940 | memcpy(buf, chip->buffers->databuf + col, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1942 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1943 | if (unlikely(oob)) { |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 1944 | int toread = min(oobreadlen, max_oobsize); |
| 1945 | |
| 1946 | if (toread) { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1947 | oob = nand_transfer_oob(mtd, |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 1948 | oob, ops, toread); |
| 1949 | oobreadlen -= toread; |
| 1950 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1951 | } |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 1952 | |
| 1953 | if (chip->options & NAND_NEED_READRDY) { |
| 1954 | /* Apply delay or wait for ready/busy pin */ |
| 1955 | if (!chip->dev_ready) |
| 1956 | udelay(chip->chip_delay); |
| 1957 | else |
| 1958 | nand_wait_ready(mtd); |
| 1959 | } |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 1960 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1961 | if (mtd->ecc_stats.failed - ecc_failures) { |
Brian Norris | 28fa65e | 2014-02-12 16:08:28 -0800 | [diff] [blame] | 1962 | if (retry_mode + 1 < chip->read_retries) { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1963 | retry_mode++; |
| 1964 | ret = nand_setup_read_retry(mtd, |
| 1965 | retry_mode); |
| 1966 | if (ret < 0) |
| 1967 | break; |
| 1968 | |
| 1969 | /* Reset failures; retry */ |
| 1970 | mtd->ecc_stats.failed = ecc_failures; |
| 1971 | goto read_retry; |
| 1972 | } else { |
| 1973 | /* No more retry modes; real failure */ |
| 1974 | ecc_fail = true; |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | buf += bytes; |
Masahiro Yamada | 0760468 | 2017-03-30 15:45:47 +0900 | [diff] [blame] | 1979 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1980 | } else { |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1981 | memcpy(buf, chip->buffers->databuf + col, bytes); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1982 | buf += bytes; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1983 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 1984 | chip->pagebuf_bitflips); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1985 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1987 | readlen -= bytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1988 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1989 | /* Reset to retry mode 0 */ |
| 1990 | if (retry_mode) { |
| 1991 | ret = nand_setup_read_retry(mtd, 0); |
| 1992 | if (ret < 0) |
| 1993 | break; |
| 1994 | retry_mode = 0; |
| 1995 | } |
| 1996 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1997 | if (!readlen) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1998 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2000 | /* For subsequent reads align to page boundary */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | col = 0; |
| 2002 | /* Increment page address */ |
| 2003 | realpage++; |
| 2004 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2005 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | /* Check, if we cross a chip boundary */ |
| 2007 | if (!page) { |
| 2008 | chipnr++; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2009 | chip->select_chip(mtd, -1); |
| 2010 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | } |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2013 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2015 | ops->retlen = ops->len - (size_t) readlen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2016 | if (oob) |
| 2017 | ops->oobretlen = ops->ooblen - oobreadlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2019 | if (ret < 0) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2020 | return ret; |
| 2021 | |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 2022 | if (ecc_fail) |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 2023 | return -EBADMSG; |
| 2024 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2025 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2029 | * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2030 | * @mtd: MTD device structure |
| 2031 | * @from: offset to read from |
| 2032 | * @len: number of bytes to read |
| 2033 | * @retlen: pointer to variable to store the number of read bytes |
| 2034 | * @buf: the databuffer to put data |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2035 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2036 | * Get hold of the chip and call nand_do_read. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2037 | */ |
| 2038 | static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 2039 | size_t *retlen, uint8_t *buf) |
| 2040 | { |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2041 | struct mtd_oob_ops ops; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2042 | int ret; |
| 2043 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 2044 | nand_get_device(mtd, FL_READING); |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 2045 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2046 | ops.len = len; |
| 2047 | ops.datbuf = buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 2048 | ops.mode = MTD_OPS_PLACE_OOB; |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2049 | ret = nand_do_read_ops(mtd, from, &ops); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2050 | *retlen = ops.retlen; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2051 | nand_release_device(mtd); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2052 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2056 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2057 | * @mtd: mtd info structure |
| 2058 | * @chip: nand chip info structure |
| 2059 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2060 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2061 | int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2062 | { |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 2063 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2064 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 2065 | return 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2066 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2067 | EXPORT_SYMBOL(nand_read_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2068 | |
| 2069 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2070 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2071 | * with syndromes |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2072 | * @mtd: mtd info structure |
| 2073 | * @chip: nand chip info structure |
| 2074 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2075 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2076 | int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 2077 | int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2078 | { |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2079 | int length = mtd->oobsize; |
| 2080 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2081 | int eccsize = chip->ecc.size; |
Baruch Siach | 2ea69d2 | 2015-01-22 15:23:05 +0200 | [diff] [blame] | 2082 | uint8_t *bufpoi = chip->oob_poi; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2083 | int i, toread, sndrnd = 0, pos; |
| 2084 | |
| 2085 | chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page); |
| 2086 | for (i = 0; i < chip->ecc.steps; i++) { |
| 2087 | if (sndrnd) { |
| 2088 | pos = eccsize + i * (eccsize + chunk); |
| 2089 | if (mtd->writesize > 512) |
| 2090 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1); |
| 2091 | else |
| 2092 | chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page); |
| 2093 | } else |
| 2094 | sndrnd = 1; |
| 2095 | toread = min_t(int, length, chunk); |
| 2096 | chip->read_buf(mtd, bufpoi, toread); |
| 2097 | bufpoi += toread; |
| 2098 | length -= toread; |
| 2099 | } |
| 2100 | if (length > 0) |
| 2101 | chip->read_buf(mtd, bufpoi, length); |
| 2102 | |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 2103 | return 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2104 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2105 | EXPORT_SYMBOL(nand_read_oob_syndrome); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2106 | |
| 2107 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2108 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2109 | * @mtd: mtd info structure |
| 2110 | * @chip: nand chip info structure |
| 2111 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2112 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2113 | int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2114 | { |
| 2115 | int status = 0; |
| 2116 | const uint8_t *buf = chip->oob_poi; |
| 2117 | int length = mtd->oobsize; |
| 2118 | |
| 2119 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page); |
| 2120 | chip->write_buf(mtd, buf, length); |
| 2121 | /* Send command to program the OOB data */ |
| 2122 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 2123 | |
| 2124 | status = chip->waitfunc(mtd, chip); |
| 2125 | |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 2126 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2127 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2128 | EXPORT_SYMBOL(nand_write_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2129 | |
| 2130 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2131 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2132 | * with syndrome - only for large page flash |
| 2133 | * @mtd: mtd info structure |
| 2134 | * @chip: nand chip info structure |
| 2135 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2136 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2137 | int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 2138 | int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2139 | { |
| 2140 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2141 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
| 2142 | int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps; |
| 2143 | const uint8_t *bufpoi = chip->oob_poi; |
| 2144 | |
| 2145 | /* |
| 2146 | * data-ecc-data-ecc ... ecc-oob |
| 2147 | * or |
| 2148 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
| 2149 | */ |
| 2150 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 2151 | pos = steps * (eccsize + chunk); |
| 2152 | steps = 0; |
| 2153 | } else |
Vitaly Wool | 8b0036e | 2006-07-11 09:11:25 +0200 | [diff] [blame] | 2154 | pos = eccsize; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2155 | |
| 2156 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page); |
| 2157 | for (i = 0; i < steps; i++) { |
| 2158 | if (sndcmd) { |
| 2159 | if (mtd->writesize <= 512) { |
| 2160 | uint32_t fill = 0xFFFFFFFF; |
| 2161 | |
| 2162 | len = eccsize; |
| 2163 | while (len > 0) { |
| 2164 | int num = min_t(int, len, 4); |
| 2165 | chip->write_buf(mtd, (uint8_t *)&fill, |
| 2166 | num); |
| 2167 | len -= num; |
| 2168 | } |
| 2169 | } else { |
| 2170 | pos = eccsize + i * (eccsize + chunk); |
| 2171 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1); |
| 2172 | } |
| 2173 | } else |
| 2174 | sndcmd = 1; |
| 2175 | len = min_t(int, length, chunk); |
| 2176 | chip->write_buf(mtd, bufpoi, len); |
| 2177 | bufpoi += len; |
| 2178 | length -= len; |
| 2179 | } |
| 2180 | if (length > 0) |
| 2181 | chip->write_buf(mtd, bufpoi, length); |
| 2182 | |
| 2183 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 2184 | status = chip->waitfunc(mtd, chip); |
| 2185 | |
| 2186 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
| 2187 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2188 | EXPORT_SYMBOL(nand_write_oob_syndrome); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2189 | |
| 2190 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2191 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2192 | * @mtd: MTD device structure |
| 2193 | * @from: offset to read from |
| 2194 | * @ops: oob operations description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2196 | * NAND read out-of-band data from the spare area. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2198 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 2199 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | { |
Brian Norris | c00a099 | 2012-05-01 17:12:54 -0700 | [diff] [blame] | 2201 | int page, realpage, chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2202 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 2203 | struct mtd_ecc_stats stats; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2204 | int readlen = ops->ooblen; |
| 2205 | int len; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2206 | uint8_t *buf = ops->oobbuf; |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2207 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2209 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2210 | __func__, (unsigned long long)from, readlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 2212 | stats = mtd->ecc_stats; |
| 2213 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 2214 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2215 | |
| 2216 | if (unlikely(ops->ooboffs >= len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2217 | pr_debug("%s: attempt to start read outside oob\n", |
| 2218 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2219 | return -EINVAL; |
| 2220 | } |
| 2221 | |
| 2222 | /* Do not allow reads past end of device */ |
| 2223 | if (unlikely(from >= mtd->size || |
| 2224 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - |
| 2225 | (from >> chip->page_shift)) * len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2226 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2227 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2228 | return -EINVAL; |
| 2229 | } |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2230 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2231 | chipnr = (int)(from >> chip->chip_shift); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2232 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2234 | /* Shift to get page */ |
| 2235 | realpage = (int)(from >> chip->page_shift); |
| 2236 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2238 | while (1) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2239 | if (ops->mode == MTD_OPS_RAW) |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2240 | ret = chip->ecc.read_oob_raw(mtd, chip, page); |
Brian Norris | c46f648 | 2011-08-30 18:45:38 -0700 | [diff] [blame] | 2241 | else |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2242 | ret = chip->ecc.read_oob(mtd, chip, page); |
| 2243 | |
| 2244 | if (ret < 0) |
| 2245 | break; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2246 | |
| 2247 | len = min(len, readlen); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2248 | buf = nand_transfer_oob(mtd, buf, ops, len); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2249 | |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 2250 | if (chip->options & NAND_NEED_READRDY) { |
| 2251 | /* Apply delay or wait for ready/busy pin */ |
| 2252 | if (!chip->dev_ready) |
| 2253 | udelay(chip->chip_delay); |
| 2254 | else |
| 2255 | nand_wait_ready(mtd); |
| 2256 | } |
| 2257 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2258 | readlen -= len; |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 2259 | if (!readlen) |
| 2260 | break; |
| 2261 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2262 | /* Increment page address */ |
| 2263 | realpage++; |
| 2264 | |
| 2265 | page = realpage & chip->pagemask; |
| 2266 | /* Check, if we cross a chip boundary */ |
| 2267 | if (!page) { |
| 2268 | chipnr++; |
| 2269 | chip->select_chip(mtd, -1); |
| 2270 | chip->select_chip(mtd, chipnr); |
| 2271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | } |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2273 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2275 | ops->oobretlen = ops->ooblen - readlen; |
| 2276 | |
| 2277 | if (ret < 0) |
| 2278 | return ret; |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 2279 | |
| 2280 | if (mtd->ecc_stats.failed - stats.failed) |
| 2281 | return -EBADMSG; |
| 2282 | |
| 2283 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2287 | * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2288 | * @mtd: MTD device structure |
| 2289 | * @from: offset to read from |
| 2290 | * @ops: oob operation description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2292 | * NAND read data and/or out-of-band data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2294 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 2295 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | { |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 2297 | int ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2298 | |
| 2299 | ops->retlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | |
| 2301 | /* Do not allow reads past end of device */ |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2302 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2303 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2304 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | return -EINVAL; |
| 2306 | } |
| 2307 | |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 2308 | if (ops->mode != MTD_OPS_PLACE_OOB && |
| 2309 | ops->mode != MTD_OPS_AUTO_OOB && |
| 2310 | ops->mode != MTD_OPS_RAW) |
| 2311 | return -ENOTSUPP; |
| 2312 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 2313 | nand_get_device(mtd, FL_READING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2315 | if (!ops->datbuf) |
| 2316 | ret = nand_do_read_oob(mtd, from, ops); |
| 2317 | else |
| 2318 | ret = nand_do_read_ops(mtd, from, ops); |
| 2319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | nand_release_device(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2321 | return ret; |
| 2322 | } |
| 2323 | |
| 2324 | |
| 2325 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2326 | * nand_write_page_raw - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2327 | * @mtd: mtd info structure |
| 2328 | * @chip: nand chip info structure |
| 2329 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2330 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2331 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2332 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2333 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2334 | */ |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 2335 | int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 2336 | const uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2337 | { |
| 2338 | chip->write_buf(mtd, buf, mtd->writesize); |
Brian Norris | 279f08d | 2012-05-02 10:15:03 -0700 | [diff] [blame] | 2339 | if (oob_required) |
| 2340 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2341 | |
| 2342 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2343 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 2344 | EXPORT_SYMBOL(nand_write_page_raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2346 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2347 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2348 | * @mtd: mtd info structure |
| 2349 | * @chip: nand chip info structure |
| 2350 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2351 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2352 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2353 | * |
| 2354 | * We need a special oob layout and handling even when ECC isn't checked. |
| 2355 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2356 | static int nand_write_page_raw_syndrome(struct mtd_info *mtd, |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 2357 | struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2358 | const uint8_t *buf, int oob_required, |
| 2359 | int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2360 | { |
| 2361 | int eccsize = chip->ecc.size; |
| 2362 | int eccbytes = chip->ecc.bytes; |
| 2363 | uint8_t *oob = chip->oob_poi; |
| 2364 | int steps, size; |
| 2365 | |
| 2366 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
| 2367 | chip->write_buf(mtd, buf, eccsize); |
| 2368 | buf += eccsize; |
| 2369 | |
| 2370 | if (chip->ecc.prepad) { |
| 2371 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 2372 | oob += chip->ecc.prepad; |
| 2373 | } |
| 2374 | |
Boris BREZILLON | 60c3bc1 | 2014-02-01 19:10:28 +0100 | [diff] [blame] | 2375 | chip->write_buf(mtd, oob, eccbytes); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2376 | oob += eccbytes; |
| 2377 | |
| 2378 | if (chip->ecc.postpad) { |
| 2379 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 2380 | oob += chip->ecc.postpad; |
| 2381 | } |
| 2382 | } |
| 2383 | |
| 2384 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 2385 | if (size) |
| 2386 | chip->write_buf(mtd, oob, size); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2387 | |
| 2388 | return 0; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2389 | } |
| 2390 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2391 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2392 | * @mtd: mtd info structure |
| 2393 | * @chip: nand chip info structure |
| 2394 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2395 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2396 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2397 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2398 | static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2399 | const uint8_t *buf, int oob_required, |
| 2400 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2401 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2402 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2403 | int eccbytes = chip->ecc.bytes; |
| 2404 | int eccsteps = chip->ecc.steps; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2405 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2406 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2407 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2408 | /* Software ECC calculation */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2409 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 2410 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2411 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2412 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 2413 | chip->ecc.total); |
| 2414 | if (ret) |
| 2415 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2416 | |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2417 | return chip->ecc.write_page_raw(mtd, chip, buf, 1, page); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2421 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2422 | * @mtd: mtd info structure |
| 2423 | * @chip: nand chip info structure |
| 2424 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2425 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2426 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2427 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2428 | static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2429 | const uint8_t *buf, int oob_required, |
| 2430 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2431 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2432 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2433 | int eccbytes = chip->ecc.bytes; |
| 2434 | int eccsteps = chip->ecc.steps; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2435 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2436 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2437 | |
| 2438 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2439 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
David Woodhouse | 29da9ce | 2006-05-26 23:05:44 +0100 | [diff] [blame] | 2440 | chip->write_buf(mtd, p, eccsize); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2441 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2442 | } |
| 2443 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2444 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 2445 | chip->ecc.total); |
| 2446 | if (ret) |
| 2447 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2448 | |
| 2449 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2450 | |
| 2451 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2452 | } |
| 2453 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2454 | |
| 2455 | /** |
Brian Norris | 73c8aaf | 2015-02-28 02:04:18 -0800 | [diff] [blame] | 2456 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2457 | * @mtd: mtd info structure |
| 2458 | * @chip: nand chip info structure |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2459 | * @offset: column address of subpage within the page |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2460 | * @data_len: data length |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2461 | * @buf: data buffer |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2462 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2463 | * @page: page number to write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2464 | */ |
| 2465 | static int nand_write_subpage_hwecc(struct mtd_info *mtd, |
| 2466 | struct nand_chip *chip, uint32_t offset, |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2467 | uint32_t data_len, const uint8_t *buf, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2468 | int oob_required, int page) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2469 | { |
| 2470 | uint8_t *oob_buf = chip->oob_poi; |
| 2471 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2472 | int ecc_size = chip->ecc.size; |
| 2473 | int ecc_bytes = chip->ecc.bytes; |
| 2474 | int ecc_steps = chip->ecc.steps; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2475 | uint32_t start_step = offset / ecc_size; |
| 2476 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 2477 | int oob_bytes = mtd->oobsize / ecc_steps; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2478 | int step, ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2479 | |
| 2480 | for (step = 0; step < ecc_steps; step++) { |
| 2481 | /* configure controller for WRITE access */ |
| 2482 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2483 | |
| 2484 | /* write data (untouched subpages already masked by 0xFF) */ |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2485 | chip->write_buf(mtd, buf, ecc_size); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2486 | |
| 2487 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 2488 | if ((step < start_step) || (step > end_step)) |
| 2489 | memset(ecc_calc, 0xff, ecc_bytes); |
| 2490 | else |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2491 | chip->ecc.calculate(mtd, buf, ecc_calc); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2492 | |
| 2493 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 2494 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 2495 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 2496 | memset(oob_buf, 0xff, oob_bytes); |
| 2497 | |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2498 | buf += ecc_size; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2499 | ecc_calc += ecc_bytes; |
| 2500 | oob_buf += oob_bytes; |
| 2501 | } |
| 2502 | |
| 2503 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 2504 | /* this include masked-value(0xFF) for unwritten subpages */ |
| 2505 | ecc_calc = chip->buffers->ecccalc; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2506 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 2507 | chip->ecc.total); |
| 2508 | if (ret) |
| 2509 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2510 | |
| 2511 | /* write OOB buffer to NAND device */ |
| 2512 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 2513 | |
| 2514 | return 0; |
| 2515 | } |
| 2516 | |
| 2517 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2518 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2519 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2520 | * @mtd: mtd info structure |
| 2521 | * @chip: nand chip info structure |
| 2522 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2523 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2524 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2525 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2526 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 2527 | * need a special oob layout and handling. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2528 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2529 | static int nand_write_page_syndrome(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2530 | struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2531 | const uint8_t *buf, int oob_required, |
| 2532 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2533 | { |
| 2534 | int i, eccsize = chip->ecc.size; |
| 2535 | int eccbytes = chip->ecc.bytes; |
| 2536 | int eccsteps = chip->ecc.steps; |
| 2537 | const uint8_t *p = buf; |
| 2538 | uint8_t *oob = chip->oob_poi; |
| 2539 | |
| 2540 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2541 | |
| 2542 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2543 | chip->write_buf(mtd, p, eccsize); |
| 2544 | |
| 2545 | if (chip->ecc.prepad) { |
| 2546 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 2547 | oob += chip->ecc.prepad; |
| 2548 | } |
| 2549 | |
| 2550 | chip->ecc.calculate(mtd, p, oob); |
| 2551 | chip->write_buf(mtd, oob, eccbytes); |
| 2552 | oob += eccbytes; |
| 2553 | |
| 2554 | if (chip->ecc.postpad) { |
| 2555 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 2556 | oob += chip->ecc.postpad; |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 2561 | i = mtd->oobsize - (oob - chip->oob_poi); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2562 | if (i) |
| 2563 | chip->write_buf(mtd, oob, i); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2564 | |
| 2565 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | /** |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 2569 | * nand_write_page - write one page |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2570 | * @mtd: MTD device structure |
| 2571 | * @chip: NAND chip descriptor |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2572 | * @offset: address offset within the page |
| 2573 | * @data_len: length of actual data to be written |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2574 | * @buf: the data to write |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2575 | * @oob_required: must write chip->oob_poi to OOB |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2576 | * @page: page number to write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2577 | * @raw: use _raw version of write_page |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2578 | */ |
| 2579 | static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2580 | uint32_t offset, int data_len, const uint8_t *buf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 2581 | int oob_required, int page, int raw) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2582 | { |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2583 | int status, subpage; |
| 2584 | |
| 2585 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 2586 | chip->ecc.write_subpage) |
| 2587 | subpage = offset || (data_len < mtd->writesize); |
| 2588 | else |
| 2589 | subpage = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2590 | |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 2591 | if (nand_standard_page_accessors(&chip->ecc)) |
| 2592 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2593 | |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 2594 | if (unlikely(raw)) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2595 | status = chip->ecc.write_page_raw(mtd, chip, buf, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2596 | oob_required, page); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2597 | else if (subpage) |
| 2598 | status = chip->ecc.write_subpage(mtd, chip, offset, data_len, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2599 | buf, oob_required, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 2600 | else |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2601 | status = chip->ecc.write_page(mtd, chip, buf, oob_required, |
| 2602 | page); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2603 | |
| 2604 | if (status < 0) |
| 2605 | return status; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2606 | |
Boris Brezillon | 4114564 | 2017-05-16 18:27:49 +0200 | [diff] [blame] | 2607 | if (nand_standard_page_accessors(&chip->ecc)) { |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 2608 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2609 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2610 | status = chip->waitfunc(mtd, chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2611 | if (status & NAND_STATUS_FAIL) |
| 2612 | return -EIO; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2613 | } |
| 2614 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2615 | return 0; |
| 2616 | } |
| 2617 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2618 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2619 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2620 | * @mtd: MTD device structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2621 | * @oob: oob data buffer |
| 2622 | * @len: oob data write length |
| 2623 | * @ops: oob ops structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2624 | */ |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2625 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
| 2626 | struct mtd_oob_ops *ops) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2627 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2628 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2629 | int ret; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2630 | |
| 2631 | /* |
| 2632 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 2633 | * data from a previous OOB read. |
| 2634 | */ |
| 2635 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 2636 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2637 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2638 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2639 | case MTD_OPS_PLACE_OOB: |
| 2640 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2641 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 2642 | return oob + len; |
| 2643 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2644 | case MTD_OPS_AUTO_OOB: |
| 2645 | ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi, |
| 2646 | ops->ooboffs, len); |
| 2647 | BUG_ON(ret); |
| 2648 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2649 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2650 | default: |
| 2651 | BUG(); |
| 2652 | } |
| 2653 | return NULL; |
| 2654 | } |
| 2655 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2656 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2657 | |
| 2658 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2659 | * nand_do_write_ops - [INTERN] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2660 | * @mtd: MTD device structure |
| 2661 | * @to: offset to write to |
| 2662 | * @ops: oob operations description structure |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2663 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2664 | * NAND write with ECC. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2665 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2666 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 2667 | struct mtd_oob_ops *ops) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2668 | { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2669 | int chipnr, realpage, page, blockmask, column; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2670 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2671 | uint32_t writelen = ops->len; |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2672 | |
| 2673 | uint32_t oobwritelen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 2674 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2675 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2676 | uint8_t *oob = ops->oobbuf; |
| 2677 | uint8_t *buf = ops->datbuf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2678 | int ret; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 2679 | int oob_required = oob ? 1 : 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2680 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2681 | ops->retlen = 0; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2682 | if (!writelen) |
| 2683 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2684 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2685 | /* Reject writes, which are not page aligned */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2686 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 2687 | pr_notice("%s: attempt to write non page aligned data\n", |
| 2688 | __func__); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2689 | return -EINVAL; |
| 2690 | } |
| 2691 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2692 | column = to & (mtd->writesize - 1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2693 | |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 2694 | chipnr = (int)(to >> chip->chip_shift); |
| 2695 | chip->select_chip(mtd, chipnr); |
| 2696 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2697 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2698 | if (nand_check_wp(mtd)) { |
| 2699 | ret = -EIO; |
| 2700 | goto err_out; |
| 2701 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2702 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2703 | realpage = (int)(to >> chip->page_shift); |
| 2704 | page = realpage & chip->pagemask; |
| 2705 | blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; |
| 2706 | |
| 2707 | /* Invalidate the page cache, when we write to the cached page */ |
Brian Norris | 537ab1b | 2014-07-21 19:08:03 -0700 | [diff] [blame] | 2708 | if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && |
| 2709 | ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2710 | chip->pagebuf = -1; |
| 2711 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2712 | /* Don't allow multipage oob writes with offset */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2713 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 2714 | ret = -EINVAL; |
| 2715 | goto err_out; |
| 2716 | } |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2717 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2718 | while (1) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2719 | int bytes = mtd->writesize; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2720 | uint8_t *wbuf = buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2721 | int use_bufpoi; |
Hector Palacios | 144f4c9 | 2016-07-18 10:39:18 +0200 | [diff] [blame] | 2722 | int part_pagewr = (column || writelen < mtd->writesize); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2723 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2724 | if (part_pagewr) |
| 2725 | use_bufpoi = 1; |
| 2726 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 2727 | use_bufpoi = !virt_addr_valid(buf) || |
| 2728 | !IS_ALIGNED((unsigned long)buf, |
| 2729 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2730 | else |
| 2731 | use_bufpoi = 0; |
| 2732 | |
| 2733 | /* Partial page write?, or need to use bounce buffer */ |
| 2734 | if (use_bufpoi) { |
| 2735 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 2736 | __func__, buf); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2737 | if (part_pagewr) |
| 2738 | bytes = min_t(int, bytes - column, writelen); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2739 | chip->pagebuf = -1; |
| 2740 | memset(chip->buffers->databuf, 0xff, mtd->writesize); |
| 2741 | memcpy(&chip->buffers->databuf[column], buf, bytes); |
| 2742 | wbuf = chip->buffers->databuf; |
| 2743 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2744 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2745 | if (unlikely(oob)) { |
| 2746 | size_t len = min(oobwritelen, oobmaxlen); |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2747 | oob = nand_fill_oob(mtd, oob, len, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2748 | oobwritelen -= len; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2749 | } else { |
| 2750 | /* We still need to erase leftover OOB data */ |
| 2751 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2752 | } |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 2753 | |
| 2754 | ret = nand_write_page(mtd, chip, column, bytes, wbuf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 2755 | oob_required, page, |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 2756 | (ops->mode == MTD_OPS_RAW)); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2757 | if (ret) |
| 2758 | break; |
| 2759 | |
| 2760 | writelen -= bytes; |
| 2761 | if (!writelen) |
| 2762 | break; |
| 2763 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2764 | column = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2765 | buf += bytes; |
| 2766 | realpage++; |
| 2767 | |
| 2768 | page = realpage & chip->pagemask; |
| 2769 | /* Check, if we cross a chip boundary */ |
| 2770 | if (!page) { |
| 2771 | chipnr++; |
| 2772 | chip->select_chip(mtd, -1); |
| 2773 | chip->select_chip(mtd, chipnr); |
| 2774 | } |
| 2775 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2776 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2777 | ops->retlen = ops->len - writelen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2778 | if (unlikely(oob)) |
| 2779 | ops->oobretlen = ops->ooblen; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2780 | |
| 2781 | err_out: |
| 2782 | chip->select_chip(mtd, -1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2783 | return ret; |
| 2784 | } |
| 2785 | |
| 2786 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2787 | * panic_nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2788 | * @mtd: MTD device structure |
| 2789 | * @to: offset to write to |
| 2790 | * @len: number of bytes to write |
| 2791 | * @retlen: pointer to variable to store the number of written bytes |
| 2792 | * @buf: the data to write |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2793 | * |
| 2794 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 2795 | * may for example be called by mtdoops when writing an oops while in panic. |
| 2796 | */ |
| 2797 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 2798 | size_t *retlen, const uint8_t *buf) |
| 2799 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2800 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2801 | struct mtd_oob_ops ops; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2802 | int ret; |
| 2803 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2804 | /* Wait for the device to get ready */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2805 | panic_nand_wait(mtd, chip, 400); |
| 2806 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2807 | /* Grab the device */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2808 | panic_nand_get_device(chip, mtd, FL_WRITING); |
| 2809 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 2810 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2811 | ops.len = len; |
| 2812 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 2813 | ops.mode = MTD_OPS_PLACE_OOB; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2814 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2815 | ret = nand_do_write_ops(mtd, to, &ops); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2816 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2817 | *retlen = ops.retlen; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2818 | return ret; |
| 2819 | } |
| 2820 | |
| 2821 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2822 | * nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2823 | * @mtd: MTD device structure |
| 2824 | * @to: offset to write to |
| 2825 | * @len: number of bytes to write |
| 2826 | * @retlen: pointer to variable to store the number of written bytes |
| 2827 | * @buf: the data to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2829 | * NAND write with ECC. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2831 | static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2832 | size_t *retlen, const uint8_t *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | { |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2834 | struct mtd_oob_ops ops; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2835 | int ret; |
| 2836 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 2837 | nand_get_device(mtd, FL_WRITING); |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 2838 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2839 | ops.len = len; |
| 2840 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 2841 | ops.mode = MTD_OPS_PLACE_OOB; |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2842 | ret = nand_do_write_ops(mtd, to, &ops); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2843 | *retlen = ops.retlen; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2844 | nand_release_device(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2845 | return ret; |
| 2846 | } |
| 2847 | |
| 2848 | /** |
| 2849 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2850 | * @mtd: MTD device structure |
| 2851 | * @to: offset to write to |
| 2852 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2853 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2854 | * NAND write out-of-band. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2855 | */ |
| 2856 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 2857 | struct mtd_oob_ops *ops) |
| 2858 | { |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2859 | int chipnr, page, status, len; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2860 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2862 | pr_debug("%s: to = 0x%08x, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2863 | __func__, (unsigned int)to, (int)ops->ooblen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 2865 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2867 | /* Do not allow write past end of page */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2868 | if ((ops->ooboffs + ops->ooblen) > len) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2869 | pr_debug("%s: attempt to write past end of page\n", |
| 2870 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | return -EINVAL; |
| 2872 | } |
| 2873 | |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2874 | if (unlikely(ops->ooboffs >= len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2875 | pr_debug("%s: attempt to start write outside oob\n", |
| 2876 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2877 | return -EINVAL; |
| 2878 | } |
| 2879 | |
Jason Liu | 775adc3d4 | 2011-02-25 13:06:18 +0800 | [diff] [blame] | 2880 | /* Do not allow write past end of device */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2881 | if (unlikely(to >= mtd->size || |
| 2882 | ops->ooboffs + ops->ooblen > |
| 2883 | ((mtd->size >> chip->page_shift) - |
| 2884 | (to >> chip->page_shift)) * len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2885 | pr_debug("%s: attempt to write beyond end of device\n", |
| 2886 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2887 | return -EINVAL; |
| 2888 | } |
| 2889 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2890 | chipnr = (int)(to >> chip->chip_shift); |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2891 | |
| 2892 | /* |
| 2893 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 2894 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 2895 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 2896 | * it in the doc2000 driver in August 1999. dwmw2. |
| 2897 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2898 | nand_reset(chip, chipnr); |
| 2899 | |
| 2900 | chip->select_chip(mtd, chipnr); |
| 2901 | |
| 2902 | /* Shift to get page */ |
| 2903 | page = (int)(to >> chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | |
| 2905 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2906 | if (nand_check_wp(mtd)) { |
| 2907 | chip->select_chip(mtd, -1); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2908 | return -EROFS; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2909 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2911 | /* Invalidate the page cache, if we write to the cached page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2912 | if (page == chip->pagebuf) |
| 2913 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2914 | |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2915 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 2916 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2917 | if (ops->mode == MTD_OPS_RAW) |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 2918 | status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask); |
| 2919 | else |
| 2920 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2921 | |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2922 | chip->select_chip(mtd, -1); |
| 2923 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2924 | if (status) |
| 2925 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2927 | ops->oobretlen = ops->ooblen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2929 | return 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2932 | /** |
| 2933 | * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2934 | * @mtd: MTD device structure |
| 2935 | * @to: offset to write to |
| 2936 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2937 | */ |
| 2938 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 2939 | struct mtd_oob_ops *ops) |
| 2940 | { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2941 | int ret = -ENOTSUPP; |
| 2942 | |
| 2943 | ops->retlen = 0; |
| 2944 | |
| 2945 | /* Do not allow writes past end of device */ |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2946 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2947 | pr_debug("%s: attempt to write beyond end of device\n", |
| 2948 | __func__); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2949 | return -EINVAL; |
| 2950 | } |
| 2951 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 2952 | nand_get_device(mtd, FL_WRITING); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2953 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2954 | switch (ops->mode) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2955 | case MTD_OPS_PLACE_OOB: |
| 2956 | case MTD_OPS_AUTO_OOB: |
| 2957 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2958 | break; |
| 2959 | |
| 2960 | default: |
| 2961 | goto out; |
| 2962 | } |
| 2963 | |
| 2964 | if (!ops->datbuf) |
| 2965 | ret = nand_do_write_oob(mtd, to, ops); |
| 2966 | else |
| 2967 | ret = nand_do_write_ops(mtd, to, ops); |
| 2968 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 2969 | out: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2970 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | return ret; |
| 2972 | } |
| 2973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | /** |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 2975 | * single_erase - [GENERIC] NAND standard block erase command function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2976 | * @mtd: MTD device structure |
| 2977 | * @page: the page address of the block which will be erased |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | * |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 2979 | * Standard erase command for NAND chips. Returns NAND status. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | */ |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 2981 | static int single_erase(struct mtd_info *mtd, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2983 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | /* Send commands to erase a block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2985 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 2986 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 2987 | |
| 2988 | return chip->waitfunc(mtd, chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2992 | * nand_erase - [MTD Interface] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2993 | * @mtd: MTD device structure |
| 2994 | * @instr: erase instruction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2995 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2996 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2998 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3000 | return nand_erase_nand(mtd, instr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3002 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3004 | * nand_erase_nand - [INTERN] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3005 | * @mtd: MTD device structure |
| 3006 | * @instr: erase instruction |
| 3007 | * @allowbbt: allow erasing the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3009 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3010 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3011 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
| 3012 | int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | { |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3014 | int page, status, pages_per_block, ret, chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3015 | struct nand_chip *chip = mtd_to_nand(mtd); |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3016 | loff_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3017 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3018 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 3019 | __func__, (unsigned long long)instr->addr, |
| 3020 | (unsigned long long)instr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 3022 | if (check_offs_len(mtd, instr->addr, instr->len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3023 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3025 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3026 | nand_get_device(mtd, FL_ERASING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3027 | |
| 3028 | /* Shift to get first page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3029 | page = (int)(instr->addr >> chip->page_shift); |
| 3030 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | |
| 3032 | /* Calculate pages in each block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3033 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | |
| 3035 | /* Select the NAND device */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3036 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | /* Check, if it is write protected */ |
| 3039 | if (nand_check_wp(mtd)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3040 | pr_debug("%s: device is write protected!\n", |
| 3041 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | instr->state = MTD_ERASE_FAILED; |
| 3043 | goto erase_exit; |
| 3044 | } |
| 3045 | |
| 3046 | /* Loop through the pages */ |
| 3047 | len = instr->len; |
| 3048 | |
| 3049 | instr->state = MTD_ERASING; |
| 3050 | |
| 3051 | while (len) { |
Wolfram Sang | 12183a2 | 2011-12-21 23:01:20 +0100 | [diff] [blame] | 3052 | /* Check if we have a bad block, we do not erase bad blocks! */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3053 | if (nand_block_checkbad(mtd, ((loff_t) page) << |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 3054 | chip->page_shift, allowbbt)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 3055 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
| 3056 | __func__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | instr->state = MTD_ERASE_FAILED; |
| 3058 | goto erase_exit; |
| 3059 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3060 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3061 | /* |
| 3062 | * Invalidate the page cache, if we erase the block which |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3063 | * contains the current cached page. |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3064 | */ |
| 3065 | if (page <= chip->pagebuf && chip->pagebuf < |
| 3066 | (page + pages_per_block)) |
| 3067 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3069 | status = chip->erase(mtd, page & chip->pagemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3070 | |
| 3071 | /* See if block erase succeeded */ |
David A. Marlin | a4ab4c5 | 2005-01-23 18:30:53 +0000 | [diff] [blame] | 3072 | if (status & NAND_STATUS_FAIL) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3073 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 3074 | __func__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3075 | instr->state = MTD_ERASE_FAILED; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3076 | instr->fail_addr = |
| 3077 | ((loff_t)page << chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | goto erase_exit; |
| 3079 | } |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 3080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | /* Increment page address and decrement length */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 3082 | len -= (1ULL << chip->phys_erase_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | page += pages_per_block; |
| 3084 | |
| 3085 | /* Check, if we cross a chip boundary */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3086 | if (len && !(page & chip->pagemask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3087 | chipnr++; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3088 | chip->select_chip(mtd, -1); |
| 3089 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | } |
| 3091 | } |
| 3092 | instr->state = MTD_ERASE_DONE; |
| 3093 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3094 | erase_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | |
| 3096 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | |
| 3098 | /* Deselect and wake up anyone waiting on the device */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3099 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | nand_release_device(mtd); |
| 3101 | |
David Woodhouse | 49defc0 | 2007-10-06 15:01:59 -0400 | [diff] [blame] | 3102 | /* Do call back function */ |
| 3103 | if (!ret) |
| 3104 | mtd_erase_callback(instr); |
| 3105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | /* Return more or less happy */ |
| 3107 | return ret; |
| 3108 | } |
| 3109 | |
| 3110 | /** |
| 3111 | * nand_sync - [MTD Interface] sync |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3112 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3114 | * Sync is actually a wait for chip ready function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3116 | static void nand_sync(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3118 | pr_debug("%s: called\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | |
| 3120 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3121 | nand_get_device(mtd, FL_SYNCING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | /* Release it and go back */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3123 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3124 | } |
| 3125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3127 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3128 | * @mtd: MTD device structure |
| 3129 | * @offs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3130 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3131 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | { |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 3133 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3134 | int chipnr = (int)(offs >> chip->chip_shift); |
| 3135 | int ret; |
| 3136 | |
| 3137 | /* Select the NAND device */ |
| 3138 | nand_get_device(mtd, FL_READING); |
| 3139 | chip->select_chip(mtd, chipnr); |
| 3140 | |
| 3141 | ret = nand_block_checkbad(mtd, offs, 0); |
| 3142 | |
| 3143 | chip->select_chip(mtd, -1); |
| 3144 | nand_release_device(mtd); |
| 3145 | |
| 3146 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3150 | * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3151 | * @mtd: MTD device structure |
| 3152 | * @ofs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3154 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | int ret; |
| 3157 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3158 | ret = nand_block_isbad(mtd, ofs); |
| 3159 | if (ret) { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3160 | /* If it was bad already, return success and do nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | if (ret > 0) |
| 3162 | return 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3163 | return ret; |
| 3164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3165 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 3166 | return nand_block_markbad_lowlevel(mtd, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | /** |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 3170 | * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd |
| 3171 | * @mtd: MTD device structure |
| 3172 | * @ofs: offset relative to mtd start |
| 3173 | * @len: length of mtd |
| 3174 | */ |
| 3175 | static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len) |
| 3176 | { |
| 3177 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3178 | u32 part_start_block; |
| 3179 | u32 part_end_block; |
| 3180 | u32 part_start_die; |
| 3181 | u32 part_end_die; |
| 3182 | |
| 3183 | /* |
| 3184 | * max_bb_per_die and blocks_per_die used to determine |
| 3185 | * the maximum bad block count. |
| 3186 | */ |
| 3187 | if (!chip->max_bb_per_die || !chip->blocks_per_die) |
| 3188 | return -ENOTSUPP; |
| 3189 | |
| 3190 | /* Get the start and end of the partition in erase blocks. */ |
| 3191 | part_start_block = mtd_div_by_eb(ofs, mtd); |
| 3192 | part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1; |
| 3193 | |
| 3194 | /* Get the start and end LUNs of the partition. */ |
| 3195 | part_start_die = part_start_block / chip->blocks_per_die; |
| 3196 | part_end_die = part_end_block / chip->blocks_per_die; |
| 3197 | |
| 3198 | /* |
| 3199 | * Look up the bad blocks per unit and multiply by the number of units |
| 3200 | * that the partition spans. |
| 3201 | */ |
| 3202 | return chip->max_bb_per_die * (part_end_die - part_start_die + 1); |
| 3203 | } |
| 3204 | |
| 3205 | /** |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3206 | * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand |
| 3207 | * @mtd: MTD device structure |
| 3208 | * @chip: nand chip info structure |
| 3209 | * @addr: feature address. |
| 3210 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3211 | */ |
| 3212 | static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3213 | int addr, uint8_t *subfeature_param) |
| 3214 | { |
| 3215 | int status; |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3216 | int i; |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3217 | |
David Mosberger | d914c93 | 2013-05-29 15:30:13 +0300 | [diff] [blame] | 3218 | if (!chip->onfi_version || |
| 3219 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3220 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3221 | return -EINVAL; |
| 3222 | |
| 3223 | chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3224 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 3225 | chip->write_byte(mtd, subfeature_param[i]); |
| 3226 | |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3227 | status = chip->waitfunc(mtd, chip); |
| 3228 | if (status & NAND_STATUS_FAIL) |
| 3229 | return -EIO; |
| 3230 | return 0; |
| 3231 | } |
| 3232 | |
| 3233 | /** |
| 3234 | * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand |
| 3235 | * @mtd: MTD device structure |
| 3236 | * @chip: nand chip info structure |
| 3237 | * @addr: feature address. |
| 3238 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3239 | */ |
| 3240 | static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3241 | int addr, uint8_t *subfeature_param) |
| 3242 | { |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3243 | int i; |
| 3244 | |
David Mosberger | d914c93 | 2013-05-29 15:30:13 +0300 | [diff] [blame] | 3245 | if (!chip->onfi_version || |
| 3246 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3247 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3248 | return -EINVAL; |
| 3249 | |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3250 | chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3251 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 3252 | *subfeature_param++ = chip->read_byte(mtd); |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3253 | return 0; |
| 3254 | } |
| 3255 | |
| 3256 | /** |
Boris Brezillon | 4a78cc6 | 2017-05-26 17:10:15 +0200 | [diff] [blame] | 3257 | * nand_onfi_get_set_features_notsupp - set/get features stub returning |
| 3258 | * -ENOTSUPP |
| 3259 | * @mtd: MTD device structure |
| 3260 | * @chip: nand chip info structure |
| 3261 | * @addr: feature address. |
| 3262 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3263 | * |
| 3264 | * Should be used by NAND controller drivers that do not support the SET/GET |
| 3265 | * FEATURES operations. |
| 3266 | */ |
| 3267 | int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd, |
| 3268 | struct nand_chip *chip, int addr, |
| 3269 | u8 *subfeature_param) |
| 3270 | { |
| 3271 | return -ENOTSUPP; |
| 3272 | } |
| 3273 | EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp); |
| 3274 | |
| 3275 | /** |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3276 | * nand_suspend - [MTD Interface] Suspend the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3277 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3278 | */ |
| 3279 | static int nand_suspend(struct mtd_info *mtd) |
| 3280 | { |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3281 | return nand_get_device(mtd, FL_PM_SUSPENDED); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3282 | } |
| 3283 | |
| 3284 | /** |
| 3285 | * nand_resume - [MTD Interface] Resume the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3286 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3287 | */ |
| 3288 | static void nand_resume(struct mtd_info *mtd) |
| 3289 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3290 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3291 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3292 | if (chip->state == FL_PM_SUSPENDED) |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3293 | nand_release_device(mtd); |
| 3294 | else |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 3295 | pr_err("%s called for a chip which is not in suspended state\n", |
| 3296 | __func__); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3297 | } |
| 3298 | |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 3299 | /** |
| 3300 | * nand_shutdown - [MTD Interface] Finish the current NAND operation and |
| 3301 | * prevent further operations |
| 3302 | * @mtd: MTD device structure |
| 3303 | */ |
| 3304 | static void nand_shutdown(struct mtd_info *mtd) |
| 3305 | { |
Brian Norris | 9ca641b | 2015-11-09 16:37:28 -0800 | [diff] [blame] | 3306 | nand_get_device(mtd, FL_PM_SUSPENDED); |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 3307 | } |
| 3308 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3309 | /* Set default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3310 | static void nand_set_defaults(struct nand_chip *chip) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3311 | { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3312 | unsigned int busw = chip->options & NAND_BUSWIDTH_16; |
| 3313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3314 | /* check for proper chip_delay setup, set 20us if not */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3315 | if (!chip->chip_delay) |
| 3316 | chip->chip_delay = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3317 | |
| 3318 | /* check, if a user supplied command function given */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3319 | if (chip->cmdfunc == NULL) |
| 3320 | chip->cmdfunc = nand_command; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3321 | |
| 3322 | /* check, if a user supplied wait function given */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3323 | if (chip->waitfunc == NULL) |
| 3324 | chip->waitfunc = nand_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3325 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3326 | if (!chip->select_chip) |
| 3327 | chip->select_chip = nand_select_chip; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3328 | |
Huang Shijie | 4204ccc | 2013-08-16 10:10:07 +0800 | [diff] [blame] | 3329 | /* set for ONFI nand */ |
| 3330 | if (!chip->onfi_set_features) |
| 3331 | chip->onfi_set_features = nand_onfi_set_features; |
| 3332 | if (!chip->onfi_get_features) |
| 3333 | chip->onfi_get_features = nand_onfi_get_features; |
| 3334 | |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3335 | /* If called twice, pointers that depend on busw may need to be reset */ |
| 3336 | if (!chip->read_byte || chip->read_byte == nand_read_byte) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3337 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
| 3338 | if (!chip->read_word) |
| 3339 | chip->read_word = nand_read_word; |
| 3340 | if (!chip->block_bad) |
| 3341 | chip->block_bad = nand_block_bad; |
| 3342 | if (!chip->block_markbad) |
| 3343 | chip->block_markbad = nand_default_block_markbad; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3344 | if (!chip->write_buf || chip->write_buf == nand_write_buf) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3345 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3346 | if (!chip->write_byte || chip->write_byte == nand_write_byte) |
| 3347 | chip->write_byte = busw ? nand_write_byte16 : nand_write_byte; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3348 | if (!chip->read_buf || chip->read_buf == nand_read_buf) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3349 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3350 | if (!chip->scan_bbt) |
| 3351 | chip->scan_bbt = nand_default_bbt; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3352 | |
| 3353 | if (!chip->controller) { |
| 3354 | chip->controller = &chip->hwcontrol; |
Marc Gonzalez | d45bc58 | 2016-07-27 11:23:52 +0200 | [diff] [blame] | 3355 | nand_hw_control_init(chip->controller); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3356 | } |
| 3357 | |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 3358 | if (!chip->buf_align) |
| 3359 | chip->buf_align = 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3360 | } |
| 3361 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3362 | /* Sanitize ONFI strings so we can safely print them */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3363 | static void sanitize_string(uint8_t *s, size_t len) |
| 3364 | { |
| 3365 | ssize_t i; |
| 3366 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3367 | /* Null terminate */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3368 | s[len - 1] = 0; |
| 3369 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3370 | /* Remove non printable chars */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3371 | for (i = 0; i < len - 1; i++) { |
| 3372 | if (s[i] < ' ' || s[i] > 127) |
| 3373 | s[i] = '?'; |
| 3374 | } |
| 3375 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3376 | /* Remove trailing spaces */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3377 | strim(s); |
| 3378 | } |
| 3379 | |
| 3380 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) |
| 3381 | { |
| 3382 | int i; |
| 3383 | while (len--) { |
| 3384 | crc ^= *p++ << 8; |
| 3385 | for (i = 0; i < 8; i++) |
| 3386 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 3387 | } |
| 3388 | |
| 3389 | return crc; |
| 3390 | } |
| 3391 | |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3392 | /* Parse the Extended Parameter Page. */ |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3393 | static int nand_flash_detect_ext_param_page(struct nand_chip *chip, |
| 3394 | struct nand_onfi_params *p) |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3395 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3396 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3397 | struct onfi_ext_param_page *ep; |
| 3398 | struct onfi_ext_section *s; |
| 3399 | struct onfi_ext_ecc_info *ecc; |
| 3400 | uint8_t *cursor; |
| 3401 | int ret = -EINVAL; |
| 3402 | int len; |
| 3403 | int i; |
| 3404 | |
| 3405 | len = le16_to_cpu(p->ext_param_page_length) * 16; |
| 3406 | ep = kmalloc(len, GFP_KERNEL); |
Brian Norris | 5cb1327 | 2013-09-16 17:59:20 -0700 | [diff] [blame] | 3407 | if (!ep) |
| 3408 | return -ENOMEM; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3409 | |
| 3410 | /* Send our own NAND_CMD_PARAM. */ |
| 3411 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
| 3412 | |
| 3413 | /* Use the Change Read Column command to skip the ONFI param pages. */ |
| 3414 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
| 3415 | sizeof(*p) * p->num_of_param_pages , -1); |
| 3416 | |
| 3417 | /* Read out the Extended Parameter Page. */ |
| 3418 | chip->read_buf(mtd, (uint8_t *)ep, len); |
| 3419 | if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2) |
| 3420 | != le16_to_cpu(ep->crc))) { |
| 3421 | pr_debug("fail in the CRC.\n"); |
| 3422 | goto ext_out; |
| 3423 | } |
| 3424 | |
| 3425 | /* |
| 3426 | * Check the signature. |
| 3427 | * Do not strictly follow the ONFI spec, maybe changed in future. |
| 3428 | */ |
| 3429 | if (strncmp(ep->sig, "EPPS", 4)) { |
| 3430 | pr_debug("The signature is invalid.\n"); |
| 3431 | goto ext_out; |
| 3432 | } |
| 3433 | |
| 3434 | /* find the ECC section. */ |
| 3435 | cursor = (uint8_t *)(ep + 1); |
| 3436 | for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) { |
| 3437 | s = ep->sections + i; |
| 3438 | if (s->type == ONFI_SECTION_TYPE_2) |
| 3439 | break; |
| 3440 | cursor += s->length * 16; |
| 3441 | } |
| 3442 | if (i == ONFI_EXT_SECTION_MAX) { |
| 3443 | pr_debug("We can not find the ECC section.\n"); |
| 3444 | goto ext_out; |
| 3445 | } |
| 3446 | |
| 3447 | /* get the info we want. */ |
| 3448 | ecc = (struct onfi_ext_ecc_info *)cursor; |
| 3449 | |
Brian Norris | 4ae7d22 | 2013-09-16 18:20:21 -0700 | [diff] [blame] | 3450 | if (!ecc->codeword_size) { |
| 3451 | pr_debug("Invalid codeword size\n"); |
| 3452 | goto ext_out; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3453 | } |
| 3454 | |
Brian Norris | 4ae7d22 | 2013-09-16 18:20:21 -0700 | [diff] [blame] | 3455 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 3456 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
Brian Norris | 5cb1327 | 2013-09-16 17:59:20 -0700 | [diff] [blame] | 3457 | ret = 0; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3458 | |
| 3459 | ext_out: |
| 3460 | kfree(ep); |
| 3461 | return ret; |
| 3462 | } |
| 3463 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3464 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3465 | * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise. |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3466 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3467 | static int nand_flash_detect_onfi(struct nand_chip *chip) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3468 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3469 | struct mtd_info *mtd = nand_to_mtd(chip); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3470 | struct nand_onfi_params *p = &chip->onfi_params; |
Brian Norris | bd9c6e9 | 2013-11-29 22:04:28 -0800 | [diff] [blame] | 3471 | int i, j; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3472 | int val; |
| 3473 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3474 | /* Try ONFI for unknown chip or LP */ |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3475 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1); |
| 3476 | if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' || |
| 3477 | chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') |
| 3478 | return 0; |
| 3479 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3480 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
| 3481 | for (i = 0; i < 3; i++) { |
Brian Norris | bd9c6e9 | 2013-11-29 22:04:28 -0800 | [diff] [blame] | 3482 | for (j = 0; j < sizeof(*p); j++) |
| 3483 | ((uint8_t *)p)[j] = chip->read_byte(mtd); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3484 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == |
| 3485 | le16_to_cpu(p->crc)) { |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3486 | break; |
| 3487 | } |
| 3488 | } |
| 3489 | |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 3490 | if (i == 3) { |
| 3491 | pr_err("Could not find valid ONFI parameter page; aborting\n"); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3492 | return 0; |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 3493 | } |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3494 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3495 | /* Check version */ |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3496 | val = le16_to_cpu(p->revision); |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3497 | if (val & (1 << 5)) |
| 3498 | chip->onfi_version = 23; |
| 3499 | else if (val & (1 << 4)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3500 | chip->onfi_version = 22; |
| 3501 | else if (val & (1 << 3)) |
| 3502 | chip->onfi_version = 21; |
| 3503 | else if (val & (1 << 2)) |
| 3504 | chip->onfi_version = 20; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3505 | else if (val & (1 << 1)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3506 | chip->onfi_version = 10; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3507 | |
| 3508 | if (!chip->onfi_version) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 3509 | pr_info("unsupported ONFI version: %d\n", val); |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3510 | return 0; |
| 3511 | } |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3512 | |
| 3513 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 3514 | sanitize_string(p->model, sizeof(p->model)); |
| 3515 | if (!mtd->name) |
| 3516 | mtd->name = p->model; |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 3517 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3518 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 3519 | |
| 3520 | /* |
| 3521 | * pages_per_block and blocks_per_lun may not be a power-of-2 size |
| 3522 | * (don't ask me who thought of this...). MTD assumes that these |
| 3523 | * dimensions will be power-of-2, so just truncate the remaining area. |
| 3524 | */ |
| 3525 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 3526 | mtd->erasesize *= mtd->writesize; |
| 3527 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3528 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 3529 | |
| 3530 | /* See erasesize comment */ |
| 3531 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
Matthieu CASTET | 6379575 | 2012-03-19 15:35:25 +0100 | [diff] [blame] | 3532 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
Huang Shijie | 13fbd17 | 2013-09-25 14:58:13 +0800 | [diff] [blame] | 3533 | chip->bits_per_cell = p->bits_per_cell; |
Huang Shijie | e2985fc | 2013-05-17 11:17:30 +0800 | [diff] [blame] | 3534 | |
Zach Brown | 34da5f5 | 2017-01-10 13:30:21 -0600 | [diff] [blame] | 3535 | chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun); |
| 3536 | chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun); |
| 3537 | |
Huang Shijie | e2985fc | 2013-05-17 11:17:30 +0800 | [diff] [blame] | 3538 | if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS) |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3539 | chip->options |= NAND_BUSWIDTH_16; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3540 | |
Huang Shijie | 10c86ba | 2013-05-17 11:17:26 +0800 | [diff] [blame] | 3541 | if (p->ecc_bits != 0xff) { |
| 3542 | chip->ecc_strength_ds = p->ecc_bits; |
| 3543 | chip->ecc_step_ds = 512; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3544 | } else if (chip->onfi_version >= 21 && |
| 3545 | (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) { |
| 3546 | |
| 3547 | /* |
| 3548 | * The nand_flash_detect_ext_param_page() uses the |
| 3549 | * Change Read Column command which maybe not supported |
| 3550 | * by the chip->cmdfunc. So try to update the chip->cmdfunc |
| 3551 | * now. We do not replace user supplied command function. |
| 3552 | */ |
| 3553 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 3554 | chip->cmdfunc = nand_command_lp; |
| 3555 | |
| 3556 | /* The Extended Parameter Page is supported since ONFI 2.1. */ |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3557 | if (nand_flash_detect_ext_param_page(chip, p)) |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 3558 | pr_warn("Failed to detect ONFI extended param page\n"); |
| 3559 | } else { |
| 3560 | pr_warn("Could not retrieve ONFI ECC requirements\n"); |
Huang Shijie | 10c86ba | 2013-05-17 11:17:26 +0800 | [diff] [blame] | 3561 | } |
| 3562 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3563 | return 1; |
| 3564 | } |
| 3565 | |
| 3566 | /* |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3567 | * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise. |
| 3568 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3569 | static int nand_flash_detect_jedec(struct nand_chip *chip) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3570 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3571 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3572 | struct nand_jedec_params *p = &chip->jedec_params; |
| 3573 | struct jedec_ecc_info *ecc; |
| 3574 | int val; |
| 3575 | int i, j; |
| 3576 | |
| 3577 | /* Try JEDEC for unknown chip or LP */ |
| 3578 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1); |
| 3579 | if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' || |
| 3580 | chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' || |
| 3581 | chip->read_byte(mtd) != 'C') |
| 3582 | return 0; |
| 3583 | |
| 3584 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1); |
| 3585 | for (i = 0; i < 3; i++) { |
| 3586 | for (j = 0; j < sizeof(*p); j++) |
| 3587 | ((uint8_t *)p)[j] = chip->read_byte(mtd); |
| 3588 | |
| 3589 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) == |
| 3590 | le16_to_cpu(p->crc)) |
| 3591 | break; |
| 3592 | } |
| 3593 | |
| 3594 | if (i == 3) { |
| 3595 | pr_err("Could not find valid JEDEC parameter page; aborting\n"); |
| 3596 | return 0; |
| 3597 | } |
| 3598 | |
| 3599 | /* Check version */ |
| 3600 | val = le16_to_cpu(p->revision); |
| 3601 | if (val & (1 << 2)) |
| 3602 | chip->jedec_version = 10; |
| 3603 | else if (val & (1 << 1)) |
| 3604 | chip->jedec_version = 1; /* vendor specific version */ |
| 3605 | |
| 3606 | if (!chip->jedec_version) { |
| 3607 | pr_info("unsupported JEDEC version: %d\n", val); |
| 3608 | return 0; |
| 3609 | } |
| 3610 | |
| 3611 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 3612 | sanitize_string(p->model, sizeof(p->model)); |
| 3613 | if (!mtd->name) |
| 3614 | mtd->name = p->model; |
| 3615 | |
| 3616 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 3617 | |
| 3618 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 3619 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 3620 | mtd->erasesize *= mtd->writesize; |
| 3621 | |
| 3622 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 3623 | |
| 3624 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 3625 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
| 3626 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 3627 | chip->bits_per_cell = p->bits_per_cell; |
| 3628 | |
| 3629 | if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS) |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3630 | chip->options |= NAND_BUSWIDTH_16; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3631 | |
| 3632 | /* ECC info */ |
| 3633 | ecc = &p->ecc_info[0]; |
| 3634 | |
| 3635 | if (ecc->codeword_size >= 9) { |
| 3636 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 3637 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 3638 | } else { |
| 3639 | pr_warn("Invalid codeword size\n"); |
| 3640 | } |
| 3641 | |
| 3642 | return 1; |
| 3643 | } |
| 3644 | |
| 3645 | /* |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 3646 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 3647 | * @id_data: the ID string |
| 3648 | * @arrlen: the length of the @id_data array |
| 3649 | * @period: the period of repitition |
| 3650 | * |
| 3651 | * Check if an ID string is repeated within a given sequence of bytes at |
| 3652 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Brian Norris | d4d4f1b | 2012-11-14 21:54:20 -0800 | [diff] [blame] | 3653 | * period of 3). This is a helper function for nand_id_len(). Returns non-zero |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 3654 | * if the repetition has a period of @period; otherwise, returns zero. |
| 3655 | */ |
| 3656 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 3657 | { |
| 3658 | int i, j; |
| 3659 | for (i = 0; i < period; i++) |
| 3660 | for (j = i + period; j < arrlen; j += period) |
| 3661 | if (id_data[i] != id_data[j]) |
| 3662 | return 0; |
| 3663 | return 1; |
| 3664 | } |
| 3665 | |
| 3666 | /* |
| 3667 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 3668 | * @id_data: the ID string |
| 3669 | * @arrlen: the length of the @id_data array |
| 3670 | |
| 3671 | * Returns the length of the ID string, according to known wraparound/trailing |
| 3672 | * zero patterns. If no pattern exists, returns the length of the array. |
| 3673 | */ |
| 3674 | static int nand_id_len(u8 *id_data, int arrlen) |
| 3675 | { |
| 3676 | int last_nonzero, period; |
| 3677 | |
| 3678 | /* Find last non-zero byte */ |
| 3679 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 3680 | if (id_data[last_nonzero]) |
| 3681 | break; |
| 3682 | |
| 3683 | /* All zeros */ |
| 3684 | if (last_nonzero < 0) |
| 3685 | return 0; |
| 3686 | |
| 3687 | /* Calculate wraparound period */ |
| 3688 | for (period = 1; period < arrlen; period++) |
| 3689 | if (nand_id_has_period(id_data, arrlen, period)) |
| 3690 | break; |
| 3691 | |
| 3692 | /* There's a repeated pattern */ |
| 3693 | if (period < arrlen) |
| 3694 | return period; |
| 3695 | |
| 3696 | /* There are trailing zeros */ |
| 3697 | if (last_nonzero < arrlen - 1) |
| 3698 | return last_nonzero + 1; |
| 3699 | |
| 3700 | /* No pattern detected */ |
| 3701 | return arrlen; |
| 3702 | } |
| 3703 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 3704 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 3705 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 3706 | { |
| 3707 | int bits; |
| 3708 | |
| 3709 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 3710 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 3711 | return bits + 1; |
| 3712 | } |
| 3713 | |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 3714 | /* |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3715 | * Many new NAND share similar device ID codes, which represent the size of the |
| 3716 | * chip. The rest of the parameters must be decoded according to generic or |
| 3717 | * manufacturer-specific "extended ID" decoding patterns. |
| 3718 | */ |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3719 | void nand_decode_ext_id(struct nand_chip *chip) |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3720 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3721 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 9b2d61f | 2016-06-08 10:34:57 +0200 | [diff] [blame] | 3722 | int extid; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3723 | u8 *id_data = chip->id.data; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3724 | /* The 3rd id byte holds MLC / multichip data */ |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 3725 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3726 | /* The 4th id byte is the important one */ |
| 3727 | extid = id_data[3]; |
| 3728 | |
Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 3729 | /* Calc pagesize */ |
| 3730 | mtd->writesize = 1024 << (extid & 0x03); |
| 3731 | extid >>= 2; |
| 3732 | /* Calc oobsize */ |
| 3733 | mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9); |
| 3734 | extid >>= 2; |
| 3735 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 3736 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 3737 | extid >>= 2; |
| 3738 | /* Get buswidth information */ |
| 3739 | if (extid & 0x1) |
| 3740 | chip->options |= NAND_BUSWIDTH_16; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3741 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3742 | EXPORT_SYMBOL_GPL(nand_decode_ext_id); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3743 | |
| 3744 | /* |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3745 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 3746 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 3747 | * the chip. |
| 3748 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3749 | static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type) |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3750 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3751 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3752 | |
| 3753 | mtd->erasesize = type->erasesize; |
| 3754 | mtd->writesize = type->pagesize; |
| 3755 | mtd->oobsize = mtd->writesize / 32; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3756 | |
Huang Shijie | 1c195e9 | 2013-09-25 14:58:12 +0800 | [diff] [blame] | 3757 | /* All legacy ID NAND are small-page, SLC */ |
| 3758 | chip->bits_per_cell = 1; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3759 | } |
| 3760 | |
| 3761 | /* |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3762 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 3763 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 3764 | * page size, cell-type information). |
| 3765 | */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3766 | static void nand_decode_bbm_options(struct nand_chip *chip) |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3767 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3768 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3769 | |
| 3770 | /* Set the bad block position */ |
| 3771 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 3772 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 3773 | else |
| 3774 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3775 | } |
| 3776 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3777 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 3778 | { |
| 3779 | return type->id_len; |
| 3780 | } |
| 3781 | |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3782 | static bool find_full_id_nand(struct nand_chip *chip, |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3783 | struct nand_flash_dev *type) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3784 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3785 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3786 | u8 *id_data = chip->id.data; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3787 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3788 | if (!strncmp(type->id, id_data, type->id_len)) { |
| 3789 | mtd->writesize = type->pagesize; |
| 3790 | mtd->erasesize = type->erasesize; |
| 3791 | mtd->oobsize = type->oobsize; |
| 3792 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 3793 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3794 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 3795 | chip->options |= type->options; |
Huang Shijie | 5721934 | 2013-05-17 11:17:32 +0800 | [diff] [blame] | 3796 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
| 3797 | chip->ecc_step_ds = NAND_ECC_STEP(type); |
Boris BREZILLON | 57a94e2 | 2014-09-22 20:11:50 +0200 | [diff] [blame] | 3798 | chip->onfi_timing_mode_default = |
| 3799 | type->onfi_timing_mode_default; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3800 | |
Cai Zhiyong | 092b6a1 | 2013-12-25 21:19:21 +0800 | [diff] [blame] | 3801 | if (!mtd->name) |
| 3802 | mtd->name = type->name; |
| 3803 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3804 | return true; |
| 3805 | } |
| 3806 | return false; |
| 3807 | } |
| 3808 | |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3809 | /* |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3810 | * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC |
| 3811 | * compliant and does not have a full-id or legacy-id entry in the nand_ids |
| 3812 | * table. |
| 3813 | */ |
| 3814 | static void nand_manufacturer_detect(struct nand_chip *chip) |
| 3815 | { |
| 3816 | /* |
| 3817 | * Try manufacturer detection if available and use |
| 3818 | * nand_decode_ext_id() otherwise. |
| 3819 | */ |
| 3820 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 3821 | chip->manufacturer.desc->ops->detect) { |
| 3822 | /* The 3rd id byte holds MLC / multichip data */ |
| 3823 | chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3824 | chip->manufacturer.desc->ops->detect(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 3825 | } else { |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3826 | nand_decode_ext_id(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 3827 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3828 | } |
| 3829 | |
| 3830 | /* |
| 3831 | * Manufacturer initialization. This function is called for all NANDs including |
| 3832 | * ONFI and JEDEC compliant ones. |
| 3833 | * Manufacturer drivers should put all their specific initialization code in |
| 3834 | * their ->init() hook. |
| 3835 | */ |
| 3836 | static int nand_manufacturer_init(struct nand_chip *chip) |
| 3837 | { |
| 3838 | if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops || |
| 3839 | !chip->manufacturer.desc->ops->init) |
| 3840 | return 0; |
| 3841 | |
| 3842 | return chip->manufacturer.desc->ops->init(chip); |
| 3843 | } |
| 3844 | |
| 3845 | /* |
| 3846 | * Manufacturer cleanup. This function is called for all NANDs including |
| 3847 | * ONFI and JEDEC compliant ones. |
| 3848 | * Manufacturer drivers should put all their specific cleanup code in their |
| 3849 | * ->cleanup() hook. |
| 3850 | */ |
| 3851 | static void nand_manufacturer_cleanup(struct nand_chip *chip) |
| 3852 | { |
| 3853 | /* Release manufacturer private data */ |
| 3854 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
| 3855 | chip->manufacturer.desc->ops->cleanup) |
| 3856 | chip->manufacturer.desc->ops->cleanup(chip); |
| 3857 | } |
| 3858 | |
| 3859 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3860 | * Get the flash and manufacturer id and lookup if the type is supported. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3861 | */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 3862 | static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3863 | { |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 3864 | const struct nand_manufacturer *manufacturer; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3865 | struct mtd_info *mtd = nand_to_mtd(chip); |
Cai Zhiyong | bb77082 | 2013-12-25 20:11:15 +0800 | [diff] [blame] | 3866 | int busw; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 3867 | int i; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3868 | u8 *id_data = chip->id.data; |
| 3869 | u8 maf_id, dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3870 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 3871 | /* |
| 3872 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3873 | * after power-up. |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 3874 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 3875 | nand_reset(chip, 0); |
| 3876 | |
| 3877 | /* Select the device */ |
| 3878 | chip->select_chip(mtd, 0); |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 3879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3880 | /* Send the command for reading device ID */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3881 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3882 | |
| 3883 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3884 | maf_id = chip->read_byte(mtd); |
| 3885 | dev_id = chip->read_byte(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3886 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3887 | /* |
| 3888 | * Try again to make sure, as some systems the bus-hold or other |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 3889 | * interface concerns can cause random data which looks like a |
| 3890 | * possibly credible NAND flash to appear. If the two results do |
| 3891 | * not match, ignore the device completely. |
| 3892 | */ |
| 3893 | |
| 3894 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 3895 | |
Brian Norris | 4aef9b7 | 2012-09-24 20:40:48 -0700 | [diff] [blame] | 3896 | /* Read entire ID string */ |
Jean-Louis Thekekara | 5158bd5 | 2017-06-29 19:08:30 +0200 | [diff] [blame] | 3897 | for (i = 0; i < ARRAY_SIZE(chip->id.data); i++) |
Kevin Cernekee | 426c457 | 2010-05-04 20:58:03 -0700 | [diff] [blame] | 3898 | id_data[i] = chip->read_byte(mtd); |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 3899 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3900 | if (id_data[0] != maf_id || id_data[1] != dev_id) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 3901 | pr_info("second ID read did not match %02x,%02x against %02x,%02x\n", |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3902 | maf_id, dev_id, id_data[0], id_data[1]); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 3903 | return -ENODEV; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 3904 | } |
| 3905 | |
Jean-Louis Thekekara | 5158bd5 | 2017-06-29 19:08:30 +0200 | [diff] [blame] | 3906 | chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data)); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3907 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3908 | /* Try to identify manufacturer */ |
| 3909 | manufacturer = nand_get_manufacturer(maf_id); |
| 3910 | chip->manufacturer.desc = manufacturer; |
| 3911 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3912 | if (!type) |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 3913 | type = nand_flash_ids; |
| 3914 | |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3915 | /* |
| 3916 | * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic |
| 3917 | * override it. |
| 3918 | * This is required to make sure initial NAND bus width set by the |
| 3919 | * NAND controller driver is coherent with the real NAND bus width |
| 3920 | * (extracted by auto-detection code). |
| 3921 | */ |
| 3922 | busw = chip->options & NAND_BUSWIDTH_16; |
| 3923 | |
| 3924 | /* |
| 3925 | * The flag is only set (never cleared), reset it to its default value |
| 3926 | * before starting auto-detection. |
| 3927 | */ |
| 3928 | chip->options &= ~NAND_BUSWIDTH_16; |
| 3929 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3930 | for (; type->name != NULL; type++) { |
| 3931 | if (is_full_id_nand(type)) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3932 | if (find_full_id_nand(chip, type)) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3933 | goto ident_done; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3934 | } else if (dev_id == type->dev_id) { |
Brian Norris | db5b09f | 2015-05-22 10:43:12 -0700 | [diff] [blame] | 3935 | break; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3936 | } |
| 3937 | } |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 3938 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3939 | chip->onfi_version = 0; |
| 3940 | if (!type->name || !type->pagesize) { |
Masahiro Yamada | 35fc519 | 2014-04-09 16:26:26 +0900 | [diff] [blame] | 3941 | /* Check if the chip is ONFI compliant */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3942 | if (nand_flash_detect_onfi(chip)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3943 | goto ident_done; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3944 | |
| 3945 | /* Check if the chip is JEDEC compliant */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3946 | if (nand_flash_detect_jedec(chip)) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3947 | goto ident_done; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3948 | } |
| 3949 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 3950 | if (!type->name) |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 3951 | return -ENODEV; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3952 | |
Thomas Gleixner | ba0251fe | 2006-05-27 01:02:13 +0200 | [diff] [blame] | 3953 | if (!mtd->name) |
| 3954 | mtd->name = type->name; |
| 3955 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3956 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3957 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3958 | if (!type->pagesize) |
| 3959 | nand_manufacturer_detect(chip); |
| 3960 | else |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3961 | nand_decode_id(chip, type); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3962 | |
Brian Norris | bf7a01b | 2012-07-13 09:28:24 -0700 | [diff] [blame] | 3963 | /* Get chip options */ |
| 3964 | chip->options |= type->options; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3965 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3966 | ident_done: |
| 3967 | |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 3968 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3969 | WARN_ON(busw & NAND_BUSWIDTH_16); |
| 3970 | nand_set_defaults(chip); |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 3971 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 3972 | /* |
| 3973 | * Check, if buswidth is correct. Hardware drivers should set |
| 3974 | * chip correct! |
| 3975 | */ |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 3976 | pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3977 | maf_id, dev_id); |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 3978 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 3979 | mtd->name); |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3980 | pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8, |
| 3981 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 3982 | return -EINVAL; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3983 | } |
| 3984 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3985 | nand_decode_bbm_options(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3986 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3987 | /* Calculate the address shift from the page size */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3988 | chip->page_shift = ffs(mtd->writesize) - 1; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3989 | /* Convert chipsize to number of pages per chip -1 */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3990 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3991 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3992 | chip->bbt_erase_shift = chip->phys_erase_shift = |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3993 | ffs(mtd->erasesize) - 1; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3994 | if (chip->chipsize & 0xffffffff) |
| 3995 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3996 | else { |
| 3997 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 3998 | chip->chip_shift += 32 - 1; |
| 3999 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4000 | |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame^] | 4001 | if (chip->chip_shift - chip->page_shift > 16) |
| 4002 | chip->options |= NAND_ROW_ADDR_3; |
| 4003 | |
Artem Bityutskiy | 26d9be1 | 2011-04-28 20:26:59 +0300 | [diff] [blame] | 4004 | chip->badblockbits = 8; |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4005 | chip->erase = single_erase; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4006 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4007 | /* Do not replace user supplied command function! */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4008 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 4009 | chip->cmdfunc = nand_command_lp; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4010 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4011 | pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4012 | maf_id, dev_id); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4013 | |
| 4014 | if (chip->onfi_version) |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4015 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4016 | chip->onfi_params.model); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4017 | else if (chip->jedec_version) |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4018 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4019 | chip->jedec_params.model); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4020 | else |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4021 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4022 | type->name); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4023 | |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4024 | pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n", |
Huang Shijie | 3723e93 | 2013-09-25 14:58:14 +0800 | [diff] [blame] | 4025 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4026 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4027 | return 0; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4028 | } |
| 4029 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4030 | static const char * const nand_ecc_modes[] = { |
| 4031 | [NAND_ECC_NONE] = "none", |
| 4032 | [NAND_ECC_SOFT] = "soft", |
| 4033 | [NAND_ECC_HW] = "hw", |
| 4034 | [NAND_ECC_HW_SYNDROME] = "hw_syndrome", |
| 4035 | [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first", |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 4036 | [NAND_ECC_ON_DIE] = "on-die", |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4037 | }; |
| 4038 | |
| 4039 | static int of_get_nand_ecc_mode(struct device_node *np) |
| 4040 | { |
| 4041 | const char *pm; |
| 4042 | int err, i; |
| 4043 | |
| 4044 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4045 | if (err < 0) |
| 4046 | return err; |
| 4047 | |
| 4048 | for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++) |
| 4049 | if (!strcasecmp(pm, nand_ecc_modes[i])) |
| 4050 | return i; |
| 4051 | |
Rafał Miłecki | ae211bc | 2016-04-17 22:53:06 +0200 | [diff] [blame] | 4052 | /* |
| 4053 | * For backward compatibility we support few obsoleted values that don't |
| 4054 | * have their mappings into nand_ecc_modes_t anymore (they were merged |
| 4055 | * with other enums). |
| 4056 | */ |
| 4057 | if (!strcasecmp(pm, "soft_bch")) |
| 4058 | return NAND_ECC_SOFT; |
| 4059 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4060 | return -ENODEV; |
| 4061 | } |
| 4062 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4063 | static const char * const nand_ecc_algos[] = { |
| 4064 | [NAND_ECC_HAMMING] = "hamming", |
| 4065 | [NAND_ECC_BCH] = "bch", |
| 4066 | }; |
| 4067 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4068 | static int of_get_nand_ecc_algo(struct device_node *np) |
| 4069 | { |
| 4070 | const char *pm; |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4071 | int err, i; |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4072 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4073 | err = of_property_read_string(np, "nand-ecc-algo", &pm); |
| 4074 | if (!err) { |
| 4075 | for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++) |
| 4076 | if (!strcasecmp(pm, nand_ecc_algos[i])) |
| 4077 | return i; |
| 4078 | return -ENODEV; |
| 4079 | } |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4080 | |
| 4081 | /* |
| 4082 | * For backward compatibility we also read "nand-ecc-mode" checking |
| 4083 | * for some obsoleted values that were specifying ECC algorithm. |
| 4084 | */ |
| 4085 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4086 | if (err < 0) |
| 4087 | return err; |
| 4088 | |
| 4089 | if (!strcasecmp(pm, "soft")) |
| 4090 | return NAND_ECC_HAMMING; |
| 4091 | else if (!strcasecmp(pm, "soft_bch")) |
| 4092 | return NAND_ECC_BCH; |
| 4093 | |
| 4094 | return -ENODEV; |
| 4095 | } |
| 4096 | |
| 4097 | static int of_get_nand_ecc_step_size(struct device_node *np) |
| 4098 | { |
| 4099 | int ret; |
| 4100 | u32 val; |
| 4101 | |
| 4102 | ret = of_property_read_u32(np, "nand-ecc-step-size", &val); |
| 4103 | return ret ? ret : val; |
| 4104 | } |
| 4105 | |
| 4106 | static int of_get_nand_ecc_strength(struct device_node *np) |
| 4107 | { |
| 4108 | int ret; |
| 4109 | u32 val; |
| 4110 | |
| 4111 | ret = of_property_read_u32(np, "nand-ecc-strength", &val); |
| 4112 | return ret ? ret : val; |
| 4113 | } |
| 4114 | |
| 4115 | static int of_get_nand_bus_width(struct device_node *np) |
| 4116 | { |
| 4117 | u32 val; |
| 4118 | |
| 4119 | if (of_property_read_u32(np, "nand-bus-width", &val)) |
| 4120 | return 8; |
| 4121 | |
| 4122 | switch (val) { |
| 4123 | case 8: |
| 4124 | case 16: |
| 4125 | return val; |
| 4126 | default: |
| 4127 | return -EIO; |
| 4128 | } |
| 4129 | } |
| 4130 | |
| 4131 | static bool of_get_nand_on_flash_bbt(struct device_node *np) |
| 4132 | { |
| 4133 | return of_property_read_bool(np, "nand-on-flash-bbt"); |
| 4134 | } |
| 4135 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4136 | static int nand_dt_init(struct nand_chip *chip) |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4137 | { |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4138 | struct device_node *dn = nand_get_flash_node(chip); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4139 | int ecc_mode, ecc_algo, ecc_strength, ecc_step; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4140 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4141 | if (!dn) |
| 4142 | return 0; |
| 4143 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4144 | if (of_get_nand_bus_width(dn) == 16) |
| 4145 | chip->options |= NAND_BUSWIDTH_16; |
| 4146 | |
| 4147 | if (of_get_nand_on_flash_bbt(dn)) |
| 4148 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 4149 | |
| 4150 | ecc_mode = of_get_nand_ecc_mode(dn); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4151 | ecc_algo = of_get_nand_ecc_algo(dn); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4152 | ecc_strength = of_get_nand_ecc_strength(dn); |
| 4153 | ecc_step = of_get_nand_ecc_step_size(dn); |
| 4154 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4155 | if (ecc_mode >= 0) |
| 4156 | chip->ecc.mode = ecc_mode; |
| 4157 | |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4158 | if (ecc_algo >= 0) |
| 4159 | chip->ecc.algo = ecc_algo; |
| 4160 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4161 | if (ecc_strength >= 0) |
| 4162 | chip->ecc.strength = ecc_strength; |
| 4163 | |
| 4164 | if (ecc_step > 0) |
| 4165 | chip->ecc.size = ecc_step; |
| 4166 | |
Boris Brezillon | ba78ee0 | 2016-06-08 17:04:22 +0200 | [diff] [blame] | 4167 | if (of_property_read_bool(dn, "nand-ecc-maximize")) |
| 4168 | chip->ecc.options |= NAND_ECC_MAXIMIZE; |
| 4169 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4170 | return 0; |
| 4171 | } |
| 4172 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4173 | /** |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4174 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4175 | * @mtd: MTD device structure |
| 4176 | * @maxchips: number of chips to scan for |
| 4177 | * @table: alternative NAND ID table |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4178 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4179 | * This is the first phase of the normal nand_scan() function. It reads the |
| 4180 | * flash ID and sets up MTD fields accordingly. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4181 | * |
| 4182 | */ |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4183 | int nand_scan_ident(struct mtd_info *mtd, int maxchips, |
| 4184 | struct nand_flash_dev *table) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4185 | { |
Cai Zhiyong | bb77082 | 2013-12-25 20:11:15 +0800 | [diff] [blame] | 4186 | int i, nand_maf_id, nand_dev_id; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4187 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4188 | int ret; |
| 4189 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4190 | ret = nand_dt_init(chip); |
| 4191 | if (ret) |
| 4192 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4193 | |
Brian Norris | f7a8e38 | 2016-01-05 10:39:45 -0800 | [diff] [blame] | 4194 | if (!mtd->name && mtd->dev.parent) |
| 4195 | mtd->name = dev_name(mtd->dev.parent); |
| 4196 | |
Andrey Smirnov | 76fe334 | 2016-07-21 14:59:20 -0700 | [diff] [blame] | 4197 | if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) { |
| 4198 | /* |
| 4199 | * Default functions assigned for chip_select() and |
| 4200 | * cmdfunc() both expect cmd_ctrl() to be populated, |
| 4201 | * so we need to check that that's the case |
| 4202 | */ |
| 4203 | pr_err("chip.cmd_ctrl() callback is not provided"); |
| 4204 | return -EINVAL; |
| 4205 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4206 | /* Set the default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4207 | nand_set_defaults(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4208 | |
| 4209 | /* Read the flash type */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 4210 | ret = nand_detect(chip, table); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4211 | if (ret) { |
Ben Dooks | b1c6e6d | 2009-11-02 18:12:33 +0000 | [diff] [blame] | 4212 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4213 | pr_warn("No NAND device found\n"); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4214 | chip->select_chip(mtd, -1); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4215 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | } |
| 4217 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4218 | nand_maf_id = chip->id.data[0]; |
| 4219 | nand_dev_id = chip->id.data[1]; |
| 4220 | |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 4221 | chip->select_chip(mtd, -1); |
| 4222 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4223 | /* Check for a chip array */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4224 | for (i = 1; i < maxchips; i++) { |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4225 | /* See comment in nand_get_flash_type for reset */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4226 | nand_reset(chip, i); |
| 4227 | |
| 4228 | chip->select_chip(mtd, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | /* Send the command for reading device ID */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4230 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | /* Read manufacturer and device IDs */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4232 | if (nand_maf_id != chip->read_byte(mtd) || |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 4233 | nand_dev_id != chip->read_byte(mtd)) { |
| 4234 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4235 | break; |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 4236 | } |
| 4237 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 | } |
| 4239 | if (i > 1) |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4240 | pr_info("%d chips detected\n", i); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | /* Store the number of chips and calc total size for mtd */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4243 | chip->numchips = i; |
| 4244 | mtd->size = i * chip->chipsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4246 | return 0; |
| 4247 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4248 | EXPORT_SYMBOL(nand_scan_ident); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4249 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4250 | static int nand_set_ecc_soft_ops(struct mtd_info *mtd) |
| 4251 | { |
| 4252 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4253 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4254 | |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 4255 | if (WARN_ON(ecc->mode != NAND_ECC_SOFT)) |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4256 | return -EINVAL; |
| 4257 | |
| 4258 | switch (ecc->algo) { |
| 4259 | case NAND_ECC_HAMMING: |
| 4260 | ecc->calculate = nand_calculate_ecc; |
| 4261 | ecc->correct = nand_correct_data; |
| 4262 | ecc->read_page = nand_read_page_swecc; |
| 4263 | ecc->read_subpage = nand_read_subpage; |
| 4264 | ecc->write_page = nand_write_page_swecc; |
| 4265 | ecc->read_page_raw = nand_read_page_raw; |
| 4266 | ecc->write_page_raw = nand_write_page_raw; |
| 4267 | ecc->read_oob = nand_read_oob_std; |
| 4268 | ecc->write_oob = nand_write_oob_std; |
| 4269 | if (!ecc->size) |
| 4270 | ecc->size = 256; |
| 4271 | ecc->bytes = 3; |
| 4272 | ecc->strength = 1; |
| 4273 | return 0; |
| 4274 | case NAND_ECC_BCH: |
| 4275 | if (!mtd_nand_has_bch()) { |
| 4276 | WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n"); |
| 4277 | return -EINVAL; |
| 4278 | } |
| 4279 | ecc->calculate = nand_bch_calculate_ecc; |
| 4280 | ecc->correct = nand_bch_correct_data; |
| 4281 | ecc->read_page = nand_read_page_swecc; |
| 4282 | ecc->read_subpage = nand_read_subpage; |
| 4283 | ecc->write_page = nand_write_page_swecc; |
| 4284 | ecc->read_page_raw = nand_read_page_raw; |
| 4285 | ecc->write_page_raw = nand_write_page_raw; |
| 4286 | ecc->read_oob = nand_read_oob_std; |
| 4287 | ecc->write_oob = nand_write_oob_std; |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 4288 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4289 | /* |
| 4290 | * Board driver should supply ecc.size and ecc.strength |
| 4291 | * values to select how many bits are correctable. |
| 4292 | * Otherwise, default to 4 bits for large page devices. |
| 4293 | */ |
| 4294 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 4295 | ecc->size = 512; |
| 4296 | ecc->strength = 4; |
| 4297 | } |
| 4298 | |
| 4299 | /* |
| 4300 | * if no ecc placement scheme was provided pickup the default |
| 4301 | * large page one. |
| 4302 | */ |
| 4303 | if (!mtd->ooblayout) { |
| 4304 | /* handle large page devices only */ |
| 4305 | if (mtd->oobsize < 64) { |
| 4306 | WARN(1, "OOB layout is required when using software BCH on small pages\n"); |
| 4307 | return -EINVAL; |
| 4308 | } |
| 4309 | |
| 4310 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 4311 | |
| 4312 | } |
| 4313 | |
| 4314 | /* |
| 4315 | * We can only maximize ECC config when the default layout is |
| 4316 | * used, otherwise we don't know how many bytes can really be |
| 4317 | * used. |
| 4318 | */ |
| 4319 | if (mtd->ooblayout == &nand_ooblayout_lp_ops && |
| 4320 | ecc->options & NAND_ECC_MAXIMIZE) { |
| 4321 | int steps, bytes; |
| 4322 | |
| 4323 | /* Always prefer 1k blocks over 512bytes ones */ |
| 4324 | ecc->size = 1024; |
| 4325 | steps = mtd->writesize / ecc->size; |
| 4326 | |
| 4327 | /* Reserve 2 bytes for the BBM */ |
| 4328 | bytes = (mtd->oobsize - 2) / steps; |
| 4329 | ecc->strength = bytes * 8 / fls(8 * ecc->size); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4330 | } |
| 4331 | |
| 4332 | /* See nand_bch_init() for details. */ |
| 4333 | ecc->bytes = 0; |
| 4334 | ecc->priv = nand_bch_init(mtd); |
| 4335 | if (!ecc->priv) { |
| 4336 | WARN(1, "BCH ECC initialization failed!\n"); |
| 4337 | return -EINVAL; |
| 4338 | } |
| 4339 | return 0; |
| 4340 | default: |
| 4341 | WARN(1, "Unsupported ECC algorithm!\n"); |
| 4342 | return -EINVAL; |
| 4343 | } |
| 4344 | } |
| 4345 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 4346 | /** |
| 4347 | * nand_check_ecc_caps - check the sanity of preset ECC settings |
| 4348 | * @chip: nand chip info structure |
| 4349 | * @caps: ECC caps info structure |
| 4350 | * @oobavail: OOB size that the ECC engine can use |
| 4351 | * |
| 4352 | * When ECC step size and strength are already set, check if they are supported |
| 4353 | * by the controller and the calculated ECC bytes fit within the chip's OOB. |
| 4354 | * On success, the calculated ECC bytes is set. |
| 4355 | */ |
| 4356 | int nand_check_ecc_caps(struct nand_chip *chip, |
| 4357 | const struct nand_ecc_caps *caps, int oobavail) |
| 4358 | { |
| 4359 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4360 | const struct nand_ecc_step_info *stepinfo; |
| 4361 | int preset_step = chip->ecc.size; |
| 4362 | int preset_strength = chip->ecc.strength; |
| 4363 | int nsteps, ecc_bytes; |
| 4364 | int i, j; |
| 4365 | |
| 4366 | if (WARN_ON(oobavail < 0)) |
| 4367 | return -EINVAL; |
| 4368 | |
| 4369 | if (!preset_step || !preset_strength) |
| 4370 | return -ENODATA; |
| 4371 | |
| 4372 | nsteps = mtd->writesize / preset_step; |
| 4373 | |
| 4374 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4375 | stepinfo = &caps->stepinfos[i]; |
| 4376 | |
| 4377 | if (stepinfo->stepsize != preset_step) |
| 4378 | continue; |
| 4379 | |
| 4380 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4381 | if (stepinfo->strengths[j] != preset_strength) |
| 4382 | continue; |
| 4383 | |
| 4384 | ecc_bytes = caps->calc_ecc_bytes(preset_step, |
| 4385 | preset_strength); |
| 4386 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4387 | return ecc_bytes; |
| 4388 | |
| 4389 | if (ecc_bytes * nsteps > oobavail) { |
| 4390 | pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB", |
| 4391 | preset_step, preset_strength); |
| 4392 | return -ENOSPC; |
| 4393 | } |
| 4394 | |
| 4395 | chip->ecc.bytes = ecc_bytes; |
| 4396 | |
| 4397 | return 0; |
| 4398 | } |
| 4399 | } |
| 4400 | |
| 4401 | pr_err("ECC (step, strength) = (%d, %d) not supported on this controller", |
| 4402 | preset_step, preset_strength); |
| 4403 | |
| 4404 | return -ENOTSUPP; |
| 4405 | } |
| 4406 | EXPORT_SYMBOL_GPL(nand_check_ecc_caps); |
| 4407 | |
| 4408 | /** |
| 4409 | * nand_match_ecc_req - meet the chip's requirement with least ECC bytes |
| 4410 | * @chip: nand chip info structure |
| 4411 | * @caps: ECC engine caps info structure |
| 4412 | * @oobavail: OOB size that the ECC engine can use |
| 4413 | * |
| 4414 | * If a chip's ECC requirement is provided, try to meet it with the least |
| 4415 | * number of ECC bytes (i.e. with the largest number of OOB-free bytes). |
| 4416 | * On success, the chosen ECC settings are set. |
| 4417 | */ |
| 4418 | int nand_match_ecc_req(struct nand_chip *chip, |
| 4419 | const struct nand_ecc_caps *caps, int oobavail) |
| 4420 | { |
| 4421 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4422 | const struct nand_ecc_step_info *stepinfo; |
| 4423 | int req_step = chip->ecc_step_ds; |
| 4424 | int req_strength = chip->ecc_strength_ds; |
| 4425 | int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total; |
| 4426 | int best_step, best_strength, best_ecc_bytes; |
| 4427 | int best_ecc_bytes_total = INT_MAX; |
| 4428 | int i, j; |
| 4429 | |
| 4430 | if (WARN_ON(oobavail < 0)) |
| 4431 | return -EINVAL; |
| 4432 | |
| 4433 | /* No information provided by the NAND chip */ |
| 4434 | if (!req_step || !req_strength) |
| 4435 | return -ENOTSUPP; |
| 4436 | |
| 4437 | /* number of correctable bits the chip requires in a page */ |
| 4438 | req_corr = mtd->writesize / req_step * req_strength; |
| 4439 | |
| 4440 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4441 | stepinfo = &caps->stepinfos[i]; |
| 4442 | step_size = stepinfo->stepsize; |
| 4443 | |
| 4444 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4445 | strength = stepinfo->strengths[j]; |
| 4446 | |
| 4447 | /* |
| 4448 | * If both step size and strength are smaller than the |
| 4449 | * chip's requirement, it is not easy to compare the |
| 4450 | * resulted reliability. |
| 4451 | */ |
| 4452 | if (step_size < req_step && strength < req_strength) |
| 4453 | continue; |
| 4454 | |
| 4455 | if (mtd->writesize % step_size) |
| 4456 | continue; |
| 4457 | |
| 4458 | nsteps = mtd->writesize / step_size; |
| 4459 | |
| 4460 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 4461 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4462 | continue; |
| 4463 | ecc_bytes_total = ecc_bytes * nsteps; |
| 4464 | |
| 4465 | if (ecc_bytes_total > oobavail || |
| 4466 | strength * nsteps < req_corr) |
| 4467 | continue; |
| 4468 | |
| 4469 | /* |
| 4470 | * We assume the best is to meet the chip's requrement |
| 4471 | * with the least number of ECC bytes. |
| 4472 | */ |
| 4473 | if (ecc_bytes_total < best_ecc_bytes_total) { |
| 4474 | best_ecc_bytes_total = ecc_bytes_total; |
| 4475 | best_step = step_size; |
| 4476 | best_strength = strength; |
| 4477 | best_ecc_bytes = ecc_bytes; |
| 4478 | } |
| 4479 | } |
| 4480 | } |
| 4481 | |
| 4482 | if (best_ecc_bytes_total == INT_MAX) |
| 4483 | return -ENOTSUPP; |
| 4484 | |
| 4485 | chip->ecc.size = best_step; |
| 4486 | chip->ecc.strength = best_strength; |
| 4487 | chip->ecc.bytes = best_ecc_bytes; |
| 4488 | |
| 4489 | return 0; |
| 4490 | } |
| 4491 | EXPORT_SYMBOL_GPL(nand_match_ecc_req); |
| 4492 | |
| 4493 | /** |
| 4494 | * nand_maximize_ecc - choose the max ECC strength available |
| 4495 | * @chip: nand chip info structure |
| 4496 | * @caps: ECC engine caps info structure |
| 4497 | * @oobavail: OOB size that the ECC engine can use |
| 4498 | * |
| 4499 | * Choose the max ECC strength that is supported on the controller, and can fit |
| 4500 | * within the chip's OOB. On success, the chosen ECC settings are set. |
| 4501 | */ |
| 4502 | int nand_maximize_ecc(struct nand_chip *chip, |
| 4503 | const struct nand_ecc_caps *caps, int oobavail) |
| 4504 | { |
| 4505 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4506 | const struct nand_ecc_step_info *stepinfo; |
| 4507 | int step_size, strength, nsteps, ecc_bytes, corr; |
| 4508 | int best_corr = 0; |
| 4509 | int best_step = 0; |
| 4510 | int best_strength, best_ecc_bytes; |
| 4511 | int i, j; |
| 4512 | |
| 4513 | if (WARN_ON(oobavail < 0)) |
| 4514 | return -EINVAL; |
| 4515 | |
| 4516 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4517 | stepinfo = &caps->stepinfos[i]; |
| 4518 | step_size = stepinfo->stepsize; |
| 4519 | |
| 4520 | /* If chip->ecc.size is already set, respect it */ |
| 4521 | if (chip->ecc.size && step_size != chip->ecc.size) |
| 4522 | continue; |
| 4523 | |
| 4524 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4525 | strength = stepinfo->strengths[j]; |
| 4526 | |
| 4527 | if (mtd->writesize % step_size) |
| 4528 | continue; |
| 4529 | |
| 4530 | nsteps = mtd->writesize / step_size; |
| 4531 | |
| 4532 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 4533 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4534 | continue; |
| 4535 | |
| 4536 | if (ecc_bytes * nsteps > oobavail) |
| 4537 | continue; |
| 4538 | |
| 4539 | corr = strength * nsteps; |
| 4540 | |
| 4541 | /* |
| 4542 | * If the number of correctable bits is the same, |
| 4543 | * bigger step_size has more reliability. |
| 4544 | */ |
| 4545 | if (corr > best_corr || |
| 4546 | (corr == best_corr && step_size > best_step)) { |
| 4547 | best_corr = corr; |
| 4548 | best_step = step_size; |
| 4549 | best_strength = strength; |
| 4550 | best_ecc_bytes = ecc_bytes; |
| 4551 | } |
| 4552 | } |
| 4553 | } |
| 4554 | |
| 4555 | if (!best_corr) |
| 4556 | return -ENOTSUPP; |
| 4557 | |
| 4558 | chip->ecc.size = best_step; |
| 4559 | chip->ecc.strength = best_strength; |
| 4560 | chip->ecc.bytes = best_ecc_bytes; |
| 4561 | |
| 4562 | return 0; |
| 4563 | } |
| 4564 | EXPORT_SYMBOL_GPL(nand_maximize_ecc); |
| 4565 | |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 4566 | /* |
| 4567 | * Check if the chip configuration meet the datasheet requirements. |
| 4568 | |
| 4569 | * If our configuration corrects A bits per B bytes and the minimum |
| 4570 | * required correction level is X bits per Y bytes, then we must ensure |
| 4571 | * both of the following are true: |
| 4572 | * |
| 4573 | * (1) A / B >= X / Y |
| 4574 | * (2) A >= X |
| 4575 | * |
| 4576 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 4577 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 4578 | * in the same sector. |
| 4579 | */ |
| 4580 | static bool nand_ecc_strength_good(struct mtd_info *mtd) |
| 4581 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4582 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 4583 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4584 | int corr, ds_corr; |
| 4585 | |
| 4586 | if (ecc->size == 0 || chip->ecc_step_ds == 0) |
| 4587 | /* Not enough information */ |
| 4588 | return true; |
| 4589 | |
| 4590 | /* |
| 4591 | * We get the number of corrected bits per page to compare |
| 4592 | * the correction density. |
| 4593 | */ |
| 4594 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
| 4595 | ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; |
| 4596 | |
| 4597 | return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; |
| 4598 | } |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4599 | |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 4600 | static bool invalid_ecc_page_accessors(struct nand_chip *chip) |
| 4601 | { |
| 4602 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4603 | |
| 4604 | if (nand_standard_page_accessors(ecc)) |
| 4605 | return false; |
| 4606 | |
| 4607 | /* |
| 4608 | * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND |
| 4609 | * controller driver implements all the page accessors because |
| 4610 | * default helpers are not suitable when the core does not |
| 4611 | * send the READ0/PAGEPROG commands. |
| 4612 | */ |
| 4613 | return (!ecc->read_page || !ecc->write_page || |
| 4614 | !ecc->read_page_raw || !ecc->write_page_raw || |
| 4615 | (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) || |
| 4616 | (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage && |
| 4617 | ecc->hwctl && ecc->calculate)); |
| 4618 | } |
| 4619 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4620 | /** |
| 4621 | * nand_scan_tail - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4622 | * @mtd: MTD device structure |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4623 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4624 | * This is the second phase of the normal nand_scan() function. It fills out |
| 4625 | * all the uninitialized function pointers with the defaults and scans for a |
| 4626 | * bad block table if appropriate. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4627 | */ |
| 4628 | int nand_scan_tail(struct mtd_info *mtd) |
| 4629 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4630 | struct nand_chip *chip = mtd_to_nand(mtd); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4631 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4632 | struct nand_buffers *nbuf = NULL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4633 | int ret, i; |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4634 | |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 4635 | /* New bad blocks should be marked in OOB, flash-based BBT, or both */ |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4636 | if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 4637 | !(chip->bbt_options & NAND_BBT_USE_FLASH))) { |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4638 | return -EINVAL; |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 4639 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 4640 | |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 4641 | if (invalid_ecc_page_accessors(chip)) { |
| 4642 | pr_err("Invalid ECC page accessors setup\n"); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4643 | return -EINVAL; |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 4644 | } |
| 4645 | |
Huang Shijie | f02ea4e | 2014-01-13 14:27:12 +0800 | [diff] [blame] | 4646 | if (!(chip->options & NAND_OWN_BUFFERS)) { |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4647 | nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4648 | if (!nbuf) |
| 4649 | return -ENOMEM; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4650 | |
| 4651 | nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 4652 | if (!nbuf->ecccalc) { |
| 4653 | ret = -ENOMEM; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4654 | goto err_free_nbuf; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4655 | } |
| 4656 | |
| 4657 | nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 4658 | if (!nbuf->ecccode) { |
| 4659 | ret = -ENOMEM; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4660 | goto err_free_nbuf; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4661 | } |
| 4662 | |
| 4663 | nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize, |
| 4664 | GFP_KERNEL); |
| 4665 | if (!nbuf->databuf) { |
| 4666 | ret = -ENOMEM; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4667 | goto err_free_nbuf; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4668 | } |
Huang Shijie | f02ea4e | 2014-01-13 14:27:12 +0800 | [diff] [blame] | 4669 | |
| 4670 | chip->buffers = nbuf; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4671 | } else if (!chip->buffers) { |
| 4672 | return -ENOMEM; |
Huang Shijie | f02ea4e | 2014-01-13 14:27:12 +0800 | [diff] [blame] | 4673 | } |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 4674 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4675 | /* |
| 4676 | * FIXME: some NAND manufacturer drivers expect the first die to be |
| 4677 | * selected when manufacturer->init() is called. They should be fixed |
| 4678 | * to explictly select the relevant die when interacting with the NAND |
| 4679 | * chip. |
| 4680 | */ |
| 4681 | chip->select_chip(mtd, 0); |
| 4682 | ret = nand_manufacturer_init(chip); |
| 4683 | chip->select_chip(mtd, -1); |
| 4684 | if (ret) |
| 4685 | goto err_free_nbuf; |
| 4686 | |
David Woodhouse | 7dcdcbef | 2006-10-21 17:09:53 +0100 | [diff] [blame] | 4687 | /* Set the internal oob buffer location, just after the page data */ |
David Woodhouse | 784f4d5 | 2006-10-22 01:47:45 +0100 | [diff] [blame] | 4688 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4689 | |
| 4690 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4691 | * If no default placement scheme is given, select an appropriate one. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4692 | */ |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4693 | if (!mtd->ooblayout && |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 4694 | !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4695 | switch (mtd->oobsize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | case 8: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | case 16: |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 4698 | mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | break; |
| 4700 | case 64: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 4701 | case 128: |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 4702 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops); |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 4703 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4704 | default: |
Miquel Raynal | 882fd15 | 2017-08-26 17:19:15 +0200 | [diff] [blame] | 4705 | /* |
| 4706 | * Expose the whole OOB area to users if ECC_NONE |
| 4707 | * is passed. We could do that for all kind of |
| 4708 | * ->oobsize, but we must keep the old large/small |
| 4709 | * page with ECC layout when ->oobsize <= 128 for |
| 4710 | * compatibility reasons. |
| 4711 | */ |
| 4712 | if (ecc->mode == NAND_ECC_NONE) { |
| 4713 | mtd_set_ooblayout(mtd, |
| 4714 | &nand_ooblayout_lp_ops); |
| 4715 | break; |
| 4716 | } |
| 4717 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4718 | WARN(1, "No oob scheme defined for oobsize %d\n", |
| 4719 | mtd->oobsize); |
| 4720 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4721 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | } |
| 4723 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4724 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4725 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4726 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4727 | * selected and we have 256 byte pagesize fallback to software ECC |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4728 | */ |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 4729 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4730 | switch (ecc->mode) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 4731 | case NAND_ECC_HW_OOB_FIRST: |
| 4732 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4733 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4734 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 4735 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4736 | goto err_nand_manuf_cleanup; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 4737 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4738 | if (!ecc->read_page) |
| 4739 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 4740 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4741 | case NAND_ECC_HW: |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4742 | /* Use standard hwecc read page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4743 | if (!ecc->read_page) |
| 4744 | ecc->read_page = nand_read_page_hwecc; |
| 4745 | if (!ecc->write_page) |
| 4746 | ecc->write_page = nand_write_page_hwecc; |
| 4747 | if (!ecc->read_page_raw) |
| 4748 | ecc->read_page_raw = nand_read_page_raw; |
| 4749 | if (!ecc->write_page_raw) |
| 4750 | ecc->write_page_raw = nand_write_page_raw; |
| 4751 | if (!ecc->read_oob) |
| 4752 | ecc->read_oob = nand_read_oob_std; |
| 4753 | if (!ecc->write_oob) |
| 4754 | ecc->write_oob = nand_write_oob_std; |
| 4755 | if (!ecc->read_subpage) |
| 4756 | ecc->read_subpage = nand_read_subpage; |
Helmut Schaa | 44991b3 | 2014-04-09 11:13:24 +0200 | [diff] [blame] | 4757 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4758 | ecc->write_subpage = nand_write_subpage_hwecc; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 4759 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4760 | case NAND_ECC_HW_SYNDROME: |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4761 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 4762 | (!ecc->read_page || |
| 4763 | ecc->read_page == nand_read_page_hwecc || |
| 4764 | !ecc->write_page || |
| 4765 | ecc->write_page == nand_write_page_hwecc)) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4766 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 4767 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4768 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4769 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4770 | /* Use standard syndrome read/write page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4771 | if (!ecc->read_page) |
| 4772 | ecc->read_page = nand_read_page_syndrome; |
| 4773 | if (!ecc->write_page) |
| 4774 | ecc->write_page = nand_write_page_syndrome; |
| 4775 | if (!ecc->read_page_raw) |
| 4776 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 4777 | if (!ecc->write_page_raw) |
| 4778 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 4779 | if (!ecc->read_oob) |
| 4780 | ecc->read_oob = nand_read_oob_syndrome; |
| 4781 | if (!ecc->write_oob) |
| 4782 | ecc->write_oob = nand_write_oob_syndrome; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 4783 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4784 | if (mtd->writesize >= ecc->size) { |
| 4785 | if (!ecc->strength) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4786 | WARN(1, "Driver must set ecc.strength when using hardware ECC\n"); |
| 4787 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4788 | goto err_nand_manuf_cleanup; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 4789 | } |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4790 | break; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 4791 | } |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 4792 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 4793 | ecc->size, mtd->writesize); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4794 | ecc->mode = NAND_ECC_SOFT; |
Rafał Miłecki | e9d4fae | 2016-04-17 22:53:02 +0200 | [diff] [blame] | 4795 | ecc->algo = NAND_ECC_HAMMING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4796 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4797 | case NAND_ECC_SOFT: |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4798 | ret = nand_set_ecc_soft_ops(mtd); |
| 4799 | if (ret) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4800 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4801 | goto err_nand_manuf_cleanup; |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 4802 | } |
| 4803 | break; |
| 4804 | |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 4805 | case NAND_ECC_ON_DIE: |
| 4806 | if (!ecc->read_page || !ecc->write_page) { |
| 4807 | WARN(1, "No ECC functions supplied; on-die ECC not possible\n"); |
| 4808 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4809 | goto err_nand_manuf_cleanup; |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 4810 | } |
| 4811 | if (!ecc->read_oob) |
| 4812 | ecc->read_oob = nand_read_oob_std; |
| 4813 | if (!ecc->write_oob) |
| 4814 | ecc->write_oob = nand_write_oob_std; |
| 4815 | break; |
| 4816 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4817 | case NAND_ECC_NONE: |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 4818 | pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n"); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4819 | ecc->read_page = nand_read_page_raw; |
| 4820 | ecc->write_page = nand_write_page_raw; |
| 4821 | ecc->read_oob = nand_read_oob_std; |
| 4822 | ecc->read_page_raw = nand_read_page_raw; |
| 4823 | ecc->write_page_raw = nand_write_page_raw; |
| 4824 | ecc->write_oob = nand_write_oob_std; |
| 4825 | ecc->size = mtd->writesize; |
| 4826 | ecc->bytes = 0; |
| 4827 | ecc->strength = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4828 | break; |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 4829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4830 | default: |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4831 | WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode); |
| 4832 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4833 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4834 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 4836 | /* For many systems, the standard OOB write also works for raw */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4837 | if (!ecc->read_oob_raw) |
| 4838 | ecc->read_oob_raw = ecc->read_oob; |
| 4839 | if (!ecc->write_oob_raw) |
| 4840 | ecc->write_oob_raw = ecc->write_oob; |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 4841 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4842 | /* propagate ecc info to mtd_info */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4843 | mtd->ecc_strength = ecc->strength; |
| 4844 | mtd->ecc_step_size = ecc->size; |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 4845 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 4846 | /* |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4847 | * Set the number of read / write steps for one page depending on ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4848 | * mode. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4849 | */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4850 | ecc->steps = mtd->writesize / ecc->size; |
| 4851 | if (ecc->steps * ecc->size != mtd->writesize) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4852 | WARN(1, "Invalid ECC parameters\n"); |
| 4853 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4854 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4855 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4856 | ecc->total = ecc->steps * ecc->bytes; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 4857 | if (ecc->total > mtd->oobsize) { |
| 4858 | WARN(1, "Total number of ECC bytes exceeded oobsize\n"); |
| 4859 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4860 | goto err_nand_manuf_cleanup; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 4861 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4862 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4863 | /* |
| 4864 | * The number of bytes available for a client to place data into |
| 4865 | * the out of band area. |
| 4866 | */ |
| 4867 | ret = mtd_ooblayout_count_freebytes(mtd); |
| 4868 | if (ret < 0) |
| 4869 | ret = 0; |
| 4870 | |
| 4871 | mtd->oobavail = ret; |
| 4872 | |
| 4873 | /* ECC sanity check: warn if it's too weak */ |
| 4874 | if (!nand_ecc_strength_good(mtd)) |
| 4875 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 4876 | mtd->name); |
| 4877 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4878 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 4879 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4880 | switch (ecc->steps) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4881 | case 2: |
| 4882 | mtd->subpage_sft = 1; |
| 4883 | break; |
| 4884 | case 4: |
| 4885 | case 8: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 4886 | case 16: |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4887 | mtd->subpage_sft = 2; |
| 4888 | break; |
| 4889 | } |
| 4890 | } |
| 4891 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 4892 | |
Thomas Gleixner | 04bbd0e | 2006-05-25 09:45:29 +0200 | [diff] [blame] | 4893 | /* Initialize state */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4894 | chip->state = FL_READY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | /* Invalidate the pagebuffer reference */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4897 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4898 | |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 4899 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 4900 | switch (ecc->mode) { |
| 4901 | case NAND_ECC_SOFT: |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 4902 | if (chip->page_shift > 9) |
| 4903 | chip->options |= NAND_SUBPAGE_READ; |
| 4904 | break; |
| 4905 | |
| 4906 | default: |
| 4907 | break; |
| 4908 | } |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 4909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4910 | /* Fill in remaining MTD driver data */ |
Huang Shijie | 963d1c2 | 2013-09-25 14:58:21 +0800 | [diff] [blame] | 4911 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 4912 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 4913 | MTD_CAP_NANDFLASH; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 4914 | mtd->_erase = nand_erase; |
| 4915 | mtd->_point = NULL; |
| 4916 | mtd->_unpoint = NULL; |
| 4917 | mtd->_read = nand_read; |
| 4918 | mtd->_write = nand_write; |
| 4919 | mtd->_panic_write = panic_nand_write; |
| 4920 | mtd->_read_oob = nand_read_oob; |
| 4921 | mtd->_write_oob = nand_write_oob; |
| 4922 | mtd->_sync = nand_sync; |
| 4923 | mtd->_lock = NULL; |
| 4924 | mtd->_unlock = NULL; |
| 4925 | mtd->_suspend = nand_suspend; |
| 4926 | mtd->_resume = nand_resume; |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 4927 | mtd->_reboot = nand_shutdown; |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 4928 | mtd->_block_isreserved = nand_block_isreserved; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 4929 | mtd->_block_isbad = nand_block_isbad; |
| 4930 | mtd->_block_markbad = nand_block_markbad; |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 4931 | mtd->_max_bad_blocks = nand_max_bad_blocks; |
Anatolij Gustschin | cbcab65 | 2010-12-16 23:42:16 +0100 | [diff] [blame] | 4932 | mtd->writebufsize = mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | |
Shmulik Ladkani | ea3b2ea | 2012-06-08 18:29:06 +0300 | [diff] [blame] | 4934 | /* |
| 4935 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 4936 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 4937 | * properly set. |
| 4938 | */ |
| 4939 | if (!mtd->bitflip_threshold) |
Brian Norris | 240181f | 2015-01-12 12:51:29 -0800 | [diff] [blame] | 4940 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4942 | /* Initialize the ->data_interface field. */ |
| 4943 | ret = nand_init_data_interface(chip); |
| 4944 | if (ret) |
| 4945 | goto err_nand_manuf_cleanup; |
| 4946 | |
| 4947 | /* Enter fastest possible mode on all dies. */ |
| 4948 | for (i = 0; i < chip->numchips; i++) { |
| 4949 | chip->select_chip(mtd, i); |
| 4950 | ret = nand_setup_data_interface(chip, i); |
| 4951 | chip->select_chip(mtd, -1); |
| 4952 | |
| 4953 | if (ret) |
| 4954 | goto err_nand_data_iface_cleanup; |
| 4955 | } |
| 4956 | |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 4957 | /* Check, if we should skip the bad block table scan */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4958 | if (chip->options & NAND_SKIP_BBTSCAN) |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 4959 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4960 | |
| 4961 | /* Build bad block table */ |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 4962 | ret = chip->scan_bbt(mtd); |
| 4963 | if (ret) |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4964 | goto err_nand_data_iface_cleanup; |
| 4965 | |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 4966 | return 0; |
| 4967 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4968 | err_nand_data_iface_cleanup: |
| 4969 | nand_release_data_interface(chip); |
| 4970 | |
| 4971 | err_nand_manuf_cleanup: |
| 4972 | nand_manufacturer_cleanup(chip); |
| 4973 | |
| 4974 | err_free_nbuf: |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4975 | if (nbuf) { |
| 4976 | kfree(nbuf->databuf); |
| 4977 | kfree(nbuf->ecccode); |
| 4978 | kfree(nbuf->ecccalc); |
| 4979 | kfree(nbuf); |
| 4980 | } |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 4981 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4982 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4983 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4984 | EXPORT_SYMBOL(nand_scan_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4986 | /* |
| 4987 | * is_module_text_address() isn't exported, and it's mostly a pointless |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4988 | * test if this is a module _anyway_ -- they'd have to try _really_ hard |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4989 | * to call us from in-kernel code if the core NAND support is modular. |
| 4990 | */ |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4991 | #ifdef MODULE |
| 4992 | #define caller_is_module() (1) |
| 4993 | #else |
| 4994 | #define caller_is_module() \ |
Rusty Russell | a6e6abd | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 4995 | is_module_text_address((unsigned long)__builtin_return_address(0)) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4996 | #endif |
| 4997 | |
| 4998 | /** |
| 4999 | * nand_scan - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5000 | * @mtd: MTD device structure |
| 5001 | * @maxchips: number of chips to scan for |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5002 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5003 | * This fills out all the uninitialized function pointers with the defaults. |
| 5004 | * The flash ID is read and the mtd/chip structures are filled with the |
Ezequiel García | 20c07a5 | 2016-04-01 18:29:23 -0300 | [diff] [blame] | 5005 | * appropriate values. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5006 | */ |
| 5007 | int nand_scan(struct mtd_info *mtd, int maxchips) |
| 5008 | { |
| 5009 | int ret; |
| 5010 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 5011 | ret = nand_scan_ident(mtd, maxchips, NULL); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5012 | if (!ret) |
| 5013 | ret = nand_scan_tail(mtd); |
| 5014 | return ret; |
| 5015 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5016 | EXPORT_SYMBOL(nand_scan); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5018 | /** |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5019 | * nand_cleanup - [NAND Interface] Free resources held by the NAND device |
| 5020 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5021 | */ |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5022 | void nand_cleanup(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | { |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5024 | if (chip->ecc.mode == NAND_ECC_SOFT && |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5025 | chip->ecc.algo == NAND_ECC_BCH) |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 5026 | nand_bch_free((struct nand_bch_control *)chip->ecc.priv); |
| 5027 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 5028 | nand_release_data_interface(chip); |
| 5029 | |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 5030 | /* Free bad block table memory */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5031 | kfree(chip->bbt); |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5032 | if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) { |
| 5033 | kfree(chip->buffers->databuf); |
| 5034 | kfree(chip->buffers->ecccode); |
| 5035 | kfree(chip->buffers->ecccalc); |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 5036 | kfree(chip->buffers); |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5037 | } |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 5038 | |
| 5039 | /* Free bad block descriptor memory */ |
| 5040 | if (chip->badblock_pattern && chip->badblock_pattern->options |
| 5041 | & NAND_BBT_DYNAMICSTRUCT) |
| 5042 | kfree(chip->badblock_pattern); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5043 | |
| 5044 | /* Free manufacturer priv data. */ |
| 5045 | nand_manufacturer_cleanup(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | } |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5047 | EXPORT_SYMBOL_GPL(nand_cleanup); |
| 5048 | |
| 5049 | /** |
| 5050 | * nand_release - [NAND Interface] Unregister the MTD device and free resources |
| 5051 | * held by the NAND device |
| 5052 | * @mtd: MTD device structure |
| 5053 | */ |
| 5054 | void nand_release(struct mtd_info *mtd) |
| 5055 | { |
| 5056 | mtd_device_unregister(mtd); |
| 5057 | nand_cleanup(mtd_to_nand(mtd)); |
| 5058 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5059 | EXPORT_SYMBOL_GPL(nand_release); |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 5060 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5061 | MODULE_LICENSE("GPL"); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5062 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 5063 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5064 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |