Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Overview: |
| 4 | * This is the generic MTD driver for NAND flash devices. It should be |
| 5 | * capable of working with almost all NAND chips currently available. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Additional technical information is available on |
maximilian attems | 8b2b403 | 2007-07-28 13:07:16 +0200 | [diff] [blame] | 8 | * http://www.linux-mtd.infradead.org/doc/nand.html |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 11 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 13 | * Credits: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 14 | * David Woodhouse for adding multichip support |
| 15 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
| 17 | * rework for 2K page size chips |
| 18 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 19 | * TODO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | * Enable cached programming for 2k page size chips |
| 21 | * Check, if mtd->ecctype should be set to MTD_ECC_HW |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 22 | * if we have HW ECC support. |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 23 | * BBT table is not serialized, has to be fixed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 26 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 27 | |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 28 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/delay.h> |
| 30 | #include <linux/errno.h> |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 31 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/sched.h> |
| 33 | #include <linux/slab.h> |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 34 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/types.h> |
| 36 | #include <linux/mtd/mtd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/mtd/nand_ecc.h> |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 38 | #include <linux/mtd/nand_bch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/interrupt.h> |
| 40 | #include <linux/bitops.h> |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 41 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/mtd/partitions.h> |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 43 | #include <linux/of.h> |
Janusz Krzysztofik | b0e137a | 2018-10-15 21:41:28 +0200 | [diff] [blame] | 44 | #include <linux/gpio/consumer.h> |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 45 | |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 46 | #include "internals.h" |
| 47 | |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 48 | /* Define default oob placement schemes for large and small page devices */ |
| 49 | static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section, |
| 50 | struct mtd_oob_region *oobregion) |
| 51 | { |
| 52 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 53 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 54 | |
| 55 | if (section > 1) |
| 56 | return -ERANGE; |
| 57 | |
| 58 | if (!section) { |
| 59 | oobregion->offset = 0; |
Miquel Raynal | f7f8c17 | 2017-07-05 08:51:09 +0200 | [diff] [blame] | 60 | if (mtd->oobsize == 16) |
| 61 | oobregion->length = 4; |
| 62 | else |
| 63 | oobregion->length = 3; |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 64 | } else { |
Miquel Raynal | f7f8c17 | 2017-07-05 08:51:09 +0200 | [diff] [blame] | 65 | if (mtd->oobsize == 8) |
| 66 | return -ERANGE; |
| 67 | |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 68 | oobregion->offset = 6; |
| 69 | oobregion->length = ecc->total - 4; |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section, |
| 76 | struct mtd_oob_region *oobregion) |
| 77 | { |
| 78 | if (section > 1) |
| 79 | return -ERANGE; |
| 80 | |
| 81 | if (mtd->oobsize == 16) { |
| 82 | if (section) |
| 83 | return -ERANGE; |
| 84 | |
| 85 | oobregion->length = 8; |
| 86 | oobregion->offset = 8; |
| 87 | } else { |
| 88 | oobregion->length = 2; |
| 89 | if (!section) |
| 90 | oobregion->offset = 3; |
| 91 | else |
| 92 | oobregion->offset = 6; |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = { |
| 99 | .ecc = nand_ooblayout_ecc_sp, |
| 100 | .free = nand_ooblayout_free_sp, |
| 101 | }; |
| 102 | EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops); |
| 103 | |
| 104 | static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section, |
| 105 | struct mtd_oob_region *oobregion) |
| 106 | { |
| 107 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 108 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 109 | |
Miquel Raynal | 882fd15 | 2017-08-26 17:19:15 +0200 | [diff] [blame] | 110 | if (section || !ecc->total) |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 111 | return -ERANGE; |
| 112 | |
| 113 | oobregion->length = ecc->total; |
| 114 | oobregion->offset = mtd->oobsize - oobregion->length; |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section, |
| 120 | struct mtd_oob_region *oobregion) |
| 121 | { |
| 122 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 123 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 124 | |
| 125 | if (section) |
| 126 | return -ERANGE; |
| 127 | |
| 128 | oobregion->length = mtd->oobsize - ecc->total - 2; |
| 129 | oobregion->offset = 2; |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = { |
| 135 | .ecc = nand_ooblayout_ecc_lp, |
| 136 | .free = nand_ooblayout_free_lp, |
| 137 | }; |
| 138 | EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops); |
Thomas Gleixner | d470a97 | 2006-05-23 23:48:57 +0200 | [diff] [blame] | 139 | |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 140 | /* |
| 141 | * Support the old "large page" layout used for 1-bit Hamming ECC where ECC |
| 142 | * are placed at a fixed offset. |
| 143 | */ |
| 144 | static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section, |
| 145 | struct mtd_oob_region *oobregion) |
| 146 | { |
| 147 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 148 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 149 | |
| 150 | if (section) |
| 151 | return -ERANGE; |
| 152 | |
| 153 | switch (mtd->oobsize) { |
| 154 | case 64: |
| 155 | oobregion->offset = 40; |
| 156 | break; |
| 157 | case 128: |
| 158 | oobregion->offset = 80; |
| 159 | break; |
| 160 | default: |
| 161 | return -EINVAL; |
| 162 | } |
| 163 | |
| 164 | oobregion->length = ecc->total; |
| 165 | if (oobregion->offset + oobregion->length > mtd->oobsize) |
| 166 | return -ERANGE; |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section, |
| 172 | struct mtd_oob_region *oobregion) |
| 173 | { |
| 174 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 175 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 176 | int ecc_offset = 0; |
| 177 | |
| 178 | if (section < 0 || section > 1) |
| 179 | return -ERANGE; |
| 180 | |
| 181 | switch (mtd->oobsize) { |
| 182 | case 64: |
| 183 | ecc_offset = 40; |
| 184 | break; |
| 185 | case 128: |
| 186 | ecc_offset = 80; |
| 187 | break; |
| 188 | default: |
| 189 | return -EINVAL; |
| 190 | } |
| 191 | |
| 192 | if (section == 0) { |
| 193 | oobregion->offset = 2; |
| 194 | oobregion->length = ecc_offset - 2; |
| 195 | } else { |
| 196 | oobregion->offset = ecc_offset + ecc->total; |
| 197 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Colin Ian King | d4ed3b9 | 2017-05-04 13:11:00 +0100 | [diff] [blame] | 203 | static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = { |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 204 | .ecc = nand_ooblayout_ecc_lp_hamming, |
| 205 | .free = nand_ooblayout_free_lp_hamming, |
| 206 | }; |
| 207 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 208 | static int check_offs_len(struct nand_chip *chip, loff_t ofs, uint64_t len) |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 209 | { |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 210 | int ret = 0; |
| 211 | |
| 212 | /* Start address must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 213 | if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 214 | pr_debug("%s: unaligned address\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 215 | ret = -EINVAL; |
| 216 | } |
| 217 | |
| 218 | /* Length must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 219 | if (len & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 220 | pr_debug("%s: length not block aligned\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 221 | ret = -EINVAL; |
| 222 | } |
| 223 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 224 | return ret; |
| 225 | } |
| 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | /** |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 228 | * nand_select_target() - Select a NAND target (A.K.A. die) |
| 229 | * @chip: NAND chip object |
| 230 | * @cs: the CS line to select. Note that this CS id is always from the chip |
| 231 | * PoV, not the controller one |
| 232 | * |
| 233 | * Select a NAND target so that further operations executed on @chip go to the |
| 234 | * selected NAND target. |
| 235 | */ |
| 236 | void nand_select_target(struct nand_chip *chip, unsigned int cs) |
| 237 | { |
| 238 | /* |
Boris Brezillon | 32813e2 | 2018-10-29 11:58:29 +0100 | [diff] [blame] | 239 | * cs should always lie between 0 and nanddev_ntargets(), when that's |
| 240 | * not the case it's a bug and the caller should be fixed. |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 241 | */ |
Boris Brezillon | 32813e2 | 2018-10-29 11:58:29 +0100 | [diff] [blame] | 242 | if (WARN_ON(cs > nanddev_ntargets(&chip->base))) |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 243 | return; |
| 244 | |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 245 | chip->cur_cs = cs; |
Boris Brezillon | 02b4a52 | 2018-11-11 08:55:16 +0100 | [diff] [blame] | 246 | |
Boris Brezillon | 7d6c37e | 2018-11-11 08:55:22 +0100 | [diff] [blame] | 247 | if (chip->legacy.select_chip) |
| 248 | chip->legacy.select_chip(chip, cs); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 249 | } |
| 250 | EXPORT_SYMBOL_GPL(nand_select_target); |
| 251 | |
| 252 | /** |
| 253 | * nand_deselect_target() - Deselect the currently selected target |
| 254 | * @chip: NAND chip object |
| 255 | * |
| 256 | * Deselect the currently selected NAND target. The result of operations |
| 257 | * executed on @chip after the target has been deselected is undefined. |
| 258 | */ |
| 259 | void nand_deselect_target(struct nand_chip *chip) |
| 260 | { |
Boris Brezillon | 7d6c37e | 2018-11-11 08:55:22 +0100 | [diff] [blame] | 261 | if (chip->legacy.select_chip) |
| 262 | chip->legacy.select_chip(chip, -1); |
Boris Brezillon | 02b4a52 | 2018-11-11 08:55:16 +0100 | [diff] [blame] | 263 | |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 264 | chip->cur_cs = -1; |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 265 | } |
| 266 | EXPORT_SYMBOL_GPL(nand_deselect_target); |
| 267 | |
| 268 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | * nand_release_device - [GENERIC] release chip |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 270 | * @chip: NAND chip object |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 271 | * |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 272 | * Release chip lock and wake up anyone waiting on the device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 274 | static void nand_release_device(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 276 | /* Release the controller and the chip */ |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 277 | mutex_unlock(&chip->controller->lock); |
| 278 | mutex_unlock(&chip->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /** |
Frieder Schrempf | f90da78 | 2019-04-17 12:36:37 +0000 | [diff] [blame] | 282 | * nand_bbm_get_next_page - Get the next page for bad block markers |
| 283 | * @chip: NAND chip object |
| 284 | * @page: First page to start checking for bad block marker usage |
| 285 | * |
| 286 | * Returns an integer that corresponds to the page offset within a block, for |
| 287 | * a page that is used to store bad block markers. If no more pages are |
| 288 | * available, -EINVAL is returned. |
| 289 | */ |
| 290 | int nand_bbm_get_next_page(struct nand_chip *chip, int page) |
| 291 | { |
| 292 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 293 | int last_page = ((mtd->erasesize - mtd->writesize) >> |
| 294 | chip->page_shift) & chip->pagemask; |
| 295 | |
| 296 | if (page == 0 && chip->options & NAND_BBM_FIRSTPAGE) |
| 297 | return 0; |
| 298 | else if (page <= 1 && chip->options & NAND_BBM_SECONDPAGE) |
| 299 | return 1; |
| 300 | else if (page <= last_page && chip->options & NAND_BBM_LASTPAGE) |
| 301 | return last_page; |
| 302 | |
| 303 | return -EINVAL; |
| 304 | } |
| 305 | |
| 306 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 308 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 309 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 311 | * Check, if the block is bad. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | */ |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 313 | static int nand_block_bad(struct nand_chip *chip, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
Frieder Schrempf | f90da78 | 2019-04-17 12:36:37 +0000 | [diff] [blame] | 315 | int first_page, page_offset; |
| 316 | int res; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 317 | u8 bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Frieder Schrempf | f90da78 | 2019-04-17 12:36:37 +0000 | [diff] [blame] | 319 | first_page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
| 320 | page_offset = nand_bbm_get_next_page(chip, 0); |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 321 | |
Frieder Schrempf | f90da78 | 2019-04-17 12:36:37 +0000 | [diff] [blame] | 322 | while (page_offset >= 0) { |
| 323 | res = chip->ecc.read_oob(chip, first_page + page_offset); |
Abhishek Sahu | e9893e6 | 2018-06-13 14:32:36 +0530 | [diff] [blame] | 324 | if (res < 0) |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 325 | return res; |
| 326 | |
| 327 | bad = chip->oob_poi[chip->badblockpos]; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 328 | |
Brian Norris | cdbec05 | 2012-01-13 18:11:48 -0800 | [diff] [blame] | 329 | if (likely(chip->badblockbits == 8)) |
| 330 | res = bad != 0xFF; |
| 331 | else |
| 332 | res = hweight8(bad) < chip->badblockbits; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 333 | if (res) |
| 334 | return res; |
Frieder Schrempf | f90da78 | 2019-04-17 12:36:37 +0000 | [diff] [blame] | 335 | |
| 336 | page_offset = nand_bbm_get_next_page(chip, page_offset + 1); |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 337 | } |
Maxim Levitsky | e0b58d0 | 2010-02-22 20:39:38 +0200 | [diff] [blame] | 338 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 339 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 342 | static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs) |
| 343 | { |
| 344 | if (chip->legacy.block_bad) |
| 345 | return chip->legacy.block_bad(chip, ofs); |
| 346 | |
| 347 | return nand_block_bad(chip, ofs); |
| 348 | } |
| 349 | |
| 350 | /** |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 351 | * nand_get_device - [GENERIC] Get chip for selected access |
| 352 | * @chip: NAND chip structure |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 353 | * |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 354 | * Lock the device and its controller for exclusive access |
| 355 | * |
| 356 | * Return: -EBUSY if the chip has been suspended, 0 otherwise |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 357 | */ |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 358 | static int nand_get_device(struct nand_chip *chip) |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 359 | { |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 360 | mutex_lock(&chip->lock); |
| 361 | if (chip->suspended) { |
| 362 | mutex_unlock(&chip->lock); |
| 363 | return -EBUSY; |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 364 | } |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 365 | mutex_lock(&chip->controller->lock); |
| 366 | |
| 367 | return 0; |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /** |
| 371 | * nand_check_wp - [GENERIC] check if the chip is write protected |
| 372 | * @chip: NAND chip object |
| 373 | * |
| 374 | * Check, if the device is write protected. The function expects, that the |
| 375 | * device is already selected. |
| 376 | */ |
| 377 | static int nand_check_wp(struct nand_chip *chip) |
| 378 | { |
| 379 | u8 status; |
| 380 | int ret; |
| 381 | |
| 382 | /* Broken xD cards report WP despite being writable */ |
| 383 | if (chip->options & NAND_BROKEN_XD) |
| 384 | return 0; |
| 385 | |
| 386 | /* Check the WP bit */ |
| 387 | ret = nand_status_op(chip, &status); |
| 388 | if (ret) |
| 389 | return ret; |
| 390 | |
| 391 | return status & NAND_STATUS_WP ? 0 : 1; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
Randy Dunlap | 455e7b3 | 2019-01-27 18:21:42 -0800 | [diff] [blame] | 396 | * @chip: NAND chip object |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 397 | * @oob: oob data buffer |
| 398 | * @len: oob data write length |
| 399 | * @ops: oob ops structure |
| 400 | */ |
| 401 | static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len, |
| 402 | struct mtd_oob_ops *ops) |
| 403 | { |
| 404 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 405 | int ret; |
| 406 | |
| 407 | /* |
| 408 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 409 | * data from a previous OOB read. |
| 410 | */ |
| 411 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 412 | |
| 413 | switch (ops->mode) { |
| 414 | |
| 415 | case MTD_OPS_PLACE_OOB: |
| 416 | case MTD_OPS_RAW: |
| 417 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 418 | return oob + len; |
| 419 | |
| 420 | case MTD_OPS_AUTO_OOB: |
| 421 | ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi, |
| 422 | ops->ooboffs, len); |
| 423 | BUG_ON(ret); |
| 424 | return oob + len; |
| 425 | |
| 426 | default: |
| 427 | BUG(); |
| 428 | } |
| 429 | return NULL; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
| 434 | * @chip: NAND chip object |
| 435 | * @to: offset to write to |
| 436 | * @ops: oob operation description structure |
| 437 | * |
| 438 | * NAND write out-of-band. |
| 439 | */ |
| 440 | static int nand_do_write_oob(struct nand_chip *chip, loff_t to, |
| 441 | struct mtd_oob_ops *ops) |
| 442 | { |
| 443 | struct mtd_info *mtd = nand_to_mtd(chip); |
Masahiro Yamada | f9ffb40 | 2019-01-21 22:05:34 +0900 | [diff] [blame] | 444 | int chipnr, page, status, len, ret; |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 445 | |
| 446 | pr_debug("%s: to = 0x%08x, len = %i\n", |
| 447 | __func__, (unsigned int)to, (int)ops->ooblen); |
| 448 | |
| 449 | len = mtd_oobavail(mtd, ops); |
| 450 | |
| 451 | /* Do not allow write past end of page */ |
| 452 | if ((ops->ooboffs + ops->ooblen) > len) { |
| 453 | pr_debug("%s: attempt to write past end of page\n", |
| 454 | __func__); |
| 455 | return -EINVAL; |
| 456 | } |
| 457 | |
| 458 | chipnr = (int)(to >> chip->chip_shift); |
| 459 | |
| 460 | /* |
| 461 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 462 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 463 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 464 | * it in the doc2000 driver in August 1999. dwmw2. |
| 465 | */ |
Masahiro Yamada | f9ffb40 | 2019-01-21 22:05:34 +0900 | [diff] [blame] | 466 | ret = nand_reset(chip, chipnr); |
| 467 | if (ret) |
| 468 | return ret; |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 469 | |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 470 | nand_select_target(chip, chipnr); |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 471 | |
| 472 | /* Shift to get page */ |
| 473 | page = (int)(to >> chip->page_shift); |
| 474 | |
| 475 | /* Check, if it is write protected */ |
| 476 | if (nand_check_wp(chip)) { |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 477 | nand_deselect_target(chip); |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 478 | return -EROFS; |
| 479 | } |
| 480 | |
| 481 | /* Invalidate the page cache, if we write to the cached page */ |
Boris Brezillon | d974541 | 2018-10-28 16:12:45 +0100 | [diff] [blame] | 482 | if (page == chip->pagecache.page) |
| 483 | chip->pagecache.page = -1; |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 484 | |
| 485 | nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops); |
| 486 | |
| 487 | if (ops->mode == MTD_OPS_RAW) |
| 488 | status = chip->ecc.write_oob_raw(chip, page & chip->pagemask); |
| 489 | else |
| 490 | status = chip->ecc.write_oob(chip, page & chip->pagemask); |
| 491 | |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 492 | nand_deselect_target(chip); |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 493 | |
| 494 | if (status) |
| 495 | return status; |
| 496 | |
| 497 | ops->oobretlen = ops->ooblen; |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | /** |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 503 | * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 504 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 505 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 507 | * This is the default implementation, which can be overridden by a hardware |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 508 | * specific driver. It provides the details for writing a bad block marker to a |
| 509 | * block. |
| 510 | */ |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 511 | static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs) |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 512 | { |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 513 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 514 | struct mtd_oob_ops ops; |
| 515 | uint8_t buf[2] = { 0, 0 }; |
Frieder Schrempf | f90da78 | 2019-04-17 12:36:37 +0000 | [diff] [blame] | 516 | int ret = 0, res, page_offset; |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 517 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 518 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 519 | ops.oobbuf = buf; |
| 520 | ops.ooboffs = chip->badblockpos; |
| 521 | if (chip->options & NAND_BUSWIDTH_16) { |
| 522 | ops.ooboffs &= ~0x01; |
| 523 | ops.len = ops.ooblen = 2; |
| 524 | } else { |
| 525 | ops.len = ops.ooblen = 1; |
| 526 | } |
| 527 | ops.mode = MTD_OPS_PLACE_OOB; |
| 528 | |
Frieder Schrempf | f90da78 | 2019-04-17 12:36:37 +0000 | [diff] [blame] | 529 | page_offset = nand_bbm_get_next_page(chip, 0); |
| 530 | |
| 531 | while (page_offset >= 0) { |
| 532 | res = nand_do_write_oob(chip, |
| 533 | ofs + (page_offset * mtd->writesize), |
| 534 | &ops); |
| 535 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 536 | if (!ret) |
| 537 | ret = res; |
| 538 | |
Frieder Schrempf | f90da78 | 2019-04-17 12:36:37 +0000 | [diff] [blame] | 539 | page_offset = nand_bbm_get_next_page(chip, page_offset + 1); |
| 540 | } |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 541 | |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | /** |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 546 | * nand_markbad_bbm - mark a block by updating the BBM |
| 547 | * @chip: NAND chip object |
| 548 | * @ofs: offset of the block to mark bad |
| 549 | */ |
| 550 | int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs) |
| 551 | { |
| 552 | if (chip->legacy.block_markbad) |
| 553 | return chip->legacy.block_markbad(chip, ofs); |
| 554 | |
| 555 | return nand_default_block_markbad(chip, ofs); |
| 556 | } |
| 557 | |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 558 | /** |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 559 | * nand_block_markbad_lowlevel - mark a block bad |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 560 | * @chip: NAND chip object |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 561 | * @ofs: offset from device start |
| 562 | * |
| 563 | * This function performs the generic NAND bad block marking steps (i.e., bad |
| 564 | * block table(s) and/or marker(s)). We only allow the hardware driver to |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 565 | * specify how to write bad block markers to OOB (chip->legacy.block_markbad). |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 566 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 567 | * We try operations in the following order: |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 568 | * |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 569 | * (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] | 570 | * (2) write bad block marker to OOB area of affected block (unless flag |
| 571 | * NAND_BBT_NO_OOB_BBM is present) |
| 572 | * (3) update the BBT |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 573 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 574 | * 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] | 575 | * procedures, and dump the error in the end. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 577 | static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 579 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 580 | int res, ret = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 581 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 582 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 583 | struct erase_info einfo; |
| 584 | |
| 585 | /* Attempt erase before marking OOB */ |
| 586 | memset(&einfo, 0, sizeof(einfo)); |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 587 | einfo.addr = ofs; |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 588 | einfo.len = 1ULL << chip->phys_erase_shift; |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 589 | nand_erase_nand(chip, &einfo, 0); |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 590 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 591 | /* Write bad block marker to OOB */ |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 592 | ret = nand_get_device(chip); |
| 593 | if (ret) |
| 594 | return ret; |
| 595 | |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 596 | ret = nand_markbad_bbm(chip, ofs); |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 597 | nand_release_device(chip); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 598 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 599 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 600 | /* Mark block bad in BBT */ |
| 601 | if (chip->bbt) { |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 602 | res = nand_markbad_bbt(chip, ofs); |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 603 | if (!ret) |
| 604 | ret = res; |
| 605 | } |
| 606 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 607 | if (!ret) |
| 608 | mtd->ecc_stats.badblocks++; |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 609 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 610 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 613 | /** |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 614 | * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 615 | * @mtd: MTD device structure |
| 616 | * @ofs: offset from device start |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 617 | * |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 618 | * Check if the block is marked as reserved. |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 619 | */ |
| 620 | static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 621 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 622 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 623 | |
| 624 | if (!chip->bbt) |
| 625 | return 0; |
| 626 | /* Return info from the table */ |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 627 | return nand_isreserved_bbt(chip, ofs); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /** |
| 631 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 632 | * @chip: NAND chip object |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 633 | * @ofs: offset from device start |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 634 | * @allowbbt: 1, if its allowed to access the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | * |
| 636 | * Check, if the block is bad. Either by reading the bad block table or |
| 637 | * calling of the scan function. |
| 638 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 639 | static int nand_block_checkbad(struct nand_chip *chip, loff_t ofs, int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | /* Return info from the table */ |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 642 | if (chip->bbt) |
| 643 | return nand_isbad_bbt(chip, ofs, allowbbt); |
| 644 | |
| 645 | return nand_isbad_bbm(chip, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 648 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 649 | * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1 |
| 650 | * @chip: NAND chip structure |
| 651 | * @timeout_ms: Timeout in ms |
| 652 | * |
| 653 | * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1. |
| 654 | * If that does not happen whitin the specified timeout, -ETIMEDOUT is |
| 655 | * returned. |
| 656 | * |
| 657 | * This helper is intended to be used when the controller does not have access |
| 658 | * to the NAND R/B pin. |
| 659 | * |
| 660 | * Be aware that calling this helper from an ->exec_op() implementation means |
| 661 | * ->exec_op() must be re-entrant. |
| 662 | * |
| 663 | * Return 0 if the NAND chip is ready, a negative error otherwise. |
| 664 | */ |
| 665 | int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms) |
| 666 | { |
Boris Brezillon | 3057fce | 2018-05-04 21:24:31 +0200 | [diff] [blame] | 667 | const struct nand_sdr_timings *timings; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 668 | u8 status = 0; |
| 669 | int ret; |
| 670 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 671 | if (!nand_has_exec_op(chip)) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 672 | return -ENOTSUPP; |
| 673 | |
Boris Brezillon | 3057fce | 2018-05-04 21:24:31 +0200 | [diff] [blame] | 674 | /* Wait tWB before polling the STATUS reg. */ |
| 675 | timings = nand_get_sdr_timings(&chip->data_interface); |
| 676 | ndelay(PSEC_TO_NSEC(timings->tWB_max)); |
| 677 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 678 | ret = nand_status_op(chip, NULL); |
| 679 | if (ret) |
| 680 | return ret; |
| 681 | |
| 682 | timeout_ms = jiffies + msecs_to_jiffies(timeout_ms); |
| 683 | do { |
| 684 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 685 | if (ret) |
| 686 | break; |
| 687 | |
| 688 | if (status & NAND_STATUS_READY) |
| 689 | break; |
| 690 | |
| 691 | /* |
| 692 | * Typical lowest execution time for a tR on most NANDs is 10us, |
| 693 | * use this as polling delay before doing something smarter (ie. |
| 694 | * deriving a delay from the timeout value, timeout_ms/ratio). |
| 695 | */ |
| 696 | udelay(10); |
| 697 | } while (time_before(jiffies, timeout_ms)); |
| 698 | |
| 699 | /* |
| 700 | * We have to exit READ_STATUS mode in order to read real data on the |
| 701 | * bus in case the WAITRDY instruction is preceding a DATA_IN |
| 702 | * instruction. |
| 703 | */ |
| 704 | nand_exit_status_op(chip); |
| 705 | |
| 706 | if (ret) |
| 707 | return ret; |
| 708 | |
| 709 | return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT; |
| 710 | }; |
| 711 | EXPORT_SYMBOL_GPL(nand_soft_waitrdy); |
| 712 | |
| 713 | /** |
Janusz Krzysztofik | b0e137a | 2018-10-15 21:41:28 +0200 | [diff] [blame] | 714 | * nand_gpio_waitrdy - Poll R/B GPIO pin until ready |
| 715 | * @chip: NAND chip structure |
| 716 | * @gpiod: GPIO descriptor of R/B pin |
| 717 | * @timeout_ms: Timeout in ms |
| 718 | * |
| 719 | * Poll the R/B GPIO pin until it becomes ready. If that does not happen |
| 720 | * whitin the specified timeout, -ETIMEDOUT is returned. |
| 721 | * |
| 722 | * This helper is intended to be used when the controller has access to the |
| 723 | * NAND R/B pin over GPIO. |
| 724 | * |
| 725 | * Return 0 if the R/B pin indicates chip is ready, a negative error otherwise. |
| 726 | */ |
| 727 | int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod, |
| 728 | unsigned long timeout_ms) |
| 729 | { |
| 730 | /* Wait until R/B pin indicates chip is ready or timeout occurs */ |
| 731 | timeout_ms = jiffies + msecs_to_jiffies(timeout_ms); |
| 732 | do { |
| 733 | if (gpiod_get_value_cansleep(gpiod)) |
| 734 | return 0; |
| 735 | |
| 736 | cond_resched(); |
| 737 | } while (time_before(jiffies, timeout_ms)); |
| 738 | |
| 739 | return gpiod_get_value_cansleep(gpiod) ? 0 : -ETIMEDOUT; |
| 740 | }; |
| 741 | EXPORT_SYMBOL_GPL(nand_gpio_waitrdy); |
| 742 | |
| 743 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 744 | * panic_nand_wait - [GENERIC] wait until the command is done |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 745 | * @chip: NAND chip structure |
| 746 | * @timeo: timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 747 | * |
| 748 | * Wait for command done. This is a helper function for nand_wait used when |
| 749 | * 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] | 750 | * an oops through mtdoops. |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 751 | */ |
Boris Brezillon | 3d4af7c | 2018-09-07 00:38:49 +0200 | [diff] [blame] | 752 | void panic_nand_wait(struct nand_chip *chip, unsigned long timeo) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 753 | { |
| 754 | int i; |
| 755 | for (i = 0; i < timeo; i++) { |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 756 | if (chip->legacy.dev_ready) { |
| 757 | if (chip->legacy.dev_ready(chip)) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 758 | break; |
| 759 | } else { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 760 | int ret; |
| 761 | u8 status; |
| 762 | |
| 763 | ret = nand_read_data_op(chip, &status, sizeof(status), |
| 764 | true); |
| 765 | if (ret) |
| 766 | return; |
| 767 | |
| 768 | if (status & NAND_STATUS_READY) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 769 | break; |
| 770 | } |
| 771 | mdelay(1); |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 772 | } |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 773 | } |
| 774 | |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 775 | static bool nand_supports_get_features(struct nand_chip *chip, int addr) |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 776 | { |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 777 | return (chip->parameters.supports_set_get_features && |
| 778 | test_bit(addr, chip->parameters.get_feature_list)); |
| 779 | } |
| 780 | |
| 781 | static bool nand_supports_set_features(struct nand_chip *chip, int addr) |
| 782 | { |
| 783 | return (chip->parameters.supports_set_get_features && |
| 784 | test_bit(addr, chip->parameters.set_feature_list)); |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /** |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 788 | * nand_reset_data_interface - Reset data interface and timings |
| 789 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 790 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 791 | * |
| 792 | * Reset the Data interface and timings to ONFI mode 0. |
| 793 | * |
| 794 | * Returns 0 for success or negative error code otherwise. |
| 795 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 796 | static int nand_reset_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 797 | { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 798 | int ret; |
| 799 | |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 800 | if (!nand_has_setup_data_iface(chip)) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 801 | return 0; |
| 802 | |
| 803 | /* |
| 804 | * The ONFI specification says: |
| 805 | * " |
| 806 | * To transition from NV-DDR or NV-DDR2 to the SDR data |
| 807 | * interface, the host shall use the Reset (FFh) command |
| 808 | * using SDR timing mode 0. A device in any timing mode is |
| 809 | * required to recognize Reset (FFh) command issued in SDR |
| 810 | * timing mode 0. |
| 811 | * " |
| 812 | * |
| 813 | * Configure the data interface in SDR mode and set the |
| 814 | * timings to timing mode 0. |
| 815 | */ |
| 816 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 817 | onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0); |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 818 | ret = chip->controller->ops->setup_data_interface(chip, chipnr, |
| 819 | &chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 820 | if (ret) |
| 821 | pr_err("Failed to configure data interface to SDR timing mode 0\n"); |
| 822 | |
| 823 | return ret; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * nand_setup_data_interface - Setup the best data interface and timings |
| 828 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 829 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 830 | * |
| 831 | * Find and configure the best data interface and NAND timings supported by |
| 832 | * the chip and the driver. |
| 833 | * First tries to retrieve supported timing modes from ONFI information, |
| 834 | * and if the NAND chip does not support ONFI, relies on the |
| 835 | * ->onfi_timing_mode_default specified in the nand_ids table. |
| 836 | * |
| 837 | * Returns 0 for success or negative error code otherwise. |
| 838 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 839 | static int nand_setup_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 840 | { |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 841 | u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { |
| 842 | chip->onfi_timing_mode_default, |
| 843 | }; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 844 | int ret; |
| 845 | |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 846 | if (!nand_has_setup_data_iface(chip)) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 847 | return 0; |
| 848 | |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 849 | /* Change the mode on the chip side (if supported by the NAND chip) */ |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 850 | if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) { |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 851 | nand_select_target(chip, chipnr); |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 852 | ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE, |
| 853 | tmode_param); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 854 | nand_deselect_target(chip); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 855 | if (ret) |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 856 | return ret; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 857 | } |
| 858 | |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 859 | /* Change the mode on the controller side */ |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 860 | ret = chip->controller->ops->setup_data_interface(chip, chipnr, |
| 861 | &chip->data_interface); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 862 | if (ret) |
| 863 | return ret; |
| 864 | |
| 865 | /* Check the mode has been accepted by the chip, if supported */ |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 866 | if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 867 | return 0; |
| 868 | |
| 869 | memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 870 | nand_select_target(chip, chipnr); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 871 | ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE, |
| 872 | tmode_param); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 873 | nand_deselect_target(chip); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 874 | if (ret) |
| 875 | goto err_reset_chip; |
| 876 | |
| 877 | if (tmode_param[0] != chip->onfi_timing_mode_default) { |
| 878 | pr_warn("timing mode %d not acknowledged by the NAND chip\n", |
| 879 | chip->onfi_timing_mode_default); |
| 880 | goto err_reset_chip; |
| 881 | } |
| 882 | |
| 883 | return 0; |
| 884 | |
| 885 | err_reset_chip: |
| 886 | /* |
| 887 | * Fallback to mode 0 if the chip explicitly did not ack the chosen |
| 888 | * timing mode. |
| 889 | */ |
| 890 | nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 891 | nand_select_target(chip, chipnr); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 892 | nand_reset_op(chip); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 893 | nand_deselect_target(chip); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 894 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 895 | return ret; |
| 896 | } |
| 897 | |
| 898 | /** |
| 899 | * nand_init_data_interface - find the best data interface and timings |
| 900 | * @chip: The NAND chip |
| 901 | * |
| 902 | * Find the best data interface and NAND timings supported by the chip |
| 903 | * and the driver. |
| 904 | * First tries to retrieve supported timing modes from ONFI information, |
| 905 | * and if the NAND chip does not support ONFI, relies on the |
| 906 | * ->onfi_timing_mode_default specified in the nand_ids table. After this |
| 907 | * function nand_chip->data_interface is initialized with the best timing mode |
| 908 | * available. |
| 909 | * |
| 910 | * Returns 0 for success or negative error code otherwise. |
| 911 | */ |
| 912 | static int nand_init_data_interface(struct nand_chip *chip) |
| 913 | { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 914 | int modes, mode, ret; |
| 915 | |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 916 | if (!nand_has_setup_data_iface(chip)) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 917 | return 0; |
| 918 | |
| 919 | /* |
| 920 | * First try to identify the best timings from ONFI parameters and |
| 921 | * if the NAND does not support ONFI, fallback to the default ONFI |
| 922 | * timing mode. |
| 923 | */ |
Boris Brezillon | 462f35d | 2018-09-07 00:38:47 +0200 | [diff] [blame] | 924 | if (chip->parameters.onfi) { |
| 925 | modes = chip->parameters.onfi->async_timing_mode; |
| 926 | } else { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 927 | if (!chip->onfi_timing_mode_default) |
| 928 | return 0; |
| 929 | |
| 930 | modes = GENMASK(chip->onfi_timing_mode_default, 0); |
| 931 | } |
| 932 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 933 | for (mode = fls(modes) - 1; mode >= 0; mode--) { |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 934 | ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 935 | if (ret) |
| 936 | continue; |
| 937 | |
Miquel Raynal | d787b8b | 2017-12-22 18:12:41 +0100 | [diff] [blame] | 938 | /* |
| 939 | * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the |
| 940 | * controller supports the requested timings. |
| 941 | */ |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 942 | ret = chip->controller->ops->setup_data_interface(chip, |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 943 | NAND_DATA_IFACE_CHECK_ONLY, |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 944 | &chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 945 | if (!ret) { |
| 946 | chip->onfi_timing_mode_default = mode; |
| 947 | break; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | return 0; |
| 952 | } |
| 953 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 954 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 955 | * nand_fill_column_cycles - fill the column cycles of an address |
| 956 | * @chip: The NAND chip |
| 957 | * @addrs: Array of address cycles to fill |
| 958 | * @offset_in_page: The offset in the page |
| 959 | * |
| 960 | * Fills the first or the first two bytes of the @addrs field depending |
| 961 | * on the NAND bus width and the page size. |
| 962 | * |
| 963 | * Returns the number of cycles needed to encode the column, or a negative |
| 964 | * error code in case one of the arguments is invalid. |
| 965 | */ |
| 966 | static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs, |
| 967 | unsigned int offset_in_page) |
Matthieu CASTET | f251b8d | 2012-11-05 15:00:44 +0100 | [diff] [blame] | 968 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 969 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 970 | |
| 971 | /* Make sure the offset is less than the actual page size. */ |
| 972 | if (offset_in_page > mtd->writesize + mtd->oobsize) |
| 973 | return -EINVAL; |
| 974 | |
| 975 | /* |
| 976 | * On small page NANDs, there's a dedicated command to access the OOB |
| 977 | * area, and the column address is relative to the start of the OOB |
| 978 | * area, not the start of the page. Asjust the address accordingly. |
| 979 | */ |
| 980 | if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize) |
| 981 | offset_in_page -= mtd->writesize; |
| 982 | |
| 983 | /* |
| 984 | * The offset in page is expressed in bytes, if the NAND bus is 16-bit |
| 985 | * wide, then it must be divided by 2. |
| 986 | */ |
| 987 | if (chip->options & NAND_BUSWIDTH_16) { |
| 988 | if (WARN_ON(offset_in_page % 2)) |
| 989 | return -EINVAL; |
| 990 | |
| 991 | offset_in_page /= 2; |
| 992 | } |
| 993 | |
| 994 | addrs[0] = offset_in_page; |
| 995 | |
| 996 | /* |
| 997 | * Small page NANDs use 1 cycle for the columns, while large page NANDs |
| 998 | * need 2 |
| 999 | */ |
| 1000 | if (mtd->writesize <= 512) |
| 1001 | return 1; |
| 1002 | |
| 1003 | addrs[1] = offset_in_page >> 8; |
| 1004 | |
| 1005 | return 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1008 | static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1009 | unsigned int offset_in_page, void *buf, |
| 1010 | unsigned int len) |
| 1011 | { |
| 1012 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1013 | const struct nand_sdr_timings *sdr = |
| 1014 | nand_get_sdr_timings(&chip->data_interface); |
| 1015 | u8 addrs[4]; |
| 1016 | struct nand_op_instr instrs[] = { |
| 1017 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1018 | NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1019 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 1020 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1021 | NAND_OP_DATA_IN(len, buf, 0), |
| 1022 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1023 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1024 | int ret; |
| 1025 | |
| 1026 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1027 | if (!len) |
| 1028 | op.ninstrs--; |
| 1029 | |
| 1030 | if (offset_in_page >= mtd->writesize) |
| 1031 | instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB; |
| 1032 | else if (offset_in_page >= 256 && |
| 1033 | !(chip->options & NAND_BUSWIDTH_16)) |
| 1034 | instrs[0].ctx.cmd.opcode = NAND_CMD_READ1; |
| 1035 | |
| 1036 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1037 | if (ret < 0) |
| 1038 | return ret; |
| 1039 | |
| 1040 | addrs[1] = page; |
| 1041 | addrs[2] = page >> 8; |
| 1042 | |
| 1043 | if (chip->options & NAND_ROW_ADDR_3) { |
| 1044 | addrs[3] = page >> 16; |
| 1045 | instrs[1].ctx.addr.naddrs++; |
| 1046 | } |
| 1047 | |
| 1048 | return nand_exec_op(chip, &op); |
| 1049 | } |
| 1050 | |
| 1051 | static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1052 | unsigned int offset_in_page, void *buf, |
| 1053 | unsigned int len) |
| 1054 | { |
| 1055 | const struct nand_sdr_timings *sdr = |
| 1056 | nand_get_sdr_timings(&chip->data_interface); |
| 1057 | u8 addrs[5]; |
| 1058 | struct nand_op_instr instrs[] = { |
| 1059 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1060 | NAND_OP_ADDR(4, addrs, 0), |
| 1061 | NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1062 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 1063 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1064 | NAND_OP_DATA_IN(len, buf, 0), |
| 1065 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1066 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1067 | int ret; |
| 1068 | |
| 1069 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1070 | if (!len) |
| 1071 | op.ninstrs--; |
| 1072 | |
| 1073 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1074 | if (ret < 0) |
| 1075 | return ret; |
| 1076 | |
| 1077 | addrs[2] = page; |
| 1078 | addrs[3] = page >> 8; |
| 1079 | |
| 1080 | if (chip->options & NAND_ROW_ADDR_3) { |
| 1081 | addrs[4] = page >> 16; |
| 1082 | instrs[1].ctx.addr.naddrs++; |
| 1083 | } |
| 1084 | |
| 1085 | return nand_exec_op(chip, &op); |
| 1086 | } |
| 1087 | |
| 1088 | /** |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1089 | * nand_read_page_op - Do a READ PAGE operation |
| 1090 | * @chip: The NAND chip |
| 1091 | * @page: page to read |
| 1092 | * @offset_in_page: offset within the page |
| 1093 | * @buf: buffer used to store the data |
| 1094 | * @len: length of the buffer |
| 1095 | * |
| 1096 | * This function issues a READ PAGE operation. |
| 1097 | * This function does not select/unselect the CS line. |
| 1098 | * |
| 1099 | * Returns 0 on success, a negative error code otherwise. |
| 1100 | */ |
| 1101 | int nand_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1102 | unsigned int offset_in_page, void *buf, unsigned int len) |
| 1103 | { |
| 1104 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1105 | |
| 1106 | if (len && !buf) |
| 1107 | return -EINVAL; |
| 1108 | |
| 1109 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1110 | return -EINVAL; |
| 1111 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1112 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1113 | if (mtd->writesize > 512) |
| 1114 | return nand_lp_exec_read_page_op(chip, page, |
| 1115 | offset_in_page, buf, |
| 1116 | len); |
| 1117 | |
| 1118 | return nand_sp_exec_read_page_op(chip, page, offset_in_page, |
| 1119 | buf, len); |
| 1120 | } |
| 1121 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1122 | chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1123 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1124 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1125 | |
| 1126 | return 0; |
| 1127 | } |
| 1128 | EXPORT_SYMBOL_GPL(nand_read_page_op); |
| 1129 | |
| 1130 | /** |
| 1131 | * nand_read_param_page_op - Do a READ PARAMETER PAGE operation |
| 1132 | * @chip: The NAND chip |
| 1133 | * @page: parameter page to read |
| 1134 | * @buf: buffer used to store the data |
| 1135 | * @len: length of the buffer |
| 1136 | * |
| 1137 | * This function issues a READ PARAMETER PAGE operation. |
| 1138 | * This function does not select/unselect the CS line. |
| 1139 | * |
| 1140 | * Returns 0 on success, a negative error code otherwise. |
| 1141 | */ |
Boris Brezillon | 1c325cc | 2018-09-07 00:38:50 +0200 | [diff] [blame] | 1142 | int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf, |
| 1143 | unsigned int len) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1144 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1145 | unsigned int i; |
| 1146 | u8 *p = buf; |
| 1147 | |
| 1148 | if (len && !buf) |
| 1149 | return -EINVAL; |
| 1150 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1151 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1152 | const struct nand_sdr_timings *sdr = |
| 1153 | nand_get_sdr_timings(&chip->data_interface); |
| 1154 | struct nand_op_instr instrs[] = { |
| 1155 | NAND_OP_CMD(NAND_CMD_PARAM, 0), |
| 1156 | NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1157 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 1158 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1159 | NAND_OP_8BIT_DATA_IN(len, buf, 0), |
| 1160 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1161 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1162 | |
| 1163 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1164 | if (!len) |
| 1165 | op.ninstrs--; |
| 1166 | |
| 1167 | return nand_exec_op(chip, &op); |
| 1168 | } |
| 1169 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1170 | chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1171 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1172 | p[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1173 | |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
| 1177 | /** |
| 1178 | * nand_change_read_column_op - Do a CHANGE READ COLUMN operation |
| 1179 | * @chip: The NAND chip |
| 1180 | * @offset_in_page: offset within the page |
| 1181 | * @buf: buffer used to store the data |
| 1182 | * @len: length of the buffer |
| 1183 | * @force_8bit: force 8-bit bus access |
| 1184 | * |
| 1185 | * This function issues a CHANGE READ COLUMN operation. |
| 1186 | * This function does not select/unselect the CS line. |
| 1187 | * |
| 1188 | * Returns 0 on success, a negative error code otherwise. |
| 1189 | */ |
| 1190 | int nand_change_read_column_op(struct nand_chip *chip, |
| 1191 | unsigned int offset_in_page, void *buf, |
| 1192 | unsigned int len, bool force_8bit) |
| 1193 | { |
| 1194 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1195 | |
| 1196 | if (len && !buf) |
| 1197 | return -EINVAL; |
| 1198 | |
| 1199 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1200 | return -EINVAL; |
| 1201 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1202 | /* Small page NANDs do not support column change. */ |
| 1203 | if (mtd->writesize <= 512) |
| 1204 | return -ENOTSUPP; |
| 1205 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1206 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1207 | const struct nand_sdr_timings *sdr = |
| 1208 | nand_get_sdr_timings(&chip->data_interface); |
| 1209 | u8 addrs[2] = {}; |
| 1210 | struct nand_op_instr instrs[] = { |
| 1211 | NAND_OP_CMD(NAND_CMD_RNDOUT, 0), |
| 1212 | NAND_OP_ADDR(2, addrs, 0), |
| 1213 | NAND_OP_CMD(NAND_CMD_RNDOUTSTART, |
| 1214 | PSEC_TO_NSEC(sdr->tCCS_min)), |
| 1215 | NAND_OP_DATA_IN(len, buf, 0), |
| 1216 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1217 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1218 | int ret; |
| 1219 | |
| 1220 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1221 | if (ret < 0) |
| 1222 | return ret; |
| 1223 | |
| 1224 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1225 | if (!len) |
| 1226 | op.ninstrs--; |
| 1227 | |
| 1228 | instrs[3].ctx.data.force_8bit = force_8bit; |
| 1229 | |
| 1230 | return nand_exec_op(chip, &op); |
| 1231 | } |
| 1232 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1233 | chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1234 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1235 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1236 | |
| 1237 | return 0; |
| 1238 | } |
| 1239 | EXPORT_SYMBOL_GPL(nand_change_read_column_op); |
| 1240 | |
| 1241 | /** |
| 1242 | * nand_read_oob_op - Do a READ OOB operation |
| 1243 | * @chip: The NAND chip |
| 1244 | * @page: page to read |
| 1245 | * @offset_in_oob: offset within the OOB area |
| 1246 | * @buf: buffer used to store the data |
| 1247 | * @len: length of the buffer |
| 1248 | * |
| 1249 | * This function issues a READ OOB operation. |
| 1250 | * This function does not select/unselect the CS line. |
| 1251 | * |
| 1252 | * Returns 0 on success, a negative error code otherwise. |
| 1253 | */ |
| 1254 | int nand_read_oob_op(struct nand_chip *chip, unsigned int page, |
| 1255 | unsigned int offset_in_oob, void *buf, unsigned int len) |
| 1256 | { |
| 1257 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1258 | |
| 1259 | if (len && !buf) |
| 1260 | return -EINVAL; |
| 1261 | |
| 1262 | if (offset_in_oob + len > mtd->oobsize) |
| 1263 | return -EINVAL; |
| 1264 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1265 | if (nand_has_exec_op(chip)) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1266 | return nand_read_page_op(chip, page, |
| 1267 | mtd->writesize + offset_in_oob, |
| 1268 | buf, len); |
| 1269 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1270 | chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1271 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1272 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1273 | |
| 1274 | return 0; |
| 1275 | } |
| 1276 | EXPORT_SYMBOL_GPL(nand_read_oob_op); |
| 1277 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1278 | static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1279 | unsigned int offset_in_page, const void *buf, |
| 1280 | unsigned int len, bool prog) |
| 1281 | { |
| 1282 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1283 | const struct nand_sdr_timings *sdr = |
| 1284 | nand_get_sdr_timings(&chip->data_interface); |
| 1285 | u8 addrs[5] = {}; |
| 1286 | struct nand_op_instr instrs[] = { |
| 1287 | /* |
| 1288 | * The first instruction will be dropped if we're dealing |
| 1289 | * with a large page NAND and adjusted if we're dealing |
| 1290 | * with a small page NAND and the page offset is > 255. |
| 1291 | */ |
| 1292 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1293 | NAND_OP_CMD(NAND_CMD_SEQIN, 0), |
| 1294 | NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1295 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1296 | NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1297 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0), |
| 1298 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1299 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1300 | int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1301 | int ret; |
| 1302 | u8 status; |
| 1303 | |
| 1304 | if (naddrs < 0) |
| 1305 | return naddrs; |
| 1306 | |
| 1307 | addrs[naddrs++] = page; |
| 1308 | addrs[naddrs++] = page >> 8; |
| 1309 | if (chip->options & NAND_ROW_ADDR_3) |
| 1310 | addrs[naddrs++] = page >> 16; |
| 1311 | |
| 1312 | instrs[2].ctx.addr.naddrs = naddrs; |
| 1313 | |
| 1314 | /* Drop the last two instructions if we're not programming the page. */ |
| 1315 | if (!prog) { |
| 1316 | op.ninstrs -= 2; |
| 1317 | /* Also drop the DATA_OUT instruction if empty. */ |
| 1318 | if (!len) |
| 1319 | op.ninstrs--; |
| 1320 | } |
| 1321 | |
| 1322 | if (mtd->writesize <= 512) { |
| 1323 | /* |
| 1324 | * Small pages need some more tweaking: we have to adjust the |
| 1325 | * first instruction depending on the page offset we're trying |
| 1326 | * to access. |
| 1327 | */ |
| 1328 | if (offset_in_page >= mtd->writesize) |
| 1329 | instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB; |
| 1330 | else if (offset_in_page >= 256 && |
| 1331 | !(chip->options & NAND_BUSWIDTH_16)) |
| 1332 | instrs[0].ctx.cmd.opcode = NAND_CMD_READ1; |
| 1333 | } else { |
| 1334 | /* |
| 1335 | * Drop the first command if we're dealing with a large page |
| 1336 | * NAND. |
| 1337 | */ |
| 1338 | op.instrs++; |
| 1339 | op.ninstrs--; |
| 1340 | } |
| 1341 | |
| 1342 | ret = nand_exec_op(chip, &op); |
| 1343 | if (!prog || ret) |
| 1344 | return ret; |
| 1345 | |
| 1346 | ret = nand_status_op(chip, &status); |
| 1347 | if (ret) |
| 1348 | return ret; |
| 1349 | |
| 1350 | return status; |
| 1351 | } |
| 1352 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1353 | /** |
| 1354 | * nand_prog_page_begin_op - starts a PROG PAGE operation |
| 1355 | * @chip: The NAND chip |
| 1356 | * @page: page to write |
| 1357 | * @offset_in_page: offset within the page |
| 1358 | * @buf: buffer containing the data to write to the page |
| 1359 | * @len: length of the buffer |
| 1360 | * |
| 1361 | * This function issues the first half of a PROG PAGE operation. |
| 1362 | * This function does not select/unselect the CS line. |
| 1363 | * |
| 1364 | * Returns 0 on success, a negative error code otherwise. |
| 1365 | */ |
| 1366 | int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page, |
| 1367 | unsigned int offset_in_page, const void *buf, |
| 1368 | unsigned int len) |
| 1369 | { |
| 1370 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1371 | |
| 1372 | if (len && !buf) |
| 1373 | return -EINVAL; |
| 1374 | |
| 1375 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1376 | return -EINVAL; |
| 1377 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1378 | if (nand_has_exec_op(chip)) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1379 | return nand_exec_prog_page_op(chip, page, offset_in_page, buf, |
| 1380 | len, false); |
| 1381 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1382 | chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1383 | |
| 1384 | if (buf) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1385 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1386 | |
| 1387 | return 0; |
| 1388 | } |
| 1389 | EXPORT_SYMBOL_GPL(nand_prog_page_begin_op); |
| 1390 | |
| 1391 | /** |
| 1392 | * nand_prog_page_end_op - ends a PROG PAGE operation |
| 1393 | * @chip: The NAND chip |
| 1394 | * |
| 1395 | * This function issues the second half of a PROG PAGE operation. |
| 1396 | * This function does not select/unselect the CS line. |
| 1397 | * |
| 1398 | * Returns 0 on success, a negative error code otherwise. |
| 1399 | */ |
| 1400 | int nand_prog_page_end_op(struct nand_chip *chip) |
| 1401 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1402 | int ret; |
| 1403 | u8 status; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1404 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1405 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1406 | const struct nand_sdr_timings *sdr = |
| 1407 | nand_get_sdr_timings(&chip->data_interface); |
| 1408 | struct nand_op_instr instrs[] = { |
| 1409 | NAND_OP_CMD(NAND_CMD_PAGEPROG, |
| 1410 | PSEC_TO_NSEC(sdr->tWB_max)), |
| 1411 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0), |
| 1412 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1413 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1414 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1415 | ret = nand_exec_op(chip, &op); |
| 1416 | if (ret) |
| 1417 | return ret; |
| 1418 | |
| 1419 | ret = nand_status_op(chip, &status); |
| 1420 | if (ret) |
| 1421 | return ret; |
| 1422 | } else { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1423 | chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1); |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1424 | ret = chip->legacy.waitfunc(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1425 | if (ret < 0) |
| 1426 | return ret; |
| 1427 | |
| 1428 | status = ret; |
| 1429 | } |
| 1430 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1431 | if (status & NAND_STATUS_FAIL) |
| 1432 | return -EIO; |
| 1433 | |
| 1434 | return 0; |
| 1435 | } |
| 1436 | EXPORT_SYMBOL_GPL(nand_prog_page_end_op); |
| 1437 | |
| 1438 | /** |
| 1439 | * nand_prog_page_op - Do a full PROG PAGE operation |
| 1440 | * @chip: The NAND chip |
| 1441 | * @page: page to write |
| 1442 | * @offset_in_page: offset within the page |
| 1443 | * @buf: buffer containing the data to write to the page |
| 1444 | * @len: length of the buffer |
| 1445 | * |
| 1446 | * This function issues a full PROG PAGE operation. |
| 1447 | * This function does not select/unselect the CS line. |
| 1448 | * |
| 1449 | * Returns 0 on success, a negative error code otherwise. |
| 1450 | */ |
| 1451 | int nand_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1452 | unsigned int offset_in_page, const void *buf, |
| 1453 | unsigned int len) |
| 1454 | { |
| 1455 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1456 | int status; |
| 1457 | |
| 1458 | if (!len || !buf) |
| 1459 | return -EINVAL; |
| 1460 | |
| 1461 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1462 | return -EINVAL; |
| 1463 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1464 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1465 | status = nand_exec_prog_page_op(chip, page, offset_in_page, buf, |
| 1466 | len, true); |
| 1467 | } else { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1468 | chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, |
| 1469 | page); |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1470 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1471 | chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1); |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1472 | status = chip->legacy.waitfunc(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1473 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1474 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1475 | if (status & NAND_STATUS_FAIL) |
| 1476 | return -EIO; |
| 1477 | |
| 1478 | return 0; |
| 1479 | } |
| 1480 | EXPORT_SYMBOL_GPL(nand_prog_page_op); |
| 1481 | |
| 1482 | /** |
| 1483 | * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation |
| 1484 | * @chip: The NAND chip |
| 1485 | * @offset_in_page: offset within the page |
| 1486 | * @buf: buffer containing the data to send to the NAND |
| 1487 | * @len: length of the buffer |
| 1488 | * @force_8bit: force 8-bit bus access |
| 1489 | * |
| 1490 | * This function issues a CHANGE WRITE COLUMN operation. |
| 1491 | * This function does not select/unselect the CS line. |
| 1492 | * |
| 1493 | * Returns 0 on success, a negative error code otherwise. |
| 1494 | */ |
| 1495 | int nand_change_write_column_op(struct nand_chip *chip, |
| 1496 | unsigned int offset_in_page, |
| 1497 | const void *buf, unsigned int len, |
| 1498 | bool force_8bit) |
| 1499 | { |
| 1500 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1501 | |
| 1502 | if (len && !buf) |
| 1503 | return -EINVAL; |
| 1504 | |
| 1505 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1506 | return -EINVAL; |
| 1507 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1508 | /* Small page NANDs do not support column change. */ |
| 1509 | if (mtd->writesize <= 512) |
| 1510 | return -ENOTSUPP; |
| 1511 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1512 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1513 | const struct nand_sdr_timings *sdr = |
| 1514 | nand_get_sdr_timings(&chip->data_interface); |
| 1515 | u8 addrs[2]; |
| 1516 | struct nand_op_instr instrs[] = { |
| 1517 | NAND_OP_CMD(NAND_CMD_RNDIN, 0), |
| 1518 | NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)), |
| 1519 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1520 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1521 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1522 | int ret; |
| 1523 | |
| 1524 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1525 | if (ret < 0) |
| 1526 | return ret; |
| 1527 | |
| 1528 | instrs[2].ctx.data.force_8bit = force_8bit; |
| 1529 | |
| 1530 | /* Drop the DATA_OUT instruction if len is set to 0. */ |
| 1531 | if (!len) |
| 1532 | op.ninstrs--; |
| 1533 | |
| 1534 | return nand_exec_op(chip, &op); |
| 1535 | } |
| 1536 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1537 | chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1538 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1539 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1540 | |
| 1541 | return 0; |
| 1542 | } |
| 1543 | EXPORT_SYMBOL_GPL(nand_change_write_column_op); |
| 1544 | |
| 1545 | /** |
| 1546 | * nand_readid_op - Do a READID operation |
| 1547 | * @chip: The NAND chip |
| 1548 | * @addr: address cycle to pass after the READID command |
| 1549 | * @buf: buffer used to store the ID |
| 1550 | * @len: length of the buffer |
| 1551 | * |
| 1552 | * This function sends a READID command and reads back the ID returned by the |
| 1553 | * NAND. |
| 1554 | * This function does not select/unselect the CS line. |
| 1555 | * |
| 1556 | * Returns 0 on success, a negative error code otherwise. |
| 1557 | */ |
| 1558 | int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf, |
| 1559 | unsigned int len) |
| 1560 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1561 | unsigned int i; |
| 1562 | u8 *id = buf; |
| 1563 | |
| 1564 | if (len && !buf) |
| 1565 | return -EINVAL; |
| 1566 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1567 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1568 | const struct nand_sdr_timings *sdr = |
| 1569 | nand_get_sdr_timings(&chip->data_interface); |
| 1570 | struct nand_op_instr instrs[] = { |
| 1571 | NAND_OP_CMD(NAND_CMD_READID, 0), |
| 1572 | NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1573 | NAND_OP_8BIT_DATA_IN(len, buf, 0), |
| 1574 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1575 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1576 | |
| 1577 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1578 | if (!len) |
| 1579 | op.ninstrs--; |
| 1580 | |
| 1581 | return nand_exec_op(chip, &op); |
| 1582 | } |
| 1583 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1584 | chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1585 | |
| 1586 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1587 | id[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1588 | |
| 1589 | return 0; |
| 1590 | } |
| 1591 | EXPORT_SYMBOL_GPL(nand_readid_op); |
| 1592 | |
| 1593 | /** |
| 1594 | * nand_status_op - Do a STATUS operation |
| 1595 | * @chip: The NAND chip |
| 1596 | * @status: out variable to store the NAND status |
| 1597 | * |
| 1598 | * This function sends a STATUS command and reads back the status returned by |
| 1599 | * the NAND. |
| 1600 | * This function does not select/unselect the CS line. |
| 1601 | * |
| 1602 | * Returns 0 on success, a negative error code otherwise. |
| 1603 | */ |
| 1604 | int nand_status_op(struct nand_chip *chip, u8 *status) |
| 1605 | { |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1606 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1607 | const struct nand_sdr_timings *sdr = |
| 1608 | nand_get_sdr_timings(&chip->data_interface); |
| 1609 | struct nand_op_instr instrs[] = { |
| 1610 | NAND_OP_CMD(NAND_CMD_STATUS, |
| 1611 | PSEC_TO_NSEC(sdr->tADL_min)), |
| 1612 | NAND_OP_8BIT_DATA_IN(1, status, 0), |
| 1613 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1614 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1615 | |
| 1616 | if (!status) |
| 1617 | op.ninstrs--; |
| 1618 | |
| 1619 | return nand_exec_op(chip, &op); |
| 1620 | } |
| 1621 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1622 | chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1623 | if (status) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1624 | *status = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1625 | |
| 1626 | return 0; |
| 1627 | } |
| 1628 | EXPORT_SYMBOL_GPL(nand_status_op); |
| 1629 | |
| 1630 | /** |
| 1631 | * nand_exit_status_op - Exit a STATUS operation |
| 1632 | * @chip: The NAND chip |
| 1633 | * |
| 1634 | * This function sends a READ0 command to cancel the effect of the STATUS |
| 1635 | * command to avoid reading only the status until a new read command is sent. |
| 1636 | * |
| 1637 | * This function does not select/unselect the CS line. |
| 1638 | * |
| 1639 | * Returns 0 on success, a negative error code otherwise. |
| 1640 | */ |
| 1641 | int nand_exit_status_op(struct nand_chip *chip) |
| 1642 | { |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1643 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1644 | struct nand_op_instr instrs[] = { |
| 1645 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1646 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1647 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1648 | |
| 1649 | return nand_exec_op(chip, &op); |
| 1650 | } |
| 1651 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1652 | chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1653 | |
| 1654 | return 0; |
| 1655 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1656 | |
| 1657 | /** |
| 1658 | * nand_erase_op - Do an erase operation |
| 1659 | * @chip: The NAND chip |
| 1660 | * @eraseblock: block to erase |
| 1661 | * |
| 1662 | * This function sends an ERASE command and waits for the NAND to be ready |
| 1663 | * before returning. |
| 1664 | * This function does not select/unselect the CS line. |
| 1665 | * |
| 1666 | * Returns 0 on success, a negative error code otherwise. |
| 1667 | */ |
| 1668 | int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock) |
| 1669 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1670 | unsigned int page = eraseblock << |
| 1671 | (chip->phys_erase_shift - chip->page_shift); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1672 | int ret; |
| 1673 | u8 status; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1674 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1675 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1676 | const struct nand_sdr_timings *sdr = |
| 1677 | nand_get_sdr_timings(&chip->data_interface); |
| 1678 | u8 addrs[3] = { page, page >> 8, page >> 16 }; |
| 1679 | struct nand_op_instr instrs[] = { |
| 1680 | NAND_OP_CMD(NAND_CMD_ERASE1, 0), |
| 1681 | NAND_OP_ADDR(2, addrs, 0), |
| 1682 | NAND_OP_CMD(NAND_CMD_ERASE2, |
| 1683 | PSEC_TO_MSEC(sdr->tWB_max)), |
| 1684 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0), |
| 1685 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1686 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1687 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1688 | if (chip->options & NAND_ROW_ADDR_3) |
| 1689 | instrs[1].ctx.addr.naddrs++; |
| 1690 | |
| 1691 | ret = nand_exec_op(chip, &op); |
| 1692 | if (ret) |
| 1693 | return ret; |
| 1694 | |
| 1695 | ret = nand_status_op(chip, &status); |
| 1696 | if (ret) |
| 1697 | return ret; |
| 1698 | } else { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1699 | chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page); |
| 1700 | chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1701 | |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1702 | ret = chip->legacy.waitfunc(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1703 | if (ret < 0) |
| 1704 | return ret; |
| 1705 | |
| 1706 | status = ret; |
| 1707 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1708 | |
| 1709 | if (status & NAND_STATUS_FAIL) |
| 1710 | return -EIO; |
| 1711 | |
| 1712 | return 0; |
| 1713 | } |
| 1714 | EXPORT_SYMBOL_GPL(nand_erase_op); |
| 1715 | |
| 1716 | /** |
| 1717 | * nand_set_features_op - Do a SET FEATURES operation |
| 1718 | * @chip: The NAND chip |
| 1719 | * @feature: feature id |
| 1720 | * @data: 4 bytes of data |
| 1721 | * |
| 1722 | * This function sends a SET FEATURES command and waits for the NAND to be |
| 1723 | * ready before returning. |
| 1724 | * This function does not select/unselect the CS line. |
| 1725 | * |
| 1726 | * Returns 0 on success, a negative error code otherwise. |
| 1727 | */ |
| 1728 | static int nand_set_features_op(struct nand_chip *chip, u8 feature, |
| 1729 | const void *data) |
| 1730 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1731 | const u8 *params = data; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1732 | int i, ret; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1733 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1734 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1735 | const struct nand_sdr_timings *sdr = |
| 1736 | nand_get_sdr_timings(&chip->data_interface); |
| 1737 | struct nand_op_instr instrs[] = { |
| 1738 | NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0), |
| 1739 | NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1740 | NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data, |
| 1741 | PSEC_TO_NSEC(sdr->tWB_max)), |
| 1742 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0), |
| 1743 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1744 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1745 | |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1746 | return nand_exec_op(chip, &op); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1747 | } |
| 1748 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1749 | chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1); |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1750 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1751 | chip->legacy.write_byte(chip, params[i]); |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1752 | |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1753 | ret = chip->legacy.waitfunc(chip); |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1754 | if (ret < 0) |
| 1755 | return ret; |
| 1756 | |
| 1757 | if (ret & NAND_STATUS_FAIL) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1758 | return -EIO; |
| 1759 | |
| 1760 | return 0; |
| 1761 | } |
| 1762 | |
| 1763 | /** |
| 1764 | * nand_get_features_op - Do a GET FEATURES operation |
| 1765 | * @chip: The NAND chip |
| 1766 | * @feature: feature id |
| 1767 | * @data: 4 bytes of data |
| 1768 | * |
| 1769 | * This function sends a GET FEATURES command and waits for the NAND to be |
| 1770 | * ready before returning. |
| 1771 | * This function does not select/unselect the CS line. |
| 1772 | * |
| 1773 | * Returns 0 on success, a negative error code otherwise. |
| 1774 | */ |
| 1775 | static int nand_get_features_op(struct nand_chip *chip, u8 feature, |
| 1776 | void *data) |
| 1777 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1778 | u8 *params = data; |
| 1779 | int i; |
| 1780 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1781 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1782 | const struct nand_sdr_timings *sdr = |
| 1783 | nand_get_sdr_timings(&chip->data_interface); |
| 1784 | struct nand_op_instr instrs[] = { |
| 1785 | NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0), |
| 1786 | NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1787 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), |
| 1788 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1789 | NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN, |
| 1790 | data, 0), |
| 1791 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1792 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1793 | |
| 1794 | return nand_exec_op(chip, &op); |
| 1795 | } |
| 1796 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1797 | chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1798 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1799 | params[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1800 | |
| 1801 | return 0; |
| 1802 | } |
| 1803 | |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1804 | static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms, |
| 1805 | unsigned int delay_ns) |
| 1806 | { |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1807 | if (nand_has_exec_op(chip)) { |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1808 | struct nand_op_instr instrs[] = { |
| 1809 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms), |
| 1810 | PSEC_TO_NSEC(delay_ns)), |
| 1811 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1812 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1813 | |
| 1814 | return nand_exec_op(chip, &op); |
| 1815 | } |
| 1816 | |
| 1817 | /* Apply delay or wait for ready/busy pin */ |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1818 | if (!chip->legacy.dev_ready) |
Boris Brezillon | 3cece3a | 2018-09-07 00:38:41 +0200 | [diff] [blame] | 1819 | udelay(chip->legacy.chip_delay); |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1820 | else |
Boris Brezillon | 2b356ab | 2018-09-06 14:05:16 +0200 | [diff] [blame] | 1821 | nand_wait_ready(chip); |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1822 | |
| 1823 | return 0; |
| 1824 | } |
| 1825 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1826 | /** |
| 1827 | * nand_reset_op - Do a reset operation |
| 1828 | * @chip: The NAND chip |
| 1829 | * |
| 1830 | * This function sends a RESET command and waits for the NAND to be ready |
| 1831 | * before returning. |
| 1832 | * This function does not select/unselect the CS line. |
| 1833 | * |
| 1834 | * Returns 0 on success, a negative error code otherwise. |
| 1835 | */ |
| 1836 | int nand_reset_op(struct nand_chip *chip) |
| 1837 | { |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1838 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1839 | const struct nand_sdr_timings *sdr = |
| 1840 | nand_get_sdr_timings(&chip->data_interface); |
| 1841 | struct nand_op_instr instrs[] = { |
| 1842 | NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1843 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0), |
| 1844 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1845 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1846 | |
| 1847 | return nand_exec_op(chip, &op); |
| 1848 | } |
| 1849 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1850 | chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1851 | |
| 1852 | return 0; |
| 1853 | } |
| 1854 | EXPORT_SYMBOL_GPL(nand_reset_op); |
| 1855 | |
| 1856 | /** |
| 1857 | * nand_read_data_op - Read data from the NAND |
| 1858 | * @chip: The NAND chip |
| 1859 | * @buf: buffer used to store the data |
| 1860 | * @len: length of the buffer |
| 1861 | * @force_8bit: force 8-bit bus access |
| 1862 | * |
| 1863 | * This function does a raw data read on the bus. Usually used after launching |
| 1864 | * another NAND operation like nand_read_page_op(). |
| 1865 | * This function does not select/unselect the CS line. |
| 1866 | * |
| 1867 | * Returns 0 on success, a negative error code otherwise. |
| 1868 | */ |
| 1869 | int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len, |
| 1870 | bool force_8bit) |
| 1871 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1872 | if (!len || !buf) |
| 1873 | return -EINVAL; |
| 1874 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1875 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1876 | struct nand_op_instr instrs[] = { |
| 1877 | NAND_OP_DATA_IN(len, buf, 0), |
| 1878 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1879 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1880 | |
| 1881 | instrs[0].ctx.data.force_8bit = force_8bit; |
| 1882 | |
| 1883 | return nand_exec_op(chip, &op); |
| 1884 | } |
| 1885 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1886 | if (force_8bit) { |
| 1887 | u8 *p = buf; |
| 1888 | unsigned int i; |
| 1889 | |
| 1890 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1891 | p[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1892 | } else { |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1893 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | return 0; |
| 1897 | } |
| 1898 | EXPORT_SYMBOL_GPL(nand_read_data_op); |
| 1899 | |
| 1900 | /** |
| 1901 | * nand_write_data_op - Write data from the NAND |
| 1902 | * @chip: The NAND chip |
| 1903 | * @buf: buffer containing the data to send on the bus |
| 1904 | * @len: length of the buffer |
| 1905 | * @force_8bit: force 8-bit bus access |
| 1906 | * |
| 1907 | * This function does a raw data write on the bus. Usually used after launching |
| 1908 | * another NAND operation like nand_write_page_begin_op(). |
| 1909 | * This function does not select/unselect the CS line. |
| 1910 | * |
| 1911 | * Returns 0 on success, a negative error code otherwise. |
| 1912 | */ |
| 1913 | int nand_write_data_op(struct nand_chip *chip, const void *buf, |
| 1914 | unsigned int len, bool force_8bit) |
| 1915 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1916 | if (!len || !buf) |
| 1917 | return -EINVAL; |
| 1918 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1919 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1920 | struct nand_op_instr instrs[] = { |
| 1921 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1922 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1923 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1924 | |
| 1925 | instrs[0].ctx.data.force_8bit = force_8bit; |
| 1926 | |
| 1927 | return nand_exec_op(chip, &op); |
| 1928 | } |
| 1929 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1930 | if (force_8bit) { |
| 1931 | const u8 *p = buf; |
| 1932 | unsigned int i; |
| 1933 | |
| 1934 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1935 | chip->legacy.write_byte(chip, p[i]); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1936 | } else { |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1937 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1938 | } |
| 1939 | |
| 1940 | return 0; |
| 1941 | } |
| 1942 | EXPORT_SYMBOL_GPL(nand_write_data_op); |
| 1943 | |
| 1944 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1945 | * struct nand_op_parser_ctx - Context used by the parser |
| 1946 | * @instrs: array of all the instructions that must be addressed |
| 1947 | * @ninstrs: length of the @instrs array |
| 1948 | * @subop: Sub-operation to be passed to the NAND controller |
| 1949 | * |
| 1950 | * This structure is used by the core to split NAND operations into |
| 1951 | * sub-operations that can be handled by the NAND controller. |
| 1952 | */ |
| 1953 | struct nand_op_parser_ctx { |
| 1954 | const struct nand_op_instr *instrs; |
| 1955 | unsigned int ninstrs; |
| 1956 | struct nand_subop subop; |
| 1957 | }; |
| 1958 | |
| 1959 | /** |
| 1960 | * nand_op_parser_must_split_instr - Checks if an instruction must be split |
| 1961 | * @pat: the parser pattern element that matches @instr |
| 1962 | * @instr: pointer to the instruction to check |
| 1963 | * @start_offset: this is an in/out parameter. If @instr has already been |
| 1964 | * split, then @start_offset is the offset from which to start |
| 1965 | * (either an address cycle or an offset in the data buffer). |
| 1966 | * Conversely, if the function returns true (ie. instr must be |
| 1967 | * split), this parameter is updated to point to the first |
| 1968 | * data/address cycle that has not been taken care of. |
| 1969 | * |
| 1970 | * Some NAND controllers are limited and cannot send X address cycles with a |
| 1971 | * unique operation, or cannot read/write more than Y bytes at the same time. |
| 1972 | * In this case, split the instruction that does not fit in a single |
| 1973 | * controller-operation into two or more chunks. |
| 1974 | * |
| 1975 | * Returns true if the instruction must be split, false otherwise. |
| 1976 | * The @start_offset parameter is also updated to the offset at which the next |
| 1977 | * bundle of instruction must start (if an address or a data instruction). |
| 1978 | */ |
| 1979 | static bool |
| 1980 | nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat, |
| 1981 | const struct nand_op_instr *instr, |
| 1982 | unsigned int *start_offset) |
| 1983 | { |
| 1984 | switch (pat->type) { |
| 1985 | case NAND_OP_ADDR_INSTR: |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 1986 | if (!pat->ctx.addr.maxcycles) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1987 | break; |
| 1988 | |
| 1989 | if (instr->ctx.addr.naddrs - *start_offset > |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 1990 | pat->ctx.addr.maxcycles) { |
| 1991 | *start_offset += pat->ctx.addr.maxcycles; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1992 | return true; |
| 1993 | } |
| 1994 | break; |
| 1995 | |
| 1996 | case NAND_OP_DATA_IN_INSTR: |
| 1997 | case NAND_OP_DATA_OUT_INSTR: |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 1998 | if (!pat->ctx.data.maxlen) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1999 | break; |
| 2000 | |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 2001 | if (instr->ctx.data.len - *start_offset > |
| 2002 | pat->ctx.data.maxlen) { |
| 2003 | *start_offset += pat->ctx.data.maxlen; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2004 | return true; |
| 2005 | } |
| 2006 | break; |
| 2007 | |
| 2008 | default: |
| 2009 | break; |
| 2010 | } |
| 2011 | |
| 2012 | return false; |
| 2013 | } |
| 2014 | |
| 2015 | /** |
| 2016 | * nand_op_parser_match_pat - Checks if a pattern matches the instructions |
| 2017 | * remaining in the parser context |
| 2018 | * @pat: the pattern to test |
| 2019 | * @ctx: the parser context structure to match with the pattern @pat |
| 2020 | * |
| 2021 | * Check if @pat matches the set or a sub-set of instructions remaining in @ctx. |
| 2022 | * Returns true if this is the case, false ortherwise. When true is returned, |
| 2023 | * @ctx->subop is updated with the set of instructions to be passed to the |
| 2024 | * controller driver. |
| 2025 | */ |
| 2026 | static bool |
| 2027 | nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat, |
| 2028 | struct nand_op_parser_ctx *ctx) |
| 2029 | { |
| 2030 | unsigned int instr_offset = ctx->subop.first_instr_start_off; |
| 2031 | const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs; |
| 2032 | const struct nand_op_instr *instr = ctx->subop.instrs; |
| 2033 | unsigned int i, ninstrs; |
| 2034 | |
| 2035 | for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) { |
| 2036 | /* |
| 2037 | * The pattern instruction does not match the operation |
| 2038 | * instruction. If the instruction is marked optional in the |
| 2039 | * pattern definition, we skip the pattern element and continue |
| 2040 | * to the next one. If the element is mandatory, there's no |
| 2041 | * match and we can return false directly. |
| 2042 | */ |
| 2043 | if (instr->type != pat->elems[i].type) { |
| 2044 | if (!pat->elems[i].optional) |
| 2045 | return false; |
| 2046 | |
| 2047 | continue; |
| 2048 | } |
| 2049 | |
| 2050 | /* |
| 2051 | * Now check the pattern element constraints. If the pattern is |
| 2052 | * not able to handle the whole instruction in a single step, |
| 2053 | * we have to split it. |
| 2054 | * The last_instr_end_off value comes back updated to point to |
| 2055 | * the position where we have to split the instruction (the |
| 2056 | * start of the next subop chunk). |
| 2057 | */ |
| 2058 | if (nand_op_parser_must_split_instr(&pat->elems[i], instr, |
| 2059 | &instr_offset)) { |
| 2060 | ninstrs++; |
| 2061 | i++; |
| 2062 | break; |
| 2063 | } |
| 2064 | |
| 2065 | instr++; |
| 2066 | ninstrs++; |
| 2067 | instr_offset = 0; |
| 2068 | } |
| 2069 | |
| 2070 | /* |
| 2071 | * This can happen if all instructions of a pattern are optional. |
| 2072 | * Still, if there's not at least one instruction handled by this |
| 2073 | * pattern, this is not a match, and we should try the next one (if |
| 2074 | * any). |
| 2075 | */ |
| 2076 | if (!ninstrs) |
| 2077 | return false; |
| 2078 | |
| 2079 | /* |
| 2080 | * We had a match on the pattern head, but the pattern may be longer |
| 2081 | * than the instructions we're asked to execute. We need to make sure |
| 2082 | * there's no mandatory elements in the pattern tail. |
| 2083 | */ |
| 2084 | for (; i < pat->nelems; i++) { |
| 2085 | if (!pat->elems[i].optional) |
| 2086 | return false; |
| 2087 | } |
| 2088 | |
| 2089 | /* |
| 2090 | * We have a match: update the subop structure accordingly and return |
| 2091 | * true. |
| 2092 | */ |
| 2093 | ctx->subop.ninstrs = ninstrs; |
| 2094 | ctx->subop.last_instr_end_off = instr_offset; |
| 2095 | |
| 2096 | return true; |
| 2097 | } |
| 2098 | |
| 2099 | #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG) |
| 2100 | static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx) |
| 2101 | { |
| 2102 | const struct nand_op_instr *instr; |
| 2103 | char *prefix = " "; |
| 2104 | unsigned int i; |
| 2105 | |
| 2106 | pr_debug("executing subop:\n"); |
| 2107 | |
| 2108 | for (i = 0; i < ctx->ninstrs; i++) { |
| 2109 | instr = &ctx->instrs[i]; |
| 2110 | |
| 2111 | if (instr == &ctx->subop.instrs[0]) |
| 2112 | prefix = " ->"; |
| 2113 | |
| 2114 | switch (instr->type) { |
| 2115 | case NAND_OP_CMD_INSTR: |
| 2116 | pr_debug("%sCMD [0x%02x]\n", prefix, |
| 2117 | instr->ctx.cmd.opcode); |
| 2118 | break; |
| 2119 | case NAND_OP_ADDR_INSTR: |
| 2120 | pr_debug("%sADDR [%d cyc: %*ph]\n", prefix, |
| 2121 | instr->ctx.addr.naddrs, |
| 2122 | instr->ctx.addr.naddrs < 64 ? |
| 2123 | instr->ctx.addr.naddrs : 64, |
| 2124 | instr->ctx.addr.addrs); |
| 2125 | break; |
| 2126 | case NAND_OP_DATA_IN_INSTR: |
| 2127 | pr_debug("%sDATA_IN [%d B%s]\n", prefix, |
| 2128 | instr->ctx.data.len, |
| 2129 | instr->ctx.data.force_8bit ? |
| 2130 | ", force 8-bit" : ""); |
| 2131 | break; |
| 2132 | case NAND_OP_DATA_OUT_INSTR: |
| 2133 | pr_debug("%sDATA_OUT [%d B%s]\n", prefix, |
| 2134 | instr->ctx.data.len, |
| 2135 | instr->ctx.data.force_8bit ? |
| 2136 | ", force 8-bit" : ""); |
| 2137 | break; |
| 2138 | case NAND_OP_WAITRDY_INSTR: |
| 2139 | pr_debug("%sWAITRDY [max %d ms]\n", prefix, |
| 2140 | instr->ctx.waitrdy.timeout_ms); |
| 2141 | break; |
| 2142 | } |
| 2143 | |
| 2144 | if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1]) |
| 2145 | prefix = " "; |
| 2146 | } |
| 2147 | } |
| 2148 | #else |
| 2149 | static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx) |
| 2150 | { |
| 2151 | /* NOP */ |
| 2152 | } |
| 2153 | #endif |
| 2154 | |
| 2155 | /** |
| 2156 | * nand_op_parser_exec_op - exec_op parser |
| 2157 | * @chip: the NAND chip |
| 2158 | * @parser: patterns description provided by the controller driver |
| 2159 | * @op: the NAND operation to address |
| 2160 | * @check_only: when true, the function only checks if @op can be handled but |
| 2161 | * does not execute the operation |
| 2162 | * |
| 2163 | * Helper function designed to ease integration of NAND controller drivers that |
| 2164 | * only support a limited set of instruction sequences. The supported sequences |
| 2165 | * are described in @parser, and the framework takes care of splitting @op into |
| 2166 | * multiple sub-operations (if required) and pass them back to the ->exec() |
| 2167 | * callback of the matching pattern if @check_only is set to false. |
| 2168 | * |
| 2169 | * NAND controller drivers should call this function from their own ->exec_op() |
| 2170 | * implementation. |
| 2171 | * |
| 2172 | * Returns 0 on success, a negative error code otherwise. A failure can be |
| 2173 | * caused by an unsupported operation (none of the supported patterns is able |
| 2174 | * to handle the requested operation), or an error returned by one of the |
| 2175 | * matching pattern->exec() hook. |
| 2176 | */ |
| 2177 | int nand_op_parser_exec_op(struct nand_chip *chip, |
| 2178 | const struct nand_op_parser *parser, |
| 2179 | const struct nand_operation *op, bool check_only) |
| 2180 | { |
| 2181 | struct nand_op_parser_ctx ctx = { |
| 2182 | .subop.instrs = op->instrs, |
| 2183 | .instrs = op->instrs, |
| 2184 | .ninstrs = op->ninstrs, |
| 2185 | }; |
| 2186 | unsigned int i; |
| 2187 | |
| 2188 | while (ctx.subop.instrs < op->instrs + op->ninstrs) { |
| 2189 | int ret; |
| 2190 | |
| 2191 | for (i = 0; i < parser->npatterns; i++) { |
| 2192 | const struct nand_op_parser_pattern *pattern; |
| 2193 | |
| 2194 | pattern = &parser->patterns[i]; |
| 2195 | if (!nand_op_parser_match_pat(pattern, &ctx)) |
| 2196 | continue; |
| 2197 | |
| 2198 | nand_op_parser_trace(&ctx); |
| 2199 | |
| 2200 | if (check_only) |
| 2201 | break; |
| 2202 | |
| 2203 | ret = pattern->exec(chip, &ctx.subop); |
| 2204 | if (ret) |
| 2205 | return ret; |
| 2206 | |
| 2207 | break; |
| 2208 | } |
| 2209 | |
| 2210 | if (i == parser->npatterns) { |
| 2211 | pr_debug("->exec_op() parser: pattern not found!\n"); |
| 2212 | return -ENOTSUPP; |
| 2213 | } |
| 2214 | |
| 2215 | /* |
| 2216 | * Update the context structure by pointing to the start of the |
| 2217 | * next subop. |
| 2218 | */ |
| 2219 | ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs; |
| 2220 | if (ctx.subop.last_instr_end_off) |
| 2221 | ctx.subop.instrs -= 1; |
| 2222 | |
| 2223 | ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off; |
| 2224 | } |
| 2225 | |
| 2226 | return 0; |
| 2227 | } |
| 2228 | EXPORT_SYMBOL_GPL(nand_op_parser_exec_op); |
| 2229 | |
| 2230 | static bool nand_instr_is_data(const struct nand_op_instr *instr) |
| 2231 | { |
| 2232 | return instr && (instr->type == NAND_OP_DATA_IN_INSTR || |
| 2233 | instr->type == NAND_OP_DATA_OUT_INSTR); |
| 2234 | } |
| 2235 | |
| 2236 | static bool nand_subop_instr_is_valid(const struct nand_subop *subop, |
| 2237 | unsigned int instr_idx) |
| 2238 | { |
| 2239 | return subop && instr_idx < subop->ninstrs; |
| 2240 | } |
| 2241 | |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2242 | static unsigned int nand_subop_get_start_off(const struct nand_subop *subop, |
| 2243 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2244 | { |
| 2245 | if (instr_idx) |
| 2246 | return 0; |
| 2247 | |
| 2248 | return subop->first_instr_start_off; |
| 2249 | } |
| 2250 | |
| 2251 | /** |
| 2252 | * nand_subop_get_addr_start_off - Get the start offset in an address array |
| 2253 | * @subop: The entire sub-operation |
| 2254 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2255 | * |
| 2256 | * During driver development, one could be tempted to directly use the |
| 2257 | * ->addr.addrs field of address instructions. This is wrong as address |
| 2258 | * instructions might be split. |
| 2259 | * |
| 2260 | * Given an address instruction, returns the offset of the first cycle to issue. |
| 2261 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2262 | unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop, |
| 2263 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2264 | { |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2265 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2266 | subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)) |
| 2267 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2268 | |
| 2269 | return nand_subop_get_start_off(subop, instr_idx); |
| 2270 | } |
| 2271 | EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off); |
| 2272 | |
| 2273 | /** |
| 2274 | * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert |
| 2275 | * @subop: The entire sub-operation |
| 2276 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2277 | * |
| 2278 | * During driver development, one could be tempted to directly use the |
| 2279 | * ->addr->naddrs field of a data instruction. This is wrong as instructions |
| 2280 | * might be split. |
| 2281 | * |
| 2282 | * Given an address instruction, returns the number of address cycle to issue. |
| 2283 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2284 | unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop, |
| 2285 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2286 | { |
| 2287 | int start_off, end_off; |
| 2288 | |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2289 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2290 | subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)) |
| 2291 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2292 | |
| 2293 | start_off = nand_subop_get_addr_start_off(subop, instr_idx); |
| 2294 | |
| 2295 | if (instr_idx == subop->ninstrs - 1 && |
| 2296 | subop->last_instr_end_off) |
| 2297 | end_off = subop->last_instr_end_off; |
| 2298 | else |
| 2299 | end_off = subop->instrs[instr_idx].ctx.addr.naddrs; |
| 2300 | |
| 2301 | return end_off - start_off; |
| 2302 | } |
| 2303 | EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc); |
| 2304 | |
| 2305 | /** |
| 2306 | * nand_subop_get_data_start_off - Get the start offset in a data array |
| 2307 | * @subop: The entire sub-operation |
| 2308 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2309 | * |
| 2310 | * During driver development, one could be tempted to directly use the |
| 2311 | * ->data->buf.{in,out} field of data instructions. This is wrong as data |
| 2312 | * instructions might be split. |
| 2313 | * |
| 2314 | * Given a data instruction, returns the offset to start from. |
| 2315 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2316 | unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop, |
| 2317 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2318 | { |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2319 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2320 | !nand_instr_is_data(&subop->instrs[instr_idx]))) |
| 2321 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2322 | |
| 2323 | return nand_subop_get_start_off(subop, instr_idx); |
| 2324 | } |
| 2325 | EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off); |
| 2326 | |
| 2327 | /** |
| 2328 | * nand_subop_get_data_len - Get the number of bytes to retrieve |
| 2329 | * @subop: The entire sub-operation |
| 2330 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2331 | * |
| 2332 | * During driver development, one could be tempted to directly use the |
| 2333 | * ->data->len field of a data instruction. This is wrong as data instructions |
| 2334 | * might be split. |
| 2335 | * |
| 2336 | * Returns the length of the chunk of data to send/receive. |
| 2337 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2338 | unsigned int nand_subop_get_data_len(const struct nand_subop *subop, |
| 2339 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2340 | { |
| 2341 | int start_off = 0, end_off; |
| 2342 | |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2343 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2344 | !nand_instr_is_data(&subop->instrs[instr_idx]))) |
| 2345 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2346 | |
| 2347 | start_off = nand_subop_get_data_start_off(subop, instr_idx); |
| 2348 | |
| 2349 | if (instr_idx == subop->ninstrs - 1 && |
| 2350 | subop->last_instr_end_off) |
| 2351 | end_off = subop->last_instr_end_off; |
| 2352 | else |
| 2353 | end_off = subop->instrs[instr_idx].ctx.data.len; |
| 2354 | |
| 2355 | return end_off - start_off; |
| 2356 | } |
| 2357 | EXPORT_SYMBOL_GPL(nand_subop_get_data_len); |
| 2358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | /** |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2360 | * nand_reset - Reset and initialize a NAND device |
| 2361 | * @chip: The NAND chip |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2362 | * @chipnr: Internal die id |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2363 | * |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2364 | * Save the timings data structure, then apply SDR timings mode 0 (see |
| 2365 | * nand_reset_data_interface for details), do the reset operation, and |
| 2366 | * apply back the previous timings. |
| 2367 | * |
| 2368 | * Returns 0 on success, a negative error code otherwise. |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2369 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2370 | int nand_reset(struct nand_chip *chip, int chipnr) |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2371 | { |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2372 | struct nand_data_interface saved_data_intf = chip->data_interface; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2373 | int ret; |
| 2374 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 2375 | ret = nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2376 | if (ret) |
| 2377 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2378 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2379 | /* |
| 2380 | * The CS line has to be released before we can apply the new NAND |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 2381 | * interface settings, hence this weird nand_select_target() |
| 2382 | * nand_deselect_target() dance. |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2383 | */ |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 2384 | nand_select_target(chip, chipnr); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2385 | ret = nand_reset_op(chip); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 2386 | nand_deselect_target(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2387 | if (ret) |
| 2388 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2389 | |
Miquel Raynal | 107b7d6 | 2018-03-19 14:47:25 +0100 | [diff] [blame] | 2390 | /* |
| 2391 | * A nand_reset_data_interface() put both the NAND chip and the NAND |
| 2392 | * controller in timings mode 0. If the default mode for this chip is |
| 2393 | * also 0, no need to proceed to the change again. Plus, at probe time, |
| 2394 | * nand_setup_data_interface() uses ->set/get_features() which would |
| 2395 | * fail anyway as the parameter page is not available yet. |
| 2396 | */ |
| 2397 | if (!chip->onfi_timing_mode_default) |
| 2398 | return 0; |
| 2399 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2400 | chip->data_interface = saved_data_intf; |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 2401 | ret = nand_setup_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2402 | if (ret) |
| 2403 | return ret; |
| 2404 | |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2405 | return 0; |
| 2406 | } |
Boris Brezillon | b9bb984 | 2017-10-05 18:53:19 +0200 | [diff] [blame] | 2407 | EXPORT_SYMBOL_GPL(nand_reset); |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2408 | |
| 2409 | /** |
Boris Brezillon | 4524036 | 2018-09-07 00:38:40 +0200 | [diff] [blame] | 2410 | * nand_get_features - wrapper to perform a GET_FEATURE |
| 2411 | * @chip: NAND chip info structure |
| 2412 | * @addr: feature address |
| 2413 | * @subfeature_param: the subfeature parameters, a four bytes array |
| 2414 | * |
| 2415 | * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the |
| 2416 | * operation cannot be handled. |
| 2417 | */ |
| 2418 | int nand_get_features(struct nand_chip *chip, int addr, |
| 2419 | u8 *subfeature_param) |
| 2420 | { |
| 2421 | if (!nand_supports_get_features(chip, addr)) |
| 2422 | return -ENOTSUPP; |
| 2423 | |
| 2424 | if (chip->legacy.get_features) |
| 2425 | return chip->legacy.get_features(chip, addr, subfeature_param); |
| 2426 | |
| 2427 | return nand_get_features_op(chip, addr, subfeature_param); |
| 2428 | } |
Boris Brezillon | 4524036 | 2018-09-07 00:38:40 +0200 | [diff] [blame] | 2429 | |
| 2430 | /** |
| 2431 | * nand_set_features - wrapper to perform a SET_FEATURE |
| 2432 | * @chip: NAND chip info structure |
| 2433 | * @addr: feature address |
| 2434 | * @subfeature_param: the subfeature parameters, a four bytes array |
| 2435 | * |
| 2436 | * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the |
| 2437 | * operation cannot be handled. |
| 2438 | */ |
| 2439 | int nand_set_features(struct nand_chip *chip, int addr, |
| 2440 | u8 *subfeature_param) |
| 2441 | { |
| 2442 | if (!nand_supports_set_features(chip, addr)) |
| 2443 | return -ENOTSUPP; |
| 2444 | |
| 2445 | if (chip->legacy.set_features) |
| 2446 | return chip->legacy.set_features(chip, addr, subfeature_param); |
| 2447 | |
| 2448 | return nand_set_features_op(chip, addr, subfeature_param); |
| 2449 | } |
Boris Brezillon | 4524036 | 2018-09-07 00:38:40 +0200 | [diff] [blame] | 2450 | |
| 2451 | /** |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 2452 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 2453 | * @buf: buffer to test |
| 2454 | * @len: buffer length |
| 2455 | * @bitflips_threshold: maximum number of bitflips |
| 2456 | * |
| 2457 | * Check if a buffer contains only 0xff, which means the underlying region |
| 2458 | * has been erased and is ready to be programmed. |
| 2459 | * The bitflips_threshold specify the maximum number of bitflips before |
| 2460 | * considering the region is not erased. |
| 2461 | * Note: The logic of this function has been extracted from the memweight |
| 2462 | * implementation, except that nand_check_erased_buf function exit before |
| 2463 | * testing the whole buffer if the number of bitflips exceed the |
| 2464 | * bitflips_threshold value. |
| 2465 | * |
| 2466 | * Returns a positive number of bitflips less than or equal to |
| 2467 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 2468 | * threshold. |
| 2469 | */ |
| 2470 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 2471 | { |
| 2472 | const unsigned char *bitmap = buf; |
| 2473 | int bitflips = 0; |
| 2474 | int weight; |
| 2475 | |
| 2476 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 2477 | len--, bitmap++) { |
| 2478 | weight = hweight8(*bitmap); |
| 2479 | bitflips += BITS_PER_BYTE - weight; |
| 2480 | if (unlikely(bitflips > bitflips_threshold)) |
| 2481 | return -EBADMSG; |
| 2482 | } |
| 2483 | |
| 2484 | for (; len >= sizeof(long); |
| 2485 | len -= sizeof(long), bitmap += sizeof(long)) { |
Pavel Machek | 086567f | 2017-04-21 12:51:07 +0200 | [diff] [blame] | 2486 | unsigned long d = *((unsigned long *)bitmap); |
| 2487 | if (d == ~0UL) |
| 2488 | continue; |
| 2489 | weight = hweight_long(d); |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 2490 | bitflips += BITS_PER_LONG - weight; |
| 2491 | if (unlikely(bitflips > bitflips_threshold)) |
| 2492 | return -EBADMSG; |
| 2493 | } |
| 2494 | |
| 2495 | for (; len > 0; len--, bitmap++) { |
| 2496 | weight = hweight8(*bitmap); |
| 2497 | bitflips += BITS_PER_BYTE - weight; |
| 2498 | if (unlikely(bitflips > bitflips_threshold)) |
| 2499 | return -EBADMSG; |
| 2500 | } |
| 2501 | |
| 2502 | return bitflips; |
| 2503 | } |
| 2504 | |
| 2505 | /** |
| 2506 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 2507 | * 0xff data |
| 2508 | * @data: data buffer to test |
| 2509 | * @datalen: data length |
| 2510 | * @ecc: ECC buffer |
| 2511 | * @ecclen: ECC length |
| 2512 | * @extraoob: extra OOB buffer |
| 2513 | * @extraooblen: extra OOB length |
| 2514 | * @bitflips_threshold: maximum number of bitflips |
| 2515 | * |
| 2516 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 2517 | * 0xff pattern, which means the underlying region has been erased and is |
| 2518 | * ready to be programmed. |
| 2519 | * The bitflips_threshold specify the maximum number of bitflips before |
| 2520 | * considering the region as not erased. |
| 2521 | * |
| 2522 | * Note: |
| 2523 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 2524 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 2525 | * report the number of errors per chunk, and the NAND core infrastructure |
| 2526 | * expect you to return the maximum number of bitflips for the whole page. |
| 2527 | * This is why you should always use this function on a single chunk and |
| 2528 | * not on the whole page. After checking each chunk you should update your |
| 2529 | * max_bitflips value accordingly. |
| 2530 | * 2/ When checking for bitflips in erased pages you should not only check |
| 2531 | * the payload data but also their associated ECC data, because a user might |
| 2532 | * have programmed almost all bits to 1 but a few. In this case, we |
| 2533 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 2534 | * this case. |
| 2535 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 2536 | * data are protected by the ECC engine. |
| 2537 | * It could also be used if you support subpages and want to attach some |
| 2538 | * extra OOB data to an ECC chunk. |
| 2539 | * |
| 2540 | * Returns a positive number of bitflips less than or equal to |
| 2541 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 2542 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 2543 | */ |
| 2544 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 2545 | void *ecc, int ecclen, |
| 2546 | void *extraoob, int extraooblen, |
| 2547 | int bitflips_threshold) |
| 2548 | { |
| 2549 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 2550 | |
| 2551 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 2552 | bitflips_threshold); |
| 2553 | if (data_bitflips < 0) |
| 2554 | return data_bitflips; |
| 2555 | |
| 2556 | bitflips_threshold -= data_bitflips; |
| 2557 | |
| 2558 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 2559 | if (ecc_bitflips < 0) |
| 2560 | return ecc_bitflips; |
| 2561 | |
| 2562 | bitflips_threshold -= ecc_bitflips; |
| 2563 | |
| 2564 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 2565 | bitflips_threshold); |
| 2566 | if (extraoob_bitflips < 0) |
| 2567 | return extraoob_bitflips; |
| 2568 | |
| 2569 | if (data_bitflips) |
| 2570 | memset(data, 0xff, datalen); |
| 2571 | |
| 2572 | if (ecc_bitflips) |
| 2573 | memset(ecc, 0xff, ecclen); |
| 2574 | |
| 2575 | if (extraoob_bitflips) |
| 2576 | memset(extraoob, 0xff, extraooblen); |
| 2577 | |
| 2578 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 2579 | } |
| 2580 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
| 2581 | |
| 2582 | /** |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2583 | * nand_read_page_raw_notsupp - dummy read raw page function |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2584 | * @chip: nand chip info structure |
| 2585 | * @buf: buffer to store read data |
| 2586 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2587 | * @page: page number to read |
| 2588 | * |
| 2589 | * Returns -ENOTSUPP unconditionally. |
| 2590 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2591 | int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf, |
| 2592 | int oob_required, int page) |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2593 | { |
| 2594 | return -ENOTSUPP; |
| 2595 | } |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2596 | |
| 2597 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2598 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2599 | * @chip: nand chip info structure |
| 2600 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2601 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2602 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2603 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2604 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2605 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2606 | int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required, |
| 2607 | int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2608 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2609 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2610 | int ret; |
| 2611 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2612 | ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2613 | if (ret) |
| 2614 | return ret; |
| 2615 | |
| 2616 | if (oob_required) { |
| 2617 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 2618 | false); |
| 2619 | if (ret) |
| 2620 | return ret; |
| 2621 | } |
| 2622 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2623 | return 0; |
| 2624 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 2625 | EXPORT_SYMBOL(nand_read_page_raw); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2626 | |
| 2627 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2628 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2629 | * @chip: nand chip info structure |
| 2630 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2631 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2632 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2633 | * |
| 2634 | * We need a special oob layout and handling even when OOB isn't used. |
| 2635 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2636 | static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2637 | int oob_required, int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2638 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2639 | struct mtd_info *mtd = nand_to_mtd(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2640 | int eccsize = chip->ecc.size; |
| 2641 | int eccbytes = chip->ecc.bytes; |
| 2642 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2643 | int steps, size, ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2644 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2645 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2646 | if (ret) |
| 2647 | return ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2648 | |
| 2649 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2650 | ret = nand_read_data_op(chip, buf, eccsize, false); |
| 2651 | if (ret) |
| 2652 | return ret; |
| 2653 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2654 | buf += eccsize; |
| 2655 | |
| 2656 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2657 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 2658 | false); |
| 2659 | if (ret) |
| 2660 | return ret; |
| 2661 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2662 | oob += chip->ecc.prepad; |
| 2663 | } |
| 2664 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2665 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 2666 | if (ret) |
| 2667 | return ret; |
| 2668 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2669 | oob += eccbytes; |
| 2670 | |
| 2671 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2672 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 2673 | false); |
| 2674 | if (ret) |
| 2675 | return ret; |
| 2676 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2677 | oob += chip->ecc.postpad; |
| 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2682 | if (size) { |
| 2683 | ret = nand_read_data_op(chip, oob, size, false); |
| 2684 | if (ret) |
| 2685 | return ret; |
| 2686 | } |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2687 | |
| 2688 | return 0; |
| 2689 | } |
| 2690 | |
| 2691 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2692 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2693 | * @chip: nand chip info structure |
| 2694 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2695 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2696 | * @page: page number to read |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 2697 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2698 | static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf, |
| 2699 | int oob_required, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2701 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2702 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2703 | int eccbytes = chip->ecc.bytes; |
| 2704 | int eccsteps = chip->ecc.steps; |
| 2705 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2706 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
| 2707 | uint8_t *ecc_code = chip->ecc.code_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2708 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2709 | |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2710 | chip->ecc.read_page_raw(chip, buf, 1, page); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2711 | |
| 2712 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 2713 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2714 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2715 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 2716 | chip->ecc.total); |
| 2717 | if (ret) |
| 2718 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2719 | |
| 2720 | eccsteps = chip->ecc.steps; |
| 2721 | p = buf; |
| 2722 | |
| 2723 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2724 | int stat; |
| 2725 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2726 | stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]); |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2727 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2728 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2729 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2730 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2731 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2732 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2733 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2734 | return max_bitflips; |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 2735 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | /** |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2738 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2739 | * @chip: nand chip info structure |
| 2740 | * @data_offs: offset of requested data within the page |
| 2741 | * @readlen: data length |
| 2742 | * @bufpoi: buffer to store read data |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 2743 | * @page: page number to read |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2744 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2745 | static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs, |
| 2746 | uint32_t readlen, uint8_t *bufpoi, int page) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2747 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2748 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2749 | int start_step, end_step, num_steps, ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2750 | uint8_t *p; |
| 2751 | int data_col_addr, i, gaps = 0; |
| 2752 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 2753 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2754 | int index, section = 0; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2755 | unsigned int max_bitflips = 0; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2756 | struct mtd_oob_region oobregion = { }; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2757 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2758 | /* Column address within the page aligned to ECC size (256bytes) */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2759 | start_step = data_offs / chip->ecc.size; |
| 2760 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 2761 | num_steps = end_step - start_step + 1; |
Ron | 4a4163ca | 2014-03-16 04:01:07 +1030 | [diff] [blame] | 2762 | index = start_step * chip->ecc.bytes; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2763 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2764 | /* Data size aligned to ECC ecc.size */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2765 | datafrag_len = num_steps * chip->ecc.size; |
| 2766 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 2767 | |
| 2768 | data_col_addr = start_step * chip->ecc.size; |
| 2769 | /* If we read not a page aligned data */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2770 | p = bufpoi + data_col_addr; |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2771 | 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] | 2772 | if (ret) |
| 2773 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2774 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2775 | /* Calculate ECC */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2776 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 2777 | chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2778 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2779 | /* |
| 2780 | * The performance is faster if we position offsets according to |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2781 | * 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] | 2782 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2783 | ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion); |
| 2784 | if (ret) |
| 2785 | return ret; |
| 2786 | |
| 2787 | if (oobregion.length < eccfrag_len) |
| 2788 | gaps = 1; |
| 2789 | |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2790 | if (gaps) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2791 | ret = nand_change_read_column_op(chip, mtd->writesize, |
| 2792 | chip->oob_poi, mtd->oobsize, |
| 2793 | false); |
| 2794 | if (ret) |
| 2795 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2796 | } else { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2797 | /* |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2798 | * Send the command to read the particular ECC bytes take care |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2799 | * about buswidth alignment in read_buf. |
| 2800 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2801 | aligned_pos = oobregion.offset & ~(busw - 1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2802 | aligned_len = eccfrag_len; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2803 | if (oobregion.offset & (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2804 | aligned_len++; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2805 | if ((oobregion.offset + (num_steps * chip->ecc.bytes)) & |
| 2806 | (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2807 | aligned_len++; |
| 2808 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2809 | ret = nand_change_read_column_op(chip, |
| 2810 | mtd->writesize + aligned_pos, |
| 2811 | &chip->oob_poi[aligned_pos], |
| 2812 | aligned_len, false); |
| 2813 | if (ret) |
| 2814 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2815 | } |
| 2816 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2817 | ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf, |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2818 | chip->oob_poi, index, eccfrag_len); |
| 2819 | if (ret) |
| 2820 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2821 | |
| 2822 | p = bufpoi + data_col_addr; |
| 2823 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 2824 | int stat; |
| 2825 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2826 | stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i], |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2827 | &chip->ecc.calc_buf[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2828 | if (stat == -EBADMSG && |
| 2829 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2830 | /* check for empty pages with bitflips */ |
| 2831 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2832 | &chip->ecc.code_buf[i], |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2833 | chip->ecc.bytes, |
| 2834 | NULL, 0, |
| 2835 | chip->ecc.strength); |
| 2836 | } |
| 2837 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2838 | if (stat < 0) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2839 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2840 | } else { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2841 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2842 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2843 | } |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2844 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2845 | return max_bitflips; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2849 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2850 | * @chip: nand chip info structure |
| 2851 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2852 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2853 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2854 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2855 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2856 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2857 | static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf, |
| 2858 | int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2859 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2860 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2861 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2862 | int eccbytes = chip->ecc.bytes; |
| 2863 | int eccsteps = chip->ecc.steps; |
| 2864 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2865 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
| 2866 | uint8_t *ecc_code = chip->ecc.code_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2867 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2868 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2869 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2870 | if (ret) |
| 2871 | return ret; |
| 2872 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2873 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 2874 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2875 | |
| 2876 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2877 | if (ret) |
| 2878 | return ret; |
| 2879 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 2880 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2881 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2882 | |
| 2883 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 2884 | if (ret) |
| 2885 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2886 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2887 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 2888 | chip->ecc.total); |
| 2889 | if (ret) |
| 2890 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2891 | |
| 2892 | eccsteps = chip->ecc.steps; |
| 2893 | p = buf; |
| 2894 | |
| 2895 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2896 | int stat; |
| 2897 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2898 | stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2899 | if (stat == -EBADMSG && |
| 2900 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2901 | /* check for empty pages with bitflips */ |
| 2902 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2903 | &ecc_code[i], eccbytes, |
| 2904 | NULL, 0, |
| 2905 | chip->ecc.strength); |
| 2906 | } |
| 2907 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2908 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2909 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2910 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2911 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2912 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2913 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2914 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2915 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2919 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2920 | * @chip: nand chip info structure |
| 2921 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2922 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2923 | * @page: page number to read |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2924 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2925 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 2926 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 2927 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 2928 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 2929 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2930 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2931 | static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf, |
| 2932 | int oob_required, int page) |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2933 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2934 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2935 | int i, eccsize = chip->ecc.size, ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2936 | int eccbytes = chip->ecc.bytes; |
| 2937 | int eccsteps = chip->ecc.steps; |
| 2938 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2939 | uint8_t *ecc_code = chip->ecc.code_buf; |
| 2940 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2941 | unsigned int max_bitflips = 0; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2942 | |
| 2943 | /* Read the OOB area first */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2944 | ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
| 2945 | if (ret) |
| 2946 | return ret; |
| 2947 | |
| 2948 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2949 | if (ret) |
| 2950 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2951 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2952 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 2953 | chip->ecc.total); |
| 2954 | if (ret) |
| 2955 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2956 | |
| 2957 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2958 | int stat; |
| 2959 | |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 2960 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2961 | |
| 2962 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2963 | if (ret) |
| 2964 | return ret; |
| 2965 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 2966 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2967 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2968 | stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2969 | if (stat == -EBADMSG && |
| 2970 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2971 | /* check for empty pages with bitflips */ |
| 2972 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2973 | &ecc_code[i], eccbytes, |
| 2974 | NULL, 0, |
| 2975 | chip->ecc.strength); |
| 2976 | } |
| 2977 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2978 | if (stat < 0) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2979 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2980 | } else { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2981 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2982 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2983 | } |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2984 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2985 | return max_bitflips; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2989 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2990 | * @chip: nand chip info structure |
| 2991 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2992 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2993 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2994 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2995 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 2996 | * need a special oob layout and handling. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2997 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2998 | static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf, |
| 2999 | int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3000 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3001 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3002 | int ret, i, eccsize = chip->ecc.size; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3003 | int eccbytes = chip->ecc.bytes; |
| 3004 | int eccsteps = chip->ecc.steps; |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 3005 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3006 | uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3007 | uint8_t *oob = chip->oob_poi; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3008 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3009 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3010 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 3011 | if (ret) |
| 3012 | return ret; |
| 3013 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3014 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 3015 | int stat; |
| 3016 | |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3017 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3018 | |
| 3019 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 3020 | if (ret) |
| 3021 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3022 | |
| 3023 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3024 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 3025 | false); |
| 3026 | if (ret) |
| 3027 | return ret; |
| 3028 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3029 | oob += chip->ecc.prepad; |
| 3030 | } |
| 3031 | |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3032 | chip->ecc.hwctl(chip, NAND_ECC_READSYN); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3033 | |
| 3034 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 3035 | if (ret) |
| 3036 | return ret; |
| 3037 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 3038 | stat = chip->ecc.correct(chip, p, oob, NULL); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3039 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3040 | oob += eccbytes; |
| 3041 | |
| 3042 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3043 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 3044 | false); |
| 3045 | if (ret) |
| 3046 | return ret; |
| 3047 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3048 | oob += chip->ecc.postpad; |
| 3049 | } |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 3050 | |
| 3051 | if (stat == -EBADMSG && |
| 3052 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 3053 | /* check for empty pages with bitflips */ |
| 3054 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 3055 | oob - eccpadbytes, |
| 3056 | eccpadbytes, |
| 3057 | NULL, 0, |
| 3058 | chip->ecc.strength); |
| 3059 | } |
| 3060 | |
| 3061 | if (stat < 0) { |
| 3062 | mtd->ecc_stats.failed++; |
| 3063 | } else { |
| 3064 | mtd->ecc_stats.corrected += stat; |
| 3065 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 3066 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3067 | } |
| 3068 | |
| 3069 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 3070 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3071 | if (i) { |
| 3072 | ret = nand_read_data_op(chip, oob, i, false); |
| 3073 | if (ret) |
| 3074 | return ret; |
| 3075 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3076 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3077 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3078 | } |
| 3079 | |
| 3080 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3081 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3082 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3083 | * @oob: oob destination address |
| 3084 | * @ops: oob ops structure |
| 3085 | * @len: size of oob to transfer |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3086 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3087 | static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob, |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3088 | struct mtd_oob_ops *ops, size_t len) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3089 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3090 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3091 | int ret; |
| 3092 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3093 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3094 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3095 | case MTD_OPS_PLACE_OOB: |
| 3096 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3097 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 3098 | return oob + len; |
| 3099 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3100 | case MTD_OPS_AUTO_OOB: |
| 3101 | ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi, |
| 3102 | ops->ooboffs, len); |
| 3103 | BUG_ON(ret); |
| 3104 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3105 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3106 | default: |
| 3107 | BUG(); |
| 3108 | } |
| 3109 | return NULL; |
| 3110 | } |
| 3111 | |
| 3112 | /** |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3113 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3114 | * @chip: NAND chip object |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3115 | * @retry_mode: the retry mode to use |
| 3116 | * |
| 3117 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 3118 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 3119 | * a new threshold, the host should retry reading the page. |
| 3120 | */ |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3121 | static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode) |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3122 | { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3123 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 3124 | |
| 3125 | if (retry_mode >= chip->read_retries) |
| 3126 | return -EINVAL; |
| 3127 | |
| 3128 | if (!chip->setup_read_retry) |
| 3129 | return -EOPNOTSUPP; |
| 3130 | |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3131 | return chip->setup_read_retry(chip, retry_mode); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3132 | } |
| 3133 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3134 | static void nand_wait_readrdy(struct nand_chip *chip) |
| 3135 | { |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 3136 | const struct nand_sdr_timings *sdr; |
| 3137 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3138 | if (!(chip->options & NAND_NEED_READRDY)) |
| 3139 | return; |
| 3140 | |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 3141 | sdr = nand_get_sdr_timings(&chip->data_interface); |
| 3142 | WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0)); |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3143 | } |
| 3144 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3145 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3146 | * nand_do_read_ops - [INTERN] Read data with ECC |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3147 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3148 | * @from: offset to read from |
| 3149 | * @ops: oob ops structure |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3150 | * |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3151 | * Internal function. Called with chip held. |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3152 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3153 | static int nand_do_read_ops(struct nand_chip *chip, loff_t from, |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3154 | struct mtd_oob_ops *ops) |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3155 | { |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3156 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3157 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3158 | int ret = 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3159 | uint32_t readlen = ops->len; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3160 | uint32_t oobreadlen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3161 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 9aca334 | 2010-02-22 20:39:35 +0200 | [diff] [blame] | 3162 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3163 | uint8_t *bufpoi, *oob, *buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3164 | int use_bufpoi; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3165 | unsigned int max_bitflips = 0; |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3166 | int retry_mode = 0; |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3167 | bool ecc_fail = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3169 | chipnr = (int)(from >> chip->chip_shift); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 3170 | nand_select_target(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3172 | realpage = (int)(from >> chip->page_shift); |
| 3173 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3175 | col = (int)(from & (mtd->writesize - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3177 | buf = ops->datbuf; |
| 3178 | oob = ops->oobbuf; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3179 | oob_required = oob ? 1 : 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3180 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3181 | while (1) { |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3182 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
| 3183 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3184 | bytes = min(mtd->writesize - col, readlen); |
| 3185 | aligned = (bytes == mtd->writesize); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3186 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3187 | if (!aligned) |
| 3188 | use_bufpoi = 1; |
| 3189 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 3190 | use_bufpoi = !virt_addr_valid(buf) || |
| 3191 | !IS_ALIGNED((unsigned long)buf, |
| 3192 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3193 | else |
| 3194 | use_bufpoi = 0; |
| 3195 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3196 | /* Is the current page in the buffer? */ |
Boris Brezillon | d974541 | 2018-10-28 16:12:45 +0100 | [diff] [blame] | 3197 | if (realpage != chip->pagecache.page || oob) { |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3198 | bufpoi = use_bufpoi ? chip->data_buf : buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3199 | |
| 3200 | if (use_bufpoi && aligned) |
| 3201 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 3202 | __func__, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3204 | read_retry: |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3205 | /* |
| 3206 | * Now read the page into the buffer. Absent an error, |
| 3207 | * the read methods return max bitflips per ecc step. |
| 3208 | */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3209 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3210 | ret = chip->ecc.read_page_raw(chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3211 | oob_required, |
| 3212 | page); |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 3213 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
| 3214 | !oob) |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3215 | ret = chip->ecc.read_subpage(chip, col, bytes, |
| 3216 | bufpoi, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3217 | else |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3218 | ret = chip->ecc.read_page(chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3219 | oob_required, page); |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3220 | if (ret < 0) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3221 | if (use_bufpoi) |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3222 | /* Invalidate page cache */ |
Boris Brezillon | d974541 | 2018-10-28 16:12:45 +0100 | [diff] [blame] | 3223 | chip->pagecache.page = -1; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3224 | break; |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3225 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3226 | |
| 3227 | /* Transfer not aligned data */ |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3228 | if (use_bufpoi) { |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 3229 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3230 | !(mtd->ecc_stats.failed - ecc_failures) && |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3231 | (ops->mode != MTD_OPS_RAW)) { |
Boris Brezillon | d974541 | 2018-10-28 16:12:45 +0100 | [diff] [blame] | 3232 | chip->pagecache.page = realpage; |
| 3233 | chip->pagecache.bitflips = ret; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3234 | } else { |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3235 | /* Invalidate page cache */ |
Boris Brezillon | d974541 | 2018-10-28 16:12:45 +0100 | [diff] [blame] | 3236 | chip->pagecache.page = -1; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3237 | } |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3238 | memcpy(buf, chip->data_buf + col, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3239 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3240 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3241 | if (unlikely(oob)) { |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 3242 | int toread = min(oobreadlen, max_oobsize); |
| 3243 | |
| 3244 | if (toread) { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3245 | oob = nand_transfer_oob(chip, oob, ops, |
| 3246 | toread); |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 3247 | oobreadlen -= toread; |
| 3248 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3249 | } |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 3250 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3251 | nand_wait_readrdy(chip); |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3252 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3253 | if (mtd->ecc_stats.failed - ecc_failures) { |
Brian Norris | 28fa65e | 2014-02-12 16:08:28 -0800 | [diff] [blame] | 3254 | if (retry_mode + 1 < chip->read_retries) { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3255 | retry_mode++; |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3256 | ret = nand_setup_read_retry(chip, |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3257 | retry_mode); |
| 3258 | if (ret < 0) |
| 3259 | break; |
| 3260 | |
| 3261 | /* Reset failures; retry */ |
| 3262 | mtd->ecc_stats.failed = ecc_failures; |
| 3263 | goto read_retry; |
| 3264 | } else { |
| 3265 | /* No more retry modes; real failure */ |
| 3266 | ecc_fail = true; |
| 3267 | } |
| 3268 | } |
| 3269 | |
| 3270 | buf += bytes; |
Masahiro Yamada | 0760468 | 2017-03-30 15:45:47 +0900 | [diff] [blame] | 3271 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3272 | } else { |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3273 | memcpy(buf, chip->data_buf + col, bytes); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3274 | buf += bytes; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3275 | max_bitflips = max_t(unsigned int, max_bitflips, |
Boris Brezillon | d974541 | 2018-10-28 16:12:45 +0100 | [diff] [blame] | 3276 | chip->pagecache.bitflips); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3277 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3279 | readlen -= bytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3280 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3281 | /* Reset to retry mode 0 */ |
| 3282 | if (retry_mode) { |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3283 | ret = nand_setup_read_retry(chip, 0); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3284 | if (ret < 0) |
| 3285 | break; |
| 3286 | retry_mode = 0; |
| 3287 | } |
| 3288 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3289 | if (!readlen) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3290 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3291 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3292 | /* For subsequent reads align to page boundary */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3293 | col = 0; |
| 3294 | /* Increment page address */ |
| 3295 | realpage++; |
| 3296 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3297 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | /* Check, if we cross a chip boundary */ |
| 3299 | if (!page) { |
| 3300 | chipnr++; |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 3301 | nand_deselect_target(chip); |
| 3302 | nand_select_target(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3304 | } |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 3305 | nand_deselect_target(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3306 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3307 | ops->retlen = ops->len - (size_t) readlen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3308 | if (oob) |
| 3309 | ops->oobretlen = ops->ooblen - oobreadlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3310 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3311 | if (ret < 0) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3312 | return ret; |
| 3313 | |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3314 | if (ecc_fail) |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 3315 | return -EBADMSG; |
| 3316 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3317 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3321 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3322 | * @chip: nand chip info structure |
| 3323 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3324 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3325 | int nand_read_oob_std(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3326 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3327 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 3328 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3329 | 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] | 3330 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3331 | EXPORT_SYMBOL(nand_read_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3332 | |
| 3333 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3334 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3335 | * with syndromes |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3336 | * @chip: nand chip info structure |
| 3337 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3338 | */ |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 3339 | static int nand_read_oob_syndrome(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3340 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3341 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3342 | int length = mtd->oobsize; |
| 3343 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 3344 | int eccsize = chip->ecc.size; |
Baruch Siach | 2ea69d2 | 2015-01-22 15:23:05 +0200 | [diff] [blame] | 3345 | uint8_t *bufpoi = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3346 | int i, toread, sndrnd = 0, pos, ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3347 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3348 | ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0); |
| 3349 | if (ret) |
| 3350 | return ret; |
| 3351 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3352 | for (i = 0; i < chip->ecc.steps; i++) { |
| 3353 | if (sndrnd) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3354 | int ret; |
| 3355 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3356 | pos = eccsize + i * (eccsize + chunk); |
| 3357 | if (mtd->writesize > 512) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3358 | ret = nand_change_read_column_op(chip, pos, |
| 3359 | NULL, 0, |
| 3360 | false); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3361 | else |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3362 | ret = nand_read_page_op(chip, page, pos, NULL, |
| 3363 | 0); |
| 3364 | |
| 3365 | if (ret) |
| 3366 | return ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3367 | } else |
| 3368 | sndrnd = 1; |
| 3369 | toread = min_t(int, length, chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3370 | |
| 3371 | ret = nand_read_data_op(chip, bufpoi, toread, false); |
| 3372 | if (ret) |
| 3373 | return ret; |
| 3374 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3375 | bufpoi += toread; |
| 3376 | length -= toread; |
| 3377 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3378 | if (length > 0) { |
| 3379 | ret = nand_read_data_op(chip, bufpoi, length, false); |
| 3380 | if (ret) |
| 3381 | return ret; |
| 3382 | } |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3383 | |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 3384 | return 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3385 | } |
| 3386 | |
| 3387 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3388 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3389 | * @chip: nand chip info structure |
| 3390 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3391 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3392 | int nand_write_oob_std(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3393 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3394 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 3395 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3396 | return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi, |
| 3397 | mtd->oobsize); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3398 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3399 | EXPORT_SYMBOL(nand_write_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3400 | |
| 3401 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3402 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3403 | * with syndrome - only for large page flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3404 | * @chip: nand chip info structure |
| 3405 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3406 | */ |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 3407 | static int nand_write_oob_syndrome(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3408 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3409 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3410 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 3411 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3412 | int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3413 | const uint8_t *bufpoi = chip->oob_poi; |
| 3414 | |
| 3415 | /* |
| 3416 | * data-ecc-data-ecc ... ecc-oob |
| 3417 | * or |
| 3418 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
| 3419 | */ |
| 3420 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 3421 | pos = steps * (eccsize + chunk); |
| 3422 | steps = 0; |
| 3423 | } else |
Vitaly Wool | 8b0036e | 2006-07-11 09:11:25 +0200 | [diff] [blame] | 3424 | pos = eccsize; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3425 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3426 | ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0); |
| 3427 | if (ret) |
| 3428 | return ret; |
| 3429 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3430 | for (i = 0; i < steps; i++) { |
| 3431 | if (sndcmd) { |
| 3432 | if (mtd->writesize <= 512) { |
| 3433 | uint32_t fill = 0xFFFFFFFF; |
| 3434 | |
| 3435 | len = eccsize; |
| 3436 | while (len > 0) { |
| 3437 | int num = min_t(int, len, 4); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3438 | |
| 3439 | ret = nand_write_data_op(chip, &fill, |
| 3440 | num, false); |
| 3441 | if (ret) |
| 3442 | return ret; |
| 3443 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3444 | len -= num; |
| 3445 | } |
| 3446 | } else { |
| 3447 | pos = eccsize + i * (eccsize + chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3448 | ret = nand_change_write_column_op(chip, pos, |
| 3449 | NULL, 0, |
| 3450 | false); |
| 3451 | if (ret) |
| 3452 | return ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3453 | } |
| 3454 | } else |
| 3455 | sndcmd = 1; |
| 3456 | len = min_t(int, length, chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3457 | |
| 3458 | ret = nand_write_data_op(chip, bufpoi, len, false); |
| 3459 | if (ret) |
| 3460 | return ret; |
| 3461 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3462 | bufpoi += len; |
| 3463 | length -= len; |
| 3464 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3465 | if (length > 0) { |
| 3466 | ret = nand_write_data_op(chip, bufpoi, length, false); |
| 3467 | if (ret) |
| 3468 | return ret; |
| 3469 | } |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3470 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3471 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3472 | } |
| 3473 | |
| 3474 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3475 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3476 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3477 | * @from: offset to read from |
| 3478 | * @ops: oob operations description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3480 | * NAND read out-of-band data from the spare area. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3482 | static int nand_do_read_oob(struct nand_chip *chip, loff_t from, |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3483 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3485 | struct mtd_info *mtd = nand_to_mtd(chip); |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3486 | unsigned int max_bitflips = 0; |
Brian Norris | c00a099 | 2012-05-01 17:12:54 -0700 | [diff] [blame] | 3487 | int page, realpage, chipnr; |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3488 | struct mtd_ecc_stats stats; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3489 | int readlen = ops->ooblen; |
| 3490 | int len; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3491 | uint8_t *buf = ops->oobbuf; |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3492 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3494 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 3495 | __func__, (unsigned long long)from, readlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3497 | stats = mtd->ecc_stats; |
| 3498 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3499 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3500 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3501 | chipnr = (int)(from >> chip->chip_shift); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 3502 | nand_select_target(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3504 | /* Shift to get page */ |
| 3505 | realpage = (int)(from >> chip->page_shift); |
| 3506 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3507 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3508 | while (1) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3509 | if (ops->mode == MTD_OPS_RAW) |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3510 | ret = chip->ecc.read_oob_raw(chip, page); |
Brian Norris | c46f648 | 2011-08-30 18:45:38 -0700 | [diff] [blame] | 3511 | else |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3512 | ret = chip->ecc.read_oob(chip, page); |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3513 | |
| 3514 | if (ret < 0) |
| 3515 | break; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3516 | |
| 3517 | len = min(len, readlen); |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3518 | buf = nand_transfer_oob(chip, buf, ops, len); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3519 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3520 | nand_wait_readrdy(chip); |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 3521 | |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3522 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
| 3523 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3524 | readlen -= len; |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 3525 | if (!readlen) |
| 3526 | break; |
| 3527 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3528 | /* Increment page address */ |
| 3529 | realpage++; |
| 3530 | |
| 3531 | page = realpage & chip->pagemask; |
| 3532 | /* Check, if we cross a chip boundary */ |
| 3533 | if (!page) { |
| 3534 | chipnr++; |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 3535 | nand_deselect_target(chip); |
| 3536 | nand_select_target(chip, chipnr); |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3537 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3538 | } |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 3539 | nand_deselect_target(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3540 | |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3541 | ops->oobretlen = ops->ooblen - readlen; |
| 3542 | |
| 3543 | if (ret < 0) |
| 3544 | return ret; |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3545 | |
| 3546 | if (mtd->ecc_stats.failed - stats.failed) |
| 3547 | return -EBADMSG; |
| 3548 | |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3549 | return max_bitflips; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3550 | } |
| 3551 | |
| 3552 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3553 | * 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] | 3554 | * @mtd: MTD device structure |
| 3555 | * @from: offset to read from |
| 3556 | * @ops: oob operation description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3557 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3558 | * NAND read data and/or out-of-band data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3560 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 3561 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3562 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3563 | struct nand_chip *chip = mtd_to_nand(mtd); |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 3564 | int ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3565 | |
| 3566 | ops->retlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3567 | |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 3568 | if (ops->mode != MTD_OPS_PLACE_OOB && |
| 3569 | ops->mode != MTD_OPS_AUTO_OOB && |
| 3570 | ops->mode != MTD_OPS_RAW) |
| 3571 | return -ENOTSUPP; |
| 3572 | |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 3573 | ret = nand_get_device(chip); |
| 3574 | if (ret) |
| 3575 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3576 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3577 | if (!ops->datbuf) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3578 | ret = nand_do_read_oob(chip, from, ops); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3579 | else |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3580 | ret = nand_do_read_ops(chip, from, ops); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3581 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3582 | nand_release_device(chip); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3583 | return ret; |
| 3584 | } |
| 3585 | |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 3586 | /** |
| 3587 | * nand_write_page_raw_notsupp - dummy raw page write function |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 3588 | * @chip: nand chip info structure |
| 3589 | * @buf: data buffer |
| 3590 | * @oob_required: must write chip->oob_poi to OOB |
| 3591 | * @page: page number to write |
| 3592 | * |
| 3593 | * Returns -ENOTSUPP unconditionally. |
| 3594 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3595 | int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf, |
| 3596 | int oob_required, int page) |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 3597 | { |
| 3598 | return -ENOTSUPP; |
| 3599 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3600 | |
| 3601 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3602 | * nand_write_page_raw - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3603 | * @chip: nand chip info structure |
| 3604 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3605 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3606 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3607 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3608 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3609 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3610 | int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf, |
| 3611 | int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3612 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3613 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3614 | int ret; |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3615 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3616 | ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3617 | if (ret) |
| 3618 | return ret; |
| 3619 | |
| 3620 | if (oob_required) { |
| 3621 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 3622 | false); |
| 3623 | if (ret) |
| 3624 | return ret; |
| 3625 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3626 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3627 | return nand_prog_page_end_op(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3628 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 3629 | EXPORT_SYMBOL(nand_write_page_raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3631 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3632 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3633 | * @chip: nand chip info structure |
| 3634 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3635 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3636 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3637 | * |
| 3638 | * We need a special oob layout and handling even when ECC isn't checked. |
| 3639 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3640 | static int nand_write_page_raw_syndrome(struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3641 | const uint8_t *buf, int oob_required, |
| 3642 | int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3643 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3644 | struct mtd_info *mtd = nand_to_mtd(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3645 | int eccsize = chip->ecc.size; |
| 3646 | int eccbytes = chip->ecc.bytes; |
| 3647 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3648 | int steps, size, ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3649 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3650 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3651 | if (ret) |
| 3652 | return ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3653 | |
| 3654 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3655 | ret = nand_write_data_op(chip, buf, eccsize, false); |
| 3656 | if (ret) |
| 3657 | return ret; |
| 3658 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3659 | buf += eccsize; |
| 3660 | |
| 3661 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3662 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 3663 | false); |
| 3664 | if (ret) |
| 3665 | return ret; |
| 3666 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3667 | oob += chip->ecc.prepad; |
| 3668 | } |
| 3669 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3670 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 3671 | if (ret) |
| 3672 | return ret; |
| 3673 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3674 | oob += eccbytes; |
| 3675 | |
| 3676 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3677 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 3678 | false); |
| 3679 | if (ret) |
| 3680 | return ret; |
| 3681 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3682 | oob += chip->ecc.postpad; |
| 3683 | } |
| 3684 | } |
| 3685 | |
| 3686 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3687 | if (size) { |
| 3688 | ret = nand_write_data_op(chip, oob, size, false); |
| 3689 | if (ret) |
| 3690 | return ret; |
| 3691 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3692 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3693 | return nand_prog_page_end_op(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3694 | } |
| 3695 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3696 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3697 | * @chip: nand chip info structure |
| 3698 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3699 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3700 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3701 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3702 | static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf, |
| 3703 | int oob_required, int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3704 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3705 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3706 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3707 | int eccbytes = chip->ecc.bytes; |
| 3708 | int eccsteps = chip->ecc.steps; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3709 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3710 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3711 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3712 | /* Software ECC calculation */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3713 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3714 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3715 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3716 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 3717 | chip->ecc.total); |
| 3718 | if (ret) |
| 3719 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3720 | |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3721 | return chip->ecc.write_page_raw(chip, buf, 1, page); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3722 | } |
| 3723 | |
| 3724 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3725 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3726 | * @chip: nand chip info structure |
| 3727 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3728 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3729 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3730 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3731 | static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf, |
| 3732 | int oob_required, int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3733 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3734 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3735 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3736 | int eccbytes = chip->ecc.bytes; |
| 3737 | int eccsteps = chip->ecc.steps; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3738 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3739 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3740 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3741 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3742 | if (ret) |
| 3743 | return ret; |
| 3744 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3745 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3746 | chip->ecc.hwctl(chip, NAND_ECC_WRITE); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3747 | |
| 3748 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 3749 | if (ret) |
| 3750 | return ret; |
| 3751 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3752 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3753 | } |
| 3754 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3755 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 3756 | chip->ecc.total); |
| 3757 | if (ret) |
| 3758 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3759 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3760 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 3761 | if (ret) |
| 3762 | return ret; |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3763 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3764 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3765 | } |
| 3766 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3767 | |
| 3768 | /** |
Brian Norris | 73c8aaf | 2015-02-28 02:04:18 -0800 | [diff] [blame] | 3769 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3770 | * @chip: nand chip info structure |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3771 | * @offset: column address of subpage within the page |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3772 | * @data_len: data length |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3773 | * @buf: data buffer |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3774 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3775 | * @page: page number to write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3776 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3777 | static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset, |
| 3778 | uint32_t data_len, const uint8_t *buf, |
| 3779 | int oob_required, int page) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3780 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3781 | struct mtd_info *mtd = nand_to_mtd(chip); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3782 | uint8_t *oob_buf = chip->oob_poi; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3783 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3784 | int ecc_size = chip->ecc.size; |
| 3785 | int ecc_bytes = chip->ecc.bytes; |
| 3786 | int ecc_steps = chip->ecc.steps; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3787 | uint32_t start_step = offset / ecc_size; |
| 3788 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 3789 | int oob_bytes = mtd->oobsize / ecc_steps; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3790 | int step, ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3791 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3792 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3793 | if (ret) |
| 3794 | return ret; |
| 3795 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3796 | for (step = 0; step < ecc_steps; step++) { |
| 3797 | /* configure controller for WRITE access */ |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3798 | chip->ecc.hwctl(chip, NAND_ECC_WRITE); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3799 | |
| 3800 | /* write data (untouched subpages already masked by 0xFF) */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3801 | ret = nand_write_data_op(chip, buf, ecc_size, false); |
| 3802 | if (ret) |
| 3803 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3804 | |
| 3805 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 3806 | if ((step < start_step) || (step > end_step)) |
| 3807 | memset(ecc_calc, 0xff, ecc_bytes); |
| 3808 | else |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3809 | chip->ecc.calculate(chip, buf, ecc_calc); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3810 | |
| 3811 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 3812 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 3813 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 3814 | memset(oob_buf, 0xff, oob_bytes); |
| 3815 | |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3816 | buf += ecc_size; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3817 | ecc_calc += ecc_bytes; |
| 3818 | oob_buf += oob_bytes; |
| 3819 | } |
| 3820 | |
| 3821 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 3822 | /* this include masked-value(0xFF) for unwritten subpages */ |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3823 | ecc_calc = chip->ecc.calc_buf; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3824 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 3825 | chip->ecc.total); |
| 3826 | if (ret) |
| 3827 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3828 | |
| 3829 | /* write OOB buffer to NAND device */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3830 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 3831 | if (ret) |
| 3832 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3833 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3834 | return nand_prog_page_end_op(chip); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3838 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3839 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3840 | * @chip: nand chip info structure |
| 3841 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3842 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3843 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3844 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3845 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 3846 | * need a special oob layout and handling. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3847 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3848 | static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf, |
| 3849 | int oob_required, int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3850 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3851 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3852 | int i, eccsize = chip->ecc.size; |
| 3853 | int eccbytes = chip->ecc.bytes; |
| 3854 | int eccsteps = chip->ecc.steps; |
| 3855 | const uint8_t *p = buf; |
| 3856 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3857 | int ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3858 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3859 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3860 | if (ret) |
| 3861 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3862 | |
| 3863 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3864 | chip->ecc.hwctl(chip, NAND_ECC_WRITE); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3865 | |
| 3866 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 3867 | if (ret) |
| 3868 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3869 | |
| 3870 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3871 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 3872 | false); |
| 3873 | if (ret) |
| 3874 | return ret; |
| 3875 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3876 | oob += chip->ecc.prepad; |
| 3877 | } |
| 3878 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3879 | chip->ecc.calculate(chip, p, oob); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3880 | |
| 3881 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 3882 | if (ret) |
| 3883 | return ret; |
| 3884 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3885 | oob += eccbytes; |
| 3886 | |
| 3887 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3888 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 3889 | false); |
| 3890 | if (ret) |
| 3891 | return ret; |
| 3892 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3893 | oob += chip->ecc.postpad; |
| 3894 | } |
| 3895 | } |
| 3896 | |
| 3897 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 3898 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3899 | if (i) { |
| 3900 | ret = nand_write_data_op(chip, oob, i, false); |
| 3901 | if (ret) |
| 3902 | return ret; |
| 3903 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3904 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3905 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | /** |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 3909 | * nand_write_page - write one page |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3910 | * @chip: NAND chip descriptor |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3911 | * @offset: address offset within the page |
| 3912 | * @data_len: length of actual data to be written |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3913 | * @buf: the data to write |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3914 | * @oob_required: must write chip->oob_poi to OOB |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3915 | * @page: page number to write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3916 | * @raw: use _raw version of write_page |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3917 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3918 | static int nand_write_page(struct nand_chip *chip, uint32_t offset, |
| 3919 | int data_len, const uint8_t *buf, int oob_required, |
| 3920 | int page, int raw) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3921 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3922 | struct mtd_info *mtd = nand_to_mtd(chip); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3923 | int status, subpage; |
| 3924 | |
| 3925 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 3926 | chip->ecc.write_subpage) |
| 3927 | subpage = offset || (data_len < mtd->writesize); |
| 3928 | else |
| 3929 | subpage = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3930 | |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3931 | if (unlikely(raw)) |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3932 | status = chip->ecc.write_page_raw(chip, buf, oob_required, |
| 3933 | page); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3934 | else if (subpage) |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3935 | status = chip->ecc.write_subpage(chip, offset, data_len, buf, |
| 3936 | oob_required, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3937 | else |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3938 | status = chip->ecc.write_page(chip, buf, oob_required, page); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3939 | |
| 3940 | if (status < 0) |
| 3941 | return status; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3942 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3943 | return 0; |
| 3944 | } |
| 3945 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3946 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3947 | |
| 3948 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3949 | * nand_do_write_ops - [INTERN] NAND write with ECC |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3950 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3951 | * @to: offset to write to |
| 3952 | * @ops: oob operations description structure |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3953 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3954 | * NAND write with ECC. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3955 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3956 | static int nand_do_write_ops(struct nand_chip *chip, loff_t to, |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3957 | struct mtd_oob_ops *ops) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3958 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3959 | struct mtd_info *mtd = nand_to_mtd(chip); |
Corentin Labbe | 73600b6 | 2017-09-02 10:49:38 +0200 | [diff] [blame] | 3960 | int chipnr, realpage, page, column; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3961 | uint32_t writelen = ops->len; |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3962 | |
| 3963 | uint32_t oobwritelen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3964 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3965 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3966 | uint8_t *oob = ops->oobbuf; |
| 3967 | uint8_t *buf = ops->datbuf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3968 | int ret; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3969 | int oob_required = oob ? 1 : 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3970 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3971 | ops->retlen = 0; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3972 | if (!writelen) |
| 3973 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3974 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3975 | /* Reject writes, which are not page aligned */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3976 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 3977 | pr_notice("%s: attempt to write non page aligned data\n", |
| 3978 | __func__); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3979 | return -EINVAL; |
| 3980 | } |
| 3981 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3982 | column = to & (mtd->writesize - 1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3983 | |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 3984 | chipnr = (int)(to >> chip->chip_shift); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 3985 | nand_select_target(chip, chipnr); |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 3986 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3987 | /* Check, if it is write protected */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3988 | if (nand_check_wp(chip)) { |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3989 | ret = -EIO; |
| 3990 | goto err_out; |
| 3991 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3992 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3993 | realpage = (int)(to >> chip->page_shift); |
| 3994 | page = realpage & chip->pagemask; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3995 | |
| 3996 | /* Invalidate the page cache, when we write to the cached page */ |
Boris Brezillon | d974541 | 2018-10-28 16:12:45 +0100 | [diff] [blame] | 3997 | if (to <= ((loff_t)chip->pagecache.page << chip->page_shift) && |
| 3998 | ((loff_t)chip->pagecache.page << chip->page_shift) < (to + ops->len)) |
| 3999 | chip->pagecache.page = -1; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4000 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4001 | /* Don't allow multipage oob writes with offset */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4002 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 4003 | ret = -EINVAL; |
| 4004 | goto err_out; |
| 4005 | } |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4006 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4007 | while (1) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4008 | int bytes = mtd->writesize; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4009 | uint8_t *wbuf = buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 4010 | int use_bufpoi; |
Hector Palacios | 144f4c9 | 2016-07-18 10:39:18 +0200 | [diff] [blame] | 4011 | int part_pagewr = (column || writelen < mtd->writesize); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4012 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 4013 | if (part_pagewr) |
| 4014 | use_bufpoi = 1; |
| 4015 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 4016 | use_bufpoi = !virt_addr_valid(buf) || |
| 4017 | !IS_ALIGNED((unsigned long)buf, |
| 4018 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 4019 | else |
| 4020 | use_bufpoi = 0; |
| 4021 | |
| 4022 | /* Partial page write?, or need to use bounce buffer */ |
| 4023 | if (use_bufpoi) { |
| 4024 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 4025 | __func__, buf); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 4026 | if (part_pagewr) |
| 4027 | bytes = min_t(int, bytes - column, writelen); |
Boris Brezillon | eeab717 | 2018-10-28 15:27:55 +0100 | [diff] [blame] | 4028 | wbuf = nand_get_data_buf(chip); |
| 4029 | memset(wbuf, 0xff, mtd->writesize); |
| 4030 | memcpy(&wbuf[column], buf, bytes); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4031 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4032 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4033 | if (unlikely(oob)) { |
| 4034 | size_t len = min(oobwritelen, oobmaxlen); |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4035 | oob = nand_fill_oob(chip, oob, len, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4036 | oobwritelen -= len; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 4037 | } else { |
| 4038 | /* We still need to erase leftover OOB data */ |
| 4039 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4040 | } |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 4041 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4042 | ret = nand_write_page(chip, column, bytes, wbuf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 4043 | oob_required, page, |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 4044 | (ops->mode == MTD_OPS_RAW)); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4045 | if (ret) |
| 4046 | break; |
| 4047 | |
| 4048 | writelen -= bytes; |
| 4049 | if (!writelen) |
| 4050 | break; |
| 4051 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4052 | column = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4053 | buf += bytes; |
| 4054 | realpage++; |
| 4055 | |
| 4056 | page = realpage & chip->pagemask; |
| 4057 | /* Check, if we cross a chip boundary */ |
| 4058 | if (!page) { |
| 4059 | chipnr++; |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4060 | nand_deselect_target(chip); |
| 4061 | nand_select_target(chip, chipnr); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4062 | } |
| 4063 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4064 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4065 | ops->retlen = ops->len - writelen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 4066 | if (unlikely(oob)) |
| 4067 | ops->oobretlen = ops->ooblen; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4068 | |
| 4069 | err_out: |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4070 | nand_deselect_target(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4071 | return ret; |
| 4072 | } |
| 4073 | |
| 4074 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4075 | * panic_nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4076 | * @mtd: MTD device structure |
| 4077 | * @to: offset to write to |
| 4078 | * @len: number of bytes to write |
| 4079 | * @retlen: pointer to variable to store the number of written bytes |
| 4080 | * @buf: the data to write |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4081 | * |
| 4082 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 4083 | * may for example be called by mtdoops when writing an oops while in panic. |
| 4084 | */ |
| 4085 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 4086 | size_t *retlen, const uint8_t *buf) |
| 4087 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4088 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4089 | int chipnr = (int)(to >> chip->chip_shift); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4090 | struct mtd_oob_ops ops; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4091 | int ret; |
| 4092 | |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4093 | nand_select_target(chip, chipnr); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4094 | |
| 4095 | /* Wait for the device to get ready */ |
Boris Brezillon | f1d4694 | 2018-09-06 14:05:29 +0200 | [diff] [blame] | 4096 | panic_nand_wait(chip, 400); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4097 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 4098 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4099 | ops.len = len; |
| 4100 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 4101 | ops.mode = MTD_OPS_PLACE_OOB; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4102 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4103 | ret = nand_do_write_ops(chip, to, &ops); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4104 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4105 | *retlen = ops.retlen; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4106 | return ret; |
| 4107 | } |
| 4108 | |
| 4109 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4110 | * 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] | 4111 | * @mtd: MTD device structure |
| 4112 | * @to: offset to write to |
| 4113 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4114 | */ |
| 4115 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 4116 | struct mtd_oob_ops *ops) |
| 4117 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4118 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4119 | int ret = -ENOTSUPP; |
| 4120 | |
| 4121 | ops->retlen = 0; |
| 4122 | |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4123 | ret = nand_get_device(chip); |
| 4124 | if (ret) |
| 4125 | return ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4126 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4127 | switch (ops->mode) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 4128 | case MTD_OPS_PLACE_OOB: |
| 4129 | case MTD_OPS_AUTO_OOB: |
| 4130 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4131 | break; |
| 4132 | |
| 4133 | default: |
| 4134 | goto out; |
| 4135 | } |
| 4136 | |
| 4137 | if (!ops->datbuf) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4138 | ret = nand_do_write_oob(chip, to, ops); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4139 | else |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4140 | ret = nand_do_write_ops(chip, to, ops); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4141 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4142 | out: |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4143 | nand_release_device(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4144 | return ret; |
| 4145 | } |
| 4146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4147 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4148 | * nand_erase - [MTD Interface] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4149 | * @mtd: MTD device structure |
| 4150 | * @instr: erase instruction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4152 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4154 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4155 | { |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4156 | return nand_erase_nand(mtd_to_nand(mtd), instr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4160 | * nand_erase_nand - [INTERN] erase block(s) |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4161 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4162 | * @instr: erase instruction |
| 4163 | * @allowbbt: allow erasing the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4164 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4165 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | */ |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4167 | int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr, |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4168 | int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4169 | { |
Masahiro Yamada | 2d73f3d | 2019-01-21 15:32:07 +0900 | [diff] [blame] | 4170 | int page, pages_per_block, ret, chipnr; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4171 | loff_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4173 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 4174 | __func__, (unsigned long long)instr->addr, |
| 4175 | (unsigned long long)instr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4176 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4177 | if (check_offs_len(chip, instr->addr, instr->len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4178 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | /* Grab the lock and see if the device is available */ |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4181 | ret = nand_get_device(chip); |
| 4182 | if (ret) |
| 4183 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | |
| 4185 | /* Shift to get first page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4186 | page = (int)(instr->addr >> chip->page_shift); |
| 4187 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | |
| 4189 | /* Calculate pages in each block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4190 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | |
| 4192 | /* Select the NAND device */ |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4193 | nand_select_target(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | /* Check, if it is write protected */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4196 | if (nand_check_wp(chip)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4197 | pr_debug("%s: device is write protected!\n", |
| 4198 | __func__); |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4199 | ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4200 | goto erase_exit; |
| 4201 | } |
| 4202 | |
| 4203 | /* Loop through the pages */ |
| 4204 | len = instr->len; |
| 4205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4206 | while (len) { |
Wolfram Sang | 12183a2 | 2011-12-21 23:01:20 +0100 | [diff] [blame] | 4207 | /* Check if we have a bad block, we do not erase bad blocks! */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4208 | if (nand_block_checkbad(chip, ((loff_t) page) << |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4209 | chip->page_shift, allowbbt)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4210 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
| 4211 | __func__, page); |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4212 | ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | goto erase_exit; |
| 4214 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4215 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4216 | /* |
| 4217 | * Invalidate the page cache, if we erase the block which |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4218 | * contains the current cached page. |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4219 | */ |
Boris Brezillon | d974541 | 2018-10-28 16:12:45 +0100 | [diff] [blame] | 4220 | if (page <= chip->pagecache.page && chip->pagecache.page < |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4221 | (page + pages_per_block)) |
Boris Brezillon | d974541 | 2018-10-28 16:12:45 +0100 | [diff] [blame] | 4222 | chip->pagecache.page = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4223 | |
Masahiro Yamada | 2d73f3d | 2019-01-21 15:32:07 +0900 | [diff] [blame] | 4224 | ret = nand_erase_op(chip, (page & chip->pagemask) >> |
| 4225 | (chip->phys_erase_shift - chip->page_shift)); |
| 4226 | if (ret) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4227 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 4228 | __func__, page); |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4229 | instr->fail_addr = |
| 4230 | ((loff_t)page << chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | goto erase_exit; |
| 4232 | } |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 4233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | /* Increment page address and decrement length */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 4235 | len -= (1ULL << chip->phys_erase_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | page += pages_per_block; |
| 4237 | |
| 4238 | /* Check, if we cross a chip boundary */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4239 | if (len && !(page & chip->pagemask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | chipnr++; |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4241 | nand_deselect_target(chip); |
| 4242 | nand_select_target(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | } |
| 4244 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4246 | ret = 0; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4247 | erase_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4249 | /* Deselect and wake up anyone waiting on the device */ |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4250 | nand_deselect_target(chip); |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4251 | nand_release_device(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4253 | /* Return more or less happy */ |
| 4254 | return ret; |
| 4255 | } |
| 4256 | |
| 4257 | /** |
| 4258 | * nand_sync - [MTD Interface] sync |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4259 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4261 | * Sync is actually a wait for chip ready function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4263 | static void nand_sync(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4264 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4265 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4266 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4267 | pr_debug("%s: called\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | |
| 4269 | /* Grab the lock and see if the device is available */ |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4270 | WARN_ON(nand_get_device(chip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | /* Release it and go back */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4272 | nand_release_device(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | } |
| 4274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4276 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4277 | * @mtd: MTD device structure |
| 4278 | * @offs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4280 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | { |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4282 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4283 | int chipnr = (int)(offs >> chip->chip_shift); |
| 4284 | int ret; |
| 4285 | |
| 4286 | /* Select the NAND device */ |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4287 | ret = nand_get_device(chip); |
| 4288 | if (ret) |
| 4289 | return ret; |
| 4290 | |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4291 | nand_select_target(chip, chipnr); |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4292 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4293 | ret = nand_block_checkbad(chip, offs, 0); |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4294 | |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4295 | nand_deselect_target(chip); |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4296 | nand_release_device(chip); |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4297 | |
| 4298 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4299 | } |
| 4300 | |
| 4301 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4302 | * 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] | 4303 | * @mtd: MTD device structure |
| 4304 | * @ofs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4306 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4308 | int ret; |
| 4309 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4310 | ret = nand_block_isbad(mtd, ofs); |
| 4311 | if (ret) { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4312 | /* If it was bad already, return success and do nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | if (ret > 0) |
| 4314 | return 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4315 | return ret; |
| 4316 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4317 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4318 | return nand_block_markbad_lowlevel(mtd_to_nand(mtd), ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | /** |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4322 | * nand_suspend - [MTD Interface] Suspend the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4323 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4324 | */ |
| 4325 | static int nand_suspend(struct mtd_info *mtd) |
| 4326 | { |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4327 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4328 | |
| 4329 | mutex_lock(&chip->lock); |
| 4330 | chip->suspended = 1; |
| 4331 | mutex_unlock(&chip->lock); |
| 4332 | |
| 4333 | return 0; |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | /** |
| 4337 | * nand_resume - [MTD Interface] Resume the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4338 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4339 | */ |
| 4340 | static void nand_resume(struct mtd_info *mtd) |
| 4341 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4342 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4343 | |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4344 | mutex_lock(&chip->lock); |
| 4345 | if (chip->suspended) |
| 4346 | chip->suspended = 0; |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4347 | else |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4348 | pr_err("%s called for a chip which is not in suspended state\n", |
| 4349 | __func__); |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4350 | mutex_unlock(&chip->lock); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4351 | } |
| 4352 | |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 4353 | /** |
| 4354 | * nand_shutdown - [MTD Interface] Finish the current NAND operation and |
| 4355 | * prevent further operations |
| 4356 | * @mtd: MTD device structure |
| 4357 | */ |
| 4358 | static void nand_shutdown(struct mtd_info *mtd) |
| 4359 | { |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4360 | nand_suspend(mtd); |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 4361 | } |
| 4362 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4363 | /* Set default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4364 | static void nand_set_defaults(struct nand_chip *chip) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4365 | { |
Boris Brezillon | 7b6a9b2 | 2018-11-20 10:02:39 +0100 | [diff] [blame] | 4366 | /* If no controller is provided, use the dummy, legacy one. */ |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4367 | if (!chip->controller) { |
Boris Brezillon | 7b6a9b2 | 2018-11-20 10:02:39 +0100 | [diff] [blame] | 4368 | chip->controller = &chip->legacy.dummy_controller; |
Miquel Raynal | 7da4513 | 2018-07-17 09:08:02 +0200 | [diff] [blame] | 4369 | nand_controller_init(chip->controller); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4370 | } |
| 4371 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 4372 | nand_legacy_set_defaults(chip); |
| 4373 | |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 4374 | if (!chip->buf_align) |
| 4375 | chip->buf_align = 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4376 | } |
| 4377 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4378 | /* Sanitize ONFI strings so we can safely print them */ |
Boris Brezillon | 1c325cc | 2018-09-07 00:38:50 +0200 | [diff] [blame] | 4379 | void sanitize_string(uint8_t *s, size_t len) |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4380 | { |
| 4381 | ssize_t i; |
| 4382 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4383 | /* Null terminate */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4384 | s[len - 1] = 0; |
| 4385 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4386 | /* Remove non printable chars */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4387 | for (i = 0; i < len - 1; i++) { |
| 4388 | if (s[i] < ' ' || s[i] > 127) |
| 4389 | s[i] = '?'; |
| 4390 | } |
| 4391 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4392 | /* Remove trailing spaces */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4393 | strim(s); |
| 4394 | } |
| 4395 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4396 | /* |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 4397 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 4398 | * @id_data: the ID string |
| 4399 | * @arrlen: the length of the @id_data array |
| 4400 | * @period: the period of repitition |
| 4401 | * |
| 4402 | * Check if an ID string is repeated within a given sequence of bytes at |
| 4403 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Brian Norris | d4d4f1b | 2012-11-14 21:54:20 -0800 | [diff] [blame] | 4404 | * 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] | 4405 | * if the repetition has a period of @period; otherwise, returns zero. |
| 4406 | */ |
| 4407 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 4408 | { |
| 4409 | int i, j; |
| 4410 | for (i = 0; i < period; i++) |
| 4411 | for (j = i + period; j < arrlen; j += period) |
| 4412 | if (id_data[i] != id_data[j]) |
| 4413 | return 0; |
| 4414 | return 1; |
| 4415 | } |
| 4416 | |
| 4417 | /* |
| 4418 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 4419 | * @id_data: the ID string |
| 4420 | * @arrlen: the length of the @id_data array |
| 4421 | |
| 4422 | * Returns the length of the ID string, according to known wraparound/trailing |
| 4423 | * zero patterns. If no pattern exists, returns the length of the array. |
| 4424 | */ |
| 4425 | static int nand_id_len(u8 *id_data, int arrlen) |
| 4426 | { |
| 4427 | int last_nonzero, period; |
| 4428 | |
| 4429 | /* Find last non-zero byte */ |
| 4430 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 4431 | if (id_data[last_nonzero]) |
| 4432 | break; |
| 4433 | |
| 4434 | /* All zeros */ |
| 4435 | if (last_nonzero < 0) |
| 4436 | return 0; |
| 4437 | |
| 4438 | /* Calculate wraparound period */ |
| 4439 | for (period = 1; period < arrlen; period++) |
| 4440 | if (nand_id_has_period(id_data, arrlen, period)) |
| 4441 | break; |
| 4442 | |
| 4443 | /* There's a repeated pattern */ |
| 4444 | if (period < arrlen) |
| 4445 | return period; |
| 4446 | |
| 4447 | /* There are trailing zeros */ |
| 4448 | if (last_nonzero < arrlen - 1) |
| 4449 | return last_nonzero + 1; |
| 4450 | |
| 4451 | /* No pattern detected */ |
| 4452 | return arrlen; |
| 4453 | } |
| 4454 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 4455 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 4456 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 4457 | { |
| 4458 | int bits; |
| 4459 | |
| 4460 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 4461 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 4462 | return bits + 1; |
| 4463 | } |
| 4464 | |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 4465 | /* |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4466 | * Many new NAND share similar device ID codes, which represent the size of the |
| 4467 | * chip. The rest of the parameters must be decoded according to generic or |
| 4468 | * manufacturer-specific "extended ID" decoding patterns. |
| 4469 | */ |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4470 | void nand_decode_ext_id(struct nand_chip *chip) |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4471 | { |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4472 | struct nand_memory_organization *memorg; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4473 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 9b2d61f | 2016-06-08 10:34:57 +0200 | [diff] [blame] | 4474 | int extid; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4475 | u8 *id_data = chip->id.data; |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4476 | |
| 4477 | memorg = nanddev_get_memorg(&chip->base); |
| 4478 | |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4479 | /* The 3rd id byte holds MLC / multichip data */ |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4480 | memorg->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4481 | /* The 4th id byte is the important one */ |
| 4482 | extid = id_data[3]; |
| 4483 | |
Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 4484 | /* Calc pagesize */ |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4485 | memorg->pagesize = 1024 << (extid & 0x03); |
| 4486 | mtd->writesize = memorg->pagesize; |
Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 4487 | extid >>= 2; |
| 4488 | /* Calc oobsize */ |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4489 | memorg->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9); |
| 4490 | mtd->oobsize = memorg->oobsize; |
Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 4491 | extid >>= 2; |
| 4492 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4493 | memorg->pages_per_eraseblock = ((64 * 1024) << (extid & 0x03)) / |
| 4494 | memorg->pagesize; |
Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 4495 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 4496 | extid >>= 2; |
| 4497 | /* Get buswidth information */ |
| 4498 | if (extid & 0x1) |
| 4499 | chip->options |= NAND_BUSWIDTH_16; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4500 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4501 | EXPORT_SYMBOL_GPL(nand_decode_ext_id); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4502 | |
| 4503 | /* |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4504 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 4505 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 4506 | * the chip. |
| 4507 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4508 | 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] | 4509 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4510 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4511 | struct nand_memory_organization *memorg; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4512 | |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4513 | memorg = nanddev_get_memorg(&chip->base); |
| 4514 | |
| 4515 | memorg->pages_per_eraseblock = type->erasesize / type->pagesize; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4516 | mtd->erasesize = type->erasesize; |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4517 | memorg->pagesize = type->pagesize; |
| 4518 | mtd->writesize = memorg->pagesize; |
| 4519 | memorg->oobsize = memorg->pagesize / 32; |
| 4520 | mtd->oobsize = memorg->oobsize; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4521 | |
Huang Shijie | 1c195e9 | 2013-09-25 14:58:12 +0800 | [diff] [blame] | 4522 | /* All legacy ID NAND are small-page, SLC */ |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4523 | memorg->bits_per_cell = 1; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4524 | } |
| 4525 | |
| 4526 | /* |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4527 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 4528 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 4529 | * page size, cell-type information). |
| 4530 | */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4531 | static void nand_decode_bbm_options(struct nand_chip *chip) |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4532 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4533 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4534 | |
| 4535 | /* Set the bad block position */ |
| 4536 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
Frieder Schrempf | 04649ec | 2019-04-17 12:36:34 +0000 | [diff] [blame] | 4537 | chip->badblockpos = NAND_BBM_POS_LARGE; |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4538 | else |
Frieder Schrempf | 04649ec | 2019-04-17 12:36:34 +0000 | [diff] [blame] | 4539 | chip->badblockpos = NAND_BBM_POS_SMALL; |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4540 | } |
| 4541 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4542 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 4543 | { |
| 4544 | return type->id_len; |
| 4545 | } |
| 4546 | |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4547 | static bool find_full_id_nand(struct nand_chip *chip, |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4548 | struct nand_flash_dev *type) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4549 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4550 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4551 | struct nand_memory_organization *memorg; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4552 | u8 *id_data = chip->id.data; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4553 | |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4554 | memorg = nanddev_get_memorg(&chip->base); |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4555 | |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4556 | if (!strncmp(type->id, id_data, type->id_len)) { |
| 4557 | memorg->pagesize = type->pagesize; |
| 4558 | mtd->writesize = memorg->pagesize; |
| 4559 | memorg->pages_per_eraseblock = type->erasesize / |
| 4560 | type->pagesize; |
| 4561 | mtd->erasesize = type->erasesize; |
| 4562 | memorg->oobsize = type->oobsize; |
| 4563 | mtd->oobsize = memorg->oobsize; |
| 4564 | |
| 4565 | memorg->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4566 | memorg->eraseblocks_per_lun = |
| 4567 | DIV_ROUND_DOWN_ULL((u64)type->chipsize << 20, |
| 4568 | memorg->pagesize * |
| 4569 | memorg->pages_per_eraseblock); |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4570 | chip->options |= type->options; |
Boris Brezillon | 6a1b66d | 2018-11-04 16:09:42 +0100 | [diff] [blame] | 4571 | chip->base.eccreq.strength = NAND_ECC_STRENGTH(type); |
| 4572 | chip->base.eccreq.step_size = NAND_ECC_STEP(type); |
Boris BREZILLON | 57a94e2 | 2014-09-22 20:11:50 +0200 | [diff] [blame] | 4573 | chip->onfi_timing_mode_default = |
| 4574 | type->onfi_timing_mode_default; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4575 | |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4576 | chip->parameters.model = kstrdup(type->name, GFP_KERNEL); |
| 4577 | if (!chip->parameters.model) |
| 4578 | return false; |
Cai Zhiyong | 092b6a1 | 2013-12-25 21:19:21 +0800 | [diff] [blame] | 4579 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4580 | return true; |
| 4581 | } |
| 4582 | return false; |
| 4583 | } |
| 4584 | |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4585 | /* |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4586 | * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC |
| 4587 | * compliant and does not have a full-id or legacy-id entry in the nand_ids |
| 4588 | * table. |
| 4589 | */ |
| 4590 | static void nand_manufacturer_detect(struct nand_chip *chip) |
| 4591 | { |
| 4592 | /* |
| 4593 | * Try manufacturer detection if available and use |
| 4594 | * nand_decode_ext_id() otherwise. |
| 4595 | */ |
| 4596 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4597 | chip->manufacturer.desc->ops->detect) { |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4598 | struct nand_memory_organization *memorg; |
| 4599 | |
| 4600 | memorg = nanddev_get_memorg(&chip->base); |
| 4601 | |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4602 | /* The 3rd id byte holds MLC / multichip data */ |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4603 | memorg->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4604 | chip->manufacturer.desc->ops->detect(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4605 | } else { |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4606 | nand_decode_ext_id(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4607 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4608 | } |
| 4609 | |
| 4610 | /* |
| 4611 | * Manufacturer initialization. This function is called for all NANDs including |
| 4612 | * ONFI and JEDEC compliant ones. |
| 4613 | * Manufacturer drivers should put all their specific initialization code in |
| 4614 | * their ->init() hook. |
| 4615 | */ |
| 4616 | static int nand_manufacturer_init(struct nand_chip *chip) |
| 4617 | { |
| 4618 | if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops || |
| 4619 | !chip->manufacturer.desc->ops->init) |
| 4620 | return 0; |
| 4621 | |
| 4622 | return chip->manufacturer.desc->ops->init(chip); |
| 4623 | } |
| 4624 | |
| 4625 | /* |
| 4626 | * Manufacturer cleanup. This function is called for all NANDs including |
| 4627 | * ONFI and JEDEC compliant ones. |
| 4628 | * Manufacturer drivers should put all their specific cleanup code in their |
| 4629 | * ->cleanup() hook. |
| 4630 | */ |
| 4631 | static void nand_manufacturer_cleanup(struct nand_chip *chip) |
| 4632 | { |
| 4633 | /* Release manufacturer private data */ |
| 4634 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
| 4635 | chip->manufacturer.desc->ops->cleanup) |
| 4636 | chip->manufacturer.desc->ops->cleanup(chip); |
| 4637 | } |
| 4638 | |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 4639 | static const char * |
| 4640 | nand_manufacturer_name(const struct nand_manufacturer *manufacturer) |
| 4641 | { |
| 4642 | return manufacturer ? manufacturer->name : "Unknown"; |
| 4643 | } |
| 4644 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4645 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4646 | * 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] | 4647 | */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 4648 | 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] | 4649 | { |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4650 | const struct nand_manufacturer *manufacturer; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4651 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4652 | struct nand_memory_organization *memorg; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4653 | int busw, ret; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4654 | u8 *id_data = chip->id.data; |
| 4655 | u8 maf_id, dev_id; |
Boris Brezillon | 6c836d5 | 2018-10-29 11:22:16 +0100 | [diff] [blame] | 4656 | u64 targetsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4658 | /* |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4659 | * Let's start by initializing memorg fields that might be left |
| 4660 | * unassigned by the ID-based detection logic. |
| 4661 | */ |
| 4662 | memorg = nanddev_get_memorg(&chip->base); |
| 4663 | memorg->planes_per_lun = 1; |
| 4664 | memorg->luns_per_target = 1; |
| 4665 | memorg->ntargets = 1; |
| 4666 | |
| 4667 | /* |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4668 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4669 | * after power-up. |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4670 | */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4671 | ret = nand_reset(chip, 0); |
| 4672 | if (ret) |
| 4673 | return ret; |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4674 | |
| 4675 | /* Select the device */ |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4676 | nand_select_target(chip, 0); |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | /* Send the command for reading device ID */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4679 | ret = nand_readid_op(chip, 0, id_data, 2); |
| 4680 | if (ret) |
| 4681 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | |
| 4683 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4684 | maf_id = id_data[0]; |
| 4685 | dev_id = id_data[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4686 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4687 | /* |
| 4688 | * 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] | 4689 | * interface concerns can cause random data which looks like a |
| 4690 | * possibly credible NAND flash to appear. If the two results do |
| 4691 | * not match, ignore the device completely. |
| 4692 | */ |
| 4693 | |
Brian Norris | 4aef9b7 | 2012-09-24 20:40:48 -0700 | [diff] [blame] | 4694 | /* Read entire ID string */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4695 | ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data)); |
| 4696 | if (ret) |
| 4697 | return ret; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 4698 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4699 | if (id_data[0] != maf_id || id_data[1] != dev_id) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4700 | 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] | 4701 | maf_id, dev_id, id_data[0], id_data[1]); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4702 | return -ENODEV; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 4703 | } |
| 4704 | |
Jean-Louis Thekekara | 5158bd5 | 2017-06-29 19:08:30 +0200 | [diff] [blame] | 4705 | 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] | 4706 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4707 | /* Try to identify manufacturer */ |
| 4708 | manufacturer = nand_get_manufacturer(maf_id); |
| 4709 | chip->manufacturer.desc = manufacturer; |
| 4710 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4711 | if (!type) |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4712 | type = nand_flash_ids; |
| 4713 | |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4714 | /* |
| 4715 | * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic |
| 4716 | * override it. |
| 4717 | * This is required to make sure initial NAND bus width set by the |
| 4718 | * NAND controller driver is coherent with the real NAND bus width |
| 4719 | * (extracted by auto-detection code). |
| 4720 | */ |
| 4721 | busw = chip->options & NAND_BUSWIDTH_16; |
| 4722 | |
| 4723 | /* |
| 4724 | * The flag is only set (never cleared), reset it to its default value |
| 4725 | * before starting auto-detection. |
| 4726 | */ |
| 4727 | chip->options &= ~NAND_BUSWIDTH_16; |
| 4728 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4729 | for (; type->name != NULL; type++) { |
| 4730 | if (is_full_id_nand(type)) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4731 | if (find_full_id_nand(chip, type)) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4732 | goto ident_done; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4733 | } else if (dev_id == type->dev_id) { |
Brian Norris | db5b09f | 2015-05-22 10:43:12 -0700 | [diff] [blame] | 4734 | break; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4735 | } |
| 4736 | } |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4737 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4738 | if (!type->name || !type->pagesize) { |
Masahiro Yamada | 35fc519 | 2014-04-09 16:26:26 +0900 | [diff] [blame] | 4739 | /* Check if the chip is ONFI compliant */ |
Boris Brezillon | 1c325cc | 2018-09-07 00:38:50 +0200 | [diff] [blame] | 4740 | ret = nand_onfi_detect(chip); |
Miquel Raynal | bd0b643 | 2018-03-19 14:47:31 +0100 | [diff] [blame] | 4741 | if (ret < 0) |
| 4742 | return ret; |
| 4743 | else if (ret) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4744 | goto ident_done; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4745 | |
| 4746 | /* Check if the chip is JEDEC compliant */ |
Boris Brezillon | 8ae3fbf | 2018-09-07 00:38:51 +0200 | [diff] [blame] | 4747 | ret = nand_jedec_detect(chip); |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 4748 | if (ret < 0) |
| 4749 | return ret; |
| 4750 | else if (ret) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4751 | goto ident_done; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4752 | } |
| 4753 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4754 | if (!type->name) |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4755 | return -ENODEV; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4756 | |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4757 | chip->parameters.model = kstrdup(type->name, GFP_KERNEL); |
| 4758 | if (!chip->parameters.model) |
| 4759 | return -ENOMEM; |
Thomas Gleixner | ba0251fe | 2006-05-27 01:02:13 +0200 | [diff] [blame] | 4760 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4761 | if (!type->pagesize) |
| 4762 | nand_manufacturer_detect(chip); |
| 4763 | else |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4764 | nand_decode_id(chip, type); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4765 | |
Brian Norris | bf7a01b | 2012-07-13 09:28:24 -0700 | [diff] [blame] | 4766 | /* Get chip options */ |
| 4767 | chip->options |= type->options; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4768 | |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 4769 | memorg->eraseblocks_per_lun = |
| 4770 | DIV_ROUND_DOWN_ULL((u64)type->chipsize << 20, |
| 4771 | memorg->pagesize * |
| 4772 | memorg->pages_per_eraseblock); |
| 4773 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4774 | ident_done: |
Miquel Raynal | f4531b2 | 2018-03-19 14:47:26 +0100 | [diff] [blame] | 4775 | if (!mtd->name) |
| 4776 | mtd->name = chip->parameters.model; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4777 | |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 4778 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4779 | WARN_ON(busw & NAND_BUSWIDTH_16); |
| 4780 | nand_set_defaults(chip); |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 4781 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 4782 | /* |
| 4783 | * Check, if buswidth is correct. Hardware drivers should set |
| 4784 | * chip correct! |
| 4785 | */ |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4786 | 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] | 4787 | maf_id, dev_id); |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4788 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4789 | mtd->name); |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4790 | pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8, |
| 4791 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4792 | ret = -EINVAL; |
| 4793 | |
| 4794 | goto free_detect_allocation; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4795 | } |
| 4796 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4797 | nand_decode_bbm_options(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4798 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4799 | /* Calculate the address shift from the page size */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4800 | chip->page_shift = ffs(mtd->writesize) - 1; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4801 | /* Convert chipsize to number of pages per chip -1 */ |
Boris Brezillon | 6c836d5 | 2018-10-29 11:22:16 +0100 | [diff] [blame] | 4802 | targetsize = nanddev_target_size(&chip->base); |
| 4803 | chip->pagemask = (targetsize >> chip->page_shift) - 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4804 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4805 | chip->bbt_erase_shift = chip->phys_erase_shift = |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4806 | ffs(mtd->erasesize) - 1; |
Boris Brezillon | 6c836d5 | 2018-10-29 11:22:16 +0100 | [diff] [blame] | 4807 | if (targetsize & 0xffffffff) |
| 4808 | chip->chip_shift = ffs((unsigned)targetsize) - 1; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4809 | else { |
Boris Brezillon | 6c836d5 | 2018-10-29 11:22:16 +0100 | [diff] [blame] | 4810 | chip->chip_shift = ffs((unsigned)(targetsize >> 32)); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4811 | chip->chip_shift += 32 - 1; |
| 4812 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4813 | |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame] | 4814 | if (chip->chip_shift - chip->page_shift > 16) |
| 4815 | chip->options |= NAND_ROW_ADDR_3; |
| 4816 | |
Artem Bityutskiy | 26d9be1 | 2011-04-28 20:26:59 +0300 | [diff] [blame] | 4817 | chip->badblockbits = 8; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4818 | |
Boris Brezillon | 3d4af7c | 2018-09-07 00:38:49 +0200 | [diff] [blame] | 4819 | nand_legacy_adjust_cmdfunc(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4820 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4821 | 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] | 4822 | maf_id, dev_id); |
Miquel Raynal | f4531b2 | 2018-03-19 14:47:26 +0100 | [diff] [blame] | 4823 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4824 | chip->parameters.model); |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4825 | pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n", |
Boris Brezillon | 6c836d5 | 2018-10-29 11:22:16 +0100 | [diff] [blame] | 4826 | (int)(targetsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4827 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4828 | return 0; |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4829 | |
| 4830 | free_detect_allocation: |
| 4831 | kfree(chip->parameters.model); |
| 4832 | |
| 4833 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4834 | } |
| 4835 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4836 | static const char * const nand_ecc_modes[] = { |
| 4837 | [NAND_ECC_NONE] = "none", |
| 4838 | [NAND_ECC_SOFT] = "soft", |
| 4839 | [NAND_ECC_HW] = "hw", |
| 4840 | [NAND_ECC_HW_SYNDROME] = "hw_syndrome", |
| 4841 | [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first", |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 4842 | [NAND_ECC_ON_DIE] = "on-die", |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4843 | }; |
| 4844 | |
| 4845 | static int of_get_nand_ecc_mode(struct device_node *np) |
| 4846 | { |
| 4847 | const char *pm; |
| 4848 | int err, i; |
| 4849 | |
| 4850 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4851 | if (err < 0) |
| 4852 | return err; |
| 4853 | |
| 4854 | for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++) |
| 4855 | if (!strcasecmp(pm, nand_ecc_modes[i])) |
| 4856 | return i; |
| 4857 | |
Rafał Miłecki | ae211bc | 2016-04-17 22:53:06 +0200 | [diff] [blame] | 4858 | /* |
| 4859 | * For backward compatibility we support few obsoleted values that don't |
| 4860 | * have their mappings into nand_ecc_modes_t anymore (they were merged |
| 4861 | * with other enums). |
| 4862 | */ |
| 4863 | if (!strcasecmp(pm, "soft_bch")) |
| 4864 | return NAND_ECC_SOFT; |
| 4865 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4866 | return -ENODEV; |
| 4867 | } |
| 4868 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4869 | static const char * const nand_ecc_algos[] = { |
| 4870 | [NAND_ECC_HAMMING] = "hamming", |
| 4871 | [NAND_ECC_BCH] = "bch", |
Stefan Agner | f308d73 | 2018-06-24 23:27:22 +0200 | [diff] [blame] | 4872 | [NAND_ECC_RS] = "rs", |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4873 | }; |
| 4874 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4875 | static int of_get_nand_ecc_algo(struct device_node *np) |
| 4876 | { |
| 4877 | const char *pm; |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4878 | int err, i; |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4879 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4880 | err = of_property_read_string(np, "nand-ecc-algo", &pm); |
| 4881 | if (!err) { |
| 4882 | for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++) |
| 4883 | if (!strcasecmp(pm, nand_ecc_algos[i])) |
| 4884 | return i; |
| 4885 | return -ENODEV; |
| 4886 | } |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4887 | |
| 4888 | /* |
| 4889 | * For backward compatibility we also read "nand-ecc-mode" checking |
| 4890 | * for some obsoleted values that were specifying ECC algorithm. |
| 4891 | */ |
| 4892 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4893 | if (err < 0) |
| 4894 | return err; |
| 4895 | |
| 4896 | if (!strcasecmp(pm, "soft")) |
| 4897 | return NAND_ECC_HAMMING; |
| 4898 | else if (!strcasecmp(pm, "soft_bch")) |
| 4899 | return NAND_ECC_BCH; |
| 4900 | |
| 4901 | return -ENODEV; |
| 4902 | } |
| 4903 | |
| 4904 | static int of_get_nand_ecc_step_size(struct device_node *np) |
| 4905 | { |
| 4906 | int ret; |
| 4907 | u32 val; |
| 4908 | |
| 4909 | ret = of_property_read_u32(np, "nand-ecc-step-size", &val); |
| 4910 | return ret ? ret : val; |
| 4911 | } |
| 4912 | |
| 4913 | static int of_get_nand_ecc_strength(struct device_node *np) |
| 4914 | { |
| 4915 | int ret; |
| 4916 | u32 val; |
| 4917 | |
| 4918 | ret = of_property_read_u32(np, "nand-ecc-strength", &val); |
| 4919 | return ret ? ret : val; |
| 4920 | } |
| 4921 | |
| 4922 | static int of_get_nand_bus_width(struct device_node *np) |
| 4923 | { |
| 4924 | u32 val; |
| 4925 | |
| 4926 | if (of_property_read_u32(np, "nand-bus-width", &val)) |
| 4927 | return 8; |
| 4928 | |
| 4929 | switch (val) { |
| 4930 | case 8: |
| 4931 | case 16: |
| 4932 | return val; |
| 4933 | default: |
| 4934 | return -EIO; |
| 4935 | } |
| 4936 | } |
| 4937 | |
| 4938 | static bool of_get_nand_on_flash_bbt(struct device_node *np) |
| 4939 | { |
| 4940 | return of_property_read_bool(np, "nand-on-flash-bbt"); |
| 4941 | } |
| 4942 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4943 | static int nand_dt_init(struct nand_chip *chip) |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4944 | { |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4945 | struct device_node *dn = nand_get_flash_node(chip); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4946 | int ecc_mode, ecc_algo, ecc_strength, ecc_step; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4947 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4948 | if (!dn) |
| 4949 | return 0; |
| 4950 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4951 | if (of_get_nand_bus_width(dn) == 16) |
| 4952 | chip->options |= NAND_BUSWIDTH_16; |
| 4953 | |
Stefan Agner | f922bd7 | 2018-06-24 23:27:23 +0200 | [diff] [blame] | 4954 | if (of_property_read_bool(dn, "nand-is-boot-medium")) |
| 4955 | chip->options |= NAND_IS_BOOT_MEDIUM; |
| 4956 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4957 | if (of_get_nand_on_flash_bbt(dn)) |
| 4958 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 4959 | |
| 4960 | ecc_mode = of_get_nand_ecc_mode(dn); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4961 | ecc_algo = of_get_nand_ecc_algo(dn); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4962 | ecc_strength = of_get_nand_ecc_strength(dn); |
| 4963 | ecc_step = of_get_nand_ecc_step_size(dn); |
| 4964 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4965 | if (ecc_mode >= 0) |
| 4966 | chip->ecc.mode = ecc_mode; |
| 4967 | |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4968 | if (ecc_algo >= 0) |
| 4969 | chip->ecc.algo = ecc_algo; |
| 4970 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4971 | if (ecc_strength >= 0) |
| 4972 | chip->ecc.strength = ecc_strength; |
| 4973 | |
| 4974 | if (ecc_step > 0) |
| 4975 | chip->ecc.size = ecc_step; |
| 4976 | |
Boris Brezillon | ba78ee0 | 2016-06-08 17:04:22 +0200 | [diff] [blame] | 4977 | if (of_property_read_bool(dn, "nand-ecc-maximize")) |
| 4978 | chip->ecc.options |= NAND_ECC_MAXIMIZE; |
| 4979 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4980 | return 0; |
| 4981 | } |
| 4982 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4983 | /** |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 4984 | * nand_scan_ident - Scan for the NAND device |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 4985 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4986 | * @maxchips: number of chips to scan for |
| 4987 | * @table: alternative NAND ID table |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4988 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4989 | * This is the first phase of the normal nand_scan() function. It reads the |
| 4990 | * flash ID and sets up MTD fields accordingly. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4991 | * |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 4992 | * This helper used to be called directly from controller drivers that needed |
| 4993 | * to tweak some ECC-related parameters before nand_scan_tail(). This separation |
| 4994 | * prevented dynamic allocations during this phase which was unconvenient and |
| 4995 | * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4996 | */ |
Boris Brezillon | 871a407 | 2018-08-04 22:59:22 +0200 | [diff] [blame] | 4997 | static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips, |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 4998 | struct nand_flash_dev *table) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4999 | { |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5000 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 5001 | struct nand_memory_organization *memorg; |
Boris Brezillon | 871a407 | 2018-08-04 22:59:22 +0200 | [diff] [blame] | 5002 | int nand_maf_id, nand_dev_id; |
| 5003 | unsigned int i; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5004 | int ret; |
| 5005 | |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 5006 | memorg = nanddev_get_memorg(&chip->base); |
| 5007 | |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 5008 | /* Assume all dies are deselected when we enter nand_scan_ident(). */ |
| 5009 | chip->cur_cs = -1; |
| 5010 | |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 5011 | mutex_init(&chip->lock); |
| 5012 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 5013 | /* Enforce the right timings for reset/detection */ |
| 5014 | onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0); |
| 5015 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 5016 | ret = nand_dt_init(chip); |
| 5017 | if (ret) |
| 5018 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5019 | |
Brian Norris | f7a8e38 | 2016-01-05 10:39:45 -0800 | [diff] [blame] | 5020 | if (!mtd->name && mtd->dev.parent) |
| 5021 | mtd->name = dev_name(mtd->dev.parent); |
| 5022 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5023 | /* Set the default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5024 | nand_set_defaults(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5025 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 5026 | ret = nand_legacy_check_hooks(chip); |
| 5027 | if (ret) |
| 5028 | return ret; |
| 5029 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5030 | /* Read the flash type */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 5031 | ret = nand_detect(chip, table); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5032 | if (ret) { |
Ben Dooks | b1c6e6d | 2009-11-02 18:12:33 +0000 | [diff] [blame] | 5033 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 5034 | pr_warn("No NAND device found\n"); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 5035 | nand_deselect_target(chip); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5036 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | } |
| 5038 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5039 | nand_maf_id = chip->id.data[0]; |
| 5040 | nand_dev_id = chip->id.data[1]; |
| 5041 | |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 5042 | nand_deselect_target(chip); |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 5043 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5044 | /* Check for a chip array */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5045 | for (i = 1; i < maxchips; i++) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5046 | u8 id[2]; |
| 5047 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 5048 | /* See comment in nand_get_flash_type for reset */ |
Masahiro Yamada | f9ffb40 | 2019-01-21 22:05:34 +0900 | [diff] [blame] | 5049 | ret = nand_reset(chip, i); |
| 5050 | if (ret) |
| 5051 | break; |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 5052 | |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 5053 | nand_select_target(chip, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5054 | /* Send the command for reading device ID */ |
Masahiro Yamada | f9ffb40 | 2019-01-21 22:05:34 +0900 | [diff] [blame] | 5055 | ret = nand_readid_op(chip, 0, id, sizeof(id)); |
| 5056 | if (ret) |
| 5057 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5058 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5059 | if (nand_maf_id != id[0] || nand_dev_id != id[1]) { |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 5060 | nand_deselect_target(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5061 | break; |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 5062 | } |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 5063 | nand_deselect_target(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | } |
| 5065 | if (i > 1) |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 5066 | pr_info("%d chips detected\n", i); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | /* Store the number of chips and calc total size for mtd */ |
Boris Brezillon | 629a442 | 2018-10-25 17:10:37 +0200 | [diff] [blame] | 5069 | memorg->ntargets = i; |
Boris Brezillon | 6c836d5 | 2018-10-29 11:22:16 +0100 | [diff] [blame] | 5070 | mtd->size = i * nanddev_target_size(&chip->base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5072 | return 0; |
| 5073 | } |
| 5074 | |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5075 | static void nand_scan_ident_cleanup(struct nand_chip *chip) |
| 5076 | { |
| 5077 | kfree(chip->parameters.model); |
Miquel Raynal | 3d3fe3c | 2018-07-25 15:31:52 +0200 | [diff] [blame] | 5078 | kfree(chip->parameters.onfi); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5079 | } |
| 5080 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 5081 | static int nand_set_ecc_soft_ops(struct nand_chip *chip) |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5082 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 5083 | struct mtd_info *mtd = nand_to_mtd(chip); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5084 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 5085 | |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5086 | if (WARN_ON(ecc->mode != NAND_ECC_SOFT)) |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5087 | return -EINVAL; |
| 5088 | |
| 5089 | switch (ecc->algo) { |
| 5090 | case NAND_ECC_HAMMING: |
| 5091 | ecc->calculate = nand_calculate_ecc; |
| 5092 | ecc->correct = nand_correct_data; |
| 5093 | ecc->read_page = nand_read_page_swecc; |
| 5094 | ecc->read_subpage = nand_read_subpage; |
| 5095 | ecc->write_page = nand_write_page_swecc; |
| 5096 | ecc->read_page_raw = nand_read_page_raw; |
| 5097 | ecc->write_page_raw = nand_write_page_raw; |
| 5098 | ecc->read_oob = nand_read_oob_std; |
| 5099 | ecc->write_oob = nand_write_oob_std; |
| 5100 | if (!ecc->size) |
| 5101 | ecc->size = 256; |
| 5102 | ecc->bytes = 3; |
| 5103 | ecc->strength = 1; |
Boris Brezillon | 309600c | 2018-09-04 16:23:28 +0200 | [diff] [blame] | 5104 | |
Miquel Raynal | 9bb9464 | 2019-02-08 08:48:37 +0100 | [diff] [blame] | 5105 | if (IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING_SMC)) |
Boris Brezillon | 309600c | 2018-09-04 16:23:28 +0200 | [diff] [blame] | 5106 | ecc->options |= NAND_ECC_SOFT_HAMMING_SM_ORDER; |
| 5107 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5108 | return 0; |
| 5109 | case NAND_ECC_BCH: |
| 5110 | if (!mtd_nand_has_bch()) { |
Miquel Raynal | 714c068 | 2019-02-06 15:12:27 +0100 | [diff] [blame] | 5111 | WARN(1, "CONFIG_MTD_NAND_ECC_SW_BCH not enabled\n"); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5112 | return -EINVAL; |
| 5113 | } |
| 5114 | ecc->calculate = nand_bch_calculate_ecc; |
| 5115 | ecc->correct = nand_bch_correct_data; |
| 5116 | ecc->read_page = nand_read_page_swecc; |
| 5117 | ecc->read_subpage = nand_read_subpage; |
| 5118 | ecc->write_page = nand_write_page_swecc; |
| 5119 | ecc->read_page_raw = nand_read_page_raw; |
| 5120 | ecc->write_page_raw = nand_write_page_raw; |
| 5121 | ecc->read_oob = nand_read_oob_std; |
| 5122 | ecc->write_oob = nand_write_oob_std; |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 5123 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5124 | /* |
| 5125 | * Board driver should supply ecc.size and ecc.strength |
| 5126 | * values to select how many bits are correctable. |
| 5127 | * Otherwise, default to 4 bits for large page devices. |
| 5128 | */ |
| 5129 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 5130 | ecc->size = 512; |
| 5131 | ecc->strength = 4; |
| 5132 | } |
| 5133 | |
| 5134 | /* |
| 5135 | * if no ecc placement scheme was provided pickup the default |
| 5136 | * large page one. |
| 5137 | */ |
| 5138 | if (!mtd->ooblayout) { |
| 5139 | /* handle large page devices only */ |
| 5140 | if (mtd->oobsize < 64) { |
| 5141 | WARN(1, "OOB layout is required when using software BCH on small pages\n"); |
| 5142 | return -EINVAL; |
| 5143 | } |
| 5144 | |
| 5145 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 5146 | |
| 5147 | } |
| 5148 | |
| 5149 | /* |
| 5150 | * We can only maximize ECC config when the default layout is |
| 5151 | * used, otherwise we don't know how many bytes can really be |
| 5152 | * used. |
| 5153 | */ |
| 5154 | if (mtd->ooblayout == &nand_ooblayout_lp_ops && |
| 5155 | ecc->options & NAND_ECC_MAXIMIZE) { |
| 5156 | int steps, bytes; |
| 5157 | |
| 5158 | /* Always prefer 1k blocks over 512bytes ones */ |
| 5159 | ecc->size = 1024; |
| 5160 | steps = mtd->writesize / ecc->size; |
| 5161 | |
| 5162 | /* Reserve 2 bytes for the BBM */ |
| 5163 | bytes = (mtd->oobsize - 2) / steps; |
| 5164 | ecc->strength = bytes * 8 / fls(8 * ecc->size); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5165 | } |
| 5166 | |
| 5167 | /* See nand_bch_init() for details. */ |
| 5168 | ecc->bytes = 0; |
| 5169 | ecc->priv = nand_bch_init(mtd); |
| 5170 | if (!ecc->priv) { |
| 5171 | WARN(1, "BCH ECC initialization failed!\n"); |
| 5172 | return -EINVAL; |
| 5173 | } |
| 5174 | return 0; |
| 5175 | default: |
| 5176 | WARN(1, "Unsupported ECC algorithm!\n"); |
| 5177 | return -EINVAL; |
| 5178 | } |
| 5179 | } |
| 5180 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5181 | /** |
| 5182 | * nand_check_ecc_caps - check the sanity of preset ECC settings |
| 5183 | * @chip: nand chip info structure |
| 5184 | * @caps: ECC caps info structure |
| 5185 | * @oobavail: OOB size that the ECC engine can use |
| 5186 | * |
| 5187 | * When ECC step size and strength are already set, check if they are supported |
| 5188 | * by the controller and the calculated ECC bytes fit within the chip's OOB. |
| 5189 | * On success, the calculated ECC bytes is set. |
| 5190 | */ |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5191 | static int |
| 5192 | nand_check_ecc_caps(struct nand_chip *chip, |
| 5193 | const struct nand_ecc_caps *caps, int oobavail) |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5194 | { |
| 5195 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5196 | const struct nand_ecc_step_info *stepinfo; |
| 5197 | int preset_step = chip->ecc.size; |
| 5198 | int preset_strength = chip->ecc.strength; |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5199 | int ecc_bytes, nsteps = mtd->writesize / preset_step; |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5200 | int i, j; |
| 5201 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5202 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5203 | stepinfo = &caps->stepinfos[i]; |
| 5204 | |
| 5205 | if (stepinfo->stepsize != preset_step) |
| 5206 | continue; |
| 5207 | |
| 5208 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5209 | if (stepinfo->strengths[j] != preset_strength) |
| 5210 | continue; |
| 5211 | |
| 5212 | ecc_bytes = caps->calc_ecc_bytes(preset_step, |
| 5213 | preset_strength); |
| 5214 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5215 | return ecc_bytes; |
| 5216 | |
| 5217 | if (ecc_bytes * nsteps > oobavail) { |
| 5218 | pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB", |
| 5219 | preset_step, preset_strength); |
| 5220 | return -ENOSPC; |
| 5221 | } |
| 5222 | |
| 5223 | chip->ecc.bytes = ecc_bytes; |
| 5224 | |
| 5225 | return 0; |
| 5226 | } |
| 5227 | } |
| 5228 | |
| 5229 | pr_err("ECC (step, strength) = (%d, %d) not supported on this controller", |
| 5230 | preset_step, preset_strength); |
| 5231 | |
| 5232 | return -ENOTSUPP; |
| 5233 | } |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5234 | |
| 5235 | /** |
| 5236 | * nand_match_ecc_req - meet the chip's requirement with least ECC bytes |
| 5237 | * @chip: nand chip info structure |
| 5238 | * @caps: ECC engine caps info structure |
| 5239 | * @oobavail: OOB size that the ECC engine can use |
| 5240 | * |
| 5241 | * If a chip's ECC requirement is provided, try to meet it with the least |
| 5242 | * number of ECC bytes (i.e. with the largest number of OOB-free bytes). |
| 5243 | * On success, the chosen ECC settings are set. |
| 5244 | */ |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5245 | static int |
| 5246 | nand_match_ecc_req(struct nand_chip *chip, |
| 5247 | const struct nand_ecc_caps *caps, int oobavail) |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5248 | { |
| 5249 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5250 | const struct nand_ecc_step_info *stepinfo; |
Boris Brezillon | 6a1b66d | 2018-11-04 16:09:42 +0100 | [diff] [blame] | 5251 | int req_step = chip->base.eccreq.step_size; |
| 5252 | int req_strength = chip->base.eccreq.strength; |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5253 | int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total; |
| 5254 | int best_step, best_strength, best_ecc_bytes; |
| 5255 | int best_ecc_bytes_total = INT_MAX; |
| 5256 | int i, j; |
| 5257 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5258 | /* No information provided by the NAND chip */ |
| 5259 | if (!req_step || !req_strength) |
| 5260 | return -ENOTSUPP; |
| 5261 | |
| 5262 | /* number of correctable bits the chip requires in a page */ |
| 5263 | req_corr = mtd->writesize / req_step * req_strength; |
| 5264 | |
| 5265 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5266 | stepinfo = &caps->stepinfos[i]; |
| 5267 | step_size = stepinfo->stepsize; |
| 5268 | |
| 5269 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5270 | strength = stepinfo->strengths[j]; |
| 5271 | |
| 5272 | /* |
| 5273 | * If both step size and strength are smaller than the |
| 5274 | * chip's requirement, it is not easy to compare the |
| 5275 | * resulted reliability. |
| 5276 | */ |
| 5277 | if (step_size < req_step && strength < req_strength) |
| 5278 | continue; |
| 5279 | |
| 5280 | if (mtd->writesize % step_size) |
| 5281 | continue; |
| 5282 | |
| 5283 | nsteps = mtd->writesize / step_size; |
| 5284 | |
| 5285 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 5286 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5287 | continue; |
| 5288 | ecc_bytes_total = ecc_bytes * nsteps; |
| 5289 | |
| 5290 | if (ecc_bytes_total > oobavail || |
| 5291 | strength * nsteps < req_corr) |
| 5292 | continue; |
| 5293 | |
| 5294 | /* |
| 5295 | * We assume the best is to meet the chip's requrement |
| 5296 | * with the least number of ECC bytes. |
| 5297 | */ |
| 5298 | if (ecc_bytes_total < best_ecc_bytes_total) { |
| 5299 | best_ecc_bytes_total = ecc_bytes_total; |
| 5300 | best_step = step_size; |
| 5301 | best_strength = strength; |
| 5302 | best_ecc_bytes = ecc_bytes; |
| 5303 | } |
| 5304 | } |
| 5305 | } |
| 5306 | |
| 5307 | if (best_ecc_bytes_total == INT_MAX) |
| 5308 | return -ENOTSUPP; |
| 5309 | |
| 5310 | chip->ecc.size = best_step; |
| 5311 | chip->ecc.strength = best_strength; |
| 5312 | chip->ecc.bytes = best_ecc_bytes; |
| 5313 | |
| 5314 | return 0; |
| 5315 | } |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5316 | |
| 5317 | /** |
| 5318 | * nand_maximize_ecc - choose the max ECC strength available |
| 5319 | * @chip: nand chip info structure |
| 5320 | * @caps: ECC engine caps info structure |
| 5321 | * @oobavail: OOB size that the ECC engine can use |
| 5322 | * |
| 5323 | * Choose the max ECC strength that is supported on the controller, and can fit |
| 5324 | * within the chip's OOB. On success, the chosen ECC settings are set. |
| 5325 | */ |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5326 | static int |
| 5327 | nand_maximize_ecc(struct nand_chip *chip, |
| 5328 | const struct nand_ecc_caps *caps, int oobavail) |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5329 | { |
| 5330 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5331 | const struct nand_ecc_step_info *stepinfo; |
| 5332 | int step_size, strength, nsteps, ecc_bytes, corr; |
| 5333 | int best_corr = 0; |
| 5334 | int best_step = 0; |
| 5335 | int best_strength, best_ecc_bytes; |
| 5336 | int i, j; |
| 5337 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5338 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5339 | stepinfo = &caps->stepinfos[i]; |
| 5340 | step_size = stepinfo->stepsize; |
| 5341 | |
| 5342 | /* If chip->ecc.size is already set, respect it */ |
| 5343 | if (chip->ecc.size && step_size != chip->ecc.size) |
| 5344 | continue; |
| 5345 | |
| 5346 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5347 | strength = stepinfo->strengths[j]; |
| 5348 | |
| 5349 | if (mtd->writesize % step_size) |
| 5350 | continue; |
| 5351 | |
| 5352 | nsteps = mtd->writesize / step_size; |
| 5353 | |
| 5354 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 5355 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5356 | continue; |
| 5357 | |
| 5358 | if (ecc_bytes * nsteps > oobavail) |
| 5359 | continue; |
| 5360 | |
| 5361 | corr = strength * nsteps; |
| 5362 | |
| 5363 | /* |
| 5364 | * If the number of correctable bits is the same, |
| 5365 | * bigger step_size has more reliability. |
| 5366 | */ |
| 5367 | if (corr > best_corr || |
| 5368 | (corr == best_corr && step_size > best_step)) { |
| 5369 | best_corr = corr; |
| 5370 | best_step = step_size; |
| 5371 | best_strength = strength; |
| 5372 | best_ecc_bytes = ecc_bytes; |
| 5373 | } |
| 5374 | } |
| 5375 | } |
| 5376 | |
| 5377 | if (!best_corr) |
| 5378 | return -ENOTSUPP; |
| 5379 | |
| 5380 | chip->ecc.size = best_step; |
| 5381 | chip->ecc.strength = best_strength; |
| 5382 | chip->ecc.bytes = best_ecc_bytes; |
| 5383 | |
| 5384 | return 0; |
| 5385 | } |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5386 | |
Abhishek Sahu | 181ace9 | 2018-06-20 12:57:28 +0530 | [diff] [blame] | 5387 | /** |
| 5388 | * nand_ecc_choose_conf - Set the ECC strength and ECC step size |
| 5389 | * @chip: nand chip info structure |
| 5390 | * @caps: ECC engine caps info structure |
| 5391 | * @oobavail: OOB size that the ECC engine can use |
| 5392 | * |
| 5393 | * Choose the ECC configuration according to following logic |
| 5394 | * |
| 5395 | * 1. If both ECC step size and ECC strength are already set (usually by DT) |
| 5396 | * then check if it is supported by this controller. |
| 5397 | * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength. |
| 5398 | * 3. Otherwise, try to match the ECC step size and ECC strength closest |
| 5399 | * to the chip's requirement. If available OOB size can't fit the chip |
| 5400 | * requirement then fallback to the maximum ECC step size and ECC strength. |
| 5401 | * |
| 5402 | * On success, the chosen ECC settings are set. |
| 5403 | */ |
| 5404 | int nand_ecc_choose_conf(struct nand_chip *chip, |
| 5405 | const struct nand_ecc_caps *caps, int oobavail) |
| 5406 | { |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5407 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5408 | |
| 5409 | if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize)) |
| 5410 | return -EINVAL; |
| 5411 | |
Abhishek Sahu | 181ace9 | 2018-06-20 12:57:28 +0530 | [diff] [blame] | 5412 | if (chip->ecc.size && chip->ecc.strength) |
| 5413 | return nand_check_ecc_caps(chip, caps, oobavail); |
| 5414 | |
| 5415 | if (chip->ecc.options & NAND_ECC_MAXIMIZE) |
| 5416 | return nand_maximize_ecc(chip, caps, oobavail); |
| 5417 | |
| 5418 | if (!nand_match_ecc_req(chip, caps, oobavail)) |
| 5419 | return 0; |
| 5420 | |
| 5421 | return nand_maximize_ecc(chip, caps, oobavail); |
| 5422 | } |
| 5423 | EXPORT_SYMBOL_GPL(nand_ecc_choose_conf); |
| 5424 | |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5425 | /* |
| 5426 | * Check if the chip configuration meet the datasheet requirements. |
| 5427 | |
| 5428 | * If our configuration corrects A bits per B bytes and the minimum |
| 5429 | * required correction level is X bits per Y bytes, then we must ensure |
| 5430 | * both of the following are true: |
| 5431 | * |
| 5432 | * (1) A / B >= X / Y |
| 5433 | * (2) A >= X |
| 5434 | * |
| 5435 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 5436 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 5437 | * in the same sector. |
| 5438 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 5439 | static bool nand_ecc_strength_good(struct nand_chip *chip) |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5440 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 5441 | struct mtd_info *mtd = nand_to_mtd(chip); |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5442 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 5443 | int corr, ds_corr; |
| 5444 | |
Boris Brezillon | 6a1b66d | 2018-11-04 16:09:42 +0100 | [diff] [blame] | 5445 | if (ecc->size == 0 || chip->base.eccreq.step_size == 0) |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5446 | /* Not enough information */ |
| 5447 | return true; |
| 5448 | |
| 5449 | /* |
| 5450 | * We get the number of corrected bits per page to compare |
| 5451 | * the correction density. |
| 5452 | */ |
| 5453 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
Boris Brezillon | 6a1b66d | 2018-11-04 16:09:42 +0100 | [diff] [blame] | 5454 | ds_corr = (mtd->writesize * chip->base.eccreq.strength) / |
| 5455 | chip->base.eccreq.step_size; |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5456 | |
Boris Brezillon | 6a1b66d | 2018-11-04 16:09:42 +0100 | [diff] [blame] | 5457 | return corr >= ds_corr && ecc->strength >= chip->base.eccreq.strength; |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5458 | } |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5459 | |
Boris Brezillon | a7ab085 | 2018-10-25 22:10:36 +0200 | [diff] [blame] | 5460 | static int rawnand_erase(struct nand_device *nand, const struct nand_pos *pos) |
| 5461 | { |
| 5462 | struct nand_chip *chip = container_of(nand, struct nand_chip, |
| 5463 | base); |
| 5464 | unsigned int eb = nanddev_pos_to_row(nand, pos); |
| 5465 | int ret; |
| 5466 | |
| 5467 | eb >>= nand->rowconv.eraseblock_addr_shift; |
| 5468 | |
| 5469 | nand_select_target(chip, pos->target); |
| 5470 | ret = nand_erase_op(chip, eb); |
| 5471 | nand_deselect_target(chip); |
| 5472 | |
| 5473 | return ret; |
| 5474 | } |
| 5475 | |
| 5476 | static int rawnand_markbad(struct nand_device *nand, |
| 5477 | const struct nand_pos *pos) |
| 5478 | { |
| 5479 | struct nand_chip *chip = container_of(nand, struct nand_chip, |
| 5480 | base); |
| 5481 | |
| 5482 | return nand_markbad_bbm(chip, nanddev_pos_to_offs(nand, pos)); |
| 5483 | } |
| 5484 | |
| 5485 | static bool rawnand_isbad(struct nand_device *nand, const struct nand_pos *pos) |
| 5486 | { |
| 5487 | struct nand_chip *chip = container_of(nand, struct nand_chip, |
| 5488 | base); |
| 5489 | int ret; |
| 5490 | |
| 5491 | nand_select_target(chip, pos->target); |
| 5492 | ret = nand_isbad_bbm(chip, nanddev_pos_to_offs(nand, pos)); |
| 5493 | nand_deselect_target(chip); |
| 5494 | |
| 5495 | return ret; |
| 5496 | } |
| 5497 | |
| 5498 | static const struct nand_ops rawnand_ops = { |
| 5499 | .erase = rawnand_erase, |
| 5500 | .markbad = rawnand_markbad, |
| 5501 | .isbad = rawnand_isbad, |
| 5502 | }; |
| 5503 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5504 | /** |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 5505 | * nand_scan_tail - Scan for the NAND device |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5506 | * @chip: NAND chip object |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5507 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5508 | * This is the second phase of the normal nand_scan() function. It fills out |
| 5509 | * all the uninitialized function pointers with the defaults and scans for a |
| 5510 | * bad block table if appropriate. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5511 | */ |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5512 | static int nand_scan_tail(struct nand_chip *chip) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5513 | { |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5514 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5515 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5516 | int ret, i; |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5517 | |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 5518 | /* 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] | 5519 | if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5520 | !(chip->bbt_options & NAND_BBT_USE_FLASH))) { |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5521 | return -EINVAL; |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5522 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 5523 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5524 | chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL); |
Boris Brezillon | aeb93af | 2017-12-05 12:09:29 +0100 | [diff] [blame] | 5525 | if (!chip->data_buf) |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5526 | return -ENOMEM; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 5527 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5528 | /* |
| 5529 | * FIXME: some NAND manufacturer drivers expect the first die to be |
| 5530 | * selected when manufacturer->init() is called. They should be fixed |
| 5531 | * to explictly select the relevant die when interacting with the NAND |
| 5532 | * chip. |
| 5533 | */ |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 5534 | nand_select_target(chip, 0); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5535 | ret = nand_manufacturer_init(chip); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 5536 | nand_deselect_target(chip); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5537 | if (ret) |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5538 | goto err_free_buf; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5539 | |
David Woodhouse | 7dcdcbef | 2006-10-21 17:09:53 +0100 | [diff] [blame] | 5540 | /* Set the internal oob buffer location, just after the page data */ |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5541 | chip->oob_poi = chip->data_buf + mtd->writesize; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5542 | |
| 5543 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5544 | * If no default placement scheme is given, select an appropriate one. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5545 | */ |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5546 | if (!mtd->ooblayout && |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5547 | !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5548 | switch (mtd->oobsize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5549 | case 8: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5550 | case 16: |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 5551 | mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5552 | break; |
| 5553 | case 64: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5554 | case 128: |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 5555 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops); |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5556 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5557 | default: |
Miquel Raynal | 882fd15 | 2017-08-26 17:19:15 +0200 | [diff] [blame] | 5558 | /* |
| 5559 | * Expose the whole OOB area to users if ECC_NONE |
| 5560 | * is passed. We could do that for all kind of |
| 5561 | * ->oobsize, but we must keep the old large/small |
| 5562 | * page with ECC layout when ->oobsize <= 128 for |
| 5563 | * compatibility reasons. |
| 5564 | */ |
| 5565 | if (ecc->mode == NAND_ECC_NONE) { |
| 5566 | mtd_set_ooblayout(mtd, |
| 5567 | &nand_ooblayout_lp_ops); |
| 5568 | break; |
| 5569 | } |
| 5570 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5571 | WARN(1, "No oob scheme defined for oobsize %d\n", |
| 5572 | mtd->oobsize); |
| 5573 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5574 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5575 | } |
| 5576 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5577 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5578 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5579 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5580 | * selected and we have 256 byte pagesize fallback to software ECC |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5581 | */ |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 5582 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5583 | switch (ecc->mode) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 5584 | case NAND_ECC_HW_OOB_FIRST: |
| 5585 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5586 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5587 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 5588 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5589 | goto err_nand_manuf_cleanup; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 5590 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5591 | if (!ecc->read_page) |
| 5592 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Mathieu Malaterre | 3175e12 | 2019-01-16 20:50:04 +0100 | [diff] [blame] | 5593 | /* fall through */ |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 5594 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5595 | case NAND_ECC_HW: |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5596 | /* Use standard hwecc read page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5597 | if (!ecc->read_page) |
| 5598 | ecc->read_page = nand_read_page_hwecc; |
| 5599 | if (!ecc->write_page) |
| 5600 | ecc->write_page = nand_write_page_hwecc; |
| 5601 | if (!ecc->read_page_raw) |
| 5602 | ecc->read_page_raw = nand_read_page_raw; |
| 5603 | if (!ecc->write_page_raw) |
| 5604 | ecc->write_page_raw = nand_write_page_raw; |
| 5605 | if (!ecc->read_oob) |
| 5606 | ecc->read_oob = nand_read_oob_std; |
| 5607 | if (!ecc->write_oob) |
| 5608 | ecc->write_oob = nand_write_oob_std; |
| 5609 | if (!ecc->read_subpage) |
| 5610 | ecc->read_subpage = nand_read_subpage; |
Helmut Schaa | 44991b3 | 2014-04-09 11:13:24 +0200 | [diff] [blame] | 5611 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5612 | ecc->write_subpage = nand_write_subpage_hwecc; |
Mathieu Malaterre | 3175e12 | 2019-01-16 20:50:04 +0100 | [diff] [blame] | 5613 | /* fall through */ |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 5614 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5615 | case NAND_ECC_HW_SYNDROME: |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5616 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 5617 | (!ecc->read_page || |
| 5618 | ecc->read_page == nand_read_page_hwecc || |
| 5619 | !ecc->write_page || |
| 5620 | ecc->write_page == nand_write_page_hwecc)) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5621 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 5622 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5623 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5624 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5625 | /* Use standard syndrome read/write page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5626 | if (!ecc->read_page) |
| 5627 | ecc->read_page = nand_read_page_syndrome; |
| 5628 | if (!ecc->write_page) |
| 5629 | ecc->write_page = nand_write_page_syndrome; |
| 5630 | if (!ecc->read_page_raw) |
| 5631 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 5632 | if (!ecc->write_page_raw) |
| 5633 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 5634 | if (!ecc->read_oob) |
| 5635 | ecc->read_oob = nand_read_oob_syndrome; |
| 5636 | if (!ecc->write_oob) |
| 5637 | ecc->write_oob = nand_write_oob_syndrome; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 5638 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5639 | if (mtd->writesize >= ecc->size) { |
| 5640 | if (!ecc->strength) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5641 | WARN(1, "Driver must set ecc.strength when using hardware ECC\n"); |
| 5642 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5643 | goto err_nand_manuf_cleanup; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 5644 | } |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5645 | break; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 5646 | } |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 5647 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 5648 | ecc->size, mtd->writesize); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5649 | ecc->mode = NAND_ECC_SOFT; |
Rafał Miłecki | e9d4fae | 2016-04-17 22:53:02 +0200 | [diff] [blame] | 5650 | ecc->algo = NAND_ECC_HAMMING; |
Mathieu Malaterre | 3175e12 | 2019-01-16 20:50:04 +0100 | [diff] [blame] | 5651 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5652 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5653 | case NAND_ECC_SOFT: |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 5654 | ret = nand_set_ecc_soft_ops(chip); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5655 | if (ret) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5656 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5657 | goto err_nand_manuf_cleanup; |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 5658 | } |
| 5659 | break; |
| 5660 | |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 5661 | case NAND_ECC_ON_DIE: |
| 5662 | if (!ecc->read_page || !ecc->write_page) { |
| 5663 | WARN(1, "No ECC functions supplied; on-die ECC not possible\n"); |
| 5664 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5665 | goto err_nand_manuf_cleanup; |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 5666 | } |
| 5667 | if (!ecc->read_oob) |
| 5668 | ecc->read_oob = nand_read_oob_std; |
| 5669 | if (!ecc->write_oob) |
| 5670 | ecc->write_oob = nand_write_oob_std; |
| 5671 | break; |
| 5672 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5673 | case NAND_ECC_NONE: |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 5674 | 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] | 5675 | ecc->read_page = nand_read_page_raw; |
| 5676 | ecc->write_page = nand_write_page_raw; |
| 5677 | ecc->read_oob = nand_read_oob_std; |
| 5678 | ecc->read_page_raw = nand_read_page_raw; |
| 5679 | ecc->write_page_raw = nand_write_page_raw; |
| 5680 | ecc->write_oob = nand_write_oob_std; |
| 5681 | ecc->size = mtd->writesize; |
| 5682 | ecc->bytes = 0; |
| 5683 | ecc->strength = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5684 | break; |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 5685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | default: |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5687 | WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode); |
| 5688 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5689 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | |
Boris Brezillon | aeb93af | 2017-12-05 12:09:29 +0100 | [diff] [blame] | 5692 | if (ecc->correct || ecc->calculate) { |
| 5693 | ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 5694 | ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 5695 | if (!ecc->calc_buf || !ecc->code_buf) { |
| 5696 | ret = -ENOMEM; |
| 5697 | goto err_nand_manuf_cleanup; |
| 5698 | } |
| 5699 | } |
| 5700 | |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 5701 | /* For many systems, the standard OOB write also works for raw */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5702 | if (!ecc->read_oob_raw) |
| 5703 | ecc->read_oob_raw = ecc->read_oob; |
| 5704 | if (!ecc->write_oob_raw) |
| 5705 | ecc->write_oob_raw = ecc->write_oob; |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 5706 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5707 | /* propagate ecc info to mtd_info */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5708 | mtd->ecc_strength = ecc->strength; |
| 5709 | mtd->ecc_step_size = ecc->size; |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5710 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 5711 | /* |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5712 | * 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] | 5713 | * mode. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5714 | */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5715 | ecc->steps = mtd->writesize / ecc->size; |
| 5716 | if (ecc->steps * ecc->size != mtd->writesize) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5717 | WARN(1, "Invalid ECC parameters\n"); |
| 5718 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5719 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5720 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5721 | ecc->total = ecc->steps * ecc->bytes; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 5722 | if (ecc->total > mtd->oobsize) { |
| 5723 | WARN(1, "Total number of ECC bytes exceeded oobsize\n"); |
| 5724 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5725 | goto err_nand_manuf_cleanup; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 5726 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5727 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5728 | /* |
| 5729 | * The number of bytes available for a client to place data into |
| 5730 | * the out of band area. |
| 5731 | */ |
| 5732 | ret = mtd_ooblayout_count_freebytes(mtd); |
| 5733 | if (ret < 0) |
| 5734 | ret = 0; |
| 5735 | |
| 5736 | mtd->oobavail = ret; |
| 5737 | |
| 5738 | /* ECC sanity check: warn if it's too weak */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 5739 | if (!nand_ecc_strength_good(chip)) |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5740 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 5741 | mtd->name); |
| 5742 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5743 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 5744 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5745 | switch (ecc->steps) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 5746 | case 2: |
| 5747 | mtd->subpage_sft = 1; |
| 5748 | break; |
| 5749 | case 4: |
| 5750 | case 8: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5751 | case 16: |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 5752 | mtd->subpage_sft = 2; |
| 5753 | break; |
| 5754 | } |
| 5755 | } |
| 5756 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 5757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | /* Invalidate the pagebuffer reference */ |
Boris Brezillon | d974541 | 2018-10-28 16:12:45 +0100 | [diff] [blame] | 5759 | chip->pagecache.page = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5760 | |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 5761 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 5762 | switch (ecc->mode) { |
| 5763 | case NAND_ECC_SOFT: |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 5764 | if (chip->page_shift > 9) |
| 5765 | chip->options |= NAND_SUBPAGE_READ; |
| 5766 | break; |
| 5767 | |
| 5768 | default: |
| 5769 | break; |
| 5770 | } |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 5771 | |
Boris Brezillon | a7ab085 | 2018-10-25 22:10:36 +0200 | [diff] [blame] | 5772 | ret = nanddev_init(&chip->base, &rawnand_ops, mtd->owner); |
| 5773 | if (ret) |
| 5774 | goto err_nand_manuf_cleanup; |
| 5775 | |
| 5776 | /* Adjust the MTD_CAP_ flags when NAND_ROM is set. */ |
| 5777 | if (chip->options & NAND_ROM) |
| 5778 | mtd->flags = MTD_CAP_ROM; |
| 5779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5780 | /* Fill in remaining MTD driver data */ |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5781 | mtd->_erase = nand_erase; |
| 5782 | mtd->_point = NULL; |
| 5783 | mtd->_unpoint = NULL; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5784 | mtd->_panic_write = panic_nand_write; |
| 5785 | mtd->_read_oob = nand_read_oob; |
| 5786 | mtd->_write_oob = nand_write_oob; |
| 5787 | mtd->_sync = nand_sync; |
| 5788 | mtd->_lock = NULL; |
| 5789 | mtd->_unlock = NULL; |
| 5790 | mtd->_suspend = nand_suspend; |
| 5791 | mtd->_resume = nand_resume; |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 5792 | mtd->_reboot = nand_shutdown; |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 5793 | mtd->_block_isreserved = nand_block_isreserved; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5794 | mtd->_block_isbad = nand_block_isbad; |
| 5795 | mtd->_block_markbad = nand_block_markbad; |
Boris Brezillon | 7beb37e | 2018-11-04 14:50:28 +0100 | [diff] [blame] | 5796 | mtd->_max_bad_blocks = nanddev_mtd_max_bad_blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | |
Shmulik Ladkani | ea3b2ea | 2012-06-08 18:29:06 +0300 | [diff] [blame] | 5798 | /* |
| 5799 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 5800 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 5801 | * properly set. |
| 5802 | */ |
| 5803 | if (!mtd->bitflip_threshold) |
Brian Norris | 240181f | 2015-01-12 12:51:29 -0800 | [diff] [blame] | 5804 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5806 | /* Initialize the ->data_interface field. */ |
| 5807 | ret = nand_init_data_interface(chip); |
| 5808 | if (ret) |
Boris Brezillon | a7ab085 | 2018-10-25 22:10:36 +0200 | [diff] [blame] | 5809 | goto err_nanddev_cleanup; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5810 | |
| 5811 | /* Enter fastest possible mode on all dies. */ |
Boris Brezillon | 32813e2 | 2018-10-29 11:58:29 +0100 | [diff] [blame] | 5812 | for (i = 0; i < nanddev_ntargets(&chip->base); i++) { |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5813 | ret = nand_setup_data_interface(chip, i); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5814 | if (ret) |
Boris Brezillon | a7ab085 | 2018-10-25 22:10:36 +0200 | [diff] [blame] | 5815 | goto err_nanddev_cleanup; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5816 | } |
| 5817 | |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 5818 | /* Check, if we should skip the bad block table scan */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5819 | if (chip->options & NAND_SKIP_BBTSCAN) |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 5820 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5821 | |
| 5822 | /* Build bad block table */ |
Boris Brezillon | e80eba7 | 2018-07-05 12:27:31 +0200 | [diff] [blame] | 5823 | ret = nand_create_bbt(chip); |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 5824 | if (ret) |
Boris Brezillon | a7ab085 | 2018-10-25 22:10:36 +0200 | [diff] [blame] | 5825 | goto err_nanddev_cleanup; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5826 | |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 5827 | return 0; |
| 5828 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5829 | |
Boris Brezillon | a7ab085 | 2018-10-25 22:10:36 +0200 | [diff] [blame] | 5830 | err_nanddev_cleanup: |
| 5831 | nanddev_cleanup(&chip->base); |
| 5832 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5833 | err_nand_manuf_cleanup: |
| 5834 | nand_manufacturer_cleanup(chip); |
| 5835 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5836 | err_free_buf: |
| 5837 | kfree(chip->data_buf); |
| 5838 | kfree(ecc->code_buf); |
| 5839 | kfree(ecc->calc_buf); |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5840 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5841 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5842 | } |
| 5843 | |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5844 | static int nand_attach(struct nand_chip *chip) |
| 5845 | { |
| 5846 | if (chip->controller->ops && chip->controller->ops->attach_chip) |
| 5847 | return chip->controller->ops->attach_chip(chip); |
| 5848 | |
| 5849 | return 0; |
| 5850 | } |
| 5851 | |
| 5852 | static void nand_detach(struct nand_chip *chip) |
| 5853 | { |
| 5854 | if (chip->controller->ops && chip->controller->ops->detach_chip) |
| 5855 | chip->controller->ops->detach_chip(chip); |
| 5856 | } |
| 5857 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5858 | /** |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5859 | * nand_scan_with_ids - [NAND Interface] Scan for the NAND device |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5860 | * @chip: NAND chip object |
Boris Brezillon | 800342d | 2018-08-04 22:59:23 +0200 | [diff] [blame] | 5861 | * @maxchips: number of chips to scan for. |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5862 | * @ids: optional flash IDs table |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5863 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5864 | * This fills out all the uninitialized function pointers with the defaults. |
| 5865 | * 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] | 5866 | * appropriate values. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5867 | */ |
Boris Brezillon | 871a407 | 2018-08-04 22:59:22 +0200 | [diff] [blame] | 5868 | int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips, |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5869 | struct nand_flash_dev *ids) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5870 | { |
| 5871 | int ret; |
| 5872 | |
Boris Brezillon | 800342d | 2018-08-04 22:59:23 +0200 | [diff] [blame] | 5873 | if (!maxchips) |
| 5874 | return -EINVAL; |
| 5875 | |
| 5876 | ret = nand_scan_ident(chip, maxchips, ids); |
| 5877 | if (ret) |
| 5878 | return ret; |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5879 | |
| 5880 | ret = nand_attach(chip); |
| 5881 | if (ret) |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5882 | goto cleanup_ident; |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5883 | |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5884 | ret = nand_scan_tail(chip); |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5885 | if (ret) |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5886 | goto detach_chip; |
| 5887 | |
| 5888 | return 0; |
| 5889 | |
| 5890 | detach_chip: |
| 5891 | nand_detach(chip); |
| 5892 | cleanup_ident: |
| 5893 | nand_scan_ident_cleanup(chip); |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5894 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5895 | return ret; |
| 5896 | } |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5897 | EXPORT_SYMBOL(nand_scan_with_ids); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5899 | /** |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5900 | * nand_cleanup - [NAND Interface] Free resources held by the NAND device |
| 5901 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5902 | */ |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5903 | void nand_cleanup(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5904 | { |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5905 | if (chip->ecc.mode == NAND_ECC_SOFT && |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5906 | chip->ecc.algo == NAND_ECC_BCH) |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 5907 | nand_bch_free((struct nand_bch_control *)chip->ecc.priv); |
| 5908 | |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 5909 | /* Free bad block table memory */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5910 | kfree(chip->bbt); |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5911 | kfree(chip->data_buf); |
| 5912 | kfree(chip->ecc.code_buf); |
| 5913 | kfree(chip->ecc.calc_buf); |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 5914 | |
| 5915 | /* Free bad block descriptor memory */ |
| 5916 | if (chip->badblock_pattern && chip->badblock_pattern->options |
| 5917 | & NAND_BBT_DYNAMICSTRUCT) |
| 5918 | kfree(chip->badblock_pattern); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5919 | |
| 5920 | /* Free manufacturer priv data. */ |
| 5921 | nand_manufacturer_cleanup(chip); |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5922 | |
| 5923 | /* Free controller specific allocations after chip identification */ |
| 5924 | nand_detach(chip); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5925 | |
| 5926 | /* Free identification phase allocations */ |
| 5927 | nand_scan_ident_cleanup(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5928 | } |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5929 | |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5930 | EXPORT_SYMBOL_GPL(nand_cleanup); |
| 5931 | |
| 5932 | /** |
| 5933 | * nand_release - [NAND Interface] Unregister the MTD device and free resources |
| 5934 | * held by the NAND device |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 5935 | * @chip: NAND chip object |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5936 | */ |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 5937 | void nand_release(struct nand_chip *chip) |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5938 | { |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 5939 | mtd_device_unregister(nand_to_mtd(chip)); |
| 5940 | nand_cleanup(chip); |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5941 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5942 | EXPORT_SYMBOL_GPL(nand_release); |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 5943 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5944 | MODULE_LICENSE("GPL"); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5945 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 5946 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5947 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |