Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Overview: |
| 3 | * This is the generic MTD driver for NAND flash devices. It should be |
| 4 | * capable of working with almost all NAND chips currently available. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Additional technical information is available on |
maximilian attems | 8b2b403 | 2007-07-28 13:07:16 +0200 | [diff] [blame] | 7 | * http://www.linux-mtd.infradead.org/doc/nand.html |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 10 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 12 | * Credits: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 13 | * David Woodhouse for adding multichip support |
| 14 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
| 16 | * rework for 2K page size chips |
| 17 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 18 | * TODO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * Enable cached programming for 2k page size chips |
| 20 | * Check, if mtd->ecctype should be set to MTD_ECC_HW |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 21 | * if we have HW ECC support. |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 22 | * BBT table is not serialized, has to be fixed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * This program is free software; you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License version 2 as |
| 26 | * published by the Free Software Foundation. |
| 27 | * |
| 28 | */ |
| 29 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 31 | |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 32 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/delay.h> |
| 34 | #include <linux/errno.h> |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 35 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/sched.h> |
| 37 | #include <linux/slab.h> |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 38 | #include <linux/mm.h> |
Ingo Molnar | 38b8d20 | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 39 | #include <linux/nmi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/types.h> |
| 41 | #include <linux/mtd/mtd.h> |
Boris Brezillon | d4092d7 | 2017-08-04 17:29:10 +0200 | [diff] [blame] | 42 | #include <linux/mtd/rawnand.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/mtd/nand_ecc.h> |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 44 | #include <linux/mtd/nand_bch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/interrupt.h> |
| 46 | #include <linux/bitops.h> |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 47 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/mtd/partitions.h> |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 49 | #include <linux/of.h> |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 50 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 51 | static int nand_get_device(struct mtd_info *mtd, int new_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 53 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 54 | struct mtd_oob_ops *ops); |
| 55 | |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 56 | /* Define default oob placement schemes for large and small page devices */ |
| 57 | static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section, |
| 58 | struct mtd_oob_region *oobregion) |
| 59 | { |
| 60 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 61 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 62 | |
| 63 | if (section > 1) |
| 64 | return -ERANGE; |
| 65 | |
| 66 | if (!section) { |
| 67 | oobregion->offset = 0; |
Miquel Raynal | f7f8c17 | 2017-07-05 08:51:09 +0200 | [diff] [blame] | 68 | if (mtd->oobsize == 16) |
| 69 | oobregion->length = 4; |
| 70 | else |
| 71 | oobregion->length = 3; |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 72 | } else { |
Miquel Raynal | f7f8c17 | 2017-07-05 08:51:09 +0200 | [diff] [blame] | 73 | if (mtd->oobsize == 8) |
| 74 | return -ERANGE; |
| 75 | |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 76 | oobregion->offset = 6; |
| 77 | oobregion->length = ecc->total - 4; |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section, |
| 84 | struct mtd_oob_region *oobregion) |
| 85 | { |
| 86 | if (section > 1) |
| 87 | return -ERANGE; |
| 88 | |
| 89 | if (mtd->oobsize == 16) { |
| 90 | if (section) |
| 91 | return -ERANGE; |
| 92 | |
| 93 | oobregion->length = 8; |
| 94 | oobregion->offset = 8; |
| 95 | } else { |
| 96 | oobregion->length = 2; |
| 97 | if (!section) |
| 98 | oobregion->offset = 3; |
| 99 | else |
| 100 | oobregion->offset = 6; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = { |
| 107 | .ecc = nand_ooblayout_ecc_sp, |
| 108 | .free = nand_ooblayout_free_sp, |
| 109 | }; |
| 110 | EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops); |
| 111 | |
| 112 | static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section, |
| 113 | struct mtd_oob_region *oobregion) |
| 114 | { |
| 115 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 116 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 117 | |
Miquel Raynal | 882fd15 | 2017-08-26 17:19:15 +0200 | [diff] [blame] | 118 | if (section || !ecc->total) |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 119 | return -ERANGE; |
| 120 | |
| 121 | oobregion->length = ecc->total; |
| 122 | oobregion->offset = mtd->oobsize - oobregion->length; |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section, |
| 128 | struct mtd_oob_region *oobregion) |
| 129 | { |
| 130 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 131 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 132 | |
| 133 | if (section) |
| 134 | return -ERANGE; |
| 135 | |
| 136 | oobregion->length = mtd->oobsize - ecc->total - 2; |
| 137 | oobregion->offset = 2; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = { |
| 143 | .ecc = nand_ooblayout_ecc_lp, |
| 144 | .free = nand_ooblayout_free_lp, |
| 145 | }; |
| 146 | EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops); |
Thomas Gleixner | d470a97 | 2006-05-23 23:48:57 +0200 | [diff] [blame] | 147 | |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 148 | /* |
| 149 | * Support the old "large page" layout used for 1-bit Hamming ECC where ECC |
| 150 | * are placed at a fixed offset. |
| 151 | */ |
| 152 | static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section, |
| 153 | struct mtd_oob_region *oobregion) |
| 154 | { |
| 155 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 156 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 157 | |
| 158 | if (section) |
| 159 | return -ERANGE; |
| 160 | |
| 161 | switch (mtd->oobsize) { |
| 162 | case 64: |
| 163 | oobregion->offset = 40; |
| 164 | break; |
| 165 | case 128: |
| 166 | oobregion->offset = 80; |
| 167 | break; |
| 168 | default: |
| 169 | return -EINVAL; |
| 170 | } |
| 171 | |
| 172 | oobregion->length = ecc->total; |
| 173 | if (oobregion->offset + oobregion->length > mtd->oobsize) |
| 174 | return -ERANGE; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section, |
| 180 | struct mtd_oob_region *oobregion) |
| 181 | { |
| 182 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 183 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 184 | int ecc_offset = 0; |
| 185 | |
| 186 | if (section < 0 || section > 1) |
| 187 | return -ERANGE; |
| 188 | |
| 189 | switch (mtd->oobsize) { |
| 190 | case 64: |
| 191 | ecc_offset = 40; |
| 192 | break; |
| 193 | case 128: |
| 194 | ecc_offset = 80; |
| 195 | break; |
| 196 | default: |
| 197 | return -EINVAL; |
| 198 | } |
| 199 | |
| 200 | if (section == 0) { |
| 201 | oobregion->offset = 2; |
| 202 | oobregion->length = ecc_offset - 2; |
| 203 | } else { |
| 204 | oobregion->offset = ecc_offset + ecc->total; |
| 205 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Colin Ian King | d4ed3b9 | 2017-05-04 13:11:00 +0100 | [diff] [blame] | 211 | static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = { |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 212 | .ecc = nand_ooblayout_ecc_lp_hamming, |
| 213 | .free = nand_ooblayout_free_lp_hamming, |
| 214 | }; |
| 215 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 216 | static int check_offs_len(struct mtd_info *mtd, |
| 217 | loff_t ofs, uint64_t len) |
| 218 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 219 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 220 | int ret = 0; |
| 221 | |
| 222 | /* Start address must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 223 | if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 224 | pr_debug("%s: unaligned address\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 225 | ret = -EINVAL; |
| 226 | } |
| 227 | |
| 228 | /* Length must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 229 | if (len & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 230 | pr_debug("%s: length not block aligned\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 231 | ret = -EINVAL; |
| 232 | } |
| 233 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 234 | return ret; |
| 235 | } |
| 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | /** |
| 238 | * nand_release_device - [GENERIC] release chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 239 | * @mtd: MTD device structure |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 240 | * |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 241 | * Release chip lock and wake up anyone waiting on the device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 243 | static void nand_release_device(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 245 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 247 | /* Release the controller and the chip */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 248 | spin_lock(&chip->controller->lock); |
| 249 | chip->controller->active = NULL; |
| 250 | chip->state = FL_READY; |
| 251 | wake_up(&chip->controller->wq); |
| 252 | spin_unlock(&chip->controller->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** |
| 256 | * nand_read_byte - [DEFAULT] read one byte from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 257 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 259 | * Default read function for 8bit buswidth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 261 | static uint8_t nand_read_byte(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 263 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 264 | return readb(chip->IO_ADDR_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /** |
Masanari Iida | 064a769 | 2012-11-09 23:20:58 +0900 | [diff] [blame] | 268 | * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 269 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 271 | * Default read function for 16bit buswidth with endianness conversion. |
| 272 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 274 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 276 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 277 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | * nand_read_word - [DEFAULT] read one word from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 282 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 284 | * Default read function for 16bit buswidth without endianness conversion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | */ |
| 286 | static u16 nand_read_word(struct mtd_info *mtd) |
| 287 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 288 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 289 | return readw(chip->IO_ADDR_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | * nand_select_chip - [DEFAULT] control CE line |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 294 | * @mtd: MTD device structure |
| 295 | * @chipnr: chipnumber to select, -1 for deselect |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | * |
| 297 | * Default select function for 1 chip devices. |
| 298 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 299 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 301 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 302 | |
| 303 | switch (chipnr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | case -1: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 305 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | break; |
| 307 | case 0: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | break; |
| 309 | |
| 310 | default: |
| 311 | BUG(); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 316 | * nand_write_byte - [DEFAULT] write single byte to chip |
| 317 | * @mtd: MTD device structure |
| 318 | * @byte: value to write |
| 319 | * |
| 320 | * Default function to write a byte to I/O[7:0] |
| 321 | */ |
| 322 | static void nand_write_byte(struct mtd_info *mtd, uint8_t byte) |
| 323 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 324 | struct nand_chip *chip = mtd_to_nand(mtd); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 325 | |
| 326 | chip->write_buf(mtd, &byte, 1); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16 |
| 331 | * @mtd: MTD device structure |
| 332 | * @byte: value to write |
| 333 | * |
| 334 | * Default function to write a byte to I/O[7:0] on a 16-bit wide chip. |
| 335 | */ |
| 336 | static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte) |
| 337 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 338 | struct nand_chip *chip = mtd_to_nand(mtd); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 339 | uint16_t word = byte; |
| 340 | |
| 341 | /* |
| 342 | * It's not entirely clear what should happen to I/O[15:8] when writing |
| 343 | * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads: |
| 344 | * |
| 345 | * When the host supports a 16-bit bus width, only data is |
| 346 | * transferred at the 16-bit width. All address and command line |
| 347 | * transfers shall use only the lower 8-bits of the data bus. During |
| 348 | * command transfers, the host may place any value on the upper |
| 349 | * 8-bits of the data bus. During address transfers, the host shall |
| 350 | * set the upper 8-bits of the data bus to 00h. |
| 351 | * |
| 352 | * One user of the write_byte callback is nand_onfi_set_features. The |
| 353 | * four parameters are specified to be written to I/O[7:0], but this is |
| 354 | * neither an address nor a command transfer. Let's assume a 0 on the |
| 355 | * upper I/O lines is OK. |
| 356 | */ |
| 357 | chip->write_buf(mtd, (uint8_t *)&word, 2); |
| 358 | } |
| 359 | |
| 360 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | * nand_write_buf - [DEFAULT] write buffer to chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 362 | * @mtd: MTD device structure |
| 363 | * @buf: data buffer |
| 364 | * @len: number of bytes to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 366 | * Default write function for 8bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 368 | static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 370 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 372 | iowrite8_rep(chip->IO_ADDR_W, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 376 | * nand_read_buf - [DEFAULT] read chip data into buffer |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 377 | * @mtd: MTD device structure |
| 378 | * @buf: buffer to store date |
| 379 | * @len: number of bytes to read |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 381 | * Default read function for 8bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 383 | static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 385 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 387 | ioread8_rep(chip->IO_ADDR_R, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | * nand_write_buf16 - [DEFAULT] write buffer to chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 392 | * @mtd: MTD device structure |
| 393 | * @buf: data buffer |
| 394 | * @len: number of bytes to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 396 | * Default write function for 16bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 398 | static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 400 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | u16 *p = (u16 *) buf; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 402 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 403 | iowrite16_rep(chip->IO_ADDR_W, p, len >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 407 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 408 | * @mtd: MTD device structure |
| 409 | * @buf: buffer to store date |
| 410 | * @len: number of bytes to read |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 412 | * Default read function for 16bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 414 | static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 416 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | u16 *p = (u16 *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 419 | ioread16_rep(chip->IO_ADDR_R, p, len >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 424 | * @mtd: MTD device structure |
| 425 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 427 | * Check, if the block is bad. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | */ |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 429 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 431 | int page, page_end, res; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 432 | struct nand_chip *chip = mtd_to_nand(mtd); |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 433 | u8 bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Brian Norris | 5fb1549 | 2011-05-31 16:31:21 -0700 | [diff] [blame] | 435 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 436 | ofs += mtd->erasesize - mtd->writesize; |
| 437 | |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 438 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 439 | page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1); |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 440 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 441 | for (; page < page_end; page++) { |
| 442 | res = chip->ecc.read_oob(mtd, chip, page); |
| 443 | if (res) |
| 444 | return res; |
| 445 | |
| 446 | bad = chip->oob_poi[chip->badblockpos]; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 447 | |
Brian Norris | cdbec05 | 2012-01-13 18:11:48 -0800 | [diff] [blame] | 448 | if (likely(chip->badblockbits == 8)) |
| 449 | res = bad != 0xFF; |
| 450 | else |
| 451 | res = hweight8(bad) < chip->badblockbits; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 452 | if (res) |
| 453 | return res; |
| 454 | } |
Maxim Levitsky | e0b58d0 | 2010-02-22 20:39:38 +0200 | [diff] [blame] | 455 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 456 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /** |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 460 | * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 461 | * @mtd: MTD device structure |
| 462 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 464 | * This is the default implementation, which can be overridden by a hardware |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 465 | * specific driver. It provides the details for writing a bad block marker to a |
| 466 | * block. |
| 467 | */ |
| 468 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 469 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 470 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 471 | struct mtd_oob_ops ops; |
| 472 | uint8_t buf[2] = { 0, 0 }; |
| 473 | int ret = 0, res, i = 0; |
| 474 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 475 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 476 | ops.oobbuf = buf; |
| 477 | ops.ooboffs = chip->badblockpos; |
| 478 | if (chip->options & NAND_BUSWIDTH_16) { |
| 479 | ops.ooboffs &= ~0x01; |
| 480 | ops.len = ops.ooblen = 2; |
| 481 | } else { |
| 482 | ops.len = ops.ooblen = 1; |
| 483 | } |
| 484 | ops.mode = MTD_OPS_PLACE_OOB; |
| 485 | |
| 486 | /* Write to first/last page(s) if necessary */ |
| 487 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 488 | ofs += mtd->erasesize - mtd->writesize; |
| 489 | do { |
| 490 | res = nand_do_write_oob(mtd, ofs, &ops); |
| 491 | if (!ret) |
| 492 | ret = res; |
| 493 | |
| 494 | i++; |
| 495 | ofs += mtd->writesize; |
| 496 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
| 497 | |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * nand_block_markbad_lowlevel - mark a block bad |
| 503 | * @mtd: MTD device structure |
| 504 | * @ofs: offset from device start |
| 505 | * |
| 506 | * This function performs the generic NAND bad block marking steps (i.e., bad |
| 507 | * block table(s) and/or marker(s)). We only allow the hardware driver to |
| 508 | * specify how to write bad block markers to OOB (chip->block_markbad). |
| 509 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 510 | * We try operations in the following order: |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 511 | * |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 512 | * (1) erase the affected block, to allow OOB marker to be written cleanly |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 513 | * (2) write bad block marker to OOB area of affected block (unless flag |
| 514 | * NAND_BBT_NO_OOB_BBM is present) |
| 515 | * (3) update the BBT |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 516 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 517 | * Note that we retain the first error encountered in (2) or (3), finish the |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 518 | * procedures, and dump the error in the end. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | */ |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 520 | static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 522 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 523 | int res, ret = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 524 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 525 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 526 | struct erase_info einfo; |
| 527 | |
| 528 | /* Attempt erase before marking OOB */ |
| 529 | memset(&einfo, 0, sizeof(einfo)); |
| 530 | einfo.mtd = mtd; |
| 531 | einfo.addr = ofs; |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 532 | einfo.len = 1ULL << chip->phys_erase_shift; |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 533 | nand_erase_nand(mtd, &einfo, 0); |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 534 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 535 | /* Write bad block marker to OOB */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 536 | nand_get_device(mtd, FL_WRITING); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 537 | ret = chip->block_markbad(mtd, ofs); |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 538 | nand_release_device(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 539 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 540 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 541 | /* Mark block bad in BBT */ |
| 542 | if (chip->bbt) { |
| 543 | res = nand_markbad_bbt(mtd, ofs); |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 544 | if (!ret) |
| 545 | ret = res; |
| 546 | } |
| 547 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 548 | if (!ret) |
| 549 | mtd->ecc_stats.badblocks++; |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 550 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 551 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 554 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | * nand_check_wp - [GENERIC] check if the chip is write protected |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 556 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 558 | * Check, if the device is write protected. The function expects, that the |
| 559 | * device is already selected. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 561 | static int nand_check_wp(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 563 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 564 | u8 status; |
| 565 | int ret; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 566 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 567 | /* Broken xD cards report WP despite being writable */ |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 568 | if (chip->options & NAND_BROKEN_XD) |
| 569 | return 0; |
| 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | /* Check the WP bit */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 572 | ret = nand_status_op(chip, &status); |
| 573 | if (ret) |
| 574 | return ret; |
| 575 | |
| 576 | return status & NAND_STATUS_WP ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /** |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 580 | * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 581 | * @mtd: MTD device structure |
| 582 | * @ofs: offset from device start |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 583 | * |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 584 | * Check if the block is marked as reserved. |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 585 | */ |
| 586 | static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 587 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 588 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 589 | |
| 590 | if (!chip->bbt) |
| 591 | return 0; |
| 592 | /* Return info from the table */ |
| 593 | return nand_isreserved_bbt(mtd, ofs); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 598 | * @mtd: MTD device structure |
| 599 | * @ofs: offset from device start |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 600 | * @allowbbt: 1, if its allowed to access the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | * |
| 602 | * Check, if the block is bad. Either by reading the bad block table or |
| 603 | * calling of the scan function. |
| 604 | */ |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 605 | 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] | 606 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 607 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 608 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 609 | if (!chip->bbt) |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 610 | return chip->block_bad(mtd, ofs); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | /* Return info from the table */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 613 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 616 | /** |
| 617 | * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 618 | * @mtd: MTD device structure |
| 619 | * @timeo: Timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 620 | * |
| 621 | * Helper function for nand_wait_ready used when needing to wait in interrupt |
| 622 | * context. |
| 623 | */ |
| 624 | static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo) |
| 625 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 626 | struct nand_chip *chip = mtd_to_nand(mtd); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 627 | int i; |
| 628 | |
| 629 | /* Wait for the device to get ready */ |
| 630 | for (i = 0; i < timeo; i++) { |
| 631 | if (chip->dev_ready(mtd)) |
| 632 | break; |
| 633 | touch_softlockup_watchdog(); |
| 634 | mdelay(1); |
| 635 | } |
| 636 | } |
| 637 | |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 638 | /** |
| 639 | * nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
| 640 | * @mtd: MTD device structure |
| 641 | * |
| 642 | * Wait for the ready pin after a command, and warn if a timeout occurs. |
| 643 | */ |
David Woodhouse | 4b648b0 | 2006-09-25 17:05:24 +0100 | [diff] [blame] | 644 | void nand_wait_ready(struct mtd_info *mtd) |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 645 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 646 | struct nand_chip *chip = mtd_to_nand(mtd); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 647 | unsigned long timeo = 400; |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 648 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 649 | if (in_interrupt() || oops_in_progress) |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 650 | return panic_nand_wait_ready(mtd, timeo); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 651 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 652 | /* Wait until command is processed or timeout occurs */ |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 653 | timeo = jiffies + msecs_to_jiffies(timeo); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 654 | do { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 655 | if (chip->dev_ready(mtd)) |
Ezequiel Garcia | 4c7e054 | 2016-04-12 17:46:41 -0300 | [diff] [blame] | 656 | return; |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 657 | cond_resched(); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 658 | } while (time_before(jiffies, timeo)); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 659 | |
Brian Norris | 9ebfdf5 | 2016-03-04 17:19:23 -0800 | [diff] [blame] | 660 | if (!chip->dev_ready(mtd)) |
| 661 | pr_warn_ratelimited("timeout while waiting for chip to become ready\n"); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 662 | } |
David Woodhouse | 4b648b0 | 2006-09-25 17:05:24 +0100 | [diff] [blame] | 663 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | /** |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 666 | * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands. |
| 667 | * @mtd: MTD device structure |
| 668 | * @timeo: Timeout in ms |
| 669 | * |
| 670 | * Wait for status ready (i.e. command done) or timeout. |
| 671 | */ |
| 672 | static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo) |
| 673 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 674 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 675 | int ret; |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 676 | |
| 677 | timeo = jiffies + msecs_to_jiffies(timeo); |
| 678 | do { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 679 | u8 status; |
| 680 | |
| 681 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 682 | if (ret) |
| 683 | return; |
| 684 | |
| 685 | if (status & NAND_STATUS_READY) |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 686 | break; |
| 687 | touch_softlockup_watchdog(); |
| 688 | } while (time_before(jiffies, timeo)); |
| 689 | }; |
| 690 | |
| 691 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | * nand_command - [DEFAULT] Send command to NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 693 | * @mtd: MTD device structure |
| 694 | * @command: the command to be sent |
| 695 | * @column: the column address for this command, -1 if none |
| 696 | * @page_addr: the page address for this command, -1 if none |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 698 | * 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] | 699 | * (512 Bytes per page). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 701 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
| 702 | int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 704 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 705 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 707 | /* Write out the command to the device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | if (command == NAND_CMD_SEQIN) { |
| 709 | int readcmd; |
| 710 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 711 | if (column >= mtd->writesize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | /* OOB area */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 713 | column -= mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | readcmd = NAND_CMD_READOOB; |
| 715 | } else if (column < 256) { |
| 716 | /* First 256 bytes --> READ0 */ |
| 717 | readcmd = NAND_CMD_READ0; |
| 718 | } else { |
| 719 | column -= 256; |
| 720 | readcmd = NAND_CMD_READ1; |
| 721 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 722 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 723 | ctrl &= ~NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | } |
Miquel Raynal | df46789 | 2017-11-08 17:00:27 +0100 | [diff] [blame] | 725 | if (command != NAND_CMD_NONE) |
| 726 | chip->cmd_ctrl(mtd, command, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 728 | /* Address cycle, when necessary */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 729 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
| 730 | /* Serially input address */ |
| 731 | if (column != -1) { |
| 732 | /* Adjust columns for 16 bit buswidth */ |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 733 | if (chip->options & NAND_BUSWIDTH_16 && |
| 734 | !nand_opcode_8bits(command)) |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 735 | column >>= 1; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 736 | chip->cmd_ctrl(mtd, column, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 737 | ctrl &= ~NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 739 | if (page_addr != -1) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 740 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 741 | ctrl &= ~NAND_CTRL_CHANGE; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 742 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame] | 743 | if (chip->options & NAND_ROW_ADDR_3) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 744 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 745 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 746 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 747 | |
| 748 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 749 | * Program and erase have their own busy handlers status and sequential |
| 750 | * in needs no delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 751 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 753 | |
Miquel Raynal | df46789 | 2017-11-08 17:00:27 +0100 | [diff] [blame] | 754 | case NAND_CMD_NONE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | case NAND_CMD_PAGEPROG: |
| 756 | case NAND_CMD_ERASE1: |
| 757 | case NAND_CMD_ERASE2: |
| 758 | case NAND_CMD_SEQIN: |
| 759 | case NAND_CMD_STATUS: |
Masahiro Yamada | 3158fa0 | 2017-03-23 09:17:49 +0900 | [diff] [blame] | 760 | case NAND_CMD_READID: |
Masahiro Yamada | c5d664a | 2017-03-23 09:17:50 +0900 | [diff] [blame] | 761 | case NAND_CMD_SET_FEATURES: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | return; |
| 763 | |
| 764 | case NAND_CMD_RESET: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 765 | if (chip->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | break; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 767 | udelay(chip->chip_delay); |
| 768 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 769 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 770 | chip->cmd_ctrl(mtd, |
| 771 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 772 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 773 | nand_wait_status_ready(mtd, 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | return; |
| 775 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 776 | /* This applies to read commands */ |
Boris Brezillon | 2165c4a | 2017-05-16 18:35:45 +0200 | [diff] [blame] | 777 | case NAND_CMD_READ0: |
| 778 | /* |
| 779 | * READ0 is sometimes used to exit GET STATUS mode. When this |
| 780 | * is the case no address cycles are requested, and we can use |
| 781 | * this information to detect that we should not wait for the |
| 782 | * device to be ready. |
| 783 | */ |
| 784 | if (column == -1 && page_addr == -1) |
| 785 | return; |
| 786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 788 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | * If we don't have access to the busy pin, we apply the given |
| 790 | * command delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 791 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 792 | if (!chip->dev_ready) { |
| 793 | udelay(chip->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 795 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 797 | /* |
| 798 | * Apply this short delay always to ensure that we do wait tWB in |
| 799 | * any case on any machine. |
| 800 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 801 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 802 | |
| 803 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 806 | static void nand_ccs_delay(struct nand_chip *chip) |
| 807 | { |
| 808 | /* |
| 809 | * The controller already takes care of waiting for tCCS when the RNDIN |
| 810 | * or RNDOUT command is sent, return directly. |
| 811 | */ |
| 812 | if (!(chip->options & NAND_WAIT_TCCS)) |
| 813 | return; |
| 814 | |
| 815 | /* |
| 816 | * Wait tCCS_min if it is correctly defined, otherwise wait 500ns |
| 817 | * (which should be safe for all NANDs). |
| 818 | */ |
| 819 | if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min) |
| 820 | ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000); |
| 821 | else |
| 822 | ndelay(500); |
| 823 | } |
| 824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | /** |
| 826 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 827 | * @mtd: MTD device structure |
| 828 | * @command: the command to be sent |
| 829 | * @column: the column address for this command, -1 if none |
| 830 | * @page_addr: the page address for this command, -1 if none |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | * |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 832 | * 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] | 833 | * devices. We don't have the separate regions as we have in the small page |
| 834 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 836 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
| 837 | int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 839 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
| 841 | /* Emulate NAND_CMD_READOOB */ |
| 842 | if (command == NAND_CMD_READOOB) { |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 843 | column += mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | command = NAND_CMD_READ0; |
| 845 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 846 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 847 | /* Command latch cycle */ |
Miquel Raynal | df46789 | 2017-11-08 17:00:27 +0100 | [diff] [blame] | 848 | if (command != NAND_CMD_NONE) |
| 849 | chip->cmd_ctrl(mtd, command, |
| 850 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | |
| 852 | if (column != -1 || page_addr != -1) { |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 853 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | |
| 855 | /* Serially input address */ |
| 856 | if (column != -1) { |
| 857 | /* Adjust columns for 16 bit buswidth */ |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 858 | if (chip->options & NAND_BUSWIDTH_16 && |
| 859 | !nand_opcode_8bits(command)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | column >>= 1; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 861 | chip->cmd_ctrl(mtd, column, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 862 | ctrl &= ~NAND_CTRL_CHANGE; |
Boris Brezillon | fde85cf | 2016-06-15 13:09:51 +0200 | [diff] [blame] | 863 | |
Brian Norris | f5b88de | 2016-10-03 09:49:35 -0700 | [diff] [blame] | 864 | /* Only output a single addr cycle for 8bits opcodes. */ |
Boris Brezillon | fde85cf | 2016-06-15 13:09:51 +0200 | [diff] [blame] | 865 | if (!nand_opcode_8bits(command)) |
| 866 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 867 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | if (page_addr != -1) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 869 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 870 | chip->cmd_ctrl(mtd, page_addr >> 8, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 871 | NAND_NCE | NAND_ALE); |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame] | 872 | if (chip->options & NAND_ROW_ADDR_3) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 873 | chip->cmd_ctrl(mtd, page_addr >> 16, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 874 | NAND_NCE | NAND_ALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 877 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 878 | |
| 879 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 880 | * Program and erase have their own busy handlers status, sequential |
Gerhard Sittig | 7a442f1 | 2014-03-29 14:36:22 +0100 | [diff] [blame] | 881 | * in and status need no delay. |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 882 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 884 | |
Miquel Raynal | df46789 | 2017-11-08 17:00:27 +0100 | [diff] [blame] | 885 | case NAND_CMD_NONE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | case NAND_CMD_CACHEDPROG: |
| 887 | case NAND_CMD_PAGEPROG: |
| 888 | case NAND_CMD_ERASE1: |
| 889 | case NAND_CMD_ERASE2: |
| 890 | case NAND_CMD_SEQIN: |
| 891 | case NAND_CMD_STATUS: |
Masahiro Yamada | 3158fa0 | 2017-03-23 09:17:49 +0900 | [diff] [blame] | 892 | case NAND_CMD_READID: |
Masahiro Yamada | c5d664a | 2017-03-23 09:17:50 +0900 | [diff] [blame] | 893 | case NAND_CMD_SET_FEATURES: |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 894 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 896 | case NAND_CMD_RNDIN: |
| 897 | nand_ccs_delay(chip); |
| 898 | return; |
| 899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | case NAND_CMD_RESET: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 901 | if (chip->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | break; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 903 | udelay(chip->chip_delay); |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 904 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 905 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 906 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 907 | NAND_NCE | NAND_CTRL_CHANGE); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 908 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 909 | nand_wait_status_ready(mtd, 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | return; |
| 911 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 912 | case NAND_CMD_RNDOUT: |
| 913 | /* No ready / busy check necessary */ |
| 914 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, |
| 915 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 916 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 917 | NAND_NCE | NAND_CTRL_CHANGE); |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 918 | |
| 919 | nand_ccs_delay(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 920 | return; |
| 921 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | case NAND_CMD_READ0: |
Boris Brezillon | 2165c4a | 2017-05-16 18:35:45 +0200 | [diff] [blame] | 923 | /* |
| 924 | * READ0 is sometimes used to exit GET STATUS mode. When this |
| 925 | * is the case no address cycles are requested, and we can use |
| 926 | * this information to detect that READSTART should not be |
| 927 | * issued. |
| 928 | */ |
| 929 | if (column == -1 && page_addr == -1) |
| 930 | return; |
| 931 | |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 932 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
| 933 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 934 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 935 | NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 936 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 937 | /* This applies to read commands */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 939 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | * 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] | 941 | * command delay. |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 942 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 943 | if (!chip->dev_ready) { |
| 944 | udelay(chip->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 946 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | } |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 948 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 949 | /* |
| 950 | * Apply this short delay always to ensure that we do wait tWB in |
| 951 | * any case on any machine. |
| 952 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 953 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 954 | |
| 955 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 959 | * panic_nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 960 | * @chip: the nand chip descriptor |
| 961 | * @mtd: MTD device structure |
| 962 | * @new_state: the state which is requested |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 963 | * |
| 964 | * Used when in panic, no locks are taken. |
| 965 | */ |
| 966 | static void panic_nand_get_device(struct nand_chip *chip, |
| 967 | struct mtd_info *mtd, int new_state) |
| 968 | { |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 969 | /* Hardware controller shared among independent devices */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 970 | chip->controller->active = chip; |
| 971 | chip->state = new_state; |
| 972 | } |
| 973 | |
| 974 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | * nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 976 | * @mtd: MTD device structure |
| 977 | * @new_state: the state which is requested |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | * |
| 979 | * Get the device and lock it for exclusive access |
| 980 | */ |
Thomas Gleixner | 2c0a2be | 2006-05-23 11:50:56 +0200 | [diff] [blame] | 981 | static int |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 982 | nand_get_device(struct mtd_info *mtd, int new_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 984 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 985 | spinlock_t *lock = &chip->controller->lock; |
| 986 | wait_queue_head_t *wq = &chip->controller->wq; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 987 | DECLARE_WAITQUEUE(wait, current); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 988 | retry: |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 989 | spin_lock(lock); |
| 990 | |
vimal singh | b8b3ee9 | 2009-07-09 20:41:22 +0530 | [diff] [blame] | 991 | /* Hardware controller shared among independent devices */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 992 | if (!chip->controller->active) |
| 993 | chip->controller->active = chip; |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 994 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 995 | if (chip->controller->active == chip && chip->state == FL_READY) { |
| 996 | chip->state = new_state; |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 997 | spin_unlock(lock); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 998 | return 0; |
| 999 | } |
| 1000 | if (new_state == FL_PM_SUSPENDED) { |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 1001 | if (chip->controller->active->state == FL_PM_SUSPENDED) { |
| 1002 | chip->state = FL_PM_SUSPENDED; |
| 1003 | spin_unlock(lock); |
| 1004 | return 0; |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 1005 | } |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 1006 | } |
| 1007 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1008 | add_wait_queue(wq, &wait); |
| 1009 | spin_unlock(lock); |
| 1010 | schedule(); |
| 1011 | remove_wait_queue(wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | goto retry; |
| 1013 | } |
| 1014 | |
| 1015 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1016 | * panic_nand_wait - [GENERIC] wait until the command is done |
| 1017 | * @mtd: MTD device structure |
| 1018 | * @chip: NAND chip structure |
| 1019 | * @timeo: timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1020 | * |
| 1021 | * Wait for command done. This is a helper function for nand_wait used when |
| 1022 | * 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] | 1023 | * an oops through mtdoops. |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1024 | */ |
| 1025 | static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, |
| 1026 | unsigned long timeo) |
| 1027 | { |
| 1028 | int i; |
| 1029 | for (i = 0; i < timeo; i++) { |
| 1030 | if (chip->dev_ready) { |
| 1031 | if (chip->dev_ready(mtd)) |
| 1032 | break; |
| 1033 | } else { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1034 | int ret; |
| 1035 | u8 status; |
| 1036 | |
| 1037 | ret = nand_read_data_op(chip, &status, sizeof(status), |
| 1038 | true); |
| 1039 | if (ret) |
| 1040 | return; |
| 1041 | |
| 1042 | if (status & NAND_STATUS_READY) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1043 | break; |
| 1044 | } |
| 1045 | mdelay(1); |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1046 | } |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1050 | * nand_wait - [DEFAULT] wait until the command is done |
| 1051 | * @mtd: MTD device structure |
| 1052 | * @chip: NAND chip structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | * |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1054 | * Wait for command done. This applies to erase and program only. |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 1055 | */ |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 1056 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | { |
| 1058 | |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1059 | unsigned long timeo = 400; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1060 | u8 status; |
| 1061 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1063 | /* |
| 1064 | * Apply this short delay always to ensure that we do wait tWB in any |
| 1065 | * case on any machine. |
| 1066 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1067 | ndelay(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1069 | ret = nand_status_op(chip, NULL); |
| 1070 | if (ret) |
| 1071 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1073 | if (in_interrupt() || oops_in_progress) |
| 1074 | panic_nand_wait(mtd, chip, timeo); |
| 1075 | else { |
Huang Shijie | 6d2559f | 2013-01-30 10:03:56 +0800 | [diff] [blame] | 1076 | timeo = jiffies + msecs_to_jiffies(timeo); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1077 | do { |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1078 | if (chip->dev_ready) { |
| 1079 | if (chip->dev_ready(mtd)) |
| 1080 | break; |
| 1081 | } else { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1082 | ret = nand_read_data_op(chip, &status, |
| 1083 | sizeof(status), true); |
| 1084 | if (ret) |
| 1085 | return ret; |
| 1086 | |
| 1087 | if (status & NAND_STATUS_READY) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1088 | break; |
| 1089 | } |
| 1090 | cond_resched(); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1091 | } while (time_before(jiffies, timeo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | } |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 1093 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1094 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 1095 | if (ret) |
| 1096 | return ret; |
| 1097 | |
Matthieu CASTET | f251b8d | 2012-11-05 15:00:44 +0100 | [diff] [blame] | 1098 | /* This can happen if in case of timeout or buggy dev_ready */ |
| 1099 | WARN_ON(!(status & NAND_STATUS_READY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | return status; |
| 1101 | } |
| 1102 | |
| 1103 | /** |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1104 | * nand_reset_data_interface - Reset data interface and timings |
| 1105 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1106 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1107 | * |
| 1108 | * Reset the Data interface and timings to ONFI mode 0. |
| 1109 | * |
| 1110 | * Returns 0 for success or negative error code otherwise. |
| 1111 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1112 | static int nand_reset_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1113 | { |
| 1114 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1115 | const struct nand_data_interface *conf; |
| 1116 | int ret; |
| 1117 | |
| 1118 | if (!chip->setup_data_interface) |
| 1119 | return 0; |
| 1120 | |
| 1121 | /* |
| 1122 | * The ONFI specification says: |
| 1123 | * " |
| 1124 | * To transition from NV-DDR or NV-DDR2 to the SDR data |
| 1125 | * interface, the host shall use the Reset (FFh) command |
| 1126 | * using SDR timing mode 0. A device in any timing mode is |
| 1127 | * required to recognize Reset (FFh) command issued in SDR |
| 1128 | * timing mode 0. |
| 1129 | * " |
| 1130 | * |
| 1131 | * Configure the data interface in SDR mode and set the |
| 1132 | * timings to timing mode 0. |
| 1133 | */ |
| 1134 | |
| 1135 | conf = nand_get_default_data_interface(); |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1136 | ret = chip->setup_data_interface(mtd, chipnr, conf); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1137 | if (ret) |
| 1138 | pr_err("Failed to configure data interface to SDR timing mode 0\n"); |
| 1139 | |
| 1140 | return ret; |
| 1141 | } |
| 1142 | |
| 1143 | /** |
| 1144 | * nand_setup_data_interface - Setup the best data interface and timings |
| 1145 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1146 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1147 | * |
| 1148 | * Find and configure the best data interface and NAND timings supported by |
| 1149 | * the chip and the driver. |
| 1150 | * First tries to retrieve supported timing modes from ONFI information, |
| 1151 | * and if the NAND chip does not support ONFI, relies on the |
| 1152 | * ->onfi_timing_mode_default specified in the nand_ids table. |
| 1153 | * |
| 1154 | * Returns 0 for success or negative error code otherwise. |
| 1155 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1156 | static int nand_setup_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1157 | { |
| 1158 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1159 | int ret; |
| 1160 | |
| 1161 | if (!chip->setup_data_interface || !chip->data_interface) |
| 1162 | return 0; |
| 1163 | |
| 1164 | /* |
| 1165 | * Ensure the timing mode has been changed on the chip side |
| 1166 | * before changing timings on the controller side. |
| 1167 | */ |
Boris Brezillon | a11bf5e | 2017-07-31 10:29:56 +0200 | [diff] [blame] | 1168 | if (chip->onfi_version && |
| 1169 | (le16_to_cpu(chip->onfi_params.opt_cmd) & |
| 1170 | ONFI_OPT_CMD_SET_GET_FEATURES)) { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1171 | u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { |
| 1172 | chip->onfi_timing_mode_default, |
| 1173 | }; |
| 1174 | |
| 1175 | ret = chip->onfi_set_features(mtd, chip, |
| 1176 | ONFI_FEATURE_ADDR_TIMING_MODE, |
| 1177 | tmode_param); |
| 1178 | if (ret) |
| 1179 | goto err; |
| 1180 | } |
| 1181 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1182 | ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1183 | err: |
| 1184 | return ret; |
| 1185 | } |
| 1186 | |
| 1187 | /** |
| 1188 | * nand_init_data_interface - find the best data interface and timings |
| 1189 | * @chip: The NAND chip |
| 1190 | * |
| 1191 | * Find the best data interface and NAND timings supported by the chip |
| 1192 | * and the driver. |
| 1193 | * First tries to retrieve supported timing modes from ONFI information, |
| 1194 | * and if the NAND chip does not support ONFI, relies on the |
| 1195 | * ->onfi_timing_mode_default specified in the nand_ids table. After this |
| 1196 | * function nand_chip->data_interface is initialized with the best timing mode |
| 1197 | * available. |
| 1198 | * |
| 1199 | * Returns 0 for success or negative error code otherwise. |
| 1200 | */ |
| 1201 | static int nand_init_data_interface(struct nand_chip *chip) |
| 1202 | { |
| 1203 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1204 | int modes, mode, ret; |
| 1205 | |
| 1206 | if (!chip->setup_data_interface) |
| 1207 | return 0; |
| 1208 | |
| 1209 | /* |
| 1210 | * First try to identify the best timings from ONFI parameters and |
| 1211 | * if the NAND does not support ONFI, fallback to the default ONFI |
| 1212 | * timing mode. |
| 1213 | */ |
| 1214 | modes = onfi_get_async_timing_mode(chip); |
| 1215 | if (modes == ONFI_TIMING_MODE_UNKNOWN) { |
| 1216 | if (!chip->onfi_timing_mode_default) |
| 1217 | return 0; |
| 1218 | |
| 1219 | modes = GENMASK(chip->onfi_timing_mode_default, 0); |
| 1220 | } |
| 1221 | |
| 1222 | chip->data_interface = kzalloc(sizeof(*chip->data_interface), |
| 1223 | GFP_KERNEL); |
| 1224 | if (!chip->data_interface) |
| 1225 | return -ENOMEM; |
| 1226 | |
| 1227 | for (mode = fls(modes) - 1; mode >= 0; mode--) { |
| 1228 | ret = onfi_init_data_interface(chip, chip->data_interface, |
| 1229 | NAND_SDR_IFACE, mode); |
| 1230 | if (ret) |
| 1231 | continue; |
| 1232 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1233 | /* Pass -1 to only */ |
| 1234 | ret = chip->setup_data_interface(mtd, |
| 1235 | NAND_DATA_IFACE_CHECK_ONLY, |
| 1236 | chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1237 | if (!ret) { |
| 1238 | chip->onfi_timing_mode_default = mode; |
| 1239 | break; |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | static void nand_release_data_interface(struct nand_chip *chip) |
| 1247 | { |
| 1248 | kfree(chip->data_interface); |
| 1249 | } |
| 1250 | |
| 1251 | /** |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1252 | * nand_read_page_op - Do a READ PAGE operation |
| 1253 | * @chip: The NAND chip |
| 1254 | * @page: page to read |
| 1255 | * @offset_in_page: offset within the page |
| 1256 | * @buf: buffer used to store the data |
| 1257 | * @len: length of the buffer |
| 1258 | * |
| 1259 | * This function issues a READ PAGE operation. |
| 1260 | * This function does not select/unselect the CS line. |
| 1261 | * |
| 1262 | * Returns 0 on success, a negative error code otherwise. |
| 1263 | */ |
| 1264 | int nand_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1265 | unsigned int offset_in_page, void *buf, unsigned int len) |
| 1266 | { |
| 1267 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1268 | |
| 1269 | if (len && !buf) |
| 1270 | return -EINVAL; |
| 1271 | |
| 1272 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1273 | return -EINVAL; |
| 1274 | |
| 1275 | chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page); |
| 1276 | if (len) |
| 1277 | chip->read_buf(mtd, buf, len); |
| 1278 | |
| 1279 | return 0; |
| 1280 | } |
| 1281 | EXPORT_SYMBOL_GPL(nand_read_page_op); |
| 1282 | |
| 1283 | /** |
| 1284 | * nand_read_param_page_op - Do a READ PARAMETER PAGE operation |
| 1285 | * @chip: The NAND chip |
| 1286 | * @page: parameter page to read |
| 1287 | * @buf: buffer used to store the data |
| 1288 | * @len: length of the buffer |
| 1289 | * |
| 1290 | * This function issues a READ PARAMETER PAGE operation. |
| 1291 | * This function does not select/unselect the CS line. |
| 1292 | * |
| 1293 | * Returns 0 on success, a negative error code otherwise. |
| 1294 | */ |
| 1295 | static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf, |
| 1296 | unsigned int len) |
| 1297 | { |
| 1298 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1299 | unsigned int i; |
| 1300 | u8 *p = buf; |
| 1301 | |
| 1302 | if (len && !buf) |
| 1303 | return -EINVAL; |
| 1304 | |
| 1305 | chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1); |
| 1306 | for (i = 0; i < len; i++) |
| 1307 | p[i] = chip->read_byte(mtd); |
| 1308 | |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | /** |
| 1313 | * nand_change_read_column_op - Do a CHANGE READ COLUMN operation |
| 1314 | * @chip: The NAND chip |
| 1315 | * @offset_in_page: offset within the page |
| 1316 | * @buf: buffer used to store the data |
| 1317 | * @len: length of the buffer |
| 1318 | * @force_8bit: force 8-bit bus access |
| 1319 | * |
| 1320 | * This function issues a CHANGE READ COLUMN operation. |
| 1321 | * This function does not select/unselect the CS line. |
| 1322 | * |
| 1323 | * Returns 0 on success, a negative error code otherwise. |
| 1324 | */ |
| 1325 | int nand_change_read_column_op(struct nand_chip *chip, |
| 1326 | unsigned int offset_in_page, void *buf, |
| 1327 | unsigned int len, bool force_8bit) |
| 1328 | { |
| 1329 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1330 | |
| 1331 | if (len && !buf) |
| 1332 | return -EINVAL; |
| 1333 | |
| 1334 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1335 | return -EINVAL; |
| 1336 | |
| 1337 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1); |
| 1338 | if (len) |
| 1339 | chip->read_buf(mtd, buf, len); |
| 1340 | |
| 1341 | return 0; |
| 1342 | } |
| 1343 | EXPORT_SYMBOL_GPL(nand_change_read_column_op); |
| 1344 | |
| 1345 | /** |
| 1346 | * nand_read_oob_op - Do a READ OOB operation |
| 1347 | * @chip: The NAND chip |
| 1348 | * @page: page to read |
| 1349 | * @offset_in_oob: offset within the OOB area |
| 1350 | * @buf: buffer used to store the data |
| 1351 | * @len: length of the buffer |
| 1352 | * |
| 1353 | * This function issues a READ OOB operation. |
| 1354 | * This function does not select/unselect the CS line. |
| 1355 | * |
| 1356 | * Returns 0 on success, a negative error code otherwise. |
| 1357 | */ |
| 1358 | int nand_read_oob_op(struct nand_chip *chip, unsigned int page, |
| 1359 | unsigned int offset_in_oob, void *buf, unsigned int len) |
| 1360 | { |
| 1361 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1362 | |
| 1363 | if (len && !buf) |
| 1364 | return -EINVAL; |
| 1365 | |
| 1366 | if (offset_in_oob + len > mtd->oobsize) |
| 1367 | return -EINVAL; |
| 1368 | |
| 1369 | chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page); |
| 1370 | if (len) |
| 1371 | chip->read_buf(mtd, buf, len); |
| 1372 | |
| 1373 | return 0; |
| 1374 | } |
| 1375 | EXPORT_SYMBOL_GPL(nand_read_oob_op); |
| 1376 | |
| 1377 | /** |
| 1378 | * nand_prog_page_begin_op - starts a PROG PAGE operation |
| 1379 | * @chip: The NAND chip |
| 1380 | * @page: page to write |
| 1381 | * @offset_in_page: offset within the page |
| 1382 | * @buf: buffer containing the data to write to the page |
| 1383 | * @len: length of the buffer |
| 1384 | * |
| 1385 | * This function issues the first half of a PROG PAGE operation. |
| 1386 | * This function does not select/unselect the CS line. |
| 1387 | * |
| 1388 | * Returns 0 on success, a negative error code otherwise. |
| 1389 | */ |
| 1390 | int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page, |
| 1391 | unsigned int offset_in_page, const void *buf, |
| 1392 | unsigned int len) |
| 1393 | { |
| 1394 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1395 | |
| 1396 | if (len && !buf) |
| 1397 | return -EINVAL; |
| 1398 | |
| 1399 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1400 | return -EINVAL; |
| 1401 | |
| 1402 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page); |
| 1403 | |
| 1404 | if (buf) |
| 1405 | chip->write_buf(mtd, buf, len); |
| 1406 | |
| 1407 | return 0; |
| 1408 | } |
| 1409 | EXPORT_SYMBOL_GPL(nand_prog_page_begin_op); |
| 1410 | |
| 1411 | /** |
| 1412 | * nand_prog_page_end_op - ends a PROG PAGE operation |
| 1413 | * @chip: The NAND chip |
| 1414 | * |
| 1415 | * This function issues the second half of a PROG PAGE operation. |
| 1416 | * This function does not select/unselect the CS line. |
| 1417 | * |
| 1418 | * Returns 0 on success, a negative error code otherwise. |
| 1419 | */ |
| 1420 | int nand_prog_page_end_op(struct nand_chip *chip) |
| 1421 | { |
| 1422 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1423 | int status; |
| 1424 | |
| 1425 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1426 | |
| 1427 | status = chip->waitfunc(mtd, chip); |
| 1428 | if (status & NAND_STATUS_FAIL) |
| 1429 | return -EIO; |
| 1430 | |
| 1431 | return 0; |
| 1432 | } |
| 1433 | EXPORT_SYMBOL_GPL(nand_prog_page_end_op); |
| 1434 | |
| 1435 | /** |
| 1436 | * nand_prog_page_op - Do a full PROG PAGE operation |
| 1437 | * @chip: The NAND chip |
| 1438 | * @page: page to write |
| 1439 | * @offset_in_page: offset within the page |
| 1440 | * @buf: buffer containing the data to write to the page |
| 1441 | * @len: length of the buffer |
| 1442 | * |
| 1443 | * This function issues a full PROG PAGE operation. |
| 1444 | * This function does not select/unselect the CS line. |
| 1445 | * |
| 1446 | * Returns 0 on success, a negative error code otherwise. |
| 1447 | */ |
| 1448 | int nand_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1449 | unsigned int offset_in_page, const void *buf, |
| 1450 | unsigned int len) |
| 1451 | { |
| 1452 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1453 | int status; |
| 1454 | |
| 1455 | if (!len || !buf) |
| 1456 | return -EINVAL; |
| 1457 | |
| 1458 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1459 | return -EINVAL; |
| 1460 | |
| 1461 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page); |
| 1462 | chip->write_buf(mtd, buf, len); |
| 1463 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1464 | |
| 1465 | status = chip->waitfunc(mtd, chip); |
| 1466 | if (status & NAND_STATUS_FAIL) |
| 1467 | return -EIO; |
| 1468 | |
| 1469 | return 0; |
| 1470 | } |
| 1471 | EXPORT_SYMBOL_GPL(nand_prog_page_op); |
| 1472 | |
| 1473 | /** |
| 1474 | * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation |
| 1475 | * @chip: The NAND chip |
| 1476 | * @offset_in_page: offset within the page |
| 1477 | * @buf: buffer containing the data to send to the NAND |
| 1478 | * @len: length of the buffer |
| 1479 | * @force_8bit: force 8-bit bus access |
| 1480 | * |
| 1481 | * This function issues a CHANGE WRITE COLUMN operation. |
| 1482 | * This function does not select/unselect the CS line. |
| 1483 | * |
| 1484 | * Returns 0 on success, a negative error code otherwise. |
| 1485 | */ |
| 1486 | int nand_change_write_column_op(struct nand_chip *chip, |
| 1487 | unsigned int offset_in_page, |
| 1488 | const void *buf, unsigned int len, |
| 1489 | bool force_8bit) |
| 1490 | { |
| 1491 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1492 | |
| 1493 | if (len && !buf) |
| 1494 | return -EINVAL; |
| 1495 | |
| 1496 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1497 | return -EINVAL; |
| 1498 | |
| 1499 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1); |
| 1500 | if (len) |
| 1501 | chip->write_buf(mtd, buf, len); |
| 1502 | |
| 1503 | return 0; |
| 1504 | } |
| 1505 | EXPORT_SYMBOL_GPL(nand_change_write_column_op); |
| 1506 | |
| 1507 | /** |
| 1508 | * nand_readid_op - Do a READID operation |
| 1509 | * @chip: The NAND chip |
| 1510 | * @addr: address cycle to pass after the READID command |
| 1511 | * @buf: buffer used to store the ID |
| 1512 | * @len: length of the buffer |
| 1513 | * |
| 1514 | * This function sends a READID command and reads back the ID returned by the |
| 1515 | * NAND. |
| 1516 | * This function does not select/unselect the CS line. |
| 1517 | * |
| 1518 | * Returns 0 on success, a negative error code otherwise. |
| 1519 | */ |
| 1520 | int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf, |
| 1521 | unsigned int len) |
| 1522 | { |
| 1523 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1524 | unsigned int i; |
| 1525 | u8 *id = buf; |
| 1526 | |
| 1527 | if (len && !buf) |
| 1528 | return -EINVAL; |
| 1529 | |
| 1530 | chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1); |
| 1531 | |
| 1532 | for (i = 0; i < len; i++) |
| 1533 | id[i] = chip->read_byte(mtd); |
| 1534 | |
| 1535 | return 0; |
| 1536 | } |
| 1537 | EXPORT_SYMBOL_GPL(nand_readid_op); |
| 1538 | |
| 1539 | /** |
| 1540 | * nand_status_op - Do a STATUS operation |
| 1541 | * @chip: The NAND chip |
| 1542 | * @status: out variable to store the NAND status |
| 1543 | * |
| 1544 | * This function sends a STATUS command and reads back the status returned by |
| 1545 | * the NAND. |
| 1546 | * This function does not select/unselect the CS line. |
| 1547 | * |
| 1548 | * Returns 0 on success, a negative error code otherwise. |
| 1549 | */ |
| 1550 | int nand_status_op(struct nand_chip *chip, u8 *status) |
| 1551 | { |
| 1552 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1553 | |
| 1554 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 1555 | if (status) |
| 1556 | *status = chip->read_byte(mtd); |
| 1557 | |
| 1558 | return 0; |
| 1559 | } |
| 1560 | EXPORT_SYMBOL_GPL(nand_status_op); |
| 1561 | |
| 1562 | /** |
| 1563 | * nand_exit_status_op - Exit a STATUS operation |
| 1564 | * @chip: The NAND chip |
| 1565 | * |
| 1566 | * This function sends a READ0 command to cancel the effect of the STATUS |
| 1567 | * command to avoid reading only the status until a new read command is sent. |
| 1568 | * |
| 1569 | * This function does not select/unselect the CS line. |
| 1570 | * |
| 1571 | * Returns 0 on success, a negative error code otherwise. |
| 1572 | */ |
| 1573 | int nand_exit_status_op(struct nand_chip *chip) |
| 1574 | { |
| 1575 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1576 | |
| 1577 | chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1); |
| 1578 | |
| 1579 | return 0; |
| 1580 | } |
| 1581 | EXPORT_SYMBOL_GPL(nand_exit_status_op); |
| 1582 | |
| 1583 | /** |
| 1584 | * nand_erase_op - Do an erase operation |
| 1585 | * @chip: The NAND chip |
| 1586 | * @eraseblock: block to erase |
| 1587 | * |
| 1588 | * This function sends an ERASE command and waits for the NAND to be ready |
| 1589 | * before returning. |
| 1590 | * This function does not select/unselect the CS line. |
| 1591 | * |
| 1592 | * Returns 0 on success, a negative error code otherwise. |
| 1593 | */ |
| 1594 | int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock) |
| 1595 | { |
| 1596 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1597 | unsigned int page = eraseblock << |
| 1598 | (chip->phys_erase_shift - chip->page_shift); |
| 1599 | int status; |
| 1600 | |
| 1601 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 1602 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
| 1603 | |
| 1604 | status = chip->waitfunc(mtd, chip); |
| 1605 | if (status < 0) |
| 1606 | return status; |
| 1607 | |
| 1608 | if (status & NAND_STATUS_FAIL) |
| 1609 | return -EIO; |
| 1610 | |
| 1611 | return 0; |
| 1612 | } |
| 1613 | EXPORT_SYMBOL_GPL(nand_erase_op); |
| 1614 | |
| 1615 | /** |
| 1616 | * nand_set_features_op - Do a SET FEATURES operation |
| 1617 | * @chip: The NAND chip |
| 1618 | * @feature: feature id |
| 1619 | * @data: 4 bytes of data |
| 1620 | * |
| 1621 | * This function sends a SET FEATURES command and waits for the NAND to be |
| 1622 | * ready before returning. |
| 1623 | * This function does not select/unselect the CS line. |
| 1624 | * |
| 1625 | * Returns 0 on success, a negative error code otherwise. |
| 1626 | */ |
| 1627 | static int nand_set_features_op(struct nand_chip *chip, u8 feature, |
| 1628 | const void *data) |
| 1629 | { |
| 1630 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1631 | const u8 *params = data; |
| 1632 | int i, status; |
| 1633 | |
| 1634 | chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1); |
| 1635 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 1636 | chip->write_byte(mtd, params[i]); |
| 1637 | |
| 1638 | status = chip->waitfunc(mtd, chip); |
| 1639 | if (status & NAND_STATUS_FAIL) |
| 1640 | return -EIO; |
| 1641 | |
| 1642 | return 0; |
| 1643 | } |
| 1644 | |
| 1645 | /** |
| 1646 | * nand_get_features_op - Do a GET FEATURES operation |
| 1647 | * @chip: The NAND chip |
| 1648 | * @feature: feature id |
| 1649 | * @data: 4 bytes of data |
| 1650 | * |
| 1651 | * This function sends a GET FEATURES command and waits for the NAND to be |
| 1652 | * ready before returning. |
| 1653 | * This function does not select/unselect the CS line. |
| 1654 | * |
| 1655 | * Returns 0 on success, a negative error code otherwise. |
| 1656 | */ |
| 1657 | static int nand_get_features_op(struct nand_chip *chip, u8 feature, |
| 1658 | void *data) |
| 1659 | { |
| 1660 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1661 | u8 *params = data; |
| 1662 | int i; |
| 1663 | |
| 1664 | chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1); |
| 1665 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 1666 | params[i] = chip->read_byte(mtd); |
| 1667 | |
| 1668 | return 0; |
| 1669 | } |
| 1670 | |
| 1671 | /** |
| 1672 | * nand_reset_op - Do a reset operation |
| 1673 | * @chip: The NAND chip |
| 1674 | * |
| 1675 | * This function sends a RESET command and waits for the NAND to be ready |
| 1676 | * before returning. |
| 1677 | * This function does not select/unselect the CS line. |
| 1678 | * |
| 1679 | * Returns 0 on success, a negative error code otherwise. |
| 1680 | */ |
| 1681 | int nand_reset_op(struct nand_chip *chip) |
| 1682 | { |
| 1683 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1684 | |
| 1685 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 1686 | |
| 1687 | return 0; |
| 1688 | } |
| 1689 | EXPORT_SYMBOL_GPL(nand_reset_op); |
| 1690 | |
| 1691 | /** |
| 1692 | * nand_read_data_op - Read data from the NAND |
| 1693 | * @chip: The NAND chip |
| 1694 | * @buf: buffer used to store the data |
| 1695 | * @len: length of the buffer |
| 1696 | * @force_8bit: force 8-bit bus access |
| 1697 | * |
| 1698 | * This function does a raw data read on the bus. Usually used after launching |
| 1699 | * another NAND operation like nand_read_page_op(). |
| 1700 | * This function does not select/unselect the CS line. |
| 1701 | * |
| 1702 | * Returns 0 on success, a negative error code otherwise. |
| 1703 | */ |
| 1704 | int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len, |
| 1705 | bool force_8bit) |
| 1706 | { |
| 1707 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1708 | |
| 1709 | if (!len || !buf) |
| 1710 | return -EINVAL; |
| 1711 | |
| 1712 | if (force_8bit) { |
| 1713 | u8 *p = buf; |
| 1714 | unsigned int i; |
| 1715 | |
| 1716 | for (i = 0; i < len; i++) |
| 1717 | p[i] = chip->read_byte(mtd); |
| 1718 | } else { |
| 1719 | chip->read_buf(mtd, buf, len); |
| 1720 | } |
| 1721 | |
| 1722 | return 0; |
| 1723 | } |
| 1724 | EXPORT_SYMBOL_GPL(nand_read_data_op); |
| 1725 | |
| 1726 | /** |
| 1727 | * nand_write_data_op - Write data from the NAND |
| 1728 | * @chip: The NAND chip |
| 1729 | * @buf: buffer containing the data to send on the bus |
| 1730 | * @len: length of the buffer |
| 1731 | * @force_8bit: force 8-bit bus access |
| 1732 | * |
| 1733 | * This function does a raw data write on the bus. Usually used after launching |
| 1734 | * another NAND operation like nand_write_page_begin_op(). |
| 1735 | * This function does not select/unselect the CS line. |
| 1736 | * |
| 1737 | * Returns 0 on success, a negative error code otherwise. |
| 1738 | */ |
| 1739 | int nand_write_data_op(struct nand_chip *chip, const void *buf, |
| 1740 | unsigned int len, bool force_8bit) |
| 1741 | { |
| 1742 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1743 | |
| 1744 | if (!len || !buf) |
| 1745 | return -EINVAL; |
| 1746 | |
| 1747 | if (force_8bit) { |
| 1748 | const u8 *p = buf; |
| 1749 | unsigned int i; |
| 1750 | |
| 1751 | for (i = 0; i < len; i++) |
| 1752 | chip->write_byte(mtd, p[i]); |
| 1753 | } else { |
| 1754 | chip->write_buf(mtd, buf, len); |
| 1755 | } |
| 1756 | |
| 1757 | return 0; |
| 1758 | } |
| 1759 | EXPORT_SYMBOL_GPL(nand_write_data_op); |
| 1760 | |
| 1761 | /** |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1762 | * nand_reset - Reset and initialize a NAND device |
| 1763 | * @chip: The NAND chip |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1764 | * @chipnr: Internal die id |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1765 | * |
| 1766 | * Returns 0 for success or negative error code otherwise |
| 1767 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1768 | int nand_reset(struct nand_chip *chip, int chipnr) |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1769 | { |
| 1770 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1771 | int ret; |
| 1772 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1773 | ret = nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1774 | if (ret) |
| 1775 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1776 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1777 | /* |
| 1778 | * The CS line has to be released before we can apply the new NAND |
| 1779 | * interface settings, hence this weird ->select_chip() dance. |
| 1780 | */ |
| 1781 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1782 | ret = nand_reset_op(chip); |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1783 | chip->select_chip(mtd, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1784 | if (ret) |
| 1785 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1786 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1787 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1788 | ret = nand_setup_data_interface(chip, chipnr); |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1789 | chip->select_chip(mtd, -1); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1790 | if (ret) |
| 1791 | return ret; |
| 1792 | |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1793 | return 0; |
| 1794 | } |
Boris Brezillon | b9bb984 | 2017-10-05 18:53:19 +0200 | [diff] [blame] | 1795 | EXPORT_SYMBOL_GPL(nand_reset); |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1796 | |
| 1797 | /** |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 1798 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 1799 | * @buf: buffer to test |
| 1800 | * @len: buffer length |
| 1801 | * @bitflips_threshold: maximum number of bitflips |
| 1802 | * |
| 1803 | * Check if a buffer contains only 0xff, which means the underlying region |
| 1804 | * has been erased and is ready to be programmed. |
| 1805 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1806 | * considering the region is not erased. |
| 1807 | * Note: The logic of this function has been extracted from the memweight |
| 1808 | * implementation, except that nand_check_erased_buf function exit before |
| 1809 | * testing the whole buffer if the number of bitflips exceed the |
| 1810 | * bitflips_threshold value. |
| 1811 | * |
| 1812 | * Returns a positive number of bitflips less than or equal to |
| 1813 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1814 | * threshold. |
| 1815 | */ |
| 1816 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 1817 | { |
| 1818 | const unsigned char *bitmap = buf; |
| 1819 | int bitflips = 0; |
| 1820 | int weight; |
| 1821 | |
| 1822 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 1823 | len--, bitmap++) { |
| 1824 | weight = hweight8(*bitmap); |
| 1825 | bitflips += BITS_PER_BYTE - weight; |
| 1826 | if (unlikely(bitflips > bitflips_threshold)) |
| 1827 | return -EBADMSG; |
| 1828 | } |
| 1829 | |
| 1830 | for (; len >= sizeof(long); |
| 1831 | len -= sizeof(long), bitmap += sizeof(long)) { |
Pavel Machek | 086567f | 2017-04-21 12:51:07 +0200 | [diff] [blame] | 1832 | unsigned long d = *((unsigned long *)bitmap); |
| 1833 | if (d == ~0UL) |
| 1834 | continue; |
| 1835 | weight = hweight_long(d); |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 1836 | bitflips += BITS_PER_LONG - weight; |
| 1837 | if (unlikely(bitflips > bitflips_threshold)) |
| 1838 | return -EBADMSG; |
| 1839 | } |
| 1840 | |
| 1841 | for (; len > 0; len--, bitmap++) { |
| 1842 | weight = hweight8(*bitmap); |
| 1843 | bitflips += BITS_PER_BYTE - weight; |
| 1844 | if (unlikely(bitflips > bitflips_threshold)) |
| 1845 | return -EBADMSG; |
| 1846 | } |
| 1847 | |
| 1848 | return bitflips; |
| 1849 | } |
| 1850 | |
| 1851 | /** |
| 1852 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 1853 | * 0xff data |
| 1854 | * @data: data buffer to test |
| 1855 | * @datalen: data length |
| 1856 | * @ecc: ECC buffer |
| 1857 | * @ecclen: ECC length |
| 1858 | * @extraoob: extra OOB buffer |
| 1859 | * @extraooblen: extra OOB length |
| 1860 | * @bitflips_threshold: maximum number of bitflips |
| 1861 | * |
| 1862 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 1863 | * 0xff pattern, which means the underlying region has been erased and is |
| 1864 | * ready to be programmed. |
| 1865 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1866 | * considering the region as not erased. |
| 1867 | * |
| 1868 | * Note: |
| 1869 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 1870 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 1871 | * report the number of errors per chunk, and the NAND core infrastructure |
| 1872 | * expect you to return the maximum number of bitflips for the whole page. |
| 1873 | * This is why you should always use this function on a single chunk and |
| 1874 | * not on the whole page. After checking each chunk you should update your |
| 1875 | * max_bitflips value accordingly. |
| 1876 | * 2/ When checking for bitflips in erased pages you should not only check |
| 1877 | * the payload data but also their associated ECC data, because a user might |
| 1878 | * have programmed almost all bits to 1 but a few. In this case, we |
| 1879 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 1880 | * this case. |
| 1881 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 1882 | * data are protected by the ECC engine. |
| 1883 | * It could also be used if you support subpages and want to attach some |
| 1884 | * extra OOB data to an ECC chunk. |
| 1885 | * |
| 1886 | * Returns a positive number of bitflips less than or equal to |
| 1887 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1888 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 1889 | */ |
| 1890 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 1891 | void *ecc, int ecclen, |
| 1892 | void *extraoob, int extraooblen, |
| 1893 | int bitflips_threshold) |
| 1894 | { |
| 1895 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 1896 | |
| 1897 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 1898 | bitflips_threshold); |
| 1899 | if (data_bitflips < 0) |
| 1900 | return data_bitflips; |
| 1901 | |
| 1902 | bitflips_threshold -= data_bitflips; |
| 1903 | |
| 1904 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 1905 | if (ecc_bitflips < 0) |
| 1906 | return ecc_bitflips; |
| 1907 | |
| 1908 | bitflips_threshold -= ecc_bitflips; |
| 1909 | |
| 1910 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 1911 | bitflips_threshold); |
| 1912 | if (extraoob_bitflips < 0) |
| 1913 | return extraoob_bitflips; |
| 1914 | |
| 1915 | if (data_bitflips) |
| 1916 | memset(data, 0xff, datalen); |
| 1917 | |
| 1918 | if (ecc_bitflips) |
| 1919 | memset(ecc, 0xff, ecclen); |
| 1920 | |
| 1921 | if (extraoob_bitflips) |
| 1922 | memset(extraoob, 0xff, extraooblen); |
| 1923 | |
| 1924 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 1925 | } |
| 1926 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
| 1927 | |
| 1928 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1929 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1930 | * @mtd: mtd info structure |
| 1931 | * @chip: nand chip info structure |
| 1932 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1933 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1934 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1935 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1936 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1937 | */ |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 1938 | int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 1939 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1940 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1941 | int ret; |
| 1942 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame^] | 1943 | ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1944 | if (ret) |
| 1945 | return ret; |
| 1946 | |
| 1947 | if (oob_required) { |
| 1948 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 1949 | false); |
| 1950 | if (ret) |
| 1951 | return ret; |
| 1952 | } |
| 1953 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1954 | return 0; |
| 1955 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 1956 | EXPORT_SYMBOL(nand_read_page_raw); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1957 | |
| 1958 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1959 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1960 | * @mtd: mtd info structure |
| 1961 | * @chip: nand chip info structure |
| 1962 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1963 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1964 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1965 | * |
| 1966 | * We need a special oob layout and handling even when OOB isn't used. |
| 1967 | */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1968 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1969 | struct nand_chip *chip, uint8_t *buf, |
| 1970 | int oob_required, int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1971 | { |
| 1972 | int eccsize = chip->ecc.size; |
| 1973 | int eccbytes = chip->ecc.bytes; |
| 1974 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1975 | int steps, size, ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1976 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame^] | 1977 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 1978 | if (ret) |
| 1979 | return ret; |
| 1980 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1981 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1982 | ret = nand_read_data_op(chip, buf, eccsize, false); |
| 1983 | if (ret) |
| 1984 | return ret; |
| 1985 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1986 | buf += eccsize; |
| 1987 | |
| 1988 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1989 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 1990 | false); |
| 1991 | if (ret) |
| 1992 | return ret; |
| 1993 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1994 | oob += chip->ecc.prepad; |
| 1995 | } |
| 1996 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1997 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 1998 | if (ret) |
| 1999 | return ret; |
| 2000 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2001 | oob += eccbytes; |
| 2002 | |
| 2003 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2004 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 2005 | false); |
| 2006 | if (ret) |
| 2007 | return ret; |
| 2008 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2009 | oob += chip->ecc.postpad; |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2014 | if (size) { |
| 2015 | ret = nand_read_data_op(chip, oob, size, false); |
| 2016 | if (ret) |
| 2017 | return ret; |
| 2018 | } |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2019 | |
| 2020 | return 0; |
| 2021 | } |
| 2022 | |
| 2023 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2024 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2025 | * @mtd: mtd info structure |
| 2026 | * @chip: nand chip info structure |
| 2027 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2028 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2029 | * @page: page number to read |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 2030 | */ |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2031 | 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] | 2032 | uint8_t *buf, int oob_required, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2034 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2035 | int eccbytes = chip->ecc.bytes; |
| 2036 | int eccsteps = chip->ecc.steps; |
| 2037 | uint8_t *p = buf; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2038 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2039 | uint8_t *ecc_code = chip->buffers->ecccode; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2040 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2041 | |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2042 | chip->ecc.read_page_raw(mtd, chip, buf, 1, page); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2043 | |
| 2044 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 2045 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2046 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2047 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 2048 | chip->ecc.total); |
| 2049 | if (ret) |
| 2050 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2051 | |
| 2052 | eccsteps = chip->ecc.steps; |
| 2053 | p = buf; |
| 2054 | |
| 2055 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2056 | int stat; |
| 2057 | |
| 2058 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2059 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2060 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2061 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2062 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2063 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2064 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2065 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2066 | return max_bitflips; |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 2067 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | /** |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2070 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2071 | * @mtd: mtd info structure |
| 2072 | * @chip: nand chip info structure |
| 2073 | * @data_offs: offset of requested data within the page |
| 2074 | * @readlen: data length |
| 2075 | * @bufpoi: buffer to store read data |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 2076 | * @page: page number to read |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2077 | */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 2078 | 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] | 2079 | uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi, |
| 2080 | int page) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2081 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2082 | int start_step, end_step, num_steps, ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2083 | uint8_t *p; |
| 2084 | int data_col_addr, i, gaps = 0; |
| 2085 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 2086 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2087 | int index, section = 0; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2088 | unsigned int max_bitflips = 0; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2089 | struct mtd_oob_region oobregion = { }; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2090 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2091 | /* Column address within the page aligned to ECC size (256bytes) */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2092 | start_step = data_offs / chip->ecc.size; |
| 2093 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 2094 | num_steps = end_step - start_step + 1; |
Ron | 4a4163ca | 2014-03-16 04:01:07 +1030 | [diff] [blame] | 2095 | index = start_step * chip->ecc.bytes; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2096 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2097 | /* Data size aligned to ECC ecc.size */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2098 | datafrag_len = num_steps * chip->ecc.size; |
| 2099 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 2100 | |
| 2101 | data_col_addr = start_step * chip->ecc.size; |
| 2102 | /* If we read not a page aligned data */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2103 | p = bufpoi + data_col_addr; |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame^] | 2104 | ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2105 | if (ret) |
| 2106 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2107 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2108 | /* Calculate ECC */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2109 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
| 2110 | chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); |
| 2111 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2112 | /* |
| 2113 | * The performance is faster if we position offsets according to |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2114 | * 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] | 2115 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2116 | ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion); |
| 2117 | if (ret) |
| 2118 | return ret; |
| 2119 | |
| 2120 | if (oobregion.length < eccfrag_len) |
| 2121 | gaps = 1; |
| 2122 | |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2123 | if (gaps) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2124 | ret = nand_change_read_column_op(chip, mtd->writesize, |
| 2125 | chip->oob_poi, mtd->oobsize, |
| 2126 | false); |
| 2127 | if (ret) |
| 2128 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2129 | } else { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2130 | /* |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2131 | * Send the command to read the particular ECC bytes take care |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2132 | * about buswidth alignment in read_buf. |
| 2133 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2134 | aligned_pos = oobregion.offset & ~(busw - 1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2135 | aligned_len = eccfrag_len; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2136 | if (oobregion.offset & (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2137 | aligned_len++; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2138 | if ((oobregion.offset + (num_steps * chip->ecc.bytes)) & |
| 2139 | (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2140 | aligned_len++; |
| 2141 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2142 | ret = nand_change_read_column_op(chip, |
| 2143 | mtd->writesize + aligned_pos, |
| 2144 | &chip->oob_poi[aligned_pos], |
| 2145 | aligned_len, false); |
| 2146 | if (ret) |
| 2147 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2148 | } |
| 2149 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2150 | ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode, |
| 2151 | chip->oob_poi, index, eccfrag_len); |
| 2152 | if (ret) |
| 2153 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2154 | |
| 2155 | p = bufpoi + data_col_addr; |
| 2156 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 2157 | int stat; |
| 2158 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 2159 | stat = chip->ecc.correct(mtd, p, |
| 2160 | &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2161 | if (stat == -EBADMSG && |
| 2162 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2163 | /* check for empty pages with bitflips */ |
| 2164 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 2165 | &chip->buffers->ecccode[i], |
| 2166 | chip->ecc.bytes, |
| 2167 | NULL, 0, |
| 2168 | chip->ecc.strength); |
| 2169 | } |
| 2170 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2171 | if (stat < 0) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2172 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2173 | } else { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2174 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2175 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2176 | } |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2177 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2178 | return max_bitflips; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2182 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2183 | * @mtd: mtd info structure |
| 2184 | * @chip: nand chip info structure |
| 2185 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2186 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2187 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2188 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2189 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2190 | */ |
| 2191 | 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] | 2192 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2193 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2194 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2195 | int eccbytes = chip->ecc.bytes; |
| 2196 | int eccsteps = chip->ecc.steps; |
| 2197 | uint8_t *p = buf; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2198 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2199 | uint8_t *ecc_code = chip->buffers->ecccode; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2200 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2201 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame^] | 2202 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2203 | if (ret) |
| 2204 | return ret; |
| 2205 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2206 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2207 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2208 | |
| 2209 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2210 | if (ret) |
| 2211 | return ret; |
| 2212 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2213 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2214 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2215 | |
| 2216 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 2217 | if (ret) |
| 2218 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2219 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2220 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 2221 | chip->ecc.total); |
| 2222 | if (ret) |
| 2223 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2224 | |
| 2225 | eccsteps = chip->ecc.steps; |
| 2226 | p = buf; |
| 2227 | |
| 2228 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2229 | int stat; |
| 2230 | |
| 2231 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2232 | if (stat == -EBADMSG && |
| 2233 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2234 | /* check for empty pages with bitflips */ |
| 2235 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2236 | &ecc_code[i], eccbytes, |
| 2237 | NULL, 0, |
| 2238 | chip->ecc.strength); |
| 2239 | } |
| 2240 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2241 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2242 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2243 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2244 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2245 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2246 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2247 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2248 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2252 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2253 | * @mtd: mtd info structure |
| 2254 | * @chip: nand chip info structure |
| 2255 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2256 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2257 | * @page: page number to read |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2258 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2259 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 2260 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 2261 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 2262 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 2263 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2264 | */ |
| 2265 | static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2266 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2267 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2268 | int i, eccsize = chip->ecc.size, ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2269 | int eccbytes = chip->ecc.bytes; |
| 2270 | int eccsteps = chip->ecc.steps; |
| 2271 | uint8_t *p = buf; |
| 2272 | uint8_t *ecc_code = chip->buffers->ecccode; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2273 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2274 | unsigned int max_bitflips = 0; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2275 | |
| 2276 | /* Read the OOB area first */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2277 | ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
| 2278 | if (ret) |
| 2279 | return ret; |
| 2280 | |
| 2281 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2282 | if (ret) |
| 2283 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2284 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2285 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 2286 | chip->ecc.total); |
| 2287 | if (ret) |
| 2288 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2289 | |
| 2290 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2291 | int stat; |
| 2292 | |
| 2293 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2294 | |
| 2295 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2296 | if (ret) |
| 2297 | return ret; |
| 2298 | |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2299 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2300 | |
| 2301 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2302 | if (stat == -EBADMSG && |
| 2303 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2304 | /* check for empty pages with bitflips */ |
| 2305 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2306 | &ecc_code[i], eccbytes, |
| 2307 | NULL, 0, |
| 2308 | chip->ecc.strength); |
| 2309 | } |
| 2310 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2311 | if (stat < 0) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2312 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2313 | } else { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2314 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2315 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2316 | } |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2317 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2318 | return max_bitflips; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2322 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2323 | * @mtd: mtd info structure |
| 2324 | * @chip: nand chip info structure |
| 2325 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2326 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2327 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2328 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2329 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 2330 | * need a special oob layout and handling. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2331 | */ |
| 2332 | 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] | 2333 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2334 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2335 | int ret, i, eccsize = chip->ecc.size; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2336 | int eccbytes = chip->ecc.bytes; |
| 2337 | int eccsteps = chip->ecc.steps; |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2338 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2339 | uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2340 | uint8_t *oob = chip->oob_poi; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2341 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2342 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame^] | 2343 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2344 | if (ret) |
| 2345 | return ret; |
| 2346 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2347 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2348 | int stat; |
| 2349 | |
| 2350 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2351 | |
| 2352 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2353 | if (ret) |
| 2354 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2355 | |
| 2356 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2357 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 2358 | false); |
| 2359 | if (ret) |
| 2360 | return ret; |
| 2361 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2362 | oob += chip->ecc.prepad; |
| 2363 | } |
| 2364 | |
| 2365 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2366 | |
| 2367 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 2368 | if (ret) |
| 2369 | return ret; |
| 2370 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2371 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
| 2372 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2373 | oob += eccbytes; |
| 2374 | |
| 2375 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2376 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 2377 | false); |
| 2378 | if (ret) |
| 2379 | return ret; |
| 2380 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2381 | oob += chip->ecc.postpad; |
| 2382 | } |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2383 | |
| 2384 | if (stat == -EBADMSG && |
| 2385 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2386 | /* check for empty pages with bitflips */ |
| 2387 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 2388 | oob - eccpadbytes, |
| 2389 | eccpadbytes, |
| 2390 | NULL, 0, |
| 2391 | chip->ecc.strength); |
| 2392 | } |
| 2393 | |
| 2394 | if (stat < 0) { |
| 2395 | mtd->ecc_stats.failed++; |
| 2396 | } else { |
| 2397 | mtd->ecc_stats.corrected += stat; |
| 2398 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2399 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 2403 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2404 | if (i) { |
| 2405 | ret = nand_read_data_op(chip, oob, i, false); |
| 2406 | if (ret) |
| 2407 | return ret; |
| 2408 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2409 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2410 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2414 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2415 | * @mtd: mtd info structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2416 | * @oob: oob destination address |
| 2417 | * @ops: oob ops structure |
| 2418 | * @len: size of oob to transfer |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2419 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2420 | 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] | 2421 | struct mtd_oob_ops *ops, size_t len) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2422 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2423 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 2424 | int ret; |
| 2425 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2426 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2427 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2428 | case MTD_OPS_PLACE_OOB: |
| 2429 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2430 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 2431 | return oob + len; |
| 2432 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2433 | case MTD_OPS_AUTO_OOB: |
| 2434 | ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi, |
| 2435 | ops->ooboffs, len); |
| 2436 | BUG_ON(ret); |
| 2437 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2438 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2439 | default: |
| 2440 | BUG(); |
| 2441 | } |
| 2442 | return NULL; |
| 2443 | } |
| 2444 | |
| 2445 | /** |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2446 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
| 2447 | * @mtd: MTD device structure |
| 2448 | * @retry_mode: the retry mode to use |
| 2449 | * |
| 2450 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 2451 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 2452 | * a new threshold, the host should retry reading the page. |
| 2453 | */ |
| 2454 | static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode) |
| 2455 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2456 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2457 | |
| 2458 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 2459 | |
| 2460 | if (retry_mode >= chip->read_retries) |
| 2461 | return -EINVAL; |
| 2462 | |
| 2463 | if (!chip->setup_read_retry) |
| 2464 | return -EOPNOTSUPP; |
| 2465 | |
| 2466 | return chip->setup_read_retry(mtd, retry_mode); |
| 2467 | } |
| 2468 | |
| 2469 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2470 | * nand_do_read_ops - [INTERN] Read data with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2471 | * @mtd: MTD device structure |
| 2472 | * @from: offset to read from |
| 2473 | * @ops: oob ops structure |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 2474 | * |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2475 | * Internal function. Called with chip held. |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 2476 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2477 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 2478 | struct mtd_oob_ops *ops) |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 2479 | { |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 2480 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2481 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2482 | int ret = 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2483 | uint32_t readlen = ops->len; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2484 | uint32_t oobreadlen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 2485 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 9aca334 | 2010-02-22 20:39:35 +0200 | [diff] [blame] | 2486 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2487 | uint8_t *bufpoi, *oob, *buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2488 | int use_bufpoi; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2489 | unsigned int max_bitflips = 0; |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2490 | int retry_mode = 0; |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 2491 | bool ecc_fail = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2493 | chipnr = (int)(from >> chip->chip_shift); |
| 2494 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2496 | realpage = (int)(from >> chip->page_shift); |
| 2497 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2499 | col = (int)(from & (mtd->writesize - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2501 | buf = ops->datbuf; |
| 2502 | oob = ops->oobbuf; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 2503 | oob_required = oob ? 1 : 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2504 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2505 | while (1) { |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 2506 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
| 2507 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2508 | bytes = min(mtd->writesize - col, readlen); |
| 2509 | aligned = (bytes == mtd->writesize); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2510 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2511 | if (!aligned) |
| 2512 | use_bufpoi = 1; |
| 2513 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 2514 | use_bufpoi = !virt_addr_valid(buf) || |
| 2515 | !IS_ALIGNED((unsigned long)buf, |
| 2516 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2517 | else |
| 2518 | use_bufpoi = 0; |
| 2519 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2520 | /* Is the current page in the buffer? */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2521 | if (realpage != chip->pagebuf || oob) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2522 | bufpoi = use_bufpoi ? chip->buffers->databuf : buf; |
| 2523 | |
| 2524 | if (use_bufpoi && aligned) |
| 2525 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 2526 | __func__, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2528 | read_retry: |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2529 | /* |
| 2530 | * Now read the page into the buffer. Absent an error, |
| 2531 | * the read methods return max bitflips per ecc step. |
| 2532 | */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2533 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2534 | ret = chip->ecc.read_page_raw(mtd, chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 2535 | oob_required, |
| 2536 | page); |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 2537 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
| 2538 | !oob) |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 2539 | ret = chip->ecc.read_subpage(mtd, chip, |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 2540 | col, bytes, bufpoi, |
| 2541 | page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 2542 | else |
Sneha Narnakaje | 46a8cf2 | 2009-09-18 12:51:46 -0700 | [diff] [blame] | 2543 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 2544 | oob_required, page); |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 2545 | if (ret < 0) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2546 | if (use_bufpoi) |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 2547 | /* Invalidate page cache */ |
| 2548 | chip->pagebuf = -1; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2549 | break; |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 2550 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2551 | |
| 2552 | /* Transfer not aligned data */ |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2553 | if (use_bufpoi) { |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 2554 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 2555 | !(mtd->ecc_stats.failed - ecc_failures) && |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2556 | (ops->mode != MTD_OPS_RAW)) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2557 | chip->pagebuf = realpage; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2558 | chip->pagebuf_bitflips = ret; |
| 2559 | } else { |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 2560 | /* Invalidate page cache */ |
| 2561 | chip->pagebuf = -1; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2562 | } |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2563 | memcpy(buf, chip->buffers->databuf + col, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2565 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2566 | if (unlikely(oob)) { |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 2567 | int toread = min(oobreadlen, max_oobsize); |
| 2568 | |
| 2569 | if (toread) { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2570 | oob = nand_transfer_oob(mtd, |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 2571 | oob, ops, toread); |
| 2572 | oobreadlen -= toread; |
| 2573 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2574 | } |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 2575 | |
| 2576 | if (chip->options & NAND_NEED_READRDY) { |
| 2577 | /* Apply delay or wait for ready/busy pin */ |
| 2578 | if (!chip->dev_ready) |
| 2579 | udelay(chip->chip_delay); |
| 2580 | else |
| 2581 | nand_wait_ready(mtd); |
| 2582 | } |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 2583 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2584 | if (mtd->ecc_stats.failed - ecc_failures) { |
Brian Norris | 28fa65e | 2014-02-12 16:08:28 -0800 | [diff] [blame] | 2585 | if (retry_mode + 1 < chip->read_retries) { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2586 | retry_mode++; |
| 2587 | ret = nand_setup_read_retry(mtd, |
| 2588 | retry_mode); |
| 2589 | if (ret < 0) |
| 2590 | break; |
| 2591 | |
| 2592 | /* Reset failures; retry */ |
| 2593 | mtd->ecc_stats.failed = ecc_failures; |
| 2594 | goto read_retry; |
| 2595 | } else { |
| 2596 | /* No more retry modes; real failure */ |
| 2597 | ecc_fail = true; |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | buf += bytes; |
Masahiro Yamada | 0760468 | 2017-03-30 15:45:47 +0900 | [diff] [blame] | 2602 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2603 | } else { |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2604 | memcpy(buf, chip->buffers->databuf + col, bytes); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2605 | buf += bytes; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2606 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 2607 | chip->pagebuf_bitflips); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2608 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2610 | readlen -= bytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2611 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2612 | /* Reset to retry mode 0 */ |
| 2613 | if (retry_mode) { |
| 2614 | ret = nand_setup_read_retry(mtd, 0); |
| 2615 | if (ret < 0) |
| 2616 | break; |
| 2617 | retry_mode = 0; |
| 2618 | } |
| 2619 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2620 | if (!readlen) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2621 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2623 | /* For subsequent reads align to page boundary */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | col = 0; |
| 2625 | /* Increment page address */ |
| 2626 | realpage++; |
| 2627 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2628 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | /* Check, if we cross a chip boundary */ |
| 2630 | if (!page) { |
| 2631 | chipnr++; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2632 | chip->select_chip(mtd, -1); |
| 2633 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | } |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2636 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2638 | ops->retlen = ops->len - (size_t) readlen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2639 | if (oob) |
| 2640 | ops->oobretlen = ops->ooblen - oobreadlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2642 | if (ret < 0) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2643 | return ret; |
| 2644 | |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 2645 | if (ecc_fail) |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 2646 | return -EBADMSG; |
| 2647 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2648 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2649 | } |
| 2650 | |
| 2651 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2652 | * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2653 | * @mtd: MTD device structure |
| 2654 | * @from: offset to read from |
| 2655 | * @len: number of bytes to read |
| 2656 | * @retlen: pointer to variable to store the number of read bytes |
| 2657 | * @buf: the databuffer to put data |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2658 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2659 | * Get hold of the chip and call nand_do_read. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2660 | */ |
| 2661 | static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 2662 | size_t *retlen, uint8_t *buf) |
| 2663 | { |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2664 | struct mtd_oob_ops ops; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2665 | int ret; |
| 2666 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 2667 | nand_get_device(mtd, FL_READING); |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 2668 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2669 | ops.len = len; |
| 2670 | ops.datbuf = buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 2671 | ops.mode = MTD_OPS_PLACE_OOB; |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2672 | ret = nand_do_read_ops(mtd, from, &ops); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2673 | *retlen = ops.retlen; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2674 | nand_release_device(mtd); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2675 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | } |
| 2677 | |
| 2678 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2679 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2680 | * @mtd: mtd info structure |
| 2681 | * @chip: nand chip info structure |
| 2682 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2683 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2684 | 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] | 2685 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2686 | return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2687 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2688 | EXPORT_SYMBOL(nand_read_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2689 | |
| 2690 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2691 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2692 | * with syndromes |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2693 | * @mtd: mtd info structure |
| 2694 | * @chip: nand chip info structure |
| 2695 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2696 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2697 | int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 2698 | int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2699 | { |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2700 | int length = mtd->oobsize; |
| 2701 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2702 | int eccsize = chip->ecc.size; |
Baruch Siach | 2ea69d2 | 2015-01-22 15:23:05 +0200 | [diff] [blame] | 2703 | uint8_t *bufpoi = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2704 | int i, toread, sndrnd = 0, pos, ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2705 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2706 | ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0); |
| 2707 | if (ret) |
| 2708 | return ret; |
| 2709 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2710 | for (i = 0; i < chip->ecc.steps; i++) { |
| 2711 | if (sndrnd) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2712 | int ret; |
| 2713 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2714 | pos = eccsize + i * (eccsize + chunk); |
| 2715 | if (mtd->writesize > 512) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2716 | ret = nand_change_read_column_op(chip, pos, |
| 2717 | NULL, 0, |
| 2718 | false); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2719 | else |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2720 | ret = nand_read_page_op(chip, page, pos, NULL, |
| 2721 | 0); |
| 2722 | |
| 2723 | if (ret) |
| 2724 | return ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2725 | } else |
| 2726 | sndrnd = 1; |
| 2727 | toread = min_t(int, length, chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2728 | |
| 2729 | ret = nand_read_data_op(chip, bufpoi, toread, false); |
| 2730 | if (ret) |
| 2731 | return ret; |
| 2732 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2733 | bufpoi += toread; |
| 2734 | length -= toread; |
| 2735 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2736 | if (length > 0) { |
| 2737 | ret = nand_read_data_op(chip, bufpoi, length, false); |
| 2738 | if (ret) |
| 2739 | return ret; |
| 2740 | } |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2741 | |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 2742 | return 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2743 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2744 | EXPORT_SYMBOL(nand_read_oob_syndrome); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2745 | |
| 2746 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2747 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2748 | * @mtd: mtd info structure |
| 2749 | * @chip: nand chip info structure |
| 2750 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2751 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2752 | 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] | 2753 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2754 | return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi, |
| 2755 | mtd->oobsize); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2756 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2757 | EXPORT_SYMBOL(nand_write_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2758 | |
| 2759 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2760 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2761 | * with syndrome - only for large page flash |
| 2762 | * @mtd: mtd info structure |
| 2763 | * @chip: nand chip info structure |
| 2764 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2765 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2766 | int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 2767 | int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2768 | { |
| 2769 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2770 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2771 | int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2772 | const uint8_t *bufpoi = chip->oob_poi; |
| 2773 | |
| 2774 | /* |
| 2775 | * data-ecc-data-ecc ... ecc-oob |
| 2776 | * or |
| 2777 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
| 2778 | */ |
| 2779 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 2780 | pos = steps * (eccsize + chunk); |
| 2781 | steps = 0; |
| 2782 | } else |
Vitaly Wool | 8b0036e | 2006-07-11 09:11:25 +0200 | [diff] [blame] | 2783 | pos = eccsize; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2784 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2785 | ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0); |
| 2786 | if (ret) |
| 2787 | return ret; |
| 2788 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2789 | for (i = 0; i < steps; i++) { |
| 2790 | if (sndcmd) { |
| 2791 | if (mtd->writesize <= 512) { |
| 2792 | uint32_t fill = 0xFFFFFFFF; |
| 2793 | |
| 2794 | len = eccsize; |
| 2795 | while (len > 0) { |
| 2796 | int num = min_t(int, len, 4); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2797 | |
| 2798 | ret = nand_write_data_op(chip, &fill, |
| 2799 | num, false); |
| 2800 | if (ret) |
| 2801 | return ret; |
| 2802 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2803 | len -= num; |
| 2804 | } |
| 2805 | } else { |
| 2806 | pos = eccsize + i * (eccsize + chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2807 | ret = nand_change_write_column_op(chip, pos, |
| 2808 | NULL, 0, |
| 2809 | false); |
| 2810 | if (ret) |
| 2811 | return ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2812 | } |
| 2813 | } else |
| 2814 | sndcmd = 1; |
| 2815 | len = min_t(int, length, chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2816 | |
| 2817 | ret = nand_write_data_op(chip, bufpoi, len, false); |
| 2818 | if (ret) |
| 2819 | return ret; |
| 2820 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2821 | bufpoi += len; |
| 2822 | length -= len; |
| 2823 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2824 | if (length > 0) { |
| 2825 | ret = nand_write_data_op(chip, bufpoi, length, false); |
| 2826 | if (ret) |
| 2827 | return ret; |
| 2828 | } |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2829 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2830 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2831 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2832 | EXPORT_SYMBOL(nand_write_oob_syndrome); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2833 | |
| 2834 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2835 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2836 | * @mtd: MTD device structure |
| 2837 | * @from: offset to read from |
| 2838 | * @ops: oob operations description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2840 | * NAND read out-of-band data from the spare area. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2842 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 2843 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | { |
Brian Norris | c00a099 | 2012-05-01 17:12:54 -0700 | [diff] [blame] | 2845 | int page, realpage, chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2846 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 2847 | struct mtd_ecc_stats stats; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2848 | int readlen = ops->ooblen; |
| 2849 | int len; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2850 | uint8_t *buf = ops->oobbuf; |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2851 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2853 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2854 | __func__, (unsigned long long)from, readlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2855 | |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 2856 | stats = mtd->ecc_stats; |
| 2857 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 2858 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2859 | |
| 2860 | if (unlikely(ops->ooboffs >= len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2861 | pr_debug("%s: attempt to start read outside oob\n", |
| 2862 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2863 | return -EINVAL; |
| 2864 | } |
| 2865 | |
| 2866 | /* Do not allow reads past end of device */ |
| 2867 | if (unlikely(from >= mtd->size || |
| 2868 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - |
| 2869 | (from >> chip->page_shift)) * len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2870 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2871 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2872 | return -EINVAL; |
| 2873 | } |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2874 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2875 | chipnr = (int)(from >> chip->chip_shift); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2876 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2878 | /* Shift to get page */ |
| 2879 | realpage = (int)(from >> chip->page_shift); |
| 2880 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2882 | while (1) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2883 | if (ops->mode == MTD_OPS_RAW) |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2884 | ret = chip->ecc.read_oob_raw(mtd, chip, page); |
Brian Norris | c46f648 | 2011-08-30 18:45:38 -0700 | [diff] [blame] | 2885 | else |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2886 | ret = chip->ecc.read_oob(mtd, chip, page); |
| 2887 | |
| 2888 | if (ret < 0) |
| 2889 | break; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2890 | |
| 2891 | len = min(len, readlen); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2892 | buf = nand_transfer_oob(mtd, buf, ops, len); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2893 | |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 2894 | if (chip->options & NAND_NEED_READRDY) { |
| 2895 | /* Apply delay or wait for ready/busy pin */ |
| 2896 | if (!chip->dev_ready) |
| 2897 | udelay(chip->chip_delay); |
| 2898 | else |
| 2899 | nand_wait_ready(mtd); |
| 2900 | } |
| 2901 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2902 | readlen -= len; |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 2903 | if (!readlen) |
| 2904 | break; |
| 2905 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2906 | /* Increment page address */ |
| 2907 | realpage++; |
| 2908 | |
| 2909 | page = realpage & chip->pagemask; |
| 2910 | /* Check, if we cross a chip boundary */ |
| 2911 | if (!page) { |
| 2912 | chipnr++; |
| 2913 | chip->select_chip(mtd, -1); |
| 2914 | chip->select_chip(mtd, chipnr); |
| 2915 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | } |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2917 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2919 | ops->oobretlen = ops->ooblen - readlen; |
| 2920 | |
| 2921 | if (ret < 0) |
| 2922 | return ret; |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 2923 | |
| 2924 | if (mtd->ecc_stats.failed - stats.failed) |
| 2925 | return -EBADMSG; |
| 2926 | |
| 2927 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2931 | * 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] | 2932 | * @mtd: MTD device structure |
| 2933 | * @from: offset to read from |
| 2934 | * @ops: oob operation description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2936 | * NAND read data and/or out-of-band data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2938 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 2939 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2940 | { |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 2941 | int ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2942 | |
| 2943 | ops->retlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | |
| 2945 | /* Do not allow reads past end of device */ |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2946 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2947 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2948 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | return -EINVAL; |
| 2950 | } |
| 2951 | |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 2952 | if (ops->mode != MTD_OPS_PLACE_OOB && |
| 2953 | ops->mode != MTD_OPS_AUTO_OOB && |
| 2954 | ops->mode != MTD_OPS_RAW) |
| 2955 | return -ENOTSUPP; |
| 2956 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 2957 | nand_get_device(mtd, FL_READING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2959 | if (!ops->datbuf) |
| 2960 | ret = nand_do_read_oob(mtd, from, ops); |
| 2961 | else |
| 2962 | ret = nand_do_read_ops(mtd, from, ops); |
| 2963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2964 | nand_release_device(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2965 | return ret; |
| 2966 | } |
| 2967 | |
| 2968 | |
| 2969 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2970 | * nand_write_page_raw - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2971 | * @mtd: mtd info structure |
| 2972 | * @chip: nand chip info structure |
| 2973 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2974 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2975 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2976 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2977 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2978 | */ |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 2979 | int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 2980 | const uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2981 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2982 | int ret; |
| 2983 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame^] | 2984 | ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2985 | if (ret) |
| 2986 | return ret; |
| 2987 | |
| 2988 | if (oob_required) { |
| 2989 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 2990 | false); |
| 2991 | if (ret) |
| 2992 | return ret; |
| 2993 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2994 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame^] | 2995 | return nand_prog_page_end_op(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2996 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 2997 | EXPORT_SYMBOL(nand_write_page_raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2999 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3000 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3001 | * @mtd: mtd info structure |
| 3002 | * @chip: nand chip info structure |
| 3003 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3004 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3005 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3006 | * |
| 3007 | * We need a special oob layout and handling even when ECC isn't checked. |
| 3008 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3009 | static int nand_write_page_raw_syndrome(struct mtd_info *mtd, |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3010 | struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3011 | const uint8_t *buf, int oob_required, |
| 3012 | int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3013 | { |
| 3014 | int eccsize = chip->ecc.size; |
| 3015 | int eccbytes = chip->ecc.bytes; |
| 3016 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3017 | int steps, size, ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3018 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame^] | 3019 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3020 | if (ret) |
| 3021 | return ret; |
| 3022 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3023 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3024 | ret = nand_write_data_op(chip, buf, eccsize, false); |
| 3025 | if (ret) |
| 3026 | return ret; |
| 3027 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3028 | buf += eccsize; |
| 3029 | |
| 3030 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3031 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 3032 | false); |
| 3033 | if (ret) |
| 3034 | return ret; |
| 3035 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3036 | oob += chip->ecc.prepad; |
| 3037 | } |
| 3038 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3039 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 3040 | if (ret) |
| 3041 | return ret; |
| 3042 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3043 | oob += eccbytes; |
| 3044 | |
| 3045 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3046 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 3047 | false); |
| 3048 | if (ret) |
| 3049 | return ret; |
| 3050 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3051 | oob += chip->ecc.postpad; |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3056 | if (size) { |
| 3057 | ret = nand_write_data_op(chip, oob, size, false); |
| 3058 | if (ret) |
| 3059 | return ret; |
| 3060 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3061 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame^] | 3062 | return nand_prog_page_end_op(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3063 | } |
| 3064 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3065 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3066 | * @mtd: mtd info structure |
| 3067 | * @chip: nand chip info structure |
| 3068 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3069 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3070 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3071 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3072 | 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] | 3073 | const uint8_t *buf, int oob_required, |
| 3074 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3075 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3076 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3077 | int eccbytes = chip->ecc.bytes; |
| 3078 | int eccsteps = chip->ecc.steps; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 3079 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3080 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3081 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3082 | /* Software ECC calculation */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3083 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 3084 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3085 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3086 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 3087 | chip->ecc.total); |
| 3088 | if (ret) |
| 3089 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3090 | |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3091 | return chip->ecc.write_page_raw(mtd, chip, buf, 1, page); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3092 | } |
| 3093 | |
| 3094 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3095 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3096 | * @mtd: mtd info structure |
| 3097 | * @chip: nand chip info structure |
| 3098 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3099 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3100 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3101 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3102 | 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] | 3103 | const uint8_t *buf, int oob_required, |
| 3104 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3105 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3106 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3107 | int eccbytes = chip->ecc.bytes; |
| 3108 | int eccsteps = chip->ecc.steps; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 3109 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3110 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3111 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame^] | 3112 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3113 | if (ret) |
| 3114 | return ret; |
| 3115 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3116 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 3117 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3118 | |
| 3119 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 3120 | if (ret) |
| 3121 | return ret; |
| 3122 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3123 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 3124 | } |
| 3125 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3126 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 3127 | chip->ecc.total); |
| 3128 | if (ret) |
| 3129 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3130 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3131 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 3132 | if (ret) |
| 3133 | return ret; |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3134 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame^] | 3135 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3136 | } |
| 3137 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3138 | |
| 3139 | /** |
Brian Norris | 73c8aaf | 2015-02-28 02:04:18 -0800 | [diff] [blame] | 3140 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3141 | * @mtd: mtd info structure |
| 3142 | * @chip: nand chip info structure |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3143 | * @offset: column address of subpage within the page |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3144 | * @data_len: data length |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3145 | * @buf: data buffer |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3146 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3147 | * @page: page number to write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3148 | */ |
| 3149 | static int nand_write_subpage_hwecc(struct mtd_info *mtd, |
| 3150 | struct nand_chip *chip, uint32_t offset, |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3151 | uint32_t data_len, const uint8_t *buf, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3152 | int oob_required, int page) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3153 | { |
| 3154 | uint8_t *oob_buf = chip->oob_poi; |
| 3155 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 3156 | int ecc_size = chip->ecc.size; |
| 3157 | int ecc_bytes = chip->ecc.bytes; |
| 3158 | int ecc_steps = chip->ecc.steps; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3159 | uint32_t start_step = offset / ecc_size; |
| 3160 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 3161 | int oob_bytes = mtd->oobsize / ecc_steps; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3162 | int step, ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3163 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame^] | 3164 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3165 | if (ret) |
| 3166 | return ret; |
| 3167 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3168 | for (step = 0; step < ecc_steps; step++) { |
| 3169 | /* configure controller for WRITE access */ |
| 3170 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 3171 | |
| 3172 | /* write data (untouched subpages already masked by 0xFF) */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3173 | ret = nand_write_data_op(chip, buf, ecc_size, false); |
| 3174 | if (ret) |
| 3175 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3176 | |
| 3177 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 3178 | if ((step < start_step) || (step > end_step)) |
| 3179 | memset(ecc_calc, 0xff, ecc_bytes); |
| 3180 | else |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3181 | chip->ecc.calculate(mtd, buf, ecc_calc); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3182 | |
| 3183 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 3184 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 3185 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 3186 | memset(oob_buf, 0xff, oob_bytes); |
| 3187 | |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3188 | buf += ecc_size; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3189 | ecc_calc += ecc_bytes; |
| 3190 | oob_buf += oob_bytes; |
| 3191 | } |
| 3192 | |
| 3193 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 3194 | /* this include masked-value(0xFF) for unwritten subpages */ |
| 3195 | ecc_calc = chip->buffers->ecccalc; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3196 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 3197 | chip->ecc.total); |
| 3198 | if (ret) |
| 3199 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3200 | |
| 3201 | /* write OOB buffer to NAND device */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3202 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 3203 | if (ret) |
| 3204 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3205 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame^] | 3206 | return nand_prog_page_end_op(chip); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3210 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3211 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3212 | * @mtd: mtd info structure |
| 3213 | * @chip: nand chip info structure |
| 3214 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3215 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3216 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3217 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3218 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 3219 | * need a special oob layout and handling. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3220 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3221 | static int nand_write_page_syndrome(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3222 | struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3223 | const uint8_t *buf, int oob_required, |
| 3224 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3225 | { |
| 3226 | int i, eccsize = chip->ecc.size; |
| 3227 | int eccbytes = chip->ecc.bytes; |
| 3228 | int eccsteps = chip->ecc.steps; |
| 3229 | const uint8_t *p = buf; |
| 3230 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3231 | int ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3232 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame^] | 3233 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3234 | if (ret) |
| 3235 | return ret; |
| 3236 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3237 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3238 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3239 | |
| 3240 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 3241 | if (ret) |
| 3242 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3243 | |
| 3244 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3245 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 3246 | false); |
| 3247 | if (ret) |
| 3248 | return ret; |
| 3249 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3250 | oob += chip->ecc.prepad; |
| 3251 | } |
| 3252 | |
| 3253 | chip->ecc.calculate(mtd, p, oob); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3254 | |
| 3255 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 3256 | if (ret) |
| 3257 | return ret; |
| 3258 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3259 | oob += eccbytes; |
| 3260 | |
| 3261 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3262 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 3263 | false); |
| 3264 | if (ret) |
| 3265 | return ret; |
| 3266 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3267 | oob += chip->ecc.postpad; |
| 3268 | } |
| 3269 | } |
| 3270 | |
| 3271 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 3272 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3273 | if (i) { |
| 3274 | ret = nand_write_data_op(chip, oob, i, false); |
| 3275 | if (ret) |
| 3276 | return ret; |
| 3277 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3278 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame^] | 3279 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3280 | } |
| 3281 | |
| 3282 | /** |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 3283 | * nand_write_page - write one page |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3284 | * @mtd: MTD device structure |
| 3285 | * @chip: NAND chip descriptor |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3286 | * @offset: address offset within the page |
| 3287 | * @data_len: length of actual data to be written |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3288 | * @buf: the data to write |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3289 | * @oob_required: must write chip->oob_poi to OOB |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3290 | * @page: page number to write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3291 | * @raw: use _raw version of write_page |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3292 | */ |
| 3293 | 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] | 3294 | uint32_t offset, int data_len, const uint8_t *buf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 3295 | int oob_required, int page, int raw) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3296 | { |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3297 | int status, subpage; |
| 3298 | |
| 3299 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 3300 | chip->ecc.write_subpage) |
| 3301 | subpage = offset || (data_len < mtd->writesize); |
| 3302 | else |
| 3303 | subpage = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3304 | |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3305 | if (unlikely(raw)) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3306 | status = chip->ecc.write_page_raw(mtd, chip, buf, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3307 | oob_required, page); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3308 | else if (subpage) |
| 3309 | status = chip->ecc.write_subpage(mtd, chip, offset, data_len, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3310 | buf, oob_required, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3311 | else |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3312 | status = chip->ecc.write_page(mtd, chip, buf, oob_required, |
| 3313 | page); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3314 | |
| 3315 | if (status < 0) |
| 3316 | return status; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3317 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3318 | return 0; |
| 3319 | } |
| 3320 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3321 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3322 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3323 | * @mtd: MTD device structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3324 | * @oob: oob data buffer |
| 3325 | * @len: oob data write length |
| 3326 | * @ops: oob ops structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3327 | */ |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3328 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
| 3329 | struct mtd_oob_ops *ops) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3330 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3331 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3332 | int ret; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3333 | |
| 3334 | /* |
| 3335 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 3336 | * data from a previous OOB read. |
| 3337 | */ |
| 3338 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 3339 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3340 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3341 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3342 | case MTD_OPS_PLACE_OOB: |
| 3343 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3344 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 3345 | return oob + len; |
| 3346 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3347 | case MTD_OPS_AUTO_OOB: |
| 3348 | ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi, |
| 3349 | ops->ooboffs, len); |
| 3350 | BUG_ON(ret); |
| 3351 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3352 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3353 | default: |
| 3354 | BUG(); |
| 3355 | } |
| 3356 | return NULL; |
| 3357 | } |
| 3358 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3359 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3360 | |
| 3361 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3362 | * nand_do_write_ops - [INTERN] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3363 | * @mtd: MTD device structure |
| 3364 | * @to: offset to write to |
| 3365 | * @ops: oob operations description structure |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3366 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3367 | * NAND write with ECC. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3368 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3369 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 3370 | struct mtd_oob_ops *ops) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3371 | { |
Corentin Labbe | 73600b6 | 2017-09-02 10:49:38 +0200 | [diff] [blame] | 3372 | int chipnr, realpage, page, column; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3373 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3374 | uint32_t writelen = ops->len; |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3375 | |
| 3376 | uint32_t oobwritelen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3377 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3378 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3379 | uint8_t *oob = ops->oobbuf; |
| 3380 | uint8_t *buf = ops->datbuf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3381 | int ret; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3382 | int oob_required = oob ? 1 : 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3383 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3384 | ops->retlen = 0; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3385 | if (!writelen) |
| 3386 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3387 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3388 | /* Reject writes, which are not page aligned */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3389 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 3390 | pr_notice("%s: attempt to write non page aligned data\n", |
| 3391 | __func__); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3392 | return -EINVAL; |
| 3393 | } |
| 3394 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3395 | column = to & (mtd->writesize - 1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3396 | |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 3397 | chipnr = (int)(to >> chip->chip_shift); |
| 3398 | chip->select_chip(mtd, chipnr); |
| 3399 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3400 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3401 | if (nand_check_wp(mtd)) { |
| 3402 | ret = -EIO; |
| 3403 | goto err_out; |
| 3404 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3405 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3406 | realpage = (int)(to >> chip->page_shift); |
| 3407 | page = realpage & chip->pagemask; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3408 | |
| 3409 | /* Invalidate the page cache, when we write to the cached page */ |
Brian Norris | 537ab1b | 2014-07-21 19:08:03 -0700 | [diff] [blame] | 3410 | if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && |
| 3411 | ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3412 | chip->pagebuf = -1; |
| 3413 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3414 | /* Don't allow multipage oob writes with offset */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3415 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 3416 | ret = -EINVAL; |
| 3417 | goto err_out; |
| 3418 | } |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3419 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3420 | while (1) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3421 | int bytes = mtd->writesize; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3422 | uint8_t *wbuf = buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3423 | int use_bufpoi; |
Hector Palacios | 144f4c9 | 2016-07-18 10:39:18 +0200 | [diff] [blame] | 3424 | int part_pagewr = (column || writelen < mtd->writesize); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3425 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3426 | if (part_pagewr) |
| 3427 | use_bufpoi = 1; |
| 3428 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 3429 | use_bufpoi = !virt_addr_valid(buf) || |
| 3430 | !IS_ALIGNED((unsigned long)buf, |
| 3431 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3432 | else |
| 3433 | use_bufpoi = 0; |
| 3434 | |
| 3435 | /* Partial page write?, or need to use bounce buffer */ |
| 3436 | if (use_bufpoi) { |
| 3437 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 3438 | __func__, buf); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3439 | if (part_pagewr) |
| 3440 | bytes = min_t(int, bytes - column, writelen); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3441 | chip->pagebuf = -1; |
| 3442 | memset(chip->buffers->databuf, 0xff, mtd->writesize); |
| 3443 | memcpy(&chip->buffers->databuf[column], buf, bytes); |
| 3444 | wbuf = chip->buffers->databuf; |
| 3445 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3446 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3447 | if (unlikely(oob)) { |
| 3448 | size_t len = min(oobwritelen, oobmaxlen); |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3449 | oob = nand_fill_oob(mtd, oob, len, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3450 | oobwritelen -= len; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3451 | } else { |
| 3452 | /* We still need to erase leftover OOB data */ |
| 3453 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3454 | } |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 3455 | |
| 3456 | ret = nand_write_page(mtd, chip, column, bytes, wbuf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 3457 | oob_required, page, |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 3458 | (ops->mode == MTD_OPS_RAW)); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3459 | if (ret) |
| 3460 | break; |
| 3461 | |
| 3462 | writelen -= bytes; |
| 3463 | if (!writelen) |
| 3464 | break; |
| 3465 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3466 | column = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3467 | buf += bytes; |
| 3468 | realpage++; |
| 3469 | |
| 3470 | page = realpage & chip->pagemask; |
| 3471 | /* Check, if we cross a chip boundary */ |
| 3472 | if (!page) { |
| 3473 | chipnr++; |
| 3474 | chip->select_chip(mtd, -1); |
| 3475 | chip->select_chip(mtd, chipnr); |
| 3476 | } |
| 3477 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3478 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3479 | ops->retlen = ops->len - writelen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3480 | if (unlikely(oob)) |
| 3481 | ops->oobretlen = ops->ooblen; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3482 | |
| 3483 | err_out: |
| 3484 | chip->select_chip(mtd, -1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3485 | return ret; |
| 3486 | } |
| 3487 | |
| 3488 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 3489 | * panic_nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3490 | * @mtd: MTD device structure |
| 3491 | * @to: offset to write to |
| 3492 | * @len: number of bytes to write |
| 3493 | * @retlen: pointer to variable to store the number of written bytes |
| 3494 | * @buf: the data to write |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 3495 | * |
| 3496 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 3497 | * may for example be called by mtdoops when writing an oops while in panic. |
| 3498 | */ |
| 3499 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 3500 | size_t *retlen, const uint8_t *buf) |
| 3501 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3502 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 3503 | int chipnr = (int)(to >> chip->chip_shift); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 3504 | struct mtd_oob_ops ops; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 3505 | int ret; |
| 3506 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3507 | /* Grab the device */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 3508 | panic_nand_get_device(chip, mtd, FL_WRITING); |
| 3509 | |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 3510 | chip->select_chip(mtd, chipnr); |
| 3511 | |
| 3512 | /* Wait for the device to get ready */ |
| 3513 | panic_nand_wait(mtd, chip, 400); |
| 3514 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 3515 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 3516 | ops.len = len; |
| 3517 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 3518 | ops.mode = MTD_OPS_PLACE_OOB; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 3519 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 3520 | ret = nand_do_write_ops(mtd, to, &ops); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 3521 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 3522 | *retlen = ops.retlen; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 3523 | return ret; |
| 3524 | } |
| 3525 | |
| 3526 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3527 | * nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3528 | * @mtd: MTD device structure |
| 3529 | * @to: offset to write to |
| 3530 | * @len: number of bytes to write |
| 3531 | * @retlen: pointer to variable to store the number of written bytes |
| 3532 | * @buf: the data to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3533 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3534 | * NAND write with ECC. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3535 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3536 | 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] | 3537 | size_t *retlen, const uint8_t *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3538 | { |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 3539 | struct mtd_oob_ops ops; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3540 | int ret; |
| 3541 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3542 | nand_get_device(mtd, FL_WRITING); |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 3543 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 3544 | ops.len = len; |
| 3545 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 3546 | ops.mode = MTD_OPS_PLACE_OOB; |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 3547 | ret = nand_do_write_ops(mtd, to, &ops); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 3548 | *retlen = ops.retlen; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3549 | nand_release_device(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3550 | return ret; |
| 3551 | } |
| 3552 | |
| 3553 | /** |
| 3554 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3555 | * @mtd: MTD device structure |
| 3556 | * @to: offset to write to |
| 3557 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3558 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3559 | * NAND write out-of-band. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3560 | */ |
| 3561 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 3562 | struct mtd_oob_ops *ops) |
| 3563 | { |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3564 | int chipnr, page, status, len; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3565 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3567 | pr_debug("%s: to = 0x%08x, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 3568 | __func__, (unsigned int)to, (int)ops->ooblen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3569 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3570 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3572 | /* Do not allow write past end of page */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3573 | if ((ops->ooboffs + ops->ooblen) > len) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3574 | pr_debug("%s: attempt to write past end of page\n", |
| 3575 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3576 | return -EINVAL; |
| 3577 | } |
| 3578 | |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3579 | if (unlikely(ops->ooboffs >= len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3580 | pr_debug("%s: attempt to start write outside oob\n", |
| 3581 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3582 | return -EINVAL; |
| 3583 | } |
| 3584 | |
Jason Liu | 775adc3d4 | 2011-02-25 13:06:18 +0800 | [diff] [blame] | 3585 | /* Do not allow write past end of device */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3586 | if (unlikely(to >= mtd->size || |
| 3587 | ops->ooboffs + ops->ooblen > |
| 3588 | ((mtd->size >> chip->page_shift) - |
| 3589 | (to >> chip->page_shift)) * len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3590 | pr_debug("%s: attempt to write beyond end of device\n", |
| 3591 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3592 | return -EINVAL; |
| 3593 | } |
| 3594 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3595 | chipnr = (int)(to >> chip->chip_shift); |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3596 | |
| 3597 | /* |
| 3598 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 3599 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 3600 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 3601 | * it in the doc2000 driver in August 1999. dwmw2. |
| 3602 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 3603 | nand_reset(chip, chipnr); |
| 3604 | |
| 3605 | chip->select_chip(mtd, chipnr); |
| 3606 | |
| 3607 | /* Shift to get page */ |
| 3608 | page = (int)(to >> chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | |
| 3610 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3611 | if (nand_check_wp(mtd)) { |
| 3612 | chip->select_chip(mtd, -1); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3613 | return -EROFS; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3614 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3616 | /* Invalidate the page cache, if we write to the cached page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3617 | if (page == chip->pagebuf) |
| 3618 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3620 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 3621 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3622 | if (ops->mode == MTD_OPS_RAW) |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 3623 | status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask); |
| 3624 | else |
| 3625 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3626 | |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3627 | chip->select_chip(mtd, -1); |
| 3628 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3629 | if (status) |
| 3630 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3632 | ops->oobretlen = ops->ooblen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3633 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3634 | return 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3635 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3636 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3637 | /** |
| 3638 | * 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] | 3639 | * @mtd: MTD device structure |
| 3640 | * @to: offset to write to |
| 3641 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3642 | */ |
| 3643 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 3644 | struct mtd_oob_ops *ops) |
| 3645 | { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3646 | int ret = -ENOTSUPP; |
| 3647 | |
| 3648 | ops->retlen = 0; |
| 3649 | |
| 3650 | /* Do not allow writes past end of device */ |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3651 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3652 | pr_debug("%s: attempt to write beyond end of device\n", |
| 3653 | __func__); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3654 | return -EINVAL; |
| 3655 | } |
| 3656 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3657 | nand_get_device(mtd, FL_WRITING); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3658 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3659 | switch (ops->mode) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3660 | case MTD_OPS_PLACE_OOB: |
| 3661 | case MTD_OPS_AUTO_OOB: |
| 3662 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3663 | break; |
| 3664 | |
| 3665 | default: |
| 3666 | goto out; |
| 3667 | } |
| 3668 | |
| 3669 | if (!ops->datbuf) |
| 3670 | ret = nand_do_write_oob(mtd, to, ops); |
| 3671 | else |
| 3672 | ret = nand_do_write_ops(mtd, to, ops); |
| 3673 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3674 | out: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3675 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3676 | return ret; |
| 3677 | } |
| 3678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3679 | /** |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3680 | * single_erase - [GENERIC] NAND standard block erase command function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3681 | * @mtd: MTD device structure |
| 3682 | * @page: the page address of the block which will be erased |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3683 | * |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3684 | * Standard erase command for NAND chips. Returns NAND status. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | */ |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3686 | static int single_erase(struct mtd_info *mtd, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3687 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3688 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3689 | unsigned int eraseblock; |
Miquel Raynal | eb94555 | 2017-11-30 18:01:28 +0100 | [diff] [blame] | 3690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3691 | /* Send commands to erase a block */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3692 | eraseblock = page >> (chip->phys_erase_shift - chip->page_shift); |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3693 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3694 | return nand_erase_op(chip, eraseblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | * nand_erase - [MTD Interface] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3699 | * @mtd: MTD device structure |
| 3700 | * @instr: erase instruction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3701 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3702 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3703 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3704 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3705 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3706 | return nand_erase_nand(mtd, instr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3707 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3709 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3710 | * nand_erase_nand - [INTERN] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3711 | * @mtd: MTD device structure |
| 3712 | * @instr: erase instruction |
| 3713 | * @allowbbt: allow erasing the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3714 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3715 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3717 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
| 3718 | int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3719 | { |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3720 | int page, status, pages_per_block, ret, chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3721 | struct nand_chip *chip = mtd_to_nand(mtd); |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3722 | loff_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3723 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3724 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 3725 | __func__, (unsigned long long)instr->addr, |
| 3726 | (unsigned long long)instr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3727 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 3728 | if (check_offs_len(mtd, instr->addr, instr->len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3729 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3731 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3732 | nand_get_device(mtd, FL_ERASING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3733 | |
| 3734 | /* Shift to get first page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3735 | page = (int)(instr->addr >> chip->page_shift); |
| 3736 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3737 | |
| 3738 | /* Calculate pages in each block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3739 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3740 | |
| 3741 | /* Select the NAND device */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3742 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3744 | /* Check, if it is write protected */ |
| 3745 | if (nand_check_wp(mtd)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3746 | pr_debug("%s: device is write protected!\n", |
| 3747 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3748 | instr->state = MTD_ERASE_FAILED; |
| 3749 | goto erase_exit; |
| 3750 | } |
| 3751 | |
| 3752 | /* Loop through the pages */ |
| 3753 | len = instr->len; |
| 3754 | |
| 3755 | instr->state = MTD_ERASING; |
| 3756 | |
| 3757 | while (len) { |
Wolfram Sang | 12183a2 | 2011-12-21 23:01:20 +0100 | [diff] [blame] | 3758 | /* 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] | 3759 | if (nand_block_checkbad(mtd, ((loff_t) page) << |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 3760 | chip->page_shift, allowbbt)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 3761 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
| 3762 | __func__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3763 | instr->state = MTD_ERASE_FAILED; |
| 3764 | goto erase_exit; |
| 3765 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3766 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3767 | /* |
| 3768 | * Invalidate the page cache, if we erase the block which |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3769 | * contains the current cached page. |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3770 | */ |
| 3771 | if (page <= chip->pagebuf && chip->pagebuf < |
| 3772 | (page + pages_per_block)) |
| 3773 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3774 | |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3775 | status = chip->erase(mtd, page & chip->pagemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | |
| 3777 | /* See if block erase succeeded */ |
Miquel Raynal | eb94555 | 2017-11-30 18:01:28 +0100 | [diff] [blame] | 3778 | if (status) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3779 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 3780 | __func__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3781 | instr->state = MTD_ERASE_FAILED; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3782 | instr->fail_addr = |
| 3783 | ((loff_t)page << chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | goto erase_exit; |
| 3785 | } |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 3786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3787 | /* Increment page address and decrement length */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 3788 | len -= (1ULL << chip->phys_erase_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | page += pages_per_block; |
| 3790 | |
| 3791 | /* Check, if we cross a chip boundary */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3792 | if (len && !(page & chip->pagemask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3793 | chipnr++; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3794 | chip->select_chip(mtd, -1); |
| 3795 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | } |
| 3797 | } |
| 3798 | instr->state = MTD_ERASE_DONE; |
| 3799 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3800 | erase_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3801 | |
| 3802 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3803 | |
| 3804 | /* Deselect and wake up anyone waiting on the device */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3805 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3806 | nand_release_device(mtd); |
| 3807 | |
David Woodhouse | 49defc0 | 2007-10-06 15:01:59 -0400 | [diff] [blame] | 3808 | /* Do call back function */ |
| 3809 | if (!ret) |
| 3810 | mtd_erase_callback(instr); |
| 3811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | /* Return more or less happy */ |
| 3813 | return ret; |
| 3814 | } |
| 3815 | |
| 3816 | /** |
| 3817 | * nand_sync - [MTD Interface] sync |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3818 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3819 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3820 | * Sync is actually a wait for chip ready function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3822 | static void nand_sync(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3823 | { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3824 | pr_debug("%s: called\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3825 | |
| 3826 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3827 | nand_get_device(mtd, FL_SYNCING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | /* Release it and go back */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3829 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | } |
| 3831 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3832 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3833 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3834 | * @mtd: MTD device structure |
| 3835 | * @offs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3836 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3837 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3838 | { |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 3839 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3840 | int chipnr = (int)(offs >> chip->chip_shift); |
| 3841 | int ret; |
| 3842 | |
| 3843 | /* Select the NAND device */ |
| 3844 | nand_get_device(mtd, FL_READING); |
| 3845 | chip->select_chip(mtd, chipnr); |
| 3846 | |
| 3847 | ret = nand_block_checkbad(mtd, offs, 0); |
| 3848 | |
| 3849 | chip->select_chip(mtd, -1); |
| 3850 | nand_release_device(mtd); |
| 3851 | |
| 3852 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | } |
| 3854 | |
| 3855 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3856 | * 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] | 3857 | * @mtd: MTD device structure |
| 3858 | * @ofs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3859 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3860 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3861 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3862 | int ret; |
| 3863 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3864 | ret = nand_block_isbad(mtd, ofs); |
| 3865 | if (ret) { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3866 | /* If it was bad already, return success and do nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3867 | if (ret > 0) |
| 3868 | return 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3869 | return ret; |
| 3870 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3871 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 3872 | return nand_block_markbad_lowlevel(mtd, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | } |
| 3874 | |
| 3875 | /** |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 3876 | * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd |
| 3877 | * @mtd: MTD device structure |
| 3878 | * @ofs: offset relative to mtd start |
| 3879 | * @len: length of mtd |
| 3880 | */ |
| 3881 | static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len) |
| 3882 | { |
| 3883 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3884 | u32 part_start_block; |
| 3885 | u32 part_end_block; |
| 3886 | u32 part_start_die; |
| 3887 | u32 part_end_die; |
| 3888 | |
| 3889 | /* |
| 3890 | * max_bb_per_die and blocks_per_die used to determine |
| 3891 | * the maximum bad block count. |
| 3892 | */ |
| 3893 | if (!chip->max_bb_per_die || !chip->blocks_per_die) |
| 3894 | return -ENOTSUPP; |
| 3895 | |
| 3896 | /* Get the start and end of the partition in erase blocks. */ |
| 3897 | part_start_block = mtd_div_by_eb(ofs, mtd); |
| 3898 | part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1; |
| 3899 | |
| 3900 | /* Get the start and end LUNs of the partition. */ |
| 3901 | part_start_die = part_start_block / chip->blocks_per_die; |
| 3902 | part_end_die = part_end_block / chip->blocks_per_die; |
| 3903 | |
| 3904 | /* |
| 3905 | * Look up the bad blocks per unit and multiply by the number of units |
| 3906 | * that the partition spans. |
| 3907 | */ |
| 3908 | return chip->max_bb_per_die * (part_end_die - part_start_die + 1); |
| 3909 | } |
| 3910 | |
| 3911 | /** |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3912 | * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand |
| 3913 | * @mtd: MTD device structure |
| 3914 | * @chip: nand chip info structure |
| 3915 | * @addr: feature address. |
| 3916 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3917 | */ |
| 3918 | static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3919 | int addr, uint8_t *subfeature_param) |
| 3920 | { |
David Mosberger | d914c93 | 2013-05-29 15:30:13 +0300 | [diff] [blame] | 3921 | if (!chip->onfi_version || |
| 3922 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3923 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3924 | return -EINVAL; |
| 3925 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3926 | return nand_set_features_op(chip, addr, subfeature_param); |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | /** |
| 3930 | * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand |
| 3931 | * @mtd: MTD device structure |
| 3932 | * @chip: nand chip info structure |
| 3933 | * @addr: feature address. |
| 3934 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3935 | */ |
| 3936 | static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3937 | int addr, uint8_t *subfeature_param) |
| 3938 | { |
David Mosberger | d914c93 | 2013-05-29 15:30:13 +0300 | [diff] [blame] | 3939 | if (!chip->onfi_version || |
| 3940 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3941 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3942 | return -EINVAL; |
| 3943 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3944 | return nand_get_features_op(chip, addr, subfeature_param); |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3945 | } |
| 3946 | |
| 3947 | /** |
Boris Brezillon | 4a78cc6 | 2017-05-26 17:10:15 +0200 | [diff] [blame] | 3948 | * nand_onfi_get_set_features_notsupp - set/get features stub returning |
| 3949 | * -ENOTSUPP |
| 3950 | * @mtd: MTD device structure |
| 3951 | * @chip: nand chip info structure |
| 3952 | * @addr: feature address. |
| 3953 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3954 | * |
| 3955 | * Should be used by NAND controller drivers that do not support the SET/GET |
| 3956 | * FEATURES operations. |
| 3957 | */ |
| 3958 | int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd, |
| 3959 | struct nand_chip *chip, int addr, |
| 3960 | u8 *subfeature_param) |
| 3961 | { |
| 3962 | return -ENOTSUPP; |
| 3963 | } |
| 3964 | EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp); |
| 3965 | |
| 3966 | /** |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3967 | * nand_suspend - [MTD Interface] Suspend the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3968 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3969 | */ |
| 3970 | static int nand_suspend(struct mtd_info *mtd) |
| 3971 | { |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3972 | return nand_get_device(mtd, FL_PM_SUSPENDED); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3973 | } |
| 3974 | |
| 3975 | /** |
| 3976 | * nand_resume - [MTD Interface] Resume the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3977 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3978 | */ |
| 3979 | static void nand_resume(struct mtd_info *mtd) |
| 3980 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3981 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3982 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3983 | if (chip->state == FL_PM_SUSPENDED) |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3984 | nand_release_device(mtd); |
| 3985 | else |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 3986 | pr_err("%s called for a chip which is not in suspended state\n", |
| 3987 | __func__); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3988 | } |
| 3989 | |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 3990 | /** |
| 3991 | * nand_shutdown - [MTD Interface] Finish the current NAND operation and |
| 3992 | * prevent further operations |
| 3993 | * @mtd: MTD device structure |
| 3994 | */ |
| 3995 | static void nand_shutdown(struct mtd_info *mtd) |
| 3996 | { |
Brian Norris | 9ca641b | 2015-11-09 16:37:28 -0800 | [diff] [blame] | 3997 | nand_get_device(mtd, FL_PM_SUSPENDED); |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 3998 | } |
| 3999 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4000 | /* Set default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4001 | static void nand_set_defaults(struct nand_chip *chip) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4002 | { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4003 | unsigned int busw = chip->options & NAND_BUSWIDTH_16; |
| 4004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | /* check for proper chip_delay setup, set 20us if not */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4006 | if (!chip->chip_delay) |
| 4007 | chip->chip_delay = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | |
| 4009 | /* check, if a user supplied command function given */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4010 | if (chip->cmdfunc == NULL) |
| 4011 | chip->cmdfunc = nand_command; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | |
| 4013 | /* check, if a user supplied wait function given */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4014 | if (chip->waitfunc == NULL) |
| 4015 | chip->waitfunc = nand_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4017 | if (!chip->select_chip) |
| 4018 | chip->select_chip = nand_select_chip; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 4019 | |
Huang Shijie | 4204ccc | 2013-08-16 10:10:07 +0800 | [diff] [blame] | 4020 | /* set for ONFI nand */ |
| 4021 | if (!chip->onfi_set_features) |
| 4022 | chip->onfi_set_features = nand_onfi_set_features; |
| 4023 | if (!chip->onfi_get_features) |
| 4024 | chip->onfi_get_features = nand_onfi_get_features; |
| 4025 | |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 4026 | /* If called twice, pointers that depend on busw may need to be reset */ |
| 4027 | if (!chip->read_byte || chip->read_byte == nand_read_byte) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4028 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
| 4029 | if (!chip->read_word) |
| 4030 | chip->read_word = nand_read_word; |
| 4031 | if (!chip->block_bad) |
| 4032 | chip->block_bad = nand_block_bad; |
| 4033 | if (!chip->block_markbad) |
| 4034 | chip->block_markbad = nand_default_block_markbad; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 4035 | if (!chip->write_buf || chip->write_buf == nand_write_buf) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4036 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 4037 | if (!chip->write_byte || chip->write_byte == nand_write_byte) |
| 4038 | chip->write_byte = busw ? nand_write_byte16 : nand_write_byte; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 4039 | if (!chip->read_buf || chip->read_buf == nand_read_buf) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4040 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4041 | if (!chip->scan_bbt) |
| 4042 | chip->scan_bbt = nand_default_bbt; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4043 | |
| 4044 | if (!chip->controller) { |
| 4045 | chip->controller = &chip->hwcontrol; |
Marc Gonzalez | d45bc58 | 2016-07-27 11:23:52 +0200 | [diff] [blame] | 4046 | nand_hw_control_init(chip->controller); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4047 | } |
| 4048 | |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 4049 | if (!chip->buf_align) |
| 4050 | chip->buf_align = 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4051 | } |
| 4052 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4053 | /* Sanitize ONFI strings so we can safely print them */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4054 | static void sanitize_string(uint8_t *s, size_t len) |
| 4055 | { |
| 4056 | ssize_t i; |
| 4057 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4058 | /* Null terminate */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4059 | s[len - 1] = 0; |
| 4060 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4061 | /* Remove non printable chars */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4062 | for (i = 0; i < len - 1; i++) { |
| 4063 | if (s[i] < ' ' || s[i] > 127) |
| 4064 | s[i] = '?'; |
| 4065 | } |
| 4066 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4067 | /* Remove trailing spaces */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4068 | strim(s); |
| 4069 | } |
| 4070 | |
| 4071 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) |
| 4072 | { |
| 4073 | int i; |
| 4074 | while (len--) { |
| 4075 | crc ^= *p++ << 8; |
| 4076 | for (i = 0; i < 8; i++) |
| 4077 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 4078 | } |
| 4079 | |
| 4080 | return crc; |
| 4081 | } |
| 4082 | |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4083 | /* Parse the Extended Parameter Page. */ |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4084 | static int nand_flash_detect_ext_param_page(struct nand_chip *chip, |
| 4085 | struct nand_onfi_params *p) |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4086 | { |
| 4087 | struct onfi_ext_param_page *ep; |
| 4088 | struct onfi_ext_section *s; |
| 4089 | struct onfi_ext_ecc_info *ecc; |
| 4090 | uint8_t *cursor; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4091 | int ret; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4092 | int len; |
| 4093 | int i; |
| 4094 | |
| 4095 | len = le16_to_cpu(p->ext_param_page_length) * 16; |
| 4096 | ep = kmalloc(len, GFP_KERNEL); |
Brian Norris | 5cb1327 | 2013-09-16 17:59:20 -0700 | [diff] [blame] | 4097 | if (!ep) |
| 4098 | return -ENOMEM; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4099 | |
| 4100 | /* Send our own NAND_CMD_PARAM. */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4101 | ret = nand_read_param_page_op(chip, 0, NULL, 0); |
| 4102 | if (ret) |
| 4103 | goto ext_out; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4104 | |
| 4105 | /* Use the Change Read Column command to skip the ONFI param pages. */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4106 | ret = nand_change_read_column_op(chip, |
| 4107 | sizeof(*p) * p->num_of_param_pages, |
| 4108 | ep, len, true); |
| 4109 | if (ret) |
| 4110 | goto ext_out; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4111 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4112 | ret = -EINVAL; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4113 | if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2) |
| 4114 | != le16_to_cpu(ep->crc))) { |
| 4115 | pr_debug("fail in the CRC.\n"); |
| 4116 | goto ext_out; |
| 4117 | } |
| 4118 | |
| 4119 | /* |
| 4120 | * Check the signature. |
| 4121 | * Do not strictly follow the ONFI spec, maybe changed in future. |
| 4122 | */ |
| 4123 | if (strncmp(ep->sig, "EPPS", 4)) { |
| 4124 | pr_debug("The signature is invalid.\n"); |
| 4125 | goto ext_out; |
| 4126 | } |
| 4127 | |
| 4128 | /* find the ECC section. */ |
| 4129 | cursor = (uint8_t *)(ep + 1); |
| 4130 | for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) { |
| 4131 | s = ep->sections + i; |
| 4132 | if (s->type == ONFI_SECTION_TYPE_2) |
| 4133 | break; |
| 4134 | cursor += s->length * 16; |
| 4135 | } |
| 4136 | if (i == ONFI_EXT_SECTION_MAX) { |
| 4137 | pr_debug("We can not find the ECC section.\n"); |
| 4138 | goto ext_out; |
| 4139 | } |
| 4140 | |
| 4141 | /* get the info we want. */ |
| 4142 | ecc = (struct onfi_ext_ecc_info *)cursor; |
| 4143 | |
Brian Norris | 4ae7d22 | 2013-09-16 18:20:21 -0700 | [diff] [blame] | 4144 | if (!ecc->codeword_size) { |
| 4145 | pr_debug("Invalid codeword size\n"); |
| 4146 | goto ext_out; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4147 | } |
| 4148 | |
Brian Norris | 4ae7d22 | 2013-09-16 18:20:21 -0700 | [diff] [blame] | 4149 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 4150 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
Brian Norris | 5cb1327 | 2013-09-16 17:59:20 -0700 | [diff] [blame] | 4151 | ret = 0; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4152 | |
| 4153 | ext_out: |
| 4154 | kfree(ep); |
| 4155 | return ret; |
| 4156 | } |
| 4157 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4158 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4159 | * 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] | 4160 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4161 | static int nand_flash_detect_onfi(struct nand_chip *chip) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4162 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4163 | struct mtd_info *mtd = nand_to_mtd(chip); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4164 | struct nand_onfi_params *p = &chip->onfi_params; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4165 | char id[4]; |
| 4166 | int i, ret, val; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4167 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4168 | /* Try ONFI for unknown chip or LP */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4169 | ret = nand_readid_op(chip, 0x20, id, sizeof(id)); |
| 4170 | if (ret || strncmp(id, "ONFI", 4)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4171 | return 0; |
| 4172 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4173 | ret = nand_read_param_page_op(chip, 0, NULL, 0); |
| 4174 | if (ret) |
| 4175 | return 0; |
| 4176 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4177 | for (i = 0; i < 3; i++) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4178 | ret = nand_read_data_op(chip, p, sizeof(*p), true); |
| 4179 | if (ret) |
| 4180 | return 0; |
| 4181 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4182 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == |
| 4183 | le16_to_cpu(p->crc)) { |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4184 | break; |
| 4185 | } |
| 4186 | } |
| 4187 | |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 4188 | if (i == 3) { |
| 4189 | pr_err("Could not find valid ONFI parameter page; aborting\n"); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4190 | return 0; |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 4191 | } |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4192 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4193 | /* Check version */ |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4194 | val = le16_to_cpu(p->revision); |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 4195 | if (val & (1 << 5)) |
| 4196 | chip->onfi_version = 23; |
| 4197 | else if (val & (1 << 4)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4198 | chip->onfi_version = 22; |
| 4199 | else if (val & (1 << 3)) |
| 4200 | chip->onfi_version = 21; |
| 4201 | else if (val & (1 << 2)) |
| 4202 | chip->onfi_version = 20; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 4203 | else if (val & (1 << 1)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4204 | chip->onfi_version = 10; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 4205 | |
| 4206 | if (!chip->onfi_version) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4207 | pr_info("unsupported ONFI version: %d\n", val); |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 4208 | return 0; |
| 4209 | } |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4210 | |
| 4211 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 4212 | sanitize_string(p->model, sizeof(p->model)); |
| 4213 | if (!mtd->name) |
| 4214 | mtd->name = p->model; |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 4215 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4216 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 4217 | |
| 4218 | /* |
| 4219 | * pages_per_block and blocks_per_lun may not be a power-of-2 size |
| 4220 | * (don't ask me who thought of this...). MTD assumes that these |
| 4221 | * dimensions will be power-of-2, so just truncate the remaining area. |
| 4222 | */ |
| 4223 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 4224 | mtd->erasesize *= mtd->writesize; |
| 4225 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4226 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 4227 | |
| 4228 | /* See erasesize comment */ |
| 4229 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
Matthieu CASTET | 6379575 | 2012-03-19 15:35:25 +0100 | [diff] [blame] | 4230 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
Huang Shijie | 13fbd17 | 2013-09-25 14:58:13 +0800 | [diff] [blame] | 4231 | chip->bits_per_cell = p->bits_per_cell; |
Huang Shijie | e2985fc | 2013-05-17 11:17:30 +0800 | [diff] [blame] | 4232 | |
Zach Brown | 34da5f5 | 2017-01-10 13:30:21 -0600 | [diff] [blame] | 4233 | chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun); |
| 4234 | chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun); |
| 4235 | |
Huang Shijie | e2985fc | 2013-05-17 11:17:30 +0800 | [diff] [blame] | 4236 | if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS) |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4237 | chip->options |= NAND_BUSWIDTH_16; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4238 | |
Huang Shijie | 10c86ba | 2013-05-17 11:17:26 +0800 | [diff] [blame] | 4239 | if (p->ecc_bits != 0xff) { |
| 4240 | chip->ecc_strength_ds = p->ecc_bits; |
| 4241 | chip->ecc_step_ds = 512; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4242 | } else if (chip->onfi_version >= 21 && |
| 4243 | (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) { |
| 4244 | |
| 4245 | /* |
| 4246 | * The nand_flash_detect_ext_param_page() uses the |
| 4247 | * Change Read Column command which maybe not supported |
| 4248 | * by the chip->cmdfunc. So try to update the chip->cmdfunc |
| 4249 | * now. We do not replace user supplied command function. |
| 4250 | */ |
| 4251 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 4252 | chip->cmdfunc = nand_command_lp; |
| 4253 | |
| 4254 | /* The Extended Parameter Page is supported since ONFI 2.1. */ |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4255 | if (nand_flash_detect_ext_param_page(chip, p)) |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 4256 | pr_warn("Failed to detect ONFI extended param page\n"); |
| 4257 | } else { |
| 4258 | pr_warn("Could not retrieve ONFI ECC requirements\n"); |
Huang Shijie | 10c86ba | 2013-05-17 11:17:26 +0800 | [diff] [blame] | 4259 | } |
| 4260 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4261 | return 1; |
| 4262 | } |
| 4263 | |
| 4264 | /* |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4265 | * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise. |
| 4266 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4267 | static int nand_flash_detect_jedec(struct nand_chip *chip) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4268 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4269 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4270 | struct nand_jedec_params *p = &chip->jedec_params; |
| 4271 | struct jedec_ecc_info *ecc; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4272 | char id[5]; |
| 4273 | int i, val, ret; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4274 | |
| 4275 | /* Try JEDEC for unknown chip or LP */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4276 | ret = nand_readid_op(chip, 0x40, id, sizeof(id)); |
| 4277 | if (ret || strncmp(id, "JEDEC", sizeof(id))) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4278 | return 0; |
| 4279 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4280 | ret = nand_read_param_page_op(chip, 0x40, NULL, 0); |
| 4281 | if (ret) |
| 4282 | return 0; |
| 4283 | |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4284 | for (i = 0; i < 3; i++) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4285 | ret = nand_read_data_op(chip, p, sizeof(*p), true); |
| 4286 | if (ret) |
| 4287 | return 0; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4288 | |
| 4289 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) == |
| 4290 | le16_to_cpu(p->crc)) |
| 4291 | break; |
| 4292 | } |
| 4293 | |
| 4294 | if (i == 3) { |
| 4295 | pr_err("Could not find valid JEDEC parameter page; aborting\n"); |
| 4296 | return 0; |
| 4297 | } |
| 4298 | |
| 4299 | /* Check version */ |
| 4300 | val = le16_to_cpu(p->revision); |
| 4301 | if (val & (1 << 2)) |
| 4302 | chip->jedec_version = 10; |
| 4303 | else if (val & (1 << 1)) |
| 4304 | chip->jedec_version = 1; /* vendor specific version */ |
| 4305 | |
| 4306 | if (!chip->jedec_version) { |
| 4307 | pr_info("unsupported JEDEC version: %d\n", val); |
| 4308 | return 0; |
| 4309 | } |
| 4310 | |
| 4311 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 4312 | sanitize_string(p->model, sizeof(p->model)); |
| 4313 | if (!mtd->name) |
| 4314 | mtd->name = p->model; |
| 4315 | |
| 4316 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 4317 | |
| 4318 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 4319 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 4320 | mtd->erasesize *= mtd->writesize; |
| 4321 | |
| 4322 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 4323 | |
| 4324 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 4325 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
| 4326 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 4327 | chip->bits_per_cell = p->bits_per_cell; |
| 4328 | |
| 4329 | if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS) |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4330 | chip->options |= NAND_BUSWIDTH_16; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4331 | |
| 4332 | /* ECC info */ |
| 4333 | ecc = &p->ecc_info[0]; |
| 4334 | |
| 4335 | if (ecc->codeword_size >= 9) { |
| 4336 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 4337 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 4338 | } else { |
| 4339 | pr_warn("Invalid codeword size\n"); |
| 4340 | } |
| 4341 | |
| 4342 | return 1; |
| 4343 | } |
| 4344 | |
| 4345 | /* |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 4346 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 4347 | * @id_data: the ID string |
| 4348 | * @arrlen: the length of the @id_data array |
| 4349 | * @period: the period of repitition |
| 4350 | * |
| 4351 | * Check if an ID string is repeated within a given sequence of bytes at |
| 4352 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Brian Norris | d4d4f1b | 2012-11-14 21:54:20 -0800 | [diff] [blame] | 4353 | * 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] | 4354 | * if the repetition has a period of @period; otherwise, returns zero. |
| 4355 | */ |
| 4356 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 4357 | { |
| 4358 | int i, j; |
| 4359 | for (i = 0; i < period; i++) |
| 4360 | for (j = i + period; j < arrlen; j += period) |
| 4361 | if (id_data[i] != id_data[j]) |
| 4362 | return 0; |
| 4363 | return 1; |
| 4364 | } |
| 4365 | |
| 4366 | /* |
| 4367 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 4368 | * @id_data: the ID string |
| 4369 | * @arrlen: the length of the @id_data array |
| 4370 | |
| 4371 | * Returns the length of the ID string, according to known wraparound/trailing |
| 4372 | * zero patterns. If no pattern exists, returns the length of the array. |
| 4373 | */ |
| 4374 | static int nand_id_len(u8 *id_data, int arrlen) |
| 4375 | { |
| 4376 | int last_nonzero, period; |
| 4377 | |
| 4378 | /* Find last non-zero byte */ |
| 4379 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 4380 | if (id_data[last_nonzero]) |
| 4381 | break; |
| 4382 | |
| 4383 | /* All zeros */ |
| 4384 | if (last_nonzero < 0) |
| 4385 | return 0; |
| 4386 | |
| 4387 | /* Calculate wraparound period */ |
| 4388 | for (period = 1; period < arrlen; period++) |
| 4389 | if (nand_id_has_period(id_data, arrlen, period)) |
| 4390 | break; |
| 4391 | |
| 4392 | /* There's a repeated pattern */ |
| 4393 | if (period < arrlen) |
| 4394 | return period; |
| 4395 | |
| 4396 | /* There are trailing zeros */ |
| 4397 | if (last_nonzero < arrlen - 1) |
| 4398 | return last_nonzero + 1; |
| 4399 | |
| 4400 | /* No pattern detected */ |
| 4401 | return arrlen; |
| 4402 | } |
| 4403 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 4404 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 4405 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 4406 | { |
| 4407 | int bits; |
| 4408 | |
| 4409 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 4410 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 4411 | return bits + 1; |
| 4412 | } |
| 4413 | |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 4414 | /* |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4415 | * Many new NAND share similar device ID codes, which represent the size of the |
| 4416 | * chip. The rest of the parameters must be decoded according to generic or |
| 4417 | * manufacturer-specific "extended ID" decoding patterns. |
| 4418 | */ |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4419 | void nand_decode_ext_id(struct nand_chip *chip) |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4420 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4421 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 9b2d61f | 2016-06-08 10:34:57 +0200 | [diff] [blame] | 4422 | int extid; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4423 | u8 *id_data = chip->id.data; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4424 | /* The 3rd id byte holds MLC / multichip data */ |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 4425 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4426 | /* The 4th id byte is the important one */ |
| 4427 | extid = id_data[3]; |
| 4428 | |
Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 4429 | /* Calc pagesize */ |
| 4430 | mtd->writesize = 1024 << (extid & 0x03); |
| 4431 | extid >>= 2; |
| 4432 | /* Calc oobsize */ |
| 4433 | mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9); |
| 4434 | extid >>= 2; |
| 4435 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 4436 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 4437 | extid >>= 2; |
| 4438 | /* Get buswidth information */ |
| 4439 | if (extid & 0x1) |
| 4440 | chip->options |= NAND_BUSWIDTH_16; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4441 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4442 | EXPORT_SYMBOL_GPL(nand_decode_ext_id); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4443 | |
| 4444 | /* |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4445 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 4446 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 4447 | * the chip. |
| 4448 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4449 | 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] | 4450 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4451 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4452 | |
| 4453 | mtd->erasesize = type->erasesize; |
| 4454 | mtd->writesize = type->pagesize; |
| 4455 | mtd->oobsize = mtd->writesize / 32; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4456 | |
Huang Shijie | 1c195e9 | 2013-09-25 14:58:12 +0800 | [diff] [blame] | 4457 | /* All legacy ID NAND are small-page, SLC */ |
| 4458 | chip->bits_per_cell = 1; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4459 | } |
| 4460 | |
| 4461 | /* |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4462 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 4463 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 4464 | * page size, cell-type information). |
| 4465 | */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4466 | static void nand_decode_bbm_options(struct nand_chip *chip) |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4467 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4468 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4469 | |
| 4470 | /* Set the bad block position */ |
| 4471 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 4472 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 4473 | else |
| 4474 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4475 | } |
| 4476 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4477 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 4478 | { |
| 4479 | return type->id_len; |
| 4480 | } |
| 4481 | |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4482 | static bool find_full_id_nand(struct nand_chip *chip, |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4483 | struct nand_flash_dev *type) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4484 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4485 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4486 | u8 *id_data = chip->id.data; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4487 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4488 | if (!strncmp(type->id, id_data, type->id_len)) { |
| 4489 | mtd->writesize = type->pagesize; |
| 4490 | mtd->erasesize = type->erasesize; |
| 4491 | mtd->oobsize = type->oobsize; |
| 4492 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 4493 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4494 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 4495 | chip->options |= type->options; |
Huang Shijie | 5721934 | 2013-05-17 11:17:32 +0800 | [diff] [blame] | 4496 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
| 4497 | chip->ecc_step_ds = NAND_ECC_STEP(type); |
Boris BREZILLON | 57a94e2 | 2014-09-22 20:11:50 +0200 | [diff] [blame] | 4498 | chip->onfi_timing_mode_default = |
| 4499 | type->onfi_timing_mode_default; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4500 | |
Cai Zhiyong | 092b6a1 | 2013-12-25 21:19:21 +0800 | [diff] [blame] | 4501 | if (!mtd->name) |
| 4502 | mtd->name = type->name; |
| 4503 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4504 | return true; |
| 4505 | } |
| 4506 | return false; |
| 4507 | } |
| 4508 | |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4509 | /* |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4510 | * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC |
| 4511 | * compliant and does not have a full-id or legacy-id entry in the nand_ids |
| 4512 | * table. |
| 4513 | */ |
| 4514 | static void nand_manufacturer_detect(struct nand_chip *chip) |
| 4515 | { |
| 4516 | /* |
| 4517 | * Try manufacturer detection if available and use |
| 4518 | * nand_decode_ext_id() otherwise. |
| 4519 | */ |
| 4520 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4521 | chip->manufacturer.desc->ops->detect) { |
| 4522 | /* The 3rd id byte holds MLC / multichip data */ |
| 4523 | chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4524 | chip->manufacturer.desc->ops->detect(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4525 | } else { |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4526 | nand_decode_ext_id(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4527 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | /* |
| 4531 | * Manufacturer initialization. This function is called for all NANDs including |
| 4532 | * ONFI and JEDEC compliant ones. |
| 4533 | * Manufacturer drivers should put all their specific initialization code in |
| 4534 | * their ->init() hook. |
| 4535 | */ |
| 4536 | static int nand_manufacturer_init(struct nand_chip *chip) |
| 4537 | { |
| 4538 | if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops || |
| 4539 | !chip->manufacturer.desc->ops->init) |
| 4540 | return 0; |
| 4541 | |
| 4542 | return chip->manufacturer.desc->ops->init(chip); |
| 4543 | } |
| 4544 | |
| 4545 | /* |
| 4546 | * Manufacturer cleanup. This function is called for all NANDs including |
| 4547 | * ONFI and JEDEC compliant ones. |
| 4548 | * Manufacturer drivers should put all their specific cleanup code in their |
| 4549 | * ->cleanup() hook. |
| 4550 | */ |
| 4551 | static void nand_manufacturer_cleanup(struct nand_chip *chip) |
| 4552 | { |
| 4553 | /* Release manufacturer private data */ |
| 4554 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
| 4555 | chip->manufacturer.desc->ops->cleanup) |
| 4556 | chip->manufacturer.desc->ops->cleanup(chip); |
| 4557 | } |
| 4558 | |
| 4559 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4560 | * 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] | 4561 | */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 4562 | 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] | 4563 | { |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4564 | const struct nand_manufacturer *manufacturer; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4565 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4566 | int busw, ret; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4567 | u8 *id_data = chip->id.data; |
| 4568 | u8 maf_id, dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4570 | /* |
| 4571 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4572 | * after power-up. |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4573 | */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4574 | ret = nand_reset(chip, 0); |
| 4575 | if (ret) |
| 4576 | return ret; |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4577 | |
| 4578 | /* Select the device */ |
| 4579 | chip->select_chip(mtd, 0); |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | /* Send the command for reading device ID */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4582 | ret = nand_readid_op(chip, 0, id_data, 2); |
| 4583 | if (ret) |
| 4584 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | |
| 4586 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4587 | maf_id = id_data[0]; |
| 4588 | dev_id = id_data[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4590 | /* |
| 4591 | * 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] | 4592 | * interface concerns can cause random data which looks like a |
| 4593 | * possibly credible NAND flash to appear. If the two results do |
| 4594 | * not match, ignore the device completely. |
| 4595 | */ |
| 4596 | |
Brian Norris | 4aef9b7 | 2012-09-24 20:40:48 -0700 | [diff] [blame] | 4597 | /* Read entire ID string */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4598 | ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data)); |
| 4599 | if (ret) |
| 4600 | return ret; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 4601 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4602 | if (id_data[0] != maf_id || id_data[1] != dev_id) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4603 | 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] | 4604 | maf_id, dev_id, id_data[0], id_data[1]); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4605 | return -ENODEV; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 4606 | } |
| 4607 | |
Jean-Louis Thekekara | 5158bd5 | 2017-06-29 19:08:30 +0200 | [diff] [blame] | 4608 | chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data)); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4609 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4610 | /* Try to identify manufacturer */ |
| 4611 | manufacturer = nand_get_manufacturer(maf_id); |
| 4612 | chip->manufacturer.desc = manufacturer; |
| 4613 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4614 | if (!type) |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4615 | type = nand_flash_ids; |
| 4616 | |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4617 | /* |
| 4618 | * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic |
| 4619 | * override it. |
| 4620 | * This is required to make sure initial NAND bus width set by the |
| 4621 | * NAND controller driver is coherent with the real NAND bus width |
| 4622 | * (extracted by auto-detection code). |
| 4623 | */ |
| 4624 | busw = chip->options & NAND_BUSWIDTH_16; |
| 4625 | |
| 4626 | /* |
| 4627 | * The flag is only set (never cleared), reset it to its default value |
| 4628 | * before starting auto-detection. |
| 4629 | */ |
| 4630 | chip->options &= ~NAND_BUSWIDTH_16; |
| 4631 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4632 | for (; type->name != NULL; type++) { |
| 4633 | if (is_full_id_nand(type)) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4634 | if (find_full_id_nand(chip, type)) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4635 | goto ident_done; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4636 | } else if (dev_id == type->dev_id) { |
Brian Norris | db5b09f | 2015-05-22 10:43:12 -0700 | [diff] [blame] | 4637 | break; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4638 | } |
| 4639 | } |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4640 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4641 | chip->onfi_version = 0; |
| 4642 | if (!type->name || !type->pagesize) { |
Masahiro Yamada | 35fc519 | 2014-04-09 16:26:26 +0900 | [diff] [blame] | 4643 | /* Check if the chip is ONFI compliant */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4644 | if (nand_flash_detect_onfi(chip)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4645 | goto ident_done; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4646 | |
| 4647 | /* Check if the chip is JEDEC compliant */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4648 | if (nand_flash_detect_jedec(chip)) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4649 | goto ident_done; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4650 | } |
| 4651 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4652 | if (!type->name) |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4653 | return -ENODEV; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4654 | |
Thomas Gleixner | ba0251fe | 2006-05-27 01:02:13 +0200 | [diff] [blame] | 4655 | if (!mtd->name) |
| 4656 | mtd->name = type->name; |
| 4657 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4658 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4659 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4660 | if (!type->pagesize) |
| 4661 | nand_manufacturer_detect(chip); |
| 4662 | else |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4663 | nand_decode_id(chip, type); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4664 | |
Brian Norris | bf7a01b | 2012-07-13 09:28:24 -0700 | [diff] [blame] | 4665 | /* Get chip options */ |
| 4666 | chip->options |= type->options; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4667 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4668 | ident_done: |
| 4669 | |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 4670 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4671 | WARN_ON(busw & NAND_BUSWIDTH_16); |
| 4672 | nand_set_defaults(chip); |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 4673 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 4674 | /* |
| 4675 | * Check, if buswidth is correct. Hardware drivers should set |
| 4676 | * chip correct! |
| 4677 | */ |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4678 | 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] | 4679 | maf_id, dev_id); |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4680 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4681 | mtd->name); |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4682 | pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8, |
| 4683 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4684 | return -EINVAL; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4685 | } |
| 4686 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4687 | nand_decode_bbm_options(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4688 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4689 | /* Calculate the address shift from the page size */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4690 | chip->page_shift = ffs(mtd->writesize) - 1; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4691 | /* Convert chipsize to number of pages per chip -1 */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4692 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4693 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4694 | chip->bbt_erase_shift = chip->phys_erase_shift = |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4695 | ffs(mtd->erasesize) - 1; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4696 | if (chip->chipsize & 0xffffffff) |
| 4697 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4698 | else { |
| 4699 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 4700 | chip->chip_shift += 32 - 1; |
| 4701 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4702 | |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame] | 4703 | if (chip->chip_shift - chip->page_shift > 16) |
| 4704 | chip->options |= NAND_ROW_ADDR_3; |
| 4705 | |
Artem Bityutskiy | 26d9be1 | 2011-04-28 20:26:59 +0300 | [diff] [blame] | 4706 | chip->badblockbits = 8; |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4707 | chip->erase = single_erase; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4708 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4709 | /* Do not replace user supplied command function! */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4710 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 4711 | chip->cmdfunc = nand_command_lp; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4712 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4713 | 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] | 4714 | maf_id, dev_id); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4715 | |
| 4716 | if (chip->onfi_version) |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4717 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4718 | chip->onfi_params.model); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4719 | else if (chip->jedec_version) |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4720 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4721 | chip->jedec_params.model); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4722 | else |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4723 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4724 | type->name); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4725 | |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4726 | 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] | 4727 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4728 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4729 | return 0; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4730 | } |
| 4731 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4732 | static const char * const nand_ecc_modes[] = { |
| 4733 | [NAND_ECC_NONE] = "none", |
| 4734 | [NAND_ECC_SOFT] = "soft", |
| 4735 | [NAND_ECC_HW] = "hw", |
| 4736 | [NAND_ECC_HW_SYNDROME] = "hw_syndrome", |
| 4737 | [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first", |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 4738 | [NAND_ECC_ON_DIE] = "on-die", |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4739 | }; |
| 4740 | |
| 4741 | static int of_get_nand_ecc_mode(struct device_node *np) |
| 4742 | { |
| 4743 | const char *pm; |
| 4744 | int err, i; |
| 4745 | |
| 4746 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4747 | if (err < 0) |
| 4748 | return err; |
| 4749 | |
| 4750 | for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++) |
| 4751 | if (!strcasecmp(pm, nand_ecc_modes[i])) |
| 4752 | return i; |
| 4753 | |
Rafał Miłecki | ae211bc | 2016-04-17 22:53:06 +0200 | [diff] [blame] | 4754 | /* |
| 4755 | * For backward compatibility we support few obsoleted values that don't |
| 4756 | * have their mappings into nand_ecc_modes_t anymore (they were merged |
| 4757 | * with other enums). |
| 4758 | */ |
| 4759 | if (!strcasecmp(pm, "soft_bch")) |
| 4760 | return NAND_ECC_SOFT; |
| 4761 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4762 | return -ENODEV; |
| 4763 | } |
| 4764 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4765 | static const char * const nand_ecc_algos[] = { |
| 4766 | [NAND_ECC_HAMMING] = "hamming", |
| 4767 | [NAND_ECC_BCH] = "bch", |
| 4768 | }; |
| 4769 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4770 | static int of_get_nand_ecc_algo(struct device_node *np) |
| 4771 | { |
| 4772 | const char *pm; |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4773 | int err, i; |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4774 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4775 | err = of_property_read_string(np, "nand-ecc-algo", &pm); |
| 4776 | if (!err) { |
| 4777 | for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++) |
| 4778 | if (!strcasecmp(pm, nand_ecc_algos[i])) |
| 4779 | return i; |
| 4780 | return -ENODEV; |
| 4781 | } |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4782 | |
| 4783 | /* |
| 4784 | * For backward compatibility we also read "nand-ecc-mode" checking |
| 4785 | * for some obsoleted values that were specifying ECC algorithm. |
| 4786 | */ |
| 4787 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4788 | if (err < 0) |
| 4789 | return err; |
| 4790 | |
| 4791 | if (!strcasecmp(pm, "soft")) |
| 4792 | return NAND_ECC_HAMMING; |
| 4793 | else if (!strcasecmp(pm, "soft_bch")) |
| 4794 | return NAND_ECC_BCH; |
| 4795 | |
| 4796 | return -ENODEV; |
| 4797 | } |
| 4798 | |
| 4799 | static int of_get_nand_ecc_step_size(struct device_node *np) |
| 4800 | { |
| 4801 | int ret; |
| 4802 | u32 val; |
| 4803 | |
| 4804 | ret = of_property_read_u32(np, "nand-ecc-step-size", &val); |
| 4805 | return ret ? ret : val; |
| 4806 | } |
| 4807 | |
| 4808 | static int of_get_nand_ecc_strength(struct device_node *np) |
| 4809 | { |
| 4810 | int ret; |
| 4811 | u32 val; |
| 4812 | |
| 4813 | ret = of_property_read_u32(np, "nand-ecc-strength", &val); |
| 4814 | return ret ? ret : val; |
| 4815 | } |
| 4816 | |
| 4817 | static int of_get_nand_bus_width(struct device_node *np) |
| 4818 | { |
| 4819 | u32 val; |
| 4820 | |
| 4821 | if (of_property_read_u32(np, "nand-bus-width", &val)) |
| 4822 | return 8; |
| 4823 | |
| 4824 | switch (val) { |
| 4825 | case 8: |
| 4826 | case 16: |
| 4827 | return val; |
| 4828 | default: |
| 4829 | return -EIO; |
| 4830 | } |
| 4831 | } |
| 4832 | |
| 4833 | static bool of_get_nand_on_flash_bbt(struct device_node *np) |
| 4834 | { |
| 4835 | return of_property_read_bool(np, "nand-on-flash-bbt"); |
| 4836 | } |
| 4837 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4838 | static int nand_dt_init(struct nand_chip *chip) |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4839 | { |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4840 | struct device_node *dn = nand_get_flash_node(chip); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4841 | int ecc_mode, ecc_algo, ecc_strength, ecc_step; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4842 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4843 | if (!dn) |
| 4844 | return 0; |
| 4845 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4846 | if (of_get_nand_bus_width(dn) == 16) |
| 4847 | chip->options |= NAND_BUSWIDTH_16; |
| 4848 | |
| 4849 | if (of_get_nand_on_flash_bbt(dn)) |
| 4850 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 4851 | |
| 4852 | ecc_mode = of_get_nand_ecc_mode(dn); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4853 | ecc_algo = of_get_nand_ecc_algo(dn); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4854 | ecc_strength = of_get_nand_ecc_strength(dn); |
| 4855 | ecc_step = of_get_nand_ecc_step_size(dn); |
| 4856 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4857 | if (ecc_mode >= 0) |
| 4858 | chip->ecc.mode = ecc_mode; |
| 4859 | |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4860 | if (ecc_algo >= 0) |
| 4861 | chip->ecc.algo = ecc_algo; |
| 4862 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4863 | if (ecc_strength >= 0) |
| 4864 | chip->ecc.strength = ecc_strength; |
| 4865 | |
| 4866 | if (ecc_step > 0) |
| 4867 | chip->ecc.size = ecc_step; |
| 4868 | |
Boris Brezillon | ba78ee0 | 2016-06-08 17:04:22 +0200 | [diff] [blame] | 4869 | if (of_property_read_bool(dn, "nand-ecc-maximize")) |
| 4870 | chip->ecc.options |= NAND_ECC_MAXIMIZE; |
| 4871 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4872 | return 0; |
| 4873 | } |
| 4874 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4875 | /** |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4876 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4877 | * @mtd: MTD device structure |
| 4878 | * @maxchips: number of chips to scan for |
| 4879 | * @table: alternative NAND ID table |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4880 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4881 | * This is the first phase of the normal nand_scan() function. It reads the |
| 4882 | * flash ID and sets up MTD fields accordingly. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4883 | * |
| 4884 | */ |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4885 | int nand_scan_ident(struct mtd_info *mtd, int maxchips, |
| 4886 | struct nand_flash_dev *table) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4887 | { |
Cai Zhiyong | bb77082 | 2013-12-25 20:11:15 +0800 | [diff] [blame] | 4888 | int i, nand_maf_id, nand_dev_id; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4889 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4890 | int ret; |
| 4891 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4892 | ret = nand_dt_init(chip); |
| 4893 | if (ret) |
| 4894 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4895 | |
Brian Norris | f7a8e38 | 2016-01-05 10:39:45 -0800 | [diff] [blame] | 4896 | if (!mtd->name && mtd->dev.parent) |
| 4897 | mtd->name = dev_name(mtd->dev.parent); |
| 4898 | |
Andrey Smirnov | 76fe334 | 2016-07-21 14:59:20 -0700 | [diff] [blame] | 4899 | if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) { |
| 4900 | /* |
| 4901 | * Default functions assigned for chip_select() and |
| 4902 | * cmdfunc() both expect cmd_ctrl() to be populated, |
| 4903 | * so we need to check that that's the case |
| 4904 | */ |
| 4905 | pr_err("chip.cmd_ctrl() callback is not provided"); |
| 4906 | return -EINVAL; |
| 4907 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4908 | /* Set the default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4909 | nand_set_defaults(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4910 | |
| 4911 | /* Read the flash type */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 4912 | ret = nand_detect(chip, table); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4913 | if (ret) { |
Ben Dooks | b1c6e6d | 2009-11-02 18:12:33 +0000 | [diff] [blame] | 4914 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4915 | pr_warn("No NAND device found\n"); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4916 | chip->select_chip(mtd, -1); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4917 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4918 | } |
| 4919 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4920 | nand_maf_id = chip->id.data[0]; |
| 4921 | nand_dev_id = chip->id.data[1]; |
| 4922 | |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 4923 | chip->select_chip(mtd, -1); |
| 4924 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4925 | /* Check for a chip array */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4926 | for (i = 1; i < maxchips; i++) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4927 | u8 id[2]; |
| 4928 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4929 | /* See comment in nand_get_flash_type for reset */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4930 | nand_reset(chip, i); |
| 4931 | |
| 4932 | chip->select_chip(mtd, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | /* Send the command for reading device ID */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4934 | nand_readid_op(chip, 0, id, sizeof(id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4936 | if (nand_maf_id != id[0] || nand_dev_id != id[1]) { |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 4937 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4938 | break; |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 4939 | } |
| 4940 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | } |
| 4942 | if (i > 1) |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4943 | pr_info("%d chips detected\n", i); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4945 | /* Store the number of chips and calc total size for mtd */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4946 | chip->numchips = i; |
| 4947 | mtd->size = i * chip->chipsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4949 | return 0; |
| 4950 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4951 | EXPORT_SYMBOL(nand_scan_ident); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4952 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4953 | static int nand_set_ecc_soft_ops(struct mtd_info *mtd) |
| 4954 | { |
| 4955 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4956 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4957 | |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 4958 | if (WARN_ON(ecc->mode != NAND_ECC_SOFT)) |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4959 | return -EINVAL; |
| 4960 | |
| 4961 | switch (ecc->algo) { |
| 4962 | case NAND_ECC_HAMMING: |
| 4963 | ecc->calculate = nand_calculate_ecc; |
| 4964 | ecc->correct = nand_correct_data; |
| 4965 | ecc->read_page = nand_read_page_swecc; |
| 4966 | ecc->read_subpage = nand_read_subpage; |
| 4967 | ecc->write_page = nand_write_page_swecc; |
| 4968 | ecc->read_page_raw = nand_read_page_raw; |
| 4969 | ecc->write_page_raw = nand_write_page_raw; |
| 4970 | ecc->read_oob = nand_read_oob_std; |
| 4971 | ecc->write_oob = nand_write_oob_std; |
| 4972 | if (!ecc->size) |
| 4973 | ecc->size = 256; |
| 4974 | ecc->bytes = 3; |
| 4975 | ecc->strength = 1; |
| 4976 | return 0; |
| 4977 | case NAND_ECC_BCH: |
| 4978 | if (!mtd_nand_has_bch()) { |
| 4979 | WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n"); |
| 4980 | return -EINVAL; |
| 4981 | } |
| 4982 | ecc->calculate = nand_bch_calculate_ecc; |
| 4983 | ecc->correct = nand_bch_correct_data; |
| 4984 | ecc->read_page = nand_read_page_swecc; |
| 4985 | ecc->read_subpage = nand_read_subpage; |
| 4986 | ecc->write_page = nand_write_page_swecc; |
| 4987 | ecc->read_page_raw = nand_read_page_raw; |
| 4988 | ecc->write_page_raw = nand_write_page_raw; |
| 4989 | ecc->read_oob = nand_read_oob_std; |
| 4990 | ecc->write_oob = nand_write_oob_std; |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 4991 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4992 | /* |
| 4993 | * Board driver should supply ecc.size and ecc.strength |
| 4994 | * values to select how many bits are correctable. |
| 4995 | * Otherwise, default to 4 bits for large page devices. |
| 4996 | */ |
| 4997 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 4998 | ecc->size = 512; |
| 4999 | ecc->strength = 4; |
| 5000 | } |
| 5001 | |
| 5002 | /* |
| 5003 | * if no ecc placement scheme was provided pickup the default |
| 5004 | * large page one. |
| 5005 | */ |
| 5006 | if (!mtd->ooblayout) { |
| 5007 | /* handle large page devices only */ |
| 5008 | if (mtd->oobsize < 64) { |
| 5009 | WARN(1, "OOB layout is required when using software BCH on small pages\n"); |
| 5010 | return -EINVAL; |
| 5011 | } |
| 5012 | |
| 5013 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 5014 | |
| 5015 | } |
| 5016 | |
| 5017 | /* |
| 5018 | * We can only maximize ECC config when the default layout is |
| 5019 | * used, otherwise we don't know how many bytes can really be |
| 5020 | * used. |
| 5021 | */ |
| 5022 | if (mtd->ooblayout == &nand_ooblayout_lp_ops && |
| 5023 | ecc->options & NAND_ECC_MAXIMIZE) { |
| 5024 | int steps, bytes; |
| 5025 | |
| 5026 | /* Always prefer 1k blocks over 512bytes ones */ |
| 5027 | ecc->size = 1024; |
| 5028 | steps = mtd->writesize / ecc->size; |
| 5029 | |
| 5030 | /* Reserve 2 bytes for the BBM */ |
| 5031 | bytes = (mtd->oobsize - 2) / steps; |
| 5032 | ecc->strength = bytes * 8 / fls(8 * ecc->size); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5033 | } |
| 5034 | |
| 5035 | /* See nand_bch_init() for details. */ |
| 5036 | ecc->bytes = 0; |
| 5037 | ecc->priv = nand_bch_init(mtd); |
| 5038 | if (!ecc->priv) { |
| 5039 | WARN(1, "BCH ECC initialization failed!\n"); |
| 5040 | return -EINVAL; |
| 5041 | } |
| 5042 | return 0; |
| 5043 | default: |
| 5044 | WARN(1, "Unsupported ECC algorithm!\n"); |
| 5045 | return -EINVAL; |
| 5046 | } |
| 5047 | } |
| 5048 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5049 | /** |
| 5050 | * nand_check_ecc_caps - check the sanity of preset ECC settings |
| 5051 | * @chip: nand chip info structure |
| 5052 | * @caps: ECC caps info structure |
| 5053 | * @oobavail: OOB size that the ECC engine can use |
| 5054 | * |
| 5055 | * When ECC step size and strength are already set, check if they are supported |
| 5056 | * by the controller and the calculated ECC bytes fit within the chip's OOB. |
| 5057 | * On success, the calculated ECC bytes is set. |
| 5058 | */ |
| 5059 | int nand_check_ecc_caps(struct nand_chip *chip, |
| 5060 | const struct nand_ecc_caps *caps, int oobavail) |
| 5061 | { |
| 5062 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5063 | const struct nand_ecc_step_info *stepinfo; |
| 5064 | int preset_step = chip->ecc.size; |
| 5065 | int preset_strength = chip->ecc.strength; |
| 5066 | int nsteps, ecc_bytes; |
| 5067 | int i, j; |
| 5068 | |
| 5069 | if (WARN_ON(oobavail < 0)) |
| 5070 | return -EINVAL; |
| 5071 | |
| 5072 | if (!preset_step || !preset_strength) |
| 5073 | return -ENODATA; |
| 5074 | |
| 5075 | nsteps = mtd->writesize / preset_step; |
| 5076 | |
| 5077 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5078 | stepinfo = &caps->stepinfos[i]; |
| 5079 | |
| 5080 | if (stepinfo->stepsize != preset_step) |
| 5081 | continue; |
| 5082 | |
| 5083 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5084 | if (stepinfo->strengths[j] != preset_strength) |
| 5085 | continue; |
| 5086 | |
| 5087 | ecc_bytes = caps->calc_ecc_bytes(preset_step, |
| 5088 | preset_strength); |
| 5089 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5090 | return ecc_bytes; |
| 5091 | |
| 5092 | if (ecc_bytes * nsteps > oobavail) { |
| 5093 | pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB", |
| 5094 | preset_step, preset_strength); |
| 5095 | return -ENOSPC; |
| 5096 | } |
| 5097 | |
| 5098 | chip->ecc.bytes = ecc_bytes; |
| 5099 | |
| 5100 | return 0; |
| 5101 | } |
| 5102 | } |
| 5103 | |
| 5104 | pr_err("ECC (step, strength) = (%d, %d) not supported on this controller", |
| 5105 | preset_step, preset_strength); |
| 5106 | |
| 5107 | return -ENOTSUPP; |
| 5108 | } |
| 5109 | EXPORT_SYMBOL_GPL(nand_check_ecc_caps); |
| 5110 | |
| 5111 | /** |
| 5112 | * nand_match_ecc_req - meet the chip's requirement with least ECC bytes |
| 5113 | * @chip: nand chip info structure |
| 5114 | * @caps: ECC engine caps info structure |
| 5115 | * @oobavail: OOB size that the ECC engine can use |
| 5116 | * |
| 5117 | * If a chip's ECC requirement is provided, try to meet it with the least |
| 5118 | * number of ECC bytes (i.e. with the largest number of OOB-free bytes). |
| 5119 | * On success, the chosen ECC settings are set. |
| 5120 | */ |
| 5121 | int nand_match_ecc_req(struct nand_chip *chip, |
| 5122 | const struct nand_ecc_caps *caps, int oobavail) |
| 5123 | { |
| 5124 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5125 | const struct nand_ecc_step_info *stepinfo; |
| 5126 | int req_step = chip->ecc_step_ds; |
| 5127 | int req_strength = chip->ecc_strength_ds; |
| 5128 | int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total; |
| 5129 | int best_step, best_strength, best_ecc_bytes; |
| 5130 | int best_ecc_bytes_total = INT_MAX; |
| 5131 | int i, j; |
| 5132 | |
| 5133 | if (WARN_ON(oobavail < 0)) |
| 5134 | return -EINVAL; |
| 5135 | |
| 5136 | /* No information provided by the NAND chip */ |
| 5137 | if (!req_step || !req_strength) |
| 5138 | return -ENOTSUPP; |
| 5139 | |
| 5140 | /* number of correctable bits the chip requires in a page */ |
| 5141 | req_corr = mtd->writesize / req_step * req_strength; |
| 5142 | |
| 5143 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5144 | stepinfo = &caps->stepinfos[i]; |
| 5145 | step_size = stepinfo->stepsize; |
| 5146 | |
| 5147 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5148 | strength = stepinfo->strengths[j]; |
| 5149 | |
| 5150 | /* |
| 5151 | * If both step size and strength are smaller than the |
| 5152 | * chip's requirement, it is not easy to compare the |
| 5153 | * resulted reliability. |
| 5154 | */ |
| 5155 | if (step_size < req_step && strength < req_strength) |
| 5156 | continue; |
| 5157 | |
| 5158 | if (mtd->writesize % step_size) |
| 5159 | continue; |
| 5160 | |
| 5161 | nsteps = mtd->writesize / step_size; |
| 5162 | |
| 5163 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 5164 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5165 | continue; |
| 5166 | ecc_bytes_total = ecc_bytes * nsteps; |
| 5167 | |
| 5168 | if (ecc_bytes_total > oobavail || |
| 5169 | strength * nsteps < req_corr) |
| 5170 | continue; |
| 5171 | |
| 5172 | /* |
| 5173 | * We assume the best is to meet the chip's requrement |
| 5174 | * with the least number of ECC bytes. |
| 5175 | */ |
| 5176 | if (ecc_bytes_total < best_ecc_bytes_total) { |
| 5177 | best_ecc_bytes_total = ecc_bytes_total; |
| 5178 | best_step = step_size; |
| 5179 | best_strength = strength; |
| 5180 | best_ecc_bytes = ecc_bytes; |
| 5181 | } |
| 5182 | } |
| 5183 | } |
| 5184 | |
| 5185 | if (best_ecc_bytes_total == INT_MAX) |
| 5186 | return -ENOTSUPP; |
| 5187 | |
| 5188 | chip->ecc.size = best_step; |
| 5189 | chip->ecc.strength = best_strength; |
| 5190 | chip->ecc.bytes = best_ecc_bytes; |
| 5191 | |
| 5192 | return 0; |
| 5193 | } |
| 5194 | EXPORT_SYMBOL_GPL(nand_match_ecc_req); |
| 5195 | |
| 5196 | /** |
| 5197 | * nand_maximize_ecc - choose the max ECC strength available |
| 5198 | * @chip: nand chip info structure |
| 5199 | * @caps: ECC engine caps info structure |
| 5200 | * @oobavail: OOB size that the ECC engine can use |
| 5201 | * |
| 5202 | * Choose the max ECC strength that is supported on the controller, and can fit |
| 5203 | * within the chip's OOB. On success, the chosen ECC settings are set. |
| 5204 | */ |
| 5205 | int nand_maximize_ecc(struct nand_chip *chip, |
| 5206 | const struct nand_ecc_caps *caps, int oobavail) |
| 5207 | { |
| 5208 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5209 | const struct nand_ecc_step_info *stepinfo; |
| 5210 | int step_size, strength, nsteps, ecc_bytes, corr; |
| 5211 | int best_corr = 0; |
| 5212 | int best_step = 0; |
| 5213 | int best_strength, best_ecc_bytes; |
| 5214 | int i, j; |
| 5215 | |
| 5216 | if (WARN_ON(oobavail < 0)) |
| 5217 | return -EINVAL; |
| 5218 | |
| 5219 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5220 | stepinfo = &caps->stepinfos[i]; |
| 5221 | step_size = stepinfo->stepsize; |
| 5222 | |
| 5223 | /* If chip->ecc.size is already set, respect it */ |
| 5224 | if (chip->ecc.size && step_size != chip->ecc.size) |
| 5225 | continue; |
| 5226 | |
| 5227 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5228 | strength = stepinfo->strengths[j]; |
| 5229 | |
| 5230 | if (mtd->writesize % step_size) |
| 5231 | continue; |
| 5232 | |
| 5233 | nsteps = mtd->writesize / step_size; |
| 5234 | |
| 5235 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 5236 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5237 | continue; |
| 5238 | |
| 5239 | if (ecc_bytes * nsteps > oobavail) |
| 5240 | continue; |
| 5241 | |
| 5242 | corr = strength * nsteps; |
| 5243 | |
| 5244 | /* |
| 5245 | * If the number of correctable bits is the same, |
| 5246 | * bigger step_size has more reliability. |
| 5247 | */ |
| 5248 | if (corr > best_corr || |
| 5249 | (corr == best_corr && step_size > best_step)) { |
| 5250 | best_corr = corr; |
| 5251 | best_step = step_size; |
| 5252 | best_strength = strength; |
| 5253 | best_ecc_bytes = ecc_bytes; |
| 5254 | } |
| 5255 | } |
| 5256 | } |
| 5257 | |
| 5258 | if (!best_corr) |
| 5259 | return -ENOTSUPP; |
| 5260 | |
| 5261 | chip->ecc.size = best_step; |
| 5262 | chip->ecc.strength = best_strength; |
| 5263 | chip->ecc.bytes = best_ecc_bytes; |
| 5264 | |
| 5265 | return 0; |
| 5266 | } |
| 5267 | EXPORT_SYMBOL_GPL(nand_maximize_ecc); |
| 5268 | |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5269 | /* |
| 5270 | * Check if the chip configuration meet the datasheet requirements. |
| 5271 | |
| 5272 | * If our configuration corrects A bits per B bytes and the minimum |
| 5273 | * required correction level is X bits per Y bytes, then we must ensure |
| 5274 | * both of the following are true: |
| 5275 | * |
| 5276 | * (1) A / B >= X / Y |
| 5277 | * (2) A >= X |
| 5278 | * |
| 5279 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 5280 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 5281 | * in the same sector. |
| 5282 | */ |
| 5283 | static bool nand_ecc_strength_good(struct mtd_info *mtd) |
| 5284 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 5285 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5286 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 5287 | int corr, ds_corr; |
| 5288 | |
| 5289 | if (ecc->size == 0 || chip->ecc_step_ds == 0) |
| 5290 | /* Not enough information */ |
| 5291 | return true; |
| 5292 | |
| 5293 | /* |
| 5294 | * We get the number of corrected bits per page to compare |
| 5295 | * the correction density. |
| 5296 | */ |
| 5297 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
| 5298 | ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; |
| 5299 | |
| 5300 | return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; |
| 5301 | } |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5302 | |
| 5303 | /** |
| 5304 | * nand_scan_tail - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5305 | * @mtd: MTD device structure |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5306 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5307 | * This is the second phase of the normal nand_scan() function. It fills out |
| 5308 | * all the uninitialized function pointers with the defaults and scans for a |
| 5309 | * bad block table if appropriate. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5310 | */ |
| 5311 | int nand_scan_tail(struct mtd_info *mtd) |
| 5312 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 5313 | struct nand_chip *chip = mtd_to_nand(mtd); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5314 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5315 | struct nand_buffers *nbuf = NULL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5316 | int ret, i; |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5317 | |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 5318 | /* 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] | 5319 | if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5320 | !(chip->bbt_options & NAND_BBT_USE_FLASH))) { |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5321 | return -EINVAL; |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5322 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 5323 | |
Huang Shijie | f02ea4e | 2014-01-13 14:27:12 +0800 | [diff] [blame] | 5324 | if (!(chip->options & NAND_OWN_BUFFERS)) { |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5325 | nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5326 | if (!nbuf) |
| 5327 | return -ENOMEM; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5328 | |
| 5329 | nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 5330 | if (!nbuf->ecccalc) { |
| 5331 | ret = -ENOMEM; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5332 | goto err_free_nbuf; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5333 | } |
| 5334 | |
| 5335 | nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 5336 | if (!nbuf->ecccode) { |
| 5337 | ret = -ENOMEM; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5338 | goto err_free_nbuf; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5339 | } |
| 5340 | |
| 5341 | nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize, |
| 5342 | GFP_KERNEL); |
| 5343 | if (!nbuf->databuf) { |
| 5344 | ret = -ENOMEM; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5345 | goto err_free_nbuf; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5346 | } |
Huang Shijie | f02ea4e | 2014-01-13 14:27:12 +0800 | [diff] [blame] | 5347 | |
| 5348 | chip->buffers = nbuf; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5349 | } else if (!chip->buffers) { |
| 5350 | return -ENOMEM; |
Huang Shijie | f02ea4e | 2014-01-13 14:27:12 +0800 | [diff] [blame] | 5351 | } |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 5352 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5353 | /* |
| 5354 | * FIXME: some NAND manufacturer drivers expect the first die to be |
| 5355 | * selected when manufacturer->init() is called. They should be fixed |
| 5356 | * to explictly select the relevant die when interacting with the NAND |
| 5357 | * chip. |
| 5358 | */ |
| 5359 | chip->select_chip(mtd, 0); |
| 5360 | ret = nand_manufacturer_init(chip); |
| 5361 | chip->select_chip(mtd, -1); |
| 5362 | if (ret) |
| 5363 | goto err_free_nbuf; |
| 5364 | |
David Woodhouse | 7dcdcbef | 2006-10-21 17:09:53 +0100 | [diff] [blame] | 5365 | /* Set the internal oob buffer location, just after the page data */ |
David Woodhouse | 784f4d5 | 2006-10-22 01:47:45 +0100 | [diff] [blame] | 5366 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5367 | |
| 5368 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5369 | * If no default placement scheme is given, select an appropriate one. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5370 | */ |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5371 | if (!mtd->ooblayout && |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5372 | !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5373 | switch (mtd->oobsize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | case 8: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5375 | case 16: |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 5376 | mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | break; |
| 5378 | case 64: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5379 | case 128: |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 5380 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops); |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5381 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | default: |
Miquel Raynal | 882fd15 | 2017-08-26 17:19:15 +0200 | [diff] [blame] | 5383 | /* |
| 5384 | * Expose the whole OOB area to users if ECC_NONE |
| 5385 | * is passed. We could do that for all kind of |
| 5386 | * ->oobsize, but we must keep the old large/small |
| 5387 | * page with ECC layout when ->oobsize <= 128 for |
| 5388 | * compatibility reasons. |
| 5389 | */ |
| 5390 | if (ecc->mode == NAND_ECC_NONE) { |
| 5391 | mtd_set_ooblayout(mtd, |
| 5392 | &nand_ooblayout_lp_ops); |
| 5393 | break; |
| 5394 | } |
| 5395 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5396 | WARN(1, "No oob scheme defined for oobsize %d\n", |
| 5397 | mtd->oobsize); |
| 5398 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5399 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5400 | } |
| 5401 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5402 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5403 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5404 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5405 | * selected and we have 256 byte pagesize fallback to software ECC |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5406 | */ |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 5407 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5408 | switch (ecc->mode) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 5409 | case NAND_ECC_HW_OOB_FIRST: |
| 5410 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5411 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5412 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 5413 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5414 | goto err_nand_manuf_cleanup; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 5415 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5416 | if (!ecc->read_page) |
| 5417 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 5418 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5419 | case NAND_ECC_HW: |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5420 | /* Use standard hwecc read page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5421 | if (!ecc->read_page) |
| 5422 | ecc->read_page = nand_read_page_hwecc; |
| 5423 | if (!ecc->write_page) |
| 5424 | ecc->write_page = nand_write_page_hwecc; |
| 5425 | if (!ecc->read_page_raw) |
| 5426 | ecc->read_page_raw = nand_read_page_raw; |
| 5427 | if (!ecc->write_page_raw) |
| 5428 | ecc->write_page_raw = nand_write_page_raw; |
| 5429 | if (!ecc->read_oob) |
| 5430 | ecc->read_oob = nand_read_oob_std; |
| 5431 | if (!ecc->write_oob) |
| 5432 | ecc->write_oob = nand_write_oob_std; |
| 5433 | if (!ecc->read_subpage) |
| 5434 | ecc->read_subpage = nand_read_subpage; |
Helmut Schaa | 44991b3 | 2014-04-09 11:13:24 +0200 | [diff] [blame] | 5435 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5436 | ecc->write_subpage = nand_write_subpage_hwecc; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 5437 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5438 | case NAND_ECC_HW_SYNDROME: |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5439 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 5440 | (!ecc->read_page || |
| 5441 | ecc->read_page == nand_read_page_hwecc || |
| 5442 | !ecc->write_page || |
| 5443 | ecc->write_page == nand_write_page_hwecc)) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5444 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 5445 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5446 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5447 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5448 | /* Use standard syndrome read/write page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5449 | if (!ecc->read_page) |
| 5450 | ecc->read_page = nand_read_page_syndrome; |
| 5451 | if (!ecc->write_page) |
| 5452 | ecc->write_page = nand_write_page_syndrome; |
| 5453 | if (!ecc->read_page_raw) |
| 5454 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 5455 | if (!ecc->write_page_raw) |
| 5456 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 5457 | if (!ecc->read_oob) |
| 5458 | ecc->read_oob = nand_read_oob_syndrome; |
| 5459 | if (!ecc->write_oob) |
| 5460 | ecc->write_oob = nand_write_oob_syndrome; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 5461 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5462 | if (mtd->writesize >= ecc->size) { |
| 5463 | if (!ecc->strength) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5464 | WARN(1, "Driver must set ecc.strength when using hardware ECC\n"); |
| 5465 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5466 | goto err_nand_manuf_cleanup; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 5467 | } |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5468 | break; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 5469 | } |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 5470 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 5471 | ecc->size, mtd->writesize); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5472 | ecc->mode = NAND_ECC_SOFT; |
Rafał Miłecki | e9d4fae | 2016-04-17 22:53:02 +0200 | [diff] [blame] | 5473 | ecc->algo = NAND_ECC_HAMMING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5475 | case NAND_ECC_SOFT: |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5476 | ret = nand_set_ecc_soft_ops(mtd); |
| 5477 | if (ret) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5478 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5479 | goto err_nand_manuf_cleanup; |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 5480 | } |
| 5481 | break; |
| 5482 | |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 5483 | case NAND_ECC_ON_DIE: |
| 5484 | if (!ecc->read_page || !ecc->write_page) { |
| 5485 | WARN(1, "No ECC functions supplied; on-die ECC not possible\n"); |
| 5486 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5487 | goto err_nand_manuf_cleanup; |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 5488 | } |
| 5489 | if (!ecc->read_oob) |
| 5490 | ecc->read_oob = nand_read_oob_std; |
| 5491 | if (!ecc->write_oob) |
| 5492 | ecc->write_oob = nand_write_oob_std; |
| 5493 | break; |
| 5494 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5495 | case NAND_ECC_NONE: |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 5496 | 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] | 5497 | ecc->read_page = nand_read_page_raw; |
| 5498 | ecc->write_page = nand_write_page_raw; |
| 5499 | ecc->read_oob = nand_read_oob_std; |
| 5500 | ecc->read_page_raw = nand_read_page_raw; |
| 5501 | ecc->write_page_raw = nand_write_page_raw; |
| 5502 | ecc->write_oob = nand_write_oob_std; |
| 5503 | ecc->size = mtd->writesize; |
| 5504 | ecc->bytes = 0; |
| 5505 | ecc->strength = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5506 | break; |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 5507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | default: |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5509 | WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode); |
| 5510 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5511 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5512 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5513 | |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 5514 | /* For many systems, the standard OOB write also works for raw */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5515 | if (!ecc->read_oob_raw) |
| 5516 | ecc->read_oob_raw = ecc->read_oob; |
| 5517 | if (!ecc->write_oob_raw) |
| 5518 | ecc->write_oob_raw = ecc->write_oob; |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 5519 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5520 | /* propagate ecc info to mtd_info */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5521 | mtd->ecc_strength = ecc->strength; |
| 5522 | mtd->ecc_step_size = ecc->size; |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5523 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 5524 | /* |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5525 | * 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] | 5526 | * mode. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5527 | */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5528 | ecc->steps = mtd->writesize / ecc->size; |
| 5529 | if (ecc->steps * ecc->size != mtd->writesize) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5530 | WARN(1, "Invalid ECC parameters\n"); |
| 5531 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5532 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5534 | ecc->total = ecc->steps * ecc->bytes; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 5535 | if (ecc->total > mtd->oobsize) { |
| 5536 | WARN(1, "Total number of ECC bytes exceeded oobsize\n"); |
| 5537 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5538 | goto err_nand_manuf_cleanup; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 5539 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5540 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5541 | /* |
| 5542 | * The number of bytes available for a client to place data into |
| 5543 | * the out of band area. |
| 5544 | */ |
| 5545 | ret = mtd_ooblayout_count_freebytes(mtd); |
| 5546 | if (ret < 0) |
| 5547 | ret = 0; |
| 5548 | |
| 5549 | mtd->oobavail = ret; |
| 5550 | |
| 5551 | /* ECC sanity check: warn if it's too weak */ |
| 5552 | if (!nand_ecc_strength_good(mtd)) |
| 5553 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 5554 | mtd->name); |
| 5555 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5556 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 5557 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5558 | switch (ecc->steps) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 5559 | case 2: |
| 5560 | mtd->subpage_sft = 1; |
| 5561 | break; |
| 5562 | case 4: |
| 5563 | case 8: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5564 | case 16: |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 5565 | mtd->subpage_sft = 2; |
| 5566 | break; |
| 5567 | } |
| 5568 | } |
| 5569 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 5570 | |
Thomas Gleixner | 04bbd0e | 2006-05-25 09:45:29 +0200 | [diff] [blame] | 5571 | /* Initialize state */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5572 | chip->state = FL_READY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5574 | /* Invalidate the pagebuffer reference */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5575 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5576 | |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 5577 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 5578 | switch (ecc->mode) { |
| 5579 | case NAND_ECC_SOFT: |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 5580 | if (chip->page_shift > 9) |
| 5581 | chip->options |= NAND_SUBPAGE_READ; |
| 5582 | break; |
| 5583 | |
| 5584 | default: |
| 5585 | break; |
| 5586 | } |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 5587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | /* Fill in remaining MTD driver data */ |
Huang Shijie | 963d1c2 | 2013-09-25 14:58:21 +0800 | [diff] [blame] | 5589 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 5590 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 5591 | MTD_CAP_NANDFLASH; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5592 | mtd->_erase = nand_erase; |
| 5593 | mtd->_point = NULL; |
| 5594 | mtd->_unpoint = NULL; |
| 5595 | mtd->_read = nand_read; |
| 5596 | mtd->_write = nand_write; |
| 5597 | mtd->_panic_write = panic_nand_write; |
| 5598 | mtd->_read_oob = nand_read_oob; |
| 5599 | mtd->_write_oob = nand_write_oob; |
| 5600 | mtd->_sync = nand_sync; |
| 5601 | mtd->_lock = NULL; |
| 5602 | mtd->_unlock = NULL; |
| 5603 | mtd->_suspend = nand_suspend; |
| 5604 | mtd->_resume = nand_resume; |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 5605 | mtd->_reboot = nand_shutdown; |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 5606 | mtd->_block_isreserved = nand_block_isreserved; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5607 | mtd->_block_isbad = nand_block_isbad; |
| 5608 | mtd->_block_markbad = nand_block_markbad; |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 5609 | mtd->_max_bad_blocks = nand_max_bad_blocks; |
Anatolij Gustschin | cbcab65 | 2010-12-16 23:42:16 +0100 | [diff] [blame] | 5610 | mtd->writebufsize = mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | |
Shmulik Ladkani | ea3b2ea | 2012-06-08 18:29:06 +0300 | [diff] [blame] | 5612 | /* |
| 5613 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 5614 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 5615 | * properly set. |
| 5616 | */ |
| 5617 | if (!mtd->bitflip_threshold) |
Brian Norris | 240181f | 2015-01-12 12:51:29 -0800 | [diff] [blame] | 5618 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5619 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5620 | /* Initialize the ->data_interface field. */ |
| 5621 | ret = nand_init_data_interface(chip); |
| 5622 | if (ret) |
| 5623 | goto err_nand_manuf_cleanup; |
| 5624 | |
| 5625 | /* Enter fastest possible mode on all dies. */ |
| 5626 | for (i = 0; i < chip->numchips; i++) { |
| 5627 | chip->select_chip(mtd, i); |
| 5628 | ret = nand_setup_data_interface(chip, i); |
| 5629 | chip->select_chip(mtd, -1); |
| 5630 | |
| 5631 | if (ret) |
| 5632 | goto err_nand_data_iface_cleanup; |
| 5633 | } |
| 5634 | |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 5635 | /* Check, if we should skip the bad block table scan */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5636 | if (chip->options & NAND_SKIP_BBTSCAN) |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 5637 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | |
| 5639 | /* Build bad block table */ |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 5640 | ret = chip->scan_bbt(mtd); |
| 5641 | if (ret) |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5642 | goto err_nand_data_iface_cleanup; |
| 5643 | |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 5644 | return 0; |
| 5645 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5646 | err_nand_data_iface_cleanup: |
| 5647 | nand_release_data_interface(chip); |
| 5648 | |
| 5649 | err_nand_manuf_cleanup: |
| 5650 | nand_manufacturer_cleanup(chip); |
| 5651 | |
| 5652 | err_free_nbuf: |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5653 | if (nbuf) { |
| 5654 | kfree(nbuf->databuf); |
| 5655 | kfree(nbuf->ecccode); |
| 5656 | kfree(nbuf->ecccalc); |
| 5657 | kfree(nbuf); |
| 5658 | } |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5659 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5660 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5661 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5662 | EXPORT_SYMBOL(nand_scan_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5663 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5664 | /* |
| 5665 | * 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] | 5666 | * 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] | 5667 | * to call us from in-kernel code if the core NAND support is modular. |
| 5668 | */ |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5669 | #ifdef MODULE |
| 5670 | #define caller_is_module() (1) |
| 5671 | #else |
| 5672 | #define caller_is_module() \ |
Rusty Russell | a6e6abd | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 5673 | is_module_text_address((unsigned long)__builtin_return_address(0)) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5674 | #endif |
| 5675 | |
| 5676 | /** |
| 5677 | * nand_scan - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5678 | * @mtd: MTD device structure |
| 5679 | * @maxchips: number of chips to scan for |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5680 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5681 | * This fills out all the uninitialized function pointers with the defaults. |
| 5682 | * 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] | 5683 | * appropriate values. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5684 | */ |
| 5685 | int nand_scan(struct mtd_info *mtd, int maxchips) |
| 5686 | { |
| 5687 | int ret; |
| 5688 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 5689 | ret = nand_scan_ident(mtd, maxchips, NULL); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5690 | if (!ret) |
| 5691 | ret = nand_scan_tail(mtd); |
| 5692 | return ret; |
| 5693 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5694 | EXPORT_SYMBOL(nand_scan); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | /** |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5697 | * nand_cleanup - [NAND Interface] Free resources held by the NAND device |
| 5698 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5699 | */ |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5700 | void nand_cleanup(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | { |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5702 | if (chip->ecc.mode == NAND_ECC_SOFT && |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5703 | chip->ecc.algo == NAND_ECC_BCH) |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 5704 | nand_bch_free((struct nand_bch_control *)chip->ecc.priv); |
| 5705 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 5706 | nand_release_data_interface(chip); |
| 5707 | |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 5708 | /* Free bad block table memory */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5709 | kfree(chip->bbt); |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5710 | if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) { |
| 5711 | kfree(chip->buffers->databuf); |
| 5712 | kfree(chip->buffers->ecccode); |
| 5713 | kfree(chip->buffers->ecccalc); |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 5714 | kfree(chip->buffers); |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5715 | } |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 5716 | |
| 5717 | /* Free bad block descriptor memory */ |
| 5718 | if (chip->badblock_pattern && chip->badblock_pattern->options |
| 5719 | & NAND_BBT_DYNAMICSTRUCT) |
| 5720 | kfree(chip->badblock_pattern); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5721 | |
| 5722 | /* Free manufacturer priv data. */ |
| 5723 | nand_manufacturer_cleanup(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | } |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5725 | EXPORT_SYMBOL_GPL(nand_cleanup); |
| 5726 | |
| 5727 | /** |
| 5728 | * nand_release - [NAND Interface] Unregister the MTD device and free resources |
| 5729 | * held by the NAND device |
| 5730 | * @mtd: MTD device structure |
| 5731 | */ |
| 5732 | void nand_release(struct mtd_info *mtd) |
| 5733 | { |
| 5734 | mtd_device_unregister(mtd); |
| 5735 | nand_cleanup(mtd_to_nand(mtd)); |
| 5736 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5737 | EXPORT_SYMBOL_GPL(nand_release); |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 5738 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5739 | MODULE_LICENSE("GPL"); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5740 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 5741 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5742 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |