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> |
| 42 | #include <linux/mtd/nand.h> |
| 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 | |
| 118 | if (section) |
| 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); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 730 | /* One more address cycle for devices > 32MiB */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 731 | if (chip->chipsize > (32 << 20)) |
| 732 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 733 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 734 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 735 | |
| 736 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 737 | * Program and erase have their own busy handlers status and sequential |
| 738 | * in needs no delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 739 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | case NAND_CMD_PAGEPROG: |
| 743 | case NAND_CMD_ERASE1: |
| 744 | case NAND_CMD_ERASE2: |
| 745 | case NAND_CMD_SEQIN: |
| 746 | case NAND_CMD_STATUS: |
Masahiro Yamada | 3158fa0 | 2017-03-23 09:17:49 +0900 | [diff] [blame] | 747 | case NAND_CMD_READID: |
Masahiro Yamada | c5d664a | 2017-03-23 09:17:50 +0900 | [diff] [blame] | 748 | case NAND_CMD_SET_FEATURES: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | return; |
| 750 | |
| 751 | case NAND_CMD_RESET: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 752 | if (chip->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | break; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 754 | udelay(chip->chip_delay); |
| 755 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 756 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 757 | chip->cmd_ctrl(mtd, |
| 758 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 759 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 760 | nand_wait_status_ready(mtd, 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | return; |
| 762 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 763 | /* This applies to read commands */ |
Boris Brezillon | 2165c4a | 2017-05-16 18:35:45 +0200 | [diff] [blame] | 764 | case NAND_CMD_READ0: |
| 765 | /* |
| 766 | * READ0 is sometimes used to exit GET STATUS mode. When this |
| 767 | * is the case no address cycles are requested, and we can use |
| 768 | * this information to detect that we should not wait for the |
| 769 | * device to be ready. |
| 770 | */ |
| 771 | if (column == -1 && page_addr == -1) |
| 772 | return; |
| 773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 775 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | * If we don't have access to the busy pin, we apply the given |
| 777 | * command delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 778 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 779 | if (!chip->dev_ready) { |
| 780 | udelay(chip->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 782 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 784 | /* |
| 785 | * Apply this short delay always to ensure that we do wait tWB in |
| 786 | * any case on any machine. |
| 787 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 788 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 789 | |
| 790 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 793 | static void nand_ccs_delay(struct nand_chip *chip) |
| 794 | { |
| 795 | /* |
| 796 | * The controller already takes care of waiting for tCCS when the RNDIN |
| 797 | * or RNDOUT command is sent, return directly. |
| 798 | */ |
| 799 | if (!(chip->options & NAND_WAIT_TCCS)) |
| 800 | return; |
| 801 | |
| 802 | /* |
| 803 | * Wait tCCS_min if it is correctly defined, otherwise wait 500ns |
| 804 | * (which should be safe for all NANDs). |
| 805 | */ |
| 806 | if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min) |
| 807 | ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000); |
| 808 | else |
| 809 | ndelay(500); |
| 810 | } |
| 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | /** |
| 813 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 814 | * @mtd: MTD device structure |
| 815 | * @command: the command to be sent |
| 816 | * @column: the column address for this command, -1 if none |
| 817 | * @page_addr: the page address for this command, -1 if none |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | * |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 819 | * 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] | 820 | * devices. We don't have the separate regions as we have in the small page |
| 821 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 823 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
| 824 | int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 826 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
| 828 | /* Emulate NAND_CMD_READOOB */ |
| 829 | if (command == NAND_CMD_READOOB) { |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 830 | column += mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | command = NAND_CMD_READ0; |
| 832 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 833 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 834 | /* Command latch cycle */ |
Alexander Shiyan | fb066ad | 2013-02-28 12:02:19 +0400 | [diff] [blame] | 835 | chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
| 837 | if (column != -1 || page_addr != -1) { |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 838 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
| 840 | /* Serially input address */ |
| 841 | if (column != -1) { |
| 842 | /* Adjust columns for 16 bit buswidth */ |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 843 | if (chip->options & NAND_BUSWIDTH_16 && |
| 844 | !nand_opcode_8bits(command)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | column >>= 1; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 846 | chip->cmd_ctrl(mtd, column, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 847 | ctrl &= ~NAND_CTRL_CHANGE; |
Boris Brezillon | fde85cf | 2016-06-15 13:09:51 +0200 | [diff] [blame] | 848 | |
Brian Norris | f5b88de | 2016-10-03 09:49:35 -0700 | [diff] [blame] | 849 | /* Only output a single addr cycle for 8bits opcodes. */ |
Boris Brezillon | fde85cf | 2016-06-15 13:09:51 +0200 | [diff] [blame] | 850 | if (!nand_opcode_8bits(command)) |
| 851 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 852 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | if (page_addr != -1) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 854 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 855 | chip->cmd_ctrl(mtd, page_addr >> 8, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 856 | NAND_NCE | NAND_ALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | /* One more address cycle for devices > 128MiB */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 858 | if (chip->chipsize > (128 << 20)) |
| 859 | chip->cmd_ctrl(mtd, page_addr >> 16, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 860 | NAND_NCE | NAND_ALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 863 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 864 | |
| 865 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 866 | * Program and erase have their own busy handlers status, sequential |
Gerhard Sittig | 7a442f1 | 2014-03-29 14:36:22 +0100 | [diff] [blame] | 867 | * in and status need no delay. |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 868 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | case NAND_CMD_CACHEDPROG: |
| 872 | case NAND_CMD_PAGEPROG: |
| 873 | case NAND_CMD_ERASE1: |
| 874 | case NAND_CMD_ERASE2: |
| 875 | case NAND_CMD_SEQIN: |
| 876 | case NAND_CMD_STATUS: |
Masahiro Yamada | 3158fa0 | 2017-03-23 09:17:49 +0900 | [diff] [blame] | 877 | case NAND_CMD_READID: |
Masahiro Yamada | c5d664a | 2017-03-23 09:17:50 +0900 | [diff] [blame] | 878 | case NAND_CMD_SET_FEATURES: |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 879 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 881 | case NAND_CMD_RNDIN: |
| 882 | nand_ccs_delay(chip); |
| 883 | return; |
| 884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | case NAND_CMD_RESET: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 886 | if (chip->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | break; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 888 | udelay(chip->chip_delay); |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 889 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 890 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 891 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 892 | NAND_NCE | NAND_CTRL_CHANGE); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 893 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 894 | nand_wait_status_ready(mtd, 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | return; |
| 896 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 897 | case NAND_CMD_RNDOUT: |
| 898 | /* No ready / busy check necessary */ |
| 899 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, |
| 900 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 901 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 902 | NAND_NCE | NAND_CTRL_CHANGE); |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 903 | |
| 904 | nand_ccs_delay(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 905 | return; |
| 906 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | case NAND_CMD_READ0: |
Boris Brezillon | 2165c4a | 2017-05-16 18:35:45 +0200 | [diff] [blame] | 908 | /* |
| 909 | * READ0 is sometimes used to exit GET STATUS mode. When this |
| 910 | * is the case no address cycles are requested, and we can use |
| 911 | * this information to detect that READSTART should not be |
| 912 | * issued. |
| 913 | */ |
| 914 | if (column == -1 && page_addr == -1) |
| 915 | return; |
| 916 | |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 917 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
| 918 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 919 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 920 | NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 921 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 922 | /* This applies to read commands */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 924 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | * 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] | 926 | * command delay. |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 927 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 928 | if (!chip->dev_ready) { |
| 929 | udelay(chip->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 931 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | } |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 933 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 934 | /* |
| 935 | * Apply this short delay always to ensure that we do wait tWB in |
| 936 | * any case on any machine. |
| 937 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 938 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 939 | |
| 940 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 944 | * panic_nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 945 | * @chip: the nand chip descriptor |
| 946 | * @mtd: MTD device structure |
| 947 | * @new_state: the state which is requested |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 948 | * |
| 949 | * Used when in panic, no locks are taken. |
| 950 | */ |
| 951 | static void panic_nand_get_device(struct nand_chip *chip, |
| 952 | struct mtd_info *mtd, int new_state) |
| 953 | { |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 954 | /* Hardware controller shared among independent devices */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 955 | chip->controller->active = chip; |
| 956 | chip->state = new_state; |
| 957 | } |
| 958 | |
| 959 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | * nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 961 | * @mtd: MTD device structure |
| 962 | * @new_state: the state which is requested |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | * |
| 964 | * Get the device and lock it for exclusive access |
| 965 | */ |
Thomas Gleixner | 2c0a2be | 2006-05-23 11:50:56 +0200 | [diff] [blame] | 966 | static int |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 967 | nand_get_device(struct mtd_info *mtd, int new_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 969 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 970 | spinlock_t *lock = &chip->controller->lock; |
| 971 | wait_queue_head_t *wq = &chip->controller->wq; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 972 | DECLARE_WAITQUEUE(wait, current); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 973 | retry: |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 974 | spin_lock(lock); |
| 975 | |
vimal singh | b8b3ee9 | 2009-07-09 20:41:22 +0530 | [diff] [blame] | 976 | /* Hardware controller shared among independent devices */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 977 | if (!chip->controller->active) |
| 978 | chip->controller->active = chip; |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 979 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 980 | if (chip->controller->active == chip && chip->state == FL_READY) { |
| 981 | chip->state = new_state; |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 982 | spin_unlock(lock); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 983 | return 0; |
| 984 | } |
| 985 | if (new_state == FL_PM_SUSPENDED) { |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 986 | if (chip->controller->active->state == FL_PM_SUSPENDED) { |
| 987 | chip->state = FL_PM_SUSPENDED; |
| 988 | spin_unlock(lock); |
| 989 | return 0; |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 990 | } |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 991 | } |
| 992 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 993 | add_wait_queue(wq, &wait); |
| 994 | spin_unlock(lock); |
| 995 | schedule(); |
| 996 | remove_wait_queue(wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | goto retry; |
| 998 | } |
| 999 | |
| 1000 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1001 | * panic_nand_wait - [GENERIC] wait until the command is done |
| 1002 | * @mtd: MTD device structure |
| 1003 | * @chip: NAND chip structure |
| 1004 | * @timeo: timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1005 | * |
| 1006 | * Wait for command done. This is a helper function for nand_wait used when |
| 1007 | * 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] | 1008 | * an oops through mtdoops. |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1009 | */ |
| 1010 | static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, |
| 1011 | unsigned long timeo) |
| 1012 | { |
| 1013 | int i; |
| 1014 | for (i = 0; i < timeo; i++) { |
| 1015 | if (chip->dev_ready) { |
| 1016 | if (chip->dev_ready(mtd)) |
| 1017 | break; |
| 1018 | } else { |
| 1019 | if (chip->read_byte(mtd) & NAND_STATUS_READY) |
| 1020 | break; |
| 1021 | } |
| 1022 | mdelay(1); |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1023 | } |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1027 | * nand_wait - [DEFAULT] wait until the command is done |
| 1028 | * @mtd: MTD device structure |
| 1029 | * @chip: NAND chip structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | * |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1031 | * Wait for command done. This applies to erase and program only. |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 1032 | */ |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 1033 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | { |
| 1035 | |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1036 | int status; |
| 1037 | unsigned long timeo = 400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1039 | /* |
| 1040 | * Apply this short delay always to ensure that we do wait tWB in any |
| 1041 | * case on any machine. |
| 1042 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1043 | ndelay(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | |
Artem Bityutskiy | 14c6578 | 2013-03-04 14:21:34 +0200 | [diff] [blame] | 1045 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1047 | if (in_interrupt() || oops_in_progress) |
| 1048 | panic_nand_wait(mtd, chip, timeo); |
| 1049 | else { |
Huang Shijie | 6d2559f | 2013-01-30 10:03:56 +0800 | [diff] [blame] | 1050 | timeo = jiffies + msecs_to_jiffies(timeo); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1051 | do { |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1052 | if (chip->dev_ready) { |
| 1053 | if (chip->dev_ready(mtd)) |
| 1054 | break; |
| 1055 | } else { |
| 1056 | if (chip->read_byte(mtd) & NAND_STATUS_READY) |
| 1057 | break; |
| 1058 | } |
| 1059 | cond_resched(); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1060 | } while (time_before(jiffies, timeo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | } |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 1062 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1063 | status = (int)chip->read_byte(mtd); |
Matthieu CASTET | f251b8d | 2012-11-05 15:00:44 +0100 | [diff] [blame] | 1064 | /* This can happen if in case of timeout or buggy dev_ready */ |
| 1065 | WARN_ON(!(status & NAND_STATUS_READY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | return status; |
| 1067 | } |
| 1068 | |
| 1069 | /** |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1070 | * nand_reset_data_interface - Reset data interface and timings |
| 1071 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1072 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1073 | * |
| 1074 | * Reset the Data interface and timings to ONFI mode 0. |
| 1075 | * |
| 1076 | * Returns 0 for success or negative error code otherwise. |
| 1077 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1078 | static int nand_reset_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1079 | { |
| 1080 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1081 | const struct nand_data_interface *conf; |
| 1082 | int ret; |
| 1083 | |
| 1084 | if (!chip->setup_data_interface) |
| 1085 | return 0; |
| 1086 | |
| 1087 | /* |
| 1088 | * The ONFI specification says: |
| 1089 | * " |
| 1090 | * To transition from NV-DDR or NV-DDR2 to the SDR data |
| 1091 | * interface, the host shall use the Reset (FFh) command |
| 1092 | * using SDR timing mode 0. A device in any timing mode is |
| 1093 | * required to recognize Reset (FFh) command issued in SDR |
| 1094 | * timing mode 0. |
| 1095 | * " |
| 1096 | * |
| 1097 | * Configure the data interface in SDR mode and set the |
| 1098 | * timings to timing mode 0. |
| 1099 | */ |
| 1100 | |
| 1101 | conf = nand_get_default_data_interface(); |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1102 | ret = chip->setup_data_interface(mtd, chipnr, conf); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1103 | if (ret) |
| 1104 | pr_err("Failed to configure data interface to SDR timing mode 0\n"); |
| 1105 | |
| 1106 | return ret; |
| 1107 | } |
| 1108 | |
| 1109 | /** |
| 1110 | * nand_setup_data_interface - Setup the best data interface and timings |
| 1111 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1112 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1113 | * |
| 1114 | * Find and configure the best data interface and NAND timings supported by |
| 1115 | * the chip and the driver. |
| 1116 | * First tries to retrieve supported timing modes from ONFI information, |
| 1117 | * and if the NAND chip does not support ONFI, relies on the |
| 1118 | * ->onfi_timing_mode_default specified in the nand_ids table. |
| 1119 | * |
| 1120 | * Returns 0 for success or negative error code otherwise. |
| 1121 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1122 | static int nand_setup_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1123 | { |
| 1124 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1125 | int ret; |
| 1126 | |
| 1127 | if (!chip->setup_data_interface || !chip->data_interface) |
| 1128 | return 0; |
| 1129 | |
| 1130 | /* |
| 1131 | * Ensure the timing mode has been changed on the chip side |
| 1132 | * before changing timings on the controller side. |
| 1133 | */ |
Boris Brezillon | a11bf5e | 2017-07-31 10:29:56 +0200 | [diff] [blame^] | 1134 | if (chip->onfi_version && |
| 1135 | (le16_to_cpu(chip->onfi_params.opt_cmd) & |
| 1136 | ONFI_OPT_CMD_SET_GET_FEATURES)) { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1137 | u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { |
| 1138 | chip->onfi_timing_mode_default, |
| 1139 | }; |
| 1140 | |
| 1141 | ret = chip->onfi_set_features(mtd, chip, |
| 1142 | ONFI_FEATURE_ADDR_TIMING_MODE, |
| 1143 | tmode_param); |
| 1144 | if (ret) |
| 1145 | goto err; |
| 1146 | } |
| 1147 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1148 | ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1149 | err: |
| 1150 | return ret; |
| 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * nand_init_data_interface - find the best data interface and timings |
| 1155 | * @chip: The NAND chip |
| 1156 | * |
| 1157 | * Find the best data interface and NAND timings supported by the chip |
| 1158 | * and the driver. |
| 1159 | * First tries to retrieve supported timing modes from ONFI information, |
| 1160 | * and if the NAND chip does not support ONFI, relies on the |
| 1161 | * ->onfi_timing_mode_default specified in the nand_ids table. After this |
| 1162 | * function nand_chip->data_interface is initialized with the best timing mode |
| 1163 | * available. |
| 1164 | * |
| 1165 | * Returns 0 for success or negative error code otherwise. |
| 1166 | */ |
| 1167 | static int nand_init_data_interface(struct nand_chip *chip) |
| 1168 | { |
| 1169 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1170 | int modes, mode, ret; |
| 1171 | |
| 1172 | if (!chip->setup_data_interface) |
| 1173 | return 0; |
| 1174 | |
| 1175 | /* |
| 1176 | * First try to identify the best timings from ONFI parameters and |
| 1177 | * if the NAND does not support ONFI, fallback to the default ONFI |
| 1178 | * timing mode. |
| 1179 | */ |
| 1180 | modes = onfi_get_async_timing_mode(chip); |
| 1181 | if (modes == ONFI_TIMING_MODE_UNKNOWN) { |
| 1182 | if (!chip->onfi_timing_mode_default) |
| 1183 | return 0; |
| 1184 | |
| 1185 | modes = GENMASK(chip->onfi_timing_mode_default, 0); |
| 1186 | } |
| 1187 | |
| 1188 | chip->data_interface = kzalloc(sizeof(*chip->data_interface), |
| 1189 | GFP_KERNEL); |
| 1190 | if (!chip->data_interface) |
| 1191 | return -ENOMEM; |
| 1192 | |
| 1193 | for (mode = fls(modes) - 1; mode >= 0; mode--) { |
| 1194 | ret = onfi_init_data_interface(chip, chip->data_interface, |
| 1195 | NAND_SDR_IFACE, mode); |
| 1196 | if (ret) |
| 1197 | continue; |
| 1198 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1199 | /* Pass -1 to only */ |
| 1200 | ret = chip->setup_data_interface(mtd, |
| 1201 | NAND_DATA_IFACE_CHECK_ONLY, |
| 1202 | chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1203 | if (!ret) { |
| 1204 | chip->onfi_timing_mode_default = mode; |
| 1205 | break; |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
| 1212 | static void nand_release_data_interface(struct nand_chip *chip) |
| 1213 | { |
| 1214 | kfree(chip->data_interface); |
| 1215 | } |
| 1216 | |
| 1217 | /** |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1218 | * nand_reset - Reset and initialize a NAND device |
| 1219 | * @chip: The NAND chip |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1220 | * @chipnr: Internal die id |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1221 | * |
| 1222 | * Returns 0 for success or negative error code otherwise |
| 1223 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1224 | int nand_reset(struct nand_chip *chip, int chipnr) |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1225 | { |
| 1226 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1227 | int ret; |
| 1228 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1229 | ret = nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1230 | if (ret) |
| 1231 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1232 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1233 | /* |
| 1234 | * The CS line has to be released before we can apply the new NAND |
| 1235 | * interface settings, hence this weird ->select_chip() dance. |
| 1236 | */ |
| 1237 | chip->select_chip(mtd, chipnr); |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1238 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1239 | chip->select_chip(mtd, -1); |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1240 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1241 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1242 | ret = nand_setup_data_interface(chip, chipnr); |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1243 | chip->select_chip(mtd, -1); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1244 | if (ret) |
| 1245 | return ret; |
| 1246 | |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1247 | return 0; |
| 1248 | } |
| 1249 | |
| 1250 | /** |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 1251 | * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 1252 | * @mtd: mtd info |
| 1253 | * @ofs: offset to start unlock from |
| 1254 | * @len: length to unlock |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 1255 | * @invert: |
| 1256 | * - when = 0, unlock the range of blocks within the lower and |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1257 | * upper boundary address |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 1258 | * - when = 1, unlock the range of blocks outside the boundaries |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1259 | * of the lower and upper boundary address |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1260 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1261 | * Returs unlock status. |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1262 | */ |
| 1263 | static int __nand_unlock(struct mtd_info *mtd, loff_t ofs, |
| 1264 | uint64_t len, int invert) |
| 1265 | { |
| 1266 | int ret = 0; |
| 1267 | int status, page; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 1268 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1269 | |
| 1270 | /* Submit address of first page to unlock */ |
| 1271 | page = ofs >> chip->page_shift; |
| 1272 | chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask); |
| 1273 | |
| 1274 | /* Submit address of last page to unlock */ |
| 1275 | page = (ofs + len) >> chip->page_shift; |
| 1276 | chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1, |
| 1277 | (page | invert) & chip->pagemask); |
| 1278 | |
| 1279 | /* Call wait ready function */ |
| 1280 | status = chip->waitfunc(mtd, chip); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1281 | /* See if device thinks it succeeded */ |
Huang Shijie | 7483096 | 2012-10-14 23:47:24 -0400 | [diff] [blame] | 1282 | if (status & NAND_STATUS_FAIL) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1283 | pr_debug("%s: error status = 0x%08x\n", |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1284 | __func__, status); |
| 1285 | ret = -EIO; |
| 1286 | } |
| 1287 | |
| 1288 | return ret; |
| 1289 | } |
| 1290 | |
| 1291 | /** |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 1292 | * nand_unlock - [REPLACEABLE] unlocks specified locked blocks |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 1293 | * @mtd: mtd info |
| 1294 | * @ofs: offset to start unlock from |
| 1295 | * @len: length to unlock |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1296 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1297 | * Returns unlock status. |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1298 | */ |
| 1299 | int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 1300 | { |
| 1301 | int ret = 0; |
| 1302 | int chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 1303 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1304 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1305 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1306 | __func__, (unsigned long long)ofs, len); |
| 1307 | |
| 1308 | if (check_offs_len(mtd, ofs, len)) |
Brian Norris | b1a2348 | 2015-02-28 02:02:27 -0800 | [diff] [blame] | 1309 | return -EINVAL; |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1310 | |
| 1311 | /* Align to last block address if size addresses end of the device */ |
| 1312 | if (ofs + len == mtd->size) |
| 1313 | len -= mtd->erasesize; |
| 1314 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 1315 | nand_get_device(mtd, FL_UNLOCKING); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1316 | |
| 1317 | /* Shift to get chip number */ |
| 1318 | chipnr = ofs >> chip->chip_shift; |
| 1319 | |
White Ding | 57d3a9a | 2014-07-24 00:10:45 +0800 | [diff] [blame] | 1320 | /* |
| 1321 | * Reset the chip. |
| 1322 | * If we want to check the WP through READ STATUS and check the bit 7 |
| 1323 | * we must reset the chip |
| 1324 | * some operation can also clear the bit 7 of status register |
| 1325 | * eg. erase/program a locked block |
| 1326 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1327 | nand_reset(chip, chipnr); |
| 1328 | |
| 1329 | chip->select_chip(mtd, chipnr); |
White Ding | 57d3a9a | 2014-07-24 00:10:45 +0800 | [diff] [blame] | 1330 | |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1331 | /* Check, if it is write protected */ |
| 1332 | if (nand_check_wp(mtd)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1333 | pr_debug("%s: device is write protected!\n", |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1334 | __func__); |
| 1335 | ret = -EIO; |
| 1336 | goto out; |
| 1337 | } |
| 1338 | |
| 1339 | ret = __nand_unlock(mtd, ofs, len, 0); |
| 1340 | |
| 1341 | out: |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 1342 | chip->select_chip(mtd, -1); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1343 | nand_release_device(mtd); |
| 1344 | |
| 1345 | return ret; |
| 1346 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1347 | EXPORT_SYMBOL(nand_unlock); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1348 | |
| 1349 | /** |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 1350 | * nand_lock - [REPLACEABLE] locks all blocks present in the device |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 1351 | * @mtd: mtd info |
| 1352 | * @ofs: offset to start unlock from |
| 1353 | * @len: length to unlock |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1354 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1355 | * This feature is not supported in many NAND parts. 'Micron' NAND parts do |
| 1356 | * have this feature, but it allows only to lock all blocks, not for specified |
| 1357 | * range for block. Implementing 'lock' feature by making use of 'unlock', for |
| 1358 | * now. |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1359 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1360 | * Returns lock status. |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1361 | */ |
| 1362 | int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 1363 | { |
| 1364 | int ret = 0; |
| 1365 | int chipnr, status, page; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 1366 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1367 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1368 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1369 | __func__, (unsigned long long)ofs, len); |
| 1370 | |
| 1371 | if (check_offs_len(mtd, ofs, len)) |
Brian Norris | b1a2348 | 2015-02-28 02:02:27 -0800 | [diff] [blame] | 1372 | return -EINVAL; |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1373 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 1374 | nand_get_device(mtd, FL_LOCKING); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1375 | |
| 1376 | /* Shift to get chip number */ |
| 1377 | chipnr = ofs >> chip->chip_shift; |
| 1378 | |
White Ding | 57d3a9a | 2014-07-24 00:10:45 +0800 | [diff] [blame] | 1379 | /* |
| 1380 | * Reset the chip. |
| 1381 | * If we want to check the WP through READ STATUS and check the bit 7 |
| 1382 | * we must reset the chip |
| 1383 | * some operation can also clear the bit 7 of status register |
| 1384 | * eg. erase/program a locked block |
| 1385 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1386 | nand_reset(chip, chipnr); |
| 1387 | |
| 1388 | chip->select_chip(mtd, chipnr); |
White Ding | 57d3a9a | 2014-07-24 00:10:45 +0800 | [diff] [blame] | 1389 | |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1390 | /* Check, if it is write protected */ |
| 1391 | if (nand_check_wp(mtd)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1392 | pr_debug("%s: device is write protected!\n", |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1393 | __func__); |
| 1394 | status = MTD_ERASE_FAILED; |
| 1395 | ret = -EIO; |
| 1396 | goto out; |
| 1397 | } |
| 1398 | |
| 1399 | /* Submit address of first page to lock */ |
| 1400 | page = ofs >> chip->page_shift; |
| 1401 | chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask); |
| 1402 | |
| 1403 | /* Call wait ready function */ |
| 1404 | status = chip->waitfunc(mtd, chip); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1405 | /* See if device thinks it succeeded */ |
Huang Shijie | 7483096 | 2012-10-14 23:47:24 -0400 | [diff] [blame] | 1406 | if (status & NAND_STATUS_FAIL) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1407 | pr_debug("%s: error status = 0x%08x\n", |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1408 | __func__, status); |
| 1409 | ret = -EIO; |
| 1410 | goto out; |
| 1411 | } |
| 1412 | |
| 1413 | ret = __nand_unlock(mtd, ofs, len, 0x1); |
| 1414 | |
| 1415 | out: |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 1416 | chip->select_chip(mtd, -1); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1417 | nand_release_device(mtd); |
| 1418 | |
| 1419 | return ret; |
| 1420 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1421 | EXPORT_SYMBOL(nand_lock); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1422 | |
| 1423 | /** |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 1424 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 1425 | * @buf: buffer to test |
| 1426 | * @len: buffer length |
| 1427 | * @bitflips_threshold: maximum number of bitflips |
| 1428 | * |
| 1429 | * Check if a buffer contains only 0xff, which means the underlying region |
| 1430 | * has been erased and is ready to be programmed. |
| 1431 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1432 | * considering the region is not erased. |
| 1433 | * Note: The logic of this function has been extracted from the memweight |
| 1434 | * implementation, except that nand_check_erased_buf function exit before |
| 1435 | * testing the whole buffer if the number of bitflips exceed the |
| 1436 | * bitflips_threshold value. |
| 1437 | * |
| 1438 | * Returns a positive number of bitflips less than or equal to |
| 1439 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1440 | * threshold. |
| 1441 | */ |
| 1442 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 1443 | { |
| 1444 | const unsigned char *bitmap = buf; |
| 1445 | int bitflips = 0; |
| 1446 | int weight; |
| 1447 | |
| 1448 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 1449 | len--, bitmap++) { |
| 1450 | weight = hweight8(*bitmap); |
| 1451 | bitflips += BITS_PER_BYTE - weight; |
| 1452 | if (unlikely(bitflips > bitflips_threshold)) |
| 1453 | return -EBADMSG; |
| 1454 | } |
| 1455 | |
| 1456 | for (; len >= sizeof(long); |
| 1457 | len -= sizeof(long), bitmap += sizeof(long)) { |
Pavel Machek | 086567f | 2017-04-21 12:51:07 +0200 | [diff] [blame] | 1458 | unsigned long d = *((unsigned long *)bitmap); |
| 1459 | if (d == ~0UL) |
| 1460 | continue; |
| 1461 | weight = hweight_long(d); |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 1462 | bitflips += BITS_PER_LONG - weight; |
| 1463 | if (unlikely(bitflips > bitflips_threshold)) |
| 1464 | return -EBADMSG; |
| 1465 | } |
| 1466 | |
| 1467 | for (; len > 0; len--, bitmap++) { |
| 1468 | weight = hweight8(*bitmap); |
| 1469 | bitflips += BITS_PER_BYTE - weight; |
| 1470 | if (unlikely(bitflips > bitflips_threshold)) |
| 1471 | return -EBADMSG; |
| 1472 | } |
| 1473 | |
| 1474 | return bitflips; |
| 1475 | } |
| 1476 | |
| 1477 | /** |
| 1478 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 1479 | * 0xff data |
| 1480 | * @data: data buffer to test |
| 1481 | * @datalen: data length |
| 1482 | * @ecc: ECC buffer |
| 1483 | * @ecclen: ECC length |
| 1484 | * @extraoob: extra OOB buffer |
| 1485 | * @extraooblen: extra OOB length |
| 1486 | * @bitflips_threshold: maximum number of bitflips |
| 1487 | * |
| 1488 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 1489 | * 0xff pattern, which means the underlying region has been erased and is |
| 1490 | * ready to be programmed. |
| 1491 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1492 | * considering the region as not erased. |
| 1493 | * |
| 1494 | * Note: |
| 1495 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 1496 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 1497 | * report the number of errors per chunk, and the NAND core infrastructure |
| 1498 | * expect you to return the maximum number of bitflips for the whole page. |
| 1499 | * This is why you should always use this function on a single chunk and |
| 1500 | * not on the whole page. After checking each chunk you should update your |
| 1501 | * max_bitflips value accordingly. |
| 1502 | * 2/ When checking for bitflips in erased pages you should not only check |
| 1503 | * the payload data but also their associated ECC data, because a user might |
| 1504 | * have programmed almost all bits to 1 but a few. In this case, we |
| 1505 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 1506 | * this case. |
| 1507 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 1508 | * data are protected by the ECC engine. |
| 1509 | * It could also be used if you support subpages and want to attach some |
| 1510 | * extra OOB data to an ECC chunk. |
| 1511 | * |
| 1512 | * Returns a positive number of bitflips less than or equal to |
| 1513 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1514 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 1515 | */ |
| 1516 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 1517 | void *ecc, int ecclen, |
| 1518 | void *extraoob, int extraooblen, |
| 1519 | int bitflips_threshold) |
| 1520 | { |
| 1521 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 1522 | |
| 1523 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 1524 | bitflips_threshold); |
| 1525 | if (data_bitflips < 0) |
| 1526 | return data_bitflips; |
| 1527 | |
| 1528 | bitflips_threshold -= data_bitflips; |
| 1529 | |
| 1530 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 1531 | if (ecc_bitflips < 0) |
| 1532 | return ecc_bitflips; |
| 1533 | |
| 1534 | bitflips_threshold -= ecc_bitflips; |
| 1535 | |
| 1536 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 1537 | bitflips_threshold); |
| 1538 | if (extraoob_bitflips < 0) |
| 1539 | return extraoob_bitflips; |
| 1540 | |
| 1541 | if (data_bitflips) |
| 1542 | memset(data, 0xff, datalen); |
| 1543 | |
| 1544 | if (ecc_bitflips) |
| 1545 | memset(ecc, 0xff, ecclen); |
| 1546 | |
| 1547 | if (extraoob_bitflips) |
| 1548 | memset(extraoob, 0xff, extraooblen); |
| 1549 | |
| 1550 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 1551 | } |
| 1552 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
| 1553 | |
| 1554 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1555 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1556 | * @mtd: mtd info structure |
| 1557 | * @chip: nand chip info structure |
| 1558 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1559 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1560 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1561 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1562 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1563 | */ |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 1564 | int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 1565 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1566 | { |
| 1567 | chip->read_buf(mtd, buf, mtd->writesize); |
Brian Norris | 279f08d | 2012-05-02 10:15:03 -0700 | [diff] [blame] | 1568 | if (oob_required) |
| 1569 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1570 | return 0; |
| 1571 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 1572 | EXPORT_SYMBOL(nand_read_page_raw); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1573 | |
| 1574 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1575 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1576 | * @mtd: mtd info structure |
| 1577 | * @chip: nand chip info structure |
| 1578 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1579 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1580 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1581 | * |
| 1582 | * We need a special oob layout and handling even when OOB isn't used. |
| 1583 | */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1584 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1585 | struct nand_chip *chip, uint8_t *buf, |
| 1586 | int oob_required, int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1587 | { |
| 1588 | int eccsize = chip->ecc.size; |
| 1589 | int eccbytes = chip->ecc.bytes; |
| 1590 | uint8_t *oob = chip->oob_poi; |
| 1591 | int steps, size; |
| 1592 | |
| 1593 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
| 1594 | chip->read_buf(mtd, buf, eccsize); |
| 1595 | buf += eccsize; |
| 1596 | |
| 1597 | if (chip->ecc.prepad) { |
| 1598 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 1599 | oob += chip->ecc.prepad; |
| 1600 | } |
| 1601 | |
| 1602 | chip->read_buf(mtd, oob, eccbytes); |
| 1603 | oob += eccbytes; |
| 1604 | |
| 1605 | if (chip->ecc.postpad) { |
| 1606 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 1607 | oob += chip->ecc.postpad; |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 1612 | if (size) |
| 1613 | chip->read_buf(mtd, oob, size); |
| 1614 | |
| 1615 | return 0; |
| 1616 | } |
| 1617 | |
| 1618 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1619 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1620 | * @mtd: mtd info structure |
| 1621 | * @chip: nand chip info structure |
| 1622 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1623 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1624 | * @page: page number to read |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1625 | */ |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1626 | 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] | 1627 | uint8_t *buf, int oob_required, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1629 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1630 | int eccbytes = chip->ecc.bytes; |
| 1631 | int eccsteps = chip->ecc.steps; |
| 1632 | uint8_t *p = buf; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1633 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1634 | uint8_t *ecc_code = chip->buffers->ecccode; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1635 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1636 | |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1637 | chip->ecc.read_page_raw(mtd, chip, buf, 1, page); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1638 | |
| 1639 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 1640 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1641 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1642 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 1643 | chip->ecc.total); |
| 1644 | if (ret) |
| 1645 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1646 | |
| 1647 | eccsteps = chip->ecc.steps; |
| 1648 | p = buf; |
| 1649 | |
| 1650 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1651 | int stat; |
| 1652 | |
| 1653 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1654 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1655 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1656 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1657 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1658 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1659 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1660 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1661 | return max_bitflips; |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 1662 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | /** |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 1665 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1666 | * @mtd: mtd info structure |
| 1667 | * @chip: nand chip info structure |
| 1668 | * @data_offs: offset of requested data within the page |
| 1669 | * @readlen: data length |
| 1670 | * @bufpoi: buffer to store read data |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 1671 | * @page: page number to read |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1672 | */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1673 | 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] | 1674 | uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi, |
| 1675 | int page) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1676 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1677 | int start_step, end_step, num_steps, ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1678 | uint8_t *p; |
| 1679 | int data_col_addr, i, gaps = 0; |
| 1680 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 1681 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1682 | int index, section = 0; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1683 | unsigned int max_bitflips = 0; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1684 | struct mtd_oob_region oobregion = { }; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1685 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1686 | /* Column address within the page aligned to ECC size (256bytes) */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1687 | start_step = data_offs / chip->ecc.size; |
| 1688 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 1689 | num_steps = end_step - start_step + 1; |
Ron | 4a4163ca | 2014-03-16 04:01:07 +1030 | [diff] [blame] | 1690 | index = start_step * chip->ecc.bytes; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1691 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1692 | /* Data size aligned to ECC ecc.size */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1693 | datafrag_len = num_steps * chip->ecc.size; |
| 1694 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 1695 | |
| 1696 | data_col_addr = start_step * chip->ecc.size; |
| 1697 | /* If we read not a page aligned data */ |
| 1698 | if (data_col_addr != 0) |
| 1699 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1); |
| 1700 | |
| 1701 | p = bufpoi + data_col_addr; |
| 1702 | chip->read_buf(mtd, p, datafrag_len); |
| 1703 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1704 | /* Calculate ECC */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1705 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
| 1706 | chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); |
| 1707 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1708 | /* |
| 1709 | * The performance is faster if we position offsets according to |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1710 | * 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] | 1711 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1712 | ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion); |
| 1713 | if (ret) |
| 1714 | return ret; |
| 1715 | |
| 1716 | if (oobregion.length < eccfrag_len) |
| 1717 | gaps = 1; |
| 1718 | |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1719 | if (gaps) { |
| 1720 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); |
| 1721 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1722 | } else { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1723 | /* |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1724 | * Send the command to read the particular ECC bytes take care |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1725 | * about buswidth alignment in read_buf. |
| 1726 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1727 | aligned_pos = oobregion.offset & ~(busw - 1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1728 | aligned_len = eccfrag_len; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1729 | if (oobregion.offset & (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1730 | aligned_len++; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1731 | if ((oobregion.offset + (num_steps * chip->ecc.bytes)) & |
| 1732 | (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1733 | aligned_len++; |
| 1734 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1735 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1736 | mtd->writesize + aligned_pos, -1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1737 | chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len); |
| 1738 | } |
| 1739 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1740 | ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode, |
| 1741 | chip->oob_poi, index, eccfrag_len); |
| 1742 | if (ret) |
| 1743 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1744 | |
| 1745 | p = bufpoi + data_col_addr; |
| 1746 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 1747 | int stat; |
| 1748 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1749 | stat = chip->ecc.correct(mtd, p, |
| 1750 | &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1751 | if (stat == -EBADMSG && |
| 1752 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1753 | /* check for empty pages with bitflips */ |
| 1754 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 1755 | &chip->buffers->ecccode[i], |
| 1756 | chip->ecc.bytes, |
| 1757 | NULL, 0, |
| 1758 | chip->ecc.strength); |
| 1759 | } |
| 1760 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1761 | if (stat < 0) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1762 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1763 | } else { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1764 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1765 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1766 | } |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1767 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1768 | return max_bitflips; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1772 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1773 | * @mtd: mtd info structure |
| 1774 | * @chip: nand chip info structure |
| 1775 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1776 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1777 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1778 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1779 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1780 | */ |
| 1781 | 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] | 1782 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1783 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1784 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1785 | int eccbytes = chip->ecc.bytes; |
| 1786 | int eccsteps = chip->ecc.steps; |
| 1787 | uint8_t *p = buf; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1788 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1789 | uint8_t *ecc_code = chip->buffers->ecccode; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1790 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1791 | |
| 1792 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1793 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1794 | chip->read_buf(mtd, p, eccsize); |
| 1795 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1796 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1797 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1798 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1799 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 1800 | chip->ecc.total); |
| 1801 | if (ret) |
| 1802 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1803 | |
| 1804 | eccsteps = chip->ecc.steps; |
| 1805 | p = buf; |
| 1806 | |
| 1807 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1808 | int stat; |
| 1809 | |
| 1810 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1811 | if (stat == -EBADMSG && |
| 1812 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1813 | /* check for empty pages with bitflips */ |
| 1814 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 1815 | &ecc_code[i], eccbytes, |
| 1816 | NULL, 0, |
| 1817 | chip->ecc.strength); |
| 1818 | } |
| 1819 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1820 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1821 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1822 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1823 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1824 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1825 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1826 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1827 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1831 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1832 | * @mtd: mtd info structure |
| 1833 | * @chip: nand chip info structure |
| 1834 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1835 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1836 | * @page: page number to read |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1837 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1838 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 1839 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 1840 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 1841 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 1842 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1843 | */ |
| 1844 | static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1845 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1846 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1847 | int i, eccsize = chip->ecc.size, ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1848 | int eccbytes = chip->ecc.bytes; |
| 1849 | int eccsteps = chip->ecc.steps; |
| 1850 | uint8_t *p = buf; |
| 1851 | uint8_t *ecc_code = chip->buffers->ecccode; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1852 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1853 | unsigned int max_bitflips = 0; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1854 | |
| 1855 | /* Read the OOB area first */ |
| 1856 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
| 1857 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1858 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 1859 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1860 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 1861 | chip->ecc.total); |
| 1862 | if (ret) |
| 1863 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1864 | |
| 1865 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1866 | int stat; |
| 1867 | |
| 1868 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1869 | chip->read_buf(mtd, p, eccsize); |
| 1870 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1871 | |
| 1872 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1873 | if (stat == -EBADMSG && |
| 1874 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1875 | /* check for empty pages with bitflips */ |
| 1876 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 1877 | &ecc_code[i], eccbytes, |
| 1878 | NULL, 0, |
| 1879 | chip->ecc.strength); |
| 1880 | } |
| 1881 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1882 | if (stat < 0) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1883 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1884 | } else { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1885 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1886 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1887 | } |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1888 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1889 | return max_bitflips; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1893 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1894 | * @mtd: mtd info structure |
| 1895 | * @chip: nand chip info structure |
| 1896 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1897 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1898 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1899 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1900 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 1901 | * need a special oob layout and handling. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1902 | */ |
| 1903 | 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] | 1904 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1905 | { |
| 1906 | int i, eccsize = chip->ecc.size; |
| 1907 | int eccbytes = chip->ecc.bytes; |
| 1908 | int eccsteps = chip->ecc.steps; |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1909 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1910 | uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1911 | uint8_t *oob = chip->oob_poi; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1912 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1913 | |
| 1914 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1915 | int stat; |
| 1916 | |
| 1917 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1918 | chip->read_buf(mtd, p, eccsize); |
| 1919 | |
| 1920 | if (chip->ecc.prepad) { |
| 1921 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 1922 | oob += chip->ecc.prepad; |
| 1923 | } |
| 1924 | |
| 1925 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
| 1926 | chip->read_buf(mtd, oob, eccbytes); |
| 1927 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
| 1928 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1929 | oob += eccbytes; |
| 1930 | |
| 1931 | if (chip->ecc.postpad) { |
| 1932 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 1933 | oob += chip->ecc.postpad; |
| 1934 | } |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1935 | |
| 1936 | if (stat == -EBADMSG && |
| 1937 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1938 | /* check for empty pages with bitflips */ |
| 1939 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 1940 | oob - eccpadbytes, |
| 1941 | eccpadbytes, |
| 1942 | NULL, 0, |
| 1943 | chip->ecc.strength); |
| 1944 | } |
| 1945 | |
| 1946 | if (stat < 0) { |
| 1947 | mtd->ecc_stats.failed++; |
| 1948 | } else { |
| 1949 | mtd->ecc_stats.corrected += stat; |
| 1950 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1951 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 1955 | i = mtd->oobsize - (oob - chip->oob_poi); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1956 | if (i) |
| 1957 | chip->read_buf(mtd, oob, i); |
| 1958 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1959 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1963 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1964 | * @mtd: mtd info structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1965 | * @oob: oob destination address |
| 1966 | * @ops: oob ops structure |
| 1967 | * @len: size of oob to transfer |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1968 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1969 | 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] | 1970 | struct mtd_oob_ops *ops, size_t len) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1971 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1972 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1973 | int ret; |
| 1974 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1975 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1976 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 1977 | case MTD_OPS_PLACE_OOB: |
| 1978 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1979 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 1980 | return oob + len; |
| 1981 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1982 | case MTD_OPS_AUTO_OOB: |
| 1983 | ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi, |
| 1984 | ops->ooboffs, len); |
| 1985 | BUG_ON(ret); |
| 1986 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1987 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1988 | default: |
| 1989 | BUG(); |
| 1990 | } |
| 1991 | return NULL; |
| 1992 | } |
| 1993 | |
| 1994 | /** |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1995 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
| 1996 | * @mtd: MTD device structure |
| 1997 | * @retry_mode: the retry mode to use |
| 1998 | * |
| 1999 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 2000 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 2001 | * a new threshold, the host should retry reading the page. |
| 2002 | */ |
| 2003 | static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode) |
| 2004 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2005 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2006 | |
| 2007 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 2008 | |
| 2009 | if (retry_mode >= chip->read_retries) |
| 2010 | return -EINVAL; |
| 2011 | |
| 2012 | if (!chip->setup_read_retry) |
| 2013 | return -EOPNOTSUPP; |
| 2014 | |
| 2015 | return chip->setup_read_retry(mtd, retry_mode); |
| 2016 | } |
| 2017 | |
| 2018 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2019 | * nand_do_read_ops - [INTERN] Read data with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2020 | * @mtd: MTD device structure |
| 2021 | * @from: offset to read from |
| 2022 | * @ops: oob ops structure |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 2023 | * |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2024 | * Internal function. Called with chip held. |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 2025 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2026 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 2027 | struct mtd_oob_ops *ops) |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 2028 | { |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 2029 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2030 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2031 | int ret = 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2032 | uint32_t readlen = ops->len; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2033 | uint32_t oobreadlen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 2034 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 9aca334 | 2010-02-22 20:39:35 +0200 | [diff] [blame] | 2035 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2036 | uint8_t *bufpoi, *oob, *buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2037 | int use_bufpoi; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2038 | unsigned int max_bitflips = 0; |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2039 | int retry_mode = 0; |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 2040 | bool ecc_fail = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2042 | chipnr = (int)(from >> chip->chip_shift); |
| 2043 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2045 | realpage = (int)(from >> chip->page_shift); |
| 2046 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2048 | col = (int)(from & (mtd->writesize - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2050 | buf = ops->datbuf; |
| 2051 | oob = ops->oobbuf; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 2052 | oob_required = oob ? 1 : 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2053 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2054 | while (1) { |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 2055 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
| 2056 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2057 | bytes = min(mtd->writesize - col, readlen); |
| 2058 | aligned = (bytes == mtd->writesize); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2059 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2060 | if (!aligned) |
| 2061 | use_bufpoi = 1; |
| 2062 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 2063 | use_bufpoi = !virt_addr_valid(buf) || |
| 2064 | !IS_ALIGNED((unsigned long)buf, |
| 2065 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2066 | else |
| 2067 | use_bufpoi = 0; |
| 2068 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2069 | /* Is the current page in the buffer? */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2070 | if (realpage != chip->pagebuf || oob) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2071 | bufpoi = use_bufpoi ? chip->buffers->databuf : buf; |
| 2072 | |
| 2073 | if (use_bufpoi && aligned) |
| 2074 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 2075 | __func__, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2077 | read_retry: |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 2078 | if (nand_standard_page_accessors(&chip->ecc)) |
| 2079 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2081 | /* |
| 2082 | * Now read the page into the buffer. Absent an error, |
| 2083 | * the read methods return max bitflips per ecc step. |
| 2084 | */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2085 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2086 | ret = chip->ecc.read_page_raw(mtd, chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 2087 | oob_required, |
| 2088 | page); |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 2089 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
| 2090 | !oob) |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 2091 | ret = chip->ecc.read_subpage(mtd, chip, |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 2092 | col, bytes, bufpoi, |
| 2093 | page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 2094 | else |
Sneha Narnakaje | 46a8cf2 | 2009-09-18 12:51:46 -0700 | [diff] [blame] | 2095 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 2096 | oob_required, page); |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 2097 | if (ret < 0) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2098 | if (use_bufpoi) |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 2099 | /* Invalidate page cache */ |
| 2100 | chip->pagebuf = -1; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2101 | break; |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 2102 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2103 | |
| 2104 | /* Transfer not aligned data */ |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2105 | if (use_bufpoi) { |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 2106 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 2107 | !(mtd->ecc_stats.failed - ecc_failures) && |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2108 | (ops->mode != MTD_OPS_RAW)) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2109 | chip->pagebuf = realpage; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2110 | chip->pagebuf_bitflips = ret; |
| 2111 | } else { |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 2112 | /* Invalidate page cache */ |
| 2113 | chip->pagebuf = -1; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2114 | } |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2115 | memcpy(buf, chip->buffers->databuf + col, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2117 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2118 | if (unlikely(oob)) { |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 2119 | int toread = min(oobreadlen, max_oobsize); |
| 2120 | |
| 2121 | if (toread) { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2122 | oob = nand_transfer_oob(mtd, |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 2123 | oob, ops, toread); |
| 2124 | oobreadlen -= toread; |
| 2125 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2126 | } |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 2127 | |
| 2128 | if (chip->options & NAND_NEED_READRDY) { |
| 2129 | /* Apply delay or wait for ready/busy pin */ |
| 2130 | if (!chip->dev_ready) |
| 2131 | udelay(chip->chip_delay); |
| 2132 | else |
| 2133 | nand_wait_ready(mtd); |
| 2134 | } |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 2135 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2136 | if (mtd->ecc_stats.failed - ecc_failures) { |
Brian Norris | 28fa65e | 2014-02-12 16:08:28 -0800 | [diff] [blame] | 2137 | if (retry_mode + 1 < chip->read_retries) { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2138 | retry_mode++; |
| 2139 | ret = nand_setup_read_retry(mtd, |
| 2140 | retry_mode); |
| 2141 | if (ret < 0) |
| 2142 | break; |
| 2143 | |
| 2144 | /* Reset failures; retry */ |
| 2145 | mtd->ecc_stats.failed = ecc_failures; |
| 2146 | goto read_retry; |
| 2147 | } else { |
| 2148 | /* No more retry modes; real failure */ |
| 2149 | ecc_fail = true; |
| 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | buf += bytes; |
Masahiro Yamada | 0760468 | 2017-03-30 15:45:47 +0900 | [diff] [blame] | 2154 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2155 | } else { |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2156 | memcpy(buf, chip->buffers->databuf + col, bytes); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2157 | buf += bytes; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2158 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 2159 | chip->pagebuf_bitflips); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2162 | readlen -= bytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2163 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2164 | /* Reset to retry mode 0 */ |
| 2165 | if (retry_mode) { |
| 2166 | ret = nand_setup_read_retry(mtd, 0); |
| 2167 | if (ret < 0) |
| 2168 | break; |
| 2169 | retry_mode = 0; |
| 2170 | } |
| 2171 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2172 | if (!readlen) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2173 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2175 | /* For subsequent reads align to page boundary */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | col = 0; |
| 2177 | /* Increment page address */ |
| 2178 | realpage++; |
| 2179 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2180 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | /* Check, if we cross a chip boundary */ |
| 2182 | if (!page) { |
| 2183 | chipnr++; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2184 | chip->select_chip(mtd, -1); |
| 2185 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | } |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2188 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2190 | ops->retlen = ops->len - (size_t) readlen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2191 | if (oob) |
| 2192 | ops->oobretlen = ops->ooblen - oobreadlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2194 | if (ret < 0) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2195 | return ret; |
| 2196 | |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 2197 | if (ecc_fail) |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 2198 | return -EBADMSG; |
| 2199 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2200 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2204 | * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2205 | * @mtd: MTD device structure |
| 2206 | * @from: offset to read from |
| 2207 | * @len: number of bytes to read |
| 2208 | * @retlen: pointer to variable to store the number of read bytes |
| 2209 | * @buf: the databuffer to put data |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2210 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2211 | * Get hold of the chip and call nand_do_read. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2212 | */ |
| 2213 | static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 2214 | size_t *retlen, uint8_t *buf) |
| 2215 | { |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2216 | struct mtd_oob_ops ops; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2217 | int ret; |
| 2218 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 2219 | nand_get_device(mtd, FL_READING); |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 2220 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2221 | ops.len = len; |
| 2222 | ops.datbuf = buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 2223 | ops.mode = MTD_OPS_PLACE_OOB; |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2224 | ret = nand_do_read_ops(mtd, from, &ops); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2225 | *retlen = ops.retlen; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2226 | nand_release_device(mtd); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2227 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2231 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2232 | * @mtd: mtd info structure |
| 2233 | * @chip: nand chip info structure |
| 2234 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2235 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2236 | 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] | 2237 | { |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 2238 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2239 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 2240 | return 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2241 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2242 | EXPORT_SYMBOL(nand_read_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2243 | |
| 2244 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2245 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2246 | * with syndromes |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2247 | * @mtd: mtd info structure |
| 2248 | * @chip: nand chip info structure |
| 2249 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2250 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2251 | int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 2252 | int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2253 | { |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2254 | int length = mtd->oobsize; |
| 2255 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2256 | int eccsize = chip->ecc.size; |
Baruch Siach | 2ea69d2 | 2015-01-22 15:23:05 +0200 | [diff] [blame] | 2257 | uint8_t *bufpoi = chip->oob_poi; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2258 | int i, toread, sndrnd = 0, pos; |
| 2259 | |
| 2260 | chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page); |
| 2261 | for (i = 0; i < chip->ecc.steps; i++) { |
| 2262 | if (sndrnd) { |
| 2263 | pos = eccsize + i * (eccsize + chunk); |
| 2264 | if (mtd->writesize > 512) |
| 2265 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1); |
| 2266 | else |
| 2267 | chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page); |
| 2268 | } else |
| 2269 | sndrnd = 1; |
| 2270 | toread = min_t(int, length, chunk); |
| 2271 | chip->read_buf(mtd, bufpoi, toread); |
| 2272 | bufpoi += toread; |
| 2273 | length -= toread; |
| 2274 | } |
| 2275 | if (length > 0) |
| 2276 | chip->read_buf(mtd, bufpoi, length); |
| 2277 | |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 2278 | return 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2279 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2280 | EXPORT_SYMBOL(nand_read_oob_syndrome); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2281 | |
| 2282 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2283 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2284 | * @mtd: mtd info structure |
| 2285 | * @chip: nand chip info structure |
| 2286 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2287 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2288 | 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] | 2289 | { |
| 2290 | int status = 0; |
| 2291 | const uint8_t *buf = chip->oob_poi; |
| 2292 | int length = mtd->oobsize; |
| 2293 | |
| 2294 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page); |
| 2295 | chip->write_buf(mtd, buf, length); |
| 2296 | /* Send command to program the OOB data */ |
| 2297 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 2298 | |
| 2299 | status = chip->waitfunc(mtd, chip); |
| 2300 | |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 2301 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2302 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2303 | EXPORT_SYMBOL(nand_write_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2304 | |
| 2305 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2306 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2307 | * with syndrome - only for large page flash |
| 2308 | * @mtd: mtd info structure |
| 2309 | * @chip: nand chip info structure |
| 2310 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2311 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2312 | int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 2313 | int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2314 | { |
| 2315 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2316 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
| 2317 | int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps; |
| 2318 | const uint8_t *bufpoi = chip->oob_poi; |
| 2319 | |
| 2320 | /* |
| 2321 | * data-ecc-data-ecc ... ecc-oob |
| 2322 | * or |
| 2323 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
| 2324 | */ |
| 2325 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 2326 | pos = steps * (eccsize + chunk); |
| 2327 | steps = 0; |
| 2328 | } else |
Vitaly Wool | 8b0036e | 2006-07-11 09:11:25 +0200 | [diff] [blame] | 2329 | pos = eccsize; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2330 | |
| 2331 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page); |
| 2332 | for (i = 0; i < steps; i++) { |
| 2333 | if (sndcmd) { |
| 2334 | if (mtd->writesize <= 512) { |
| 2335 | uint32_t fill = 0xFFFFFFFF; |
| 2336 | |
| 2337 | len = eccsize; |
| 2338 | while (len > 0) { |
| 2339 | int num = min_t(int, len, 4); |
| 2340 | chip->write_buf(mtd, (uint8_t *)&fill, |
| 2341 | num); |
| 2342 | len -= num; |
| 2343 | } |
| 2344 | } else { |
| 2345 | pos = eccsize + i * (eccsize + chunk); |
| 2346 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1); |
| 2347 | } |
| 2348 | } else |
| 2349 | sndcmd = 1; |
| 2350 | len = min_t(int, length, chunk); |
| 2351 | chip->write_buf(mtd, bufpoi, len); |
| 2352 | bufpoi += len; |
| 2353 | length -= len; |
| 2354 | } |
| 2355 | if (length > 0) |
| 2356 | chip->write_buf(mtd, bufpoi, length); |
| 2357 | |
| 2358 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 2359 | status = chip->waitfunc(mtd, chip); |
| 2360 | |
| 2361 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
| 2362 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2363 | EXPORT_SYMBOL(nand_write_oob_syndrome); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2364 | |
| 2365 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2366 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2367 | * @mtd: MTD device structure |
| 2368 | * @from: offset to read from |
| 2369 | * @ops: oob operations description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2371 | * NAND read out-of-band data from the spare area. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2373 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 2374 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | { |
Brian Norris | c00a099 | 2012-05-01 17:12:54 -0700 | [diff] [blame] | 2376 | int page, realpage, chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2377 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 2378 | struct mtd_ecc_stats stats; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2379 | int readlen = ops->ooblen; |
| 2380 | int len; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2381 | uint8_t *buf = ops->oobbuf; |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2382 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2384 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2385 | __func__, (unsigned long long)from, readlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 2387 | stats = mtd->ecc_stats; |
| 2388 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 2389 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2390 | |
| 2391 | if (unlikely(ops->ooboffs >= len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2392 | pr_debug("%s: attempt to start read outside oob\n", |
| 2393 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2394 | return -EINVAL; |
| 2395 | } |
| 2396 | |
| 2397 | /* Do not allow reads past end of device */ |
| 2398 | if (unlikely(from >= mtd->size || |
| 2399 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - |
| 2400 | (from >> chip->page_shift)) * len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2401 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2402 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2403 | return -EINVAL; |
| 2404 | } |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2405 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2406 | chipnr = (int)(from >> chip->chip_shift); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2407 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2409 | /* Shift to get page */ |
| 2410 | realpage = (int)(from >> chip->page_shift); |
| 2411 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2413 | while (1) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2414 | if (ops->mode == MTD_OPS_RAW) |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2415 | ret = chip->ecc.read_oob_raw(mtd, chip, page); |
Brian Norris | c46f648 | 2011-08-30 18:45:38 -0700 | [diff] [blame] | 2416 | else |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2417 | ret = chip->ecc.read_oob(mtd, chip, page); |
| 2418 | |
| 2419 | if (ret < 0) |
| 2420 | break; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2421 | |
| 2422 | len = min(len, readlen); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2423 | buf = nand_transfer_oob(mtd, buf, ops, len); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2424 | |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 2425 | if (chip->options & NAND_NEED_READRDY) { |
| 2426 | /* Apply delay or wait for ready/busy pin */ |
| 2427 | if (!chip->dev_ready) |
| 2428 | udelay(chip->chip_delay); |
| 2429 | else |
| 2430 | nand_wait_ready(mtd); |
| 2431 | } |
| 2432 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2433 | readlen -= len; |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 2434 | if (!readlen) |
| 2435 | break; |
| 2436 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2437 | /* Increment page address */ |
| 2438 | realpage++; |
| 2439 | |
| 2440 | page = realpage & chip->pagemask; |
| 2441 | /* Check, if we cross a chip boundary */ |
| 2442 | if (!page) { |
| 2443 | chipnr++; |
| 2444 | chip->select_chip(mtd, -1); |
| 2445 | chip->select_chip(mtd, chipnr); |
| 2446 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | } |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2448 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2450 | ops->oobretlen = ops->ooblen - readlen; |
| 2451 | |
| 2452 | if (ret < 0) |
| 2453 | return ret; |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 2454 | |
| 2455 | if (mtd->ecc_stats.failed - stats.failed) |
| 2456 | return -EBADMSG; |
| 2457 | |
| 2458 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2462 | * 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] | 2463 | * @mtd: MTD device structure |
| 2464 | * @from: offset to read from |
| 2465 | * @ops: oob operation description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2467 | * NAND read data and/or out-of-band data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2469 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 2470 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | { |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 2472 | int ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2473 | |
| 2474 | ops->retlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | |
| 2476 | /* Do not allow reads past end of device */ |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2477 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2478 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2479 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | return -EINVAL; |
| 2481 | } |
| 2482 | |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 2483 | if (ops->mode != MTD_OPS_PLACE_OOB && |
| 2484 | ops->mode != MTD_OPS_AUTO_OOB && |
| 2485 | ops->mode != MTD_OPS_RAW) |
| 2486 | return -ENOTSUPP; |
| 2487 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 2488 | nand_get_device(mtd, FL_READING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2490 | if (!ops->datbuf) |
| 2491 | ret = nand_do_read_oob(mtd, from, ops); |
| 2492 | else |
| 2493 | ret = nand_do_read_ops(mtd, from, ops); |
| 2494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | nand_release_device(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2496 | return ret; |
| 2497 | } |
| 2498 | |
| 2499 | |
| 2500 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2501 | * nand_write_page_raw - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2502 | * @mtd: mtd info structure |
| 2503 | * @chip: nand chip info structure |
| 2504 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2505 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2506 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2507 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2508 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2509 | */ |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 2510 | int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 2511 | const uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2512 | { |
| 2513 | chip->write_buf(mtd, buf, mtd->writesize); |
Brian Norris | 279f08d | 2012-05-02 10:15:03 -0700 | [diff] [blame] | 2514 | if (oob_required) |
| 2515 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2516 | |
| 2517 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 2519 | EXPORT_SYMBOL(nand_write_page_raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2521 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2522 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2523 | * @mtd: mtd info structure |
| 2524 | * @chip: nand chip info structure |
| 2525 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2526 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2527 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2528 | * |
| 2529 | * We need a special oob layout and handling even when ECC isn't checked. |
| 2530 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2531 | static int nand_write_page_raw_syndrome(struct mtd_info *mtd, |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 2532 | struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2533 | const uint8_t *buf, int oob_required, |
| 2534 | int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2535 | { |
| 2536 | int eccsize = chip->ecc.size; |
| 2537 | int eccbytes = chip->ecc.bytes; |
| 2538 | uint8_t *oob = chip->oob_poi; |
| 2539 | int steps, size; |
| 2540 | |
| 2541 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
| 2542 | chip->write_buf(mtd, buf, eccsize); |
| 2543 | buf += eccsize; |
| 2544 | |
| 2545 | if (chip->ecc.prepad) { |
| 2546 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 2547 | oob += chip->ecc.prepad; |
| 2548 | } |
| 2549 | |
Boris BREZILLON | 60c3bc1 | 2014-02-01 19:10:28 +0100 | [diff] [blame] | 2550 | chip->write_buf(mtd, oob, eccbytes); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2551 | oob += eccbytes; |
| 2552 | |
| 2553 | if (chip->ecc.postpad) { |
| 2554 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 2555 | oob += chip->ecc.postpad; |
| 2556 | } |
| 2557 | } |
| 2558 | |
| 2559 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 2560 | if (size) |
| 2561 | chip->write_buf(mtd, oob, size); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2562 | |
| 2563 | return 0; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2564 | } |
| 2565 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2566 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2567 | * @mtd: mtd info structure |
| 2568 | * @chip: nand chip info structure |
| 2569 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2570 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2571 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2572 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2573 | 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] | 2574 | const uint8_t *buf, int oob_required, |
| 2575 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2576 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2577 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2578 | int eccbytes = chip->ecc.bytes; |
| 2579 | int eccsteps = chip->ecc.steps; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2580 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2581 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2582 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2583 | /* Software ECC calculation */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2584 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 2585 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2586 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2587 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 2588 | chip->ecc.total); |
| 2589 | if (ret) |
| 2590 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2591 | |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2592 | return chip->ecc.write_page_raw(mtd, chip, buf, 1, page); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2596 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2597 | * @mtd: mtd info structure |
| 2598 | * @chip: nand chip info structure |
| 2599 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2600 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2601 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2602 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2603 | 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] | 2604 | const uint8_t *buf, int oob_required, |
| 2605 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2606 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2607 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2608 | int eccbytes = chip->ecc.bytes; |
| 2609 | int eccsteps = chip->ecc.steps; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2610 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2611 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2612 | |
| 2613 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2614 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
David Woodhouse | 29da9ce | 2006-05-26 23:05:44 +0100 | [diff] [blame] | 2615 | chip->write_buf(mtd, p, eccsize); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2616 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2617 | } |
| 2618 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2619 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 2620 | chip->ecc.total); |
| 2621 | if (ret) |
| 2622 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2623 | |
| 2624 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2625 | |
| 2626 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2627 | } |
| 2628 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2629 | |
| 2630 | /** |
Brian Norris | 73c8aaf | 2015-02-28 02:04:18 -0800 | [diff] [blame] | 2631 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2632 | * @mtd: mtd info structure |
| 2633 | * @chip: nand chip info structure |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2634 | * @offset: column address of subpage within the page |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2635 | * @data_len: data length |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2636 | * @buf: data buffer |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2637 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2638 | * @page: page number to write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2639 | */ |
| 2640 | static int nand_write_subpage_hwecc(struct mtd_info *mtd, |
| 2641 | struct nand_chip *chip, uint32_t offset, |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2642 | uint32_t data_len, const uint8_t *buf, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2643 | int oob_required, int page) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2644 | { |
| 2645 | uint8_t *oob_buf = chip->oob_poi; |
| 2646 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2647 | int ecc_size = chip->ecc.size; |
| 2648 | int ecc_bytes = chip->ecc.bytes; |
| 2649 | int ecc_steps = chip->ecc.steps; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2650 | uint32_t start_step = offset / ecc_size; |
| 2651 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 2652 | int oob_bytes = mtd->oobsize / ecc_steps; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2653 | int step, ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2654 | |
| 2655 | for (step = 0; step < ecc_steps; step++) { |
| 2656 | /* configure controller for WRITE access */ |
| 2657 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2658 | |
| 2659 | /* write data (untouched subpages already masked by 0xFF) */ |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2660 | chip->write_buf(mtd, buf, ecc_size); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2661 | |
| 2662 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 2663 | if ((step < start_step) || (step > end_step)) |
| 2664 | memset(ecc_calc, 0xff, ecc_bytes); |
| 2665 | else |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2666 | chip->ecc.calculate(mtd, buf, ecc_calc); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2667 | |
| 2668 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 2669 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 2670 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 2671 | memset(oob_buf, 0xff, oob_bytes); |
| 2672 | |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2673 | buf += ecc_size; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2674 | ecc_calc += ecc_bytes; |
| 2675 | oob_buf += oob_bytes; |
| 2676 | } |
| 2677 | |
| 2678 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 2679 | /* this include masked-value(0xFF) for unwritten subpages */ |
| 2680 | ecc_calc = chip->buffers->ecccalc; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2681 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 2682 | chip->ecc.total); |
| 2683 | if (ret) |
| 2684 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2685 | |
| 2686 | /* write OOB buffer to NAND device */ |
| 2687 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 2688 | |
| 2689 | return 0; |
| 2690 | } |
| 2691 | |
| 2692 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2693 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2694 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2695 | * @mtd: mtd info structure |
| 2696 | * @chip: nand chip info structure |
| 2697 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2698 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2699 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2700 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2701 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 2702 | * need a special oob layout and handling. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2703 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2704 | static int nand_write_page_syndrome(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2705 | struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2706 | const uint8_t *buf, int oob_required, |
| 2707 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2708 | { |
| 2709 | int i, eccsize = chip->ecc.size; |
| 2710 | int eccbytes = chip->ecc.bytes; |
| 2711 | int eccsteps = chip->ecc.steps; |
| 2712 | const uint8_t *p = buf; |
| 2713 | uint8_t *oob = chip->oob_poi; |
| 2714 | |
| 2715 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2716 | |
| 2717 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2718 | chip->write_buf(mtd, p, eccsize); |
| 2719 | |
| 2720 | if (chip->ecc.prepad) { |
| 2721 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 2722 | oob += chip->ecc.prepad; |
| 2723 | } |
| 2724 | |
| 2725 | chip->ecc.calculate(mtd, p, oob); |
| 2726 | chip->write_buf(mtd, oob, eccbytes); |
| 2727 | oob += eccbytes; |
| 2728 | |
| 2729 | if (chip->ecc.postpad) { |
| 2730 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 2731 | oob += chip->ecc.postpad; |
| 2732 | } |
| 2733 | } |
| 2734 | |
| 2735 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 2736 | i = mtd->oobsize - (oob - chip->oob_poi); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2737 | if (i) |
| 2738 | chip->write_buf(mtd, oob, i); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2739 | |
| 2740 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | /** |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 2744 | * nand_write_page - write one page |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2745 | * @mtd: MTD device structure |
| 2746 | * @chip: NAND chip descriptor |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2747 | * @offset: address offset within the page |
| 2748 | * @data_len: length of actual data to be written |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2749 | * @buf: the data to write |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2750 | * @oob_required: must write chip->oob_poi to OOB |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2751 | * @page: page number to write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2752 | * @raw: use _raw version of write_page |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2753 | */ |
| 2754 | 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] | 2755 | uint32_t offset, int data_len, const uint8_t *buf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 2756 | int oob_required, int page, int raw) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2757 | { |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2758 | int status, subpage; |
| 2759 | |
| 2760 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 2761 | chip->ecc.write_subpage) |
| 2762 | subpage = offset || (data_len < mtd->writesize); |
| 2763 | else |
| 2764 | subpage = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2765 | |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 2766 | if (nand_standard_page_accessors(&chip->ecc)) |
| 2767 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2768 | |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 2769 | if (unlikely(raw)) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2770 | status = chip->ecc.write_page_raw(mtd, chip, buf, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2771 | oob_required, page); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2772 | else if (subpage) |
| 2773 | status = chip->ecc.write_subpage(mtd, chip, offset, data_len, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2774 | buf, oob_required, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 2775 | else |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2776 | status = chip->ecc.write_page(mtd, chip, buf, oob_required, |
| 2777 | page); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2778 | |
| 2779 | if (status < 0) |
| 2780 | return status; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2781 | |
Boris Brezillon | 4114564 | 2017-05-16 18:27:49 +0200 | [diff] [blame] | 2782 | if (nand_standard_page_accessors(&chip->ecc)) { |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 2783 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2784 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2785 | status = chip->waitfunc(mtd, chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2786 | if (status & NAND_STATUS_FAIL) |
| 2787 | return -EIO; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2788 | } |
| 2789 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2790 | return 0; |
| 2791 | } |
| 2792 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2793 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2794 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2795 | * @mtd: MTD device structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2796 | * @oob: oob data buffer |
| 2797 | * @len: oob data write length |
| 2798 | * @ops: oob ops structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2799 | */ |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2800 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
| 2801 | struct mtd_oob_ops *ops) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2802 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2803 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2804 | int ret; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2805 | |
| 2806 | /* |
| 2807 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 2808 | * data from a previous OOB read. |
| 2809 | */ |
| 2810 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 2811 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2812 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2813 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2814 | case MTD_OPS_PLACE_OOB: |
| 2815 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2816 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 2817 | return oob + len; |
| 2818 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2819 | case MTD_OPS_AUTO_OOB: |
| 2820 | ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi, |
| 2821 | ops->ooboffs, len); |
| 2822 | BUG_ON(ret); |
| 2823 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2824 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2825 | default: |
| 2826 | BUG(); |
| 2827 | } |
| 2828 | return NULL; |
| 2829 | } |
| 2830 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2831 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2832 | |
| 2833 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2834 | * nand_do_write_ops - [INTERN] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2835 | * @mtd: MTD device structure |
| 2836 | * @to: offset to write to |
| 2837 | * @ops: oob operations description structure |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2838 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2839 | * NAND write with ECC. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2840 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2841 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 2842 | struct mtd_oob_ops *ops) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2843 | { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2844 | int chipnr, realpage, page, blockmask, column; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2845 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2846 | uint32_t writelen = ops->len; |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2847 | |
| 2848 | uint32_t oobwritelen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 2849 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2850 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2851 | uint8_t *oob = ops->oobbuf; |
| 2852 | uint8_t *buf = ops->datbuf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2853 | int ret; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 2854 | int oob_required = oob ? 1 : 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2855 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2856 | ops->retlen = 0; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2857 | if (!writelen) |
| 2858 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2859 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2860 | /* Reject writes, which are not page aligned */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2861 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 2862 | pr_notice("%s: attempt to write non page aligned data\n", |
| 2863 | __func__); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2864 | return -EINVAL; |
| 2865 | } |
| 2866 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2867 | column = to & (mtd->writesize - 1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2868 | |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 2869 | chipnr = (int)(to >> chip->chip_shift); |
| 2870 | chip->select_chip(mtd, chipnr); |
| 2871 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2872 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2873 | if (nand_check_wp(mtd)) { |
| 2874 | ret = -EIO; |
| 2875 | goto err_out; |
| 2876 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2877 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2878 | realpage = (int)(to >> chip->page_shift); |
| 2879 | page = realpage & chip->pagemask; |
| 2880 | blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; |
| 2881 | |
| 2882 | /* Invalidate the page cache, when we write to the cached page */ |
Brian Norris | 537ab1b | 2014-07-21 19:08:03 -0700 | [diff] [blame] | 2883 | if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && |
| 2884 | ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2885 | chip->pagebuf = -1; |
| 2886 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2887 | /* Don't allow multipage oob writes with offset */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2888 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 2889 | ret = -EINVAL; |
| 2890 | goto err_out; |
| 2891 | } |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2892 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2893 | while (1) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2894 | int bytes = mtd->writesize; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2895 | uint8_t *wbuf = buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2896 | int use_bufpoi; |
Hector Palacios | 144f4c9 | 2016-07-18 10:39:18 +0200 | [diff] [blame] | 2897 | int part_pagewr = (column || writelen < mtd->writesize); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2898 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2899 | if (part_pagewr) |
| 2900 | use_bufpoi = 1; |
| 2901 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 2902 | use_bufpoi = !virt_addr_valid(buf) || |
| 2903 | !IS_ALIGNED((unsigned long)buf, |
| 2904 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2905 | else |
| 2906 | use_bufpoi = 0; |
| 2907 | |
| 2908 | /* Partial page write?, or need to use bounce buffer */ |
| 2909 | if (use_bufpoi) { |
| 2910 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 2911 | __func__, buf); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2912 | if (part_pagewr) |
| 2913 | bytes = min_t(int, bytes - column, writelen); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2914 | chip->pagebuf = -1; |
| 2915 | memset(chip->buffers->databuf, 0xff, mtd->writesize); |
| 2916 | memcpy(&chip->buffers->databuf[column], buf, bytes); |
| 2917 | wbuf = chip->buffers->databuf; |
| 2918 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2919 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2920 | if (unlikely(oob)) { |
| 2921 | size_t len = min(oobwritelen, oobmaxlen); |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2922 | oob = nand_fill_oob(mtd, oob, len, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2923 | oobwritelen -= len; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2924 | } else { |
| 2925 | /* We still need to erase leftover OOB data */ |
| 2926 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2927 | } |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 2928 | |
| 2929 | ret = nand_write_page(mtd, chip, column, bytes, wbuf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 2930 | oob_required, page, |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 2931 | (ops->mode == MTD_OPS_RAW)); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2932 | if (ret) |
| 2933 | break; |
| 2934 | |
| 2935 | writelen -= bytes; |
| 2936 | if (!writelen) |
| 2937 | break; |
| 2938 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2939 | column = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2940 | buf += bytes; |
| 2941 | realpage++; |
| 2942 | |
| 2943 | page = realpage & chip->pagemask; |
| 2944 | /* Check, if we cross a chip boundary */ |
| 2945 | if (!page) { |
| 2946 | chipnr++; |
| 2947 | chip->select_chip(mtd, -1); |
| 2948 | chip->select_chip(mtd, chipnr); |
| 2949 | } |
| 2950 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2951 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2952 | ops->retlen = ops->len - writelen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2953 | if (unlikely(oob)) |
| 2954 | ops->oobretlen = ops->ooblen; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2955 | |
| 2956 | err_out: |
| 2957 | chip->select_chip(mtd, -1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2958 | return ret; |
| 2959 | } |
| 2960 | |
| 2961 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2962 | * panic_nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2963 | * @mtd: MTD device structure |
| 2964 | * @to: offset to write to |
| 2965 | * @len: number of bytes to write |
| 2966 | * @retlen: pointer to variable to store the number of written bytes |
| 2967 | * @buf: the data to write |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2968 | * |
| 2969 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 2970 | * may for example be called by mtdoops when writing an oops while in panic. |
| 2971 | */ |
| 2972 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 2973 | size_t *retlen, const uint8_t *buf) |
| 2974 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2975 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2976 | struct mtd_oob_ops ops; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2977 | int ret; |
| 2978 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2979 | /* Wait for the device to get ready */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2980 | panic_nand_wait(mtd, chip, 400); |
| 2981 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2982 | /* Grab the device */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2983 | panic_nand_get_device(chip, mtd, FL_WRITING); |
| 2984 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 2985 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2986 | ops.len = len; |
| 2987 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 2988 | ops.mode = MTD_OPS_PLACE_OOB; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2989 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2990 | ret = nand_do_write_ops(mtd, to, &ops); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2991 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2992 | *retlen = ops.retlen; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2993 | return ret; |
| 2994 | } |
| 2995 | |
| 2996 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2997 | * nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2998 | * @mtd: MTD device structure |
| 2999 | * @to: offset to write to |
| 3000 | * @len: number of bytes to write |
| 3001 | * @retlen: pointer to variable to store the number of written bytes |
| 3002 | * @buf: the data to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3004 | * NAND write with ECC. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3006 | 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] | 3007 | size_t *retlen, const uint8_t *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | { |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 3009 | struct mtd_oob_ops ops; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3010 | int ret; |
| 3011 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3012 | nand_get_device(mtd, FL_WRITING); |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 3013 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 3014 | ops.len = len; |
| 3015 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 3016 | ops.mode = MTD_OPS_PLACE_OOB; |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 3017 | ret = nand_do_write_ops(mtd, to, &ops); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 3018 | *retlen = ops.retlen; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3019 | nand_release_device(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3020 | return ret; |
| 3021 | } |
| 3022 | |
| 3023 | /** |
| 3024 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3025 | * @mtd: MTD device structure |
| 3026 | * @to: offset to write to |
| 3027 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3028 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3029 | * NAND write out-of-band. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3030 | */ |
| 3031 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 3032 | struct mtd_oob_ops *ops) |
| 3033 | { |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3034 | int chipnr, page, status, len; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3035 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3037 | pr_debug("%s: to = 0x%08x, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 3038 | __func__, (unsigned int)to, (int)ops->ooblen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3040 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | /* Do not allow write past end of page */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3043 | if ((ops->ooboffs + ops->ooblen) > len) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3044 | pr_debug("%s: attempt to write past end of page\n", |
| 3045 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | return -EINVAL; |
| 3047 | } |
| 3048 | |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3049 | if (unlikely(ops->ooboffs >= len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3050 | pr_debug("%s: attempt to start write outside oob\n", |
| 3051 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3052 | return -EINVAL; |
| 3053 | } |
| 3054 | |
Jason Liu | 775adc3d4 | 2011-02-25 13:06:18 +0800 | [diff] [blame] | 3055 | /* Do not allow write past end of device */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3056 | if (unlikely(to >= mtd->size || |
| 3057 | ops->ooboffs + ops->ooblen > |
| 3058 | ((mtd->size >> chip->page_shift) - |
| 3059 | (to >> chip->page_shift)) * len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3060 | pr_debug("%s: attempt to write beyond end of device\n", |
| 3061 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3062 | return -EINVAL; |
| 3063 | } |
| 3064 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3065 | chipnr = (int)(to >> chip->chip_shift); |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3066 | |
| 3067 | /* |
| 3068 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 3069 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 3070 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 3071 | * it in the doc2000 driver in August 1999. dwmw2. |
| 3072 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 3073 | nand_reset(chip, chipnr); |
| 3074 | |
| 3075 | chip->select_chip(mtd, chipnr); |
| 3076 | |
| 3077 | /* Shift to get page */ |
| 3078 | page = (int)(to >> chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | |
| 3080 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3081 | if (nand_check_wp(mtd)) { |
| 3082 | chip->select_chip(mtd, -1); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3083 | return -EROFS; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3084 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | /* Invalidate the page cache, if we write to the cached page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3087 | if (page == chip->pagebuf) |
| 3088 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3090 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 3091 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3092 | if (ops->mode == MTD_OPS_RAW) |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 3093 | status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask); |
| 3094 | else |
| 3095 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3096 | |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3097 | chip->select_chip(mtd, -1); |
| 3098 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3099 | if (status) |
| 3100 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3102 | ops->oobretlen = ops->ooblen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3103 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3104 | return 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3107 | /** |
| 3108 | * 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] | 3109 | * @mtd: MTD device structure |
| 3110 | * @to: offset to write to |
| 3111 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3112 | */ |
| 3113 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 3114 | struct mtd_oob_ops *ops) |
| 3115 | { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3116 | int ret = -ENOTSUPP; |
| 3117 | |
| 3118 | ops->retlen = 0; |
| 3119 | |
| 3120 | /* Do not allow writes past end of device */ |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3121 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3122 | pr_debug("%s: attempt to write beyond end of device\n", |
| 3123 | __func__); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3124 | return -EINVAL; |
| 3125 | } |
| 3126 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3127 | nand_get_device(mtd, FL_WRITING); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3128 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3129 | switch (ops->mode) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3130 | case MTD_OPS_PLACE_OOB: |
| 3131 | case MTD_OPS_AUTO_OOB: |
| 3132 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3133 | break; |
| 3134 | |
| 3135 | default: |
| 3136 | goto out; |
| 3137 | } |
| 3138 | |
| 3139 | if (!ops->datbuf) |
| 3140 | ret = nand_do_write_oob(mtd, to, ops); |
| 3141 | else |
| 3142 | ret = nand_do_write_ops(mtd, to, ops); |
| 3143 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3144 | out: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3145 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | return ret; |
| 3147 | } |
| 3148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | /** |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3150 | * single_erase - [GENERIC] NAND standard block erase command function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3151 | * @mtd: MTD device structure |
| 3152 | * @page: the page address of the block which will be erased |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | * |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3154 | * Standard erase command for NAND chips. Returns NAND status. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | */ |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3156 | static int single_erase(struct mtd_info *mtd, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3158 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | /* Send commands to erase a block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3160 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 3161 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3162 | |
| 3163 | return chip->waitfunc(mtd, chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | * nand_erase - [MTD Interface] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3168 | * @mtd: MTD device structure |
| 3169 | * @instr: erase instruction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3170 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3171 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3173 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3175 | return nand_erase_nand(mtd, instr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3179 | * nand_erase_nand - [INTERN] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3180 | * @mtd: MTD device structure |
| 3181 | * @instr: erase instruction |
| 3182 | * @allowbbt: allow erasing the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3184 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3186 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
| 3187 | int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3188 | { |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3189 | int page, status, pages_per_block, ret, chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3190 | struct nand_chip *chip = mtd_to_nand(mtd); |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3191 | loff_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3192 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3193 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 3194 | __func__, (unsigned long long)instr->addr, |
| 3195 | (unsigned long long)instr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3196 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 3197 | if (check_offs_len(mtd, instr->addr, instr->len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3201 | nand_get_device(mtd, FL_ERASING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3202 | |
| 3203 | /* Shift to get first page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3204 | page = (int)(instr->addr >> chip->page_shift); |
| 3205 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | |
| 3207 | /* Calculate pages in each block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3208 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | |
| 3210 | /* Select the NAND device */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3211 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3213 | /* Check, if it is write protected */ |
| 3214 | if (nand_check_wp(mtd)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3215 | pr_debug("%s: device is write protected!\n", |
| 3216 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | instr->state = MTD_ERASE_FAILED; |
| 3218 | goto erase_exit; |
| 3219 | } |
| 3220 | |
| 3221 | /* Loop through the pages */ |
| 3222 | len = instr->len; |
| 3223 | |
| 3224 | instr->state = MTD_ERASING; |
| 3225 | |
| 3226 | while (len) { |
Wolfram Sang | 12183a2 | 2011-12-21 23:01:20 +0100 | [diff] [blame] | 3227 | /* 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] | 3228 | if (nand_block_checkbad(mtd, ((loff_t) page) << |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 3229 | chip->page_shift, allowbbt)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 3230 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
| 3231 | __func__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | instr->state = MTD_ERASE_FAILED; |
| 3233 | goto erase_exit; |
| 3234 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3235 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3236 | /* |
| 3237 | * Invalidate the page cache, if we erase the block which |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3238 | * contains the current cached page. |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3239 | */ |
| 3240 | if (page <= chip->pagebuf && chip->pagebuf < |
| 3241 | (page + pages_per_block)) |
| 3242 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3243 | |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3244 | status = chip->erase(mtd, page & chip->pagemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | |
| 3246 | /* See if block erase succeeded */ |
David A. Marlin | a4ab4c5 | 2005-01-23 18:30:53 +0000 | [diff] [blame] | 3247 | if (status & NAND_STATUS_FAIL) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3248 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 3249 | __func__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3250 | instr->state = MTD_ERASE_FAILED; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3251 | instr->fail_addr = |
| 3252 | ((loff_t)page << chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3253 | goto erase_exit; |
| 3254 | } |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 3255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3256 | /* Increment page address and decrement length */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 3257 | len -= (1ULL << chip->phys_erase_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3258 | page += pages_per_block; |
| 3259 | |
| 3260 | /* Check, if we cross a chip boundary */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3261 | if (len && !(page & chip->pagemask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | chipnr++; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3263 | chip->select_chip(mtd, -1); |
| 3264 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | } |
| 3266 | } |
| 3267 | instr->state = MTD_ERASE_DONE; |
| 3268 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3269 | erase_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | |
| 3271 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | |
| 3273 | /* Deselect and wake up anyone waiting on the device */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3274 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3275 | nand_release_device(mtd); |
| 3276 | |
David Woodhouse | 49defc0 | 2007-10-06 15:01:59 -0400 | [diff] [blame] | 3277 | /* Do call back function */ |
| 3278 | if (!ret) |
| 3279 | mtd_erase_callback(instr); |
| 3280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3281 | /* Return more or less happy */ |
| 3282 | return ret; |
| 3283 | } |
| 3284 | |
| 3285 | /** |
| 3286 | * nand_sync - [MTD Interface] sync |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3287 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3289 | * Sync is actually a wait for chip ready function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3290 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3291 | static void nand_sync(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3292 | { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3293 | pr_debug("%s: called\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | |
| 3295 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3296 | nand_get_device(mtd, FL_SYNCING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | /* Release it and go back */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3298 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | } |
| 3300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3301 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3302 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3303 | * @mtd: MTD device structure |
| 3304 | * @offs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3306 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3307 | { |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 3308 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3309 | int chipnr = (int)(offs >> chip->chip_shift); |
| 3310 | int ret; |
| 3311 | |
| 3312 | /* Select the NAND device */ |
| 3313 | nand_get_device(mtd, FL_READING); |
| 3314 | chip->select_chip(mtd, chipnr); |
| 3315 | |
| 3316 | ret = nand_block_checkbad(mtd, offs, 0); |
| 3317 | |
| 3318 | chip->select_chip(mtd, -1); |
| 3319 | nand_release_device(mtd); |
| 3320 | |
| 3321 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3325 | * 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] | 3326 | * @mtd: MTD device structure |
| 3327 | * @ofs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3328 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3329 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3331 | int ret; |
| 3332 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3333 | ret = nand_block_isbad(mtd, ofs); |
| 3334 | if (ret) { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3335 | /* If it was bad already, return success and do nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3336 | if (ret > 0) |
| 3337 | return 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3338 | return ret; |
| 3339 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 3341 | return nand_block_markbad_lowlevel(mtd, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | /** |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 3345 | * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd |
| 3346 | * @mtd: MTD device structure |
| 3347 | * @ofs: offset relative to mtd start |
| 3348 | * @len: length of mtd |
| 3349 | */ |
| 3350 | static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len) |
| 3351 | { |
| 3352 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3353 | u32 part_start_block; |
| 3354 | u32 part_end_block; |
| 3355 | u32 part_start_die; |
| 3356 | u32 part_end_die; |
| 3357 | |
| 3358 | /* |
| 3359 | * max_bb_per_die and blocks_per_die used to determine |
| 3360 | * the maximum bad block count. |
| 3361 | */ |
| 3362 | if (!chip->max_bb_per_die || !chip->blocks_per_die) |
| 3363 | return -ENOTSUPP; |
| 3364 | |
| 3365 | /* Get the start and end of the partition in erase blocks. */ |
| 3366 | part_start_block = mtd_div_by_eb(ofs, mtd); |
| 3367 | part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1; |
| 3368 | |
| 3369 | /* Get the start and end LUNs of the partition. */ |
| 3370 | part_start_die = part_start_block / chip->blocks_per_die; |
| 3371 | part_end_die = part_end_block / chip->blocks_per_die; |
| 3372 | |
| 3373 | /* |
| 3374 | * Look up the bad blocks per unit and multiply by the number of units |
| 3375 | * that the partition spans. |
| 3376 | */ |
| 3377 | return chip->max_bb_per_die * (part_end_die - part_start_die + 1); |
| 3378 | } |
| 3379 | |
| 3380 | /** |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3381 | * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand |
| 3382 | * @mtd: MTD device structure |
| 3383 | * @chip: nand chip info structure |
| 3384 | * @addr: feature address. |
| 3385 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3386 | */ |
| 3387 | static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3388 | int addr, uint8_t *subfeature_param) |
| 3389 | { |
| 3390 | int status; |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3391 | int i; |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3392 | |
David Mosberger | d914c93 | 2013-05-29 15:30:13 +0300 | [diff] [blame] | 3393 | if (!chip->onfi_version || |
| 3394 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3395 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3396 | return -EINVAL; |
| 3397 | |
| 3398 | chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3399 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 3400 | chip->write_byte(mtd, subfeature_param[i]); |
| 3401 | |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3402 | status = chip->waitfunc(mtd, chip); |
| 3403 | if (status & NAND_STATUS_FAIL) |
| 3404 | return -EIO; |
| 3405 | return 0; |
| 3406 | } |
| 3407 | |
| 3408 | /** |
| 3409 | * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand |
| 3410 | * @mtd: MTD device structure |
| 3411 | * @chip: nand chip info structure |
| 3412 | * @addr: feature address. |
| 3413 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3414 | */ |
| 3415 | static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3416 | int addr, uint8_t *subfeature_param) |
| 3417 | { |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3418 | int i; |
| 3419 | |
David Mosberger | d914c93 | 2013-05-29 15:30:13 +0300 | [diff] [blame] | 3420 | if (!chip->onfi_version || |
| 3421 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3422 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3423 | return -EINVAL; |
| 3424 | |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3425 | chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3426 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 3427 | *subfeature_param++ = chip->read_byte(mtd); |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3428 | return 0; |
| 3429 | } |
| 3430 | |
| 3431 | /** |
Boris Brezillon | 4a78cc6 | 2017-05-26 17:10:15 +0200 | [diff] [blame] | 3432 | * nand_onfi_get_set_features_notsupp - set/get features stub returning |
| 3433 | * -ENOTSUPP |
| 3434 | * @mtd: MTD device structure |
| 3435 | * @chip: nand chip info structure |
| 3436 | * @addr: feature address. |
| 3437 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3438 | * |
| 3439 | * Should be used by NAND controller drivers that do not support the SET/GET |
| 3440 | * FEATURES operations. |
| 3441 | */ |
| 3442 | int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd, |
| 3443 | struct nand_chip *chip, int addr, |
| 3444 | u8 *subfeature_param) |
| 3445 | { |
| 3446 | return -ENOTSUPP; |
| 3447 | } |
| 3448 | EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp); |
| 3449 | |
| 3450 | /** |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3451 | * nand_suspend - [MTD Interface] Suspend the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3452 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3453 | */ |
| 3454 | static int nand_suspend(struct mtd_info *mtd) |
| 3455 | { |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3456 | return nand_get_device(mtd, FL_PM_SUSPENDED); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3457 | } |
| 3458 | |
| 3459 | /** |
| 3460 | * nand_resume - [MTD Interface] Resume the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3461 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3462 | */ |
| 3463 | static void nand_resume(struct mtd_info *mtd) |
| 3464 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3465 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3466 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3467 | if (chip->state == FL_PM_SUSPENDED) |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3468 | nand_release_device(mtd); |
| 3469 | else |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 3470 | pr_err("%s called for a chip which is not in suspended state\n", |
| 3471 | __func__); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3472 | } |
| 3473 | |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 3474 | /** |
| 3475 | * nand_shutdown - [MTD Interface] Finish the current NAND operation and |
| 3476 | * prevent further operations |
| 3477 | * @mtd: MTD device structure |
| 3478 | */ |
| 3479 | static void nand_shutdown(struct mtd_info *mtd) |
| 3480 | { |
Brian Norris | 9ca641b | 2015-11-09 16:37:28 -0800 | [diff] [blame] | 3481 | nand_get_device(mtd, FL_PM_SUSPENDED); |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 3482 | } |
| 3483 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3484 | /* Set default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3485 | static void nand_set_defaults(struct nand_chip *chip) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3486 | { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3487 | unsigned int busw = chip->options & NAND_BUSWIDTH_16; |
| 3488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3489 | /* check for proper chip_delay setup, set 20us if not */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3490 | if (!chip->chip_delay) |
| 3491 | chip->chip_delay = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | |
| 3493 | /* check, if a user supplied command function given */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3494 | if (chip->cmdfunc == NULL) |
| 3495 | chip->cmdfunc = nand_command; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | |
| 3497 | /* check, if a user supplied wait function given */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3498 | if (chip->waitfunc == NULL) |
| 3499 | chip->waitfunc = nand_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3500 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3501 | if (!chip->select_chip) |
| 3502 | chip->select_chip = nand_select_chip; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3503 | |
Huang Shijie | 4204ccc | 2013-08-16 10:10:07 +0800 | [diff] [blame] | 3504 | /* set for ONFI nand */ |
| 3505 | if (!chip->onfi_set_features) |
| 3506 | chip->onfi_set_features = nand_onfi_set_features; |
| 3507 | if (!chip->onfi_get_features) |
| 3508 | chip->onfi_get_features = nand_onfi_get_features; |
| 3509 | |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3510 | /* If called twice, pointers that depend on busw may need to be reset */ |
| 3511 | if (!chip->read_byte || chip->read_byte == nand_read_byte) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3512 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
| 3513 | if (!chip->read_word) |
| 3514 | chip->read_word = nand_read_word; |
| 3515 | if (!chip->block_bad) |
| 3516 | chip->block_bad = nand_block_bad; |
| 3517 | if (!chip->block_markbad) |
| 3518 | chip->block_markbad = nand_default_block_markbad; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3519 | if (!chip->write_buf || chip->write_buf == nand_write_buf) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3520 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3521 | if (!chip->write_byte || chip->write_byte == nand_write_byte) |
| 3522 | chip->write_byte = busw ? nand_write_byte16 : nand_write_byte; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3523 | if (!chip->read_buf || chip->read_buf == nand_read_buf) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3524 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3525 | if (!chip->scan_bbt) |
| 3526 | chip->scan_bbt = nand_default_bbt; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3527 | |
| 3528 | if (!chip->controller) { |
| 3529 | chip->controller = &chip->hwcontrol; |
Marc Gonzalez | d45bc58 | 2016-07-27 11:23:52 +0200 | [diff] [blame] | 3530 | nand_hw_control_init(chip->controller); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3531 | } |
| 3532 | |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 3533 | if (!chip->buf_align) |
| 3534 | chip->buf_align = 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3535 | } |
| 3536 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3537 | /* Sanitize ONFI strings so we can safely print them */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3538 | static void sanitize_string(uint8_t *s, size_t len) |
| 3539 | { |
| 3540 | ssize_t i; |
| 3541 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3542 | /* Null terminate */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3543 | s[len - 1] = 0; |
| 3544 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3545 | /* Remove non printable chars */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3546 | for (i = 0; i < len - 1; i++) { |
| 3547 | if (s[i] < ' ' || s[i] > 127) |
| 3548 | s[i] = '?'; |
| 3549 | } |
| 3550 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3551 | /* Remove trailing spaces */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3552 | strim(s); |
| 3553 | } |
| 3554 | |
| 3555 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) |
| 3556 | { |
| 3557 | int i; |
| 3558 | while (len--) { |
| 3559 | crc ^= *p++ << 8; |
| 3560 | for (i = 0; i < 8; i++) |
| 3561 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 3562 | } |
| 3563 | |
| 3564 | return crc; |
| 3565 | } |
| 3566 | |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3567 | /* Parse the Extended Parameter Page. */ |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3568 | static int nand_flash_detect_ext_param_page(struct nand_chip *chip, |
| 3569 | struct nand_onfi_params *p) |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +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 | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3572 | struct onfi_ext_param_page *ep; |
| 3573 | struct onfi_ext_section *s; |
| 3574 | struct onfi_ext_ecc_info *ecc; |
| 3575 | uint8_t *cursor; |
| 3576 | int ret = -EINVAL; |
| 3577 | int len; |
| 3578 | int i; |
| 3579 | |
| 3580 | len = le16_to_cpu(p->ext_param_page_length) * 16; |
| 3581 | ep = kmalloc(len, GFP_KERNEL); |
Brian Norris | 5cb1327 | 2013-09-16 17:59:20 -0700 | [diff] [blame] | 3582 | if (!ep) |
| 3583 | return -ENOMEM; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3584 | |
| 3585 | /* Send our own NAND_CMD_PARAM. */ |
| 3586 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
| 3587 | |
| 3588 | /* Use the Change Read Column command to skip the ONFI param pages. */ |
| 3589 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
| 3590 | sizeof(*p) * p->num_of_param_pages , -1); |
| 3591 | |
| 3592 | /* Read out the Extended Parameter Page. */ |
| 3593 | chip->read_buf(mtd, (uint8_t *)ep, len); |
| 3594 | if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2) |
| 3595 | != le16_to_cpu(ep->crc))) { |
| 3596 | pr_debug("fail in the CRC.\n"); |
| 3597 | goto ext_out; |
| 3598 | } |
| 3599 | |
| 3600 | /* |
| 3601 | * Check the signature. |
| 3602 | * Do not strictly follow the ONFI spec, maybe changed in future. |
| 3603 | */ |
| 3604 | if (strncmp(ep->sig, "EPPS", 4)) { |
| 3605 | pr_debug("The signature is invalid.\n"); |
| 3606 | goto ext_out; |
| 3607 | } |
| 3608 | |
| 3609 | /* find the ECC section. */ |
| 3610 | cursor = (uint8_t *)(ep + 1); |
| 3611 | for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) { |
| 3612 | s = ep->sections + i; |
| 3613 | if (s->type == ONFI_SECTION_TYPE_2) |
| 3614 | break; |
| 3615 | cursor += s->length * 16; |
| 3616 | } |
| 3617 | if (i == ONFI_EXT_SECTION_MAX) { |
| 3618 | pr_debug("We can not find the ECC section.\n"); |
| 3619 | goto ext_out; |
| 3620 | } |
| 3621 | |
| 3622 | /* get the info we want. */ |
| 3623 | ecc = (struct onfi_ext_ecc_info *)cursor; |
| 3624 | |
Brian Norris | 4ae7d22 | 2013-09-16 18:20:21 -0700 | [diff] [blame] | 3625 | if (!ecc->codeword_size) { |
| 3626 | pr_debug("Invalid codeword size\n"); |
| 3627 | goto ext_out; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3628 | } |
| 3629 | |
Brian Norris | 4ae7d22 | 2013-09-16 18:20:21 -0700 | [diff] [blame] | 3630 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 3631 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
Brian Norris | 5cb1327 | 2013-09-16 17:59:20 -0700 | [diff] [blame] | 3632 | ret = 0; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3633 | |
| 3634 | ext_out: |
| 3635 | kfree(ep); |
| 3636 | return ret; |
| 3637 | } |
| 3638 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3639 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3640 | * 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] | 3641 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3642 | static int nand_flash_detect_onfi(struct nand_chip *chip) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3643 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3644 | struct mtd_info *mtd = nand_to_mtd(chip); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3645 | struct nand_onfi_params *p = &chip->onfi_params; |
Brian Norris | bd9c6e9 | 2013-11-29 22:04:28 -0800 | [diff] [blame] | 3646 | int i, j; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3647 | int val; |
| 3648 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3649 | /* Try ONFI for unknown chip or LP */ |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3650 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1); |
| 3651 | if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' || |
| 3652 | chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') |
| 3653 | return 0; |
| 3654 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3655 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
| 3656 | for (i = 0; i < 3; i++) { |
Brian Norris | bd9c6e9 | 2013-11-29 22:04:28 -0800 | [diff] [blame] | 3657 | for (j = 0; j < sizeof(*p); j++) |
| 3658 | ((uint8_t *)p)[j] = chip->read_byte(mtd); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3659 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == |
| 3660 | le16_to_cpu(p->crc)) { |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3661 | break; |
| 3662 | } |
| 3663 | } |
| 3664 | |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 3665 | if (i == 3) { |
| 3666 | pr_err("Could not find valid ONFI parameter page; aborting\n"); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3667 | return 0; |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 3668 | } |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3669 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3670 | /* Check version */ |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3671 | val = le16_to_cpu(p->revision); |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3672 | if (val & (1 << 5)) |
| 3673 | chip->onfi_version = 23; |
| 3674 | else if (val & (1 << 4)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3675 | chip->onfi_version = 22; |
| 3676 | else if (val & (1 << 3)) |
| 3677 | chip->onfi_version = 21; |
| 3678 | else if (val & (1 << 2)) |
| 3679 | chip->onfi_version = 20; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3680 | else if (val & (1 << 1)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3681 | chip->onfi_version = 10; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3682 | |
| 3683 | if (!chip->onfi_version) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 3684 | pr_info("unsupported ONFI version: %d\n", val); |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3685 | return 0; |
| 3686 | } |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3687 | |
| 3688 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 3689 | sanitize_string(p->model, sizeof(p->model)); |
| 3690 | if (!mtd->name) |
| 3691 | mtd->name = p->model; |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 3692 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3693 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 3694 | |
| 3695 | /* |
| 3696 | * pages_per_block and blocks_per_lun may not be a power-of-2 size |
| 3697 | * (don't ask me who thought of this...). MTD assumes that these |
| 3698 | * dimensions will be power-of-2, so just truncate the remaining area. |
| 3699 | */ |
| 3700 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 3701 | mtd->erasesize *= mtd->writesize; |
| 3702 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3703 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 3704 | |
| 3705 | /* See erasesize comment */ |
| 3706 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
Matthieu CASTET | 6379575 | 2012-03-19 15:35:25 +0100 | [diff] [blame] | 3707 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
Huang Shijie | 13fbd17 | 2013-09-25 14:58:13 +0800 | [diff] [blame] | 3708 | chip->bits_per_cell = p->bits_per_cell; |
Huang Shijie | e2985fc | 2013-05-17 11:17:30 +0800 | [diff] [blame] | 3709 | |
Zach Brown | 34da5f5 | 2017-01-10 13:30:21 -0600 | [diff] [blame] | 3710 | chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun); |
| 3711 | chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun); |
| 3712 | |
Huang Shijie | e2985fc | 2013-05-17 11:17:30 +0800 | [diff] [blame] | 3713 | if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS) |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3714 | chip->options |= NAND_BUSWIDTH_16; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3715 | |
Huang Shijie | 10c86ba | 2013-05-17 11:17:26 +0800 | [diff] [blame] | 3716 | if (p->ecc_bits != 0xff) { |
| 3717 | chip->ecc_strength_ds = p->ecc_bits; |
| 3718 | chip->ecc_step_ds = 512; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3719 | } else if (chip->onfi_version >= 21 && |
| 3720 | (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) { |
| 3721 | |
| 3722 | /* |
| 3723 | * The nand_flash_detect_ext_param_page() uses the |
| 3724 | * Change Read Column command which maybe not supported |
| 3725 | * by the chip->cmdfunc. So try to update the chip->cmdfunc |
| 3726 | * now. We do not replace user supplied command function. |
| 3727 | */ |
| 3728 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 3729 | chip->cmdfunc = nand_command_lp; |
| 3730 | |
| 3731 | /* The Extended Parameter Page is supported since ONFI 2.1. */ |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3732 | if (nand_flash_detect_ext_param_page(chip, p)) |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 3733 | pr_warn("Failed to detect ONFI extended param page\n"); |
| 3734 | } else { |
| 3735 | pr_warn("Could not retrieve ONFI ECC requirements\n"); |
Huang Shijie | 10c86ba | 2013-05-17 11:17:26 +0800 | [diff] [blame] | 3736 | } |
| 3737 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3738 | return 1; |
| 3739 | } |
| 3740 | |
| 3741 | /* |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3742 | * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise. |
| 3743 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3744 | static int nand_flash_detect_jedec(struct nand_chip *chip) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3745 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3746 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3747 | struct nand_jedec_params *p = &chip->jedec_params; |
| 3748 | struct jedec_ecc_info *ecc; |
| 3749 | int val; |
| 3750 | int i, j; |
| 3751 | |
| 3752 | /* Try JEDEC for unknown chip or LP */ |
| 3753 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1); |
| 3754 | if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' || |
| 3755 | chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' || |
| 3756 | chip->read_byte(mtd) != 'C') |
| 3757 | return 0; |
| 3758 | |
| 3759 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1); |
| 3760 | for (i = 0; i < 3; i++) { |
| 3761 | for (j = 0; j < sizeof(*p); j++) |
| 3762 | ((uint8_t *)p)[j] = chip->read_byte(mtd); |
| 3763 | |
| 3764 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) == |
| 3765 | le16_to_cpu(p->crc)) |
| 3766 | break; |
| 3767 | } |
| 3768 | |
| 3769 | if (i == 3) { |
| 3770 | pr_err("Could not find valid JEDEC parameter page; aborting\n"); |
| 3771 | return 0; |
| 3772 | } |
| 3773 | |
| 3774 | /* Check version */ |
| 3775 | val = le16_to_cpu(p->revision); |
| 3776 | if (val & (1 << 2)) |
| 3777 | chip->jedec_version = 10; |
| 3778 | else if (val & (1 << 1)) |
| 3779 | chip->jedec_version = 1; /* vendor specific version */ |
| 3780 | |
| 3781 | if (!chip->jedec_version) { |
| 3782 | pr_info("unsupported JEDEC version: %d\n", val); |
| 3783 | return 0; |
| 3784 | } |
| 3785 | |
| 3786 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 3787 | sanitize_string(p->model, sizeof(p->model)); |
| 3788 | if (!mtd->name) |
| 3789 | mtd->name = p->model; |
| 3790 | |
| 3791 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 3792 | |
| 3793 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 3794 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 3795 | mtd->erasesize *= mtd->writesize; |
| 3796 | |
| 3797 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 3798 | |
| 3799 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 3800 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
| 3801 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 3802 | chip->bits_per_cell = p->bits_per_cell; |
| 3803 | |
| 3804 | if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS) |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3805 | chip->options |= NAND_BUSWIDTH_16; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3806 | |
| 3807 | /* ECC info */ |
| 3808 | ecc = &p->ecc_info[0]; |
| 3809 | |
| 3810 | if (ecc->codeword_size >= 9) { |
| 3811 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 3812 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 3813 | } else { |
| 3814 | pr_warn("Invalid codeword size\n"); |
| 3815 | } |
| 3816 | |
| 3817 | return 1; |
| 3818 | } |
| 3819 | |
| 3820 | /* |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 3821 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 3822 | * @id_data: the ID string |
| 3823 | * @arrlen: the length of the @id_data array |
| 3824 | * @period: the period of repitition |
| 3825 | * |
| 3826 | * Check if an ID string is repeated within a given sequence of bytes at |
| 3827 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Brian Norris | d4d4f1b | 2012-11-14 21:54:20 -0800 | [diff] [blame] | 3828 | * 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] | 3829 | * if the repetition has a period of @period; otherwise, returns zero. |
| 3830 | */ |
| 3831 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 3832 | { |
| 3833 | int i, j; |
| 3834 | for (i = 0; i < period; i++) |
| 3835 | for (j = i + period; j < arrlen; j += period) |
| 3836 | if (id_data[i] != id_data[j]) |
| 3837 | return 0; |
| 3838 | return 1; |
| 3839 | } |
| 3840 | |
| 3841 | /* |
| 3842 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 3843 | * @id_data: the ID string |
| 3844 | * @arrlen: the length of the @id_data array |
| 3845 | |
| 3846 | * Returns the length of the ID string, according to known wraparound/trailing |
| 3847 | * zero patterns. If no pattern exists, returns the length of the array. |
| 3848 | */ |
| 3849 | static int nand_id_len(u8 *id_data, int arrlen) |
| 3850 | { |
| 3851 | int last_nonzero, period; |
| 3852 | |
| 3853 | /* Find last non-zero byte */ |
| 3854 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 3855 | if (id_data[last_nonzero]) |
| 3856 | break; |
| 3857 | |
| 3858 | /* All zeros */ |
| 3859 | if (last_nonzero < 0) |
| 3860 | return 0; |
| 3861 | |
| 3862 | /* Calculate wraparound period */ |
| 3863 | for (period = 1; period < arrlen; period++) |
| 3864 | if (nand_id_has_period(id_data, arrlen, period)) |
| 3865 | break; |
| 3866 | |
| 3867 | /* There's a repeated pattern */ |
| 3868 | if (period < arrlen) |
| 3869 | return period; |
| 3870 | |
| 3871 | /* There are trailing zeros */ |
| 3872 | if (last_nonzero < arrlen - 1) |
| 3873 | return last_nonzero + 1; |
| 3874 | |
| 3875 | /* No pattern detected */ |
| 3876 | return arrlen; |
| 3877 | } |
| 3878 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 3879 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 3880 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 3881 | { |
| 3882 | int bits; |
| 3883 | |
| 3884 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 3885 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 3886 | return bits + 1; |
| 3887 | } |
| 3888 | |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 3889 | /* |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3890 | * Many new NAND share similar device ID codes, which represent the size of the |
| 3891 | * chip. The rest of the parameters must be decoded according to generic or |
| 3892 | * manufacturer-specific "extended ID" decoding patterns. |
| 3893 | */ |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3894 | void nand_decode_ext_id(struct nand_chip *chip) |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3895 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3896 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 9b2d61f | 2016-06-08 10:34:57 +0200 | [diff] [blame] | 3897 | int extid; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3898 | u8 *id_data = chip->id.data; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3899 | /* The 3rd id byte holds MLC / multichip data */ |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 3900 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3901 | /* The 4th id byte is the important one */ |
| 3902 | extid = id_data[3]; |
| 3903 | |
Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 3904 | /* Calc pagesize */ |
| 3905 | mtd->writesize = 1024 << (extid & 0x03); |
| 3906 | extid >>= 2; |
| 3907 | /* Calc oobsize */ |
| 3908 | mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9); |
| 3909 | extid >>= 2; |
| 3910 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 3911 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 3912 | extid >>= 2; |
| 3913 | /* Get buswidth information */ |
| 3914 | if (extid & 0x1) |
| 3915 | chip->options |= NAND_BUSWIDTH_16; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3916 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3917 | EXPORT_SYMBOL_GPL(nand_decode_ext_id); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3918 | |
| 3919 | /* |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3920 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 3921 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 3922 | * the chip. |
| 3923 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3924 | 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] | 3925 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3926 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3927 | |
| 3928 | mtd->erasesize = type->erasesize; |
| 3929 | mtd->writesize = type->pagesize; |
| 3930 | mtd->oobsize = mtd->writesize / 32; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3931 | |
Huang Shijie | 1c195e9 | 2013-09-25 14:58:12 +0800 | [diff] [blame] | 3932 | /* All legacy ID NAND are small-page, SLC */ |
| 3933 | chip->bits_per_cell = 1; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | /* |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3937 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 3938 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 3939 | * page size, cell-type information). |
| 3940 | */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3941 | static void nand_decode_bbm_options(struct nand_chip *chip) |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3942 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3943 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3944 | |
| 3945 | /* Set the bad block position */ |
| 3946 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 3947 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 3948 | else |
| 3949 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3950 | } |
| 3951 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3952 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 3953 | { |
| 3954 | return type->id_len; |
| 3955 | } |
| 3956 | |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3957 | static bool find_full_id_nand(struct nand_chip *chip, |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3958 | struct nand_flash_dev *type) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3959 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3960 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3961 | u8 *id_data = chip->id.data; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3962 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3963 | if (!strncmp(type->id, id_data, type->id_len)) { |
| 3964 | mtd->writesize = type->pagesize; |
| 3965 | mtd->erasesize = type->erasesize; |
| 3966 | mtd->oobsize = type->oobsize; |
| 3967 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 3968 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3969 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 3970 | chip->options |= type->options; |
Huang Shijie | 5721934 | 2013-05-17 11:17:32 +0800 | [diff] [blame] | 3971 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
| 3972 | chip->ecc_step_ds = NAND_ECC_STEP(type); |
Boris BREZILLON | 57a94e2 | 2014-09-22 20:11:50 +0200 | [diff] [blame] | 3973 | chip->onfi_timing_mode_default = |
| 3974 | type->onfi_timing_mode_default; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3975 | |
Cai Zhiyong | 092b6a1 | 2013-12-25 21:19:21 +0800 | [diff] [blame] | 3976 | if (!mtd->name) |
| 3977 | mtd->name = type->name; |
| 3978 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3979 | return true; |
| 3980 | } |
| 3981 | return false; |
| 3982 | } |
| 3983 | |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3984 | /* |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3985 | * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC |
| 3986 | * compliant and does not have a full-id or legacy-id entry in the nand_ids |
| 3987 | * table. |
| 3988 | */ |
| 3989 | static void nand_manufacturer_detect(struct nand_chip *chip) |
| 3990 | { |
| 3991 | /* |
| 3992 | * Try manufacturer detection if available and use |
| 3993 | * nand_decode_ext_id() otherwise. |
| 3994 | */ |
| 3995 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
| 3996 | chip->manufacturer.desc->ops->detect) |
| 3997 | chip->manufacturer.desc->ops->detect(chip); |
| 3998 | else |
| 3999 | nand_decode_ext_id(chip); |
| 4000 | } |
| 4001 | |
| 4002 | /* |
| 4003 | * Manufacturer initialization. This function is called for all NANDs including |
| 4004 | * ONFI and JEDEC compliant ones. |
| 4005 | * Manufacturer drivers should put all their specific initialization code in |
| 4006 | * their ->init() hook. |
| 4007 | */ |
| 4008 | static int nand_manufacturer_init(struct nand_chip *chip) |
| 4009 | { |
| 4010 | if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops || |
| 4011 | !chip->manufacturer.desc->ops->init) |
| 4012 | return 0; |
| 4013 | |
| 4014 | return chip->manufacturer.desc->ops->init(chip); |
| 4015 | } |
| 4016 | |
| 4017 | /* |
| 4018 | * Manufacturer cleanup. This function is called for all NANDs including |
| 4019 | * ONFI and JEDEC compliant ones. |
| 4020 | * Manufacturer drivers should put all their specific cleanup code in their |
| 4021 | * ->cleanup() hook. |
| 4022 | */ |
| 4023 | static void nand_manufacturer_cleanup(struct nand_chip *chip) |
| 4024 | { |
| 4025 | /* Release manufacturer private data */ |
| 4026 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
| 4027 | chip->manufacturer.desc->ops->cleanup) |
| 4028 | chip->manufacturer.desc->ops->cleanup(chip); |
| 4029 | } |
| 4030 | |
| 4031 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4032 | * 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] | 4033 | */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 4034 | 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] | 4035 | { |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4036 | const struct nand_manufacturer *manufacturer; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4037 | struct mtd_info *mtd = nand_to_mtd(chip); |
Cai Zhiyong | bb77082 | 2013-12-25 20:11:15 +0800 | [diff] [blame] | 4038 | int busw; |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4039 | int i, ret; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4040 | u8 *id_data = chip->id.data; |
| 4041 | u8 maf_id, dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4043 | /* |
| 4044 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4045 | * after power-up. |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4046 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4047 | nand_reset(chip, 0); |
| 4048 | |
| 4049 | /* Select the device */ |
| 4050 | chip->select_chip(mtd, 0); |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | /* Send the command for reading device ID */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4053 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | |
| 4055 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4056 | maf_id = chip->read_byte(mtd); |
| 4057 | dev_id = chip->read_byte(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4058 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4059 | /* |
| 4060 | * 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] | 4061 | * interface concerns can cause random data which looks like a |
| 4062 | * possibly credible NAND flash to appear. If the two results do |
| 4063 | * not match, ignore the device completely. |
| 4064 | */ |
| 4065 | |
| 4066 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 4067 | |
Brian Norris | 4aef9b7 | 2012-09-24 20:40:48 -0700 | [diff] [blame] | 4068 | /* Read entire ID string */ |
| 4069 | for (i = 0; i < 8; i++) |
Kevin Cernekee | 426c457 | 2010-05-04 20:58:03 -0700 | [diff] [blame] | 4070 | id_data[i] = chip->read_byte(mtd); |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 4071 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4072 | if (id_data[0] != maf_id || id_data[1] != dev_id) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4073 | 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] | 4074 | maf_id, dev_id, id_data[0], id_data[1]); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4075 | return -ENODEV; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 4076 | } |
| 4077 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4078 | chip->id.len = nand_id_len(id_data, 8); |
| 4079 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4080 | /* Try to identify manufacturer */ |
| 4081 | manufacturer = nand_get_manufacturer(maf_id); |
| 4082 | chip->manufacturer.desc = manufacturer; |
| 4083 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4084 | if (!type) |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4085 | type = nand_flash_ids; |
| 4086 | |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4087 | /* |
| 4088 | * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic |
| 4089 | * override it. |
| 4090 | * This is required to make sure initial NAND bus width set by the |
| 4091 | * NAND controller driver is coherent with the real NAND bus width |
| 4092 | * (extracted by auto-detection code). |
| 4093 | */ |
| 4094 | busw = chip->options & NAND_BUSWIDTH_16; |
| 4095 | |
| 4096 | /* |
| 4097 | * The flag is only set (never cleared), reset it to its default value |
| 4098 | * before starting auto-detection. |
| 4099 | */ |
| 4100 | chip->options &= ~NAND_BUSWIDTH_16; |
| 4101 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4102 | for (; type->name != NULL; type++) { |
| 4103 | if (is_full_id_nand(type)) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4104 | if (find_full_id_nand(chip, type)) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4105 | goto ident_done; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4106 | } else if (dev_id == type->dev_id) { |
Brian Norris | db5b09f | 2015-05-22 10:43:12 -0700 | [diff] [blame] | 4107 | break; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4108 | } |
| 4109 | } |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4110 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4111 | chip->onfi_version = 0; |
| 4112 | if (!type->name || !type->pagesize) { |
Masahiro Yamada | 35fc519 | 2014-04-09 16:26:26 +0900 | [diff] [blame] | 4113 | /* Check if the chip is ONFI compliant */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4114 | if (nand_flash_detect_onfi(chip)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4115 | goto ident_done; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4116 | |
| 4117 | /* Check if the chip is JEDEC compliant */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4118 | if (nand_flash_detect_jedec(chip)) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4119 | goto ident_done; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4120 | } |
| 4121 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4122 | if (!type->name) |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4123 | return -ENODEV; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4124 | |
Thomas Gleixner | ba0251fe | 2006-05-27 01:02:13 +0200 | [diff] [blame] | 4125 | if (!mtd->name) |
| 4126 | mtd->name = type->name; |
| 4127 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4128 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4129 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4130 | if (!type->pagesize) |
| 4131 | nand_manufacturer_detect(chip); |
| 4132 | else |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4133 | nand_decode_id(chip, type); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4134 | |
Brian Norris | bf7a01b | 2012-07-13 09:28:24 -0700 | [diff] [blame] | 4135 | /* Get chip options */ |
| 4136 | chip->options |= type->options; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4137 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4138 | ident_done: |
| 4139 | |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 4140 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4141 | WARN_ON(busw & NAND_BUSWIDTH_16); |
| 4142 | nand_set_defaults(chip); |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 4143 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 4144 | /* |
| 4145 | * Check, if buswidth is correct. Hardware drivers should set |
| 4146 | * chip correct! |
| 4147 | */ |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4148 | 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] | 4149 | maf_id, dev_id); |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4150 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4151 | mtd->name); |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4152 | pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8, |
| 4153 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4154 | return -EINVAL; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4155 | } |
| 4156 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4157 | nand_decode_bbm_options(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4158 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4159 | /* Calculate the address shift from the page size */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4160 | chip->page_shift = ffs(mtd->writesize) - 1; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4161 | /* Convert chipsize to number of pages per chip -1 */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4162 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4163 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4164 | chip->bbt_erase_shift = chip->phys_erase_shift = |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4165 | ffs(mtd->erasesize) - 1; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4166 | if (chip->chipsize & 0xffffffff) |
| 4167 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4168 | else { |
| 4169 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 4170 | chip->chip_shift += 32 - 1; |
| 4171 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4172 | |
Artem Bityutskiy | 26d9be1 | 2011-04-28 20:26:59 +0300 | [diff] [blame] | 4173 | chip->badblockbits = 8; |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4174 | chip->erase = single_erase; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4175 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4176 | /* Do not replace user supplied command function! */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4177 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 4178 | chip->cmdfunc = nand_command_lp; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4179 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4180 | ret = nand_manufacturer_init(chip); |
| 4181 | if (ret) |
| 4182 | return ret; |
| 4183 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4184 | 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] | 4185 | maf_id, dev_id); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4186 | |
| 4187 | if (chip->onfi_version) |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4188 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4189 | chip->onfi_params.model); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4190 | else if (chip->jedec_version) |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4191 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4192 | chip->jedec_params.model); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4193 | else |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4194 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4195 | type->name); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4196 | |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4197 | 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] | 4198 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4199 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4200 | return 0; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4201 | } |
| 4202 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4203 | static const char * const nand_ecc_modes[] = { |
| 4204 | [NAND_ECC_NONE] = "none", |
| 4205 | [NAND_ECC_SOFT] = "soft", |
| 4206 | [NAND_ECC_HW] = "hw", |
| 4207 | [NAND_ECC_HW_SYNDROME] = "hw_syndrome", |
| 4208 | [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first", |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 4209 | [NAND_ECC_ON_DIE] = "on-die", |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4210 | }; |
| 4211 | |
| 4212 | static int of_get_nand_ecc_mode(struct device_node *np) |
| 4213 | { |
| 4214 | const char *pm; |
| 4215 | int err, i; |
| 4216 | |
| 4217 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4218 | if (err < 0) |
| 4219 | return err; |
| 4220 | |
| 4221 | for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++) |
| 4222 | if (!strcasecmp(pm, nand_ecc_modes[i])) |
| 4223 | return i; |
| 4224 | |
Rafał Miłecki | ae211bc | 2016-04-17 22:53:06 +0200 | [diff] [blame] | 4225 | /* |
| 4226 | * For backward compatibility we support few obsoleted values that don't |
| 4227 | * have their mappings into nand_ecc_modes_t anymore (they were merged |
| 4228 | * with other enums). |
| 4229 | */ |
| 4230 | if (!strcasecmp(pm, "soft_bch")) |
| 4231 | return NAND_ECC_SOFT; |
| 4232 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4233 | return -ENODEV; |
| 4234 | } |
| 4235 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4236 | static const char * const nand_ecc_algos[] = { |
| 4237 | [NAND_ECC_HAMMING] = "hamming", |
| 4238 | [NAND_ECC_BCH] = "bch", |
| 4239 | }; |
| 4240 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4241 | static int of_get_nand_ecc_algo(struct device_node *np) |
| 4242 | { |
| 4243 | const char *pm; |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4244 | int err, i; |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4245 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4246 | err = of_property_read_string(np, "nand-ecc-algo", &pm); |
| 4247 | if (!err) { |
| 4248 | for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++) |
| 4249 | if (!strcasecmp(pm, nand_ecc_algos[i])) |
| 4250 | return i; |
| 4251 | return -ENODEV; |
| 4252 | } |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4253 | |
| 4254 | /* |
| 4255 | * For backward compatibility we also read "nand-ecc-mode" checking |
| 4256 | * for some obsoleted values that were specifying ECC algorithm. |
| 4257 | */ |
| 4258 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4259 | if (err < 0) |
| 4260 | return err; |
| 4261 | |
| 4262 | if (!strcasecmp(pm, "soft")) |
| 4263 | return NAND_ECC_HAMMING; |
| 4264 | else if (!strcasecmp(pm, "soft_bch")) |
| 4265 | return NAND_ECC_BCH; |
| 4266 | |
| 4267 | return -ENODEV; |
| 4268 | } |
| 4269 | |
| 4270 | static int of_get_nand_ecc_step_size(struct device_node *np) |
| 4271 | { |
| 4272 | int ret; |
| 4273 | u32 val; |
| 4274 | |
| 4275 | ret = of_property_read_u32(np, "nand-ecc-step-size", &val); |
| 4276 | return ret ? ret : val; |
| 4277 | } |
| 4278 | |
| 4279 | static int of_get_nand_ecc_strength(struct device_node *np) |
| 4280 | { |
| 4281 | int ret; |
| 4282 | u32 val; |
| 4283 | |
| 4284 | ret = of_property_read_u32(np, "nand-ecc-strength", &val); |
| 4285 | return ret ? ret : val; |
| 4286 | } |
| 4287 | |
| 4288 | static int of_get_nand_bus_width(struct device_node *np) |
| 4289 | { |
| 4290 | u32 val; |
| 4291 | |
| 4292 | if (of_property_read_u32(np, "nand-bus-width", &val)) |
| 4293 | return 8; |
| 4294 | |
| 4295 | switch (val) { |
| 4296 | case 8: |
| 4297 | case 16: |
| 4298 | return val; |
| 4299 | default: |
| 4300 | return -EIO; |
| 4301 | } |
| 4302 | } |
| 4303 | |
| 4304 | static bool of_get_nand_on_flash_bbt(struct device_node *np) |
| 4305 | { |
| 4306 | return of_property_read_bool(np, "nand-on-flash-bbt"); |
| 4307 | } |
| 4308 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4309 | static int nand_dt_init(struct nand_chip *chip) |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4310 | { |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4311 | struct device_node *dn = nand_get_flash_node(chip); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4312 | int ecc_mode, ecc_algo, ecc_strength, ecc_step; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4313 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4314 | if (!dn) |
| 4315 | return 0; |
| 4316 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4317 | if (of_get_nand_bus_width(dn) == 16) |
| 4318 | chip->options |= NAND_BUSWIDTH_16; |
| 4319 | |
| 4320 | if (of_get_nand_on_flash_bbt(dn)) |
| 4321 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 4322 | |
| 4323 | ecc_mode = of_get_nand_ecc_mode(dn); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4324 | ecc_algo = of_get_nand_ecc_algo(dn); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4325 | ecc_strength = of_get_nand_ecc_strength(dn); |
| 4326 | ecc_step = of_get_nand_ecc_step_size(dn); |
| 4327 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4328 | if (ecc_mode >= 0) |
| 4329 | chip->ecc.mode = ecc_mode; |
| 4330 | |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4331 | if (ecc_algo >= 0) |
| 4332 | chip->ecc.algo = ecc_algo; |
| 4333 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4334 | if (ecc_strength >= 0) |
| 4335 | chip->ecc.strength = ecc_strength; |
| 4336 | |
| 4337 | if (ecc_step > 0) |
| 4338 | chip->ecc.size = ecc_step; |
| 4339 | |
Boris Brezillon | ba78ee0 | 2016-06-08 17:04:22 +0200 | [diff] [blame] | 4340 | if (of_property_read_bool(dn, "nand-ecc-maximize")) |
| 4341 | chip->ecc.options |= NAND_ECC_MAXIMIZE; |
| 4342 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4343 | return 0; |
| 4344 | } |
| 4345 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4346 | /** |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4347 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4348 | * @mtd: MTD device structure |
| 4349 | * @maxchips: number of chips to scan for |
| 4350 | * @table: alternative NAND ID table |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4351 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4352 | * This is the first phase of the normal nand_scan() function. It reads the |
| 4353 | * flash ID and sets up MTD fields accordingly. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4354 | * |
| 4355 | */ |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4356 | int nand_scan_ident(struct mtd_info *mtd, int maxchips, |
| 4357 | struct nand_flash_dev *table) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4358 | { |
Cai Zhiyong | bb77082 | 2013-12-25 20:11:15 +0800 | [diff] [blame] | 4359 | int i, nand_maf_id, nand_dev_id; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4360 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4361 | int ret; |
| 4362 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4363 | ret = nand_dt_init(chip); |
| 4364 | if (ret) |
| 4365 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4366 | |
Brian Norris | f7a8e38 | 2016-01-05 10:39:45 -0800 | [diff] [blame] | 4367 | if (!mtd->name && mtd->dev.parent) |
| 4368 | mtd->name = dev_name(mtd->dev.parent); |
| 4369 | |
Andrey Smirnov | 76fe334 | 2016-07-21 14:59:20 -0700 | [diff] [blame] | 4370 | if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) { |
| 4371 | /* |
| 4372 | * Default functions assigned for chip_select() and |
| 4373 | * cmdfunc() both expect cmd_ctrl() to be populated, |
| 4374 | * so we need to check that that's the case |
| 4375 | */ |
| 4376 | pr_err("chip.cmd_ctrl() callback is not provided"); |
| 4377 | return -EINVAL; |
| 4378 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4379 | /* Set the default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4380 | nand_set_defaults(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4381 | |
| 4382 | /* Read the flash type */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 4383 | ret = nand_detect(chip, table); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4384 | if (ret) { |
Ben Dooks | b1c6e6d | 2009-11-02 18:12:33 +0000 | [diff] [blame] | 4385 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4386 | pr_warn("No NAND device found\n"); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4387 | chip->select_chip(mtd, -1); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4388 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4389 | } |
| 4390 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4391 | /* Initialize the ->data_interface field. */ |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 4392 | ret = nand_init_data_interface(chip); |
| 4393 | if (ret) |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 4394 | goto err_nand_init; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 4395 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4396 | /* |
| 4397 | * Setup the data interface correctly on the chip and controller side. |
| 4398 | * This explicit call to nand_setup_data_interface() is only required |
| 4399 | * for the first die, because nand_reset() has been called before |
| 4400 | * ->data_interface and ->default_onfi_timing_mode were set. |
| 4401 | * For the other dies, nand_reset() will automatically switch to the |
| 4402 | * best mode for us. |
| 4403 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 4404 | ret = nand_setup_data_interface(chip, 0); |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4405 | if (ret) |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 4406 | goto err_nand_init; |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4407 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4408 | nand_maf_id = chip->id.data[0]; |
| 4409 | nand_dev_id = chip->id.data[1]; |
| 4410 | |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 4411 | chip->select_chip(mtd, -1); |
| 4412 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4413 | /* Check for a chip array */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4414 | for (i = 1; i < maxchips; i++) { |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4415 | /* See comment in nand_get_flash_type for reset */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4416 | nand_reset(chip, i); |
| 4417 | |
| 4418 | chip->select_chip(mtd, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | /* Send the command for reading device ID */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4420 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | /* Read manufacturer and device IDs */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4422 | if (nand_maf_id != chip->read_byte(mtd) || |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 4423 | nand_dev_id != chip->read_byte(mtd)) { |
| 4424 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | break; |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 4426 | } |
| 4427 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4428 | } |
| 4429 | if (i > 1) |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4430 | pr_info("%d chips detected\n", i); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4432 | /* Store the number of chips and calc total size for mtd */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4433 | chip->numchips = i; |
| 4434 | mtd->size = i * chip->chipsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4436 | return 0; |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 4437 | |
| 4438 | err_nand_init: |
| 4439 | /* Free manufacturer priv data. */ |
| 4440 | nand_manufacturer_cleanup(chip); |
| 4441 | |
| 4442 | return ret; |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4443 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4444 | EXPORT_SYMBOL(nand_scan_ident); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4445 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4446 | static int nand_set_ecc_soft_ops(struct mtd_info *mtd) |
| 4447 | { |
| 4448 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4449 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4450 | |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 4451 | if (WARN_ON(ecc->mode != NAND_ECC_SOFT)) |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4452 | return -EINVAL; |
| 4453 | |
| 4454 | switch (ecc->algo) { |
| 4455 | case NAND_ECC_HAMMING: |
| 4456 | ecc->calculate = nand_calculate_ecc; |
| 4457 | ecc->correct = nand_correct_data; |
| 4458 | ecc->read_page = nand_read_page_swecc; |
| 4459 | ecc->read_subpage = nand_read_subpage; |
| 4460 | ecc->write_page = nand_write_page_swecc; |
| 4461 | ecc->read_page_raw = nand_read_page_raw; |
| 4462 | ecc->write_page_raw = nand_write_page_raw; |
| 4463 | ecc->read_oob = nand_read_oob_std; |
| 4464 | ecc->write_oob = nand_write_oob_std; |
| 4465 | if (!ecc->size) |
| 4466 | ecc->size = 256; |
| 4467 | ecc->bytes = 3; |
| 4468 | ecc->strength = 1; |
| 4469 | return 0; |
| 4470 | case NAND_ECC_BCH: |
| 4471 | if (!mtd_nand_has_bch()) { |
| 4472 | WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n"); |
| 4473 | return -EINVAL; |
| 4474 | } |
| 4475 | ecc->calculate = nand_bch_calculate_ecc; |
| 4476 | ecc->correct = nand_bch_correct_data; |
| 4477 | ecc->read_page = nand_read_page_swecc; |
| 4478 | ecc->read_subpage = nand_read_subpage; |
| 4479 | ecc->write_page = nand_write_page_swecc; |
| 4480 | ecc->read_page_raw = nand_read_page_raw; |
| 4481 | ecc->write_page_raw = nand_write_page_raw; |
| 4482 | ecc->read_oob = nand_read_oob_std; |
| 4483 | ecc->write_oob = nand_write_oob_std; |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 4484 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4485 | /* |
| 4486 | * Board driver should supply ecc.size and ecc.strength |
| 4487 | * values to select how many bits are correctable. |
| 4488 | * Otherwise, default to 4 bits for large page devices. |
| 4489 | */ |
| 4490 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 4491 | ecc->size = 512; |
| 4492 | ecc->strength = 4; |
| 4493 | } |
| 4494 | |
| 4495 | /* |
| 4496 | * if no ecc placement scheme was provided pickup the default |
| 4497 | * large page one. |
| 4498 | */ |
| 4499 | if (!mtd->ooblayout) { |
| 4500 | /* handle large page devices only */ |
| 4501 | if (mtd->oobsize < 64) { |
| 4502 | WARN(1, "OOB layout is required when using software BCH on small pages\n"); |
| 4503 | return -EINVAL; |
| 4504 | } |
| 4505 | |
| 4506 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 4507 | |
| 4508 | } |
| 4509 | |
| 4510 | /* |
| 4511 | * We can only maximize ECC config when the default layout is |
| 4512 | * used, otherwise we don't know how many bytes can really be |
| 4513 | * used. |
| 4514 | */ |
| 4515 | if (mtd->ooblayout == &nand_ooblayout_lp_ops && |
| 4516 | ecc->options & NAND_ECC_MAXIMIZE) { |
| 4517 | int steps, bytes; |
| 4518 | |
| 4519 | /* Always prefer 1k blocks over 512bytes ones */ |
| 4520 | ecc->size = 1024; |
| 4521 | steps = mtd->writesize / ecc->size; |
| 4522 | |
| 4523 | /* Reserve 2 bytes for the BBM */ |
| 4524 | bytes = (mtd->oobsize - 2) / steps; |
| 4525 | ecc->strength = bytes * 8 / fls(8 * ecc->size); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | /* See nand_bch_init() for details. */ |
| 4529 | ecc->bytes = 0; |
| 4530 | ecc->priv = nand_bch_init(mtd); |
| 4531 | if (!ecc->priv) { |
| 4532 | WARN(1, "BCH ECC initialization failed!\n"); |
| 4533 | return -EINVAL; |
| 4534 | } |
| 4535 | return 0; |
| 4536 | default: |
| 4537 | WARN(1, "Unsupported ECC algorithm!\n"); |
| 4538 | return -EINVAL; |
| 4539 | } |
| 4540 | } |
| 4541 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 4542 | /** |
| 4543 | * nand_check_ecc_caps - check the sanity of preset ECC settings |
| 4544 | * @chip: nand chip info structure |
| 4545 | * @caps: ECC caps info structure |
| 4546 | * @oobavail: OOB size that the ECC engine can use |
| 4547 | * |
| 4548 | * When ECC step size and strength are already set, check if they are supported |
| 4549 | * by the controller and the calculated ECC bytes fit within the chip's OOB. |
| 4550 | * On success, the calculated ECC bytes is set. |
| 4551 | */ |
| 4552 | int nand_check_ecc_caps(struct nand_chip *chip, |
| 4553 | const struct nand_ecc_caps *caps, int oobavail) |
| 4554 | { |
| 4555 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4556 | const struct nand_ecc_step_info *stepinfo; |
| 4557 | int preset_step = chip->ecc.size; |
| 4558 | int preset_strength = chip->ecc.strength; |
| 4559 | int nsteps, ecc_bytes; |
| 4560 | int i, j; |
| 4561 | |
| 4562 | if (WARN_ON(oobavail < 0)) |
| 4563 | return -EINVAL; |
| 4564 | |
| 4565 | if (!preset_step || !preset_strength) |
| 4566 | return -ENODATA; |
| 4567 | |
| 4568 | nsteps = mtd->writesize / preset_step; |
| 4569 | |
| 4570 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4571 | stepinfo = &caps->stepinfos[i]; |
| 4572 | |
| 4573 | if (stepinfo->stepsize != preset_step) |
| 4574 | continue; |
| 4575 | |
| 4576 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4577 | if (stepinfo->strengths[j] != preset_strength) |
| 4578 | continue; |
| 4579 | |
| 4580 | ecc_bytes = caps->calc_ecc_bytes(preset_step, |
| 4581 | preset_strength); |
| 4582 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4583 | return ecc_bytes; |
| 4584 | |
| 4585 | if (ecc_bytes * nsteps > oobavail) { |
| 4586 | pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB", |
| 4587 | preset_step, preset_strength); |
| 4588 | return -ENOSPC; |
| 4589 | } |
| 4590 | |
| 4591 | chip->ecc.bytes = ecc_bytes; |
| 4592 | |
| 4593 | return 0; |
| 4594 | } |
| 4595 | } |
| 4596 | |
| 4597 | pr_err("ECC (step, strength) = (%d, %d) not supported on this controller", |
| 4598 | preset_step, preset_strength); |
| 4599 | |
| 4600 | return -ENOTSUPP; |
| 4601 | } |
| 4602 | EXPORT_SYMBOL_GPL(nand_check_ecc_caps); |
| 4603 | |
| 4604 | /** |
| 4605 | * nand_match_ecc_req - meet the chip's requirement with least ECC bytes |
| 4606 | * @chip: nand chip info structure |
| 4607 | * @caps: ECC engine caps info structure |
| 4608 | * @oobavail: OOB size that the ECC engine can use |
| 4609 | * |
| 4610 | * If a chip's ECC requirement is provided, try to meet it with the least |
| 4611 | * number of ECC bytes (i.e. with the largest number of OOB-free bytes). |
| 4612 | * On success, the chosen ECC settings are set. |
| 4613 | */ |
| 4614 | int nand_match_ecc_req(struct nand_chip *chip, |
| 4615 | const struct nand_ecc_caps *caps, int oobavail) |
| 4616 | { |
| 4617 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4618 | const struct nand_ecc_step_info *stepinfo; |
| 4619 | int req_step = chip->ecc_step_ds; |
| 4620 | int req_strength = chip->ecc_strength_ds; |
| 4621 | int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total; |
| 4622 | int best_step, best_strength, best_ecc_bytes; |
| 4623 | int best_ecc_bytes_total = INT_MAX; |
| 4624 | int i, j; |
| 4625 | |
| 4626 | if (WARN_ON(oobavail < 0)) |
| 4627 | return -EINVAL; |
| 4628 | |
| 4629 | /* No information provided by the NAND chip */ |
| 4630 | if (!req_step || !req_strength) |
| 4631 | return -ENOTSUPP; |
| 4632 | |
| 4633 | /* number of correctable bits the chip requires in a page */ |
| 4634 | req_corr = mtd->writesize / req_step * req_strength; |
| 4635 | |
| 4636 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4637 | stepinfo = &caps->stepinfos[i]; |
| 4638 | step_size = stepinfo->stepsize; |
| 4639 | |
| 4640 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4641 | strength = stepinfo->strengths[j]; |
| 4642 | |
| 4643 | /* |
| 4644 | * If both step size and strength are smaller than the |
| 4645 | * chip's requirement, it is not easy to compare the |
| 4646 | * resulted reliability. |
| 4647 | */ |
| 4648 | if (step_size < req_step && strength < req_strength) |
| 4649 | continue; |
| 4650 | |
| 4651 | if (mtd->writesize % step_size) |
| 4652 | continue; |
| 4653 | |
| 4654 | nsteps = mtd->writesize / step_size; |
| 4655 | |
| 4656 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 4657 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4658 | continue; |
| 4659 | ecc_bytes_total = ecc_bytes * nsteps; |
| 4660 | |
| 4661 | if (ecc_bytes_total > oobavail || |
| 4662 | strength * nsteps < req_corr) |
| 4663 | continue; |
| 4664 | |
| 4665 | /* |
| 4666 | * We assume the best is to meet the chip's requrement |
| 4667 | * with the least number of ECC bytes. |
| 4668 | */ |
| 4669 | if (ecc_bytes_total < best_ecc_bytes_total) { |
| 4670 | best_ecc_bytes_total = ecc_bytes_total; |
| 4671 | best_step = step_size; |
| 4672 | best_strength = strength; |
| 4673 | best_ecc_bytes = ecc_bytes; |
| 4674 | } |
| 4675 | } |
| 4676 | } |
| 4677 | |
| 4678 | if (best_ecc_bytes_total == INT_MAX) |
| 4679 | return -ENOTSUPP; |
| 4680 | |
| 4681 | chip->ecc.size = best_step; |
| 4682 | chip->ecc.strength = best_strength; |
| 4683 | chip->ecc.bytes = best_ecc_bytes; |
| 4684 | |
| 4685 | return 0; |
| 4686 | } |
| 4687 | EXPORT_SYMBOL_GPL(nand_match_ecc_req); |
| 4688 | |
| 4689 | /** |
| 4690 | * nand_maximize_ecc - choose the max ECC strength available |
| 4691 | * @chip: nand chip info structure |
| 4692 | * @caps: ECC engine caps info structure |
| 4693 | * @oobavail: OOB size that the ECC engine can use |
| 4694 | * |
| 4695 | * Choose the max ECC strength that is supported on the controller, and can fit |
| 4696 | * within the chip's OOB. On success, the chosen ECC settings are set. |
| 4697 | */ |
| 4698 | int nand_maximize_ecc(struct nand_chip *chip, |
| 4699 | const struct nand_ecc_caps *caps, int oobavail) |
| 4700 | { |
| 4701 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4702 | const struct nand_ecc_step_info *stepinfo; |
| 4703 | int step_size, strength, nsteps, ecc_bytes, corr; |
| 4704 | int best_corr = 0; |
| 4705 | int best_step = 0; |
| 4706 | int best_strength, best_ecc_bytes; |
| 4707 | int i, j; |
| 4708 | |
| 4709 | if (WARN_ON(oobavail < 0)) |
| 4710 | return -EINVAL; |
| 4711 | |
| 4712 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4713 | stepinfo = &caps->stepinfos[i]; |
| 4714 | step_size = stepinfo->stepsize; |
| 4715 | |
| 4716 | /* If chip->ecc.size is already set, respect it */ |
| 4717 | if (chip->ecc.size && step_size != chip->ecc.size) |
| 4718 | continue; |
| 4719 | |
| 4720 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4721 | strength = stepinfo->strengths[j]; |
| 4722 | |
| 4723 | if (mtd->writesize % step_size) |
| 4724 | continue; |
| 4725 | |
| 4726 | nsteps = mtd->writesize / step_size; |
| 4727 | |
| 4728 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 4729 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4730 | continue; |
| 4731 | |
| 4732 | if (ecc_bytes * nsteps > oobavail) |
| 4733 | continue; |
| 4734 | |
| 4735 | corr = strength * nsteps; |
| 4736 | |
| 4737 | /* |
| 4738 | * If the number of correctable bits is the same, |
| 4739 | * bigger step_size has more reliability. |
| 4740 | */ |
| 4741 | if (corr > best_corr || |
| 4742 | (corr == best_corr && step_size > best_step)) { |
| 4743 | best_corr = corr; |
| 4744 | best_step = step_size; |
| 4745 | best_strength = strength; |
| 4746 | best_ecc_bytes = ecc_bytes; |
| 4747 | } |
| 4748 | } |
| 4749 | } |
| 4750 | |
| 4751 | if (!best_corr) |
| 4752 | return -ENOTSUPP; |
| 4753 | |
| 4754 | chip->ecc.size = best_step; |
| 4755 | chip->ecc.strength = best_strength; |
| 4756 | chip->ecc.bytes = best_ecc_bytes; |
| 4757 | |
| 4758 | return 0; |
| 4759 | } |
| 4760 | EXPORT_SYMBOL_GPL(nand_maximize_ecc); |
| 4761 | |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 4762 | /* |
| 4763 | * Check if the chip configuration meet the datasheet requirements. |
| 4764 | |
| 4765 | * If our configuration corrects A bits per B bytes and the minimum |
| 4766 | * required correction level is X bits per Y bytes, then we must ensure |
| 4767 | * both of the following are true: |
| 4768 | * |
| 4769 | * (1) A / B >= X / Y |
| 4770 | * (2) A >= X |
| 4771 | * |
| 4772 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 4773 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 4774 | * in the same sector. |
| 4775 | */ |
| 4776 | static bool nand_ecc_strength_good(struct mtd_info *mtd) |
| 4777 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4778 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 4779 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4780 | int corr, ds_corr; |
| 4781 | |
| 4782 | if (ecc->size == 0 || chip->ecc_step_ds == 0) |
| 4783 | /* Not enough information */ |
| 4784 | return true; |
| 4785 | |
| 4786 | /* |
| 4787 | * We get the number of corrected bits per page to compare |
| 4788 | * the correction density. |
| 4789 | */ |
| 4790 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
| 4791 | ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; |
| 4792 | |
| 4793 | return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; |
| 4794 | } |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4795 | |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 4796 | static bool invalid_ecc_page_accessors(struct nand_chip *chip) |
| 4797 | { |
| 4798 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4799 | |
| 4800 | if (nand_standard_page_accessors(ecc)) |
| 4801 | return false; |
| 4802 | |
| 4803 | /* |
| 4804 | * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND |
| 4805 | * controller driver implements all the page accessors because |
| 4806 | * default helpers are not suitable when the core does not |
| 4807 | * send the READ0/PAGEPROG commands. |
| 4808 | */ |
| 4809 | return (!ecc->read_page || !ecc->write_page || |
| 4810 | !ecc->read_page_raw || !ecc->write_page_raw || |
| 4811 | (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) || |
| 4812 | (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage && |
| 4813 | ecc->hwctl && ecc->calculate)); |
| 4814 | } |
| 4815 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4816 | /** |
| 4817 | * nand_scan_tail - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4818 | * @mtd: MTD device structure |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4819 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4820 | * This is the second phase of the normal nand_scan() function. It fills out |
| 4821 | * all the uninitialized function pointers with the defaults and scans for a |
| 4822 | * bad block table if appropriate. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4823 | */ |
| 4824 | int nand_scan_tail(struct mtd_info *mtd) |
| 4825 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4826 | struct nand_chip *chip = mtd_to_nand(mtd); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4827 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4828 | struct nand_buffers *nbuf = NULL; |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4829 | int ret; |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4830 | |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 4831 | /* 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] | 4832 | if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 4833 | !(chip->bbt_options & NAND_BBT_USE_FLASH))) { |
| 4834 | ret = -EINVAL; |
| 4835 | goto err_ident; |
| 4836 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 4837 | |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 4838 | if (invalid_ecc_page_accessors(chip)) { |
| 4839 | pr_err("Invalid ECC page accessors setup\n"); |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 4840 | ret = -EINVAL; |
| 4841 | goto err_ident; |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 4842 | } |
| 4843 | |
Huang Shijie | f02ea4e | 2014-01-13 14:27:12 +0800 | [diff] [blame] | 4844 | if (!(chip->options & NAND_OWN_BUFFERS)) { |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4845 | nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL); |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 4846 | if (!nbuf) { |
| 4847 | ret = -ENOMEM; |
| 4848 | goto err_ident; |
| 4849 | } |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4850 | |
| 4851 | nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 4852 | if (!nbuf->ecccalc) { |
| 4853 | ret = -ENOMEM; |
| 4854 | goto err_free; |
| 4855 | } |
| 4856 | |
| 4857 | nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 4858 | if (!nbuf->ecccode) { |
| 4859 | ret = -ENOMEM; |
| 4860 | goto err_free; |
| 4861 | } |
| 4862 | |
| 4863 | nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize, |
| 4864 | GFP_KERNEL); |
| 4865 | if (!nbuf->databuf) { |
| 4866 | ret = -ENOMEM; |
| 4867 | goto err_free; |
| 4868 | } |
Huang Shijie | f02ea4e | 2014-01-13 14:27:12 +0800 | [diff] [blame] | 4869 | |
| 4870 | chip->buffers = nbuf; |
| 4871 | } else { |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 4872 | if (!chip->buffers) { |
| 4873 | ret = -ENOMEM; |
| 4874 | goto err_ident; |
| 4875 | } |
Huang Shijie | f02ea4e | 2014-01-13 14:27:12 +0800 | [diff] [blame] | 4876 | } |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 4877 | |
David Woodhouse | 7dcdcbef | 2006-10-21 17:09:53 +0100 | [diff] [blame] | 4878 | /* Set the internal oob buffer location, just after the page data */ |
David Woodhouse | 784f4d5 | 2006-10-22 01:47:45 +0100 | [diff] [blame] | 4879 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4880 | |
| 4881 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4882 | * If no default placement scheme is given, select an appropriate one. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4883 | */ |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4884 | if (!mtd->ooblayout && |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 4885 | !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4886 | switch (mtd->oobsize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4887 | case 8: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | case 16: |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 4889 | mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4890 | break; |
| 4891 | case 64: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 4892 | case 128: |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 4893 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops); |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 4894 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4895 | default: |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4896 | WARN(1, "No oob scheme defined for oobsize %d\n", |
| 4897 | mtd->oobsize); |
| 4898 | ret = -EINVAL; |
| 4899 | goto err_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4900 | } |
| 4901 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4902 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4903 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4904 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4905 | * selected and we have 256 byte pagesize fallback to software ECC |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4906 | */ |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 4907 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4908 | switch (ecc->mode) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 4909 | case NAND_ECC_HW_OOB_FIRST: |
| 4910 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4911 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4912 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 4913 | ret = -EINVAL; |
| 4914 | goto err_free; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 4915 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4916 | if (!ecc->read_page) |
| 4917 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 4918 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4919 | case NAND_ECC_HW: |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4920 | /* Use standard hwecc read page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4921 | if (!ecc->read_page) |
| 4922 | ecc->read_page = nand_read_page_hwecc; |
| 4923 | if (!ecc->write_page) |
| 4924 | ecc->write_page = nand_write_page_hwecc; |
| 4925 | if (!ecc->read_page_raw) |
| 4926 | ecc->read_page_raw = nand_read_page_raw; |
| 4927 | if (!ecc->write_page_raw) |
| 4928 | ecc->write_page_raw = nand_write_page_raw; |
| 4929 | if (!ecc->read_oob) |
| 4930 | ecc->read_oob = nand_read_oob_std; |
| 4931 | if (!ecc->write_oob) |
| 4932 | ecc->write_oob = nand_write_oob_std; |
| 4933 | if (!ecc->read_subpage) |
| 4934 | ecc->read_subpage = nand_read_subpage; |
Helmut Schaa | 44991b3 | 2014-04-09 11:13:24 +0200 | [diff] [blame] | 4935 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4936 | ecc->write_subpage = nand_write_subpage_hwecc; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 4937 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4938 | case NAND_ECC_HW_SYNDROME: |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4939 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 4940 | (!ecc->read_page || |
| 4941 | ecc->read_page == nand_read_page_hwecc || |
| 4942 | !ecc->write_page || |
| 4943 | ecc->write_page == nand_write_page_hwecc)) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4944 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 4945 | ret = -EINVAL; |
| 4946 | goto err_free; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4947 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4948 | /* Use standard syndrome read/write page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4949 | if (!ecc->read_page) |
| 4950 | ecc->read_page = nand_read_page_syndrome; |
| 4951 | if (!ecc->write_page) |
| 4952 | ecc->write_page = nand_write_page_syndrome; |
| 4953 | if (!ecc->read_page_raw) |
| 4954 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 4955 | if (!ecc->write_page_raw) |
| 4956 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 4957 | if (!ecc->read_oob) |
| 4958 | ecc->read_oob = nand_read_oob_syndrome; |
| 4959 | if (!ecc->write_oob) |
| 4960 | ecc->write_oob = nand_write_oob_syndrome; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 4961 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4962 | if (mtd->writesize >= ecc->size) { |
| 4963 | if (!ecc->strength) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4964 | WARN(1, "Driver must set ecc.strength when using hardware ECC\n"); |
| 4965 | ret = -EINVAL; |
| 4966 | goto err_free; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 4967 | } |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4968 | break; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 4969 | } |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 4970 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 4971 | ecc->size, mtd->writesize); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4972 | ecc->mode = NAND_ECC_SOFT; |
Rafał Miłecki | e9d4fae | 2016-04-17 22:53:02 +0200 | [diff] [blame] | 4973 | ecc->algo = NAND_ECC_HAMMING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4974 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4975 | case NAND_ECC_SOFT: |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4976 | ret = nand_set_ecc_soft_ops(mtd); |
| 4977 | if (ret) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4978 | ret = -EINVAL; |
| 4979 | goto err_free; |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 4980 | } |
| 4981 | break; |
| 4982 | |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 4983 | case NAND_ECC_ON_DIE: |
| 4984 | if (!ecc->read_page || !ecc->write_page) { |
| 4985 | WARN(1, "No ECC functions supplied; on-die ECC not possible\n"); |
| 4986 | ret = -EINVAL; |
| 4987 | goto err_free; |
| 4988 | } |
| 4989 | if (!ecc->read_oob) |
| 4990 | ecc->read_oob = nand_read_oob_std; |
| 4991 | if (!ecc->write_oob) |
| 4992 | ecc->write_oob = nand_write_oob_std; |
| 4993 | break; |
| 4994 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4995 | case NAND_ECC_NONE: |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 4996 | 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] | 4997 | ecc->read_page = nand_read_page_raw; |
| 4998 | ecc->write_page = nand_write_page_raw; |
| 4999 | ecc->read_oob = nand_read_oob_std; |
| 5000 | ecc->read_page_raw = nand_read_page_raw; |
| 5001 | ecc->write_page_raw = nand_write_page_raw; |
| 5002 | ecc->write_oob = nand_write_oob_std; |
| 5003 | ecc->size = mtd->writesize; |
| 5004 | ecc->bytes = 0; |
| 5005 | ecc->strength = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5006 | break; |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 5007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | default: |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5009 | WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode); |
| 5010 | ret = -EINVAL; |
| 5011 | goto err_free; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5012 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 5014 | /* For many systems, the standard OOB write also works for raw */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5015 | if (!ecc->read_oob_raw) |
| 5016 | ecc->read_oob_raw = ecc->read_oob; |
| 5017 | if (!ecc->write_oob_raw) |
| 5018 | ecc->write_oob_raw = ecc->write_oob; |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 5019 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5020 | /* propagate ecc info to mtd_info */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5021 | mtd->ecc_strength = ecc->strength; |
| 5022 | mtd->ecc_step_size = ecc->size; |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5023 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 5024 | /* |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5025 | * 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] | 5026 | * mode. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5027 | */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5028 | ecc->steps = mtd->writesize / ecc->size; |
| 5029 | if (ecc->steps * ecc->size != mtd->writesize) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5030 | WARN(1, "Invalid ECC parameters\n"); |
| 5031 | ret = -EINVAL; |
| 5032 | goto err_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5034 | ecc->total = ecc->steps * ecc->bytes; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 5035 | if (ecc->total > mtd->oobsize) { |
| 5036 | WARN(1, "Total number of ECC bytes exceeded oobsize\n"); |
| 5037 | ret = -EINVAL; |
| 5038 | goto err_free; |
| 5039 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5040 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5041 | /* |
| 5042 | * The number of bytes available for a client to place data into |
| 5043 | * the out of band area. |
| 5044 | */ |
| 5045 | ret = mtd_ooblayout_count_freebytes(mtd); |
| 5046 | if (ret < 0) |
| 5047 | ret = 0; |
| 5048 | |
| 5049 | mtd->oobavail = ret; |
| 5050 | |
| 5051 | /* ECC sanity check: warn if it's too weak */ |
| 5052 | if (!nand_ecc_strength_good(mtd)) |
| 5053 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 5054 | mtd->name); |
| 5055 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5056 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 5057 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5058 | switch (ecc->steps) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 5059 | case 2: |
| 5060 | mtd->subpage_sft = 1; |
| 5061 | break; |
| 5062 | case 4: |
| 5063 | case 8: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5064 | case 16: |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 5065 | mtd->subpage_sft = 2; |
| 5066 | break; |
| 5067 | } |
| 5068 | } |
| 5069 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 5070 | |
Thomas Gleixner | 04bbd0e | 2006-05-25 09:45:29 +0200 | [diff] [blame] | 5071 | /* Initialize state */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5072 | chip->state = FL_READY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | /* Invalidate the pagebuffer reference */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5075 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 5077 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 5078 | switch (ecc->mode) { |
| 5079 | case NAND_ECC_SOFT: |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 5080 | if (chip->page_shift > 9) |
| 5081 | chip->options |= NAND_SUBPAGE_READ; |
| 5082 | break; |
| 5083 | |
| 5084 | default: |
| 5085 | break; |
| 5086 | } |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 5087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5088 | /* Fill in remaining MTD driver data */ |
Huang Shijie | 963d1c2 | 2013-09-25 14:58:21 +0800 | [diff] [blame] | 5089 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 5090 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 5091 | MTD_CAP_NANDFLASH; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5092 | mtd->_erase = nand_erase; |
| 5093 | mtd->_point = NULL; |
| 5094 | mtd->_unpoint = NULL; |
| 5095 | mtd->_read = nand_read; |
| 5096 | mtd->_write = nand_write; |
| 5097 | mtd->_panic_write = panic_nand_write; |
| 5098 | mtd->_read_oob = nand_read_oob; |
| 5099 | mtd->_write_oob = nand_write_oob; |
| 5100 | mtd->_sync = nand_sync; |
| 5101 | mtd->_lock = NULL; |
| 5102 | mtd->_unlock = NULL; |
| 5103 | mtd->_suspend = nand_suspend; |
| 5104 | mtd->_resume = nand_resume; |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 5105 | mtd->_reboot = nand_shutdown; |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 5106 | mtd->_block_isreserved = nand_block_isreserved; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5107 | mtd->_block_isbad = nand_block_isbad; |
| 5108 | mtd->_block_markbad = nand_block_markbad; |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 5109 | mtd->_max_bad_blocks = nand_max_bad_blocks; |
Anatolij Gustschin | cbcab65 | 2010-12-16 23:42:16 +0100 | [diff] [blame] | 5110 | mtd->writebufsize = mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | |
Shmulik Ladkani | ea3b2ea | 2012-06-08 18:29:06 +0300 | [diff] [blame] | 5112 | /* |
| 5113 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 5114 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 5115 | * properly set. |
| 5116 | */ |
| 5117 | if (!mtd->bitflip_threshold) |
Brian Norris | 240181f | 2015-01-12 12:51:29 -0800 | [diff] [blame] | 5118 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5119 | |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 5120 | /* Check, if we should skip the bad block table scan */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5121 | if (chip->options & NAND_SKIP_BBTSCAN) |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 5122 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5123 | |
| 5124 | /* Build bad block table */ |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 5125 | ret = chip->scan_bbt(mtd); |
| 5126 | if (ret) |
| 5127 | goto err_free; |
| 5128 | return 0; |
| 5129 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5130 | err_free: |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5131 | if (nbuf) { |
| 5132 | kfree(nbuf->databuf); |
| 5133 | kfree(nbuf->ecccode); |
| 5134 | kfree(nbuf->ecccalc); |
| 5135 | kfree(nbuf); |
| 5136 | } |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5137 | |
| 5138 | err_ident: |
| 5139 | /* Clean up nand_scan_ident(). */ |
| 5140 | |
| 5141 | /* Free manufacturer priv data. */ |
| 5142 | nand_manufacturer_cleanup(chip); |
| 5143 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5144 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5146 | EXPORT_SYMBOL(nand_scan_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5148 | /* |
| 5149 | * 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] | 5150 | * 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] | 5151 | * to call us from in-kernel code if the core NAND support is modular. |
| 5152 | */ |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5153 | #ifdef MODULE |
| 5154 | #define caller_is_module() (1) |
| 5155 | #else |
| 5156 | #define caller_is_module() \ |
Rusty Russell | a6e6abd | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 5157 | is_module_text_address((unsigned long)__builtin_return_address(0)) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5158 | #endif |
| 5159 | |
| 5160 | /** |
| 5161 | * nand_scan - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5162 | * @mtd: MTD device structure |
| 5163 | * @maxchips: number of chips to scan for |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5164 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5165 | * This fills out all the uninitialized function pointers with the defaults. |
| 5166 | * 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] | 5167 | * appropriate values. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5168 | */ |
| 5169 | int nand_scan(struct mtd_info *mtd, int maxchips) |
| 5170 | { |
| 5171 | int ret; |
| 5172 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 5173 | ret = nand_scan_ident(mtd, maxchips, NULL); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5174 | if (!ret) |
| 5175 | ret = nand_scan_tail(mtd); |
| 5176 | return ret; |
| 5177 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5178 | EXPORT_SYMBOL(nand_scan); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | /** |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5181 | * nand_cleanup - [NAND Interface] Free resources held by the NAND device |
| 5182 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5183 | */ |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5184 | void nand_cleanup(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5185 | { |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5186 | if (chip->ecc.mode == NAND_ECC_SOFT && |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5187 | chip->ecc.algo == NAND_ECC_BCH) |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 5188 | nand_bch_free((struct nand_bch_control *)chip->ecc.priv); |
| 5189 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 5190 | nand_release_data_interface(chip); |
| 5191 | |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 5192 | /* Free bad block table memory */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5193 | kfree(chip->bbt); |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5194 | if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) { |
| 5195 | kfree(chip->buffers->databuf); |
| 5196 | kfree(chip->buffers->ecccode); |
| 5197 | kfree(chip->buffers->ecccalc); |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 5198 | kfree(chip->buffers); |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5199 | } |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 5200 | |
| 5201 | /* Free bad block descriptor memory */ |
| 5202 | if (chip->badblock_pattern && chip->badblock_pattern->options |
| 5203 | & NAND_BBT_DYNAMICSTRUCT) |
| 5204 | kfree(chip->badblock_pattern); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5205 | |
| 5206 | /* Free manufacturer priv data. */ |
| 5207 | nand_manufacturer_cleanup(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5208 | } |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5209 | EXPORT_SYMBOL_GPL(nand_cleanup); |
| 5210 | |
| 5211 | /** |
| 5212 | * nand_release - [NAND Interface] Unregister the MTD device and free resources |
| 5213 | * held by the NAND device |
| 5214 | * @mtd: MTD device structure |
| 5215 | */ |
| 5216 | void nand_release(struct mtd_info *mtd) |
| 5217 | { |
| 5218 | mtd_device_unregister(mtd); |
| 5219 | nand_cleanup(mtd_to_nand(mtd)); |
| 5220 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5221 | EXPORT_SYMBOL_GPL(nand_release); |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 5222 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5223 | MODULE_LICENSE("GPL"); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5224 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 5225 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5226 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |