Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Overview: |
| 3 | * This is the generic MTD driver for NAND flash devices. It should be |
| 4 | * capable of working with almost all NAND chips currently available. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Additional technical information is available on |
maximilian attems | 8b2b403 | 2007-07-28 13:07:16 +0200 | [diff] [blame] | 7 | * http://www.linux-mtd.infradead.org/doc/nand.html |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 10 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 12 | * Credits: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 13 | * David Woodhouse for adding multichip support |
| 14 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
| 16 | * rework for 2K page size chips |
| 17 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 18 | * TODO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * Enable cached programming for 2k page size chips |
| 20 | * Check, if mtd->ecctype should be set to MTD_ECC_HW |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 21 | * if we have HW ECC support. |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 22 | * BBT table is not serialized, has to be fixed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * This program is free software; you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License version 2 as |
| 26 | * published by the Free Software Foundation. |
| 27 | * |
| 28 | */ |
| 29 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 31 | |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 32 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/delay.h> |
| 34 | #include <linux/errno.h> |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 35 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/sched.h> |
| 37 | #include <linux/slab.h> |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 38 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/types.h> |
| 40 | #include <linux/mtd/mtd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/mtd/nand_ecc.h> |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 42 | #include <linux/mtd/nand_bch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/interrupt.h> |
| 44 | #include <linux/bitops.h> |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 45 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/mtd/partitions.h> |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 47 | #include <linux/of.h> |
Janusz Krzysztofik | b0e137a | 2018-10-15 21:41:28 +0200 | [diff] [blame] | 48 | #include <linux/gpio/consumer.h> |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 49 | |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 50 | #include "internals.h" |
| 51 | |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 52 | /* Define default oob placement schemes for large and small page devices */ |
| 53 | static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section, |
| 54 | struct mtd_oob_region *oobregion) |
| 55 | { |
| 56 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 57 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 58 | |
| 59 | if (section > 1) |
| 60 | return -ERANGE; |
| 61 | |
| 62 | if (!section) { |
| 63 | oobregion->offset = 0; |
Miquel Raynal | f7f8c17 | 2017-07-05 08:51:09 +0200 | [diff] [blame] | 64 | if (mtd->oobsize == 16) |
| 65 | oobregion->length = 4; |
| 66 | else |
| 67 | oobregion->length = 3; |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 68 | } else { |
Miquel Raynal | f7f8c17 | 2017-07-05 08:51:09 +0200 | [diff] [blame] | 69 | if (mtd->oobsize == 8) |
| 70 | return -ERANGE; |
| 71 | |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 72 | oobregion->offset = 6; |
| 73 | oobregion->length = ecc->total - 4; |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section, |
| 80 | struct mtd_oob_region *oobregion) |
| 81 | { |
| 82 | if (section > 1) |
| 83 | return -ERANGE; |
| 84 | |
| 85 | if (mtd->oobsize == 16) { |
| 86 | if (section) |
| 87 | return -ERANGE; |
| 88 | |
| 89 | oobregion->length = 8; |
| 90 | oobregion->offset = 8; |
| 91 | } else { |
| 92 | oobregion->length = 2; |
| 93 | if (!section) |
| 94 | oobregion->offset = 3; |
| 95 | else |
| 96 | oobregion->offset = 6; |
| 97 | } |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = { |
| 103 | .ecc = nand_ooblayout_ecc_sp, |
| 104 | .free = nand_ooblayout_free_sp, |
| 105 | }; |
| 106 | EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops); |
| 107 | |
| 108 | static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section, |
| 109 | struct mtd_oob_region *oobregion) |
| 110 | { |
| 111 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 112 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 113 | |
Miquel Raynal | 882fd15 | 2017-08-26 17:19:15 +0200 | [diff] [blame] | 114 | if (section || !ecc->total) |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 115 | return -ERANGE; |
| 116 | |
| 117 | oobregion->length = ecc->total; |
| 118 | oobregion->offset = mtd->oobsize - oobregion->length; |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section, |
| 124 | struct mtd_oob_region *oobregion) |
| 125 | { |
| 126 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 127 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 128 | |
| 129 | if (section) |
| 130 | return -ERANGE; |
| 131 | |
| 132 | oobregion->length = mtd->oobsize - ecc->total - 2; |
| 133 | oobregion->offset = 2; |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = { |
| 139 | .ecc = nand_ooblayout_ecc_lp, |
| 140 | .free = nand_ooblayout_free_lp, |
| 141 | }; |
| 142 | EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops); |
Thomas Gleixner | d470a97 | 2006-05-23 23:48:57 +0200 | [diff] [blame] | 143 | |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 144 | /* |
| 145 | * Support the old "large page" layout used for 1-bit Hamming ECC where ECC |
| 146 | * are placed at a fixed offset. |
| 147 | */ |
| 148 | static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section, |
| 149 | struct mtd_oob_region *oobregion) |
| 150 | { |
| 151 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 152 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 153 | |
| 154 | if (section) |
| 155 | return -ERANGE; |
| 156 | |
| 157 | switch (mtd->oobsize) { |
| 158 | case 64: |
| 159 | oobregion->offset = 40; |
| 160 | break; |
| 161 | case 128: |
| 162 | oobregion->offset = 80; |
| 163 | break; |
| 164 | default: |
| 165 | return -EINVAL; |
| 166 | } |
| 167 | |
| 168 | oobregion->length = ecc->total; |
| 169 | if (oobregion->offset + oobregion->length > mtd->oobsize) |
| 170 | return -ERANGE; |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section, |
| 176 | struct mtd_oob_region *oobregion) |
| 177 | { |
| 178 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 179 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 180 | int ecc_offset = 0; |
| 181 | |
| 182 | if (section < 0 || section > 1) |
| 183 | return -ERANGE; |
| 184 | |
| 185 | switch (mtd->oobsize) { |
| 186 | case 64: |
| 187 | ecc_offset = 40; |
| 188 | break; |
| 189 | case 128: |
| 190 | ecc_offset = 80; |
| 191 | break; |
| 192 | default: |
| 193 | return -EINVAL; |
| 194 | } |
| 195 | |
| 196 | if (section == 0) { |
| 197 | oobregion->offset = 2; |
| 198 | oobregion->length = ecc_offset - 2; |
| 199 | } else { |
| 200 | oobregion->offset = ecc_offset + ecc->total; |
| 201 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
Colin Ian King | d4ed3b9 | 2017-05-04 13:11:00 +0100 | [diff] [blame] | 207 | static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = { |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 208 | .ecc = nand_ooblayout_ecc_lp_hamming, |
| 209 | .free = nand_ooblayout_free_lp_hamming, |
| 210 | }; |
| 211 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 212 | 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] | 213 | { |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 214 | int ret = 0; |
| 215 | |
| 216 | /* Start address must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 217 | if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 218 | pr_debug("%s: unaligned address\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 219 | ret = -EINVAL; |
| 220 | } |
| 221 | |
| 222 | /* Length must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 223 | if (len & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 224 | pr_debug("%s: length not block aligned\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 225 | ret = -EINVAL; |
| 226 | } |
| 227 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 228 | return ret; |
| 229 | } |
| 230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | /** |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 232 | * nand_select_target() - Select a NAND target (A.K.A. die) |
| 233 | * @chip: NAND chip object |
| 234 | * @cs: the CS line to select. Note that this CS id is always from the chip |
| 235 | * PoV, not the controller one |
| 236 | * |
| 237 | * Select a NAND target so that further operations executed on @chip go to the |
| 238 | * selected NAND target. |
| 239 | */ |
| 240 | void nand_select_target(struct nand_chip *chip, unsigned int cs) |
| 241 | { |
| 242 | /* |
| 243 | * cs should always lie between 0 and chip->numchips, when that's not |
| 244 | * the case it's a bug and the caller should be fixed. |
| 245 | */ |
| 246 | if (WARN_ON(cs > chip->numchips)) |
| 247 | return; |
| 248 | |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 249 | chip->cur_cs = cs; |
Boris Brezillon | 02b4a52 | 2018-11-11 08:55:16 +0100 | [diff] [blame] | 250 | |
Boris Brezillon | 7d6c37e | 2018-11-11 08:55:22 +0100 | [diff] [blame] | 251 | if (chip->legacy.select_chip) |
| 252 | chip->legacy.select_chip(chip, cs); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 253 | } |
| 254 | EXPORT_SYMBOL_GPL(nand_select_target); |
| 255 | |
| 256 | /** |
| 257 | * nand_deselect_target() - Deselect the currently selected target |
| 258 | * @chip: NAND chip object |
| 259 | * |
| 260 | * Deselect the currently selected NAND target. The result of operations |
| 261 | * executed on @chip after the target has been deselected is undefined. |
| 262 | */ |
| 263 | void nand_deselect_target(struct nand_chip *chip) |
| 264 | { |
Boris Brezillon | 7d6c37e | 2018-11-11 08:55:22 +0100 | [diff] [blame] | 265 | if (chip->legacy.select_chip) |
| 266 | chip->legacy.select_chip(chip, -1); |
Boris Brezillon | 02b4a52 | 2018-11-11 08:55:16 +0100 | [diff] [blame] | 267 | |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 268 | chip->cur_cs = -1; |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 269 | } |
| 270 | EXPORT_SYMBOL_GPL(nand_deselect_target); |
| 271 | |
| 272 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | * nand_release_device - [GENERIC] release chip |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 274 | * @chip: NAND chip object |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 275 | * |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 276 | * Release chip lock and wake up anyone waiting on the device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 278 | static void nand_release_device(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 280 | /* Release the controller and the chip */ |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 281 | mutex_unlock(&chip->controller->lock); |
| 282 | mutex_unlock(&chip->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 287 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 288 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 290 | * Check, if the block is bad. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | */ |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 292 | static int nand_block_bad(struct nand_chip *chip, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 294 | struct mtd_info *mtd = nand_to_mtd(chip); |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 295 | int page, page_end, res; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 296 | u8 bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Brian Norris | 5fb1549 | 2011-05-31 16:31:21 -0700 | [diff] [blame] | 298 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 299 | ofs += mtd->erasesize - mtd->writesize; |
| 300 | |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 301 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 302 | page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1); |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 303 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 304 | for (; page < page_end; page++) { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 305 | res = chip->ecc.read_oob(chip, page); |
Abhishek Sahu | e9893e6 | 2018-06-13 14:32:36 +0530 | [diff] [blame] | 306 | if (res < 0) |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 307 | return res; |
| 308 | |
| 309 | bad = chip->oob_poi[chip->badblockpos]; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 310 | |
Brian Norris | cdbec05 | 2012-01-13 18:11:48 -0800 | [diff] [blame] | 311 | if (likely(chip->badblockbits == 8)) |
| 312 | res = bad != 0xFF; |
| 313 | else |
| 314 | res = hweight8(bad) < chip->badblockbits; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 315 | if (res) |
| 316 | return res; |
| 317 | } |
Maxim Levitsky | e0b58d0 | 2010-02-22 20:39:38 +0200 | [diff] [blame] | 318 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 319 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 322 | static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs) |
| 323 | { |
| 324 | if (chip->legacy.block_bad) |
| 325 | return chip->legacy.block_bad(chip, ofs); |
| 326 | |
| 327 | return nand_block_bad(chip, ofs); |
| 328 | } |
| 329 | |
| 330 | /** |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 331 | * nand_get_device - [GENERIC] Get chip for selected access |
| 332 | * @chip: NAND chip structure |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 333 | * |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 334 | * Lock the device and its controller for exclusive access |
| 335 | * |
| 336 | * Return: -EBUSY if the chip has been suspended, 0 otherwise |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 337 | */ |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 338 | static int nand_get_device(struct nand_chip *chip) |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 339 | { |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 340 | mutex_lock(&chip->lock); |
| 341 | if (chip->suspended) { |
| 342 | mutex_unlock(&chip->lock); |
| 343 | return -EBUSY; |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 344 | } |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 345 | mutex_lock(&chip->controller->lock); |
| 346 | |
| 347 | return 0; |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /** |
| 351 | * nand_check_wp - [GENERIC] check if the chip is write protected |
| 352 | * @chip: NAND chip object |
| 353 | * |
| 354 | * Check, if the device is write protected. The function expects, that the |
| 355 | * device is already selected. |
| 356 | */ |
| 357 | static int nand_check_wp(struct nand_chip *chip) |
| 358 | { |
| 359 | u8 status; |
| 360 | int ret; |
| 361 | |
| 362 | /* Broken xD cards report WP despite being writable */ |
| 363 | if (chip->options & NAND_BROKEN_XD) |
| 364 | return 0; |
| 365 | |
| 366 | /* Check the WP bit */ |
| 367 | ret = nand_status_op(chip, &status); |
| 368 | if (ret) |
| 369 | return ret; |
| 370 | |
| 371 | return status & NAND_STATUS_WP ? 0 : 1; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
| 376 | * @oob: oob data buffer |
| 377 | * @len: oob data write length |
| 378 | * @ops: oob ops structure |
| 379 | */ |
| 380 | static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len, |
| 381 | struct mtd_oob_ops *ops) |
| 382 | { |
| 383 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 384 | int ret; |
| 385 | |
| 386 | /* |
| 387 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 388 | * data from a previous OOB read. |
| 389 | */ |
| 390 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 391 | |
| 392 | switch (ops->mode) { |
| 393 | |
| 394 | case MTD_OPS_PLACE_OOB: |
| 395 | case MTD_OPS_RAW: |
| 396 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 397 | return oob + len; |
| 398 | |
| 399 | case MTD_OPS_AUTO_OOB: |
| 400 | ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi, |
| 401 | ops->ooboffs, len); |
| 402 | BUG_ON(ret); |
| 403 | return oob + len; |
| 404 | |
| 405 | default: |
| 406 | BUG(); |
| 407 | } |
| 408 | return NULL; |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
| 413 | * @chip: NAND chip object |
| 414 | * @to: offset to write to |
| 415 | * @ops: oob operation description structure |
| 416 | * |
| 417 | * NAND write out-of-band. |
| 418 | */ |
| 419 | static int nand_do_write_oob(struct nand_chip *chip, loff_t to, |
| 420 | struct mtd_oob_ops *ops) |
| 421 | { |
| 422 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 423 | int chipnr, page, status, len; |
| 424 | |
| 425 | pr_debug("%s: to = 0x%08x, len = %i\n", |
| 426 | __func__, (unsigned int)to, (int)ops->ooblen); |
| 427 | |
| 428 | len = mtd_oobavail(mtd, ops); |
| 429 | |
| 430 | /* Do not allow write past end of page */ |
| 431 | if ((ops->ooboffs + ops->ooblen) > len) { |
| 432 | pr_debug("%s: attempt to write past end of page\n", |
| 433 | __func__); |
| 434 | return -EINVAL; |
| 435 | } |
| 436 | |
| 437 | chipnr = (int)(to >> chip->chip_shift); |
| 438 | |
| 439 | /* |
| 440 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 441 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 442 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 443 | * it in the doc2000 driver in August 1999. dwmw2. |
| 444 | */ |
| 445 | nand_reset(chip, chipnr); |
| 446 | |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 447 | nand_select_target(chip, chipnr); |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 448 | |
| 449 | /* Shift to get page */ |
| 450 | page = (int)(to >> chip->page_shift); |
| 451 | |
| 452 | /* Check, if it is write protected */ |
| 453 | if (nand_check_wp(chip)) { |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 454 | nand_deselect_target(chip); |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 455 | return -EROFS; |
| 456 | } |
| 457 | |
| 458 | /* Invalidate the page cache, if we write to the cached page */ |
| 459 | if (page == chip->pagebuf) |
| 460 | chip->pagebuf = -1; |
| 461 | |
| 462 | nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops); |
| 463 | |
| 464 | if (ops->mode == MTD_OPS_RAW) |
| 465 | status = chip->ecc.write_oob_raw(chip, page & chip->pagemask); |
| 466 | else |
| 467 | status = chip->ecc.write_oob(chip, page & chip->pagemask); |
| 468 | |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 469 | nand_deselect_target(chip); |
Boris Brezillon | 99f3351 | 2018-11-11 08:55:04 +0100 | [diff] [blame] | 470 | |
| 471 | if (status) |
| 472 | return status; |
| 473 | |
| 474 | ops->oobretlen = ops->ooblen; |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | /** |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 480 | * 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] | 481 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 482 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 484 | * This is the default implementation, which can be overridden by a hardware |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 485 | * specific driver. It provides the details for writing a bad block marker to a |
| 486 | * block. |
| 487 | */ |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 488 | 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] | 489 | { |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 490 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 491 | struct mtd_oob_ops ops; |
| 492 | uint8_t buf[2] = { 0, 0 }; |
| 493 | int ret = 0, res, i = 0; |
| 494 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 495 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 496 | ops.oobbuf = buf; |
| 497 | ops.ooboffs = chip->badblockpos; |
| 498 | if (chip->options & NAND_BUSWIDTH_16) { |
| 499 | ops.ooboffs &= ~0x01; |
| 500 | ops.len = ops.ooblen = 2; |
| 501 | } else { |
| 502 | ops.len = ops.ooblen = 1; |
| 503 | } |
| 504 | ops.mode = MTD_OPS_PLACE_OOB; |
| 505 | |
| 506 | /* Write to first/last page(s) if necessary */ |
| 507 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 508 | ofs += mtd->erasesize - mtd->writesize; |
| 509 | do { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 510 | res = nand_do_write_oob(chip, ofs, &ops); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 511 | if (!ret) |
| 512 | ret = res; |
| 513 | |
| 514 | i++; |
| 515 | ofs += mtd->writesize; |
| 516 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
| 517 | |
| 518 | return ret; |
| 519 | } |
| 520 | |
| 521 | /** |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 522 | * nand_markbad_bbm - mark a block by updating the BBM |
| 523 | * @chip: NAND chip object |
| 524 | * @ofs: offset of the block to mark bad |
| 525 | */ |
| 526 | int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs) |
| 527 | { |
| 528 | if (chip->legacy.block_markbad) |
| 529 | return chip->legacy.block_markbad(chip, ofs); |
| 530 | |
| 531 | return nand_default_block_markbad(chip, ofs); |
| 532 | } |
| 533 | |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 534 | /** |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 535 | * nand_block_markbad_lowlevel - mark a block bad |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 536 | * @chip: NAND chip object |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 537 | * @ofs: offset from device start |
| 538 | * |
| 539 | * This function performs the generic NAND bad block marking steps (i.e., bad |
| 540 | * 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] | 541 | * 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] | 542 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 543 | * We try operations in the following order: |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 544 | * |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 545 | * (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] | 546 | * (2) write bad block marker to OOB area of affected block (unless flag |
| 547 | * NAND_BBT_NO_OOB_BBM is present) |
| 548 | * (3) update the BBT |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 549 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 550 | * 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] | 551 | * procedures, and dump the error in the end. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 553 | 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] | 554 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 555 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 556 | int res, ret = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 557 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 558 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 559 | struct erase_info einfo; |
| 560 | |
| 561 | /* Attempt erase before marking OOB */ |
| 562 | memset(&einfo, 0, sizeof(einfo)); |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 563 | einfo.addr = ofs; |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 564 | einfo.len = 1ULL << chip->phys_erase_shift; |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 565 | nand_erase_nand(chip, &einfo, 0); |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 566 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 567 | /* Write bad block marker to OOB */ |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 568 | ret = nand_get_device(chip); |
| 569 | if (ret) |
| 570 | return ret; |
| 571 | |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 572 | ret = nand_markbad_bbm(chip, ofs); |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 573 | nand_release_device(chip); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 574 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 575 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 576 | /* Mark block bad in BBT */ |
| 577 | if (chip->bbt) { |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 578 | res = nand_markbad_bbt(chip, ofs); |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 579 | if (!ret) |
| 580 | ret = res; |
| 581 | } |
| 582 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 583 | if (!ret) |
| 584 | mtd->ecc_stats.badblocks++; |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 585 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 586 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 589 | /** |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 590 | * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 591 | * @mtd: MTD device structure |
| 592 | * @ofs: offset from device start |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 593 | * |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 594 | * Check if the block is marked as reserved. |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 595 | */ |
| 596 | static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 597 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 598 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 599 | |
| 600 | if (!chip->bbt) |
| 601 | return 0; |
| 602 | /* Return info from the table */ |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 603 | return nand_isreserved_bbt(chip, ofs); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /** |
| 607 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 608 | * @chip: NAND chip object |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 609 | * @ofs: offset from device start |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 610 | * @allowbbt: 1, if its allowed to access the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | * |
| 612 | * Check, if the block is bad. Either by reading the bad block table or |
| 613 | * calling of the scan function. |
| 614 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 615 | 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] | 616 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | /* Return info from the table */ |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 618 | if (chip->bbt) |
| 619 | return nand_isbad_bbt(chip, ofs, allowbbt); |
| 620 | |
| 621 | return nand_isbad_bbm(chip, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 624 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 625 | * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1 |
| 626 | * @chip: NAND chip structure |
| 627 | * @timeout_ms: Timeout in ms |
| 628 | * |
| 629 | * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1. |
| 630 | * If that does not happen whitin the specified timeout, -ETIMEDOUT is |
| 631 | * returned. |
| 632 | * |
| 633 | * This helper is intended to be used when the controller does not have access |
| 634 | * to the NAND R/B pin. |
| 635 | * |
| 636 | * Be aware that calling this helper from an ->exec_op() implementation means |
| 637 | * ->exec_op() must be re-entrant. |
| 638 | * |
| 639 | * Return 0 if the NAND chip is ready, a negative error otherwise. |
| 640 | */ |
| 641 | int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms) |
| 642 | { |
Boris Brezillon | 3057fce | 2018-05-04 21:24:31 +0200 | [diff] [blame] | 643 | const struct nand_sdr_timings *timings; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 644 | u8 status = 0; |
| 645 | int ret; |
| 646 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 647 | if (!nand_has_exec_op(chip)) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 648 | return -ENOTSUPP; |
| 649 | |
Boris Brezillon | 3057fce | 2018-05-04 21:24:31 +0200 | [diff] [blame] | 650 | /* Wait tWB before polling the STATUS reg. */ |
| 651 | timings = nand_get_sdr_timings(&chip->data_interface); |
| 652 | ndelay(PSEC_TO_NSEC(timings->tWB_max)); |
| 653 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 654 | ret = nand_status_op(chip, NULL); |
| 655 | if (ret) |
| 656 | return ret; |
| 657 | |
| 658 | timeout_ms = jiffies + msecs_to_jiffies(timeout_ms); |
| 659 | do { |
| 660 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 661 | if (ret) |
| 662 | break; |
| 663 | |
| 664 | if (status & NAND_STATUS_READY) |
| 665 | break; |
| 666 | |
| 667 | /* |
| 668 | * Typical lowest execution time for a tR on most NANDs is 10us, |
| 669 | * use this as polling delay before doing something smarter (ie. |
| 670 | * deriving a delay from the timeout value, timeout_ms/ratio). |
| 671 | */ |
| 672 | udelay(10); |
| 673 | } while (time_before(jiffies, timeout_ms)); |
| 674 | |
| 675 | /* |
| 676 | * We have to exit READ_STATUS mode in order to read real data on the |
| 677 | * bus in case the WAITRDY instruction is preceding a DATA_IN |
| 678 | * instruction. |
| 679 | */ |
| 680 | nand_exit_status_op(chip); |
| 681 | |
| 682 | if (ret) |
| 683 | return ret; |
| 684 | |
| 685 | return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT; |
| 686 | }; |
| 687 | EXPORT_SYMBOL_GPL(nand_soft_waitrdy); |
| 688 | |
| 689 | /** |
Janusz Krzysztofik | b0e137a | 2018-10-15 21:41:28 +0200 | [diff] [blame] | 690 | * nand_gpio_waitrdy - Poll R/B GPIO pin until ready |
| 691 | * @chip: NAND chip structure |
| 692 | * @gpiod: GPIO descriptor of R/B pin |
| 693 | * @timeout_ms: Timeout in ms |
| 694 | * |
| 695 | * Poll the R/B GPIO pin until it becomes ready. If that does not happen |
| 696 | * whitin the specified timeout, -ETIMEDOUT is returned. |
| 697 | * |
| 698 | * This helper is intended to be used when the controller has access to the |
| 699 | * NAND R/B pin over GPIO. |
| 700 | * |
| 701 | * Return 0 if the R/B pin indicates chip is ready, a negative error otherwise. |
| 702 | */ |
| 703 | int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod, |
| 704 | unsigned long timeout_ms) |
| 705 | { |
| 706 | /* Wait until R/B pin indicates chip is ready or timeout occurs */ |
| 707 | timeout_ms = jiffies + msecs_to_jiffies(timeout_ms); |
| 708 | do { |
| 709 | if (gpiod_get_value_cansleep(gpiod)) |
| 710 | return 0; |
| 711 | |
| 712 | cond_resched(); |
| 713 | } while (time_before(jiffies, timeout_ms)); |
| 714 | |
| 715 | return gpiod_get_value_cansleep(gpiod) ? 0 : -ETIMEDOUT; |
| 716 | }; |
| 717 | EXPORT_SYMBOL_GPL(nand_gpio_waitrdy); |
| 718 | |
| 719 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 720 | * panic_nand_wait - [GENERIC] wait until the command is done |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 721 | * @chip: NAND chip structure |
| 722 | * @timeo: timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 723 | * |
| 724 | * Wait for command done. This is a helper function for nand_wait used when |
| 725 | * 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] | 726 | * an oops through mtdoops. |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 727 | */ |
Boris Brezillon | 3d4af7c | 2018-09-07 00:38:49 +0200 | [diff] [blame] | 728 | void panic_nand_wait(struct nand_chip *chip, unsigned long timeo) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 729 | { |
| 730 | int i; |
| 731 | for (i = 0; i < timeo; i++) { |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 732 | if (chip->legacy.dev_ready) { |
| 733 | if (chip->legacy.dev_ready(chip)) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 734 | break; |
| 735 | } else { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 736 | int ret; |
| 737 | u8 status; |
| 738 | |
| 739 | ret = nand_read_data_op(chip, &status, sizeof(status), |
| 740 | true); |
| 741 | if (ret) |
| 742 | return; |
| 743 | |
| 744 | if (status & NAND_STATUS_READY) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 745 | break; |
| 746 | } |
| 747 | mdelay(1); |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 748 | } |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 749 | } |
| 750 | |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 751 | static bool nand_supports_get_features(struct nand_chip *chip, int addr) |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 752 | { |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 753 | return (chip->parameters.supports_set_get_features && |
| 754 | test_bit(addr, chip->parameters.get_feature_list)); |
| 755 | } |
| 756 | |
| 757 | static bool nand_supports_set_features(struct nand_chip *chip, int addr) |
| 758 | { |
| 759 | return (chip->parameters.supports_set_get_features && |
| 760 | test_bit(addr, chip->parameters.set_feature_list)); |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | /** |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 764 | * nand_reset_data_interface - Reset data interface and timings |
| 765 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 766 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 767 | * |
| 768 | * Reset the Data interface and timings to ONFI mode 0. |
| 769 | * |
| 770 | * Returns 0 for success or negative error code otherwise. |
| 771 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 772 | static int nand_reset_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 773 | { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 774 | int ret; |
| 775 | |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 776 | if (!nand_has_setup_data_iface(chip)) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 777 | return 0; |
| 778 | |
| 779 | /* |
| 780 | * The ONFI specification says: |
| 781 | * " |
| 782 | * To transition from NV-DDR or NV-DDR2 to the SDR data |
| 783 | * interface, the host shall use the Reset (FFh) command |
| 784 | * using SDR timing mode 0. A device in any timing mode is |
| 785 | * required to recognize Reset (FFh) command issued in SDR |
| 786 | * timing mode 0. |
| 787 | * " |
| 788 | * |
| 789 | * Configure the data interface in SDR mode and set the |
| 790 | * timings to timing mode 0. |
| 791 | */ |
| 792 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 793 | onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0); |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 794 | ret = chip->controller->ops->setup_data_interface(chip, chipnr, |
| 795 | &chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 796 | if (ret) |
| 797 | pr_err("Failed to configure data interface to SDR timing mode 0\n"); |
| 798 | |
| 799 | return ret; |
| 800 | } |
| 801 | |
| 802 | /** |
| 803 | * nand_setup_data_interface - Setup the best data interface and timings |
| 804 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 805 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 806 | * |
| 807 | * Find and configure the best data interface and NAND timings supported by |
| 808 | * the chip and the driver. |
| 809 | * First tries to retrieve supported timing modes from ONFI information, |
| 810 | * and if the NAND chip does not support ONFI, relies on the |
| 811 | * ->onfi_timing_mode_default specified in the nand_ids table. |
| 812 | * |
| 813 | * Returns 0 for success or negative error code otherwise. |
| 814 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 815 | static int nand_setup_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 816 | { |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 817 | u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { |
| 818 | chip->onfi_timing_mode_default, |
| 819 | }; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 820 | int ret; |
| 821 | |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 822 | if (!nand_has_setup_data_iface(chip)) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 823 | return 0; |
| 824 | |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 825 | /* 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] | 826 | if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) { |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 827 | nand_select_target(chip, chipnr); |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 828 | ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE, |
| 829 | tmode_param); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 830 | nand_deselect_target(chip); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 831 | if (ret) |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 832 | return ret; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 833 | } |
| 834 | |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 835 | /* Change the mode on the controller side */ |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 836 | ret = chip->controller->ops->setup_data_interface(chip, chipnr, |
| 837 | &chip->data_interface); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 838 | if (ret) |
| 839 | return ret; |
| 840 | |
| 841 | /* Check the mode has been accepted by the chip, if supported */ |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 842 | if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 843 | return 0; |
| 844 | |
| 845 | memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 846 | nand_select_target(chip, chipnr); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 847 | ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE, |
| 848 | tmode_param); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 849 | nand_deselect_target(chip); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 850 | if (ret) |
| 851 | goto err_reset_chip; |
| 852 | |
| 853 | if (tmode_param[0] != chip->onfi_timing_mode_default) { |
| 854 | pr_warn("timing mode %d not acknowledged by the NAND chip\n", |
| 855 | chip->onfi_timing_mode_default); |
| 856 | goto err_reset_chip; |
| 857 | } |
| 858 | |
| 859 | return 0; |
| 860 | |
| 861 | err_reset_chip: |
| 862 | /* |
| 863 | * Fallback to mode 0 if the chip explicitly did not ack the chosen |
| 864 | * timing mode. |
| 865 | */ |
| 866 | nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 867 | nand_select_target(chip, chipnr); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 868 | nand_reset_op(chip); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 869 | nand_deselect_target(chip); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 870 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 871 | return ret; |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * nand_init_data_interface - find the best data interface and timings |
| 876 | * @chip: The NAND chip |
| 877 | * |
| 878 | * Find the best data interface and NAND timings supported by the chip |
| 879 | * and the driver. |
| 880 | * First tries to retrieve supported timing modes from ONFI information, |
| 881 | * and if the NAND chip does not support ONFI, relies on the |
| 882 | * ->onfi_timing_mode_default specified in the nand_ids table. After this |
| 883 | * function nand_chip->data_interface is initialized with the best timing mode |
| 884 | * available. |
| 885 | * |
| 886 | * Returns 0 for success or negative error code otherwise. |
| 887 | */ |
| 888 | static int nand_init_data_interface(struct nand_chip *chip) |
| 889 | { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 890 | int modes, mode, ret; |
| 891 | |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 892 | if (!nand_has_setup_data_iface(chip)) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 893 | return 0; |
| 894 | |
| 895 | /* |
| 896 | * First try to identify the best timings from ONFI parameters and |
| 897 | * if the NAND does not support ONFI, fallback to the default ONFI |
| 898 | * timing mode. |
| 899 | */ |
Boris Brezillon | 462f35d | 2018-09-07 00:38:47 +0200 | [diff] [blame] | 900 | if (chip->parameters.onfi) { |
| 901 | modes = chip->parameters.onfi->async_timing_mode; |
| 902 | } else { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 903 | if (!chip->onfi_timing_mode_default) |
| 904 | return 0; |
| 905 | |
| 906 | modes = GENMASK(chip->onfi_timing_mode_default, 0); |
| 907 | } |
| 908 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 909 | for (mode = fls(modes) - 1; mode >= 0; mode--) { |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 910 | ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 911 | if (ret) |
| 912 | continue; |
| 913 | |
Miquel Raynal | d787b8b | 2017-12-22 18:12:41 +0100 | [diff] [blame] | 914 | /* |
| 915 | * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the |
| 916 | * controller supports the requested timings. |
| 917 | */ |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 918 | ret = chip->controller->ops->setup_data_interface(chip, |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 919 | NAND_DATA_IFACE_CHECK_ONLY, |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 920 | &chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 921 | if (!ret) { |
| 922 | chip->onfi_timing_mode_default = mode; |
| 923 | break; |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | return 0; |
| 928 | } |
| 929 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 930 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 931 | * nand_fill_column_cycles - fill the column cycles of an address |
| 932 | * @chip: The NAND chip |
| 933 | * @addrs: Array of address cycles to fill |
| 934 | * @offset_in_page: The offset in the page |
| 935 | * |
| 936 | * Fills the first or the first two bytes of the @addrs field depending |
| 937 | * on the NAND bus width and the page size. |
| 938 | * |
| 939 | * Returns the number of cycles needed to encode the column, or a negative |
| 940 | * error code in case one of the arguments is invalid. |
| 941 | */ |
| 942 | static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs, |
| 943 | unsigned int offset_in_page) |
Matthieu CASTET | f251b8d | 2012-11-05 15:00:44 +0100 | [diff] [blame] | 944 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 945 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 946 | |
| 947 | /* Make sure the offset is less than the actual page size. */ |
| 948 | if (offset_in_page > mtd->writesize + mtd->oobsize) |
| 949 | return -EINVAL; |
| 950 | |
| 951 | /* |
| 952 | * On small page NANDs, there's a dedicated command to access the OOB |
| 953 | * area, and the column address is relative to the start of the OOB |
| 954 | * area, not the start of the page. Asjust the address accordingly. |
| 955 | */ |
| 956 | if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize) |
| 957 | offset_in_page -= mtd->writesize; |
| 958 | |
| 959 | /* |
| 960 | * The offset in page is expressed in bytes, if the NAND bus is 16-bit |
| 961 | * wide, then it must be divided by 2. |
| 962 | */ |
| 963 | if (chip->options & NAND_BUSWIDTH_16) { |
| 964 | if (WARN_ON(offset_in_page % 2)) |
| 965 | return -EINVAL; |
| 966 | |
| 967 | offset_in_page /= 2; |
| 968 | } |
| 969 | |
| 970 | addrs[0] = offset_in_page; |
| 971 | |
| 972 | /* |
| 973 | * Small page NANDs use 1 cycle for the columns, while large page NANDs |
| 974 | * need 2 |
| 975 | */ |
| 976 | if (mtd->writesize <= 512) |
| 977 | return 1; |
| 978 | |
| 979 | addrs[1] = offset_in_page >> 8; |
| 980 | |
| 981 | return 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | } |
| 983 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 984 | static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page, |
| 985 | unsigned int offset_in_page, void *buf, |
| 986 | unsigned int len) |
| 987 | { |
| 988 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 989 | const struct nand_sdr_timings *sdr = |
| 990 | nand_get_sdr_timings(&chip->data_interface); |
| 991 | u8 addrs[4]; |
| 992 | struct nand_op_instr instrs[] = { |
| 993 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 994 | NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)), |
| 995 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 996 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 997 | NAND_OP_DATA_IN(len, buf, 0), |
| 998 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 999 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1000 | int ret; |
| 1001 | |
| 1002 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1003 | if (!len) |
| 1004 | op.ninstrs--; |
| 1005 | |
| 1006 | if (offset_in_page >= mtd->writesize) |
| 1007 | instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB; |
| 1008 | else if (offset_in_page >= 256 && |
| 1009 | !(chip->options & NAND_BUSWIDTH_16)) |
| 1010 | instrs[0].ctx.cmd.opcode = NAND_CMD_READ1; |
| 1011 | |
| 1012 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1013 | if (ret < 0) |
| 1014 | return ret; |
| 1015 | |
| 1016 | addrs[1] = page; |
| 1017 | addrs[2] = page >> 8; |
| 1018 | |
| 1019 | if (chip->options & NAND_ROW_ADDR_3) { |
| 1020 | addrs[3] = page >> 16; |
| 1021 | instrs[1].ctx.addr.naddrs++; |
| 1022 | } |
| 1023 | |
| 1024 | return nand_exec_op(chip, &op); |
| 1025 | } |
| 1026 | |
| 1027 | static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1028 | unsigned int offset_in_page, void *buf, |
| 1029 | unsigned int len) |
| 1030 | { |
| 1031 | const struct nand_sdr_timings *sdr = |
| 1032 | nand_get_sdr_timings(&chip->data_interface); |
| 1033 | u8 addrs[5]; |
| 1034 | struct nand_op_instr instrs[] = { |
| 1035 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1036 | NAND_OP_ADDR(4, addrs, 0), |
| 1037 | NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1038 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 1039 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1040 | NAND_OP_DATA_IN(len, buf, 0), |
| 1041 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1042 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1043 | int ret; |
| 1044 | |
| 1045 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1046 | if (!len) |
| 1047 | op.ninstrs--; |
| 1048 | |
| 1049 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1050 | if (ret < 0) |
| 1051 | return ret; |
| 1052 | |
| 1053 | addrs[2] = page; |
| 1054 | addrs[3] = page >> 8; |
| 1055 | |
| 1056 | if (chip->options & NAND_ROW_ADDR_3) { |
| 1057 | addrs[4] = page >> 16; |
| 1058 | instrs[1].ctx.addr.naddrs++; |
| 1059 | } |
| 1060 | |
| 1061 | return nand_exec_op(chip, &op); |
| 1062 | } |
| 1063 | |
| 1064 | /** |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1065 | * nand_read_page_op - Do a READ PAGE operation |
| 1066 | * @chip: The NAND chip |
| 1067 | * @page: page to read |
| 1068 | * @offset_in_page: offset within the page |
| 1069 | * @buf: buffer used to store the data |
| 1070 | * @len: length of the buffer |
| 1071 | * |
| 1072 | * This function issues a READ PAGE operation. |
| 1073 | * This function does not select/unselect the CS line. |
| 1074 | * |
| 1075 | * Returns 0 on success, a negative error code otherwise. |
| 1076 | */ |
| 1077 | int nand_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1078 | unsigned int offset_in_page, void *buf, unsigned int len) |
| 1079 | { |
| 1080 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1081 | |
| 1082 | if (len && !buf) |
| 1083 | return -EINVAL; |
| 1084 | |
| 1085 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1086 | return -EINVAL; |
| 1087 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1088 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1089 | if (mtd->writesize > 512) |
| 1090 | return nand_lp_exec_read_page_op(chip, page, |
| 1091 | offset_in_page, buf, |
| 1092 | len); |
| 1093 | |
| 1094 | return nand_sp_exec_read_page_op(chip, page, offset_in_page, |
| 1095 | buf, len); |
| 1096 | } |
| 1097 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1098 | chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1099 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1100 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1101 | |
| 1102 | return 0; |
| 1103 | } |
| 1104 | EXPORT_SYMBOL_GPL(nand_read_page_op); |
| 1105 | |
| 1106 | /** |
| 1107 | * nand_read_param_page_op - Do a READ PARAMETER PAGE operation |
| 1108 | * @chip: The NAND chip |
| 1109 | * @page: parameter page to read |
| 1110 | * @buf: buffer used to store the data |
| 1111 | * @len: length of the buffer |
| 1112 | * |
| 1113 | * This function issues a READ PARAMETER PAGE operation. |
| 1114 | * This function does not select/unselect the CS line. |
| 1115 | * |
| 1116 | * Returns 0 on success, a negative error code otherwise. |
| 1117 | */ |
Boris Brezillon | 1c325cc | 2018-09-07 00:38:50 +0200 | [diff] [blame] | 1118 | int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf, |
| 1119 | unsigned int len) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1120 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1121 | unsigned int i; |
| 1122 | u8 *p = buf; |
| 1123 | |
| 1124 | if (len && !buf) |
| 1125 | return -EINVAL; |
| 1126 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1127 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1128 | const struct nand_sdr_timings *sdr = |
| 1129 | nand_get_sdr_timings(&chip->data_interface); |
| 1130 | struct nand_op_instr instrs[] = { |
| 1131 | NAND_OP_CMD(NAND_CMD_PARAM, 0), |
| 1132 | NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1133 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 1134 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1135 | NAND_OP_8BIT_DATA_IN(len, buf, 0), |
| 1136 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1137 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1138 | |
| 1139 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1140 | if (!len) |
| 1141 | op.ninstrs--; |
| 1142 | |
| 1143 | return nand_exec_op(chip, &op); |
| 1144 | } |
| 1145 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1146 | chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1147 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1148 | p[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1149 | |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * nand_change_read_column_op - Do a CHANGE READ COLUMN operation |
| 1155 | * @chip: The NAND chip |
| 1156 | * @offset_in_page: offset within the page |
| 1157 | * @buf: buffer used to store the data |
| 1158 | * @len: length of the buffer |
| 1159 | * @force_8bit: force 8-bit bus access |
| 1160 | * |
| 1161 | * This function issues a CHANGE READ COLUMN operation. |
| 1162 | * This function does not select/unselect the CS line. |
| 1163 | * |
| 1164 | * Returns 0 on success, a negative error code otherwise. |
| 1165 | */ |
| 1166 | int nand_change_read_column_op(struct nand_chip *chip, |
| 1167 | unsigned int offset_in_page, void *buf, |
| 1168 | unsigned int len, bool force_8bit) |
| 1169 | { |
| 1170 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1171 | |
| 1172 | if (len && !buf) |
| 1173 | return -EINVAL; |
| 1174 | |
| 1175 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1176 | return -EINVAL; |
| 1177 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1178 | /* Small page NANDs do not support column change. */ |
| 1179 | if (mtd->writesize <= 512) |
| 1180 | return -ENOTSUPP; |
| 1181 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1182 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1183 | const struct nand_sdr_timings *sdr = |
| 1184 | nand_get_sdr_timings(&chip->data_interface); |
| 1185 | u8 addrs[2] = {}; |
| 1186 | struct nand_op_instr instrs[] = { |
| 1187 | NAND_OP_CMD(NAND_CMD_RNDOUT, 0), |
| 1188 | NAND_OP_ADDR(2, addrs, 0), |
| 1189 | NAND_OP_CMD(NAND_CMD_RNDOUTSTART, |
| 1190 | PSEC_TO_NSEC(sdr->tCCS_min)), |
| 1191 | NAND_OP_DATA_IN(len, buf, 0), |
| 1192 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1193 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1194 | int ret; |
| 1195 | |
| 1196 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1197 | if (ret < 0) |
| 1198 | return ret; |
| 1199 | |
| 1200 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1201 | if (!len) |
| 1202 | op.ninstrs--; |
| 1203 | |
| 1204 | instrs[3].ctx.data.force_8bit = force_8bit; |
| 1205 | |
| 1206 | return nand_exec_op(chip, &op); |
| 1207 | } |
| 1208 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1209 | chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1210 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1211 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1212 | |
| 1213 | return 0; |
| 1214 | } |
| 1215 | EXPORT_SYMBOL_GPL(nand_change_read_column_op); |
| 1216 | |
| 1217 | /** |
| 1218 | * nand_read_oob_op - Do a READ OOB operation |
| 1219 | * @chip: The NAND chip |
| 1220 | * @page: page to read |
| 1221 | * @offset_in_oob: offset within the OOB area |
| 1222 | * @buf: buffer used to store the data |
| 1223 | * @len: length of the buffer |
| 1224 | * |
| 1225 | * This function issues a READ OOB operation. |
| 1226 | * This function does not select/unselect the CS line. |
| 1227 | * |
| 1228 | * Returns 0 on success, a negative error code otherwise. |
| 1229 | */ |
| 1230 | int nand_read_oob_op(struct nand_chip *chip, unsigned int page, |
| 1231 | unsigned int offset_in_oob, void *buf, unsigned int len) |
| 1232 | { |
| 1233 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1234 | |
| 1235 | if (len && !buf) |
| 1236 | return -EINVAL; |
| 1237 | |
| 1238 | if (offset_in_oob + len > mtd->oobsize) |
| 1239 | return -EINVAL; |
| 1240 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1241 | if (nand_has_exec_op(chip)) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1242 | return nand_read_page_op(chip, page, |
| 1243 | mtd->writesize + offset_in_oob, |
| 1244 | buf, len); |
| 1245 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1246 | chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1247 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1248 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1249 | |
| 1250 | return 0; |
| 1251 | } |
| 1252 | EXPORT_SYMBOL_GPL(nand_read_oob_op); |
| 1253 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1254 | static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1255 | unsigned int offset_in_page, const void *buf, |
| 1256 | unsigned int len, bool prog) |
| 1257 | { |
| 1258 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1259 | const struct nand_sdr_timings *sdr = |
| 1260 | nand_get_sdr_timings(&chip->data_interface); |
| 1261 | u8 addrs[5] = {}; |
| 1262 | struct nand_op_instr instrs[] = { |
| 1263 | /* |
| 1264 | * The first instruction will be dropped if we're dealing |
| 1265 | * with a large page NAND and adjusted if we're dealing |
| 1266 | * with a small page NAND and the page offset is > 255. |
| 1267 | */ |
| 1268 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1269 | NAND_OP_CMD(NAND_CMD_SEQIN, 0), |
| 1270 | NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1271 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1272 | NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1273 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0), |
| 1274 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1275 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1276 | int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1277 | int ret; |
| 1278 | u8 status; |
| 1279 | |
| 1280 | if (naddrs < 0) |
| 1281 | return naddrs; |
| 1282 | |
| 1283 | addrs[naddrs++] = page; |
| 1284 | addrs[naddrs++] = page >> 8; |
| 1285 | if (chip->options & NAND_ROW_ADDR_3) |
| 1286 | addrs[naddrs++] = page >> 16; |
| 1287 | |
| 1288 | instrs[2].ctx.addr.naddrs = naddrs; |
| 1289 | |
| 1290 | /* Drop the last two instructions if we're not programming the page. */ |
| 1291 | if (!prog) { |
| 1292 | op.ninstrs -= 2; |
| 1293 | /* Also drop the DATA_OUT instruction if empty. */ |
| 1294 | if (!len) |
| 1295 | op.ninstrs--; |
| 1296 | } |
| 1297 | |
| 1298 | if (mtd->writesize <= 512) { |
| 1299 | /* |
| 1300 | * Small pages need some more tweaking: we have to adjust the |
| 1301 | * first instruction depending on the page offset we're trying |
| 1302 | * to access. |
| 1303 | */ |
| 1304 | if (offset_in_page >= mtd->writesize) |
| 1305 | instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB; |
| 1306 | else if (offset_in_page >= 256 && |
| 1307 | !(chip->options & NAND_BUSWIDTH_16)) |
| 1308 | instrs[0].ctx.cmd.opcode = NAND_CMD_READ1; |
| 1309 | } else { |
| 1310 | /* |
| 1311 | * Drop the first command if we're dealing with a large page |
| 1312 | * NAND. |
| 1313 | */ |
| 1314 | op.instrs++; |
| 1315 | op.ninstrs--; |
| 1316 | } |
| 1317 | |
| 1318 | ret = nand_exec_op(chip, &op); |
| 1319 | if (!prog || ret) |
| 1320 | return ret; |
| 1321 | |
| 1322 | ret = nand_status_op(chip, &status); |
| 1323 | if (ret) |
| 1324 | return ret; |
| 1325 | |
| 1326 | return status; |
| 1327 | } |
| 1328 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1329 | /** |
| 1330 | * nand_prog_page_begin_op - starts a PROG PAGE operation |
| 1331 | * @chip: The NAND chip |
| 1332 | * @page: page to write |
| 1333 | * @offset_in_page: offset within the page |
| 1334 | * @buf: buffer containing the data to write to the page |
| 1335 | * @len: length of the buffer |
| 1336 | * |
| 1337 | * This function issues the first half of a PROG PAGE operation. |
| 1338 | * This function does not select/unselect the CS line. |
| 1339 | * |
| 1340 | * Returns 0 on success, a negative error code otherwise. |
| 1341 | */ |
| 1342 | int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page, |
| 1343 | unsigned int offset_in_page, const void *buf, |
| 1344 | unsigned int len) |
| 1345 | { |
| 1346 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1347 | |
| 1348 | if (len && !buf) |
| 1349 | return -EINVAL; |
| 1350 | |
| 1351 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1352 | return -EINVAL; |
| 1353 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1354 | if (nand_has_exec_op(chip)) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1355 | return nand_exec_prog_page_op(chip, page, offset_in_page, buf, |
| 1356 | len, false); |
| 1357 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1358 | chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1359 | |
| 1360 | if (buf) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1361 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1362 | |
| 1363 | return 0; |
| 1364 | } |
| 1365 | EXPORT_SYMBOL_GPL(nand_prog_page_begin_op); |
| 1366 | |
| 1367 | /** |
| 1368 | * nand_prog_page_end_op - ends a PROG PAGE operation |
| 1369 | * @chip: The NAND chip |
| 1370 | * |
| 1371 | * This function issues the second half of a PROG PAGE operation. |
| 1372 | * This function does not select/unselect the CS line. |
| 1373 | * |
| 1374 | * Returns 0 on success, a negative error code otherwise. |
| 1375 | */ |
| 1376 | int nand_prog_page_end_op(struct nand_chip *chip) |
| 1377 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1378 | int ret; |
| 1379 | u8 status; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1380 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1381 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1382 | const struct nand_sdr_timings *sdr = |
| 1383 | nand_get_sdr_timings(&chip->data_interface); |
| 1384 | struct nand_op_instr instrs[] = { |
| 1385 | NAND_OP_CMD(NAND_CMD_PAGEPROG, |
| 1386 | PSEC_TO_NSEC(sdr->tWB_max)), |
| 1387 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0), |
| 1388 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1389 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1390 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1391 | ret = nand_exec_op(chip, &op); |
| 1392 | if (ret) |
| 1393 | return ret; |
| 1394 | |
| 1395 | ret = nand_status_op(chip, &status); |
| 1396 | if (ret) |
| 1397 | return ret; |
| 1398 | } else { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1399 | chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1); |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1400 | ret = chip->legacy.waitfunc(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1401 | if (ret < 0) |
| 1402 | return ret; |
| 1403 | |
| 1404 | status = ret; |
| 1405 | } |
| 1406 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1407 | if (status & NAND_STATUS_FAIL) |
| 1408 | return -EIO; |
| 1409 | |
| 1410 | return 0; |
| 1411 | } |
| 1412 | EXPORT_SYMBOL_GPL(nand_prog_page_end_op); |
| 1413 | |
| 1414 | /** |
| 1415 | * nand_prog_page_op - Do a full PROG PAGE operation |
| 1416 | * @chip: The NAND chip |
| 1417 | * @page: page to write |
| 1418 | * @offset_in_page: offset within the page |
| 1419 | * @buf: buffer containing the data to write to the page |
| 1420 | * @len: length of the buffer |
| 1421 | * |
| 1422 | * This function issues a full PROG PAGE operation. |
| 1423 | * This function does not select/unselect the CS line. |
| 1424 | * |
| 1425 | * Returns 0 on success, a negative error code otherwise. |
| 1426 | */ |
| 1427 | int nand_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1428 | unsigned int offset_in_page, const void *buf, |
| 1429 | unsigned int len) |
| 1430 | { |
| 1431 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1432 | int status; |
| 1433 | |
| 1434 | if (!len || !buf) |
| 1435 | return -EINVAL; |
| 1436 | |
| 1437 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1438 | return -EINVAL; |
| 1439 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1440 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1441 | status = nand_exec_prog_page_op(chip, page, offset_in_page, buf, |
| 1442 | len, true); |
| 1443 | } else { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1444 | chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, |
| 1445 | page); |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1446 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1447 | chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1); |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1448 | status = chip->legacy.waitfunc(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1449 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1450 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1451 | if (status & NAND_STATUS_FAIL) |
| 1452 | return -EIO; |
| 1453 | |
| 1454 | return 0; |
| 1455 | } |
| 1456 | EXPORT_SYMBOL_GPL(nand_prog_page_op); |
| 1457 | |
| 1458 | /** |
| 1459 | * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation |
| 1460 | * @chip: The NAND chip |
| 1461 | * @offset_in_page: offset within the page |
| 1462 | * @buf: buffer containing the data to send to the NAND |
| 1463 | * @len: length of the buffer |
| 1464 | * @force_8bit: force 8-bit bus access |
| 1465 | * |
| 1466 | * This function issues a CHANGE WRITE COLUMN operation. |
| 1467 | * This function does not select/unselect the CS line. |
| 1468 | * |
| 1469 | * Returns 0 on success, a negative error code otherwise. |
| 1470 | */ |
| 1471 | int nand_change_write_column_op(struct nand_chip *chip, |
| 1472 | unsigned int offset_in_page, |
| 1473 | const void *buf, unsigned int len, |
| 1474 | bool force_8bit) |
| 1475 | { |
| 1476 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1477 | |
| 1478 | if (len && !buf) |
| 1479 | return -EINVAL; |
| 1480 | |
| 1481 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1482 | return -EINVAL; |
| 1483 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1484 | /* Small page NANDs do not support column change. */ |
| 1485 | if (mtd->writesize <= 512) |
| 1486 | return -ENOTSUPP; |
| 1487 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1488 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1489 | const struct nand_sdr_timings *sdr = |
| 1490 | nand_get_sdr_timings(&chip->data_interface); |
| 1491 | u8 addrs[2]; |
| 1492 | struct nand_op_instr instrs[] = { |
| 1493 | NAND_OP_CMD(NAND_CMD_RNDIN, 0), |
| 1494 | NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)), |
| 1495 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1496 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1497 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1498 | int ret; |
| 1499 | |
| 1500 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1501 | if (ret < 0) |
| 1502 | return ret; |
| 1503 | |
| 1504 | instrs[2].ctx.data.force_8bit = force_8bit; |
| 1505 | |
| 1506 | /* Drop the DATA_OUT instruction if len is set to 0. */ |
| 1507 | if (!len) |
| 1508 | op.ninstrs--; |
| 1509 | |
| 1510 | return nand_exec_op(chip, &op); |
| 1511 | } |
| 1512 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1513 | chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1514 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1515 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1516 | |
| 1517 | return 0; |
| 1518 | } |
| 1519 | EXPORT_SYMBOL_GPL(nand_change_write_column_op); |
| 1520 | |
| 1521 | /** |
| 1522 | * nand_readid_op - Do a READID operation |
| 1523 | * @chip: The NAND chip |
| 1524 | * @addr: address cycle to pass after the READID command |
| 1525 | * @buf: buffer used to store the ID |
| 1526 | * @len: length of the buffer |
| 1527 | * |
| 1528 | * This function sends a READID command and reads back the ID returned by the |
| 1529 | * NAND. |
| 1530 | * This function does not select/unselect the CS line. |
| 1531 | * |
| 1532 | * Returns 0 on success, a negative error code otherwise. |
| 1533 | */ |
| 1534 | int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf, |
| 1535 | unsigned int len) |
| 1536 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1537 | unsigned int i; |
| 1538 | u8 *id = buf; |
| 1539 | |
| 1540 | if (len && !buf) |
| 1541 | return -EINVAL; |
| 1542 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1543 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1544 | const struct nand_sdr_timings *sdr = |
| 1545 | nand_get_sdr_timings(&chip->data_interface); |
| 1546 | struct nand_op_instr instrs[] = { |
| 1547 | NAND_OP_CMD(NAND_CMD_READID, 0), |
| 1548 | NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1549 | NAND_OP_8BIT_DATA_IN(len, buf, 0), |
| 1550 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1551 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1552 | |
| 1553 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1554 | if (!len) |
| 1555 | op.ninstrs--; |
| 1556 | |
| 1557 | return nand_exec_op(chip, &op); |
| 1558 | } |
| 1559 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1560 | chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1561 | |
| 1562 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1563 | id[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1564 | |
| 1565 | return 0; |
| 1566 | } |
| 1567 | EXPORT_SYMBOL_GPL(nand_readid_op); |
| 1568 | |
| 1569 | /** |
| 1570 | * nand_status_op - Do a STATUS operation |
| 1571 | * @chip: The NAND chip |
| 1572 | * @status: out variable to store the NAND status |
| 1573 | * |
| 1574 | * This function sends a STATUS command and reads back the status returned by |
| 1575 | * the NAND. |
| 1576 | * This function does not select/unselect the CS line. |
| 1577 | * |
| 1578 | * Returns 0 on success, a negative error code otherwise. |
| 1579 | */ |
| 1580 | int nand_status_op(struct nand_chip *chip, u8 *status) |
| 1581 | { |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1582 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1583 | const struct nand_sdr_timings *sdr = |
| 1584 | nand_get_sdr_timings(&chip->data_interface); |
| 1585 | struct nand_op_instr instrs[] = { |
| 1586 | NAND_OP_CMD(NAND_CMD_STATUS, |
| 1587 | PSEC_TO_NSEC(sdr->tADL_min)), |
| 1588 | NAND_OP_8BIT_DATA_IN(1, status, 0), |
| 1589 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1590 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1591 | |
| 1592 | if (!status) |
| 1593 | op.ninstrs--; |
| 1594 | |
| 1595 | return nand_exec_op(chip, &op); |
| 1596 | } |
| 1597 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1598 | chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1599 | if (status) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1600 | *status = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1601 | |
| 1602 | return 0; |
| 1603 | } |
| 1604 | EXPORT_SYMBOL_GPL(nand_status_op); |
| 1605 | |
| 1606 | /** |
| 1607 | * nand_exit_status_op - Exit a STATUS operation |
| 1608 | * @chip: The NAND chip |
| 1609 | * |
| 1610 | * This function sends a READ0 command to cancel the effect of the STATUS |
| 1611 | * command to avoid reading only the status until a new read command is sent. |
| 1612 | * |
| 1613 | * This function does not select/unselect the CS line. |
| 1614 | * |
| 1615 | * Returns 0 on success, a negative error code otherwise. |
| 1616 | */ |
| 1617 | int nand_exit_status_op(struct nand_chip *chip) |
| 1618 | { |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1619 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1620 | struct nand_op_instr instrs[] = { |
| 1621 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1622 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1623 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1624 | |
| 1625 | return nand_exec_op(chip, &op); |
| 1626 | } |
| 1627 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1628 | chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1629 | |
| 1630 | return 0; |
| 1631 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1632 | |
| 1633 | /** |
| 1634 | * nand_erase_op - Do an erase operation |
| 1635 | * @chip: The NAND chip |
| 1636 | * @eraseblock: block to erase |
| 1637 | * |
| 1638 | * This function sends an ERASE command and waits for the NAND to be ready |
| 1639 | * before returning. |
| 1640 | * This function does not select/unselect the CS line. |
| 1641 | * |
| 1642 | * Returns 0 on success, a negative error code otherwise. |
| 1643 | */ |
| 1644 | int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock) |
| 1645 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1646 | unsigned int page = eraseblock << |
| 1647 | (chip->phys_erase_shift - chip->page_shift); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1648 | int ret; |
| 1649 | u8 status; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1650 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1651 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1652 | const struct nand_sdr_timings *sdr = |
| 1653 | nand_get_sdr_timings(&chip->data_interface); |
| 1654 | u8 addrs[3] = { page, page >> 8, page >> 16 }; |
| 1655 | struct nand_op_instr instrs[] = { |
| 1656 | NAND_OP_CMD(NAND_CMD_ERASE1, 0), |
| 1657 | NAND_OP_ADDR(2, addrs, 0), |
| 1658 | NAND_OP_CMD(NAND_CMD_ERASE2, |
| 1659 | PSEC_TO_MSEC(sdr->tWB_max)), |
| 1660 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0), |
| 1661 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1662 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1663 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1664 | if (chip->options & NAND_ROW_ADDR_3) |
| 1665 | instrs[1].ctx.addr.naddrs++; |
| 1666 | |
| 1667 | ret = nand_exec_op(chip, &op); |
| 1668 | if (ret) |
| 1669 | return ret; |
| 1670 | |
| 1671 | ret = nand_status_op(chip, &status); |
| 1672 | if (ret) |
| 1673 | return ret; |
| 1674 | } else { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1675 | chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page); |
| 1676 | chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1677 | |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1678 | ret = chip->legacy.waitfunc(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1679 | if (ret < 0) |
| 1680 | return ret; |
| 1681 | |
| 1682 | status = ret; |
| 1683 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1684 | |
| 1685 | if (status & NAND_STATUS_FAIL) |
| 1686 | return -EIO; |
| 1687 | |
| 1688 | return 0; |
| 1689 | } |
| 1690 | EXPORT_SYMBOL_GPL(nand_erase_op); |
| 1691 | |
| 1692 | /** |
| 1693 | * nand_set_features_op - Do a SET FEATURES operation |
| 1694 | * @chip: The NAND chip |
| 1695 | * @feature: feature id |
| 1696 | * @data: 4 bytes of data |
| 1697 | * |
| 1698 | * This function sends a SET FEATURES command and waits for the NAND to be |
| 1699 | * ready before returning. |
| 1700 | * This function does not select/unselect the CS line. |
| 1701 | * |
| 1702 | * Returns 0 on success, a negative error code otherwise. |
| 1703 | */ |
| 1704 | static int nand_set_features_op(struct nand_chip *chip, u8 feature, |
| 1705 | const void *data) |
| 1706 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1707 | const u8 *params = data; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1708 | int i, ret; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1709 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1710 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1711 | const struct nand_sdr_timings *sdr = |
| 1712 | nand_get_sdr_timings(&chip->data_interface); |
| 1713 | struct nand_op_instr instrs[] = { |
| 1714 | NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0), |
| 1715 | NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1716 | NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data, |
| 1717 | PSEC_TO_NSEC(sdr->tWB_max)), |
| 1718 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0), |
| 1719 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1720 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1721 | |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1722 | return nand_exec_op(chip, &op); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1723 | } |
| 1724 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1725 | chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1); |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1726 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1727 | chip->legacy.write_byte(chip, params[i]); |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1728 | |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1729 | ret = chip->legacy.waitfunc(chip); |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1730 | if (ret < 0) |
| 1731 | return ret; |
| 1732 | |
| 1733 | if (ret & NAND_STATUS_FAIL) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1734 | return -EIO; |
| 1735 | |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
| 1739 | /** |
| 1740 | * nand_get_features_op - Do a GET FEATURES operation |
| 1741 | * @chip: The NAND chip |
| 1742 | * @feature: feature id |
| 1743 | * @data: 4 bytes of data |
| 1744 | * |
| 1745 | * This function sends a GET FEATURES command and waits for the NAND to be |
| 1746 | * ready before returning. |
| 1747 | * This function does not select/unselect the CS line. |
| 1748 | * |
| 1749 | * Returns 0 on success, a negative error code otherwise. |
| 1750 | */ |
| 1751 | static int nand_get_features_op(struct nand_chip *chip, u8 feature, |
| 1752 | void *data) |
| 1753 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1754 | u8 *params = data; |
| 1755 | int i; |
| 1756 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1757 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1758 | const struct nand_sdr_timings *sdr = |
| 1759 | nand_get_sdr_timings(&chip->data_interface); |
| 1760 | struct nand_op_instr instrs[] = { |
| 1761 | NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0), |
| 1762 | NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1763 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), |
| 1764 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1765 | NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN, |
| 1766 | data, 0), |
| 1767 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1768 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1769 | |
| 1770 | return nand_exec_op(chip, &op); |
| 1771 | } |
| 1772 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1773 | chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1774 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1775 | params[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1776 | |
| 1777 | return 0; |
| 1778 | } |
| 1779 | |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1780 | static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms, |
| 1781 | unsigned int delay_ns) |
| 1782 | { |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1783 | if (nand_has_exec_op(chip)) { |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1784 | struct nand_op_instr instrs[] = { |
| 1785 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms), |
| 1786 | PSEC_TO_NSEC(delay_ns)), |
| 1787 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1788 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1789 | |
| 1790 | return nand_exec_op(chip, &op); |
| 1791 | } |
| 1792 | |
| 1793 | /* Apply delay or wait for ready/busy pin */ |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1794 | if (!chip->legacy.dev_ready) |
Boris Brezillon | 3cece3a | 2018-09-07 00:38:41 +0200 | [diff] [blame] | 1795 | udelay(chip->legacy.chip_delay); |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1796 | else |
Boris Brezillon | 2b356ab | 2018-09-06 14:05:16 +0200 | [diff] [blame] | 1797 | nand_wait_ready(chip); |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1798 | |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1802 | /** |
| 1803 | * nand_reset_op - Do a reset operation |
| 1804 | * @chip: The NAND chip |
| 1805 | * |
| 1806 | * This function sends a RESET command and waits for the NAND to be ready |
| 1807 | * before returning. |
| 1808 | * This function does not select/unselect the CS line. |
| 1809 | * |
| 1810 | * Returns 0 on success, a negative error code otherwise. |
| 1811 | */ |
| 1812 | int nand_reset_op(struct nand_chip *chip) |
| 1813 | { |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1814 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1815 | const struct nand_sdr_timings *sdr = |
| 1816 | nand_get_sdr_timings(&chip->data_interface); |
| 1817 | struct nand_op_instr instrs[] = { |
| 1818 | NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1819 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0), |
| 1820 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1821 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1822 | |
| 1823 | return nand_exec_op(chip, &op); |
| 1824 | } |
| 1825 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1826 | chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1827 | |
| 1828 | return 0; |
| 1829 | } |
| 1830 | EXPORT_SYMBOL_GPL(nand_reset_op); |
| 1831 | |
| 1832 | /** |
| 1833 | * nand_read_data_op - Read data from the NAND |
| 1834 | * @chip: The NAND chip |
| 1835 | * @buf: buffer used to store the data |
| 1836 | * @len: length of the buffer |
| 1837 | * @force_8bit: force 8-bit bus access |
| 1838 | * |
| 1839 | * This function does a raw data read on the bus. Usually used after launching |
| 1840 | * another NAND operation like nand_read_page_op(). |
| 1841 | * This function does not select/unselect the CS line. |
| 1842 | * |
| 1843 | * Returns 0 on success, a negative error code otherwise. |
| 1844 | */ |
| 1845 | int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len, |
| 1846 | bool force_8bit) |
| 1847 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1848 | if (!len || !buf) |
| 1849 | return -EINVAL; |
| 1850 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1851 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1852 | struct nand_op_instr instrs[] = { |
| 1853 | NAND_OP_DATA_IN(len, buf, 0), |
| 1854 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1855 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1856 | |
| 1857 | instrs[0].ctx.data.force_8bit = force_8bit; |
| 1858 | |
| 1859 | return nand_exec_op(chip, &op); |
| 1860 | } |
| 1861 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1862 | if (force_8bit) { |
| 1863 | u8 *p = buf; |
| 1864 | unsigned int i; |
| 1865 | |
| 1866 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1867 | p[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1868 | } else { |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1869 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | return 0; |
| 1873 | } |
| 1874 | EXPORT_SYMBOL_GPL(nand_read_data_op); |
| 1875 | |
| 1876 | /** |
| 1877 | * nand_write_data_op - Write data from the NAND |
| 1878 | * @chip: The NAND chip |
| 1879 | * @buf: buffer containing the data to send on the bus |
| 1880 | * @len: length of the buffer |
| 1881 | * @force_8bit: force 8-bit bus access |
| 1882 | * |
| 1883 | * This function does a raw data write on the bus. Usually used after launching |
| 1884 | * another NAND operation like nand_write_page_begin_op(). |
| 1885 | * This function does not select/unselect the CS line. |
| 1886 | * |
| 1887 | * Returns 0 on success, a negative error code otherwise. |
| 1888 | */ |
| 1889 | int nand_write_data_op(struct nand_chip *chip, const void *buf, |
| 1890 | unsigned int len, bool force_8bit) |
| 1891 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1892 | if (!len || !buf) |
| 1893 | return -EINVAL; |
| 1894 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 1895 | if (nand_has_exec_op(chip)) { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1896 | struct nand_op_instr instrs[] = { |
| 1897 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1898 | }; |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 1899 | struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1900 | |
| 1901 | instrs[0].ctx.data.force_8bit = force_8bit; |
| 1902 | |
| 1903 | return nand_exec_op(chip, &op); |
| 1904 | } |
| 1905 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1906 | if (force_8bit) { |
| 1907 | const u8 *p = buf; |
| 1908 | unsigned int i; |
| 1909 | |
| 1910 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1911 | chip->legacy.write_byte(chip, p[i]); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1912 | } else { |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1913 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | return 0; |
| 1917 | } |
| 1918 | EXPORT_SYMBOL_GPL(nand_write_data_op); |
| 1919 | |
| 1920 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1921 | * struct nand_op_parser_ctx - Context used by the parser |
| 1922 | * @instrs: array of all the instructions that must be addressed |
| 1923 | * @ninstrs: length of the @instrs array |
| 1924 | * @subop: Sub-operation to be passed to the NAND controller |
| 1925 | * |
| 1926 | * This structure is used by the core to split NAND operations into |
| 1927 | * sub-operations that can be handled by the NAND controller. |
| 1928 | */ |
| 1929 | struct nand_op_parser_ctx { |
| 1930 | const struct nand_op_instr *instrs; |
| 1931 | unsigned int ninstrs; |
| 1932 | struct nand_subop subop; |
| 1933 | }; |
| 1934 | |
| 1935 | /** |
| 1936 | * nand_op_parser_must_split_instr - Checks if an instruction must be split |
| 1937 | * @pat: the parser pattern element that matches @instr |
| 1938 | * @instr: pointer to the instruction to check |
| 1939 | * @start_offset: this is an in/out parameter. If @instr has already been |
| 1940 | * split, then @start_offset is the offset from which to start |
| 1941 | * (either an address cycle or an offset in the data buffer). |
| 1942 | * Conversely, if the function returns true (ie. instr must be |
| 1943 | * split), this parameter is updated to point to the first |
| 1944 | * data/address cycle that has not been taken care of. |
| 1945 | * |
| 1946 | * Some NAND controllers are limited and cannot send X address cycles with a |
| 1947 | * unique operation, or cannot read/write more than Y bytes at the same time. |
| 1948 | * In this case, split the instruction that does not fit in a single |
| 1949 | * controller-operation into two or more chunks. |
| 1950 | * |
| 1951 | * Returns true if the instruction must be split, false otherwise. |
| 1952 | * The @start_offset parameter is also updated to the offset at which the next |
| 1953 | * bundle of instruction must start (if an address or a data instruction). |
| 1954 | */ |
| 1955 | static bool |
| 1956 | nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat, |
| 1957 | const struct nand_op_instr *instr, |
| 1958 | unsigned int *start_offset) |
| 1959 | { |
| 1960 | switch (pat->type) { |
| 1961 | case NAND_OP_ADDR_INSTR: |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 1962 | if (!pat->ctx.addr.maxcycles) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1963 | break; |
| 1964 | |
| 1965 | if (instr->ctx.addr.naddrs - *start_offset > |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 1966 | pat->ctx.addr.maxcycles) { |
| 1967 | *start_offset += pat->ctx.addr.maxcycles; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1968 | return true; |
| 1969 | } |
| 1970 | break; |
| 1971 | |
| 1972 | case NAND_OP_DATA_IN_INSTR: |
| 1973 | case NAND_OP_DATA_OUT_INSTR: |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 1974 | if (!pat->ctx.data.maxlen) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1975 | break; |
| 1976 | |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 1977 | if (instr->ctx.data.len - *start_offset > |
| 1978 | pat->ctx.data.maxlen) { |
| 1979 | *start_offset += pat->ctx.data.maxlen; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1980 | return true; |
| 1981 | } |
| 1982 | break; |
| 1983 | |
| 1984 | default: |
| 1985 | break; |
| 1986 | } |
| 1987 | |
| 1988 | return false; |
| 1989 | } |
| 1990 | |
| 1991 | /** |
| 1992 | * nand_op_parser_match_pat - Checks if a pattern matches the instructions |
| 1993 | * remaining in the parser context |
| 1994 | * @pat: the pattern to test |
| 1995 | * @ctx: the parser context structure to match with the pattern @pat |
| 1996 | * |
| 1997 | * Check if @pat matches the set or a sub-set of instructions remaining in @ctx. |
| 1998 | * Returns true if this is the case, false ortherwise. When true is returned, |
| 1999 | * @ctx->subop is updated with the set of instructions to be passed to the |
| 2000 | * controller driver. |
| 2001 | */ |
| 2002 | static bool |
| 2003 | nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat, |
| 2004 | struct nand_op_parser_ctx *ctx) |
| 2005 | { |
| 2006 | unsigned int instr_offset = ctx->subop.first_instr_start_off; |
| 2007 | const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs; |
| 2008 | const struct nand_op_instr *instr = ctx->subop.instrs; |
| 2009 | unsigned int i, ninstrs; |
| 2010 | |
| 2011 | for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) { |
| 2012 | /* |
| 2013 | * The pattern instruction does not match the operation |
| 2014 | * instruction. If the instruction is marked optional in the |
| 2015 | * pattern definition, we skip the pattern element and continue |
| 2016 | * to the next one. If the element is mandatory, there's no |
| 2017 | * match and we can return false directly. |
| 2018 | */ |
| 2019 | if (instr->type != pat->elems[i].type) { |
| 2020 | if (!pat->elems[i].optional) |
| 2021 | return false; |
| 2022 | |
| 2023 | continue; |
| 2024 | } |
| 2025 | |
| 2026 | /* |
| 2027 | * Now check the pattern element constraints. If the pattern is |
| 2028 | * not able to handle the whole instruction in a single step, |
| 2029 | * we have to split it. |
| 2030 | * The last_instr_end_off value comes back updated to point to |
| 2031 | * the position where we have to split the instruction (the |
| 2032 | * start of the next subop chunk). |
| 2033 | */ |
| 2034 | if (nand_op_parser_must_split_instr(&pat->elems[i], instr, |
| 2035 | &instr_offset)) { |
| 2036 | ninstrs++; |
| 2037 | i++; |
| 2038 | break; |
| 2039 | } |
| 2040 | |
| 2041 | instr++; |
| 2042 | ninstrs++; |
| 2043 | instr_offset = 0; |
| 2044 | } |
| 2045 | |
| 2046 | /* |
| 2047 | * This can happen if all instructions of a pattern are optional. |
| 2048 | * Still, if there's not at least one instruction handled by this |
| 2049 | * pattern, this is not a match, and we should try the next one (if |
| 2050 | * any). |
| 2051 | */ |
| 2052 | if (!ninstrs) |
| 2053 | return false; |
| 2054 | |
| 2055 | /* |
| 2056 | * We had a match on the pattern head, but the pattern may be longer |
| 2057 | * than the instructions we're asked to execute. We need to make sure |
| 2058 | * there's no mandatory elements in the pattern tail. |
| 2059 | */ |
| 2060 | for (; i < pat->nelems; i++) { |
| 2061 | if (!pat->elems[i].optional) |
| 2062 | return false; |
| 2063 | } |
| 2064 | |
| 2065 | /* |
| 2066 | * We have a match: update the subop structure accordingly and return |
| 2067 | * true. |
| 2068 | */ |
| 2069 | ctx->subop.ninstrs = ninstrs; |
| 2070 | ctx->subop.last_instr_end_off = instr_offset; |
| 2071 | |
| 2072 | return true; |
| 2073 | } |
| 2074 | |
| 2075 | #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG) |
| 2076 | static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx) |
| 2077 | { |
| 2078 | const struct nand_op_instr *instr; |
| 2079 | char *prefix = " "; |
| 2080 | unsigned int i; |
| 2081 | |
| 2082 | pr_debug("executing subop:\n"); |
| 2083 | |
| 2084 | for (i = 0; i < ctx->ninstrs; i++) { |
| 2085 | instr = &ctx->instrs[i]; |
| 2086 | |
| 2087 | if (instr == &ctx->subop.instrs[0]) |
| 2088 | prefix = " ->"; |
| 2089 | |
| 2090 | switch (instr->type) { |
| 2091 | case NAND_OP_CMD_INSTR: |
| 2092 | pr_debug("%sCMD [0x%02x]\n", prefix, |
| 2093 | instr->ctx.cmd.opcode); |
| 2094 | break; |
| 2095 | case NAND_OP_ADDR_INSTR: |
| 2096 | pr_debug("%sADDR [%d cyc: %*ph]\n", prefix, |
| 2097 | instr->ctx.addr.naddrs, |
| 2098 | instr->ctx.addr.naddrs < 64 ? |
| 2099 | instr->ctx.addr.naddrs : 64, |
| 2100 | instr->ctx.addr.addrs); |
| 2101 | break; |
| 2102 | case NAND_OP_DATA_IN_INSTR: |
| 2103 | pr_debug("%sDATA_IN [%d B%s]\n", prefix, |
| 2104 | instr->ctx.data.len, |
| 2105 | instr->ctx.data.force_8bit ? |
| 2106 | ", force 8-bit" : ""); |
| 2107 | break; |
| 2108 | case NAND_OP_DATA_OUT_INSTR: |
| 2109 | pr_debug("%sDATA_OUT [%d B%s]\n", prefix, |
| 2110 | instr->ctx.data.len, |
| 2111 | instr->ctx.data.force_8bit ? |
| 2112 | ", force 8-bit" : ""); |
| 2113 | break; |
| 2114 | case NAND_OP_WAITRDY_INSTR: |
| 2115 | pr_debug("%sWAITRDY [max %d ms]\n", prefix, |
| 2116 | instr->ctx.waitrdy.timeout_ms); |
| 2117 | break; |
| 2118 | } |
| 2119 | |
| 2120 | if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1]) |
| 2121 | prefix = " "; |
| 2122 | } |
| 2123 | } |
| 2124 | #else |
| 2125 | static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx) |
| 2126 | { |
| 2127 | /* NOP */ |
| 2128 | } |
| 2129 | #endif |
| 2130 | |
| 2131 | /** |
| 2132 | * nand_op_parser_exec_op - exec_op parser |
| 2133 | * @chip: the NAND chip |
| 2134 | * @parser: patterns description provided by the controller driver |
| 2135 | * @op: the NAND operation to address |
| 2136 | * @check_only: when true, the function only checks if @op can be handled but |
| 2137 | * does not execute the operation |
| 2138 | * |
| 2139 | * Helper function designed to ease integration of NAND controller drivers that |
| 2140 | * only support a limited set of instruction sequences. The supported sequences |
| 2141 | * are described in @parser, and the framework takes care of splitting @op into |
| 2142 | * multiple sub-operations (if required) and pass them back to the ->exec() |
| 2143 | * callback of the matching pattern if @check_only is set to false. |
| 2144 | * |
| 2145 | * NAND controller drivers should call this function from their own ->exec_op() |
| 2146 | * implementation. |
| 2147 | * |
| 2148 | * Returns 0 on success, a negative error code otherwise. A failure can be |
| 2149 | * caused by an unsupported operation (none of the supported patterns is able |
| 2150 | * to handle the requested operation), or an error returned by one of the |
| 2151 | * matching pattern->exec() hook. |
| 2152 | */ |
| 2153 | int nand_op_parser_exec_op(struct nand_chip *chip, |
| 2154 | const struct nand_op_parser *parser, |
| 2155 | const struct nand_operation *op, bool check_only) |
| 2156 | { |
| 2157 | struct nand_op_parser_ctx ctx = { |
| 2158 | .subop.instrs = op->instrs, |
| 2159 | .instrs = op->instrs, |
| 2160 | .ninstrs = op->ninstrs, |
| 2161 | }; |
| 2162 | unsigned int i; |
| 2163 | |
| 2164 | while (ctx.subop.instrs < op->instrs + op->ninstrs) { |
| 2165 | int ret; |
| 2166 | |
| 2167 | for (i = 0; i < parser->npatterns; i++) { |
| 2168 | const struct nand_op_parser_pattern *pattern; |
| 2169 | |
| 2170 | pattern = &parser->patterns[i]; |
| 2171 | if (!nand_op_parser_match_pat(pattern, &ctx)) |
| 2172 | continue; |
| 2173 | |
| 2174 | nand_op_parser_trace(&ctx); |
| 2175 | |
| 2176 | if (check_only) |
| 2177 | break; |
| 2178 | |
| 2179 | ret = pattern->exec(chip, &ctx.subop); |
| 2180 | if (ret) |
| 2181 | return ret; |
| 2182 | |
| 2183 | break; |
| 2184 | } |
| 2185 | |
| 2186 | if (i == parser->npatterns) { |
| 2187 | pr_debug("->exec_op() parser: pattern not found!\n"); |
| 2188 | return -ENOTSUPP; |
| 2189 | } |
| 2190 | |
| 2191 | /* |
| 2192 | * Update the context structure by pointing to the start of the |
| 2193 | * next subop. |
| 2194 | */ |
| 2195 | ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs; |
| 2196 | if (ctx.subop.last_instr_end_off) |
| 2197 | ctx.subop.instrs -= 1; |
| 2198 | |
| 2199 | ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off; |
| 2200 | } |
| 2201 | |
| 2202 | return 0; |
| 2203 | } |
| 2204 | EXPORT_SYMBOL_GPL(nand_op_parser_exec_op); |
| 2205 | |
| 2206 | static bool nand_instr_is_data(const struct nand_op_instr *instr) |
| 2207 | { |
| 2208 | return instr && (instr->type == NAND_OP_DATA_IN_INSTR || |
| 2209 | instr->type == NAND_OP_DATA_OUT_INSTR); |
| 2210 | } |
| 2211 | |
| 2212 | static bool nand_subop_instr_is_valid(const struct nand_subop *subop, |
| 2213 | unsigned int instr_idx) |
| 2214 | { |
| 2215 | return subop && instr_idx < subop->ninstrs; |
| 2216 | } |
| 2217 | |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2218 | static unsigned int nand_subop_get_start_off(const struct nand_subop *subop, |
| 2219 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2220 | { |
| 2221 | if (instr_idx) |
| 2222 | return 0; |
| 2223 | |
| 2224 | return subop->first_instr_start_off; |
| 2225 | } |
| 2226 | |
| 2227 | /** |
| 2228 | * nand_subop_get_addr_start_off - Get the start offset in an address array |
| 2229 | * @subop: The entire sub-operation |
| 2230 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2231 | * |
| 2232 | * During driver development, one could be tempted to directly use the |
| 2233 | * ->addr.addrs field of address instructions. This is wrong as address |
| 2234 | * instructions might be split. |
| 2235 | * |
| 2236 | * Given an address instruction, returns the offset of the first cycle to issue. |
| 2237 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2238 | unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop, |
| 2239 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2240 | { |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2241 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2242 | subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)) |
| 2243 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2244 | |
| 2245 | return nand_subop_get_start_off(subop, instr_idx); |
| 2246 | } |
| 2247 | EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off); |
| 2248 | |
| 2249 | /** |
| 2250 | * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert |
| 2251 | * @subop: The entire sub-operation |
| 2252 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2253 | * |
| 2254 | * During driver development, one could be tempted to directly use the |
| 2255 | * ->addr->naddrs field of a data instruction. This is wrong as instructions |
| 2256 | * might be split. |
| 2257 | * |
| 2258 | * Given an address instruction, returns the number of address cycle to issue. |
| 2259 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2260 | unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop, |
| 2261 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2262 | { |
| 2263 | int start_off, end_off; |
| 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 | start_off = nand_subop_get_addr_start_off(subop, instr_idx); |
| 2270 | |
| 2271 | if (instr_idx == subop->ninstrs - 1 && |
| 2272 | subop->last_instr_end_off) |
| 2273 | end_off = subop->last_instr_end_off; |
| 2274 | else |
| 2275 | end_off = subop->instrs[instr_idx].ctx.addr.naddrs; |
| 2276 | |
| 2277 | return end_off - start_off; |
| 2278 | } |
| 2279 | EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc); |
| 2280 | |
| 2281 | /** |
| 2282 | * nand_subop_get_data_start_off - Get the start offset in a data array |
| 2283 | * @subop: The entire sub-operation |
| 2284 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2285 | * |
| 2286 | * During driver development, one could be tempted to directly use the |
| 2287 | * ->data->buf.{in,out} field of data instructions. This is wrong as data |
| 2288 | * instructions might be split. |
| 2289 | * |
| 2290 | * Given a data instruction, returns the offset to start from. |
| 2291 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2292 | unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop, |
| 2293 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2294 | { |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2295 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2296 | !nand_instr_is_data(&subop->instrs[instr_idx]))) |
| 2297 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2298 | |
| 2299 | return nand_subop_get_start_off(subop, instr_idx); |
| 2300 | } |
| 2301 | EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off); |
| 2302 | |
| 2303 | /** |
| 2304 | * nand_subop_get_data_len - Get the number of bytes to retrieve |
| 2305 | * @subop: The entire sub-operation |
| 2306 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2307 | * |
| 2308 | * During driver development, one could be tempted to directly use the |
| 2309 | * ->data->len field of a data instruction. This is wrong as data instructions |
| 2310 | * might be split. |
| 2311 | * |
| 2312 | * Returns the length of the chunk of data to send/receive. |
| 2313 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2314 | unsigned int nand_subop_get_data_len(const struct nand_subop *subop, |
| 2315 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2316 | { |
| 2317 | int start_off = 0, end_off; |
| 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 | start_off = nand_subop_get_data_start_off(subop, instr_idx); |
| 2324 | |
| 2325 | if (instr_idx == subop->ninstrs - 1 && |
| 2326 | subop->last_instr_end_off) |
| 2327 | end_off = subop->last_instr_end_off; |
| 2328 | else |
| 2329 | end_off = subop->instrs[instr_idx].ctx.data.len; |
| 2330 | |
| 2331 | return end_off - start_off; |
| 2332 | } |
| 2333 | EXPORT_SYMBOL_GPL(nand_subop_get_data_len); |
| 2334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | /** |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2336 | * nand_reset - Reset and initialize a NAND device |
| 2337 | * @chip: The NAND chip |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2338 | * @chipnr: Internal die id |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2339 | * |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2340 | * Save the timings data structure, then apply SDR timings mode 0 (see |
| 2341 | * nand_reset_data_interface for details), do the reset operation, and |
| 2342 | * apply back the previous timings. |
| 2343 | * |
| 2344 | * Returns 0 on success, a negative error code otherwise. |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2345 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2346 | int nand_reset(struct nand_chip *chip, int chipnr) |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2347 | { |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2348 | struct nand_data_interface saved_data_intf = chip->data_interface; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2349 | int ret; |
| 2350 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 2351 | ret = nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2352 | if (ret) |
| 2353 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2354 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2355 | /* |
| 2356 | * 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] | 2357 | * interface settings, hence this weird nand_select_target() |
| 2358 | * nand_deselect_target() dance. |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2359 | */ |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 2360 | nand_select_target(chip, chipnr); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2361 | ret = nand_reset_op(chip); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 2362 | nand_deselect_target(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2363 | if (ret) |
| 2364 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2365 | |
Miquel Raynal | 107b7d6 | 2018-03-19 14:47:25 +0100 | [diff] [blame] | 2366 | /* |
| 2367 | * A nand_reset_data_interface() put both the NAND chip and the NAND |
| 2368 | * controller in timings mode 0. If the default mode for this chip is |
| 2369 | * also 0, no need to proceed to the change again. Plus, at probe time, |
| 2370 | * nand_setup_data_interface() uses ->set/get_features() which would |
| 2371 | * fail anyway as the parameter page is not available yet. |
| 2372 | */ |
| 2373 | if (!chip->onfi_timing_mode_default) |
| 2374 | return 0; |
| 2375 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2376 | chip->data_interface = saved_data_intf; |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 2377 | ret = nand_setup_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2378 | if (ret) |
| 2379 | return ret; |
| 2380 | |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2381 | return 0; |
| 2382 | } |
Boris Brezillon | b9bb984 | 2017-10-05 18:53:19 +0200 | [diff] [blame] | 2383 | EXPORT_SYMBOL_GPL(nand_reset); |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2384 | |
| 2385 | /** |
Boris Brezillon | 4524036 | 2018-09-07 00:38:40 +0200 | [diff] [blame] | 2386 | * nand_get_features - wrapper to perform a GET_FEATURE |
| 2387 | * @chip: NAND chip info structure |
| 2388 | * @addr: feature address |
| 2389 | * @subfeature_param: the subfeature parameters, a four bytes array |
| 2390 | * |
| 2391 | * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the |
| 2392 | * operation cannot be handled. |
| 2393 | */ |
| 2394 | int nand_get_features(struct nand_chip *chip, int addr, |
| 2395 | u8 *subfeature_param) |
| 2396 | { |
| 2397 | if (!nand_supports_get_features(chip, addr)) |
| 2398 | return -ENOTSUPP; |
| 2399 | |
| 2400 | if (chip->legacy.get_features) |
| 2401 | return chip->legacy.get_features(chip, addr, subfeature_param); |
| 2402 | |
| 2403 | return nand_get_features_op(chip, addr, subfeature_param); |
| 2404 | } |
Boris Brezillon | 4524036 | 2018-09-07 00:38:40 +0200 | [diff] [blame] | 2405 | |
| 2406 | /** |
| 2407 | * nand_set_features - wrapper to perform a SET_FEATURE |
| 2408 | * @chip: NAND chip info structure |
| 2409 | * @addr: feature address |
| 2410 | * @subfeature_param: the subfeature parameters, a four bytes array |
| 2411 | * |
| 2412 | * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the |
| 2413 | * operation cannot be handled. |
| 2414 | */ |
| 2415 | int nand_set_features(struct nand_chip *chip, int addr, |
| 2416 | u8 *subfeature_param) |
| 2417 | { |
| 2418 | if (!nand_supports_set_features(chip, addr)) |
| 2419 | return -ENOTSUPP; |
| 2420 | |
| 2421 | if (chip->legacy.set_features) |
| 2422 | return chip->legacy.set_features(chip, addr, subfeature_param); |
| 2423 | |
| 2424 | return nand_set_features_op(chip, addr, subfeature_param); |
| 2425 | } |
Boris Brezillon | 4524036 | 2018-09-07 00:38:40 +0200 | [diff] [blame] | 2426 | |
| 2427 | /** |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 2428 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 2429 | * @buf: buffer to test |
| 2430 | * @len: buffer length |
| 2431 | * @bitflips_threshold: maximum number of bitflips |
| 2432 | * |
| 2433 | * Check if a buffer contains only 0xff, which means the underlying region |
| 2434 | * has been erased and is ready to be programmed. |
| 2435 | * The bitflips_threshold specify the maximum number of bitflips before |
| 2436 | * considering the region is not erased. |
| 2437 | * Note: The logic of this function has been extracted from the memweight |
| 2438 | * implementation, except that nand_check_erased_buf function exit before |
| 2439 | * testing the whole buffer if the number of bitflips exceed the |
| 2440 | * bitflips_threshold value. |
| 2441 | * |
| 2442 | * Returns a positive number of bitflips less than or equal to |
| 2443 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 2444 | * threshold. |
| 2445 | */ |
| 2446 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 2447 | { |
| 2448 | const unsigned char *bitmap = buf; |
| 2449 | int bitflips = 0; |
| 2450 | int weight; |
| 2451 | |
| 2452 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 2453 | len--, bitmap++) { |
| 2454 | weight = hweight8(*bitmap); |
| 2455 | bitflips += BITS_PER_BYTE - weight; |
| 2456 | if (unlikely(bitflips > bitflips_threshold)) |
| 2457 | return -EBADMSG; |
| 2458 | } |
| 2459 | |
| 2460 | for (; len >= sizeof(long); |
| 2461 | len -= sizeof(long), bitmap += sizeof(long)) { |
Pavel Machek | 086567f | 2017-04-21 12:51:07 +0200 | [diff] [blame] | 2462 | unsigned long d = *((unsigned long *)bitmap); |
| 2463 | if (d == ~0UL) |
| 2464 | continue; |
| 2465 | weight = hweight_long(d); |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 2466 | bitflips += BITS_PER_LONG - weight; |
| 2467 | if (unlikely(bitflips > bitflips_threshold)) |
| 2468 | return -EBADMSG; |
| 2469 | } |
| 2470 | |
| 2471 | for (; len > 0; len--, bitmap++) { |
| 2472 | weight = hweight8(*bitmap); |
| 2473 | bitflips += BITS_PER_BYTE - weight; |
| 2474 | if (unlikely(bitflips > bitflips_threshold)) |
| 2475 | return -EBADMSG; |
| 2476 | } |
| 2477 | |
| 2478 | return bitflips; |
| 2479 | } |
| 2480 | |
| 2481 | /** |
| 2482 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 2483 | * 0xff data |
| 2484 | * @data: data buffer to test |
| 2485 | * @datalen: data length |
| 2486 | * @ecc: ECC buffer |
| 2487 | * @ecclen: ECC length |
| 2488 | * @extraoob: extra OOB buffer |
| 2489 | * @extraooblen: extra OOB length |
| 2490 | * @bitflips_threshold: maximum number of bitflips |
| 2491 | * |
| 2492 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 2493 | * 0xff pattern, which means the underlying region has been erased and is |
| 2494 | * ready to be programmed. |
| 2495 | * The bitflips_threshold specify the maximum number of bitflips before |
| 2496 | * considering the region as not erased. |
| 2497 | * |
| 2498 | * Note: |
| 2499 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 2500 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 2501 | * report the number of errors per chunk, and the NAND core infrastructure |
| 2502 | * expect you to return the maximum number of bitflips for the whole page. |
| 2503 | * This is why you should always use this function on a single chunk and |
| 2504 | * not on the whole page. After checking each chunk you should update your |
| 2505 | * max_bitflips value accordingly. |
| 2506 | * 2/ When checking for bitflips in erased pages you should not only check |
| 2507 | * the payload data but also their associated ECC data, because a user might |
| 2508 | * have programmed almost all bits to 1 but a few. In this case, we |
| 2509 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 2510 | * this case. |
| 2511 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 2512 | * data are protected by the ECC engine. |
| 2513 | * It could also be used if you support subpages and want to attach some |
| 2514 | * extra OOB data to an ECC chunk. |
| 2515 | * |
| 2516 | * Returns a positive number of bitflips less than or equal to |
| 2517 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 2518 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 2519 | */ |
| 2520 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 2521 | void *ecc, int ecclen, |
| 2522 | void *extraoob, int extraooblen, |
| 2523 | int bitflips_threshold) |
| 2524 | { |
| 2525 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 2526 | |
| 2527 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 2528 | bitflips_threshold); |
| 2529 | if (data_bitflips < 0) |
| 2530 | return data_bitflips; |
| 2531 | |
| 2532 | bitflips_threshold -= data_bitflips; |
| 2533 | |
| 2534 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 2535 | if (ecc_bitflips < 0) |
| 2536 | return ecc_bitflips; |
| 2537 | |
| 2538 | bitflips_threshold -= ecc_bitflips; |
| 2539 | |
| 2540 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 2541 | bitflips_threshold); |
| 2542 | if (extraoob_bitflips < 0) |
| 2543 | return extraoob_bitflips; |
| 2544 | |
| 2545 | if (data_bitflips) |
| 2546 | memset(data, 0xff, datalen); |
| 2547 | |
| 2548 | if (ecc_bitflips) |
| 2549 | memset(ecc, 0xff, ecclen); |
| 2550 | |
| 2551 | if (extraoob_bitflips) |
| 2552 | memset(extraoob, 0xff, extraooblen); |
| 2553 | |
| 2554 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 2555 | } |
| 2556 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
| 2557 | |
| 2558 | /** |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2559 | * nand_read_page_raw_notsupp - dummy read raw page function |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2560 | * @chip: nand chip info structure |
| 2561 | * @buf: buffer to store read data |
| 2562 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2563 | * @page: page number to read |
| 2564 | * |
| 2565 | * Returns -ENOTSUPP unconditionally. |
| 2566 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2567 | int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf, |
| 2568 | int oob_required, int page) |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2569 | { |
| 2570 | return -ENOTSUPP; |
| 2571 | } |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2572 | |
| 2573 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2574 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2575 | * @chip: nand chip info structure |
| 2576 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2577 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2578 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2579 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2580 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2581 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2582 | int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required, |
| 2583 | int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2584 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2585 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2586 | int ret; |
| 2587 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2588 | ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2589 | if (ret) |
| 2590 | return ret; |
| 2591 | |
| 2592 | if (oob_required) { |
| 2593 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 2594 | false); |
| 2595 | if (ret) |
| 2596 | return ret; |
| 2597 | } |
| 2598 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2599 | return 0; |
| 2600 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 2601 | EXPORT_SYMBOL(nand_read_page_raw); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2602 | |
| 2603 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2604 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2605 | * @chip: nand chip info structure |
| 2606 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2607 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2608 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2609 | * |
| 2610 | * We need a special oob layout and handling even when OOB isn't used. |
| 2611 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2612 | 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] | 2613 | int oob_required, int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2614 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2615 | struct mtd_info *mtd = nand_to_mtd(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2616 | int eccsize = chip->ecc.size; |
| 2617 | int eccbytes = chip->ecc.bytes; |
| 2618 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2619 | int steps, size, ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2620 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2621 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2622 | if (ret) |
| 2623 | return ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2624 | |
| 2625 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2626 | ret = nand_read_data_op(chip, buf, eccsize, false); |
| 2627 | if (ret) |
| 2628 | return ret; |
| 2629 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2630 | buf += eccsize; |
| 2631 | |
| 2632 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2633 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 2634 | false); |
| 2635 | if (ret) |
| 2636 | return ret; |
| 2637 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2638 | oob += chip->ecc.prepad; |
| 2639 | } |
| 2640 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2641 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 2642 | if (ret) |
| 2643 | return ret; |
| 2644 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2645 | oob += eccbytes; |
| 2646 | |
| 2647 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2648 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 2649 | false); |
| 2650 | if (ret) |
| 2651 | return ret; |
| 2652 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2653 | oob += chip->ecc.postpad; |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2658 | if (size) { |
| 2659 | ret = nand_read_data_op(chip, oob, size, false); |
| 2660 | if (ret) |
| 2661 | return ret; |
| 2662 | } |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2663 | |
| 2664 | return 0; |
| 2665 | } |
| 2666 | |
| 2667 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2668 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2669 | * @chip: nand chip info structure |
| 2670 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2671 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2672 | * @page: page number to read |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 2673 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2674 | static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf, |
| 2675 | int oob_required, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2677 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2678 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2679 | int eccbytes = chip->ecc.bytes; |
| 2680 | int eccsteps = chip->ecc.steps; |
| 2681 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2682 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
| 2683 | uint8_t *ecc_code = chip->ecc.code_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2684 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2685 | |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2686 | chip->ecc.read_page_raw(chip, buf, 1, page); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2687 | |
| 2688 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 2689 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2690 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2691 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 2692 | chip->ecc.total); |
| 2693 | if (ret) |
| 2694 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2695 | |
| 2696 | eccsteps = chip->ecc.steps; |
| 2697 | p = buf; |
| 2698 | |
| 2699 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2700 | int stat; |
| 2701 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2702 | stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]); |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2703 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2704 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2705 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2706 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2707 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2708 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2709 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2710 | return max_bitflips; |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 2711 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | /** |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2714 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2715 | * @chip: nand chip info structure |
| 2716 | * @data_offs: offset of requested data within the page |
| 2717 | * @readlen: data length |
| 2718 | * @bufpoi: buffer to store read data |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 2719 | * @page: page number to read |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2720 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2721 | static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs, |
| 2722 | uint32_t readlen, uint8_t *bufpoi, int page) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2723 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2724 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2725 | int start_step, end_step, num_steps, ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2726 | uint8_t *p; |
| 2727 | int data_col_addr, i, gaps = 0; |
| 2728 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 2729 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2730 | int index, section = 0; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2731 | unsigned int max_bitflips = 0; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2732 | struct mtd_oob_region oobregion = { }; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2733 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2734 | /* Column address within the page aligned to ECC size (256bytes) */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2735 | start_step = data_offs / chip->ecc.size; |
| 2736 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 2737 | num_steps = end_step - start_step + 1; |
Ron | 4a4163ca | 2014-03-16 04:01:07 +1030 | [diff] [blame] | 2738 | index = start_step * chip->ecc.bytes; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2739 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2740 | /* Data size aligned to ECC ecc.size */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2741 | datafrag_len = num_steps * chip->ecc.size; |
| 2742 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 2743 | |
| 2744 | data_col_addr = start_step * chip->ecc.size; |
| 2745 | /* If we read not a page aligned data */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2746 | p = bufpoi + data_col_addr; |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2747 | 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] | 2748 | if (ret) |
| 2749 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2750 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2751 | /* Calculate ECC */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2752 | 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] | 2753 | chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2754 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2755 | /* |
| 2756 | * The performance is faster if we position offsets according to |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2757 | * 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] | 2758 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2759 | ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion); |
| 2760 | if (ret) |
| 2761 | return ret; |
| 2762 | |
| 2763 | if (oobregion.length < eccfrag_len) |
| 2764 | gaps = 1; |
| 2765 | |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2766 | if (gaps) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2767 | ret = nand_change_read_column_op(chip, mtd->writesize, |
| 2768 | chip->oob_poi, mtd->oobsize, |
| 2769 | false); |
| 2770 | if (ret) |
| 2771 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2772 | } else { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2773 | /* |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2774 | * Send the command to read the particular ECC bytes take care |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2775 | * about buswidth alignment in read_buf. |
| 2776 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2777 | aligned_pos = oobregion.offset & ~(busw - 1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2778 | aligned_len = eccfrag_len; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2779 | if (oobregion.offset & (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2780 | aligned_len++; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2781 | if ((oobregion.offset + (num_steps * chip->ecc.bytes)) & |
| 2782 | (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2783 | aligned_len++; |
| 2784 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2785 | ret = nand_change_read_column_op(chip, |
| 2786 | mtd->writesize + aligned_pos, |
| 2787 | &chip->oob_poi[aligned_pos], |
| 2788 | aligned_len, false); |
| 2789 | if (ret) |
| 2790 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2791 | } |
| 2792 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2793 | ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf, |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2794 | chip->oob_poi, index, eccfrag_len); |
| 2795 | if (ret) |
| 2796 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2797 | |
| 2798 | p = bufpoi + data_col_addr; |
| 2799 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 2800 | int stat; |
| 2801 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2802 | stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i], |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2803 | &chip->ecc.calc_buf[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2804 | if (stat == -EBADMSG && |
| 2805 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2806 | /* check for empty pages with bitflips */ |
| 2807 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2808 | &chip->ecc.code_buf[i], |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2809 | chip->ecc.bytes, |
| 2810 | NULL, 0, |
| 2811 | chip->ecc.strength); |
| 2812 | } |
| 2813 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2814 | if (stat < 0) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2815 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2816 | } else { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2817 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2818 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2819 | } |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2820 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2821 | return max_bitflips; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2822 | } |
| 2823 | |
| 2824 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2825 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2826 | * @chip: nand chip info structure |
| 2827 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2828 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2829 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2830 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2831 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2832 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2833 | static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf, |
| 2834 | int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2835 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2836 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2837 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2838 | int eccbytes = chip->ecc.bytes; |
| 2839 | int eccsteps = chip->ecc.steps; |
| 2840 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2841 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
| 2842 | uint8_t *ecc_code = chip->ecc.code_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2843 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2844 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2845 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2846 | if (ret) |
| 2847 | return ret; |
| 2848 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2849 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 2850 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2851 | |
| 2852 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2853 | if (ret) |
| 2854 | return ret; |
| 2855 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 2856 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2857 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2858 | |
| 2859 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 2860 | if (ret) |
| 2861 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2862 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2863 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 2864 | chip->ecc.total); |
| 2865 | if (ret) |
| 2866 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2867 | |
| 2868 | eccsteps = chip->ecc.steps; |
| 2869 | p = buf; |
| 2870 | |
| 2871 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2872 | int stat; |
| 2873 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2874 | stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2875 | if (stat == -EBADMSG && |
| 2876 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2877 | /* check for empty pages with bitflips */ |
| 2878 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2879 | &ecc_code[i], eccbytes, |
| 2880 | NULL, 0, |
| 2881 | chip->ecc.strength); |
| 2882 | } |
| 2883 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2884 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2885 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2886 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2887 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2888 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2889 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2890 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2891 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2895 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2896 | * @chip: nand chip info structure |
| 2897 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2898 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2899 | * @page: page number to read |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2900 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2901 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 2902 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 2903 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 2904 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 2905 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2906 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2907 | static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf, |
| 2908 | int oob_required, int page) |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2909 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2910 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2911 | int i, eccsize = chip->ecc.size, ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2912 | int eccbytes = chip->ecc.bytes; |
| 2913 | int eccsteps = chip->ecc.steps; |
| 2914 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2915 | uint8_t *ecc_code = chip->ecc.code_buf; |
| 2916 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2917 | unsigned int max_bitflips = 0; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2918 | |
| 2919 | /* Read the OOB area first */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2920 | ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
| 2921 | if (ret) |
| 2922 | return ret; |
| 2923 | |
| 2924 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2925 | if (ret) |
| 2926 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2927 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2928 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 2929 | chip->ecc.total); |
| 2930 | if (ret) |
| 2931 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2932 | |
| 2933 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2934 | int stat; |
| 2935 | |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 2936 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2937 | |
| 2938 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2939 | if (ret) |
| 2940 | return ret; |
| 2941 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 2942 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2943 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2944 | stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2945 | if (stat == -EBADMSG && |
| 2946 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2947 | /* check for empty pages with bitflips */ |
| 2948 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2949 | &ecc_code[i], eccbytes, |
| 2950 | NULL, 0, |
| 2951 | chip->ecc.strength); |
| 2952 | } |
| 2953 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2954 | if (stat < 0) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2955 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2956 | } else { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2957 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2958 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2959 | } |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2960 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2961 | return max_bitflips; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2962 | } |
| 2963 | |
| 2964 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2965 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2966 | * @chip: nand chip info structure |
| 2967 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2968 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2969 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2970 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2971 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 2972 | * need a special oob layout and handling. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2973 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2974 | static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf, |
| 2975 | int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2976 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2977 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2978 | int ret, i, eccsize = chip->ecc.size; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2979 | int eccbytes = chip->ecc.bytes; |
| 2980 | int eccsteps = chip->ecc.steps; |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2981 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2982 | uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2983 | uint8_t *oob = chip->oob_poi; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2984 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2985 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2986 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2987 | if (ret) |
| 2988 | return ret; |
| 2989 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2990 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2991 | int stat; |
| 2992 | |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 2993 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2994 | |
| 2995 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2996 | if (ret) |
| 2997 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2998 | |
| 2999 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3000 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 3001 | false); |
| 3002 | if (ret) |
| 3003 | return ret; |
| 3004 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3005 | oob += chip->ecc.prepad; |
| 3006 | } |
| 3007 | |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3008 | chip->ecc.hwctl(chip, NAND_ECC_READSYN); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3009 | |
| 3010 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 3011 | if (ret) |
| 3012 | return ret; |
| 3013 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 3014 | stat = chip->ecc.correct(chip, p, oob, NULL); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3015 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3016 | oob += eccbytes; |
| 3017 | |
| 3018 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3019 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 3020 | false); |
| 3021 | if (ret) |
| 3022 | return ret; |
| 3023 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3024 | oob += chip->ecc.postpad; |
| 3025 | } |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 3026 | |
| 3027 | if (stat == -EBADMSG && |
| 3028 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 3029 | /* check for empty pages with bitflips */ |
| 3030 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 3031 | oob - eccpadbytes, |
| 3032 | eccpadbytes, |
| 3033 | NULL, 0, |
| 3034 | chip->ecc.strength); |
| 3035 | } |
| 3036 | |
| 3037 | if (stat < 0) { |
| 3038 | mtd->ecc_stats.failed++; |
| 3039 | } else { |
| 3040 | mtd->ecc_stats.corrected += stat; |
| 3041 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 3042 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 3046 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3047 | if (i) { |
| 3048 | ret = nand_read_data_op(chip, oob, i, false); |
| 3049 | if (ret) |
| 3050 | return ret; |
| 3051 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3052 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3053 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3057 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3058 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3059 | * @oob: oob destination address |
| 3060 | * @ops: oob ops structure |
| 3061 | * @len: size of oob to transfer |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3062 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3063 | 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] | 3064 | struct mtd_oob_ops *ops, size_t len) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3065 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3066 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3067 | int ret; |
| 3068 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3069 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3070 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3071 | case MTD_OPS_PLACE_OOB: |
| 3072 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3073 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 3074 | return oob + len; |
| 3075 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3076 | case MTD_OPS_AUTO_OOB: |
| 3077 | ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi, |
| 3078 | ops->ooboffs, len); |
| 3079 | BUG_ON(ret); |
| 3080 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3081 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3082 | default: |
| 3083 | BUG(); |
| 3084 | } |
| 3085 | return NULL; |
| 3086 | } |
| 3087 | |
| 3088 | /** |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3089 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3090 | * @chip: NAND chip object |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3091 | * @retry_mode: the retry mode to use |
| 3092 | * |
| 3093 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 3094 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 3095 | * a new threshold, the host should retry reading the page. |
| 3096 | */ |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3097 | 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] | 3098 | { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3099 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 3100 | |
| 3101 | if (retry_mode >= chip->read_retries) |
| 3102 | return -EINVAL; |
| 3103 | |
| 3104 | if (!chip->setup_read_retry) |
| 3105 | return -EOPNOTSUPP; |
| 3106 | |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3107 | return chip->setup_read_retry(chip, retry_mode); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3108 | } |
| 3109 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3110 | static void nand_wait_readrdy(struct nand_chip *chip) |
| 3111 | { |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 3112 | const struct nand_sdr_timings *sdr; |
| 3113 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3114 | if (!(chip->options & NAND_NEED_READRDY)) |
| 3115 | return; |
| 3116 | |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 3117 | sdr = nand_get_sdr_timings(&chip->data_interface); |
| 3118 | 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] | 3119 | } |
| 3120 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3121 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3122 | * nand_do_read_ops - [INTERN] Read data with ECC |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3123 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3124 | * @from: offset to read from |
| 3125 | * @ops: oob ops structure |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3126 | * |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3127 | * Internal function. Called with chip held. |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3128 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3129 | 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] | 3130 | struct mtd_oob_ops *ops) |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3131 | { |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3132 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3133 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3134 | int ret = 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3135 | uint32_t readlen = ops->len; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3136 | uint32_t oobreadlen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3137 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 9aca334 | 2010-02-22 20:39:35 +0200 | [diff] [blame] | 3138 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3139 | uint8_t *bufpoi, *oob, *buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3140 | int use_bufpoi; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3141 | unsigned int max_bitflips = 0; |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3142 | int retry_mode = 0; |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3143 | bool ecc_fail = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3145 | chipnr = (int)(from >> chip->chip_shift); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 3146 | nand_select_target(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3148 | realpage = (int)(from >> chip->page_shift); |
| 3149 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3151 | col = (int)(from & (mtd->writesize - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3153 | buf = ops->datbuf; |
| 3154 | oob = ops->oobbuf; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3155 | oob_required = oob ? 1 : 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3156 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3157 | while (1) { |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3158 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
| 3159 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3160 | bytes = min(mtd->writesize - col, readlen); |
| 3161 | aligned = (bytes == mtd->writesize); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3162 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3163 | if (!aligned) |
| 3164 | use_bufpoi = 1; |
| 3165 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 3166 | use_bufpoi = !virt_addr_valid(buf) || |
| 3167 | !IS_ALIGNED((unsigned long)buf, |
| 3168 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3169 | else |
| 3170 | use_bufpoi = 0; |
| 3171 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3172 | /* Is the current page in the buffer? */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3173 | if (realpage != chip->pagebuf || oob) { |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3174 | bufpoi = use_bufpoi ? chip->data_buf : buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3175 | |
| 3176 | if (use_bufpoi && aligned) |
| 3177 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 3178 | __func__, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3180 | read_retry: |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3181 | /* |
| 3182 | * Now read the page into the buffer. Absent an error, |
| 3183 | * the read methods return max bitflips per ecc step. |
| 3184 | */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3185 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3186 | ret = chip->ecc.read_page_raw(chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3187 | oob_required, |
| 3188 | page); |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 3189 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
| 3190 | !oob) |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3191 | ret = chip->ecc.read_subpage(chip, col, bytes, |
| 3192 | bufpoi, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3193 | else |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3194 | ret = chip->ecc.read_page(chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3195 | oob_required, page); |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3196 | if (ret < 0) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3197 | if (use_bufpoi) |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3198 | /* Invalidate page cache */ |
| 3199 | chip->pagebuf = -1; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3200 | break; |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3201 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3202 | |
| 3203 | /* Transfer not aligned data */ |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3204 | if (use_bufpoi) { |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 3205 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3206 | !(mtd->ecc_stats.failed - ecc_failures) && |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3207 | (ops->mode != MTD_OPS_RAW)) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3208 | chip->pagebuf = realpage; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3209 | chip->pagebuf_bitflips = ret; |
| 3210 | } else { |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3211 | /* Invalidate page cache */ |
| 3212 | chip->pagebuf = -1; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3213 | } |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3214 | memcpy(buf, chip->data_buf + col, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3215 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3216 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3217 | if (unlikely(oob)) { |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 3218 | int toread = min(oobreadlen, max_oobsize); |
| 3219 | |
| 3220 | if (toread) { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3221 | oob = nand_transfer_oob(chip, oob, ops, |
| 3222 | toread); |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 3223 | oobreadlen -= toread; |
| 3224 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3225 | } |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 3226 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3227 | nand_wait_readrdy(chip); |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3228 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3229 | if (mtd->ecc_stats.failed - ecc_failures) { |
Brian Norris | 28fa65e | 2014-02-12 16:08:28 -0800 | [diff] [blame] | 3230 | if (retry_mode + 1 < chip->read_retries) { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3231 | retry_mode++; |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3232 | ret = nand_setup_read_retry(chip, |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3233 | retry_mode); |
| 3234 | if (ret < 0) |
| 3235 | break; |
| 3236 | |
| 3237 | /* Reset failures; retry */ |
| 3238 | mtd->ecc_stats.failed = ecc_failures; |
| 3239 | goto read_retry; |
| 3240 | } else { |
| 3241 | /* No more retry modes; real failure */ |
| 3242 | ecc_fail = true; |
| 3243 | } |
| 3244 | } |
| 3245 | |
| 3246 | buf += bytes; |
Masahiro Yamada | 0760468 | 2017-03-30 15:45:47 +0900 | [diff] [blame] | 3247 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3248 | } else { |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3249 | memcpy(buf, chip->data_buf + col, bytes); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3250 | buf += bytes; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3251 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 3252 | chip->pagebuf_bitflips); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3253 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3255 | readlen -= bytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3256 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3257 | /* Reset to retry mode 0 */ |
| 3258 | if (retry_mode) { |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3259 | ret = nand_setup_read_retry(chip, 0); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3260 | if (ret < 0) |
| 3261 | break; |
| 3262 | retry_mode = 0; |
| 3263 | } |
| 3264 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3265 | if (!readlen) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3266 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3268 | /* For subsequent reads align to page boundary */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | col = 0; |
| 3270 | /* Increment page address */ |
| 3271 | realpage++; |
| 3272 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3273 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | /* Check, if we cross a chip boundary */ |
| 3275 | if (!page) { |
| 3276 | chipnr++; |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 3277 | nand_deselect_target(chip); |
| 3278 | nand_select_target(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3279 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3280 | } |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 3281 | nand_deselect_target(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3283 | ops->retlen = ops->len - (size_t) readlen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3284 | if (oob) |
| 3285 | ops->oobretlen = ops->ooblen - oobreadlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3286 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3287 | if (ret < 0) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3288 | return ret; |
| 3289 | |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3290 | if (ecc_fail) |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 3291 | return -EBADMSG; |
| 3292 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3293 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3297 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3298 | * @chip: nand chip info structure |
| 3299 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3300 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3301 | int nand_read_oob_std(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3302 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3303 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 3304 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3305 | 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] | 3306 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3307 | EXPORT_SYMBOL(nand_read_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3308 | |
| 3309 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3310 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3311 | * with syndromes |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3312 | * @chip: nand chip info structure |
| 3313 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3314 | */ |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 3315 | static int nand_read_oob_syndrome(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3316 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3317 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3318 | int length = mtd->oobsize; |
| 3319 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 3320 | int eccsize = chip->ecc.size; |
Baruch Siach | 2ea69d2 | 2015-01-22 15:23:05 +0200 | [diff] [blame] | 3321 | uint8_t *bufpoi = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3322 | int i, toread, sndrnd = 0, pos, ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3323 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3324 | ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0); |
| 3325 | if (ret) |
| 3326 | return ret; |
| 3327 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3328 | for (i = 0; i < chip->ecc.steps; i++) { |
| 3329 | if (sndrnd) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3330 | int ret; |
| 3331 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3332 | pos = eccsize + i * (eccsize + chunk); |
| 3333 | if (mtd->writesize > 512) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3334 | ret = nand_change_read_column_op(chip, pos, |
| 3335 | NULL, 0, |
| 3336 | false); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3337 | else |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3338 | ret = nand_read_page_op(chip, page, pos, NULL, |
| 3339 | 0); |
| 3340 | |
| 3341 | if (ret) |
| 3342 | return ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3343 | } else |
| 3344 | sndrnd = 1; |
| 3345 | toread = min_t(int, length, chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3346 | |
| 3347 | ret = nand_read_data_op(chip, bufpoi, toread, false); |
| 3348 | if (ret) |
| 3349 | return ret; |
| 3350 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3351 | bufpoi += toread; |
| 3352 | length -= toread; |
| 3353 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3354 | if (length > 0) { |
| 3355 | ret = nand_read_data_op(chip, bufpoi, length, false); |
| 3356 | if (ret) |
| 3357 | return ret; |
| 3358 | } |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3359 | |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 3360 | return 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3364 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3365 | * @chip: nand chip info structure |
| 3366 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3367 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3368 | int nand_write_oob_std(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3369 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3370 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 3371 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3372 | return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi, |
| 3373 | mtd->oobsize); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3374 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3375 | EXPORT_SYMBOL(nand_write_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3376 | |
| 3377 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3378 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3379 | * with syndrome - only for large page flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3380 | * @chip: nand chip info structure |
| 3381 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3382 | */ |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 3383 | static int nand_write_oob_syndrome(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3384 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3385 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3386 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 3387 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3388 | int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3389 | const uint8_t *bufpoi = chip->oob_poi; |
| 3390 | |
| 3391 | /* |
| 3392 | * data-ecc-data-ecc ... ecc-oob |
| 3393 | * or |
| 3394 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
| 3395 | */ |
| 3396 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 3397 | pos = steps * (eccsize + chunk); |
| 3398 | steps = 0; |
| 3399 | } else |
Vitaly Wool | 8b0036e | 2006-07-11 09:11:25 +0200 | [diff] [blame] | 3400 | pos = eccsize; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3401 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3402 | ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0); |
| 3403 | if (ret) |
| 3404 | return ret; |
| 3405 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3406 | for (i = 0; i < steps; i++) { |
| 3407 | if (sndcmd) { |
| 3408 | if (mtd->writesize <= 512) { |
| 3409 | uint32_t fill = 0xFFFFFFFF; |
| 3410 | |
| 3411 | len = eccsize; |
| 3412 | while (len > 0) { |
| 3413 | int num = min_t(int, len, 4); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3414 | |
| 3415 | ret = nand_write_data_op(chip, &fill, |
| 3416 | num, false); |
| 3417 | if (ret) |
| 3418 | return ret; |
| 3419 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3420 | len -= num; |
| 3421 | } |
| 3422 | } else { |
| 3423 | pos = eccsize + i * (eccsize + chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3424 | ret = nand_change_write_column_op(chip, pos, |
| 3425 | NULL, 0, |
| 3426 | false); |
| 3427 | if (ret) |
| 3428 | return ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3429 | } |
| 3430 | } else |
| 3431 | sndcmd = 1; |
| 3432 | len = min_t(int, length, chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3433 | |
| 3434 | ret = nand_write_data_op(chip, bufpoi, len, false); |
| 3435 | if (ret) |
| 3436 | return ret; |
| 3437 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3438 | bufpoi += len; |
| 3439 | length -= len; |
| 3440 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3441 | if (length > 0) { |
| 3442 | ret = nand_write_data_op(chip, bufpoi, length, false); |
| 3443 | if (ret) |
| 3444 | return ret; |
| 3445 | } |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3446 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3447 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3448 | } |
| 3449 | |
| 3450 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3451 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3452 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3453 | * @from: offset to read from |
| 3454 | * @ops: oob operations description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3455 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3456 | * NAND read out-of-band data from the spare area. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3458 | 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] | 3459 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3460 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3461 | struct mtd_info *mtd = nand_to_mtd(chip); |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3462 | unsigned int max_bitflips = 0; |
Brian Norris | c00a099 | 2012-05-01 17:12:54 -0700 | [diff] [blame] | 3463 | int page, realpage, chipnr; |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3464 | struct mtd_ecc_stats stats; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3465 | int readlen = ops->ooblen; |
| 3466 | int len; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3467 | uint8_t *buf = ops->oobbuf; |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3468 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3470 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 3471 | __func__, (unsigned long long)from, readlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3473 | stats = mtd->ecc_stats; |
| 3474 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3475 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3476 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3477 | chipnr = (int)(from >> chip->chip_shift); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 3478 | nand_select_target(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3480 | /* Shift to get page */ |
| 3481 | realpage = (int)(from >> chip->page_shift); |
| 3482 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3483 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3484 | while (1) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3485 | if (ops->mode == MTD_OPS_RAW) |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3486 | ret = chip->ecc.read_oob_raw(chip, page); |
Brian Norris | c46f648 | 2011-08-30 18:45:38 -0700 | [diff] [blame] | 3487 | else |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3488 | ret = chip->ecc.read_oob(chip, page); |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3489 | |
| 3490 | if (ret < 0) |
| 3491 | break; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3492 | |
| 3493 | len = min(len, readlen); |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3494 | buf = nand_transfer_oob(chip, buf, ops, len); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3495 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3496 | nand_wait_readrdy(chip); |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 3497 | |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3498 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
| 3499 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3500 | readlen -= len; |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 3501 | if (!readlen) |
| 3502 | break; |
| 3503 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3504 | /* Increment page address */ |
| 3505 | realpage++; |
| 3506 | |
| 3507 | page = realpage & chip->pagemask; |
| 3508 | /* Check, if we cross a chip boundary */ |
| 3509 | if (!page) { |
| 3510 | chipnr++; |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 3511 | nand_deselect_target(chip); |
| 3512 | nand_select_target(chip, chipnr); |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3513 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3514 | } |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 3515 | nand_deselect_target(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3517 | ops->oobretlen = ops->ooblen - readlen; |
| 3518 | |
| 3519 | if (ret < 0) |
| 3520 | return ret; |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3521 | |
| 3522 | if (mtd->ecc_stats.failed - stats.failed) |
| 3523 | return -EBADMSG; |
| 3524 | |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3525 | return max_bitflips; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3529 | * 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] | 3530 | * @mtd: MTD device structure |
| 3531 | * @from: offset to read from |
| 3532 | * @ops: oob operation description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3533 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3534 | * NAND read data and/or out-of-band data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3535 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3536 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 3537 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3538 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3539 | struct nand_chip *chip = mtd_to_nand(mtd); |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 3540 | int ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3541 | |
| 3542 | ops->retlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 3544 | if (ops->mode != MTD_OPS_PLACE_OOB && |
| 3545 | ops->mode != MTD_OPS_AUTO_OOB && |
| 3546 | ops->mode != MTD_OPS_RAW) |
| 3547 | return -ENOTSUPP; |
| 3548 | |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 3549 | ret = nand_get_device(chip); |
| 3550 | if (ret) |
| 3551 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3552 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3553 | if (!ops->datbuf) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3554 | ret = nand_do_read_oob(chip, from, ops); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3555 | else |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3556 | ret = nand_do_read_ops(chip, from, ops); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3557 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3558 | nand_release_device(chip); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3559 | return ret; |
| 3560 | } |
| 3561 | |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 3562 | /** |
| 3563 | * nand_write_page_raw_notsupp - dummy raw page write function |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 3564 | * @chip: nand chip info structure |
| 3565 | * @buf: data buffer |
| 3566 | * @oob_required: must write chip->oob_poi to OOB |
| 3567 | * @page: page number to write |
| 3568 | * |
| 3569 | * Returns -ENOTSUPP unconditionally. |
| 3570 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3571 | int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf, |
| 3572 | int oob_required, int page) |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 3573 | { |
| 3574 | return -ENOTSUPP; |
| 3575 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3576 | |
| 3577 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3578 | * nand_write_page_raw - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3579 | * @chip: nand chip info structure |
| 3580 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3581 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3582 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3583 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3584 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3585 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3586 | int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf, |
| 3587 | int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3588 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3589 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3590 | int ret; |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3591 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3592 | ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3593 | if (ret) |
| 3594 | return ret; |
| 3595 | |
| 3596 | if (oob_required) { |
| 3597 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 3598 | false); |
| 3599 | if (ret) |
| 3600 | return ret; |
| 3601 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3602 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3603 | return nand_prog_page_end_op(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3604 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 3605 | EXPORT_SYMBOL(nand_write_page_raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3606 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3607 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3608 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3609 | * @chip: nand chip info structure |
| 3610 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3611 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3612 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3613 | * |
| 3614 | * We need a special oob layout and handling even when ECC isn't checked. |
| 3615 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3616 | static int nand_write_page_raw_syndrome(struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3617 | const uint8_t *buf, int oob_required, |
| 3618 | int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3619 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3620 | struct mtd_info *mtd = nand_to_mtd(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3621 | int eccsize = chip->ecc.size; |
| 3622 | int eccbytes = chip->ecc.bytes; |
| 3623 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3624 | int steps, size, ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3625 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3626 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3627 | if (ret) |
| 3628 | return ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3629 | |
| 3630 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3631 | ret = nand_write_data_op(chip, buf, eccsize, false); |
| 3632 | if (ret) |
| 3633 | return ret; |
| 3634 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3635 | buf += eccsize; |
| 3636 | |
| 3637 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3638 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 3639 | false); |
| 3640 | if (ret) |
| 3641 | return ret; |
| 3642 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3643 | oob += chip->ecc.prepad; |
| 3644 | } |
| 3645 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3646 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 3647 | if (ret) |
| 3648 | return ret; |
| 3649 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3650 | oob += eccbytes; |
| 3651 | |
| 3652 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3653 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 3654 | false); |
| 3655 | if (ret) |
| 3656 | return ret; |
| 3657 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3658 | oob += chip->ecc.postpad; |
| 3659 | } |
| 3660 | } |
| 3661 | |
| 3662 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3663 | if (size) { |
| 3664 | ret = nand_write_data_op(chip, oob, size, false); |
| 3665 | if (ret) |
| 3666 | return ret; |
| 3667 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3668 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3669 | return nand_prog_page_end_op(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3670 | } |
| 3671 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3672 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3673 | * @chip: nand chip info structure |
| 3674 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3675 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3676 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3677 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3678 | static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf, |
| 3679 | int oob_required, int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3680 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3681 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3682 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3683 | int eccbytes = chip->ecc.bytes; |
| 3684 | int eccsteps = chip->ecc.steps; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3685 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3686 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3687 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3688 | /* Software ECC calculation */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3689 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3690 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3691 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3692 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 3693 | chip->ecc.total); |
| 3694 | if (ret) |
| 3695 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3696 | |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3697 | return chip->ecc.write_page_raw(chip, buf, 1, page); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3701 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3702 | * @chip: nand chip info structure |
| 3703 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3704 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3705 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3706 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3707 | static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf, |
| 3708 | int oob_required, int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3709 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3710 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3711 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3712 | int eccbytes = chip->ecc.bytes; |
| 3713 | int eccsteps = chip->ecc.steps; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3714 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3715 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3716 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3717 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3718 | if (ret) |
| 3719 | return ret; |
| 3720 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3721 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3722 | chip->ecc.hwctl(chip, NAND_ECC_WRITE); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3723 | |
| 3724 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 3725 | if (ret) |
| 3726 | return ret; |
| 3727 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3728 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3729 | } |
| 3730 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3731 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 3732 | chip->ecc.total); |
| 3733 | if (ret) |
| 3734 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3735 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3736 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 3737 | if (ret) |
| 3738 | return ret; |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3739 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3740 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3741 | } |
| 3742 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3743 | |
| 3744 | /** |
Brian Norris | 73c8aaf | 2015-02-28 02:04:18 -0800 | [diff] [blame] | 3745 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3746 | * @chip: nand chip info structure |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3747 | * @offset: column address of subpage within the page |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3748 | * @data_len: data length |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3749 | * @buf: data buffer |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3750 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3751 | * @page: page number to write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3752 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3753 | static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset, |
| 3754 | uint32_t data_len, const uint8_t *buf, |
| 3755 | int oob_required, int page) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3756 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3757 | struct mtd_info *mtd = nand_to_mtd(chip); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3758 | uint8_t *oob_buf = chip->oob_poi; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3759 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3760 | int ecc_size = chip->ecc.size; |
| 3761 | int ecc_bytes = chip->ecc.bytes; |
| 3762 | int ecc_steps = chip->ecc.steps; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3763 | uint32_t start_step = offset / ecc_size; |
| 3764 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 3765 | int oob_bytes = mtd->oobsize / ecc_steps; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3766 | int step, ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3767 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3768 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3769 | if (ret) |
| 3770 | return ret; |
| 3771 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3772 | for (step = 0; step < ecc_steps; step++) { |
| 3773 | /* configure controller for WRITE access */ |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3774 | chip->ecc.hwctl(chip, NAND_ECC_WRITE); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3775 | |
| 3776 | /* write data (untouched subpages already masked by 0xFF) */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3777 | ret = nand_write_data_op(chip, buf, ecc_size, false); |
| 3778 | if (ret) |
| 3779 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3780 | |
| 3781 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 3782 | if ((step < start_step) || (step > end_step)) |
| 3783 | memset(ecc_calc, 0xff, ecc_bytes); |
| 3784 | else |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3785 | chip->ecc.calculate(chip, buf, ecc_calc); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3786 | |
| 3787 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 3788 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 3789 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 3790 | memset(oob_buf, 0xff, oob_bytes); |
| 3791 | |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3792 | buf += ecc_size; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3793 | ecc_calc += ecc_bytes; |
| 3794 | oob_buf += oob_bytes; |
| 3795 | } |
| 3796 | |
| 3797 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 3798 | /* this include masked-value(0xFF) for unwritten subpages */ |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3799 | ecc_calc = chip->ecc.calc_buf; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3800 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 3801 | chip->ecc.total); |
| 3802 | if (ret) |
| 3803 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3804 | |
| 3805 | /* write OOB buffer to NAND device */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3806 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 3807 | if (ret) |
| 3808 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3809 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3810 | return nand_prog_page_end_op(chip); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3814 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3815 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3816 | * @chip: nand chip info structure |
| 3817 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3818 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3819 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3820 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3821 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 3822 | * need a special oob layout and handling. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3823 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3824 | static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf, |
| 3825 | int oob_required, int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3826 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3827 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3828 | int i, eccsize = chip->ecc.size; |
| 3829 | int eccbytes = chip->ecc.bytes; |
| 3830 | int eccsteps = chip->ecc.steps; |
| 3831 | const uint8_t *p = buf; |
| 3832 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3833 | int ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3834 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3835 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3836 | if (ret) |
| 3837 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3838 | |
| 3839 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3840 | chip->ecc.hwctl(chip, NAND_ECC_WRITE); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3841 | |
| 3842 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 3843 | if (ret) |
| 3844 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3845 | |
| 3846 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3847 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 3848 | false); |
| 3849 | if (ret) |
| 3850 | return ret; |
| 3851 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3852 | oob += chip->ecc.prepad; |
| 3853 | } |
| 3854 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3855 | chip->ecc.calculate(chip, p, oob); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3856 | |
| 3857 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 3858 | if (ret) |
| 3859 | return ret; |
| 3860 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3861 | oob += eccbytes; |
| 3862 | |
| 3863 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3864 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 3865 | false); |
| 3866 | if (ret) |
| 3867 | return ret; |
| 3868 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3869 | oob += chip->ecc.postpad; |
| 3870 | } |
| 3871 | } |
| 3872 | |
| 3873 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 3874 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3875 | if (i) { |
| 3876 | ret = nand_write_data_op(chip, oob, i, false); |
| 3877 | if (ret) |
| 3878 | return ret; |
| 3879 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3880 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3881 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3882 | } |
| 3883 | |
| 3884 | /** |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 3885 | * nand_write_page - write one page |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3886 | * @chip: NAND chip descriptor |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3887 | * @offset: address offset within the page |
| 3888 | * @data_len: length of actual data to be written |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3889 | * @buf: the data to write |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3890 | * @oob_required: must write chip->oob_poi to OOB |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3891 | * @page: page number to write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3892 | * @raw: use _raw version of write_page |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3893 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3894 | static int nand_write_page(struct nand_chip *chip, uint32_t offset, |
| 3895 | int data_len, const uint8_t *buf, int oob_required, |
| 3896 | int page, int raw) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3897 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3898 | struct mtd_info *mtd = nand_to_mtd(chip); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3899 | int status, subpage; |
| 3900 | |
| 3901 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 3902 | chip->ecc.write_subpage) |
| 3903 | subpage = offset || (data_len < mtd->writesize); |
| 3904 | else |
| 3905 | subpage = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3906 | |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3907 | if (unlikely(raw)) |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3908 | status = chip->ecc.write_page_raw(chip, buf, oob_required, |
| 3909 | page); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3910 | else if (subpage) |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3911 | status = chip->ecc.write_subpage(chip, offset, data_len, buf, |
| 3912 | oob_required, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3913 | else |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3914 | status = chip->ecc.write_page(chip, buf, oob_required, page); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3915 | |
| 3916 | if (status < 0) |
| 3917 | return status; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3918 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3919 | return 0; |
| 3920 | } |
| 3921 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3922 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3923 | |
| 3924 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3925 | * nand_do_write_ops - [INTERN] NAND write with ECC |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3926 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3927 | * @to: offset to write to |
| 3928 | * @ops: oob operations description structure |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3929 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3930 | * NAND write with ECC. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3931 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3932 | 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] | 3933 | struct mtd_oob_ops *ops) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3934 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3935 | struct mtd_info *mtd = nand_to_mtd(chip); |
Corentin Labbe | 73600b6 | 2017-09-02 10:49:38 +0200 | [diff] [blame] | 3936 | int chipnr, realpage, page, column; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3937 | uint32_t writelen = ops->len; |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3938 | |
| 3939 | uint32_t oobwritelen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3940 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3941 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3942 | uint8_t *oob = ops->oobbuf; |
| 3943 | uint8_t *buf = ops->datbuf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3944 | int ret; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3945 | int oob_required = oob ? 1 : 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3946 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3947 | ops->retlen = 0; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3948 | if (!writelen) |
| 3949 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3950 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3951 | /* Reject writes, which are not page aligned */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3952 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 3953 | pr_notice("%s: attempt to write non page aligned data\n", |
| 3954 | __func__); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3955 | return -EINVAL; |
| 3956 | } |
| 3957 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3958 | column = to & (mtd->writesize - 1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3959 | |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 3960 | chipnr = (int)(to >> chip->chip_shift); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 3961 | nand_select_target(chip, chipnr); |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 3962 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3963 | /* Check, if it is write protected */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 3964 | if (nand_check_wp(chip)) { |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3965 | ret = -EIO; |
| 3966 | goto err_out; |
| 3967 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3968 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3969 | realpage = (int)(to >> chip->page_shift); |
| 3970 | page = realpage & chip->pagemask; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3971 | |
| 3972 | /* Invalidate the page cache, when we write to the cached page */ |
Brian Norris | 537ab1b | 2014-07-21 19:08:03 -0700 | [diff] [blame] | 3973 | if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && |
| 3974 | ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3975 | chip->pagebuf = -1; |
| 3976 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3977 | /* Don't allow multipage oob writes with offset */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3978 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 3979 | ret = -EINVAL; |
| 3980 | goto err_out; |
| 3981 | } |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3982 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3983 | while (1) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3984 | int bytes = mtd->writesize; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3985 | uint8_t *wbuf = buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3986 | int use_bufpoi; |
Hector Palacios | 144f4c9 | 2016-07-18 10:39:18 +0200 | [diff] [blame] | 3987 | int part_pagewr = (column || writelen < mtd->writesize); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3988 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3989 | if (part_pagewr) |
| 3990 | use_bufpoi = 1; |
| 3991 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 3992 | use_bufpoi = !virt_addr_valid(buf) || |
| 3993 | !IS_ALIGNED((unsigned long)buf, |
| 3994 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3995 | else |
| 3996 | use_bufpoi = 0; |
| 3997 | |
| 3998 | /* Partial page write?, or need to use bounce buffer */ |
| 3999 | if (use_bufpoi) { |
| 4000 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 4001 | __func__, buf); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 4002 | if (part_pagewr) |
| 4003 | bytes = min_t(int, bytes - column, writelen); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4004 | chip->pagebuf = -1; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 4005 | memset(chip->data_buf, 0xff, mtd->writesize); |
| 4006 | memcpy(&chip->data_buf[column], buf, bytes); |
| 4007 | wbuf = chip->data_buf; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4008 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4009 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4010 | if (unlikely(oob)) { |
| 4011 | size_t len = min(oobwritelen, oobmaxlen); |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4012 | oob = nand_fill_oob(chip, oob, len, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4013 | oobwritelen -= len; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 4014 | } else { |
| 4015 | /* We still need to erase leftover OOB data */ |
| 4016 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4017 | } |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 4018 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4019 | ret = nand_write_page(chip, column, bytes, wbuf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 4020 | oob_required, page, |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 4021 | (ops->mode == MTD_OPS_RAW)); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4022 | if (ret) |
| 4023 | break; |
| 4024 | |
| 4025 | writelen -= bytes; |
| 4026 | if (!writelen) |
| 4027 | break; |
| 4028 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4029 | column = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4030 | buf += bytes; |
| 4031 | realpage++; |
| 4032 | |
| 4033 | page = realpage & chip->pagemask; |
| 4034 | /* Check, if we cross a chip boundary */ |
| 4035 | if (!page) { |
| 4036 | chipnr++; |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4037 | nand_deselect_target(chip); |
| 4038 | nand_select_target(chip, chipnr); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4039 | } |
| 4040 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4041 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4042 | ops->retlen = ops->len - writelen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 4043 | if (unlikely(oob)) |
| 4044 | ops->oobretlen = ops->ooblen; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4045 | |
| 4046 | err_out: |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4047 | nand_deselect_target(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4048 | return ret; |
| 4049 | } |
| 4050 | |
| 4051 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4052 | * panic_nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4053 | * @mtd: MTD device structure |
| 4054 | * @to: offset to write to |
| 4055 | * @len: number of bytes to write |
| 4056 | * @retlen: pointer to variable to store the number of written bytes |
| 4057 | * @buf: the data to write |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4058 | * |
| 4059 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 4060 | * may for example be called by mtdoops when writing an oops while in panic. |
| 4061 | */ |
| 4062 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 4063 | size_t *retlen, const uint8_t *buf) |
| 4064 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4065 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4066 | int chipnr = (int)(to >> chip->chip_shift); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4067 | struct mtd_oob_ops ops; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4068 | int ret; |
| 4069 | |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4070 | nand_select_target(chip, chipnr); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4071 | |
| 4072 | /* Wait for the device to get ready */ |
Boris Brezillon | f1d4694 | 2018-09-06 14:05:29 +0200 | [diff] [blame] | 4073 | panic_nand_wait(chip, 400); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4074 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 4075 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4076 | ops.len = len; |
| 4077 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 4078 | ops.mode = MTD_OPS_PLACE_OOB; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4079 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4080 | ret = nand_do_write_ops(chip, to, &ops); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4081 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4082 | *retlen = ops.retlen; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4083 | return ret; |
| 4084 | } |
| 4085 | |
| 4086 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4087 | * 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] | 4088 | * @mtd: MTD device structure |
| 4089 | * @to: offset to write to |
| 4090 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4091 | */ |
| 4092 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 4093 | struct mtd_oob_ops *ops) |
| 4094 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4095 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4096 | int ret = -ENOTSUPP; |
| 4097 | |
| 4098 | ops->retlen = 0; |
| 4099 | |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4100 | ret = nand_get_device(chip); |
| 4101 | if (ret) |
| 4102 | return ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4103 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4104 | switch (ops->mode) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 4105 | case MTD_OPS_PLACE_OOB: |
| 4106 | case MTD_OPS_AUTO_OOB: |
| 4107 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4108 | break; |
| 4109 | |
| 4110 | default: |
| 4111 | goto out; |
| 4112 | } |
| 4113 | |
| 4114 | if (!ops->datbuf) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4115 | ret = nand_do_write_oob(chip, to, ops); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4116 | else |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4117 | ret = nand_do_write_ops(chip, to, ops); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4118 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4119 | out: |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4120 | nand_release_device(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | return ret; |
| 4122 | } |
| 4123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | * nand_erase - [MTD Interface] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4126 | * @mtd: MTD device structure |
| 4127 | * @instr: erase instruction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4128 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4129 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4130 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4131 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4132 | { |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4133 | return nand_erase_nand(mtd_to_nand(mtd), instr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4137 | * nand_erase_nand - [INTERN] erase block(s) |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4138 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4139 | * @instr: erase instruction |
| 4140 | * @allowbbt: allow erasing the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4141 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4142 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4143 | */ |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4144 | int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr, |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4145 | int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | { |
Masahiro Yamada | 2d73f3d | 2019-01-21 15:32:07 +0900 | [diff] [blame^] | 4147 | int page, pages_per_block, ret, chipnr; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4148 | loff_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4149 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4150 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 4151 | __func__, (unsigned long long)instr->addr, |
| 4152 | (unsigned long long)instr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4154 | if (check_offs_len(chip, instr->addr, instr->len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4155 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | /* Grab the lock and see if the device is available */ |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4158 | ret = nand_get_device(chip); |
| 4159 | if (ret) |
| 4160 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | |
| 4162 | /* Shift to get first page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4163 | page = (int)(instr->addr >> chip->page_shift); |
| 4164 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | |
| 4166 | /* Calculate pages in each block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4167 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4168 | |
| 4169 | /* Select the NAND device */ |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4170 | nand_select_target(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | /* Check, if it is write protected */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4173 | if (nand_check_wp(chip)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4174 | pr_debug("%s: device is write protected!\n", |
| 4175 | __func__); |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4176 | ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | goto erase_exit; |
| 4178 | } |
| 4179 | |
| 4180 | /* Loop through the pages */ |
| 4181 | len = instr->len; |
| 4182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4183 | while (len) { |
Wolfram Sang | 12183a2 | 2011-12-21 23:01:20 +0100 | [diff] [blame] | 4184 | /* 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] | 4185 | if (nand_block_checkbad(chip, ((loff_t) page) << |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4186 | chip->page_shift, allowbbt)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4187 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
| 4188 | __func__, page); |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4189 | ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4190 | goto erase_exit; |
| 4191 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4192 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4193 | /* |
| 4194 | * Invalidate the page cache, if we erase the block which |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4195 | * contains the current cached page. |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4196 | */ |
| 4197 | if (page <= chip->pagebuf && chip->pagebuf < |
| 4198 | (page + pages_per_block)) |
| 4199 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4200 | |
Masahiro Yamada | 2d73f3d | 2019-01-21 15:32:07 +0900 | [diff] [blame^] | 4201 | ret = nand_erase_op(chip, (page & chip->pagemask) >> |
| 4202 | (chip->phys_erase_shift - chip->page_shift)); |
| 4203 | if (ret) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4204 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 4205 | __func__, page); |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4206 | instr->fail_addr = |
| 4207 | ((loff_t)page << chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | goto erase_exit; |
| 4209 | } |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 4210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4211 | /* Increment page address and decrement length */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 4212 | len -= (1ULL << chip->phys_erase_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | page += pages_per_block; |
| 4214 | |
| 4215 | /* Check, if we cross a chip boundary */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4216 | if (len && !(page & chip->pagemask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | chipnr++; |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4218 | nand_deselect_target(chip); |
| 4219 | nand_select_target(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4220 | } |
| 4221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4222 | |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4223 | ret = 0; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4224 | erase_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | /* Deselect and wake up anyone waiting on the device */ |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4227 | nand_deselect_target(chip); |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4228 | nand_release_device(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | /* Return more or less happy */ |
| 4231 | return ret; |
| 4232 | } |
| 4233 | |
| 4234 | /** |
| 4235 | * nand_sync - [MTD Interface] sync |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4236 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4238 | * Sync is actually a wait for chip ready function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4239 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4240 | static void nand_sync(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4242 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4243 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4244 | pr_debug("%s: called\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | |
| 4246 | /* Grab the lock and see if the device is available */ |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4247 | WARN_ON(nand_get_device(chip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4248 | /* Release it and go back */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4249 | nand_release_device(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | } |
| 4251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4252 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4253 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4254 | * @mtd: MTD device structure |
| 4255 | * @offs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4257 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | { |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4259 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4260 | int chipnr = (int)(offs >> chip->chip_shift); |
| 4261 | int ret; |
| 4262 | |
| 4263 | /* Select the NAND device */ |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4264 | ret = nand_get_device(chip); |
| 4265 | if (ret) |
| 4266 | return ret; |
| 4267 | |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4268 | nand_select_target(chip, chipnr); |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4269 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4270 | ret = nand_block_checkbad(chip, offs, 0); |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4271 | |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4272 | nand_deselect_target(chip); |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4273 | nand_release_device(chip); |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4274 | |
| 4275 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4279 | * 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] | 4280 | * @mtd: MTD device structure |
| 4281 | * @ofs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4283 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4284 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4285 | int ret; |
| 4286 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4287 | ret = nand_block_isbad(mtd, ofs); |
| 4288 | if (ret) { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4289 | /* If it was bad already, return success and do nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4290 | if (ret > 0) |
| 4291 | return 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4292 | return ret; |
| 4293 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4294 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 4295 | return nand_block_markbad_lowlevel(mtd_to_nand(mtd), ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4296 | } |
| 4297 | |
| 4298 | /** |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 4299 | * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd |
| 4300 | * @mtd: MTD device structure |
| 4301 | * @ofs: offset relative to mtd start |
| 4302 | * @len: length of mtd |
| 4303 | */ |
| 4304 | static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len) |
| 4305 | { |
| 4306 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4307 | u32 part_start_block; |
| 4308 | u32 part_end_block; |
| 4309 | u32 part_start_die; |
| 4310 | u32 part_end_die; |
| 4311 | |
| 4312 | /* |
| 4313 | * max_bb_per_die and blocks_per_die used to determine |
| 4314 | * the maximum bad block count. |
| 4315 | */ |
| 4316 | if (!chip->max_bb_per_die || !chip->blocks_per_die) |
| 4317 | return -ENOTSUPP; |
| 4318 | |
| 4319 | /* Get the start and end of the partition in erase blocks. */ |
| 4320 | part_start_block = mtd_div_by_eb(ofs, mtd); |
| 4321 | part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1; |
| 4322 | |
| 4323 | /* Get the start and end LUNs of the partition. */ |
| 4324 | part_start_die = part_start_block / chip->blocks_per_die; |
| 4325 | part_end_die = part_end_block / chip->blocks_per_die; |
| 4326 | |
| 4327 | /* |
| 4328 | * Look up the bad blocks per unit and multiply by the number of units |
| 4329 | * that the partition spans. |
| 4330 | */ |
| 4331 | return chip->max_bb_per_die * (part_end_die - part_start_die + 1); |
| 4332 | } |
| 4333 | |
| 4334 | /** |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4335 | * nand_suspend - [MTD Interface] Suspend the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4336 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4337 | */ |
| 4338 | static int nand_suspend(struct mtd_info *mtd) |
| 4339 | { |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4340 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4341 | |
| 4342 | mutex_lock(&chip->lock); |
| 4343 | chip->suspended = 1; |
| 4344 | mutex_unlock(&chip->lock); |
| 4345 | |
| 4346 | return 0; |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4347 | } |
| 4348 | |
| 4349 | /** |
| 4350 | * nand_resume - [MTD Interface] Resume the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4351 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4352 | */ |
| 4353 | static void nand_resume(struct mtd_info *mtd) |
| 4354 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4355 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4356 | |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4357 | mutex_lock(&chip->lock); |
| 4358 | if (chip->suspended) |
| 4359 | chip->suspended = 0; |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4360 | else |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4361 | pr_err("%s called for a chip which is not in suspended state\n", |
| 4362 | __func__); |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4363 | mutex_unlock(&chip->lock); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4364 | } |
| 4365 | |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 4366 | /** |
| 4367 | * nand_shutdown - [MTD Interface] Finish the current NAND operation and |
| 4368 | * prevent further operations |
| 4369 | * @mtd: MTD device structure |
| 4370 | */ |
| 4371 | static void nand_shutdown(struct mtd_info *mtd) |
| 4372 | { |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4373 | nand_suspend(mtd); |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 4374 | } |
| 4375 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4376 | /* Set default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4377 | static void nand_set_defaults(struct nand_chip *chip) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4378 | { |
Boris Brezillon | 7b6a9b2 | 2018-11-20 10:02:39 +0100 | [diff] [blame] | 4379 | /* If no controller is provided, use the dummy, legacy one. */ |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4380 | if (!chip->controller) { |
Boris Brezillon | 7b6a9b2 | 2018-11-20 10:02:39 +0100 | [diff] [blame] | 4381 | chip->controller = &chip->legacy.dummy_controller; |
Miquel Raynal | 7da4513 | 2018-07-17 09:08:02 +0200 | [diff] [blame] | 4382 | nand_controller_init(chip->controller); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4383 | } |
| 4384 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 4385 | nand_legacy_set_defaults(chip); |
| 4386 | |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 4387 | if (!chip->buf_align) |
| 4388 | chip->buf_align = 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4389 | } |
| 4390 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4391 | /* Sanitize ONFI strings so we can safely print them */ |
Boris Brezillon | 1c325cc | 2018-09-07 00:38:50 +0200 | [diff] [blame] | 4392 | void sanitize_string(uint8_t *s, size_t len) |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4393 | { |
| 4394 | ssize_t i; |
| 4395 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4396 | /* Null terminate */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4397 | s[len - 1] = 0; |
| 4398 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4399 | /* Remove non printable chars */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4400 | for (i = 0; i < len - 1; i++) { |
| 4401 | if (s[i] < ' ' || s[i] > 127) |
| 4402 | s[i] = '?'; |
| 4403 | } |
| 4404 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4405 | /* Remove trailing spaces */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4406 | strim(s); |
| 4407 | } |
| 4408 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4409 | /* |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 4410 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 4411 | * @id_data: the ID string |
| 4412 | * @arrlen: the length of the @id_data array |
| 4413 | * @period: the period of repitition |
| 4414 | * |
| 4415 | * Check if an ID string is repeated within a given sequence of bytes at |
| 4416 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Brian Norris | d4d4f1b | 2012-11-14 21:54:20 -0800 | [diff] [blame] | 4417 | * 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] | 4418 | * if the repetition has a period of @period; otherwise, returns zero. |
| 4419 | */ |
| 4420 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 4421 | { |
| 4422 | int i, j; |
| 4423 | for (i = 0; i < period; i++) |
| 4424 | for (j = i + period; j < arrlen; j += period) |
| 4425 | if (id_data[i] != id_data[j]) |
| 4426 | return 0; |
| 4427 | return 1; |
| 4428 | } |
| 4429 | |
| 4430 | /* |
| 4431 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 4432 | * @id_data: the ID string |
| 4433 | * @arrlen: the length of the @id_data array |
| 4434 | |
| 4435 | * Returns the length of the ID string, according to known wraparound/trailing |
| 4436 | * zero patterns. If no pattern exists, returns the length of the array. |
| 4437 | */ |
| 4438 | static int nand_id_len(u8 *id_data, int arrlen) |
| 4439 | { |
| 4440 | int last_nonzero, period; |
| 4441 | |
| 4442 | /* Find last non-zero byte */ |
| 4443 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 4444 | if (id_data[last_nonzero]) |
| 4445 | break; |
| 4446 | |
| 4447 | /* All zeros */ |
| 4448 | if (last_nonzero < 0) |
| 4449 | return 0; |
| 4450 | |
| 4451 | /* Calculate wraparound period */ |
| 4452 | for (period = 1; period < arrlen; period++) |
| 4453 | if (nand_id_has_period(id_data, arrlen, period)) |
| 4454 | break; |
| 4455 | |
| 4456 | /* There's a repeated pattern */ |
| 4457 | if (period < arrlen) |
| 4458 | return period; |
| 4459 | |
| 4460 | /* There are trailing zeros */ |
| 4461 | if (last_nonzero < arrlen - 1) |
| 4462 | return last_nonzero + 1; |
| 4463 | |
| 4464 | /* No pattern detected */ |
| 4465 | return arrlen; |
| 4466 | } |
| 4467 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 4468 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 4469 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 4470 | { |
| 4471 | int bits; |
| 4472 | |
| 4473 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 4474 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 4475 | return bits + 1; |
| 4476 | } |
| 4477 | |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 4478 | /* |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4479 | * Many new NAND share similar device ID codes, which represent the size of the |
| 4480 | * chip. The rest of the parameters must be decoded according to generic or |
| 4481 | * manufacturer-specific "extended ID" decoding patterns. |
| 4482 | */ |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4483 | void nand_decode_ext_id(struct nand_chip *chip) |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4484 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4485 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 9b2d61f | 2016-06-08 10:34:57 +0200 | [diff] [blame] | 4486 | int extid; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4487 | u8 *id_data = chip->id.data; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4488 | /* The 3rd id byte holds MLC / multichip data */ |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 4489 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4490 | /* The 4th id byte is the important one */ |
| 4491 | extid = id_data[3]; |
| 4492 | |
Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 4493 | /* Calc pagesize */ |
| 4494 | mtd->writesize = 1024 << (extid & 0x03); |
| 4495 | extid >>= 2; |
| 4496 | /* Calc oobsize */ |
| 4497 | mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9); |
| 4498 | extid >>= 2; |
| 4499 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 4500 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 4501 | extid >>= 2; |
| 4502 | /* Get buswidth information */ |
| 4503 | if (extid & 0x1) |
| 4504 | chip->options |= NAND_BUSWIDTH_16; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4505 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4506 | EXPORT_SYMBOL_GPL(nand_decode_ext_id); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4507 | |
| 4508 | /* |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4509 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 4510 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 4511 | * the chip. |
| 4512 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4513 | 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] | 4514 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4515 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4516 | |
| 4517 | mtd->erasesize = type->erasesize; |
| 4518 | mtd->writesize = type->pagesize; |
| 4519 | mtd->oobsize = mtd->writesize / 32; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4520 | |
Huang Shijie | 1c195e9 | 2013-09-25 14:58:12 +0800 | [diff] [blame] | 4521 | /* All legacy ID NAND are small-page, SLC */ |
| 4522 | chip->bits_per_cell = 1; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4523 | } |
| 4524 | |
| 4525 | /* |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4526 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 4527 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 4528 | * page size, cell-type information). |
| 4529 | */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4530 | static void nand_decode_bbm_options(struct nand_chip *chip) |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4531 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4532 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4533 | |
| 4534 | /* Set the bad block position */ |
| 4535 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 4536 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 4537 | else |
| 4538 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4539 | } |
| 4540 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4541 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 4542 | { |
| 4543 | return type->id_len; |
| 4544 | } |
| 4545 | |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4546 | static bool find_full_id_nand(struct nand_chip *chip, |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4547 | struct nand_flash_dev *type) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4548 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4549 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4550 | u8 *id_data = chip->id.data; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4551 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4552 | if (!strncmp(type->id, id_data, type->id_len)) { |
| 4553 | mtd->writesize = type->pagesize; |
| 4554 | mtd->erasesize = type->erasesize; |
| 4555 | mtd->oobsize = type->oobsize; |
| 4556 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 4557 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4558 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 4559 | chip->options |= type->options; |
Huang Shijie | 5721934 | 2013-05-17 11:17:32 +0800 | [diff] [blame] | 4560 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
| 4561 | chip->ecc_step_ds = NAND_ECC_STEP(type); |
Boris BREZILLON | 57a94e2 | 2014-09-22 20:11:50 +0200 | [diff] [blame] | 4562 | chip->onfi_timing_mode_default = |
| 4563 | type->onfi_timing_mode_default; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4564 | |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4565 | chip->parameters.model = kstrdup(type->name, GFP_KERNEL); |
| 4566 | if (!chip->parameters.model) |
| 4567 | return false; |
Cai Zhiyong | 092b6a1 | 2013-12-25 21:19:21 +0800 | [diff] [blame] | 4568 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4569 | return true; |
| 4570 | } |
| 4571 | return false; |
| 4572 | } |
| 4573 | |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4574 | /* |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4575 | * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC |
| 4576 | * compliant and does not have a full-id or legacy-id entry in the nand_ids |
| 4577 | * table. |
| 4578 | */ |
| 4579 | static void nand_manufacturer_detect(struct nand_chip *chip) |
| 4580 | { |
| 4581 | /* |
| 4582 | * Try manufacturer detection if available and use |
| 4583 | * nand_decode_ext_id() otherwise. |
| 4584 | */ |
| 4585 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4586 | chip->manufacturer.desc->ops->detect) { |
| 4587 | /* The 3rd id byte holds MLC / multichip data */ |
| 4588 | chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4589 | chip->manufacturer.desc->ops->detect(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4590 | } else { |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4591 | nand_decode_ext_id(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4592 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | /* |
| 4596 | * Manufacturer initialization. This function is called for all NANDs including |
| 4597 | * ONFI and JEDEC compliant ones. |
| 4598 | * Manufacturer drivers should put all their specific initialization code in |
| 4599 | * their ->init() hook. |
| 4600 | */ |
| 4601 | static int nand_manufacturer_init(struct nand_chip *chip) |
| 4602 | { |
| 4603 | if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops || |
| 4604 | !chip->manufacturer.desc->ops->init) |
| 4605 | return 0; |
| 4606 | |
| 4607 | return chip->manufacturer.desc->ops->init(chip); |
| 4608 | } |
| 4609 | |
| 4610 | /* |
| 4611 | * Manufacturer cleanup. This function is called for all NANDs including |
| 4612 | * ONFI and JEDEC compliant ones. |
| 4613 | * Manufacturer drivers should put all their specific cleanup code in their |
| 4614 | * ->cleanup() hook. |
| 4615 | */ |
| 4616 | static void nand_manufacturer_cleanup(struct nand_chip *chip) |
| 4617 | { |
| 4618 | /* Release manufacturer private data */ |
| 4619 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
| 4620 | chip->manufacturer.desc->ops->cleanup) |
| 4621 | chip->manufacturer.desc->ops->cleanup(chip); |
| 4622 | } |
| 4623 | |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 4624 | static const char * |
| 4625 | nand_manufacturer_name(const struct nand_manufacturer *manufacturer) |
| 4626 | { |
| 4627 | return manufacturer ? manufacturer->name : "Unknown"; |
| 4628 | } |
| 4629 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4630 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4631 | * 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] | 4632 | */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 4633 | 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] | 4634 | { |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4635 | const struct nand_manufacturer *manufacturer; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4636 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4637 | int busw, ret; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4638 | u8 *id_data = chip->id.data; |
| 4639 | u8 maf_id, dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4641 | /* |
| 4642 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4643 | * after power-up. |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4644 | */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4645 | ret = nand_reset(chip, 0); |
| 4646 | if (ret) |
| 4647 | return ret; |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4648 | |
| 4649 | /* Select the device */ |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4650 | nand_select_target(chip, 0); |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4651 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4652 | /* Send the command for reading device ID */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4653 | ret = nand_readid_op(chip, 0, id_data, 2); |
| 4654 | if (ret) |
| 4655 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | |
| 4657 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4658 | maf_id = id_data[0]; |
| 4659 | dev_id = id_data[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4661 | /* |
| 4662 | * 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] | 4663 | * interface concerns can cause random data which looks like a |
| 4664 | * possibly credible NAND flash to appear. If the two results do |
| 4665 | * not match, ignore the device completely. |
| 4666 | */ |
| 4667 | |
Brian Norris | 4aef9b7 | 2012-09-24 20:40:48 -0700 | [diff] [blame] | 4668 | /* Read entire ID string */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4669 | ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data)); |
| 4670 | if (ret) |
| 4671 | return ret; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 4672 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4673 | if (id_data[0] != maf_id || id_data[1] != dev_id) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4674 | 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] | 4675 | maf_id, dev_id, id_data[0], id_data[1]); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4676 | return -ENODEV; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 4677 | } |
| 4678 | |
Jean-Louis Thekekara | 5158bd5 | 2017-06-29 19:08:30 +0200 | [diff] [blame] | 4679 | 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] | 4680 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4681 | /* Try to identify manufacturer */ |
| 4682 | manufacturer = nand_get_manufacturer(maf_id); |
| 4683 | chip->manufacturer.desc = manufacturer; |
| 4684 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4685 | if (!type) |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4686 | type = nand_flash_ids; |
| 4687 | |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4688 | /* |
| 4689 | * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic |
| 4690 | * override it. |
| 4691 | * This is required to make sure initial NAND bus width set by the |
| 4692 | * NAND controller driver is coherent with the real NAND bus width |
| 4693 | * (extracted by auto-detection code). |
| 4694 | */ |
| 4695 | busw = chip->options & NAND_BUSWIDTH_16; |
| 4696 | |
| 4697 | /* |
| 4698 | * The flag is only set (never cleared), reset it to its default value |
| 4699 | * before starting auto-detection. |
| 4700 | */ |
| 4701 | chip->options &= ~NAND_BUSWIDTH_16; |
| 4702 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4703 | for (; type->name != NULL; type++) { |
| 4704 | if (is_full_id_nand(type)) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4705 | if (find_full_id_nand(chip, type)) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4706 | goto ident_done; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4707 | } else if (dev_id == type->dev_id) { |
Brian Norris | db5b09f | 2015-05-22 10:43:12 -0700 | [diff] [blame] | 4708 | break; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4709 | } |
| 4710 | } |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4711 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4712 | if (!type->name || !type->pagesize) { |
Masahiro Yamada | 35fc519 | 2014-04-09 16:26:26 +0900 | [diff] [blame] | 4713 | /* Check if the chip is ONFI compliant */ |
Boris Brezillon | 1c325cc | 2018-09-07 00:38:50 +0200 | [diff] [blame] | 4714 | ret = nand_onfi_detect(chip); |
Miquel Raynal | bd0b643 | 2018-03-19 14:47:31 +0100 | [diff] [blame] | 4715 | if (ret < 0) |
| 4716 | return ret; |
| 4717 | else if (ret) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4718 | goto ident_done; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4719 | |
| 4720 | /* Check if the chip is JEDEC compliant */ |
Boris Brezillon | 8ae3fbf | 2018-09-07 00:38:51 +0200 | [diff] [blame] | 4721 | ret = nand_jedec_detect(chip); |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 4722 | if (ret < 0) |
| 4723 | return ret; |
| 4724 | else if (ret) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4725 | goto ident_done; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4726 | } |
| 4727 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4728 | if (!type->name) |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4729 | return -ENODEV; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4730 | |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4731 | chip->parameters.model = kstrdup(type->name, GFP_KERNEL); |
| 4732 | if (!chip->parameters.model) |
| 4733 | return -ENOMEM; |
Thomas Gleixner | ba0251fe | 2006-05-27 01:02:13 +0200 | [diff] [blame] | 4734 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4735 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4736 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4737 | if (!type->pagesize) |
| 4738 | nand_manufacturer_detect(chip); |
| 4739 | else |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4740 | nand_decode_id(chip, type); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4741 | |
Brian Norris | bf7a01b | 2012-07-13 09:28:24 -0700 | [diff] [blame] | 4742 | /* Get chip options */ |
| 4743 | chip->options |= type->options; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4744 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4745 | ident_done: |
Miquel Raynal | f4531b2 | 2018-03-19 14:47:26 +0100 | [diff] [blame] | 4746 | if (!mtd->name) |
| 4747 | mtd->name = chip->parameters.model; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4748 | |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 4749 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4750 | WARN_ON(busw & NAND_BUSWIDTH_16); |
| 4751 | nand_set_defaults(chip); |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 4752 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 4753 | /* |
| 4754 | * Check, if buswidth is correct. Hardware drivers should set |
| 4755 | * chip correct! |
| 4756 | */ |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4757 | 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] | 4758 | maf_id, dev_id); |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4759 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4760 | mtd->name); |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4761 | pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8, |
| 4762 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4763 | ret = -EINVAL; |
| 4764 | |
| 4765 | goto free_detect_allocation; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4766 | } |
| 4767 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4768 | nand_decode_bbm_options(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4769 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4770 | /* Calculate the address shift from the page size */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4771 | chip->page_shift = ffs(mtd->writesize) - 1; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4772 | /* Convert chipsize to number of pages per chip -1 */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4773 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4774 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4775 | chip->bbt_erase_shift = chip->phys_erase_shift = |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4776 | ffs(mtd->erasesize) - 1; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4777 | if (chip->chipsize & 0xffffffff) |
| 4778 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4779 | else { |
| 4780 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 4781 | chip->chip_shift += 32 - 1; |
| 4782 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4783 | |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame] | 4784 | if (chip->chip_shift - chip->page_shift > 16) |
| 4785 | chip->options |= NAND_ROW_ADDR_3; |
| 4786 | |
Artem Bityutskiy | 26d9be1 | 2011-04-28 20:26:59 +0300 | [diff] [blame] | 4787 | chip->badblockbits = 8; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4788 | |
Boris Brezillon | 3d4af7c | 2018-09-07 00:38:49 +0200 | [diff] [blame] | 4789 | nand_legacy_adjust_cmdfunc(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4790 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4791 | 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] | 4792 | maf_id, dev_id); |
Miquel Raynal | f4531b2 | 2018-03-19 14:47:26 +0100 | [diff] [blame] | 4793 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4794 | chip->parameters.model); |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4795 | pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n", |
Huang Shijie | 3723e93 | 2013-09-25 14:58:14 +0800 | [diff] [blame] | 4796 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4797 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4798 | return 0; |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4799 | |
| 4800 | free_detect_allocation: |
| 4801 | kfree(chip->parameters.model); |
| 4802 | |
| 4803 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4804 | } |
| 4805 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4806 | static const char * const nand_ecc_modes[] = { |
| 4807 | [NAND_ECC_NONE] = "none", |
| 4808 | [NAND_ECC_SOFT] = "soft", |
| 4809 | [NAND_ECC_HW] = "hw", |
| 4810 | [NAND_ECC_HW_SYNDROME] = "hw_syndrome", |
| 4811 | [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first", |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 4812 | [NAND_ECC_ON_DIE] = "on-die", |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4813 | }; |
| 4814 | |
| 4815 | static int of_get_nand_ecc_mode(struct device_node *np) |
| 4816 | { |
| 4817 | const char *pm; |
| 4818 | int err, i; |
| 4819 | |
| 4820 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4821 | if (err < 0) |
| 4822 | return err; |
| 4823 | |
| 4824 | for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++) |
| 4825 | if (!strcasecmp(pm, nand_ecc_modes[i])) |
| 4826 | return i; |
| 4827 | |
Rafał Miłecki | ae211bc | 2016-04-17 22:53:06 +0200 | [diff] [blame] | 4828 | /* |
| 4829 | * For backward compatibility we support few obsoleted values that don't |
| 4830 | * have their mappings into nand_ecc_modes_t anymore (they were merged |
| 4831 | * with other enums). |
| 4832 | */ |
| 4833 | if (!strcasecmp(pm, "soft_bch")) |
| 4834 | return NAND_ECC_SOFT; |
| 4835 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4836 | return -ENODEV; |
| 4837 | } |
| 4838 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4839 | static const char * const nand_ecc_algos[] = { |
| 4840 | [NAND_ECC_HAMMING] = "hamming", |
| 4841 | [NAND_ECC_BCH] = "bch", |
Stefan Agner | f308d73 | 2018-06-24 23:27:22 +0200 | [diff] [blame] | 4842 | [NAND_ECC_RS] = "rs", |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4843 | }; |
| 4844 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4845 | static int of_get_nand_ecc_algo(struct device_node *np) |
| 4846 | { |
| 4847 | const char *pm; |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4848 | int err, i; |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4849 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4850 | err = of_property_read_string(np, "nand-ecc-algo", &pm); |
| 4851 | if (!err) { |
| 4852 | for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++) |
| 4853 | if (!strcasecmp(pm, nand_ecc_algos[i])) |
| 4854 | return i; |
| 4855 | return -ENODEV; |
| 4856 | } |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4857 | |
| 4858 | /* |
| 4859 | * For backward compatibility we also read "nand-ecc-mode" checking |
| 4860 | * for some obsoleted values that were specifying ECC algorithm. |
| 4861 | */ |
| 4862 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4863 | if (err < 0) |
| 4864 | return err; |
| 4865 | |
| 4866 | if (!strcasecmp(pm, "soft")) |
| 4867 | return NAND_ECC_HAMMING; |
| 4868 | else if (!strcasecmp(pm, "soft_bch")) |
| 4869 | return NAND_ECC_BCH; |
| 4870 | |
| 4871 | return -ENODEV; |
| 4872 | } |
| 4873 | |
| 4874 | static int of_get_nand_ecc_step_size(struct device_node *np) |
| 4875 | { |
| 4876 | int ret; |
| 4877 | u32 val; |
| 4878 | |
| 4879 | ret = of_property_read_u32(np, "nand-ecc-step-size", &val); |
| 4880 | return ret ? ret : val; |
| 4881 | } |
| 4882 | |
| 4883 | static int of_get_nand_ecc_strength(struct device_node *np) |
| 4884 | { |
| 4885 | int ret; |
| 4886 | u32 val; |
| 4887 | |
| 4888 | ret = of_property_read_u32(np, "nand-ecc-strength", &val); |
| 4889 | return ret ? ret : val; |
| 4890 | } |
| 4891 | |
| 4892 | static int of_get_nand_bus_width(struct device_node *np) |
| 4893 | { |
| 4894 | u32 val; |
| 4895 | |
| 4896 | if (of_property_read_u32(np, "nand-bus-width", &val)) |
| 4897 | return 8; |
| 4898 | |
| 4899 | switch (val) { |
| 4900 | case 8: |
| 4901 | case 16: |
| 4902 | return val; |
| 4903 | default: |
| 4904 | return -EIO; |
| 4905 | } |
| 4906 | } |
| 4907 | |
| 4908 | static bool of_get_nand_on_flash_bbt(struct device_node *np) |
| 4909 | { |
| 4910 | return of_property_read_bool(np, "nand-on-flash-bbt"); |
| 4911 | } |
| 4912 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4913 | static int nand_dt_init(struct nand_chip *chip) |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4914 | { |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4915 | struct device_node *dn = nand_get_flash_node(chip); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4916 | int ecc_mode, ecc_algo, ecc_strength, ecc_step; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4917 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4918 | if (!dn) |
| 4919 | return 0; |
| 4920 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4921 | if (of_get_nand_bus_width(dn) == 16) |
| 4922 | chip->options |= NAND_BUSWIDTH_16; |
| 4923 | |
Stefan Agner | f922bd7 | 2018-06-24 23:27:23 +0200 | [diff] [blame] | 4924 | if (of_property_read_bool(dn, "nand-is-boot-medium")) |
| 4925 | chip->options |= NAND_IS_BOOT_MEDIUM; |
| 4926 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4927 | if (of_get_nand_on_flash_bbt(dn)) |
| 4928 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 4929 | |
| 4930 | ecc_mode = of_get_nand_ecc_mode(dn); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4931 | ecc_algo = of_get_nand_ecc_algo(dn); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4932 | ecc_strength = of_get_nand_ecc_strength(dn); |
| 4933 | ecc_step = of_get_nand_ecc_step_size(dn); |
| 4934 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4935 | if (ecc_mode >= 0) |
| 4936 | chip->ecc.mode = ecc_mode; |
| 4937 | |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4938 | if (ecc_algo >= 0) |
| 4939 | chip->ecc.algo = ecc_algo; |
| 4940 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4941 | if (ecc_strength >= 0) |
| 4942 | chip->ecc.strength = ecc_strength; |
| 4943 | |
| 4944 | if (ecc_step > 0) |
| 4945 | chip->ecc.size = ecc_step; |
| 4946 | |
Boris Brezillon | ba78ee0 | 2016-06-08 17:04:22 +0200 | [diff] [blame] | 4947 | if (of_property_read_bool(dn, "nand-ecc-maximize")) |
| 4948 | chip->ecc.options |= NAND_ECC_MAXIMIZE; |
| 4949 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4950 | return 0; |
| 4951 | } |
| 4952 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4953 | /** |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 4954 | * nand_scan_ident - Scan for the NAND device |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 4955 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4956 | * @maxchips: number of chips to scan for |
| 4957 | * @table: alternative NAND ID table |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4958 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4959 | * This is the first phase of the normal nand_scan() function. It reads the |
| 4960 | * flash ID and sets up MTD fields accordingly. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4961 | * |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 4962 | * This helper used to be called directly from controller drivers that needed |
| 4963 | * to tweak some ECC-related parameters before nand_scan_tail(). This separation |
| 4964 | * prevented dynamic allocations during this phase which was unconvenient and |
| 4965 | * 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] | 4966 | */ |
Boris Brezillon | 871a407 | 2018-08-04 22:59:22 +0200 | [diff] [blame] | 4967 | static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips, |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 4968 | struct nand_flash_dev *table) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4969 | { |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 4970 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 871a407 | 2018-08-04 22:59:22 +0200 | [diff] [blame] | 4971 | int nand_maf_id, nand_dev_id; |
| 4972 | unsigned int i; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4973 | int ret; |
| 4974 | |
Boris Brezillon | ae2294b | 2018-11-11 08:55:15 +0100 | [diff] [blame] | 4975 | /* Assume all dies are deselected when we enter nand_scan_ident(). */ |
| 4976 | chip->cur_cs = -1; |
| 4977 | |
Boris Brezillon | 013e629 | 2018-11-20 11:57:20 +0100 | [diff] [blame] | 4978 | mutex_init(&chip->lock); |
| 4979 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 4980 | /* Enforce the right timings for reset/detection */ |
| 4981 | onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0); |
| 4982 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4983 | ret = nand_dt_init(chip); |
| 4984 | if (ret) |
| 4985 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4986 | |
Brian Norris | f7a8e38 | 2016-01-05 10:39:45 -0800 | [diff] [blame] | 4987 | if (!mtd->name && mtd->dev.parent) |
| 4988 | mtd->name = dev_name(mtd->dev.parent); |
| 4989 | |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 4990 | /* |
| 4991 | * Start with chips->numchips = maxchips to let nand_select_target() do |
| 4992 | * its job. chip->numchips will be adjusted after. |
| 4993 | */ |
| 4994 | chip->numchips = maxchips; |
| 4995 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4996 | /* Set the default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4997 | nand_set_defaults(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4998 | |
Boris Brezillon | f2abfeb | 2018-11-11 08:55:23 +0100 | [diff] [blame] | 4999 | ret = nand_legacy_check_hooks(chip); |
| 5000 | if (ret) |
| 5001 | return ret; |
| 5002 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5003 | /* Read the flash type */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 5004 | ret = nand_detect(chip, table); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5005 | if (ret) { |
Ben Dooks | b1c6e6d | 2009-11-02 18:12:33 +0000 | [diff] [blame] | 5006 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 5007 | pr_warn("No NAND device found\n"); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 5008 | nand_deselect_target(chip); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5009 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5010 | } |
| 5011 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5012 | nand_maf_id = chip->id.data[0]; |
| 5013 | nand_dev_id = chip->id.data[1]; |
| 5014 | |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 5015 | nand_deselect_target(chip); |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 5016 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5017 | /* Check for a chip array */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5018 | for (i = 1; i < maxchips; i++) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5019 | u8 id[2]; |
| 5020 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 5021 | /* See comment in nand_get_flash_type for reset */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 5022 | nand_reset(chip, i); |
| 5023 | |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 5024 | nand_select_target(chip, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | /* Send the command for reading device ID */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5026 | nand_readid_op(chip, 0, id, sizeof(id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5028 | if (nand_maf_id != id[0] || nand_dev_id != id[1]) { |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 5029 | nand_deselect_target(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | break; |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 5031 | } |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 5032 | nand_deselect_target(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | } |
| 5034 | if (i > 1) |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 5035 | pr_info("%d chips detected\n", i); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | /* Store the number of chips and calc total size for mtd */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5038 | chip->numchips = i; |
| 5039 | mtd->size = i * chip->chipsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5040 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5041 | return 0; |
| 5042 | } |
| 5043 | |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5044 | static void nand_scan_ident_cleanup(struct nand_chip *chip) |
| 5045 | { |
| 5046 | kfree(chip->parameters.model); |
Miquel Raynal | 3d3fe3c | 2018-07-25 15:31:52 +0200 | [diff] [blame] | 5047 | kfree(chip->parameters.onfi); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5048 | } |
| 5049 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 5050 | static int nand_set_ecc_soft_ops(struct nand_chip *chip) |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5051 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 5052 | struct mtd_info *mtd = nand_to_mtd(chip); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5053 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 5054 | |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5055 | if (WARN_ON(ecc->mode != NAND_ECC_SOFT)) |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5056 | return -EINVAL; |
| 5057 | |
| 5058 | switch (ecc->algo) { |
| 5059 | case NAND_ECC_HAMMING: |
| 5060 | ecc->calculate = nand_calculate_ecc; |
| 5061 | ecc->correct = nand_correct_data; |
| 5062 | ecc->read_page = nand_read_page_swecc; |
| 5063 | ecc->read_subpage = nand_read_subpage; |
| 5064 | ecc->write_page = nand_write_page_swecc; |
| 5065 | ecc->read_page_raw = nand_read_page_raw; |
| 5066 | ecc->write_page_raw = nand_write_page_raw; |
| 5067 | ecc->read_oob = nand_read_oob_std; |
| 5068 | ecc->write_oob = nand_write_oob_std; |
| 5069 | if (!ecc->size) |
| 5070 | ecc->size = 256; |
| 5071 | ecc->bytes = 3; |
| 5072 | ecc->strength = 1; |
Boris Brezillon | 309600c | 2018-09-04 16:23:28 +0200 | [diff] [blame] | 5073 | |
| 5074 | if (IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC)) |
| 5075 | ecc->options |= NAND_ECC_SOFT_HAMMING_SM_ORDER; |
| 5076 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5077 | return 0; |
| 5078 | case NAND_ECC_BCH: |
| 5079 | if (!mtd_nand_has_bch()) { |
| 5080 | WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n"); |
| 5081 | return -EINVAL; |
| 5082 | } |
| 5083 | ecc->calculate = nand_bch_calculate_ecc; |
| 5084 | ecc->correct = nand_bch_correct_data; |
| 5085 | ecc->read_page = nand_read_page_swecc; |
| 5086 | ecc->read_subpage = nand_read_subpage; |
| 5087 | ecc->write_page = nand_write_page_swecc; |
| 5088 | ecc->read_page_raw = nand_read_page_raw; |
| 5089 | ecc->write_page_raw = nand_write_page_raw; |
| 5090 | ecc->read_oob = nand_read_oob_std; |
| 5091 | ecc->write_oob = nand_write_oob_std; |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 5092 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5093 | /* |
| 5094 | * Board driver should supply ecc.size and ecc.strength |
| 5095 | * values to select how many bits are correctable. |
| 5096 | * Otherwise, default to 4 bits for large page devices. |
| 5097 | */ |
| 5098 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 5099 | ecc->size = 512; |
| 5100 | ecc->strength = 4; |
| 5101 | } |
| 5102 | |
| 5103 | /* |
| 5104 | * if no ecc placement scheme was provided pickup the default |
| 5105 | * large page one. |
| 5106 | */ |
| 5107 | if (!mtd->ooblayout) { |
| 5108 | /* handle large page devices only */ |
| 5109 | if (mtd->oobsize < 64) { |
| 5110 | WARN(1, "OOB layout is required when using software BCH on small pages\n"); |
| 5111 | return -EINVAL; |
| 5112 | } |
| 5113 | |
| 5114 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 5115 | |
| 5116 | } |
| 5117 | |
| 5118 | /* |
| 5119 | * We can only maximize ECC config when the default layout is |
| 5120 | * used, otherwise we don't know how many bytes can really be |
| 5121 | * used. |
| 5122 | */ |
| 5123 | if (mtd->ooblayout == &nand_ooblayout_lp_ops && |
| 5124 | ecc->options & NAND_ECC_MAXIMIZE) { |
| 5125 | int steps, bytes; |
| 5126 | |
| 5127 | /* Always prefer 1k blocks over 512bytes ones */ |
| 5128 | ecc->size = 1024; |
| 5129 | steps = mtd->writesize / ecc->size; |
| 5130 | |
| 5131 | /* Reserve 2 bytes for the BBM */ |
| 5132 | bytes = (mtd->oobsize - 2) / steps; |
| 5133 | ecc->strength = bytes * 8 / fls(8 * ecc->size); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5134 | } |
| 5135 | |
| 5136 | /* See nand_bch_init() for details. */ |
| 5137 | ecc->bytes = 0; |
| 5138 | ecc->priv = nand_bch_init(mtd); |
| 5139 | if (!ecc->priv) { |
| 5140 | WARN(1, "BCH ECC initialization failed!\n"); |
| 5141 | return -EINVAL; |
| 5142 | } |
| 5143 | return 0; |
| 5144 | default: |
| 5145 | WARN(1, "Unsupported ECC algorithm!\n"); |
| 5146 | return -EINVAL; |
| 5147 | } |
| 5148 | } |
| 5149 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5150 | /** |
| 5151 | * nand_check_ecc_caps - check the sanity of preset ECC settings |
| 5152 | * @chip: nand chip info structure |
| 5153 | * @caps: ECC caps info structure |
| 5154 | * @oobavail: OOB size that the ECC engine can use |
| 5155 | * |
| 5156 | * When ECC step size and strength are already set, check if they are supported |
| 5157 | * by the controller and the calculated ECC bytes fit within the chip's OOB. |
| 5158 | * On success, the calculated ECC bytes is set. |
| 5159 | */ |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5160 | static int |
| 5161 | nand_check_ecc_caps(struct nand_chip *chip, |
| 5162 | const struct nand_ecc_caps *caps, int oobavail) |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5163 | { |
| 5164 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5165 | const struct nand_ecc_step_info *stepinfo; |
| 5166 | int preset_step = chip->ecc.size; |
| 5167 | int preset_strength = chip->ecc.strength; |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5168 | int ecc_bytes, nsteps = mtd->writesize / preset_step; |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5169 | int i, j; |
| 5170 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5171 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5172 | stepinfo = &caps->stepinfos[i]; |
| 5173 | |
| 5174 | if (stepinfo->stepsize != preset_step) |
| 5175 | continue; |
| 5176 | |
| 5177 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5178 | if (stepinfo->strengths[j] != preset_strength) |
| 5179 | continue; |
| 5180 | |
| 5181 | ecc_bytes = caps->calc_ecc_bytes(preset_step, |
| 5182 | preset_strength); |
| 5183 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5184 | return ecc_bytes; |
| 5185 | |
| 5186 | if (ecc_bytes * nsteps > oobavail) { |
| 5187 | pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB", |
| 5188 | preset_step, preset_strength); |
| 5189 | return -ENOSPC; |
| 5190 | } |
| 5191 | |
| 5192 | chip->ecc.bytes = ecc_bytes; |
| 5193 | |
| 5194 | return 0; |
| 5195 | } |
| 5196 | } |
| 5197 | |
| 5198 | pr_err("ECC (step, strength) = (%d, %d) not supported on this controller", |
| 5199 | preset_step, preset_strength); |
| 5200 | |
| 5201 | return -ENOTSUPP; |
| 5202 | } |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5203 | |
| 5204 | /** |
| 5205 | * nand_match_ecc_req - meet the chip's requirement with least ECC bytes |
| 5206 | * @chip: nand chip info structure |
| 5207 | * @caps: ECC engine caps info structure |
| 5208 | * @oobavail: OOB size that the ECC engine can use |
| 5209 | * |
| 5210 | * If a chip's ECC requirement is provided, try to meet it with the least |
| 5211 | * number of ECC bytes (i.e. with the largest number of OOB-free bytes). |
| 5212 | * On success, the chosen ECC settings are set. |
| 5213 | */ |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5214 | static int |
| 5215 | nand_match_ecc_req(struct nand_chip *chip, |
| 5216 | const struct nand_ecc_caps *caps, int oobavail) |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5217 | { |
| 5218 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5219 | const struct nand_ecc_step_info *stepinfo; |
| 5220 | int req_step = chip->ecc_step_ds; |
| 5221 | int req_strength = chip->ecc_strength_ds; |
| 5222 | int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total; |
| 5223 | int best_step, best_strength, best_ecc_bytes; |
| 5224 | int best_ecc_bytes_total = INT_MAX; |
| 5225 | int i, j; |
| 5226 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5227 | /* No information provided by the NAND chip */ |
| 5228 | if (!req_step || !req_strength) |
| 5229 | return -ENOTSUPP; |
| 5230 | |
| 5231 | /* number of correctable bits the chip requires in a page */ |
| 5232 | req_corr = mtd->writesize / req_step * req_strength; |
| 5233 | |
| 5234 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5235 | stepinfo = &caps->stepinfos[i]; |
| 5236 | step_size = stepinfo->stepsize; |
| 5237 | |
| 5238 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5239 | strength = stepinfo->strengths[j]; |
| 5240 | |
| 5241 | /* |
| 5242 | * If both step size and strength are smaller than the |
| 5243 | * chip's requirement, it is not easy to compare the |
| 5244 | * resulted reliability. |
| 5245 | */ |
| 5246 | if (step_size < req_step && strength < req_strength) |
| 5247 | continue; |
| 5248 | |
| 5249 | if (mtd->writesize % step_size) |
| 5250 | continue; |
| 5251 | |
| 5252 | nsteps = mtd->writesize / step_size; |
| 5253 | |
| 5254 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 5255 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5256 | continue; |
| 5257 | ecc_bytes_total = ecc_bytes * nsteps; |
| 5258 | |
| 5259 | if (ecc_bytes_total > oobavail || |
| 5260 | strength * nsteps < req_corr) |
| 5261 | continue; |
| 5262 | |
| 5263 | /* |
| 5264 | * We assume the best is to meet the chip's requrement |
| 5265 | * with the least number of ECC bytes. |
| 5266 | */ |
| 5267 | if (ecc_bytes_total < best_ecc_bytes_total) { |
| 5268 | best_ecc_bytes_total = ecc_bytes_total; |
| 5269 | best_step = step_size; |
| 5270 | best_strength = strength; |
| 5271 | best_ecc_bytes = ecc_bytes; |
| 5272 | } |
| 5273 | } |
| 5274 | } |
| 5275 | |
| 5276 | if (best_ecc_bytes_total == INT_MAX) |
| 5277 | return -ENOTSUPP; |
| 5278 | |
| 5279 | chip->ecc.size = best_step; |
| 5280 | chip->ecc.strength = best_strength; |
| 5281 | chip->ecc.bytes = best_ecc_bytes; |
| 5282 | |
| 5283 | return 0; |
| 5284 | } |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5285 | |
| 5286 | /** |
| 5287 | * nand_maximize_ecc - choose the max ECC strength available |
| 5288 | * @chip: nand chip info structure |
| 5289 | * @caps: ECC engine caps info structure |
| 5290 | * @oobavail: OOB size that the ECC engine can use |
| 5291 | * |
| 5292 | * Choose the max ECC strength that is supported on the controller, and can fit |
| 5293 | * within the chip's OOB. On success, the chosen ECC settings are set. |
| 5294 | */ |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5295 | static int |
| 5296 | nand_maximize_ecc(struct nand_chip *chip, |
| 5297 | const struct nand_ecc_caps *caps, int oobavail) |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5298 | { |
| 5299 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5300 | const struct nand_ecc_step_info *stepinfo; |
| 5301 | int step_size, strength, nsteps, ecc_bytes, corr; |
| 5302 | int best_corr = 0; |
| 5303 | int best_step = 0; |
| 5304 | int best_strength, best_ecc_bytes; |
| 5305 | int i, j; |
| 5306 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5307 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5308 | stepinfo = &caps->stepinfos[i]; |
| 5309 | step_size = stepinfo->stepsize; |
| 5310 | |
| 5311 | /* If chip->ecc.size is already set, respect it */ |
| 5312 | if (chip->ecc.size && step_size != chip->ecc.size) |
| 5313 | continue; |
| 5314 | |
| 5315 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5316 | strength = stepinfo->strengths[j]; |
| 5317 | |
| 5318 | if (mtd->writesize % step_size) |
| 5319 | continue; |
| 5320 | |
| 5321 | nsteps = mtd->writesize / step_size; |
| 5322 | |
| 5323 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 5324 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5325 | continue; |
| 5326 | |
| 5327 | if (ecc_bytes * nsteps > oobavail) |
| 5328 | continue; |
| 5329 | |
| 5330 | corr = strength * nsteps; |
| 5331 | |
| 5332 | /* |
| 5333 | * If the number of correctable bits is the same, |
| 5334 | * bigger step_size has more reliability. |
| 5335 | */ |
| 5336 | if (corr > best_corr || |
| 5337 | (corr == best_corr && step_size > best_step)) { |
| 5338 | best_corr = corr; |
| 5339 | best_step = step_size; |
| 5340 | best_strength = strength; |
| 5341 | best_ecc_bytes = ecc_bytes; |
| 5342 | } |
| 5343 | } |
| 5344 | } |
| 5345 | |
| 5346 | if (!best_corr) |
| 5347 | return -ENOTSUPP; |
| 5348 | |
| 5349 | chip->ecc.size = best_step; |
| 5350 | chip->ecc.strength = best_strength; |
| 5351 | chip->ecc.bytes = best_ecc_bytes; |
| 5352 | |
| 5353 | return 0; |
| 5354 | } |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5355 | |
Abhishek Sahu | 181ace9 | 2018-06-20 12:57:28 +0530 | [diff] [blame] | 5356 | /** |
| 5357 | * nand_ecc_choose_conf - Set the ECC strength and ECC step size |
| 5358 | * @chip: nand chip info structure |
| 5359 | * @caps: ECC engine caps info structure |
| 5360 | * @oobavail: OOB size that the ECC engine can use |
| 5361 | * |
| 5362 | * Choose the ECC configuration according to following logic |
| 5363 | * |
| 5364 | * 1. If both ECC step size and ECC strength are already set (usually by DT) |
| 5365 | * then check if it is supported by this controller. |
| 5366 | * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength. |
| 5367 | * 3. Otherwise, try to match the ECC step size and ECC strength closest |
| 5368 | * to the chip's requirement. If available OOB size can't fit the chip |
| 5369 | * requirement then fallback to the maximum ECC step size and ECC strength. |
| 5370 | * |
| 5371 | * On success, the chosen ECC settings are set. |
| 5372 | */ |
| 5373 | int nand_ecc_choose_conf(struct nand_chip *chip, |
| 5374 | const struct nand_ecc_caps *caps, int oobavail) |
| 5375 | { |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5376 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5377 | |
| 5378 | if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize)) |
| 5379 | return -EINVAL; |
| 5380 | |
Abhishek Sahu | 181ace9 | 2018-06-20 12:57:28 +0530 | [diff] [blame] | 5381 | if (chip->ecc.size && chip->ecc.strength) |
| 5382 | return nand_check_ecc_caps(chip, caps, oobavail); |
| 5383 | |
| 5384 | if (chip->ecc.options & NAND_ECC_MAXIMIZE) |
| 5385 | return nand_maximize_ecc(chip, caps, oobavail); |
| 5386 | |
| 5387 | if (!nand_match_ecc_req(chip, caps, oobavail)) |
| 5388 | return 0; |
| 5389 | |
| 5390 | return nand_maximize_ecc(chip, caps, oobavail); |
| 5391 | } |
| 5392 | EXPORT_SYMBOL_GPL(nand_ecc_choose_conf); |
| 5393 | |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5394 | /* |
| 5395 | * Check if the chip configuration meet the datasheet requirements. |
| 5396 | |
| 5397 | * If our configuration corrects A bits per B bytes and the minimum |
| 5398 | * required correction level is X bits per Y bytes, then we must ensure |
| 5399 | * both of the following are true: |
| 5400 | * |
| 5401 | * (1) A / B >= X / Y |
| 5402 | * (2) A >= X |
| 5403 | * |
| 5404 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 5405 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 5406 | * in the same sector. |
| 5407 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 5408 | static bool nand_ecc_strength_good(struct nand_chip *chip) |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5409 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 5410 | struct mtd_info *mtd = nand_to_mtd(chip); |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5411 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 5412 | int corr, ds_corr; |
| 5413 | |
| 5414 | if (ecc->size == 0 || chip->ecc_step_ds == 0) |
| 5415 | /* Not enough information */ |
| 5416 | return true; |
| 5417 | |
| 5418 | /* |
| 5419 | * We get the number of corrected bits per page to compare |
| 5420 | * the correction density. |
| 5421 | */ |
| 5422 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
| 5423 | ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; |
| 5424 | |
| 5425 | return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; |
| 5426 | } |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5427 | |
| 5428 | /** |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 5429 | * nand_scan_tail - Scan for the NAND device |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5430 | * @chip: NAND chip object |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5431 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5432 | * This is the second phase of the normal nand_scan() function. It fills out |
| 5433 | * all the uninitialized function pointers with the defaults and scans for a |
| 5434 | * bad block table if appropriate. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5435 | */ |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5436 | static int nand_scan_tail(struct nand_chip *chip) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5437 | { |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5438 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5439 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5440 | int ret, i; |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5441 | |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 5442 | /* 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] | 5443 | if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5444 | !(chip->bbt_options & NAND_BBT_USE_FLASH))) { |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5445 | return -EINVAL; |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5446 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 5447 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5448 | chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL); |
Boris Brezillon | aeb93af | 2017-12-05 12:09:29 +0100 | [diff] [blame] | 5449 | if (!chip->data_buf) |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5450 | return -ENOMEM; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 5451 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5452 | /* |
| 5453 | * FIXME: some NAND manufacturer drivers expect the first die to be |
| 5454 | * selected when manufacturer->init() is called. They should be fixed |
| 5455 | * to explictly select the relevant die when interacting with the NAND |
| 5456 | * chip. |
| 5457 | */ |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 5458 | nand_select_target(chip, 0); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5459 | ret = nand_manufacturer_init(chip); |
Boris Brezillon | 1d01785 | 2018-11-11 08:55:14 +0100 | [diff] [blame] | 5460 | nand_deselect_target(chip); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5461 | if (ret) |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5462 | goto err_free_buf; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5463 | |
David Woodhouse | 7dcdcbef | 2006-10-21 17:09:53 +0100 | [diff] [blame] | 5464 | /* Set the internal oob buffer location, just after the page data */ |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5465 | chip->oob_poi = chip->data_buf + mtd->writesize; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5466 | |
| 5467 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5468 | * If no default placement scheme is given, select an appropriate one. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5469 | */ |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5470 | if (!mtd->ooblayout && |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5471 | !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5472 | switch (mtd->oobsize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | case 8: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | case 16: |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 5475 | mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | break; |
| 5477 | case 64: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5478 | case 128: |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 5479 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops); |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5480 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | default: |
Miquel Raynal | 882fd15 | 2017-08-26 17:19:15 +0200 | [diff] [blame] | 5482 | /* |
| 5483 | * Expose the whole OOB area to users if ECC_NONE |
| 5484 | * is passed. We could do that for all kind of |
| 5485 | * ->oobsize, but we must keep the old large/small |
| 5486 | * page with ECC layout when ->oobsize <= 128 for |
| 5487 | * compatibility reasons. |
| 5488 | */ |
| 5489 | if (ecc->mode == NAND_ECC_NONE) { |
| 5490 | mtd_set_ooblayout(mtd, |
| 5491 | &nand_ooblayout_lp_ops); |
| 5492 | break; |
| 5493 | } |
| 5494 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5495 | WARN(1, "No oob scheme defined for oobsize %d\n", |
| 5496 | mtd->oobsize); |
| 5497 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5498 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5499 | } |
| 5500 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5501 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5502 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5503 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5504 | * selected and we have 256 byte pagesize fallback to software ECC |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5505 | */ |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 5506 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5507 | switch (ecc->mode) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 5508 | case NAND_ECC_HW_OOB_FIRST: |
| 5509 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5510 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5511 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 5512 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5513 | goto err_nand_manuf_cleanup; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 5514 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5515 | if (!ecc->read_page) |
| 5516 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Mathieu Malaterre | 3175e12 | 2019-01-16 20:50:04 +0100 | [diff] [blame] | 5517 | /* fall through */ |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 5518 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5519 | case NAND_ECC_HW: |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5520 | /* Use standard hwecc read page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5521 | if (!ecc->read_page) |
| 5522 | ecc->read_page = nand_read_page_hwecc; |
| 5523 | if (!ecc->write_page) |
| 5524 | ecc->write_page = nand_write_page_hwecc; |
| 5525 | if (!ecc->read_page_raw) |
| 5526 | ecc->read_page_raw = nand_read_page_raw; |
| 5527 | if (!ecc->write_page_raw) |
| 5528 | ecc->write_page_raw = nand_write_page_raw; |
| 5529 | if (!ecc->read_oob) |
| 5530 | ecc->read_oob = nand_read_oob_std; |
| 5531 | if (!ecc->write_oob) |
| 5532 | ecc->write_oob = nand_write_oob_std; |
| 5533 | if (!ecc->read_subpage) |
| 5534 | ecc->read_subpage = nand_read_subpage; |
Helmut Schaa | 44991b3 | 2014-04-09 11:13:24 +0200 | [diff] [blame] | 5535 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5536 | ecc->write_subpage = nand_write_subpage_hwecc; |
Mathieu Malaterre | 3175e12 | 2019-01-16 20:50:04 +0100 | [diff] [blame] | 5537 | /* fall through */ |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 5538 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5539 | case NAND_ECC_HW_SYNDROME: |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5540 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 5541 | (!ecc->read_page || |
| 5542 | ecc->read_page == nand_read_page_hwecc || |
| 5543 | !ecc->write_page || |
| 5544 | ecc->write_page == nand_write_page_hwecc)) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5545 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 5546 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5547 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5548 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5549 | /* Use standard syndrome read/write page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5550 | if (!ecc->read_page) |
| 5551 | ecc->read_page = nand_read_page_syndrome; |
| 5552 | if (!ecc->write_page) |
| 5553 | ecc->write_page = nand_write_page_syndrome; |
| 5554 | if (!ecc->read_page_raw) |
| 5555 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 5556 | if (!ecc->write_page_raw) |
| 5557 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 5558 | if (!ecc->read_oob) |
| 5559 | ecc->read_oob = nand_read_oob_syndrome; |
| 5560 | if (!ecc->write_oob) |
| 5561 | ecc->write_oob = nand_write_oob_syndrome; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 5562 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5563 | if (mtd->writesize >= ecc->size) { |
| 5564 | if (!ecc->strength) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5565 | WARN(1, "Driver must set ecc.strength when using hardware ECC\n"); |
| 5566 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5567 | goto err_nand_manuf_cleanup; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 5568 | } |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5569 | break; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 5570 | } |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 5571 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 5572 | ecc->size, mtd->writesize); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5573 | ecc->mode = NAND_ECC_SOFT; |
Rafał Miłecki | e9d4fae | 2016-04-17 22:53:02 +0200 | [diff] [blame] | 5574 | ecc->algo = NAND_ECC_HAMMING; |
Mathieu Malaterre | 3175e12 | 2019-01-16 20:50:04 +0100 | [diff] [blame] | 5575 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5576 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5577 | case NAND_ECC_SOFT: |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 5578 | ret = nand_set_ecc_soft_ops(chip); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5579 | if (ret) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5580 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5581 | goto err_nand_manuf_cleanup; |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 5582 | } |
| 5583 | break; |
| 5584 | |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 5585 | case NAND_ECC_ON_DIE: |
| 5586 | if (!ecc->read_page || !ecc->write_page) { |
| 5587 | WARN(1, "No ECC functions supplied; on-die 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; |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 5590 | } |
| 5591 | if (!ecc->read_oob) |
| 5592 | ecc->read_oob = nand_read_oob_std; |
| 5593 | if (!ecc->write_oob) |
| 5594 | ecc->write_oob = nand_write_oob_std; |
| 5595 | break; |
| 5596 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5597 | case NAND_ECC_NONE: |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 5598 | 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] | 5599 | ecc->read_page = nand_read_page_raw; |
| 5600 | ecc->write_page = nand_write_page_raw; |
| 5601 | ecc->read_oob = nand_read_oob_std; |
| 5602 | ecc->read_page_raw = nand_read_page_raw; |
| 5603 | ecc->write_page_raw = nand_write_page_raw; |
| 5604 | ecc->write_oob = nand_write_oob_std; |
| 5605 | ecc->size = mtd->writesize; |
| 5606 | ecc->bytes = 0; |
| 5607 | ecc->strength = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5608 | break; |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 5609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | default: |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5611 | WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode); |
| 5612 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5613 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5614 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5615 | |
Boris Brezillon | aeb93af | 2017-12-05 12:09:29 +0100 | [diff] [blame] | 5616 | if (ecc->correct || ecc->calculate) { |
| 5617 | ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 5618 | ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 5619 | if (!ecc->calc_buf || !ecc->code_buf) { |
| 5620 | ret = -ENOMEM; |
| 5621 | goto err_nand_manuf_cleanup; |
| 5622 | } |
| 5623 | } |
| 5624 | |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 5625 | /* For many systems, the standard OOB write also works for raw */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5626 | if (!ecc->read_oob_raw) |
| 5627 | ecc->read_oob_raw = ecc->read_oob; |
| 5628 | if (!ecc->write_oob_raw) |
| 5629 | ecc->write_oob_raw = ecc->write_oob; |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 5630 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5631 | /* propagate ecc info to mtd_info */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5632 | mtd->ecc_strength = ecc->strength; |
| 5633 | mtd->ecc_step_size = ecc->size; |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5634 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 5635 | /* |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5636 | * 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] | 5637 | * mode. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5638 | */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5639 | ecc->steps = mtd->writesize / ecc->size; |
| 5640 | if (ecc->steps * ecc->size != mtd->writesize) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5641 | WARN(1, "Invalid ECC parameters\n"); |
| 5642 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5643 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5644 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5645 | ecc->total = ecc->steps * ecc->bytes; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 5646 | if (ecc->total > mtd->oobsize) { |
| 5647 | WARN(1, "Total number of ECC bytes exceeded oobsize\n"); |
| 5648 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5649 | goto err_nand_manuf_cleanup; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 5650 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5651 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5652 | /* |
| 5653 | * The number of bytes available for a client to place data into |
| 5654 | * the out of band area. |
| 5655 | */ |
| 5656 | ret = mtd_ooblayout_count_freebytes(mtd); |
| 5657 | if (ret < 0) |
| 5658 | ret = 0; |
| 5659 | |
| 5660 | mtd->oobavail = ret; |
| 5661 | |
| 5662 | /* ECC sanity check: warn if it's too weak */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame] | 5663 | if (!nand_ecc_strength_good(chip)) |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5664 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 5665 | mtd->name); |
| 5666 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5667 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 5668 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5669 | switch (ecc->steps) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 5670 | case 2: |
| 5671 | mtd->subpage_sft = 1; |
| 5672 | break; |
| 5673 | case 4: |
| 5674 | case 8: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5675 | case 16: |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 5676 | mtd->subpage_sft = 2; |
| 5677 | break; |
| 5678 | } |
| 5679 | } |
| 5680 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 5681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | /* Invalidate the pagebuffer reference */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5683 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5684 | |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 5685 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 5686 | switch (ecc->mode) { |
| 5687 | case NAND_ECC_SOFT: |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 5688 | if (chip->page_shift > 9) |
| 5689 | chip->options |= NAND_SUBPAGE_READ; |
| 5690 | break; |
| 5691 | |
| 5692 | default: |
| 5693 | break; |
| 5694 | } |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 5695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | /* Fill in remaining MTD driver data */ |
Huang Shijie | 963d1c2 | 2013-09-25 14:58:21 +0800 | [diff] [blame] | 5697 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 5698 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 5699 | MTD_CAP_NANDFLASH; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5700 | mtd->_erase = nand_erase; |
| 5701 | mtd->_point = NULL; |
| 5702 | mtd->_unpoint = NULL; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5703 | mtd->_panic_write = panic_nand_write; |
| 5704 | mtd->_read_oob = nand_read_oob; |
| 5705 | mtd->_write_oob = nand_write_oob; |
| 5706 | mtd->_sync = nand_sync; |
| 5707 | mtd->_lock = NULL; |
| 5708 | mtd->_unlock = NULL; |
| 5709 | mtd->_suspend = nand_suspend; |
| 5710 | mtd->_resume = nand_resume; |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 5711 | mtd->_reboot = nand_shutdown; |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 5712 | mtd->_block_isreserved = nand_block_isreserved; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5713 | mtd->_block_isbad = nand_block_isbad; |
| 5714 | mtd->_block_markbad = nand_block_markbad; |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 5715 | mtd->_max_bad_blocks = nand_max_bad_blocks; |
Anatolij Gustschin | cbcab65 | 2010-12-16 23:42:16 +0100 | [diff] [blame] | 5716 | mtd->writebufsize = mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5717 | |
Shmulik Ladkani | ea3b2ea | 2012-06-08 18:29:06 +0300 | [diff] [blame] | 5718 | /* |
| 5719 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 5720 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 5721 | * properly set. |
| 5722 | */ |
| 5723 | if (!mtd->bitflip_threshold) |
Brian Norris | 240181f | 2015-01-12 12:51:29 -0800 | [diff] [blame] | 5724 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5725 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5726 | /* Initialize the ->data_interface field. */ |
| 5727 | ret = nand_init_data_interface(chip); |
| 5728 | if (ret) |
| 5729 | goto err_nand_manuf_cleanup; |
| 5730 | |
| 5731 | /* Enter fastest possible mode on all dies. */ |
| 5732 | for (i = 0; i < chip->numchips; i++) { |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5733 | ret = nand_setup_data_interface(chip, i); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5734 | if (ret) |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 5735 | goto err_nand_manuf_cleanup; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5736 | } |
| 5737 | |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 5738 | /* Check, if we should skip the bad block table scan */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5739 | if (chip->options & NAND_SKIP_BBTSCAN) |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 5740 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5741 | |
| 5742 | /* Build bad block table */ |
Boris Brezillon | e80eba7 | 2018-07-05 12:27:31 +0200 | [diff] [blame] | 5743 | ret = nand_create_bbt(chip); |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 5744 | if (ret) |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 5745 | goto err_nand_manuf_cleanup; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5746 | |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 5747 | return 0; |
| 5748 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5749 | |
| 5750 | err_nand_manuf_cleanup: |
| 5751 | nand_manufacturer_cleanup(chip); |
| 5752 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5753 | err_free_buf: |
| 5754 | kfree(chip->data_buf); |
| 5755 | kfree(ecc->code_buf); |
| 5756 | kfree(ecc->calc_buf); |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5757 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5758 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5759 | } |
| 5760 | |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5761 | static int nand_attach(struct nand_chip *chip) |
| 5762 | { |
| 5763 | if (chip->controller->ops && chip->controller->ops->attach_chip) |
| 5764 | return chip->controller->ops->attach_chip(chip); |
| 5765 | |
| 5766 | return 0; |
| 5767 | } |
| 5768 | |
| 5769 | static void nand_detach(struct nand_chip *chip) |
| 5770 | { |
| 5771 | if (chip->controller->ops && chip->controller->ops->detach_chip) |
| 5772 | chip->controller->ops->detach_chip(chip); |
| 5773 | } |
| 5774 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5775 | /** |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5776 | * nand_scan_with_ids - [NAND Interface] Scan for the NAND device |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5777 | * @chip: NAND chip object |
Boris Brezillon | 800342d | 2018-08-04 22:59:23 +0200 | [diff] [blame] | 5778 | * @maxchips: number of chips to scan for. |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5779 | * @ids: optional flash IDs table |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5780 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5781 | * This fills out all the uninitialized function pointers with the defaults. |
| 5782 | * 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] | 5783 | * appropriate values. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5784 | */ |
Boris Brezillon | 871a407 | 2018-08-04 22:59:22 +0200 | [diff] [blame] | 5785 | int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips, |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5786 | struct nand_flash_dev *ids) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5787 | { |
| 5788 | int ret; |
| 5789 | |
Boris Brezillon | 800342d | 2018-08-04 22:59:23 +0200 | [diff] [blame] | 5790 | if (!maxchips) |
| 5791 | return -EINVAL; |
| 5792 | |
| 5793 | ret = nand_scan_ident(chip, maxchips, ids); |
| 5794 | if (ret) |
| 5795 | return ret; |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5796 | |
| 5797 | ret = nand_attach(chip); |
| 5798 | if (ret) |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5799 | goto cleanup_ident; |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5800 | |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5801 | ret = nand_scan_tail(chip); |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5802 | if (ret) |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5803 | goto detach_chip; |
| 5804 | |
| 5805 | return 0; |
| 5806 | |
| 5807 | detach_chip: |
| 5808 | nand_detach(chip); |
| 5809 | cleanup_ident: |
| 5810 | nand_scan_ident_cleanup(chip); |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5811 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5812 | return ret; |
| 5813 | } |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5814 | EXPORT_SYMBOL(nand_scan_with_ids); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5816 | /** |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5817 | * nand_cleanup - [NAND Interface] Free resources held by the NAND device |
| 5818 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5819 | */ |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5820 | void nand_cleanup(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5821 | { |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5822 | if (chip->ecc.mode == NAND_ECC_SOFT && |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5823 | chip->ecc.algo == NAND_ECC_BCH) |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 5824 | nand_bch_free((struct nand_bch_control *)chip->ecc.priv); |
| 5825 | |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 5826 | /* Free bad block table memory */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5827 | kfree(chip->bbt); |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5828 | kfree(chip->data_buf); |
| 5829 | kfree(chip->ecc.code_buf); |
| 5830 | kfree(chip->ecc.calc_buf); |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 5831 | |
| 5832 | /* Free bad block descriptor memory */ |
| 5833 | if (chip->badblock_pattern && chip->badblock_pattern->options |
| 5834 | & NAND_BBT_DYNAMICSTRUCT) |
| 5835 | kfree(chip->badblock_pattern); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5836 | |
| 5837 | /* Free manufacturer priv data. */ |
| 5838 | nand_manufacturer_cleanup(chip); |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5839 | |
| 5840 | /* Free controller specific allocations after chip identification */ |
| 5841 | nand_detach(chip); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5842 | |
| 5843 | /* Free identification phase allocations */ |
| 5844 | nand_scan_ident_cleanup(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5845 | } |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5846 | |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5847 | EXPORT_SYMBOL_GPL(nand_cleanup); |
| 5848 | |
| 5849 | /** |
| 5850 | * nand_release - [NAND Interface] Unregister the MTD device and free resources |
| 5851 | * held by the NAND device |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 5852 | * @chip: NAND chip object |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5853 | */ |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 5854 | void nand_release(struct nand_chip *chip) |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5855 | { |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 5856 | mtd_device_unregister(nand_to_mtd(chip)); |
| 5857 | nand_cleanup(chip); |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5858 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5859 | EXPORT_SYMBOL_GPL(nand_release); |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 5860 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5861 | MODULE_LICENSE("GPL"); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5862 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 5863 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5864 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |