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 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 52 | static int nand_get_device(struct mtd_info *mtd, int new_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 54 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 55 | struct mtd_oob_ops *ops); |
| 56 | |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 57 | /* Define default oob placement schemes for large and small page devices */ |
| 58 | static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section, |
| 59 | struct mtd_oob_region *oobregion) |
| 60 | { |
| 61 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 62 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 63 | |
| 64 | if (section > 1) |
| 65 | return -ERANGE; |
| 66 | |
| 67 | if (!section) { |
| 68 | oobregion->offset = 0; |
Miquel Raynal | f7f8c17 | 2017-07-05 08:51:09 +0200 | [diff] [blame] | 69 | if (mtd->oobsize == 16) |
| 70 | oobregion->length = 4; |
| 71 | else |
| 72 | oobregion->length = 3; |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 73 | } else { |
Miquel Raynal | f7f8c17 | 2017-07-05 08:51:09 +0200 | [diff] [blame] | 74 | if (mtd->oobsize == 8) |
| 75 | return -ERANGE; |
| 76 | |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 77 | oobregion->offset = 6; |
| 78 | oobregion->length = ecc->total - 4; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section, |
| 85 | struct mtd_oob_region *oobregion) |
| 86 | { |
| 87 | if (section > 1) |
| 88 | return -ERANGE; |
| 89 | |
| 90 | if (mtd->oobsize == 16) { |
| 91 | if (section) |
| 92 | return -ERANGE; |
| 93 | |
| 94 | oobregion->length = 8; |
| 95 | oobregion->offset = 8; |
| 96 | } else { |
| 97 | oobregion->length = 2; |
| 98 | if (!section) |
| 99 | oobregion->offset = 3; |
| 100 | else |
| 101 | oobregion->offset = 6; |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = { |
| 108 | .ecc = nand_ooblayout_ecc_sp, |
| 109 | .free = nand_ooblayout_free_sp, |
| 110 | }; |
| 111 | EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops); |
| 112 | |
| 113 | static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section, |
| 114 | struct mtd_oob_region *oobregion) |
| 115 | { |
| 116 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 117 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 118 | |
Miquel Raynal | 882fd15 | 2017-08-26 17:19:15 +0200 | [diff] [blame] | 119 | if (section || !ecc->total) |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 120 | return -ERANGE; |
| 121 | |
| 122 | oobregion->length = ecc->total; |
| 123 | oobregion->offset = mtd->oobsize - oobregion->length; |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section, |
| 129 | struct mtd_oob_region *oobregion) |
| 130 | { |
| 131 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 132 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 133 | |
| 134 | if (section) |
| 135 | return -ERANGE; |
| 136 | |
| 137 | oobregion->length = mtd->oobsize - ecc->total - 2; |
| 138 | oobregion->offset = 2; |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = { |
| 144 | .ecc = nand_ooblayout_ecc_lp, |
| 145 | .free = nand_ooblayout_free_lp, |
| 146 | }; |
| 147 | EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops); |
Thomas Gleixner | d470a97 | 2006-05-23 23:48:57 +0200 | [diff] [blame] | 148 | |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 149 | /* |
| 150 | * Support the old "large page" layout used for 1-bit Hamming ECC where ECC |
| 151 | * are placed at a fixed offset. |
| 152 | */ |
| 153 | static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section, |
| 154 | struct mtd_oob_region *oobregion) |
| 155 | { |
| 156 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 157 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 158 | |
| 159 | if (section) |
| 160 | return -ERANGE; |
| 161 | |
| 162 | switch (mtd->oobsize) { |
| 163 | case 64: |
| 164 | oobregion->offset = 40; |
| 165 | break; |
| 166 | case 128: |
| 167 | oobregion->offset = 80; |
| 168 | break; |
| 169 | default: |
| 170 | return -EINVAL; |
| 171 | } |
| 172 | |
| 173 | oobregion->length = ecc->total; |
| 174 | if (oobregion->offset + oobregion->length > mtd->oobsize) |
| 175 | return -ERANGE; |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section, |
| 181 | struct mtd_oob_region *oobregion) |
| 182 | { |
| 183 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 184 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 185 | int ecc_offset = 0; |
| 186 | |
| 187 | if (section < 0 || section > 1) |
| 188 | return -ERANGE; |
| 189 | |
| 190 | switch (mtd->oobsize) { |
| 191 | case 64: |
| 192 | ecc_offset = 40; |
| 193 | break; |
| 194 | case 128: |
| 195 | ecc_offset = 80; |
| 196 | break; |
| 197 | default: |
| 198 | return -EINVAL; |
| 199 | } |
| 200 | |
| 201 | if (section == 0) { |
| 202 | oobregion->offset = 2; |
| 203 | oobregion->length = ecc_offset - 2; |
| 204 | } else { |
| 205 | oobregion->offset = ecc_offset + ecc->total; |
| 206 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Colin Ian King | d4ed3b9 | 2017-05-04 13:11:00 +0100 | [diff] [blame] | 212 | static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = { |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 213 | .ecc = nand_ooblayout_ecc_lp_hamming, |
| 214 | .free = nand_ooblayout_free_lp_hamming, |
| 215 | }; |
| 216 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 217 | static int check_offs_len(struct mtd_info *mtd, |
| 218 | loff_t ofs, uint64_t len) |
| 219 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 220 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 221 | int ret = 0; |
| 222 | |
| 223 | /* Start address must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 224 | if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 225 | pr_debug("%s: unaligned address\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 226 | ret = -EINVAL; |
| 227 | } |
| 228 | |
| 229 | /* Length must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 230 | if (len & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 231 | pr_debug("%s: length not block aligned\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 232 | ret = -EINVAL; |
| 233 | } |
| 234 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 235 | return ret; |
| 236 | } |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | /** |
| 239 | * nand_release_device - [GENERIC] release chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 240 | * @mtd: MTD device structure |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 241 | * |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 242 | * Release chip lock and wake up anyone waiting on the device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 244 | static void nand_release_device(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 246 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 248 | /* Release the controller and the chip */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 249 | spin_lock(&chip->controller->lock); |
| 250 | chip->controller->active = NULL; |
| 251 | chip->state = FL_READY; |
| 252 | wake_up(&chip->controller->wq); |
| 253 | spin_unlock(&chip->controller->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 258 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 259 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 261 | * Check, if the block is bad. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | */ |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 263 | static int nand_block_bad(struct nand_chip *chip, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 265 | struct mtd_info *mtd = nand_to_mtd(chip); |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 266 | int page, page_end, res; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 267 | u8 bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Brian Norris | 5fb1549 | 2011-05-31 16:31:21 -0700 | [diff] [blame] | 269 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 270 | ofs += mtd->erasesize - mtd->writesize; |
| 271 | |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 272 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 273 | page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1); |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 274 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 275 | for (; page < page_end; page++) { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 276 | res = chip->ecc.read_oob(chip, page); |
Abhishek Sahu | e9893e6 | 2018-06-13 14:32:36 +0530 | [diff] [blame] | 277 | if (res < 0) |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 278 | return res; |
| 279 | |
| 280 | bad = chip->oob_poi[chip->badblockpos]; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 281 | |
Brian Norris | cdbec05 | 2012-01-13 18:11:48 -0800 | [diff] [blame] | 282 | if (likely(chip->badblockbits == 8)) |
| 283 | res = bad != 0xFF; |
| 284 | else |
| 285 | res = hweight8(bad) < chip->badblockbits; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 286 | if (res) |
| 287 | return res; |
| 288 | } |
Maxim Levitsky | e0b58d0 | 2010-02-22 20:39:38 +0200 | [diff] [blame] | 289 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 290 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 294 | * 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] | 295 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 296 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 298 | * This is the default implementation, which can be overridden by a hardware |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 299 | * specific driver. It provides the details for writing a bad block marker to a |
| 300 | * block. |
| 301 | */ |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 302 | 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] | 303 | { |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 304 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 305 | struct mtd_oob_ops ops; |
| 306 | uint8_t buf[2] = { 0, 0 }; |
| 307 | int ret = 0, res, i = 0; |
| 308 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 309 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 310 | ops.oobbuf = buf; |
| 311 | ops.ooboffs = chip->badblockpos; |
| 312 | if (chip->options & NAND_BUSWIDTH_16) { |
| 313 | ops.ooboffs &= ~0x01; |
| 314 | ops.len = ops.ooblen = 2; |
| 315 | } else { |
| 316 | ops.len = ops.ooblen = 1; |
| 317 | } |
| 318 | ops.mode = MTD_OPS_PLACE_OOB; |
| 319 | |
| 320 | /* Write to first/last page(s) if necessary */ |
| 321 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 322 | ofs += mtd->erasesize - mtd->writesize; |
| 323 | do { |
| 324 | res = nand_do_write_oob(mtd, ofs, &ops); |
| 325 | if (!ret) |
| 326 | ret = res; |
| 327 | |
| 328 | i++; |
| 329 | ofs += mtd->writesize; |
| 330 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
| 331 | |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | /** |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 336 | * nand_markbad_bbm - mark a block by updating the BBM |
| 337 | * @chip: NAND chip object |
| 338 | * @ofs: offset of the block to mark bad |
| 339 | */ |
| 340 | int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs) |
| 341 | { |
| 342 | if (chip->legacy.block_markbad) |
| 343 | return chip->legacy.block_markbad(chip, ofs); |
| 344 | |
| 345 | return nand_default_block_markbad(chip, ofs); |
| 346 | } |
| 347 | |
| 348 | static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs) |
| 349 | { |
| 350 | if (chip->legacy.block_bad) |
| 351 | return chip->legacy.block_bad(chip, ofs); |
| 352 | |
| 353 | return nand_block_bad(chip, ofs); |
| 354 | } |
| 355 | |
| 356 | /** |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 357 | * nand_block_markbad_lowlevel - mark a block bad |
| 358 | * @mtd: MTD device structure |
| 359 | * @ofs: offset from device start |
| 360 | * |
| 361 | * This function performs the generic NAND bad block marking steps (i.e., bad |
| 362 | * 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] | 363 | * 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] | 364 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 365 | * We try operations in the following order: |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 366 | * |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 367 | * (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] | 368 | * (2) write bad block marker to OOB area of affected block (unless flag |
| 369 | * NAND_BBT_NO_OOB_BBM is present) |
| 370 | * (3) update the BBT |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 371 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 372 | * 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] | 373 | * procedures, and dump the error in the end. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | */ |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 375 | static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 377 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 378 | int res, ret = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 379 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 380 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 381 | struct erase_info einfo; |
| 382 | |
| 383 | /* Attempt erase before marking OOB */ |
| 384 | memset(&einfo, 0, sizeof(einfo)); |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 385 | einfo.addr = ofs; |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 386 | einfo.len = 1ULL << chip->phys_erase_shift; |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 387 | nand_erase_nand(chip, &einfo, 0); |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 388 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 389 | /* Write bad block marker to OOB */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 390 | nand_get_device(mtd, FL_WRITING); |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 391 | ret = nand_markbad_bbm(chip, ofs); |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 392 | nand_release_device(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 393 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 394 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 395 | /* Mark block bad in BBT */ |
| 396 | if (chip->bbt) { |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 397 | res = nand_markbad_bbt(chip, ofs); |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 398 | if (!ret) |
| 399 | ret = res; |
| 400 | } |
| 401 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 402 | if (!ret) |
| 403 | mtd->ecc_stats.badblocks++; |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 404 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 405 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 408 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | * nand_check_wp - [GENERIC] check if the chip is write protected |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 410 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 412 | * Check, if the device is write protected. The function expects, that the |
| 413 | * device is already selected. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 415 | static int nand_check_wp(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 417 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 418 | u8 status; |
| 419 | int ret; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 420 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 421 | /* Broken xD cards report WP despite being writable */ |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 422 | if (chip->options & NAND_BROKEN_XD) |
| 423 | return 0; |
| 424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | /* Check the WP bit */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 426 | ret = nand_status_op(chip, &status); |
| 427 | if (ret) |
| 428 | return ret; |
| 429 | |
| 430 | return status & NAND_STATUS_WP ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /** |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 434 | * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 435 | * @mtd: MTD device structure |
| 436 | * @ofs: offset from device start |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 437 | * |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 438 | * Check if the block is marked as reserved. |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 439 | */ |
| 440 | static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 441 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 442 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 443 | |
| 444 | if (!chip->bbt) |
| 445 | return 0; |
| 446 | /* Return info from the table */ |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 447 | return nand_isreserved_bbt(chip, ofs); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /** |
| 451 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 452 | * @mtd: MTD device structure |
| 453 | * @ofs: offset from device start |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 454 | * @allowbbt: 1, if its allowed to access the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | * |
| 456 | * Check, if the block is bad. Either by reading the bad block table or |
| 457 | * calling of the scan function. |
| 458 | */ |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 459 | static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 461 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | /* Return info from the table */ |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 464 | if (chip->bbt) |
| 465 | return nand_isbad_bbt(chip, ofs, allowbbt); |
| 466 | |
| 467 | return nand_isbad_bbm(chip, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 470 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 471 | * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1 |
| 472 | * @chip: NAND chip structure |
| 473 | * @timeout_ms: Timeout in ms |
| 474 | * |
| 475 | * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1. |
| 476 | * If that does not happen whitin the specified timeout, -ETIMEDOUT is |
| 477 | * returned. |
| 478 | * |
| 479 | * This helper is intended to be used when the controller does not have access |
| 480 | * to the NAND R/B pin. |
| 481 | * |
| 482 | * Be aware that calling this helper from an ->exec_op() implementation means |
| 483 | * ->exec_op() must be re-entrant. |
| 484 | * |
| 485 | * Return 0 if the NAND chip is ready, a negative error otherwise. |
| 486 | */ |
| 487 | int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms) |
| 488 | { |
Boris Brezillon | 3057fce | 2018-05-04 21:24:31 +0200 | [diff] [blame] | 489 | const struct nand_sdr_timings *timings; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 490 | u8 status = 0; |
| 491 | int ret; |
| 492 | |
| 493 | if (!chip->exec_op) |
| 494 | return -ENOTSUPP; |
| 495 | |
Boris Brezillon | 3057fce | 2018-05-04 21:24:31 +0200 | [diff] [blame] | 496 | /* Wait tWB before polling the STATUS reg. */ |
| 497 | timings = nand_get_sdr_timings(&chip->data_interface); |
| 498 | ndelay(PSEC_TO_NSEC(timings->tWB_max)); |
| 499 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 500 | ret = nand_status_op(chip, NULL); |
| 501 | if (ret) |
| 502 | return ret; |
| 503 | |
| 504 | timeout_ms = jiffies + msecs_to_jiffies(timeout_ms); |
| 505 | do { |
| 506 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 507 | if (ret) |
| 508 | break; |
| 509 | |
| 510 | if (status & NAND_STATUS_READY) |
| 511 | break; |
| 512 | |
| 513 | /* |
| 514 | * Typical lowest execution time for a tR on most NANDs is 10us, |
| 515 | * use this as polling delay before doing something smarter (ie. |
| 516 | * deriving a delay from the timeout value, timeout_ms/ratio). |
| 517 | */ |
| 518 | udelay(10); |
| 519 | } while (time_before(jiffies, timeout_ms)); |
| 520 | |
| 521 | /* |
| 522 | * We have to exit READ_STATUS mode in order to read real data on the |
| 523 | * bus in case the WAITRDY instruction is preceding a DATA_IN |
| 524 | * instruction. |
| 525 | */ |
| 526 | nand_exit_status_op(chip); |
| 527 | |
| 528 | if (ret) |
| 529 | return ret; |
| 530 | |
| 531 | return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT; |
| 532 | }; |
| 533 | EXPORT_SYMBOL_GPL(nand_soft_waitrdy); |
| 534 | |
| 535 | /** |
Janusz Krzysztofik | b0e137a | 2018-10-15 21:41:28 +0200 | [diff] [blame] | 536 | * nand_gpio_waitrdy - Poll R/B GPIO pin until ready |
| 537 | * @chip: NAND chip structure |
| 538 | * @gpiod: GPIO descriptor of R/B pin |
| 539 | * @timeout_ms: Timeout in ms |
| 540 | * |
| 541 | * Poll the R/B GPIO pin until it becomes ready. If that does not happen |
| 542 | * whitin the specified timeout, -ETIMEDOUT is returned. |
| 543 | * |
| 544 | * This helper is intended to be used when the controller has access to the |
| 545 | * NAND R/B pin over GPIO. |
| 546 | * |
| 547 | * Return 0 if the R/B pin indicates chip is ready, a negative error otherwise. |
| 548 | */ |
| 549 | int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod, |
| 550 | unsigned long timeout_ms) |
| 551 | { |
| 552 | /* Wait until R/B pin indicates chip is ready or timeout occurs */ |
| 553 | timeout_ms = jiffies + msecs_to_jiffies(timeout_ms); |
| 554 | do { |
| 555 | if (gpiod_get_value_cansleep(gpiod)) |
| 556 | return 0; |
| 557 | |
| 558 | cond_resched(); |
| 559 | } while (time_before(jiffies, timeout_ms)); |
| 560 | |
| 561 | return gpiod_get_value_cansleep(gpiod) ? 0 : -ETIMEDOUT; |
| 562 | }; |
| 563 | EXPORT_SYMBOL_GPL(nand_gpio_waitrdy); |
| 564 | |
| 565 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 566 | * panic_nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 567 | * @chip: the nand chip descriptor |
| 568 | * @mtd: MTD device structure |
| 569 | * @new_state: the state which is requested |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 570 | * |
| 571 | * Used when in panic, no locks are taken. |
| 572 | */ |
| 573 | static void panic_nand_get_device(struct nand_chip *chip, |
| 574 | struct mtd_info *mtd, int new_state) |
| 575 | { |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 576 | /* Hardware controller shared among independent devices */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 577 | chip->controller->active = chip; |
| 578 | chip->state = new_state; |
| 579 | } |
| 580 | |
| 581 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | * nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 583 | * @mtd: MTD device structure |
| 584 | * @new_state: the state which is requested |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | * |
| 586 | * Get the device and lock it for exclusive access |
| 587 | */ |
Thomas Gleixner | 2c0a2be | 2006-05-23 11:50:56 +0200 | [diff] [blame] | 588 | static int |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 589 | nand_get_device(struct mtd_info *mtd, int new_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 591 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 592 | spinlock_t *lock = &chip->controller->lock; |
| 593 | wait_queue_head_t *wq = &chip->controller->wq; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 594 | DECLARE_WAITQUEUE(wait, current); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 595 | retry: |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 596 | spin_lock(lock); |
| 597 | |
vimal singh | b8b3ee9 | 2009-07-09 20:41:22 +0530 | [diff] [blame] | 598 | /* Hardware controller shared among independent devices */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 599 | if (!chip->controller->active) |
| 600 | chip->controller->active = chip; |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 601 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 602 | if (chip->controller->active == chip && chip->state == FL_READY) { |
| 603 | chip->state = new_state; |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 604 | spin_unlock(lock); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 605 | return 0; |
| 606 | } |
| 607 | if (new_state == FL_PM_SUSPENDED) { |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 608 | if (chip->controller->active->state == FL_PM_SUSPENDED) { |
| 609 | chip->state = FL_PM_SUSPENDED; |
| 610 | spin_unlock(lock); |
| 611 | return 0; |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 612 | } |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 613 | } |
| 614 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 615 | add_wait_queue(wq, &wait); |
| 616 | spin_unlock(lock); |
| 617 | schedule(); |
| 618 | remove_wait_queue(wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | goto retry; |
| 620 | } |
| 621 | |
| 622 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 623 | * panic_nand_wait - [GENERIC] wait until the command is done |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 624 | * @chip: NAND chip structure |
| 625 | * @timeo: timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 626 | * |
| 627 | * Wait for command done. This is a helper function for nand_wait used when |
| 628 | * 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] | 629 | * an oops through mtdoops. |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 630 | */ |
Boris Brezillon | 3d4af7c | 2018-09-07 00:38:49 +0200 | [diff] [blame] | 631 | void panic_nand_wait(struct nand_chip *chip, unsigned long timeo) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 632 | { |
| 633 | int i; |
| 634 | for (i = 0; i < timeo; i++) { |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 635 | if (chip->legacy.dev_ready) { |
| 636 | if (chip->legacy.dev_ready(chip)) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 637 | break; |
| 638 | } else { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 639 | int ret; |
| 640 | u8 status; |
| 641 | |
| 642 | ret = nand_read_data_op(chip, &status, sizeof(status), |
| 643 | true); |
| 644 | if (ret) |
| 645 | return; |
| 646 | |
| 647 | if (status & NAND_STATUS_READY) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 648 | break; |
| 649 | } |
| 650 | mdelay(1); |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 651 | } |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 652 | } |
| 653 | |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 654 | static bool nand_supports_get_features(struct nand_chip *chip, int addr) |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 655 | { |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 656 | return (chip->parameters.supports_set_get_features && |
| 657 | test_bit(addr, chip->parameters.get_feature_list)); |
| 658 | } |
| 659 | |
| 660 | static bool nand_supports_set_features(struct nand_chip *chip, int addr) |
| 661 | { |
| 662 | return (chip->parameters.supports_set_get_features && |
| 663 | test_bit(addr, chip->parameters.set_feature_list)); |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /** |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 667 | * nand_reset_data_interface - Reset data interface and timings |
| 668 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 669 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 670 | * |
| 671 | * Reset the Data interface and timings to ONFI mode 0. |
| 672 | * |
| 673 | * Returns 0 for success or negative error code otherwise. |
| 674 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 675 | static int nand_reset_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 676 | { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 677 | int ret; |
| 678 | |
| 679 | if (!chip->setup_data_interface) |
| 680 | return 0; |
| 681 | |
| 682 | /* |
| 683 | * The ONFI specification says: |
| 684 | * " |
| 685 | * To transition from NV-DDR or NV-DDR2 to the SDR data |
| 686 | * interface, the host shall use the Reset (FFh) command |
| 687 | * using SDR timing mode 0. A device in any timing mode is |
| 688 | * required to recognize Reset (FFh) command issued in SDR |
| 689 | * timing mode 0. |
| 690 | * " |
| 691 | * |
| 692 | * Configure the data interface in SDR mode and set the |
| 693 | * timings to timing mode 0. |
| 694 | */ |
| 695 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 696 | onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0); |
Boris Brezillon | 858838b | 2018-09-06 14:05:33 +0200 | [diff] [blame] | 697 | ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 698 | if (ret) |
| 699 | pr_err("Failed to configure data interface to SDR timing mode 0\n"); |
| 700 | |
| 701 | return ret; |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * nand_setup_data_interface - Setup the best data interface and timings |
| 706 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 707 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 708 | * |
| 709 | * Find and configure the best data interface and NAND timings supported by |
| 710 | * the chip and the driver. |
| 711 | * First tries to retrieve supported timing modes from ONFI information, |
| 712 | * and if the NAND chip does not support ONFI, relies on the |
| 713 | * ->onfi_timing_mode_default specified in the nand_ids table. |
| 714 | * |
| 715 | * Returns 0 for success or negative error code otherwise. |
| 716 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 717 | static int nand_setup_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 718 | { |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 719 | u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { |
| 720 | chip->onfi_timing_mode_default, |
| 721 | }; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 722 | int ret; |
| 723 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 724 | if (!chip->setup_data_interface) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 725 | return 0; |
| 726 | |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 727 | /* 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] | 728 | if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) { |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 729 | chip->select_chip(chip, chipnr); |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 730 | ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE, |
| 731 | tmode_param); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 732 | chip->select_chip(chip, -1); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 733 | if (ret) |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 734 | return ret; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 735 | } |
| 736 | |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 737 | /* Change the mode on the controller side */ |
Boris Brezillon | 858838b | 2018-09-06 14:05:33 +0200 | [diff] [blame] | 738 | ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 739 | if (ret) |
| 740 | return ret; |
| 741 | |
| 742 | /* Check the mode has been accepted by the chip, if supported */ |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 743 | if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 744 | return 0; |
| 745 | |
| 746 | memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 747 | chip->select_chip(chip, chipnr); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 748 | ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE, |
| 749 | tmode_param); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 750 | chip->select_chip(chip, -1); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 751 | if (ret) |
| 752 | goto err_reset_chip; |
| 753 | |
| 754 | if (tmode_param[0] != chip->onfi_timing_mode_default) { |
| 755 | pr_warn("timing mode %d not acknowledged by the NAND chip\n", |
| 756 | chip->onfi_timing_mode_default); |
| 757 | goto err_reset_chip; |
| 758 | } |
| 759 | |
| 760 | return 0; |
| 761 | |
| 762 | err_reset_chip: |
| 763 | /* |
| 764 | * Fallback to mode 0 if the chip explicitly did not ack the chosen |
| 765 | * timing mode. |
| 766 | */ |
| 767 | nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 768 | chip->select_chip(chip, chipnr); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 769 | nand_reset_op(chip); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 770 | chip->select_chip(chip, -1); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 771 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 772 | return ret; |
| 773 | } |
| 774 | |
| 775 | /** |
| 776 | * nand_init_data_interface - find the best data interface and timings |
| 777 | * @chip: The NAND chip |
| 778 | * |
| 779 | * Find the best data interface and NAND timings supported by the chip |
| 780 | * and the driver. |
| 781 | * First tries to retrieve supported timing modes from ONFI information, |
| 782 | * and if the NAND chip does not support ONFI, relies on the |
| 783 | * ->onfi_timing_mode_default specified in the nand_ids table. After this |
| 784 | * function nand_chip->data_interface is initialized with the best timing mode |
| 785 | * available. |
| 786 | * |
| 787 | * Returns 0 for success or negative error code otherwise. |
| 788 | */ |
| 789 | static int nand_init_data_interface(struct nand_chip *chip) |
| 790 | { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 791 | int modes, mode, ret; |
| 792 | |
| 793 | if (!chip->setup_data_interface) |
| 794 | return 0; |
| 795 | |
| 796 | /* |
| 797 | * First try to identify the best timings from ONFI parameters and |
| 798 | * if the NAND does not support ONFI, fallback to the default ONFI |
| 799 | * timing mode. |
| 800 | */ |
Boris Brezillon | 462f35d | 2018-09-07 00:38:47 +0200 | [diff] [blame] | 801 | if (chip->parameters.onfi) { |
| 802 | modes = chip->parameters.onfi->async_timing_mode; |
| 803 | } else { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 804 | if (!chip->onfi_timing_mode_default) |
| 805 | return 0; |
| 806 | |
| 807 | modes = GENMASK(chip->onfi_timing_mode_default, 0); |
| 808 | } |
| 809 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 810 | for (mode = fls(modes) - 1; mode >= 0; mode--) { |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 811 | ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 812 | if (ret) |
| 813 | continue; |
| 814 | |
Miquel Raynal | d787b8b | 2017-12-22 18:12:41 +0100 | [diff] [blame] | 815 | /* |
| 816 | * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the |
| 817 | * controller supports the requested timings. |
| 818 | */ |
Boris Brezillon | 858838b | 2018-09-06 14:05:33 +0200 | [diff] [blame] | 819 | ret = chip->setup_data_interface(chip, |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 820 | NAND_DATA_IFACE_CHECK_ONLY, |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 821 | &chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 822 | if (!ret) { |
| 823 | chip->onfi_timing_mode_default = mode; |
| 824 | break; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | return 0; |
| 829 | } |
| 830 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 831 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 832 | * nand_fill_column_cycles - fill the column cycles of an address |
| 833 | * @chip: The NAND chip |
| 834 | * @addrs: Array of address cycles to fill |
| 835 | * @offset_in_page: The offset in the page |
| 836 | * |
| 837 | * Fills the first or the first two bytes of the @addrs field depending |
| 838 | * on the NAND bus width and the page size. |
| 839 | * |
| 840 | * Returns the number of cycles needed to encode the column, or a negative |
| 841 | * error code in case one of the arguments is invalid. |
| 842 | */ |
| 843 | static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs, |
| 844 | unsigned int offset_in_page) |
Matthieu CASTET | f251b8d | 2012-11-05 15:00:44 +0100 | [diff] [blame] | 845 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 846 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 847 | |
| 848 | /* Make sure the offset is less than the actual page size. */ |
| 849 | if (offset_in_page > mtd->writesize + mtd->oobsize) |
| 850 | return -EINVAL; |
| 851 | |
| 852 | /* |
| 853 | * On small page NANDs, there's a dedicated command to access the OOB |
| 854 | * area, and the column address is relative to the start of the OOB |
| 855 | * area, not the start of the page. Asjust the address accordingly. |
| 856 | */ |
| 857 | if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize) |
| 858 | offset_in_page -= mtd->writesize; |
| 859 | |
| 860 | /* |
| 861 | * The offset in page is expressed in bytes, if the NAND bus is 16-bit |
| 862 | * wide, then it must be divided by 2. |
| 863 | */ |
| 864 | if (chip->options & NAND_BUSWIDTH_16) { |
| 865 | if (WARN_ON(offset_in_page % 2)) |
| 866 | return -EINVAL; |
| 867 | |
| 868 | offset_in_page /= 2; |
| 869 | } |
| 870 | |
| 871 | addrs[0] = offset_in_page; |
| 872 | |
| 873 | /* |
| 874 | * Small page NANDs use 1 cycle for the columns, while large page NANDs |
| 875 | * need 2 |
| 876 | */ |
| 877 | if (mtd->writesize <= 512) |
| 878 | return 1; |
| 879 | |
| 880 | addrs[1] = offset_in_page >> 8; |
| 881 | |
| 882 | return 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 885 | static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page, |
| 886 | unsigned int offset_in_page, void *buf, |
| 887 | unsigned int len) |
| 888 | { |
| 889 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 890 | const struct nand_sdr_timings *sdr = |
| 891 | nand_get_sdr_timings(&chip->data_interface); |
| 892 | u8 addrs[4]; |
| 893 | struct nand_op_instr instrs[] = { |
| 894 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 895 | NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)), |
| 896 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 897 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 898 | NAND_OP_DATA_IN(len, buf, 0), |
| 899 | }; |
| 900 | struct nand_operation op = NAND_OPERATION(instrs); |
| 901 | int ret; |
| 902 | |
| 903 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 904 | if (!len) |
| 905 | op.ninstrs--; |
| 906 | |
| 907 | if (offset_in_page >= mtd->writesize) |
| 908 | instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB; |
| 909 | else if (offset_in_page >= 256 && |
| 910 | !(chip->options & NAND_BUSWIDTH_16)) |
| 911 | instrs[0].ctx.cmd.opcode = NAND_CMD_READ1; |
| 912 | |
| 913 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 914 | if (ret < 0) |
| 915 | return ret; |
| 916 | |
| 917 | addrs[1] = page; |
| 918 | addrs[2] = page >> 8; |
| 919 | |
| 920 | if (chip->options & NAND_ROW_ADDR_3) { |
| 921 | addrs[3] = page >> 16; |
| 922 | instrs[1].ctx.addr.naddrs++; |
| 923 | } |
| 924 | |
| 925 | return nand_exec_op(chip, &op); |
| 926 | } |
| 927 | |
| 928 | static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page, |
| 929 | unsigned int offset_in_page, void *buf, |
| 930 | unsigned int len) |
| 931 | { |
| 932 | const struct nand_sdr_timings *sdr = |
| 933 | nand_get_sdr_timings(&chip->data_interface); |
| 934 | u8 addrs[5]; |
| 935 | struct nand_op_instr instrs[] = { |
| 936 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 937 | NAND_OP_ADDR(4, addrs, 0), |
| 938 | NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)), |
| 939 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 940 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 941 | NAND_OP_DATA_IN(len, buf, 0), |
| 942 | }; |
| 943 | struct nand_operation op = NAND_OPERATION(instrs); |
| 944 | int ret; |
| 945 | |
| 946 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 947 | if (!len) |
| 948 | op.ninstrs--; |
| 949 | |
| 950 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 951 | if (ret < 0) |
| 952 | return ret; |
| 953 | |
| 954 | addrs[2] = page; |
| 955 | addrs[3] = page >> 8; |
| 956 | |
| 957 | if (chip->options & NAND_ROW_ADDR_3) { |
| 958 | addrs[4] = page >> 16; |
| 959 | instrs[1].ctx.addr.naddrs++; |
| 960 | } |
| 961 | |
| 962 | return nand_exec_op(chip, &op); |
| 963 | } |
| 964 | |
| 965 | /** |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 966 | * nand_read_page_op - Do a READ PAGE operation |
| 967 | * @chip: The NAND chip |
| 968 | * @page: page to read |
| 969 | * @offset_in_page: offset within the page |
| 970 | * @buf: buffer used to store the data |
| 971 | * @len: length of the buffer |
| 972 | * |
| 973 | * This function issues a READ PAGE operation. |
| 974 | * This function does not select/unselect the CS line. |
| 975 | * |
| 976 | * Returns 0 on success, a negative error code otherwise. |
| 977 | */ |
| 978 | int nand_read_page_op(struct nand_chip *chip, unsigned int page, |
| 979 | unsigned int offset_in_page, void *buf, unsigned int len) |
| 980 | { |
| 981 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 982 | |
| 983 | if (len && !buf) |
| 984 | return -EINVAL; |
| 985 | |
| 986 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 987 | return -EINVAL; |
| 988 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 989 | if (chip->exec_op) { |
| 990 | if (mtd->writesize > 512) |
| 991 | return nand_lp_exec_read_page_op(chip, page, |
| 992 | offset_in_page, buf, |
| 993 | len); |
| 994 | |
| 995 | return nand_sp_exec_read_page_op(chip, page, offset_in_page, |
| 996 | buf, len); |
| 997 | } |
| 998 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 999 | chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1000 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1001 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1002 | |
| 1003 | return 0; |
| 1004 | } |
| 1005 | EXPORT_SYMBOL_GPL(nand_read_page_op); |
| 1006 | |
| 1007 | /** |
| 1008 | * nand_read_param_page_op - Do a READ PARAMETER PAGE operation |
| 1009 | * @chip: The NAND chip |
| 1010 | * @page: parameter page to read |
| 1011 | * @buf: buffer used to store the data |
| 1012 | * @len: length of the buffer |
| 1013 | * |
| 1014 | * This function issues a READ PARAMETER PAGE operation. |
| 1015 | * This function does not select/unselect the CS line. |
| 1016 | * |
| 1017 | * Returns 0 on success, a negative error code otherwise. |
| 1018 | */ |
Boris Brezillon | 1c325cc | 2018-09-07 00:38:50 +0200 | [diff] [blame] | 1019 | int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf, |
| 1020 | unsigned int len) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1021 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1022 | unsigned int i; |
| 1023 | u8 *p = buf; |
| 1024 | |
| 1025 | if (len && !buf) |
| 1026 | return -EINVAL; |
| 1027 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1028 | if (chip->exec_op) { |
| 1029 | const struct nand_sdr_timings *sdr = |
| 1030 | nand_get_sdr_timings(&chip->data_interface); |
| 1031 | struct nand_op_instr instrs[] = { |
| 1032 | NAND_OP_CMD(NAND_CMD_PARAM, 0), |
| 1033 | NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1034 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 1035 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1036 | NAND_OP_8BIT_DATA_IN(len, buf, 0), |
| 1037 | }; |
| 1038 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1039 | |
| 1040 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1041 | if (!len) |
| 1042 | op.ninstrs--; |
| 1043 | |
| 1044 | return nand_exec_op(chip, &op); |
| 1045 | } |
| 1046 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1047 | chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1048 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1049 | p[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1050 | |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | /** |
| 1055 | * nand_change_read_column_op - Do a CHANGE READ COLUMN operation |
| 1056 | * @chip: The NAND chip |
| 1057 | * @offset_in_page: offset within the page |
| 1058 | * @buf: buffer used to store the data |
| 1059 | * @len: length of the buffer |
| 1060 | * @force_8bit: force 8-bit bus access |
| 1061 | * |
| 1062 | * This function issues a CHANGE READ COLUMN operation. |
| 1063 | * This function does not select/unselect the CS line. |
| 1064 | * |
| 1065 | * Returns 0 on success, a negative error code otherwise. |
| 1066 | */ |
| 1067 | int nand_change_read_column_op(struct nand_chip *chip, |
| 1068 | unsigned int offset_in_page, void *buf, |
| 1069 | unsigned int len, bool force_8bit) |
| 1070 | { |
| 1071 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1072 | |
| 1073 | if (len && !buf) |
| 1074 | return -EINVAL; |
| 1075 | |
| 1076 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1077 | return -EINVAL; |
| 1078 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1079 | /* Small page NANDs do not support column change. */ |
| 1080 | if (mtd->writesize <= 512) |
| 1081 | return -ENOTSUPP; |
| 1082 | |
| 1083 | if (chip->exec_op) { |
| 1084 | const struct nand_sdr_timings *sdr = |
| 1085 | nand_get_sdr_timings(&chip->data_interface); |
| 1086 | u8 addrs[2] = {}; |
| 1087 | struct nand_op_instr instrs[] = { |
| 1088 | NAND_OP_CMD(NAND_CMD_RNDOUT, 0), |
| 1089 | NAND_OP_ADDR(2, addrs, 0), |
| 1090 | NAND_OP_CMD(NAND_CMD_RNDOUTSTART, |
| 1091 | PSEC_TO_NSEC(sdr->tCCS_min)), |
| 1092 | NAND_OP_DATA_IN(len, buf, 0), |
| 1093 | }; |
| 1094 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1095 | int ret; |
| 1096 | |
| 1097 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1098 | if (ret < 0) |
| 1099 | return ret; |
| 1100 | |
| 1101 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1102 | if (!len) |
| 1103 | op.ninstrs--; |
| 1104 | |
| 1105 | instrs[3].ctx.data.force_8bit = force_8bit; |
| 1106 | |
| 1107 | return nand_exec_op(chip, &op); |
| 1108 | } |
| 1109 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1110 | chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1111 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1112 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1113 | |
| 1114 | return 0; |
| 1115 | } |
| 1116 | EXPORT_SYMBOL_GPL(nand_change_read_column_op); |
| 1117 | |
| 1118 | /** |
| 1119 | * nand_read_oob_op - Do a READ OOB operation |
| 1120 | * @chip: The NAND chip |
| 1121 | * @page: page to read |
| 1122 | * @offset_in_oob: offset within the OOB area |
| 1123 | * @buf: buffer used to store the data |
| 1124 | * @len: length of the buffer |
| 1125 | * |
| 1126 | * This function issues a READ OOB operation. |
| 1127 | * This function does not select/unselect the CS line. |
| 1128 | * |
| 1129 | * Returns 0 on success, a negative error code otherwise. |
| 1130 | */ |
| 1131 | int nand_read_oob_op(struct nand_chip *chip, unsigned int page, |
| 1132 | unsigned int offset_in_oob, void *buf, unsigned int len) |
| 1133 | { |
| 1134 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1135 | |
| 1136 | if (len && !buf) |
| 1137 | return -EINVAL; |
| 1138 | |
| 1139 | if (offset_in_oob + len > mtd->oobsize) |
| 1140 | return -EINVAL; |
| 1141 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1142 | if (chip->exec_op) |
| 1143 | return nand_read_page_op(chip, page, |
| 1144 | mtd->writesize + offset_in_oob, |
| 1145 | buf, len); |
| 1146 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1147 | chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1148 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1149 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1150 | |
| 1151 | return 0; |
| 1152 | } |
| 1153 | EXPORT_SYMBOL_GPL(nand_read_oob_op); |
| 1154 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1155 | static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1156 | unsigned int offset_in_page, const void *buf, |
| 1157 | unsigned int len, bool prog) |
| 1158 | { |
| 1159 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1160 | const struct nand_sdr_timings *sdr = |
| 1161 | nand_get_sdr_timings(&chip->data_interface); |
| 1162 | u8 addrs[5] = {}; |
| 1163 | struct nand_op_instr instrs[] = { |
| 1164 | /* |
| 1165 | * The first instruction will be dropped if we're dealing |
| 1166 | * with a large page NAND and adjusted if we're dealing |
| 1167 | * with a small page NAND and the page offset is > 255. |
| 1168 | */ |
| 1169 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1170 | NAND_OP_CMD(NAND_CMD_SEQIN, 0), |
| 1171 | NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1172 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1173 | NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1174 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0), |
| 1175 | }; |
| 1176 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1177 | int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1178 | int ret; |
| 1179 | u8 status; |
| 1180 | |
| 1181 | if (naddrs < 0) |
| 1182 | return naddrs; |
| 1183 | |
| 1184 | addrs[naddrs++] = page; |
| 1185 | addrs[naddrs++] = page >> 8; |
| 1186 | if (chip->options & NAND_ROW_ADDR_3) |
| 1187 | addrs[naddrs++] = page >> 16; |
| 1188 | |
| 1189 | instrs[2].ctx.addr.naddrs = naddrs; |
| 1190 | |
| 1191 | /* Drop the last two instructions if we're not programming the page. */ |
| 1192 | if (!prog) { |
| 1193 | op.ninstrs -= 2; |
| 1194 | /* Also drop the DATA_OUT instruction if empty. */ |
| 1195 | if (!len) |
| 1196 | op.ninstrs--; |
| 1197 | } |
| 1198 | |
| 1199 | if (mtd->writesize <= 512) { |
| 1200 | /* |
| 1201 | * Small pages need some more tweaking: we have to adjust the |
| 1202 | * first instruction depending on the page offset we're trying |
| 1203 | * to access. |
| 1204 | */ |
| 1205 | if (offset_in_page >= mtd->writesize) |
| 1206 | instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB; |
| 1207 | else if (offset_in_page >= 256 && |
| 1208 | !(chip->options & NAND_BUSWIDTH_16)) |
| 1209 | instrs[0].ctx.cmd.opcode = NAND_CMD_READ1; |
| 1210 | } else { |
| 1211 | /* |
| 1212 | * Drop the first command if we're dealing with a large page |
| 1213 | * NAND. |
| 1214 | */ |
| 1215 | op.instrs++; |
| 1216 | op.ninstrs--; |
| 1217 | } |
| 1218 | |
| 1219 | ret = nand_exec_op(chip, &op); |
| 1220 | if (!prog || ret) |
| 1221 | return ret; |
| 1222 | |
| 1223 | ret = nand_status_op(chip, &status); |
| 1224 | if (ret) |
| 1225 | return ret; |
| 1226 | |
| 1227 | return status; |
| 1228 | } |
| 1229 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1230 | /** |
| 1231 | * nand_prog_page_begin_op - starts a PROG PAGE operation |
| 1232 | * @chip: The NAND chip |
| 1233 | * @page: page to write |
| 1234 | * @offset_in_page: offset within the page |
| 1235 | * @buf: buffer containing the data to write to the page |
| 1236 | * @len: length of the buffer |
| 1237 | * |
| 1238 | * This function issues the first half of a PROG PAGE operation. |
| 1239 | * This function does not select/unselect the CS line. |
| 1240 | * |
| 1241 | * Returns 0 on success, a negative error code otherwise. |
| 1242 | */ |
| 1243 | int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page, |
| 1244 | unsigned int offset_in_page, const void *buf, |
| 1245 | unsigned int len) |
| 1246 | { |
| 1247 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1248 | |
| 1249 | if (len && !buf) |
| 1250 | return -EINVAL; |
| 1251 | |
| 1252 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1253 | return -EINVAL; |
| 1254 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1255 | if (chip->exec_op) |
| 1256 | return nand_exec_prog_page_op(chip, page, offset_in_page, buf, |
| 1257 | len, false); |
| 1258 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1259 | chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1260 | |
| 1261 | if (buf) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1262 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1263 | |
| 1264 | return 0; |
| 1265 | } |
| 1266 | EXPORT_SYMBOL_GPL(nand_prog_page_begin_op); |
| 1267 | |
| 1268 | /** |
| 1269 | * nand_prog_page_end_op - ends a PROG PAGE operation |
| 1270 | * @chip: The NAND chip |
| 1271 | * |
| 1272 | * This function issues the second half of a PROG PAGE operation. |
| 1273 | * This function does not select/unselect the CS line. |
| 1274 | * |
| 1275 | * Returns 0 on success, a negative error code otherwise. |
| 1276 | */ |
| 1277 | int nand_prog_page_end_op(struct nand_chip *chip) |
| 1278 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1279 | int ret; |
| 1280 | u8 status; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1281 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1282 | if (chip->exec_op) { |
| 1283 | const struct nand_sdr_timings *sdr = |
| 1284 | nand_get_sdr_timings(&chip->data_interface); |
| 1285 | struct nand_op_instr instrs[] = { |
| 1286 | NAND_OP_CMD(NAND_CMD_PAGEPROG, |
| 1287 | PSEC_TO_NSEC(sdr->tWB_max)), |
| 1288 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0), |
| 1289 | }; |
| 1290 | struct nand_operation op = NAND_OPERATION(instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1291 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1292 | ret = nand_exec_op(chip, &op); |
| 1293 | if (ret) |
| 1294 | return ret; |
| 1295 | |
| 1296 | ret = nand_status_op(chip, &status); |
| 1297 | if (ret) |
| 1298 | return ret; |
| 1299 | } else { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1300 | chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1); |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1301 | ret = chip->legacy.waitfunc(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1302 | if (ret < 0) |
| 1303 | return ret; |
| 1304 | |
| 1305 | status = ret; |
| 1306 | } |
| 1307 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1308 | if (status & NAND_STATUS_FAIL) |
| 1309 | return -EIO; |
| 1310 | |
| 1311 | return 0; |
| 1312 | } |
| 1313 | EXPORT_SYMBOL_GPL(nand_prog_page_end_op); |
| 1314 | |
| 1315 | /** |
| 1316 | * nand_prog_page_op - Do a full PROG PAGE operation |
| 1317 | * @chip: The NAND chip |
| 1318 | * @page: page to write |
| 1319 | * @offset_in_page: offset within the page |
| 1320 | * @buf: buffer containing the data to write to the page |
| 1321 | * @len: length of the buffer |
| 1322 | * |
| 1323 | * This function issues a full PROG PAGE operation. |
| 1324 | * This function does not select/unselect the CS line. |
| 1325 | * |
| 1326 | * Returns 0 on success, a negative error code otherwise. |
| 1327 | */ |
| 1328 | int nand_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1329 | unsigned int offset_in_page, const void *buf, |
| 1330 | unsigned int len) |
| 1331 | { |
| 1332 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1333 | int status; |
| 1334 | |
| 1335 | if (!len || !buf) |
| 1336 | return -EINVAL; |
| 1337 | |
| 1338 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1339 | return -EINVAL; |
| 1340 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1341 | if (chip->exec_op) { |
| 1342 | status = nand_exec_prog_page_op(chip, page, offset_in_page, buf, |
| 1343 | len, true); |
| 1344 | } else { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1345 | chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, |
| 1346 | page); |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1347 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1348 | chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1); |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1349 | status = chip->legacy.waitfunc(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1350 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1351 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1352 | if (status & NAND_STATUS_FAIL) |
| 1353 | return -EIO; |
| 1354 | |
| 1355 | return 0; |
| 1356 | } |
| 1357 | EXPORT_SYMBOL_GPL(nand_prog_page_op); |
| 1358 | |
| 1359 | /** |
| 1360 | * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation |
| 1361 | * @chip: The NAND chip |
| 1362 | * @offset_in_page: offset within the page |
| 1363 | * @buf: buffer containing the data to send to the NAND |
| 1364 | * @len: length of the buffer |
| 1365 | * @force_8bit: force 8-bit bus access |
| 1366 | * |
| 1367 | * This function issues a CHANGE WRITE COLUMN operation. |
| 1368 | * This function does not select/unselect the CS line. |
| 1369 | * |
| 1370 | * Returns 0 on success, a negative error code otherwise. |
| 1371 | */ |
| 1372 | int nand_change_write_column_op(struct nand_chip *chip, |
| 1373 | unsigned int offset_in_page, |
| 1374 | const void *buf, unsigned int len, |
| 1375 | bool force_8bit) |
| 1376 | { |
| 1377 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1378 | |
| 1379 | if (len && !buf) |
| 1380 | return -EINVAL; |
| 1381 | |
| 1382 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1383 | return -EINVAL; |
| 1384 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1385 | /* Small page NANDs do not support column change. */ |
| 1386 | if (mtd->writesize <= 512) |
| 1387 | return -ENOTSUPP; |
| 1388 | |
| 1389 | if (chip->exec_op) { |
| 1390 | const struct nand_sdr_timings *sdr = |
| 1391 | nand_get_sdr_timings(&chip->data_interface); |
| 1392 | u8 addrs[2]; |
| 1393 | struct nand_op_instr instrs[] = { |
| 1394 | NAND_OP_CMD(NAND_CMD_RNDIN, 0), |
| 1395 | NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)), |
| 1396 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1397 | }; |
| 1398 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1399 | int ret; |
| 1400 | |
| 1401 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1402 | if (ret < 0) |
| 1403 | return ret; |
| 1404 | |
| 1405 | instrs[2].ctx.data.force_8bit = force_8bit; |
| 1406 | |
| 1407 | /* Drop the DATA_OUT instruction if len is set to 0. */ |
| 1408 | if (!len) |
| 1409 | op.ninstrs--; |
| 1410 | |
| 1411 | return nand_exec_op(chip, &op); |
| 1412 | } |
| 1413 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1414 | chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1415 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1416 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1417 | |
| 1418 | return 0; |
| 1419 | } |
| 1420 | EXPORT_SYMBOL_GPL(nand_change_write_column_op); |
| 1421 | |
| 1422 | /** |
| 1423 | * nand_readid_op - Do a READID operation |
| 1424 | * @chip: The NAND chip |
| 1425 | * @addr: address cycle to pass after the READID command |
| 1426 | * @buf: buffer used to store the ID |
| 1427 | * @len: length of the buffer |
| 1428 | * |
| 1429 | * This function sends a READID command and reads back the ID returned by the |
| 1430 | * NAND. |
| 1431 | * This function does not select/unselect the CS line. |
| 1432 | * |
| 1433 | * Returns 0 on success, a negative error code otherwise. |
| 1434 | */ |
| 1435 | int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf, |
| 1436 | unsigned int len) |
| 1437 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1438 | unsigned int i; |
| 1439 | u8 *id = buf; |
| 1440 | |
| 1441 | if (len && !buf) |
| 1442 | return -EINVAL; |
| 1443 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1444 | if (chip->exec_op) { |
| 1445 | const struct nand_sdr_timings *sdr = |
| 1446 | nand_get_sdr_timings(&chip->data_interface); |
| 1447 | struct nand_op_instr instrs[] = { |
| 1448 | NAND_OP_CMD(NAND_CMD_READID, 0), |
| 1449 | NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1450 | NAND_OP_8BIT_DATA_IN(len, buf, 0), |
| 1451 | }; |
| 1452 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1453 | |
| 1454 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1455 | if (!len) |
| 1456 | op.ninstrs--; |
| 1457 | |
| 1458 | return nand_exec_op(chip, &op); |
| 1459 | } |
| 1460 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1461 | chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1462 | |
| 1463 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1464 | id[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1465 | |
| 1466 | return 0; |
| 1467 | } |
| 1468 | EXPORT_SYMBOL_GPL(nand_readid_op); |
| 1469 | |
| 1470 | /** |
| 1471 | * nand_status_op - Do a STATUS operation |
| 1472 | * @chip: The NAND chip |
| 1473 | * @status: out variable to store the NAND status |
| 1474 | * |
| 1475 | * This function sends a STATUS command and reads back the status returned by |
| 1476 | * the NAND. |
| 1477 | * This function does not select/unselect the CS line. |
| 1478 | * |
| 1479 | * Returns 0 on success, a negative error code otherwise. |
| 1480 | */ |
| 1481 | int nand_status_op(struct nand_chip *chip, u8 *status) |
| 1482 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1483 | if (chip->exec_op) { |
| 1484 | const struct nand_sdr_timings *sdr = |
| 1485 | nand_get_sdr_timings(&chip->data_interface); |
| 1486 | struct nand_op_instr instrs[] = { |
| 1487 | NAND_OP_CMD(NAND_CMD_STATUS, |
| 1488 | PSEC_TO_NSEC(sdr->tADL_min)), |
| 1489 | NAND_OP_8BIT_DATA_IN(1, status, 0), |
| 1490 | }; |
| 1491 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1492 | |
| 1493 | if (!status) |
| 1494 | op.ninstrs--; |
| 1495 | |
| 1496 | return nand_exec_op(chip, &op); |
| 1497 | } |
| 1498 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1499 | chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1500 | if (status) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1501 | *status = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1502 | |
| 1503 | return 0; |
| 1504 | } |
| 1505 | EXPORT_SYMBOL_GPL(nand_status_op); |
| 1506 | |
| 1507 | /** |
| 1508 | * nand_exit_status_op - Exit a STATUS operation |
| 1509 | * @chip: The NAND chip |
| 1510 | * |
| 1511 | * This function sends a READ0 command to cancel the effect of the STATUS |
| 1512 | * command to avoid reading only the status until a new read command is sent. |
| 1513 | * |
| 1514 | * This function does not select/unselect the CS line. |
| 1515 | * |
| 1516 | * Returns 0 on success, a negative error code otherwise. |
| 1517 | */ |
| 1518 | int nand_exit_status_op(struct nand_chip *chip) |
| 1519 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1520 | if (chip->exec_op) { |
| 1521 | struct nand_op_instr instrs[] = { |
| 1522 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1523 | }; |
| 1524 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1525 | |
| 1526 | return nand_exec_op(chip, &op); |
| 1527 | } |
| 1528 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1529 | chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1530 | |
| 1531 | return 0; |
| 1532 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1533 | |
| 1534 | /** |
| 1535 | * nand_erase_op - Do an erase operation |
| 1536 | * @chip: The NAND chip |
| 1537 | * @eraseblock: block to erase |
| 1538 | * |
| 1539 | * This function sends an ERASE command and waits for the NAND to be ready |
| 1540 | * before returning. |
| 1541 | * This function does not select/unselect the CS line. |
| 1542 | * |
| 1543 | * Returns 0 on success, a negative error code otherwise. |
| 1544 | */ |
| 1545 | int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock) |
| 1546 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1547 | unsigned int page = eraseblock << |
| 1548 | (chip->phys_erase_shift - chip->page_shift); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1549 | int ret; |
| 1550 | u8 status; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1551 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1552 | if (chip->exec_op) { |
| 1553 | const struct nand_sdr_timings *sdr = |
| 1554 | nand_get_sdr_timings(&chip->data_interface); |
| 1555 | u8 addrs[3] = { page, page >> 8, page >> 16 }; |
| 1556 | struct nand_op_instr instrs[] = { |
| 1557 | NAND_OP_CMD(NAND_CMD_ERASE1, 0), |
| 1558 | NAND_OP_ADDR(2, addrs, 0), |
| 1559 | NAND_OP_CMD(NAND_CMD_ERASE2, |
| 1560 | PSEC_TO_MSEC(sdr->tWB_max)), |
| 1561 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0), |
| 1562 | }; |
| 1563 | struct nand_operation op = NAND_OPERATION(instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1564 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1565 | if (chip->options & NAND_ROW_ADDR_3) |
| 1566 | instrs[1].ctx.addr.naddrs++; |
| 1567 | |
| 1568 | ret = nand_exec_op(chip, &op); |
| 1569 | if (ret) |
| 1570 | return ret; |
| 1571 | |
| 1572 | ret = nand_status_op(chip, &status); |
| 1573 | if (ret) |
| 1574 | return ret; |
| 1575 | } else { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1576 | chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page); |
| 1577 | chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1578 | |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1579 | ret = chip->legacy.waitfunc(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1580 | if (ret < 0) |
| 1581 | return ret; |
| 1582 | |
| 1583 | status = ret; |
| 1584 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1585 | |
| 1586 | if (status & NAND_STATUS_FAIL) |
| 1587 | return -EIO; |
| 1588 | |
| 1589 | return 0; |
| 1590 | } |
| 1591 | EXPORT_SYMBOL_GPL(nand_erase_op); |
| 1592 | |
| 1593 | /** |
| 1594 | * nand_set_features_op - Do a SET FEATURES operation |
| 1595 | * @chip: The NAND chip |
| 1596 | * @feature: feature id |
| 1597 | * @data: 4 bytes of data |
| 1598 | * |
| 1599 | * This function sends a SET FEATURES command and waits for the NAND to be |
| 1600 | * ready before returning. |
| 1601 | * This function does not select/unselect the CS line. |
| 1602 | * |
| 1603 | * Returns 0 on success, a negative error code otherwise. |
| 1604 | */ |
| 1605 | static int nand_set_features_op(struct nand_chip *chip, u8 feature, |
| 1606 | const void *data) |
| 1607 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1608 | const u8 *params = data; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1609 | int i, ret; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1610 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1611 | if (chip->exec_op) { |
| 1612 | const struct nand_sdr_timings *sdr = |
| 1613 | nand_get_sdr_timings(&chip->data_interface); |
| 1614 | struct nand_op_instr instrs[] = { |
| 1615 | NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0), |
| 1616 | NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1617 | NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data, |
| 1618 | PSEC_TO_NSEC(sdr->tWB_max)), |
| 1619 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0), |
| 1620 | }; |
| 1621 | struct nand_operation op = NAND_OPERATION(instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1622 | |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1623 | return nand_exec_op(chip, &op); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1624 | } |
| 1625 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1626 | chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1); |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1627 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1628 | chip->legacy.write_byte(chip, params[i]); |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1629 | |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1630 | ret = chip->legacy.waitfunc(chip); |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1631 | if (ret < 0) |
| 1632 | return ret; |
| 1633 | |
| 1634 | if (ret & NAND_STATUS_FAIL) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1635 | return -EIO; |
| 1636 | |
| 1637 | return 0; |
| 1638 | } |
| 1639 | |
| 1640 | /** |
| 1641 | * nand_get_features_op - Do a GET FEATURES operation |
| 1642 | * @chip: The NAND chip |
| 1643 | * @feature: feature id |
| 1644 | * @data: 4 bytes of data |
| 1645 | * |
| 1646 | * This function sends a GET FEATURES command and waits for the NAND to be |
| 1647 | * ready before returning. |
| 1648 | * This function does not select/unselect the CS line. |
| 1649 | * |
| 1650 | * Returns 0 on success, a negative error code otherwise. |
| 1651 | */ |
| 1652 | static int nand_get_features_op(struct nand_chip *chip, u8 feature, |
| 1653 | void *data) |
| 1654 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1655 | u8 *params = data; |
| 1656 | int i; |
| 1657 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1658 | if (chip->exec_op) { |
| 1659 | const struct nand_sdr_timings *sdr = |
| 1660 | nand_get_sdr_timings(&chip->data_interface); |
| 1661 | struct nand_op_instr instrs[] = { |
| 1662 | NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0), |
| 1663 | NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1664 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), |
| 1665 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1666 | NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN, |
| 1667 | data, 0), |
| 1668 | }; |
| 1669 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1670 | |
| 1671 | return nand_exec_op(chip, &op); |
| 1672 | } |
| 1673 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1674 | chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1675 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1676 | params[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1677 | |
| 1678 | return 0; |
| 1679 | } |
| 1680 | |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1681 | static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms, |
| 1682 | unsigned int delay_ns) |
| 1683 | { |
| 1684 | if (chip->exec_op) { |
| 1685 | struct nand_op_instr instrs[] = { |
| 1686 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms), |
| 1687 | PSEC_TO_NSEC(delay_ns)), |
| 1688 | }; |
| 1689 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1690 | |
| 1691 | return nand_exec_op(chip, &op); |
| 1692 | } |
| 1693 | |
| 1694 | /* Apply delay or wait for ready/busy pin */ |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1695 | if (!chip->legacy.dev_ready) |
Boris Brezillon | 3cece3a | 2018-09-07 00:38:41 +0200 | [diff] [blame] | 1696 | udelay(chip->legacy.chip_delay); |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1697 | else |
Boris Brezillon | 2b356ab | 2018-09-06 14:05:16 +0200 | [diff] [blame] | 1698 | nand_wait_ready(chip); |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1699 | |
| 1700 | return 0; |
| 1701 | } |
| 1702 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1703 | /** |
| 1704 | * nand_reset_op - Do a reset operation |
| 1705 | * @chip: The NAND chip |
| 1706 | * |
| 1707 | * This function sends a RESET command and waits for the NAND to be ready |
| 1708 | * before returning. |
| 1709 | * This function does not select/unselect the CS line. |
| 1710 | * |
| 1711 | * Returns 0 on success, a negative error code otherwise. |
| 1712 | */ |
| 1713 | int nand_reset_op(struct nand_chip *chip) |
| 1714 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1715 | if (chip->exec_op) { |
| 1716 | const struct nand_sdr_timings *sdr = |
| 1717 | nand_get_sdr_timings(&chip->data_interface); |
| 1718 | struct nand_op_instr instrs[] = { |
| 1719 | NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1720 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0), |
| 1721 | }; |
| 1722 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1723 | |
| 1724 | return nand_exec_op(chip, &op); |
| 1725 | } |
| 1726 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1727 | chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1728 | |
| 1729 | return 0; |
| 1730 | } |
| 1731 | EXPORT_SYMBOL_GPL(nand_reset_op); |
| 1732 | |
| 1733 | /** |
| 1734 | * nand_read_data_op - Read data from the NAND |
| 1735 | * @chip: The NAND chip |
| 1736 | * @buf: buffer used to store the data |
| 1737 | * @len: length of the buffer |
| 1738 | * @force_8bit: force 8-bit bus access |
| 1739 | * |
| 1740 | * This function does a raw data read on the bus. Usually used after launching |
| 1741 | * another NAND operation like nand_read_page_op(). |
| 1742 | * This function does not select/unselect the CS line. |
| 1743 | * |
| 1744 | * Returns 0 on success, a negative error code otherwise. |
| 1745 | */ |
| 1746 | int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len, |
| 1747 | bool force_8bit) |
| 1748 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1749 | if (!len || !buf) |
| 1750 | return -EINVAL; |
| 1751 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1752 | if (chip->exec_op) { |
| 1753 | struct nand_op_instr instrs[] = { |
| 1754 | NAND_OP_DATA_IN(len, buf, 0), |
| 1755 | }; |
| 1756 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1757 | |
| 1758 | instrs[0].ctx.data.force_8bit = force_8bit; |
| 1759 | |
| 1760 | return nand_exec_op(chip, &op); |
| 1761 | } |
| 1762 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1763 | if (force_8bit) { |
| 1764 | u8 *p = buf; |
| 1765 | unsigned int i; |
| 1766 | |
| 1767 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1768 | p[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1769 | } else { |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1770 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | return 0; |
| 1774 | } |
| 1775 | EXPORT_SYMBOL_GPL(nand_read_data_op); |
| 1776 | |
| 1777 | /** |
| 1778 | * nand_write_data_op - Write data from the NAND |
| 1779 | * @chip: The NAND chip |
| 1780 | * @buf: buffer containing the data to send on the bus |
| 1781 | * @len: length of the buffer |
| 1782 | * @force_8bit: force 8-bit bus access |
| 1783 | * |
| 1784 | * This function does a raw data write on the bus. Usually used after launching |
| 1785 | * another NAND operation like nand_write_page_begin_op(). |
| 1786 | * This function does not select/unselect the CS line. |
| 1787 | * |
| 1788 | * Returns 0 on success, a negative error code otherwise. |
| 1789 | */ |
| 1790 | int nand_write_data_op(struct nand_chip *chip, const void *buf, |
| 1791 | unsigned int len, bool force_8bit) |
| 1792 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1793 | if (!len || !buf) |
| 1794 | return -EINVAL; |
| 1795 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1796 | if (chip->exec_op) { |
| 1797 | struct nand_op_instr instrs[] = { |
| 1798 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1799 | }; |
| 1800 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1801 | |
| 1802 | instrs[0].ctx.data.force_8bit = force_8bit; |
| 1803 | |
| 1804 | return nand_exec_op(chip, &op); |
| 1805 | } |
| 1806 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1807 | if (force_8bit) { |
| 1808 | const u8 *p = buf; |
| 1809 | unsigned int i; |
| 1810 | |
| 1811 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1812 | chip->legacy.write_byte(chip, p[i]); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1813 | } else { |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1814 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | return 0; |
| 1818 | } |
| 1819 | EXPORT_SYMBOL_GPL(nand_write_data_op); |
| 1820 | |
| 1821 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1822 | * struct nand_op_parser_ctx - Context used by the parser |
| 1823 | * @instrs: array of all the instructions that must be addressed |
| 1824 | * @ninstrs: length of the @instrs array |
| 1825 | * @subop: Sub-operation to be passed to the NAND controller |
| 1826 | * |
| 1827 | * This structure is used by the core to split NAND operations into |
| 1828 | * sub-operations that can be handled by the NAND controller. |
| 1829 | */ |
| 1830 | struct nand_op_parser_ctx { |
| 1831 | const struct nand_op_instr *instrs; |
| 1832 | unsigned int ninstrs; |
| 1833 | struct nand_subop subop; |
| 1834 | }; |
| 1835 | |
| 1836 | /** |
| 1837 | * nand_op_parser_must_split_instr - Checks if an instruction must be split |
| 1838 | * @pat: the parser pattern element that matches @instr |
| 1839 | * @instr: pointer to the instruction to check |
| 1840 | * @start_offset: this is an in/out parameter. If @instr has already been |
| 1841 | * split, then @start_offset is the offset from which to start |
| 1842 | * (either an address cycle or an offset in the data buffer). |
| 1843 | * Conversely, if the function returns true (ie. instr must be |
| 1844 | * split), this parameter is updated to point to the first |
| 1845 | * data/address cycle that has not been taken care of. |
| 1846 | * |
| 1847 | * Some NAND controllers are limited and cannot send X address cycles with a |
| 1848 | * unique operation, or cannot read/write more than Y bytes at the same time. |
| 1849 | * In this case, split the instruction that does not fit in a single |
| 1850 | * controller-operation into two or more chunks. |
| 1851 | * |
| 1852 | * Returns true if the instruction must be split, false otherwise. |
| 1853 | * The @start_offset parameter is also updated to the offset at which the next |
| 1854 | * bundle of instruction must start (if an address or a data instruction). |
| 1855 | */ |
| 1856 | static bool |
| 1857 | nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat, |
| 1858 | const struct nand_op_instr *instr, |
| 1859 | unsigned int *start_offset) |
| 1860 | { |
| 1861 | switch (pat->type) { |
| 1862 | case NAND_OP_ADDR_INSTR: |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 1863 | if (!pat->ctx.addr.maxcycles) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1864 | break; |
| 1865 | |
| 1866 | if (instr->ctx.addr.naddrs - *start_offset > |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 1867 | pat->ctx.addr.maxcycles) { |
| 1868 | *start_offset += pat->ctx.addr.maxcycles; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1869 | return true; |
| 1870 | } |
| 1871 | break; |
| 1872 | |
| 1873 | case NAND_OP_DATA_IN_INSTR: |
| 1874 | case NAND_OP_DATA_OUT_INSTR: |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 1875 | if (!pat->ctx.data.maxlen) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1876 | break; |
| 1877 | |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 1878 | if (instr->ctx.data.len - *start_offset > |
| 1879 | pat->ctx.data.maxlen) { |
| 1880 | *start_offset += pat->ctx.data.maxlen; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1881 | return true; |
| 1882 | } |
| 1883 | break; |
| 1884 | |
| 1885 | default: |
| 1886 | break; |
| 1887 | } |
| 1888 | |
| 1889 | return false; |
| 1890 | } |
| 1891 | |
| 1892 | /** |
| 1893 | * nand_op_parser_match_pat - Checks if a pattern matches the instructions |
| 1894 | * remaining in the parser context |
| 1895 | * @pat: the pattern to test |
| 1896 | * @ctx: the parser context structure to match with the pattern @pat |
| 1897 | * |
| 1898 | * Check if @pat matches the set or a sub-set of instructions remaining in @ctx. |
| 1899 | * Returns true if this is the case, false ortherwise. When true is returned, |
| 1900 | * @ctx->subop is updated with the set of instructions to be passed to the |
| 1901 | * controller driver. |
| 1902 | */ |
| 1903 | static bool |
| 1904 | nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat, |
| 1905 | struct nand_op_parser_ctx *ctx) |
| 1906 | { |
| 1907 | unsigned int instr_offset = ctx->subop.first_instr_start_off; |
| 1908 | const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs; |
| 1909 | const struct nand_op_instr *instr = ctx->subop.instrs; |
| 1910 | unsigned int i, ninstrs; |
| 1911 | |
| 1912 | for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) { |
| 1913 | /* |
| 1914 | * The pattern instruction does not match the operation |
| 1915 | * instruction. If the instruction is marked optional in the |
| 1916 | * pattern definition, we skip the pattern element and continue |
| 1917 | * to the next one. If the element is mandatory, there's no |
| 1918 | * match and we can return false directly. |
| 1919 | */ |
| 1920 | if (instr->type != pat->elems[i].type) { |
| 1921 | if (!pat->elems[i].optional) |
| 1922 | return false; |
| 1923 | |
| 1924 | continue; |
| 1925 | } |
| 1926 | |
| 1927 | /* |
| 1928 | * Now check the pattern element constraints. If the pattern is |
| 1929 | * not able to handle the whole instruction in a single step, |
| 1930 | * we have to split it. |
| 1931 | * The last_instr_end_off value comes back updated to point to |
| 1932 | * the position where we have to split the instruction (the |
| 1933 | * start of the next subop chunk). |
| 1934 | */ |
| 1935 | if (nand_op_parser_must_split_instr(&pat->elems[i], instr, |
| 1936 | &instr_offset)) { |
| 1937 | ninstrs++; |
| 1938 | i++; |
| 1939 | break; |
| 1940 | } |
| 1941 | |
| 1942 | instr++; |
| 1943 | ninstrs++; |
| 1944 | instr_offset = 0; |
| 1945 | } |
| 1946 | |
| 1947 | /* |
| 1948 | * This can happen if all instructions of a pattern are optional. |
| 1949 | * Still, if there's not at least one instruction handled by this |
| 1950 | * pattern, this is not a match, and we should try the next one (if |
| 1951 | * any). |
| 1952 | */ |
| 1953 | if (!ninstrs) |
| 1954 | return false; |
| 1955 | |
| 1956 | /* |
| 1957 | * We had a match on the pattern head, but the pattern may be longer |
| 1958 | * than the instructions we're asked to execute. We need to make sure |
| 1959 | * there's no mandatory elements in the pattern tail. |
| 1960 | */ |
| 1961 | for (; i < pat->nelems; i++) { |
| 1962 | if (!pat->elems[i].optional) |
| 1963 | return false; |
| 1964 | } |
| 1965 | |
| 1966 | /* |
| 1967 | * We have a match: update the subop structure accordingly and return |
| 1968 | * true. |
| 1969 | */ |
| 1970 | ctx->subop.ninstrs = ninstrs; |
| 1971 | ctx->subop.last_instr_end_off = instr_offset; |
| 1972 | |
| 1973 | return true; |
| 1974 | } |
| 1975 | |
| 1976 | #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG) |
| 1977 | static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx) |
| 1978 | { |
| 1979 | const struct nand_op_instr *instr; |
| 1980 | char *prefix = " "; |
| 1981 | unsigned int i; |
| 1982 | |
| 1983 | pr_debug("executing subop:\n"); |
| 1984 | |
| 1985 | for (i = 0; i < ctx->ninstrs; i++) { |
| 1986 | instr = &ctx->instrs[i]; |
| 1987 | |
| 1988 | if (instr == &ctx->subop.instrs[0]) |
| 1989 | prefix = " ->"; |
| 1990 | |
| 1991 | switch (instr->type) { |
| 1992 | case NAND_OP_CMD_INSTR: |
| 1993 | pr_debug("%sCMD [0x%02x]\n", prefix, |
| 1994 | instr->ctx.cmd.opcode); |
| 1995 | break; |
| 1996 | case NAND_OP_ADDR_INSTR: |
| 1997 | pr_debug("%sADDR [%d cyc: %*ph]\n", prefix, |
| 1998 | instr->ctx.addr.naddrs, |
| 1999 | instr->ctx.addr.naddrs < 64 ? |
| 2000 | instr->ctx.addr.naddrs : 64, |
| 2001 | instr->ctx.addr.addrs); |
| 2002 | break; |
| 2003 | case NAND_OP_DATA_IN_INSTR: |
| 2004 | pr_debug("%sDATA_IN [%d B%s]\n", prefix, |
| 2005 | instr->ctx.data.len, |
| 2006 | instr->ctx.data.force_8bit ? |
| 2007 | ", force 8-bit" : ""); |
| 2008 | break; |
| 2009 | case NAND_OP_DATA_OUT_INSTR: |
| 2010 | pr_debug("%sDATA_OUT [%d B%s]\n", prefix, |
| 2011 | instr->ctx.data.len, |
| 2012 | instr->ctx.data.force_8bit ? |
| 2013 | ", force 8-bit" : ""); |
| 2014 | break; |
| 2015 | case NAND_OP_WAITRDY_INSTR: |
| 2016 | pr_debug("%sWAITRDY [max %d ms]\n", prefix, |
| 2017 | instr->ctx.waitrdy.timeout_ms); |
| 2018 | break; |
| 2019 | } |
| 2020 | |
| 2021 | if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1]) |
| 2022 | prefix = " "; |
| 2023 | } |
| 2024 | } |
| 2025 | #else |
| 2026 | static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx) |
| 2027 | { |
| 2028 | /* NOP */ |
| 2029 | } |
| 2030 | #endif |
| 2031 | |
| 2032 | /** |
| 2033 | * nand_op_parser_exec_op - exec_op parser |
| 2034 | * @chip: the NAND chip |
| 2035 | * @parser: patterns description provided by the controller driver |
| 2036 | * @op: the NAND operation to address |
| 2037 | * @check_only: when true, the function only checks if @op can be handled but |
| 2038 | * does not execute the operation |
| 2039 | * |
| 2040 | * Helper function designed to ease integration of NAND controller drivers that |
| 2041 | * only support a limited set of instruction sequences. The supported sequences |
| 2042 | * are described in @parser, and the framework takes care of splitting @op into |
| 2043 | * multiple sub-operations (if required) and pass them back to the ->exec() |
| 2044 | * callback of the matching pattern if @check_only is set to false. |
| 2045 | * |
| 2046 | * NAND controller drivers should call this function from their own ->exec_op() |
| 2047 | * implementation. |
| 2048 | * |
| 2049 | * Returns 0 on success, a negative error code otherwise. A failure can be |
| 2050 | * caused by an unsupported operation (none of the supported patterns is able |
| 2051 | * to handle the requested operation), or an error returned by one of the |
| 2052 | * matching pattern->exec() hook. |
| 2053 | */ |
| 2054 | int nand_op_parser_exec_op(struct nand_chip *chip, |
| 2055 | const struct nand_op_parser *parser, |
| 2056 | const struct nand_operation *op, bool check_only) |
| 2057 | { |
| 2058 | struct nand_op_parser_ctx ctx = { |
| 2059 | .subop.instrs = op->instrs, |
| 2060 | .instrs = op->instrs, |
| 2061 | .ninstrs = op->ninstrs, |
| 2062 | }; |
| 2063 | unsigned int i; |
| 2064 | |
| 2065 | while (ctx.subop.instrs < op->instrs + op->ninstrs) { |
| 2066 | int ret; |
| 2067 | |
| 2068 | for (i = 0; i < parser->npatterns; i++) { |
| 2069 | const struct nand_op_parser_pattern *pattern; |
| 2070 | |
| 2071 | pattern = &parser->patterns[i]; |
| 2072 | if (!nand_op_parser_match_pat(pattern, &ctx)) |
| 2073 | continue; |
| 2074 | |
| 2075 | nand_op_parser_trace(&ctx); |
| 2076 | |
| 2077 | if (check_only) |
| 2078 | break; |
| 2079 | |
| 2080 | ret = pattern->exec(chip, &ctx.subop); |
| 2081 | if (ret) |
| 2082 | return ret; |
| 2083 | |
| 2084 | break; |
| 2085 | } |
| 2086 | |
| 2087 | if (i == parser->npatterns) { |
| 2088 | pr_debug("->exec_op() parser: pattern not found!\n"); |
| 2089 | return -ENOTSUPP; |
| 2090 | } |
| 2091 | |
| 2092 | /* |
| 2093 | * Update the context structure by pointing to the start of the |
| 2094 | * next subop. |
| 2095 | */ |
| 2096 | ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs; |
| 2097 | if (ctx.subop.last_instr_end_off) |
| 2098 | ctx.subop.instrs -= 1; |
| 2099 | |
| 2100 | ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off; |
| 2101 | } |
| 2102 | |
| 2103 | return 0; |
| 2104 | } |
| 2105 | EXPORT_SYMBOL_GPL(nand_op_parser_exec_op); |
| 2106 | |
| 2107 | static bool nand_instr_is_data(const struct nand_op_instr *instr) |
| 2108 | { |
| 2109 | return instr && (instr->type == NAND_OP_DATA_IN_INSTR || |
| 2110 | instr->type == NAND_OP_DATA_OUT_INSTR); |
| 2111 | } |
| 2112 | |
| 2113 | static bool nand_subop_instr_is_valid(const struct nand_subop *subop, |
| 2114 | unsigned int instr_idx) |
| 2115 | { |
| 2116 | return subop && instr_idx < subop->ninstrs; |
| 2117 | } |
| 2118 | |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2119 | static unsigned int nand_subop_get_start_off(const struct nand_subop *subop, |
| 2120 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2121 | { |
| 2122 | if (instr_idx) |
| 2123 | return 0; |
| 2124 | |
| 2125 | return subop->first_instr_start_off; |
| 2126 | } |
| 2127 | |
| 2128 | /** |
| 2129 | * nand_subop_get_addr_start_off - Get the start offset in an address array |
| 2130 | * @subop: The entire sub-operation |
| 2131 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2132 | * |
| 2133 | * During driver development, one could be tempted to directly use the |
| 2134 | * ->addr.addrs field of address instructions. This is wrong as address |
| 2135 | * instructions might be split. |
| 2136 | * |
| 2137 | * Given an address instruction, returns the offset of the first cycle to issue. |
| 2138 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2139 | unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop, |
| 2140 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2141 | { |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2142 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2143 | subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)) |
| 2144 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2145 | |
| 2146 | return nand_subop_get_start_off(subop, instr_idx); |
| 2147 | } |
| 2148 | EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off); |
| 2149 | |
| 2150 | /** |
| 2151 | * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert |
| 2152 | * @subop: The entire sub-operation |
| 2153 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2154 | * |
| 2155 | * During driver development, one could be tempted to directly use the |
| 2156 | * ->addr->naddrs field of a data instruction. This is wrong as instructions |
| 2157 | * might be split. |
| 2158 | * |
| 2159 | * Given an address instruction, returns the number of address cycle to issue. |
| 2160 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2161 | unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop, |
| 2162 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2163 | { |
| 2164 | int start_off, end_off; |
| 2165 | |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2166 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2167 | subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)) |
| 2168 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2169 | |
| 2170 | start_off = nand_subop_get_addr_start_off(subop, instr_idx); |
| 2171 | |
| 2172 | if (instr_idx == subop->ninstrs - 1 && |
| 2173 | subop->last_instr_end_off) |
| 2174 | end_off = subop->last_instr_end_off; |
| 2175 | else |
| 2176 | end_off = subop->instrs[instr_idx].ctx.addr.naddrs; |
| 2177 | |
| 2178 | return end_off - start_off; |
| 2179 | } |
| 2180 | EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc); |
| 2181 | |
| 2182 | /** |
| 2183 | * nand_subop_get_data_start_off - Get the start offset in a data array |
| 2184 | * @subop: The entire sub-operation |
| 2185 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2186 | * |
| 2187 | * During driver development, one could be tempted to directly use the |
| 2188 | * ->data->buf.{in,out} field of data instructions. This is wrong as data |
| 2189 | * instructions might be split. |
| 2190 | * |
| 2191 | * Given a data instruction, returns the offset to start from. |
| 2192 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2193 | unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop, |
| 2194 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2195 | { |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2196 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2197 | !nand_instr_is_data(&subop->instrs[instr_idx]))) |
| 2198 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2199 | |
| 2200 | return nand_subop_get_start_off(subop, instr_idx); |
| 2201 | } |
| 2202 | EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off); |
| 2203 | |
| 2204 | /** |
| 2205 | * nand_subop_get_data_len - Get the number of bytes to retrieve |
| 2206 | * @subop: The entire sub-operation |
| 2207 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2208 | * |
| 2209 | * During driver development, one could be tempted to directly use the |
| 2210 | * ->data->len field of a data instruction. This is wrong as data instructions |
| 2211 | * might be split. |
| 2212 | * |
| 2213 | * Returns the length of the chunk of data to send/receive. |
| 2214 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2215 | unsigned int nand_subop_get_data_len(const struct nand_subop *subop, |
| 2216 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2217 | { |
| 2218 | int start_off = 0, end_off; |
| 2219 | |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2220 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2221 | !nand_instr_is_data(&subop->instrs[instr_idx]))) |
| 2222 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2223 | |
| 2224 | start_off = nand_subop_get_data_start_off(subop, instr_idx); |
| 2225 | |
| 2226 | if (instr_idx == subop->ninstrs - 1 && |
| 2227 | subop->last_instr_end_off) |
| 2228 | end_off = subop->last_instr_end_off; |
| 2229 | else |
| 2230 | end_off = subop->instrs[instr_idx].ctx.data.len; |
| 2231 | |
| 2232 | return end_off - start_off; |
| 2233 | } |
| 2234 | EXPORT_SYMBOL_GPL(nand_subop_get_data_len); |
| 2235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | /** |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2237 | * nand_reset - Reset and initialize a NAND device |
| 2238 | * @chip: The NAND chip |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2239 | * @chipnr: Internal die id |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2240 | * |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2241 | * Save the timings data structure, then apply SDR timings mode 0 (see |
| 2242 | * nand_reset_data_interface for details), do the reset operation, and |
| 2243 | * apply back the previous timings. |
| 2244 | * |
| 2245 | * Returns 0 on success, a negative error code otherwise. |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2246 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2247 | int nand_reset(struct nand_chip *chip, int chipnr) |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2248 | { |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2249 | struct nand_data_interface saved_data_intf = chip->data_interface; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2250 | int ret; |
| 2251 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 2252 | ret = nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2253 | if (ret) |
| 2254 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2255 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2256 | /* |
| 2257 | * The CS line has to be released before we can apply the new NAND |
| 2258 | * interface settings, hence this weird ->select_chip() dance. |
| 2259 | */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 2260 | chip->select_chip(chip, chipnr); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2261 | ret = nand_reset_op(chip); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 2262 | chip->select_chip(chip, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2263 | if (ret) |
| 2264 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2265 | |
Miquel Raynal | 107b7d6 | 2018-03-19 14:47:25 +0100 | [diff] [blame] | 2266 | /* |
| 2267 | * A nand_reset_data_interface() put both the NAND chip and the NAND |
| 2268 | * controller in timings mode 0. If the default mode for this chip is |
| 2269 | * also 0, no need to proceed to the change again. Plus, at probe time, |
| 2270 | * nand_setup_data_interface() uses ->set/get_features() which would |
| 2271 | * fail anyway as the parameter page is not available yet. |
| 2272 | */ |
| 2273 | if (!chip->onfi_timing_mode_default) |
| 2274 | return 0; |
| 2275 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2276 | chip->data_interface = saved_data_intf; |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 2277 | ret = nand_setup_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2278 | if (ret) |
| 2279 | return ret; |
| 2280 | |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2281 | return 0; |
| 2282 | } |
Boris Brezillon | b9bb984 | 2017-10-05 18:53:19 +0200 | [diff] [blame] | 2283 | EXPORT_SYMBOL_GPL(nand_reset); |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2284 | |
| 2285 | /** |
Boris Brezillon | 4524036 | 2018-09-07 00:38:40 +0200 | [diff] [blame] | 2286 | * nand_get_features - wrapper to perform a GET_FEATURE |
| 2287 | * @chip: NAND chip info structure |
| 2288 | * @addr: feature address |
| 2289 | * @subfeature_param: the subfeature parameters, a four bytes array |
| 2290 | * |
| 2291 | * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the |
| 2292 | * operation cannot be handled. |
| 2293 | */ |
| 2294 | int nand_get_features(struct nand_chip *chip, int addr, |
| 2295 | u8 *subfeature_param) |
| 2296 | { |
| 2297 | if (!nand_supports_get_features(chip, addr)) |
| 2298 | return -ENOTSUPP; |
| 2299 | |
| 2300 | if (chip->legacy.get_features) |
| 2301 | return chip->legacy.get_features(chip, addr, subfeature_param); |
| 2302 | |
| 2303 | return nand_get_features_op(chip, addr, subfeature_param); |
| 2304 | } |
Boris Brezillon | 4524036 | 2018-09-07 00:38:40 +0200 | [diff] [blame] | 2305 | |
| 2306 | /** |
| 2307 | * nand_set_features - wrapper to perform a SET_FEATURE |
| 2308 | * @chip: NAND chip info structure |
| 2309 | * @addr: feature address |
| 2310 | * @subfeature_param: the subfeature parameters, a four bytes array |
| 2311 | * |
| 2312 | * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the |
| 2313 | * operation cannot be handled. |
| 2314 | */ |
| 2315 | int nand_set_features(struct nand_chip *chip, int addr, |
| 2316 | u8 *subfeature_param) |
| 2317 | { |
| 2318 | if (!nand_supports_set_features(chip, addr)) |
| 2319 | return -ENOTSUPP; |
| 2320 | |
| 2321 | if (chip->legacy.set_features) |
| 2322 | return chip->legacy.set_features(chip, addr, subfeature_param); |
| 2323 | |
| 2324 | return nand_set_features_op(chip, addr, subfeature_param); |
| 2325 | } |
Boris Brezillon | 4524036 | 2018-09-07 00:38:40 +0200 | [diff] [blame] | 2326 | |
| 2327 | /** |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 2328 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 2329 | * @buf: buffer to test |
| 2330 | * @len: buffer length |
| 2331 | * @bitflips_threshold: maximum number of bitflips |
| 2332 | * |
| 2333 | * Check if a buffer contains only 0xff, which means the underlying region |
| 2334 | * has been erased and is ready to be programmed. |
| 2335 | * The bitflips_threshold specify the maximum number of bitflips before |
| 2336 | * considering the region is not erased. |
| 2337 | * Note: The logic of this function has been extracted from the memweight |
| 2338 | * implementation, except that nand_check_erased_buf function exit before |
| 2339 | * testing the whole buffer if the number of bitflips exceed the |
| 2340 | * bitflips_threshold value. |
| 2341 | * |
| 2342 | * Returns a positive number of bitflips less than or equal to |
| 2343 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 2344 | * threshold. |
| 2345 | */ |
| 2346 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 2347 | { |
| 2348 | const unsigned char *bitmap = buf; |
| 2349 | int bitflips = 0; |
| 2350 | int weight; |
| 2351 | |
| 2352 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 2353 | len--, bitmap++) { |
| 2354 | weight = hweight8(*bitmap); |
| 2355 | bitflips += BITS_PER_BYTE - weight; |
| 2356 | if (unlikely(bitflips > bitflips_threshold)) |
| 2357 | return -EBADMSG; |
| 2358 | } |
| 2359 | |
| 2360 | for (; len >= sizeof(long); |
| 2361 | len -= sizeof(long), bitmap += sizeof(long)) { |
Pavel Machek | 086567f | 2017-04-21 12:51:07 +0200 | [diff] [blame] | 2362 | unsigned long d = *((unsigned long *)bitmap); |
| 2363 | if (d == ~0UL) |
| 2364 | continue; |
| 2365 | weight = hweight_long(d); |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 2366 | bitflips += BITS_PER_LONG - weight; |
| 2367 | if (unlikely(bitflips > bitflips_threshold)) |
| 2368 | return -EBADMSG; |
| 2369 | } |
| 2370 | |
| 2371 | for (; len > 0; len--, bitmap++) { |
| 2372 | weight = hweight8(*bitmap); |
| 2373 | bitflips += BITS_PER_BYTE - weight; |
| 2374 | if (unlikely(bitflips > bitflips_threshold)) |
| 2375 | return -EBADMSG; |
| 2376 | } |
| 2377 | |
| 2378 | return bitflips; |
| 2379 | } |
| 2380 | |
| 2381 | /** |
| 2382 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 2383 | * 0xff data |
| 2384 | * @data: data buffer to test |
| 2385 | * @datalen: data length |
| 2386 | * @ecc: ECC buffer |
| 2387 | * @ecclen: ECC length |
| 2388 | * @extraoob: extra OOB buffer |
| 2389 | * @extraooblen: extra OOB length |
| 2390 | * @bitflips_threshold: maximum number of bitflips |
| 2391 | * |
| 2392 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 2393 | * 0xff pattern, which means the underlying region has been erased and is |
| 2394 | * ready to be programmed. |
| 2395 | * The bitflips_threshold specify the maximum number of bitflips before |
| 2396 | * considering the region as not erased. |
| 2397 | * |
| 2398 | * Note: |
| 2399 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 2400 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 2401 | * report the number of errors per chunk, and the NAND core infrastructure |
| 2402 | * expect you to return the maximum number of bitflips for the whole page. |
| 2403 | * This is why you should always use this function on a single chunk and |
| 2404 | * not on the whole page. After checking each chunk you should update your |
| 2405 | * max_bitflips value accordingly. |
| 2406 | * 2/ When checking for bitflips in erased pages you should not only check |
| 2407 | * the payload data but also their associated ECC data, because a user might |
| 2408 | * have programmed almost all bits to 1 but a few. In this case, we |
| 2409 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 2410 | * this case. |
| 2411 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 2412 | * data are protected by the ECC engine. |
| 2413 | * It could also be used if you support subpages and want to attach some |
| 2414 | * extra OOB data to an ECC chunk. |
| 2415 | * |
| 2416 | * Returns a positive number of bitflips less than or equal to |
| 2417 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 2418 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 2419 | */ |
| 2420 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 2421 | void *ecc, int ecclen, |
| 2422 | void *extraoob, int extraooblen, |
| 2423 | int bitflips_threshold) |
| 2424 | { |
| 2425 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 2426 | |
| 2427 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 2428 | bitflips_threshold); |
| 2429 | if (data_bitflips < 0) |
| 2430 | return data_bitflips; |
| 2431 | |
| 2432 | bitflips_threshold -= data_bitflips; |
| 2433 | |
| 2434 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 2435 | if (ecc_bitflips < 0) |
| 2436 | return ecc_bitflips; |
| 2437 | |
| 2438 | bitflips_threshold -= ecc_bitflips; |
| 2439 | |
| 2440 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 2441 | bitflips_threshold); |
| 2442 | if (extraoob_bitflips < 0) |
| 2443 | return extraoob_bitflips; |
| 2444 | |
| 2445 | if (data_bitflips) |
| 2446 | memset(data, 0xff, datalen); |
| 2447 | |
| 2448 | if (ecc_bitflips) |
| 2449 | memset(ecc, 0xff, ecclen); |
| 2450 | |
| 2451 | if (extraoob_bitflips) |
| 2452 | memset(extraoob, 0xff, extraooblen); |
| 2453 | |
| 2454 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 2455 | } |
| 2456 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
| 2457 | |
| 2458 | /** |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2459 | * nand_read_page_raw_notsupp - dummy read raw page function |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2460 | * @chip: nand chip info structure |
| 2461 | * @buf: buffer to store read data |
| 2462 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2463 | * @page: page number to read |
| 2464 | * |
| 2465 | * Returns -ENOTSUPP unconditionally. |
| 2466 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2467 | int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf, |
| 2468 | int oob_required, int page) |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2469 | { |
| 2470 | return -ENOTSUPP; |
| 2471 | } |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2472 | |
| 2473 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2474 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2475 | * @chip: nand chip info structure |
| 2476 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2477 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2478 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2479 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2480 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2481 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2482 | int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required, |
| 2483 | int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2484 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2485 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2486 | int ret; |
| 2487 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2488 | ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2489 | if (ret) |
| 2490 | return ret; |
| 2491 | |
| 2492 | if (oob_required) { |
| 2493 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 2494 | false); |
| 2495 | if (ret) |
| 2496 | return ret; |
| 2497 | } |
| 2498 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2499 | return 0; |
| 2500 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 2501 | EXPORT_SYMBOL(nand_read_page_raw); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2502 | |
| 2503 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2504 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2505 | * @chip: nand chip info structure |
| 2506 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2507 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2508 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2509 | * |
| 2510 | * We need a special oob layout and handling even when OOB isn't used. |
| 2511 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2512 | 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] | 2513 | int oob_required, int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2514 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2515 | struct mtd_info *mtd = nand_to_mtd(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2516 | int eccsize = chip->ecc.size; |
| 2517 | int eccbytes = chip->ecc.bytes; |
| 2518 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2519 | int steps, size, ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2520 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2521 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2522 | if (ret) |
| 2523 | return ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2524 | |
| 2525 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2526 | ret = nand_read_data_op(chip, buf, eccsize, false); |
| 2527 | if (ret) |
| 2528 | return ret; |
| 2529 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2530 | buf += eccsize; |
| 2531 | |
| 2532 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2533 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 2534 | false); |
| 2535 | if (ret) |
| 2536 | return ret; |
| 2537 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2538 | oob += chip->ecc.prepad; |
| 2539 | } |
| 2540 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2541 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 2542 | if (ret) |
| 2543 | return ret; |
| 2544 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2545 | oob += eccbytes; |
| 2546 | |
| 2547 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2548 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 2549 | false); |
| 2550 | if (ret) |
| 2551 | return ret; |
| 2552 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2553 | oob += chip->ecc.postpad; |
| 2554 | } |
| 2555 | } |
| 2556 | |
| 2557 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2558 | if (size) { |
| 2559 | ret = nand_read_data_op(chip, oob, size, false); |
| 2560 | if (ret) |
| 2561 | return ret; |
| 2562 | } |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2563 | |
| 2564 | return 0; |
| 2565 | } |
| 2566 | |
| 2567 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2568 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2569 | * @chip: nand chip info structure |
| 2570 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2571 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2572 | * @page: page number to read |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 2573 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2574 | static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf, |
| 2575 | int oob_required, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2577 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2578 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2579 | int eccbytes = chip->ecc.bytes; |
| 2580 | int eccsteps = chip->ecc.steps; |
| 2581 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2582 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
| 2583 | uint8_t *ecc_code = chip->ecc.code_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2584 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2585 | |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2586 | chip->ecc.read_page_raw(chip, buf, 1, page); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2587 | |
| 2588 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 2589 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2590 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2591 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 2592 | chip->ecc.total); |
| 2593 | if (ret) |
| 2594 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2595 | |
| 2596 | eccsteps = chip->ecc.steps; |
| 2597 | p = buf; |
| 2598 | |
| 2599 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2600 | int stat; |
| 2601 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2602 | stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]); |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2603 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2604 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2605 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2606 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2607 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2608 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2609 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2610 | return max_bitflips; |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 2611 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | /** |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2614 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2615 | * @chip: nand chip info structure |
| 2616 | * @data_offs: offset of requested data within the page |
| 2617 | * @readlen: data length |
| 2618 | * @bufpoi: buffer to store read data |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 2619 | * @page: page number to read |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2620 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2621 | static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs, |
| 2622 | uint32_t readlen, uint8_t *bufpoi, int page) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2623 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2624 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2625 | int start_step, end_step, num_steps, ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2626 | uint8_t *p; |
| 2627 | int data_col_addr, i, gaps = 0; |
| 2628 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 2629 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2630 | int index, section = 0; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2631 | unsigned int max_bitflips = 0; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2632 | struct mtd_oob_region oobregion = { }; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2633 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2634 | /* Column address within the page aligned to ECC size (256bytes) */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2635 | start_step = data_offs / chip->ecc.size; |
| 2636 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 2637 | num_steps = end_step - start_step + 1; |
Ron | 4a4163ca | 2014-03-16 04:01:07 +1030 | [diff] [blame] | 2638 | index = start_step * chip->ecc.bytes; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2639 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2640 | /* Data size aligned to ECC ecc.size */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2641 | datafrag_len = num_steps * chip->ecc.size; |
| 2642 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 2643 | |
| 2644 | data_col_addr = start_step * chip->ecc.size; |
| 2645 | /* If we read not a page aligned data */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2646 | p = bufpoi + data_col_addr; |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2647 | 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] | 2648 | if (ret) |
| 2649 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2650 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2651 | /* Calculate ECC */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2652 | 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] | 2653 | chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2654 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2655 | /* |
| 2656 | * The performance is faster if we position offsets according to |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2657 | * 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] | 2658 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2659 | ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion); |
| 2660 | if (ret) |
| 2661 | return ret; |
| 2662 | |
| 2663 | if (oobregion.length < eccfrag_len) |
| 2664 | gaps = 1; |
| 2665 | |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2666 | if (gaps) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2667 | ret = nand_change_read_column_op(chip, mtd->writesize, |
| 2668 | chip->oob_poi, mtd->oobsize, |
| 2669 | false); |
| 2670 | if (ret) |
| 2671 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2672 | } else { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2673 | /* |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2674 | * Send the command to read the particular ECC bytes take care |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2675 | * about buswidth alignment in read_buf. |
| 2676 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2677 | aligned_pos = oobregion.offset & ~(busw - 1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2678 | aligned_len = eccfrag_len; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2679 | if (oobregion.offset & (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2680 | aligned_len++; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2681 | if ((oobregion.offset + (num_steps * chip->ecc.bytes)) & |
| 2682 | (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2683 | aligned_len++; |
| 2684 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2685 | ret = nand_change_read_column_op(chip, |
| 2686 | mtd->writesize + aligned_pos, |
| 2687 | &chip->oob_poi[aligned_pos], |
| 2688 | aligned_len, false); |
| 2689 | if (ret) |
| 2690 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2691 | } |
| 2692 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2693 | ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf, |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2694 | chip->oob_poi, index, eccfrag_len); |
| 2695 | if (ret) |
| 2696 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2697 | |
| 2698 | p = bufpoi + data_col_addr; |
| 2699 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 2700 | int stat; |
| 2701 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2702 | stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i], |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2703 | &chip->ecc.calc_buf[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2704 | if (stat == -EBADMSG && |
| 2705 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2706 | /* check for empty pages with bitflips */ |
| 2707 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2708 | &chip->ecc.code_buf[i], |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2709 | chip->ecc.bytes, |
| 2710 | NULL, 0, |
| 2711 | chip->ecc.strength); |
| 2712 | } |
| 2713 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2714 | if (stat < 0) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2715 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2716 | } else { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2717 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2718 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2719 | } |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2720 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2721 | return max_bitflips; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2725 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2726 | * @chip: nand chip info structure |
| 2727 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2728 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2729 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2730 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2731 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2732 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2733 | static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf, |
| 2734 | int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2735 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2736 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2737 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2738 | int eccbytes = chip->ecc.bytes; |
| 2739 | int eccsteps = chip->ecc.steps; |
| 2740 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2741 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
| 2742 | uint8_t *ecc_code = chip->ecc.code_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2743 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2744 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2745 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2746 | if (ret) |
| 2747 | return ret; |
| 2748 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2749 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 2750 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2751 | |
| 2752 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2753 | if (ret) |
| 2754 | return ret; |
| 2755 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 2756 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2757 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2758 | |
| 2759 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 2760 | if (ret) |
| 2761 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2762 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2763 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 2764 | chip->ecc.total); |
| 2765 | if (ret) |
| 2766 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2767 | |
| 2768 | eccsteps = chip->ecc.steps; |
| 2769 | p = buf; |
| 2770 | |
| 2771 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2772 | int stat; |
| 2773 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2774 | stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2775 | if (stat == -EBADMSG && |
| 2776 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2777 | /* check for empty pages with bitflips */ |
| 2778 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2779 | &ecc_code[i], eccbytes, |
| 2780 | NULL, 0, |
| 2781 | chip->ecc.strength); |
| 2782 | } |
| 2783 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2784 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2785 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2786 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2787 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2788 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2789 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2790 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2791 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2795 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2796 | * @chip: nand chip info structure |
| 2797 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2798 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2799 | * @page: page number to read |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2800 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2801 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 2802 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 2803 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 2804 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 2805 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2806 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2807 | static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf, |
| 2808 | int oob_required, int page) |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2809 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2810 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2811 | int i, eccsize = chip->ecc.size, ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2812 | int eccbytes = chip->ecc.bytes; |
| 2813 | int eccsteps = chip->ecc.steps; |
| 2814 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2815 | uint8_t *ecc_code = chip->ecc.code_buf; |
| 2816 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2817 | unsigned int max_bitflips = 0; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2818 | |
| 2819 | /* Read the OOB area first */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2820 | ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
| 2821 | if (ret) |
| 2822 | return ret; |
| 2823 | |
| 2824 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2825 | if (ret) |
| 2826 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2827 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2828 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 2829 | chip->ecc.total); |
| 2830 | if (ret) |
| 2831 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2832 | |
| 2833 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2834 | int stat; |
| 2835 | |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 2836 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2837 | |
| 2838 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2839 | if (ret) |
| 2840 | return ret; |
| 2841 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 2842 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2843 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2844 | stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2845 | if (stat == -EBADMSG && |
| 2846 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2847 | /* check for empty pages with bitflips */ |
| 2848 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2849 | &ecc_code[i], eccbytes, |
| 2850 | NULL, 0, |
| 2851 | chip->ecc.strength); |
| 2852 | } |
| 2853 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2854 | if (stat < 0) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2855 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2856 | } else { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2857 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2858 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2859 | } |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2860 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2861 | return max_bitflips; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2865 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2866 | * @chip: nand chip info structure |
| 2867 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2868 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2869 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2870 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2871 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 2872 | * need a special oob layout and handling. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2873 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2874 | static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf, |
| 2875 | int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2876 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2877 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2878 | int ret, i, eccsize = chip->ecc.size; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2879 | int eccbytes = chip->ecc.bytes; |
| 2880 | int eccsteps = chip->ecc.steps; |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2881 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2882 | uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2883 | uint8_t *oob = chip->oob_poi; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2884 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2885 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2886 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2887 | if (ret) |
| 2888 | return ret; |
| 2889 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2890 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2891 | int stat; |
| 2892 | |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 2893 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2894 | |
| 2895 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2896 | if (ret) |
| 2897 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2898 | |
| 2899 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2900 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 2901 | false); |
| 2902 | if (ret) |
| 2903 | return ret; |
| 2904 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2905 | oob += chip->ecc.prepad; |
| 2906 | } |
| 2907 | |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 2908 | chip->ecc.hwctl(chip, NAND_ECC_READSYN); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2909 | |
| 2910 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 2911 | if (ret) |
| 2912 | return ret; |
| 2913 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2914 | stat = chip->ecc.correct(chip, p, oob, NULL); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2915 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2916 | oob += eccbytes; |
| 2917 | |
| 2918 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2919 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 2920 | false); |
| 2921 | if (ret) |
| 2922 | return ret; |
| 2923 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2924 | oob += chip->ecc.postpad; |
| 2925 | } |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2926 | |
| 2927 | if (stat == -EBADMSG && |
| 2928 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2929 | /* check for empty pages with bitflips */ |
| 2930 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 2931 | oob - eccpadbytes, |
| 2932 | eccpadbytes, |
| 2933 | NULL, 0, |
| 2934 | chip->ecc.strength); |
| 2935 | } |
| 2936 | |
| 2937 | if (stat < 0) { |
| 2938 | mtd->ecc_stats.failed++; |
| 2939 | } else { |
| 2940 | mtd->ecc_stats.corrected += stat; |
| 2941 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2942 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 2946 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2947 | if (i) { |
| 2948 | ret = nand_read_data_op(chip, oob, i, false); |
| 2949 | if (ret) |
| 2950 | return ret; |
| 2951 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2952 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2953 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2957 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2958 | * @mtd: mtd info structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2959 | * @oob: oob destination address |
| 2960 | * @ops: oob ops structure |
| 2961 | * @len: size of oob to transfer |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2962 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2963 | static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob, |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2964 | struct mtd_oob_ops *ops, size_t len) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2965 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2966 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 2967 | int ret; |
| 2968 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2969 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2970 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2971 | case MTD_OPS_PLACE_OOB: |
| 2972 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2973 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 2974 | return oob + len; |
| 2975 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2976 | case MTD_OPS_AUTO_OOB: |
| 2977 | ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi, |
| 2978 | ops->ooboffs, len); |
| 2979 | BUG_ON(ret); |
| 2980 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2981 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2982 | default: |
| 2983 | BUG(); |
| 2984 | } |
| 2985 | return NULL; |
| 2986 | } |
| 2987 | |
| 2988 | /** |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2989 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 2990 | * @chip: NAND chip object |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2991 | * @retry_mode: the retry mode to use |
| 2992 | * |
| 2993 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 2994 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 2995 | * a new threshold, the host should retry reading the page. |
| 2996 | */ |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 2997 | 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] | 2998 | { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2999 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 3000 | |
| 3001 | if (retry_mode >= chip->read_retries) |
| 3002 | return -EINVAL; |
| 3003 | |
| 3004 | if (!chip->setup_read_retry) |
| 3005 | return -EOPNOTSUPP; |
| 3006 | |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3007 | return chip->setup_read_retry(chip, retry_mode); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3008 | } |
| 3009 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3010 | static void nand_wait_readrdy(struct nand_chip *chip) |
| 3011 | { |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 3012 | const struct nand_sdr_timings *sdr; |
| 3013 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3014 | if (!(chip->options & NAND_NEED_READRDY)) |
| 3015 | return; |
| 3016 | |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 3017 | sdr = nand_get_sdr_timings(&chip->data_interface); |
| 3018 | 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] | 3019 | } |
| 3020 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3021 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3022 | * nand_do_read_ops - [INTERN] Read data with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3023 | * @mtd: MTD device structure |
| 3024 | * @from: offset to read from |
| 3025 | * @ops: oob ops structure |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3026 | * |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3027 | * Internal function. Called with chip held. |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3028 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3029 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 3030 | struct mtd_oob_ops *ops) |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3031 | { |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3032 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3033 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3034 | int ret = 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3035 | uint32_t readlen = ops->len; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3036 | uint32_t oobreadlen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3037 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 9aca334 | 2010-02-22 20:39:35 +0200 | [diff] [blame] | 3038 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3039 | uint8_t *bufpoi, *oob, *buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3040 | int use_bufpoi; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3041 | unsigned int max_bitflips = 0; |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3042 | int retry_mode = 0; |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3043 | bool ecc_fail = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3045 | chipnr = (int)(from >> chip->chip_shift); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3046 | chip->select_chip(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3048 | realpage = (int)(from >> chip->page_shift); |
| 3049 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3050 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3051 | col = (int)(from & (mtd->writesize - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3052 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3053 | buf = ops->datbuf; |
| 3054 | oob = ops->oobbuf; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3055 | oob_required = oob ? 1 : 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3056 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3057 | while (1) { |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3058 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
| 3059 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3060 | bytes = min(mtd->writesize - col, readlen); |
| 3061 | aligned = (bytes == mtd->writesize); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3062 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3063 | if (!aligned) |
| 3064 | use_bufpoi = 1; |
| 3065 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 3066 | use_bufpoi = !virt_addr_valid(buf) || |
| 3067 | !IS_ALIGNED((unsigned long)buf, |
| 3068 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3069 | else |
| 3070 | use_bufpoi = 0; |
| 3071 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3072 | /* Is the current page in the buffer? */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3073 | if (realpage != chip->pagebuf || oob) { |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3074 | bufpoi = use_bufpoi ? chip->data_buf : buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3075 | |
| 3076 | if (use_bufpoi && aligned) |
| 3077 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 3078 | __func__, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3080 | read_retry: |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3081 | /* |
| 3082 | * Now read the page into the buffer. Absent an error, |
| 3083 | * the read methods return max bitflips per ecc step. |
| 3084 | */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3085 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3086 | ret = chip->ecc.read_page_raw(chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3087 | oob_required, |
| 3088 | page); |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 3089 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
| 3090 | !oob) |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3091 | ret = chip->ecc.read_subpage(chip, col, bytes, |
| 3092 | bufpoi, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3093 | else |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3094 | ret = chip->ecc.read_page(chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3095 | oob_required, page); |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3096 | if (ret < 0) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3097 | if (use_bufpoi) |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3098 | /* Invalidate page cache */ |
| 3099 | chip->pagebuf = -1; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3100 | break; |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3101 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3102 | |
| 3103 | /* Transfer not aligned data */ |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3104 | if (use_bufpoi) { |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 3105 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3106 | !(mtd->ecc_stats.failed - ecc_failures) && |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3107 | (ops->mode != MTD_OPS_RAW)) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3108 | chip->pagebuf = realpage; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3109 | chip->pagebuf_bitflips = ret; |
| 3110 | } else { |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3111 | /* Invalidate page cache */ |
| 3112 | chip->pagebuf = -1; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3113 | } |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3114 | memcpy(buf, chip->data_buf + col, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3116 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3117 | if (unlikely(oob)) { |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 3118 | int toread = min(oobreadlen, max_oobsize); |
| 3119 | |
| 3120 | if (toread) { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3121 | oob = nand_transfer_oob(mtd, |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 3122 | oob, ops, toread); |
| 3123 | oobreadlen -= toread; |
| 3124 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3125 | } |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 3126 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3127 | nand_wait_readrdy(chip); |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3128 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3129 | if (mtd->ecc_stats.failed - ecc_failures) { |
Brian Norris | 28fa65e | 2014-02-12 16:08:28 -0800 | [diff] [blame] | 3130 | if (retry_mode + 1 < chip->read_retries) { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3131 | retry_mode++; |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3132 | ret = nand_setup_read_retry(chip, |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3133 | retry_mode); |
| 3134 | if (ret < 0) |
| 3135 | break; |
| 3136 | |
| 3137 | /* Reset failures; retry */ |
| 3138 | mtd->ecc_stats.failed = ecc_failures; |
| 3139 | goto read_retry; |
| 3140 | } else { |
| 3141 | /* No more retry modes; real failure */ |
| 3142 | ecc_fail = true; |
| 3143 | } |
| 3144 | } |
| 3145 | |
| 3146 | buf += bytes; |
Masahiro Yamada | 0760468 | 2017-03-30 15:45:47 +0900 | [diff] [blame] | 3147 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3148 | } else { |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3149 | memcpy(buf, chip->data_buf + col, bytes); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3150 | buf += bytes; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3151 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 3152 | chip->pagebuf_bitflips); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3155 | readlen -= bytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3156 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3157 | /* Reset to retry mode 0 */ |
| 3158 | if (retry_mode) { |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3159 | ret = nand_setup_read_retry(chip, 0); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3160 | if (ret < 0) |
| 3161 | break; |
| 3162 | retry_mode = 0; |
| 3163 | } |
| 3164 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3165 | if (!readlen) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3166 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3168 | /* For subsequent reads align to page boundary */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3169 | col = 0; |
| 3170 | /* Increment page address */ |
| 3171 | realpage++; |
| 3172 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3173 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | /* Check, if we cross a chip boundary */ |
| 3175 | if (!page) { |
| 3176 | chipnr++; |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3177 | chip->select_chip(chip, -1); |
| 3178 | chip->select_chip(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3180 | } |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3181 | chip->select_chip(chip, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3182 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3183 | ops->retlen = ops->len - (size_t) readlen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3184 | if (oob) |
| 3185 | ops->oobretlen = ops->ooblen - oobreadlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3186 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3187 | if (ret < 0) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3188 | return ret; |
| 3189 | |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3190 | if (ecc_fail) |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 3191 | return -EBADMSG; |
| 3192 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3193 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3197 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3198 | * @chip: nand chip info structure |
| 3199 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3200 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3201 | int nand_read_oob_std(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3202 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3203 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 3204 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3205 | 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] | 3206 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3207 | EXPORT_SYMBOL(nand_read_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3208 | |
| 3209 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3210 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3211 | * with syndromes |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3212 | * @chip: nand chip info structure |
| 3213 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3214 | */ |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 3215 | static int nand_read_oob_syndrome(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3216 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3217 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3218 | int length = mtd->oobsize; |
| 3219 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 3220 | int eccsize = chip->ecc.size; |
Baruch Siach | 2ea69d2 | 2015-01-22 15:23:05 +0200 | [diff] [blame] | 3221 | uint8_t *bufpoi = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3222 | int i, toread, sndrnd = 0, pos, ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3223 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3224 | ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0); |
| 3225 | if (ret) |
| 3226 | return ret; |
| 3227 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3228 | for (i = 0; i < chip->ecc.steps; i++) { |
| 3229 | if (sndrnd) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3230 | int ret; |
| 3231 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3232 | pos = eccsize + i * (eccsize + chunk); |
| 3233 | if (mtd->writesize > 512) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3234 | ret = nand_change_read_column_op(chip, pos, |
| 3235 | NULL, 0, |
| 3236 | false); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3237 | else |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3238 | ret = nand_read_page_op(chip, page, pos, NULL, |
| 3239 | 0); |
| 3240 | |
| 3241 | if (ret) |
| 3242 | return ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3243 | } else |
| 3244 | sndrnd = 1; |
| 3245 | toread = min_t(int, length, chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3246 | |
| 3247 | ret = nand_read_data_op(chip, bufpoi, toread, false); |
| 3248 | if (ret) |
| 3249 | return ret; |
| 3250 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3251 | bufpoi += toread; |
| 3252 | length -= toread; |
| 3253 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3254 | if (length > 0) { |
| 3255 | ret = nand_read_data_op(chip, bufpoi, length, false); |
| 3256 | if (ret) |
| 3257 | return ret; |
| 3258 | } |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3259 | |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 3260 | return 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3264 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3265 | * @chip: nand chip info structure |
| 3266 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3267 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3268 | int nand_write_oob_std(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3269 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3270 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 3271 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3272 | return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi, |
| 3273 | mtd->oobsize); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3274 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3275 | EXPORT_SYMBOL(nand_write_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3276 | |
| 3277 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3278 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3279 | * with syndrome - only for large page flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3280 | * @chip: nand chip info structure |
| 3281 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3282 | */ |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 3283 | static int nand_write_oob_syndrome(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3284 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3285 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3286 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 3287 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3288 | int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3289 | const uint8_t *bufpoi = chip->oob_poi; |
| 3290 | |
| 3291 | /* |
| 3292 | * data-ecc-data-ecc ... ecc-oob |
| 3293 | * or |
| 3294 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
| 3295 | */ |
| 3296 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 3297 | pos = steps * (eccsize + chunk); |
| 3298 | steps = 0; |
| 3299 | } else |
Vitaly Wool | 8b0036e | 2006-07-11 09:11:25 +0200 | [diff] [blame] | 3300 | pos = eccsize; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3301 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3302 | ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0); |
| 3303 | if (ret) |
| 3304 | return ret; |
| 3305 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3306 | for (i = 0; i < steps; i++) { |
| 3307 | if (sndcmd) { |
| 3308 | if (mtd->writesize <= 512) { |
| 3309 | uint32_t fill = 0xFFFFFFFF; |
| 3310 | |
| 3311 | len = eccsize; |
| 3312 | while (len > 0) { |
| 3313 | int num = min_t(int, len, 4); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3314 | |
| 3315 | ret = nand_write_data_op(chip, &fill, |
| 3316 | num, false); |
| 3317 | if (ret) |
| 3318 | return ret; |
| 3319 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3320 | len -= num; |
| 3321 | } |
| 3322 | } else { |
| 3323 | pos = eccsize + i * (eccsize + chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3324 | ret = nand_change_write_column_op(chip, pos, |
| 3325 | NULL, 0, |
| 3326 | false); |
| 3327 | if (ret) |
| 3328 | return ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3329 | } |
| 3330 | } else |
| 3331 | sndcmd = 1; |
| 3332 | len = min_t(int, length, chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3333 | |
| 3334 | ret = nand_write_data_op(chip, bufpoi, len, false); |
| 3335 | if (ret) |
| 3336 | return ret; |
| 3337 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3338 | bufpoi += len; |
| 3339 | length -= len; |
| 3340 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3341 | if (length > 0) { |
| 3342 | ret = nand_write_data_op(chip, bufpoi, length, false); |
| 3343 | if (ret) |
| 3344 | return ret; |
| 3345 | } |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3346 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3347 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3351 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3352 | * @mtd: MTD device structure |
| 3353 | * @from: offset to read from |
| 3354 | * @ops: oob operations description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3355 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3356 | * NAND read out-of-band data from the spare area. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3357 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3358 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 3359 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | { |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3361 | unsigned int max_bitflips = 0; |
Brian Norris | c00a099 | 2012-05-01 17:12:54 -0700 | [diff] [blame] | 3362 | int page, realpage, chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3363 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3364 | struct mtd_ecc_stats stats; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3365 | int readlen = ops->ooblen; |
| 3366 | int len; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3367 | uint8_t *buf = ops->oobbuf; |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3368 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3369 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3370 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 3371 | __func__, (unsigned long long)from, readlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3372 | |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3373 | stats = mtd->ecc_stats; |
| 3374 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3375 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3376 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3377 | chipnr = (int)(from >> chip->chip_shift); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3378 | chip->select_chip(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3380 | /* Shift to get page */ |
| 3381 | realpage = (int)(from >> chip->page_shift); |
| 3382 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3383 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3384 | while (1) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3385 | if (ops->mode == MTD_OPS_RAW) |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3386 | ret = chip->ecc.read_oob_raw(chip, page); |
Brian Norris | c46f648 | 2011-08-30 18:45:38 -0700 | [diff] [blame] | 3387 | else |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3388 | ret = chip->ecc.read_oob(chip, page); |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3389 | |
| 3390 | if (ret < 0) |
| 3391 | break; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3392 | |
| 3393 | len = min(len, readlen); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3394 | buf = nand_transfer_oob(mtd, buf, ops, len); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3395 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3396 | nand_wait_readrdy(chip); |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 3397 | |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3398 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
| 3399 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3400 | readlen -= len; |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 3401 | if (!readlen) |
| 3402 | break; |
| 3403 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3404 | /* Increment page address */ |
| 3405 | realpage++; |
| 3406 | |
| 3407 | page = realpage & chip->pagemask; |
| 3408 | /* Check, if we cross a chip boundary */ |
| 3409 | if (!page) { |
| 3410 | chipnr++; |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3411 | chip->select_chip(chip, -1); |
| 3412 | chip->select_chip(chip, chipnr); |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3413 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3414 | } |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3415 | chip->select_chip(chip, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3417 | ops->oobretlen = ops->ooblen - readlen; |
| 3418 | |
| 3419 | if (ret < 0) |
| 3420 | return ret; |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3421 | |
| 3422 | if (mtd->ecc_stats.failed - stats.failed) |
| 3423 | return -EBADMSG; |
| 3424 | |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3425 | return max_bitflips; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3426 | } |
| 3427 | |
| 3428 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3429 | * 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] | 3430 | * @mtd: MTD device structure |
| 3431 | * @from: offset to read from |
| 3432 | * @ops: oob operation description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3434 | * NAND read data and/or out-of-band data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3436 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 3437 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3438 | { |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 3439 | int ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3440 | |
| 3441 | ops->retlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 3443 | if (ops->mode != MTD_OPS_PLACE_OOB && |
| 3444 | ops->mode != MTD_OPS_AUTO_OOB && |
| 3445 | ops->mode != MTD_OPS_RAW) |
| 3446 | return -ENOTSUPP; |
| 3447 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3448 | nand_get_device(mtd, FL_READING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3450 | if (!ops->datbuf) |
| 3451 | ret = nand_do_read_oob(mtd, from, ops); |
| 3452 | else |
| 3453 | ret = nand_do_read_ops(mtd, from, ops); |
| 3454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3455 | nand_release_device(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3456 | return ret; |
| 3457 | } |
| 3458 | |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 3459 | /** |
| 3460 | * nand_write_page_raw_notsupp - dummy raw page write function |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 3461 | * @chip: nand chip info structure |
| 3462 | * @buf: data buffer |
| 3463 | * @oob_required: must write chip->oob_poi to OOB |
| 3464 | * @page: page number to write |
| 3465 | * |
| 3466 | * Returns -ENOTSUPP unconditionally. |
| 3467 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3468 | int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf, |
| 3469 | int oob_required, int page) |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 3470 | { |
| 3471 | return -ENOTSUPP; |
| 3472 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3473 | |
| 3474 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3475 | * nand_write_page_raw - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3476 | * @chip: nand chip info structure |
| 3477 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3478 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3479 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3480 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3481 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3482 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3483 | int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf, |
| 3484 | int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3485 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3486 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3487 | int ret; |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3488 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3489 | ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3490 | if (ret) |
| 3491 | return ret; |
| 3492 | |
| 3493 | if (oob_required) { |
| 3494 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 3495 | false); |
| 3496 | if (ret) |
| 3497 | return ret; |
| 3498 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3499 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3500 | return nand_prog_page_end_op(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 3502 | EXPORT_SYMBOL(nand_write_page_raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3504 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3505 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3506 | * @chip: nand chip info structure |
| 3507 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3508 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3509 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3510 | * |
| 3511 | * We need a special oob layout and handling even when ECC isn't checked. |
| 3512 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3513 | static int nand_write_page_raw_syndrome(struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3514 | const uint8_t *buf, int oob_required, |
| 3515 | int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3516 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3517 | struct mtd_info *mtd = nand_to_mtd(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3518 | int eccsize = chip->ecc.size; |
| 3519 | int eccbytes = chip->ecc.bytes; |
| 3520 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3521 | int steps, size, ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3522 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3523 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3524 | if (ret) |
| 3525 | return ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3526 | |
| 3527 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3528 | ret = nand_write_data_op(chip, buf, eccsize, false); |
| 3529 | if (ret) |
| 3530 | return ret; |
| 3531 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3532 | buf += eccsize; |
| 3533 | |
| 3534 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3535 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 3536 | false); |
| 3537 | if (ret) |
| 3538 | return ret; |
| 3539 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3540 | oob += chip->ecc.prepad; |
| 3541 | } |
| 3542 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3543 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 3544 | if (ret) |
| 3545 | return ret; |
| 3546 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3547 | oob += eccbytes; |
| 3548 | |
| 3549 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3550 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 3551 | false); |
| 3552 | if (ret) |
| 3553 | return ret; |
| 3554 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3555 | oob += chip->ecc.postpad; |
| 3556 | } |
| 3557 | } |
| 3558 | |
| 3559 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3560 | if (size) { |
| 3561 | ret = nand_write_data_op(chip, oob, size, false); |
| 3562 | if (ret) |
| 3563 | return ret; |
| 3564 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3565 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3566 | return nand_prog_page_end_op(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3567 | } |
| 3568 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3569 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3570 | * @chip: nand chip info structure |
| 3571 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3572 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3573 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3574 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3575 | static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf, |
| 3576 | int oob_required, int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3577 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3578 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3579 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3580 | int eccbytes = chip->ecc.bytes; |
| 3581 | int eccsteps = chip->ecc.steps; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3582 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3583 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3584 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3585 | /* Software ECC calculation */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3586 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3587 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3588 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3589 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 3590 | chip->ecc.total); |
| 3591 | if (ret) |
| 3592 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3593 | |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3594 | return chip->ecc.write_page_raw(chip, buf, 1, page); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3595 | } |
| 3596 | |
| 3597 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3598 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3599 | * @chip: nand chip info structure |
| 3600 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3601 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3602 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3603 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3604 | static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf, |
| 3605 | int oob_required, int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3606 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3607 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3608 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3609 | int eccbytes = chip->ecc.bytes; |
| 3610 | int eccsteps = chip->ecc.steps; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3611 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3612 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3613 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3614 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3615 | if (ret) |
| 3616 | return ret; |
| 3617 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3618 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3619 | chip->ecc.hwctl(chip, NAND_ECC_WRITE); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3620 | |
| 3621 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 3622 | if (ret) |
| 3623 | return ret; |
| 3624 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3625 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3626 | } |
| 3627 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3628 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 3629 | chip->ecc.total); |
| 3630 | if (ret) |
| 3631 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3632 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3633 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 3634 | if (ret) |
| 3635 | return ret; |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3636 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3637 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3638 | } |
| 3639 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3640 | |
| 3641 | /** |
Brian Norris | 73c8aaf | 2015-02-28 02:04:18 -0800 | [diff] [blame] | 3642 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3643 | * @chip: nand chip info structure |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3644 | * @offset: column address of subpage within the page |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3645 | * @data_len: data length |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3646 | * @buf: data buffer |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3647 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3648 | * @page: page number to write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3649 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3650 | static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset, |
| 3651 | uint32_t data_len, const uint8_t *buf, |
| 3652 | int oob_required, int page) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3653 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3654 | struct mtd_info *mtd = nand_to_mtd(chip); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3655 | uint8_t *oob_buf = chip->oob_poi; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3656 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3657 | int ecc_size = chip->ecc.size; |
| 3658 | int ecc_bytes = chip->ecc.bytes; |
| 3659 | int ecc_steps = chip->ecc.steps; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3660 | uint32_t start_step = offset / ecc_size; |
| 3661 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 3662 | int oob_bytes = mtd->oobsize / ecc_steps; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3663 | int step, ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3664 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3665 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3666 | if (ret) |
| 3667 | return ret; |
| 3668 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3669 | for (step = 0; step < ecc_steps; step++) { |
| 3670 | /* configure controller for WRITE access */ |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3671 | chip->ecc.hwctl(chip, NAND_ECC_WRITE); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3672 | |
| 3673 | /* write data (untouched subpages already masked by 0xFF) */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3674 | ret = nand_write_data_op(chip, buf, ecc_size, false); |
| 3675 | if (ret) |
| 3676 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3677 | |
| 3678 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 3679 | if ((step < start_step) || (step > end_step)) |
| 3680 | memset(ecc_calc, 0xff, ecc_bytes); |
| 3681 | else |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3682 | chip->ecc.calculate(chip, buf, ecc_calc); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3683 | |
| 3684 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 3685 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 3686 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 3687 | memset(oob_buf, 0xff, oob_bytes); |
| 3688 | |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3689 | buf += ecc_size; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3690 | ecc_calc += ecc_bytes; |
| 3691 | oob_buf += oob_bytes; |
| 3692 | } |
| 3693 | |
| 3694 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 3695 | /* this include masked-value(0xFF) for unwritten subpages */ |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3696 | ecc_calc = chip->ecc.calc_buf; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3697 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 3698 | chip->ecc.total); |
| 3699 | if (ret) |
| 3700 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3701 | |
| 3702 | /* write OOB buffer to NAND device */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3703 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 3704 | if (ret) |
| 3705 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3706 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3707 | return nand_prog_page_end_op(chip); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3711 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3712 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3713 | * @chip: nand chip info structure |
| 3714 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3715 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3716 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3717 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3718 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 3719 | * need a special oob layout and handling. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3720 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3721 | static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf, |
| 3722 | int oob_required, int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3723 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3724 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3725 | int i, eccsize = chip->ecc.size; |
| 3726 | int eccbytes = chip->ecc.bytes; |
| 3727 | int eccsteps = chip->ecc.steps; |
| 3728 | const uint8_t *p = buf; |
| 3729 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3730 | int ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3731 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3732 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3733 | if (ret) |
| 3734 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3735 | |
| 3736 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3737 | chip->ecc.hwctl(chip, NAND_ECC_WRITE); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3738 | |
| 3739 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 3740 | if (ret) |
| 3741 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3742 | |
| 3743 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3744 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 3745 | false); |
| 3746 | if (ret) |
| 3747 | return ret; |
| 3748 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3749 | oob += chip->ecc.prepad; |
| 3750 | } |
| 3751 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3752 | chip->ecc.calculate(chip, p, oob); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3753 | |
| 3754 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 3755 | if (ret) |
| 3756 | return ret; |
| 3757 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3758 | oob += eccbytes; |
| 3759 | |
| 3760 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3761 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 3762 | false); |
| 3763 | if (ret) |
| 3764 | return ret; |
| 3765 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3766 | oob += chip->ecc.postpad; |
| 3767 | } |
| 3768 | } |
| 3769 | |
| 3770 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 3771 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3772 | if (i) { |
| 3773 | ret = nand_write_data_op(chip, oob, i, false); |
| 3774 | if (ret) |
| 3775 | return ret; |
| 3776 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3777 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3778 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3779 | } |
| 3780 | |
| 3781 | /** |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 3782 | * nand_write_page - write one page |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3783 | * @mtd: MTD device structure |
| 3784 | * @chip: NAND chip descriptor |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3785 | * @offset: address offset within the page |
| 3786 | * @data_len: length of actual data to be written |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3787 | * @buf: the data to write |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3788 | * @oob_required: must write chip->oob_poi to OOB |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3789 | * @page: page number to write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3790 | * @raw: use _raw version of write_page |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3791 | */ |
| 3792 | static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3793 | uint32_t offset, int data_len, const uint8_t *buf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 3794 | int oob_required, int page, int raw) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3795 | { |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3796 | int status, subpage; |
| 3797 | |
| 3798 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 3799 | chip->ecc.write_subpage) |
| 3800 | subpage = offset || (data_len < mtd->writesize); |
| 3801 | else |
| 3802 | subpage = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3803 | |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3804 | if (unlikely(raw)) |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3805 | status = chip->ecc.write_page_raw(chip, buf, oob_required, |
| 3806 | page); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3807 | else if (subpage) |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3808 | status = chip->ecc.write_subpage(chip, offset, data_len, buf, |
| 3809 | oob_required, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3810 | else |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3811 | status = chip->ecc.write_page(chip, buf, oob_required, page); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3812 | |
| 3813 | if (status < 0) |
| 3814 | return status; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3815 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3816 | return 0; |
| 3817 | } |
| 3818 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3819 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3820 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3821 | * @mtd: MTD device structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3822 | * @oob: oob data buffer |
| 3823 | * @len: oob data write length |
| 3824 | * @ops: oob ops structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3825 | */ |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3826 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
| 3827 | struct mtd_oob_ops *ops) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3828 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3829 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3830 | int ret; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3831 | |
| 3832 | /* |
| 3833 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 3834 | * data from a previous OOB read. |
| 3835 | */ |
| 3836 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 3837 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3838 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3839 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3840 | case MTD_OPS_PLACE_OOB: |
| 3841 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3842 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 3843 | return oob + len; |
| 3844 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3845 | case MTD_OPS_AUTO_OOB: |
| 3846 | ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi, |
| 3847 | ops->ooboffs, len); |
| 3848 | BUG_ON(ret); |
| 3849 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3850 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3851 | default: |
| 3852 | BUG(); |
| 3853 | } |
| 3854 | return NULL; |
| 3855 | } |
| 3856 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3857 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3858 | |
| 3859 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3860 | * nand_do_write_ops - [INTERN] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3861 | * @mtd: MTD device structure |
| 3862 | * @to: offset to write to |
| 3863 | * @ops: oob operations description structure |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3864 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3865 | * NAND write with ECC. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3866 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3867 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 3868 | struct mtd_oob_ops *ops) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3869 | { |
Corentin Labbe | 73600b6 | 2017-09-02 10:49:38 +0200 | [diff] [blame] | 3870 | int chipnr, realpage, page, column; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3871 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3872 | uint32_t writelen = ops->len; |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3873 | |
| 3874 | uint32_t oobwritelen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3875 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3876 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3877 | uint8_t *oob = ops->oobbuf; |
| 3878 | uint8_t *buf = ops->datbuf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3879 | int ret; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3880 | int oob_required = oob ? 1 : 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3881 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3882 | ops->retlen = 0; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3883 | if (!writelen) |
| 3884 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3885 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3886 | /* Reject writes, which are not page aligned */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3887 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 3888 | pr_notice("%s: attempt to write non page aligned data\n", |
| 3889 | __func__); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3890 | return -EINVAL; |
| 3891 | } |
| 3892 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3893 | column = to & (mtd->writesize - 1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3894 | |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 3895 | chipnr = (int)(to >> chip->chip_shift); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3896 | chip->select_chip(chip, chipnr); |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 3897 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3898 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3899 | if (nand_check_wp(mtd)) { |
| 3900 | ret = -EIO; |
| 3901 | goto err_out; |
| 3902 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3903 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3904 | realpage = (int)(to >> chip->page_shift); |
| 3905 | page = realpage & chip->pagemask; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3906 | |
| 3907 | /* Invalidate the page cache, when we write to the cached page */ |
Brian Norris | 537ab1b | 2014-07-21 19:08:03 -0700 | [diff] [blame] | 3908 | if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && |
| 3909 | ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3910 | chip->pagebuf = -1; |
| 3911 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3912 | /* Don't allow multipage oob writes with offset */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3913 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 3914 | ret = -EINVAL; |
| 3915 | goto err_out; |
| 3916 | } |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3917 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3918 | while (1) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3919 | int bytes = mtd->writesize; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3920 | uint8_t *wbuf = buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3921 | int use_bufpoi; |
Hector Palacios | 144f4c9 | 2016-07-18 10:39:18 +0200 | [diff] [blame] | 3922 | int part_pagewr = (column || writelen < mtd->writesize); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3923 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3924 | if (part_pagewr) |
| 3925 | use_bufpoi = 1; |
| 3926 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 3927 | use_bufpoi = !virt_addr_valid(buf) || |
| 3928 | !IS_ALIGNED((unsigned long)buf, |
| 3929 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3930 | else |
| 3931 | use_bufpoi = 0; |
| 3932 | |
| 3933 | /* Partial page write?, or need to use bounce buffer */ |
| 3934 | if (use_bufpoi) { |
| 3935 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 3936 | __func__, buf); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3937 | if (part_pagewr) |
| 3938 | bytes = min_t(int, bytes - column, writelen); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3939 | chip->pagebuf = -1; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3940 | memset(chip->data_buf, 0xff, mtd->writesize); |
| 3941 | memcpy(&chip->data_buf[column], buf, bytes); |
| 3942 | wbuf = chip->data_buf; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3943 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3944 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3945 | if (unlikely(oob)) { |
| 3946 | size_t len = min(oobwritelen, oobmaxlen); |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3947 | oob = nand_fill_oob(mtd, oob, len, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3948 | oobwritelen -= len; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3949 | } else { |
| 3950 | /* We still need to erase leftover OOB data */ |
| 3951 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3952 | } |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 3953 | |
| 3954 | ret = nand_write_page(mtd, chip, column, bytes, wbuf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 3955 | oob_required, page, |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 3956 | (ops->mode == MTD_OPS_RAW)); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3957 | if (ret) |
| 3958 | break; |
| 3959 | |
| 3960 | writelen -= bytes; |
| 3961 | if (!writelen) |
| 3962 | break; |
| 3963 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3964 | column = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3965 | buf += bytes; |
| 3966 | realpage++; |
| 3967 | |
| 3968 | page = realpage & chip->pagemask; |
| 3969 | /* Check, if we cross a chip boundary */ |
| 3970 | if (!page) { |
| 3971 | chipnr++; |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3972 | chip->select_chip(chip, -1); |
| 3973 | chip->select_chip(chip, chipnr); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3974 | } |
| 3975 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3976 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3977 | ops->retlen = ops->len - writelen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3978 | if (unlikely(oob)) |
| 3979 | ops->oobretlen = ops->ooblen; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3980 | |
| 3981 | err_out: |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3982 | chip->select_chip(chip, -1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3983 | return ret; |
| 3984 | } |
| 3985 | |
| 3986 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 3987 | * panic_nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3988 | * @mtd: MTD device structure |
| 3989 | * @to: offset to write to |
| 3990 | * @len: number of bytes to write |
| 3991 | * @retlen: pointer to variable to store the number of written bytes |
| 3992 | * @buf: the data to write |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 3993 | * |
| 3994 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 3995 | * may for example be called by mtdoops when writing an oops while in panic. |
| 3996 | */ |
| 3997 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 3998 | size_t *retlen, const uint8_t *buf) |
| 3999 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4000 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4001 | int chipnr = (int)(to >> chip->chip_shift); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4002 | struct mtd_oob_ops ops; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4003 | int ret; |
| 4004 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4005 | /* Grab the device */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4006 | panic_nand_get_device(chip, mtd, FL_WRITING); |
| 4007 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4008 | chip->select_chip(chip, chipnr); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4009 | |
| 4010 | /* Wait for the device to get ready */ |
Boris Brezillon | f1d4694 | 2018-09-06 14:05:29 +0200 | [diff] [blame] | 4011 | panic_nand_wait(chip, 400); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4012 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 4013 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4014 | ops.len = len; |
| 4015 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 4016 | ops.mode = MTD_OPS_PLACE_OOB; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4017 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4018 | ret = nand_do_write_ops(mtd, to, &ops); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4019 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4020 | *retlen = ops.retlen; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4021 | return ret; |
| 4022 | } |
| 4023 | |
| 4024 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4025 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4026 | * @mtd: MTD device structure |
| 4027 | * @to: offset to write to |
| 4028 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4029 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4030 | * NAND write out-of-band. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4031 | */ |
| 4032 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 4033 | struct mtd_oob_ops *ops) |
| 4034 | { |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 4035 | int chipnr, page, status, len; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4036 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4037 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4038 | pr_debug("%s: to = 0x%08x, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 4039 | __func__, (unsigned int)to, (int)ops->ooblen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4040 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 4041 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 4042 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4043 | /* Do not allow write past end of page */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 4044 | if ((ops->ooboffs + ops->ooblen) > len) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4045 | pr_debug("%s: attempt to write past end of page\n", |
| 4046 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4047 | return -EINVAL; |
| 4048 | } |
| 4049 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 4050 | chipnr = (int)(to >> chip->chip_shift); |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 4051 | |
| 4052 | /* |
| 4053 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 4054 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 4055 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 4056 | * it in the doc2000 driver in August 1999. dwmw2. |
| 4057 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4058 | nand_reset(chip, chipnr); |
| 4059 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4060 | chip->select_chip(chip, chipnr); |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4061 | |
| 4062 | /* Shift to get page */ |
| 4063 | page = (int)(to >> chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4064 | |
| 4065 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4066 | if (nand_check_wp(mtd)) { |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4067 | chip->select_chip(chip, -1); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4068 | return -EROFS; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4069 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | /* Invalidate the page cache, if we write to the cached page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4072 | if (page == chip->pagebuf) |
| 4073 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 4075 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 4076 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 4077 | if (ops->mode == MTD_OPS_RAW) |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4078 | status = chip->ecc.write_oob_raw(chip, page & chip->pagemask); |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 4079 | else |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4080 | status = chip->ecc.write_oob(chip, page & chip->pagemask); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4081 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4082 | chip->select_chip(chip, -1); |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4083 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 4084 | if (status) |
| 4085 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4086 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 4087 | ops->oobretlen = ops->ooblen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4088 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 4089 | return 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4090 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4091 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4092 | /** |
| 4093 | * 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] | 4094 | * @mtd: MTD device structure |
| 4095 | * @to: offset to write to |
| 4096 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4097 | */ |
| 4098 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 4099 | struct mtd_oob_ops *ops) |
| 4100 | { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4101 | int ret = -ENOTSUPP; |
| 4102 | |
| 4103 | ops->retlen = 0; |
| 4104 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 4105 | nand_get_device(mtd, FL_WRITING); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4106 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4107 | switch (ops->mode) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 4108 | case MTD_OPS_PLACE_OOB: |
| 4109 | case MTD_OPS_AUTO_OOB: |
| 4110 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4111 | break; |
| 4112 | |
| 4113 | default: |
| 4114 | goto out; |
| 4115 | } |
| 4116 | |
| 4117 | if (!ops->datbuf) |
| 4118 | ret = nand_do_write_oob(mtd, to, ops); |
| 4119 | else |
| 4120 | ret = nand_do_write_ops(mtd, to, ops); |
| 4121 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4122 | out: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4123 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | return ret; |
| 4125 | } |
| 4126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4127 | /** |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4128 | * single_erase - [GENERIC] NAND standard block erase command function |
Boris Brezillon | a2098a9 | 2018-09-06 14:05:30 +0200 | [diff] [blame] | 4129 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4130 | * @page: the page address of the block which will be erased |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4131 | * |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4132 | * Standard erase command for NAND chips. Returns NAND status. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4133 | */ |
Boris Brezillon | a2098a9 | 2018-09-06 14:05:30 +0200 | [diff] [blame] | 4134 | static int single_erase(struct nand_chip *chip, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4135 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4136 | unsigned int eraseblock; |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4138 | /* Send commands to erase a block */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4139 | eraseblock = page >> (chip->phys_erase_shift - chip->page_shift); |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4140 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4141 | return nand_erase_op(chip, eraseblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4142 | } |
| 4143 | |
| 4144 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | * nand_erase - [MTD Interface] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4146 | * @mtd: MTD device structure |
| 4147 | * @instr: erase instruction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4148 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4149 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4151 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4152 | { |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4153 | return nand_erase_nand(mtd_to_nand(mtd), instr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4154 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4157 | * nand_erase_nand - [INTERN] erase block(s) |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4158 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4159 | * @instr: erase instruction |
| 4160 | * @allowbbt: allow erasing the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4162 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | */ |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4164 | int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr, |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4165 | int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | { |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4167 | struct mtd_info *mtd = nand_to_mtd(chip); |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4168 | int page, status, pages_per_block, ret, chipnr; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4169 | loff_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4170 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4171 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 4172 | __func__, (unsigned long long)instr->addr, |
| 4173 | (unsigned long long)instr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4174 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 4175 | if (check_offs_len(mtd, instr->addr, instr->len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4176 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4178 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 4179 | nand_get_device(mtd, FL_ERASING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | |
| 4181 | /* Shift to get first page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4182 | page = (int)(instr->addr >> chip->page_shift); |
| 4183 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | |
| 4185 | /* Calculate pages in each block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4186 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | |
| 4188 | /* Select the NAND device */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4189 | chip->select_chip(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | /* Check, if it is write protected */ |
| 4192 | if (nand_check_wp(mtd)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4193 | pr_debug("%s: device is write protected!\n", |
| 4194 | __func__); |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4195 | ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | goto erase_exit; |
| 4197 | } |
| 4198 | |
| 4199 | /* Loop through the pages */ |
| 4200 | len = instr->len; |
| 4201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | while (len) { |
Wolfram Sang | 12183a2 | 2011-12-21 23:01:20 +0100 | [diff] [blame] | 4203 | /* Check if we have a bad block, we do not erase bad blocks! */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4204 | if (nand_block_checkbad(mtd, ((loff_t) page) << |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4205 | chip->page_shift, allowbbt)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4206 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
| 4207 | __func__, page); |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4208 | ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4209 | goto erase_exit; |
| 4210 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4211 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4212 | /* |
| 4213 | * Invalidate the page cache, if we erase the block which |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4214 | * contains the current cached page. |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4215 | */ |
| 4216 | if (page <= chip->pagebuf && chip->pagebuf < |
| 4217 | (page + pages_per_block)) |
| 4218 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | |
Boris Brezillon | f9ebd1b | 2018-09-07 00:38:39 +0200 | [diff] [blame] | 4220 | if (chip->legacy.erase) |
| 4221 | status = chip->legacy.erase(chip, |
| 4222 | page & chip->pagemask); |
| 4223 | else |
| 4224 | status = single_erase(chip, page & chip->pagemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | |
| 4226 | /* See if block erase succeeded */ |
Miquel Raynal | eb94555 | 2017-11-30 18:01:28 +0100 | [diff] [blame] | 4227 | if (status) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4228 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 4229 | __func__, page); |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4230 | ret = -EIO; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4231 | instr->fail_addr = |
| 4232 | ((loff_t)page << chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4233 | goto erase_exit; |
| 4234 | } |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 4235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | /* Increment page address and decrement length */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 4237 | len -= (1ULL << chip->phys_erase_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 | page += pages_per_block; |
| 4239 | |
| 4240 | /* Check, if we cross a chip boundary */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4241 | if (len && !(page & chip->pagemask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | chipnr++; |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4243 | chip->select_chip(chip, -1); |
| 4244 | chip->select_chip(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | } |
| 4246 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4247 | |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4248 | ret = 0; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4249 | erase_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | /* Deselect and wake up anyone waiting on the device */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4252 | chip->select_chip(chip, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4253 | nand_release_device(mtd); |
| 4254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4255 | /* Return more or less happy */ |
| 4256 | return ret; |
| 4257 | } |
| 4258 | |
| 4259 | /** |
| 4260 | * nand_sync - [MTD Interface] sync |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4261 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4263 | * Sync is actually a wait for chip ready function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4264 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4265 | static void nand_sync(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4266 | { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4267 | pr_debug("%s: called\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | |
| 4269 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 4270 | nand_get_device(mtd, FL_SYNCING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | /* Release it and go back */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4272 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | } |
| 4274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4276 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4277 | * @mtd: MTD device structure |
| 4278 | * @offs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4280 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | { |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4282 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4283 | int chipnr = (int)(offs >> chip->chip_shift); |
| 4284 | int ret; |
| 4285 | |
| 4286 | /* Select the NAND device */ |
| 4287 | nand_get_device(mtd, FL_READING); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4288 | chip->select_chip(chip, chipnr); |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4289 | |
| 4290 | ret = nand_block_checkbad(mtd, offs, 0); |
| 4291 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4292 | chip->select_chip(chip, -1); |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4293 | nand_release_device(mtd); |
| 4294 | |
| 4295 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4296 | } |
| 4297 | |
| 4298 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4299 | * 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] | 4300 | * @mtd: MTD device structure |
| 4301 | * @ofs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4303 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | int ret; |
| 4306 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4307 | ret = nand_block_isbad(mtd, ofs); |
| 4308 | if (ret) { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4309 | /* If it was bad already, return success and do nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | if (ret > 0) |
| 4311 | return 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4312 | return ret; |
| 4313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 4315 | return nand_block_markbad_lowlevel(mtd, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4316 | } |
| 4317 | |
| 4318 | /** |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 4319 | * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd |
| 4320 | * @mtd: MTD device structure |
| 4321 | * @ofs: offset relative to mtd start |
| 4322 | * @len: length of mtd |
| 4323 | */ |
| 4324 | static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len) |
| 4325 | { |
| 4326 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4327 | u32 part_start_block; |
| 4328 | u32 part_end_block; |
| 4329 | u32 part_start_die; |
| 4330 | u32 part_end_die; |
| 4331 | |
| 4332 | /* |
| 4333 | * max_bb_per_die and blocks_per_die used to determine |
| 4334 | * the maximum bad block count. |
| 4335 | */ |
| 4336 | if (!chip->max_bb_per_die || !chip->blocks_per_die) |
| 4337 | return -ENOTSUPP; |
| 4338 | |
| 4339 | /* Get the start and end of the partition in erase blocks. */ |
| 4340 | part_start_block = mtd_div_by_eb(ofs, mtd); |
| 4341 | part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1; |
| 4342 | |
| 4343 | /* Get the start and end LUNs of the partition. */ |
| 4344 | part_start_die = part_start_block / chip->blocks_per_die; |
| 4345 | part_end_die = part_end_block / chip->blocks_per_die; |
| 4346 | |
| 4347 | /* |
| 4348 | * Look up the bad blocks per unit and multiply by the number of units |
| 4349 | * that the partition spans. |
| 4350 | */ |
| 4351 | return chip->max_bb_per_die * (part_end_die - part_start_die + 1); |
| 4352 | } |
| 4353 | |
| 4354 | /** |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4355 | * nand_suspend - [MTD Interface] Suspend the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4356 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4357 | */ |
| 4358 | static int nand_suspend(struct mtd_info *mtd) |
| 4359 | { |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 4360 | return nand_get_device(mtd, FL_PM_SUSPENDED); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | /** |
| 4364 | * nand_resume - [MTD Interface] Resume the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4365 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4366 | */ |
| 4367 | static void nand_resume(struct mtd_info *mtd) |
| 4368 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4369 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4370 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4371 | if (chip->state == FL_PM_SUSPENDED) |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4372 | nand_release_device(mtd); |
| 4373 | else |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4374 | pr_err("%s called for a chip which is not in suspended state\n", |
| 4375 | __func__); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4376 | } |
| 4377 | |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 4378 | /** |
| 4379 | * nand_shutdown - [MTD Interface] Finish the current NAND operation and |
| 4380 | * prevent further operations |
| 4381 | * @mtd: MTD device structure |
| 4382 | */ |
| 4383 | static void nand_shutdown(struct mtd_info *mtd) |
| 4384 | { |
Brian Norris | 9ca641b | 2015-11-09 16:37:28 -0800 | [diff] [blame] | 4385 | nand_get_device(mtd, FL_PM_SUSPENDED); |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 4386 | } |
| 4387 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4388 | /* Set default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4389 | static void nand_set_defaults(struct nand_chip *chip) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4390 | { |
Boris Brezillon | 3d4af7c | 2018-09-07 00:38:49 +0200 | [diff] [blame] | 4391 | nand_legacy_set_defaults(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4392 | |
| 4393 | if (!chip->controller) { |
Miquel Raynal | 7da4513 | 2018-07-17 09:08:02 +0200 | [diff] [blame] | 4394 | chip->controller = &chip->dummy_controller; |
| 4395 | nand_controller_init(chip->controller); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4396 | } |
| 4397 | |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 4398 | if (!chip->buf_align) |
| 4399 | chip->buf_align = 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4400 | } |
| 4401 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4402 | /* Sanitize ONFI strings so we can safely print them */ |
Boris Brezillon | 1c325cc | 2018-09-07 00:38:50 +0200 | [diff] [blame] | 4403 | void sanitize_string(uint8_t *s, size_t len) |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4404 | { |
| 4405 | ssize_t i; |
| 4406 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4407 | /* Null terminate */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4408 | s[len - 1] = 0; |
| 4409 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4410 | /* Remove non printable chars */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4411 | for (i = 0; i < len - 1; i++) { |
| 4412 | if (s[i] < ' ' || s[i] > 127) |
| 4413 | s[i] = '?'; |
| 4414 | } |
| 4415 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4416 | /* Remove trailing spaces */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4417 | strim(s); |
| 4418 | } |
| 4419 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4420 | /* |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 4421 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 4422 | * @id_data: the ID string |
| 4423 | * @arrlen: the length of the @id_data array |
| 4424 | * @period: the period of repitition |
| 4425 | * |
| 4426 | * Check if an ID string is repeated within a given sequence of bytes at |
| 4427 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Brian Norris | d4d4f1b | 2012-11-14 21:54:20 -0800 | [diff] [blame] | 4428 | * 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] | 4429 | * if the repetition has a period of @period; otherwise, returns zero. |
| 4430 | */ |
| 4431 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 4432 | { |
| 4433 | int i, j; |
| 4434 | for (i = 0; i < period; i++) |
| 4435 | for (j = i + period; j < arrlen; j += period) |
| 4436 | if (id_data[i] != id_data[j]) |
| 4437 | return 0; |
| 4438 | return 1; |
| 4439 | } |
| 4440 | |
| 4441 | /* |
| 4442 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 4443 | * @id_data: the ID string |
| 4444 | * @arrlen: the length of the @id_data array |
| 4445 | |
| 4446 | * Returns the length of the ID string, according to known wraparound/trailing |
| 4447 | * zero patterns. If no pattern exists, returns the length of the array. |
| 4448 | */ |
| 4449 | static int nand_id_len(u8 *id_data, int arrlen) |
| 4450 | { |
| 4451 | int last_nonzero, period; |
| 4452 | |
| 4453 | /* Find last non-zero byte */ |
| 4454 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 4455 | if (id_data[last_nonzero]) |
| 4456 | break; |
| 4457 | |
| 4458 | /* All zeros */ |
| 4459 | if (last_nonzero < 0) |
| 4460 | return 0; |
| 4461 | |
| 4462 | /* Calculate wraparound period */ |
| 4463 | for (period = 1; period < arrlen; period++) |
| 4464 | if (nand_id_has_period(id_data, arrlen, period)) |
| 4465 | break; |
| 4466 | |
| 4467 | /* There's a repeated pattern */ |
| 4468 | if (period < arrlen) |
| 4469 | return period; |
| 4470 | |
| 4471 | /* There are trailing zeros */ |
| 4472 | if (last_nonzero < arrlen - 1) |
| 4473 | return last_nonzero + 1; |
| 4474 | |
| 4475 | /* No pattern detected */ |
| 4476 | return arrlen; |
| 4477 | } |
| 4478 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 4479 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 4480 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 4481 | { |
| 4482 | int bits; |
| 4483 | |
| 4484 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 4485 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 4486 | return bits + 1; |
| 4487 | } |
| 4488 | |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 4489 | /* |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4490 | * Many new NAND share similar device ID codes, which represent the size of the |
| 4491 | * chip. The rest of the parameters must be decoded according to generic or |
| 4492 | * manufacturer-specific "extended ID" decoding patterns. |
| 4493 | */ |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4494 | void nand_decode_ext_id(struct nand_chip *chip) |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4495 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4496 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 9b2d61f | 2016-06-08 10:34:57 +0200 | [diff] [blame] | 4497 | int extid; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4498 | u8 *id_data = chip->id.data; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4499 | /* The 3rd id byte holds MLC / multichip data */ |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 4500 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4501 | /* The 4th id byte is the important one */ |
| 4502 | extid = id_data[3]; |
| 4503 | |
Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 4504 | /* Calc pagesize */ |
| 4505 | mtd->writesize = 1024 << (extid & 0x03); |
| 4506 | extid >>= 2; |
| 4507 | /* Calc oobsize */ |
| 4508 | mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9); |
| 4509 | extid >>= 2; |
| 4510 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 4511 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 4512 | extid >>= 2; |
| 4513 | /* Get buswidth information */ |
| 4514 | if (extid & 0x1) |
| 4515 | chip->options |= NAND_BUSWIDTH_16; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4516 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4517 | EXPORT_SYMBOL_GPL(nand_decode_ext_id); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4518 | |
| 4519 | /* |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4520 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 4521 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 4522 | * the chip. |
| 4523 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4524 | 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] | 4525 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4526 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4527 | |
| 4528 | mtd->erasesize = type->erasesize; |
| 4529 | mtd->writesize = type->pagesize; |
| 4530 | mtd->oobsize = mtd->writesize / 32; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4531 | |
Huang Shijie | 1c195e9 | 2013-09-25 14:58:12 +0800 | [diff] [blame] | 4532 | /* All legacy ID NAND are small-page, SLC */ |
| 4533 | chip->bits_per_cell = 1; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | /* |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4537 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 4538 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 4539 | * page size, cell-type information). |
| 4540 | */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4541 | static void nand_decode_bbm_options(struct nand_chip *chip) |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4542 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4543 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4544 | |
| 4545 | /* Set the bad block position */ |
| 4546 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 4547 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 4548 | else |
| 4549 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4550 | } |
| 4551 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4552 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 4553 | { |
| 4554 | return type->id_len; |
| 4555 | } |
| 4556 | |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4557 | static bool find_full_id_nand(struct nand_chip *chip, |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4558 | struct nand_flash_dev *type) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4559 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4560 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4561 | u8 *id_data = chip->id.data; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4562 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4563 | if (!strncmp(type->id, id_data, type->id_len)) { |
| 4564 | mtd->writesize = type->pagesize; |
| 4565 | mtd->erasesize = type->erasesize; |
| 4566 | mtd->oobsize = type->oobsize; |
| 4567 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 4568 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4569 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 4570 | chip->options |= type->options; |
Huang Shijie | 5721934 | 2013-05-17 11:17:32 +0800 | [diff] [blame] | 4571 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
| 4572 | chip->ecc_step_ds = NAND_ECC_STEP(type); |
Boris BREZILLON | 57a94e2 | 2014-09-22 20:11:50 +0200 | [diff] [blame] | 4573 | chip->onfi_timing_mode_default = |
| 4574 | type->onfi_timing_mode_default; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4575 | |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4576 | chip->parameters.model = kstrdup(type->name, GFP_KERNEL); |
| 4577 | if (!chip->parameters.model) |
| 4578 | return false; |
Cai Zhiyong | 092b6a1 | 2013-12-25 21:19:21 +0800 | [diff] [blame] | 4579 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4580 | return true; |
| 4581 | } |
| 4582 | return false; |
| 4583 | } |
| 4584 | |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4585 | /* |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4586 | * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC |
| 4587 | * compliant and does not have a full-id or legacy-id entry in the nand_ids |
| 4588 | * table. |
| 4589 | */ |
| 4590 | static void nand_manufacturer_detect(struct nand_chip *chip) |
| 4591 | { |
| 4592 | /* |
| 4593 | * Try manufacturer detection if available and use |
| 4594 | * nand_decode_ext_id() otherwise. |
| 4595 | */ |
| 4596 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4597 | chip->manufacturer.desc->ops->detect) { |
| 4598 | /* The 3rd id byte holds MLC / multichip data */ |
| 4599 | 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] | 4600 | chip->manufacturer.desc->ops->detect(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4601 | } else { |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4602 | nand_decode_ext_id(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4603 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4604 | } |
| 4605 | |
| 4606 | /* |
| 4607 | * Manufacturer initialization. This function is called for all NANDs including |
| 4608 | * ONFI and JEDEC compliant ones. |
| 4609 | * Manufacturer drivers should put all their specific initialization code in |
| 4610 | * their ->init() hook. |
| 4611 | */ |
| 4612 | static int nand_manufacturer_init(struct nand_chip *chip) |
| 4613 | { |
| 4614 | if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops || |
| 4615 | !chip->manufacturer.desc->ops->init) |
| 4616 | return 0; |
| 4617 | |
| 4618 | return chip->manufacturer.desc->ops->init(chip); |
| 4619 | } |
| 4620 | |
| 4621 | /* |
| 4622 | * Manufacturer cleanup. This function is called for all NANDs including |
| 4623 | * ONFI and JEDEC compliant ones. |
| 4624 | * Manufacturer drivers should put all their specific cleanup code in their |
| 4625 | * ->cleanup() hook. |
| 4626 | */ |
| 4627 | static void nand_manufacturer_cleanup(struct nand_chip *chip) |
| 4628 | { |
| 4629 | /* Release manufacturer private data */ |
| 4630 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
| 4631 | chip->manufacturer.desc->ops->cleanup) |
| 4632 | chip->manufacturer.desc->ops->cleanup(chip); |
| 4633 | } |
| 4634 | |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 4635 | static const char * |
| 4636 | nand_manufacturer_name(const struct nand_manufacturer *manufacturer) |
| 4637 | { |
| 4638 | return manufacturer ? manufacturer->name : "Unknown"; |
| 4639 | } |
| 4640 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4641 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4642 | * 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] | 4643 | */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 4644 | 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] | 4645 | { |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4646 | const struct nand_manufacturer *manufacturer; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4647 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4648 | int busw, ret; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4649 | u8 *id_data = chip->id.data; |
| 4650 | u8 maf_id, dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4651 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4652 | /* |
| 4653 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4654 | * after power-up. |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4655 | */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4656 | ret = nand_reset(chip, 0); |
| 4657 | if (ret) |
| 4658 | return ret; |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4659 | |
| 4660 | /* Select the device */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4661 | chip->select_chip(chip, 0); |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4663 | /* Send the command for reading device ID */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4664 | ret = nand_readid_op(chip, 0, id_data, 2); |
| 4665 | if (ret) |
| 4666 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | |
| 4668 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4669 | maf_id = id_data[0]; |
| 4670 | dev_id = id_data[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4671 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4672 | /* |
| 4673 | * 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] | 4674 | * interface concerns can cause random data which looks like a |
| 4675 | * possibly credible NAND flash to appear. If the two results do |
| 4676 | * not match, ignore the device completely. |
| 4677 | */ |
| 4678 | |
Brian Norris | 4aef9b7 | 2012-09-24 20:40:48 -0700 | [diff] [blame] | 4679 | /* Read entire ID string */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4680 | ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data)); |
| 4681 | if (ret) |
| 4682 | return ret; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 4683 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4684 | if (id_data[0] != maf_id || id_data[1] != dev_id) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4685 | 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] | 4686 | maf_id, dev_id, id_data[0], id_data[1]); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4687 | return -ENODEV; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 4688 | } |
| 4689 | |
Jean-Louis Thekekara | 5158bd5 | 2017-06-29 19:08:30 +0200 | [diff] [blame] | 4690 | 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] | 4691 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4692 | /* Try to identify manufacturer */ |
| 4693 | manufacturer = nand_get_manufacturer(maf_id); |
| 4694 | chip->manufacturer.desc = manufacturer; |
| 4695 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4696 | if (!type) |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4697 | type = nand_flash_ids; |
| 4698 | |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4699 | /* |
| 4700 | * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic |
| 4701 | * override it. |
| 4702 | * This is required to make sure initial NAND bus width set by the |
| 4703 | * NAND controller driver is coherent with the real NAND bus width |
| 4704 | * (extracted by auto-detection code). |
| 4705 | */ |
| 4706 | busw = chip->options & NAND_BUSWIDTH_16; |
| 4707 | |
| 4708 | /* |
| 4709 | * The flag is only set (never cleared), reset it to its default value |
| 4710 | * before starting auto-detection. |
| 4711 | */ |
| 4712 | chip->options &= ~NAND_BUSWIDTH_16; |
| 4713 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4714 | for (; type->name != NULL; type++) { |
| 4715 | if (is_full_id_nand(type)) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4716 | if (find_full_id_nand(chip, type)) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4717 | goto ident_done; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4718 | } else if (dev_id == type->dev_id) { |
Brian Norris | db5b09f | 2015-05-22 10:43:12 -0700 | [diff] [blame] | 4719 | break; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4720 | } |
| 4721 | } |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4722 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4723 | if (!type->name || !type->pagesize) { |
Masahiro Yamada | 35fc519 | 2014-04-09 16:26:26 +0900 | [diff] [blame] | 4724 | /* Check if the chip is ONFI compliant */ |
Boris Brezillon | 1c325cc | 2018-09-07 00:38:50 +0200 | [diff] [blame] | 4725 | ret = nand_onfi_detect(chip); |
Miquel Raynal | bd0b643 | 2018-03-19 14:47:31 +0100 | [diff] [blame] | 4726 | if (ret < 0) |
| 4727 | return ret; |
| 4728 | else if (ret) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4729 | goto ident_done; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4730 | |
| 4731 | /* Check if the chip is JEDEC compliant */ |
Boris Brezillon | 8ae3fbf | 2018-09-07 00:38:51 +0200 | [diff] [blame] | 4732 | ret = nand_jedec_detect(chip); |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 4733 | if (ret < 0) |
| 4734 | return ret; |
| 4735 | else if (ret) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4736 | goto ident_done; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4737 | } |
| 4738 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4739 | if (!type->name) |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4740 | return -ENODEV; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4741 | |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4742 | chip->parameters.model = kstrdup(type->name, GFP_KERNEL); |
| 4743 | if (!chip->parameters.model) |
| 4744 | return -ENOMEM; |
Thomas Gleixner | ba0251fe | 2006-05-27 01:02:13 +0200 | [diff] [blame] | 4745 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4746 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4747 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4748 | if (!type->pagesize) |
| 4749 | nand_manufacturer_detect(chip); |
| 4750 | else |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4751 | nand_decode_id(chip, type); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4752 | |
Brian Norris | bf7a01b | 2012-07-13 09:28:24 -0700 | [diff] [blame] | 4753 | /* Get chip options */ |
| 4754 | chip->options |= type->options; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4755 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4756 | ident_done: |
Miquel Raynal | f4531b2 | 2018-03-19 14:47:26 +0100 | [diff] [blame] | 4757 | if (!mtd->name) |
| 4758 | mtd->name = chip->parameters.model; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4759 | |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 4760 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4761 | WARN_ON(busw & NAND_BUSWIDTH_16); |
| 4762 | nand_set_defaults(chip); |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 4763 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 4764 | /* |
| 4765 | * Check, if buswidth is correct. Hardware drivers should set |
| 4766 | * chip correct! |
| 4767 | */ |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4768 | 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] | 4769 | maf_id, dev_id); |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4770 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4771 | mtd->name); |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4772 | pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8, |
| 4773 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4774 | ret = -EINVAL; |
| 4775 | |
| 4776 | goto free_detect_allocation; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4777 | } |
| 4778 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4779 | nand_decode_bbm_options(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4780 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4781 | /* Calculate the address shift from the page size */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4782 | chip->page_shift = ffs(mtd->writesize) - 1; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4783 | /* Convert chipsize to number of pages per chip -1 */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4784 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4785 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4786 | chip->bbt_erase_shift = chip->phys_erase_shift = |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4787 | ffs(mtd->erasesize) - 1; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4788 | if (chip->chipsize & 0xffffffff) |
| 4789 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4790 | else { |
| 4791 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 4792 | chip->chip_shift += 32 - 1; |
| 4793 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4794 | |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame] | 4795 | if (chip->chip_shift - chip->page_shift > 16) |
| 4796 | chip->options |= NAND_ROW_ADDR_3; |
| 4797 | |
Artem Bityutskiy | 26d9be1 | 2011-04-28 20:26:59 +0300 | [diff] [blame] | 4798 | chip->badblockbits = 8; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4799 | |
Boris Brezillon | 3d4af7c | 2018-09-07 00:38:49 +0200 | [diff] [blame] | 4800 | nand_legacy_adjust_cmdfunc(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4801 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4802 | 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] | 4803 | maf_id, dev_id); |
Miquel Raynal | f4531b2 | 2018-03-19 14:47:26 +0100 | [diff] [blame] | 4804 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4805 | chip->parameters.model); |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4806 | 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] | 4807 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4808 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4809 | return 0; |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4810 | |
| 4811 | free_detect_allocation: |
| 4812 | kfree(chip->parameters.model); |
| 4813 | |
| 4814 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4815 | } |
| 4816 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4817 | static const char * const nand_ecc_modes[] = { |
| 4818 | [NAND_ECC_NONE] = "none", |
| 4819 | [NAND_ECC_SOFT] = "soft", |
| 4820 | [NAND_ECC_HW] = "hw", |
| 4821 | [NAND_ECC_HW_SYNDROME] = "hw_syndrome", |
| 4822 | [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first", |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 4823 | [NAND_ECC_ON_DIE] = "on-die", |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4824 | }; |
| 4825 | |
| 4826 | static int of_get_nand_ecc_mode(struct device_node *np) |
| 4827 | { |
| 4828 | const char *pm; |
| 4829 | int err, i; |
| 4830 | |
| 4831 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4832 | if (err < 0) |
| 4833 | return err; |
| 4834 | |
| 4835 | for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++) |
| 4836 | if (!strcasecmp(pm, nand_ecc_modes[i])) |
| 4837 | return i; |
| 4838 | |
Rafał Miłecki | ae211bc | 2016-04-17 22:53:06 +0200 | [diff] [blame] | 4839 | /* |
| 4840 | * For backward compatibility we support few obsoleted values that don't |
| 4841 | * have their mappings into nand_ecc_modes_t anymore (they were merged |
| 4842 | * with other enums). |
| 4843 | */ |
| 4844 | if (!strcasecmp(pm, "soft_bch")) |
| 4845 | return NAND_ECC_SOFT; |
| 4846 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4847 | return -ENODEV; |
| 4848 | } |
| 4849 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4850 | static const char * const nand_ecc_algos[] = { |
| 4851 | [NAND_ECC_HAMMING] = "hamming", |
| 4852 | [NAND_ECC_BCH] = "bch", |
Stefan Agner | f308d73 | 2018-06-24 23:27:22 +0200 | [diff] [blame] | 4853 | [NAND_ECC_RS] = "rs", |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4854 | }; |
| 4855 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4856 | static int of_get_nand_ecc_algo(struct device_node *np) |
| 4857 | { |
| 4858 | const char *pm; |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4859 | int err, i; |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4860 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4861 | err = of_property_read_string(np, "nand-ecc-algo", &pm); |
| 4862 | if (!err) { |
| 4863 | for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++) |
| 4864 | if (!strcasecmp(pm, nand_ecc_algos[i])) |
| 4865 | return i; |
| 4866 | return -ENODEV; |
| 4867 | } |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4868 | |
| 4869 | /* |
| 4870 | * For backward compatibility we also read "nand-ecc-mode" checking |
| 4871 | * for some obsoleted values that were specifying ECC algorithm. |
| 4872 | */ |
| 4873 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4874 | if (err < 0) |
| 4875 | return err; |
| 4876 | |
| 4877 | if (!strcasecmp(pm, "soft")) |
| 4878 | return NAND_ECC_HAMMING; |
| 4879 | else if (!strcasecmp(pm, "soft_bch")) |
| 4880 | return NAND_ECC_BCH; |
| 4881 | |
| 4882 | return -ENODEV; |
| 4883 | } |
| 4884 | |
| 4885 | static int of_get_nand_ecc_step_size(struct device_node *np) |
| 4886 | { |
| 4887 | int ret; |
| 4888 | u32 val; |
| 4889 | |
| 4890 | ret = of_property_read_u32(np, "nand-ecc-step-size", &val); |
| 4891 | return ret ? ret : val; |
| 4892 | } |
| 4893 | |
| 4894 | static int of_get_nand_ecc_strength(struct device_node *np) |
| 4895 | { |
| 4896 | int ret; |
| 4897 | u32 val; |
| 4898 | |
| 4899 | ret = of_property_read_u32(np, "nand-ecc-strength", &val); |
| 4900 | return ret ? ret : val; |
| 4901 | } |
| 4902 | |
| 4903 | static int of_get_nand_bus_width(struct device_node *np) |
| 4904 | { |
| 4905 | u32 val; |
| 4906 | |
| 4907 | if (of_property_read_u32(np, "nand-bus-width", &val)) |
| 4908 | return 8; |
| 4909 | |
| 4910 | switch (val) { |
| 4911 | case 8: |
| 4912 | case 16: |
| 4913 | return val; |
| 4914 | default: |
| 4915 | return -EIO; |
| 4916 | } |
| 4917 | } |
| 4918 | |
| 4919 | static bool of_get_nand_on_flash_bbt(struct device_node *np) |
| 4920 | { |
| 4921 | return of_property_read_bool(np, "nand-on-flash-bbt"); |
| 4922 | } |
| 4923 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4924 | static int nand_dt_init(struct nand_chip *chip) |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4925 | { |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4926 | struct device_node *dn = nand_get_flash_node(chip); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4927 | int ecc_mode, ecc_algo, ecc_strength, ecc_step; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4928 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4929 | if (!dn) |
| 4930 | return 0; |
| 4931 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4932 | if (of_get_nand_bus_width(dn) == 16) |
| 4933 | chip->options |= NAND_BUSWIDTH_16; |
| 4934 | |
Stefan Agner | f922bd7 | 2018-06-24 23:27:23 +0200 | [diff] [blame] | 4935 | if (of_property_read_bool(dn, "nand-is-boot-medium")) |
| 4936 | chip->options |= NAND_IS_BOOT_MEDIUM; |
| 4937 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4938 | if (of_get_nand_on_flash_bbt(dn)) |
| 4939 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 4940 | |
| 4941 | ecc_mode = of_get_nand_ecc_mode(dn); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4942 | ecc_algo = of_get_nand_ecc_algo(dn); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4943 | ecc_strength = of_get_nand_ecc_strength(dn); |
| 4944 | ecc_step = of_get_nand_ecc_step_size(dn); |
| 4945 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4946 | if (ecc_mode >= 0) |
| 4947 | chip->ecc.mode = ecc_mode; |
| 4948 | |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4949 | if (ecc_algo >= 0) |
| 4950 | chip->ecc.algo = ecc_algo; |
| 4951 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4952 | if (ecc_strength >= 0) |
| 4953 | chip->ecc.strength = ecc_strength; |
| 4954 | |
| 4955 | if (ecc_step > 0) |
| 4956 | chip->ecc.size = ecc_step; |
| 4957 | |
Boris Brezillon | ba78ee0 | 2016-06-08 17:04:22 +0200 | [diff] [blame] | 4958 | if (of_property_read_bool(dn, "nand-ecc-maximize")) |
| 4959 | chip->ecc.options |= NAND_ECC_MAXIMIZE; |
| 4960 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4961 | return 0; |
| 4962 | } |
| 4963 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4964 | /** |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 4965 | * nand_scan_ident - Scan for the NAND device |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 4966 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4967 | * @maxchips: number of chips to scan for |
| 4968 | * @table: alternative NAND ID table |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4969 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4970 | * This is the first phase of the normal nand_scan() function. It reads the |
| 4971 | * flash ID and sets up MTD fields accordingly. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4972 | * |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 4973 | * This helper used to be called directly from controller drivers that needed |
| 4974 | * to tweak some ECC-related parameters before nand_scan_tail(). This separation |
| 4975 | * prevented dynamic allocations during this phase which was unconvenient and |
| 4976 | * 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] | 4977 | */ |
Boris Brezillon | 871a407 | 2018-08-04 22:59:22 +0200 | [diff] [blame] | 4978 | static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips, |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 4979 | struct nand_flash_dev *table) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4980 | { |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 4981 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 871a407 | 2018-08-04 22:59:22 +0200 | [diff] [blame] | 4982 | int nand_maf_id, nand_dev_id; |
| 4983 | unsigned int i; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4984 | int ret; |
| 4985 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 4986 | /* Enforce the right timings for reset/detection */ |
| 4987 | onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0); |
| 4988 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4989 | ret = nand_dt_init(chip); |
| 4990 | if (ret) |
| 4991 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4992 | |
Brian Norris | f7a8e38 | 2016-01-05 10:39:45 -0800 | [diff] [blame] | 4993 | if (!mtd->name && mtd->dev.parent) |
| 4994 | mtd->name = dev_name(mtd->dev.parent); |
| 4995 | |
Boris Brezillon | 3d4af7c | 2018-09-07 00:38:49 +0200 | [diff] [blame] | 4996 | if (chip->exec_op && !chip->select_chip) { |
| 4997 | pr_err("->select_chip() is mandatory when implementing ->exec_op()\n"); |
| 4998 | return -EINVAL; |
Andrey Smirnov | 76fe334 | 2016-07-21 14:59:20 -0700 | [diff] [blame] | 4999 | } |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 5000 | |
Boris Brezillon | 3d4af7c | 2018-09-07 00:38:49 +0200 | [diff] [blame] | 5001 | ret = nand_legacy_check_hooks(chip); |
| 5002 | if (ret) |
| 5003 | return ret; |
| 5004 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5005 | /* Set the default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5006 | nand_set_defaults(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5007 | |
| 5008 | /* Read the flash type */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 5009 | ret = nand_detect(chip, table); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5010 | if (ret) { |
Ben Dooks | b1c6e6d | 2009-11-02 18:12:33 +0000 | [diff] [blame] | 5011 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 5012 | pr_warn("No NAND device found\n"); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5013 | chip->select_chip(chip, -1); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5014 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | } |
| 5016 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5017 | nand_maf_id = chip->id.data[0]; |
| 5018 | nand_dev_id = chip->id.data[1]; |
| 5019 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5020 | chip->select_chip(chip, -1); |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 5021 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5022 | /* Check for a chip array */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5023 | for (i = 1; i < maxchips; i++) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5024 | u8 id[2]; |
| 5025 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 5026 | /* See comment in nand_get_flash_type for reset */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 5027 | nand_reset(chip, i); |
| 5028 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5029 | chip->select_chip(chip, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | /* Send the command for reading device ID */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5031 | nand_readid_op(chip, 0, id, sizeof(id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5032 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5033 | if (nand_maf_id != id[0] || nand_dev_id != id[1]) { |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5034 | chip->select_chip(chip, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5035 | break; |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 5036 | } |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5037 | chip->select_chip(chip, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | } |
| 5039 | if (i > 1) |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 5040 | pr_info("%d chips detected\n", i); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5042 | /* Store the number of chips and calc total size for mtd */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5043 | chip->numchips = i; |
| 5044 | mtd->size = i * chip->chipsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5045 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5046 | return 0; |
| 5047 | } |
| 5048 | |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5049 | static void nand_scan_ident_cleanup(struct nand_chip *chip) |
| 5050 | { |
| 5051 | kfree(chip->parameters.model); |
Miquel Raynal | 3d3fe3c | 2018-07-25 15:31:52 +0200 | [diff] [blame] | 5052 | kfree(chip->parameters.onfi); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5053 | } |
| 5054 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5055 | static int nand_set_ecc_soft_ops(struct mtd_info *mtd) |
| 5056 | { |
| 5057 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 5058 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 5059 | |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5060 | if (WARN_ON(ecc->mode != NAND_ECC_SOFT)) |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5061 | return -EINVAL; |
| 5062 | |
| 5063 | switch (ecc->algo) { |
| 5064 | case NAND_ECC_HAMMING: |
| 5065 | ecc->calculate = nand_calculate_ecc; |
| 5066 | ecc->correct = nand_correct_data; |
| 5067 | ecc->read_page = nand_read_page_swecc; |
| 5068 | ecc->read_subpage = nand_read_subpage; |
| 5069 | ecc->write_page = nand_write_page_swecc; |
| 5070 | ecc->read_page_raw = nand_read_page_raw; |
| 5071 | ecc->write_page_raw = nand_write_page_raw; |
| 5072 | ecc->read_oob = nand_read_oob_std; |
| 5073 | ecc->write_oob = nand_write_oob_std; |
| 5074 | if (!ecc->size) |
| 5075 | ecc->size = 256; |
| 5076 | ecc->bytes = 3; |
| 5077 | ecc->strength = 1; |
Boris Brezillon | 309600c | 2018-09-04 16:23:28 +0200 | [diff] [blame] | 5078 | |
| 5079 | if (IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC)) |
| 5080 | ecc->options |= NAND_ECC_SOFT_HAMMING_SM_ORDER; |
| 5081 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5082 | return 0; |
| 5083 | case NAND_ECC_BCH: |
| 5084 | if (!mtd_nand_has_bch()) { |
| 5085 | WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n"); |
| 5086 | return -EINVAL; |
| 5087 | } |
| 5088 | ecc->calculate = nand_bch_calculate_ecc; |
| 5089 | ecc->correct = nand_bch_correct_data; |
| 5090 | ecc->read_page = nand_read_page_swecc; |
| 5091 | ecc->read_subpage = nand_read_subpage; |
| 5092 | ecc->write_page = nand_write_page_swecc; |
| 5093 | ecc->read_page_raw = nand_read_page_raw; |
| 5094 | ecc->write_page_raw = nand_write_page_raw; |
| 5095 | ecc->read_oob = nand_read_oob_std; |
| 5096 | ecc->write_oob = nand_write_oob_std; |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 5097 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5098 | /* |
| 5099 | * Board driver should supply ecc.size and ecc.strength |
| 5100 | * values to select how many bits are correctable. |
| 5101 | * Otherwise, default to 4 bits for large page devices. |
| 5102 | */ |
| 5103 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 5104 | ecc->size = 512; |
| 5105 | ecc->strength = 4; |
| 5106 | } |
| 5107 | |
| 5108 | /* |
| 5109 | * if no ecc placement scheme was provided pickup the default |
| 5110 | * large page one. |
| 5111 | */ |
| 5112 | if (!mtd->ooblayout) { |
| 5113 | /* handle large page devices only */ |
| 5114 | if (mtd->oobsize < 64) { |
| 5115 | WARN(1, "OOB layout is required when using software BCH on small pages\n"); |
| 5116 | return -EINVAL; |
| 5117 | } |
| 5118 | |
| 5119 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 5120 | |
| 5121 | } |
| 5122 | |
| 5123 | /* |
| 5124 | * We can only maximize ECC config when the default layout is |
| 5125 | * used, otherwise we don't know how many bytes can really be |
| 5126 | * used. |
| 5127 | */ |
| 5128 | if (mtd->ooblayout == &nand_ooblayout_lp_ops && |
| 5129 | ecc->options & NAND_ECC_MAXIMIZE) { |
| 5130 | int steps, bytes; |
| 5131 | |
| 5132 | /* Always prefer 1k blocks over 512bytes ones */ |
| 5133 | ecc->size = 1024; |
| 5134 | steps = mtd->writesize / ecc->size; |
| 5135 | |
| 5136 | /* Reserve 2 bytes for the BBM */ |
| 5137 | bytes = (mtd->oobsize - 2) / steps; |
| 5138 | ecc->strength = bytes * 8 / fls(8 * ecc->size); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5139 | } |
| 5140 | |
| 5141 | /* See nand_bch_init() for details. */ |
| 5142 | ecc->bytes = 0; |
| 5143 | ecc->priv = nand_bch_init(mtd); |
| 5144 | if (!ecc->priv) { |
| 5145 | WARN(1, "BCH ECC initialization failed!\n"); |
| 5146 | return -EINVAL; |
| 5147 | } |
| 5148 | return 0; |
| 5149 | default: |
| 5150 | WARN(1, "Unsupported ECC algorithm!\n"); |
| 5151 | return -EINVAL; |
| 5152 | } |
| 5153 | } |
| 5154 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5155 | /** |
| 5156 | * nand_check_ecc_caps - check the sanity of preset ECC settings |
| 5157 | * @chip: nand chip info structure |
| 5158 | * @caps: ECC caps info structure |
| 5159 | * @oobavail: OOB size that the ECC engine can use |
| 5160 | * |
| 5161 | * When ECC step size and strength are already set, check if they are supported |
| 5162 | * by the controller and the calculated ECC bytes fit within the chip's OOB. |
| 5163 | * On success, the calculated ECC bytes is set. |
| 5164 | */ |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5165 | static int |
| 5166 | nand_check_ecc_caps(struct nand_chip *chip, |
| 5167 | const struct nand_ecc_caps *caps, int oobavail) |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5168 | { |
| 5169 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5170 | const struct nand_ecc_step_info *stepinfo; |
| 5171 | int preset_step = chip->ecc.size; |
| 5172 | int preset_strength = chip->ecc.strength; |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5173 | int ecc_bytes, nsteps = mtd->writesize / preset_step; |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5174 | int i, j; |
| 5175 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5176 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5177 | stepinfo = &caps->stepinfos[i]; |
| 5178 | |
| 5179 | if (stepinfo->stepsize != preset_step) |
| 5180 | continue; |
| 5181 | |
| 5182 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5183 | if (stepinfo->strengths[j] != preset_strength) |
| 5184 | continue; |
| 5185 | |
| 5186 | ecc_bytes = caps->calc_ecc_bytes(preset_step, |
| 5187 | preset_strength); |
| 5188 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5189 | return ecc_bytes; |
| 5190 | |
| 5191 | if (ecc_bytes * nsteps > oobavail) { |
| 5192 | pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB", |
| 5193 | preset_step, preset_strength); |
| 5194 | return -ENOSPC; |
| 5195 | } |
| 5196 | |
| 5197 | chip->ecc.bytes = ecc_bytes; |
| 5198 | |
| 5199 | return 0; |
| 5200 | } |
| 5201 | } |
| 5202 | |
| 5203 | pr_err("ECC (step, strength) = (%d, %d) not supported on this controller", |
| 5204 | preset_step, preset_strength); |
| 5205 | |
| 5206 | return -ENOTSUPP; |
| 5207 | } |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5208 | |
| 5209 | /** |
| 5210 | * nand_match_ecc_req - meet the chip's requirement with least ECC bytes |
| 5211 | * @chip: nand chip info structure |
| 5212 | * @caps: ECC engine caps info structure |
| 5213 | * @oobavail: OOB size that the ECC engine can use |
| 5214 | * |
| 5215 | * If a chip's ECC requirement is provided, try to meet it with the least |
| 5216 | * number of ECC bytes (i.e. with the largest number of OOB-free bytes). |
| 5217 | * On success, the chosen ECC settings are set. |
| 5218 | */ |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5219 | static int |
| 5220 | nand_match_ecc_req(struct nand_chip *chip, |
| 5221 | const struct nand_ecc_caps *caps, int oobavail) |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5222 | { |
| 5223 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5224 | const struct nand_ecc_step_info *stepinfo; |
| 5225 | int req_step = chip->ecc_step_ds; |
| 5226 | int req_strength = chip->ecc_strength_ds; |
| 5227 | int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total; |
| 5228 | int best_step, best_strength, best_ecc_bytes; |
| 5229 | int best_ecc_bytes_total = INT_MAX; |
| 5230 | int i, j; |
| 5231 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5232 | /* No information provided by the NAND chip */ |
| 5233 | if (!req_step || !req_strength) |
| 5234 | return -ENOTSUPP; |
| 5235 | |
| 5236 | /* number of correctable bits the chip requires in a page */ |
| 5237 | req_corr = mtd->writesize / req_step * req_strength; |
| 5238 | |
| 5239 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5240 | stepinfo = &caps->stepinfos[i]; |
| 5241 | step_size = stepinfo->stepsize; |
| 5242 | |
| 5243 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5244 | strength = stepinfo->strengths[j]; |
| 5245 | |
| 5246 | /* |
| 5247 | * If both step size and strength are smaller than the |
| 5248 | * chip's requirement, it is not easy to compare the |
| 5249 | * resulted reliability. |
| 5250 | */ |
| 5251 | if (step_size < req_step && strength < req_strength) |
| 5252 | continue; |
| 5253 | |
| 5254 | if (mtd->writesize % step_size) |
| 5255 | continue; |
| 5256 | |
| 5257 | nsteps = mtd->writesize / step_size; |
| 5258 | |
| 5259 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 5260 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5261 | continue; |
| 5262 | ecc_bytes_total = ecc_bytes * nsteps; |
| 5263 | |
| 5264 | if (ecc_bytes_total > oobavail || |
| 5265 | strength * nsteps < req_corr) |
| 5266 | continue; |
| 5267 | |
| 5268 | /* |
| 5269 | * We assume the best is to meet the chip's requrement |
| 5270 | * with the least number of ECC bytes. |
| 5271 | */ |
| 5272 | if (ecc_bytes_total < best_ecc_bytes_total) { |
| 5273 | best_ecc_bytes_total = ecc_bytes_total; |
| 5274 | best_step = step_size; |
| 5275 | best_strength = strength; |
| 5276 | best_ecc_bytes = ecc_bytes; |
| 5277 | } |
| 5278 | } |
| 5279 | } |
| 5280 | |
| 5281 | if (best_ecc_bytes_total == INT_MAX) |
| 5282 | return -ENOTSUPP; |
| 5283 | |
| 5284 | chip->ecc.size = best_step; |
| 5285 | chip->ecc.strength = best_strength; |
| 5286 | chip->ecc.bytes = best_ecc_bytes; |
| 5287 | |
| 5288 | return 0; |
| 5289 | } |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5290 | |
| 5291 | /** |
| 5292 | * nand_maximize_ecc - choose the max ECC strength available |
| 5293 | * @chip: nand chip info structure |
| 5294 | * @caps: ECC engine caps info structure |
| 5295 | * @oobavail: OOB size that the ECC engine can use |
| 5296 | * |
| 5297 | * Choose the max ECC strength that is supported on the controller, and can fit |
| 5298 | * within the chip's OOB. On success, the chosen ECC settings are set. |
| 5299 | */ |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5300 | static int |
| 5301 | nand_maximize_ecc(struct nand_chip *chip, |
| 5302 | const struct nand_ecc_caps *caps, int oobavail) |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5303 | { |
| 5304 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5305 | const struct nand_ecc_step_info *stepinfo; |
| 5306 | int step_size, strength, nsteps, ecc_bytes, corr; |
| 5307 | int best_corr = 0; |
| 5308 | int best_step = 0; |
| 5309 | int best_strength, best_ecc_bytes; |
| 5310 | int i, j; |
| 5311 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5312 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5313 | stepinfo = &caps->stepinfos[i]; |
| 5314 | step_size = stepinfo->stepsize; |
| 5315 | |
| 5316 | /* If chip->ecc.size is already set, respect it */ |
| 5317 | if (chip->ecc.size && step_size != chip->ecc.size) |
| 5318 | continue; |
| 5319 | |
| 5320 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5321 | strength = stepinfo->strengths[j]; |
| 5322 | |
| 5323 | if (mtd->writesize % step_size) |
| 5324 | continue; |
| 5325 | |
| 5326 | nsteps = mtd->writesize / step_size; |
| 5327 | |
| 5328 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 5329 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5330 | continue; |
| 5331 | |
| 5332 | if (ecc_bytes * nsteps > oobavail) |
| 5333 | continue; |
| 5334 | |
| 5335 | corr = strength * nsteps; |
| 5336 | |
| 5337 | /* |
| 5338 | * If the number of correctable bits is the same, |
| 5339 | * bigger step_size has more reliability. |
| 5340 | */ |
| 5341 | if (corr > best_corr || |
| 5342 | (corr == best_corr && step_size > best_step)) { |
| 5343 | best_corr = corr; |
| 5344 | best_step = step_size; |
| 5345 | best_strength = strength; |
| 5346 | best_ecc_bytes = ecc_bytes; |
| 5347 | } |
| 5348 | } |
| 5349 | } |
| 5350 | |
| 5351 | if (!best_corr) |
| 5352 | return -ENOTSUPP; |
| 5353 | |
| 5354 | chip->ecc.size = best_step; |
| 5355 | chip->ecc.strength = best_strength; |
| 5356 | chip->ecc.bytes = best_ecc_bytes; |
| 5357 | |
| 5358 | return 0; |
| 5359 | } |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5360 | |
Abhishek Sahu | 181ace9 | 2018-06-20 12:57:28 +0530 | [diff] [blame] | 5361 | /** |
| 5362 | * nand_ecc_choose_conf - Set the ECC strength and ECC step size |
| 5363 | * @chip: nand chip info structure |
| 5364 | * @caps: ECC engine caps info structure |
| 5365 | * @oobavail: OOB size that the ECC engine can use |
| 5366 | * |
| 5367 | * Choose the ECC configuration according to following logic |
| 5368 | * |
| 5369 | * 1. If both ECC step size and ECC strength are already set (usually by DT) |
| 5370 | * then check if it is supported by this controller. |
| 5371 | * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength. |
| 5372 | * 3. Otherwise, try to match the ECC step size and ECC strength closest |
| 5373 | * to the chip's requirement. If available OOB size can't fit the chip |
| 5374 | * requirement then fallback to the maximum ECC step size and ECC strength. |
| 5375 | * |
| 5376 | * On success, the chosen ECC settings are set. |
| 5377 | */ |
| 5378 | int nand_ecc_choose_conf(struct nand_chip *chip, |
| 5379 | const struct nand_ecc_caps *caps, int oobavail) |
| 5380 | { |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5381 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5382 | |
| 5383 | if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize)) |
| 5384 | return -EINVAL; |
| 5385 | |
Abhishek Sahu | 181ace9 | 2018-06-20 12:57:28 +0530 | [diff] [blame] | 5386 | if (chip->ecc.size && chip->ecc.strength) |
| 5387 | return nand_check_ecc_caps(chip, caps, oobavail); |
| 5388 | |
| 5389 | if (chip->ecc.options & NAND_ECC_MAXIMIZE) |
| 5390 | return nand_maximize_ecc(chip, caps, oobavail); |
| 5391 | |
| 5392 | if (!nand_match_ecc_req(chip, caps, oobavail)) |
| 5393 | return 0; |
| 5394 | |
| 5395 | return nand_maximize_ecc(chip, caps, oobavail); |
| 5396 | } |
| 5397 | EXPORT_SYMBOL_GPL(nand_ecc_choose_conf); |
| 5398 | |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5399 | /* |
| 5400 | * Check if the chip configuration meet the datasheet requirements. |
| 5401 | |
| 5402 | * If our configuration corrects A bits per B bytes and the minimum |
| 5403 | * required correction level is X bits per Y bytes, then we must ensure |
| 5404 | * both of the following are true: |
| 5405 | * |
| 5406 | * (1) A / B >= X / Y |
| 5407 | * (2) A >= X |
| 5408 | * |
| 5409 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 5410 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 5411 | * in the same sector. |
| 5412 | */ |
| 5413 | static bool nand_ecc_strength_good(struct mtd_info *mtd) |
| 5414 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 5415 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5416 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 5417 | int corr, ds_corr; |
| 5418 | |
| 5419 | if (ecc->size == 0 || chip->ecc_step_ds == 0) |
| 5420 | /* Not enough information */ |
| 5421 | return true; |
| 5422 | |
| 5423 | /* |
| 5424 | * We get the number of corrected bits per page to compare |
| 5425 | * the correction density. |
| 5426 | */ |
| 5427 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
| 5428 | ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; |
| 5429 | |
| 5430 | return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; |
| 5431 | } |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5432 | |
| 5433 | /** |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 5434 | * nand_scan_tail - Scan for the NAND device |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5435 | * @chip: NAND chip object |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5436 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5437 | * This is the second phase of the normal nand_scan() function. It fills out |
| 5438 | * all the uninitialized function pointers with the defaults and scans for a |
| 5439 | * bad block table if appropriate. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5440 | */ |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5441 | static int nand_scan_tail(struct nand_chip *chip) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5442 | { |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5443 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5444 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5445 | int ret, i; |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5446 | |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 5447 | /* 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] | 5448 | if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5449 | !(chip->bbt_options & NAND_BBT_USE_FLASH))) { |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5450 | return -EINVAL; |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5451 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 5452 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5453 | chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL); |
Boris Brezillon | aeb93af | 2017-12-05 12:09:29 +0100 | [diff] [blame] | 5454 | if (!chip->data_buf) |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5455 | return -ENOMEM; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 5456 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5457 | /* |
| 5458 | * FIXME: some NAND manufacturer drivers expect the first die to be |
| 5459 | * selected when manufacturer->init() is called. They should be fixed |
| 5460 | * to explictly select the relevant die when interacting with the NAND |
| 5461 | * chip. |
| 5462 | */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5463 | chip->select_chip(chip, 0); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5464 | ret = nand_manufacturer_init(chip); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5465 | chip->select_chip(chip, -1); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5466 | if (ret) |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5467 | goto err_free_buf; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5468 | |
David Woodhouse | 7dcdcbef | 2006-10-21 17:09:53 +0100 | [diff] [blame] | 5469 | /* Set the internal oob buffer location, just after the page data */ |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5470 | chip->oob_poi = chip->data_buf + mtd->writesize; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5471 | |
| 5472 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5473 | * If no default placement scheme is given, select an appropriate one. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5474 | */ |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5475 | if (!mtd->ooblayout && |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5476 | !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5477 | switch (mtd->oobsize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | case 8: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | case 16: |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 5480 | mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | break; |
| 5482 | case 64: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5483 | case 128: |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 5484 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops); |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5485 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5486 | default: |
Miquel Raynal | 882fd15 | 2017-08-26 17:19:15 +0200 | [diff] [blame] | 5487 | /* |
| 5488 | * Expose the whole OOB area to users if ECC_NONE |
| 5489 | * is passed. We could do that for all kind of |
| 5490 | * ->oobsize, but we must keep the old large/small |
| 5491 | * page with ECC layout when ->oobsize <= 128 for |
| 5492 | * compatibility reasons. |
| 5493 | */ |
| 5494 | if (ecc->mode == NAND_ECC_NONE) { |
| 5495 | mtd_set_ooblayout(mtd, |
| 5496 | &nand_ooblayout_lp_ops); |
| 5497 | break; |
| 5498 | } |
| 5499 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5500 | WARN(1, "No oob scheme defined for oobsize %d\n", |
| 5501 | mtd->oobsize); |
| 5502 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5503 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5504 | } |
| 5505 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5506 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5507 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5508 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5509 | * selected and we have 256 byte pagesize fallback to software ECC |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5510 | */ |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 5511 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5512 | switch (ecc->mode) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 5513 | case NAND_ECC_HW_OOB_FIRST: |
| 5514 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5515 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5516 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 5517 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5518 | goto err_nand_manuf_cleanup; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 5519 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5520 | if (!ecc->read_page) |
| 5521 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 5522 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5523 | case NAND_ECC_HW: |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5524 | /* Use standard hwecc read page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5525 | if (!ecc->read_page) |
| 5526 | ecc->read_page = nand_read_page_hwecc; |
| 5527 | if (!ecc->write_page) |
| 5528 | ecc->write_page = nand_write_page_hwecc; |
| 5529 | if (!ecc->read_page_raw) |
| 5530 | ecc->read_page_raw = nand_read_page_raw; |
| 5531 | if (!ecc->write_page_raw) |
| 5532 | ecc->write_page_raw = nand_write_page_raw; |
| 5533 | if (!ecc->read_oob) |
| 5534 | ecc->read_oob = nand_read_oob_std; |
| 5535 | if (!ecc->write_oob) |
| 5536 | ecc->write_oob = nand_write_oob_std; |
| 5537 | if (!ecc->read_subpage) |
| 5538 | ecc->read_subpage = nand_read_subpage; |
Helmut Schaa | 44991b3 | 2014-04-09 11:13:24 +0200 | [diff] [blame] | 5539 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5540 | ecc->write_subpage = nand_write_subpage_hwecc; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 5541 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5542 | case NAND_ECC_HW_SYNDROME: |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5543 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 5544 | (!ecc->read_page || |
| 5545 | ecc->read_page == nand_read_page_hwecc || |
| 5546 | !ecc->write_page || |
| 5547 | ecc->write_page == nand_write_page_hwecc)) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5548 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 5549 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5550 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5551 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5552 | /* Use standard syndrome read/write page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5553 | if (!ecc->read_page) |
| 5554 | ecc->read_page = nand_read_page_syndrome; |
| 5555 | if (!ecc->write_page) |
| 5556 | ecc->write_page = nand_write_page_syndrome; |
| 5557 | if (!ecc->read_page_raw) |
| 5558 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 5559 | if (!ecc->write_page_raw) |
| 5560 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 5561 | if (!ecc->read_oob) |
| 5562 | ecc->read_oob = nand_read_oob_syndrome; |
| 5563 | if (!ecc->write_oob) |
| 5564 | ecc->write_oob = nand_write_oob_syndrome; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 5565 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5566 | if (mtd->writesize >= ecc->size) { |
| 5567 | if (!ecc->strength) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5568 | WARN(1, "Driver must set ecc.strength when using hardware ECC\n"); |
| 5569 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5570 | goto err_nand_manuf_cleanup; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 5571 | } |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5572 | break; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 5573 | } |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 5574 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 5575 | ecc->size, mtd->writesize); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5576 | ecc->mode = NAND_ECC_SOFT; |
Rafał Miłecki | e9d4fae | 2016-04-17 22:53:02 +0200 | [diff] [blame] | 5577 | ecc->algo = NAND_ECC_HAMMING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5578 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5579 | case NAND_ECC_SOFT: |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5580 | ret = nand_set_ecc_soft_ops(mtd); |
| 5581 | if (ret) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5582 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5583 | goto err_nand_manuf_cleanup; |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 5584 | } |
| 5585 | break; |
| 5586 | |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 5587 | case NAND_ECC_ON_DIE: |
| 5588 | if (!ecc->read_page || !ecc->write_page) { |
| 5589 | WARN(1, "No ECC functions supplied; on-die ECC not possible\n"); |
| 5590 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5591 | goto err_nand_manuf_cleanup; |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 5592 | } |
| 5593 | if (!ecc->read_oob) |
| 5594 | ecc->read_oob = nand_read_oob_std; |
| 5595 | if (!ecc->write_oob) |
| 5596 | ecc->write_oob = nand_write_oob_std; |
| 5597 | break; |
| 5598 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5599 | case NAND_ECC_NONE: |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 5600 | 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] | 5601 | ecc->read_page = nand_read_page_raw; |
| 5602 | ecc->write_page = nand_write_page_raw; |
| 5603 | ecc->read_oob = nand_read_oob_std; |
| 5604 | ecc->read_page_raw = nand_read_page_raw; |
| 5605 | ecc->write_page_raw = nand_write_page_raw; |
| 5606 | ecc->write_oob = nand_write_oob_std; |
| 5607 | ecc->size = mtd->writesize; |
| 5608 | ecc->bytes = 0; |
| 5609 | ecc->strength = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | break; |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 5611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5612 | default: |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5613 | WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode); |
| 5614 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5615 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5616 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5617 | |
Boris Brezillon | aeb93af | 2017-12-05 12:09:29 +0100 | [diff] [blame] | 5618 | if (ecc->correct || ecc->calculate) { |
| 5619 | ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 5620 | ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 5621 | if (!ecc->calc_buf || !ecc->code_buf) { |
| 5622 | ret = -ENOMEM; |
| 5623 | goto err_nand_manuf_cleanup; |
| 5624 | } |
| 5625 | } |
| 5626 | |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 5627 | /* For many systems, the standard OOB write also works for raw */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5628 | if (!ecc->read_oob_raw) |
| 5629 | ecc->read_oob_raw = ecc->read_oob; |
| 5630 | if (!ecc->write_oob_raw) |
| 5631 | ecc->write_oob_raw = ecc->write_oob; |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 5632 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5633 | /* propagate ecc info to mtd_info */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5634 | mtd->ecc_strength = ecc->strength; |
| 5635 | mtd->ecc_step_size = ecc->size; |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5636 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 5637 | /* |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5638 | * 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] | 5639 | * mode. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5640 | */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5641 | ecc->steps = mtd->writesize / ecc->size; |
| 5642 | if (ecc->steps * ecc->size != mtd->writesize) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5643 | WARN(1, "Invalid ECC parameters\n"); |
| 5644 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5645 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5647 | ecc->total = ecc->steps * ecc->bytes; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 5648 | if (ecc->total > mtd->oobsize) { |
| 5649 | WARN(1, "Total number of ECC bytes exceeded oobsize\n"); |
| 5650 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5651 | goto err_nand_manuf_cleanup; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 5652 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5653 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5654 | /* |
| 5655 | * The number of bytes available for a client to place data into |
| 5656 | * the out of band area. |
| 5657 | */ |
| 5658 | ret = mtd_ooblayout_count_freebytes(mtd); |
| 5659 | if (ret < 0) |
| 5660 | ret = 0; |
| 5661 | |
| 5662 | mtd->oobavail = ret; |
| 5663 | |
| 5664 | /* ECC sanity check: warn if it's too weak */ |
| 5665 | if (!nand_ecc_strength_good(mtd)) |
| 5666 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 5667 | mtd->name); |
| 5668 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5669 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 5670 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5671 | switch (ecc->steps) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 5672 | case 2: |
| 5673 | mtd->subpage_sft = 1; |
| 5674 | break; |
| 5675 | case 4: |
| 5676 | case 8: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5677 | case 16: |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 5678 | mtd->subpage_sft = 2; |
| 5679 | break; |
| 5680 | } |
| 5681 | } |
| 5682 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 5683 | |
Thomas Gleixner | 04bbd0e | 2006-05-25 09:45:29 +0200 | [diff] [blame] | 5684 | /* Initialize state */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5685 | chip->state = FL_READY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5687 | /* Invalidate the pagebuffer reference */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5688 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5689 | |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 5690 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 5691 | switch (ecc->mode) { |
| 5692 | case NAND_ECC_SOFT: |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 5693 | if (chip->page_shift > 9) |
| 5694 | chip->options |= NAND_SUBPAGE_READ; |
| 5695 | break; |
| 5696 | |
| 5697 | default: |
| 5698 | break; |
| 5699 | } |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 5700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | /* Fill in remaining MTD driver data */ |
Huang Shijie | 963d1c2 | 2013-09-25 14:58:21 +0800 | [diff] [blame] | 5702 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 5703 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 5704 | MTD_CAP_NANDFLASH; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5705 | mtd->_erase = nand_erase; |
| 5706 | mtd->_point = NULL; |
| 5707 | mtd->_unpoint = NULL; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5708 | mtd->_panic_write = panic_nand_write; |
| 5709 | mtd->_read_oob = nand_read_oob; |
| 5710 | mtd->_write_oob = nand_write_oob; |
| 5711 | mtd->_sync = nand_sync; |
| 5712 | mtd->_lock = NULL; |
| 5713 | mtd->_unlock = NULL; |
| 5714 | mtd->_suspend = nand_suspend; |
| 5715 | mtd->_resume = nand_resume; |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 5716 | mtd->_reboot = nand_shutdown; |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 5717 | mtd->_block_isreserved = nand_block_isreserved; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5718 | mtd->_block_isbad = nand_block_isbad; |
| 5719 | mtd->_block_markbad = nand_block_markbad; |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 5720 | mtd->_max_bad_blocks = nand_max_bad_blocks; |
Anatolij Gustschin | cbcab65 | 2010-12-16 23:42:16 +0100 | [diff] [blame] | 5721 | mtd->writebufsize = mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5722 | |
Shmulik Ladkani | ea3b2ea | 2012-06-08 18:29:06 +0300 | [diff] [blame] | 5723 | /* |
| 5724 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 5725 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 5726 | * properly set. |
| 5727 | */ |
| 5728 | if (!mtd->bitflip_threshold) |
Brian Norris | 240181f | 2015-01-12 12:51:29 -0800 | [diff] [blame] | 5729 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5730 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5731 | /* Initialize the ->data_interface field. */ |
| 5732 | ret = nand_init_data_interface(chip); |
| 5733 | if (ret) |
| 5734 | goto err_nand_manuf_cleanup; |
| 5735 | |
| 5736 | /* Enter fastest possible mode on all dies. */ |
| 5737 | for (i = 0; i < chip->numchips; i++) { |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5738 | ret = nand_setup_data_interface(chip, i); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5739 | if (ret) |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 5740 | goto err_nand_manuf_cleanup; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5741 | } |
| 5742 | |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 5743 | /* Check, if we should skip the bad block table scan */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5744 | if (chip->options & NAND_SKIP_BBTSCAN) |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 5745 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | |
| 5747 | /* Build bad block table */ |
Boris Brezillon | e80eba7 | 2018-07-05 12:27:31 +0200 | [diff] [blame] | 5748 | ret = nand_create_bbt(chip); |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 5749 | if (ret) |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 5750 | goto err_nand_manuf_cleanup; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5751 | |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 5752 | return 0; |
| 5753 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5754 | |
| 5755 | err_nand_manuf_cleanup: |
| 5756 | nand_manufacturer_cleanup(chip); |
| 5757 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5758 | err_free_buf: |
| 5759 | kfree(chip->data_buf); |
| 5760 | kfree(ecc->code_buf); |
| 5761 | kfree(ecc->calc_buf); |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5762 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5763 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5764 | } |
| 5765 | |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5766 | static int nand_attach(struct nand_chip *chip) |
| 5767 | { |
| 5768 | if (chip->controller->ops && chip->controller->ops->attach_chip) |
| 5769 | return chip->controller->ops->attach_chip(chip); |
| 5770 | |
| 5771 | return 0; |
| 5772 | } |
| 5773 | |
| 5774 | static void nand_detach(struct nand_chip *chip) |
| 5775 | { |
| 5776 | if (chip->controller->ops && chip->controller->ops->detach_chip) |
| 5777 | chip->controller->ops->detach_chip(chip); |
| 5778 | } |
| 5779 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5780 | /** |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5781 | * nand_scan_with_ids - [NAND Interface] Scan for the NAND device |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5782 | * @chip: NAND chip object |
Boris Brezillon | 800342d | 2018-08-04 22:59:23 +0200 | [diff] [blame] | 5783 | * @maxchips: number of chips to scan for. |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5784 | * @ids: optional flash IDs table |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5785 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5786 | * This fills out all the uninitialized function pointers with the defaults. |
| 5787 | * 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] | 5788 | * appropriate values. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5789 | */ |
Boris Brezillon | 871a407 | 2018-08-04 22:59:22 +0200 | [diff] [blame] | 5790 | int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips, |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5791 | struct nand_flash_dev *ids) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5792 | { |
| 5793 | int ret; |
| 5794 | |
Boris Brezillon | 800342d | 2018-08-04 22:59:23 +0200 | [diff] [blame] | 5795 | if (!maxchips) |
| 5796 | return -EINVAL; |
| 5797 | |
| 5798 | ret = nand_scan_ident(chip, maxchips, ids); |
| 5799 | if (ret) |
| 5800 | return ret; |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5801 | |
| 5802 | ret = nand_attach(chip); |
| 5803 | if (ret) |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5804 | goto cleanup_ident; |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5805 | |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5806 | ret = nand_scan_tail(chip); |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5807 | if (ret) |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5808 | goto detach_chip; |
| 5809 | |
| 5810 | return 0; |
| 5811 | |
| 5812 | detach_chip: |
| 5813 | nand_detach(chip); |
| 5814 | cleanup_ident: |
| 5815 | nand_scan_ident_cleanup(chip); |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5816 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5817 | return ret; |
| 5818 | } |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5819 | EXPORT_SYMBOL(nand_scan_with_ids); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5821 | /** |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5822 | * nand_cleanup - [NAND Interface] Free resources held by the NAND device |
| 5823 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5824 | */ |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5825 | void nand_cleanup(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5826 | { |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5827 | if (chip->ecc.mode == NAND_ECC_SOFT && |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5828 | chip->ecc.algo == NAND_ECC_BCH) |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 5829 | nand_bch_free((struct nand_bch_control *)chip->ecc.priv); |
| 5830 | |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 5831 | /* Free bad block table memory */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5832 | kfree(chip->bbt); |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5833 | kfree(chip->data_buf); |
| 5834 | kfree(chip->ecc.code_buf); |
| 5835 | kfree(chip->ecc.calc_buf); |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 5836 | |
| 5837 | /* Free bad block descriptor memory */ |
| 5838 | if (chip->badblock_pattern && chip->badblock_pattern->options |
| 5839 | & NAND_BBT_DYNAMICSTRUCT) |
| 5840 | kfree(chip->badblock_pattern); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5841 | |
| 5842 | /* Free manufacturer priv data. */ |
| 5843 | nand_manufacturer_cleanup(chip); |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5844 | |
| 5845 | /* Free controller specific allocations after chip identification */ |
| 5846 | nand_detach(chip); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5847 | |
| 5848 | /* Free identification phase allocations */ |
| 5849 | nand_scan_ident_cleanup(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5850 | } |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5851 | |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5852 | EXPORT_SYMBOL_GPL(nand_cleanup); |
| 5853 | |
| 5854 | /** |
| 5855 | * nand_release - [NAND Interface] Unregister the MTD device and free resources |
| 5856 | * held by the NAND device |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 5857 | * @chip: NAND chip object |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5858 | */ |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 5859 | void nand_release(struct nand_chip *chip) |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5860 | { |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 5861 | mtd_device_unregister(nand_to_mtd(chip)); |
| 5862 | nand_cleanup(chip); |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5863 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5864 | EXPORT_SYMBOL_GPL(nand_release); |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 5865 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5866 | MODULE_LICENSE("GPL"); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5867 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 5868 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5869 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |